res_phoneprov.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2008, Digium, Inc.
  5. * Copyright (C) 2014, Fairview 5 Engineering, LLC
  6. *
  7. * Mark Spencer <markster@digium.com>
  8. * Matthew Brooks <mbrooks@digium.com>
  9. * Terry Wilson <twilson@digium.com>
  10. * George Joseph <george.joseph@fairview5.com>
  11. *
  12. * See http://www.asterisk.org for more information about
  13. * the Asterisk project. Please do not directly contact
  14. * any of the maintainers of this project for assistance;
  15. * the project provides a web site, mailing lists and IRC
  16. * channels for your use.
  17. *
  18. * This program is free software, distributed under the terms of
  19. * the GNU General Public License Version 2. See the LICENSE file
  20. * at the top of the source tree.
  21. */
  22. /*! \file
  23. *
  24. * \brief Phone provisioning application for the asterisk internal http server
  25. *
  26. * \author Matthew Brooks <mbrooks@digium.com>
  27. * \author Terry Wilson <twilson@digium.com>
  28. * \author George Joseph <george.joseph@fairview5.com>
  29. */
  30. /*! \li \ref res_phoneprov.c uses the configuration file \ref phoneprov.conf and \ref users.conf and \ref sip.conf
  31. * \addtogroup configuration_file Configuration Files
  32. */
  33. /*!
  34. * \page phoneprov.conf phoneprov.conf
  35. * \verbinclude phoneprov.conf.sample
  36. */
  37. /*** MODULEINFO
  38. <support_level>extended</support_level>
  39. ***/
  40. #define AST_API_MODULE
  41. #include "asterisk.h"
  42. #include <sys/ioctl.h>
  43. #include <sys/socket.h>
  44. #include <net/if.h>
  45. #ifdef SOLARIS
  46. #include <sys/sockio.h>
  47. #endif
  48. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  49. #include "asterisk/channel.h"
  50. #include "asterisk/file.h"
  51. #include "asterisk/paths.h"
  52. #include "asterisk/pbx.h"
  53. #include "asterisk/cli.h"
  54. #include "asterisk/module.h"
  55. #include "asterisk/http.h"
  56. #include "asterisk/utils.h"
  57. #include "asterisk/app.h"
  58. #include "asterisk/strings.h"
  59. #include "asterisk/stringfields.h"
  60. #include "asterisk/options.h"
  61. #include "asterisk/config.h"
  62. #include "asterisk/acl.h"
  63. #include "asterisk/astobj2.h"
  64. #include "asterisk/ast_version.h"
  65. #include "asterisk/phoneprov.h"
  66. #ifdef LOW_MEMORY
  67. #define MAX_PROVIDER_BUCKETS 1
  68. #define MAX_PROFILE_BUCKETS 1
  69. #define MAX_ROUTE_BUCKETS 1
  70. #define MAX_USER_BUCKETS 1
  71. #else
  72. #define MAX_PROVIDER_BUCKETS 17
  73. #define MAX_PROFILE_BUCKETS 17
  74. #define MAX_ROUTE_BUCKETS 563
  75. #define MAX_USER_BUCKETS 563
  76. #endif /* LOW_MEMORY */
  77. #define VAR_BUF_SIZE 4096
  78. /*** DOCUMENTATION
  79. <function name="PP_EACH_EXTENSION" language="en_US">
  80. <synopsis>
  81. Execute specified template for each extension.
  82. </synopsis>
  83. <syntax>
  84. <parameter name="mac" required="true" />
  85. <parameter name="template_file" required="true" />
  86. </syntax>
  87. <description>
  88. <para>Output the specified template for each extension associated with the specified MAC address.</para>
  89. </description>
  90. </function>
  91. <function name="PP_EACH_USER" language="en_US">
  92. <synopsis>
  93. Generate a string for each phoneprov user.
  94. </synopsis>
  95. <syntax>
  96. <parameter name="string" required="true" />
  97. <parameter name="exclude_mac" required="true" />
  98. </syntax>
  99. <description>
  100. <para>Pass in a string, with phoneprov variables you want substituted in the format of
  101. %{VARNAME}, and you will get the string rendered for each user in phoneprov
  102. excluding ones with MAC address <replaceable>exclude_mac</replaceable>. Probably not
  103. useful outside of res_phoneprov.</para>
  104. <para>Example: ${PP_EACH_USER(&lt;item&gt;&lt;fn&gt;%{DISPLAY_NAME}&lt;/fn&gt;&lt;/item&gt;|${MAC})</para>
  105. </description>
  106. </function>
  107. ***/
  108. /*!
  109. * \brief Creates a hash function for a structure string field.
  110. * \param fname The name to use for the function
  111. * \param stype The structure type
  112. * \param field The field in the structure to hash
  113. *
  114. * SIMPLE_HASH_FN(mystruct, myfield) will produce a function
  115. * named mystruct_hash_fn which hashes mystruct->myfield.
  116. */
  117. #define SIMPLE_HASH_FN(fname, stype, field) \
  118. static int fname(const void *obj, const int flags) \
  119. { \
  120. const struct stype *provider = obj; \
  121. const char *key; \
  122. switch (flags & OBJ_SEARCH_MASK) { \
  123. case OBJ_SEARCH_KEY: \
  124. key = obj; \
  125. break; \
  126. case OBJ_SEARCH_OBJECT: \
  127. provider = obj; \
  128. key = provider->field; \
  129. break; \
  130. default: \
  131. ast_assert(0); \
  132. return 0; \
  133. } \
  134. return ast_str_hash(key); \
  135. }
  136. /*!
  137. * \brief Creates a compare function for a structure string field.
  138. * \param fname The name to use for the function
  139. * \param stype The structure type
  140. * \param field The field in the structure to compare
  141. *
  142. * SIMPLE_CMP_FN(mystruct, myfield) will produce a function
  143. * named mystruct_cmp_fn which compares mystruct->myfield.
  144. */
  145. #define SIMPLE_CMP_FN(fname, stype, field) \
  146. static int fname(void *obj, void *arg, int flags) \
  147. { \
  148. const struct stype *object_left = obj, *object_right = arg; \
  149. const char *right_key = arg; \
  150. int cmp; \
  151. switch (flags & OBJ_SEARCH_MASK) { \
  152. case OBJ_SEARCH_OBJECT: \
  153. right_key = object_right->field; \
  154. case OBJ_SEARCH_KEY: \
  155. cmp = strcmp(object_left->field, right_key); \
  156. break; \
  157. case OBJ_SEARCH_PARTIAL_KEY: \
  158. cmp = strncmp(object_left->field, right_key, strlen(right_key)); \
  159. break; \
  160. default: \
  161. cmp = 0; \
  162. break; \
  163. } \
  164. if (cmp) { \
  165. return 0; \
  166. } \
  167. return CMP_MATCH; \
  168. }
  169. static const char *variable_lookup[] = {
  170. [AST_PHONEPROV_STD_MAC] = "MAC",
  171. [AST_PHONEPROV_STD_PROFILE] = "PROFILE",
  172. [AST_PHONEPROV_STD_USERNAME] = "USERNAME",
  173. [AST_PHONEPROV_STD_DISPLAY_NAME] = "DISPLAY_NAME",
  174. [AST_PHONEPROV_STD_SECRET] = "SECRET",
  175. [AST_PHONEPROV_STD_LABEL] = "LABEL",
  176. [AST_PHONEPROV_STD_CALLERID] = "CALLERID",
  177. [AST_PHONEPROV_STD_TIMEZONE] = "TIMEZONE",
  178. [AST_PHONEPROV_STD_LINENUMBER] = "LINE",
  179. [AST_PHONEPROV_STD_LINEKEYS] = "LINEKEYS",
  180. [AST_PHONEPROV_STD_SERVER] = "SERVER",
  181. [AST_PHONEPROV_STD_SERVER_PORT] = "SERVER_PORT",
  182. [AST_PHONEPROV_STD_SERVER_IFACE] = "SERVER_IFACE",
  183. [AST_PHONEPROV_STD_VOICEMAIL_EXTEN] = "VOICEMAIL_EXTEN",
  184. [AST_PHONEPROV_STD_EXTENSION_LENGTH] = "EXTENSION_LENGTH",
  185. [AST_PHONEPROV_STD_TZOFFSET] = "TZOFFSET",
  186. [AST_PHONEPROV_STD_DST_ENABLE] = "DST_ENABLE",
  187. [AST_PHONEPROV_STD_DST_START_MONTH] = "DST_START_MONTH",
  188. [AST_PHONEPROV_STD_DST_START_MDAY] = "DST_START_MDAY",
  189. [AST_PHONEPROV_STD_DST_START_HOUR] = "DST_START_HOUR",
  190. [AST_PHONEPROV_STD_DST_END_MONTH] = "DST_END_MONTH",
  191. [AST_PHONEPROV_STD_DST_END_MDAY] = "DST_END_MDAY",
  192. [AST_PHONEPROV_STD_DST_END_HOUR] = "DST_END_HOUR",
  193. };
  194. /* Translate the standard variables to their users.conf equivalents. */
  195. static const char *pp_user_lookup[] = {
  196. [AST_PHONEPROV_STD_MAC] = "macaddress",
  197. [AST_PHONEPROV_STD_PROFILE] = "profile",
  198. [AST_PHONEPROV_STD_USERNAME] = "username",
  199. [AST_PHONEPROV_STD_DISPLAY_NAME] = "fullname",
  200. [AST_PHONEPROV_STD_SECRET] = "secret",
  201. [AST_PHONEPROV_STD_LABEL] = "label",
  202. [AST_PHONEPROV_STD_CALLERID] = "cid_number",
  203. [AST_PHONEPROV_STD_TIMEZONE] = "timezone",
  204. [AST_PHONEPROV_STD_LINENUMBER] = "linenumber",
  205. [AST_PHONEPROV_STD_LINEKEYS] = "linekeys",
  206. [AST_PHONEPROV_STD_SERVER] = NULL,
  207. [AST_PHONEPROV_STD_SERVER_PORT] = NULL,
  208. [AST_PHONEPROV_STD_SERVER_IFACE] = NULL,
  209. [AST_PHONEPROV_STD_VOICEMAIL_EXTEN] = "vmexten",
  210. [AST_PHONEPROV_STD_EXTENSION_LENGTH] = "localextenlength",
  211. [AST_PHONEPROV_STD_TZOFFSET] = NULL,
  212. [AST_PHONEPROV_STD_DST_ENABLE] = NULL,
  213. [AST_PHONEPROV_STD_DST_START_MONTH] = NULL,
  214. [AST_PHONEPROV_STD_DST_START_MDAY] = NULL,
  215. [AST_PHONEPROV_STD_DST_START_HOUR] = NULL,
  216. [AST_PHONEPROV_STD_DST_END_MONTH] = NULL,
  217. [AST_PHONEPROV_STD_DST_END_MDAY] = NULL,
  218. [AST_PHONEPROV_STD_DST_END_HOUR] = NULL,
  219. };
  220. /* Translate the standard variables to their phoneprov.conf [general] equivalents. */
  221. static const char *pp_general_lookup[] = {
  222. [AST_PHONEPROV_STD_MAC] = NULL,
  223. [AST_PHONEPROV_STD_PROFILE] = "default_profile",
  224. [AST_PHONEPROV_STD_USERNAME] = NULL,
  225. [AST_PHONEPROV_STD_DISPLAY_NAME] = NULL,
  226. [AST_PHONEPROV_STD_SECRET] = NULL,
  227. [AST_PHONEPROV_STD_LABEL] = NULL,
  228. [AST_PHONEPROV_STD_CALLERID] = NULL,
  229. [AST_PHONEPROV_STD_TIMEZONE] = NULL,
  230. [AST_PHONEPROV_STD_LINENUMBER] = NULL,
  231. [AST_PHONEPROV_STD_LINEKEYS] = NULL,
  232. [AST_PHONEPROV_STD_SERVER] = "serveraddr",
  233. [AST_PHONEPROV_STD_SERVER_PORT] = "serverport",
  234. [AST_PHONEPROV_STD_SERVER_IFACE] = "serveriface",
  235. [AST_PHONEPROV_STD_VOICEMAIL_EXTEN] = NULL,
  236. [AST_PHONEPROV_STD_EXTENSION_LENGTH] = NULL,
  237. [AST_PHONEPROV_STD_TZOFFSET] = NULL,
  238. [AST_PHONEPROV_STD_DST_ENABLE] = NULL,
  239. [AST_PHONEPROV_STD_DST_START_MONTH] = NULL,
  240. [AST_PHONEPROV_STD_DST_START_MDAY] = NULL,
  241. [AST_PHONEPROV_STD_DST_START_HOUR] = NULL,
  242. [AST_PHONEPROV_STD_DST_END_MONTH] = NULL,
  243. [AST_PHONEPROV_STD_DST_END_MDAY] = NULL,
  244. [AST_PHONEPROV_STD_DST_END_HOUR] = NULL,
  245. };
  246. /*! \brief for use in lookup_iface */
  247. static struct in_addr __ourip = { .s_addr = 0x00000000, };
  248. /*! \brief structure to hold config providers */
  249. struct phoneprov_provider {
  250. AST_DECLARE_STRING_FIELDS(
  251. AST_STRING_FIELD(provider_name);
  252. );
  253. ast_phoneprov_load_users_cb load_users;
  254. };
  255. struct ao2_container *providers;
  256. SIMPLE_HASH_FN(phoneprov_provider_hash_fn, phoneprov_provider, provider_name)
  257. SIMPLE_CMP_FN(phoneprov_provider_cmp_fn, phoneprov_provider, provider_name)
  258. /*! \brief structure to hold file data */
  259. struct phoneprov_file {
  260. AST_DECLARE_STRING_FIELDS(
  261. AST_STRING_FIELD(format); /*!< After variable substitution, becomes route->uri */
  262. AST_STRING_FIELD(template); /*!< Template/physical file location */
  263. AST_STRING_FIELD(mime_type);/*!< Mime-type of the file */
  264. );
  265. AST_LIST_ENTRY(phoneprov_file) entry;
  266. };
  267. /*! \brief structure to hold extensions */
  268. struct extension {
  269. AST_DECLARE_STRING_FIELDS(
  270. AST_STRING_FIELD(name);
  271. );
  272. int index;
  273. struct varshead *headp; /*!< List of variables to substitute into templates */
  274. AST_LIST_ENTRY(extension) entry;
  275. };
  276. /*! \brief structure to hold phone profiles read from phoneprov.conf */
  277. struct phone_profile {
  278. AST_DECLARE_STRING_FIELDS(
  279. AST_STRING_FIELD(name); /*!< Name of phone profile */
  280. AST_STRING_FIELD(default_mime_type); /*!< Default mime type if it isn't provided */
  281. AST_STRING_FIELD(staticdir); /*!< Subdirectory that static files are stored in */
  282. );
  283. struct varshead *headp; /*!< List of variables set with 'setvar' in phoneprov.conf */
  284. AST_LIST_HEAD_NOLOCK(, phoneprov_file) static_files; /*!< List of static files */
  285. AST_LIST_HEAD_NOLOCK(, phoneprov_file) dynamic_files; /*!< List of dynamic files */
  286. };
  287. struct ao2_container *profiles;
  288. SIMPLE_HASH_FN(phone_profile_hash_fn, phone_profile, name)
  289. SIMPLE_CMP_FN(phone_profile_cmp_fn, phone_profile, name)
  290. /*! \brief structure to hold users read from users.conf */
  291. struct user {
  292. AST_DECLARE_STRING_FIELDS(
  293. AST_STRING_FIELD(macaddress); /*!< Mac address of user's phone */
  294. AST_STRING_FIELD(provider_name); /*!< Name of the provider who registered this mac */
  295. );
  296. struct phone_profile *profile; /*!< Profile the phone belongs to */
  297. AST_LIST_HEAD_NOLOCK(, extension) extensions;
  298. };
  299. struct ao2_container *users;
  300. SIMPLE_HASH_FN(user_hash_fn, user, macaddress)
  301. SIMPLE_CMP_FN(user_cmp_fn, user, macaddress)
  302. /*! \brief structure to hold http routes (valid URIs, and the files they link to) */
  303. struct http_route {
  304. AST_DECLARE_STRING_FIELDS(
  305. AST_STRING_FIELD(uri); /*!< The URI requested */
  306. );
  307. struct phoneprov_file *file; /*!< The file that links to the URI */
  308. struct user *user; /*!< The user that has variables to substitute into the file
  309. * NULL in the case of a static route */
  310. struct phone_profile *profile;
  311. };
  312. struct ao2_container *http_routes;
  313. SIMPLE_HASH_FN(http_route_hash_fn, http_route, uri)
  314. SIMPLE_CMP_FN(http_route_cmp_fn, http_route, uri)
  315. #define SIPUSERS_PROVIDER_NAME "sipusers"
  316. /* iface is the interface (e.g. eth0); address is the return value */
  317. static int lookup_iface(const char *iface, struct in_addr *address)
  318. {
  319. int mysock, res = 0;
  320. struct ifreq ifr;
  321. struct sockaddr_in *sin;
  322. memset(&ifr, 0, sizeof(ifr));
  323. ast_copy_string(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
  324. mysock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
  325. if (mysock < 0) {
  326. ast_log(LOG_ERROR, "Failed to create socket: %s\n", strerror(errno));
  327. return -1;
  328. }
  329. res = ioctl(mysock, SIOCGIFADDR, &ifr);
  330. close(mysock);
  331. if (res < 0) {
  332. ast_log(LOG_WARNING, "Unable to get IP of %s: %s\n", iface, strerror(errno));
  333. memcpy(address, &__ourip, sizeof(__ourip));
  334. return -1;
  335. } else {
  336. sin = (struct sockaddr_in *)&ifr.ifr_addr;
  337. memcpy(address, &sin->sin_addr, sizeof(*address));
  338. return 0;
  339. }
  340. }
  341. static struct phoneprov_provider *find_provider(char *name)
  342. {
  343. return ao2_find(providers, name, OBJ_SEARCH_KEY);
  344. }
  345. /*! \brief Delete all providers */
  346. static void delete_providers(void)
  347. {
  348. if (!providers) {
  349. return;
  350. }
  351. ao2_callback(providers, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
  352. }
  353. static void provider_destructor(void *obj)
  354. {
  355. struct phoneprov_provider *provider = obj;
  356. ast_string_field_free_memory(provider);
  357. }
  358. static void delete_file(struct phoneprov_file *file)
  359. {
  360. ast_string_field_free_memory(file);
  361. free(file);
  362. }
  363. /*! \brief Read a TEXT file into a string and return the length */
  364. static int load_file(const char *filename, char **ret)
  365. {
  366. int len = 0;
  367. FILE *f;
  368. if (!(f = fopen(filename, "r"))) {
  369. *ret = NULL;
  370. return -1;
  371. }
  372. fseek(f, 0, SEEK_END);
  373. len = ftell(f);
  374. fseek(f, 0, SEEK_SET);
  375. if (!(*ret = ast_malloc(len + 1)))
  376. return -2;
  377. if (len != fread(*ret, sizeof(char), len, f)) {
  378. free(*ret);
  379. *ret = NULL;
  380. return -3;
  381. }
  382. fclose(f);
  383. (*ret)[len] = '\0';
  384. return len;
  385. }
  386. /*! \brief Set all timezone-related variables based on a zone (i.e. America/New_York)
  387. \param headp pointer to list of user variables
  388. \param zone A time zone. NULL sets variables based on timezone of the machine
  389. */
  390. static void set_timezone_variables(struct varshead *headp, const char *zone)
  391. {
  392. time_t utc_time;
  393. int dstenable;
  394. time_t dststart;
  395. time_t dstend;
  396. struct ast_tm tm_info;
  397. int tzoffset;
  398. char buffer[21];
  399. struct timeval when;
  400. time(&utc_time);
  401. ast_get_dst_info(&utc_time, &dstenable, &dststart, &dstend, &tzoffset, zone);
  402. snprintf(buffer, sizeof(buffer), "%d", tzoffset);
  403. AST_VAR_LIST_INSERT_TAIL(headp, ast_var_assign("TZOFFSET", buffer));
  404. if (!dstenable) {
  405. return;
  406. }
  407. AST_VAR_LIST_INSERT_TAIL(headp, ast_var_assign("DST_ENABLE", "1"));
  408. when.tv_sec = dststart;
  409. ast_localtime(&when, &tm_info, zone);
  410. snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_mon+1);
  411. AST_VAR_LIST_INSERT_TAIL(headp, ast_var_assign("DST_START_MONTH", buffer));
  412. snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_mday);
  413. AST_VAR_LIST_INSERT_TAIL(headp, ast_var_assign("DST_START_MDAY", buffer));
  414. snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_hour);
  415. AST_VAR_LIST_INSERT_TAIL(headp, ast_var_assign("DST_START_HOUR", buffer));
  416. when.tv_sec = dstend;
  417. ast_localtime(&when, &tm_info, zone);
  418. snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_mon + 1);
  419. AST_VAR_LIST_INSERT_TAIL(headp, ast_var_assign("DST_END_MONTH", buffer));
  420. snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_mday);
  421. AST_VAR_LIST_INSERT_TAIL(headp, ast_var_assign("DST_END_MDAY", buffer));
  422. snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_hour);
  423. AST_VAR_LIST_INSERT_TAIL(headp, ast_var_assign("DST_END_HOUR", buffer));
  424. }
  425. static struct http_route *unref_route(struct http_route *route)
  426. {
  427. ao2_cleanup(route);
  428. return NULL;
  429. }
  430. static void route_destructor(void *obj)
  431. {
  432. struct http_route *route = obj;
  433. ast_string_field_free_memory(route);
  434. }
  435. /*! \brief Delete all http routes, freeing their memory */
  436. static void delete_routes(void)
  437. {
  438. if (!http_routes) {
  439. return;
  440. }
  441. ao2_callback(http_routes, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
  442. }
  443. /*! \brief Build a route structure and add it to the list of available http routes
  444. \param pp_file File to link to the route
  445. \param user User to link to the route (NULL means static route)
  446. \param uri URI of the route
  447. */
  448. static void build_route(struct phoneprov_file *pp_file, struct phone_profile *profile, struct user *user, char *uri)
  449. {
  450. struct http_route *route;
  451. if (!(route = ao2_alloc(sizeof(*route), route_destructor))) {
  452. return;
  453. }
  454. if (ast_string_field_init(route, 32)) {
  455. ast_log(LOG_ERROR, "Couldn't create string fields for %s\n", pp_file->format);
  456. route = unref_route(route);
  457. return;
  458. }
  459. ast_string_field_set(route, uri, S_OR(uri, pp_file->format));
  460. route->user = user;
  461. route->file = pp_file;
  462. route->profile = profile;
  463. ao2_link(http_routes, route);
  464. route = unref_route(route);
  465. }
  466. static struct phone_profile *unref_profile(struct phone_profile *prof)
  467. {
  468. ao2_cleanup(prof);
  469. return NULL;
  470. }
  471. /*! \brief Return a phone profile looked up by name */
  472. static struct phone_profile *find_profile(const char *name)
  473. {
  474. return ao2_find(profiles, name, OBJ_SEARCH_KEY);
  475. }
  476. static void profile_destructor(void *obj)
  477. {
  478. struct phone_profile *profile = obj;
  479. struct phoneprov_file *file;
  480. struct ast_var_t *var;
  481. while ((file = AST_LIST_REMOVE_HEAD(&profile->static_files, entry))) {
  482. delete_file(file);
  483. }
  484. while ((file = AST_LIST_REMOVE_HEAD(&profile->dynamic_files, entry))) {
  485. delete_file(file);
  486. }
  487. while ((var = AST_LIST_REMOVE_HEAD(profile->headp, entries))) {
  488. ast_var_delete(var);
  489. }
  490. ast_free(profile->headp);
  491. ast_string_field_free_memory(profile);
  492. }
  493. /*! \brief Delete all phone profiles, freeing their memory */
  494. static void delete_profiles(void)
  495. {
  496. if (!profiles) {
  497. return;
  498. }
  499. ao2_callback(profiles, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
  500. }
  501. /*! \brief Build a phone profile and add it to the list of phone profiles
  502. \param name the name of the profile
  503. \param v ast_variable from parsing phoneprov.conf
  504. */
  505. static void build_profile(const char *name, struct ast_variable *v)
  506. {
  507. struct phone_profile *profile;
  508. if (!(profile = ao2_alloc(sizeof(*profile), profile_destructor))) {
  509. return;
  510. }
  511. if (ast_string_field_init(profile, 32)) {
  512. profile = unref_profile(profile);
  513. return;
  514. }
  515. if (!(profile->headp = ast_var_list_create())) {
  516. profile = unref_profile(profile);
  517. return;
  518. }
  519. AST_LIST_HEAD_INIT_NOLOCK(&profile->static_files);
  520. AST_LIST_HEAD_INIT_NOLOCK(&profile->dynamic_files);
  521. ast_string_field_set(profile, name, name);
  522. for (; v; v = v->next) {
  523. if (!strcasecmp(v->name, "mime_type")) {
  524. ast_string_field_set(profile, default_mime_type, v->value);
  525. } else if (!strcasecmp(v->name, "setvar")) {
  526. char *value_copy = ast_strdupa(v->value);
  527. AST_DECLARE_APP_ARGS(args,
  528. AST_APP_ARG(varname);
  529. AST_APP_ARG(varval);
  530. );
  531. AST_NONSTANDARD_APP_ARGS(args, value_copy, '=');
  532. do {
  533. if (ast_strlen_zero(args.varname) || ast_strlen_zero(args.varval))
  534. break;
  535. args.varname = ast_strip(args.varname);
  536. args.varval = ast_strip(args.varval);
  537. if (ast_strlen_zero(args.varname) || ast_strlen_zero(args.varval))
  538. break;
  539. AST_VAR_LIST_INSERT_TAIL(profile->headp, ast_var_assign(args.varname, args.varval));
  540. } while (0);
  541. } else if (!strcasecmp(v->name, "staticdir")) {
  542. ast_string_field_set(profile, staticdir, v->value);
  543. } else {
  544. struct phoneprov_file *pp_file;
  545. char *file_extension;
  546. char *value_copy = ast_strdupa(v->value);
  547. AST_DECLARE_APP_ARGS(args,
  548. AST_APP_ARG(filename);
  549. AST_APP_ARG(mimetype);
  550. );
  551. if (!(pp_file = ast_calloc_with_stringfields(1, struct phoneprov_file, 32))) {
  552. profile = unref_profile(profile);
  553. return;
  554. }
  555. if ((file_extension = strrchr(pp_file->format, '.')))
  556. file_extension++;
  557. AST_STANDARD_APP_ARGS(args, value_copy);
  558. /* Mime type order of preference
  559. * 1) Specific mime-type defined for file in profile
  560. * 2) Mime determined by extension
  561. * 3) Default mime type specified in profile
  562. * 4) text/plain
  563. */
  564. ast_string_field_set(pp_file, mime_type, S_OR(args.mimetype,
  565. (S_OR(S_OR(ast_http_ftype2mtype(file_extension), profile->default_mime_type), "text/plain"))));
  566. if (!strcasecmp(v->name, "static_file")) {
  567. ast_string_field_set(pp_file, format, args.filename);
  568. ast_string_field_build(pp_file, template, "%s%s", profile->staticdir, args.filename);
  569. AST_LIST_INSERT_TAIL(&profile->static_files, pp_file, entry);
  570. /* Add a route for the static files, as their filenames won't change per-user */
  571. build_route(pp_file, profile, NULL, NULL);
  572. } else {
  573. ast_string_field_set(pp_file, format, v->name);
  574. ast_string_field_set(pp_file, template, args.filename);
  575. AST_LIST_INSERT_TAIL(&profile->dynamic_files, pp_file, entry);
  576. }
  577. }
  578. }
  579. ao2_link(profiles, profile);
  580. profile = unref_profile(profile);
  581. }
  582. static struct extension *delete_extension(struct extension *exten)
  583. {
  584. ast_var_list_destroy(exten->headp);
  585. ast_string_field_free_memory(exten);
  586. ast_free(exten);
  587. return NULL;
  588. }
  589. static struct extension *build_extension(const char *name, struct varshead *vars)
  590. {
  591. struct extension *exten;
  592. const char *tmp;
  593. if (!(exten = ast_calloc_with_stringfields(1, struct extension, 32))) {
  594. return NULL;
  595. }
  596. ast_string_field_set(exten, name, name);
  597. exten->headp = ast_var_list_clone(vars);
  598. if (!exten->headp) {
  599. ast_log(LOG_ERROR, "Unable to clone variables for extension '%s'\n", name);
  600. delete_extension(exten);
  601. return NULL;
  602. }
  603. tmp = ast_var_find(exten->headp, variable_lookup[AST_PHONEPROV_STD_LINENUMBER]);
  604. if (!tmp) {
  605. AST_VAR_LIST_INSERT_TAIL(exten->headp,
  606. ast_var_assign(variable_lookup[AST_PHONEPROV_STD_LINENUMBER], "1"));
  607. exten->index = 1;
  608. } else {
  609. sscanf(tmp, "%d", &exten->index);
  610. }
  611. if (!ast_var_find(exten->headp, variable_lookup[AST_PHONEPROV_STD_LINEKEYS])) {
  612. AST_VAR_LIST_INSERT_TAIL(exten->headp,
  613. ast_var_assign(variable_lookup[AST_PHONEPROV_STD_LINEKEYS], "1"));
  614. }
  615. set_timezone_variables(exten->headp,
  616. ast_var_find(vars, variable_lookup[AST_PHONEPROV_STD_TIMEZONE]));
  617. return exten;
  618. }
  619. static struct user *unref_user(struct user *user)
  620. {
  621. ao2_cleanup(user);
  622. return NULL;
  623. }
  624. /*! \brief Return a user looked up by name */
  625. static struct user *find_user(const char *macaddress)
  626. {
  627. return ao2_find(users, macaddress, OBJ_SEARCH_KEY);
  628. }
  629. static int routes_delete_cb(void *obj, void *arg, int flags)
  630. {
  631. struct http_route *route = obj;
  632. struct user *user = route->user;
  633. char *macaddress = arg;
  634. if (user && !strcmp(user->macaddress, macaddress)) {
  635. return CMP_MATCH;
  636. }
  637. return 0;
  638. }
  639. /*! \brief Free all memory associated with a user */
  640. static void user_destructor(void *obj)
  641. {
  642. struct user *user = obj;
  643. struct extension *exten;
  644. while ((exten = AST_LIST_REMOVE_HEAD(&user->extensions, entry))) {
  645. exten = delete_extension(exten);
  646. }
  647. if (user->profile) {
  648. user->profile = unref_profile(user->profile);
  649. }
  650. ao2_callback(http_routes, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, routes_delete_cb, (void *)user->macaddress);
  651. ast_string_field_free_memory(user);
  652. }
  653. /*! \brief Delete all users */
  654. static void delete_users(void)
  655. {
  656. if (!users) {
  657. return;
  658. }
  659. ao2_callback(users, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
  660. }
  661. /*! \brief Build and return a user structure based on gathered config data */
  662. static struct user *build_user(const char *mac, struct phone_profile *profile, char *provider_name)
  663. {
  664. struct user *user;
  665. if (!(user = ao2_alloc(sizeof(*user), user_destructor))) {
  666. return NULL;
  667. }
  668. if (ast_string_field_init(user, 64)) {
  669. user = unref_user(user);
  670. return NULL;
  671. }
  672. ast_string_field_set(user, macaddress, mac);
  673. ast_string_field_set(user, provider_name, provider_name);
  674. user->profile = profile;
  675. ao2_ref(profile, 1);
  676. return user;
  677. }
  678. /*! \brief Add an extension to a user ordered by index/linenumber */
  679. static int add_user_extension(struct user *user, struct extension *exten)
  680. {
  681. struct ast_var_t *pvar, *var2;
  682. struct ast_str *str = ast_str_create(16);
  683. if (!str) {
  684. return -1;
  685. }
  686. /* Append profile variables here, and substitute variables on profile
  687. * setvars, so that we can use user specific variables in them */
  688. AST_VAR_LIST_TRAVERSE(user->profile->headp, pvar) {
  689. if (ast_var_find(exten->headp, pvar->name)) {
  690. continue;
  691. }
  692. ast_str_substitute_variables_varshead(&str, 0, exten->headp, pvar->value);
  693. if ((var2 = ast_var_assign(pvar->name, ast_str_buffer(str)))) {
  694. AST_VAR_LIST_INSERT_TAIL(exten->headp, var2);
  695. }
  696. }
  697. ast_free(str);
  698. if (AST_LIST_EMPTY(&user->extensions)) {
  699. AST_LIST_INSERT_HEAD(&user->extensions, exten, entry);
  700. } else {
  701. struct extension *exten_iter;
  702. AST_LIST_TRAVERSE_SAFE_BEGIN(&user->extensions, exten_iter, entry) {
  703. if (exten->index < exten_iter->index) {
  704. AST_LIST_INSERT_BEFORE_CURRENT(exten, entry);
  705. } else if (exten->index == exten_iter->index) {
  706. ast_log(LOG_WARNING, "Duplicate linenumber=%d for %s\n", exten->index, user->macaddress);
  707. return -1;
  708. } else if (!AST_LIST_NEXT(exten_iter, entry)) {
  709. AST_LIST_INSERT_TAIL(&user->extensions, exten, entry);
  710. }
  711. }
  712. AST_LIST_TRAVERSE_SAFE_END;
  713. }
  714. return 0;
  715. }
  716. /*! \brief Add an http route for dynamic files attached to the profile of the user */
  717. static int build_user_routes(struct user *user)
  718. {
  719. struct phoneprov_file *pp_file;
  720. struct ast_str *str;
  721. if (!(str = ast_str_create(16))) {
  722. return -1;
  723. }
  724. AST_LIST_TRAVERSE(&user->profile->dynamic_files, pp_file, entry) {
  725. ast_str_substitute_variables_varshead(&str, 0, AST_LIST_FIRST(&user->extensions)->headp, pp_file->format);
  726. build_route(pp_file, user->profile, user, ast_str_buffer(str));
  727. }
  728. ast_free(str);
  729. return 0;
  730. }
  731. /*! \brief Callback that is executed everytime an http request is received by this module */
  732. 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)
  733. {
  734. struct http_route *route;
  735. struct ast_str *result;
  736. char path[PATH_MAX];
  737. char *file = NULL;
  738. char *server;
  739. int len;
  740. int fd;
  741. struct ast_str *http_header;
  742. if (method != AST_HTTP_GET && method != AST_HTTP_HEAD) {
  743. ast_http_error(ser, 501, "Not Implemented", "Attempt to use unimplemented / unsupported method");
  744. return 0;
  745. }
  746. if (!(route = ao2_find(http_routes, uri, OBJ_SEARCH_KEY))) {
  747. goto out404;
  748. }
  749. snprintf(path, sizeof(path), "%s/phoneprov/%s", ast_config_AST_DATA_DIR, route->file->template);
  750. if (!route->user) { /* Static file */
  751. fd = open(path, O_RDONLY);
  752. if (fd < 0) {
  753. goto out500;
  754. }
  755. len = lseek(fd, 0, SEEK_END);
  756. lseek(fd, 0, SEEK_SET);
  757. if (len < 0) {
  758. ast_log(LOG_WARNING, "Could not load file: %s (%d)\n", path, len);
  759. close(fd);
  760. goto out500;
  761. }
  762. http_header = ast_str_create(80);
  763. ast_str_set(&http_header, 0, "Content-type: %s\r\n",
  764. route->file->mime_type);
  765. ast_http_send(ser, method, 200, NULL, http_header, NULL, fd, 0);
  766. close(fd);
  767. route = unref_route(route);
  768. return 0;
  769. } else { /* Dynamic file */
  770. struct ast_str *tmp;
  771. len = load_file(path, &file);
  772. if (len < 0) {
  773. ast_log(LOG_WARNING, "Could not load file: %s (%d)\n", path, len);
  774. if (file) {
  775. ast_free(file);
  776. }
  777. goto out500;
  778. }
  779. if (!file) {
  780. goto out500;
  781. }
  782. if (!(tmp = ast_str_create(len))) {
  783. if (file) {
  784. ast_free(file);
  785. }
  786. goto out500;
  787. }
  788. /* Unless we are overridden by serveriface or serveraddr, we set the SERVER variable to
  789. * the IP address we are listening on that the phone contacted for this config file */
  790. server = ast_var_find(AST_LIST_FIRST(&route->user->extensions)->headp,
  791. variable_lookup[AST_PHONEPROV_STD_SERVER]);
  792. if (!server) {
  793. union {
  794. struct sockaddr sa;
  795. struct sockaddr_in sa_in;
  796. } name;
  797. socklen_t namelen = sizeof(name.sa);
  798. int res;
  799. if ((res = getsockname(ser->fd, &name.sa, &namelen))) {
  800. ast_log(LOG_WARNING, "Could not get server IP, breakage likely.\n");
  801. } else {
  802. struct extension *exten_iter;
  803. const char *newserver = ast_inet_ntoa(name.sa_in.sin_addr);
  804. AST_LIST_TRAVERSE(&route->user->extensions, exten_iter, entry) {
  805. AST_VAR_LIST_INSERT_TAIL(exten_iter->headp,
  806. ast_var_assign(variable_lookup[AST_PHONEPROV_STD_SERVER], newserver));
  807. }
  808. }
  809. }
  810. ast_str_substitute_variables_varshead(&tmp, 0, AST_LIST_FIRST(&route->user->extensions)->headp, file);
  811. ast_free(file);
  812. http_header = ast_str_create(80);
  813. ast_str_set(&http_header, 0, "Content-type: %s\r\n",
  814. route->file->mime_type);
  815. if (!(result = ast_str_create(512))) {
  816. ast_log(LOG_ERROR, "Could not create result string!\n");
  817. if (tmp) {
  818. ast_free(tmp);
  819. }
  820. ast_free(http_header);
  821. goto out500;
  822. }
  823. ast_str_append(&result, 0, "%s", ast_str_buffer(tmp));
  824. ast_http_send(ser, method, 200, NULL, http_header, result, 0, 0);
  825. ast_free(tmp);
  826. route = unref_route(route);
  827. return 0;
  828. }
  829. out404:
  830. ast_http_error(ser, 404, "Not Found", uri);
  831. return 0;
  832. out500:
  833. route = unref_route(route);
  834. ast_http_error(ser, 500, "Internal Error", "An internal error has occured.");
  835. return 0;
  836. }
  837. /*! \brief A dialplan function that can be used to print a string for each phoneprov user */
  838. static int pp_each_user_helper(struct ast_channel *chan, char *data, char *buf, struct ast_str **bufstr, int len)
  839. {
  840. char *tmp;
  841. struct ao2_iterator i;
  842. struct user *user;
  843. struct ast_str *str;
  844. AST_DECLARE_APP_ARGS(args,
  845. AST_APP_ARG(string);
  846. AST_APP_ARG(exclude_mac);
  847. );
  848. AST_STANDARD_APP_ARGS(args, data);
  849. if (!(str = ast_str_create(16))) {
  850. return -1;
  851. }
  852. /* Fix data by turning %{ into ${ */
  853. while ((tmp = strstr(args.string, "%{")))
  854. *tmp = '$';
  855. i = ao2_iterator_init(users, 0);
  856. while ((user = ao2_iterator_next(&i))) {
  857. if (!ast_strlen_zero(args.exclude_mac) && !strcasecmp(user->macaddress, args.exclude_mac)) {
  858. continue;
  859. }
  860. ast_str_substitute_variables_varshead(&str, len, AST_LIST_FIRST(&user->extensions)->headp, args.string);
  861. if (buf) {
  862. size_t slen = len;
  863. ast_build_string(&buf, &slen, "%s", ast_str_buffer(str));
  864. } else {
  865. ast_str_append(bufstr, len, "%s", ast_str_buffer(str));
  866. }
  867. user = unref_user(user);
  868. }
  869. ao2_iterator_destroy(&i);
  870. ast_free(str);
  871. return 0;
  872. }
  873. static int pp_each_user_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  874. {
  875. return pp_each_user_helper(chan, data, buf, NULL, len);
  876. }
  877. static int pp_each_user_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
  878. {
  879. return pp_each_user_helper(chan, data, NULL, buf, len);
  880. }
  881. static struct ast_custom_function pp_each_user_function = {
  882. .name = "PP_EACH_USER",
  883. .read = pp_each_user_read,
  884. .read2 = pp_each_user_read2,
  885. };
  886. /*! \brief A dialplan function that can be used to output a template for each extension attached to a user */
  887. static int pp_each_extension_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, struct ast_str **bufstr, int len)
  888. {
  889. struct user *user;
  890. struct extension *exten;
  891. char path[PATH_MAX];
  892. char *file;
  893. int filelen;
  894. struct ast_str *str;
  895. AST_DECLARE_APP_ARGS(args,
  896. AST_APP_ARG(mac);
  897. AST_APP_ARG(template);
  898. );
  899. AST_STANDARD_APP_ARGS(args, data);
  900. if (ast_strlen_zero(args.mac) || ast_strlen_zero(args.template)) {
  901. ast_log(LOG_WARNING, "PP_EACH_EXTENSION requries both a macaddress and template filename.\n");
  902. return 0;
  903. }
  904. if (!(user = find_user(args.mac))) {
  905. ast_log(LOG_WARNING, "Could not find user with mac = '%s'\n", args.mac);
  906. return 0;
  907. }
  908. snprintf(path, sizeof(path), "%s/phoneprov/%s", ast_config_AST_DATA_DIR, args.template);
  909. filelen = load_file(path, &file);
  910. if (filelen < 0) {
  911. ast_log(LOG_WARNING, "Could not load file: %s (%d)\n", path, filelen);
  912. if (file) {
  913. ast_free(file);
  914. }
  915. return 0;
  916. }
  917. if (!file) {
  918. return 0;
  919. }
  920. if (!(str = ast_str_create(filelen))) {
  921. return 0;
  922. }
  923. AST_LIST_TRAVERSE(&user->extensions, exten, entry) {
  924. ast_str_substitute_variables_varshead(&str, 0, exten->headp, file);
  925. if (buf) {
  926. size_t slen = len;
  927. ast_build_string(&buf, &slen, "%s", ast_str_buffer(str));
  928. } else {
  929. ast_str_append(bufstr, len, "%s", ast_str_buffer(str));
  930. }
  931. }
  932. ast_free(file);
  933. ast_free(str);
  934. user = unref_user(user);
  935. return 0;
  936. }
  937. static int pp_each_extension_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  938. {
  939. return pp_each_extension_helper(chan, cmd, data, buf, NULL, len);
  940. }
  941. static int pp_each_extension_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
  942. {
  943. return pp_each_extension_helper(chan, cmd, data, NULL, buf, len);
  944. }
  945. static struct ast_custom_function pp_each_extension_function = {
  946. .name = "PP_EACH_EXTENSION",
  947. .read = pp_each_extension_read,
  948. .read2 = pp_each_extension_read2,
  949. };
  950. #define FORMATS "%-20.20s %-40.40s %-30.30s\n"
  951. #define FORMATD "%-20.20s %-20.20s %-40.40s %-30.30s\n"
  952. static int route_list_cb(void *obj, void *arg, void *data, int flags)
  953. {
  954. int fd = *(int *)arg;
  955. struct http_route *route = obj;
  956. if (data && route->user) {
  957. ast_cli(fd, FORMATD, route->user->provider_name, route->profile->name, route->uri, route->file->template);
  958. }
  959. if (!data && !route->user) {
  960. ast_cli(fd, FORMATS, route->profile->name, route->uri, route->file->template);
  961. }
  962. return CMP_MATCH;
  963. }
  964. /*! \brief CLI command to list static and dynamic routes */
  965. static char *handle_show_routes(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  966. {
  967. int fd = a->fd;
  968. switch(cmd) {
  969. case CLI_INIT:
  970. e->command = "phoneprov show routes";
  971. e->usage =
  972. "Usage: phoneprov show routes\n"
  973. " Lists all registered phoneprov http routes.\n";
  974. return NULL;
  975. case CLI_GENERATE:
  976. return NULL;
  977. }
  978. /* This currently iterates over routes twice, but it is the only place I've needed
  979. * to really separate static an dynamic routes, so I've just left it this way. */
  980. ast_cli(a->fd, "Static routes\n\n");
  981. ast_cli(a->fd, FORMATS, "Profile", "Relative URI", "Physical location");
  982. ao2_callback_data(http_routes, OBJ_NODATA | OBJ_MULTIPLE, route_list_cb, &fd, NULL);
  983. ast_cli(a->fd, "\nDynamic routes\n\n");
  984. ast_cli(a->fd, FORMATD, "Provider", "Profile", "Relative URI", "Template");
  985. ao2_callback_data(http_routes, OBJ_NODATA | OBJ_MULTIPLE, route_list_cb, &fd, (void *)1);
  986. return CLI_SUCCESS;
  987. }
  988. static struct ast_cli_entry pp_cli[] = {
  989. AST_CLI_DEFINE(handle_show_routes, "Show registered phoneprov http routes"),
  990. };
  991. static struct ast_http_uri phoneprovuri = {
  992. .callback = phoneprov_callback,
  993. .description = "Asterisk HTTP Phone Provisioning Tool",
  994. .uri = "phoneprov",
  995. .has_subtree = 1,
  996. .data = NULL,
  997. .key = __FILE__,
  998. };
  999. static struct varshead *get_defaults(void)
  1000. {
  1001. struct ast_config *phoneprov_cfg;
  1002. struct ast_config *cfg;
  1003. const char *value;
  1004. struct ast_variable *v;
  1005. struct ast_var_t *var;
  1006. struct ast_flags config_flags = { 0 };
  1007. struct varshead *defaults = ast_var_list_create();
  1008. if (!defaults) {
  1009. ast_log(LOG_ERROR, "Unable to create default var list.\n");
  1010. return NULL;
  1011. }
  1012. if (!(phoneprov_cfg = ast_config_load("phoneprov.conf", config_flags))
  1013. || phoneprov_cfg == CONFIG_STATUS_FILEINVALID) {
  1014. ast_log(LOG_ERROR, "Unable to load config phoneprov.conf\n");
  1015. ast_var_list_destroy(defaults);
  1016. return NULL;
  1017. }
  1018. value = ast_variable_retrieve(phoneprov_cfg, "general", pp_general_lookup[AST_PHONEPROV_STD_SERVER]);
  1019. if (!value) {
  1020. struct in_addr addr;
  1021. value = ast_variable_retrieve(phoneprov_cfg, "general", pp_general_lookup[AST_PHONEPROV_STD_SERVER_IFACE]);
  1022. if (value) {
  1023. lookup_iface(value, &addr);
  1024. value = ast_inet_ntoa(addr);
  1025. }
  1026. }
  1027. if (value) {
  1028. var = ast_var_assign(variable_lookup[AST_PHONEPROV_STD_SERVER], value);
  1029. AST_VAR_LIST_INSERT_TAIL(defaults, var);
  1030. } else {
  1031. ast_log(LOG_WARNING, "Unable to find a valid server address or name.\n");
  1032. }
  1033. value = ast_variable_retrieve(phoneprov_cfg, "general", pp_general_lookup[AST_PHONEPROV_STD_SERVER_PORT]);
  1034. if (!value) {
  1035. if ((cfg = ast_config_load("sip.conf", config_flags)) && cfg != CONFIG_STATUS_FILEINVALID) {
  1036. value = ast_variable_retrieve(cfg, "general", "bindport");
  1037. ast_config_destroy(cfg);
  1038. }
  1039. }
  1040. var = ast_var_assign(variable_lookup[AST_PHONEPROV_STD_SERVER_PORT], S_OR(value, "5060"));
  1041. AST_VAR_LIST_INSERT_TAIL(defaults, var);
  1042. value = ast_variable_retrieve(phoneprov_cfg, "general", pp_general_lookup[AST_PHONEPROV_STD_PROFILE]);
  1043. if (!value) {
  1044. ast_log(LOG_ERROR, "Unable to load default profile.\n");
  1045. ast_config_destroy(phoneprov_cfg);
  1046. ast_var_list_destroy(defaults);
  1047. return NULL;
  1048. }
  1049. var = ast_var_assign(variable_lookup[AST_PHONEPROV_STD_PROFILE], value);
  1050. AST_VAR_LIST_INSERT_TAIL(defaults, var);
  1051. ast_config_destroy(phoneprov_cfg);
  1052. if (!(cfg = ast_config_load("users.conf", config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
  1053. ast_log(LOG_ERROR, "Unable to load users.conf\n");
  1054. ast_var_list_destroy(defaults);
  1055. return NULL;
  1056. }
  1057. /* Go ahead and load global variables from users.conf so we can append to profiles */
  1058. for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
  1059. if (!strcasecmp(v->name, pp_user_lookup[AST_PHONEPROV_STD_VOICEMAIL_EXTEN])) {
  1060. var = ast_var_assign(variable_lookup[AST_PHONEPROV_STD_VOICEMAIL_EXTEN], v->value);
  1061. AST_VAR_LIST_INSERT_TAIL(defaults, var);
  1062. }
  1063. if (!strcasecmp(v->name, pp_user_lookup[AST_PHONEPROV_STD_EXTENSION_LENGTH])) {
  1064. var = ast_var_assign(variable_lookup[AST_PHONEPROV_STD_EXTENSION_LENGTH], v->value);
  1065. AST_VAR_LIST_INSERT_TAIL(defaults, var);
  1066. }
  1067. }
  1068. ast_config_destroy(cfg);
  1069. return defaults;
  1070. }
  1071. static int load_users(void)
  1072. {
  1073. struct ast_config *cfg;
  1074. char *cat;
  1075. const char *value;
  1076. struct ast_flags config_flags = { 0 };
  1077. struct varshead *defaults = get_defaults();
  1078. if (!defaults) {
  1079. ast_log(LOG_WARNING, "Unable to load default variables.\n");
  1080. return -1;
  1081. }
  1082. if (!(cfg = ast_config_load("users.conf", config_flags))
  1083. || cfg == CONFIG_STATUS_FILEINVALID) {
  1084. ast_log(LOG_WARNING, "Unable to load users.conf\n");
  1085. return -1;
  1086. }
  1087. cat = NULL;
  1088. while ((cat = ast_category_browse(cfg, cat))) {
  1089. const char *tmp;
  1090. int i;
  1091. struct ast_var_t *varx;
  1092. struct ast_var_t *vard;
  1093. if (strcasecmp(cat, "general") && strcasecmp(cat, "authentication")) {
  1094. struct varshead *variables = ast_var_list_create();
  1095. if (!((tmp = ast_variable_retrieve(cfg, cat, "autoprov")) && ast_true(tmp))) {
  1096. ast_var_list_destroy(variables);
  1097. continue;
  1098. }
  1099. /* Transfer the standard variables */
  1100. for (i = 0; i < AST_PHONEPROV_STD_VAR_LIST_LENGTH; i++) {
  1101. if (pp_user_lookup[i]) {
  1102. value = ast_variable_retrieve(cfg, cat, pp_user_lookup[i]);
  1103. if (value) {
  1104. varx = ast_var_assign(variable_lookup[i],
  1105. value);
  1106. AST_VAR_LIST_INSERT_TAIL(variables, varx);
  1107. }
  1108. }
  1109. }
  1110. if (!ast_var_find(variables, variable_lookup[AST_PHONEPROV_STD_MAC])) {
  1111. ast_log(LOG_WARNING, "autoprov set for %s, but no mac address - skipping.\n", cat);
  1112. ast_var_list_destroy(variables);
  1113. continue;
  1114. }
  1115. /* Apply defaults */
  1116. AST_VAR_LIST_TRAVERSE(defaults, vard) {
  1117. if (ast_var_find(variables, vard->name)) {
  1118. continue;
  1119. }
  1120. varx = ast_var_assign(vard->name, vard->value);
  1121. AST_VAR_LIST_INSERT_TAIL(variables, varx);
  1122. }
  1123. ast_phoneprov_add_extension(SIPUSERS_PROVIDER_NAME, variables);
  1124. }
  1125. }
  1126. ast_config_destroy(cfg);
  1127. return 0;
  1128. }
  1129. static int load_common(void)
  1130. {
  1131. struct ast_config *phoneprov_cfg;
  1132. struct ast_flags config_flags = { 0 };
  1133. char *cat;
  1134. if (!(phoneprov_cfg = ast_config_load("phoneprov.conf", config_flags))
  1135. || phoneprov_cfg == CONFIG_STATUS_FILEINVALID) {
  1136. ast_log(LOG_ERROR, "Unable to load config phoneprov.conf\n");
  1137. return -1;
  1138. }
  1139. cat = NULL;
  1140. while ((cat = ast_category_browse(phoneprov_cfg, cat))) {
  1141. if (!strcasecmp(cat, "general")) {
  1142. continue;
  1143. }
  1144. build_profile(cat, ast_variable_browse(phoneprov_cfg, cat));
  1145. }
  1146. ast_config_destroy(phoneprov_cfg);
  1147. if (!ao2_container_count(profiles)) {
  1148. ast_log(LOG_ERROR, "There are no provisioning profiles in phoneprov.conf.\n");
  1149. return -1;
  1150. }
  1151. return 0;
  1152. }
  1153. static int unload_module(void)
  1154. {
  1155. ast_http_uri_unlink(&phoneprovuri);
  1156. ast_custom_function_unregister(&pp_each_user_function);
  1157. ast_custom_function_unregister(&pp_each_extension_function);
  1158. ast_cli_unregister_multiple(pp_cli, ARRAY_LEN(pp_cli));
  1159. /* This cleans up the sip.conf/users.conf provider (called specifically for clarity) */
  1160. ast_phoneprov_provider_unregister(SIPUSERS_PROVIDER_NAME);
  1161. /* This cleans up the framework which also cleans up the providers. */
  1162. delete_profiles();
  1163. ao2_cleanup(profiles);
  1164. profiles = NULL;
  1165. delete_routes();
  1166. ao2_cleanup(http_routes);
  1167. http_routes = NULL;
  1168. delete_users();
  1169. ao2_cleanup(users);
  1170. users = NULL;
  1171. delete_providers();
  1172. ao2_cleanup(providers);
  1173. providers = NULL;
  1174. return 0;
  1175. }
  1176. /*!
  1177. * \brief Load the module
  1178. *
  1179. * Module loading including tests for configuration or dependencies.
  1180. * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
  1181. * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
  1182. * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
  1183. * configuration file or other non-critical problem return
  1184. * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
  1185. */
  1186. static int load_module(void)
  1187. {
  1188. profiles = ao2_container_alloc(MAX_PROFILE_BUCKETS, phone_profile_hash_fn, phone_profile_cmp_fn);
  1189. if (!profiles) {
  1190. ast_log(LOG_ERROR, "Unable to allocate profiles container.\n");
  1191. return AST_MODULE_LOAD_DECLINE;
  1192. }
  1193. http_routes = ao2_container_alloc(MAX_ROUTE_BUCKETS, http_route_hash_fn, http_route_cmp_fn);
  1194. if (!http_routes) {
  1195. ast_log(LOG_ERROR, "Unable to allocate routes container.\n");
  1196. goto error;
  1197. }
  1198. if (load_common()) {
  1199. ast_log(LOG_ERROR, "Unable to load provisioning profiles.\n");
  1200. goto error;
  1201. }
  1202. users = ao2_container_alloc(MAX_USER_BUCKETS, user_hash_fn, user_cmp_fn);
  1203. if (!users) {
  1204. ast_log(LOG_ERROR, "Unable to allocate users container.\n");
  1205. goto error;
  1206. }
  1207. providers = ao2_container_alloc(MAX_PROVIDER_BUCKETS, phoneprov_provider_hash_fn, phoneprov_provider_cmp_fn);
  1208. if (!providers) {
  1209. ast_log(LOG_ERROR, "Unable to allocate providers container.\n");
  1210. goto error;
  1211. }
  1212. /* Register ourselves as the provider for sip.conf/users.conf */
  1213. if (ast_phoneprov_provider_register(SIPUSERS_PROVIDER_NAME, load_users)) {
  1214. ast_log(LOG_WARNING, "Unable register sip/users config provider. Others may succeed.\n");
  1215. }
  1216. ast_http_uri_link(&phoneprovuri);
  1217. ast_custom_function_register(&pp_each_user_function);
  1218. ast_custom_function_register(&pp_each_extension_function);
  1219. ast_cli_register_multiple(pp_cli, ARRAY_LEN(pp_cli));
  1220. return AST_MODULE_LOAD_SUCCESS;
  1221. error:
  1222. unload_module();
  1223. return AST_MODULE_LOAD_DECLINE;
  1224. }
  1225. static int reload(void)
  1226. {
  1227. struct ao2_iterator i;
  1228. struct phoneprov_provider *provider;
  1229. /* Clean everything except the providers */
  1230. delete_routes();
  1231. delete_users();
  1232. delete_profiles();
  1233. /* Reload the profiles */
  1234. if (load_common()) {
  1235. ast_log(LOG_ERROR, "Unable to reload provisioning profiles.\n");
  1236. unload_module();
  1237. return AST_MODULE_LOAD_DECLINE;
  1238. }
  1239. /* For each provider, reload the users */
  1240. ao2_lock(providers);
  1241. i = ao2_iterator_init(providers, 0);
  1242. for(; (provider = ao2_iterator_next(&i)); ao2_ref(provider, -1)) {
  1243. ast_log(LOG_VERBOSE, "Reloading provider '%s' users.\n", provider->provider_name);
  1244. if (provider->load_users()) {
  1245. ast_log(LOG_ERROR, "Unable to load provider '%s' users. Reload aborted.\n", provider->provider_name);
  1246. continue;
  1247. }
  1248. }
  1249. ao2_iterator_destroy(&i);
  1250. ao2_unlock(providers);
  1251. return AST_MODULE_LOAD_SUCCESS;
  1252. }
  1253. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",
  1254. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  1255. .load = load_module,
  1256. .unload = unload_module,
  1257. .reload = reload,
  1258. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  1259. );
  1260. /**** Public API for register/unregister, set defaults, and add extension. ****/
  1261. const char *ast_phoneprov_std_variable_lookup(enum ast_phoneprov_std_variables var)
  1262. {
  1263. if (var >= AST_PHONEPROV_STD_VAR_LIST_LENGTH) {
  1264. return NULL;
  1265. }
  1266. return variable_lookup[var];
  1267. }
  1268. int ast_phoneprov_provider_register(char *provider_name,
  1269. ast_phoneprov_load_users_cb load_users)
  1270. {
  1271. struct phoneprov_provider *provider;
  1272. if (ast_strlen_zero(provider_name)) {
  1273. ast_log(LOG_ERROR, "Provider name can't be empty.\n");
  1274. return -1;
  1275. }
  1276. if (!providers) {
  1277. ast_log(LOG_WARNING, "Provider '%s' cannot be registered: res_phoneprov not loaded.\n", provider_name);
  1278. return -1;
  1279. }
  1280. provider = find_provider(provider_name);
  1281. if (provider) {
  1282. ast_log(LOG_ERROR, "There is already a provider registered named '%s'.\n", provider_name);
  1283. ao2_ref(provider, -1);
  1284. return -1;
  1285. }
  1286. provider = ao2_alloc(sizeof(struct phoneprov_provider), provider_destructor);
  1287. if (!provider) {
  1288. ast_log(LOG_ERROR, "Unable to allocate sufficient memory for provider '%s'.\n", provider_name);
  1289. return -1;
  1290. }
  1291. if (ast_string_field_init(provider, 32)) {
  1292. ao2_ref(provider, -1);
  1293. ast_log(LOG_ERROR, "Unable to allocate sufficient memory for provider '%s' stringfields.\n", provider_name);
  1294. return -1;
  1295. }
  1296. ast_string_field_set(provider, provider_name, provider_name);
  1297. provider->load_users = load_users;
  1298. ao2_link(providers, provider);
  1299. ao2_ref(provider, -1);
  1300. if (provider->load_users()) {
  1301. ast_log(LOG_ERROR, "Unable to load provider '%s' users. Register aborted.\n", provider_name);
  1302. ast_phoneprov_provider_unregister(provider_name);
  1303. return -1;
  1304. }
  1305. ast_log(LOG_VERBOSE, "Registered phoneprov provider '%s'.\n", provider_name);
  1306. return 0;
  1307. }
  1308. static int extensions_delete_cb(void *obj, void *arg, int flags)
  1309. {
  1310. char *provider_name = arg;
  1311. struct user *user = obj;
  1312. if (strcmp(user->provider_name, provider_name)) {
  1313. return 0;
  1314. }
  1315. return CMP_MATCH;
  1316. }
  1317. static int extension_delete_cb(void *obj, void *arg, void *data, int flags)
  1318. {
  1319. struct user *user = obj;
  1320. char *provider_name = data;
  1321. char *macaddress = arg;
  1322. if (!strcmp(user->provider_name, provider_name) && !strcasecmp(user->macaddress, macaddress)) {
  1323. return CMP_MATCH;
  1324. }
  1325. return 0;
  1326. }
  1327. void ast_phoneprov_delete_extension(char *provider_name, char *macaddress)
  1328. {
  1329. ao2_callback_data(users, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE | OBJ_SEARCH_KEY,
  1330. extension_delete_cb, macaddress, provider_name);
  1331. }
  1332. void ast_phoneprov_delete_extensions(char *provider_name)
  1333. {
  1334. ao2_callback(users, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, extensions_delete_cb, provider_name);
  1335. }
  1336. void ast_phoneprov_provider_unregister(char *provider_name)
  1337. {
  1338. if (!providers) {
  1339. return;
  1340. }
  1341. ast_phoneprov_delete_extensions(provider_name);
  1342. ao2_find(providers, provider_name, OBJ_SEARCH_KEY | OBJ_NODATA | OBJ_UNLINK);
  1343. ast_log(LOG_VERBOSE, "Unegistered phoneprov provider '%s'.\n", provider_name);
  1344. }
  1345. int ast_phoneprov_add_extension(char *provider_name, struct varshead *vars)
  1346. {
  1347. RAII_VAR(struct phoneprov_provider *, provider, NULL, ao2_cleanup);
  1348. RAII_VAR(struct user *, user, NULL, ao2_cleanup);
  1349. RAII_VAR(struct phone_profile *, profile, NULL, ao2_cleanup);
  1350. struct extension *exten;
  1351. char *profile_name;
  1352. char *mac;
  1353. char *username;
  1354. if (ast_strlen_zero(provider_name)) {
  1355. ast_log(LOG_ERROR, "Provider name can't be empty.\n");
  1356. return -1;
  1357. }
  1358. if (!vars) {
  1359. ast_log(LOG_ERROR, "Variable list can't be empty.\n");
  1360. return -1;
  1361. }
  1362. username = ast_var_find(vars, variable_lookup[AST_PHONEPROV_STD_USERNAME]);
  1363. if (!username) {
  1364. ast_log(LOG_ERROR, "Extension name can't be empty.\n");
  1365. return -1;
  1366. }
  1367. mac = ast_var_find(vars, variable_lookup[AST_PHONEPROV_STD_MAC]);
  1368. if (!mac) {
  1369. ast_log(LOG_ERROR, "MAC Address can't be empty.\n");
  1370. return -1;
  1371. }
  1372. provider = find_provider(provider_name);
  1373. if (!provider) {
  1374. ast_log(LOG_ERROR, "Provider '%s' wasn't found in the registry.\n", provider_name);
  1375. return -1;
  1376. }
  1377. profile_name = ast_var_find(vars,
  1378. variable_lookup[AST_PHONEPROV_STD_PROFILE]);
  1379. if (!profile_name) {
  1380. ast_log(LOG_ERROR, "No profile could be found for user '%s' - skipping.\n", username);
  1381. return -1;
  1382. }
  1383. if (!(profile = find_profile(profile_name))) {
  1384. ast_log(LOG_ERROR, "Could not look up profile '%s' - skipping.\n", profile_name);
  1385. return -1;
  1386. }
  1387. if (!(user = find_user(mac))) {
  1388. if (!(user = build_user(mac, profile, provider_name))) {
  1389. ast_log(LOG_ERROR, "Could not create user for '%s' - skipping\n", mac);
  1390. return -1;
  1391. }
  1392. if (!(exten = build_extension(username, vars))) {
  1393. ast_log(LOG_ERROR, "Could not create extension for '%s' - skipping\n", user->macaddress);
  1394. return -1;
  1395. }
  1396. if (add_user_extension(user, exten)) {
  1397. ast_log(LOG_WARNING, "Could not add extension '%s' to user '%s'\n", exten->name, user->macaddress);
  1398. exten = delete_extension(exten);
  1399. return -1;
  1400. }
  1401. if (build_user_routes(user)) {
  1402. ast_log(LOG_WARNING, "Could not create http routes for '%s' - skipping\n", user->macaddress);
  1403. return -1;
  1404. }
  1405. ast_log(LOG_VERBOSE, "Created %s/%s for provider '%s'.\n", username, mac, provider_name);
  1406. ao2_link(users, user);
  1407. } else {
  1408. if (strcmp(provider_name, user->provider_name)) {
  1409. ast_log(LOG_ERROR, "MAC address '%s' was already added by provider '%s' - skipping\n", user->macaddress, user->provider_name);
  1410. return -1;
  1411. }
  1412. if (!(exten = build_extension(username, vars))) {
  1413. ast_log(LOG_ERROR, "Could not create extension for '%s' - skipping\n", user->macaddress);
  1414. return -1;
  1415. }
  1416. if (add_user_extension(user, exten)) {
  1417. ast_log(LOG_WARNING, "Could not add extension '%s' to user '%s'\n", exten->name, user->macaddress);
  1418. exten = delete_extension(exten);
  1419. return -1;
  1420. }
  1421. ast_log(LOG_VERBOSE, "Added %s/%s for provider '%s'.\n", username, mac, provider_name);
  1422. }
  1423. return 0;
  1424. }