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