Merged revisions 328247 via svnmerge from
[asterisk/asterisk.git] / res / res_phoneprov.c
index b05240a..c6dc009 100644 (file)
  * \author Terry Wilson <twilson@digium.com>
  */
 
+/*** MODULEINFO
+       <support_level>extended</support_level>
+ ***/
+
 #include "asterisk.h"
 
 #include <sys/ioctl.h>
@@ -65,6 +69,37 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
 #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, };
 
@@ -155,19 +190,6 @@ 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 */
@@ -176,22 +198,6 @@ static char global_default_profile[80] = "";       /*!< Default profile to use if one
 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)
 {
@@ -398,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;
@@ -434,33 +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) {
-                       if (fwrite(buf, 1, len, ser->f) != len) {
-                               if (errno != EPIPE) {
-                                       ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
-                               } else {
-                                       ast_debug(3, "Requester closed the connection while downloading '%s'\n", path);
-                               }
-                               break;
-                       }
-               }
+               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) {
@@ -476,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);
                        }
@@ -492,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);
                                        }
@@ -510,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, "The requested URL was not found on this server.");
+       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
@@ -635,12 +631,7 @@ static void build_profile(const char *name, struct ast_variable *v)
                                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;
                        }
@@ -656,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);
@@ -710,13 +702,7 @@ 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;
        }
 
@@ -828,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 */
@@ -856,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 {
@@ -893,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;
 }
 
@@ -944,6 +942,7 @@ static int set_config(void)
 
        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;
        }
 
@@ -1062,6 +1061,7 @@ static void delete_routes(void)
                ao2_unlink(http_routes, route);
                route = unref_route(route);
        }
+       ao2_iterator_destroy(&i);
 }
 
 /*! \brief Delete all phone profiles, freeing their memory */
@@ -1075,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 = '$';
@@ -1098,35 +1104,46 @@ 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;
+       struct ast_str *str;
        AST_DECLARE_APP_ARGS(args,
                AST_APP_ARG(mac);
                AST_APP_ARG(template);
@@ -1158,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 */
@@ -1209,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");
@@ -1219,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;
 }
@@ -1232,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__,
 };
@@ -1304,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,