AST-2009-005
[asterisk/asterisk.git] / apps / app_osplookup.c
index 0e62e78..04da140 100644 (file)
 
 /*** MODULEINFO
        <depend>osptk</depend>
-       <depend>ssl</depend>
+       <depend>openssl</depend>
  ***/
 
 #include "asterisk.h"
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
-#include <sys/types.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-#include <errno.h>
 #include <osp/osp.h>
 #include <osp/osputils.h>
+#include <osp/ospb64.h>
 
+#include "asterisk/paths.h"
 #include "asterisk/lock.h"
 #include "asterisk/config.h"
 #include "asterisk/utils.h"
@@ -53,14 +50,165 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/app.h"
 #include "asterisk/module.h"
 #include "asterisk/pbx.h"
-#include "asterisk/options.h"
 #include "asterisk/cli.h"
-#include "asterisk/logger.h"
 #include "asterisk/astosp.h"
 
+/*** DOCUMENTATION
+       <application name="OSPAuth" language="en_US">
+               <synopsis>
+                       OSP Authentication.
+               </synopsis>
+               <syntax>
+                       <parameter name="provider" />
+                       <parameter name="options" />
+               </syntax>
+               <description>
+                       <para>Authenticate a SIP INVITE by OSP and sets the variables:</para>
+                       <variablelist>
+                               <variable name="OSPINHANDLE">
+                                       <para>The inbound call transaction handle.</para>
+                               </variable>
+                               <variable name="OSPINTIMELIMIT">
+                                       <para>The inbound call duration limit in seconds.</para>
+                               </variable>
+                       </variablelist>
+                       <para>This application sets the following channel variable upon completion:</para>
+                       <variablelist>
+                               <variable name="OSPAUTHSTATUS">
+                                       <para>The status of the OSP Auth attempt as a text string, one of</para>
+                                       <value name="SUCCESS" />
+                                       <value name="FAILED" />
+                                       <value name="ERROR" />
+                               </variable>
+                       </variablelist>
+               </description>
+       </application>
+       <application name="OSPLookup" language="en_US">
+               <synopsis>
+                       Lookup destination by OSP.
+               </synopsis>
+               <syntax>
+                       <parameter name="exten" required="true" />
+                       <parameter name="provider" />
+                       <parameter name="options">
+                               <enumlist>
+                                       <enum name="h">
+                                               <para>generate H323 call id for the outbound call</para>
+                                       </enum>
+                                       <enum name="s">
+                                               <para>generate SIP call id for the outbound call.
+                                               Have not been implemented</para>
+                                       </enum>
+                                       <enum name="i">
+                                               <para>generate IAX call id for the outbound call.
+                                               Have not been implemented</para>
+                                       </enum>
+                               </enumlist>
+                       </parameter>
+               </syntax>
+               <description>
+                       <para>Looks up an extension via OSP and sets the variables, where <literal>n</literal> is the
+                       number of the result beginning with <literal>1</literal>:</para>
+                       <variablelist>
+                               <variable name="OSPOUTHANDLE">
+                                       <para>The OSP Handle for anything remaining.</para>
+                               </variable>
+                               <variable name="OSPTECH">
+                                       <para>The technology to use for the call.</para>
+                               </variable>
+                               <variable name="OSPDEST">
+                                       <para>The destination to use for the call.</para>
+                               </variable>
+                               <variable name="OSPCALLED">
+                                       <para>The called number to use for the call.</para>
+                               </variable>
+                               <variable name="OSPCALLING">
+                                       <para>The calling number to use for the call.</para>
+                               </variable>
+                               <variable name="OSPDIALSTR">
+                                       <para>The dial command string.</para>
+                               </variable>
+                               <variable name="OSPOUTTOKEN">
+                                       <para>The actual OSP token as a string.</para>
+                               </variable>
+                               <variable name="OSPOUTTIMELIMIT">
+                                       <para>The outbound call duraction limit in seconds.</para>
+                               </variable>
+                               <variable name="OSPOUTCALLIDTYPES">
+                                       <para>The outbound call id types.</para>
+                               </variable>
+                               <variable name="OSPOUTCALLID">
+                                       <para>The outbound call id.</para>
+                               </variable>
+                               <variable name="OSPRESULTS">
+                                       <para>The number of OSP results total remaining.</para>
+                               </variable>
+                       </variablelist>
+                       <variablelist>
+                               <variable name="OSPLOOKUPSTATUS">
+                                       <para>This application sets the following channel variable upon completion:</para>
+                                       <value name="SUCCESS" />
+                                       <value name="FAILED" />
+                                       <value name="ERROR" />
+                               </variable>
+                       </variablelist>
+               </description>
+       </application>
+       <application name="OSPNext" language="en_US">
+               <synopsis>
+                       Lookup next destination by OSP.
+               </synopsis>
+               <syntax>
+                       <parameter name="cause" required="true" />
+                       <parameter name="provider" />
+                       <parameter name="options" />
+               </syntax>
+               <description>
+                       <para>Looks up the next OSP Destination for <variable>OSPOUTHANDLE</variable>.</para>
+                       <para>This application sets the following channel variable upon completion:</para>
+                       <variablelist>
+                               <variable name="OSPNEXTSTATUS">
+                                       <para>The status of the OSP Next attempt as a text string, one of</para>
+                                       <value name="SUCCESS" />
+                                       <value name="FAILED" />
+                                       <value name="ERROR" />
+                               </variable>
+                       </variablelist>
+               </description>
+               <see-also>
+                       <ref type="application">OSPLookup</ref>
+               </see-also>
+       </application>
+       <application name="OSPFinish" language="en_US">
+               <synopsis>
+                       Record OSP entry.
+               </synopsis>
+               <syntax>
+                       <parameter name="status" />
+                       <parameter name="options" />
+               </syntax>
+               <description>
+                       <para>Records call state for <variable>OSPINHANDLE</variable>, according to status, which should
+                       be one of <literal>BUSY</literal>, <literal>CONGESTION</literal>, <literal>ANSWER</literal>,
+                       <literal>NOANSWER</literal>, or <literal>CHANUNAVAIL</literal> or coincidentally, just what the
+                       Dial application stores in its <variable>DIALSTATUS</variable>.</para>
+                       <para>This application sets the following channel variable upon completion:</para>
+                       <variablelist>
+                               <variable name="OSPFINISHSTATUS">
+                                       <para>The status of the OSP Finish attempt as a text string, one of</para>
+                                       <value name="SUCCESS" />
+                                       <value name="FAILED" />
+                                       <value name="ERROR" />
+                               </variable>
+                       </variablelist>
+               </description>
+       </application>
+ ***/
+
 /* OSP Buffer Sizes */
 #define OSP_INTSTR_SIZE                ((unsigned int)16)              /* OSP signed/unsigned int string buffer size */
 #define OSP_NORSTR_SIZE                ((unsigned int)256)             /* OSP normal string buffer size */
+#define OSP_KEYSTR_SIZE                ((unsigned int)1024)    /* OSP certificate string buffer size */
 #define OSP_TOKSTR_SIZE                ((unsigned int)4096)    /* OSP token string buffer size */
 #define OSP_TECHSTR_SIZE       ((unsigned int)32)              /* OSP signed/unsigned int string buffer size */
 #define OSP_UUID_SIZE          ((unsigned int)16)              /* UUID size */
@@ -133,7 +281,7 @@ struct osp_provider {
        char privatekey[OSP_NORSTR_SIZE];                               /* OSP private key file name */
        char localcert[OSP_NORSTR_SIZE];                                /* OSP local cert file name */
        unsigned int cacount;                                                   /* Number of cacerts */
-       char cacerts[OSP_MAX_CERTS][OSP_NORSTR_SIZE];   /* Cacert file names */
+       char cacerts[OSP_MAX_CERTS][OSP_NORSTR_SIZE];   /* Cacert file names */
        unsigned int spcount;                                                   /* Number of service points */
        char srvpoints[OSP_MAX_SRVS][OSP_NORSTR_SIZE];  /* Service point URLs */
        int maxconnections;                                                             /* Max number of connections */
@@ -150,7 +298,7 @@ struct osp_provider {
 /* Call ID */
 struct osp_callid {
        unsigned char buf[OSPC_CALLID_MAXSIZE];         /* Call ID string */
-       unsigned int len;                                       /* Call ID length */
+       unsigned int len;                                                       /* Call ID length */
 };
 
 /* OSP Application In/Output Results */
@@ -173,9 +321,15 @@ struct osp_result {
 AST_MUTEX_DEFINE_STATIC(osplock);                                                      /* Lock of OSP provider list */
 static int osp_initialized = 0;                                                                /* Init flag */
 static int osp_hardware = 0;                                                           /* Hardware accelleration flag */
+static int osp_security = 0;                                                           /* Using security features flag */
 static struct osp_provider* ospproviders = NULL;                       /* OSP provider list */
 static unsigned int osp_tokenformat = TOKEN_ALGO_SIGNED;       /* Token format supported */
 
+/* OSP default certificates */
+const char* B64PKey = "MIIBOgIBAAJBAK8t5l+PUbTC4lvwlNxV5lpl+2dwSZGW46dowTe6y133XyVEwNiiRma2YNk3xKs/TJ3Wl9Wpns2SYEAJsFfSTukCAwEAAQJAPz13vCm2GmZ8Zyp74usTxLCqSJZNyMRLHQWBM0g44Iuy4wE3vpi7Wq+xYuSOH2mu4OddnxswCP4QhaXVQavTAQIhAOBVCKXtppEw9UaOBL4vW0Ed/6EA/1D8hDW6St0h7EXJAiEAx+iRmZKhJD6VT84dtX5ZYNVk3j3dAcIOovpzUj9a0CECIEduTCapmZQ5xqAEsLXuVlxRtQgLTUD4ZxDElPn8x0MhAiBE2HlcND0+qDbvtwJQQOUzDgqg5xk3w8capboVdzAlQQIhAMC+lDL7+gDYkNAft5Mu+NObJmQs4Cr+DkDFsKqoxqrm";
+const char* B64LCert = "MIIBeTCCASMCEHqkOHVRRWr+1COq3CR/xsowDQYJKoZIhvcNAQEEBQAwOzElMCMGA1UEAxMcb3NwdGVzdHNlcnZlci50cmFuc25leHVzLmNvbTESMBAGA1UEChMJT1NQU2VydmVyMB4XDTA1MDYyMzAwMjkxOFoXDTA2MDYyNDAwMjkxOFowRTELMAkGA1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUxITAfBgNVBAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQCvLeZfj1G0wuJb8JTcVeZaZftncEmRluOnaME3ustd918lRMDYokZmtmDZN8SrP0yd1pfVqZ7NkmBACbBX0k7pAgMBAAEwDQYJKoZIhvcNAQEEBQADQQDnV8QNFVVJx/+7IselU0wsepqMurivXZzuxOmTEmTVDzCJx1xhA8jd3vGAj7XDIYiPub1PV23eY5a2ARJuw5w9";
+const char* B64CACert = "MIIBYDCCAQoCAQEwDQYJKoZIhvcNAQEEBQAwOzElMCMGA1UEAxMcb3NwdGVzdHNlcnZlci50cmFuc25leHVzLmNvbTESMBAGA1UEChMJT1NQU2VydmVyMB4XDTAyMDIwNDE4MjU1MloXDTEyMDIwMzE4MjU1MlowOzElMCMGA1UEAxMcb3NwdGVzdHNlcnZlci50cmFuc25leHVzLmNvbTESMBAGA1UEChMJT1NQU2VydmVyMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAPGeGwV41EIhX0jEDFLRXQhDEr50OUQPq+f55VwQd0TQNts06BP29+UiNdRW3c3IRHdZcJdC1Cg68ME9cgeq0h8CAwEAATANBgkqhkiG9w0BAQQFAANBAGkzBSj1EnnmUxbaiG1N4xjIuLAWydun7o3bFk2tV8dBIhnuh445obYyk1EnQ27kI7eACCILBZqi2MHDOIMnoN0=";
+
 /* OSP Client Wrapper APIs */
 
 /*!
@@ -188,25 +342,29 @@ static int osp_create_provider(
        struct ast_config* cfg,
        const char* provider)
 {
-       int res;
-       unsigned int t, i, j;
-       struct osp_provider* p;
+       int res = 0;
        struct ast_variable* v;
+       struct osp_provider* p;
        OSPTPRIVATEKEY privatekey;
-       OSPTCERT localcert;
+       OSPT_CERT localcert;
+       OSPT_CERT cacerts[OSP_MAX_CERTS];
+       const OSPT_CERT* pcacerts[OSP_MAX_CERTS];
        const char* psrvpoints[OSP_MAX_SRVS];
-       OSPTCERT cacerts[OSP_MAX_CERTS];
-       const OSPTCERT* pcacerts[OSP_MAX_CERTS];
-       int error = OSPC_ERR_NO_ERROR;
+       unsigned char privatekeydata[OSP_KEYSTR_SIZE];
+       unsigned char localcertdata[OSP_KEYSTR_SIZE];
+       unsigned char cacertdata[OSP_KEYSTR_SIZE];
+       int i, t, error = OSPC_ERR_NO_ERROR;
 
        if (!(p = ast_calloc(1, sizeof(*p)))) {
                ast_log(LOG_ERROR, "Out of memory\n");
                return -1;
        }
 
+       /* ast_calloc has set 0 in p */
        ast_copy_string(p->name, provider, sizeof(p->name));
        snprintf(p->privatekey, sizeof(p->privatekey), "%s/%s-privatekey.pem", ast_config_AST_KEY_DIR, provider);
        snprintf(p->localcert, sizeof(p->localcert), "%s/%s-localcert.pem", ast_config_AST_KEY_DIR, provider);
+       snprintf(p->cacerts[0], sizeof(p->cacerts[0]), "%s/%s-cacert_0.pem", ast_config_AST_KEY_DIR, provider);
        p->maxconnections = OSP_DEF_MAXCONNECTIONS;
        p->retrydelay = OSP_DEF_RETRYDELAY;
        p->retrylimit = OSP_DEF_RETRYLIMIT;
@@ -218,88 +376,84 @@ static int osp_create_provider(
        v = ast_variable_browse(cfg, provider);
        while(v) {
                if (!strcasecmp(v->name, "privatekey")) {
-                       if (v->value[0] == '/') {
-                               ast_copy_string(p->privatekey, v->value, sizeof(p->privatekey));
-                       } else {
-                               snprintf(p->privatekey, sizeof(p->privatekey), "%s/%s", ast_config_AST_KEY_DIR, v->value);
+                       if (osp_security) {
+                               if (v->value[0] == '/') {
+                                       ast_copy_string(p->privatekey, v->value, sizeof(p->privatekey));
+                               } else {
+                                       snprintf(p->privatekey, sizeof(p->privatekey), "%s/%s", ast_config_AST_KEY_DIR, v->value);
+                               }
+                               ast_debug(1, "OSP: privatekey '%s'\n", p->privatekey);
                        }
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "OSP: privatekey '%s'\n", p->privatekey);
                } else if (!strcasecmp(v->name, "localcert")) {
-                       if (v->value[0] == '/') {
-                               ast_copy_string(p->localcert, v->value, sizeof(p->localcert));
-                       } else {
-                               snprintf(p->localcert, sizeof(p->localcert), "%s/%s", ast_config_AST_KEY_DIR, v->value);
+                       if (osp_security) {
+                               if (v->value[0] == '/') {
+                                       ast_copy_string(p->localcert, v->value, sizeof(p->localcert));
+                               } else {
+                                       snprintf(p->localcert, sizeof(p->localcert), "%s/%s", ast_config_AST_KEY_DIR, v->value);
+                               }
+                               ast_debug(1, "OSP: localcert '%s'\n", p->localcert);
                        }
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "OSP: localcert '%s'\n", p->localcert);
                } else if (!strcasecmp(v->name, "cacert")) {
-                       if (p->cacount < OSP_MAX_CERTS) {
-                               if (v->value[0] == '/') {
-                                       ast_copy_string(p->cacerts[p->cacount], v->value, sizeof(p->cacerts[0]));
+                       if (osp_security) {
+                               if (p->cacount < OSP_MAX_CERTS) {
+                                       if (v->value[0] == '/') {
+                                               ast_copy_string(p->cacerts[p->cacount], v->value, sizeof(p->cacerts[0]));
+                                       } else {
+                                               snprintf(p->cacerts[p->cacount], sizeof(p->cacerts[0]), "%s/%s", ast_config_AST_KEY_DIR, v->value);
+                                       }
+                                       ast_debug(1, "OSP: cacerts[%d]: '%s'\n", p->cacount, p->cacerts[p->cacount]);
+                                       p->cacount++;
                                } else {
-                                       snprintf(p->cacerts[p->cacount], sizeof(p->cacerts[0]), "%s/%s", ast_config_AST_KEY_DIR, v->value);
+                                       ast_log(LOG_WARNING, "OSP: Too many CA Certificates at line %d\n", v->lineno);
                                }
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: cacert[%d]: '%s'\n", p->cacount, p->cacerts[p->cacount]);
-                               p->cacount++;
-                       } else {
-                               ast_log(LOG_WARNING, "OSP: Too many CA Certificates at line %d\n", v->lineno);
                        }
                } else if (!strcasecmp(v->name, "servicepoint")) {
                        if (p->spcount < OSP_MAX_SRVS) {
                                ast_copy_string(p->srvpoints[p->spcount], v->value, sizeof(p->srvpoints[0]));
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: servicepoint[%d]: '%s'\n", p->spcount, p->srvpoints[p->spcount]);
+                               ast_debug(1, "OSP: servicepoint[%d]: '%s'\n", p->spcount, p->srvpoints[p->spcount]);
                                p->spcount++;
                        } else {
                                ast_log(LOG_WARNING, "OSP: Too many Service Points at line %d\n", v->lineno);
                        }
                } else if (!strcasecmp(v->name, "maxconnections")) {
-                       if ((sscanf(v->value, "%d", &t) == 1) && (t >= OSP_MIN_MAXCONNECTIONS) && (t <= OSP_MAX_MAXCONNECTIONS)) {
+                       if ((sscanf(v->value, "%30d", &t) == 1) && (t >= OSP_MIN_MAXCONNECTIONS) && (t <= OSP_MAX_MAXCONNECTIONS)) {
                                p->maxconnections = t;
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: maxconnections '%d'\n", t);
+                               ast_debug(1, "OSP: maxconnections '%d'\n", t);
                        } else {
                                ast_log(LOG_WARNING, "OSP: maxconnections should be an integer from %d to %d, not '%s' at line %d\n",
                                        OSP_MIN_MAXCONNECTIONS, OSP_MAX_MAXCONNECTIONS, v->value, v->lineno);
                        }
                } else if (!strcasecmp(v->name, "retrydelay")) {
-                       if ((sscanf(v->value, "%d", &t) == 1) && (t >= OSP_MIN_RETRYDELAY) && (t <= OSP_MAX_RETRYDELAY)) {
+                       if ((sscanf(v->value, "%30d", &t) == 1) && (t >= OSP_MIN_RETRYDELAY) && (t <= OSP_MAX_RETRYDELAY)) {
                                p->retrydelay = t;
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: retrydelay '%d'\n", t);
+                               ast_debug(1, "OSP: retrydelay '%d'\n", t);
                        } else {
                                ast_log(LOG_WARNING, "OSP: retrydelay should be an integer from %d to %d, not '%s' at line %d\n",
                                        OSP_MIN_RETRYDELAY, OSP_MAX_RETRYDELAY, v->value, v->lineno);
                        }
                } else if (!strcasecmp(v->name, "retrylimit")) {
-                       if ((sscanf(v->value, "%d", &t) == 1) && (t >= OSP_MIN_RETRYLIMIT) && (t <= OSP_MAX_RETRYLIMIT)) {
+                       if ((sscanf(v->value, "%30d", &t) == 1) && (t >= OSP_MIN_RETRYLIMIT) && (t <= OSP_MAX_RETRYLIMIT)) {
                                p->retrylimit = t;
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: retrylimit '%d'\n", t);
+                               ast_debug(1, "OSP: retrylimit '%d'\n", t);
                        } else {
                                ast_log(LOG_WARNING, "OSP: retrylimit should be an integer from %d to %d, not '%s' at line %d\n",
                                        OSP_MIN_RETRYLIMIT, OSP_MAX_RETRYLIMIT, v->value, v->lineno);
                        }
                } else if (!strcasecmp(v->name, "timeout")) {
-                       if ((sscanf(v->value, "%d", &t) == 1) && (t >= OSP_MIN_TIMEOUT) && (t <= OSP_MAX_TIMEOUT)) {
+                       if ((sscanf(v->value, "%30d", &t) == 1) && (t >= OSP_MIN_TIMEOUT) && (t <= OSP_MAX_TIMEOUT)) {
                                p->timeout = t;
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: timeout '%d'\n", t);
+                               ast_debug(1, "OSP: timeout '%d'\n", t);
                        } else {
                                ast_log(LOG_WARNING, "OSP: timeout should be an integer from %d to %d, not '%s' at line %d\n",
                                        OSP_MIN_TIMEOUT, OSP_MAX_TIMEOUT, v->value, v->lineno);
                        }
                } else if (!strcasecmp(v->name, "source")) {
                        ast_copy_string(p->source, v->value, sizeof(p->source));
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "OSP: source '%s'\n", p->source);
+                       ast_debug(1, "OSP: source '%s'\n", p->source);
                } else if (!strcasecmp(v->name, "authpolicy")) {
-                       if ((sscanf(v->value, "%d", &t) == 1) && ((t == OSP_AUTH_NO) || (t == OSP_AUTH_YES) || (t == OSP_AUTH_EXCLUSIVE))) {
+                       if ((sscanf(v->value, "%30d", &t) == 1) && ((t == OSP_AUTH_NO) || (t == OSP_AUTH_YES) || (t == OSP_AUTH_EXCLUSIVE))) {
                                p->authpolicy = t;
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: authpolicy '%d'\n", t);
+                               ast_debug(1, "OSP: authpolicy '%d'\n", t);
                        } else {
                                ast_log(LOG_WARNING, "OSP: authpolicy should be %d, %d or %d, not '%s' at line %d\n",
                                        OSP_AUTH_NO, OSP_AUTH_YES, OSP_AUTH_EXCLUSIVE, v->value, v->lineno);
@@ -307,16 +461,13 @@ static int osp_create_provider(
                } else if (!strcasecmp(v->name, "defaultprotocol")) {
                        if (!strcasecmp(v->value, OSP_PROT_SIP)) {
                                p->defaultprotocol = OSP_PROT_SIP;
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: default protocol '%s'\n", p->defaultprotocol);
+                               ast_debug(1, "OSP: default protocol '%s'\n", p->defaultprotocol);
                        } else if (!strcasecmp(v->value, OSP_PROT_H323)) {
                                p->defaultprotocol = OSP_PROT_H323;
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: default protocol '%s'\n", p->defaultprotocol);
+                               ast_debug(1, "OSP: default protocol '%s'\n", p->defaultprotocol);
                        } else if (!strcasecmp(v->value, OSP_PROT_IAX)) {
                                p->defaultprotocol = OSP_PROT_IAX;
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: default protocol '%s'\n", p->defaultprotocol);
+                               ast_debug(1, "OSP: default protocol '%s'\n", p->defaultprotocol);
                        } else {
                                ast_log(LOG_WARNING, "OSP: default protocol should be %s, %s, %s, or %s not '%s' at line %d\n",
                                        OSP_PROT_SIP, OSP_PROT_H323, OSP_PROT_IAX, OSP_PROT_OTHER, v->value, v->lineno);
@@ -325,97 +476,109 @@ static int osp_create_provider(
                v = v->next;
        }
 
-       error = OSPPUtilLoadPEMPrivateKey((unsigned char*)p->privatekey, &privatekey);
-       if (error != OSPC_ERR_NO_ERROR) {
-               ast_log(LOG_WARNING, "OSP: Unable to load privatekey '%s', error '%d'\n", p->privatekey, error);
-               ast_free(p);
-               return 0;
+       if (p->cacount == 0) {
+               p->cacount = 1;
        }
 
-       error = OSPPUtilLoadPEMCert((unsigned char*)p->localcert, &localcert);
-       if (error != OSPC_ERR_NO_ERROR) {
-               ast_log(LOG_WARNING, "OSP: Unable to load localcert '%s', error '%d'\n", p->localcert, error);
-               if (privatekey.PrivateKeyData) {
-                       ast_free(privatekey.PrivateKeyData);
-               }
-               ast_free(p);
-               return 0;
+       for (i = 0; i < p->spcount; i++) {
+               psrvpoints[i] = p->srvpoints[i];
        }
 
-       if (p->cacount < 1) {
-               snprintf(p->cacerts[p->cacount], sizeof(p->cacerts[0]), "%s/%s-cacert.pem", ast_config_AST_KEY_DIR, provider);
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: cacert[%d]: '%s'\n", p->cacount, p->cacerts[p->cacount]);
-               p->cacount++;
-       }
-       for (i = 0; i < p->cacount; i++) {
-               error = OSPPUtilLoadPEMCert((unsigned char*)p->cacerts[i], &cacerts[i]);
-               if (error != OSPC_ERR_NO_ERROR) {
-                       ast_log(LOG_WARNING, "OSP: Unable to load cacert '%s', error '%d'\n", p->cacerts[i], error);
-                       for (j = 0; j < i; j++) {
-                               if (cacerts[j].CertData) {
-                                       ast_free(cacerts[j].CertData);
+       if (osp_security) {
+               privatekey.PrivateKeyData = NULL;
+               privatekey.PrivateKeyLength = 0;
+
+               localcert.CertData = NULL;
+               localcert.CertDataLength = 0;
+
+               for (i = 0; i < p->cacount; i++) {
+                       cacerts[i].CertData = NULL;
+                       cacerts[i].CertDataLength = 0;
+               }
+
+               if ((error = OSPPUtilLoadPEMPrivateKey((unsigned char*)p->privatekey, &privatekey)) != OSPC_ERR_NO_ERROR) {
+                       ast_log(LOG_WARNING, "OSP: Unable to load privatekey '%s', error '%d'\n", p->privatekey, error);
+               } else if ((error = OSPPUtilLoadPEMCert((unsigned char*)p->localcert, &localcert)) != OSPC_ERR_NO_ERROR) {
+                       ast_log(LOG_WARNING, "OSP: Unable to load localcert '%s', error '%d'\n", p->localcert, error);
+               } else {
+                       for (i = 0; i < p->cacount; i++) {
+                               if ((error = OSPPUtilLoadPEMCert((unsigned char*)p->cacerts[i], &cacerts[i])) != OSPC_ERR_NO_ERROR) {
+                                       ast_log(LOG_WARNING, "OSP: Unable to load cacert '%s', error '%d'\n", p->cacerts[i], error);
+                                       break;
+                               } else {
+                                       pcacerts[i] = &cacerts[i];
                                }
                        }
-                       if (localcert.CertData) {
-                               ast_free(localcert.CertData);
-                       }
-                       if (privatekey.PrivateKeyData) {
-                               ast_free(privatekey.PrivateKeyData);
-                       }
-                       ast_free(p);
-                       return 0;
                }
-               pcacerts[i] = &cacerts[i];
-       }
-
-       for (i = 0; i < p->spcount; i++) {
-               psrvpoints[i] = p->srvpoints[i];
-       }
-
-       error = OSPPProviderNew(
-                               p->spcount,
-                               psrvpoints,
-                               NULL,
-                               OSP_AUDIT_URL,
-                               &privatekey,
-                               &localcert,
-                               p->cacount,
-                               pcacerts,
-                               OSP_LOCAL_VALIDATION,
-                               OSP_SSL_LIFETIME,
-                               p->maxconnections,
-                               OSP_HTTP_PERSISTENCE,
-                               p->retrydelay,
-                               p->retrylimit,
-                               p->timeout,
-                               OSP_CUSTOMER_ID,
-                               OSP_DEVICE_ID,
-                               &p->handle);
-       if (error != OSPC_ERR_NO_ERROR) {
-               ast_log(LOG_WARNING, "OSP: Unable to create provider '%s', error '%d'\n", provider, error);
-               ast_free(p);
-               res = -1;
        } else {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: provider '%s'\n", provider);
-               ast_mutex_lock(&osplock);
-               p->next = ospproviders;
-               ospproviders = p;
-               ast_mutex_unlock(&osplock);
-               res = 1;
+               privatekey.PrivateKeyData = privatekeydata;
+               privatekey.PrivateKeyLength = sizeof(privatekeydata);
+
+               localcert.CertData = localcertdata;
+               localcert.CertDataLength = sizeof(localcertdata);
+
+               cacerts[0].CertData = cacertdata;
+               cacerts[0].CertDataLength = sizeof(cacertdata);
+               pcacerts[0] = &cacerts[0];
+
+               if ((error = OSPPBase64Decode(B64PKey, strlen(B64PKey), privatekey.PrivateKeyData, &privatekey.PrivateKeyLength)) != OSPC_ERR_NO_ERROR) {
+                       ast_log(LOG_WARNING, "OSP: Unable to decode private key, error '%d'\n", error);
+               } else if ((error = OSPPBase64Decode(B64LCert, strlen(B64LCert), localcert.CertData, &localcert.CertDataLength)) != OSPC_ERR_NO_ERROR) {
+                       ast_log(LOG_WARNING, "OSP: Unable to decode local cert, error '%d'\n", error);
+               } else if ((error = OSPPBase64Decode(B64CACert, strlen(B64CACert), cacerts[0].CertData, &cacerts[0].CertDataLength)) != OSPC_ERR_NO_ERROR) {
+                       ast_log(LOG_WARNING, "OSP: Unable to decode cacert, error '%d'\n", error);
+               }
        }
 
-       for (i = 0; i < p->cacount; i++) {
-               if (cacerts[i].CertData) {
-                       ast_free(cacerts[i].CertData);
+       if (error == OSPC_ERR_NO_ERROR) {
+               error = OSPPProviderNew(
+                       p->spcount,
+                       psrvpoints,
+                       NULL,
+                       OSP_AUDIT_URL,
+                       &privatekey,
+                       &localcert,
+                       p->cacount,
+                       pcacerts,
+                       OSP_LOCAL_VALIDATION,
+                       OSP_SSL_LIFETIME,
+                       p->maxconnections,
+                       OSP_HTTP_PERSISTENCE,
+                       p->retrydelay,
+                       p->retrylimit,
+                       p->timeout,
+                       OSP_CUSTOMER_ID,
+                       OSP_DEVICE_ID,
+                       &p->handle);
+               if (error != OSPC_ERR_NO_ERROR) {
+                       ast_log(LOG_WARNING, "OSP: Unable to create provider '%s', error '%d'\n", provider, error);
+                       res = -1;
+               } else {
+                       ast_debug(1, "OSP: provider '%s'\n", provider);
+                       ast_mutex_lock(&osplock);
+                       p->next = ospproviders;
+                       ospproviders = p;
+                       ast_mutex_unlock(&osplock);
+                       res = 1;
                }
        }
-       if (localcert.CertData) {
-               ast_free(localcert.CertData);
+
+       if (osp_security) {
+               for (i = 0; i < p->cacount; i++) {
+                       if (cacerts[i].CertData) {
+                               ast_free(cacerts[i].CertData);
+                       }
+               }
+               if (localcert.CertData) {
+                       ast_free(localcert.CertData);
+               }
+               if (privatekey.PrivateKeyData) {
+                       ast_free(privatekey.PrivateKeyData);
+               }
        }
-       if (privatekey.PrivateKeyData) {
-               ast_free(privatekey.PrivateKeyData);
+
+       if (res != 1) {
+               ast_free(p);
        }
 
        return res;
@@ -439,8 +602,7 @@ static int osp_get_provider(
        while(p) {
                if (!strcasecmp(p->name, name)) {
                        *provider = p;
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "OSP: find provider '%s'\n", name);
+                       ast_debug(1, "OSP: find provider '%s'\n", name);
                        res = 1;
                        break;
                }
@@ -475,16 +637,13 @@ static int osp_create_transaction(
                if (!strcasecmp(p->name, provider)) {
                        error = OSPPTransactionNew(p->handle, transaction);
                        if (error == OSPC_ERR_NO_ERROR) {
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: transaction '%d'\n", *transaction);
+                               ast_debug(1, "OSP: transaction '%d'\n", *transaction);
                                ast_copy_string(source, p->source, sourcesize);
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: source '%s'\n", source);
+                               ast_debug(1, "OSP: source '%s'\n", source);
                                res = 1;
                        } else {
                                *transaction = OSP_INVALID_HANDLE;
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: Unable to create transaction handle, error '%d'\n", error);
+                               ast_debug(1, "OSP: Unable to create transaction handle, error '%d'\n", error);
                                res = -1;
                        }
                        break;
@@ -539,8 +698,8 @@ static int osp_validate_token(
        int res;
        int tokenlen;
        unsigned char tokenstr[OSP_TOKSTR_SIZE];
-       char src[OSP_TOKSTR_SIZE];
-       char dst[OSP_TOKSTR_SIZE];
+       char src[OSP_NORSTR_SIZE];
+       char dst[OSP_NORSTR_SIZE];
        unsigned int authorised;
        unsigned int dummy = 0;
        int error;
@@ -549,35 +708,32 @@ static int osp_validate_token(
        osp_convert_address(source, src, sizeof(src));
        osp_convert_address(destination, dst, sizeof(dst));
        error = OSPPTransactionValidateAuthorisation(
-                               transaction,
-                               src,
-                               dst,
-                               NULL,
-                               NULL,
-                               calling ? calling : "",
-                               OSPC_E164,
-                               called,
-                               OSPC_E164,
-                               0,
-                               NULL,
-                               tokenlen,
-                               (char*)tokenstr,
-                               &authorised,
-                               timelimit,
-                               &dummy,
-                               NULL,
-                               osp_tokenformat);
+               transaction,
+               src,
+               dst,
+               NULL,
+               NULL,
+               calling ? calling : "",
+               OSPC_NFORMAT_E164,
+               called,
+               OSPC_NFORMAT_E164,
+               0,
+               NULL,
+               tokenlen,
+               (char*)tokenstr,
+               &authorised,
+               timelimit,
+               &dummy,
+               NULL,
+               osp_tokenformat);
        if (error != OSPC_ERR_NO_ERROR) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Unable to validate inbound token\n");
+               ast_debug(1, "OSP: Unable to validate inbound token, error '%d'\n", error);
                res = -1;
        } else if (authorised) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Authorised\n");
+               ast_debug(1, "OSP: Authorised\n");
                res = 1;
        } else {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Unauthorised\n");
+               ast_debug(1, "OSP: Unauthorised\n");
                res = 0;
        }
 
@@ -622,43 +778,39 @@ static int osp_check_destination(
        char* destination,
        unsigned int tokenlen,
        const char* token,
-       enum OSPEFAILREASON* reason,
+       OSPEFAILREASON* reason,
        struct osp_result* result)
 {
        int res;
-       OSPE_DEST_OSP_ENABLED enabled;
-       OSPE_DEST_PROT protocol;
+       OSPE_DEST_OSPENABLED enabled;
+       OSPE_DEST_PROTOCOL protocol;
        int error;
 
        if (strlen(destination) <= 2) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Wrong destination format '%s'\n", destination);
+               ast_debug(1, "OSP: Wrong destination format '%s'\n", destination);
                *reason = OSPC_FAIL_NORMAL_UNSPECIFIED;
                return -1;
        }
 
        if ((error = OSPPTransactionIsDestOSPEnabled(result->outhandle, &enabled)) != OSPC_ERR_NO_ERROR) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Unable to get destination OSP version, error '%d'\n", error);
+               ast_debug(1, "OSP: Unable to get destination OSP version, error '%d'\n", error);
                *reason = OSPC_FAIL_NORMAL_UNSPECIFIED;
                return -1;
        }
 
-       if (enabled == OSPE_OSP_FALSE) {
+       if (enabled == OSPC_DOSP_FALSE) {
                result->token[0] = '\0';
        } else {
                ast_base64encode(result->token, (const unsigned char*)token, tokenlen, sizeof(result->token) - 1);
        }
 
        if ((error = OSPPTransactionGetDestNetworkId(result->outhandle, result->networkid)) != OSPC_ERR_NO_ERROR) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Unable to get destination network ID, error '%d'\n", error);
+               ast_debug(1, "OSP: Unable to get destination network ID, error '%d'\n", error);
                result->networkid[0] = '\0';
        }
 
        if ((error = OSPPTransactionGetDestProtocol(result->outhandle, &protocol)) != OSPC_ERR_NO_ERROR) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Unable to get destination protocol, error '%d'\n", error);
+               ast_debug(1, "OSP: Unable to get destination protocol, error '%d'\n", error);
                *reason = OSPC_FAIL_NORMAL_UNSPECIFIED;
                result->token[0] = '\0';
                result->networkid[0] = '\0';
@@ -669,49 +821,45 @@ static int osp_check_destination(
        /* Strip leading and trailing brackets */
        destination[strlen(destination) - 1] = '\0';
        switch(protocol) {
-               case OSPE_DEST_PROT_H323_SETUP:
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "OSP: protocol '%s'\n", OSP_PROT_H323);
-                       ast_copy_string(result->tech, OSP_TECH_H323, sizeof(result->tech));
-                       ast_copy_string(result->dest, destination + 1, sizeof(result->dest));
-                       ast_copy_string(result->called, called, sizeof(result->called));
-                       ast_copy_string(result->calling, calling, sizeof(result->calling));
-                       break;
-               case OSPE_DEST_PROT_SIP:
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "OSP: protocol '%s'\n", OSP_PROT_SIP);
-                       ast_copy_string(result->tech, OSP_TECH_SIP, sizeof(result->tech));
-                       ast_copy_string(result->dest, destination + 1, sizeof(result->dest));
-                       ast_copy_string(result->called, called, sizeof(result->called));
-                       ast_copy_string(result->calling, calling, sizeof(result->calling));
-                       break;
-               case OSPE_DEST_PROT_IAX:
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "OSP: protocol '%s'\n", OSP_PROT_IAX);
-                       ast_copy_string(result->tech, OSP_TECH_IAX, sizeof(result->tech));
-                       ast_copy_string(result->dest, destination + 1, sizeof(result->dest));
-                       ast_copy_string(result->called, called, sizeof(result->called));
-                       ast_copy_string(result->calling, calling, sizeof(result->calling));
-                       break;
-               case OSPE_DEST_PROT_UNDEFINED:
-               case OSPE_DEST_PROT_UNKNOWN:
-                       if (option_debug) {
-                               ast_log(LOG_DEBUG, "OSP: unknown/undefined protocol '%d'\n", protocol);
-                               ast_log(LOG_DEBUG, "OSP: use default protocol '%s'\n", provider->defaultprotocol);
-                       }
-                       ast_copy_string(result->tech, provider->defaultprotocol, sizeof(result->tech));
-                       ast_copy_string(result->dest, destination + 1, sizeof(result->dest));
-                       ast_copy_string(result->called, called, sizeof(result->called));
-                       ast_copy_string(result->calling, calling, sizeof(result->calling));
-                       break;
-               case OSPE_DEST_PROT_H323_LRQ:
-               default:
-                       ast_log(LOG_WARNING, "OSP: unsupported protocol '%d'\n", protocol);
-                       *reason = OSPC_FAIL_PROTOCOL_ERROR;
-                       result->token[0] = '\0';
-                       result->networkid[0] = '\0';
-                       res = 0;
-                       break;
+       case OSPC_DPROT_Q931:
+               ast_debug(1, "OSP: protocol '%s'\n", OSP_PROT_H323);
+               ast_copy_string(result->tech, OSP_TECH_H323, sizeof(result->tech));
+               ast_copy_string(result->dest, destination + 1, sizeof(result->dest));
+               ast_copy_string(result->called, called, sizeof(result->called));
+               ast_copy_string(result->calling, calling, sizeof(result->calling));
+               break;
+       case OSPC_DPROT_SIP:
+               ast_debug(1, "OSP: protocol '%s'\n", OSP_PROT_SIP);
+               ast_copy_string(result->tech, OSP_TECH_SIP, sizeof(result->tech));
+               ast_copy_string(result->dest, destination + 1, sizeof(result->dest));
+               ast_copy_string(result->called, called, sizeof(result->called));
+               ast_copy_string(result->calling, calling, sizeof(result->calling));
+               break;
+       case OSPC_DPROT_IAX:
+               ast_debug(1, "OSP: protocol '%s'\n", OSP_PROT_IAX);
+               ast_copy_string(result->tech, OSP_TECH_IAX, sizeof(result->tech));
+               ast_copy_string(result->dest, destination + 1, sizeof(result->dest));
+               ast_copy_string(result->called, called, sizeof(result->called));
+               ast_copy_string(result->calling, calling, sizeof(result->calling));
+               break;
+       case OSPC_DPROT_UNDEFINED:
+       case OSPC_DPROT_UNKNOWN:
+               ast_debug(1, "OSP: unknown/undefined protocol '%d'\n", protocol);
+               ast_debug(1, "OSP: use default protocol '%s'\n", provider->defaultprotocol);
+
+               ast_copy_string(result->tech, provider->defaultprotocol, sizeof(result->tech));
+               ast_copy_string(result->dest, destination + 1, sizeof(result->dest));
+               ast_copy_string(result->called, called, sizeof(result->called));
+               ast_copy_string(result->calling, calling, sizeof(result->calling));
+               break;
+       case OSPC_DPROT_LRQ:
+       default:
+               ast_log(LOG_WARNING, "OSP: unsupported protocol '%d'\n", protocol);
+               *reason = OSPC_FAIL_PROTOCOL_ERROR;
+               result->token[0] = '\0';
+               result->networkid[0] = '\0';
+               res = 0;
+               break;
        }
 
        return res;
@@ -722,10 +870,10 @@ static int osp_check_destination(
  * \param cause Asterisk hangup cause
  * \return OSP TC code
  */
-static enum OSPEFAILREASON asterisk2osp(
+static OSPEFAILREASON asterisk2osp(
        int cause)
 {
-       return (enum OSPEFAILREASON)cause;
+       return (OSPEFAILREASON)cause;
 }
 
 /*!
@@ -749,47 +897,44 @@ static int osp_auth(
        unsigned int* timelimit)
 {
        int res;
-       struct osp_provider* p;
+       struct osp_provider* p = NULL;
        char dest[OSP_NORSTR_SIZE];
 
        *transaction = OSP_INVALID_HANDLE;
        *timelimit = OSP_DEF_TIMELIMIT;
 
        if ((res = osp_get_provider(provider, &p)) <= 0) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Unabe to find OSP provider '%s'\n", provider);
+               ast_debug(1, "OSP: Unabe to find OSP provider '%s'\n", provider);
                return res;
        }
 
        switch (p->authpolicy) {
-               case OSP_AUTH_NO:
+       case OSP_AUTH_NO:
+               res = 1;
+               break;
+       case OSP_AUTH_EXCLUSIVE:
+               if (ast_strlen_zero(token)) {
+                       res = 0;
+               } else if ((res = osp_create_transaction(provider, transaction, sizeof(dest), dest)) <= 0) {
+                       ast_debug(1, "OSP: Unable to generate transaction handle\n");
+                       *transaction = OSP_INVALID_HANDLE;
+                       res = 0;
+               } else if((res = osp_validate_token(*transaction, source, dest, calling, called, token, timelimit)) <= 0) {
+                       OSPPTransactionRecordFailure(*transaction, OSPC_FAIL_CALL_REJECTED);
+               }
+               break;
+       case OSP_AUTH_YES:
+       default:
+               if (ast_strlen_zero(token)) {
                        res = 1;
-                       break;
-               case OSP_AUTH_EXCLUSIVE:
-                       if (ast_strlen_zero(token)) {
-                               res = 0;
-                       } else if ((res = osp_create_transaction(provider, transaction, sizeof(dest), dest)) <= 0) {
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: Unable to generate transaction handle\n");
-                               *transaction = OSP_INVALID_HANDLE;
-                               res = 0;
-                       } else if((res = osp_validate_token(*transaction, source, dest, calling, called, token, timelimit)) <= 0) {
-                               OSPPTransactionRecordFailure(*transaction, OSPC_FAIL_CALL_REJECTED);
-                       }
-                       break;
-               case OSP_AUTH_YES:
-               default:
-                       if (ast_strlen_zero(token)) {
-                               res = 1;
-                       } else if ((res = osp_create_transaction(provider, transaction, sizeof(dest), dest)) <= 0) {
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: Unable to generate transaction handle\n");
-                               *transaction = OSP_INVALID_HANDLE;
-                               res = 0;
-                       } else if((res = osp_validate_token(*transaction, source, dest, calling, called, token, timelimit)) <= 0) {
-                               OSPPTransactionRecordFailure(*transaction, OSPC_FAIL_CALL_REJECTED);
-                       }
-                       break;
+               } else if ((res = osp_create_transaction(provider, transaction, sizeof(dest), dest)) <= 0) {
+                       ast_debug(1, "OSP: Unable to generate transaction handle\n");
+                       *transaction = OSP_INVALID_HANDLE;
+                       res = 0;
+               } else if((res = osp_validate_token(*transaction, source, dest, calling, called, token, timelimit)) <= 0) {
+                       OSPPTransactionRecordFailure(*transaction, OSPC_FAIL_CALL_REJECTED);
+               }
+               break;
        }
 
        return res;
@@ -862,15 +1007,15 @@ static int osp_create_callid(
 
        callid->len = sizeof(callid->buf);
        switch (type) {
-               case OSP_CALLID_H323:
-                       res = osp_create_uuid(callid->buf, &callid->len);
-                       break;
-               case OSP_CALLID_SIP:
-               case OSP_CALLID_IAX:
-                       res = 0;
-               default:
-                       res = -1;
-                       break;
+       case OSP_CALLID_H323:
+               res = osp_create_uuid(callid->buf, &callid->len);
+               break;
+       case OSP_CALLID_SIP:
+       case OSP_CALLID_IAX:
+               res = 0;
+       default:
+               res = -1;
+               break;
        }
 
        if ((res != 1) && (callid->len != 0)) {
@@ -887,6 +1032,8 @@ static int osp_create_callid(
  * \param srcdev Source device of outbound call
  * \param calling Calling number
  * \param called Called number
+ * \param snetid Source network ID
+ * \param rnumber Routing number
  * \param callidtypes Call ID types
  * \param result Lookup results
  * \return 1 Found , 0 No route, -1 Error
@@ -896,11 +1043,13 @@ static int osp_lookup(
        const char* srcdev,
        const char* calling,
        const char* called,
+       const char* snetid,
+       const char* rnumber,
        unsigned int callidtypes,
        struct osp_result* result)
 {
        int res;
-       struct osp_provider* p;
+       struct osp_provider* p = NULL;
        char source[OSP_NORSTR_SIZE];
        char callingnum[OSP_NORSTR_SIZE];
        char callednum[OSP_NORSTR_SIZE];
@@ -912,9 +1061,9 @@ static int osp_lookup(
        unsigned int i, type;
        struct osp_callid callid;
        unsigned int callidnum;
-       OSPTCALLID* callids[OSP_CALLID_MAXNUM];
+       OSPT_CALL_ID* callids[OSP_CALLID_MAXNUM];
        unsigned int dummy = 0;
-       enum OSPEFAILREASON reason;
+       OSPEFAILREASON reason;
        int error;
 
        result->outhandle = OSP_INVALID_HANDLE;
@@ -928,14 +1077,12 @@ static int osp_lookup(
        result->outtimelimit = OSP_DEF_TIMELIMIT;
 
        if ((res = osp_get_provider(provider, &p)) <= 0) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Unabe to find OSP provider '%s'\n", provider);
+               ast_debug(1, "OSP: Unabe to find OSP provider '%s'\n", provider);
                return res;
        }
 
        if ((res = osp_create_transaction(provider, &result->outhandle, sizeof(source), source)) <= 0) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Unable to generate transaction handle\n");
+               ast_debug(1, "OSP: Unable to generate transaction handle\n");
                result->outhandle = OSP_INVALID_HANDLE;
                if (result->inhandle != OSP_INVALID_HANDLE) {
                        OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NORMAL_UNSPECIFIED);
@@ -943,6 +1090,14 @@ static int osp_lookup(
                return -1;
        }
 
+       if (!ast_strlen_zero(snetid)) {
+               OSPPTransactionSetNetworkIds(result->outhandle, snetid, "");
+       }
+
+       if (!ast_strlen_zero(rnumber)) {
+               OSPPTransactionSetRoutingNumber(result->outhandle, rnumber);
+       }
+
        callidnum = 0;
        callids[0] = NULL;
        for (i = 0; i < OSP_CALLID_MAXNUM; i++) {
@@ -960,28 +1115,27 @@ static int osp_lookup(
        osp_convert_address(srcdev, dev, sizeof(dev));
        result->numresults = OSP_DEF_DESTINATIONS;
        error = OSPPTransactionRequestAuthorisation(
-                               result->outhandle,
-                               src,
-                               dev,
-                               calling ? calling : "",
-                               OSPC_E164,
-                               called,
-                               OSPC_E164,
-                               NULL,
-                               callidnum,
-                               callids,
-                               NULL,
-                               &result->numresults,
-                               &dummy,
-                               NULL);
+               result->outhandle,
+               src,
+               dev,
+               calling ? calling : "",
+               OSPC_NFORMAT_E164,
+               called,
+               OSPC_NFORMAT_E164,
+               NULL,
+               callidnum,
+               callids,
+               NULL,
+               &result->numresults,
+               &dummy,
+               NULL);
 
        for (i = 0; i < callidnum; i++) {
                OSPPCallIdDelete(&callids[i]);
        }
 
        if (error != OSPC_ERR_NO_ERROR) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Unable to request authorization\n");
+               ast_debug(1, "OSP: Unable to request authorization, error '%d'\n", error);
                result->numresults = 0;
                if (result->inhandle != OSP_INVALID_HANDLE) {
                        OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NORMAL_UNSPECIFIED);
@@ -990,8 +1144,7 @@ static int osp_lookup(
        }
 
        if (!result->numresults) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: No more destination\n");
+               ast_debug(1, "OSP: No more destination\n");
                if (result->inhandle != OSP_INVALID_HANDLE) {
                        OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NO_ROUTE_TO_DEST);
                }
@@ -1001,26 +1154,25 @@ static int osp_lookup(
        result->outcallid.len = sizeof(result->outcallid.buf);
        tokenlen = sizeof(token);
        error = OSPPTransactionGetFirstDestination(
-                               result->outhandle,
-                               0,
-                               NULL,
-                               NULL,
-                               &result->outtimelimit,
-                               &result->outcallid.len,
-                               result->outcallid.buf,
-                               sizeof(callednum),
-                               callednum,
-                               sizeof(callingnum),
-                               callingnum,
-                               sizeof(destination),
-                               destination,
-                               0,
-                               NULL,
-                               &tokenlen,
-                               token);
+               result->outhandle,
+               0,
+               NULL,
+               NULL,
+               &result->outtimelimit,
+               &result->outcallid.len,
+               result->outcallid.buf,
+               sizeof(callednum),
+               callednum,
+               sizeof(callingnum),
+               callingnum,
+               sizeof(destination),
+               destination,
+               0,
+               NULL,
+               &tokenlen,
+               token);
        if (error != OSPC_ERR_NO_ERROR) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Unable to get first route\n");
+               ast_debug(1, "OSP: Unable to get first route, error '%d'\n", error);
                result->numresults = 0;
                result->outtimelimit = OSP_DEF_TIMELIMIT;
                if (result->inhandle != OSP_INVALID_HANDLE) {
@@ -1031,21 +1183,18 @@ static int osp_lookup(
 
        result->numresults--;
        result->outtimelimit = osp_choose_timelimit(result->intimelimit, result->outtimelimit);
-       if (option_debug) {
-               ast_log(LOG_DEBUG, "OSP: outtimelimit '%d'\n", result->outtimelimit);
-               ast_log(LOG_DEBUG, "OSP: called '%s'\n", callednum);
-               ast_log(LOG_DEBUG, "OSP: calling '%s'\n", callingnum);
-               ast_log(LOG_DEBUG, "OSP: destination '%s'\n", destination);
-               ast_log(LOG_DEBUG, "OSP: token size '%d'\n", tokenlen);
-       }
+       ast_debug(1, "OSP: outtimelimit '%d'\n", result->outtimelimit);
+       ast_debug(1, "OSP: called '%s'\n", callednum);
+       ast_debug(1, "OSP: calling '%s'\n", callingnum);
+       ast_debug(1, "OSP: destination '%s'\n", destination);
+       ast_debug(1, "OSP: token size '%d'\n", tokenlen);
 
        if ((res = osp_check_destination(p, callednum, callingnum, destination, tokenlen, token, &reason, result)) > 0) {
                return 1;
        }
 
        if (!result->numresults) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: No more destination\n");
+               ast_debug(1, "OSP: No more destination\n");
                result->outtimelimit = OSP_DEF_TIMELIMIT;
                OSPPTransactionRecordFailure(result->outhandle, reason);
                if (result->inhandle != OSP_INVALID_HANDLE) {
@@ -1058,39 +1207,37 @@ static int osp_lookup(
                result->outcallid.len = sizeof(result->outcallid.buf);
                tokenlen = sizeof(token);
                error = OSPPTransactionGetNextDestination(
-                                       result->outhandle,
-                                       reason,
-                                       0,
-                                       NULL,
-                                       NULL,
-                                       &result->outtimelimit,
-                                       &result->outcallid.len,
-                                       result->outcallid.buf,
-                                       sizeof(callednum),
-                                       callednum,
-                                       sizeof(callingnum),
-                                       callingnum,
-                                       sizeof(destination),
-                                       destination,
-                                       0,
-                                       NULL,
-                                       &tokenlen,
-                                       token);
+                       result->outhandle,
+                       reason,
+                       0,
+                       NULL,
+                       NULL,
+                       &result->outtimelimit,
+                       &result->outcallid.len,
+                       result->outcallid.buf,
+                       sizeof(callednum),
+                       callednum,
+                       sizeof(callingnum),
+                       callingnum,
+                       sizeof(destination),
+                       destination,
+                       0,
+                       NULL,
+                       &tokenlen,
+                       token);
                if (error == OSPC_ERR_NO_ERROR) {
                        result->numresults--;
                        result->outtimelimit = osp_choose_timelimit(result->intimelimit, result->outtimelimit);
-                       if (option_debug) {
-                               ast_log(LOG_DEBUG, "OSP: outtimelimit '%d'\n", result->outtimelimit);
-                               ast_log(LOG_DEBUG, "OSP: called '%s'\n", callednum);
-                               ast_log(LOG_DEBUG, "OSP: calling '%s'\n", callingnum);
-                               ast_log(LOG_DEBUG, "OSP: destination '%s'\n", destination);
-                               ast_log(LOG_DEBUG, "OSP: token size '%d'\n", tokenlen);
-                       }
+                       ast_debug(1, "OSP: outtimelimit '%d'\n", result->outtimelimit);
+                       ast_debug(1, "OSP: called '%s'\n", callednum);
+                       ast_debug(1, "OSP: calling '%s'\n", callingnum);
+                       ast_debug(1, "OSP: destination '%s'\n", destination);
+                       ast_debug(1, "OSP: token size '%d'\n", tokenlen);
+
                        if ((res = osp_check_destination(p, callednum, callingnum, destination, tokenlen, token, &reason, result)) > 0) {
                                break;
                        } else if (!result->numresults) {
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: No more destination\n");
+                               ast_debug(1, "OSP: No more destination\n");
                                OSPPTransactionRecordFailure(result->outhandle, reason);
                                if (result->inhandle != OSP_INVALID_HANDLE) {
                                        OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NO_ROUTE_TO_DEST);
@@ -1099,8 +1246,7 @@ static int osp_lookup(
                                break;
                        }
                } else {
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "OSP: Unable to get route, error '%d'\n", error);
+                       ast_debug(1, "OSP: Unable to get route, error '%d'\n", error);
                        result->numresults = 0;
                        result->outtimelimit = OSP_DEF_TIMELIMIT;
                        if (result->inhandle != OSP_INVALID_HANDLE) {
@@ -1126,13 +1272,13 @@ static int osp_next(
        struct osp_result* result)
 {
        int res;
-       struct osp_provider* p;
+       struct osp_provider* p = NULL;
        char callingnum[OSP_NORSTR_SIZE];
        char callednum[OSP_NORSTR_SIZE];
        char destination[OSP_NORSTR_SIZE];
        unsigned int tokenlen;
        char token[OSP_TOKSTR_SIZE];
-       enum OSPEFAILREASON reason;
+       OSPEFAILREASON reason;
        int error;
 
        result->tech[0] = '\0';
@@ -1144,14 +1290,12 @@ static int osp_next(
        result->outtimelimit = OSP_DEF_TIMELIMIT;
 
        if ((res = osp_get_provider(provider, &p)) <= 0) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Unabe to find OSP provider '%s'\n", provider);
+               ast_debug(1, "OSP: Unabe to find OSP provider '%s'\n", provider);
                return res;
        }
 
        if (result->outhandle == OSP_INVALID_HANDLE) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Transaction handle undefined\n");
+               ast_debug(1, "OSP: Transaction handle undefined\n");
                result->numresults = 0;
                if (result->inhandle != OSP_INVALID_HANDLE) {
                        OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NORMAL_UNSPECIFIED);
@@ -1162,8 +1306,7 @@ static int osp_next(
        reason = asterisk2osp(cause);
 
        if (!result->numresults) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: No more destination\n");
+               ast_debug(1, "OSP: No more destination\n");
                OSPPTransactionRecordFailure(result->outhandle, reason);
                if (result->inhandle != OSP_INVALID_HANDLE) {
                        OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NO_ROUTE_TO_DEST);
@@ -1175,40 +1318,38 @@ static int osp_next(
                result->outcallid.len = sizeof(result->outcallid.buf);
                tokenlen = sizeof(token);
                error = OSPPTransactionGetNextDestination(
-                                       result->outhandle,
-                                       reason,
-                                       0,
-                                       NULL,
-                                       NULL,
-                                       &result->outtimelimit,
-                                       &result->outcallid.len,
-                                       result->outcallid.buf,
-                                       sizeof(callednum),
-                                       callednum,
-                                       sizeof(callingnum),
-                                       callingnum,
-                                       sizeof(destination),
-                                       destination,
-                                       0,
-                                       NULL,
-                                       &tokenlen,
-                                       token);
+                       result->outhandle,
+                       reason,
+                       0,
+                       NULL,
+                       NULL,
+                       &result->outtimelimit,
+                       &result->outcallid.len,
+                       result->outcallid.buf,
+                       sizeof(callednum),
+                       callednum,
+                       sizeof(callingnum),
+                       callingnum,
+                       sizeof(destination),
+                       destination,
+                       0,
+                       NULL,
+                       &tokenlen,
+                       token);
                if (error == OSPC_ERR_NO_ERROR) {
                        result->numresults--;
                        result->outtimelimit = osp_choose_timelimit(result->intimelimit, result->outtimelimit);
-                       if (option_debug) {
-                               ast_log(LOG_DEBUG, "OSP: outtimelimit '%d'\n", result->outtimelimit);
-                               ast_log(LOG_DEBUG, "OSP: called '%s'\n", callednum);
-                               ast_log(LOG_DEBUG, "OSP: calling '%s'\n", callingnum);
-                               ast_log(LOG_DEBUG, "OSP: destination '%s'\n", destination);
-                               ast_log(LOG_DEBUG, "OSP: token size '%d'\n", tokenlen);
-                       }
+                       ast_debug(1, "OSP: outtimelimit '%d'\n", result->outtimelimit);
+                       ast_debug(1, "OSP: called '%s'\n", callednum);
+                       ast_debug(1, "OSP: calling '%s'\n", callingnum);
+                       ast_debug(1, "OSP: destination '%s'\n", destination);
+                       ast_debug(1, "OSP: token size '%d'\n", tokenlen);
+
                        if ((res = osp_check_destination(p, callednum, callingnum, destination, tokenlen, token, &reason, result)) > 0) {
                                res = 1;
                                break;
                        } else if (!result->numresults) {
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: No more destination\n");
+                               ast_debug(1, "OSP: No more destination\n");
                                OSPPTransactionRecordFailure(result->outhandle, reason);
                                if (result->inhandle != OSP_INVALID_HANDLE) {
                                        OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NO_ROUTE_TO_DEST);
@@ -1217,8 +1358,7 @@ static int osp_next(
                                break;
                        }
                } else {
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "OSP: Unable to get route, error '%d'\n", error);
+                       ast_debug(1, "OSP: Unable to get route, error '%d'\n", error);
                        result->token[0] = '\0';
                        result->numresults = 0;
                        result->outtimelimit = OSP_DEF_TIMELIMIT;
@@ -1254,7 +1394,7 @@ static int osp_finish(
        unsigned int release)
 {
        int res;
-       enum OSPEFAILREASON reason;
+       OSPEFAILREASON reason;
        time_t alert = 0;
        unsigned isPddInfoPresent = 0;
        unsigned pdd = 0;
@@ -1271,29 +1411,27 @@ static int osp_finish(
        }
 
        error = OSPPTransactionReportUsage(
-                               handle,
-                               difftime(end, connect),
-                               start,
-                               end,
-                               alert,
-                               connect,
-                               isPddInfoPresent,
-                               pdd,
-                               release,
-                               (unsigned char*)"",
-                               0,
-                               0,
-                               0,
-                               0,
-                               &dummy,
-                               NULL);
+               handle,
+               difftime(end, connect),
+               start,
+               end,
+               alert,
+               connect,
+               isPddInfoPresent,
+               pdd,
+               release,
+               NULL,
+               -1,
+               -1,
+               -1,
+               -1,
+               &dummy,
+               NULL);
        if (error == OSPC_ERR_NO_ERROR) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Usage reported\n");
+               ast_debug(1, "OSP: Usage reported\n");
                res = 1;
        } else {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: Unable to report usage, error '%d'\n", error);
+               ast_debug(1, "OSP: Unable to report usage, error '%d'\n", error);
                res = -1;
        }
        OSPPTransactionDelete(handle);
@@ -1310,11 +1448,10 @@ static int osp_finish(
  * \return 0 Success, -1 Failed
  */
 static int ospauth_exec(
-       struct ast_channel* chan,
-       void* data)
+       struct ast_channel *chan,
+       const char *data)
 {
        int res;
-       struct ast_module_user* u;
        const char* provider = OSP_DEF_PROVIDER;
        struct varshead* headp;
        struct ast_var_t* current;
@@ -1331,11 +1468,8 @@ static int ospauth_exec(
                AST_APP_ARG(options);
        );
 
-       u = ast_module_user_add(chan);
-
        if (!(tmp = ast_strdupa(data))) {
                ast_log(LOG_ERROR, "Out of memory\n");
-               ast_module_user_remove(u);
                return -1;
        }
 
@@ -1344,10 +1478,7 @@ static int ospauth_exec(
        if (!ast_strlen_zero(args.provider)) {
                provider = args.provider;
        }
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPAuth: provider '%s'\n", provider);
-
-       }
+       ast_debug(1, "OSPAuth: provider '%s'\n", provider);
 
        headp = &chan->varshead;
        AST_LIST_TRAVERSE(headp, current, entries) {
@@ -1358,10 +1489,8 @@ static int ospauth_exec(
                }
        }
 
-       if (option_debug) {
-               ast_log(LOG_DEBUG, "OSPAuth: source '%s'\n", source);
-               ast_log(LOG_DEBUG, "OSPAuth: token size '%zd'\n", strlen(token));
-       }
+       ast_debug(1, "OSPAuth: source '%s'\n", source);
+       ast_debug(1, "OSPAuth: token size '%zd'\n", strlen(token));
 
        if ((res = osp_auth(provider, &handle, source, chan->cid.cid_num, chan->exten, token, &timelimit)) > 0) {
                status = AST_OSP_SUCCESS;
@@ -1376,15 +1505,12 @@ static int ospauth_exec(
 
        snprintf(buffer, sizeof(buffer), "%d", handle);
        pbx_builtin_setvar_helper(chan, "OSPINHANDLE", buffer);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPAuth: OSPINHANDLE '%s'\n", buffer);
+       ast_debug(1, "OSPAuth: OSPINHANDLE '%s'\n", buffer);
        snprintf(buffer, sizeof(buffer), "%d", timelimit);
        pbx_builtin_setvar_helper(chan, "OSPINTIMELIMIT", buffer);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPAuth: OSPINTIMELIMIT '%s'\n", buffer);
+       ast_debug(1, "OSPAuth: OSPINTIMELIMIT '%s'\n", buffer);
        pbx_builtin_setvar_helper(chan, "OSPAUTHSTATUS", status);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPAuth: %s\n", status);
+       ast_debug(1, "OSPAuth: %s\n", status);
 
        if(res <= 0) {
                res = -1;
@@ -1392,8 +1518,6 @@ static int ospauth_exec(
                res = 0;
        }
 
-       ast_module_user_remove(u);
-
        return res;
 }
 
@@ -1405,15 +1529,15 @@ static int ospauth_exec(
  */
 static int osplookup_exec(
        struct ast_channel* chan,
-       void* data)
+       const char * data)
 {
        int res, cres;
-       struct ast_module_user* u;
        const char* provider = OSP_DEF_PROVIDER;
        struct varshead* headp;
        struct ast_var_t* current;
        const char* srcdev = "";
-       const char* netid = "";
+       const char* snetid = "";
+       const char* rnumber = "";
        char buffer[OSP_TOKSTR_SIZE];
        unsigned int callidtypes = OSP_CALLID_UNDEFINED;
        struct osp_result result;
@@ -1431,24 +1555,19 @@ static int osplookup_exec(
                return -1;
        }
 
-       u = ast_module_user_add(chan);
-
        if (!(tmp = ast_strdupa(data))) {
                ast_log(LOG_ERROR, "Out of memory\n");
-               ast_module_user_remove(u);
                return -1;
        }
 
        AST_STANDARD_APP_ARGS(args, tmp);
 
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPLookup: exten '%s'\n", args.exten);
+       ast_debug(1, "OSPLookup: exten '%s'\n", args.exten);
 
        if (!ast_strlen_zero(args.provider)) {
                provider = args.provider;
        }
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPlookup: provider '%s'\n", provider);
+       ast_debug(1, "OSPlookup: provider '%s'\n", provider);
 
        if (args.options) {
                if (strchr(args.options, 'h')) {
@@ -1461,9 +1580,7 @@ static int osplookup_exec(
                        callidtypes |= OSP_CALLID_IAX;
                }
        }
-       if (option_debug) {
-               ast_log(LOG_DEBUG, "OSPLookup: call id types '%d'\n", callidtypes);
-       }
+       ast_debug(1, "OSPLookup: call id types '%d'\n", callidtypes);
 
        result.inhandle = OSP_INVALID_HANDLE;
        result.intimelimit = OSP_DEF_TIMELIMIT;
@@ -1471,32 +1588,32 @@ static int osplookup_exec(
        headp = &chan->varshead;
        AST_LIST_TRAVERSE(headp, current, entries) {
                if (!strcasecmp(ast_var_name(current), "OSPINHANDLE")) {
-                       if (sscanf(ast_var_value(current), "%d", &result.inhandle) != 1) {
+                       if (sscanf(ast_var_value(current), "%30d", &result.inhandle) != 1) {
                                result.inhandle = OSP_INVALID_HANDLE;
                        }
                } else if (!strcasecmp(ast_var_name(current), "OSPINTIMELIMIT")) {
-                       if (sscanf(ast_var_value(current), "%d", &result.intimelimit) != 1) {
+                       if (sscanf(ast_var_value(current), "%30d", &result.intimelimit) != 1) {
                                result.intimelimit = OSP_DEF_TIMELIMIT;
                        }
                } else if (!strcasecmp(ast_var_name(current), "OSPINNETWORKID")) {
-                       netid = ast_var_value(current);
+                       snetid = ast_var_value(current);
+               } else if (!strcasecmp(ast_var_name(current), "OSPROUTINGNUMBER")) {
+                       rnumber = ast_var_value(current);
                } else if (!strcasecmp(ast_var_name(current), "OSPPEERIP")) {
                        srcdev = ast_var_value(current);
                }
        }
-       if (option_debug) {
-               ast_log(LOG_DEBUG, "OSPLookup: OSPINHANDLE '%d'\n", result.inhandle);
-               ast_log(LOG_DEBUG, "OSPLookup: OSPINTIMELIMIT '%d'\n", result.intimelimit);
-               ast_log(LOG_DEBUG, "OSPLookup: OSPINNETWORKID '%s'\n", netid);
-               ast_log(LOG_DEBUG, "OSPLookup: source device '%s'\n", srcdev);
-       }
+       ast_debug(1, "OSPLookup: OSPINHANDLE '%d'\n", result.inhandle);
+       ast_debug(1, "OSPLookup: OSPINTIMELIMIT '%d'\n", result.intimelimit);
+       ast_debug(1, "OSPLookup: OSPINNETWORKID '%s'\n", snetid);
+       ast_debug(1, "OSPLookup: OSPROUTINGNUMBER '%s'\n", rnumber);
+       ast_debug(1, "OSPLookup: source device '%s'\n", srcdev);
 
        if ((cres = ast_autoservice_start(chan)) < 0) {
-               ast_module_user_remove(u);
                return -1;
        }
 
-       if ((res = osp_lookup(provider, srcdev, chan->cid.cid_num, args.exten, callidtypes, &result)) > 0) {
+       if ((res = osp_lookup(provider, srcdev, chan->cid.cid_num, args.exten, snetid, rnumber, callidtypes, &result)) > 0) {
                status = AST_OSP_SUCCESS;
        } else {
                result.tech[0] = '\0';
@@ -1518,38 +1635,30 @@ static int osplookup_exec(
 
        snprintf(buffer, sizeof(buffer), "%d", result.outhandle);
        pbx_builtin_setvar_helper(chan, "OSPOUTHANDLE", buffer);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPLookup: OSPOUTHANDLE '%s'\n", buffer);
+       ast_debug(1, "OSPLookup: OSPOUTHANDLE '%s'\n", buffer);
        pbx_builtin_setvar_helper(chan, "OSPTECH", result.tech);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPLookup: OSPTECH '%s'\n", result.tech);
+       ast_debug(1, "OSPLookup: OSPTECH '%s'\n", result.tech);
        pbx_builtin_setvar_helper(chan, "OSPDEST", result.dest);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPLookup: OSPDEST '%s'\n", result.dest);
+       ast_debug(1, "OSPLookup: OSPDEST '%s'\n", result.dest);
        pbx_builtin_setvar_helper(chan, "OSPCALLED", result.called);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPLookup: OSPCALLED '%s'\n", result.called);
+       ast_debug(1, "OSPLookup: OSPCALLED '%s'\n", result.called);
        pbx_builtin_setvar_helper(chan, "OSPCALLING", result.calling);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPLookup: OSPCALLING '%s'\n", result.calling);
+       ast_debug(1, "OSPLookup: OSPCALLING '%s'\n", result.calling);
+       pbx_builtin_setvar_helper(chan, "OSPOUTNETWORKID", result.networkid);
+       ast_debug(1, "OSPLookup: OSPOUTNETWORKID '%s'\n", result.networkid);
        pbx_builtin_setvar_helper(chan, "OSPOUTTOKEN", result.token);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPLookup: OSPOUTTOKEN size '%zd'\n", strlen(result.token));
+       ast_debug(1, "OSPLookup: OSPOUTTOKEN size '%zd'\n", strlen(result.token));
        snprintf(buffer, sizeof(buffer), "%d", result.numresults);
        pbx_builtin_setvar_helper(chan, "OSPRESULTS", buffer);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPLookup: OSPRESULTS '%s'\n", buffer);
+       ast_debug(1, "OSPLookup: OSPRESULTS '%s'\n", buffer);
        snprintf(buffer, sizeof(buffer), "%d", result.outtimelimit);
        pbx_builtin_setvar_helper(chan, "OSPOUTTIMELIMIT", buffer);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPLookup: OSPOUTTIMELIMIT '%s'\n", buffer);
+       ast_debug(1, "OSPLookup: OSPOUTTIMELIMIT '%s'\n", buffer);
        snprintf(buffer, sizeof(buffer), "%d", callidtypes);
        pbx_builtin_setvar_helper(chan, "OSPOUTCALLIDTYPES", buffer);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPLookup: OSPOUTCALLIDTYPES '%s'\n", buffer);
+       ast_debug(1, "OSPLookup: OSPOUTCALLIDTYPES '%s'\n", buffer);
        pbx_builtin_setvar_helper(chan, "OSPLOOKUPSTATUS", status);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPLookup: %s\n", status);
+       ast_debug(1, "OSPLookup: %s\n", status);
 
        if (!strcasecmp(result.tech, OSP_TECH_H323)) {
                if ((callidtypes & OSP_CALLID_H323) && (result.outcallid.len != 0)) {
@@ -1566,8 +1675,7 @@ static int osplookup_exec(
                if (!ast_strlen_zero(result.token)) {
                        snprintf(buffer, sizeof(buffer), "%s%s", OSP_SIP_HEADER, result.token);
                        pbx_builtin_setvar_helper(chan, "_SIPADDHEADER", buffer);
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "OSPLookup: SIPADDHEADER size '%zd'\n", strlen(buffer));
+                       ast_debug(1, "OSPLookup: SIPADDHEADER size '%zd'\n", strlen(buffer));
                }
        } else if (!strcasecmp(result.tech, OSP_TECH_IAX)) {
                snprintf(buffer, sizeof(buffer), "%s/%s/%s", result.tech, result.dest, result.called);
@@ -1575,7 +1683,6 @@ static int osplookup_exec(
        }
 
        if ((cres = ast_autoservice_stop(chan)) < 0) {
-               ast_module_user_remove(u);
                return -1;
        }
 
@@ -1585,8 +1692,6 @@ static int osplookup_exec(
                res = 0;
        }
 
-       ast_module_user_remove(u);
-
        return res;
 }
 
@@ -1598,10 +1703,9 @@ static int osplookup_exec(
  */
 static int ospnext_exec(
        struct ast_channel* chan,
-       void* data)
+       const char * data)
 {
        int res;
-       struct ast_module_user* u;
        const char* provider = OSP_DEF_PROVIDER;
        int cause = 0;
        struct varshead* headp;
@@ -1623,27 +1727,22 @@ static int ospnext_exec(
                return -1;
        }
 
-       u = ast_module_user_add(chan);
-
        if (!(tmp = ast_strdupa(data))) {
                ast_log(LOG_ERROR, "Out of memory\n");
-               ast_module_user_remove(u);
                return -1;
        }
 
        AST_STANDARD_APP_ARGS(args, tmp);
 
-       if (!ast_strlen_zero(args.cause) && sscanf(args.cause, "%d", &cause) != 1) {
+       if (!ast_strlen_zero(args.cause) && sscanf(args.cause, "%30d", &cause) != 1) {
                cause = 0;
        }
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPNext: cause '%d'\n", cause);
+       ast_debug(1, "OSPNext: cause '%d'\n", cause);
 
        if (!ast_strlen_zero(args.provider)) {
                provider = args.provider;
        }
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPlookup: provider '%s'\n", provider);
+       ast_debug(1, "OSPlookup: provider '%s'\n", provider);
 
        result.inhandle = OSP_INVALID_HANDLE;
        result.outhandle = OSP_INVALID_HANDLE;
@@ -1653,34 +1752,32 @@ static int ospnext_exec(
        headp = &chan->varshead;
        AST_LIST_TRAVERSE(headp, current, entries) {
                if (!strcasecmp(ast_var_name(current), "OSPINHANDLE")) {
-                       if (sscanf(ast_var_value(current), "%d", &result.inhandle) != 1) {
+                       if (sscanf(ast_var_value(current), "%30d", &result.inhandle) != 1) {
                                result.inhandle = OSP_INVALID_HANDLE;
                        }
                } else if (!strcasecmp(ast_var_name(current), "OSPOUTHANDLE")) {
-                       if (sscanf(ast_var_value(current), "%d", &result.outhandle) != 1) {
+                       if (sscanf(ast_var_value(current), "%30d", &result.outhandle) != 1) {
                                result.outhandle = OSP_INVALID_HANDLE;
                        }
                } else if (!strcasecmp(ast_var_name(current), "OSPINTIMELIMIT")) {
-                       if (sscanf(ast_var_value(current), "%d", &result.intimelimit) != 1) {
+                       if (sscanf(ast_var_value(current), "%30d", &result.intimelimit) != 1) {
                                result.intimelimit = OSP_DEF_TIMELIMIT;
                        }
                } else if (!strcasecmp(ast_var_name(current), "OSPOUTCALLIDTYPES")) {
-                       if (sscanf(ast_var_value(current), "%d", &callidtypes) != 1) {
+                       if (sscanf(ast_var_value(current), "%30d", &callidtypes) != 1) {
                                callidtypes = OSP_CALLID_UNDEFINED;
                        }
                } else if (!strcasecmp(ast_var_name(current), "OSPRESULTS")) {
-                       if (sscanf(ast_var_value(current), "%d", &result.numresults) != 1) {
+                       if (sscanf(ast_var_value(current), "%30d", &result.numresults) != 1) {
                                result.numresults = 0;
                        }
                }
        }
-       if (option_debug) {
-               ast_log(LOG_DEBUG, "OSPNext: OSPINHANDLE '%d'\n", result.inhandle);
-               ast_log(LOG_DEBUG, "OSPNext: OSPOUTHANDLE '%d'\n", result.outhandle);
-               ast_log(LOG_DEBUG, "OSPNext: OSPINTIMELIMIT '%d'\n", result.intimelimit);
-               ast_log(LOG_DEBUG, "OSPNext: OSPOUTCALLIDTYPES '%d'\n", callidtypes);
-               ast_log(LOG_DEBUG, "OSPNext: OSPRESULTS '%d'\n", result.numresults);
-       }
+       ast_debug(1, "OSPNext: OSPINHANDLE '%d'\n", result.inhandle);
+       ast_debug(1, "OSPNext: OSPOUTHANDLE '%d'\n", result.outhandle);
+       ast_debug(1, "OSPNext: OSPINTIMELIMIT '%d'\n", result.intimelimit);
+       ast_debug(1, "OSPNext: OSPOUTCALLIDTYPES '%d'\n", callidtypes);
+       ast_debug(1, "OSPNext: OSPRESULTS '%d'\n", result.numresults);
 
        if ((res = osp_next(provider, cause, &result)) > 0) {
                status = AST_OSP_SUCCESS;
@@ -1703,31 +1800,25 @@ static int ospnext_exec(
        }
 
        pbx_builtin_setvar_helper(chan, "OSPTECH", result.tech);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPNext: OSPTECH '%s'\n", result.tech);
+       ast_debug(1, "OSPNext: OSPTECH '%s'\n", result.tech);
        pbx_builtin_setvar_helper(chan, "OSPDEST", result.dest);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPNext: OSPDEST '%s'\n", result.dest);
+       ast_debug(1, "OSPNext: OSPDEST '%s'\n", result.dest);
        pbx_builtin_setvar_helper(chan, "OSPCALLED", result.called);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPNext: OSPCALLED'%s'\n", result.called);
+       ast_debug(1, "OSPNext: OSPCALLED'%s'\n", result.called);
        pbx_builtin_setvar_helper(chan, "OSPCALLING", result.calling);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPNext: OSPCALLING '%s'\n", result.calling);
+       ast_debug(1, "OSPNext: OSPCALLING '%s'\n", result.calling);
+       pbx_builtin_setvar_helper(chan, "OSPOUTNETWORKID", result.networkid);
+       ast_debug(1, "OSPLookup: OSPOUTNETWORKID '%s'\n", result.networkid);
        pbx_builtin_setvar_helper(chan, "OSPOUTTOKEN", result.token);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPNext: OSPOUTTOKEN size '%zd'\n", strlen(result.token));
+       ast_debug(1, "OSPNext: OSPOUTTOKEN size '%zd'\n", strlen(result.token));
        snprintf(buffer, sizeof(buffer), "%d", result.numresults);
        pbx_builtin_setvar_helper(chan, "OSPRESULTS", buffer);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPNext: OSPRESULTS '%s'\n", buffer);
+       ast_debug(1, "OSPNext: OSPRESULTS '%s'\n", buffer);
        snprintf(buffer, sizeof(buffer), "%d", result.outtimelimit);
        pbx_builtin_setvar_helper(chan, "OSPOUTTIMELIMIT", buffer);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPNext: OSPOUTTIMELIMIT '%s'\n", buffer);
+       ast_debug(1, "OSPNext: OSPOUTTIMELIMIT '%s'\n", buffer);
        pbx_builtin_setvar_helper(chan, "OSPNEXTSTATUS", status);
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPNext: %s\n", status);
+       ast_debug(1, "OSPNext: %s\n", status);
 
        if (!strcasecmp(result.tech, OSP_TECH_H323)) {
                if ((callidtypes & OSP_CALLID_H323) && (result.outcallid.len != 0)) {
@@ -1744,8 +1835,7 @@ static int ospnext_exec(
                if (!ast_strlen_zero(result.token)) {
                        snprintf(buffer, sizeof(buffer), "%s%s", OSP_SIP_HEADER, result.token);
                        pbx_builtin_setvar_helper(chan, "_SIPADDHEADER", buffer);
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "OSPLookup: SIPADDHEADER size '%zd'\n", strlen(buffer));
+                       ast_debug(1, "OSPLookup: SIPADDHEADER size '%zd'\n", strlen(buffer));
                }
        } else if (!strcasecmp(result.tech, OSP_TECH_IAX)) {
                snprintf(buffer, sizeof(buffer), "%s/%s/%s", result.tech, result.dest, result.called);
@@ -1758,8 +1848,6 @@ static int ospnext_exec(
                res = 0;
        }
 
-       ast_module_user_remove(u);
-
        return res;
 }
 
@@ -1771,10 +1859,9 @@ static int ospnext_exec(
  */
 static int ospfinished_exec(
        struct ast_channel* chan,
-       void* data)
+       const char * data)
 {
        int res = 1;
-       struct ast_module_user* u;
        int cause = 0;
        struct varshead* headp;
        struct ast_var_t* current;
@@ -1792,11 +1879,8 @@ static int ospfinished_exec(
                AST_APP_ARG(options);
        );
 
-       u = ast_module_user_add(chan);
-
        if (!(tmp = ast_strdupa(data))) {
                ast_log(LOG_ERROR, "Out of memory\n");
-               ast_module_user_remove(u);
                return -1;
        }
 
@@ -1805,11 +1889,11 @@ static int ospfinished_exec(
        headp = &chan->varshead;
        AST_LIST_TRAVERSE(headp, current, entries) {
                if (!strcasecmp(ast_var_name(current), "OSPINHANDLE")) {
-                       if (sscanf(ast_var_value(current), "%d", &inhandle) != 1) {
+                       if (sscanf(ast_var_value(current), "%30d", &inhandle) != 1) {
                                inhandle = OSP_INVALID_HANDLE;
                        }
                } else if (!strcasecmp(ast_var_name(current), "OSPOUTHANDLE")) {
-                       if (sscanf(ast_var_value(current), "%d", &outhandle) != 1) {
+                       if (sscanf(ast_var_value(current), "%30d", &outhandle) != 1) {
                                outhandle = OSP_INVALID_HANDLE;
                        }
                } else if (!recorded &&
@@ -1822,17 +1906,14 @@ static int ospfinished_exec(
                        }
                }
        }
-       if (option_debug) {
-               ast_log(LOG_DEBUG, "OSPFinish: OSPINHANDLE '%d'\n", inhandle);
-               ast_log(LOG_DEBUG, "OSPFinish: OSPOUTHANDLE '%d'\n", outhandle);
-               ast_log(LOG_DEBUG, "OSPFinish: recorded '%d'\n", recorded);
-       }
+       ast_debug(1, "OSPFinish: OSPINHANDLE '%d'\n", inhandle);
+       ast_debug(1, "OSPFinish: OSPOUTHANDLE '%d'\n", outhandle);
+       ast_debug(1, "OSPFinish: recorded '%d'\n", recorded);
 
-       if (!ast_strlen_zero(args.cause) && sscanf(args.cause, "%d", &cause) != 1) {
+       if (!ast_strlen_zero(args.cause) && sscanf(args.cause, "%30d", &cause) != 1) {
                cause = 0;
        }
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPFinish: cause '%d'\n", cause);
+       ast_debug(1, "OSPFinish: cause '%d'\n", cause);
 
        if (chan->cdr) {
                start = chan->cdr->start.tv_sec;
@@ -1847,28 +1928,24 @@ static int ospfinished_exec(
                connect = 0;
                end = 0;
        }
-       if (option_debug) {
-               ast_log(LOG_DEBUG, "OSPFinish: start '%ld'\n", start);
-               ast_log(LOG_DEBUG, "OSPFinish: connect '%ld'\n", connect);
-               ast_log(LOG_DEBUG, "OSPFinish: end '%ld'\n", end);
-       }
+       ast_debug(1, "OSPFinish: start '%ld'\n", start);
+       ast_debug(1, "OSPFinish: connect '%ld'\n", connect);
+       ast_debug(1, "OSPFinish: end '%ld'\n", end);
 
-       release = chan->_softhangup ? 0 : 1;
+       release = ast_check_hangup(chan) ? 0 : 1;
 
        if (osp_finish(outhandle, recorded, cause, start, connect, end, release) <= 0) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSPFinish: Unable to report usage for outbound call\n");
+               ast_debug(1, "OSPFinish: Unable to report usage for outbound call\n");
        }
        switch (cause) {
-               case AST_CAUSE_NORMAL_CLEARING:
-                       break;
-               default:
-                       cause = AST_CAUSE_NO_ROUTE_DESTINATION;
-                       break;
+       case AST_CAUSE_NORMAL_CLEARING:
+               break;
+       default:
+               cause = AST_CAUSE_NO_ROUTE_DESTINATION;
+               break;
        }
        if (osp_finish(inhandle, recorded, cause, start, connect, end, release) <= 0) {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSPFinish: Unable to report usage for inbound call\n");
+               ast_debug(1, "OSPFinish: Unable to report usage for inbound call\n");
        }
        snprintf(buffer, sizeof(buffer), "%d", OSP_INVALID_HANDLE);
        pbx_builtin_setvar_helper(chan, "OSPOUTHANDLE", buffer);
@@ -1889,22 +1966,31 @@ static int ospfinished_exec(
                res = 0;
        }
 
-       ast_module_user_remove(u);
-
        return res;
 }
 
 /* OSP Module APIs */
 
-static int osp_load(void)
+static int osp_unload(void);
+static int osp_load(int reload)
 {
        const char* t;
        unsigned int v;
        struct ast_config* cfg;
+       struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
        int error = OSPC_ERR_NO_ERROR;
 
-       cfg = ast_config_load(OSP_CONFIG_FILE);
+       if ((cfg = ast_config_load(OSP_CONFIG_FILE, config_flags)) == CONFIG_STATUS_FILEUNCHANGED) {
+               return 0;
+       } else if (cfg == CONFIG_STATUS_FILEINVALID) {
+               ast_log(LOG_ERROR, "Config file %s is in an invalid format.  Aborting.\n", OSP_CONFIG_FILE);
+               return 0;
+       }
+
        if (cfg) {
+               if (reload)
+                       osp_unload();
+
                t = ast_variable_retrieve(cfg, OSP_GENERAL_CAT, "accelerate");
                if (t && ast_true(t)) {
                        if ((error = OSPPInit(1)) != OSPC_ERR_NO_ERROR) {
@@ -1916,12 +2002,17 @@ static int osp_load(void)
                } else {
                        OSPPInit(0);
                }
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: osp_hardware '%d'\n", osp_hardware);
+               ast_debug(1, "OSP: osp_hardware '%d'\n", osp_hardware);
+
+               t = ast_variable_retrieve(cfg, OSP_GENERAL_CAT, "securityfeatures");
+               if (t && ast_true(t)) {
+                       osp_security = 1;
+               }
+               ast_debug(1, "OSP: osp_security '%d'\n", osp_security);
 
                t = ast_variable_retrieve(cfg, OSP_GENERAL_CAT, "tokenformat");
                if (t) {
-                       if ((sscanf(t, "%d", &v) == 1) &&
+                       if ((sscanf(t, "%30d", &v) == 1) &&
                                ((v == TOKEN_ALGO_SIGNED) || (v == TOKEN_ALGO_UNSIGNED) || (v == TOKEN_ALGO_BOTH)))
                        {
                                osp_tokenformat = v;
@@ -1930,8 +2021,7 @@ static int osp_load(void)
                                        TOKEN_ALGO_SIGNED, TOKEN_ALGO_UNSIGNED, TOKEN_ALGO_BOTH, t);
                        }
                }
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: osp_tokenformat '%d'\n", osp_tokenformat);
+               ast_debug(1, "OSP: osp_tokenformat '%d'\n", osp_tokenformat);
 
                t = ast_category_browse(cfg, NULL);
                while(t) {
@@ -1948,8 +2038,7 @@ static int osp_load(void)
                ast_log(LOG_WARNING, "OSP: Unable to find configuration. OSP support disabled\n");
                return 0;
        }
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSP: osp_initialized '%d'\n", osp_initialized);
+       ast_debug(1, "OSP: osp_initialized '%d'\n", osp_initialized);
 
        return 1;
 }
@@ -1974,16 +2063,14 @@ static int osp_unload(void)
                OSPPCleanup();
 
                osp_tokenformat = TOKEN_ALGO_SIGNED;
+               osp_security = 0;
                osp_hardware = 0;
                osp_initialized = 0;
        }
        return 0;
 }
 
-static int osp_show(
-       int fd,
-       int argc,
-       char* argv[])
+static char *handle_cli_osp_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
        int i;
        int found = 0;
@@ -1991,27 +2078,39 @@ static int osp_show(
        const char* provider = NULL;
        const char* tokenalgo;
 
-       if ((argc < 2) || (argc > 3)) {
-               return RESULT_SHOWUSAGE;
-       }
-       if (argc > 2) {
-               provider = argv[2];
-       }
+       switch (cmd) {
+       case CLI_INIT:
+               e->command = "osp show";
+               e->usage =
+                       "Usage: osp show\n"
+                       "       Displays information on Open Settlement Protocol support\n";
+               return NULL;
+       case CLI_GENERATE:
+               return NULL;
+       }
+
+       if ((a->argc < 2) || (a->argc > 3))
+               return CLI_SHOWUSAGE;
+       if (a->argc > 2) 
+               provider = a->argv[2];
        if (!provider) {
                switch (osp_tokenformat) {
-                       case TOKEN_ALGO_BOTH:
-                               tokenalgo = "Both";
-                               break;
-                       case TOKEN_ALGO_UNSIGNED:
-                               tokenalgo = "Unsigned";
-                               break;
-                       case TOKEN_ALGO_SIGNED:
-                       default:
-                               tokenalgo = "Signed";
-                               break;
+               case TOKEN_ALGO_BOTH:
+                       tokenalgo = "Both";
+                       break;
+               case TOKEN_ALGO_UNSIGNED:
+                       tokenalgo = "Unsigned";
+                       break;
+               case TOKEN_ALGO_SIGNED:
+               default:
+                       tokenalgo = "Signed";
+                       break;
                }
-               ast_cli(fd, "OSP: %s %s %s\n",
-                       osp_initialized ? "Initialized" : "Uninitialized", osp_hardware ? "Accelerated" : "Normal", tokenalgo);
+               ast_cli(a->fd, "OSP: %s/%s/%s/%s\n",
+                       osp_initialized ? "Initialized" : "Uninitialized", 
+                       osp_hardware ? "Accelerated" : "Normal", 
+                       osp_security ? "Enabled" : "Disabled", 
+                       tokenalgo);
        }
 
        ast_mutex_lock(&osplock);
@@ -2019,25 +2118,27 @@ static int osp_show(
        while(p) {
                if (!provider || !strcasecmp(p->name, provider)) {
                        if (found) {
-                               ast_cli(fd, "\n");
+                               ast_cli(a->fd, "\n");
                        }
-                       ast_cli(fd, " == OSP Provider '%s' == \n", p->name);
-                       ast_cli(fd, "Local Private Key: %s\n", p->privatekey);
-                       ast_cli(fd, "Local Certificate: %s\n", p->localcert);
-                       for (i = 0; i < p->cacount; i++) {
-                               ast_cli(fd, "CA Certificate %d:  %s\n", i + 1, p->cacerts[i]);
+                       ast_cli(a->fd, " == OSP Provider '%s' == \n", p->name);
+                       if (osp_security) {
+                               ast_cli(a->fd, "Local Private Key: %s\n", p->privatekey);
+                               ast_cli(a->fd, "Local Certificate: %s\n", p->localcert);
+                               for (i = 0; i < p->cacount; i++) {
+                                       ast_cli(a->fd, "CA Certificate %d:  %s\n", i + 1, p->cacerts[i]);
+                               }
                        }
                        for (i = 0; i < p->spcount; i++) {
-                               ast_cli(fd, "Service Point %d:   %s\n", i + 1, p->srvpoints[i]);
+                               ast_cli(a->fd, "Service Point %d:   %s\n", i + 1, p->srvpoints[i]);
                        }
-                       ast_cli(fd, "Max Connections:   %d\n", p->maxconnections);
-                       ast_cli(fd, "Retry Delay:       %d seconds\n", p->retrydelay);
-                       ast_cli(fd, "Retry Limit:       %d\n", p->retrylimit);
-                       ast_cli(fd, "Timeout:           %d milliseconds\n", p->timeout);
-                       ast_cli(fd, "Source:            %s\n", strlen(p->source) ? p->source : "<unspecified>");
-                       ast_cli(fd, "Auth Policy        %d\n", p->authpolicy);
-                       ast_cli(fd, "Default protocol   %s\n", p->defaultprotocol);
-                       ast_cli(fd, "OSP Handle:        %d\n", p->handle);
+                       ast_cli(a->fd, "Max Connections:   %d\n", p->maxconnections);
+                       ast_cli(a->fd, "Retry Delay:       %d seconds\n", p->retrydelay);
+                       ast_cli(a->fd, "Retry Limit:       %d\n", p->retrylimit);
+                       ast_cli(a->fd, "Timeout:           %d milliseconds\n", p->timeout);
+                       ast_cli(a->fd, "Source:            %s\n", strlen(p->source) ? p->source : "<unspecified>");
+                       ast_cli(a->fd, "Auth Policy        %d\n", p->authpolicy);
+                       ast_cli(a->fd, "Default protocol   %s\n", p->defaultprotocol);
+                       ast_cli(a->fd, "OSP Handle:        %d\n", p->handle);
                        found++;
                }
                p = p->next;
@@ -2046,94 +2147,42 @@ static int osp_show(
 
        if (!found) {
                if (provider) {
-                       ast_cli(fd, "Unable to find OSP provider '%s'\n", provider);
+                       ast_cli(a->fd, "Unable to find OSP provider '%s'\n", provider);
                } else {
-                       ast_cli(fd, "No OSP providers configured\n");
+                       ast_cli(a->fd, "No OSP providers configured\n");
                }
        }
-       return RESULT_SUCCESS;
+       return CLI_SUCCESS;
 }
 
-static const char* app1= "OSPAuth";
-static const char* synopsis1 = "OSP authentication";
-static const char* descrip1 =
-"  OSPAuth([provider[|options]]):  Authenticate a SIP INVITE by OSP and sets\n"
-"the variables:\n"
-" ${OSPINHANDLE}:  The inbound call transaction handle\n"
-" ${OSPINTIMELIMIT}:  The inbound call duration limit in seconds\n"
-"\n"
-"This application sets the following channel variable upon completion:\n"
-"      OSPAUTHSTATUS   The status of the OSP Auth attempt as a text string, one of\n"
-"              SUCCESS | FAILED | ERROR\n";
-
-static const char* app2= "OSPLookup";
-static const char* synopsis2 = "Lookup destination by OSP";
-static const char* descrip2 =
-"  OSPLookup(exten[|provider[|options]]):  Looks up an extension via OSP and sets\n"
-"the variables, where 'n' is the number of the result beginning with 1:\n"
-" ${OSPOUTHANDLE}:  The OSP Handle for anything remaining\n"
-" ${OSPTECH}:  The technology to use for the call\n"
-" ${OSPDEST}:  The destination to use for the call\n"
-" ${OSPCALLED}:  The called number to use for the call\n"
-" ${OSPCALLING}:  The calling number to use for the call\n"
-" ${OSPDIALSTR}:  The dial command string\n"
-" ${OSPOUTTOKEN}:  The actual OSP token as a string\n"
-" ${OSPOUTTIMELIMIT}:  The outbound call duration limit in seconds\n"
-" ${OSPOUTCALLIDTYPES}:  The outbound call id types\n"
-" ${OSPOUTCALLID}:  The outbound call id\n"
-" ${OSPRESULTS}:  The number of OSP results total remaining\n"
-"\n"
-"The option string may contain the following character:\n"
-"      'h' -- generate H323 call id for the outbound call\n"
-"      's' -- generate SIP call id for the outbound call. Have not been implemented\n"
-"      'i' -- generate IAX call id for the outbound call. Have not been implemented\n"
-"This application sets the following channel variable upon completion:\n"
-"      OSPLOOKUPSTATUS The status of the OSP Lookup attempt as a text string, one of\n"
-"              SUCCESS | FAILED | ERROR\n";
-
-static const char* app3 = "OSPNext";
-static const char* synopsis3 = "Lookup next destination by OSP";
-static const char* descrip3 =
-"  OSPNext(cause[|provider[|options]]):  Looks up the next OSP Destination for ${OSPOUTHANDLE}\n"
-"See OSPLookup for more information\n"
-"\n"
-"This application sets the following channel variable upon completion:\n"
-"      OSPNEXTSTATUS The status of the OSP Next attempt as a text string, one of\n"
-"              SUCCESS | FAILED | ERROR\n";
-
-static const char* app4 = "OSPFinish";
-static const char* synopsis4 = "Record OSP entry";
-static const char* descrip4 =
-"  OSPFinish([status[|options]]):  Records call state for ${OSPINHANDLE}, according to\n"
-"status, which should be one of BUSY, CONGESTION, ANSWER, NOANSWER, or CHANUNAVAIL\n"
-"or coincidentally, just what the Dial application stores in its ${DIALSTATUS}.\n"
-"\n"
-"This application sets the following channel variable upon completion:\n"
-"      OSPFINISHSTATUS The status of the OSP Finish attempt as a text string, one of\n"
-"              SUCCESS | FAILED | ERROR \n";
-
-static const char osp_usage[] =
-"Usage: osp show\n"
-"       Displays information on Open Settlement Protocol support\n";
+/* OSPAuth() dialplan application */
+static const char app1[] = "OSPAuth";
+
+/* OSPLookup() dialplan application */
+static const char app2[] = "OSPLookup";
+
+/* OSPNext() dialplan application */
+static const char app3[] = "OSPNext";
+
+/* OSPFinish() dialplan application */
+static const char app4[] = "OSPFinish";
 
 static struct ast_cli_entry cli_osp[] = {
-       { {"osp", "show", NULL},
-       osp_show, "Displays OSP information",
-       osp_usage },
+       AST_CLI_DEFINE(handle_cli_osp_show, "Displays OSF information")
 };
 
 static int load_module(void)
 {
        int res;
 
-       if(!osp_load())
+       if (!osp_load(0))
                return AST_MODULE_LOAD_DECLINE;
 
        ast_cli_register_multiple(cli_osp, sizeof(cli_osp) / sizeof(struct ast_cli_entry));
-       res = ast_register_application(app1, ospauth_exec, synopsis1, descrip1);
-       res |= ast_register_application(app2, osplookup_exec, synopsis2, descrip2);
-       res |= ast_register_application(app3, ospnext_exec, synopsis3, descrip3);
-       res |= ast_register_application(app4, ospfinished_exec, synopsis4, descrip4);
+       res = ast_register_application_xml(app1, ospauth_exec);
+       res |= ast_register_application_xml(app2, osplookup_exec);
+       res |= ast_register_application_xml(app3, ospnext_exec);
+       res |= ast_register_application_xml(app4, ospfinished_exec);
 
        return res;
 }
@@ -2149,15 +2198,12 @@ static int unload_module(void)
        ast_cli_unregister_multiple(cli_osp, sizeof(cli_osp) / sizeof(struct ast_cli_entry));
        osp_unload();
 
-       ast_module_user_hangup_all();
-
        return res;
 }
 
 static int reload(void)
 {
-       osp_unload();
-       osp_load();
+       osp_load(1);
 
        return 0;
 }
@@ -2166,4 +2212,4 @@ AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Open Settlement Protocol
        .load = load_module,
        .unload = unload_module,
        .reload = reload,
-       );
+);