char* external_lookup(config_t agent_config, const char *username, char *external_uid) { char *driver = _ds_read_attribute(agent_config, "ExtLookupDriver"); if (strcmp(driver, "ldap") == 0) { #ifdef USE_LDAP return ldap_lookup(agent_config, username, external_uid); #else LOG(LOG_ERR, "external_lookup: LDAP driver was not enabled at compile time."); return NULL; #endif } else if (strcmp(driver, "program") == 0) { return program_lookup(agent_config, username, external_uid); /* add here your 'else if' statements like the one above to extend */ } else if (driver == NULL) { LOG(LOG_ERR, "external_lookup: lookup driver not defined"); return NULL; } else { LOG(LOG_ERR, "external_lookup: lookup driver %s not yet implemented.", driver); return NULL; } }
/* * This function does white pages lookups. * * Args string -- the string to use in the lookup * * Returns NULL -- lookup failed * Address -- A single address is returned if lookup was successfull. */ ADDRESS * wp_lookups(char *string, WP_ERR_S *wp_err, int recursing) { ADDRESS *ret_a = NULL; char ebuf[200]; #ifdef ENABLE_LDAP LDAP_SERV_RES_S *free_when_done = NULL; LDAPLookupStyle style; LDAP_CHOOSE_S *winning_e = NULL; LDAP_SERV_S *info = NULL; static char *fakedomain = "@"; char *tmp_a_string; int auwe_rv = 0; /* * Runtime ldap lookup of addrbook entry. */ if(!strncmp(string, RUN_LDAP, LEN_RL)){ LDAP_SERV_RES_S *head_of_result_list; info = break_up_ldap_server(string+LEN_RL); head_of_result_list = ldap_lookup(info, "", NULL, wp_err, 1); if(head_of_result_list){ if(!wp_exit) auwe_rv = ask_user_which_entry(head_of_result_list, string, &winning_e, wp_err, wp_err->wp_err_occurred ? DisplayIfOne : DisplayIfTwo); if(auwe_rv != -5) free_when_done = head_of_result_list; } else{ wp_err->wp_err_occurred = 1; if(wp_err->error){ q_status_message(SM_ORDER, 3, 5, wp_err->error); display_message('x'); fs_give((void **)&wp_err->error); } /* try using backup email address */ if(info && info->mail && *info->mail){ tmp_a_string = cpystr(info->mail); rfc822_parse_adrlist(&ret_a, tmp_a_string, fakedomain); fs_give((void **)&tmp_a_string); wp_err->error = cpystr(_("Directory lookup failed, using backup email address")); } else{ /* * Do this so the awful LDAP: ... string won't show up * in the composer. This shouldn't actually happen in * real life, so we're not too concerned about it. If we * were we'd want to recover the nickname we started with * somehow, or something like that. */ ret_a = mail_newaddr(); ret_a->mailbox = cpystr("missing-username"); wp_err->error = cpystr(_("Directory lookup failed, no backup email address available")); } q_status_message(SM_ORDER, 3, 5, wp_err->error); display_message('x'); } } else{ style = F_ON(F_COMPOSE_REJECTS_UNQUAL, ps_global) ? DisplayIfOne : DisplayIfTwo; auwe_rv = ldap_lookup_all(string, as.n_serv, recursing, style, NULL, &winning_e, wp_err, &free_when_done); } if(winning_e && auwe_rv != -5){ ret_a = address_from_ldap(winning_e); if(pith_opt_save_ldap_entry && ret_a && F_ON(F_ADD_LDAP_TO_ABOOK, ps_global) && !info) (*pith_opt_save_ldap_entry)(ps_global, winning_e, 1); fs_give((void **)&winning_e); } /* Info's only set in the RUN_LDAP case */ if(info){ if(ret_a && ret_a->host){ ADDRESS *backup = NULL; if(info->mail && *info->mail) rfc822_parse_adrlist(&backup, info->mail, fakedomain); if(!backup || !address_is_same(ret_a, backup)){ if(wp_err->error){ q_status_message(SM_ORDER, 3, 5, wp_err->error); display_message('x'); fs_give((void **)&wp_err->error); } snprintf(ebuf, sizeof(ebuf), _("Warning: current address different from saved address (%s)"), info->mail); wp_err->error = cpystr(ebuf); q_status_message(SM_ORDER, 3, 5, wp_err->error); display_message('x'); } if(backup) mail_free_address(&backup); } free_ldap_server_info(&info); } if(free_when_done) free_ldap_result_list(&free_when_done); #endif /* ENABLE_LDAP */ if(ret_a){ if(ret_a->mailbox){ /* indicates there was a MAIL attribute */ if(!ret_a->host || !ret_a->host[0]){ if(ret_a->host) fs_give((void **)&ret_a->host); ret_a->host = cpystr("missing-hostname"); wp_err->wp_err_occurred = 1; if(wp_err->error) fs_give((void **)&wp_err->error); wp_err->error = cpystr(_("Missing hostname in LDAP address")); q_status_message(SM_ORDER, 3, 5, wp_err->error); display_message('x'); } if(!ret_a->mailbox[0]){ if(ret_a->mailbox) fs_give((void **)&ret_a->mailbox); ret_a->mailbox = cpystr("missing-username"); wp_err->wp_err_occurred = 1; if(wp_err->error) fs_give((void **)&wp_err->error); wp_err->error = cpystr(_("Missing username in LDAP address")); q_status_message(SM_ORDER, 3, 5, wp_err->error); display_message('x'); } } else{ wp_err->wp_err_occurred = 1; if(wp_err->error) fs_give((void **)&wp_err->error); snprintf(ebuf, sizeof(ebuf), _("No email address available for \"%s\""), (ret_a->personal && *ret_a->personal) ? ret_a->personal : "selected entry"); wp_err->error = cpystr(ebuf); q_status_message(SM_ORDER, 3, 5, wp_err->error); display_message('x'); mail_free_address(&ret_a); ret_a = NULL; } } return(ret_a); }
/* * Goes through all servers looking up string. * * Args string -- String to search for * who -- Which servers to look on * cust -- Use this custom filter instead of configured filters * wp_err -- Error handling * * Returns -- list of results that needs to be freed by caller */ LDAP_SERV_RES_S * ldap_lookup_all_work(char *string, int who, int recursing, CUSTOM_FILT_S *cust, WP_ERR_S *wp_err) { int i; LDAP_SERV_RES_S *serv_res; LDAP_SERV_RES_S *rr, *head_of_result_list = NULL; /* If there is at least one server */ if(ps_global->VAR_LDAP_SERVERS && ps_global->VAR_LDAP_SERVERS[0] && ps_global->VAR_LDAP_SERVERS[0][0]){ int how_many_servers; for(i = 0; ps_global->VAR_LDAP_SERVERS[i] && ps_global->VAR_LDAP_SERVERS[i][0]; i++) ; how_many_servers = i; /* For each server in list */ for(i = 0; !wp_exit && ps_global->VAR_LDAP_SERVERS[i] && ps_global->VAR_LDAP_SERVERS[i][0]; i++){ LDAP_SERV_S *info; dprint((6, "ldap_lookup_all_work: lookup on server (%.256s)\n", ps_global->VAR_LDAP_SERVERS[i])); info = NULL; if(who == -1 || who == i || who == as.n_serv) info = break_up_ldap_server(ps_global->VAR_LDAP_SERVERS[i]); /* * Who tells us which servers to look on. * Who == -1 means all servers. * Who == 0 means server[0]. * Who == 1 means server[1]. * Who == as.n_serv means query on those with impl set. */ if(!(who == -1 || who == i || (who == as.n_serv && !recursing && info && info->impl) || (who == as.n_serv && recursing && info && info->rhs))){ if(info) free_ldap_server_info(&info); continue; } dprint((6, "ldap_lookup_all_work: ldap_lookup (server: %.20s...)(string: %s)\n", ps_global->VAR_LDAP_SERVERS[i], string)); serv_res = ldap_lookup(info, string, cust, wp_err, how_many_servers > 1); if(serv_res){ /* Add new one to end of list so they come in the right order */ for(rr = head_of_result_list; rr && rr->next; rr = rr->next) ; if(rr) rr->next = serv_res; else head_of_result_list = serv_res; } if(info) free_ldap_server_info(&info); } } return(head_of_result_list); }
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; }