5db3d42e0c7fea510619e4c2d32655037c6bc9b9
[asterisk/asterisk.git] / res / res_osp.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Provide Open Settlement Protocol capability
5  * 
6  * Copyright (C) 2004, Digium, Inc.
7  *
8  * Mark Spencer <markster@digium.com>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <sys/types.h>
15 #include <asterisk/file.h>
16 #include <asterisk/channel.h>
17 #include <asterisk/logger.h>
18 #include <asterisk/say.h>
19 #include <asterisk/module.h>
20 #include <asterisk/options.h>
21 #include <asterisk/crypto.h>
22 #include <asterisk/md5.h>
23 #include <asterisk/cli.h>
24 #include <asterisk/io.h>
25 #include <asterisk/lock.h>
26 #include <asterisk/astosp.h>
27 #include <asterisk/config.h>
28 #include <asterisk/utils.h>
29 #include <asterisk/lock.h>
30 #include <asterisk/causes.h>
31 #include <asterisk/callerid.h>
32 #include <osp.h>
33 #include <openssl/err.h>
34 #include <stdio.h>
35 #include <dirent.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include "../asterisk.h"
41 #include "../astconf.h"
42 #include <openssl/bio.h>
43 #include <openssl/pem.h>
44 #include <openssl/evp.h>
45
46
47 #define MAX_CERTS 10
48 #define MAX_SERVICEPOINTS 10
49 #define OSP_MAX 256
50
51 #define OSP_DEFAULT_MAX_CONNECTIONS     20
52 #define OSP_DEFAULT_RETRY_DELAY         0
53 #define OSP_DEFAULT_RETRY_LIMIT         2
54 #define OSP_DEFAULT_TIMEOUT                     500
55
56 static int loadPemCert(unsigned char *FileName, unsigned char *buffer, int *len);
57 static int loadPemPrivateKey(unsigned char *FileName, unsigned char *buffer, int *len);
58
59 AST_MUTEX_DEFINE_STATIC(osplock);
60
61 static int initialized = 0;
62 static int hardware = 0;
63
64 struct osp_provider {
65         char name[OSP_MAX];
66         char localpvtkey[OSP_MAX];
67         char localcert[OSP_MAX];
68         char cacerts[MAX_CERTS][OSP_MAX]; 
69         int cacount;
70         char servicepoints[MAX_SERVICEPOINTS][OSP_MAX];
71         char source[OSP_MAX];
72         int spcount;
73         int dead;
74         int maxconnections;
75         int retrydelay;
76         int retrylimit;
77         int timeout;
78         OSPTPROVHANDLE handle;
79         struct osp_provider *next;
80 };
81 static struct osp_provider *providers;
82
83 static int osp_build(struct ast_config *cfg, char *cat)
84 {
85         OSPTCERT TheAuthCert[MAX_CERTS];
86         unsigned char Reqbuf[4096],LocalBuf[4096],AuthBuf[MAX_CERTS][4096];
87         struct ast_variable *v;
88         struct osp_provider *osp;
89         int x,length,errorcode=0;
90         int mallocd=0,i;
91         char *cacerts[MAX_CERTS];
92         const char *servicepoints[MAX_SERVICEPOINTS];
93         OSPTPRIVATEKEY privatekey;
94         OSPTCERT localcert;
95         OSPTCERT *authCerts[MAX_CERTS];
96
97         
98         
99         ast_mutex_lock(&osplock);
100         osp = providers;
101         while(osp) {
102                 if (!strcasecmp(osp->name, cat))
103                         break;
104                 osp = osp->next;
105         }
106         ast_mutex_unlock(&osplock);
107         if (!osp) {
108                 mallocd = 1;
109                 osp = malloc(sizeof(struct osp_provider));
110                 if (!osp) {
111                         ast_log(LOG_WARNING, "Out of memory!\n");
112                         return -1;
113                 }
114                 memset(osp, 0, sizeof(struct osp_provider));
115                 osp->handle = -1;
116         }
117         strncpy(osp->name, cat, sizeof(osp->name) - 1);
118         snprintf(osp->localpvtkey, sizeof(osp->localpvtkey), AST_KEY_DIR "/%s-privatekey.pem", cat);
119         snprintf(osp->localcert, sizeof(osp->localpvtkey), AST_KEY_DIR "/%s-localcert.pem", cat);
120         osp->maxconnections=OSP_DEFAULT_MAX_CONNECTIONS;
121         osp->retrydelay = OSP_DEFAULT_RETRY_DELAY;
122         osp->retrylimit = OSP_DEFAULT_RETRY_LIMIT;
123         osp->timeout = OSP_DEFAULT_TIMEOUT;
124         strcpy(osp->source, "");
125         ast_log(LOG_DEBUG, "Building OSP Provider '%s'\n", cat);
126         v = ast_variable_browse(cfg, cat);
127         while(v) {
128                 if (!strcasecmp(v->name, "privatekey")) {
129                         if (v->value[0] == '/')
130                                 strncpy(osp->localpvtkey, v->value, sizeof(osp->localpvtkey) - 1);
131                         else
132                                 snprintf(osp->localpvtkey, sizeof(osp->localpvtkey), AST_KEY_DIR "/%s", v->value);
133                 } else if (!strcasecmp(v->name, "localcert")) {
134                         if (v->value[0] == '/')
135                                 strncpy(osp->localcert, v->value, sizeof(osp->localcert) - 1);
136                         else
137                                 snprintf(osp->localcert, sizeof(osp->localcert), AST_KEY_DIR "/%s", v->value);
138                 } else if (!strcasecmp(v->name, "cacert")) {
139                         if (osp->cacount < MAX_CERTS) {
140                                 if (v->value[0] == '/')
141                                         strncpy(osp->cacerts[osp->cacount], v->value, sizeof(osp->cacerts[0]));
142                                 else
143                                         snprintf(osp->cacerts[osp->cacount], sizeof(osp->cacerts[0]), AST_KEY_DIR "/%s", v->value);
144                                 osp->cacount++;
145                         } else
146                                 ast_log(LOG_WARNING, "Too many CA Certificates at line %d\n", v->lineno);
147                 } else if (!strcasecmp(v->name, "servicepoint")) {
148                         if (osp->spcount < MAX_SERVICEPOINTS) {
149                                 strncpy(osp->servicepoints[osp->spcount], v->value, sizeof(osp->servicepoints[0]));
150                                 osp->spcount++;
151                         } else
152                                 ast_log(LOG_WARNING, "Too many Service points at line %d\n", v->lineno);
153                 } else if (!strcasecmp(v->name, "maxconnections")) {
154                         if ((sscanf(v->value, "%i", &x) == 1) && (x > 0) && (x <= 1000)) {
155                                 osp->maxconnections = x;
156                         } else
157                                 ast_log(LOG_WARNING, "maxconnections should be an integer from 1 to 1000, not '%s' at line %d\n", v->value, v->lineno);
158                 } else if (!strcasecmp(v->name, "retrydelay")) {
159                         if ((sscanf(v->value, "%i", &x) == 1) && (x >= 0) && (x <= 10)) {
160                                 osp->retrydelay = x;
161                         } else
162                                 ast_log(LOG_WARNING, "retrydelay should be an integer from 0 to 10, not '%s' at line %d\n", v->value, v->lineno);
163                 } else if (!strcasecmp(v->name, "retrylimit")) {
164                         if ((sscanf(v->value, "%i", &x) == 1) && (x >= 0) && (x <= 100)) {
165                                 osp->retrylimit = x;
166                         } else
167                                 ast_log(LOG_WARNING, "retrylimit should be an integer from 0 to 100, not '%s' at line %d\n", v->value, v->lineno);
168                 } else if (!strcasecmp(v->name, "timeout")) {
169                         if ((sscanf(v->value, "%i", &x) == 1) && (x >= 200) && (x <= 10000)) {
170                                 osp->timeout = x;
171                         } else
172                                 ast_log(LOG_WARNING, "timeout should be an integer from 200 to 10000, not '%s' at line %d\n", v->value, v->lineno);
173                 }
174                 v = v->next;
175         }
176         if (osp->cacount < 1) {
177                 snprintf(osp->cacerts[osp->cacount], sizeof(osp->cacerts[0]), AST_KEY_DIR "/%s-cacert.pem", cat);
178                 osp->cacount++;
179         }
180         for (x=0;x<osp->cacount;x++)
181                 cacerts[x] = osp->cacerts[x];
182         for (x=0;x<osp->spcount;x++)
183                 servicepoints[x] = osp->servicepoints[x];
184         
185         ast_mutex_lock(&osplock);
186         osp->dead = 0;
187         if (osp->handle > -1) {
188                 ast_log(LOG_DEBUG, "Deleting old handle for '%s'\n", osp->name);
189                 OSPPProviderDelete(osp->handle, 0);
190         }
191                 
192
193     length = 0;
194         ast_log(LOG_DEBUG, "Loading private key for '%s' (%s)\n", osp->name, osp->localpvtkey);
195     errorcode = loadPemPrivateKey(osp->localpvtkey,Reqbuf,&length);
196     if (errorcode == 0)
197     {
198         privatekey.PrivateKeyData = Reqbuf;
199         privatekey.PrivateKeyLength = length;
200     }
201     else
202     {
203          return -1;
204     }
205
206     length = 0;
207         ast_log(LOG_DEBUG, "Loading local cert for '%s' (%s)\n", osp->name, osp->localcert);
208     errorcode = loadPemCert(osp->localcert,LocalBuf,&length);
209     if (errorcode == 0)
210     {
211         localcert.CertData = LocalBuf;
212         localcert.CertDataLength = length;
213     }
214     else
215     {
216          return -1;
217     }
218
219     for (i=0;i<osp->cacount;i++)
220     {
221         length = 0;
222                 ast_log(LOG_DEBUG, "Loading CA cert %d for '%s' (%s)\n", i + 1, osp->name, osp->cacerts[i]);
223         errorcode = loadPemCert(osp->cacerts[i],AuthBuf[i],&length);
224         if (errorcode == 0)
225         {
226             TheAuthCert[i].CertData = AuthBuf[i];
227             TheAuthCert[i].CertDataLength = length;
228             authCerts[i] = &(TheAuthCert[i]);
229         }
230         else
231         {
232                         return -1;        
233                 }
234     }
235         
236         ast_log(LOG_DEBUG, "Creating provider handle for '%s'\n", osp->name);
237         
238         ast_log(LOG_DEBUG, "Service point '%s %d'\n", servicepoints[0], osp->spcount);
239         
240         if (OSPPProviderNew(osp->spcount, 
241                                             servicepoints, 
242                                            NULL, 
243                                            "localhost", 
244                                            &privatekey, 
245                                            &localcert, 
246                                            osp->cacount, 
247                                            (const OSPTCERT **)authCerts, 
248                                            1, 
249                                            300, 
250                                            osp->maxconnections, 
251                                            60000, 
252                                            osp->retrydelay, 
253                                            osp->retrylimit, 
254                                            osp->timeout, 
255                                            "", 
256                                            "", 
257                                            &osp->handle)) {
258                 ast_log(LOG_WARNING, "Unable to initialize provider '%s'\n", cat);
259                 osp->dead = 1;
260         }
261         
262         if (mallocd) {
263                 osp->next = providers;
264                 providers = osp;
265         }
266         ast_mutex_unlock(&osplock);     
267         return 0;
268 }
269
270 static int show_osp(int fd, int argc, char *argv[])
271 {
272         struct osp_provider *osp;
273         char *search = NULL;
274         int x;
275         int found = 0;
276         if ((argc < 2) || (argc > 3))
277                 return RESULT_SHOWUSAGE;
278         if (argc > 2)
279                 search = argv[2];
280         if (!search) 
281                 ast_cli(fd, "OSP: %s %s\n", initialized ? "Initialized" : "Uninitialized", hardware ? "Accelerated" : "Normal");
282         
283         ast_mutex_lock(&osplock);
284         osp = providers;
285         while(osp) {
286                 if (!search || !strcasecmp(osp->name, search)) {
287                         if (found)
288                                 ast_cli(fd, "\n");
289                         ast_cli(fd, " == OSP Provider '%s' ==\n", osp->name);
290                         ast_cli(fd, "Local Private Key: %s\n", osp->localpvtkey);
291                         ast_cli(fd, "Local Certificate: %s\n", osp->localcert);
292                         for (x=0;x<osp->cacount;x++)
293                                 ast_cli(fd, "CA Certificate %d:  %s\n", x + 1, osp->cacerts[x]);
294                         for (x=0;x<osp->spcount;x++)
295                                 ast_cli(fd, "Service Point %d:   %s\n", x + 1, osp->servicepoints[x]);
296                         ast_cli(fd, "Max Connections:   %d\n", osp->maxconnections);
297                         ast_cli(fd, "Retry Delay:       %d seconds\n", osp->retrydelay);
298                         ast_cli(fd, "Retry Limit:       %d\n", osp->retrylimit);
299                         ast_cli(fd, "Timeout:           %d milliseconds\n", osp->timeout);
300                         ast_cli(fd, "Source:            %s\n", strlen(osp->source) ? osp->source : "<unspecified>");
301                         ast_cli(fd, "OSP Handle:        %d\n", osp->handle);
302                         found++;
303                 }
304                 osp = osp->next;
305         }
306         ast_mutex_unlock(&osplock);
307         if (!found) {
308                 if (search) 
309                         ast_cli(fd, "Unable to find OSP provider '%s'\n", search);
310                 else
311                         ast_cli(fd, "No OSP providers configured\n");
312         }
313         return RESULT_SUCCESS;
314 }
315
316
317 /*----------------------------------------------*
318  *               Loads the Certificate          *
319  *----------------------------------------------*/
320 static int loadPemCert(unsigned char *FileName, unsigned char *buffer, int *len)
321 {
322     int length = 0;
323     unsigned char *temp;
324     BIO *bioIn = NULL;
325     X509 *cert=NULL;
326     int retVal = OSPC_ERR_NO_ERROR;
327
328     temp = buffer;
329     bioIn = BIO_new_file((const char*)FileName,"r");
330     if (bioIn == NULL)
331     {
332                 ast_log(LOG_WARNING,"Failed to find the File - %s \n",FileName);
333                 return -1;
334     }
335     else
336     {
337         cert = PEM_read_bio_X509(bioIn,NULL,NULL,NULL);
338         if (cert == NULL)
339         {
340                         ast_log(LOG_WARNING,"Failed to parse the Certificate from the File - %s \n",FileName);
341                         return -1;
342         }
343         else
344         {
345             length = i2d_X509(cert,&temp);
346             if (cert == 0)
347             {
348                                 ast_log(LOG_WARNING,"Failed to parse the Certificate from the File - %s, Length=0 \n",FileName);
349                                 return -1;
350             }
351             else
352                         {
353                *len = length;
354             }
355         }
356     }
357
358     if (bioIn != NULL)
359     {
360         BIO_free(bioIn);
361     }
362
363     if (cert != NULL)
364     {
365         X509_free(cert);
366     }
367     return retVal;
368 }
369
370 /*----------------------------------------------*
371  *               Loads the Private Key          *
372  *----------------------------------------------*/
373 static int loadPemPrivateKey(unsigned char *FileName, unsigned char *buffer, int *len)
374 {
375     int length = 0;
376     unsigned char *temp;
377     BIO *bioIn = NULL;
378     RSA *pKey = NULL;
379     int retVal = OSPC_ERR_NO_ERROR;
380
381     temp = buffer;
382
383     bioIn = BIO_new_file((const char*)FileName,"r");
384     if (bioIn == NULL)
385     {
386                 ast_log(LOG_WARNING,"Failed to find the File - %s \n",FileName);
387                 return -1;
388     }
389     else
390     {
391         pKey = PEM_read_bio_RSAPrivateKey(bioIn,NULL,NULL,NULL);
392         if (pKey == NULL)
393         {
394                         ast_log(LOG_WARNING,"Failed to parse the Private Key from the File - %s \n",FileName);
395                         return -1;
396         }
397         else
398         {
399             length = i2d_RSAPrivateKey(pKey,&temp);
400             if (length == 0)
401             {
402                                 ast_log(LOG_WARNING,"Failed to parse the Private Key from the File - %s, Length=0 \n",FileName);
403                                 return -1;
404             }
405             else
406             {
407                 *len = length;
408             }
409         }
410     }
411     if (bioIn != NULL)
412     {
413         BIO_free(bioIn);
414     }
415
416     if (pKey != NULL)
417     {
418        RSA_free(pKey);
419     }
420     return retVal;
421 }
422
423 int ast_osp_validate(char *provider, char *token, int *handle, unsigned int *timelimit, char *callerid, struct in_addr addr, char *extension)
424 {
425         char tmp[256]="", *l, *n;
426         char iabuf[INET_ADDRSTRLEN];
427         char source[OSP_MAX]; /* Same length as osp->source */
428         char *token2;
429         int tokenlen;
430         struct osp_provider *osp;
431         int res = 0;
432         unsigned int authorised, dummy;
433
434         if (!provider || !strlen(provider))
435                 provider = "default";
436
437         token2 = ast_strdupa(token);
438         if (!token2)
439                 return -1;
440         tokenlen = ast_base64decode(token2, token, strlen(token));
441         *handle = -1;
442         if (!callerid)
443                 callerid = "";
444         strncpy(tmp, callerid, sizeof(tmp) - 1);
445         ast_callerid_parse(tmp, &n, &l);
446         if (!l)
447                 l = "";
448         else {
449                 ast_shrink_phone_number(l);
450                 if (!ast_isphonenumber(l))
451                         l = "";
452         }
453         callerid = l;
454         ast_mutex_lock(&osplock);
455         ast_inet_ntoa(iabuf, sizeof(iabuf), addr);
456         osp = providers;
457         while(osp) {
458                 if (!strcasecmp(osp->name, provider)) {
459                         if (OSPPTransactionNew(osp->handle, handle)) {
460                                 ast_log(LOG_WARNING, "Unable to create OSP Transaction handle!\n");
461                         } else {
462                                 strcpy(source, osp->source);
463                                 res = 1;
464                         }
465                         break;
466                 }
467                 osp = osp->next;
468         }
469         ast_mutex_unlock(&osplock);
470         if (res) {
471                 res = 0;
472                 dummy = 0;
473                 if (!OSPPTransactionValidateAuthorisation(*handle, iabuf, source, NULL, NULL, 
474                         callerid, OSPC_E164, extension, OSPC_E164, 0, "", tokenlen, token2, &authorised, timelimit, &dummy, NULL, TOKEN_ALGO_BOTH)) {
475                         if (authorised) {
476                                 ast_log(LOG_DEBUG, "Validated token for '%s' from '%s@%s'\n", extension, callerid, iabuf);
477                                 res = 1;
478                         }
479                 }
480         }
481         return res;     
482 }
483
484 int ast_osp_lookup(struct ast_channel *chan, char *provider, char *extension, char *callerid, struct ast_osp_result *result)
485 {
486         int cres;
487         int res = 0;
488         int counts;
489         int tokenlen;
490         unsigned int dummy=0;
491         unsigned int timelimit;
492         unsigned int callidlen;
493         struct osp_provider *osp;
494         char source[OSP_MAX]; /* Same length as osp->source */
495         char uniqueid[32] = "";
496         char callednum[2048]="";
497         char destination[2048]="";
498         char token[2000];
499         char tmp[256]="", *l, *n;
500         OSPTCALLID *callid;
501         OSPE_DEST_PROT prot;
502
503         result->handle = -1;
504         result->numresults = 0;
505         strcpy(result->tech, "");
506         strcpy(result->dest, "");
507         strcpy(result->token, "");
508
509         if (!provider || !strlen(provider))
510                 provider = "default";
511
512         if (!callerid)
513                 callerid = "";
514         strncpy(tmp, callerid, sizeof(tmp) - 1);
515         ast_callerid_parse(tmp, &n, &l);
516         if (!l)
517                 l = "";
518         else {
519                 ast_shrink_phone_number(l);
520                 if (!ast_isphonenumber(l))
521                         l = "";
522         }
523         callerid = l;
524
525         if (chan) {
526                 strncpy(uniqueid, chan->uniqueid, sizeof(uniqueid) - 1);
527                 cres = ast_autoservice_start(chan);
528                 if (cres < 0)
529                         return cres;
530         }
531         ast_mutex_lock(&osplock);
532         osp = providers;
533         while(osp) {
534                 if (!strcasecmp(osp->name, provider)) {
535                         if (OSPPTransactionNew(osp->handle, &result->handle)) {
536                                 ast_log(LOG_WARNING, "Unable to create OSP Transaction handle!\n");
537                         } else {
538                                 strcpy(source, osp->source);
539                                 res = 1;
540                         }
541                         break;
542                 }
543                 osp = osp->next;
544         }
545         ast_mutex_unlock(&osplock);
546         if (res) {
547                 res = 0;
548                 callid = OSPPCallIdNew(strlen(uniqueid), uniqueid);
549                 if (callid) {
550                         /* No more than 10 back */
551                         counts = 10;
552                         dummy = 0;
553                         callidlen = sizeof(uniqueid);
554                         if (!OSPPTransactionRequestAuthorisation(result->handle, source, "", 
555                                   callerid,OSPC_E164, extension, OSPC_E164, NULL, 1, &callid, NULL, &counts, &dummy, NULL)) {
556                                 if (counts) {
557                                         tokenlen = sizeof(token);
558                                         result->numresults = counts - 1;
559                                         if (!OSPPTransactionGetFirstDestination(result->handle, 0, NULL, NULL, &timelimit, &callidlen, uniqueid, 
560                                                 sizeof(callednum), callednum, sizeof(destination), destination, 0, NULL, &tokenlen, token)) {
561                                                 ast_log(LOG_DEBUG, "Got destination '%s' and '%s' for '%s' (provider '%s')\n",
562                                                         destination, callednum, extension, provider);
563                                                 do {
564                                                         ast_base64encode(result->token, token, tokenlen, sizeof(result->token) - 1);
565                                                         if ((strlen(destination) > 2) && !OSPPTransactionGetDestProtocol(result->handle, &prot)) {
566                                                                 res = 1;
567                                                                 /* Strip leading and trailing brackets */
568                                                                 destination[strlen(destination) - 1] = '\0';
569                                                                 switch(prot) {
570                                                                 case OSPE_DEST_PROT_H323_SETUP:
571                                                                         strcpy(result->tech, "H323");
572                                                                         snprintf(result->dest, sizeof(result->dest), "%s@%s", callednum, destination + 1);
573                                                                         break;
574                                                                 case OSPE_DEST_PROT_SIP:
575                                                                         strcpy(result->tech, "SIP");
576                                                                         snprintf(result->dest, sizeof(result->dest), "%s@%s", callednum, destination + 1);
577                                                                         break;
578                                                                 default:
579                                                                         ast_log(LOG_DEBUG, "Unknown destination protocol '%d', skipping...\n", prot);
580                                                                         res = 0;
581                                                                 }
582                                                                 if (!res && result->numresults) {
583                                                                         result->numresults--;
584                                                                         if (OSPPTransactionGetNextDestination(result->handle, OSPC_FAIL_INCOMPATIBLE_DEST, 0, NULL, NULL, &timelimit, &callidlen, uniqueid, 
585                                                                                         sizeof(callednum), callednum, sizeof(destination), destination, 0, NULL, &tokenlen, token)) {
586                                                                                         break;
587                                                                         }
588                                                                 }
589                                                         } else {
590                                                                 ast_log(LOG_DEBUG, "Missing destination protocol\n");
591                                                                 break;
592                                                         }
593                                                 } while(!res && result->numresults);
594                                         }
595                                 }
596                                 
597                         }
598                         OSPPCallIdDelete(&callid);
599                 }
600                 if (!res) {
601                         OSPPTransactionDelete(result->handle);
602                         result->handle = -1;
603                 }
604                 
605         }
606         if (!osp) 
607                 ast_log(LOG_NOTICE, "OSP Provider '%s' does not exist!\n", provider);
608         if (chan) {
609                 cres = ast_autoservice_stop(chan);
610                 if (cres < 0)
611                         return cres;
612         }
613         return res;
614 }
615
616 int ast_osp_next(struct ast_osp_result *result, int cause)
617 {
618         int res = 0;
619         int tokenlen;
620         unsigned int dummy=0;
621         unsigned int timelimit;
622         unsigned int callidlen;
623         char uniqueid[32] = "";
624         char callednum[2048]="";
625         char destination[2048]="";
626         char token[2000];
627         OSPE_DEST_PROT prot;
628
629         strcpy(result->tech, "");
630         strcpy(result->dest, "");
631         strcpy(result->token, "");
632
633         if (result->handle > -1) {
634                 dummy = 0;
635                 callidlen = sizeof(uniqueid);
636                 if (result->numresults) {
637                         tokenlen = sizeof(token);
638                         while(!res && result->numresults) {
639                                 result->numresults--;
640                                 if (!OSPPTransactionGetNextDestination(result->handle, OSPC_FAIL_INCOMPATIBLE_DEST, 0, NULL, NULL, &timelimit, &callidlen, uniqueid, 
641                                                                         sizeof(callednum), callednum, sizeof(destination), destination, 0, NULL, &tokenlen, token)) {
642                                         ast_base64encode(result->token, token, tokenlen, sizeof(result->token) - 1);
643                                         if ((strlen(destination) > 2) && !OSPPTransactionGetDestProtocol(result->handle, &prot)) {
644                                                 res = 1;
645                                                 /* Strip leading and trailing brackets */
646                                                 destination[strlen(destination) - 1] = '\0';
647                                                 switch(prot) {
648                                                 case OSPE_DEST_PROT_H323_SETUP:
649                                                         strcpy(result->tech, "H323");
650                                                         snprintf(result->dest, sizeof(result->dest), "%s@%s", callednum, destination + 1);
651                                                         break;
652                                                 case OSPE_DEST_PROT_SIP:
653                                                         strcpy(result->tech, "SIP");
654                                                         snprintf(result->dest, sizeof(result->dest), "%s@%s", callednum, destination + 1);
655                                                         break;
656                                                 default:
657                                                         ast_log(LOG_DEBUG, "Unknown destination protocol '%d', skipping...\n", prot);
658                                                         res = 0;
659                                                 }
660                                         } else {
661                                                 ast_log(LOG_DEBUG, "Missing destination protocol\n");
662                                                 break;
663                                         }
664                                 }
665                         }
666                         
667                 }
668                 if (!res) {
669                         OSPPTransactionDelete(result->handle);
670                         result->handle = -1;
671                 }
672                 
673         }
674         return res;
675 }
676
677 static enum OSPEFAILREASON cause2reason(int cause)
678 {
679         switch(cause) {
680         case AST_CAUSE_BUSY:
681                 return OSPC_FAIL_USER_BUSY;
682         case AST_CAUSE_CONGESTION:
683                 return OSPC_FAIL_SWITCHING_EQUIPMENT_CONGESTION;
684         case AST_CAUSE_UNALLOCATED:
685                 return OSPC_FAIL_UNALLOC_NUMBER;
686         case AST_CAUSE_NOTDEFINED:
687                 return OSPC_FAIL_NORMAL_UNSPECIFIED;
688         case AST_CAUSE_NOANSWER:
689                 return OSPC_FAIL_NO_ANSWER_FROM_USER;
690         case AST_CAUSE_NORMAL:
691         default:
692                 return OSPC_FAIL_NORMAL_CALL_CLEARING;
693         }
694 }
695
696 int ast_osp_terminate(int handle, int cause, time_t start, time_t duration)
697 {
698         unsigned int dummy = 0;
699         int res = -1;
700         enum OSPEFAILREASON reason;
701         reason = cause2reason(cause);
702         if (OSPPTransactionRecordFailure(handle, reason))
703                 ast_log(LOG_WARNING, "Failed to record call termination for handle %d\n", handle);
704         else if (OSPPTransactionReportUsage(handle, duration, start, 0, 0, 0, 0, &dummy, NULL))
705                 ast_log(LOG_WARNING, "Failed to report duration for handle %d\n", handle);
706         else {
707                 ast_log(LOG_DEBUG, "Completed recording handle %d\n", handle);
708                 OSPPTransactionDelete(handle);
709                 res = 0;
710         }
711         return res;
712 }
713
714 static int config_load(void)
715 {
716         struct ast_config *cfg;
717         char *cat;
718         struct osp_provider *osp, *prev = NULL, *next;
719         ast_mutex_lock(&osplock);
720         osp = providers;
721         while(osp) {
722                 osp->dead = 1;
723                 osp = osp->next;
724         }
725         ast_mutex_unlock(&osplock);
726         cfg = ast_load("osp.conf");
727         if (cfg) {
728                 if (!initialized) {
729                         cat = ast_variable_retrieve(cfg, "general", "accelerate");
730                         if (cat && ast_true(cat))
731                                 if (OSPPInit(1)) {
732                                         ast_log(LOG_WARNING, "Failed to enable hardware accelleration, falling back to software mode\n");
733                                         OSPPInit(0);
734                                 } else
735                                         hardware = 1;
736                         else
737                                 OSPPInit(0);
738                         initialized = 1;
739                 }
740                 cat = ast_category_browse(cfg, NULL);
741                 while(cat) {
742                         if (strcasecmp(cat, "general"))
743                                 osp_build(cfg, cat);
744                         cat = ast_category_browse(cfg, cat);
745                 }
746                 ast_destroy(cfg);
747         } else
748                 ast_log(LOG_NOTICE, "No OSP configuration found.  OSP support disabled\n");
749         ast_mutex_lock(&osplock);
750         osp = providers;
751         while(osp) {
752                 next = osp->next;
753                 if (osp->dead) {
754                         if (prev)
755                                 prev->next = next;
756                         else
757                                 providers = next;
758                         /* XXX Cleanup OSP structure first XXX */
759                         free(osp);
760                 } else 
761                         prev = osp;
762                 osp = next;
763         }
764         ast_mutex_unlock(&osplock);
765         return 0;
766 }
767
768 static char show_osp_usage[] = 
769 "Usage: show osp\n"
770 "       Displays information on Open Settlement Protocol\n";
771
772 static struct ast_cli_entry cli_show_osp = 
773 { { "show", "osp", NULL }, show_osp, "Displays OSP information", show_osp_usage };
774
775 int reload(void)
776 {
777         config_load();
778         ast_log(LOG_NOTICE, "XXX Should reload OSP config XXX\n");
779         return 0;
780 }
781
782 int load_module(void)
783 {
784         config_load();
785         ast_cli_register(&cli_show_osp);
786         return 0;
787 }
788
789 int unload_module(void)
790 {
791         /* Can't unload this once we're loaded */
792         return -1;
793 }
794
795 char *description(void)
796 {
797         return "Open Settlement Protocol Support";
798 }
799
800 int usecount(void)
801 {
802         /* We should never be unloaded */
803         return 1;
804 }
805
806 char *key()
807 {
808         return ASTERISK_GPL_KEY;
809 }