00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "asterisk.h"
00030
00031 #include <sys/ioctl.h>
00032 #include <sys/socket.h>
00033 #include <net/if.h>
00034 #ifdef SOLARIS
00035 #include <sys/sockio.h>
00036 #endif
00037 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 222185 $")
00038
00039 #include "asterisk/file.h"
00040 #include "asterisk/paths.h"
00041 #include "asterisk/pbx.h"
00042 #include "asterisk/cli.h"
00043 #include "asterisk/module.h"
00044 #include "asterisk/http.h"
00045 #include "asterisk/utils.h"
00046 #include "asterisk/app.h"
00047 #include "asterisk/strings.h"
00048 #include "asterisk/stringfields.h"
00049 #include "asterisk/options.h"
00050 #include "asterisk/config.h"
00051 #include "asterisk/acl.h"
00052 #include "asterisk/astobj2.h"
00053 #include "asterisk/ast_version.h"
00054
00055 #ifdef LOW_MEMORY
00056 #define MAX_PROFILE_BUCKETS 1
00057 #define MAX_ROUTE_BUCKETS 1
00058 #else
00059 #define MAX_PROFILE_BUCKETS 17
00060 #define MAX_ROUTE_BUCKETS 563
00061 #endif
00062
00063 #define VAR_BUF_SIZE 4096
00064
00065
00066 static struct in_addr __ourip = { .s_addr = 0x00000000, };
00067
00068
00069
00070 enum pp_variables {
00071 PP_MACADDRESS,
00072 PP_USERNAME,
00073 PP_FULLNAME,
00074 PP_SECRET,
00075 PP_LABEL,
00076 PP_CALLERID,
00077 PP_TIMEZONE,
00078 PP_VAR_LIST_LENGTH,
00079 };
00080
00081
00082
00083 static const struct pp_variable_lookup {
00084 enum pp_variables id;
00085 const char * const user_var;
00086 const char * const template_var;
00087 } pp_variable_list[] = {
00088 { PP_MACADDRESS, "macaddress", "MAC" },
00089 { PP_USERNAME, "username", "USERNAME" },
00090 { PP_FULLNAME, "fullname", "DISPLAY_NAME" },
00091 { PP_SECRET, "secret", "SECRET" },
00092 { PP_LABEL, "label", "LABEL" },
00093 { PP_CALLERID, "cid_number", "CALLERID" },
00094 { PP_TIMEZONE, "timezone", "TIMEZONE" },
00095 };
00096
00097
00098 struct phoneprov_file {
00099 AST_DECLARE_STRING_FIELDS(
00100 AST_STRING_FIELD(format);
00101 AST_STRING_FIELD(template);
00102 AST_STRING_FIELD(mime_type);
00103 );
00104 AST_LIST_ENTRY(phoneprov_file) entry;
00105 };
00106
00107
00108 struct phone_profile {
00109 AST_DECLARE_STRING_FIELDS(
00110 AST_STRING_FIELD(name);
00111 AST_STRING_FIELD(default_mime_type);
00112 AST_STRING_FIELD(staticdir);
00113 );
00114 struct varshead *headp;
00115 AST_LIST_HEAD_NOLOCK(, phoneprov_file) static_files;
00116 AST_LIST_HEAD_NOLOCK(, phoneprov_file) dynamic_files;
00117 };
00118
00119
00120 struct user {
00121 AST_DECLARE_STRING_FIELDS(
00122 AST_STRING_FIELD(name);
00123 AST_STRING_FIELD(macaddress);
00124 );
00125 struct phone_profile *profile;
00126 struct varshead *headp;
00127 AST_LIST_ENTRY(user) entry;
00128 };
00129
00130
00131 struct http_route {
00132 AST_DECLARE_STRING_FIELDS(
00133 AST_STRING_FIELD(uri);
00134 );
00135 struct phoneprov_file *file;
00136 struct user *user;
00137
00138 };
00139
00140 static struct ao2_container *profiles;
00141 static struct ao2_container *http_routes;
00142 static AST_RWLIST_HEAD_STATIC(users, user);
00143
00144
00145 static struct {
00146 char *ext;
00147 char *mtype;
00148 } mimetypes[] = {
00149 { "png", "image/png" },
00150 { "xml", "text/xml" },
00151 { "jpg", "image/jpeg" },
00152 { "js", "application/x-javascript" },
00153 { "wav", "audio/x-wav" },
00154 { "mp3", "audio/mpeg" },
00155 };
00156
00157 char global_server[80] = "";
00158 char global_serverport[6] = "";
00159 char global_default_profile[80] = "";
00160
00161
00162 struct varshead global_variables;
00163
00164
00165 static char *ftype2mtype(const char *ftype)
00166 {
00167 int x;
00168
00169 if (ast_strlen_zero(ftype))
00170 return NULL;
00171
00172 for (x = 0;x < ARRAY_LEN(mimetypes);x++) {
00173 if (!strcasecmp(ftype, mimetypes[x].ext))
00174 return mimetypes[x].mtype;
00175 }
00176
00177 return NULL;
00178 }
00179
00180
00181 static int lookup_iface(const char *iface, struct in_addr *address)
00182 {
00183 int mysock, res = 0;
00184 struct ifreq ifr;
00185 struct sockaddr_in *sin;
00186
00187 memset(&ifr, 0, sizeof(ifr));
00188 ast_copy_string(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
00189
00190 mysock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
00191 if (mysock < 0) {
00192 ast_log(LOG_ERROR, "Failed to create socket: %s\n", strerror(errno));
00193 return -1;
00194 }
00195
00196 res = ioctl(mysock, SIOCGIFADDR, &ifr);
00197
00198 close(mysock);
00199
00200 if (res < 0) {
00201 ast_log(LOG_WARNING, "Unable to get IP of %s: %s\n", iface, strerror(errno));
00202 memcpy(address, &__ourip, sizeof(__ourip));
00203 return -1;
00204 } else {
00205 sin = (struct sockaddr_in *)&ifr.ifr_addr;
00206 memcpy(address, &sin->sin_addr, sizeof(*address));
00207 return 0;
00208 }
00209 }
00210
00211 static struct phone_profile *unref_profile(struct phone_profile *prof)
00212 {
00213 ao2_ref(prof, -1);
00214
00215 return NULL;
00216 }
00217
00218
00219 static struct phone_profile *find_profile(const char *name)
00220 {
00221 struct phone_profile tmp = {
00222 .name = name,
00223 };
00224
00225 return ao2_find(profiles, &tmp, OBJ_POINTER);
00226 }
00227
00228 static int profile_hash_fn(const void *obj, const int flags)
00229 {
00230 const struct phone_profile *profile = obj;
00231
00232 return ast_str_hash(profile->name);
00233 }
00234
00235 static int profile_cmp_fn(void *obj, void *arg, int flags)
00236 {
00237 const struct phone_profile *profile1 = obj, *profile2 = arg;
00238
00239 return !strcasecmp(profile1->name, profile2->name) ? CMP_MATCH : 0;
00240 }
00241
00242 static void delete_file(struct phoneprov_file *file)
00243 {
00244 ast_string_field_free_memory(file);
00245 free(file);
00246 }
00247
00248 static void profile_destructor(void *obj)
00249 {
00250 struct phone_profile *profile = obj;
00251 struct phoneprov_file *file;
00252 struct ast_var_t *var;
00253
00254 while ((file = AST_LIST_REMOVE_HEAD(&profile->static_files, entry)))
00255 delete_file(file);
00256
00257 while ((file = AST_LIST_REMOVE_HEAD(&profile->dynamic_files, entry)))
00258 delete_file(file);
00259
00260 while ((var = AST_LIST_REMOVE_HEAD(profile->headp, entries)))
00261 ast_var_delete(var);
00262
00263 free(profile->headp);
00264 ast_string_field_free_memory(profile);
00265 }
00266
00267 static struct http_route *unref_route(struct http_route *route)
00268 {
00269 ao2_ref(route, -1);
00270
00271 return NULL;
00272 }
00273
00274 static int routes_hash_fn(const void *obj, const int flags)
00275 {
00276 const struct http_route *route = obj;
00277
00278 return ast_str_hash(route->uri);
00279 }
00280
00281 static int routes_cmp_fn(void *obj, void *arg, int flags)
00282 {
00283 const struct http_route *route1 = obj, *route2 = arg;
00284
00285 return !strcmp(route1->uri, route2->uri) ? CMP_MATCH : 0;
00286 }
00287
00288 static void route_destructor(void *obj)
00289 {
00290 struct http_route *route = obj;
00291
00292 ast_string_field_free_memory(route);
00293 }
00294
00295
00296 static int load_file(const char *filename, char **ret)
00297 {
00298 int len = 0;
00299 FILE *f;
00300
00301 if (!(f = fopen(filename, "r"))) {
00302 *ret = NULL;
00303 return -1;
00304 }
00305
00306 fseek(f, 0, SEEK_END);
00307 len = ftell(f);
00308 fseek(f, 0, SEEK_SET);
00309 if (!(*ret = ast_malloc(len + 1)))
00310 return -2;
00311
00312 if (len != fread(*ret, sizeof(char), len, f)) {
00313 free(*ret);
00314 *ret = NULL;
00315 return -3;
00316 }
00317
00318 fclose(f);
00319
00320 (*ret)[len] = '\0';
00321
00322 return len;
00323 }
00324
00325
00326 static struct ast_str *phoneprov_callback(struct ast_tcptls_session_instance *ser, const char *uri, struct ast_variable *vars, int *status, char **title, int *contentlength)
00327 {
00328 struct http_route *route;
00329 struct http_route search_route = {
00330 .uri = uri,
00331 };
00332 struct ast_str *result = ast_str_create(512);
00333 char path[PATH_MAX];
00334 char *file = NULL;
00335 int len;
00336 int fd;
00337 char buf[256];
00338 struct timeval tv = ast_tvnow();
00339 struct ast_tm tm;
00340
00341 if (!(route = ao2_find(http_routes, &search_route, OBJ_POINTER)))
00342 goto out404;
00343
00344 snprintf(path, sizeof(path), "%s/phoneprov/%s", ast_config_AST_DATA_DIR, route->file->template);
00345
00346 if (!route->user) {
00347
00348 fd = open(path, O_RDONLY);
00349 if (fd < 0)
00350 goto out500;
00351
00352 len = lseek(fd, 0, SEEK_END);
00353 lseek(fd, 0, SEEK_SET);
00354 if (len < 0) {
00355 ast_log(LOG_WARNING, "Could not load file: %s (%d)\n", path, len);
00356 close(fd);
00357 goto out500;
00358 }
00359
00360 ast_strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S %Z", ast_localtime(&tv, &tm, "GMT"));
00361 fprintf(ser->f, "HTTP/1.1 200 OK\r\n"
00362 "Server: Asterisk/%s\r\n"
00363 "Date: %s\r\n"
00364 "Connection: close\r\n"
00365 "Cache-Control: no-cache, no-store\r\n"
00366 "Content-Length: %d\r\n"
00367 "Content-Type: %s\r\n\r\n",
00368 ast_get_version(), buf, len, route->file->mime_type);
00369
00370 while ((len = read(fd, buf, sizeof(buf))) > 0)
00371 if (fwrite(buf, 1, len, ser->f) != len) {
00372 if (errno != EPIPE) {
00373 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
00374 } else {
00375 ast_debug(3, "Requester closed the connection while downloading '%s'\n", path);
00376 }
00377 break;
00378 }
00379
00380 close(fd);
00381 route = unref_route(route);
00382 return NULL;
00383 } else {
00384 int bufsize;
00385 char *tmp;
00386
00387 len = load_file(path, &file);
00388 if (len < 0) {
00389 ast_log(LOG_WARNING, "Could not load file: %s (%d)\n", path, len);
00390 if (file)
00391 ast_free(file);
00392 goto out500;
00393 }
00394
00395 if (!file)
00396 goto out500;
00397
00398
00399 bufsize = len + VAR_BUF_SIZE;
00400
00401
00402
00403 if (!(tmp = ast_calloc(1, bufsize))) {
00404 if (file)
00405 ast_free(file);
00406 goto out500;
00407 }
00408
00409
00410
00411 if (ast_strlen_zero(global_server)) {
00412 union {
00413 struct sockaddr sa;
00414 struct sockaddr_in sa_in;
00415 } name;
00416 socklen_t namelen = sizeof(name.sa);
00417 int res;
00418
00419 if ((res = getsockname(ser->fd, &name.sa, &namelen)))
00420 ast_log(LOG_WARNING, "Could not get server IP, breakage likely.\n");
00421 else {
00422 struct ast_var_t *var;
00423
00424 if ((var = ast_var_assign("SERVER", ast_inet_ntoa(name.sa_in.sin_addr))))
00425 if ((var = ast_var_assign("SERVER", ast_inet_ntoa(((struct sockaddr_in *)&name)->sin_addr))))
00426 AST_LIST_INSERT_TAIL(route->user->headp, var, entries);
00427 }
00428 }
00429
00430 pbx_substitute_variables_varshead(route->user->headp, file, tmp, bufsize);
00431
00432 if (file)
00433 ast_free(file);
00434
00435 ast_str_append(&result, 0,
00436 "Content-Type: %s\r\n"
00437 "Content-length: %d\r\n"
00438 "\r\n"
00439 "%s", route->file->mime_type, (int) strlen(tmp), tmp);
00440
00441 if (tmp)
00442 ast_free(tmp);
00443
00444 route = unref_route(route);
00445
00446 return result;
00447 }
00448
00449 out404:
00450 *status = 404;
00451 *title = strdup("Not Found");
00452 *contentlength = 0;
00453 return ast_http_error(404, "Not Found", NULL, "Nothing to see here. Move along.");
00454
00455 out500:
00456 route = unref_route(route);
00457 *status = 500;
00458 *title = strdup("Internal Server Error");
00459 *contentlength = 0;
00460 return ast_http_error(500, "Internal Error", NULL, "An internal error has occured.");
00461 }
00462
00463
00464
00465
00466
00467
00468 static void build_route(struct phoneprov_file *pp_file, struct user *user, char *uri)
00469 {
00470 struct http_route *route;
00471
00472 if (!(route = ao2_alloc(sizeof(*route), route_destructor)))
00473 return;
00474
00475 if (ast_string_field_init(route, 32)) {
00476 ast_log(LOG_ERROR, "Couldn't create string fields for %s\n", pp_file->format);
00477 route = unref_route(route);
00478 return;
00479 }
00480
00481 ast_string_field_set(route, uri, S_OR(uri, pp_file->format));
00482 route->user = user;
00483 route->file = pp_file;
00484
00485 ao2_link(http_routes, route);
00486
00487 route = unref_route(route);
00488 }
00489
00490
00491
00492
00493
00494 static void build_profile(const char *name, struct ast_variable *v)
00495 {
00496 struct phone_profile *profile;
00497 struct ast_var_t *var;
00498
00499 if (!(profile = ao2_alloc(sizeof(*profile), profile_destructor)))
00500 return;
00501
00502 if (ast_string_field_init(profile, 32)) {
00503 profile = unref_profile(profile);
00504 return;
00505 }
00506
00507 if (!(profile->headp = ast_calloc(1, sizeof(*profile->headp)))) {
00508 profile = unref_profile(profile);
00509 return;
00510 }
00511
00512 AST_LIST_HEAD_INIT_NOLOCK(&profile->static_files);
00513 AST_LIST_HEAD_INIT_NOLOCK(&profile->dynamic_files);
00514
00515 ast_string_field_set(profile, name, name);
00516 for (; v; v = v->next) {
00517 if (!strcasecmp(v->name, "mime_type")) {
00518 ast_string_field_set(profile, default_mime_type, v->value);
00519 } else if (!strcasecmp(v->name, "setvar")) {
00520 struct ast_var_t *var;
00521 char *value_copy = ast_strdupa(v->value);
00522
00523 AST_DECLARE_APP_ARGS(args,
00524 AST_APP_ARG(varname);
00525 AST_APP_ARG(varval);
00526 );
00527
00528 AST_NONSTANDARD_APP_ARGS(args, value_copy, '=');
00529 do {
00530 if (ast_strlen_zero(args.varname) || ast_strlen_zero(args.varval))
00531 break;
00532 args.varname = ast_strip(args.varname);
00533 args.varval = ast_strip(args.varval);
00534 if (ast_strlen_zero(args.varname) || ast_strlen_zero(args.varval))
00535 break;
00536 if ((var = ast_var_assign(args.varname, args.varval)))
00537 AST_LIST_INSERT_TAIL(profile->headp, var, entries);
00538 } while (0);
00539 } else if (!strcasecmp(v->name, "staticdir")) {
00540 ast_string_field_set(profile, staticdir, v->value);
00541 } else {
00542 struct phoneprov_file *pp_file;
00543 char *file_extension;
00544 char *value_copy = ast_strdupa(v->value);
00545
00546 AST_DECLARE_APP_ARGS(args,
00547 AST_APP_ARG(filename);
00548 AST_APP_ARG(mimetype);
00549 );
00550
00551 if (!(pp_file = ast_calloc(1, sizeof(*pp_file)))) {
00552 profile = unref_profile(profile);
00553 return;
00554 }
00555 if (ast_string_field_init(pp_file, 32)) {
00556 ast_free(pp_file);
00557 profile = unref_profile(profile);
00558 return;
00559 }
00560
00561 if ((file_extension = strrchr(pp_file->format, '.')))
00562 file_extension++;
00563
00564 AST_STANDARD_APP_ARGS(args, value_copy);
00565
00566
00567
00568
00569
00570
00571
00572 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"))));
00573
00574 if (!strcasecmp(v->name, "static_file")) {
00575 ast_string_field_set(pp_file, format, args.filename);
00576 ast_string_field_build(pp_file, template, "%s%s", profile->staticdir, args.filename);
00577 AST_LIST_INSERT_TAIL(&profile->static_files, pp_file, entry);
00578
00579 build_route(pp_file, NULL, NULL);
00580 } else {
00581 ast_string_field_set(pp_file, format, v->name);
00582 ast_string_field_set(pp_file, template, args.filename);
00583 AST_LIST_INSERT_TAIL(&profile->dynamic_files, pp_file, entry);
00584 }
00585 }
00586 }
00587
00588
00589
00590
00591 AST_LIST_TRAVERSE(&global_variables, var, entries) {
00592 struct ast_var_t *new_var;
00593 if ((new_var = ast_var_assign(var->name, var->value)))
00594 AST_LIST_INSERT_TAIL(profile->headp, new_var, entries);
00595 }
00596
00597 ao2_link(profiles, profile);
00598
00599 profile = unref_profile(profile);
00600 }
00601
00602
00603 static void delete_user(struct user *user)
00604 {
00605 struct ast_var_t *var;
00606
00607 while ((var = AST_LIST_REMOVE_HEAD(user->headp, entries)))
00608 ast_var_delete(var);
00609
00610 ast_free(user->headp);
00611 ast_string_field_free_memory(user);
00612 user->profile = unref_profile(user->profile);
00613 free(user);
00614 }
00615
00616
00617 static void delete_users(void)
00618 {
00619 struct user *user;
00620
00621 AST_RWLIST_WRLOCK(&users);
00622 while ((user = AST_LIST_REMOVE_HEAD(&users, entry)))
00623 delete_user(user);
00624 AST_RWLIST_UNLOCK(&users);
00625 }
00626
00627
00628
00629
00630
00631 static void set_timezone_variables(struct varshead *headp, const char *zone)
00632 {
00633 time_t utc_time;
00634 int dstenable;
00635 time_t dststart;
00636 time_t dstend;
00637 struct ast_tm tm_info;
00638 int tzoffset;
00639 char buffer[21];
00640 struct ast_var_t *var;
00641 struct timeval tv;
00642
00643 time(&utc_time);
00644 ast_get_dst_info(&utc_time, &dstenable, &dststart, &dstend, &tzoffset, zone);
00645 snprintf(buffer, sizeof(buffer), "%d", tzoffset);
00646 var = ast_var_assign("TZOFFSET", buffer);
00647 if (var)
00648 AST_LIST_INSERT_TAIL(headp, var, entries);
00649
00650 if (!dstenable)
00651 return;
00652
00653 if ((var = ast_var_assign("DST_ENABLE", "1")))
00654 AST_LIST_INSERT_TAIL(headp, var, entries);
00655
00656 tv.tv_sec = dststart;
00657 ast_localtime(&tv, &tm_info, zone);
00658
00659 snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_mon+1);
00660 if ((var = ast_var_assign("DST_START_MONTH", buffer)))
00661 AST_LIST_INSERT_TAIL(headp, var, entries);
00662
00663 snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_mday);
00664 if ((var = ast_var_assign("DST_START_MDAY", buffer)))
00665 AST_LIST_INSERT_TAIL(headp, var, entries);
00666
00667 snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_hour);
00668 if ((var = ast_var_assign("DST_START_HOUR", buffer)))
00669 AST_LIST_INSERT_TAIL(headp, var, entries);
00670
00671 tv.tv_sec = dstend;
00672 ast_localtime(&tv, &tm_info, zone);
00673
00674 snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_mon + 1);
00675 if ((var = ast_var_assign("DST_END_MONTH", buffer)))
00676 AST_LIST_INSERT_TAIL(headp, var, entries);
00677
00678 snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_mday);
00679 if ((var = ast_var_assign("DST_END_MDAY", buffer)))
00680 AST_LIST_INSERT_TAIL(headp, var, entries);
00681
00682 snprintf(buffer, sizeof(buffer), "%d", tm_info.tm_hour);
00683 if ((var = ast_var_assign("DST_END_HOUR", buffer)))
00684 AST_LIST_INSERT_TAIL(headp, var, entries);
00685 }
00686
00687
00688 static struct user *build_user(struct ast_config *cfg, const char *name, const char *mac, struct phone_profile *profile)
00689 {
00690 struct user *user;
00691 struct ast_var_t *var;
00692 const char *tmp;
00693 int i;
00694
00695 if (!(user = ast_calloc(1, sizeof(*user)))) {
00696 profile = unref_profile(profile);
00697 return NULL;
00698 }
00699
00700 if (!(user->headp = ast_calloc(1, sizeof(*user->headp)))) {
00701 profile = unref_profile(profile);
00702 ast_free(user);
00703 return NULL;
00704 }
00705
00706 if (ast_string_field_init(user, 32)) {
00707 profile = unref_profile(profile);
00708 delete_user(user);
00709 return NULL;
00710 }
00711
00712 ast_string_field_set(user, name, name);
00713 ast_string_field_set(user, macaddress, mac);
00714 user->profile = profile;
00715
00716 for (i = 0; i < PP_VAR_LIST_LENGTH; i++) {
00717 tmp = ast_variable_retrieve(cfg, name, pp_variable_list[i].user_var);
00718
00719
00720 if (i == PP_USERNAME && !tmp) {
00721 if ((var = ast_var_assign(pp_variable_list[PP_USERNAME].template_var, user->name))) {
00722 AST_LIST_INSERT_TAIL(user->headp, var, entries);
00723 }
00724 continue;
00725 } else if (i == PP_TIMEZONE) {
00726
00727 set_timezone_variables(user->headp, tmp);
00728 }
00729
00730 if (tmp && (var = ast_var_assign(pp_variable_list[i].template_var, tmp)))
00731 AST_LIST_INSERT_TAIL(user->headp, var, entries);
00732 }
00733
00734 if (!ast_strlen_zero(global_server)) {
00735 if ((var = ast_var_assign("SERVER", global_server)))
00736 AST_LIST_INSERT_TAIL(user->headp, var, entries);
00737 }
00738
00739 if (!ast_strlen_zero(global_serverport)) {
00740 if ((var = ast_var_assign("SERVER_PORT", global_serverport)))
00741 AST_LIST_INSERT_TAIL(user->headp, var, entries);
00742 }
00743
00744
00745
00746 AST_LIST_TRAVERSE(user->profile->headp, var, entries) {
00747 char expand_buf[VAR_BUF_SIZE] = {0,};
00748 struct ast_var_t *var2;
00749
00750 pbx_substitute_variables_varshead(user->headp, var->value, expand_buf, sizeof(expand_buf));
00751 if ((var2 = ast_var_assign(var->name, expand_buf)))
00752 AST_LIST_INSERT_TAIL(user->headp, var2, entries);
00753 }
00754
00755 return user;
00756 }
00757
00758
00759 static int build_user_routes(struct user *user)
00760 {
00761 struct phoneprov_file *pp_file;
00762
00763 AST_LIST_TRAVERSE(&user->profile->dynamic_files, pp_file, entry) {
00764 char expand_buf[VAR_BUF_SIZE] = { 0, };
00765
00766 pbx_substitute_variables_varshead(user->headp, pp_file->format, expand_buf, sizeof(expand_buf));
00767 build_route(pp_file, user, expand_buf);
00768 }
00769
00770 return 0;
00771 }
00772
00773
00774 static int set_config(void)
00775 {
00776 struct ast_config *cfg;
00777 char *cat;
00778 struct ast_variable *v;
00779 struct ast_flags config_flags = { 0 };
00780
00781
00782
00783 if ((cfg = ast_config_load("sip.conf", config_flags))) {
00784 ast_copy_string(global_serverport, S_OR(ast_variable_retrieve(cfg, "general", "bindport"), "5060"), sizeof(global_serverport));
00785 ast_config_destroy(cfg);
00786 }
00787
00788 if (!(cfg = ast_config_load("phoneprov.conf", config_flags))) {
00789 ast_log(LOG_ERROR, "Unable to load config phoneprov.conf\n");
00790 ast_config_destroy(cfg);
00791 return -1;
00792 }
00793
00794 cat = NULL;
00795 while ((cat = ast_category_browse(cfg, cat))) {
00796 if (!strcasecmp(cat, "general")) {
00797 for (v = ast_variable_browse(cfg, cat); v; v = v->next) {
00798 if (!strcasecmp(v->name, "serveraddr"))
00799 ast_copy_string(global_server, v->value, sizeof(global_server));
00800 else if (!strcasecmp(v->name, "serveriface")) {
00801 struct in_addr addr;
00802 lookup_iface(v->value, &addr);
00803 ast_copy_string(global_server, ast_inet_ntoa(addr), sizeof(global_server));
00804 } else if (!strcasecmp(v->name, "serverport"))
00805 ast_copy_string(global_serverport, v->value, sizeof(global_serverport));
00806 else if (!strcasecmp(v->name, "default_profile"))
00807 ast_copy_string(global_default_profile, v->value, sizeof(global_default_profile));
00808 }
00809 } else
00810 build_profile(cat, ast_variable_browse(cfg, cat));
00811 }
00812
00813 ast_config_destroy(cfg);
00814
00815 if (!(cfg = ast_config_load("users.conf", config_flags))) {
00816 ast_log(LOG_WARNING, "Unable to load users.cfg\n");
00817 return 0;
00818 }
00819
00820 cat = NULL;
00821 while ((cat = ast_category_browse(cfg, cat))) {
00822 const char *tmp, *mac;
00823 struct user *user;
00824 struct phone_profile *profile;
00825 struct ast_var_t *var;
00826
00827 if (!strcasecmp(cat, "general")) {
00828 for (v = ast_variable_browse(cfg, cat); v; v = v->next) {
00829 if (!strcasecmp(v->name, "vmexten")) {
00830 if ((var = ast_var_assign("VOICEMAIL_EXTEN", v->value)))
00831 AST_LIST_INSERT_TAIL(&global_variables, var, entries);
00832 }
00833 if (!strcasecmp(v->name, "localextenlength")) {
00834 if ((var = ast_var_assign("EXTENSION_LENGTH", v->value)))
00835 AST_LIST_INSERT_TAIL(&global_variables, var, entries);
00836 }
00837 }
00838 }
00839
00840 if (!strcasecmp(cat, "authentication"))
00841 continue;
00842
00843 if (!((tmp = ast_variable_retrieve(cfg, cat, "autoprov")) && ast_true(tmp)))
00844 continue;
00845
00846 if (!(mac = ast_variable_retrieve(cfg, cat, "macaddress"))) {
00847 ast_log(LOG_WARNING, "autoprov set for %s, but no mac address - skipping.\n", cat);
00848 continue;
00849 }
00850
00851 tmp = S_OR(ast_variable_retrieve(cfg, cat, "profile"), global_default_profile);
00852 if (ast_strlen_zero(tmp)) {
00853 ast_log(LOG_WARNING, "No profile for user [%s] with mac '%s' - skipping\n", cat, mac);
00854 continue;
00855 }
00856
00857 if (!(profile = find_profile(tmp))) {
00858 ast_log(LOG_WARNING, "Could not look up profile '%s' - skipping.\n", tmp);
00859 continue;
00860 }
00861
00862 if (!(user = build_user(cfg, cat, mac, profile))) {
00863 ast_log(LOG_WARNING, "Could not create user %s - skipping.\n", cat);
00864 continue;
00865 }
00866
00867 if (build_user_routes(user)) {
00868 ast_log(LOG_WARNING, "Could not create http routes for %s - skipping\n", user->name);
00869 delete_user(user);
00870 continue;
00871 }
00872
00873 AST_RWLIST_WRLOCK(&users);
00874 AST_RWLIST_INSERT_TAIL(&users, user, entry);
00875 AST_RWLIST_UNLOCK(&users);
00876 }
00877
00878 ast_config_destroy(cfg);
00879
00880 return 0;
00881 }
00882
00883
00884 static void delete_routes(void)
00885 {
00886 struct ao2_iterator i;
00887 struct http_route *route;
00888
00889 i = ao2_iterator_init(http_routes, 0);
00890 while ((route = ao2_iterator_next(&i))) {
00891 ao2_unlink(http_routes, route);
00892 route = unref_route(route);
00893 }
00894 ao2_iterator_destroy(&i);
00895 }
00896
00897
00898 static void delete_profiles(void)
00899 {
00900 struct ao2_iterator i;
00901 struct phone_profile *profile;
00902
00903 i = ao2_iterator_init(profiles, 0);
00904 while ((profile = ao2_iterator_next(&i))) {
00905 ao2_unlink(profiles, profile);
00906 profile = unref_profile(profile);
00907 }
00908 ao2_iterator_destroy(&i);
00909 }
00910
00911
00912 static int pp_each_user_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
00913 {
00914 char *tmp, expand_buf[VAR_BUF_SIZE] = {0,};
00915 struct user *user;
00916 AST_DECLARE_APP_ARGS(args,
00917 AST_APP_ARG(string);
00918 AST_APP_ARG(exclude_mac);
00919 );
00920 AST_STANDARD_APP_ARGS(args, data);
00921
00922
00923 while ((tmp = strstr(args.string, "%{")))
00924 *tmp = '$';
00925
00926 AST_RWLIST_RDLOCK(&users);
00927 AST_RWLIST_TRAVERSE(&users, user, entry) {
00928 if (!ast_strlen_zero(args.exclude_mac) && !strcasecmp(user->macaddress, args.exclude_mac))
00929 continue;
00930 pbx_substitute_variables_varshead(user->headp, args.string, expand_buf, sizeof(expand_buf));
00931 ast_build_string(&buf, &len, "%s", expand_buf);
00932 }
00933 AST_RWLIST_UNLOCK(&users);
00934
00935 return 0;
00936 }
00937
00938 static struct ast_custom_function pp_each_user_function = {
00939 .name = "PP_EACH_USER",
00940 .synopsis = "Generate a string for each phoneprov user",
00941 .syntax = "PP_EACH_USER(<string>|<exclude_mac>)",
00942 .desc =
00943 "Pass in a string, with phoneprov variables you want substituted in the format of\n"
00944 "%{VARNAME}, and you will get the string rendered for each user in phoneprov\n"
00945 "excluding ones with MAC address <exclude_mac>. Probably not useful outside of\n"
00946 "res_phoneprov.\n"
00947 "\nExample: ${PP_EACH_USER(<item><fn>%{DISPLAY_NAME}</fn></item>|${MAC})",
00948 .read = pp_each_user_exec,
00949 };
00950
00951
00952 static char *handle_show_routes(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00953 {
00954 #define FORMAT "%-40.40s %-30.30s\n"
00955 struct ao2_iterator i;
00956 struct http_route *route;
00957
00958 switch(cmd) {
00959 case CLI_INIT:
00960 e->command = "phoneprov show routes";
00961 e->usage =
00962 "Usage: phoneprov show routes\n"
00963 " Lists all registered phoneprov http routes.\n";
00964 return NULL;
00965 case CLI_GENERATE:
00966 return NULL;
00967 }
00968
00969
00970
00971 ast_cli(a->fd, "Static routes\n\n");
00972 ast_cli(a->fd, FORMAT, "Relative URI", "Physical location");
00973 i = ao2_iterator_init(http_routes, 0);
00974 while ((route = ao2_iterator_next(&i))) {
00975 if (!route->user)
00976 ast_cli(a->fd, FORMAT, route->uri, route->file->template);
00977 route = unref_route(route);
00978 }
00979 ao2_iterator_destroy(&i);
00980
00981 ast_cli(a->fd, "\nDynamic routes\n\n");
00982 ast_cli(a->fd, FORMAT, "Relative URI", "Template");
00983
00984 i = ao2_iterator_init(http_routes, 0);
00985 while ((route = ao2_iterator_next(&i))) {
00986 if (route->user)
00987 ast_cli(a->fd, FORMAT, route->uri, route->file->template);
00988 route = unref_route(route);
00989 }
00990 ao2_iterator_destroy(&i);
00991
00992 return CLI_SUCCESS;
00993 }
00994
00995 static struct ast_cli_entry pp_cli[] = {
00996 AST_CLI_DEFINE(handle_show_routes, "Show registered phoneprov http routes"),
00997 };
00998
00999 static struct ast_http_uri phoneprovuri = {
01000 .callback = phoneprov_callback,
01001 .description = "Asterisk HTTP Phone Provisioning Tool",
01002 .uri = "phoneprov",
01003 .has_subtree = 1,
01004 };
01005
01006 static int load_module(void)
01007 {
01008 profiles = ao2_container_alloc(MAX_PROFILE_BUCKETS, profile_hash_fn, profile_cmp_fn);
01009
01010 http_routes = ao2_container_alloc(MAX_ROUTE_BUCKETS, routes_hash_fn, routes_cmp_fn);
01011
01012 AST_LIST_HEAD_INIT_NOLOCK(&global_variables);
01013
01014 ast_custom_function_register(&pp_each_user_function);
01015 ast_cli_register_multiple(pp_cli, ARRAY_LEN(pp_cli));
01016
01017 set_config();
01018 ast_http_uri_link(&phoneprovuri);
01019
01020 return 0;
01021 }
01022
01023 static int unload_module(void)
01024 {
01025 struct ast_var_t *var;
01026
01027 ast_http_uri_unlink(&phoneprovuri);
01028 ast_custom_function_unregister(&pp_each_user_function);
01029 ast_cli_unregister_multiple(pp_cli, ARRAY_LEN(pp_cli));
01030
01031 delete_routes();
01032 delete_users();
01033 delete_profiles();
01034 ao2_ref(profiles, -1);
01035 ao2_ref(http_routes, -1);
01036
01037 while ((var = AST_LIST_REMOVE_HEAD(&global_variables, entries)))
01038 ast_var_delete(var);
01039
01040 return 0;
01041 }
01042
01043 static int reload(void)
01044 {
01045 delete_routes();
01046 delete_users();
01047 delete_profiles();
01048 set_config();
01049
01050 return 0;
01051 }
01052
01053 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "HTTP Phone Provisioning",
01054 .load = load_module,
01055 .unload = unload_module,
01056 .reload = reload,
01057 );