Convert a few places to use ast_calloc_with_stringfields where applicable.
[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                         union {
491                                 struct sockaddr sa;
492                                 struct sockaddr_in sa_in;
493                         } name;
494                         socklen_t namelen = sizeof(name.sa);
495                         int res;
496
497                         if ((res = getsockname(ser->fd, &name.sa, &namelen))) {
498                                 ast_log(LOG_WARNING, "Could not get server IP, breakage likely.\n");
499                         } else {
500                                 struct ast_var_t *var;
501                                 struct extension *exten_iter;
502
503                                 if ((var = ast_var_assign("SERVER", ast_inet_ntoa(name.sa_in.sin_addr)))) {
504                                         AST_LIST_TRAVERSE(&route->user->extensions, exten_iter, entry) {
505                                                 AST_LIST_INSERT_TAIL(exten_iter->headp, var, entries);
506                                         }
507                                 }
508                         }
509                 }
510
511                 ast_str_substitute_variables_varshead(&tmp, 0, AST_LIST_FIRST(&route->user->extensions)->headp, file);
512
513                 if (file) {
514                         ast_free(file);
515                 }
516
517                 ast_str_set(&http_header, 0, "Content-type: %s\r\n",
518                         route->file->mime_type);
519
520                 if (!(result = ast_str_create(512))) {
521                         ast_log(LOG_ERROR, "Could not create result string!\n");
522                         if (tmp) {
523                                 ast_free(tmp);
524                         }
525                         goto out500;
526                 }
527                 ast_str_append(&result, 0, "%s", ast_str_buffer(tmp)); 
528
529                 ast_http_send(ser, method, 200, NULL, http_header, result, 0, 0);
530                 if (tmp) {
531                         ast_free(tmp);
532                 }
533
534                 route = unref_route(route);
535
536                 return 0;
537         }
538
539 out404:
540         ast_http_error(ser, 404, "Not Found", "Nothing to see here.  Move along.");
541         return -1;
542
543 out500:
544         route = unref_route(route);
545         ast_http_error(ser, 500, "Internal Error", "An internal error has occured.");
546         return -1;
547 }
548
549 /*! \brief Build a route structure and add it to the list of available http routes
550         \param pp_file File to link to the route
551         \param user User to link to the route (NULL means static route)
552         \param uri URI of the route
553 */
554 static void build_route(struct phoneprov_file *pp_file, struct user *user, char *uri)
555 {
556         struct http_route *route;
557
558         if (!(route = ao2_alloc(sizeof(*route), route_destructor))) {
559                 return;
560         }
561
562         if (ast_string_field_init(route, 32)) {
563                 ast_log(LOG_ERROR, "Couldn't create string fields for %s\n", pp_file->format);
564                 route = unref_route(route);
565                 return;
566         }
567
568         ast_string_field_set(route, uri, S_OR(uri, pp_file->format));
569         route->user = user;
570         route->file = pp_file;
571
572         ao2_link(http_routes, route);
573
574         route = unref_route(route);
575 }
576
577 /*! \brief Build a phone profile and add it to the list of phone profiles
578         \param name the name of the profile
579         \param v ast_variable from parsing phoneprov.conf
580 */
581 static void build_profile(const char *name, struct ast_variable *v)
582 {
583         struct phone_profile *profile;
584         struct ast_var_t *var;
585
586         if (!(profile = ao2_alloc(sizeof(*profile), profile_destructor))) {
587                 return;
588         }
589
590         if (ast_string_field_init(profile, 32)) {
591                 profile = unref_profile(profile);
592                 return;
593         }
594
595         if (!(profile->headp = ast_calloc(1, sizeof(*profile->headp)))) {
596                 profile = unref_profile(profile);
597                 return;
598         }
599
600         AST_LIST_HEAD_INIT_NOLOCK(&profile->static_files);
601         AST_LIST_HEAD_INIT_NOLOCK(&profile->dynamic_files);
602
603         ast_string_field_set(profile, name, name);
604         for (; v; v = v->next) {
605                 if (!strcasecmp(v->name, "mime_type")) {
606                         ast_string_field_set(profile, default_mime_type, v->value);
607                 } else if (!strcasecmp(v->name, "setvar")) {
608                         struct ast_var_t *variable;
609                         char *value_copy = ast_strdupa(v->value);
610
611                         AST_DECLARE_APP_ARGS(args,
612                                 AST_APP_ARG(varname);
613                                 AST_APP_ARG(varval);
614                         );
615
616                         AST_NONSTANDARD_APP_ARGS(args, value_copy, '=');
617                         do {
618                                 if (ast_strlen_zero(args.varname) || ast_strlen_zero(args.varval))
619                                         break;
620                                 args.varname = ast_strip(args.varname);
621                                 args.varval = ast_strip(args.varval);
622                                 if (ast_strlen_zero(args.varname) || ast_strlen_zero(args.varval))
623                                         break;
624                                 if ((variable = ast_var_assign(args.varname, args.varval)))
625                                         AST_LIST_INSERT_TAIL(profile->headp, variable, entries);
626                         } while (0);
627                 } else if (!strcasecmp(v->name, "staticdir")) {
628                         ast_string_field_set(profile, staticdir, v->value);
629                 } else {
630                         struct phoneprov_file *pp_file;
631                         char *file_extension;
632                         char *value_copy = ast_strdupa(v->value);
633
634                         AST_DECLARE_APP_ARGS(args,
635                                 AST_APP_ARG(filename);
636                                 AST_APP_ARG(mimetype);
637                         );
638
639                         if (!(pp_file = ast_calloc_with_stringfields(1, struct phoneprov_file, 32))) {
640                                 profile = unref_profile(profile);
641                                 return;
642                         }
643
644                         if ((file_extension = strrchr(pp_file->format, '.')))
645                                 file_extension++;
646
647                         AST_STANDARD_APP_ARGS(args, value_copy);
648
649                         /* Mime type order of preference
650                          * 1) Specific mime-type defined for file in profile
651                          * 2) Mime determined by extension
652                          * 3) Default mime type specified in profile
653                          * 4) text/plain
654                          */
655                         ast_string_field_set(pp_file, mime_type, S_OR(args.mimetype,
656                                 (S_OR(S_OR(ast_http_ftype2mtype(file_extension), profile->default_mime_type), "text/plain"))));
657
658                         if (!strcasecmp(v->name, "static_file")) {
659                                 ast_string_field_set(pp_file, format, args.filename);
660                                 ast_string_field_build(pp_file, template, "%s%s", profile->staticdir, args.filename);
661                                 AST_LIST_INSERT_TAIL(&profile->static_files, pp_file, entry);
662                                 /* Add a route for the static files, as their filenames won't change per-user */
663                                 build_route(pp_file, NULL, NULL);
664                         } else {
665                                 ast_string_field_set(pp_file, format, v->name);
666                                 ast_string_field_set(pp_file, template, args.filename);
667                                 AST_LIST_INSERT_TAIL(&profile->dynamic_files, pp_file, entry);
668                         }
669                 }
670         }
671
672         /* Append the global variables to the variables list for this profile.
673          * This is for convenience later, when we need to provide a single
674          * variable list for use in substitution. */
675         ast_mutex_lock(&globals_lock);
676         AST_LIST_TRAVERSE(&global_variables, var, entries) {
677                 struct ast_var_t *new_var;
678                 if ((new_var = ast_var_assign(var->name, var->value))) {
679                         AST_LIST_INSERT_TAIL(profile->headp, new_var, entries);
680                 }
681         }
682         ast_mutex_unlock(&globals_lock);
683
684         ao2_link(profiles, profile);
685
686         profile = unref_profile(profile);
687 }
688
689 static struct extension *delete_extension(struct extension *exten)
690 {
691         struct ast_var_t *var;
692         while ((var = AST_LIST_REMOVE_HEAD(exten->headp, entries))) {
693                 ast_var_delete(var);
694         }
695         ast_free(exten->headp);
696         ast_string_field_free_memory(exten);
697
698         ast_free(exten);
699
700         return NULL;
701 }
702
703 static struct extension *build_extension(struct ast_config *cfg, const char *name)
704 {
705         struct extension *exten;
706         struct ast_var_t *var;
707         const char *tmp;
708         int i;
709
710         if (!(exten = ast_calloc_with_stringfields(1, struct extension, 32))) {
711                 return NULL;
712         }
713
714         ast_string_field_set(exten, name, name);
715
716         if (!(exten->headp = ast_calloc(1, sizeof(*exten->headp)))) {
717                 ast_free(exten);
718                 exten = NULL;
719                 return NULL;
720         }
721
722         for (i = 0; i < PP_VAR_LIST_LENGTH; i++) {
723                 tmp = ast_variable_retrieve(cfg, name, pp_variable_list[i].user_var);
724
725                 /* If we didn't get a USERNAME variable, set it to the user->name */
726                 if (i == PP_USERNAME && !tmp) {
727                         if ((var = ast_var_assign(pp_variable_list[PP_USERNAME].template_var, exten->name))) {
728                                 AST_LIST_INSERT_TAIL(exten->headp, var, entries);
729                         }
730                         continue;
731                 } else if (i == PP_TIMEZONE) {
732                         /* perfectly ok if tmp is NULL, will set variables based on server's time zone */
733                         set_timezone_variables(exten->headp, tmp);
734                 } else if (i == PP_LINENUMBER) {
735                         if (!tmp) {
736                                 tmp = "1";
737                         }
738                         exten->index = atoi(tmp);
739                 } else if (i == PP_LINEKEYS) {
740                         if (!tmp) {
741                                 tmp = "1";
742                         }
743                 }
744
745                 if (tmp && (var = ast_var_assign(pp_variable_list[i].template_var, tmp))) {
746                         AST_LIST_INSERT_TAIL(exten->headp, var, entries);
747                 }
748         }
749
750         if (!ast_strlen_zero(global_server)) {
751                 if ((var = ast_var_assign("SERVER", global_server)))
752                         AST_LIST_INSERT_TAIL(exten->headp, var, entries);
753         }
754
755         if (!ast_strlen_zero(global_serverport)) {
756                 if ((var = ast_var_assign("SERVER_PORT", global_serverport)))
757                         AST_LIST_INSERT_TAIL(exten->headp, var, entries);
758         }
759
760         return exten;
761 }
762
763 static struct user *unref_user(struct user *user)
764 {
765         ao2_ref(user, -1);
766
767         return NULL;
768 }
769
770 /*! \brief Return a user looked up by name */
771 static struct user *find_user(const char *macaddress)
772 {
773         struct user tmp = {
774                 .macaddress = macaddress,
775         };
776
777         return ao2_find(users, &tmp, OBJ_POINTER);
778 }
779
780 static int users_hash_fn(const void *obj, const int flags)
781 {
782         const struct user *user = obj;
783
784         return ast_str_case_hash(user->macaddress);
785 }
786
787 static int users_cmp_fn(void *obj, void *arg, int flags)
788 {
789         const struct user *user1 = obj, *user2 = arg;
790
791         return !strcasecmp(user1->macaddress, user2->macaddress) ? CMP_MATCH | CMP_STOP : 0;
792 }
793
794 /*! \brief Free all memory associated with a user */
795 static void user_destructor(void *obj)
796 {
797         struct user *user = obj;
798         struct extension *exten;
799
800         while ((exten = AST_LIST_REMOVE_HEAD(&user->extensions, entry))) {
801                 exten = delete_extension(exten);
802         }
803
804         if (user->profile) {
805                 user->profile = unref_profile(user->profile);
806         }
807
808         ast_string_field_free_memory(user);
809 }
810
811 /*! \brief Delete all users */
812 static void delete_users(void)
813 {
814         struct ao2_iterator i;
815         struct user *user;
816
817         i = ao2_iterator_init(users, 0);
818         while ((user = ao2_iterator_next(&i))) {
819                 ao2_unlink(users, user);
820                 user = unref_user(user);
821         }
822         ao2_iterator_destroy(&i);
823 }
824
825 /*! \brief Build and return a user structure based on gathered config data */
826 static struct user *build_user(const char *mac, struct phone_profile *profile)
827 {
828         struct user *user;
829
830         if (!(user = ao2_alloc(sizeof(*user), user_destructor))) {
831                 profile = unref_profile(profile);
832                 return NULL;
833         }
834
835         if (ast_string_field_init(user, 32)) {
836                 profile = unref_profile(profile);
837                 user = unref_user(user);
838                 return NULL;
839         }
840
841         ast_string_field_set(user, macaddress, mac);
842         user->profile = profile; /* already ref counted by find_profile */
843
844         return user;
845 }
846
847 /*! \brief Add an extension to a user ordered by index/linenumber */
848 static int add_user_extension(struct user *user, struct extension *exten)
849 {
850         struct ast_var_t *var;
851         struct ast_str *str = ast_str_create(16);
852
853         if (!str) {
854                 return -1;
855         }
856
857         /* Append profile variables here, and substitute variables on profile
858          * setvars, so that we can use user specific variables in them */
859         AST_LIST_TRAVERSE(user->profile->headp, var, entries) {
860                 struct ast_var_t *var2;
861
862                 ast_str_substitute_variables_varshead(&str, 0, exten->headp, var->value);
863                 if ((var2 = ast_var_assign(var->name, ast_str_buffer(str)))) {
864                         AST_LIST_INSERT_TAIL(exten->headp, var2, entries);
865                 }
866         }
867
868         ast_free(str);
869
870         if (AST_LIST_EMPTY(&user->extensions)) {
871                 AST_LIST_INSERT_HEAD(&user->extensions, exten, entry);
872         } else {
873                 struct extension *exten_iter;
874
875                 AST_LIST_TRAVERSE_SAFE_BEGIN(&user->extensions, exten_iter, entry) {
876                         if (exten->index < exten_iter->index) {
877                                 AST_LIST_INSERT_BEFORE_CURRENT(exten, entry);
878                         } else if (exten->index == exten_iter->index) {
879                                 ast_log(LOG_WARNING, "Duplicate linenumber=%d for %s\n", exten->index, user->macaddress);
880                                 return -1;
881                         } else if (!AST_LIST_NEXT(exten_iter, entry)) {
882                                 AST_LIST_INSERT_TAIL(&user->extensions, exten, entry);
883                         }
884                 }
885                 AST_LIST_TRAVERSE_SAFE_END;
886         }
887
888         return 0;
889 }
890
891 /*! \brief Add an http route for dynamic files attached to the profile of the user */
892 static int build_user_routes(struct user *user)
893 {
894         struct phoneprov_file *pp_file;
895         struct ast_str *str;
896
897         if (!(str = ast_str_create(16))) {
898                 return -1;
899         }
900
901         AST_LIST_TRAVERSE(&user->profile->dynamic_files, pp_file, entry) {
902                 ast_str_substitute_variables_varshead(&str, 0, AST_LIST_FIRST(&user->extensions)->headp, pp_file->format);
903                 build_route(pp_file, user, ast_str_buffer(str));
904         }
905
906         ast_free(str);
907         return 0;
908 }
909
910 /* \brief Parse config files and create appropriate structures */
911 static int set_config(void)
912 {
913         struct ast_config *cfg, *phoneprov_cfg;
914         char *cat;
915         struct ast_variable *v;
916         struct ast_flags config_flags = { 0 };
917         struct ast_var_t *var;
918
919         /* Try to grab the port from sip.conf.  If we don't get it here, we'll set it
920          * to whatever is set in phoneprov.conf or default to 5060 */
921         if ((cfg = ast_config_load("sip.conf", config_flags)) && cfg != CONFIG_STATUS_FILEINVALID) {
922                 ast_copy_string(global_serverport, S_OR(ast_variable_retrieve(cfg, "general", "bindport"), "5060"), sizeof(global_serverport));
923                 ast_config_destroy(cfg);
924         }
925
926         if (!(cfg = ast_config_load("users.conf", config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
927                 ast_log(LOG_WARNING, "Unable to load users.conf\n");
928                 return 0;
929         }
930
931         /* Go ahead and load global variables from users.conf so we can append to profiles */
932         for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
933                 if (!strcasecmp(v->name, "vmexten")) {
934                         if ((var = ast_var_assign("VOICEMAIL_EXTEN", v->value))) {
935                                 ast_mutex_lock(&globals_lock);
936                                 AST_LIST_INSERT_TAIL(&global_variables, var, entries);
937                                 ast_mutex_unlock(&globals_lock);
938                         }
939                 }
940                 if (!strcasecmp(v->name, "localextenlength")) {
941                         if ((var = ast_var_assign("EXTENSION_LENGTH", 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
948         if (!(phoneprov_cfg = ast_config_load("phoneprov.conf", config_flags)) || phoneprov_cfg == CONFIG_STATUS_FILEINVALID) {
949                 ast_log(LOG_ERROR, "Unable to load config phoneprov.conf\n");
950                 ast_config_destroy(cfg);
951                 return -1;
952         }
953
954         cat = NULL;
955         while ((cat = ast_category_browse(phoneprov_cfg, cat))) {
956                 if (!strcasecmp(cat, "general")) {
957                         for (v = ast_variable_browse(phoneprov_cfg, cat); v; v = v->next) {
958                                 if (!strcasecmp(v->name, "serveraddr"))
959                                         ast_copy_string(global_server, v->value, sizeof(global_server));
960                                 else if (!strcasecmp(v->name, "serveriface")) {
961                                         struct in_addr addr;
962                                         lookup_iface(v->value, &addr);
963                                         ast_copy_string(global_server, ast_inet_ntoa(addr), sizeof(global_server));
964                                 } else if (!strcasecmp(v->name, "serverport"))
965                                         ast_copy_string(global_serverport, v->value, sizeof(global_serverport));
966                                 else if (!strcasecmp(v->name, "default_profile"))
967                                         ast_copy_string(global_default_profile, v->value, sizeof(global_default_profile));
968                         }
969                 } else
970                         build_profile(cat, ast_variable_browse(phoneprov_cfg, cat));
971         }
972
973         ast_config_destroy(phoneprov_cfg);
974
975         cat = NULL;
976         while ((cat = ast_category_browse(cfg, cat))) {
977                 const char *tmp, *mac;
978                 struct user *user;
979                 struct phone_profile *profile;
980                 struct extension *exten;
981
982                 if (!strcasecmp(cat, "general")) {
983                         continue;
984                 }
985
986                 if (!strcasecmp(cat, "authentication"))
987                         continue;
988
989                 if (!((tmp = ast_variable_retrieve(cfg, cat, "autoprov")) && ast_true(tmp)))
990                         continue;
991
992                 if (!(mac = ast_variable_retrieve(cfg, cat, "macaddress"))) {
993                         ast_log(LOG_WARNING, "autoprov set for %s, but no mac address - skipping.\n", cat);
994                         continue;
995                 }
996
997                 tmp = S_OR(ast_variable_retrieve(cfg, cat, "profile"), global_default_profile);
998                 if (ast_strlen_zero(tmp)) {
999                         ast_log(LOG_WARNING, "No profile for user [%s] with mac '%s' - skipping\n", cat, mac);
1000                         continue;
1001                 }
1002
1003                 if (!(user = find_user(mac))) {
1004                         if (!(profile = find_profile(tmp))) {
1005                                 ast_log(LOG_WARNING, "Could not look up profile '%s' - skipping.\n", tmp);
1006                                 continue;
1007                         }
1008
1009                         if (!(user = build_user(mac, profile))) {
1010                                 ast_log(LOG_WARNING, "Could not create user for '%s' - skipping\n", user->macaddress);
1011                                 continue;
1012                         }
1013
1014                         if (!(exten = build_extension(cfg, cat))) {
1015                                 ast_log(LOG_WARNING, "Could not create extension for %s - skipping\n", user->macaddress);
1016                                 user = unref_user(user);
1017                                 continue;
1018                         }
1019
1020                         if (add_user_extension(user, exten)) {
1021                                 ast_log(LOG_WARNING, "Could not add extension '%s' to user '%s'\n", exten->name, user->macaddress);
1022                                 user = unref_user(user);
1023                                 exten = delete_extension(exten);
1024                                 continue;
1025                         }
1026
1027                         if (build_user_routes(user)) {
1028                                 ast_log(LOG_WARNING, "Could not create http routes for %s - skipping\n", user->macaddress);
1029                                 user = unref_user(user);
1030                                 continue;
1031                         }
1032
1033                         ao2_link(users, user);
1034                         user = unref_user(user);
1035                 } else {
1036                         if (!(exten = build_extension(cfg, cat))) {
1037                                 ast_log(LOG_WARNING, "Could not create extension for %s - skipping\n", user->macaddress);
1038                                 user = unref_user(user);
1039                                 continue;
1040                         }
1041
1042                         if (add_user_extension(user, exten)) {
1043                                 ast_log(LOG_WARNING, "Could not add extension '%s' to user '%s'\n", exten->name, user->macaddress);
1044                                 user = unref_user(user);
1045                                 exten = delete_extension(exten);
1046                                 continue;
1047                         }
1048
1049                         user = unref_user(user);
1050                 }
1051         }
1052
1053         ast_config_destroy(cfg);
1054
1055         return 0;
1056 }
1057
1058 /*! \brief Delete all http routes, freeing their memory */
1059 static void delete_routes(void)
1060 {
1061         struct ao2_iterator i;
1062         struct http_route *route;
1063
1064         i = ao2_iterator_init(http_routes, 0);
1065         while ((route = ao2_iterator_next(&i))) {
1066                 ao2_unlink(http_routes, route);
1067                 route = unref_route(route);
1068         }
1069         ao2_iterator_destroy(&i);
1070 }
1071
1072 /*! \brief Delete all phone profiles, freeing their memory */
1073 static void delete_profiles(void)
1074 {
1075         struct ao2_iterator i;
1076         struct phone_profile *profile;
1077
1078         i = ao2_iterator_init(profiles, 0);
1079         while ((profile = ao2_iterator_next(&i))) {
1080                 ao2_unlink(profiles, profile);
1081                 profile = unref_profile(profile);
1082         }
1083         ao2_iterator_destroy(&i);
1084 }
1085
1086 /*! \brief A dialplan function that can be used to print a string for each phoneprov user */
1087 static int pp_each_user_helper(struct ast_channel *chan, char *data, char *buf, struct ast_str **bufstr, int len)
1088 {
1089         char *tmp;
1090         struct ao2_iterator i;
1091         struct user *user;
1092         struct ast_str *str;
1093         AST_DECLARE_APP_ARGS(args,
1094                 AST_APP_ARG(string);
1095                 AST_APP_ARG(exclude_mac);
1096         );
1097         AST_STANDARD_APP_ARGS(args, data);
1098
1099         if (!(str = ast_str_create(16))) {
1100                 return -1;
1101         }
1102
1103         /* Fix data by turning %{ into ${ */
1104         while ((tmp = strstr(args.string, "%{")))
1105                 *tmp = '$';
1106
1107         i = ao2_iterator_init(users, 0);
1108         while ((user = ao2_iterator_next(&i))) {
1109                 if (!ast_strlen_zero(args.exclude_mac) && !strcasecmp(user->macaddress, args.exclude_mac)) {
1110                         continue;
1111                 }
1112                 ast_str_substitute_variables_varshead(&str, len, AST_LIST_FIRST(&user->extensions)->headp, args.string);
1113                 if (buf) {
1114                         size_t slen = len;
1115                         ast_build_string(&buf, &slen, "%s", ast_str_buffer(str));
1116                 } else {
1117                         ast_str_append(bufstr, len, "%s", ast_str_buffer(str));
1118                 }
1119                 user = unref_user(user);
1120         }
1121         ao2_iterator_destroy(&i);
1122
1123         ast_free(str);
1124         return 0;
1125 }
1126
1127 static int pp_each_user_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
1128 {
1129         return pp_each_user_helper(chan, data, buf, NULL, len);
1130 }
1131
1132 static int pp_each_user_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
1133 {
1134         return pp_each_user_helper(chan, data, NULL, buf, len);
1135 }
1136
1137 static struct ast_custom_function pp_each_user_function = {
1138         .name = "PP_EACH_USER",
1139         .read = pp_each_user_read,
1140         .read2 = pp_each_user_read2,
1141 };
1142
1143 /*! \brief A dialplan function that can be used to output a template for each extension attached to a user */
1144 static int pp_each_extension_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, struct ast_str **bufstr, int len)
1145 {
1146         struct user *user;
1147         struct extension *exten;
1148         char path[PATH_MAX];
1149         char *file;
1150         int filelen;
1151         struct ast_str *str;
1152         AST_DECLARE_APP_ARGS(args,
1153                 AST_APP_ARG(mac);
1154                 AST_APP_ARG(template);
1155         );
1156
1157         AST_STANDARD_APP_ARGS(args, data);
1158
1159         if (ast_strlen_zero(args.mac) || ast_strlen_zero(args.template)) {
1160                 ast_log(LOG_WARNING, "PP_EACH_EXTENSION requries both a macaddress and template filename.\n");
1161                 return 0;
1162         }
1163
1164         if (!(user = find_user(args.mac))) {
1165                 ast_log(LOG_WARNING, "Could not find user with mac = '%s'\n", args.mac);
1166                 return 0;
1167         }
1168
1169         snprintf(path, sizeof(path), "%s/phoneprov/%s", ast_config_AST_DATA_DIR, args.template);
1170         filelen = load_file(path, &file);
1171         if (filelen < 0) {
1172                 ast_log(LOG_WARNING, "Could not load file: %s (%d)\n", path, filelen);
1173                 if (file) {
1174                         ast_free(file);
1175                 }
1176                 return 0;
1177         }
1178
1179         if (!file) {
1180                 return 0;
1181         }
1182
1183         if (!(str = ast_str_create(filelen))) {
1184                 return 0;
1185         }
1186
1187         AST_LIST_TRAVERSE(&user->extensions, exten, entry) {
1188                 ast_str_substitute_variables_varshead(&str, 0, exten->headp, file);
1189                 if (buf) {
1190                         size_t slen = len;
1191                         ast_build_string(&buf, &slen, "%s", ast_str_buffer(str));
1192                 } else {
1193                         ast_str_append(bufstr, len, "%s", ast_str_buffer(str));
1194                 }
1195         }
1196
1197         ast_free(file);
1198         ast_free(str);
1199
1200         user = unref_user(user);
1201
1202         return 0;
1203 }
1204
1205 static int pp_each_extension_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
1206 {
1207         return pp_each_extension_helper(chan, cmd, data, buf, NULL, len);
1208 }
1209
1210 static int pp_each_extension_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
1211 {
1212         return pp_each_extension_helper(chan, cmd, data, NULL, buf, len);
1213 }
1214
1215 static struct ast_custom_function pp_each_extension_function = {
1216         .name = "PP_EACH_EXTENSION",
1217         .read = pp_each_extension_read,
1218         .read2 = pp_each_extension_read2,
1219 };
1220
1221 /*! \brief CLI command to list static and dynamic routes */
1222 static char *handle_show_routes(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1223 {
1224 #define FORMAT "%-40.40s  %-30.30s\n"
1225         struct ao2_iterator i;
1226         struct http_route *route;
1227
1228         switch(cmd) {
1229         case CLI_INIT:
1230                 e->command = "phoneprov show routes";
1231                 e->usage =
1232                         "Usage: phoneprov show routes\n"
1233                         "       Lists all registered phoneprov http routes.\n";
1234                 return NULL;
1235         case CLI_GENERATE:
1236                 return NULL;
1237         }
1238
1239         /* This currently iterates over routes twice, but it is the only place I've needed
1240          * to really separate static an dynamic routes, so I've just left it this way. */
1241         ast_cli(a->fd, "Static routes\n\n");
1242         ast_cli(a->fd, FORMAT, "Relative URI", "Physical location");
1243         i = ao2_iterator_init(http_routes, 0);
1244         while ((route = ao2_iterator_next(&i))) {
1245                 if (!route->user)
1246                         ast_cli(a->fd, FORMAT, route->uri, route->file->template);
1247                 route = unref_route(route);
1248         }
1249         ao2_iterator_destroy(&i);
1250
1251         ast_cli(a->fd, "\nDynamic routes\n\n");
1252         ast_cli(a->fd, FORMAT, "Relative URI", "Template");
1253
1254         i = ao2_iterator_init(http_routes, 0);
1255         while ((route = ao2_iterator_next(&i))) {
1256                 if (route->user)
1257                         ast_cli(a->fd, FORMAT, route->uri, route->file->template);
1258                 route = unref_route(route);
1259         }
1260         ao2_iterator_destroy(&i);
1261
1262         return CLI_SUCCESS;
1263 }
1264
1265 static struct ast_cli_entry pp_cli[] = {
1266         AST_CLI_DEFINE(handle_show_routes, "Show registered phoneprov http routes"),
1267 };
1268
1269 static struct ast_http_uri phoneprovuri = {
1270         .callback = phoneprov_callback,
1271         .description = "Asterisk HTTP Phone Provisioning Tool",
1272         .uri = "phoneprov",
1273         .has_subtree = 1,
1274         .data = NULL,
1275         .key = __FILE__,
1276 };
1277
1278 static int load_module(void)
1279 {
1280         profiles = ao2_container_alloc(MAX_PROFILE_BUCKETS, profile_hash_fn, profile_cmp_fn);
1281
1282         http_routes = ao2_container_alloc(MAX_ROUTE_BUCKETS, routes_hash_fn, routes_cmp_fn);
1283
1284         users = ao2_container_alloc(MAX_USER_BUCKETS, users_hash_fn, users_cmp_fn);
1285
1286         AST_LIST_HEAD_INIT_NOLOCK(&global_variables);
1287         ast_mutex_init(&globals_lock);
1288
1289         ast_custom_function_register(&pp_each_user_function);
1290         ast_custom_function_register(&pp_each_extension_function);
1291         ast_cli_register_multiple(pp_cli, ARRAY_LEN(pp_cli));
1292
1293         set_config();
1294         ast_http_uri_link(&phoneprovuri);
1295
1296         return 0;
1297 }
1298
1299 static int unload_module(void)
1300 {
1301         struct ast_var_t *var;
1302
1303         ast_http_uri_unlink(&phoneprovuri);
1304         ast_custom_function_unregister(&pp_each_user_function);
1305         ast_custom_function_unregister(&pp_each_extension_function);
1306         ast_cli_unregister_multiple(pp_cli, ARRAY_LEN(pp_cli));
1307
1308         delete_routes();
1309         delete_users();
1310         delete_profiles();
1311         ao2_ref(profiles, -1);
1312         ao2_ref(http_routes, -1);
1313         ao2_ref(users, -1);
1314
1315         ast_mutex_lock(&globals_lock);
1316         while ((var = AST_LIST_REMOVE_HEAD(&global_variables, entries))) {
1317                 ast_var_delete(var);
1318         }
1319         ast_mutex_unlock(&globals_lock);
1320
1321         ast_mutex_destroy(&globals_lock);
1322
1323         return 0;
1324 }
1325
1326 static int reload(void)
1327 {
1328         struct ast_var_t *var;
1329
1330         delete_routes();
1331         delete_users();
1332         delete_profiles();
1333
1334         ast_mutex_lock(&globals_lock);
1335         while ((var = AST_LIST_REMOVE_HEAD(&global_variables, entries))) {
1336                 ast_var_delete(var);
1337         }
1338         ast_mutex_unlock(&globals_lock);
1339
1340         set_config();
1341
1342         return 0;
1343 }
1344
1345 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "HTTP Phone Provisioning",
1346                 .load = load_module,
1347                 .unload = unload_module,
1348                 .reload = reload,
1349         );