int cw_privacy_set(char *dest, char *cid, int status) { char tmp[256] = ""; char *trimcid = ""; char *n, *l; int res; char key[256]; if (cid) cw_copy_string(tmp, cid, sizeof(tmp)); cw_callerid_parse(tmp, &n, &l); if (l) { cw_shrink_phone_number(l); trimcid = l; } if (cw_strlen_zero(trimcid)) { /* Don't store anything for empty Caller*ID */ return 0; } snprintf(key, sizeof(key), "%s/%s", dest, trimcid); if (status == CW_PRIVACY_UNKNOWN) res = cw_db_del("privacy", key); else if (status == CW_PRIVACY_ALLOW) res = cw_db_put("privacy", key, "allow"); else if (status == CW_PRIVACY_DENY) res = cw_db_put("privacy", key, "deny"); else if (status == CW_PRIVACY_KILL) res = cw_db_put("privacy", key, "kill"); else if (status == CW_PRIVACY_TORTURE) res = cw_db_put("privacy", key, "torture"); else res = -1; return res; }
int cw_privacy_check(char *dest, char *cid) { char tmp[256] = ""; char *trimcid = ""; char *n, *l; int res; char key[256], result[256]; if (cid) cw_copy_string(tmp, cid, sizeof(tmp)); cw_callerid_parse(tmp, &n, &l); if (l) { cw_shrink_phone_number(l); trimcid = l; } snprintf(key, sizeof(key), "%s/%s", dest, trimcid); res = cw_db_get("privacy", key, result, sizeof(result)); if (!res) { if (!strcasecmp(result, "allow")) return CW_PRIVACY_ALLOW; if (!strcasecmp(result, "deny")) return CW_PRIVACY_DENY; if (!strcasecmp(result, "kill")) return CW_PRIVACY_KILL; if (!strcasecmp(result, "torture")) return CW_PRIVACY_TORTURE; } return CW_PRIVACY_UNKNOWN; }
int cw_osp_validate(char *provider, char *token, int *handle, unsigned int *timelimit, char *callerid, struct in_addr addr, char *extension) { char tmp[256]="", *l, *n; char iabuf[INET_ADDRSTRLEN]; char source[OSP_MAX] = ""; /* Same length as osp->source */ char *token2; int tokenlen; struct osp_provider *osp; int res = 0; unsigned int authorised, dummy; if (!provider || !strlen(provider)) provider = "default"; token2 = cw_strdupa(token); tokenlen = cw_base64decode(token2, token, strlen(token)); *handle = -1; if (!callerid) callerid = ""; cw_copy_string(tmp, callerid, sizeof(tmp)); cw_callerid_parse(tmp, &n, &l); if (!l) l = ""; else { cw_shrink_phone_number(l); if (!cw_isphonenumber(l)) l = ""; } callerid = l; cw_mutex_lock(&osplock); cw_inet_ntoa(iabuf, sizeof(iabuf), addr); osp = providers; while(osp) { if (!strcasecmp(osp->name, provider)) { if (OSPPTransactionNew(osp->handle, handle)) { cw_log(LOG_WARNING, "Unable to create OSP Transaction handle!\n"); } else { cw_copy_string(source, osp->source, sizeof(source)); res = 1; } break; } osp = osp->next; } cw_mutex_unlock(&osplock); if (res) { res = 0; dummy = 0; if (!OSPPTransactionValidateAuthorisation(*handle, iabuf, source, NULL, NULL, callerid, OSPC_E164, extension, OSPC_E164, 0, "", tokenlen, token2, &authorised, timelimit, &dummy, NULL, tokenformat)) { if (authorised) { cw_log(LOG_DEBUG, "Validated token for '%s' from '%s@%s'\n", extension, callerid, iabuf); res = 1; } } } return res; }
int cw_callerid_split(const char *buf, char *name, int namelen, char *num, int numlen) { char *tmp; char *l = NULL; char *n = NULL; tmp = cw_strdupa(buf); cw_callerid_parse(tmp, &n, &l); if (n) cw_copy_string(name, n, namelen); else name[0] = '\0'; if (l) { cw_shrink_phone_number(l); cw_copy_string(num, l, numlen); } else num[0] = '\0'; return 0; }
int cw_osp_lookup(struct cw_channel *chan, char *provider, char *extension, char *callerid, struct cw_osp_result *result) { int cres; int res = 0; int counts; int tokenlen; unsigned int dummy=0; unsigned int timelimit; unsigned int callidlen; char callidstr[OSPC_CALLID_MAXSIZE] = ""; struct osp_provider *osp; char source[OSP_MAX] = ""; /* Same length as osp->source */ char callednum[2048]=""; char callingnum[2048]=""; char destination[2048]=""; char token[2000]; char tmp[256]="", *l, *n; OSPE_DEST_PROT prot; OSPE_DEST_OSP_ENABLED ospenabled; char *devinfo = NULL; result->handle = -1; result->numresults = 0; result->tech[0] = '\0'; result->dest[0] = '\0'; result->token[0] = '\0'; if (!provider || !strlen(provider)) provider = "default"; if (!callerid) callerid = ""; cw_copy_string(tmp, callerid, sizeof(tmp)); cw_callerid_parse(tmp, &n, &l); if (!l) l = ""; else { cw_shrink_phone_number(l); if (!cw_isphonenumber(l)) l = ""; } callerid = l; if (chan) { cres = cw_autoservice_start(chan); if (cres < 0) return cres; } cw_mutex_lock(&osplock); osp = providers; while(osp) { if (!strcasecmp(osp->name, provider)) { if (OSPPTransactionNew(osp->handle, &result->handle)) { cw_log(LOG_WARNING, "Unable to create OSP Transaction handle!\n"); } else { cw_copy_string(source, osp->source, sizeof(source)); res = 1; } break; } osp = osp->next; } cw_mutex_unlock(&osplock); if (res) { res = 0; /* No more than 10 back */ counts = 10; dummy = 0; devinfo = pbx_builtin_getvar_helper (chan, "OSPPEER"); if (!devinfo) { devinfo = ""; } if (!OSPPTransactionRequestAuthorisation(result->handle, source, devinfo, callerid,OSPC_E164, extension, OSPC_E164, NULL, 0, NULL, NULL, &counts, &dummy, NULL)) { if (counts) { tokenlen = sizeof(token); result->numresults = counts - 1; callidlen = sizeof(callidstr); if (!OSPPTransactionGetFirstDestination(result->handle, 0, NULL, NULL, &timelimit, &callidlen, callidstr, sizeof(callednum), callednum, sizeof(callingnum), callingnum, sizeof(destination), destination, 0, NULL, &tokenlen, token)) { cw_log(LOG_DEBUG, "Got destination '%s' and called: '%s' calling: '%s' for '%s' (provider '%s')\n", destination, callednum, callingnum, extension, provider); /* Only support OSP server with only one duration limit */ if (cw_channel_cmpwhentohangup (chan, timelimit) < 0) { cw_channel_setwhentohangup (chan, timelimit); } do { if (!OSPPTransactionIsDestOSPEnabled (result->handle, &ospenabled) && (ospenabled == OSPE_OSP_FALSE)) { result->token[0] = 0; } else { cw_base64encode(result->token, token, tokenlen, sizeof(result->token) - 1); } if ((strlen(destination) > 2) && !OSPPTransactionGetDestProtocol(result->handle, &prot)) { res = 1; /* Strip leading and trailing brackets */ destination[strlen(destination) - 1] = '\0'; switch(prot) { case OSPE_DEST_PROT_H323_SETUP: cw_copy_string(result->tech, "H323", sizeof(result->tech)); snprintf(result->dest, sizeof(result->dest), "%s@%s", callednum, destination + 1); break; case OSPE_DEST_PROT_SIP: cw_copy_string(result->tech, "SIP", sizeof(result->tech)); snprintf(result->dest, sizeof(result->dest), "%s@%s", callednum, destination + 1); break; case OSPE_DEST_PROT_IAX: cw_copy_string(result->tech, "IAX", sizeof(result->tech)); snprintf(result->dest, sizeof(result->dest), "%s@%s", callednum, destination + 1); break; default: cw_log(LOG_DEBUG, "Unknown destination protocol '%d', skipping...\n", prot); res = 0; } if (!res && result->numresults) { result->numresults--; callidlen = sizeof(callidstr); if (OSPPTransactionGetNextDestination(result->handle, OSPC_FAIL_INCOMPATIBLE_DEST, 0, NULL, NULL, &timelimit, &callidlen, callidstr, sizeof(callednum), callednum, sizeof(callingnum), callingnum, sizeof(destination), destination, 0, NULL, &tokenlen, token)) { break; } } } else { cw_log(LOG_DEBUG, "Missing destination protocol\n"); break; } } while(!res && result->numresults); } } } if (!res) { OSPPTransactionDelete(result->handle); result->handle = -1; } } if (!osp) cw_log(LOG_NOTICE, "OSP Provider '%s' does not exist!\n", provider); if (chan) { cres = cw_autoservice_stop(chan); if (cres < 0) return cres; } return res; }