res_phoneprov.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710
  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. fclose(f);
  377. return -2;
  378. }
  379. if (len != fread(*ret, sizeof(char), len, f)) {
  380. fclose(f);
  381. free(*ret);
  382. *ret = NULL;
  383. return -3;
  384. }
  385. fclose(f);
  386. (*ret)[len] = '\0';
  387. return len;
  388. }
  389. /*! \brief Set all timezone-related variables based on a zone (i.e. America/New_York)
  390. \param headp pointer to list of user variables
  391. \param zone A time zone. NULL sets variables based on timezone of the machine
  392. */
  393. static void set_timezone_variables(struct varshead *headp, const char *zone)
  394. {
  395. time_t utc_time;
  396. int dstenable;
  397. time_t dststart;
  398. time_t dstend;
  399. struct ast_tm tm_info;
  400. int tzoffset;
  401. char buffer[21];
  402. struct timeval when;
  403. time(&utc_time);
  404. ast_get_dst_info(&utc_time, &dstenable, &dststart, &dstend, &tzoffset, zone);
  405. snprintf(buffer, sizeof(buffer), "%d", tzoffset);
  406. AST_VAR_LIST_INSERT_TAIL(headp, ast_var_assign("TZOFFSET", buffer));
  407. if (!dstenable) {
  408. return;
  409. }
  410. AST_VAR_LIST_INSERT_TAIL(headp, ast_var_assign("DST_ENABLE", "1"));
  411. when.tv_sec = dststart;
  412. ast_localtime(&when, &tm_info, zone);
  413. snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_mon+1);
  414. AST_VAR_LIST_INSERT_TAIL(headp, ast_var_assign("DST_START_MONTH", buffer));
  415. snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_mday);
  416. AST_VAR_LIST_INSERT_TAIL(headp, ast_var_assign("DST_START_MDAY", buffer));
  417. snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_hour);
  418. AST_VAR_LIST_INSERT_TAIL(headp, ast_var_assign("DST_START_HOUR", buffer));
  419. when.tv_sec = dstend;
  420. ast_localtime(&when, &tm_info, zone);
  421. snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_mon + 1);
  422. AST_VAR_LIST_INSERT_TAIL(headp, ast_var_assign("DST_END_MONTH", buffer));
  423. snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_mday);
  424. AST_VAR_LIST_INSERT_TAIL(headp, ast_var_assign("DST_END_MDAY", buffer));
  425. snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_hour);
  426. AST_VAR_LIST_INSERT_TAIL(headp, ast_var_assign("DST_END_HOUR", buffer));
  427. }
  428. static struct http_route *unref_route(struct http_route *route)
  429. {
  430. ao2_cleanup(route);
  431. return NULL;
  432. }
  433. static void route_destructor(void *obj)
  434. {
  435. struct http_route *route = obj;
  436. ast_string_field_free_memory(route);
  437. }
  438. /*! \brief Delete all http routes, freeing their memory */
  439. static void delete_routes(void)
  440. {
  441. if (!http_routes) {
  442. return;
  443. }
  444. ao2_callback(http_routes, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
  445. }
  446. /*! \brief Build a route structure and add it to the list of available http routes
  447. \param pp_file File to link to the route
  448. \param user User to link to the route (NULL means static route)
  449. \param uri URI of the route
  450. */
  451. static void build_route(struct phoneprov_file *pp_file, struct phone_profile *profile, struct user *user, char *uri)
  452. {
  453. struct http_route *route;
  454. if (!(route = ao2_alloc(sizeof(*route), route_destructor))) {
  455. return;
  456. }
  457. if (ast_string_field_init(route, 32)) {
  458. ast_log(LOG_ERROR, "Couldn't create string fields for %s\n", pp_file->format);
  459. route = unref_route(route);
  460. return;
  461. }
  462. ast_string_field_set(route, uri, S_OR(uri, pp_file->format));
  463. route->user = user;
  464. route->file = pp_file;
  465. route->profile = profile;
  466. ao2_link(http_routes, route);
  467. route = unref_route(route);
  468. }
  469. static struct phone_profile *unref_profile(struct phone_profile *prof)
  470. {
  471. ao2_cleanup(prof);
  472. return NULL;
  473. }
  474. /*! \brief Return a phone profile looked up by name */
  475. static struct phone_profile *find_profile(const char *name)
  476. {
  477. return ao2_find(profiles, name, OBJ_SEARCH_KEY);
  478. }
  479. static void profile_destructor(void *obj)
  480. {
  481. struct phone_profile *profile = obj;
  482. struct phoneprov_file *file;
  483. struct ast_var_t *var;
  484. while ((file = AST_LIST_REMOVE_HEAD(&profile->static_files, entry))) {
  485. delete_file(file);
  486. }
  487. while ((file = AST_LIST_REMOVE_HEAD(&profile->dynamic_files, entry))) {
  488. delete_file(file);
  489. }
  490. while ((var = AST_LIST_REMOVE_HEAD(profile->headp, entries))) {
  491. ast_var_delete(var);
  492. }
  493. ast_free(profile->headp);
  494. ast_string_field_free_memory(profile);
  495. }
  496. /*! \brief Delete all phone profiles, freeing their memory */
  497. static void delete_profiles(void)
  498. {
  499. if (!profiles) {
  500. return;
  501. }
  502. ao2_callback(profiles, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
  503. }
  504. /*! \brief Build a phone profile and add it to the list of phone profiles
  505. \param name the name of the profile
  506. \param v ast_variable from parsing phoneprov.conf
  507. */
  508. static void build_profile(const char *name, struct ast_variable *v)
  509. {
  510. struct phone_profile *profile;
  511. if (!(profile = ao2_alloc(sizeof(*profile), profile_destructor))) {
  512. return;
  513. }
  514. if (ast_string_field_init(profile, 32)) {
  515. profile = unref_profile(profile);
  516. return;
  517. }
  518. if (!(profile->headp = ast_var_list_create())) {
  519. profile = unref_profile(profile);
  520. return;
  521. }
  522. AST_LIST_HEAD_INIT_NOLOCK(&profile->static_files);
  523. AST_LIST_HEAD_INIT_NOLOCK(&profile->dynamic_files);
  524. ast_string_field_set(profile, name, name);
  525. for (; v; v = v->next) {
  526. if (!strcasecmp(v->name, "mime_type")) {
  527. ast_string_field_set(profile, default_mime_type, v->value);
  528. } else if (!strcasecmp(v->name, "setvar")) {
  529. char *value_copy = ast_strdupa(v->value);
  530. AST_DECLARE_APP_ARGS(args,
  531. AST_APP_ARG(varname);
  532. AST_APP_ARG(varval);
  533. );
  534. AST_NONSTANDARD_APP_ARGS(args, value_copy, '=');
  535. do {
  536. if (ast_strlen_zero(args.varname) || ast_strlen_zero(args.varval))
  537. break;
  538. args.varname = ast_strip(args.varname);
  539. args.varval = ast_strip(args.varval);
  540. if (ast_strlen_zero(args.varname) || ast_strlen_zero(args.varval))
  541. break;
  542. AST_VAR_LIST_INSERT_TAIL(profile->headp, ast_var_assign(args.varname, args.varval));
  543. } while (0);
  544. } else if (!strcasecmp(v->name, "staticdir")) {
  545. ast_string_field_set(profile, staticdir, v->value);
  546. } else {
  547. struct phoneprov_file *pp_file;
  548. char *file_extension;
  549. char *value_copy = ast_strdupa(v->value);
  550. AST_DECLARE_APP_ARGS(args,
  551. AST_APP_ARG(filename);
  552. AST_APP_ARG(mimetype);
  553. );
  554. if (!(pp_file = ast_calloc_with_stringfields(1, struct phoneprov_file, 32))) {
  555. profile = unref_profile(profile);
  556. return;
  557. }
  558. if ((file_extension = strrchr(pp_file->format, '.')))
  559. file_extension++;
  560. AST_STANDARD_APP_ARGS(args, value_copy);
  561. /* Mime type order of preference
  562. * 1) Specific mime-type defined for file in profile
  563. * 2) Mime determined by extension
  564. * 3) Default mime type specified in profile
  565. * 4) text/plain
  566. */
  567. ast_string_field_set(pp_file, mime_type, S_OR(args.mimetype,
  568. (S_OR(S_OR(ast_http_ftype2mtype(file_extension), profile->default_mime_type), "text/plain"))));
  569. if (!strcasecmp(v->name, "static_file")) {
  570. ast_string_field_set(pp_file, format, args.filename);
  571. ast_string_field_build(pp_file, template, "%s%s", profile->staticdir, args.filename);
  572. AST_LIST_INSERT_TAIL(&profile->static_files, pp_file, entry);
  573. /* Add a route for the static files, as their filenames won't change per-user */
  574. build_route(pp_file, profile, NULL, NULL);
  575. } else {
  576. ast_string_field_set(pp_file, format, v->name);
  577. ast_string_field_set(pp_file, template, args.filename);
  578. AST_LIST_INSERT_TAIL(&profile->dynamic_files, pp_file, entry);
  579. }
  580. }
  581. }
  582. ao2_link(profiles, profile);
  583. profile = unref_profile(profile);
  584. }
  585. static struct extension *delete_extension(struct extension *exten)
  586. {
  587. ast_var_list_destroy(exten->headp);
  588. ast_string_field_free_memory(exten);
  589. ast_free(exten);
  590. return NULL;
  591. }
  592. static struct extension *build_extension(const char *name, struct varshead *vars)
  593. {
  594. struct extension *exten;
  595. const char *tmp;
  596. if (!(exten = ast_calloc_with_stringfields(1, struct extension, 32))) {
  597. return NULL;
  598. }
  599. ast_string_field_set(exten, name, name);
  600. exten->headp = ast_var_list_clone(vars);
  601. if (!exten->headp) {
  602. ast_log(LOG_ERROR, "Unable to clone variables for extension '%s'\n", name);
  603. delete_extension(exten);
  604. return NULL;
  605. }
  606. tmp = ast_var_find(exten->headp, variable_lookup[AST_PHONEPROV_STD_LINENUMBER]);
  607. if (!tmp) {
  608. AST_VAR_LIST_INSERT_TAIL(exten->headp,
  609. ast_var_assign(variable_lookup[AST_PHONEPROV_STD_LINENUMBER], "1"));
  610. exten->index = 1;
  611. } else {
  612. sscanf(tmp, "%d", &exten->index);
  613. }
  614. if (!ast_var_find(exten->headp, variable_lookup[AST_PHONEPROV_STD_LINEKEYS])) {
  615. AST_VAR_LIST_INSERT_TAIL(exten->headp,
  616. ast_var_assign(variable_lookup[AST_PHONEPROV_STD_LINEKEYS], "1"));
  617. }
  618. set_timezone_variables(exten->headp,
  619. ast_var_find(vars, variable_lookup[AST_PHONEPROV_STD_TIMEZONE]));
  620. return exten;
  621. }
  622. static struct user *unref_user(struct user *user)
  623. {
  624. ao2_cleanup(user);
  625. return NULL;
  626. }
  627. /*! \brief Return a user looked up by name */
  628. static struct user *find_user(const char *macaddress)
  629. {
  630. return ao2_find(users, macaddress, OBJ_SEARCH_KEY);
  631. }
  632. static int routes_delete_cb(void *obj, void *arg, int flags)
  633. {
  634. struct http_route *route = obj;
  635. struct user *user = route->user;
  636. char *macaddress = arg;
  637. if (user && !strcmp(user->macaddress, macaddress)) {
  638. return CMP_MATCH;
  639. }
  640. return 0;
  641. }
  642. /*! \brief Free all memory associated with a user */
  643. static void user_destructor(void *obj)
  644. {
  645. struct user *user = obj;
  646. struct extension *exten;
  647. while ((exten = AST_LIST_REMOVE_HEAD(&user->extensions, entry))) {
  648. exten = delete_extension(exten);
  649. }
  650. if (user->profile) {
  651. user->profile = unref_profile(user->profile);
  652. }
  653. if (http_routes) {
  654. ao2_callback(http_routes, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, routes_delete_cb, (void *)user->macaddress);
  655. }
  656. ast_string_field_free_memory(user);
  657. }
  658. /*! \brief Delete all users */
  659. static void delete_users(void)
  660. {
  661. if (!users) {
  662. return;
  663. }
  664. ao2_callback(users, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
  665. }
  666. /*! \brief Build and return a user structure based on gathered config data */
  667. static struct user *build_user(const char *mac, struct phone_profile *profile, char *provider_name)
  668. {
  669. struct user *user;
  670. if (!(user = ao2_alloc(sizeof(*user), user_destructor))) {
  671. return NULL;
  672. }
  673. if (ast_string_field_init(user, 64)) {
  674. user = unref_user(user);
  675. return NULL;
  676. }
  677. ast_string_field_set(user, macaddress, mac);
  678. ast_string_field_set(user, provider_name, provider_name);
  679. user->profile = profile;
  680. ao2_ref(profile, 1);
  681. return user;
  682. }
  683. /*! \brief Add an extension to a user ordered by index/linenumber */
  684. static int add_user_extension(struct user *user, struct extension *exten)
  685. {
  686. struct ast_var_t *pvar, *var2;
  687. struct ast_str *str = ast_str_create(16);
  688. if (!str) {
  689. return -1;
  690. }
  691. /* Append profile variables here, and substitute variables on profile
  692. * setvars, so that we can use user specific variables in them */
  693. AST_VAR_LIST_TRAVERSE(user->profile->headp, pvar) {
  694. if (ast_var_find(exten->headp, pvar->name)) {
  695. continue;
  696. }
  697. ast_str_substitute_variables_varshead(&str, 0, exten->headp, pvar->value);
  698. if ((var2 = ast_var_assign(pvar->name, ast_str_buffer(str)))) {
  699. AST_VAR_LIST_INSERT_TAIL(exten->headp, var2);
  700. }
  701. }
  702. ast_free(str);
  703. if (AST_LIST_EMPTY(&user->extensions)) {
  704. AST_LIST_INSERT_HEAD(&user->extensions, exten, entry);
  705. } else {
  706. struct extension *exten_iter;
  707. AST_LIST_TRAVERSE_SAFE_BEGIN(&user->extensions, exten_iter, entry) {
  708. if (exten->index < exten_iter->index) {
  709. AST_LIST_INSERT_BEFORE_CURRENT(exten, entry);
  710. } else if (exten->index == exten_iter->index) {
  711. ast_log(LOG_WARNING, "Duplicate linenumber=%d for %s\n", exten->index, user->macaddress);
  712. return -1;
  713. } else if (!AST_LIST_NEXT(exten_iter, entry)) {
  714. AST_LIST_INSERT_TAIL(&user->extensions, exten, entry);
  715. }
  716. }
  717. AST_LIST_TRAVERSE_SAFE_END;
  718. }
  719. return 0;
  720. }
  721. /*! \brief Add an http route for dynamic files attached to the profile of the user */
  722. static int build_user_routes(struct user *user)
  723. {
  724. struct phoneprov_file *pp_file;
  725. struct ast_str *str;
  726. if (!(str = ast_str_create(16))) {
  727. return -1;
  728. }
  729. AST_LIST_TRAVERSE(&user->profile->dynamic_files, pp_file, entry) {
  730. ast_str_substitute_variables_varshead(&str, 0, AST_LIST_FIRST(&user->extensions)->headp, pp_file->format);
  731. build_route(pp_file, user->profile, user, ast_str_buffer(str));
  732. }
  733. ast_free(str);
  734. return 0;
  735. }
  736. /*! \brief Callback that is executed everytime an http request is received by this module */
  737. 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)
  738. {
  739. struct http_route *route;
  740. struct ast_str *result;
  741. char path[PATH_MAX];
  742. char *file = NULL;
  743. char *server;
  744. int len;
  745. int fd;
  746. struct ast_str *http_header;
  747. if (method != AST_HTTP_GET && method != AST_HTTP_HEAD) {
  748. ast_http_error(ser, 501, "Not Implemented", "Attempt to use unimplemented / unsupported method");
  749. return 0;
  750. }
  751. if (!(route = ao2_find(http_routes, uri, OBJ_SEARCH_KEY))) {
  752. goto out404;
  753. }
  754. snprintf(path, sizeof(path), "%s/phoneprov/%s", ast_config_AST_DATA_DIR, route->file->template);
  755. if (!route->user) { /* Static file */
  756. fd = open(path, O_RDONLY);
  757. if (fd < 0) {
  758. goto out500;
  759. }
  760. len = lseek(fd, 0, SEEK_END);
  761. lseek(fd, 0, SEEK_SET);
  762. if (len < 0) {
  763. ast_log(LOG_WARNING, "Could not load file: %s (%d)\n", path, len);
  764. close(fd);
  765. goto out500;
  766. }
  767. http_header = ast_str_create(80);
  768. ast_str_set(&http_header, 0, "Content-type: %s\r\n",
  769. route->file->mime_type);
  770. ast_http_send(ser, method, 200, NULL, http_header, NULL, fd, 0);
  771. close(fd);
  772. route = unref_route(route);
  773. return 0;
  774. } else { /* Dynamic file */
  775. struct ast_str *tmp;
  776. len = load_file(path, &file);
  777. if (len < 0) {
  778. ast_log(LOG_WARNING, "Could not load file: %s (%d)\n", path, len);
  779. if (file) {
  780. ast_free(file);
  781. }
  782. goto out500;
  783. }
  784. if (!file) {
  785. goto out500;
  786. }
  787. if (!(tmp = ast_str_create(len))) {
  788. if (file) {
  789. ast_free(file);
  790. }
  791. goto out500;
  792. }
  793. /* Unless we are overridden by serveriface or serveraddr, we set the SERVER variable to
  794. * the IP address we are listening on that the phone contacted for this config file */
  795. server = ast_var_find(AST_LIST_FIRST(&route->user->extensions)->headp,
  796. variable_lookup[AST_PHONEPROV_STD_SERVER]);
  797. if (!server) {
  798. union {
  799. struct sockaddr sa;
  800. struct sockaddr_in sa_in;
  801. } name;
  802. socklen_t namelen = sizeof(name.sa);
  803. int res;
  804. if ((res = getsockname(ser->fd, &name.sa, &namelen))) {
  805. ast_log(LOG_WARNING, "Could not get server IP, breakage likely.\n");
  806. } else {
  807. struct extension *exten_iter;
  808. const char *newserver = ast_inet_ntoa(name.sa_in.sin_addr);
  809. AST_LIST_TRAVERSE(&route->user->extensions, exten_iter, entry) {
  810. AST_VAR_LIST_INSERT_TAIL(exten_iter->headp,
  811. ast_var_assign(variable_lookup[AST_PHONEPROV_STD_SERVER], newserver));
  812. }
  813. }
  814. }
  815. ast_str_substitute_variables_varshead(&tmp, 0, AST_LIST_FIRST(&route->user->extensions)->headp, file);
  816. ast_free(file);
  817. http_header = ast_str_create(80);
  818. ast_str_set(&http_header, 0, "Content-type: %s\r\n",
  819. route->file->mime_type);
  820. if (!(result = ast_str_create(512))) {
  821. ast_log(LOG_ERROR, "Could not create result string!\n");
  822. if (tmp) {
  823. ast_free(tmp);
  824. }
  825. ast_free(http_header);
  826. goto out500;
  827. }
  828. ast_str_append(&result, 0, "%s", ast_str_buffer(tmp));
  829. ast_http_send(ser, method, 200, NULL, http_header, result, 0, 0);
  830. ast_free(tmp);
  831. route = unref_route(route);
  832. return 0;
  833. }
  834. out404:
  835. ast_http_error(ser, 404, "Not Found", uri);
  836. return 0;
  837. out500:
  838. route = unref_route(route);
  839. ast_http_error(ser, 500, "Internal Error", "An internal error has occured.");
  840. return 0;
  841. }
  842. /*! \brief A dialplan function that can be used to print a string for each phoneprov user */
  843. static int pp_each_user_helper(struct ast_channel *chan, char *data, char *buf, struct ast_str **bufstr, int len)
  844. {
  845. char *tmp;
  846. struct ao2_iterator i;
  847. struct user *user;
  848. struct ast_str *str;
  849. AST_DECLARE_APP_ARGS(args,
  850. AST_APP_ARG(string);
  851. AST_APP_ARG(exclude_mac);
  852. );
  853. AST_STANDARD_APP_ARGS(args, data);
  854. if (!(str = ast_str_create(16))) {
  855. return -1;
  856. }
  857. /* Fix data by turning %{ into ${ */
  858. while ((tmp = strstr(args.string, "%{")))
  859. *tmp = '$';
  860. i = ao2_iterator_init(users, 0);
  861. while ((user = ao2_iterator_next(&i))) {
  862. if (!ast_strlen_zero(args.exclude_mac) && !strcasecmp(user->macaddress, args.exclude_mac)) {
  863. continue;
  864. }
  865. ast_str_substitute_variables_varshead(&str, len, AST_LIST_FIRST(&user->extensions)->headp, args.string);
  866. if (buf) {
  867. size_t slen = len;
  868. ast_build_string(&buf, &slen, "%s", ast_str_buffer(str));
  869. } else {
  870. ast_str_append(bufstr, len, "%s", ast_str_buffer(str));
  871. }
  872. user = unref_user(user);
  873. }
  874. ao2_iterator_destroy(&i);
  875. ast_free(str);
  876. return 0;
  877. }
  878. static int pp_each_user_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  879. {
  880. return pp_each_user_helper(chan, data, buf, NULL, len);
  881. }
  882. static int pp_each_user_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
  883. {
  884. return pp_each_user_helper(chan, data, NULL, buf, len);
  885. }
  886. static struct ast_custom_function pp_each_user_function = {
  887. .name = "PP_EACH_USER",
  888. .read = pp_each_user_read,
  889. .read2 = pp_each_user_read2,
  890. };
  891. /*! \brief A dialplan function that can be used to output a template for each extension attached to a user */
  892. static int pp_each_extension_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, struct ast_str **bufstr, int len)
  893. {
  894. struct user *user;
  895. struct extension *exten;
  896. char path[PATH_MAX];
  897. char *file;
  898. int filelen;
  899. struct ast_str *str;
  900. AST_DECLARE_APP_ARGS(args,
  901. AST_APP_ARG(mac);
  902. AST_APP_ARG(template);
  903. );
  904. AST_STANDARD_APP_ARGS(args, data);
  905. if (ast_strlen_zero(args.mac) || ast_strlen_zero(args.template)) {
  906. ast_log(LOG_WARNING, "PP_EACH_EXTENSION requries both a macaddress and template filename.\n");
  907. return 0;
  908. }
  909. if (!(user = find_user(args.mac))) {
  910. ast_log(LOG_WARNING, "Could not find user with mac = '%s'\n", args.mac);
  911. return 0;
  912. }
  913. snprintf(path, sizeof(path), "%s/phoneprov/%s", ast_config_AST_DATA_DIR, args.template);
  914. filelen = load_file(path, &file);
  915. if (filelen < 0) {
  916. ast_log(LOG_WARNING, "Could not load file: %s (%d)\n", path, filelen);
  917. if (file) {
  918. ast_free(file);
  919. }
  920. return 0;
  921. }
  922. if (!file) {
  923. return 0;
  924. }
  925. if (!(str = ast_str_create(filelen))) {
  926. return 0;
  927. }
  928. AST_LIST_TRAVERSE(&user->extensions, exten, entry) {
  929. ast_str_substitute_variables_varshead(&str, 0, exten->headp, file);
  930. if (buf) {
  931. size_t slen = len;
  932. ast_build_string(&buf, &slen, "%s", ast_str_buffer(str));
  933. } else {
  934. ast_str_append(bufstr, len, "%s", ast_str_buffer(str));
  935. }
  936. }
  937. ast_free(file);
  938. ast_free(str);
  939. user = unref_user(user);
  940. return 0;
  941. }
  942. static int pp_each_extension_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  943. {
  944. return pp_each_extension_helper(chan, cmd, data, buf, NULL, len);
  945. }
  946. static int pp_each_extension_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
  947. {
  948. return pp_each_extension_helper(chan, cmd, data, NULL, buf, len);
  949. }
  950. static struct ast_custom_function pp_each_extension_function = {
  951. .name = "PP_EACH_EXTENSION",
  952. .read = pp_each_extension_read,
  953. .read2 = pp_each_extension_read2,
  954. };
  955. #define FORMATS "%-20.20s %-40.40s %-30.30s\n"
  956. #define FORMATD "%-20.20s %-20.20s %-40.40s %-30.30s\n"
  957. static int route_list_cb(void *obj, void *arg, void *data, int flags)
  958. {
  959. int fd = *(int *)arg;
  960. struct http_route *route = obj;
  961. if (data && route->user) {
  962. ast_cli(fd, FORMATD, route->user->provider_name, route->profile->name, route->uri, route->file->template);
  963. }
  964. if (!data && !route->user) {
  965. ast_cli(fd, FORMATS, route->profile->name, route->uri, route->file->template);
  966. }
  967. return CMP_MATCH;
  968. }
  969. /*! \brief CLI command to list static and dynamic routes */
  970. static char *handle_show_routes(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  971. {
  972. int fd = a->fd;
  973. switch(cmd) {
  974. case CLI_INIT:
  975. e->command = "phoneprov show routes";
  976. e->usage =
  977. "Usage: phoneprov show routes\n"
  978. " Lists all registered phoneprov http routes.\n";
  979. return NULL;
  980. case CLI_GENERATE:
  981. return NULL;
  982. }
  983. /* This currently iterates over routes twice, but it is the only place I've needed
  984. * to really separate static an dynamic routes, so I've just left it this way. */
  985. ast_cli(a->fd, "Static routes\n\n");
  986. ast_cli(a->fd, FORMATS, "Profile", "Relative URI", "Physical location");
  987. ao2_callback_data(http_routes, OBJ_NODATA | OBJ_MULTIPLE, route_list_cb, &fd, NULL);
  988. ast_cli(a->fd, "\nDynamic routes\n\n");
  989. ast_cli(a->fd, FORMATD, "Provider", "Profile", "Relative URI", "Template");
  990. ao2_callback_data(http_routes, OBJ_NODATA | OBJ_MULTIPLE, route_list_cb, &fd, (void *)1);
  991. return CLI_SUCCESS;
  992. }
  993. static struct ast_cli_entry pp_cli[] = {
  994. AST_CLI_DEFINE(handle_show_routes, "Show registered phoneprov http routes"),
  995. };
  996. static struct ast_http_uri phoneprovuri = {
  997. .callback = phoneprov_callback,
  998. .description = "Asterisk HTTP Phone Provisioning Tool",
  999. .uri = "phoneprov",
  1000. .has_subtree = 1,
  1001. .data = NULL,
  1002. .key = __FILE__,
  1003. };
  1004. static struct varshead *get_defaults(void)
  1005. {
  1006. struct ast_config *phoneprov_cfg, *cfg = CONFIG_STATUS_FILEINVALID;
  1007. const char *value;
  1008. struct ast_variable *v;
  1009. struct ast_var_t *var;
  1010. struct ast_flags config_flags = { 0 };
  1011. struct varshead *defaults = ast_var_list_create();
  1012. if (!defaults) {
  1013. ast_log(LOG_ERROR, "Unable to create default var list.\n");
  1014. return NULL;
  1015. }
  1016. if (!(phoneprov_cfg = ast_config_load("phoneprov.conf", config_flags))
  1017. || phoneprov_cfg == CONFIG_STATUS_FILEINVALID) {
  1018. ast_log(LOG_ERROR, "Unable to load config phoneprov.conf\n");
  1019. ast_var_list_destroy(defaults);
  1020. return NULL;
  1021. }
  1022. value = ast_variable_retrieve(phoneprov_cfg, "general", pp_general_lookup[AST_PHONEPROV_STD_SERVER]);
  1023. if (!value) {
  1024. struct in_addr addr;
  1025. value = ast_variable_retrieve(phoneprov_cfg, "general", pp_general_lookup[AST_PHONEPROV_STD_SERVER_IFACE]);
  1026. if (value) {
  1027. lookup_iface(value, &addr);
  1028. value = ast_inet_ntoa(addr);
  1029. }
  1030. }
  1031. if (value) {
  1032. var = ast_var_assign(variable_lookup[AST_PHONEPROV_STD_SERVER], value);
  1033. AST_VAR_LIST_INSERT_TAIL(defaults, var);
  1034. } else {
  1035. ast_log(LOG_WARNING, "Unable to find a valid server address or name.\n");
  1036. }
  1037. value = ast_variable_retrieve(phoneprov_cfg, "general", pp_general_lookup[AST_PHONEPROV_STD_SERVER_PORT]);
  1038. if (!value) {
  1039. if ((cfg = ast_config_load("sip.conf", config_flags)) && cfg != CONFIG_STATUS_FILEINVALID) {
  1040. value = ast_variable_retrieve(cfg, "general", "bindport");
  1041. }
  1042. }
  1043. var = ast_var_assign(variable_lookup[AST_PHONEPROV_STD_SERVER_PORT], S_OR(value, "5060"));
  1044. if(cfg && cfg != CONFIG_STATUS_FILEINVALID) {
  1045. ast_config_destroy(cfg);
  1046. }
  1047. AST_VAR_LIST_INSERT_TAIL(defaults, var);
  1048. value = ast_variable_retrieve(phoneprov_cfg, "general", pp_general_lookup[AST_PHONEPROV_STD_PROFILE]);
  1049. if (!value) {
  1050. ast_log(LOG_ERROR, "Unable to load default profile.\n");
  1051. ast_config_destroy(phoneprov_cfg);
  1052. ast_var_list_destroy(defaults);
  1053. return NULL;
  1054. }
  1055. var = ast_var_assign(variable_lookup[AST_PHONEPROV_STD_PROFILE], value);
  1056. AST_VAR_LIST_INSERT_TAIL(defaults, var);
  1057. ast_config_destroy(phoneprov_cfg);
  1058. if (!(cfg = ast_config_load("users.conf", config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
  1059. ast_log(LOG_ERROR, "Unable to load users.conf\n");
  1060. ast_var_list_destroy(defaults);
  1061. return NULL;
  1062. }
  1063. /* Go ahead and load global variables from users.conf so we can append to profiles */
  1064. for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
  1065. if (!strcasecmp(v->name, pp_user_lookup[AST_PHONEPROV_STD_VOICEMAIL_EXTEN])) {
  1066. var = ast_var_assign(variable_lookup[AST_PHONEPROV_STD_VOICEMAIL_EXTEN], v->value);
  1067. AST_VAR_LIST_INSERT_TAIL(defaults, var);
  1068. }
  1069. if (!strcasecmp(v->name, pp_user_lookup[AST_PHONEPROV_STD_EXTENSION_LENGTH])) {
  1070. var = ast_var_assign(variable_lookup[AST_PHONEPROV_STD_EXTENSION_LENGTH], v->value);
  1071. AST_VAR_LIST_INSERT_TAIL(defaults, var);
  1072. }
  1073. }
  1074. ast_config_destroy(cfg);
  1075. return defaults;
  1076. }
  1077. static int load_users(void)
  1078. {
  1079. struct ast_config *cfg;
  1080. char *cat;
  1081. const char *value;
  1082. struct ast_flags config_flags = { 0 };
  1083. struct varshead *defaults = get_defaults();
  1084. if (!defaults) {
  1085. ast_log(LOG_WARNING, "Unable to load default variables.\n");
  1086. return -1;
  1087. }
  1088. if (!(cfg = ast_config_load("users.conf", config_flags))
  1089. || cfg == CONFIG_STATUS_FILEINVALID) {
  1090. ast_log(LOG_WARNING, "Unable to load users.conf\n");
  1091. ast_var_list_destroy(defaults);
  1092. return -1;
  1093. }
  1094. cat = NULL;
  1095. while ((cat = ast_category_browse(cfg, cat))) {
  1096. const char *tmp;
  1097. int i;
  1098. struct ast_var_t *varx;
  1099. struct ast_var_t *vard;
  1100. if (strcasecmp(cat, "general") && strcasecmp(cat, "authentication")) {
  1101. struct varshead *variables = ast_var_list_create();
  1102. if (!((tmp = ast_variable_retrieve(cfg, cat, "autoprov")) && ast_true(tmp))) {
  1103. ast_var_list_destroy(variables);
  1104. continue;
  1105. }
  1106. /* Transfer the standard variables */
  1107. for (i = 0; i < AST_PHONEPROV_STD_VAR_LIST_LENGTH; i++) {
  1108. if (pp_user_lookup[i]) {
  1109. value = ast_variable_retrieve(cfg, cat, pp_user_lookup[i]);
  1110. if (value) {
  1111. varx = ast_var_assign(variable_lookup[i],
  1112. value);
  1113. AST_VAR_LIST_INSERT_TAIL(variables, varx);
  1114. }
  1115. }
  1116. }
  1117. if (!ast_var_find(variables, variable_lookup[AST_PHONEPROV_STD_MAC])) {
  1118. ast_log(LOG_WARNING, "autoprov set for %s, but no mac address - skipping.\n", cat);
  1119. ast_var_list_destroy(variables);
  1120. continue;
  1121. }
  1122. /* Apply defaults */
  1123. AST_VAR_LIST_TRAVERSE(defaults, vard) {
  1124. if (ast_var_find(variables, vard->name)) {
  1125. continue;
  1126. }
  1127. varx = ast_var_assign(vard->name, vard->value);
  1128. AST_VAR_LIST_INSERT_TAIL(variables, varx);
  1129. }
  1130. ast_phoneprov_add_extension(SIPUSERS_PROVIDER_NAME, variables);
  1131. }
  1132. }
  1133. ast_config_destroy(cfg);
  1134. ast_var_list_destroy(defaults);
  1135. return 0;
  1136. }
  1137. static int load_common(void)
  1138. {
  1139. struct ast_config *phoneprov_cfg;
  1140. struct ast_flags config_flags = { 0 };
  1141. char *cat;
  1142. if (!(phoneprov_cfg = ast_config_load("phoneprov.conf", config_flags))
  1143. || phoneprov_cfg == CONFIG_STATUS_FILEINVALID) {
  1144. ast_log(LOG_ERROR, "Unable to load config phoneprov.conf\n");
  1145. return -1;
  1146. }
  1147. cat = NULL;
  1148. while ((cat = ast_category_browse(phoneprov_cfg, cat))) {
  1149. if (!strcasecmp(cat, "general")) {
  1150. continue;
  1151. }
  1152. build_profile(cat, ast_variable_browse(phoneprov_cfg, cat));
  1153. }
  1154. ast_config_destroy(phoneprov_cfg);
  1155. if (!ao2_container_count(profiles)) {
  1156. ast_log(LOG_ERROR, "There are no provisioning profiles in phoneprov.conf.\n");
  1157. return -1;
  1158. }
  1159. return 0;
  1160. }
  1161. static int unload_module(void)
  1162. {
  1163. ast_http_uri_unlink(&phoneprovuri);
  1164. ast_custom_function_unregister(&pp_each_user_function);
  1165. ast_custom_function_unregister(&pp_each_extension_function);
  1166. ast_cli_unregister_multiple(pp_cli, ARRAY_LEN(pp_cli));
  1167. /* This cleans up the sip.conf/users.conf provider (called specifically for clarity) */
  1168. ast_phoneprov_provider_unregister(SIPUSERS_PROVIDER_NAME);
  1169. /* This cleans up the framework which also cleans up the providers. */
  1170. delete_profiles();
  1171. ao2_cleanup(profiles);
  1172. profiles = NULL;
  1173. delete_routes();
  1174. delete_users();
  1175. ao2_cleanup(http_routes);
  1176. http_routes = NULL;
  1177. ao2_cleanup(users);
  1178. users = NULL;
  1179. delete_providers();
  1180. ao2_cleanup(providers);
  1181. providers = NULL;
  1182. return 0;
  1183. }
  1184. /*!
  1185. * \brief Load the module
  1186. *
  1187. * Module loading including tests for configuration or dependencies.
  1188. * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
  1189. * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
  1190. * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
  1191. * configuration file or other non-critical problem return
  1192. * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
  1193. */
  1194. static int load_module(void)
  1195. {
  1196. profiles = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, MAX_PROFILE_BUCKETS,
  1197. phone_profile_hash_fn, NULL, phone_profile_cmp_fn);
  1198. if (!profiles) {
  1199. ast_log(LOG_ERROR, "Unable to allocate profiles container.\n");
  1200. return AST_MODULE_LOAD_DECLINE;
  1201. }
  1202. http_routes = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, MAX_ROUTE_BUCKETS,
  1203. http_route_hash_fn, NULL, http_route_cmp_fn);
  1204. if (!http_routes) {
  1205. ast_log(LOG_ERROR, "Unable to allocate routes container.\n");
  1206. goto error;
  1207. }
  1208. if (load_common()) {
  1209. ast_log(LOG_ERROR, "Unable to load provisioning profiles.\n");
  1210. goto error;
  1211. }
  1212. users = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, MAX_USER_BUCKETS,
  1213. user_hash_fn, NULL, user_cmp_fn);
  1214. if (!users) {
  1215. ast_log(LOG_ERROR, "Unable to allocate users container.\n");
  1216. goto error;
  1217. }
  1218. providers = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
  1219. MAX_PROVIDER_BUCKETS, phoneprov_provider_hash_fn, NULL, phoneprov_provider_cmp_fn);
  1220. if (!providers) {
  1221. ast_log(LOG_ERROR, "Unable to allocate providers container.\n");
  1222. goto error;
  1223. }
  1224. /* Register ourselves as the provider for sip.conf/users.conf */
  1225. if (ast_phoneprov_provider_register(SIPUSERS_PROVIDER_NAME, load_users)) {
  1226. ast_log(LOG_WARNING, "Unable register sip/users config provider. Others may succeed.\n");
  1227. }
  1228. ast_http_uri_link(&phoneprovuri);
  1229. ast_custom_function_register(&pp_each_user_function);
  1230. ast_custom_function_register(&pp_each_extension_function);
  1231. ast_cli_register_multiple(pp_cli, ARRAY_LEN(pp_cli));
  1232. return AST_MODULE_LOAD_SUCCESS;
  1233. error:
  1234. unload_module();
  1235. return AST_MODULE_LOAD_DECLINE;
  1236. }
  1237. static int reload(void)
  1238. {
  1239. struct ao2_iterator i;
  1240. struct phoneprov_provider *provider;
  1241. /* Clean everything except the providers */
  1242. delete_routes();
  1243. delete_users();
  1244. delete_profiles();
  1245. /* Reload the profiles */
  1246. if (load_common()) {
  1247. ast_log(LOG_ERROR, "Unable to reload provisioning profiles.\n");
  1248. unload_module();
  1249. return AST_MODULE_LOAD_DECLINE;
  1250. }
  1251. /* For each provider, reload the users */
  1252. ao2_lock(providers);
  1253. i = ao2_iterator_init(providers, 0);
  1254. for(; (provider = ao2_iterator_next(&i)); ao2_ref(provider, -1)) {
  1255. if (provider->load_users()) {
  1256. ast_log(LOG_ERROR, "Unable to load provider '%s' users. Reload aborted.\n", provider->provider_name);
  1257. continue;
  1258. }
  1259. }
  1260. ao2_iterator_destroy(&i);
  1261. ao2_unlock(providers);
  1262. return AST_MODULE_LOAD_SUCCESS;
  1263. }
  1264. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",
  1265. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  1266. .load = load_module,
  1267. .unload = unload_module,
  1268. .reload = reload,
  1269. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  1270. );
  1271. /**** Public API for register/unregister, set defaults, and add extension. ****/
  1272. const char *ast_phoneprov_std_variable_lookup(enum ast_phoneprov_std_variables var)
  1273. {
  1274. if (var >= AST_PHONEPROV_STD_VAR_LIST_LENGTH) {
  1275. return NULL;
  1276. }
  1277. return variable_lookup[var];
  1278. }
  1279. int ast_phoneprov_provider_register(char *provider_name,
  1280. ast_phoneprov_load_users_cb load_users)
  1281. {
  1282. struct phoneprov_provider *provider;
  1283. if (ast_strlen_zero(provider_name)) {
  1284. ast_log(LOG_ERROR, "Provider name can't be empty.\n");
  1285. return -1;
  1286. }
  1287. if (!providers) {
  1288. ast_log(LOG_WARNING, "Provider '%s' cannot be registered: res_phoneprov not loaded.\n", provider_name);
  1289. return -1;
  1290. }
  1291. provider = find_provider(provider_name);
  1292. if (provider) {
  1293. ast_log(LOG_ERROR, "There is already a provider registered named '%s'.\n", provider_name);
  1294. ao2_ref(provider, -1);
  1295. return -1;
  1296. }
  1297. provider = ao2_alloc(sizeof(struct phoneprov_provider), provider_destructor);
  1298. if (!provider) {
  1299. ast_log(LOG_ERROR, "Unable to allocate sufficient memory for provider '%s'.\n", provider_name);
  1300. return -1;
  1301. }
  1302. if (ast_string_field_init(provider, 32)) {
  1303. ao2_ref(provider, -1);
  1304. ast_log(LOG_ERROR, "Unable to allocate sufficient memory for provider '%s' stringfields.\n", provider_name);
  1305. return -1;
  1306. }
  1307. ast_string_field_set(provider, provider_name, provider_name);
  1308. provider->load_users = load_users;
  1309. ao2_link(providers, provider);
  1310. ao2_ref(provider, -1);
  1311. if (provider->load_users()) {
  1312. ast_log(LOG_ERROR, "Unable to load provider '%s' users. Register aborted.\n", provider_name);
  1313. ast_phoneprov_provider_unregister(provider_name);
  1314. return -1;
  1315. }
  1316. return 0;
  1317. }
  1318. static int extensions_delete_cb(void *obj, void *arg, int flags)
  1319. {
  1320. char *provider_name = arg;
  1321. struct user *user = obj;
  1322. if (strcmp(user->provider_name, provider_name)) {
  1323. return 0;
  1324. }
  1325. return CMP_MATCH;
  1326. }
  1327. static int extension_delete_cb(void *obj, void *arg, void *data, int flags)
  1328. {
  1329. struct user *user = obj;
  1330. char *provider_name = data;
  1331. char *macaddress = arg;
  1332. if (!strcmp(user->provider_name, provider_name) && !strcasecmp(user->macaddress, macaddress)) {
  1333. return CMP_MATCH;
  1334. }
  1335. return 0;
  1336. }
  1337. void ast_phoneprov_delete_extension(char *provider_name, char *macaddress)
  1338. {
  1339. if (!users) {
  1340. return;
  1341. }
  1342. ao2_callback_data(users, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE | OBJ_SEARCH_KEY,
  1343. extension_delete_cb, macaddress, provider_name);
  1344. }
  1345. void ast_phoneprov_delete_extensions(char *provider_name)
  1346. {
  1347. if (!users) {
  1348. return;
  1349. }
  1350. ao2_callback(users, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, extensions_delete_cb, provider_name);
  1351. }
  1352. void ast_phoneprov_provider_unregister(char *provider_name)
  1353. {
  1354. if (!providers) {
  1355. return;
  1356. }
  1357. ast_phoneprov_delete_extensions(provider_name);
  1358. ao2_find(providers, provider_name, OBJ_SEARCH_KEY | OBJ_NODATA | OBJ_UNLINK);
  1359. }
  1360. int ast_phoneprov_add_extension(char *provider_name, struct varshead *vars)
  1361. {
  1362. RAII_VAR(struct phoneprov_provider *, provider, NULL, ao2_cleanup);
  1363. RAII_VAR(struct user *, user, NULL, ao2_cleanup);
  1364. RAII_VAR(struct phone_profile *, profile, NULL, ao2_cleanup);
  1365. struct extension *exten;
  1366. char *profile_name;
  1367. char *mac;
  1368. char *username;
  1369. if (ast_strlen_zero(provider_name)) {
  1370. ast_log(LOG_ERROR, "Provider name can't be empty.\n");
  1371. return -1;
  1372. }
  1373. if (!vars) {
  1374. ast_log(LOG_ERROR, "Variable list can't be empty.\n");
  1375. return -1;
  1376. }
  1377. username = ast_var_find(vars, variable_lookup[AST_PHONEPROV_STD_USERNAME]);
  1378. if (!username) {
  1379. ast_log(LOG_ERROR, "Extension name can't be empty.\n");
  1380. return -1;
  1381. }
  1382. mac = ast_var_find(vars, variable_lookup[AST_PHONEPROV_STD_MAC]);
  1383. if (!mac) {
  1384. ast_log(LOG_ERROR, "MAC Address can't be empty.\n");
  1385. return -1;
  1386. }
  1387. provider = find_provider(provider_name);
  1388. if (!provider) {
  1389. ast_log(LOG_ERROR, "Provider '%s' wasn't found in the registry.\n", provider_name);
  1390. return -1;
  1391. }
  1392. profile_name = ast_var_find(vars,
  1393. variable_lookup[AST_PHONEPROV_STD_PROFILE]);
  1394. if (!profile_name) {
  1395. ast_log(LOG_ERROR, "No profile could be found for user '%s' - skipping.\n", username);
  1396. return -1;
  1397. }
  1398. if (!(profile = find_profile(profile_name))) {
  1399. ast_log(LOG_ERROR, "Could not look up profile '%s' - skipping.\n", profile_name);
  1400. return -1;
  1401. }
  1402. if (!(user = find_user(mac))) {
  1403. if (!(user = build_user(mac, profile, provider_name))) {
  1404. ast_log(LOG_ERROR, "Could not create user for '%s' - skipping\n", mac);
  1405. return -1;
  1406. }
  1407. if (!(exten = build_extension(username, vars))) {
  1408. ast_log(LOG_ERROR, "Could not create extension for '%s' - skipping\n", user->macaddress);
  1409. return -1;
  1410. }
  1411. if (add_user_extension(user, exten)) {
  1412. ast_log(LOG_WARNING, "Could not add extension '%s' to user '%s'\n", exten->name, user->macaddress);
  1413. exten = delete_extension(exten);
  1414. return -1;
  1415. }
  1416. if (build_user_routes(user)) {
  1417. ast_log(LOG_WARNING, "Could not create http routes for '%s' - skipping\n", user->macaddress);
  1418. return -1;
  1419. }
  1420. ao2_link(users, user);
  1421. } else {
  1422. if (strcmp(provider_name, user->provider_name)) {
  1423. ast_log(LOG_ERROR, "MAC address '%s' was already added by provider '%s' - skipping\n", user->macaddress, user->provider_name);
  1424. return -1;
  1425. }
  1426. if (!(exten = build_extension(username, vars))) {
  1427. ast_log(LOG_ERROR, "Could not create extension for '%s' - skipping\n", user->macaddress);
  1428. return -1;
  1429. }
  1430. if (add_user_extension(user, exten)) {
  1431. ast_log(LOG_WARNING, "Could not add extension '%s' to user '%s'\n", exten->name, user->macaddress);
  1432. exten = delete_extension(exten);
  1433. return -1;
  1434. }
  1435. }
  1436. return 0;
  1437. }