Example #1
0
static int ldap_exec(struct cw_channel *chan, int argc, char **argv)
{
	char result[2048];
	struct localuser *u;
	char *varname, *config, *keys = NULL, *key = NULL, *tail = NULL;
	char *result_conv;
	struct cw_config *cfg;

	int port = LDAP_PORT, version = LDAP_VERSION2, timeout = 10;
	char *temp, *host, *user, *pass, *base, *scope, *filter, *_filter, *attribute,
		 *convert_from = NULL, *convert_to = NULL;

	if (argc != 1) {
		cw_log(LOG_ERROR, "Syntax: %s\n", g_syntax);
		pbx_builtin_setvar_helper(chan, "LDAPSTATUS", "FAILURE");
		return 0;
	}

	LOCAL_USER_ADD(u);

	if (strchr(argv[0], '=')) {
		varname = strsep (&argv[0], "=");
		if (strchr(argv[0], '/')) {
			config = strsep(&argv[0], "/");
			keys = strsep(&argv[0], "\0");
			if (option_verbose > 2)
				cw_verbose(VERBOSE_PREFIX_3 "LDAPget: varname=%s, config-section=%s, keys=%s\n", varname, config, keys);
		} else {
			config = strsep(&argv[0], "\0");
			if (option_verbose > 2)
				cw_verbose(VERBOSE_PREFIX_3 "LDAPget: varname=%s, config-section=%s\n", varname, config);
		}
		if (!varname || !config) {
			cw_log(LOG_WARNING, "Ignoring; Syntax error in argument\n");
			pbx_builtin_setvar_helper(chan, "LDAPSTATUS", "FAILURE");
			return 0;
		}
	} else {
		cw_log(LOG_WARNING, "Ignoring, no parameters\n");
		pbx_builtin_setvar_helper(chan, "LDAPSTATUS", "FAILURE");
		return 0;
	}

	cfg = cw_config_load(LDAP_CONFIG);

	if (!cfg) {
		cw_log(LOG_WARNING, "No such configuration file %s\n", LDAP_CONFIG);
		return -1;
	}
	if (!(host = cw_variable_retrieve(cfg, config, "host"))) {
		host = "localhost";
	}
	if ((temp = cw_variable_retrieve(cfg, config, "port"))) {
		port = atoi(temp);
	}
	if ((temp = cw_variable_retrieve(cfg, config, "timeout"))) {
		timeout = atoi(temp);
	}
	if ((temp = cw_variable_retrieve(cfg, config, "version"))) {
		version = atoi(temp);
	}
	user = cw_variable_retrieve(cfg, config, "user");
	pass = cw_variable_retrieve(cfg, config, "pass");
	base = cw_variable_retrieve(cfg, config, "base");
	if (!base) base = "";
	base = replace_cw_vars(chan, base);
	if (!(scope = cw_variable_retrieve(cfg, config, "scope"))) {
		scope = "sub";
	}
	if (!(_filter = cw_variable_retrieve(cfg, config, "filter"))) {
		_filter = "(&(objectClass=person)(telephoneNumber=${CALLERIDNUM}))";
	}
	if (!(attribute = cw_variable_retrieve(cfg, config, "attribute"))) {
		attribute = "cn";
	}
	if ((temp = cw_variable_retrieve(cfg, config, "convert"))) {
		if (strchr(temp, ',')) {
			convert_from = strtrim(strsep(&temp, ","));
			convert_to = strtrim(strsep(&temp, "\0"));
		} else {
			cw_log(LOG_WARNING, "syntax error: convert = <source-charset>,<destination charset>\n");
		}
	}

	if (option_verbose > 3)
		cw_verbose (VERBOSE_PREFIX_4 "LDAPget: ldap://%s/%s?%s?%s?%s\n", host, base, attribute, scope, _filter);

	filter = replace_cw_vars(chan, _filter);
	if (option_verbose > 3)
		cw_verbose (VERBOSE_PREFIX_4 "LDAPget: %s\n", filter);

	if (keys && strstr(filter, "%s") != NULL) {
		filter = (char *)realloc(filter, (strlen(filter)+strlen(keys)+1)*sizeof(char));
		while((key = strsep(&keys, "|")) != NULL) {
			if ((tail = strstr(filter, "%s")) != NULL) {
				memmove(tail+strlen(key), tail+2, strlen(tail+2)+1);
				memcpy(tail, key, strlen(key));
			}
		}
	}

	if (option_verbose > 2)
		cw_verbose (VERBOSE_PREFIX_3 "LDAPget: ldap://%s/%s?%s?%s?%s\n", host, base, attribute, scope, filter);

	if (ldap_lookup(host, port, version, timeout, user, pass, base, scope, filter, attribute, result)) {

		if (convert_from) {
			if (option_verbose > 2)
				cw_verbose(VERBOSE_PREFIX_3 "LDAPget: convert: %s -> %s\n", convert_from, convert_to);
			result_conv = malloc(strlen(result) * 2);
			strconvert(convert_from, convert_to, result, result_conv);
			strcpy(result, result_conv);
			free(result_conv);
		}
		
		if (strcmp("CALLERIDNAME", varname)==0) {
			cw_set_callerid(chan, NULL, result, NULL);
			if (option_verbose > 2)
				cw_verbose (VERBOSE_PREFIX_3 "LDAPget: set CIDNAME to \"%s\"\n", result);
		} else {
			if (option_verbose > 2)
				cw_verbose (VERBOSE_PREFIX_3 "LDAPget: set %s='%s'\n", varname, result);
			pbx_builtin_setvar_helper(chan, varname, result);
		}
		
	} else {
		pbx_builtin_setvar_helper(chan, "LDAPSTATUS", "FAILURE");
		return 0;
	}
	cw_config_destroy(cfg);
	free(filter);
	free(base);
	pbx_builtin_setvar_helper(chan, "LDAPSTATUS", "SUCCESS");

	LOCAL_USER_REMOVE(u);
	return 0;
}
Example #2
0
static int privacy_exec (struct cw_channel *chan, int argc, char **argv)
{
	char phone[30];
	struct localuser *u;
	struct cw_config *cfg;
	char *s;
	int res=0;
	int retries;
	int maxretries = 3;
	int minlength = 10;
	int x;

	LOCAL_USER_ADD (u);

	if (!cw_strlen_zero(chan->cid.cid_num)) {
		if (option_verbose > 2)
			cw_verbose (VERBOSE_PREFIX_3 "CallerID Present: Skipping\n");
		pbx_builtin_setvar_helper(chan, "PRIVACYMGRSTATUS", "SUCCESS");
	} else {
		/*Answer the channel if it is not already*/
		if (chan->_state != CW_STATE_UP) {
			res = cw_answer(chan);
			if (res) {
				pbx_builtin_setvar_helper(chan, "PRIVACYMGRSTATUS", "FAIL");
				LOCAL_USER_REMOVE(u);
				return 0;
			}
		}
		/*Read in the config file*/
		cfg = cw_config_load(PRIV_CONFIG);
		
		
		/*Play unidentified call*/
		res = cw_safe_sleep(chan, 1000);
		if (!res)
			res = cw_streamfile(chan, "privacy-unident", chan->language);
		if (!res)
			res = cw_waitstream(chan, "");

        if (cfg && (s = cw_variable_retrieve(cfg, "general", "maxretries"))) {
                if (sscanf(s, "%d", &x) == 1) {
                        maxretries = x;
                } else {
                        cw_log(LOG_WARNING, "Invalid max retries argument\n");
                }
        }
        if (cfg && (s = cw_variable_retrieve(cfg, "general", "minlength"))) {
                if (sscanf(s, "%d", &x) == 1) {
                        minlength = x;
                } else {
                        cw_log(LOG_WARNING, "Invalid min length argument\n");
                }
        }
			
		/*Ask for 10 digit number, give 3 attempts*/
		for (retries = 0; retries < maxretries; retries++) {
			if (!res ) 
				res = cw_app_getdata(chan, "privacy-prompt", phone, sizeof(phone), 0);

			if (res < 0)
				break;

			/*Make sure we get at least our minimum of digits*/
			if (strlen(phone) >= minlength ) 
				break;
			else {
				res = cw_streamfile(chan, "privacy-incorrect", chan->language);
				if (!res)
					res = cw_waitstream(chan, "");
			}
		}
		
		/*Got a number, play sounds and send them on their way*/
		if ((retries < maxretries) && res == 1 ) {
			res = cw_streamfile(chan, "privacy-thankyou", chan->language);
			if (!res)
				res = cw_waitstream(chan, "");
			cw_set_callerid (chan, phone, "Privacy Manager", NULL);
			if (option_verbose > 2)
				cw_verbose (VERBOSE_PREFIX_3 "Changed Caller*ID to %s\n",phone);
			pbx_builtin_setvar_helper(chan, "PRIVACYMGRSTATUS", "SUCCESS");
		} else {
			/* Flag Failure  */
			pbx_builtin_setvar_helper(chan, "PRIVACYMGRSTATUS", "FAIL");
		}
		if (cfg) 
			cw_config_destroy(cfg);
	}

  LOCAL_USER_REMOVE (u);
  return 0;
}