123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710 |
- /*
- * Asterisk -- An open source telephony toolkit.
- *
- * Copyright (C) 1999 - 2008, Digium, Inc.
- * Copyright (C) 2014, Fairview 5 Engineering, LLC
- *
- * Mark Spencer <markster@digium.com>
- * Matthew Brooks <mbrooks@digium.com>
- * Terry Wilson <twilson@digium.com>
- * George Joseph <george.joseph@fairview5.com>
- *
- * See http://www.asterisk.org for more information about
- * the Asterisk project. Please do not directly contact
- * any of the maintainers of this project for assistance;
- * the project provides a web site, mailing lists and IRC
- * channels for your use.
- *
- * This program is free software, distributed under the terms of
- * the GNU General Public License Version 2. See the LICENSE file
- * at the top of the source tree.
- */
- /*! \file
- *
- * \brief Phone provisioning application for the asterisk internal http server
- *
- * \author Matthew Brooks <mbrooks@digium.com>
- * \author Terry Wilson <twilson@digium.com>
- * \author George Joseph <george.joseph@fairview5.com>
- */
- /*! \li \ref res_phoneprov.c uses the configuration file \ref phoneprov.conf and \ref users.conf and \ref sip.conf
- * \addtogroup configuration_file Configuration Files
- */
- /*!
- * \page phoneprov.conf phoneprov.conf
- * \verbinclude phoneprov.conf.sample
- */
- /*** MODULEINFO
- <support_level>extended</support_level>
- ***/
- #define AST_API_MODULE
- #include "asterisk.h"
- #include <sys/ioctl.h>
- #include <sys/socket.h>
- #include <net/if.h>
- #ifdef SOLARIS
- #include <sys/sockio.h>
- #endif
- ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
- #include "asterisk/channel.h"
- #include "asterisk/file.h"
- #include "asterisk/paths.h"
- #include "asterisk/pbx.h"
- #include "asterisk/cli.h"
- #include "asterisk/module.h"
- #include "asterisk/http.h"
- #include "asterisk/utils.h"
- #include "asterisk/app.h"
- #include "asterisk/strings.h"
- #include "asterisk/stringfields.h"
- #include "asterisk/options.h"
- #include "asterisk/config.h"
- #include "asterisk/acl.h"
- #include "asterisk/astobj2.h"
- #include "asterisk/ast_version.h"
- #include "asterisk/phoneprov.h"
- #ifdef LOW_MEMORY
- #define MAX_PROVIDER_BUCKETS 1
- #define MAX_PROFILE_BUCKETS 1
- #define MAX_ROUTE_BUCKETS 1
- #define MAX_USER_BUCKETS 1
- #else
- #define MAX_PROVIDER_BUCKETS 17
- #define MAX_PROFILE_BUCKETS 17
- #define MAX_ROUTE_BUCKETS 563
- #define MAX_USER_BUCKETS 563
- #endif /* LOW_MEMORY */
- #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_file" 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(<item><fn>%{DISPLAY_NAME}</fn></item>|${MAC})</para>
- </description>
- </function>
- ***/
- /*!
- * \brief Creates a hash function for a structure string field.
- * \param fname The name to use for the function
- * \param stype The structure type
- * \param field The field in the structure to hash
- *
- * SIMPLE_HASH_FN(mystruct, myfield) will produce a function
- * named mystruct_hash_fn which hashes mystruct->myfield.
- */
- #define SIMPLE_HASH_FN(fname, stype, field) \
- static int fname(const void *obj, const int flags) \
- { \
- const struct stype *provider = obj; \
- const char *key; \
- switch (flags & OBJ_SEARCH_MASK) { \
- case OBJ_SEARCH_KEY: \
- key = obj; \
- break; \
- case OBJ_SEARCH_OBJECT: \
- provider = obj; \
- key = provider->field; \
- break; \
- default: \
- ast_assert(0); \
- return 0; \
- } \
- return ast_str_hash(key); \
- }
- /*!
- * \brief Creates a compare function for a structure string field.
- * \param fname The name to use for the function
- * \param stype The structure type
- * \param field The field in the structure to compare
- *
- * SIMPLE_CMP_FN(mystruct, myfield) will produce a function
- * named mystruct_cmp_fn which compares mystruct->myfield.
- */
- #define SIMPLE_CMP_FN(fname, stype, field) \
- static int fname(void *obj, void *arg, int flags) \
- { \
- const struct stype *object_left = obj, *object_right = arg; \
- const char *right_key = arg; \
- int cmp; \
- switch (flags & OBJ_SEARCH_MASK) { \
- case OBJ_SEARCH_OBJECT: \
- right_key = object_right->field; \
- case OBJ_SEARCH_KEY: \
- cmp = strcmp(object_left->field, right_key); \
- break; \
- case OBJ_SEARCH_PARTIAL_KEY: \
- cmp = strncmp(object_left->field, right_key, strlen(right_key)); \
- break; \
- default: \
- cmp = 0; \
- break; \
- } \
- if (cmp) { \
- return 0; \
- } \
- return CMP_MATCH; \
- }
- static const char *variable_lookup[] = {
- [AST_PHONEPROV_STD_MAC] = "MAC",
- [AST_PHONEPROV_STD_PROFILE] = "PROFILE",
- [AST_PHONEPROV_STD_USERNAME] = "USERNAME",
- [AST_PHONEPROV_STD_DISPLAY_NAME] = "DISPLAY_NAME",
- [AST_PHONEPROV_STD_SECRET] = "SECRET",
- [AST_PHONEPROV_STD_LABEL] = "LABEL",
- [AST_PHONEPROV_STD_CALLERID] = "CALLERID",
- [AST_PHONEPROV_STD_TIMEZONE] = "TIMEZONE",
- [AST_PHONEPROV_STD_LINENUMBER] = "LINE",
- [AST_PHONEPROV_STD_LINEKEYS] = "LINEKEYS",
- [AST_PHONEPROV_STD_SERVER] = "SERVER",
- [AST_PHONEPROV_STD_SERVER_PORT] = "SERVER_PORT",
- [AST_PHONEPROV_STD_SERVER_IFACE] = "SERVER_IFACE",
- [AST_PHONEPROV_STD_VOICEMAIL_EXTEN] = "VOICEMAIL_EXTEN",
- [AST_PHONEPROV_STD_EXTENSION_LENGTH] = "EXTENSION_LENGTH",
- [AST_PHONEPROV_STD_TZOFFSET] = "TZOFFSET",
- [AST_PHONEPROV_STD_DST_ENABLE] = "DST_ENABLE",
- [AST_PHONEPROV_STD_DST_START_MONTH] = "DST_START_MONTH",
- [AST_PHONEPROV_STD_DST_START_MDAY] = "DST_START_MDAY",
- [AST_PHONEPROV_STD_DST_START_HOUR] = "DST_START_HOUR",
- [AST_PHONEPROV_STD_DST_END_MONTH] = "DST_END_MONTH",
- [AST_PHONEPROV_STD_DST_END_MDAY] = "DST_END_MDAY",
- [AST_PHONEPROV_STD_DST_END_HOUR] = "DST_END_HOUR",
- };
- /* Translate the standard variables to their users.conf equivalents. */
- static const char *pp_user_lookup[] = {
- [AST_PHONEPROV_STD_MAC] = "macaddress",
- [AST_PHONEPROV_STD_PROFILE] = "profile",
- [AST_PHONEPROV_STD_USERNAME] = "username",
- [AST_PHONEPROV_STD_DISPLAY_NAME] = "fullname",
- [AST_PHONEPROV_STD_SECRET] = "secret",
- [AST_PHONEPROV_STD_LABEL] = "label",
- [AST_PHONEPROV_STD_CALLERID] = "cid_number",
- [AST_PHONEPROV_STD_TIMEZONE] = "timezone",
- [AST_PHONEPROV_STD_LINENUMBER] = "linenumber",
- [AST_PHONEPROV_STD_LINEKEYS] = "linekeys",
- [AST_PHONEPROV_STD_SERVER] = NULL,
- [AST_PHONEPROV_STD_SERVER_PORT] = NULL,
- [AST_PHONEPROV_STD_SERVER_IFACE] = NULL,
- [AST_PHONEPROV_STD_VOICEMAIL_EXTEN] = "vmexten",
- [AST_PHONEPROV_STD_EXTENSION_LENGTH] = "localextenlength",
- [AST_PHONEPROV_STD_TZOFFSET] = NULL,
- [AST_PHONEPROV_STD_DST_ENABLE] = NULL,
- [AST_PHONEPROV_STD_DST_START_MONTH] = NULL,
- [AST_PHONEPROV_STD_DST_START_MDAY] = NULL,
- [AST_PHONEPROV_STD_DST_START_HOUR] = NULL,
- [AST_PHONEPROV_STD_DST_END_MONTH] = NULL,
- [AST_PHONEPROV_STD_DST_END_MDAY] = NULL,
- [AST_PHONEPROV_STD_DST_END_HOUR] = NULL,
- };
- /* Translate the standard variables to their phoneprov.conf [general] equivalents. */
- static const char *pp_general_lookup[] = {
- [AST_PHONEPROV_STD_MAC] = NULL,
- [AST_PHONEPROV_STD_PROFILE] = "default_profile",
- [AST_PHONEPROV_STD_USERNAME] = NULL,
- [AST_PHONEPROV_STD_DISPLAY_NAME] = NULL,
- [AST_PHONEPROV_STD_SECRET] = NULL,
- [AST_PHONEPROV_STD_LABEL] = NULL,
- [AST_PHONEPROV_STD_CALLERID] = NULL,
- [AST_PHONEPROV_STD_TIMEZONE] = NULL,
- [AST_PHONEPROV_STD_LINENUMBER] = NULL,
- [AST_PHONEPROV_STD_LINEKEYS] = NULL,
- [AST_PHONEPROV_STD_SERVER] = "serveraddr",
- [AST_PHONEPROV_STD_SERVER_PORT] = "serverport",
- [AST_PHONEPROV_STD_SERVER_IFACE] = "serveriface",
- [AST_PHONEPROV_STD_VOICEMAIL_EXTEN] = NULL,
- [AST_PHONEPROV_STD_EXTENSION_LENGTH] = NULL,
- [AST_PHONEPROV_STD_TZOFFSET] = NULL,
- [AST_PHONEPROV_STD_DST_ENABLE] = NULL,
- [AST_PHONEPROV_STD_DST_START_MONTH] = NULL,
- [AST_PHONEPROV_STD_DST_START_MDAY] = NULL,
- [AST_PHONEPROV_STD_DST_START_HOUR] = NULL,
- [AST_PHONEPROV_STD_DST_END_MONTH] = NULL,
- [AST_PHONEPROV_STD_DST_END_MDAY] = NULL,
- [AST_PHONEPROV_STD_DST_END_HOUR] = NULL,
- };
- /*! \brief for use in lookup_iface */
- static struct in_addr __ourip = { .s_addr = 0x00000000, };
- /*! \brief structure to hold config providers */
- struct phoneprov_provider {
- AST_DECLARE_STRING_FIELDS(
- AST_STRING_FIELD(provider_name);
- );
- ast_phoneprov_load_users_cb load_users;
- };
- struct ao2_container *providers;
- SIMPLE_HASH_FN(phoneprov_provider_hash_fn, phoneprov_provider, provider_name)
- SIMPLE_CMP_FN(phoneprov_provider_cmp_fn, phoneprov_provider, provider_name)
- /*! \brief structure to hold file data */
- struct phoneprov_file {
- AST_DECLARE_STRING_FIELDS(
- AST_STRING_FIELD(format); /*!< After variable substitution, becomes route->uri */
- AST_STRING_FIELD(template); /*!< Template/physical file location */
- AST_STRING_FIELD(mime_type);/*!< Mime-type of the file */
- );
- AST_LIST_ENTRY(phoneprov_file) entry;
- };
- /*! \brief structure to hold extensions */
- struct extension {
- AST_DECLARE_STRING_FIELDS(
- AST_STRING_FIELD(name);
- );
- int index;
- struct varshead *headp; /*!< List of variables to substitute into templates */
- AST_LIST_ENTRY(extension) entry;
- };
- /*! \brief structure to hold phone profiles read from phoneprov.conf */
- struct phone_profile {
- AST_DECLARE_STRING_FIELDS(
- AST_STRING_FIELD(name); /*!< Name of phone profile */
- AST_STRING_FIELD(default_mime_type); /*!< Default mime type if it isn't provided */
- AST_STRING_FIELD(staticdir); /*!< Subdirectory that static files are stored in */
- );
- struct varshead *headp; /*!< List of variables set with 'setvar' in phoneprov.conf */
- AST_LIST_HEAD_NOLOCK(, phoneprov_file) static_files; /*!< List of static files */
- AST_LIST_HEAD_NOLOCK(, phoneprov_file) dynamic_files; /*!< List of dynamic files */
- };
- struct ao2_container *profiles;
- SIMPLE_HASH_FN(phone_profile_hash_fn, phone_profile, name)
- SIMPLE_CMP_FN(phone_profile_cmp_fn, phone_profile, name)
- /*! \brief structure to hold users read from users.conf */
- struct user {
- AST_DECLARE_STRING_FIELDS(
- AST_STRING_FIELD(macaddress); /*!< Mac address of user's phone */
- AST_STRING_FIELD(provider_name); /*!< Name of the provider who registered this mac */
- );
- struct phone_profile *profile; /*!< Profile the phone belongs to */
- AST_LIST_HEAD_NOLOCK(, extension) extensions;
- };
- struct ao2_container *users;
- SIMPLE_HASH_FN(user_hash_fn, user, macaddress)
- SIMPLE_CMP_FN(user_cmp_fn, user, macaddress)
- /*! \brief structure to hold http routes (valid URIs, and the files they link to) */
- struct http_route {
- AST_DECLARE_STRING_FIELDS(
- AST_STRING_FIELD(uri); /*!< The URI requested */
- );
- struct phoneprov_file *file; /*!< The file that links to the URI */
- struct user *user; /*!< The user that has variables to substitute into the file
- * NULL in the case of a static route */
- struct phone_profile *profile;
- };
- struct ao2_container *http_routes;
- SIMPLE_HASH_FN(http_route_hash_fn, http_route, uri)
- SIMPLE_CMP_FN(http_route_cmp_fn, http_route, uri)
- #define SIPUSERS_PROVIDER_NAME "sipusers"
- /* iface is the interface (e.g. eth0); address is the return value */
- static int lookup_iface(const char *iface, struct in_addr *address)
- {
- int mysock, res = 0;
- struct ifreq ifr;
- struct sockaddr_in *sin;
- memset(&ifr, 0, sizeof(ifr));
- ast_copy_string(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
- mysock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
- if (mysock < 0) {
- ast_log(LOG_ERROR, "Failed to create socket: %s\n", strerror(errno));
- return -1;
- }
- res = ioctl(mysock, SIOCGIFADDR, &ifr);
- close(mysock);
- if (res < 0) {
- ast_log(LOG_WARNING, "Unable to get IP of %s: %s\n", iface, strerror(errno));
- memcpy(address, &__ourip, sizeof(__ourip));
- return -1;
- } else {
- sin = (struct sockaddr_in *)&ifr.ifr_addr;
- memcpy(address, &sin->sin_addr, sizeof(*address));
- return 0;
- }
- }
- static struct phoneprov_provider *find_provider(char *name)
- {
- return ao2_find(providers, name, OBJ_SEARCH_KEY);
- }
- /*! \brief Delete all providers */
- static void delete_providers(void)
- {
- if (!providers) {
- return;
- }
- ao2_callback(providers, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
- }
- static void provider_destructor(void *obj)
- {
- struct phoneprov_provider *provider = obj;
- ast_string_field_free_memory(provider);
- }
- static void delete_file(struct phoneprov_file *file)
- {
- ast_string_field_free_memory(file);
- free(file);
- }
- /*! \brief Read a TEXT file into a string and return the length */
- static int load_file(const char *filename, char **ret)
- {
- int len = 0;
- FILE *f;
- if (!(f = fopen(filename, "r"))) {
- *ret = NULL;
- return -1;
- }
- fseek(f, 0, SEEK_END);
- len = ftell(f);
- fseek(f, 0, SEEK_SET);
- if (!(*ret = ast_malloc(len + 1))) {
- fclose(f);
- return -2;
- }
- if (len != fread(*ret, sizeof(char), len, f)) {
- fclose(f);
- free(*ret);
- *ret = NULL;
- return -3;
- }
- fclose(f);
- (*ret)[len] = '\0';
- return len;
- }
- /*! \brief Set all timezone-related variables based on a zone (i.e. America/New_York)
- \param headp pointer to list of user variables
- \param zone A time zone. NULL sets variables based on timezone of the machine
- */
- static void set_timezone_variables(struct varshead *headp, const char *zone)
- {
- time_t utc_time;
- int dstenable;
- time_t dststart;
- time_t dstend;
- struct ast_tm tm_info;
- int tzoffset;
- char buffer[21];
- struct timeval when;
- time(&utc_time);
- ast_get_dst_info(&utc_time, &dstenable, &dststart, &dstend, &tzoffset, zone);
- snprintf(buffer, sizeof(buffer), "%d", tzoffset);
- AST_VAR_LIST_INSERT_TAIL(headp, ast_var_assign("TZOFFSET", buffer));
- if (!dstenable) {
- return;
- }
- AST_VAR_LIST_INSERT_TAIL(headp, ast_var_assign("DST_ENABLE", "1"));
- when.tv_sec = dststart;
- ast_localtime(&when, &tm_info, zone);
- snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_mon+1);
- AST_VAR_LIST_INSERT_TAIL(headp, ast_var_assign("DST_START_MONTH", buffer));
- snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_mday);
- AST_VAR_LIST_INSERT_TAIL(headp, ast_var_assign("DST_START_MDAY", buffer));
- snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_hour);
- AST_VAR_LIST_INSERT_TAIL(headp, ast_var_assign("DST_START_HOUR", buffer));
- when.tv_sec = dstend;
- ast_localtime(&when, &tm_info, zone);
- snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_mon + 1);
- AST_VAR_LIST_INSERT_TAIL(headp, ast_var_assign("DST_END_MONTH", buffer));
- snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_mday);
- AST_VAR_LIST_INSERT_TAIL(headp, ast_var_assign("DST_END_MDAY", buffer));
- snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_hour);
- AST_VAR_LIST_INSERT_TAIL(headp, ast_var_assign("DST_END_HOUR", buffer));
- }
- static struct http_route *unref_route(struct http_route *route)
- {
- ao2_cleanup(route);
- return NULL;
- }
- static void route_destructor(void *obj)
- {
- struct http_route *route = obj;
- ast_string_field_free_memory(route);
- }
- /*! \brief Delete all http routes, freeing their memory */
- static void delete_routes(void)
- {
- if (!http_routes) {
- return;
- }
- ao2_callback(http_routes, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
- }
- /*! \brief Build a route structure and add it to the list of available http routes
- \param pp_file File to link to the route
- \param user User to link to the route (NULL means static route)
- \param uri URI of the route
- */
- static void build_route(struct phoneprov_file *pp_file, struct phone_profile *profile, struct user *user, char *uri)
- {
- struct http_route *route;
- if (!(route = ao2_alloc(sizeof(*route), route_destructor))) {
- return;
- }
- if (ast_string_field_init(route, 32)) {
- ast_log(LOG_ERROR, "Couldn't create string fields for %s\n", pp_file->format);
- route = unref_route(route);
- return;
- }
- ast_string_field_set(route, uri, S_OR(uri, pp_file->format));
- route->user = user;
- route->file = pp_file;
- route->profile = profile;
- ao2_link(http_routes, route);
- route = unref_route(route);
- }
- static struct phone_profile *unref_profile(struct phone_profile *prof)
- {
- ao2_cleanup(prof);
- return NULL;
- }
- /*! \brief Return a phone profile looked up by name */
- static struct phone_profile *find_profile(const char *name)
- {
- return ao2_find(profiles, name, OBJ_SEARCH_KEY);
- }
- static void profile_destructor(void *obj)
- {
- struct phone_profile *profile = obj;
- struct phoneprov_file *file;
- struct ast_var_t *var;
- while ((file = AST_LIST_REMOVE_HEAD(&profile->static_files, entry))) {
- delete_file(file);
- }
- while ((file = AST_LIST_REMOVE_HEAD(&profile->dynamic_files, entry))) {
- delete_file(file);
- }
- while ((var = AST_LIST_REMOVE_HEAD(profile->headp, entries))) {
- ast_var_delete(var);
- }
- ast_free(profile->headp);
- ast_string_field_free_memory(profile);
- }
- /*! \brief Delete all phone profiles, freeing their memory */
- static void delete_profiles(void)
- {
- if (!profiles) {
- return;
- }
- ao2_callback(profiles, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
- }
- /*! \brief Build a phone profile and add it to the list of phone profiles
- \param name the name of the profile
- \param v ast_variable from parsing phoneprov.conf
- */
- static void build_profile(const char *name, struct ast_variable *v)
- {
- struct phone_profile *profile;
- if (!(profile = ao2_alloc(sizeof(*profile), profile_destructor))) {
- return;
- }
- if (ast_string_field_init(profile, 32)) {
- profile = unref_profile(profile);
- return;
- }
- if (!(profile->headp = ast_var_list_create())) {
- profile = unref_profile(profile);
- return;
- }
- AST_LIST_HEAD_INIT_NOLOCK(&profile->static_files);
- AST_LIST_HEAD_INIT_NOLOCK(&profile->dynamic_files);
- ast_string_field_set(profile, name, name);
- for (; v; v = v->next) {
- if (!strcasecmp(v->name, "mime_type")) {
- ast_string_field_set(profile, default_mime_type, v->value);
- } else if (!strcasecmp(v->name, "setvar")) {
- char *value_copy = ast_strdupa(v->value);
- AST_DECLARE_APP_ARGS(args,
- 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))
- break;
- args.varname = ast_strip(args.varname);
- args.varval = ast_strip(args.varval);
- if (ast_strlen_zero(args.varname) || ast_strlen_zero(args.varval))
- break;
- AST_VAR_LIST_INSERT_TAIL(profile->headp, ast_var_assign(args.varname, args.varval));
- } while (0);
- } else if (!strcasecmp(v->name, "staticdir")) {
- ast_string_field_set(profile, staticdir, v->value);
- } else {
- struct phoneprov_file *pp_file;
- char *file_extension;
- 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_with_stringfields(1, struct phoneprov_file, 32))) {
- profile = unref_profile(profile);
- return;
- }
- if ((file_extension = strrchr(pp_file->format, '.')))
- file_extension++;
- AST_STANDARD_APP_ARGS(args, value_copy);
- /* Mime type order of preference
- * 1) Specific mime-type defined for file in profile
- * 2) Mime determined by extension
- * 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(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);
- ast_string_field_build(pp_file, template, "%s%s", profile->staticdir, args.filename);
- AST_LIST_INSERT_TAIL(&profile->static_files, pp_file, entry);
- /* Add a route for the static files, as their filenames won't change per-user */
- build_route(pp_file, profile, NULL, NULL);
- } else {
- ast_string_field_set(pp_file, format, v->name);
- ast_string_field_set(pp_file, template, args.filename);
- AST_LIST_INSERT_TAIL(&profile->dynamic_files, pp_file, entry);
- }
- }
- }
- ao2_link(profiles, profile);
- profile = unref_profile(profile);
- }
- static struct extension *delete_extension(struct extension *exten)
- {
- ast_var_list_destroy(exten->headp);
- ast_string_field_free_memory(exten);
- ast_free(exten);
- return NULL;
- }
- static struct extension *build_extension(const char *name, struct varshead *vars)
- {
- struct extension *exten;
- const char *tmp;
- if (!(exten = ast_calloc_with_stringfields(1, struct extension, 32))) {
- return NULL;
- }
- ast_string_field_set(exten, name, name);
- exten->headp = ast_var_list_clone(vars);
- if (!exten->headp) {
- ast_log(LOG_ERROR, "Unable to clone variables for extension '%s'\n", name);
- delete_extension(exten);
- return NULL;
- }
- tmp = ast_var_find(exten->headp, variable_lookup[AST_PHONEPROV_STD_LINENUMBER]);
- if (!tmp) {
- AST_VAR_LIST_INSERT_TAIL(exten->headp,
- ast_var_assign(variable_lookup[AST_PHONEPROV_STD_LINENUMBER], "1"));
- exten->index = 1;
- } else {
- sscanf(tmp, "%d", &exten->index);
- }
- if (!ast_var_find(exten->headp, variable_lookup[AST_PHONEPROV_STD_LINEKEYS])) {
- AST_VAR_LIST_INSERT_TAIL(exten->headp,
- ast_var_assign(variable_lookup[AST_PHONEPROV_STD_LINEKEYS], "1"));
- }
- set_timezone_variables(exten->headp,
- ast_var_find(vars, variable_lookup[AST_PHONEPROV_STD_TIMEZONE]));
- return exten;
- }
- static struct user *unref_user(struct user *user)
- {
- ao2_cleanup(user);
- return NULL;
- }
- /*! \brief Return a user looked up by name */
- static struct user *find_user(const char *macaddress)
- {
- return ao2_find(users, macaddress, OBJ_SEARCH_KEY);
- }
- static int routes_delete_cb(void *obj, void *arg, int flags)
- {
- struct http_route *route = obj;
- struct user *user = route->user;
- char *macaddress = arg;
- if (user && !strcmp(user->macaddress, macaddress)) {
- return CMP_MATCH;
- }
- return 0;
- }
- /*! \brief Free all memory associated with a user */
- static void user_destructor(void *obj)
- {
- struct user *user = obj;
- struct extension *exten;
- while ((exten = AST_LIST_REMOVE_HEAD(&user->extensions, entry))) {
- exten = delete_extension(exten);
- }
- if (user->profile) {
- user->profile = unref_profile(user->profile);
- }
- if (http_routes) {
- ao2_callback(http_routes, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, routes_delete_cb, (void *)user->macaddress);
- }
- ast_string_field_free_memory(user);
- }
- /*! \brief Delete all users */
- static void delete_users(void)
- {
- if (!users) {
- return;
- }
- ao2_callback(users, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
- }
- /*! \brief Build and return a user structure based on gathered config data */
- static struct user *build_user(const char *mac, struct phone_profile *profile, char *provider_name)
- {
- struct user *user;
- if (!(user = ao2_alloc(sizeof(*user), user_destructor))) {
- return NULL;
- }
- if (ast_string_field_init(user, 64)) {
- user = unref_user(user);
- return NULL;
- }
- ast_string_field_set(user, macaddress, mac);
- ast_string_field_set(user, provider_name, provider_name);
- user->profile = profile;
- ao2_ref(profile, 1);
- return user;
- }
- /*! \brief Add an extension to a user ordered by index/linenumber */
- static int add_user_extension(struct user *user, struct extension *exten)
- {
- struct ast_var_t *pvar, *var2;
- 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_VAR_LIST_TRAVERSE(user->profile->headp, pvar) {
- if (ast_var_find(exten->headp, pvar->name)) {
- continue;
- }
- ast_str_substitute_variables_varshead(&str, 0, exten->headp, pvar->value);
- if ((var2 = ast_var_assign(pvar->name, ast_str_buffer(str)))) {
- AST_VAR_LIST_INSERT_TAIL(exten->headp, var2);
- }
- }
- ast_free(str);
- if (AST_LIST_EMPTY(&user->extensions)) {
- AST_LIST_INSERT_HEAD(&user->extensions, exten, entry);
- } else {
- struct extension *exten_iter;
- AST_LIST_TRAVERSE_SAFE_BEGIN(&user->extensions, exten_iter, entry) {
- if (exten->index < exten_iter->index) {
- AST_LIST_INSERT_BEFORE_CURRENT(exten, entry);
- } else if (exten->index == exten_iter->index) {
- ast_log(LOG_WARNING, "Duplicate linenumber=%d for %s\n", exten->index, user->macaddress);
- return -1;
- } else if (!AST_LIST_NEXT(exten_iter, entry)) {
- AST_LIST_INSERT_TAIL(&user->extensions, exten, entry);
- }
- }
- AST_LIST_TRAVERSE_SAFE_END;
- }
- return 0;
- }
- /*! \brief Add an http route for dynamic files attached to the profile of the user */
- static int build_user_routes(struct user *user)
- {
- struct phoneprov_file *pp_file;
- struct ast_str *str;
- if (!(str = ast_str_create(16))) {
- return -1;
- }
- 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->profile, user, ast_str_buffer(str));
- }
- ast_free(str);
- return 0;
- }
- /*! \brief Callback that is executed everytime an http request is received by this module */
- 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 ast_str *result;
- char path[PATH_MAX];
- char *file = NULL;
- char *server;
- int len;
- int fd;
- 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 0;
- }
- if (!(route = ao2_find(http_routes, uri, OBJ_SEARCH_KEY))) {
- goto out404;
- }
- snprintf(path, sizeof(path), "%s/phoneprov/%s", ast_config_AST_DATA_DIR, route->file->template);
- if (!route->user) { /* Static file */
- fd = open(path, O_RDONLY);
- if (fd < 0) {
- goto out500;
- }
- len = lseek(fd, 0, SEEK_END);
- lseek(fd, 0, SEEK_SET);
- if (len < 0) {
- ast_log(LOG_WARNING, "Could not load file: %s (%d)\n", path, len);
- close(fd);
- goto out500;
- }
- 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 0;
- } else { /* Dynamic file */
- struct ast_str *tmp;
- len = load_file(path, &file);
- if (len < 0) {
- ast_log(LOG_WARNING, "Could not load file: %s (%d)\n", path, len);
- if (file) {
- ast_free(file);
- }
- goto out500;
- }
- if (!file) {
- goto out500;
- }
- if (!(tmp = ast_str_create(len))) {
- if (file) {
- ast_free(file);
- }
- goto out500;
- }
- /* 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 */
- server = ast_var_find(AST_LIST_FIRST(&route->user->extensions)->headp,
- variable_lookup[AST_PHONEPROV_STD_SERVER]);
- if (!server) {
- union {
- struct sockaddr sa;
- struct sockaddr_in sa_in;
- } name;
- socklen_t namelen = sizeof(name.sa);
- int res;
- if ((res = getsockname(ser->fd, &name.sa, &namelen))) {
- ast_log(LOG_WARNING, "Could not get server IP, breakage likely.\n");
- } else {
- struct extension *exten_iter;
- const char *newserver = ast_inet_ntoa(name.sa_in.sin_addr);
- AST_LIST_TRAVERSE(&route->user->extensions, exten_iter, entry) {
- AST_VAR_LIST_INSERT_TAIL(exten_iter->headp,
- ast_var_assign(variable_lookup[AST_PHONEPROV_STD_SERVER], newserver));
- }
- }
- }
- ast_str_substitute_variables_varshead(&tmp, 0, AST_LIST_FIRST(&route->user->extensions)->headp, file);
- ast_free(file);
- 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);
- ast_free(tmp);
- route = unref_route(route);
- return 0;
- }
- out404:
- ast_http_error(ser, 404, "Not Found", uri);
- return 0;
- out500:
- route = unref_route(route);
- ast_http_error(ser, 500, "Internal Error", "An internal error has occured.");
- return 0;
- }
- /*! \brief A dialplan function that can be used to print a string for each phoneprov user */
- static int pp_each_user_helper(struct ast_channel *chan, char *data, char *buf, struct ast_str **bufstr, int len)
- {
- 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 = '$';
- i = ao2_iterator_init(users, 0);
- while ((user = ao2_iterator_next(&i))) {
- if (!ast_strlen_zero(args.exclude_mac) && !strcasecmp(user->macaddress, args.exclude_mac)) {
- continue;
- }
- 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",
- .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_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);
- );
- AST_STANDARD_APP_ARGS(args, data);
- if (ast_strlen_zero(args.mac) || ast_strlen_zero(args.template)) {
- ast_log(LOG_WARNING, "PP_EACH_EXTENSION requries both a macaddress and template filename.\n");
- return 0;
- }
- if (!(user = find_user(args.mac))) {
- ast_log(LOG_WARNING, "Could not find user with mac = '%s'\n", args.mac);
- return 0;
- }
- snprintf(path, sizeof(path), "%s/phoneprov/%s", ast_config_AST_DATA_DIR, args.template);
- filelen = load_file(path, &file);
- if (filelen < 0) {
- ast_log(LOG_WARNING, "Could not load file: %s (%d)\n", path, filelen);
- if (file) {
- ast_free(file);
- }
- return 0;
- }
- if (!file) {
- return 0;
- }
- if (!(str = ast_str_create(filelen))) {
- return 0;
- }
- AST_LIST_TRAVERSE(&user->extensions, exten, entry) {
- 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",
- .read = pp_each_extension_read,
- .read2 = pp_each_extension_read2,
- };
- #define FORMATS "%-20.20s %-40.40s %-30.30s\n"
- #define FORMATD "%-20.20s %-20.20s %-40.40s %-30.30s\n"
- static int route_list_cb(void *obj, void *arg, void *data, int flags)
- {
- int fd = *(int *)arg;
- struct http_route *route = obj;
- if (data && route->user) {
- ast_cli(fd, FORMATD, route->user->provider_name, route->profile->name, route->uri, route->file->template);
- }
- if (!data && !route->user) {
- ast_cli(fd, FORMATS, route->profile->name, route->uri, route->file->template);
- }
- return CMP_MATCH;
- }
- /*! \brief CLI command to list static and dynamic routes */
- static char *handle_show_routes(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
- {
- int fd = a->fd;
- switch(cmd) {
- case CLI_INIT:
- e->command = "phoneprov show routes";
- e->usage =
- "Usage: phoneprov show routes\n"
- " Lists all registered phoneprov http routes.\n";
- return NULL;
- case CLI_GENERATE:
- return NULL;
- }
- /* This currently iterates over routes twice, but it is the only place I've needed
- * to really separate static an dynamic routes, so I've just left it this way. */
- ast_cli(a->fd, "Static routes\n\n");
- ast_cli(a->fd, FORMATS, "Profile", "Relative URI", "Physical location");
- ao2_callback_data(http_routes, OBJ_NODATA | OBJ_MULTIPLE, route_list_cb, &fd, NULL);
- ast_cli(a->fd, "\nDynamic routes\n\n");
- ast_cli(a->fd, FORMATD, "Provider", "Profile", "Relative URI", "Template");
- ao2_callback_data(http_routes, OBJ_NODATA | OBJ_MULTIPLE, route_list_cb, &fd, (void *)1);
- return CLI_SUCCESS;
- }
- static struct ast_cli_entry pp_cli[] = {
- AST_CLI_DEFINE(handle_show_routes, "Show registered phoneprov http routes"),
- };
- static struct ast_http_uri phoneprovuri = {
- .callback = phoneprov_callback,
- .description = "Asterisk HTTP Phone Provisioning Tool",
- .uri = "phoneprov",
- .has_subtree = 1,
- .data = NULL,
- .key = __FILE__,
- };
- static struct varshead *get_defaults(void)
- {
- struct ast_config *phoneprov_cfg, *cfg = CONFIG_STATUS_FILEINVALID;
- const char *value;
- struct ast_variable *v;
- struct ast_var_t *var;
- struct ast_flags config_flags = { 0 };
- struct varshead *defaults = ast_var_list_create();
- if (!defaults) {
- ast_log(LOG_ERROR, "Unable to create default var list.\n");
- return NULL;
- }
- 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_var_list_destroy(defaults);
- return NULL;
- }
- value = ast_variable_retrieve(phoneprov_cfg, "general", pp_general_lookup[AST_PHONEPROV_STD_SERVER]);
- if (!value) {
- struct in_addr addr;
- value = ast_variable_retrieve(phoneprov_cfg, "general", pp_general_lookup[AST_PHONEPROV_STD_SERVER_IFACE]);
- if (value) {
- lookup_iface(value, &addr);
- value = ast_inet_ntoa(addr);
- }
- }
- if (value) {
- var = ast_var_assign(variable_lookup[AST_PHONEPROV_STD_SERVER], value);
- AST_VAR_LIST_INSERT_TAIL(defaults, var);
- } else {
- ast_log(LOG_WARNING, "Unable to find a valid server address or name.\n");
- }
- value = ast_variable_retrieve(phoneprov_cfg, "general", pp_general_lookup[AST_PHONEPROV_STD_SERVER_PORT]);
- if (!value) {
- if ((cfg = ast_config_load("sip.conf", config_flags)) && cfg != CONFIG_STATUS_FILEINVALID) {
- value = ast_variable_retrieve(cfg, "general", "bindport");
- }
- }
- var = ast_var_assign(variable_lookup[AST_PHONEPROV_STD_SERVER_PORT], S_OR(value, "5060"));
- if(cfg && cfg != CONFIG_STATUS_FILEINVALID) {
- ast_config_destroy(cfg);
- }
- AST_VAR_LIST_INSERT_TAIL(defaults, var);
- value = ast_variable_retrieve(phoneprov_cfg, "general", pp_general_lookup[AST_PHONEPROV_STD_PROFILE]);
- if (!value) {
- ast_log(LOG_ERROR, "Unable to load default profile.\n");
- ast_config_destroy(phoneprov_cfg);
- ast_var_list_destroy(defaults);
- return NULL;
- }
- var = ast_var_assign(variable_lookup[AST_PHONEPROV_STD_PROFILE], value);
- AST_VAR_LIST_INSERT_TAIL(defaults, var);
- ast_config_destroy(phoneprov_cfg);
- if (!(cfg = ast_config_load("users.conf", config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
- ast_log(LOG_ERROR, "Unable to load users.conf\n");
- ast_var_list_destroy(defaults);
- return NULL;
- }
- /* Go ahead and load global variables from users.conf so we can append to profiles */
- for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
- if (!strcasecmp(v->name, pp_user_lookup[AST_PHONEPROV_STD_VOICEMAIL_EXTEN])) {
- var = ast_var_assign(variable_lookup[AST_PHONEPROV_STD_VOICEMAIL_EXTEN], v->value);
- AST_VAR_LIST_INSERT_TAIL(defaults, var);
- }
- if (!strcasecmp(v->name, pp_user_lookup[AST_PHONEPROV_STD_EXTENSION_LENGTH])) {
- var = ast_var_assign(variable_lookup[AST_PHONEPROV_STD_EXTENSION_LENGTH], v->value);
- AST_VAR_LIST_INSERT_TAIL(defaults, var);
- }
- }
- ast_config_destroy(cfg);
- return defaults;
- }
- static int load_users(void)
- {
- struct ast_config *cfg;
- char *cat;
- const char *value;
- struct ast_flags config_flags = { 0 };
- struct varshead *defaults = get_defaults();
- if (!defaults) {
- ast_log(LOG_WARNING, "Unable to load default variables.\n");
- return -1;
- }
- if (!(cfg = ast_config_load("users.conf", config_flags))
- || cfg == CONFIG_STATUS_FILEINVALID) {
- ast_log(LOG_WARNING, "Unable to load users.conf\n");
- ast_var_list_destroy(defaults);
- return -1;
- }
- cat = NULL;
- while ((cat = ast_category_browse(cfg, cat))) {
- const char *tmp;
- int i;
- struct ast_var_t *varx;
- struct ast_var_t *vard;
- if (strcasecmp(cat, "general") && strcasecmp(cat, "authentication")) {
- struct varshead *variables = ast_var_list_create();
- if (!((tmp = ast_variable_retrieve(cfg, cat, "autoprov")) && ast_true(tmp))) {
- ast_var_list_destroy(variables);
- continue;
- }
- /* Transfer the standard variables */
- for (i = 0; i < AST_PHONEPROV_STD_VAR_LIST_LENGTH; i++) {
- if (pp_user_lookup[i]) {
- value = ast_variable_retrieve(cfg, cat, pp_user_lookup[i]);
- if (value) {
- varx = ast_var_assign(variable_lookup[i],
- value);
- AST_VAR_LIST_INSERT_TAIL(variables, varx);
- }
- }
- }
- if (!ast_var_find(variables, variable_lookup[AST_PHONEPROV_STD_MAC])) {
- ast_log(LOG_WARNING, "autoprov set for %s, but no mac address - skipping.\n", cat);
- ast_var_list_destroy(variables);
- continue;
- }
- /* Apply defaults */
- AST_VAR_LIST_TRAVERSE(defaults, vard) {
- if (ast_var_find(variables, vard->name)) {
- continue;
- }
- varx = ast_var_assign(vard->name, vard->value);
- AST_VAR_LIST_INSERT_TAIL(variables, varx);
- }
- ast_phoneprov_add_extension(SIPUSERS_PROVIDER_NAME, variables);
- }
- }
- ast_config_destroy(cfg);
- ast_var_list_destroy(defaults);
- return 0;
- }
- static int load_common(void)
- {
- struct ast_config *phoneprov_cfg;
- struct ast_flags config_flags = { 0 };
- char *cat;
- 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");
- return -1;
- }
- cat = NULL;
- while ((cat = ast_category_browse(phoneprov_cfg, cat))) {
- if (!strcasecmp(cat, "general")) {
- continue;
- }
- build_profile(cat, ast_variable_browse(phoneprov_cfg, cat));
- }
- ast_config_destroy(phoneprov_cfg);
- if (!ao2_container_count(profiles)) {
- ast_log(LOG_ERROR, "There are no provisioning profiles in phoneprov.conf.\n");
- return -1;
- }
- return 0;
- }
- static int unload_module(void)
- {
- ast_http_uri_unlink(&phoneprovuri);
- ast_custom_function_unregister(&pp_each_user_function);
- ast_custom_function_unregister(&pp_each_extension_function);
- ast_cli_unregister_multiple(pp_cli, ARRAY_LEN(pp_cli));
- /* This cleans up the sip.conf/users.conf provider (called specifically for clarity) */
- ast_phoneprov_provider_unregister(SIPUSERS_PROVIDER_NAME);
- /* This cleans up the framework which also cleans up the providers. */
- delete_profiles();
- ao2_cleanup(profiles);
- profiles = NULL;
- delete_routes();
- delete_users();
- ao2_cleanup(http_routes);
- http_routes = NULL;
- ao2_cleanup(users);
- users = NULL;
- delete_providers();
- ao2_cleanup(providers);
- providers = NULL;
- return 0;
- }
- /*!
- * \brief Load the module
- *
- * Module loading including tests for configuration or dependencies.
- * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
- * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
- * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
- * configuration file or other non-critical problem return
- * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
- */
- static int load_module(void)
- {
- profiles = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, MAX_PROFILE_BUCKETS,
- phone_profile_hash_fn, NULL, phone_profile_cmp_fn);
- if (!profiles) {
- ast_log(LOG_ERROR, "Unable to allocate profiles container.\n");
- return AST_MODULE_LOAD_DECLINE;
- }
- http_routes = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, MAX_ROUTE_BUCKETS,
- http_route_hash_fn, NULL, http_route_cmp_fn);
- if (!http_routes) {
- ast_log(LOG_ERROR, "Unable to allocate routes container.\n");
- goto error;
- }
- if (load_common()) {
- ast_log(LOG_ERROR, "Unable to load provisioning profiles.\n");
- goto error;
- }
- users = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, MAX_USER_BUCKETS,
- user_hash_fn, NULL, user_cmp_fn);
- if (!users) {
- ast_log(LOG_ERROR, "Unable to allocate users container.\n");
- goto error;
- }
- providers = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
- MAX_PROVIDER_BUCKETS, phoneprov_provider_hash_fn, NULL, phoneprov_provider_cmp_fn);
- if (!providers) {
- ast_log(LOG_ERROR, "Unable to allocate providers container.\n");
- goto error;
- }
- /* Register ourselves as the provider for sip.conf/users.conf */
- if (ast_phoneprov_provider_register(SIPUSERS_PROVIDER_NAME, load_users)) {
- ast_log(LOG_WARNING, "Unable register sip/users config provider. Others may succeed.\n");
- }
- ast_http_uri_link(&phoneprovuri);
- 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));
- return AST_MODULE_LOAD_SUCCESS;
- error:
- unload_module();
- return AST_MODULE_LOAD_DECLINE;
- }
- static int reload(void)
- {
- struct ao2_iterator i;
- struct phoneprov_provider *provider;
- /* Clean everything except the providers */
- delete_routes();
- delete_users();
- delete_profiles();
- /* Reload the profiles */
- if (load_common()) {
- ast_log(LOG_ERROR, "Unable to reload provisioning profiles.\n");
- unload_module();
- return AST_MODULE_LOAD_DECLINE;
- }
- /* For each provider, reload the users */
- ao2_lock(providers);
- i = ao2_iterator_init(providers, 0);
- for(; (provider = ao2_iterator_next(&i)); ao2_ref(provider, -1)) {
- if (provider->load_users()) {
- ast_log(LOG_ERROR, "Unable to load provider '%s' users. Reload aborted.\n", provider->provider_name);
- continue;
- }
- }
- ao2_iterator_destroy(&i);
- ao2_unlock(providers);
- return AST_MODULE_LOAD_SUCCESS;
- }
- AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",
- .support_level = AST_MODULE_SUPPORT_EXTENDED,
- .load = load_module,
- .unload = unload_module,
- .reload = reload,
- .load_pri = AST_MODPRI_CHANNEL_DEPEND,
- );
- /**** Public API for register/unregister, set defaults, and add extension. ****/
- const char *ast_phoneprov_std_variable_lookup(enum ast_phoneprov_std_variables var)
- {
- if (var >= AST_PHONEPROV_STD_VAR_LIST_LENGTH) {
- return NULL;
- }
- return variable_lookup[var];
- }
- int ast_phoneprov_provider_register(char *provider_name,
- ast_phoneprov_load_users_cb load_users)
- {
- struct phoneprov_provider *provider;
- if (ast_strlen_zero(provider_name)) {
- ast_log(LOG_ERROR, "Provider name can't be empty.\n");
- return -1;
- }
- if (!providers) {
- ast_log(LOG_WARNING, "Provider '%s' cannot be registered: res_phoneprov not loaded.\n", provider_name);
- return -1;
- }
- provider = find_provider(provider_name);
- if (provider) {
- ast_log(LOG_ERROR, "There is already a provider registered named '%s'.\n", provider_name);
- ao2_ref(provider, -1);
- return -1;
- }
- provider = ao2_alloc(sizeof(struct phoneprov_provider), provider_destructor);
- if (!provider) {
- ast_log(LOG_ERROR, "Unable to allocate sufficient memory for provider '%s'.\n", provider_name);
- return -1;
- }
- if (ast_string_field_init(provider, 32)) {
- ao2_ref(provider, -1);
- ast_log(LOG_ERROR, "Unable to allocate sufficient memory for provider '%s' stringfields.\n", provider_name);
- return -1;
- }
- ast_string_field_set(provider, provider_name, provider_name);
- provider->load_users = load_users;
- ao2_link(providers, provider);
- ao2_ref(provider, -1);
- if (provider->load_users()) {
- ast_log(LOG_ERROR, "Unable to load provider '%s' users. Register aborted.\n", provider_name);
- ast_phoneprov_provider_unregister(provider_name);
- return -1;
- }
- return 0;
- }
- static int extensions_delete_cb(void *obj, void *arg, int flags)
- {
- char *provider_name = arg;
- struct user *user = obj;
- if (strcmp(user->provider_name, provider_name)) {
- return 0;
- }
- return CMP_MATCH;
- }
- static int extension_delete_cb(void *obj, void *arg, void *data, int flags)
- {
- struct user *user = obj;
- char *provider_name = data;
- char *macaddress = arg;
- if (!strcmp(user->provider_name, provider_name) && !strcasecmp(user->macaddress, macaddress)) {
- return CMP_MATCH;
- }
- return 0;
- }
- void ast_phoneprov_delete_extension(char *provider_name, char *macaddress)
- {
- if (!users) {
- return;
- }
- ao2_callback_data(users, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE | OBJ_SEARCH_KEY,
- extension_delete_cb, macaddress, provider_name);
- }
- void ast_phoneprov_delete_extensions(char *provider_name)
- {
- if (!users) {
- return;
- }
- ao2_callback(users, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, extensions_delete_cb, provider_name);
- }
- void ast_phoneprov_provider_unregister(char *provider_name)
- {
- if (!providers) {
- return;
- }
- ast_phoneprov_delete_extensions(provider_name);
- ao2_find(providers, provider_name, OBJ_SEARCH_KEY | OBJ_NODATA | OBJ_UNLINK);
- }
- int ast_phoneprov_add_extension(char *provider_name, struct varshead *vars)
- {
- RAII_VAR(struct phoneprov_provider *, provider, NULL, ao2_cleanup);
- RAII_VAR(struct user *, user, NULL, ao2_cleanup);
- RAII_VAR(struct phone_profile *, profile, NULL, ao2_cleanup);
- struct extension *exten;
- char *profile_name;
- char *mac;
- char *username;
- if (ast_strlen_zero(provider_name)) {
- ast_log(LOG_ERROR, "Provider name can't be empty.\n");
- return -1;
- }
- if (!vars) {
- ast_log(LOG_ERROR, "Variable list can't be empty.\n");
- return -1;
- }
- username = ast_var_find(vars, variable_lookup[AST_PHONEPROV_STD_USERNAME]);
- if (!username) {
- ast_log(LOG_ERROR, "Extension name can't be empty.\n");
- return -1;
- }
- mac = ast_var_find(vars, variable_lookup[AST_PHONEPROV_STD_MAC]);
- if (!mac) {
- ast_log(LOG_ERROR, "MAC Address can't be empty.\n");
- return -1;
- }
- provider = find_provider(provider_name);
- if (!provider) {
- ast_log(LOG_ERROR, "Provider '%s' wasn't found in the registry.\n", provider_name);
- return -1;
- }
- profile_name = ast_var_find(vars,
- variable_lookup[AST_PHONEPROV_STD_PROFILE]);
- if (!profile_name) {
- ast_log(LOG_ERROR, "No profile could be found for user '%s' - skipping.\n", username);
- return -1;
- }
- if (!(profile = find_profile(profile_name))) {
- ast_log(LOG_ERROR, "Could not look up profile '%s' - skipping.\n", profile_name);
- return -1;
- }
- if (!(user = find_user(mac))) {
- if (!(user = build_user(mac, profile, provider_name))) {
- ast_log(LOG_ERROR, "Could not create user for '%s' - skipping\n", mac);
- return -1;
- }
- if (!(exten = build_extension(username, vars))) {
- ast_log(LOG_ERROR, "Could not create extension for '%s' - skipping\n", user->macaddress);
- return -1;
- }
- if (add_user_extension(user, exten)) {
- ast_log(LOG_WARNING, "Could not add extension '%s' to user '%s'\n", exten->name, user->macaddress);
- exten = delete_extension(exten);
- return -1;
- }
- if (build_user_routes(user)) {
- ast_log(LOG_WARNING, "Could not create http routes for '%s' - skipping\n", user->macaddress);
- return -1;
- }
- ao2_link(users, user);
- } else {
- if (strcmp(provider_name, user->provider_name)) {
- ast_log(LOG_ERROR, "MAC address '%s' was already added by provider '%s' - skipping\n", user->macaddress, user->provider_name);
- return -1;
- }
- if (!(exten = build_extension(username, vars))) {
- ast_log(LOG_ERROR, "Could not create extension for '%s' - skipping\n", user->macaddress);
- return -1;
- }
- if (add_user_extension(user, exten)) {
- ast_log(LOG_WARNING, "Could not add extension '%s' to user '%s'\n", exten->name, user->macaddress);
- exten = delete_extension(exten);
- return -1;
- }
- }
- return 0;
- }
|