res_phoneprov.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2008, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. * Matthew Brooks <mbrooks@digium.com>
  8. * Terry Wilson <twilson@digium.com>
  9. *
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. * This program is free software, distributed under the terms of
  17. * the GNU General Public License Version 2. See the LICENSE file
  18. * at the top of the source tree.
  19. */
  20. /*! \file
  21. *
  22. * \brief Phone provisioning application for the asterisk internal http server
  23. *
  24. * \author Matthew Brooks <mbrooks@digium.com>
  25. * \author Terry Wilson <twilson@digium.com>
  26. */
  27. /*** MODULEINFO
  28. <support_level>extended</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. #include <sys/ioctl.h>
  32. #include <sys/socket.h>
  33. #include <net/if.h>
  34. #ifdef SOLARIS
  35. #include <sys/sockio.h>
  36. #endif
  37. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  38. #include "asterisk/channel.h"
  39. #include "asterisk/file.h"
  40. #include "asterisk/paths.h"
  41. #include "asterisk/pbx.h"
  42. #include "asterisk/cli.h"
  43. #include "asterisk/module.h"
  44. #include "asterisk/http.h"
  45. #include "asterisk/utils.h"
  46. #include "asterisk/app.h"
  47. #include "asterisk/strings.h"
  48. #include "asterisk/stringfields.h"
  49. #include "asterisk/options.h"
  50. #include "asterisk/config.h"
  51. #include "asterisk/acl.h"
  52. #include "asterisk/astobj2.h"
  53. #include "asterisk/ast_version.h"
  54. #ifdef LOW_MEMORY
  55. #define MAX_PROFILE_BUCKETS 1
  56. #define MAX_ROUTE_BUCKETS 1
  57. #define MAX_USER_BUCKETS 1
  58. #else
  59. #define MAX_PROFILE_BUCKETS 17
  60. #define MAX_ROUTE_BUCKETS 563
  61. #define MAX_USER_BUCKETS 563
  62. #endif /* LOW_MEMORY */
  63. #define VAR_BUF_SIZE 4096
  64. /*** DOCUMENTATION
  65. <function name="PP_EACH_EXTENSION" language="en_US">
  66. <synopsis>
  67. Execute specified template for each extension.
  68. </synopsis>
  69. <syntax>
  70. <parameter name="mac" required="true" />
  71. <parameter name="template" required="true" />
  72. </syntax>
  73. <description>
  74. <para>Output the specified template for each extension associated with the specified MAC address.</para>
  75. </description>
  76. </function>
  77. <function name="PP_EACH_USER" language="en_US">
  78. <synopsis>
  79. Generate a string for each phoneprov user.
  80. </synopsis>
  81. <syntax>
  82. <parameter name="string" required="true" />
  83. <parameter name="exclude_mac" required="true" />
  84. </syntax>
  85. <description>
  86. <para>Pass in a string, with phoneprov variables you want substituted in the format of
  87. %{VARNAME}, and you will get the string rendered for each user in phoneprov
  88. excluding ones with MAC address <replaceable>exclude_mac</replaceable>. Probably not
  89. useful outside of res_phoneprov.</para>
  90. <para>Example: ${PP_EACH_USER(&lt;item&gt;&lt;fn&gt;%{DISPLAY_NAME}&lt;/fn&gt;&lt;/item&gt;|${MAC})</para>
  91. </description>
  92. </function>
  93. ***/
  94. /*! \brief for use in lookup_iface */
  95. static struct in_addr __ourip = { .s_addr = 0x00000000, };
  96. /* \note This enum and the pp_variable_list must be in the same order or
  97. * bad things happen! */
  98. enum pp_variables {
  99. PP_MACADDRESS,
  100. PP_USERNAME,
  101. PP_FULLNAME,
  102. PP_SECRET,
  103. PP_LABEL,
  104. PP_CALLERID,
  105. PP_TIMEZONE,
  106. PP_LINENUMBER,
  107. PP_LINEKEYS,
  108. PP_VAR_LIST_LENGTH, /* This entry must always be the last in the list */
  109. };
  110. /*! \brief Lookup table to translate between users.conf property names and
  111. * variables for use in phoneprov templates */
  112. static const struct pp_variable_lookup {
  113. enum pp_variables id;
  114. const char * const user_var;
  115. const char * const template_var;
  116. } pp_variable_list[] = {
  117. { PP_MACADDRESS, "macaddress", "MAC" },
  118. { PP_USERNAME, "username", "USERNAME" },
  119. { PP_FULLNAME, "fullname", "DISPLAY_NAME" },
  120. { PP_SECRET, "secret", "SECRET" },
  121. { PP_LABEL, "label", "LABEL" },
  122. { PP_CALLERID, "cid_number", "CALLERID" },
  123. { PP_TIMEZONE, "timezone", "TIMEZONE" },
  124. { PP_LINENUMBER, "linenumber", "LINE" },
  125. { PP_LINEKEYS, "linekeys", "LINEKEYS" },
  126. };
  127. /*! \brief structure to hold file data */
  128. struct phoneprov_file {
  129. AST_DECLARE_STRING_FIELDS(
  130. AST_STRING_FIELD(format); /*!< After variable substitution, becomes route->uri */
  131. AST_STRING_FIELD(template); /*!< Template/physical file location */
  132. AST_STRING_FIELD(mime_type);/*!< Mime-type of the file */
  133. );
  134. AST_LIST_ENTRY(phoneprov_file) entry;
  135. };
  136. /*! \brief structure to hold phone profiles read from phoneprov.conf */
  137. struct phone_profile {
  138. AST_DECLARE_STRING_FIELDS(
  139. AST_STRING_FIELD(name); /*!< Name of phone profile */
  140. AST_STRING_FIELD(default_mime_type); /*!< Default mime type if it isn't provided */
  141. AST_STRING_FIELD(staticdir); /*!< Subdirectory that static files are stored in */
  142. );
  143. struct varshead *headp; /*!< List of variables set with 'setvar' in phoneprov.conf */
  144. AST_LIST_HEAD_NOLOCK(, phoneprov_file) static_files; /*!< List of static files */
  145. AST_LIST_HEAD_NOLOCK(, phoneprov_file) dynamic_files; /*!< List of dynamic files */
  146. };
  147. struct extension {
  148. AST_DECLARE_STRING_FIELDS(
  149. AST_STRING_FIELD(name);
  150. );
  151. int index;
  152. struct varshead *headp; /*!< List of variables to substitute into templates */
  153. AST_LIST_ENTRY(extension) entry;
  154. };
  155. /*! \brief structure to hold users read from users.conf */
  156. struct user {
  157. AST_DECLARE_STRING_FIELDS(
  158. AST_STRING_FIELD(macaddress); /*!< Mac address of user's phone */
  159. );
  160. struct phone_profile *profile; /*!< Profile the phone belongs to */
  161. AST_LIST_HEAD_NOLOCK(, extension) extensions;
  162. };
  163. /*! \brief structure to hold http routes (valid URIs, and the files they link to) */
  164. struct http_route {
  165. AST_DECLARE_STRING_FIELDS(
  166. AST_STRING_FIELD(uri); /*!< The URI requested */
  167. );
  168. struct phoneprov_file *file; /*!< The file that links to the URI */
  169. struct user *user; /*!< The user that has variables to substitute into the file
  170. * NULL in the case of a static route */
  171. };
  172. static struct ao2_container *profiles;
  173. static struct ao2_container *http_routes;
  174. static struct ao2_container *users;
  175. static char global_server[80] = ""; /*!< Server to substitute into templates */
  176. static char global_serverport[6] = ""; /*!< Server port to substitute into templates */
  177. static char global_default_profile[80] = ""; /*!< Default profile to use if one isn't specified */
  178. /*! \brief List of global variables currently available: VOICEMAIL_EXTEN, EXTENSION_LENGTH */
  179. static struct varshead global_variables;
  180. static ast_mutex_t globals_lock;
  181. /* iface is the interface (e.g. eth0); address is the return value */
  182. static int lookup_iface(const char *iface, struct in_addr *address)
  183. {
  184. int mysock, res = 0;
  185. struct ifreq ifr;
  186. struct sockaddr_in *sin;
  187. memset(&ifr, 0, sizeof(ifr));
  188. ast_copy_string(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
  189. mysock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
  190. if (mysock < 0) {
  191. ast_log(LOG_ERROR, "Failed to create socket: %s\n", strerror(errno));
  192. return -1;
  193. }
  194. res = ioctl(mysock, SIOCGIFADDR, &ifr);
  195. close(mysock);
  196. if (res < 0) {
  197. ast_log(LOG_WARNING, "Unable to get IP of %s: %s\n", iface, strerror(errno));
  198. memcpy(address, &__ourip, sizeof(__ourip));
  199. return -1;
  200. } else {
  201. sin = (struct sockaddr_in *)&ifr.ifr_addr;
  202. memcpy(address, &sin->sin_addr, sizeof(*address));
  203. return 0;
  204. }
  205. }
  206. static struct phone_profile *unref_profile(struct phone_profile *prof)
  207. {
  208. ao2_ref(prof, -1);
  209. return NULL;
  210. }
  211. /*! \brief Return a phone profile looked up by name */
  212. static struct phone_profile *find_profile(const char *name)
  213. {
  214. struct phone_profile tmp = {
  215. .name = name,
  216. };
  217. return ao2_find(profiles, &tmp, OBJ_POINTER);
  218. }
  219. static int profile_hash_fn(const void *obj, const int flags)
  220. {
  221. const struct phone_profile *profile = obj;
  222. return ast_str_case_hash(profile->name);
  223. }
  224. static int profile_cmp_fn(void *obj, void *arg, int flags)
  225. {
  226. const struct phone_profile *profile1 = obj, *profile2 = arg;
  227. return !strcasecmp(profile1->name, profile2->name) ? CMP_MATCH | CMP_STOP : 0;
  228. }
  229. static void delete_file(struct phoneprov_file *file)
  230. {
  231. ast_string_field_free_memory(file);
  232. free(file);
  233. }
  234. static void profile_destructor(void *obj)
  235. {
  236. struct phone_profile *profile = obj;
  237. struct phoneprov_file *file;
  238. struct ast_var_t *var;
  239. while ((file = AST_LIST_REMOVE_HEAD(&profile->static_files, entry)))
  240. delete_file(file);
  241. while ((file = AST_LIST_REMOVE_HEAD(&profile->dynamic_files, entry)))
  242. delete_file(file);
  243. while ((var = AST_LIST_REMOVE_HEAD(profile->headp, entries)))
  244. ast_var_delete(var);
  245. ast_free(profile->headp);
  246. ast_string_field_free_memory(profile);
  247. }
  248. static struct http_route *unref_route(struct http_route *route)
  249. {
  250. ao2_ref(route, -1);
  251. return NULL;
  252. }
  253. static int routes_hash_fn(const void *obj, const int flags)
  254. {
  255. const struct http_route *route = obj;
  256. return ast_str_case_hash(route->uri);
  257. }
  258. static int routes_cmp_fn(void *obj, void *arg, int flags)
  259. {
  260. const struct http_route *route1 = obj, *route2 = arg;
  261. return !strcasecmp(route1->uri, route2->uri) ? CMP_MATCH | CMP_STOP : 0;
  262. }
  263. static void route_destructor(void *obj)
  264. {
  265. struct http_route *route = obj;
  266. ast_string_field_free_memory(route);
  267. }
  268. /*! \brief Read a TEXT file into a string and return the length */
  269. static int load_file(const char *filename, char **ret)
  270. {
  271. int len = 0;
  272. FILE *f;
  273. if (!(f = fopen(filename, "r"))) {
  274. *ret = NULL;
  275. return -1;
  276. }
  277. fseek(f, 0, SEEK_END);
  278. len = ftell(f);
  279. fseek(f, 0, SEEK_SET);
  280. if (!(*ret = ast_malloc(len + 1)))
  281. return -2;
  282. if (len != fread(*ret, sizeof(char), len, f)) {
  283. free(*ret);
  284. *ret = NULL;
  285. return -3;
  286. }
  287. fclose(f);
  288. (*ret)[len] = '\0';
  289. return len;
  290. }
  291. /*! \brief Set all timezone-related variables based on a zone (i.e. America/New_York)
  292. \param headp pointer to list of user variables
  293. \param zone A time zone. NULL sets variables based on timezone of the machine
  294. */
  295. static void set_timezone_variables(struct varshead *headp, const char *zone)
  296. {
  297. time_t utc_time;
  298. int dstenable;
  299. time_t dststart;
  300. time_t dstend;
  301. struct ast_tm tm_info;
  302. int tzoffset;
  303. char buffer[21];
  304. struct ast_var_t *var;
  305. struct timeval when;
  306. time(&utc_time);
  307. ast_get_dst_info(&utc_time, &dstenable, &dststart, &dstend, &tzoffset, zone);
  308. snprintf(buffer, sizeof(buffer), "%d", tzoffset);
  309. var = ast_var_assign("TZOFFSET", buffer);
  310. if (var)
  311. AST_LIST_INSERT_TAIL(headp, var, entries);
  312. if (!dstenable)
  313. return;
  314. if ((var = ast_var_assign("DST_ENABLE", "1")))
  315. AST_LIST_INSERT_TAIL(headp, var, entries);
  316. when.tv_sec = dststart;
  317. ast_localtime(&when, &tm_info, zone);
  318. snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_mon+1);
  319. if ((var = ast_var_assign("DST_START_MONTH", buffer)))
  320. AST_LIST_INSERT_TAIL(headp, var, entries);
  321. snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_mday);
  322. if ((var = ast_var_assign("DST_START_MDAY", buffer)))
  323. AST_LIST_INSERT_TAIL(headp, var, entries);
  324. snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_hour);
  325. if ((var = ast_var_assign("DST_START_HOUR", buffer)))
  326. AST_LIST_INSERT_TAIL(headp, var, entries);
  327. when.tv_sec = dstend;
  328. ast_localtime(&when, &tm_info, zone);
  329. snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_mon + 1);
  330. if ((var = ast_var_assign("DST_END_MONTH", buffer)))
  331. AST_LIST_INSERT_TAIL(headp, var, entries);
  332. snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_mday);
  333. if ((var = ast_var_assign("DST_END_MDAY", buffer)))
  334. AST_LIST_INSERT_TAIL(headp, var, entries);
  335. snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_hour);
  336. if ((var = ast_var_assign("DST_END_HOUR", buffer)))
  337. AST_LIST_INSERT_TAIL(headp, var, entries);
  338. }
  339. /*! \brief Callback that is executed everytime an http request is received by this module */
  340. static int phoneprov_callback(struct ast_tcptls_session_instance *ser, const struct ast_http_uri *urih, const char *uri, enum ast_http_method method, struct ast_variable *get_vars, struct ast_variable *headers)
  341. {
  342. struct http_route *route;
  343. struct http_route search_route = {
  344. .uri = uri,
  345. };
  346. struct ast_str *result;
  347. char path[PATH_MAX];
  348. char *file = NULL;
  349. int len;
  350. int fd;
  351. struct ast_str *http_header;
  352. if (method != AST_HTTP_GET && method != AST_HTTP_HEAD) {
  353. ast_http_error(ser, 501, "Not Implemented", "Attempt to use unimplemented / unsupported method");
  354. return -1;
  355. }
  356. if (!(route = ao2_find(http_routes, &search_route, OBJ_POINTER))) {
  357. goto out404;
  358. }
  359. snprintf(path, sizeof(path), "%s/phoneprov/%s", ast_config_AST_DATA_DIR, route->file->template);
  360. if (!route->user) { /* Static file */
  361. fd = open(path, O_RDONLY);
  362. if (fd < 0) {
  363. goto out500;
  364. }
  365. len = lseek(fd, 0, SEEK_END);
  366. lseek(fd, 0, SEEK_SET);
  367. if (len < 0) {
  368. ast_log(LOG_WARNING, "Could not load file: %s (%d)\n", path, len);
  369. close(fd);
  370. goto out500;
  371. }
  372. http_header = ast_str_create(80);
  373. ast_str_set(&http_header, 0, "Content-type: %s\r\n",
  374. route->file->mime_type);
  375. ast_http_send(ser, method, 200, NULL, http_header, NULL, fd, 0);
  376. close(fd);
  377. route = unref_route(route);
  378. return 0;
  379. } else { /* Dynamic file */
  380. struct ast_str *tmp;
  381. len = load_file(path, &file);
  382. if (len < 0) {
  383. ast_log(LOG_WARNING, "Could not load file: %s (%d)\n", path, len);
  384. if (file) {
  385. ast_free(file);
  386. }
  387. goto out500;
  388. }
  389. if (!file) {
  390. goto out500;
  391. }
  392. if (!(tmp = ast_str_create(len))) {
  393. if (file) {
  394. ast_free(file);
  395. }
  396. goto out500;
  397. }
  398. /* Unless we are overridden by serveriface or serveraddr, we set the SERVER variable to
  399. * the IP address we are listening on that the phone contacted for this config file */
  400. if (ast_strlen_zero(global_server)) {
  401. union {
  402. struct sockaddr sa;
  403. struct sockaddr_in sa_in;
  404. } name;
  405. socklen_t namelen = sizeof(name.sa);
  406. int res;
  407. if ((res = getsockname(ser->fd, &name.sa, &namelen))) {
  408. ast_log(LOG_WARNING, "Could not get server IP, breakage likely.\n");
  409. } else {
  410. struct ast_var_t *var;
  411. struct extension *exten_iter;
  412. if ((var = ast_var_assign("SERVER", ast_inet_ntoa(name.sa_in.sin_addr)))) {
  413. AST_LIST_TRAVERSE(&route->user->extensions, exten_iter, entry) {
  414. AST_LIST_INSERT_TAIL(exten_iter->headp, var, entries);
  415. }
  416. }
  417. }
  418. }
  419. ast_str_substitute_variables_varshead(&tmp, 0, AST_LIST_FIRST(&route->user->extensions)->headp, file);
  420. if (file) {
  421. ast_free(file);
  422. }
  423. http_header = ast_str_create(80);
  424. ast_str_set(&http_header, 0, "Content-type: %s\r\n",
  425. route->file->mime_type);
  426. if (!(result = ast_str_create(512))) {
  427. ast_log(LOG_ERROR, "Could not create result string!\n");
  428. if (tmp) {
  429. ast_free(tmp);
  430. }
  431. ast_free(http_header);
  432. goto out500;
  433. }
  434. ast_str_append(&result, 0, "%s", ast_str_buffer(tmp));
  435. ast_http_send(ser, method, 200, NULL, http_header, result, 0, 0);
  436. if (tmp) {
  437. ast_free(tmp);
  438. }
  439. route = unref_route(route);
  440. return 0;
  441. }
  442. out404:
  443. ast_http_error(ser, 404, "Not Found", "Nothing to see here. Move along.");
  444. return -1;
  445. out500:
  446. route = unref_route(route);
  447. ast_http_error(ser, 500, "Internal Error", "An internal error has occured.");
  448. return -1;
  449. }
  450. /*! \brief Build a route structure and add it to the list of available http routes
  451. \param pp_file File to link to the route
  452. \param user User to link to the route (NULL means static route)
  453. \param uri URI of the route
  454. */
  455. static void build_route(struct phoneprov_file *pp_file, struct user *user, char *uri)
  456. {
  457. struct http_route *route;
  458. if (!(route = ao2_alloc(sizeof(*route), route_destructor))) {
  459. return;
  460. }
  461. if (ast_string_field_init(route, 32)) {
  462. ast_log(LOG_ERROR, "Couldn't create string fields for %s\n", pp_file->format);
  463. route = unref_route(route);
  464. return;
  465. }
  466. ast_string_field_set(route, uri, S_OR(uri, pp_file->format));
  467. route->user = user;
  468. route->file = pp_file;
  469. ao2_link(http_routes, route);
  470. route = unref_route(route);
  471. }
  472. /*! \brief Build a phone profile and add it to the list of phone profiles
  473. \param name the name of the profile
  474. \param v ast_variable from parsing phoneprov.conf
  475. */
  476. static void build_profile(const char *name, struct ast_variable *v)
  477. {
  478. struct phone_profile *profile;
  479. struct ast_var_t *var;
  480. if (!(profile = ao2_alloc(sizeof(*profile), profile_destructor))) {
  481. return;
  482. }
  483. if (ast_string_field_init(profile, 32)) {
  484. profile = unref_profile(profile);
  485. return;
  486. }
  487. if (!(profile->headp = ast_calloc(1, sizeof(*profile->headp)))) {
  488. profile = unref_profile(profile);
  489. return;
  490. }
  491. AST_LIST_HEAD_INIT_NOLOCK(&profile->static_files);
  492. AST_LIST_HEAD_INIT_NOLOCK(&profile->dynamic_files);
  493. ast_string_field_set(profile, name, name);
  494. for (; v; v = v->next) {
  495. if (!strcasecmp(v->name, "mime_type")) {
  496. ast_string_field_set(profile, default_mime_type, v->value);
  497. } else if (!strcasecmp(v->name, "setvar")) {
  498. struct ast_var_t *variable;
  499. char *value_copy = ast_strdupa(v->value);
  500. AST_DECLARE_APP_ARGS(args,
  501. AST_APP_ARG(varname);
  502. AST_APP_ARG(varval);
  503. );
  504. AST_NONSTANDARD_APP_ARGS(args, value_copy, '=');
  505. do {
  506. if (ast_strlen_zero(args.varname) || ast_strlen_zero(args.varval))
  507. break;
  508. args.varname = ast_strip(args.varname);
  509. args.varval = ast_strip(args.varval);
  510. if (ast_strlen_zero(args.varname) || ast_strlen_zero(args.varval))
  511. break;
  512. if ((variable = ast_var_assign(args.varname, args.varval)))
  513. AST_LIST_INSERT_TAIL(profile->headp, variable, entries);
  514. } while (0);
  515. } else if (!strcasecmp(v->name, "staticdir")) {
  516. ast_string_field_set(profile, staticdir, v->value);
  517. } else {
  518. struct phoneprov_file *pp_file;
  519. char *file_extension;
  520. char *value_copy = ast_strdupa(v->value);
  521. AST_DECLARE_APP_ARGS(args,
  522. AST_APP_ARG(filename);
  523. AST_APP_ARG(mimetype);
  524. );
  525. if (!(pp_file = ast_calloc_with_stringfields(1, struct phoneprov_file, 32))) {
  526. profile = unref_profile(profile);
  527. return;
  528. }
  529. if ((file_extension = strrchr(pp_file->format, '.')))
  530. file_extension++;
  531. AST_STANDARD_APP_ARGS(args, value_copy);
  532. /* Mime type order of preference
  533. * 1) Specific mime-type defined for file in profile
  534. * 2) Mime determined by extension
  535. * 3) Default mime type specified in profile
  536. * 4) text/plain
  537. */
  538. ast_string_field_set(pp_file, mime_type, S_OR(args.mimetype,
  539. (S_OR(S_OR(ast_http_ftype2mtype(file_extension), profile->default_mime_type), "text/plain"))));
  540. if (!strcasecmp(v->name, "static_file")) {
  541. ast_string_field_set(pp_file, format, args.filename);
  542. ast_string_field_build(pp_file, template, "%s%s", profile->staticdir, args.filename);
  543. AST_LIST_INSERT_TAIL(&profile->static_files, pp_file, entry);
  544. /* Add a route for the static files, as their filenames won't change per-user */
  545. build_route(pp_file, NULL, NULL);
  546. } else {
  547. ast_string_field_set(pp_file, format, v->name);
  548. ast_string_field_set(pp_file, template, args.filename);
  549. AST_LIST_INSERT_TAIL(&profile->dynamic_files, pp_file, entry);
  550. }
  551. }
  552. }
  553. /* Append the global variables to the variables list for this profile.
  554. * This is for convenience later, when we need to provide a single
  555. * variable list for use in substitution. */
  556. ast_mutex_lock(&globals_lock);
  557. AST_LIST_TRAVERSE(&global_variables, var, entries) {
  558. struct ast_var_t *new_var;
  559. if ((new_var = ast_var_assign(var->name, var->value))) {
  560. AST_LIST_INSERT_TAIL(profile->headp, new_var, entries);
  561. }
  562. }
  563. ast_mutex_unlock(&globals_lock);
  564. ao2_link(profiles, profile);
  565. profile = unref_profile(profile);
  566. }
  567. static struct extension *delete_extension(struct extension *exten)
  568. {
  569. struct ast_var_t *var;
  570. while ((var = AST_LIST_REMOVE_HEAD(exten->headp, entries))) {
  571. ast_var_delete(var);
  572. }
  573. ast_free(exten->headp);
  574. ast_string_field_free_memory(exten);
  575. ast_free(exten);
  576. return NULL;
  577. }
  578. static struct extension *build_extension(struct ast_config *cfg, const char *name)
  579. {
  580. struct extension *exten;
  581. struct ast_var_t *var;
  582. const char *tmp;
  583. int i;
  584. if (!(exten = ast_calloc_with_stringfields(1, struct extension, 32))) {
  585. return NULL;
  586. }
  587. ast_string_field_set(exten, name, name);
  588. if (!(exten->headp = ast_calloc(1, sizeof(*exten->headp)))) {
  589. ast_free(exten);
  590. exten = NULL;
  591. return NULL;
  592. }
  593. for (i = 0; i < PP_VAR_LIST_LENGTH; i++) {
  594. tmp = ast_variable_retrieve(cfg, name, pp_variable_list[i].user_var);
  595. /* If we didn't get a USERNAME variable, set it to the user->name */
  596. if (i == PP_USERNAME && !tmp) {
  597. if ((var = ast_var_assign(pp_variable_list[PP_USERNAME].template_var, exten->name))) {
  598. AST_LIST_INSERT_TAIL(exten->headp, var, entries);
  599. }
  600. continue;
  601. } else if (i == PP_TIMEZONE) {
  602. /* perfectly ok if tmp is NULL, will set variables based on server's time zone */
  603. set_timezone_variables(exten->headp, tmp);
  604. } else if (i == PP_LINENUMBER) {
  605. if (!tmp) {
  606. tmp = "1";
  607. }
  608. exten->index = atoi(tmp);
  609. } else if (i == PP_LINEKEYS) {
  610. if (!tmp) {
  611. tmp = "1";
  612. }
  613. }
  614. if (tmp && (var = ast_var_assign(pp_variable_list[i].template_var, tmp))) {
  615. AST_LIST_INSERT_TAIL(exten->headp, var, entries);
  616. }
  617. }
  618. if (!ast_strlen_zero(global_server)) {
  619. if ((var = ast_var_assign("SERVER", global_server)))
  620. AST_LIST_INSERT_TAIL(exten->headp, var, entries);
  621. }
  622. if (!ast_strlen_zero(global_serverport)) {
  623. if ((var = ast_var_assign("SERVER_PORT", global_serverport)))
  624. AST_LIST_INSERT_TAIL(exten->headp, var, entries);
  625. }
  626. return exten;
  627. }
  628. static struct user *unref_user(struct user *user)
  629. {
  630. ao2_ref(user, -1);
  631. return NULL;
  632. }
  633. /*! \brief Return a user looked up by name */
  634. static struct user *find_user(const char *macaddress)
  635. {
  636. struct user tmp = {
  637. .macaddress = macaddress,
  638. };
  639. return ao2_find(users, &tmp, OBJ_POINTER);
  640. }
  641. static int users_hash_fn(const void *obj, const int flags)
  642. {
  643. const struct user *user = obj;
  644. return ast_str_case_hash(user->macaddress);
  645. }
  646. static int users_cmp_fn(void *obj, void *arg, int flags)
  647. {
  648. const struct user *user1 = obj, *user2 = arg;
  649. return !strcasecmp(user1->macaddress, user2->macaddress) ? CMP_MATCH | CMP_STOP : 0;
  650. }
  651. /*! \brief Free all memory associated with a user */
  652. static void user_destructor(void *obj)
  653. {
  654. struct user *user = obj;
  655. struct extension *exten;
  656. while ((exten = AST_LIST_REMOVE_HEAD(&user->extensions, entry))) {
  657. exten = delete_extension(exten);
  658. }
  659. if (user->profile) {
  660. user->profile = unref_profile(user->profile);
  661. }
  662. ast_string_field_free_memory(user);
  663. }
  664. /*! \brief Delete all users */
  665. static void delete_users(void)
  666. {
  667. struct ao2_iterator i;
  668. struct user *user;
  669. i = ao2_iterator_init(users, 0);
  670. while ((user = ao2_iterator_next(&i))) {
  671. ao2_unlink(users, user);
  672. user = unref_user(user);
  673. }
  674. ao2_iterator_destroy(&i);
  675. }
  676. /*! \brief Build and return a user structure based on gathered config data */
  677. static struct user *build_user(const char *mac, struct phone_profile *profile)
  678. {
  679. struct user *user;
  680. if (!(user = ao2_alloc(sizeof(*user), user_destructor))) {
  681. profile = unref_profile(profile);
  682. return NULL;
  683. }
  684. if (ast_string_field_init(user, 32)) {
  685. profile = unref_profile(profile);
  686. user = unref_user(user);
  687. return NULL;
  688. }
  689. ast_string_field_set(user, macaddress, mac);
  690. user->profile = profile; /* already ref counted by find_profile */
  691. return user;
  692. }
  693. /*! \brief Add an extension to a user ordered by index/linenumber */
  694. static int add_user_extension(struct user *user, struct extension *exten)
  695. {
  696. struct ast_var_t *var;
  697. struct ast_str *str = ast_str_create(16);
  698. if (!str) {
  699. return -1;
  700. }
  701. /* Append profile variables here, and substitute variables on profile
  702. * setvars, so that we can use user specific variables in them */
  703. AST_LIST_TRAVERSE(user->profile->headp, var, entries) {
  704. struct ast_var_t *var2;
  705. ast_str_substitute_variables_varshead(&str, 0, exten->headp, var->value);
  706. if ((var2 = ast_var_assign(var->name, ast_str_buffer(str)))) {
  707. AST_LIST_INSERT_TAIL(exten->headp, var2, entries);
  708. }
  709. }
  710. ast_free(str);
  711. if (AST_LIST_EMPTY(&user->extensions)) {
  712. AST_LIST_INSERT_HEAD(&user->extensions, exten, entry);
  713. } else {
  714. struct extension *exten_iter;
  715. AST_LIST_TRAVERSE_SAFE_BEGIN(&user->extensions, exten_iter, entry) {
  716. if (exten->index < exten_iter->index) {
  717. AST_LIST_INSERT_BEFORE_CURRENT(exten, entry);
  718. } else if (exten->index == exten_iter->index) {
  719. ast_log(LOG_WARNING, "Duplicate linenumber=%d for %s\n", exten->index, user->macaddress);
  720. return -1;
  721. } else if (!AST_LIST_NEXT(exten_iter, entry)) {
  722. AST_LIST_INSERT_TAIL(&user->extensions, exten, entry);
  723. }
  724. }
  725. AST_LIST_TRAVERSE_SAFE_END;
  726. }
  727. return 0;
  728. }
  729. /*! \brief Add an http route for dynamic files attached to the profile of the user */
  730. static int build_user_routes(struct user *user)
  731. {
  732. struct phoneprov_file *pp_file;
  733. struct ast_str *str;
  734. if (!(str = ast_str_create(16))) {
  735. return -1;
  736. }
  737. AST_LIST_TRAVERSE(&user->profile->dynamic_files, pp_file, entry) {
  738. ast_str_substitute_variables_varshead(&str, 0, AST_LIST_FIRST(&user->extensions)->headp, pp_file->format);
  739. build_route(pp_file, user, ast_str_buffer(str));
  740. }
  741. ast_free(str);
  742. return 0;
  743. }
  744. /* \brief Parse config files and create appropriate structures */
  745. static int set_config(void)
  746. {
  747. struct ast_config *cfg, *phoneprov_cfg;
  748. char *cat;
  749. struct ast_variable *v;
  750. struct ast_flags config_flags = { 0 };
  751. struct ast_var_t *var;
  752. /* Try to grab the port from sip.conf. If we don't get it here, we'll set it
  753. * to whatever is set in phoneprov.conf or default to 5060 */
  754. if ((cfg = ast_config_load("sip.conf", config_flags)) && cfg != CONFIG_STATUS_FILEINVALID) {
  755. ast_copy_string(global_serverport, S_OR(ast_variable_retrieve(cfg, "general", "bindport"), "5060"), sizeof(global_serverport));
  756. ast_config_destroy(cfg);
  757. }
  758. if (!(cfg = ast_config_load("users.conf", config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
  759. ast_log(LOG_WARNING, "Unable to load users.conf\n");
  760. return 0;
  761. }
  762. /* Go ahead and load global variables from users.conf so we can append to profiles */
  763. for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
  764. if (!strcasecmp(v->name, "vmexten")) {
  765. if ((var = ast_var_assign("VOICEMAIL_EXTEN", v->value))) {
  766. ast_mutex_lock(&globals_lock);
  767. AST_LIST_INSERT_TAIL(&global_variables, var, entries);
  768. ast_mutex_unlock(&globals_lock);
  769. }
  770. }
  771. if (!strcasecmp(v->name, "localextenlength")) {
  772. if ((var = ast_var_assign("EXTENSION_LENGTH", v->value)))
  773. ast_mutex_lock(&globals_lock);
  774. AST_LIST_INSERT_TAIL(&global_variables, var, entries);
  775. ast_mutex_unlock(&globals_lock);
  776. }
  777. }
  778. if (!(phoneprov_cfg = ast_config_load("phoneprov.conf", config_flags)) || phoneprov_cfg == CONFIG_STATUS_FILEINVALID) {
  779. ast_log(LOG_ERROR, "Unable to load config phoneprov.conf\n");
  780. ast_config_destroy(cfg);
  781. return -1;
  782. }
  783. cat = NULL;
  784. while ((cat = ast_category_browse(phoneprov_cfg, cat))) {
  785. if (!strcasecmp(cat, "general")) {
  786. for (v = ast_variable_browse(phoneprov_cfg, cat); v; v = v->next) {
  787. if (!strcasecmp(v->name, "serveraddr"))
  788. ast_copy_string(global_server, v->value, sizeof(global_server));
  789. else if (!strcasecmp(v->name, "serveriface")) {
  790. struct in_addr addr;
  791. lookup_iface(v->value, &addr);
  792. ast_copy_string(global_server, ast_inet_ntoa(addr), sizeof(global_server));
  793. } else if (!strcasecmp(v->name, "serverport"))
  794. ast_copy_string(global_serverport, v->value, sizeof(global_serverport));
  795. else if (!strcasecmp(v->name, "default_profile"))
  796. ast_copy_string(global_default_profile, v->value, sizeof(global_default_profile));
  797. }
  798. } else
  799. build_profile(cat, ast_variable_browse(phoneprov_cfg, cat));
  800. }
  801. ast_config_destroy(phoneprov_cfg);
  802. cat = NULL;
  803. while ((cat = ast_category_browse(cfg, cat))) {
  804. const char *tmp, *mac;
  805. struct user *user;
  806. struct phone_profile *profile;
  807. struct extension *exten;
  808. if (!strcasecmp(cat, "general")) {
  809. continue;
  810. }
  811. if (!strcasecmp(cat, "authentication"))
  812. continue;
  813. if (!((tmp = ast_variable_retrieve(cfg, cat, "autoprov")) && ast_true(tmp)))
  814. continue;
  815. if (!(mac = ast_variable_retrieve(cfg, cat, "macaddress"))) {
  816. ast_log(LOG_WARNING, "autoprov set for %s, but no mac address - skipping.\n", cat);
  817. continue;
  818. }
  819. tmp = S_OR(ast_variable_retrieve(cfg, cat, "profile"), global_default_profile);
  820. if (ast_strlen_zero(tmp)) {
  821. ast_log(LOG_WARNING, "No profile for user [%s] with mac '%s' - skipping\n", cat, mac);
  822. continue;
  823. }
  824. if (!(user = find_user(mac))) {
  825. if (!(profile = find_profile(tmp))) {
  826. ast_log(LOG_WARNING, "Could not look up profile '%s' - skipping.\n", tmp);
  827. continue;
  828. }
  829. if (!(user = build_user(mac, profile))) {
  830. ast_log(LOG_WARNING, "Could not create user for '%s' - skipping\n", user->macaddress);
  831. continue;
  832. }
  833. if (!(exten = build_extension(cfg, cat))) {
  834. ast_log(LOG_WARNING, "Could not create extension for %s - skipping\n", user->macaddress);
  835. user = unref_user(user);
  836. continue;
  837. }
  838. if (add_user_extension(user, exten)) {
  839. ast_log(LOG_WARNING, "Could not add extension '%s' to user '%s'\n", exten->name, user->macaddress);
  840. user = unref_user(user);
  841. exten = delete_extension(exten);
  842. continue;
  843. }
  844. if (build_user_routes(user)) {
  845. ast_log(LOG_WARNING, "Could not create http routes for %s - skipping\n", user->macaddress);
  846. user = unref_user(user);
  847. continue;
  848. }
  849. ao2_link(users, user);
  850. user = unref_user(user);
  851. } else {
  852. if (!(exten = build_extension(cfg, cat))) {
  853. ast_log(LOG_WARNING, "Could not create extension for %s - skipping\n", user->macaddress);
  854. user = unref_user(user);
  855. continue;
  856. }
  857. if (add_user_extension(user, exten)) {
  858. ast_log(LOG_WARNING, "Could not add extension '%s' to user '%s'\n", exten->name, user->macaddress);
  859. user = unref_user(user);
  860. exten = delete_extension(exten);
  861. continue;
  862. }
  863. user = unref_user(user);
  864. }
  865. }
  866. ast_config_destroy(cfg);
  867. return 0;
  868. }
  869. /*! \brief Delete all http routes, freeing their memory */
  870. static void delete_routes(void)
  871. {
  872. struct ao2_iterator i;
  873. struct http_route *route;
  874. i = ao2_iterator_init(http_routes, 0);
  875. while ((route = ao2_iterator_next(&i))) {
  876. ao2_unlink(http_routes, route);
  877. route = unref_route(route);
  878. }
  879. ao2_iterator_destroy(&i);
  880. }
  881. /*! \brief Delete all phone profiles, freeing their memory */
  882. static void delete_profiles(void)
  883. {
  884. struct ao2_iterator i;
  885. struct phone_profile *profile;
  886. i = ao2_iterator_init(profiles, 0);
  887. while ((profile = ao2_iterator_next(&i))) {
  888. ao2_unlink(profiles, profile);
  889. profile = unref_profile(profile);
  890. }
  891. ao2_iterator_destroy(&i);
  892. }
  893. /*! \brief A dialplan function that can be used to print a string for each phoneprov user */
  894. static int pp_each_user_helper(struct ast_channel *chan, char *data, char *buf, struct ast_str **bufstr, int len)
  895. {
  896. char *tmp;
  897. struct ao2_iterator i;
  898. struct user *user;
  899. struct ast_str *str;
  900. AST_DECLARE_APP_ARGS(args,
  901. AST_APP_ARG(string);
  902. AST_APP_ARG(exclude_mac);
  903. );
  904. AST_STANDARD_APP_ARGS(args, data);
  905. if (!(str = ast_str_create(16))) {
  906. return -1;
  907. }
  908. /* Fix data by turning %{ into ${ */
  909. while ((tmp = strstr(args.string, "%{")))
  910. *tmp = '$';
  911. i = ao2_iterator_init(users, 0);
  912. while ((user = ao2_iterator_next(&i))) {
  913. if (!ast_strlen_zero(args.exclude_mac) && !strcasecmp(user->macaddress, args.exclude_mac)) {
  914. continue;
  915. }
  916. ast_str_substitute_variables_varshead(&str, len, AST_LIST_FIRST(&user->extensions)->headp, args.string);
  917. if (buf) {
  918. size_t slen = len;
  919. ast_build_string(&buf, &slen, "%s", ast_str_buffer(str));
  920. } else {
  921. ast_str_append(bufstr, len, "%s", ast_str_buffer(str));
  922. }
  923. user = unref_user(user);
  924. }
  925. ao2_iterator_destroy(&i);
  926. ast_free(str);
  927. return 0;
  928. }
  929. static int pp_each_user_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  930. {
  931. return pp_each_user_helper(chan, data, buf, NULL, len);
  932. }
  933. static int pp_each_user_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
  934. {
  935. return pp_each_user_helper(chan, data, NULL, buf, len);
  936. }
  937. static struct ast_custom_function pp_each_user_function = {
  938. .name = "PP_EACH_USER",
  939. .read = pp_each_user_read,
  940. .read2 = pp_each_user_read2,
  941. };
  942. /*! \brief A dialplan function that can be used to output a template for each extension attached to a user */
  943. static int pp_each_extension_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, struct ast_str **bufstr, int len)
  944. {
  945. struct user *user;
  946. struct extension *exten;
  947. char path[PATH_MAX];
  948. char *file;
  949. int filelen;
  950. struct ast_str *str;
  951. AST_DECLARE_APP_ARGS(args,
  952. AST_APP_ARG(mac);
  953. AST_APP_ARG(template);
  954. );
  955. AST_STANDARD_APP_ARGS(args, data);
  956. if (ast_strlen_zero(args.mac) || ast_strlen_zero(args.template)) {
  957. ast_log(LOG_WARNING, "PP_EACH_EXTENSION requries both a macaddress and template filename.\n");
  958. return 0;
  959. }
  960. if (!(user = find_user(args.mac))) {
  961. ast_log(LOG_WARNING, "Could not find user with mac = '%s'\n", args.mac);
  962. return 0;
  963. }
  964. snprintf(path, sizeof(path), "%s/phoneprov/%s", ast_config_AST_DATA_DIR, args.template);
  965. filelen = load_file(path, &file);
  966. if (filelen < 0) {
  967. ast_log(LOG_WARNING, "Could not load file: %s (%d)\n", path, filelen);
  968. if (file) {
  969. ast_free(file);
  970. }
  971. return 0;
  972. }
  973. if (!file) {
  974. return 0;
  975. }
  976. if (!(str = ast_str_create(filelen))) {
  977. return 0;
  978. }
  979. AST_LIST_TRAVERSE(&user->extensions, exten, entry) {
  980. ast_str_substitute_variables_varshead(&str, 0, exten->headp, file);
  981. if (buf) {
  982. size_t slen = len;
  983. ast_build_string(&buf, &slen, "%s", ast_str_buffer(str));
  984. } else {
  985. ast_str_append(bufstr, len, "%s", ast_str_buffer(str));
  986. }
  987. }
  988. ast_free(file);
  989. ast_free(str);
  990. user = unref_user(user);
  991. return 0;
  992. }
  993. static int pp_each_extension_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  994. {
  995. return pp_each_extension_helper(chan, cmd, data, buf, NULL, len);
  996. }
  997. static int pp_each_extension_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
  998. {
  999. return pp_each_extension_helper(chan, cmd, data, NULL, buf, len);
  1000. }
  1001. static struct ast_custom_function pp_each_extension_function = {
  1002. .name = "PP_EACH_EXTENSION",
  1003. .read = pp_each_extension_read,
  1004. .read2 = pp_each_extension_read2,
  1005. };
  1006. /*! \brief CLI command to list static and dynamic routes */
  1007. static char *handle_show_routes(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1008. {
  1009. #define FORMAT "%-40.40s %-30.30s\n"
  1010. struct ao2_iterator i;
  1011. struct http_route *route;
  1012. switch(cmd) {
  1013. case CLI_INIT:
  1014. e->command = "phoneprov show routes";
  1015. e->usage =
  1016. "Usage: phoneprov show routes\n"
  1017. " Lists all registered phoneprov http routes.\n";
  1018. return NULL;
  1019. case CLI_GENERATE:
  1020. return NULL;
  1021. }
  1022. /* This currently iterates over routes twice, but it is the only place I've needed
  1023. * to really separate static an dynamic routes, so I've just left it this way. */
  1024. ast_cli(a->fd, "Static routes\n\n");
  1025. ast_cli(a->fd, FORMAT, "Relative URI", "Physical location");
  1026. i = ao2_iterator_init(http_routes, 0);
  1027. while ((route = ao2_iterator_next(&i))) {
  1028. if (!route->user)
  1029. ast_cli(a->fd, FORMAT, route->uri, route->file->template);
  1030. route = unref_route(route);
  1031. }
  1032. ao2_iterator_destroy(&i);
  1033. ast_cli(a->fd, "\nDynamic routes\n\n");
  1034. ast_cli(a->fd, FORMAT, "Relative URI", "Template");
  1035. i = ao2_iterator_init(http_routes, 0);
  1036. while ((route = ao2_iterator_next(&i))) {
  1037. if (route->user)
  1038. ast_cli(a->fd, FORMAT, route->uri, route->file->template);
  1039. route = unref_route(route);
  1040. }
  1041. ao2_iterator_destroy(&i);
  1042. return CLI_SUCCESS;
  1043. }
  1044. static struct ast_cli_entry pp_cli[] = {
  1045. AST_CLI_DEFINE(handle_show_routes, "Show registered phoneprov http routes"),
  1046. };
  1047. static struct ast_http_uri phoneprovuri = {
  1048. .callback = phoneprov_callback,
  1049. .description = "Asterisk HTTP Phone Provisioning Tool",
  1050. .uri = "phoneprov",
  1051. .has_subtree = 1,
  1052. .data = NULL,
  1053. .key = __FILE__,
  1054. };
  1055. static int load_module(void)
  1056. {
  1057. profiles = ao2_container_alloc(MAX_PROFILE_BUCKETS, profile_hash_fn, profile_cmp_fn);
  1058. http_routes = ao2_container_alloc(MAX_ROUTE_BUCKETS, routes_hash_fn, routes_cmp_fn);
  1059. users = ao2_container_alloc(MAX_USER_BUCKETS, users_hash_fn, users_cmp_fn);
  1060. AST_LIST_HEAD_INIT_NOLOCK(&global_variables);
  1061. ast_mutex_init(&globals_lock);
  1062. ast_custom_function_register(&pp_each_user_function);
  1063. ast_custom_function_register(&pp_each_extension_function);
  1064. ast_cli_register_multiple(pp_cli, ARRAY_LEN(pp_cli));
  1065. set_config();
  1066. ast_http_uri_link(&phoneprovuri);
  1067. return 0;
  1068. }
  1069. static int unload_module(void)
  1070. {
  1071. struct ast_var_t *var;
  1072. ast_http_uri_unlink(&phoneprovuri);
  1073. ast_custom_function_unregister(&pp_each_user_function);
  1074. ast_custom_function_unregister(&pp_each_extension_function);
  1075. ast_cli_unregister_multiple(pp_cli, ARRAY_LEN(pp_cli));
  1076. delete_routes();
  1077. delete_users();
  1078. delete_profiles();
  1079. ao2_ref(profiles, -1);
  1080. ao2_ref(http_routes, -1);
  1081. ao2_ref(users, -1);
  1082. ast_mutex_lock(&globals_lock);
  1083. while ((var = AST_LIST_REMOVE_HEAD(&global_variables, entries))) {
  1084. ast_var_delete(var);
  1085. }
  1086. ast_mutex_unlock(&globals_lock);
  1087. ast_mutex_destroy(&globals_lock);
  1088. return 0;
  1089. }
  1090. static int reload(void)
  1091. {
  1092. struct ast_var_t *var;
  1093. delete_routes();
  1094. delete_users();
  1095. delete_profiles();
  1096. ast_mutex_lock(&globals_lock);
  1097. while ((var = AST_LIST_REMOVE_HEAD(&global_variables, entries))) {
  1098. ast_var_delete(var);
  1099. }
  1100. ast_mutex_unlock(&globals_lock);
  1101. set_config();
  1102. return 0;
  1103. }
  1104. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "HTTP Phone Provisioning",
  1105. .load = load_module,
  1106. .unload = unload_module,
  1107. .reload = reload,
  1108. );