add missing header file
[asterisk/asterisk.git] / apps / app_osplookup.c
index 3adde50..a65ecf6 100644 (file)
@@ -22,6 +22,9 @@
  *
  * \author Mark Spencer <markster@digium.com>
  *
+ * \extref The OSP Toolkit: http://www.transnexus.com
+ * \extref OpenSSL http://www.openssl.org
+ *
  * \ingroup applications
  */
 
 
 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 "asterisk/paths.h"
 #include "asterisk/lock.h"
 #include "asterisk/config.h"
 #include "asterisk/utils.h"
@@ -50,9 +49,7 @@ 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"
 
 /* OSP Buffer Sizes */
@@ -220,16 +217,14 @@ static int osp_create_provider(
                        } else {
                                snprintf(p->privatekey, sizeof(p->privatekey), "%s/%s", ast_config_AST_KEY_DIR, v->value);
                        }
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "OSP: privatekey '%s'\n", p->privatekey);
+                       ast_debug(1, "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 (option_debug)
-                               ast_log(LOG_DEBUG, "OSP: localcert '%s'\n", p->localcert);
+                       ast_debug(1, "OSP: localcert '%s'\n", p->localcert);
                } else if (!strcasecmp(v->name, "cacert")) {
                        if (p->cacount < OSP_MAX_CERTS) {
                                if (v->value[0] == '/') {
@@ -237,8 +232,7 @@ static int osp_create_provider(
                                } else {
                                        snprintf(p->cacerts[p->cacount], sizeof(p->cacerts[0]), "%s/%s", ast_config_AST_KEY_DIR, v->value);
                                }
-                               if (option_debug)
-                                       ast_log(LOG_DEBUG, "OSP: cacert[%d]: '%s'\n", p->cacount, p->cacerts[p->cacount]);
+                               ast_debug(1, "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);
@@ -246,8 +240,7 @@ static int osp_create_provider(
                } 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);
@@ -255,8 +248,7 @@ static int osp_create_provider(
                } else if (!strcasecmp(v->name, "maxconnections")) {
                        if ((sscanf(v->value, "%d", &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);
@@ -264,8 +256,7 @@ static int osp_create_provider(
                } else if (!strcasecmp(v->name, "retrydelay")) {
                        if ((sscanf(v->value, "%d", &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);
@@ -273,8 +264,7 @@ static int osp_create_provider(
                } else if (!strcasecmp(v->name, "retrylimit")) {
                        if ((sscanf(v->value, "%d", &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);
@@ -282,21 +272,18 @@ static int osp_create_provider(
                } else if (!strcasecmp(v->name, "timeout")) {
                        if ((sscanf(v->value, "%d", &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))) {
                                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);
@@ -304,16 +291,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,7 +309,7 @@ static int osp_create_provider(
        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);
-               free(p);
+               ast_free(p);
                return 0;
        }
 
@@ -333,16 +317,15 @@ static int osp_create_provider(
        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) {
-                       free(privatekey.PrivateKeyData);
+                       ast_free(privatekey.PrivateKeyData);
                }
-               free(p);
+               ast_free(p);
                return 0;
        }
 
        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]);
+               ast_debug(1, "OSP: cacert[%d]: '%s'\n", p->cacount, p->cacerts[p->cacount]);
                p->cacount++;
        }
        for (i = 0; i < p->cacount; i++) {
@@ -351,16 +334,16 @@ static int osp_create_provider(
                        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) {
-                                       free(cacerts[j].CertData);
+                                       ast_free(cacerts[j].CertData);
                                }
                        }
                        if (localcert.CertData) {
-                               free(localcert.CertData);
+                               ast_free(localcert.CertData);
                        }
                        if (privatekey.PrivateKeyData) {
-                               free(privatekey.PrivateKeyData);
+                               ast_free(privatekey.PrivateKeyData);
                        }
-                       free(p);
+                       ast_free(p);
                        return 0;
                }
                pcacerts[i] = &cacerts[i];
@@ -391,11 +374,10 @@ static int osp_create_provider(
                                &p->handle);
        if (error != OSPC_ERR_NO_ERROR) {
                ast_log(LOG_WARNING, "OSP: Unable to create provider '%s', error '%d'\n", provider, error);
-               free(p);
+               ast_free(p);
                res = -1;
        } else {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "OSP: provider '%s'\n", provider);
+               ast_debug(1, "OSP: provider '%s'\n", provider);
                ast_mutex_lock(&osplock);
                p->next = ospproviders;
                ospproviders = p;
@@ -405,14 +387,14 @@ static int osp_create_provider(
 
        for (i = 0; i < p->cacount; i++) {
                if (cacerts[i].CertData) {
-                       free(cacerts[i].CertData);
+                       ast_free(cacerts[i].CertData);
                }
        }
        if (localcert.CertData) {
-               free(localcert.CertData);
+               ast_free(localcert.CertData);
        }
        if (privatekey.PrivateKeyData) {
-               free(privatekey.PrivateKeyData);
+               ast_free(privatekey.PrivateKeyData);
        }
 
        return res;
@@ -436,8 +418,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;
                }
@@ -472,16 +453,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;
@@ -536,8 +514,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;
@@ -565,16 +543,13 @@ static int osp_validate_token(
                                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\n");
                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;
        }
 
@@ -628,15 +603,13 @@ static int osp_check_destination(
        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;
        }
@@ -648,14 +621,12 @@ static int osp_check_destination(
        }
 
        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';
@@ -667,24 +638,21 @@ static int osp_check_destination(
        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_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 OSPE_DEST_PROT_SIP:
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "OSP: protocol '%s'\n", OSP_PROT_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 OSPE_DEST_PROT_IAX:
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "OSP: protocol '%s'\n", OSP_PROT_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));
@@ -692,10 +660,9 @@ static int osp_check_destination(
                        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_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));
@@ -753,8 +720,7 @@ static int osp_auth(
        *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;
        }
 
@@ -766,8 +732,7 @@ static int osp_auth(
                        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");
+                               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) {
@@ -779,8 +744,7 @@ static int osp_auth(
                        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");
+                               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) {
@@ -925,14 +889,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);
@@ -977,8 +939,7 @@ static int osp_lookup(
        }
 
        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\n");
                result->numresults = 0;
                if (result->inhandle != OSP_INVALID_HANDLE) {
                        OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NORMAL_UNSPECIFIED);
@@ -987,8 +948,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);
                }
@@ -1016,8 +976,7 @@ static int osp_lookup(
                                &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\n");
                result->numresults = 0;
                result->outtimelimit = OSP_DEF_TIMELIMIT;
                if (result->inhandle != OSP_INVALID_HANDLE) {
@@ -1028,21 +987,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) {
@@ -1076,18 +1032,16 @@ static int osp_lookup(
                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);
@@ -1096,8 +1050,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) {
@@ -1112,6 +1065,7 @@ static int osp_lookup(
 
 /*!
  * \brief OSP Lookup Next function
+ * \param provider OSP provider name
  * \param cause Asterisk hangup cuase
  * \param result Lookup results, in/output
  * \return 1 Found , 0 No route, -1 Error
@@ -1140,14 +1094,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);
@@ -1158,8 +1110,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);
@@ -1192,19 +1143,17 @@ static int osp_next(
                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);
@@ -1213,8 +1162,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;
@@ -1284,12 +1232,10 @@ static int osp_finish(
                                &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,9 +1256,7 @@ static int ospauth_exec(
        void* data)
 {
        int res;
-       struct ast_module_user* u;
        const char* provider = OSP_DEF_PROVIDER;
-       int priority_jump = 0;
        struct varshead* headp;
        struct ast_var_t* current;
        const char* source = "";
@@ -1328,11 +1272,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;
        }
 
@@ -1341,14 +1282,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);
-
-       if ((args.options) && (strchr(args.options, 'j'))) {
-               priority_jump = 1;
-       }
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPAuth: priority jump '%d'\n", priority_jump);
+       ast_debug(1, "OSPAuth: provider '%s'\n", provider);
 
        headp = &chan->varshead;
        AST_LIST_TRAVERSE(headp, current, entries) {
@@ -1359,10 +1293,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;
@@ -1377,29 +1309,19 @@ 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) {
-               if (priority_jump || ast_opt_priority_jumping) {
-                       ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
-                       res = 0;
-               } else {
-                       res = -1;
-               }
+               res = -1;
        } else {
                res = 0;
        }
 
-       ast_module_user_remove(u);
-
        return res;
 }
 
@@ -1414,9 +1336,7 @@ static int osplookup_exec(
        void* data)
 {
        int res, cres;
-       struct ast_module_user* u;
        const char* provider = OSP_DEF_PROVIDER;
-       int priority_jump = 0;
        struct varshead* headp;
        struct ast_var_t* current;
        const char* srcdev = "";
@@ -1438,29 +1358,21 @@ 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, 'j')) {
-                       priority_jump = 1;
-               }
                if (strchr(args.options, 'h')) {
                        callidtypes |= OSP_CALLID_H323;
                }
@@ -1471,10 +1383,7 @@ static int osplookup_exec(
                        callidtypes |= OSP_CALLID_IAX;
                }
        }
-       if (option_debug) {
-               ast_log(LOG_DEBUG, "OSPLookup: priority jump '%d'\n", priority_jump);
-               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;
@@ -1495,15 +1404,12 @@ static int osplookup_exec(
                        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", netid);
+       ast_debug(1, "OSPLookup: source device '%s'\n", srcdev);
 
        if ((cres = ast_autoservice_start(chan)) < 0) {
-               ast_module_user_remove(u);
                return -1;
        }
 
@@ -1529,38 +1435,28 @@ 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, "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)) {
@@ -1577,8 +1473,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);
@@ -1586,23 +1481,15 @@ static int osplookup_exec(
        }
 
        if ((cres = ast_autoservice_stop(chan)) < 0) {
-               ast_module_user_remove(u);
                return -1;
        }
 
        if(res <= 0) {
-               if (priority_jump || ast_opt_priority_jumping) {
-                       ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
-                       res = 0;
-               } else {
-                       res = -1;
-               }
+               res = -1;
        } else {
                res = 0;
        }
 
-       ast_module_user_remove(u);
-
        return res;
 }
 
@@ -1617,9 +1504,7 @@ static int ospnext_exec(
        void* data)
 {
        int res;
-       struct ast_module_user* u;
        const char* provider = OSP_DEF_PROVIDER;
-       int priority_jump = 0;
        int cause = 0;
        struct varshead* headp;
        struct ast_var_t* current;
@@ -1640,11 +1525,8 @@ 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;
        }
 
@@ -1653,20 +1535,12 @@ static int ospnext_exec(
        if (!ast_strlen_zero(args.cause) && sscanf(args.cause, "%d", &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);
-
-       if ((args.options) && (strchr(args.options, 'j'))) {
-               priority_jump = 1;
-       }
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPNext: priority jump '%d'\n", priority_jump);
+       ast_debug(1, "OSPlookup: provider '%s'\n", provider);
 
        result.inhandle = OSP_INVALID_HANDLE;
        result.outhandle = OSP_INVALID_HANDLE;
@@ -1697,13 +1571,11 @@ static int ospnext_exec(
                        }
                }
        }
-       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;
@@ -1726,31 +1598,23 @@ 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, "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)) {
@@ -1767,8 +1631,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);
@@ -1776,18 +1639,11 @@ static int ospnext_exec(
        }
 
        if(res <= 0) {
-               if (priority_jump || ast_opt_priority_jumping) {
-                       ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
-                       res = 0;
-               } else {
-                       res = -1;
-               }
+               res = -1;
        } else {
                res = 0;
        }
 
-       ast_module_user_remove(u);
-
        return res;
 }
 
@@ -1802,8 +1658,6 @@ static int ospfinished_exec(
        void* data)
 {
        int res = 1;
-       struct ast_module_user* u;
-       int priority_jump = 0;
        int cause = 0;
        struct varshead* headp;
        struct ast_var_t* current;
@@ -1821,22 +1675,13 @@ 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;
        }
 
        AST_STANDARD_APP_ARGS(args, tmp);
 
-       if ((args.options) && (strchr(args.options, 'j'))) {
-               priority_jump = 1;
-       }
-       if (option_debug)
-               ast_log(LOG_DEBUG, "OSPFinish: priority jump '%d'\n", priority_jump);
-
        headp = &chan->varshead;
        AST_LIST_TRAVERSE(headp, current, entries) {
                if (!strcasecmp(ast_var_name(current), "OSPINHANDLE")) {
@@ -1857,17 +1702,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) {
                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;
@@ -1882,17 +1724,14 @@ 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:
@@ -1902,8 +1741,7 @@ static int ospfinished_exec(
                        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);
@@ -1919,32 +1757,32 @@ static int ospfinished_exec(
        pbx_builtin_setvar_helper(chan, "OSPFINISHSTATUS", status);
 
        if(!res) {
-               if (priority_jump || ast_opt_priority_jumping) {
-                       ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
-                       res = 0;
-               } else {
-                       res = -1;
-               }
+               res = -1;
        } else {
                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;
+
        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) {
@@ -1956,8 +1794,7 @@ 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, "tokenformat");
                if (t) {
@@ -1970,8 +1807,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) {
@@ -1988,8 +1824,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;
 }
@@ -2005,7 +1840,7 @@ static int osp_unload(void)
                while(p) {
                        next = p->next;
                        OSPPProviderDelete(p->handle, 0);
-                       free(p);
+                       ast_free(p);
                        p = next;
                }
                ospproviders = NULL;
@@ -2020,10 +1855,7 @@ static int osp_unload(void)
        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;
@@ -2031,12 +1863,21 @@ 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:
@@ -2050,7 +1891,7 @@ static int osp_show(
                                tokenalgo = "Signed";
                                break;
                }
-               ast_cli(fd, "OSP: %s %s %s\n",
+               ast_cli(a->fd, "OSP: %s %s %s\n",
                        osp_initialized ? "Initialized" : "Uninitialized", osp_hardware ? "Accelerated" : "Normal", tokenalgo);
        }
 
@@ -2059,25 +1900,25 @@ 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);
+                       ast_cli(a->fd, " == OSP Provider '%s' == \n", p->name);
+                       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(fd, "CA Certificate %d:  %s\n", i + 1, p->cacerts[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;
@@ -2086,12 +1927,12 @@ 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";
@@ -2102,8 +1943,6 @@ static const char* descrip1 =
 " ${OSPINHANDLE}:  The inbound call transaction handle\n"
 " ${OSPINTIMELIMIT}:  The inbound call duration limit in seconds\n"
 "\n"
-"The option string may contain the following character:\n"
-"      'j' -- jump to n+101 priority if the authentication was NOT successful\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";
@@ -2126,7 +1965,6 @@ static const char* descrip2 =
 " ${OSPRESULTS}:  The number of OSP results total remaining\n"
 "\n"
 "The option string may contain the following character:\n"
-"      'j' -- jump to n+101 priority if the lookup was NOT successful\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"
@@ -2140,8 +1978,6 @@ static const char* descrip3 =
 "  OSPNext(cause[|provider[|options]]):  Looks up the next OSP Destination for ${OSPOUTHANDLE}\n"
 "See OSPLookup for more information\n"
 "\n"
-"The option string may contain the following character:\n"
-"      'j' -- jump to n+101 priority if the lookup was NOT successful\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";
@@ -2153,27 +1989,19 @@ static const char* descrip4 =
 "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"
-"The option string may contain the following character:\n"
-"      'j' -- jump to n+101 priority if the finish attempt was NOT successful\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";
-
 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));
@@ -2196,15 +2024,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;
 }