Use INET_ADDRLEN (bug #1956) (from airport!)
[asterisk/asterisk.git] / res / res_osp.c
index deac15c..5db3d42 100755 (executable)
@@ -28,6 +28,7 @@
 #include <asterisk/utils.h>
 #include <asterisk/lock.h>
 #include <asterisk/causes.h>
+#include <asterisk/callerid.h>
 #include <osp.h>
 #include <openssl/err.h>
 #include <stdio.h>
@@ -419,6 +420,67 @@ static int loadPemPrivateKey(unsigned char *FileName, unsigned char *buffer, int
     return retVal;
 }
 
+int ast_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 = ast_strdupa(token);
+       if (!token2)
+               return -1;
+       tokenlen = ast_base64decode(token2, token, strlen(token));
+       *handle = -1;
+       if (!callerid)
+               callerid = "";
+       strncpy(tmp, callerid, sizeof(tmp) - 1);
+       ast_callerid_parse(tmp, &n, &l);
+       if (!l)
+               l = "";
+       else {
+               ast_shrink_phone_number(l);
+               if (!ast_isphonenumber(l))
+                       l = "";
+       }
+       callerid = l;
+       ast_mutex_lock(&osplock);
+       ast_inet_ntoa(iabuf, sizeof(iabuf), addr);
+       osp = providers;
+       while(osp) {
+               if (!strcasecmp(osp->name, provider)) {
+                       if (OSPPTransactionNew(osp->handle, handle)) {
+                               ast_log(LOG_WARNING, "Unable to create OSP Transaction handle!\n");
+                       } else {
+                               strcpy(source, osp->source);
+                               res = 1;
+                       }
+                       break;
+               }
+               osp = osp->next;
+       }
+       ast_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, TOKEN_ALGO_BOTH)) {
+                       if (authorised) {
+                               ast_log(LOG_DEBUG, "Validated token for '%s' from '%s@%s'\n", extension, callerid, iabuf);
+                               res = 1;
+                       }
+               }
+       }
+       return res;     
+}
+
 int ast_osp_lookup(struct ast_channel *chan, char *provider, char *extension, char *callerid, struct ast_osp_result *result)
 {
        int cres;
@@ -434,6 +496,7 @@ int ast_osp_lookup(struct ast_channel *chan, char *provider, char *extension, ch
        char callednum[2048]="";
        char destination[2048]="";
        char token[2000];
+       char tmp[256]="", *l, *n;
        OSPTCALLID *callid;
        OSPE_DEST_PROT prot;
 
@@ -448,6 +511,16 @@ int ast_osp_lookup(struct ast_channel *chan, char *provider, char *extension, ch
 
        if (!callerid)
                callerid = "";
+       strncpy(tmp, callerid, sizeof(tmp) - 1);
+       ast_callerid_parse(tmp, &n, &l);
+       if (!l)
+               l = "";
+       else {
+               ast_shrink_phone_number(l);
+               if (!ast_isphonenumber(l))
+                       l = "";
+       }
+       callerid = l;
 
        if (chan) {
                strncpy(uniqueid, chan->uniqueid, sizeof(uniqueid) - 1);