Merged revisions 328247 via svnmerge from
[asterisk/asterisk.git] / res / res_phoneprov.c
index 54c0942..c6dc009 100644 (file)
  * \author Terry Wilson <twilson@digium.com>
  */
 
+/*** MODULEINFO
+       <support_level>extended</support_level>
+ ***/
+
 #include "asterisk.h"
 
 #include <sys/ioctl.h>
@@ -34,7 +38,7 @@
 #ifdef SOLARIS
 #include <sys/sockio.h>
 #endif
-ASTERISK_FILE_VERSION(__FILE__, "$Revision: 96773 $")
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
 #include "asterisk/channel.h"
 #include "asterisk/file.h"
@@ -65,6 +69,37 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision: 96773 $")
 
 #define VAR_BUF_SIZE 4096
 
+/*** DOCUMENTATION
+       <function name="PP_EACH_EXTENSION" language="en_US">
+               <synopsis>
+                       Execute specified template for each extension.
+               </synopsis>
+               <syntax>
+                       <parameter name="mac" required="true" />
+                       <parameter name="template" required="true" />
+               </syntax>
+               <description>
+                       <para>Output the specified template for each extension associated with the specified MAC address.</para>
+               </description>
+       </function>
+       <function name="PP_EACH_USER" language="en_US">
+               <synopsis>
+                       Generate a string for each phoneprov user.
+               </synopsis>
+               <syntax>
+                       <parameter name="string" required="true" />
+                       <parameter name="exclude_mac" required="true" />
+               </syntax>
+               <description>
+                       <para>Pass in a string, with phoneprov variables you want substituted in the format of
+                       %{VARNAME}, and you will get the string rendered for each user in phoneprov
+                       excluding ones with MAC address <replaceable>exclude_mac</replaceable>. Probably not
+                       useful outside of res_phoneprov.</para>
+                       <para>Example: ${PP_EACH_USER(&lt;item&gt;&lt;fn&gt;%{DISPLAY_NAME}&lt;/fn&gt;&lt;/item&gt;|${MAC})</para>
+               </description>
+       </function>
+ ***/
+
 /*! \brief for use in lookup_iface */
 static struct in_addr __ourip = { .s_addr = 0x00000000, };
 
@@ -79,6 +114,7 @@ enum pp_variables {
        PP_CALLERID,
        PP_TIMEZONE,
        PP_LINENUMBER,
+       PP_LINEKEYS,
        PP_VAR_LIST_LENGTH,     /* This entry must always be the last in the list */
 };
 
@@ -97,6 +133,7 @@ static const struct pp_variable_lookup {
        { PP_CALLERID, "cid_number", "CALLERID" },
        { PP_TIMEZONE, "timezone", "TIMEZONE" },
        { PP_LINENUMBER, "linenumber", "LINE" },
+       { PP_LINEKEYS, "linekeys", "LINEKEYS" },
 };
 
 /*! \brief structure to hold file data */
@@ -153,43 +190,14 @@ static struct ao2_container *profiles;
 static struct ao2_container *http_routes;
 static struct ao2_container *users;
 
-/*! \brief Extensions whose mime types we think we know */
-static struct {
-       char *ext;
-       char *mtype;
-} mimetypes[] = {
-       { "png", "image/png" },
-       { "xml", "text/xml" },
-       { "jpg", "image/jpeg" },
-       { "js", "application/x-javascript" },
-       { "wav", "audio/x-wav" },
-       { "mp3", "audio/mpeg" },
-};
-
 static char global_server[80] = "";    /*!< Server to substitute into templates */
 static char global_serverport[6] = ""; /*!< Server port to substitute into templates */
-static char global_default_profile[80] = "";   /*!< Default profile to use if one isn't specified */   
+static char global_default_profile[80] = "";   /*!< Default profile to use if one isn't specified */
 
 /*! \brief List of global variables currently available: VOICEMAIL_EXTEN, EXTENSION_LENGTH */
 static struct varshead global_variables;
 static ast_mutex_t globals_lock;
 
-/*! \brief Return mime type based on extension */
-static char *ftype2mtype(const char *ftype)
-{
-       int x;
-
-       if (ast_strlen_zero(ftype))
-               return NULL;
-
-       for (x = 0;x < ARRAY_LEN(mimetypes);x++) {
-               if (!strcasecmp(ftype, mimetypes[x].ext))
-                       return mimetypes[x].mtype;
-       }
-       
-       return NULL;
-}
-
 /* iface is the interface (e.g. eth0); address is the return value */
 static int lookup_iface(const char *iface, struct in_addr *address)
 {
@@ -241,8 +249,8 @@ static struct phone_profile *find_profile(const char *name)
 static int profile_hash_fn(const void *obj, const int flags)
 {
        const struct phone_profile *profile = obj;
-       
-       return ast_str_hash(profile->name);
+
+       return ast_str_case_hash(profile->name);
 }
 
 static int profile_cmp_fn(void *obj, void *arg, int flags)
@@ -287,15 +295,15 @@ static struct http_route *unref_route(struct http_route *route)
 static int routes_hash_fn(const void *obj, const int flags)
 {
        const struct http_route *route = obj;
-       
-       return ast_str_hash(route->uri);
+
+       return ast_str_case_hash(route->uri);
 }
 
 static int routes_cmp_fn(void *obj, void *arg, int flags)
 {
        const struct http_route *route1 = obj, *route2 = arg;
 
-       return !strcmp(route1->uri, route2->uri) ? CMP_MATCH | CMP_STOP : 0;
+       return !strcasecmp(route1->uri, route2->uri) ? CMP_MATCH | CMP_STOP : 0;
 }
 
 static void route_destructor(void *obj)
@@ -310,7 +318,7 @@ static int load_file(const char *filename, char **ret)
 {
        int len = 0;
        FILE *f;
-       
+
        if (!(f = fopen(filename, "r"))) {
                *ret = NULL;
                return -1;
@@ -356,7 +364,7 @@ static void set_timezone_variables(struct varshead *headp, const char *zone)
        snprintf(buffer, sizeof(buffer), "%d", tzoffset);
        var = ast_var_assign("TZOFFSET", buffer);
        if (var)
-               AST_LIST_INSERT_TAIL(headp, var, entries); 
+               AST_LIST_INSERT_TAIL(headp, var, entries);
 
        if (!dstenable)
                return;
@@ -364,7 +372,7 @@ static void set_timezone_variables(struct varshead *headp, const char *zone)
        if ((var = ast_var_assign("DST_ENABLE", "1")))
                AST_LIST_INSERT_TAIL(headp, var, entries);
 
-       when.tv_sec = dststart; 
+       when.tv_sec = dststart;
        ast_localtime(&when, &tm_info, zone);
 
        snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_mon+1);
@@ -396,20 +404,23 @@ static void set_timezone_variables(struct varshead *headp, const char *zone)
 }
 
 /*! \brief Callback that is executed everytime an http request is received by this module */
-static struct ast_str *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 *vars, struct ast_variable *headers, int *status, char **title, int *contentlength)
+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)
 {
        struct http_route *route;
        struct http_route search_route = {
                .uri = uri,
        };
-       struct ast_str *result = ast_str_create(512);
+       struct ast_str *result;
        char path[PATH_MAX];
        char *file = NULL;
        int len;
        int fd;
-       char buf[256];
-       struct timeval now = ast_tvnow();
-       struct ast_tm tm;
+       struct ast_str *http_header;
+
+       if (method != AST_HTTP_GET && method != AST_HTTP_HEAD) {
+               ast_http_error(ser, 501, "Not Implemented", "Attempt to use unimplemented / unsupported method");
+               return -1;
+       }
 
        if (!(route = ao2_find(http_routes, &search_route, OBJ_POINTER))) {
                goto out404;
@@ -432,26 +443,17 @@ static struct ast_str *phoneprov_callback(struct ast_tcptls_session_instance *se
                        goto out500;
                }
 
-               ast_strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S %Z", ast_localtime(&now, &tm, "GMT"));
-               fprintf(ser->f, "HTTP/1.1 200 OK\r\n"
-                       "Server: Asterisk/%s\r\n"
-                       "Date: %s\r\n"
-                       "Connection: close\r\n"
-                       "Cache-Control: no-cache, no-store\r\n"
-                       "Content-Length: %d\r\n"
-                       "Content-Type: %s\r\n\r\n",
-                       ast_get_version(), buf, len, route->file->mime_type);
-               
-               while ((len = read(fd, buf, sizeof(buf))) > 0) {
-                       fwrite(buf, 1, len, ser->f);
-               }
+               http_header = ast_str_create(80);
+               ast_str_set(&http_header, 0, "Content-type: %s\r\n",
+                       route->file->mime_type);
+
+               ast_http_send(ser, method, 200, NULL, http_header, NULL, fd, 0);
 
                close(fd);
                route = unref_route(route);
-               return NULL;
+               return 0;
        } else { /* Dynamic file */
-               int bufsize;
-               char *tmp;
+               struct ast_str *tmp;
 
                len = load_file(path, &file);
                if (len < 0) {
@@ -467,12 +469,7 @@ static struct ast_str *phoneprov_callback(struct ast_tcptls_session_instance *se
                        goto out500;
                }
 
-               /* XXX This is a hack -- maybe sum length of all variables in route->user->headp and add that? */
-               bufsize = len + VAR_BUF_SIZE;
-               
-               /* malloc() instead of alloca() here, just in case the file is bigger than
-                * we have enough stack space for. */
-               if (!(tmp = ast_calloc(1, bufsize))) {
+               if (!(tmp = ast_str_create(len))) {
                        if (file) {
                                ast_free(file);
                        }
@@ -483,17 +480,20 @@ static struct ast_str *phoneprov_callback(struct ast_tcptls_session_instance *se
                /* Unless we are overridden by serveriface or serveraddr, we set the SERVER variable to
                 * the IP address we are listening on that the phone contacted for this config file */
                if (ast_strlen_zero(global_server)) {
-                       struct sockaddr name;
-                       socklen_t namelen = sizeof(name);
+                       union {
+                               struct sockaddr sa;
+                               struct sockaddr_in sa_in;
+                       } name;
+                       socklen_t namelen = sizeof(name.sa);
                        int res;
 
-                       if ((res = getsockname(ser->fd, &name, &namelen))) {
+                       if ((res = getsockname(ser->fd, &name.sa, &namelen))) {
                                ast_log(LOG_WARNING, "Could not get server IP, breakage likely.\n");
                        } else {
                                struct ast_var_t *var;
                                struct extension *exten_iter;
 
-                               if ((var = ast_var_assign("SERVER", ast_inet_ntoa(((struct sockaddr_in *)&name)->sin_addr)))) {
+                               if ((var = ast_var_assign("SERVER", ast_inet_ntoa(name.sa_in.sin_addr)))) {
                                        AST_LIST_TRAVERSE(&route->user->extensions, exten_iter, entry) {
                                                AST_LIST_INSERT_TAIL(exten_iter->headp, var, entries);
                                        }
@@ -501,39 +501,44 @@ static struct ast_str *phoneprov_callback(struct ast_tcptls_session_instance *se
                        }
                }
 
-               pbx_substitute_variables_varshead(AST_LIST_FIRST(&route->user->extensions)->headp, file, tmp, bufsize);
-       
+               ast_str_substitute_variables_varshead(&tmp, 0, AST_LIST_FIRST(&route->user->extensions)->headp, file);
+
                if (file) {
                        ast_free(file);
                }
 
-               ast_str_append(&result, 0,
-                       "Content-Type: %s\r\n"
-                       "Content-length: %d\r\n"
-                       "\r\n"
-                       "%s", route->file->mime_type, (int) strlen(tmp), tmp);
+               http_header = ast_str_create(80);
+               ast_str_set(&http_header, 0, "Content-type: %s\r\n",
+                       route->file->mime_type);
+
+               if (!(result = ast_str_create(512))) {
+                       ast_log(LOG_ERROR, "Could not create result string!\n");
+                       if (tmp) {
+                               ast_free(tmp);
+                       }
+                       ast_free(http_header);
+                       goto out500;
+               }
+               ast_str_append(&result, 0, "%s", ast_str_buffer(tmp)); 
 
+               ast_http_send(ser, method, 200, NULL, http_header, result, 0, 0);
                if (tmp) {
                        ast_free(tmp);
                }
 
                route = unref_route(route);
 
-               return result;
+               return 0;
        }
 
 out404:
-       *status = 404;
-       *title = strdup("Not Found");
-       *contentlength = 0;
-       return ast_http_error(404, "Not Found", NULL, "Nothing to see here.  Move along.");
+       ast_http_error(ser, 404, "Not Found", "Nothing to see here.  Move along.");
+       return -1;
 
 out500:
        route = unref_route(route);
-       *status = 500;
-       *title = strdup("Internal Server Error");
-       *contentlength = 0;
-       return ast_http_error(500, "Internal Error", NULL, "An internal error has occured.");
+       ast_http_error(ser, 500, "Internal Error", "An internal error has occured.");
+       return -1;
 }
 
 /*! \brief Build a route structure and add it to the list of available http routes
@@ -544,7 +549,7 @@ out500:
 static void build_route(struct phoneprov_file *pp_file, struct user *user, char *uri)
 {
        struct http_route *route;
-       
+
        if (!(route = ao2_alloc(sizeof(*route), route_destructor))) {
                return;
        }
@@ -581,7 +586,7 @@ static void build_profile(const char *name, struct ast_variable *v)
                profile = unref_profile(profile);
                return;
        }
-       
+
        if (!(profile->headp = ast_calloc(1, sizeof(*profile->headp)))) {
                profile = unref_profile(profile);
                return;
@@ -602,7 +607,7 @@ static void build_profile(const char *name, struct ast_variable *v)
                                AST_APP_ARG(varname);
                                AST_APP_ARG(varval);
                        );
-                       
+
                        AST_NONSTANDARD_APP_ARGS(args, value_copy, '=');
                        do {
                                if (ast_strlen_zero(args.varname) || ast_strlen_zero(args.varval))
@@ -619,19 +624,14 @@ static void build_profile(const char *name, struct ast_variable *v)
                } else {
                        struct phoneprov_file *pp_file;
                        char *file_extension;
-                       char *value_copy = ast_strdupa(v->value); 
+                       char *value_copy = ast_strdupa(v->value);
 
                        AST_DECLARE_APP_ARGS(args,
                                AST_APP_ARG(filename);
                                AST_APP_ARG(mimetype);
                        );
 
-                       if (!(pp_file = ast_calloc(1, sizeof(*pp_file)))) {
-                               profile = unref_profile(profile);
-                               return;
-                       }
-                       if (ast_string_field_init(pp_file, 32)) {
-                               ast_free(pp_file);
+                       if (!(pp_file = ast_calloc_with_stringfields(1, struct phoneprov_file, 32))) {
                                profile = unref_profile(profile);
                                return;
                        }
@@ -647,7 +647,8 @@ static void build_profile(const char *name, struct ast_variable *v)
                         * 3) Default mime type specified in profile
                         * 4) text/plain
                         */
-                       ast_string_field_set(pp_file, mime_type, S_OR(args.mimetype, (S_OR(S_OR(ftype2mtype(file_extension), profile->default_mime_type), "text/plain"))));
+                       ast_string_field_set(pp_file, mime_type, S_OR(args.mimetype,
+                               (S_OR(S_OR(ast_http_ftype2mtype(file_extension), profile->default_mime_type), "text/plain"))));
 
                        if (!strcasecmp(v->name, "static_file")) {
                                ast_string_field_set(pp_file, format, args.filename);
@@ -701,18 +702,12 @@ static struct extension *build_extension(struct ast_config *cfg, const char *nam
        const char *tmp;
        int i;
 
-       if (!(exten = ast_calloc(1, sizeof(*exten)))) {
-               return NULL;
-       }
-       
-       if (ast_string_field_init(exten, 32)) {
-               ast_free(exten);
-               exten = NULL;
+       if (!(exten = ast_calloc_with_stringfields(1, struct extension, 32))) {
                return NULL;
        }
-       
+
        ast_string_field_set(exten, name, name);
-       
+
        if (!(exten->headp = ast_calloc(1, sizeof(*exten->headp)))) {
                ast_free(exten);
                exten = NULL;
@@ -736,6 +731,10 @@ static struct extension *build_extension(struct ast_config *cfg, const char *nam
                                tmp = "1";
                        }
                        exten->index = atoi(tmp);
+               } else if (i == PP_LINEKEYS) {
+                       if (!tmp) {
+                               tmp = "1";
+                       }
                }
 
                if (tmp && (var = ast_var_assign(pp_variable_list[i].template_var, tmp))) {
@@ -776,8 +775,8 @@ static struct user *find_user(const char *macaddress)
 static int users_hash_fn(const void *obj, const int flags)
 {
        const struct user *user = obj;
-       
-       return ast_str_hash(user->macaddress);
+
+       return ast_str_case_hash(user->macaddress);
 }
 
 static int users_cmp_fn(void *obj, void *arg, int flags)
@@ -800,7 +799,7 @@ static void user_destructor(void *obj)
        if (user->profile) {
                user->profile = unref_profile(user->profile);
        }
-       
+
        ast_string_field_free_memory(user);
 }
 
@@ -815,6 +814,7 @@ static void delete_users(void)
                ao2_unlink(users, user);
                user = unref_user(user);
        }
+       ao2_iterator_destroy(&i);
 }
 
 /*! \brief Build and return a user structure based on gathered config data */
@@ -822,12 +822,11 @@ static struct user *build_user(const char *mac, struct phone_profile *profile)
 {
        struct user *user;
 
-               
        if (!(user = ao2_alloc(sizeof(*user), user_destructor))) {
                profile = unref_profile(profile);
                return NULL;
        }
-       
+
        if (ast_string_field_init(user, 32)) {
                profile = unref_profile(profile);
                user = unref_user(user);
@@ -844,18 +843,25 @@ static struct user *build_user(const char *mac, struct phone_profile *profile)
 static int add_user_extension(struct user *user, struct extension *exten)
 {
        struct ast_var_t *var;
+       struct ast_str *str = ast_str_create(16);
+
+       if (!str) {
+               return -1;
+       }
 
        /* Append profile variables here, and substitute variables on profile
         * setvars, so that we can use user specific variables in them */
        AST_LIST_TRAVERSE(user->profile->headp, var, entries) {
-               char expand_buf[VAR_BUF_SIZE] = {0,};
                struct ast_var_t *var2;
 
-               pbx_substitute_variables_varshead(exten->headp, var->value, expand_buf, sizeof(expand_buf));
-               if ((var2 = ast_var_assign(var->name, expand_buf)))
+               ast_str_substitute_variables_varshead(&str, 0, exten->headp, var->value);
+               if ((var2 = ast_var_assign(var->name, ast_str_buffer(str)))) {
                        AST_LIST_INSERT_TAIL(exten->headp, var2, entries);
+               }
        }
 
+       ast_free(str);
+
        if (AST_LIST_EMPTY(&user->extensions)) {
                AST_LIST_INSERT_HEAD(&user->extensions, exten, entry);
        } else {
@@ -881,14 +887,18 @@ static int add_user_extension(struct user *user, struct extension *exten)
 static int build_user_routes(struct user *user)
 {
        struct phoneprov_file *pp_file;
+       struct ast_str *str;
 
-       AST_LIST_TRAVERSE(&user->profile->dynamic_files, pp_file, entry) {
-               char expand_buf[VAR_BUF_SIZE] = { 0, };
+       if (!(str = ast_str_create(16))) {
+               return -1;
+       }
 
-               pbx_substitute_variables_varshead(AST_LIST_FIRST(&user->extensions)->headp, pp_file->format, expand_buf, sizeof(expand_buf));
-               build_route(pp_file, user, expand_buf);
+       AST_LIST_TRAVERSE(&user->profile->dynamic_files, pp_file, entry) {
+               ast_str_substitute_variables_varshead(&str, 0, AST_LIST_FIRST(&user->extensions)->headp, pp_file->format);
+               build_route(pp_file, user, ast_str_buffer(str));
        }
 
+       ast_free(str);
        return 0;
 }
 
@@ -903,13 +913,13 @@ static int set_config(void)
 
        /* Try to grab the port from sip.conf.  If we don't get it here, we'll set it
         * to whatever is set in phoneprov.conf or default to 5060 */
-       if ((cfg = ast_config_load("sip.conf", config_flags))) {
+       if ((cfg = ast_config_load("sip.conf", config_flags)) && cfg != CONFIG_STATUS_FILEINVALID) {
                ast_copy_string(global_serverport, S_OR(ast_variable_retrieve(cfg, "general", "bindport"), "5060"), sizeof(global_serverport));
                ast_config_destroy(cfg);
        }
 
-       if (!(cfg = ast_config_load("users.conf", config_flags))) {
-               ast_log(LOG_WARNING, "Unable to load users.cfg\n");
+       if (!(cfg = ast_config_load("users.conf", config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
+               ast_log(LOG_WARNING, "Unable to load users.conf\n");
                return 0;
        }
 
@@ -930,8 +940,9 @@ static int set_config(void)
                }
        }
 
-       if (!(phoneprov_cfg = ast_config_load("phoneprov.conf", config_flags))) {
+       if (!(phoneprov_cfg = ast_config_load("phoneprov.conf", config_flags)) || phoneprov_cfg == CONFIG_STATUS_FILEINVALID) {
                ast_log(LOG_ERROR, "Unable to load config phoneprov.conf\n");
+               ast_config_destroy(cfg);
                return -1;
        }
 
@@ -950,7 +961,7 @@ static int set_config(void)
                                else if (!strcasecmp(v->name, "default_profile"))
                                        ast_copy_string(global_default_profile, v->value, sizeof(global_default_profile));
                        }
-               } else 
+               } else
                        build_profile(cat, ast_variable_browse(phoneprov_cfg, cat));
        }
 
@@ -966,11 +977,11 @@ static int set_config(void)
                if (!strcasecmp(cat, "general")) {
                        continue;
                }
-                         
+
                if (!strcasecmp(cat, "authentication"))
                        continue;
 
-               if (!((tmp = ast_variable_retrieve(cfg, cat, "autoprov")) && ast_true(tmp)))    
+               if (!((tmp = ast_variable_retrieve(cfg, cat, "autoprov")) && ast_true(tmp)))
                        continue;
 
                if (!(mac = ast_variable_retrieve(cfg, cat, "macaddress"))) {
@@ -1044,12 +1055,13 @@ static void delete_routes(void)
 {
        struct ao2_iterator i;
        struct http_route *route;
-       
+
        i = ao2_iterator_init(http_routes, 0);
        while ((route = ao2_iterator_next(&i))) {
                ao2_unlink(http_routes, route);
                route = unref_route(route);
        }
+       ao2_iterator_destroy(&i);
 }
 
 /*! \brief Delete all phone profiles, freeing their memory */
@@ -1063,20 +1075,26 @@ static void delete_profiles(void)
                ao2_unlink(profiles, profile);
                profile = unref_profile(profile);
        }
+       ao2_iterator_destroy(&i);
 }
 
 /*! \brief A dialplan function that can be used to print a string for each phoneprov user */
-static int pp_each_user_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
+static int pp_each_user_helper(struct ast_channel *chan, char *data, char *buf, struct ast_str **bufstr, int len)
 {
-       char *tmp, expand_buf[VAR_BUF_SIZE] = {0,};
+       char *tmp;
        struct ao2_iterator i;
        struct user *user;
+       struct ast_str *str;
        AST_DECLARE_APP_ARGS(args,
                AST_APP_ARG(string);
                AST_APP_ARG(exclude_mac);
        );
        AST_STANDARD_APP_ARGS(args, data);
 
+       if (!(str = ast_str_create(16))) {
+               return -1;
+       }
+
        /* Fix data by turning %{ into ${ */
        while ((tmp = strstr(args.string, "%{")))
                *tmp = '$';
@@ -1086,40 +1104,51 @@ static int pp_each_user_exec(struct ast_channel *chan, const char *cmd, char *da
                if (!ast_strlen_zero(args.exclude_mac) && !strcasecmp(user->macaddress, args.exclude_mac)) {
                        continue;
                }
-               pbx_substitute_variables_varshead(AST_LIST_FIRST(&user->extensions)->headp, args.string, expand_buf, sizeof(expand_buf));
-               ast_build_string(&buf, &len, "%s", expand_buf);
+               ast_str_substitute_variables_varshead(&str, len, AST_LIST_FIRST(&user->extensions)->headp, args.string);
+               if (buf) {
+                       size_t slen = len;
+                       ast_build_string(&buf, &slen, "%s", ast_str_buffer(str));
+               } else {
+                       ast_str_append(bufstr, len, "%s", ast_str_buffer(str));
+               }
                user = unref_user(user);
        }
+       ao2_iterator_destroy(&i);
 
+       ast_free(str);
        return 0;
 }
 
+static int pp_each_user_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
+{
+       return pp_each_user_helper(chan, data, buf, NULL, len);
+}
+
+static int pp_each_user_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
+{
+       return pp_each_user_helper(chan, data, NULL, buf, len);
+}
+
 static struct ast_custom_function pp_each_user_function = {
        .name = "PP_EACH_USER",
-       .synopsis = "Generate a string for each phoneprov user",
-       .syntax = "PP_EACH_USER(<string>|<exclude_mac>)",
-       .desc =
-               "Pass in a string, with phoneprov variables you want substituted in the format of\n"
-               "%{VARNAME}, and you will get the string rendered for each user in phoneprov\n"
-               "excluding ones with MAC address <exclude_mac>. Probably not useful outside of\n"
-               "res_phoneprov.\n"
-               "\nExample: ${PP_EACH_USER(<item><fn>%{DISPLAY_NAME}</fn></item>|${MAC})",
-       .read = pp_each_user_exec,
+       .read = pp_each_user_read,
+       .read2 = pp_each_user_read2,
 };
 
 /*! \brief A dialplan function that can be used to output a template for each extension attached to a user */
-static int pp_each_extension_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
+static int pp_each_extension_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, struct ast_str **bufstr, int len)
 {
        struct user *user;
        struct extension *exten;
        char path[PATH_MAX];
        char *file;
        int filelen;
-       AST_DECLARE_APP_ARGS(args, 
+       struct ast_str *str;
+       AST_DECLARE_APP_ARGS(args,
                AST_APP_ARG(mac);
                AST_APP_ARG(template);
        );
-       
+
        AST_STANDARD_APP_ARGS(args, data);
 
        if (ast_strlen_zero(args.mac) || ast_strlen_zero(args.template)) {
@@ -1146,27 +1175,42 @@ static int pp_each_extension_exec(struct ast_channel *chan, const char *cmd, cha
                return 0;
        }
 
+       if (!(str = ast_str_create(filelen))) {
+               return 0;
+       }
+
        AST_LIST_TRAVERSE(&user->extensions, exten, entry) {
-               char expand_buf[VAR_BUF_SIZE] = {0,};
-               pbx_substitute_variables_varshead(exten->headp, file, expand_buf, sizeof(expand_buf));
-               ast_build_string(&buf, &len, "%s", expand_buf);
+               ast_str_substitute_variables_varshead(&str, 0, exten->headp, file);
+               if (buf) {
+                       size_t slen = len;
+                       ast_build_string(&buf, &slen, "%s", ast_str_buffer(str));
+               } else {
+                       ast_str_append(bufstr, len, "%s", ast_str_buffer(str));
+               }
        }
 
        ast_free(file);
+       ast_free(str);
 
        user = unref_user(user);
 
        return 0;
 }
 
+static int pp_each_extension_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
+{
+       return pp_each_extension_helper(chan, cmd, data, buf, NULL, len);
+}
+
+static int pp_each_extension_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **buf, ssize_t len)
+{
+       return pp_each_extension_helper(chan, cmd, data, NULL, buf, len);
+}
+
 static struct ast_custom_function pp_each_extension_function = {
        .name = "PP_EACH_EXTENSION",
-       .synopsis = "Execute specified template for each extension",
-       .syntax = "PP_EACH_EXTENSION(<mac>|<template>)",
-       .desc =
-               "Output the specified template for each extension associated with the specified\n"
-               "MAC address.",
-       .read = pp_each_extension_exec,
+       .read = pp_each_extension_read,
+       .read2 = pp_each_extension_read2,
 };
 
 /*! \brief CLI command to list static and dynamic routes */
@@ -1175,7 +1219,7 @@ static char *handle_show_routes(struct ast_cli_entry *e, int cmd, struct ast_cli
 #define FORMAT "%-40.40s  %-30.30s\n"
        struct ao2_iterator i;
        struct http_route *route;
-       
+
        switch(cmd) {
        case CLI_INIT:
                e->command = "phoneprov show routes";
@@ -1197,6 +1241,7 @@ static char *handle_show_routes(struct ast_cli_entry *e, int cmd, struct ast_cli
                        ast_cli(a->fd, FORMAT, route->uri, route->file->template);
                route = unref_route(route);
        }
+       ao2_iterator_destroy(&i);
 
        ast_cli(a->fd, "\nDynamic routes\n\n");
        ast_cli(a->fd, FORMAT, "Relative URI", "Template");
@@ -1207,6 +1252,7 @@ static char *handle_show_routes(struct ast_cli_entry *e, int cmd, struct ast_cli
                        ast_cli(a->fd, FORMAT, route->uri, route->file->template);
                route = unref_route(route);
        }
+       ao2_iterator_destroy(&i);
 
        return CLI_SUCCESS;
 }
@@ -1220,7 +1266,6 @@ static struct ast_http_uri phoneprovuri = {
        .description = "Asterisk HTTP Phone Provisioning Tool",
        .uri = "phoneprov",
        .has_subtree = 1,
-       .supports_get = 1,
        .data = NULL,
        .key = __FILE__,
 };
@@ -1235,13 +1280,13 @@ static int load_module(void)
 
        AST_LIST_HEAD_INIT_NOLOCK(&global_variables);
        ast_mutex_init(&globals_lock);
-       
+
        ast_custom_function_register(&pp_each_user_function);
        ast_custom_function_register(&pp_each_extension_function);
        ast_cli_register_multiple(pp_cli, ARRAY_LEN(pp_cli));
 
        set_config();
-       ast_http_uri_link(&phoneprovuri); 
+       ast_http_uri_link(&phoneprovuri);
 
        return 0;
 }
@@ -1273,7 +1318,7 @@ static int unload_module(void)
        return 0;
 }
 
-static int reload(void) 
+static int reload(void)
 {
        struct ast_var_t *var;
 
@@ -1292,7 +1337,7 @@ static int reload(void)
        return 0;
 }
 
-AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "HTTP Phone Provisioning",
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "HTTP Phone Provisioning",
                .load = load_module,
                .unload = unload_module,
                .reload = reload,