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