app_osplookup.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2006, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!
  19. * \file
  20. * \brief Open Settlement Protocol (OSP) Applications
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * \extref The OSP Toolkit: http://www.transnexus.com
  25. * \extref OpenSSL http://www.openssl.org
  26. *
  27. * \ingroup applications
  28. */
  29. /*** MODULEINFO
  30. <depend>osptk</depend>
  31. <depend>ssl</depend>
  32. ***/
  33. #include "asterisk.h"
  34. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  35. #include <osp/osp.h>
  36. #include <osp/osputils.h>
  37. #include "asterisk/paths.h"
  38. #include "asterisk/lock.h"
  39. #include "asterisk/config.h"
  40. #include "asterisk/utils.h"
  41. #include "asterisk/causes.h"
  42. #include "asterisk/channel.h"
  43. #include "asterisk/app.h"
  44. #include "asterisk/module.h"
  45. #include "asterisk/pbx.h"
  46. #include "asterisk/cli.h"
  47. #include "asterisk/astosp.h"
  48. /* OSP Buffer Sizes */
  49. #define OSP_INTSTR_SIZE ((unsigned int)16) /* OSP signed/unsigned int string buffer size */
  50. #define OSP_NORSTR_SIZE ((unsigned int)256) /* OSP normal string buffer size */
  51. #define OSP_TOKSTR_SIZE ((unsigned int)4096) /* OSP token string buffer size */
  52. #define OSP_TECHSTR_SIZE ((unsigned int)32) /* OSP signed/unsigned int string buffer size */
  53. #define OSP_UUID_SIZE ((unsigned int)16) /* UUID size */
  54. #define OSP_UUIDSTR_SIZE ((unsigned int)36) /* UUID string size */
  55. /* OSP Authentication Policy */
  56. enum osp_authpolicy {
  57. OSP_AUTH_NO, /* Accept any call */
  58. OSP_AUTH_YES, /* Accept call with valid OSP token or without OSP token */
  59. OSP_AUTH_EXCLUSIVE /* Only accept call with valid OSP token */
  60. };
  61. /* Call ID type*/
  62. #define OSP_CALLID_UNDEFINED ((unsigned int)0) /* UNDEFINED */
  63. #define OSP_CALLID_H323 ((unsigned int)(1 << 0)) /* H.323 */
  64. #define OSP_CALLID_SIP ((unsigned int)(1 << 1)) /* SIP */
  65. #define OSP_CALLID_IAX ((unsigned int)(1 << 2)) /* IAX2 */
  66. #define OSP_CALLID_MAXNUM ((unsigned int)3) /* Max number of call ID type */
  67. /* OSP Supported Destination Protocols */
  68. #define OSP_PROT_H323 ((char*)"H323") /* H323 Q931 protocol name*/
  69. #define OSP_PROT_SIP ((char*)"SIP") /* SIP protocol name */
  70. #define OSP_PROT_IAX ((char*)"IAX") /* IAX protocol name */
  71. #define OSP_PROT_OTHER ((char*)"OTHER") /* Other protocol name */
  72. /* OSP supported Destination Tech */
  73. #if 0
  74. #define OSP_TECH_H323 ((char*)"OOH323") /* OOH323 tech name */
  75. #endif
  76. #define OSP_TECH_H323 ((char*)"H323") /* OH323 tech name */
  77. #define OSP_TECH_SIP ((char*)"SIP") /* SIP tech name */
  78. #define OSP_TECH_IAX ((char*)"IAX2") /* IAX2 tech name */
  79. /* SIP OSP header field name */
  80. #define OSP_SIP_HEADER ((char*)"P-OSP-Auth-Token: ")
  81. /* OSP Constants */
  82. #define OSP_INVALID_HANDLE ((int)-1) /* Invalid OSP handle, provider, transaction etc. */
  83. #define OSP_CONFIG_FILE ((const char*)"osp.conf") /* OSP configuration file name */
  84. #define OSP_GENERAL_CAT ((const char*)"general") /* OSP global configuration context name */
  85. #define OSP_DEF_PROVIDER ((const char*)"default") /* OSP default provider context name */
  86. #define OSP_MAX_CERTS ((unsigned int)10) /* OSP max number of cacerts */
  87. #define OSP_MAX_SRVS ((unsigned int)10) /* OSP max number of service points */
  88. #define OSP_DEF_MAXCONNECTIONS ((unsigned int)20) /* OSP default max_connections */
  89. #define OSP_MIN_MAXCONNECTIONS ((unsigned int)1) /* OSP min max_connections */
  90. #define OSP_MAX_MAXCONNECTIONS ((unsigned int)1000) /* OSP max max_connections */
  91. #define OSP_DEF_RETRYDELAY ((unsigned int)0) /* OSP default retry delay */
  92. #define OSP_MIN_RETRYDELAY ((unsigned int)0) /* OSP min retry delay */
  93. #define OSP_MAX_RETRYDELAY ((unsigned int)10) /* OSP max retry delay */
  94. #define OSP_DEF_RETRYLIMIT ((unsigned int)2) /* OSP default retry times */
  95. #define OSP_MIN_RETRYLIMIT ((unsigned int)0) /* OSP min retry times */
  96. #define OSP_MAX_RETRYLIMIT ((unsigned int)100) /* OSP max retry times */
  97. #define OSP_DEF_TIMEOUT ((unsigned int)500) /* OSP default timeout in ms */
  98. #define OSP_MIN_TIMEOUT ((unsigned int)200) /* OSP min timeout in ms */
  99. #define OSP_MAX_TIMEOUT ((unsigned int)10000) /* OSP max timeout in ms */
  100. #define OSP_DEF_AUTHPOLICY ((enum osp_authpolicy)OSP_AUTH_YES)
  101. #define OSP_AUDIT_URL ((const char*)"localhost") /* OSP default Audit URL */
  102. #define OSP_LOCAL_VALIDATION ((int)1) /* Validate OSP token locally */
  103. #define OSP_SSL_LIFETIME ((unsigned int)300) /* SSL life time, in seconds */
  104. #define OSP_HTTP_PERSISTENCE ((int)1) /* In seconds */
  105. #define OSP_CUSTOMER_ID ((const char*)"") /* OSP customer ID */
  106. #define OSP_DEVICE_ID ((const char*)"") /* OSP device ID */
  107. #define OSP_DEF_DESTINATIONS ((unsigned int)5) /* OSP default max number of destinations */
  108. #define OSP_DEF_TIMELIMIT ((unsigned int)0) /* OSP default duration limit, no limit */
  109. #define OSP_DEF_PROTOCOL OSP_PROT_SIP /* OSP default destination protocol, SIP */
  110. /* OSP Provider */
  111. struct osp_provider {
  112. char name[OSP_NORSTR_SIZE]; /* OSP provider context name */
  113. char privatekey[OSP_NORSTR_SIZE]; /* OSP private key file name */
  114. char localcert[OSP_NORSTR_SIZE]; /* OSP local cert file name */
  115. unsigned int cacount; /* Number of cacerts */
  116. char cacerts[OSP_MAX_CERTS][OSP_NORSTR_SIZE]; /* Cacert file names */
  117. unsigned int spcount; /* Number of service points */
  118. char srvpoints[OSP_MAX_SRVS][OSP_NORSTR_SIZE]; /* Service point URLs */
  119. int maxconnections; /* Max number of connections */
  120. int retrydelay; /* Retry delay */
  121. int retrylimit; /* Retry limit */
  122. int timeout; /* Timeout in ms */
  123. char source[OSP_NORSTR_SIZE]; /* IP of self */
  124. enum osp_authpolicy authpolicy; /* OSP authentication policy */
  125. char* defaultprotocol; /* OSP default destination protocol */
  126. OSPTPROVHANDLE handle; /* OSP provider handle */
  127. struct osp_provider* next; /* Pointer to next OSP provider */
  128. };
  129. /* Call ID */
  130. struct osp_callid {
  131. unsigned char buf[OSP_NORSTR_SIZE]; /* Call ID string */
  132. unsigned int len; /* Call ID length */
  133. };
  134. /* OSP Application In/Output Results */
  135. struct osp_result {
  136. int inhandle; /* Inbound transaction handle */
  137. int outhandle; /* Outbound transaction handle */
  138. unsigned int intimelimit; /* Inbound duration limit */
  139. unsigned int outtimelimit; /* Outbound duration limit */
  140. char tech[OSP_TECHSTR_SIZE]; /* Outbound Asterisk TECH string */
  141. char dest[OSP_NORSTR_SIZE]; /* Outbound destination IP address */
  142. char called[OSP_NORSTR_SIZE]; /* Outbound called number, may be translated */
  143. char calling[OSP_NORSTR_SIZE]; /* Outbound calling number, may be translated */
  144. char token[OSP_TOKSTR_SIZE]; /* Outbound OSP token */
  145. char networkid[OSP_NORSTR_SIZE]; /* Outbound network ID */
  146. unsigned int numresults; /* Number of remain outbound destinations */
  147. struct osp_callid outcallid; /* Outbound call ID */
  148. };
  149. /* OSP Module Global Variables */
  150. AST_MUTEX_DEFINE_STATIC(osplock); /* Lock of OSP provider list */
  151. static int osp_initialized = 0; /* Init flag */
  152. static int osp_hardware = 0; /* Hardware accelleration flag */
  153. static struct osp_provider* ospproviders = NULL; /* OSP provider list */
  154. static unsigned int osp_tokenformat = TOKEN_ALGO_SIGNED; /* Token format supported */
  155. /* OSP Client Wrapper APIs */
  156. /*!
  157. * \brief Create OSP provider handle according to configuration
  158. * \param cfg OSP configuration
  159. * \param provider OSP provider context name
  160. * \return 1 Success, 0 Failed, -1 Error
  161. */
  162. static int osp_create_provider(
  163. struct ast_config* cfg,
  164. const char* provider)
  165. {
  166. int res = 0;
  167. struct ast_variable* v;
  168. struct osp_provider* p;
  169. OSPTPRIVATEKEY privatekey;
  170. OSPT_CERT localcert;
  171. OSPT_CERT cacerts[OSP_MAX_CERTS];
  172. const OSPT_CERT* pcacerts[OSP_MAX_CERTS];
  173. const char* psrvpoints[OSP_MAX_SRVS];
  174. int t, i, j, error = OSPC_ERR_NO_ERROR;
  175. if (!(p = ast_calloc(1, sizeof(*p)))) {
  176. ast_log(LOG_ERROR, "Out of memory\n");
  177. return -1;
  178. }
  179. /* ast_calloc has set 0 in p */
  180. ast_copy_string(p->name, provider, sizeof(p->name));
  181. snprintf(p->privatekey, sizeof(p->privatekey), "%s/%s-privatekey.pem", ast_config_AST_KEY_DIR, provider);
  182. snprintf(p->localcert, sizeof(p->localcert), "%s/%s-localcert.pem", ast_config_AST_KEY_DIR, provider);
  183. snprintf(p->cacerts[0], sizeof(p->cacerts[0]), "%s/%s-cacert_0.pem", ast_config_AST_KEY_DIR, provider);
  184. p->maxconnections = OSP_DEF_MAXCONNECTIONS;
  185. p->retrydelay = OSP_DEF_RETRYDELAY;
  186. p->retrylimit = OSP_DEF_RETRYLIMIT;
  187. p->timeout = OSP_DEF_TIMEOUT;
  188. p->authpolicy = OSP_DEF_AUTHPOLICY;
  189. p->defaultprotocol = OSP_DEF_PROTOCOL;
  190. p->handle = OSP_INVALID_HANDLE;
  191. v = ast_variable_browse(cfg, provider);
  192. while(v) {
  193. if (!strcasecmp(v->name, "privatekey")) {
  194. if (v->value[0] == '/') {
  195. ast_copy_string(p->privatekey, v->value, sizeof(p->privatekey));
  196. } else {
  197. snprintf(p->privatekey, sizeof(p->privatekey), "%s/%s", ast_config_AST_KEY_DIR, v->value);
  198. }
  199. ast_debug(1, "OSP: privatekey '%s'\n", p->privatekey);
  200. } else if (!strcasecmp(v->name, "localcert")) {
  201. if (v->value[0] == '/') {
  202. ast_copy_string(p->localcert, v->value, sizeof(p->localcert));
  203. } else {
  204. snprintf(p->localcert, sizeof(p->localcert), "%s/%s", ast_config_AST_KEY_DIR, v->value);
  205. }
  206. ast_debug(1, "OSP: localcert '%s'\n", p->localcert);
  207. } else if (!strcasecmp(v->name, "cacert")) {
  208. if (p->cacount < OSP_MAX_CERTS) {
  209. if (v->value[0] == '/') {
  210. ast_copy_string(p->cacerts[p->cacount], v->value, sizeof(p->cacerts[0]));
  211. } else {
  212. snprintf(p->cacerts[p->cacount], sizeof(p->cacerts[0]), "%s/%s", ast_config_AST_KEY_DIR, v->value);
  213. }
  214. ast_debug(1, "OSP: cacert[%d]: '%s'\n", p->cacount, p->cacerts[p->cacount]);
  215. p->cacount++;
  216. } else {
  217. ast_log(LOG_WARNING, "OSP: Too many CA Certificates at line %d\n", v->lineno);
  218. }
  219. } else if (!strcasecmp(v->name, "servicepoint")) {
  220. if (p->spcount < OSP_MAX_SRVS) {
  221. ast_copy_string(p->srvpoints[p->spcount], v->value, sizeof(p->srvpoints[0]));
  222. ast_debug(1, "OSP: servicepoint[%d]: '%s'\n", p->spcount, p->srvpoints[p->spcount]);
  223. p->spcount++;
  224. } else {
  225. ast_log(LOG_WARNING, "OSP: Too many Service Points at line %d\n", v->lineno);
  226. }
  227. } else if (!strcasecmp(v->name, "maxconnections")) {
  228. if ((sscanf(v->value, "%30d", &t) == 1) && (t >= OSP_MIN_MAXCONNECTIONS) && (t <= OSP_MAX_MAXCONNECTIONS)) {
  229. p->maxconnections = t;
  230. ast_debug(1, "OSP: maxconnections '%d'\n", t);
  231. } else {
  232. ast_log(LOG_WARNING, "OSP: maxconnections should be an integer from %d to %d, not '%s' at line %d\n",
  233. OSP_MIN_MAXCONNECTIONS, OSP_MAX_MAXCONNECTIONS, v->value, v->lineno);
  234. }
  235. } else if (!strcasecmp(v->name, "retrydelay")) {
  236. if ((sscanf(v->value, "%30d", &t) == 1) && (t >= OSP_MIN_RETRYDELAY) && (t <= OSP_MAX_RETRYDELAY)) {
  237. p->retrydelay = t;
  238. ast_debug(1, "OSP: retrydelay '%d'\n", t);
  239. } else {
  240. ast_log(LOG_WARNING, "OSP: retrydelay should be an integer from %d to %d, not '%s' at line %d\n",
  241. OSP_MIN_RETRYDELAY, OSP_MAX_RETRYDELAY, v->value, v->lineno);
  242. }
  243. } else if (!strcasecmp(v->name, "retrylimit")) {
  244. if ((sscanf(v->value, "%30d", &t) == 1) && (t >= OSP_MIN_RETRYLIMIT) && (t <= OSP_MAX_RETRYLIMIT)) {
  245. p->retrylimit = t;
  246. ast_debug(1, "OSP: retrylimit '%d'\n", t);
  247. } else {
  248. ast_log(LOG_WARNING, "OSP: retrylimit should be an integer from %d to %d, not '%s' at line %d\n",
  249. OSP_MIN_RETRYLIMIT, OSP_MAX_RETRYLIMIT, v->value, v->lineno);
  250. }
  251. } else if (!strcasecmp(v->name, "timeout")) {
  252. if ((sscanf(v->value, "%30d", &t) == 1) && (t >= OSP_MIN_TIMEOUT) && (t <= OSP_MAX_TIMEOUT)) {
  253. p->timeout = t;
  254. ast_debug(1, "OSP: timeout '%d'\n", t);
  255. } else {
  256. ast_log(LOG_WARNING, "OSP: timeout should be an integer from %d to %d, not '%s' at line %d\n",
  257. OSP_MIN_TIMEOUT, OSP_MAX_TIMEOUT, v->value, v->lineno);
  258. }
  259. } else if (!strcasecmp(v->name, "source")) {
  260. ast_copy_string(p->source, v->value, sizeof(p->source));
  261. ast_debug(1, "OSP: source '%s'\n", p->source);
  262. } else if (!strcasecmp(v->name, "authpolicy")) {
  263. if ((sscanf(v->value, "%30d", &t) == 1) && ((t == OSP_AUTH_NO) || (t == OSP_AUTH_YES) || (t == OSP_AUTH_EXCLUSIVE))) {
  264. p->authpolicy = t;
  265. ast_debug(1, "OSP: authpolicy '%d'\n", t);
  266. } else {
  267. ast_log(LOG_WARNING, "OSP: authpolicy should be %d, %d or %d, not '%s' at line %d\n",
  268. OSP_AUTH_NO, OSP_AUTH_YES, OSP_AUTH_EXCLUSIVE, v->value, v->lineno);
  269. }
  270. } else if (!strcasecmp(v->name, "defaultprotocol")) {
  271. if (!strcasecmp(v->value, OSP_PROT_SIP)) {
  272. p->defaultprotocol = OSP_PROT_SIP;
  273. ast_debug(1, "OSP: default protocol '%s'\n", p->defaultprotocol);
  274. } else if (!strcasecmp(v->value, OSP_PROT_H323)) {
  275. p->defaultprotocol = OSP_PROT_H323;
  276. ast_debug(1, "OSP: default protocol '%s'\n", p->defaultprotocol);
  277. } else if (!strcasecmp(v->value, OSP_PROT_IAX)) {
  278. p->defaultprotocol = OSP_PROT_IAX;
  279. ast_debug(1, "OSP: default protocol '%s'\n", p->defaultprotocol);
  280. } else {
  281. ast_log(LOG_WARNING, "OSP: default protocol should be %s, %s, %s, or %s not '%s' at line %d\n",
  282. OSP_PROT_SIP, OSP_PROT_H323, OSP_PROT_IAX, OSP_PROT_OTHER, v->value, v->lineno);
  283. }
  284. }
  285. v = v->next;
  286. }
  287. error = OSPPUtilLoadPEMPrivateKey((unsigned char*)p->privatekey, &privatekey);
  288. if (error != OSPC_ERR_NO_ERROR) {
  289. ast_log(LOG_WARNING, "OSP: Unable to load privatekey '%s', error '%d'\n", p->privatekey, error);
  290. ast_free(p);
  291. return 0;
  292. }
  293. error = OSPPUtilLoadPEMCert((unsigned char*)p->localcert, &localcert);
  294. if (error != OSPC_ERR_NO_ERROR) {
  295. ast_log(LOG_WARNING, "OSP: Unable to load localcert '%s', error '%d'\n", p->localcert, error);
  296. if (privatekey.PrivateKeyData) {
  297. ast_free(privatekey.PrivateKeyData);
  298. }
  299. ast_free(p);
  300. return 0;
  301. }
  302. if (p->cacount < 1) {
  303. snprintf(p->cacerts[p->cacount], sizeof(p->cacerts[0]), "%s/%s-cacert.pem", ast_config_AST_KEY_DIR, provider);
  304. ast_debug(1, "OSP: cacert[%d]: '%s'\n", p->cacount, p->cacerts[p->cacount]);
  305. p->cacount++;
  306. }
  307. for (i = 0; i < p->cacount; i++) {
  308. error = OSPPUtilLoadPEMCert((unsigned char*)p->cacerts[i], &cacerts[i]);
  309. if (error != OSPC_ERR_NO_ERROR) {
  310. ast_log(LOG_WARNING, "OSP: Unable to load cacert '%s', error '%d'\n", p->cacerts[i], error);
  311. for (j = 0; j < i; j++) {
  312. if (cacerts[j].CertData) {
  313. ast_free(cacerts[j].CertData);
  314. }
  315. }
  316. if (localcert.CertData) {
  317. ast_free(localcert.CertData);
  318. }
  319. if (privatekey.PrivateKeyData) {
  320. ast_free(privatekey.PrivateKeyData);
  321. }
  322. ast_free(p);
  323. return 0;
  324. }
  325. pcacerts[i] = &cacerts[i];
  326. }
  327. for (i = 0; i < p->spcount; i++) {
  328. psrvpoints[i] = p->srvpoints[i];
  329. }
  330. error = OSPPProviderNew(
  331. p->spcount,
  332. psrvpoints,
  333. NULL,
  334. OSP_AUDIT_URL,
  335. &privatekey,
  336. &localcert,
  337. p->cacount,
  338. pcacerts,
  339. OSP_LOCAL_VALIDATION,
  340. OSP_SSL_LIFETIME,
  341. p->maxconnections,
  342. OSP_HTTP_PERSISTENCE,
  343. p->retrydelay,
  344. p->retrylimit,
  345. p->timeout,
  346. OSP_CUSTOMER_ID,
  347. OSP_DEVICE_ID,
  348. &p->handle);
  349. if (error != OSPC_ERR_NO_ERROR) {
  350. ast_log(LOG_WARNING, "OSP: Unable to create provider '%s', error '%d'\n", provider, error);
  351. ast_free(p);
  352. res = -1;
  353. } else {
  354. ast_debug(1, "OSP: provider '%s'\n", provider);
  355. ast_mutex_lock(&osplock);
  356. p->next = ospproviders;
  357. ospproviders = p;
  358. ast_mutex_unlock(&osplock);
  359. res = 1;
  360. }
  361. for (i = 0; i < p->cacount; i++) {
  362. if (cacerts[i].CertData) {
  363. ast_free(cacerts[i].CertData);
  364. }
  365. }
  366. if (localcert.CertData) {
  367. ast_free(localcert.CertData);
  368. }
  369. if (privatekey.PrivateKeyData) {
  370. ast_free(privatekey.PrivateKeyData);
  371. }
  372. return res;
  373. }
  374. /*!
  375. * \brief Get OSP provider by name
  376. * \param name OSP provider context name
  377. * \param provider OSP provider structure
  378. * \return 1 Success, 0 Failed, -1 Error
  379. */
  380. static int osp_get_provider(
  381. const char* name,
  382. struct osp_provider** provider)
  383. {
  384. int res = 0;
  385. struct osp_provider* p;
  386. ast_mutex_lock(&osplock);
  387. p = ospproviders;
  388. while(p) {
  389. if (!strcasecmp(p->name, name)) {
  390. *provider = p;
  391. ast_debug(1, "OSP: find provider '%s'\n", name);
  392. res = 1;
  393. break;
  394. }
  395. p = p->next;
  396. }
  397. ast_mutex_unlock(&osplock);
  398. return res;
  399. }
  400. /*!
  401. * \brief Create OSP transaction handle
  402. * \param provider OSP provider context name
  403. * \param transaction OSP transaction handle, output
  404. * \param sourcesize Size of source buffer, in/output
  405. * \param source Source of provider, output
  406. * \return 1 Success, 0 Failed, -1 Error
  407. */
  408. static int osp_create_transaction(
  409. const char* provider,
  410. int* transaction,
  411. unsigned int sourcesize,
  412. char* source)
  413. {
  414. int res = 0;
  415. struct osp_provider* p;
  416. int error;
  417. ast_mutex_lock(&osplock);
  418. p = ospproviders;
  419. while(p) {
  420. if (!strcasecmp(p->name, provider)) {
  421. error = OSPPTransactionNew(p->handle, transaction);
  422. if (error == OSPC_ERR_NO_ERROR) {
  423. ast_debug(1, "OSP: transaction '%d'\n", *transaction);
  424. ast_copy_string(source, p->source, sourcesize);
  425. ast_debug(1, "OSP: source '%s'\n", source);
  426. res = 1;
  427. } else {
  428. *transaction = OSP_INVALID_HANDLE;
  429. ast_debug(1, "OSP: Unable to create transaction handle, error '%d'\n", error);
  430. res = -1;
  431. }
  432. break;
  433. }
  434. p = p->next;
  435. }
  436. ast_mutex_unlock(&osplock);
  437. return res;
  438. }
  439. /*!
  440. * \brief Convert address to "[x.x.x.x]" or "host.domain" format
  441. * \param src Source address string
  442. * \param dst Destination address string
  443. * \param buffersize Size of dst buffer
  444. */
  445. static void osp_convert_address(
  446. const char* src,
  447. char* dst,
  448. int buffersize)
  449. {
  450. struct in_addr inp;
  451. if (inet_aton(src, &inp) != 0) {
  452. snprintf(dst, buffersize, "[%s]", src);
  453. } else {
  454. snprintf(dst, buffersize, "%s", src);
  455. }
  456. }
  457. /*!
  458. * \brief Validate OSP token of inbound call
  459. * \param transaction OSP transaction handle
  460. * \param source Source of inbound call
  461. * \param destination Destination of inbound call
  462. * \param calling Calling number
  463. * \param called Called number
  464. * \param token OSP token, may be empty
  465. * \param timelimit Call duration limit, output
  466. * \return 1 Success, 0 Failed, -1 Error
  467. */
  468. static int osp_validate_token(
  469. int transaction,
  470. const char* source,
  471. const char* destination,
  472. const char* calling,
  473. const char* called,
  474. const char* token,
  475. unsigned int* timelimit)
  476. {
  477. int res;
  478. int tokenlen;
  479. unsigned char tokenstr[OSP_TOKSTR_SIZE];
  480. char src[OSP_NORSTR_SIZE];
  481. char dst[OSP_NORSTR_SIZE];
  482. unsigned int authorised;
  483. unsigned int dummy = 0;
  484. int error;
  485. tokenlen = ast_base64decode(tokenstr, token, strlen(token));
  486. osp_convert_address(source, src, sizeof(src));
  487. osp_convert_address(destination, dst, sizeof(dst));
  488. error = OSPPTransactionValidateAuthorisation(
  489. transaction,
  490. src,
  491. dst,
  492. NULL,
  493. NULL,
  494. calling ? calling : "",
  495. OSPC_NFORMAT_E164,
  496. called,
  497. OSPC_NFORMAT_E164,
  498. 0,
  499. NULL,
  500. tokenlen,
  501. (char*)tokenstr,
  502. &authorised,
  503. timelimit,
  504. &dummy,
  505. NULL,
  506. osp_tokenformat);
  507. if (error != OSPC_ERR_NO_ERROR) {
  508. ast_debug(1, "OSP: Unable to validate inbound token, error '%d'\n", error);
  509. res = -1;
  510. } else if (authorised) {
  511. ast_debug(1, "OSP: Authorised\n");
  512. res = 1;
  513. } else {
  514. ast_debug(1, "OSP: Unauthorised\n");
  515. res = 0;
  516. }
  517. return res;
  518. }
  519. /*!
  520. * \brief Choose min duration limit
  521. * \param in Inbound duration limit
  522. * \param out Outbound duration limit
  523. * \return min duration limit
  524. */
  525. static unsigned int osp_choose_timelimit(
  526. unsigned int in,
  527. unsigned int out)
  528. {
  529. if (in == OSP_DEF_TIMELIMIT) {
  530. return out;
  531. } else if (out == OSP_DEF_TIMELIMIT) {
  532. return in;
  533. } else {
  534. return in < out ? in : out;
  535. }
  536. }
  537. /*!
  538. * \brief Choose min duration limit
  539. * \param provider OSP provider
  540. * \param called Called number
  541. * \param calling Calling number
  542. * \param destination Destination IP in '[x.x.x.x]' format
  543. * \param tokenlen OSP token length
  544. * \param token OSP token
  545. * \param reason Failure reason, output
  546. * \param result OSP lookup results, in/output
  547. * \return 1 Success, 0 Failed, -1 Error
  548. */
  549. static int osp_check_destination(
  550. struct osp_provider* provider,
  551. const char* called,
  552. const char* calling,
  553. char* destination,
  554. unsigned int tokenlen,
  555. const char* token,
  556. OSPEFAILREASON* reason,
  557. struct osp_result* result)
  558. {
  559. int res;
  560. OSPE_DEST_OSPENABLED enabled;
  561. OSPE_DEST_PROTOCOL protocol;
  562. int error;
  563. if (strlen(destination) <= 2) {
  564. ast_debug(1, "OSP: Wrong destination format '%s'\n", destination);
  565. *reason = OSPC_FAIL_NORMAL_UNSPECIFIED;
  566. return -1;
  567. }
  568. if ((error = OSPPTransactionIsDestOSPEnabled(result->outhandle, &enabled)) != OSPC_ERR_NO_ERROR) {
  569. ast_debug(1, "OSP: Unable to get destination OSP version, error '%d'\n", error);
  570. *reason = OSPC_FAIL_NORMAL_UNSPECIFIED;
  571. return -1;
  572. }
  573. if (enabled == OSPC_DOSP_FALSE) {
  574. result->token[0] = '\0';
  575. } else {
  576. ast_base64encode(result->token, (const unsigned char*)token, tokenlen, sizeof(result->token) - 1);
  577. }
  578. if ((error = OSPPTransactionGetDestNetworkId(result->outhandle, result->networkid)) != OSPC_ERR_NO_ERROR) {
  579. ast_debug(1, "OSP: Unable to get destination network ID, error '%d'\n", error);
  580. result->networkid[0] = '\0';
  581. }
  582. if ((error = OSPPTransactionGetDestProtocol(result->outhandle, &protocol)) != OSPC_ERR_NO_ERROR) {
  583. ast_debug(1, "OSP: Unable to get destination protocol, error '%d'\n", error);
  584. *reason = OSPC_FAIL_NORMAL_UNSPECIFIED;
  585. result->token[0] = '\0';
  586. result->networkid[0] = '\0';
  587. return -1;
  588. }
  589. res = 1;
  590. /* Strip leading and trailing brackets */
  591. destination[strlen(destination) - 1] = '\0';
  592. switch(protocol) {
  593. case OSPC_DPROT_Q931:
  594. ast_debug(1, "OSP: protocol '%s'\n", OSP_PROT_H323);
  595. ast_copy_string(result->tech, OSP_TECH_H323, sizeof(result->tech));
  596. ast_copy_string(result->dest, destination + 1, sizeof(result->dest));
  597. ast_copy_string(result->called, called, sizeof(result->called));
  598. ast_copy_string(result->calling, calling, sizeof(result->calling));
  599. break;
  600. case OSPC_DPROT_SIP:
  601. ast_debug(1, "OSP: protocol '%s'\n", OSP_PROT_SIP);
  602. ast_copy_string(result->tech, OSP_TECH_SIP, sizeof(result->tech));
  603. ast_copy_string(result->dest, destination + 1, sizeof(result->dest));
  604. ast_copy_string(result->called, called, sizeof(result->called));
  605. ast_copy_string(result->calling, calling, sizeof(result->calling));
  606. break;
  607. case OSPC_DPROT_IAX:
  608. ast_debug(1, "OSP: protocol '%s'\n", OSP_PROT_IAX);
  609. ast_copy_string(result->tech, OSP_TECH_IAX, sizeof(result->tech));
  610. ast_copy_string(result->dest, destination + 1, sizeof(result->dest));
  611. ast_copy_string(result->called, called, sizeof(result->called));
  612. ast_copy_string(result->calling, calling, sizeof(result->calling));
  613. break;
  614. case OSPC_DPROT_UNDEFINED:
  615. case OSPC_DPROT_UNKNOWN:
  616. ast_debug(1, "OSP: unknown/undefined protocol '%d'\n", protocol);
  617. ast_debug(1, "OSP: use default protocol '%s'\n", provider->defaultprotocol);
  618. ast_copy_string(result->tech, provider->defaultprotocol, sizeof(result->tech));
  619. ast_copy_string(result->dest, destination + 1, sizeof(result->dest));
  620. ast_copy_string(result->called, called, sizeof(result->called));
  621. ast_copy_string(result->calling, calling, sizeof(result->calling));
  622. break;
  623. case OSPC_DPROT_LRQ:
  624. default:
  625. ast_log(LOG_WARNING, "OSP: unsupported protocol '%d'\n", protocol);
  626. *reason = OSPC_FAIL_PROTOCOL_ERROR;
  627. result->token[0] = '\0';
  628. result->networkid[0] = '\0';
  629. res = 0;
  630. break;
  631. }
  632. return res;
  633. }
  634. /*!
  635. * \brief Convert Asterisk status to TC code
  636. * \param cause Asterisk hangup cause
  637. * \return OSP TC code
  638. */
  639. static OSPEFAILREASON asterisk2osp(
  640. int cause)
  641. {
  642. return (OSPEFAILREASON)cause;
  643. }
  644. /*!
  645. * \brief OSP Authentication function
  646. * \param provider OSP provider context name
  647. * \param transaction OSP transaction handle, output
  648. * \param source Source of inbound call
  649. * \param calling Calling number
  650. * \param called Called number
  651. * \param token OSP token, may be empty
  652. * \param timelimit Call duration limit, output
  653. * \return 1 Authenricated, 0 Unauthenticated, -1 Error
  654. */
  655. static int osp_auth(
  656. const char* provider,
  657. int* transaction,
  658. const char* source,
  659. const char* calling,
  660. const char* called,
  661. const char* token,
  662. unsigned int* timelimit)
  663. {
  664. int res;
  665. struct osp_provider* p = NULL;
  666. char dest[OSP_NORSTR_SIZE];
  667. *transaction = OSP_INVALID_HANDLE;
  668. *timelimit = OSP_DEF_TIMELIMIT;
  669. if ((res = osp_get_provider(provider, &p)) <= 0) {
  670. ast_debug(1, "OSP: Unabe to find OSP provider '%s'\n", provider);
  671. return res;
  672. }
  673. switch (p->authpolicy) {
  674. case OSP_AUTH_NO:
  675. res = 1;
  676. break;
  677. case OSP_AUTH_EXCLUSIVE:
  678. if (ast_strlen_zero(token)) {
  679. res = 0;
  680. } else if ((res = osp_create_transaction(provider, transaction, sizeof(dest), dest)) <= 0) {
  681. ast_debug(1, "OSP: Unable to generate transaction handle\n");
  682. *transaction = OSP_INVALID_HANDLE;
  683. res = 0;
  684. } else if((res = osp_validate_token(*transaction, source, dest, calling, called, token, timelimit)) <= 0) {
  685. OSPPTransactionRecordFailure(*transaction, OSPC_FAIL_CALL_REJECTED);
  686. }
  687. break;
  688. case OSP_AUTH_YES:
  689. default:
  690. if (ast_strlen_zero(token)) {
  691. res = 1;
  692. } else if ((res = osp_create_transaction(provider, transaction, sizeof(dest), dest)) <= 0) {
  693. ast_debug(1, "OSP: Unable to generate transaction handle\n");
  694. *transaction = OSP_INVALID_HANDLE;
  695. res = 0;
  696. } else if((res = osp_validate_token(*transaction, source, dest, calling, called, token, timelimit)) <= 0) {
  697. OSPPTransactionRecordFailure(*transaction, OSPC_FAIL_CALL_REJECTED);
  698. }
  699. break;
  700. }
  701. return res;
  702. }
  703. /*!
  704. * \brief Create a UUID
  705. * \param uuid UUID buffer
  706. * \param buffersize UUID buffer size
  707. * \return 1 Created, -1 Error
  708. */
  709. static int osp_create_uuid(
  710. unsigned char* uuid,
  711. unsigned int* buffersize)
  712. {
  713. int i, res;
  714. long int* tmp;
  715. if (*buffersize >= OSP_UUID_SIZE) {
  716. tmp = (long int*)uuid;
  717. for (i = 0; i < OSP_UUID_SIZE / sizeof(long int); i++) {
  718. tmp[i] = ast_random();
  719. }
  720. *buffersize = OSP_UUID_SIZE;
  721. res = 1;
  722. } else {
  723. res = -1;
  724. }
  725. return res;
  726. }
  727. /*!
  728. * \brief UUID to string
  729. * \param uuid UUID
  730. * \param buffer String buffer
  731. * \param buffersize String buffer size
  732. * \return 1 Successed, -1 Error
  733. */
  734. static int osp_uuid2str(
  735. unsigned char* uuid,
  736. char* buffer,
  737. unsigned int buffersize)
  738. {
  739. int res;
  740. if (buffersize > OSP_UUIDSTR_SIZE) {
  741. snprintf(buffer, buffersize, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
  742. uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7],
  743. uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]);
  744. res = 1;
  745. } else {
  746. res = -1;
  747. }
  748. return res;
  749. }
  750. /*!
  751. * \brief Create a call ID according to the type
  752. * \param type Call ID type
  753. * \param callid Call ID buffer
  754. * \return 1 Created, 0 Not create, -1 Error
  755. */
  756. static int osp_create_callid(
  757. unsigned int type,
  758. struct osp_callid* callid)
  759. {
  760. int res;
  761. callid->len = sizeof(callid->buf);
  762. switch (type) {
  763. case OSP_CALLID_H323:
  764. res = osp_create_uuid(callid->buf, &callid->len);
  765. break;
  766. case OSP_CALLID_SIP:
  767. case OSP_CALLID_IAX:
  768. res = 0;
  769. default:
  770. res = -1;
  771. break;
  772. }
  773. if ((res != 1) && (callid->len != 0)) {
  774. callid->buf[0] = '\0';
  775. callid->len = 0;
  776. }
  777. return res;
  778. }
  779. /*!
  780. * \brief OSP Lookup function
  781. * \param provider OSP provider context name
  782. * \param srcdev Source device of outbound call
  783. * \param calling Calling number
  784. * \param called Called number
  785. * \param callidtypes Call ID types
  786. * \param result Lookup results
  787. * \return 1 Found , 0 No route, -1 Error
  788. */
  789. static int osp_lookup(
  790. const char* provider,
  791. const char* srcdev,
  792. const char* calling,
  793. const char* called,
  794. unsigned int callidtypes,
  795. struct osp_result* result)
  796. {
  797. int res;
  798. struct osp_provider* p = NULL;
  799. char source[OSP_NORSTR_SIZE];
  800. char callingnum[OSP_NORSTR_SIZE];
  801. char callednum[OSP_NORSTR_SIZE];
  802. char destination[OSP_NORSTR_SIZE];
  803. unsigned int tokenlen;
  804. char token[OSP_TOKSTR_SIZE];
  805. char src[OSP_NORSTR_SIZE];
  806. char dev[OSP_NORSTR_SIZE];
  807. unsigned int i, type;
  808. struct osp_callid callid;
  809. unsigned int callidnum;
  810. OSPT_CALL_ID* callids[OSP_CALLID_MAXNUM];
  811. unsigned int dummy = 0;
  812. OSPEFAILREASON reason;
  813. int error;
  814. result->outhandle = OSP_INVALID_HANDLE;
  815. result->tech[0] = '\0';
  816. result->dest[0] = '\0';
  817. result->called[0] = '\0';
  818. result->calling[0] = '\0';
  819. result->token[0] = '\0';
  820. result->networkid[0] = '\0';
  821. result->numresults = 0;
  822. result->outtimelimit = OSP_DEF_TIMELIMIT;
  823. if ((res = osp_get_provider(provider, &p)) <= 0) {
  824. ast_debug(1, "OSP: Unabe to find OSP provider '%s'\n", provider);
  825. return res;
  826. }
  827. if ((res = osp_create_transaction(provider, &result->outhandle, sizeof(source), source)) <= 0) {
  828. ast_debug(1, "OSP: Unable to generate transaction handle\n");
  829. result->outhandle = OSP_INVALID_HANDLE;
  830. if (result->inhandle != OSP_INVALID_HANDLE) {
  831. OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NORMAL_UNSPECIFIED);
  832. }
  833. return -1;
  834. }
  835. callidnum = 0;
  836. callids[0] = NULL;
  837. for (i = 0; i < OSP_CALLID_MAXNUM; i++) {
  838. type = 1 << i;
  839. if (callidtypes & type) {
  840. error = osp_create_callid(type, &callid);
  841. if (error == 1) {
  842. callids[callidnum] = OSPPCallIdNew(callid.len, callid.buf);
  843. callidnum++;
  844. }
  845. }
  846. }
  847. osp_convert_address(source, src, sizeof(src));
  848. osp_convert_address(srcdev, dev, sizeof(dev));
  849. result->numresults = OSP_DEF_DESTINATIONS;
  850. error = OSPPTransactionRequestAuthorisation(
  851. result->outhandle,
  852. src,
  853. dev,
  854. calling ? calling : "",
  855. OSPC_NFORMAT_E164,
  856. called,
  857. OSPC_NFORMAT_E164,
  858. NULL,
  859. callidnum,
  860. callids,
  861. NULL,
  862. &result->numresults,
  863. &dummy,
  864. NULL);
  865. for (i = 0; i < callidnum; i++) {
  866. OSPPCallIdDelete(&callids[i]);
  867. }
  868. if (error != OSPC_ERR_NO_ERROR) {
  869. ast_debug(1, "OSP: Unable to request authorization, error '%d'\n", error);
  870. result->numresults = 0;
  871. if (result->inhandle != OSP_INVALID_HANDLE) {
  872. OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NORMAL_UNSPECIFIED);
  873. }
  874. return -1;
  875. }
  876. if (!result->numresults) {
  877. ast_debug(1, "OSP: No more destination\n");
  878. if (result->inhandle != OSP_INVALID_HANDLE) {
  879. OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NO_ROUTE_TO_DEST);
  880. }
  881. return 0;
  882. }
  883. result->outcallid.len = sizeof(result->outcallid.buf);
  884. tokenlen = sizeof(token);
  885. error = OSPPTransactionGetFirstDestination(
  886. result->outhandle,
  887. 0,
  888. NULL,
  889. NULL,
  890. &result->outtimelimit,
  891. &result->outcallid.len,
  892. result->outcallid.buf,
  893. sizeof(callednum),
  894. callednum,
  895. sizeof(callingnum),
  896. callingnum,
  897. sizeof(destination),
  898. destination,
  899. 0,
  900. NULL,
  901. &tokenlen,
  902. token);
  903. if (error != OSPC_ERR_NO_ERROR) {
  904. ast_debug(1, "OSP: Unable to get first route, error '%d'\n", error);
  905. result->numresults = 0;
  906. result->outtimelimit = OSP_DEF_TIMELIMIT;
  907. if (result->inhandle != OSP_INVALID_HANDLE) {
  908. OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NO_ROUTE_TO_DEST);
  909. }
  910. return -1;
  911. }
  912. result->numresults--;
  913. result->outtimelimit = osp_choose_timelimit(result->intimelimit, result->outtimelimit);
  914. ast_debug(1, "OSP: outtimelimit '%d'\n", result->outtimelimit);
  915. ast_debug(1, "OSP: called '%s'\n", callednum);
  916. ast_debug(1, "OSP: calling '%s'\n", callingnum);
  917. ast_debug(1, "OSP: destination '%s'\n", destination);
  918. ast_debug(1, "OSP: token size '%d'\n", tokenlen);
  919. if ((res = osp_check_destination(p, callednum, callingnum, destination, tokenlen, token, &reason, result)) > 0) {
  920. return 1;
  921. }
  922. if (!result->numresults) {
  923. ast_debug(1, "OSP: No more destination\n");
  924. result->outtimelimit = OSP_DEF_TIMELIMIT;
  925. OSPPTransactionRecordFailure(result->outhandle, reason);
  926. if (result->inhandle != OSP_INVALID_HANDLE) {
  927. OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NO_ROUTE_TO_DEST);
  928. }
  929. return 0;
  930. }
  931. while(result->numresults) {
  932. result->outcallid.len = sizeof(result->outcallid.buf);
  933. tokenlen = sizeof(token);
  934. error = OSPPTransactionGetNextDestination(
  935. result->outhandle,
  936. reason,
  937. 0,
  938. NULL,
  939. NULL,
  940. &result->outtimelimit,
  941. &result->outcallid.len,
  942. result->outcallid.buf,
  943. sizeof(callednum),
  944. callednum,
  945. sizeof(callingnum),
  946. callingnum,
  947. sizeof(destination),
  948. destination,
  949. 0,
  950. NULL,
  951. &tokenlen,
  952. token);
  953. if (error == OSPC_ERR_NO_ERROR) {
  954. result->numresults--;
  955. result->outtimelimit = osp_choose_timelimit(result->intimelimit, result->outtimelimit);
  956. ast_debug(1, "OSP: outtimelimit '%d'\n", result->outtimelimit);
  957. ast_debug(1, "OSP: called '%s'\n", callednum);
  958. ast_debug(1, "OSP: calling '%s'\n", callingnum);
  959. ast_debug(1, "OSP: destination '%s'\n", destination);
  960. ast_debug(1, "OSP: token size '%d'\n", tokenlen);
  961. if ((res = osp_check_destination(p, callednum, callingnum, destination, tokenlen, token, &reason, result)) > 0) {
  962. break;
  963. } else if (!result->numresults) {
  964. ast_debug(1, "OSP: No more destination\n");
  965. OSPPTransactionRecordFailure(result->outhandle, reason);
  966. if (result->inhandle != OSP_INVALID_HANDLE) {
  967. OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NO_ROUTE_TO_DEST);
  968. }
  969. res = 0;
  970. break;
  971. }
  972. } else {
  973. ast_debug(1, "OSP: Unable to get route, error '%d'\n", error);
  974. result->numresults = 0;
  975. result->outtimelimit = OSP_DEF_TIMELIMIT;
  976. if (result->inhandle != OSP_INVALID_HANDLE) {
  977. OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NORMAL_UNSPECIFIED);
  978. }
  979. res = -1;
  980. break;
  981. }
  982. }
  983. return res;
  984. }
  985. /*!
  986. * \brief OSP Lookup Next function
  987. * \param provider OSP provider name
  988. * \param cause Asterisk hangup cuase
  989. * \param result Lookup results, in/output
  990. * \return 1 Found , 0 No route, -1 Error
  991. */
  992. static int osp_next(
  993. const char* provider,
  994. int cause,
  995. struct osp_result* result)
  996. {
  997. int res;
  998. struct osp_provider* p = NULL;
  999. char callingnum[OSP_NORSTR_SIZE];
  1000. char callednum[OSP_NORSTR_SIZE];
  1001. char destination[OSP_NORSTR_SIZE];
  1002. unsigned int tokenlen;
  1003. char token[OSP_TOKSTR_SIZE];
  1004. OSPEFAILREASON reason;
  1005. int error;
  1006. result->tech[0] = '\0';
  1007. result->dest[0] = '\0';
  1008. result->called[0] = '\0';
  1009. result->calling[0] = '\0';
  1010. result->token[0] = '\0';
  1011. result->networkid[0] = '\0';
  1012. result->outtimelimit = OSP_DEF_TIMELIMIT;
  1013. if ((res = osp_get_provider(provider, &p)) <= 0) {
  1014. ast_debug(1, "OSP: Unabe to find OSP provider '%s'\n", provider);
  1015. return res;
  1016. }
  1017. if (result->outhandle == OSP_INVALID_HANDLE) {
  1018. ast_debug(1, "OSP: Transaction handle undefined\n");
  1019. result->numresults = 0;
  1020. if (result->inhandle != OSP_INVALID_HANDLE) {
  1021. OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NORMAL_UNSPECIFIED);
  1022. }
  1023. return -1;
  1024. }
  1025. reason = asterisk2osp(cause);
  1026. if (!result->numresults) {
  1027. ast_debug(1, "OSP: No more destination\n");
  1028. OSPPTransactionRecordFailure(result->outhandle, reason);
  1029. if (result->inhandle != OSP_INVALID_HANDLE) {
  1030. OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NO_ROUTE_TO_DEST);
  1031. }
  1032. return 0;
  1033. }
  1034. while(result->numresults) {
  1035. result->outcallid.len = sizeof(result->outcallid.buf);
  1036. tokenlen = sizeof(token);
  1037. error = OSPPTransactionGetNextDestination(
  1038. result->outhandle,
  1039. reason,
  1040. 0,
  1041. NULL,
  1042. NULL,
  1043. &result->outtimelimit,
  1044. &result->outcallid.len,
  1045. result->outcallid.buf,
  1046. sizeof(callednum),
  1047. callednum,
  1048. sizeof(callingnum),
  1049. callingnum,
  1050. sizeof(destination),
  1051. destination,
  1052. 0,
  1053. NULL,
  1054. &tokenlen,
  1055. token);
  1056. if (error == OSPC_ERR_NO_ERROR) {
  1057. result->numresults--;
  1058. result->outtimelimit = osp_choose_timelimit(result->intimelimit, result->outtimelimit);
  1059. ast_debug(1, "OSP: outtimelimit '%d'\n", result->outtimelimit);
  1060. ast_debug(1, "OSP: called '%s'\n", callednum);
  1061. ast_debug(1, "OSP: calling '%s'\n", callingnum);
  1062. ast_debug(1, "OSP: destination '%s'\n", destination);
  1063. ast_debug(1, "OSP: token size '%d'\n", tokenlen);
  1064. if ((res = osp_check_destination(p, callednum, callingnum, destination, tokenlen, token, &reason, result)) > 0) {
  1065. res = 1;
  1066. break;
  1067. } else if (!result->numresults) {
  1068. ast_debug(1, "OSP: No more destination\n");
  1069. OSPPTransactionRecordFailure(result->outhandle, reason);
  1070. if (result->inhandle != OSP_INVALID_HANDLE) {
  1071. OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NO_ROUTE_TO_DEST);
  1072. }
  1073. res = 0;
  1074. break;
  1075. }
  1076. } else {
  1077. ast_debug(1, "OSP: Unable to get route, error '%d'\n", error);
  1078. result->token[0] = '\0';
  1079. result->numresults = 0;
  1080. result->outtimelimit = OSP_DEF_TIMELIMIT;
  1081. if (result->inhandle != OSP_INVALID_HANDLE) {
  1082. OSPPTransactionRecordFailure(result->inhandle, OSPC_FAIL_NORMAL_UNSPECIFIED);
  1083. }
  1084. res = -1;
  1085. break;
  1086. }
  1087. }
  1088. return res;
  1089. }
  1090. /*!
  1091. * \brief OSP Finish function
  1092. * \param handle OSP in/outbound transaction handle
  1093. * \param recorded If failure reason has been recorded
  1094. * \param cause Asterisk hangup cause
  1095. * \param start Call start time
  1096. * \param connect Call connect time
  1097. * \param end Call end time
  1098. * \param release Who release first, 0 source, 1 destination
  1099. * \return 1 Success, 0 Failed, -1 Error
  1100. */
  1101. static int osp_finish(
  1102. int handle,
  1103. int recorded,
  1104. int cause,
  1105. time_t start,
  1106. time_t connect,
  1107. time_t end,
  1108. unsigned int release)
  1109. {
  1110. int res;
  1111. OSPEFAILREASON reason;
  1112. time_t alert = 0;
  1113. unsigned isPddInfoPresent = 0;
  1114. unsigned pdd = 0;
  1115. unsigned int dummy = 0;
  1116. int error;
  1117. if (handle == OSP_INVALID_HANDLE) {
  1118. return 0;
  1119. }
  1120. if (!recorded) {
  1121. reason = asterisk2osp(cause);
  1122. OSPPTransactionRecordFailure(handle, reason);
  1123. }
  1124. error = OSPPTransactionReportUsage(
  1125. handle,
  1126. difftime(end, connect),
  1127. start,
  1128. end,
  1129. alert,
  1130. connect,
  1131. isPddInfoPresent,
  1132. pdd,
  1133. release,
  1134. NULL,
  1135. -1,
  1136. -1,
  1137. -1,
  1138. -1,
  1139. &dummy,
  1140. NULL);
  1141. if (error == OSPC_ERR_NO_ERROR) {
  1142. ast_debug(1, "OSP: Usage reported\n");
  1143. res = 1;
  1144. } else {
  1145. ast_debug(1, "OSP: Unable to report usage, error '%d'\n", error);
  1146. res = -1;
  1147. }
  1148. OSPPTransactionDelete(handle);
  1149. return res;
  1150. }
  1151. /* OSP Application APIs */
  1152. /*!
  1153. * \brief OSP Application OSPAuth
  1154. * \param chan Channel
  1155. * \param data Parameter
  1156. * \return 0 Success, -1 Failed
  1157. */
  1158. static int ospauth_exec(
  1159. struct ast_channel* chan,
  1160. void* data)
  1161. {
  1162. int res;
  1163. const char* provider = OSP_DEF_PROVIDER;
  1164. struct varshead* headp;
  1165. struct ast_var_t* current;
  1166. const char* source = "";
  1167. const char* token = "";
  1168. int handle;
  1169. unsigned int timelimit;
  1170. char buffer[OSP_INTSTR_SIZE];
  1171. const char* status;
  1172. char* tmp;
  1173. AST_DECLARE_APP_ARGS(args,
  1174. AST_APP_ARG(provider);
  1175. AST_APP_ARG(options);
  1176. );
  1177. if (!(tmp = ast_strdupa(data))) {
  1178. ast_log(LOG_ERROR, "Out of memory\n");
  1179. return -1;
  1180. }
  1181. AST_STANDARD_APP_ARGS(args, tmp);
  1182. if (!ast_strlen_zero(args.provider)) {
  1183. provider = args.provider;
  1184. }
  1185. ast_debug(1, "OSPAuth: provider '%s'\n", provider);
  1186. headp = &chan->varshead;
  1187. AST_LIST_TRAVERSE(headp, current, entries) {
  1188. if (!strcasecmp(ast_var_name(current), "OSPPEERIP")) {
  1189. source = ast_var_value(current);
  1190. } else if (!strcasecmp(ast_var_name(current), "OSPINTOKEN")) {
  1191. token = ast_var_value(current);
  1192. }
  1193. }
  1194. ast_debug(1, "OSPAuth: source '%s'\n", source);
  1195. ast_debug(1, "OSPAuth: token size '%zd'\n", strlen(token));
  1196. if ((res = osp_auth(provider, &handle, source, chan->cid.cid_num, chan->exten, token, &timelimit)) > 0) {
  1197. status = AST_OSP_SUCCESS;
  1198. } else {
  1199. timelimit = OSP_DEF_TIMELIMIT;
  1200. if (!res) {
  1201. status = AST_OSP_FAILED;
  1202. } else {
  1203. status = AST_OSP_ERROR;
  1204. }
  1205. }
  1206. snprintf(buffer, sizeof(buffer), "%d", handle);
  1207. pbx_builtin_setvar_helper(chan, "OSPINHANDLE", buffer);
  1208. ast_debug(1, "OSPAuth: OSPINHANDLE '%s'\n", buffer);
  1209. snprintf(buffer, sizeof(buffer), "%d", timelimit);
  1210. pbx_builtin_setvar_helper(chan, "OSPINTIMELIMIT", buffer);
  1211. ast_debug(1, "OSPAuth: OSPINTIMELIMIT '%s'\n", buffer);
  1212. pbx_builtin_setvar_helper(chan, "OSPAUTHSTATUS", status);
  1213. ast_debug(1, "OSPAuth: %s\n", status);
  1214. if(res <= 0) {
  1215. res = -1;
  1216. } else {
  1217. res = 0;
  1218. }
  1219. return res;
  1220. }
  1221. /*!
  1222. * \brief OSP Application OSPLookup
  1223. * \param chan Channel
  1224. * \param data Parameter
  1225. * \return 0 Success, -1 Failed
  1226. */
  1227. static int osplookup_exec(
  1228. struct ast_channel* chan,
  1229. void* data)
  1230. {
  1231. int res, cres;
  1232. const char* provider = OSP_DEF_PROVIDER;
  1233. struct varshead* headp;
  1234. struct ast_var_t* current;
  1235. const char* srcdev = "";
  1236. const char* snetid = "";
  1237. char buffer[OSP_TOKSTR_SIZE];
  1238. unsigned int callidtypes = OSP_CALLID_UNDEFINED;
  1239. struct osp_result result;
  1240. const char* status;
  1241. char* tmp;
  1242. AST_DECLARE_APP_ARGS(args,
  1243. AST_APP_ARG(exten);
  1244. AST_APP_ARG(provider);
  1245. AST_APP_ARG(options);
  1246. );
  1247. if (ast_strlen_zero(data)) {
  1248. ast_log(LOG_WARNING, "OSPLookup: Arg required, OSPLookup(exten[|provider[|options]])\n");
  1249. return -1;
  1250. }
  1251. if (!(tmp = ast_strdupa(data))) {
  1252. ast_log(LOG_ERROR, "Out of memory\n");
  1253. return -1;
  1254. }
  1255. AST_STANDARD_APP_ARGS(args, tmp);
  1256. ast_debug(1, "OSPLookup: exten '%s'\n", args.exten);
  1257. if (!ast_strlen_zero(args.provider)) {
  1258. provider = args.provider;
  1259. }
  1260. ast_debug(1, "OSPlookup: provider '%s'\n", provider);
  1261. if (args.options) {
  1262. if (strchr(args.options, 'h')) {
  1263. callidtypes |= OSP_CALLID_H323;
  1264. }
  1265. if (strchr(args.options, 's')) {
  1266. callidtypes |= OSP_CALLID_SIP;
  1267. }
  1268. if (strchr(args.options, 'i')) {
  1269. callidtypes |= OSP_CALLID_IAX;
  1270. }
  1271. }
  1272. ast_debug(1, "OSPLookup: call id types '%d'\n", callidtypes);
  1273. result.inhandle = OSP_INVALID_HANDLE;
  1274. result.intimelimit = OSP_DEF_TIMELIMIT;
  1275. headp = &chan->varshead;
  1276. AST_LIST_TRAVERSE(headp, current, entries) {
  1277. if (!strcasecmp(ast_var_name(current), "OSPINHANDLE")) {
  1278. if (sscanf(ast_var_value(current), "%30d", &result.inhandle) != 1) {
  1279. result.inhandle = OSP_INVALID_HANDLE;
  1280. }
  1281. } else if (!strcasecmp(ast_var_name(current), "OSPINTIMELIMIT")) {
  1282. if (sscanf(ast_var_value(current), "%30d", &result.intimelimit) != 1) {
  1283. result.intimelimit = OSP_DEF_TIMELIMIT;
  1284. }
  1285. } else if (!strcasecmp(ast_var_name(current), "OSPINNETWORKID")) {
  1286. snetid = ast_var_value(current);
  1287. } else if (!strcasecmp(ast_var_name(current), "OSPPEERIP")) {
  1288. srcdev = ast_var_value(current);
  1289. }
  1290. }
  1291. ast_debug(1, "OSPLookup: OSPINHANDLE '%d'\n", result.inhandle);
  1292. ast_debug(1, "OSPLookup: OSPINTIMELIMIT '%d'\n", result.intimelimit);
  1293. ast_debug(1, "OSPLookup: OSPINNETWORKID '%s'\n", snetid);
  1294. ast_debug(1, "OSPLookup: source device '%s'\n", srcdev);
  1295. if ((cres = ast_autoservice_start(chan)) < 0) {
  1296. return -1;
  1297. }
  1298. if ((res = osp_lookup(provider, srcdev, chan->cid.cid_num, args.exten, callidtypes, &result)) > 0) {
  1299. status = AST_OSP_SUCCESS;
  1300. } else {
  1301. result.tech[0] = '\0';
  1302. result.dest[0] = '\0';
  1303. result.called[0] = '\0';
  1304. result.calling[0] = '\0';
  1305. result.token[0] = '\0';
  1306. result.networkid[0] = '\0';
  1307. result.numresults = 0;
  1308. result.outtimelimit = OSP_DEF_TIMELIMIT;
  1309. result.outcallid.buf[0] = '\0';
  1310. result.outcallid.len = 0;
  1311. if (!res) {
  1312. status = AST_OSP_FAILED;
  1313. } else {
  1314. status = AST_OSP_ERROR;
  1315. }
  1316. }
  1317. snprintf(buffer, sizeof(buffer), "%d", result.outhandle);
  1318. pbx_builtin_setvar_helper(chan, "OSPOUTHANDLE", buffer);
  1319. ast_debug(1, "OSPLookup: OSPOUTHANDLE '%s'\n", buffer);
  1320. pbx_builtin_setvar_helper(chan, "OSPTECH", result.tech);
  1321. ast_debug(1, "OSPLookup: OSPTECH '%s'\n", result.tech);
  1322. pbx_builtin_setvar_helper(chan, "OSPDEST", result.dest);
  1323. ast_debug(1, "OSPLookup: OSPDEST '%s'\n", result.dest);
  1324. pbx_builtin_setvar_helper(chan, "OSPCALLED", result.called);
  1325. ast_debug(1, "OSPLookup: OSPCALLED '%s'\n", result.called);
  1326. pbx_builtin_setvar_helper(chan, "OSPCALLING", result.calling);
  1327. ast_debug(1, "OSPLookup: OSPCALLING '%s'\n", result.calling);
  1328. pbx_builtin_setvar_helper(chan, "OSPOUTTOKEN", result.token);
  1329. ast_debug(1, "OSPLookup: OSPOUTTOKEN size '%zd'\n", strlen(result.token));
  1330. snprintf(buffer, sizeof(buffer), "%d", result.numresults);
  1331. pbx_builtin_setvar_helper(chan, "OSPRESULTS", buffer);
  1332. ast_debug(1, "OSPLookup: OSPRESULTS '%s'\n", buffer);
  1333. snprintf(buffer, sizeof(buffer), "%d", result.outtimelimit);
  1334. pbx_builtin_setvar_helper(chan, "OSPOUTTIMELIMIT", buffer);
  1335. ast_debug(1, "OSPLookup: OSPOUTTIMELIMIT '%s'\n", buffer);
  1336. snprintf(buffer, sizeof(buffer), "%d", callidtypes);
  1337. pbx_builtin_setvar_helper(chan, "OSPOUTCALLIDTYPES", buffer);
  1338. ast_debug(1, "OSPLookup: OSPOUTCALLIDTYPES '%s'\n", buffer);
  1339. pbx_builtin_setvar_helper(chan, "OSPLOOKUPSTATUS", status);
  1340. ast_debug(1, "OSPLookup: %s\n", status);
  1341. if (!strcasecmp(result.tech, OSP_TECH_H323)) {
  1342. if ((callidtypes & OSP_CALLID_H323) && (result.outcallid.len != 0)) {
  1343. osp_uuid2str(result.outcallid.buf, buffer, sizeof(buffer));
  1344. } else {
  1345. buffer[0] = '\0';
  1346. }
  1347. pbx_builtin_setvar_helper(chan, "OSPOUTCALLID", buffer);
  1348. snprintf(buffer, sizeof(buffer), "%s/%s@%s", result.tech, result.called, result.dest);
  1349. pbx_builtin_setvar_helper(chan, "OSPDIALSTR", buffer);
  1350. } else if (!strcasecmp(result.tech, OSP_TECH_SIP)) {
  1351. snprintf(buffer, sizeof(buffer), "%s/%s@%s", result.tech, result.called, result.dest);
  1352. pbx_builtin_setvar_helper(chan, "OSPDIALSTR", buffer);
  1353. if (!ast_strlen_zero(result.token)) {
  1354. snprintf(buffer, sizeof(buffer), "%s%s", OSP_SIP_HEADER, result.token);
  1355. pbx_builtin_setvar_helper(chan, "_SIPADDHEADER", buffer);
  1356. ast_debug(1, "OSPLookup: SIPADDHEADER size '%zd'\n", strlen(buffer));
  1357. }
  1358. } else if (!strcasecmp(result.tech, OSP_TECH_IAX)) {
  1359. snprintf(buffer, sizeof(buffer), "%s/%s/%s", result.tech, result.dest, result.called);
  1360. pbx_builtin_setvar_helper(chan, "OSPDIALSTR", buffer);
  1361. }
  1362. if ((cres = ast_autoservice_stop(chan)) < 0) {
  1363. return -1;
  1364. }
  1365. if(res <= 0) {
  1366. res = -1;
  1367. } else {
  1368. res = 0;
  1369. }
  1370. return res;
  1371. }
  1372. /*!
  1373. * \brief OSP Application OSPNext
  1374. * \param chan Channel
  1375. * \param data Parameter
  1376. * \return 0 Success, -1 Failed
  1377. */
  1378. static int ospnext_exec(
  1379. struct ast_channel* chan,
  1380. void* data)
  1381. {
  1382. int res;
  1383. const char* provider = OSP_DEF_PROVIDER;
  1384. int cause = 0;
  1385. struct varshead* headp;
  1386. struct ast_var_t* current;
  1387. struct osp_result result;
  1388. char buffer[OSP_TOKSTR_SIZE];
  1389. unsigned int callidtypes = OSP_CALLID_UNDEFINED;
  1390. const char* status;
  1391. char* tmp;
  1392. AST_DECLARE_APP_ARGS(args,
  1393. AST_APP_ARG(cause);
  1394. AST_APP_ARG(provider);
  1395. AST_APP_ARG(options);
  1396. );
  1397. if (ast_strlen_zero(data)) {
  1398. ast_log(LOG_WARNING, "OSPNext: Arg required, OSPNext(cause[|provider[|options]])\n");
  1399. return -1;
  1400. }
  1401. if (!(tmp = ast_strdupa(data))) {
  1402. ast_log(LOG_ERROR, "Out of memory\n");
  1403. return -1;
  1404. }
  1405. AST_STANDARD_APP_ARGS(args, tmp);
  1406. if (!ast_strlen_zero(args.cause) && sscanf(args.cause, "%30d", &cause) != 1) {
  1407. cause = 0;
  1408. }
  1409. ast_debug(1, "OSPNext: cause '%d'\n", cause);
  1410. if (!ast_strlen_zero(args.provider)) {
  1411. provider = args.provider;
  1412. }
  1413. ast_debug(1, "OSPlookup: provider '%s'\n", provider);
  1414. result.inhandle = OSP_INVALID_HANDLE;
  1415. result.outhandle = OSP_INVALID_HANDLE;
  1416. result.intimelimit = OSP_DEF_TIMELIMIT;
  1417. result.numresults = 0;
  1418. headp = &chan->varshead;
  1419. AST_LIST_TRAVERSE(headp, current, entries) {
  1420. if (!strcasecmp(ast_var_name(current), "OSPINHANDLE")) {
  1421. if (sscanf(ast_var_value(current), "%30d", &result.inhandle) != 1) {
  1422. result.inhandle = OSP_INVALID_HANDLE;
  1423. }
  1424. } else if (!strcasecmp(ast_var_name(current), "OSPOUTHANDLE")) {
  1425. if (sscanf(ast_var_value(current), "%30d", &result.outhandle) != 1) {
  1426. result.outhandle = OSP_INVALID_HANDLE;
  1427. }
  1428. } else if (!strcasecmp(ast_var_name(current), "OSPINTIMELIMIT")) {
  1429. if (sscanf(ast_var_value(current), "%30d", &result.intimelimit) != 1) {
  1430. result.intimelimit = OSP_DEF_TIMELIMIT;
  1431. }
  1432. } else if (!strcasecmp(ast_var_name(current), "OSPOUTCALLIDTYPES")) {
  1433. if (sscanf(ast_var_value(current), "%30d", &callidtypes) != 1) {
  1434. callidtypes = OSP_CALLID_UNDEFINED;
  1435. }
  1436. } else if (!strcasecmp(ast_var_name(current), "OSPRESULTS")) {
  1437. if (sscanf(ast_var_value(current), "%30d", &result.numresults) != 1) {
  1438. result.numresults = 0;
  1439. }
  1440. }
  1441. }
  1442. ast_debug(1, "OSPNext: OSPINHANDLE '%d'\n", result.inhandle);
  1443. ast_debug(1, "OSPNext: OSPOUTHANDLE '%d'\n", result.outhandle);
  1444. ast_debug(1, "OSPNext: OSPINTIMELIMIT '%d'\n", result.intimelimit);
  1445. ast_debug(1, "OSPNext: OSPOUTCALLIDTYPES '%d'\n", callidtypes);
  1446. ast_debug(1, "OSPNext: OSPRESULTS '%d'\n", result.numresults);
  1447. if ((res = osp_next(provider, cause, &result)) > 0) {
  1448. status = AST_OSP_SUCCESS;
  1449. } else {
  1450. result.tech[0] = '\0';
  1451. result.dest[0] = '\0';
  1452. result.called[0] = '\0';
  1453. result.calling[0] = '\0';
  1454. result.token[0] = '\0';
  1455. result.networkid[0] = '\0';
  1456. result.numresults = 0;
  1457. result.outtimelimit = OSP_DEF_TIMELIMIT;
  1458. result.outcallid.buf[0] = '\0';
  1459. result.outcallid.len = 0;
  1460. if (!res) {
  1461. status = AST_OSP_FAILED;
  1462. } else {
  1463. status = AST_OSP_ERROR;
  1464. }
  1465. }
  1466. pbx_builtin_setvar_helper(chan, "OSPTECH", result.tech);
  1467. ast_debug(1, "OSPNext: OSPTECH '%s'\n", result.tech);
  1468. pbx_builtin_setvar_helper(chan, "OSPDEST", result.dest);
  1469. ast_debug(1, "OSPNext: OSPDEST '%s'\n", result.dest);
  1470. pbx_builtin_setvar_helper(chan, "OSPCALLED", result.called);
  1471. ast_debug(1, "OSPNext: OSPCALLED'%s'\n", result.called);
  1472. pbx_builtin_setvar_helper(chan, "OSPCALLING", result.calling);
  1473. ast_debug(1, "OSPNext: OSPCALLING '%s'\n", result.calling);
  1474. pbx_builtin_setvar_helper(chan, "OSPOUTTOKEN", result.token);
  1475. ast_debug(1, "OSPNext: OSPOUTTOKEN size '%zd'\n", strlen(result.token));
  1476. snprintf(buffer, sizeof(buffer), "%d", result.numresults);
  1477. pbx_builtin_setvar_helper(chan, "OSPRESULTS", buffer);
  1478. ast_debug(1, "OSPNext: OSPRESULTS '%s'\n", buffer);
  1479. snprintf(buffer, sizeof(buffer), "%d", result.outtimelimit);
  1480. pbx_builtin_setvar_helper(chan, "OSPOUTTIMELIMIT", buffer);
  1481. ast_debug(1, "OSPNext: OSPOUTTIMELIMIT '%s'\n", buffer);
  1482. pbx_builtin_setvar_helper(chan, "OSPNEXTSTATUS", status);
  1483. ast_debug(1, "OSPNext: %s\n", status);
  1484. if (!strcasecmp(result.tech, OSP_TECH_H323)) {
  1485. if ((callidtypes & OSP_CALLID_H323) && (result.outcallid.len != 0)) {
  1486. osp_uuid2str(result.outcallid.buf, buffer, sizeof(buffer));
  1487. } else {
  1488. buffer[0] = '\0';
  1489. }
  1490. pbx_builtin_setvar_helper(chan, "OSPOUTCALLID", buffer);
  1491. snprintf(buffer, sizeof(buffer), "%s/%s@%s", result.tech, result.called, result.dest);
  1492. pbx_builtin_setvar_helper(chan, "OSPDIALSTR", buffer);
  1493. } else if (!strcasecmp(result.tech, OSP_TECH_SIP)) {
  1494. snprintf(buffer, sizeof(buffer), "%s/%s@%s", result.tech, result.called, result.dest);
  1495. pbx_builtin_setvar_helper(chan, "OSPDIALSTR", buffer);
  1496. if (!ast_strlen_zero(result.token)) {
  1497. snprintf(buffer, sizeof(buffer), "%s%s", OSP_SIP_HEADER, result.token);
  1498. pbx_builtin_setvar_helper(chan, "_SIPADDHEADER", buffer);
  1499. ast_debug(1, "OSPLookup: SIPADDHEADER size '%zd'\n", strlen(buffer));
  1500. }
  1501. } else if (!strcasecmp(result.tech, OSP_TECH_IAX)) {
  1502. snprintf(buffer, sizeof(buffer), "%s/%s/%s", result.tech, result.dest, result.called);
  1503. pbx_builtin_setvar_helper(chan, "OSPDIALSTR", buffer);
  1504. }
  1505. if(res <= 0) {
  1506. res = -1;
  1507. } else {
  1508. res = 0;
  1509. }
  1510. return res;
  1511. }
  1512. /*!
  1513. * \brief OSP Application OSPFinish
  1514. * \param chan Channel
  1515. * \param data Parameter
  1516. * \return 0 Success, -1 Failed
  1517. */
  1518. static int ospfinished_exec(
  1519. struct ast_channel* chan,
  1520. void* data)
  1521. {
  1522. int res = 1;
  1523. int cause = 0;
  1524. struct varshead* headp;
  1525. struct ast_var_t* current;
  1526. int inhandle = OSP_INVALID_HANDLE;
  1527. int outhandle = OSP_INVALID_HANDLE;
  1528. int recorded = 0;
  1529. time_t start, connect, end;
  1530. unsigned int release;
  1531. char buffer[OSP_INTSTR_SIZE];
  1532. const char* status;
  1533. char* tmp;
  1534. AST_DECLARE_APP_ARGS(args,
  1535. AST_APP_ARG(cause);
  1536. AST_APP_ARG(options);
  1537. );
  1538. if (!(tmp = ast_strdupa(data))) {
  1539. ast_log(LOG_ERROR, "Out of memory\n");
  1540. return -1;
  1541. }
  1542. AST_STANDARD_APP_ARGS(args, tmp);
  1543. headp = &chan->varshead;
  1544. AST_LIST_TRAVERSE(headp, current, entries) {
  1545. if (!strcasecmp(ast_var_name(current), "OSPINHANDLE")) {
  1546. if (sscanf(ast_var_value(current), "%30d", &inhandle) != 1) {
  1547. inhandle = OSP_INVALID_HANDLE;
  1548. }
  1549. } else if (!strcasecmp(ast_var_name(current), "OSPOUTHANDLE")) {
  1550. if (sscanf(ast_var_value(current), "%30d", &outhandle) != 1) {
  1551. outhandle = OSP_INVALID_HANDLE;
  1552. }
  1553. } else if (!recorded &&
  1554. (!strcasecmp(ast_var_name(current), "OSPAUTHSTATUS") ||
  1555. !strcasecmp(ast_var_name(current), "OSPLOOKUPSTATUS") ||
  1556. !strcasecmp(ast_var_name(current), "OSPNEXTSTATUS")))
  1557. {
  1558. if (strcasecmp(ast_var_value(current), AST_OSP_SUCCESS)) {
  1559. recorded = 1;
  1560. }
  1561. }
  1562. }
  1563. ast_debug(1, "OSPFinish: OSPINHANDLE '%d'\n", inhandle);
  1564. ast_debug(1, "OSPFinish: OSPOUTHANDLE '%d'\n", outhandle);
  1565. ast_debug(1, "OSPFinish: recorded '%d'\n", recorded);
  1566. if (!ast_strlen_zero(args.cause) && sscanf(args.cause, "%30d", &cause) != 1) {
  1567. cause = 0;
  1568. }
  1569. ast_debug(1, "OSPFinish: cause '%d'\n", cause);
  1570. if (chan->cdr) {
  1571. start = chan->cdr->start.tv_sec;
  1572. connect = chan->cdr->answer.tv_sec;
  1573. if (connect) {
  1574. end = time(NULL);
  1575. } else {
  1576. end = connect;
  1577. }
  1578. } else {
  1579. start = 0;
  1580. connect = 0;
  1581. end = 0;
  1582. }
  1583. ast_debug(1, "OSPFinish: start '%ld'\n", start);
  1584. ast_debug(1, "OSPFinish: connect '%ld'\n", connect);
  1585. ast_debug(1, "OSPFinish: end '%ld'\n", end);
  1586. release = ast_check_hangup(chan) ? 0 : 1;
  1587. if (osp_finish(outhandle, recorded, cause, start, connect, end, release) <= 0) {
  1588. ast_debug(1, "OSPFinish: Unable to report usage for outbound call\n");
  1589. }
  1590. switch (cause) {
  1591. case AST_CAUSE_NORMAL_CLEARING:
  1592. break;
  1593. default:
  1594. cause = AST_CAUSE_NO_ROUTE_DESTINATION;
  1595. break;
  1596. }
  1597. if (osp_finish(inhandle, recorded, cause, start, connect, end, release) <= 0) {
  1598. ast_debug(1, "OSPFinish: Unable to report usage for inbound call\n");
  1599. }
  1600. snprintf(buffer, sizeof(buffer), "%d", OSP_INVALID_HANDLE);
  1601. pbx_builtin_setvar_helper(chan, "OSPOUTHANDLE", buffer);
  1602. pbx_builtin_setvar_helper(chan, "OSPINHANDLE", buffer);
  1603. if (res > 0) {
  1604. status = AST_OSP_SUCCESS;
  1605. } else if (!res) {
  1606. status = AST_OSP_FAILED;
  1607. } else {
  1608. status = AST_OSP_ERROR;
  1609. }
  1610. pbx_builtin_setvar_helper(chan, "OSPFINISHSTATUS", status);
  1611. if(!res) {
  1612. res = -1;
  1613. } else {
  1614. res = 0;
  1615. }
  1616. return res;
  1617. }
  1618. /* OSP Module APIs */
  1619. static int osp_unload(void);
  1620. static int osp_load(int reload)
  1621. {
  1622. const char* t;
  1623. unsigned int v;
  1624. struct ast_config* cfg;
  1625. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  1626. int error = OSPC_ERR_NO_ERROR;
  1627. if ((cfg = ast_config_load(OSP_CONFIG_FILE, config_flags)) == CONFIG_STATUS_FILEUNCHANGED)
  1628. return 0;
  1629. if (cfg) {
  1630. if (reload)
  1631. osp_unload();
  1632. t = ast_variable_retrieve(cfg, OSP_GENERAL_CAT, "accelerate");
  1633. if (t && ast_true(t)) {
  1634. if ((error = OSPPInit(1)) != OSPC_ERR_NO_ERROR) {
  1635. ast_log(LOG_WARNING, "OSP: Unable to enable hardware accelleration\n");
  1636. OSPPInit(0);
  1637. } else {
  1638. osp_hardware = 1;
  1639. }
  1640. } else {
  1641. OSPPInit(0);
  1642. }
  1643. ast_debug(1, "OSP: osp_hardware '%d'\n", osp_hardware);
  1644. t = ast_variable_retrieve(cfg, OSP_GENERAL_CAT, "tokenformat");
  1645. if (t) {
  1646. if ((sscanf(t, "%30d", &v) == 1) &&
  1647. ((v == TOKEN_ALGO_SIGNED) || (v == TOKEN_ALGO_UNSIGNED) || (v == TOKEN_ALGO_BOTH)))
  1648. {
  1649. osp_tokenformat = v;
  1650. } else {
  1651. ast_log(LOG_WARNING, "tokenformat should be an integer from %d, %d or %d, not '%s'\n",
  1652. TOKEN_ALGO_SIGNED, TOKEN_ALGO_UNSIGNED, TOKEN_ALGO_BOTH, t);
  1653. }
  1654. }
  1655. ast_debug(1, "OSP: osp_tokenformat '%d'\n", osp_tokenformat);
  1656. t = ast_category_browse(cfg, NULL);
  1657. while(t) {
  1658. if (strcasecmp(t, OSP_GENERAL_CAT)) {
  1659. osp_create_provider(cfg, t);
  1660. }
  1661. t = ast_category_browse(cfg, t);
  1662. }
  1663. osp_initialized = 1;
  1664. ast_config_destroy(cfg);
  1665. } else {
  1666. ast_log(LOG_WARNING, "OSP: Unable to find configuration. OSP support disabled\n");
  1667. return 0;
  1668. }
  1669. ast_debug(1, "OSP: osp_initialized '%d'\n", osp_initialized);
  1670. return 1;
  1671. }
  1672. static int osp_unload(void)
  1673. {
  1674. struct osp_provider* p;
  1675. struct osp_provider* next;
  1676. if (osp_initialized) {
  1677. ast_mutex_lock(&osplock);
  1678. p = ospproviders;
  1679. while(p) {
  1680. next = p->next;
  1681. OSPPProviderDelete(p->handle, 0);
  1682. ast_free(p);
  1683. p = next;
  1684. }
  1685. ospproviders = NULL;
  1686. ast_mutex_unlock(&osplock);
  1687. OSPPCleanup();
  1688. osp_tokenformat = TOKEN_ALGO_SIGNED;
  1689. osp_hardware = 0;
  1690. osp_initialized = 0;
  1691. }
  1692. return 0;
  1693. }
  1694. static char *handle_cli_osp_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1695. {
  1696. int i;
  1697. int found = 0;
  1698. struct osp_provider* p;
  1699. const char* provider = NULL;
  1700. const char* tokenalgo;
  1701. switch (cmd) {
  1702. case CLI_INIT:
  1703. e->command = "osp show";
  1704. e->usage =
  1705. "Usage: osp show\n"
  1706. " Displays information on Open Settlement Protocol support\n";
  1707. return NULL;
  1708. case CLI_GENERATE:
  1709. return NULL;
  1710. }
  1711. if ((a->argc < 2) || (a->argc > 3))
  1712. return CLI_SHOWUSAGE;
  1713. if (a->argc > 2)
  1714. provider = a->argv[2];
  1715. if (!provider) {
  1716. switch (osp_tokenformat) {
  1717. case TOKEN_ALGO_BOTH:
  1718. tokenalgo = "Both";
  1719. break;
  1720. case TOKEN_ALGO_UNSIGNED:
  1721. tokenalgo = "Unsigned";
  1722. break;
  1723. case TOKEN_ALGO_SIGNED:
  1724. default:
  1725. tokenalgo = "Signed";
  1726. break;
  1727. }
  1728. ast_cli(a->fd, "OSP: %s %s %s\n",
  1729. osp_initialized ? "Initialized" : "Uninitialized", osp_hardware ? "Accelerated" : "Normal", tokenalgo);
  1730. }
  1731. ast_mutex_lock(&osplock);
  1732. p = ospproviders;
  1733. while(p) {
  1734. if (!provider || !strcasecmp(p->name, provider)) {
  1735. if (found) {
  1736. ast_cli(a->fd, "\n");
  1737. }
  1738. ast_cli(a->fd, " == OSP Provider '%s' == \n", p->name);
  1739. ast_cli(a->fd, "Local Private Key: %s\n", p->privatekey);
  1740. ast_cli(a->fd, "Local Certificate: %s\n", p->localcert);
  1741. for (i = 0; i < p->cacount; i++) {
  1742. ast_cli(a->fd, "CA Certificate %d: %s\n", i + 1, p->cacerts[i]);
  1743. }
  1744. for (i = 0; i < p->spcount; i++) {
  1745. ast_cli(a->fd, "Service Point %d: %s\n", i + 1, p->srvpoints[i]);
  1746. }
  1747. ast_cli(a->fd, "Max Connections: %d\n", p->maxconnections);
  1748. ast_cli(a->fd, "Retry Delay: %d seconds\n", p->retrydelay);
  1749. ast_cli(a->fd, "Retry Limit: %d\n", p->retrylimit);
  1750. ast_cli(a->fd, "Timeout: %d milliseconds\n", p->timeout);
  1751. ast_cli(a->fd, "Source: %s\n", strlen(p->source) ? p->source : "<unspecified>");
  1752. ast_cli(a->fd, "Auth Policy %d\n", p->authpolicy);
  1753. ast_cli(a->fd, "Default protocol %s\n", p->defaultprotocol);
  1754. ast_cli(a->fd, "OSP Handle: %d\n", p->handle);
  1755. found++;
  1756. }
  1757. p = p->next;
  1758. }
  1759. ast_mutex_unlock(&osplock);
  1760. if (!found) {
  1761. if (provider) {
  1762. ast_cli(a->fd, "Unable to find OSP provider '%s'\n", provider);
  1763. } else {
  1764. ast_cli(a->fd, "No OSP providers configured\n");
  1765. }
  1766. }
  1767. return CLI_SUCCESS;
  1768. }
  1769. static const char* app1= "OSPAuth";
  1770. static const char* synopsis1 = "OSP authentication";
  1771. static const char* descrip1 =
  1772. " OSPAuth([provider[,options]]): Authenticate a SIP INVITE by OSP and sets\n"
  1773. "the variables:\n"
  1774. " ${OSPINHANDLE}: The inbound call transaction handle\n"
  1775. " ${OSPINTIMELIMIT}: The inbound call duration limit in seconds\n"
  1776. "\n"
  1777. "This application sets the following channel variable upon completion:\n"
  1778. " OSPAUTHSTATUS The status of the OSP Auth attempt as a text string, one of\n"
  1779. " SUCCESS | FAILED | ERROR\n";
  1780. static const char* app2= "OSPLookup";
  1781. static const char* synopsis2 = "Lookup destination by OSP";
  1782. static const char* descrip2 =
  1783. " OSPLookup(exten[,provider[,options]]): Looks up an extension via OSP and sets\n"
  1784. "the variables, where 'n' is the number of the result beginning with 1:\n"
  1785. " ${OSPOUTHANDLE}: The OSP Handle for anything remaining\n"
  1786. " ${OSPTECH}: The technology to use for the call\n"
  1787. " ${OSPDEST}: The destination to use for the call\n"
  1788. " ${OSPCALLED}: The called number to use for the call\n"
  1789. " ${OSPCALLING}: The calling number to use for the call\n"
  1790. " ${OSPDIALSTR}: The dial command string\n"
  1791. " ${OSPOUTTOKEN}: The actual OSP token as a string\n"
  1792. " ${OSPOUTTIMELIMIT}: The outbound call duration limit in seconds\n"
  1793. " ${OSPOUTCALLIDTYPES}: The outbound call id types\n"
  1794. " ${OSPOUTCALLID}: The outbound call id\n"
  1795. " ${OSPRESULTS}: The number of OSP results total remaining\n"
  1796. "\n"
  1797. "The option string may contain the following character:\n"
  1798. " 'h' -- generate H323 call id for the outbound call\n"
  1799. " 's' -- generate SIP call id for the outbound call. Have not been implemented\n"
  1800. " 'i' -- generate IAX call id for the outbound call. Have not been implemented\n"
  1801. "This application sets the following channel variable upon completion:\n"
  1802. " OSPLOOKUPSTATUS The status of the OSP Lookup attempt as a text string, one of\n"
  1803. " SUCCESS | FAILED | ERROR\n";
  1804. static const char* app3 = "OSPNext";
  1805. static const char* synopsis3 = "Lookup next destination by OSP";
  1806. static const char* descrip3 =
  1807. " OSPNext(cause[,provider[,options]]): Looks up the next OSP Destination for ${OSPOUTHANDLE}\n"
  1808. "See OSPLookup for more information\n"
  1809. "\n"
  1810. "This application sets the following channel variable upon completion:\n"
  1811. " OSPNEXTSTATUS The status of the OSP Next attempt as a text string, one of\n"
  1812. " SUCCESS | FAILED | ERROR\n";
  1813. static const char* app4 = "OSPFinish";
  1814. static const char* synopsis4 = "Record OSP entry";
  1815. static const char* descrip4 =
  1816. " OSPFinish([status[,options]]): Records call state for ${OSPINHANDLE}, according to\n"
  1817. "status, which should be one of BUSY, CONGESTION, ANSWER, NOANSWER, or CHANUNAVAIL\n"
  1818. "or coincidentally, just what the Dial application stores in its ${DIALSTATUS}.\n"
  1819. "\n"
  1820. "This application sets the following channel variable upon completion:\n"
  1821. " OSPFINISHSTATUS The status of the OSP Finish attempt as a text string, one of\n"
  1822. " SUCCESS | FAILED | ERROR \n";
  1823. static struct ast_cli_entry cli_osp[] = {
  1824. AST_CLI_DEFINE(handle_cli_osp_show, "Displays OSF information")
  1825. };
  1826. static int load_module(void)
  1827. {
  1828. int res;
  1829. if (!osp_load(0))
  1830. return AST_MODULE_LOAD_DECLINE;
  1831. ast_cli_register_multiple(cli_osp, sizeof(cli_osp) / sizeof(struct ast_cli_entry));
  1832. res = ast_register_application(app1, ospauth_exec, synopsis1, descrip1);
  1833. res |= ast_register_application(app2, osplookup_exec, synopsis2, descrip2);
  1834. res |= ast_register_application(app3, ospnext_exec, synopsis3, descrip3);
  1835. res |= ast_register_application(app4, ospfinished_exec, synopsis4, descrip4);
  1836. return res;
  1837. }
  1838. static int unload_module(void)
  1839. {
  1840. int res;
  1841. res = ast_unregister_application(app4);
  1842. res |= ast_unregister_application(app3);
  1843. res |= ast_unregister_application(app2);
  1844. res |= ast_unregister_application(app1);
  1845. ast_cli_unregister_multiple(cli_osp, sizeof(cli_osp) / sizeof(struct ast_cli_entry));
  1846. osp_unload();
  1847. return res;
  1848. }
  1849. static int reload(void)
  1850. {
  1851. osp_load(1);
  1852. return 0;
  1853. }
  1854. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Open Settlement Protocol Applications",
  1855. .load = load_module,
  1856. .unload = unload_module,
  1857. .reload = reload,
  1858. );