config.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Configuration File Parser
  21. *
  22. * Includes the Asterisk Realtime API - ARA
  23. * See README.realtime
  24. */
  25. #include <stdio.h>
  26. #include <unistd.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <errno.h>
  30. #include <time.h>
  31. #include <sys/stat.h>
  32. #define AST_INCLUDE_GLOB 1
  33. #ifdef AST_INCLUDE_GLOB
  34. #if defined(__Darwin__) || defined(__CYGWIN__)
  35. #define GLOB_ABORTED GLOB_ABEND
  36. #endif
  37. # include <glob.h>
  38. #endif
  39. #include "asterisk.h"
  40. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  41. #include "asterisk/config.h"
  42. #include "asterisk/cli.h"
  43. #include "asterisk/lock.h"
  44. #include "asterisk/options.h"
  45. #include "asterisk/logger.h"
  46. #include "asterisk/utils.h"
  47. #include "asterisk/channel.h"
  48. #include "asterisk/app.h"
  49. #define MAX_NESTED_COMMENTS 128
  50. #define COMMENT_START ";--"
  51. #define COMMENT_END "--;"
  52. #define COMMENT_META ';'
  53. #define COMMENT_TAG '-'
  54. static char *extconfig_conf = "extconfig.conf";
  55. static struct ast_config_map {
  56. struct ast_config_map *next;
  57. char *name;
  58. char *driver;
  59. char *database;
  60. char *table;
  61. char stuff[0];
  62. } *config_maps = NULL;
  63. AST_MUTEX_DEFINE_STATIC(config_lock);
  64. static struct ast_config_engine *config_engine_list;
  65. #define MAX_INCLUDE_LEVEL 10
  66. struct ast_comment {
  67. struct ast_comment *next;
  68. char cmt[0];
  69. };
  70. struct ast_category {
  71. char name[80];
  72. int ignored; /* do not let user of the config see this category */
  73. struct ast_variable *root;
  74. struct ast_variable *last;
  75. struct ast_category *next;
  76. };
  77. struct ast_config {
  78. struct ast_category *root;
  79. struct ast_category *last;
  80. struct ast_category *current;
  81. struct ast_category *last_browse; /* used to cache the last category supplied via category_browse */
  82. int include_level;
  83. int max_include_level;
  84. };
  85. struct ast_variable *ast_variable_new(const char *name, const char *value)
  86. {
  87. struct ast_variable *variable;
  88. int length = strlen(name) + strlen(value) + 2 + sizeof(struct ast_variable);
  89. variable = malloc(length);
  90. if (variable) {
  91. memset(variable, 0, length);
  92. variable->name = variable->stuff;
  93. variable->value = variable->stuff + strlen(name) + 1;
  94. strcpy(variable->name,name);
  95. strcpy(variable->value,value);
  96. }
  97. return variable;
  98. }
  99. void ast_variable_append(struct ast_category *category, struct ast_variable *variable)
  100. {
  101. if (category->last)
  102. category->last->next = variable;
  103. else
  104. category->root = variable;
  105. category->last = variable;
  106. while (category->last->next)
  107. category->last = category->last->next;
  108. }
  109. void ast_variables_destroy(struct ast_variable *v)
  110. {
  111. struct ast_variable *vn;
  112. while(v) {
  113. vn = v;
  114. v = v->next;
  115. free(vn);
  116. }
  117. }
  118. struct ast_variable *ast_variable_browse(const struct ast_config *config, const char *category)
  119. {
  120. struct ast_category *cat = NULL;
  121. if (category && config->last_browse && (config->last_browse->name == category))
  122. cat = config->last_browse;
  123. else
  124. cat = ast_category_get(config, category);
  125. if (cat)
  126. return cat->root;
  127. else
  128. return NULL;
  129. }
  130. char *ast_variable_retrieve(const struct ast_config *config, const char *category, const char *variable)
  131. {
  132. struct ast_variable *v;
  133. if (category) {
  134. for (v = ast_variable_browse(config, category); v; v = v->next) {
  135. if (!strcasecmp(variable, v->name))
  136. return v->value;
  137. }
  138. } else {
  139. struct ast_category *cat;
  140. for (cat = config->root; cat; cat = cat->next)
  141. for (v = cat->root; v; v = v->next)
  142. if (!strcasecmp(variable, v->name))
  143. return v->value;
  144. }
  145. return NULL;
  146. }
  147. static struct ast_variable *variable_clone(const struct ast_variable *old)
  148. {
  149. struct ast_variable *new = ast_variable_new(old->name, old->value);
  150. if (new) {
  151. new->lineno = old->lineno;
  152. new->object = old->object;
  153. new->blanklines = old->blanklines;
  154. /* TODO: clone comments? */
  155. }
  156. return new;
  157. }
  158. static void move_variables(struct ast_category *old, struct ast_category *new)
  159. {
  160. struct ast_variable *var;
  161. struct ast_variable *next;
  162. next = old->root;
  163. old->root = NULL;
  164. for (var = next; var; var = next) {
  165. next = var->next;
  166. var->next = NULL;
  167. ast_variable_append(new, var);
  168. }
  169. }
  170. struct ast_category *ast_category_new(const char *name)
  171. {
  172. struct ast_category *category;
  173. category = malloc(sizeof(struct ast_category));
  174. if (category) {
  175. memset(category, 0, sizeof(struct ast_category));
  176. ast_copy_string(category->name, name, sizeof(category->name));
  177. }
  178. return category;
  179. }
  180. static struct ast_category *category_get(const struct ast_config *config, const char *category_name, int ignored)
  181. {
  182. struct ast_category *cat;
  183. for (cat = config->root; cat; cat = cat->next) {
  184. if (cat->name == category_name && (ignored || !cat->ignored))
  185. return cat;
  186. }
  187. for (cat = config->root; cat; cat = cat->next) {
  188. if (!strcasecmp(cat->name, category_name) && (ignored || !cat->ignored))
  189. return cat;
  190. }
  191. return NULL;
  192. }
  193. struct ast_category *ast_category_get(const struct ast_config *config, const char *category_name)
  194. {
  195. return category_get(config, category_name, 0);
  196. }
  197. int ast_category_exist(const struct ast_config *config, const char *category_name)
  198. {
  199. return !!ast_category_get(config, category_name);
  200. }
  201. void ast_category_append(struct ast_config *config, struct ast_category *category)
  202. {
  203. if (config->last)
  204. config->last->next = category;
  205. else
  206. config->root = category;
  207. config->last = category;
  208. config->current = category;
  209. }
  210. void ast_category_destroy(struct ast_category *cat)
  211. {
  212. ast_variables_destroy(cat->root);
  213. free(cat);
  214. }
  215. static struct ast_category *next_available_category(struct ast_category *cat)
  216. {
  217. for (; cat && cat->ignored; cat = cat->next);
  218. return cat;
  219. }
  220. char *ast_category_browse(struct ast_config *config, const char *prev)
  221. {
  222. struct ast_category *cat = NULL;
  223. if (prev && config->last_browse && (config->last_browse->name == prev))
  224. cat = config->last_browse->next;
  225. else if (!prev && config->root)
  226. cat = config->root;
  227. else if (prev) {
  228. for (cat = config->root; cat; cat = cat->next) {
  229. if (cat->name == prev) {
  230. cat = cat->next;
  231. break;
  232. }
  233. }
  234. if (!cat) {
  235. for (cat = config->root; cat; cat = cat->next) {
  236. if (!strcasecmp(cat->name, prev)) {
  237. cat = cat->next;
  238. break;
  239. }
  240. }
  241. }
  242. }
  243. if (cat)
  244. cat = next_available_category(cat);
  245. config->last_browse = cat;
  246. if (cat)
  247. return cat->name;
  248. else
  249. return NULL;
  250. }
  251. struct ast_variable *ast_category_detach_variables(struct ast_category *cat)
  252. {
  253. struct ast_variable *v;
  254. v = cat->root;
  255. cat->root = NULL;
  256. cat->last = NULL;
  257. return v;
  258. }
  259. void ast_category_rename(struct ast_category *cat, const char *name)
  260. {
  261. ast_copy_string(cat->name, name, sizeof(cat->name));
  262. }
  263. static void inherit_category(struct ast_category *new, const struct ast_category *base)
  264. {
  265. struct ast_variable *var;
  266. for (var = base->root; var; var = var->next) {
  267. struct ast_variable *v;
  268. v = variable_clone(var);
  269. if (v)
  270. ast_variable_append(new, v);
  271. }
  272. }
  273. struct ast_config *ast_config_new(void)
  274. {
  275. struct ast_config *config;
  276. config = malloc(sizeof(*config));
  277. if (config) {
  278. memset(config, 0, sizeof(*config));
  279. config->max_include_level = MAX_INCLUDE_LEVEL;
  280. }
  281. return config;
  282. }
  283. void ast_config_destroy(struct ast_config *cfg)
  284. {
  285. struct ast_category *cat, *catn;
  286. if (!cfg)
  287. return;
  288. cat = cfg->root;
  289. while(cat) {
  290. ast_variables_destroy(cat->root);
  291. catn = cat;
  292. cat = cat->next;
  293. free(catn);
  294. }
  295. free(cfg);
  296. }
  297. struct ast_category *ast_config_get_current_category(const struct ast_config *cfg)
  298. {
  299. return cfg->current;
  300. }
  301. void ast_config_set_current_category(struct ast_config *cfg, const struct ast_category *cat)
  302. {
  303. /* cast below is just to silence compiler warning about dropping "const" */
  304. cfg->current = (struct ast_category *) cat;
  305. }
  306. static int process_text_line(struct ast_config *cfg, struct ast_category **cat, char *buf, int lineno, const char *configfile)
  307. {
  308. char *c;
  309. char *cur = buf;
  310. struct ast_variable *v;
  311. char cmd[512], exec_file[512];
  312. int object, do_exec, do_include;
  313. /* Actually parse the entry */
  314. if (cur[0] == '[') {
  315. struct ast_category *newcat = NULL;
  316. char *catname;
  317. /* A category header */
  318. c = strchr(cur, ']');
  319. if (!c) {
  320. ast_log(LOG_WARNING, "parse error: no closing ']', line %d of %s\n", lineno, configfile);
  321. return -1;
  322. }
  323. *c++ = '\0';
  324. cur++;
  325. if (*c++ != '(')
  326. c = NULL;
  327. catname = cur;
  328. *cat = newcat = ast_category_new(catname);
  329. if (!newcat) {
  330. ast_log(LOG_WARNING, "Out of memory, line %d of %s\n", lineno, configfile);
  331. return -1;
  332. }
  333. /* If there are options or categories to inherit from, process them now */
  334. if (c) {
  335. if (!(cur = strchr(c, ')'))) {
  336. ast_log(LOG_WARNING, "parse error: no closing ')', line %d of %s\n", lineno, configfile);
  337. return -1;
  338. }
  339. *cur = '\0';
  340. while ((cur = strsep(&c, ","))) {
  341. if (!strcasecmp(cur, "!")) {
  342. (*cat)->ignored = 1;
  343. } else if (!strcasecmp(cur, "+")) {
  344. *cat = category_get(cfg, catname, 1);
  345. if (!*cat) {
  346. if (newcat)
  347. ast_category_destroy(newcat);
  348. ast_log(LOG_WARNING, "Category addition requested, but category '%s' does not exist, line %d of %s\n", catname, lineno, configfile);
  349. return -1;
  350. }
  351. if (newcat) {
  352. move_variables(newcat, *cat);
  353. ast_category_destroy(newcat);
  354. newcat = NULL;
  355. }
  356. } else {
  357. struct ast_category *base;
  358. base = category_get(cfg, cur, 1);
  359. if (!base) {
  360. ast_log(LOG_WARNING, "Inheritance requested, but category '%s' does not exist, line %d of %s\n", cur, lineno, configfile);
  361. return -1;
  362. }
  363. inherit_category(*cat, base);
  364. }
  365. }
  366. }
  367. if (newcat)
  368. ast_category_append(cfg, *cat);
  369. } else if (cur[0] == '#') {
  370. /* A directive */
  371. cur++;
  372. c = cur;
  373. while(*c && (*c > 32)) c++;
  374. if (*c) {
  375. *c = '\0';
  376. c++;
  377. /* Find real argument */
  378. while(*c && (*c < 33)) c++;
  379. if (!*c)
  380. c = NULL;
  381. } else
  382. c = NULL;
  383. do_include = !strcasecmp(cur, "include");
  384. if(!do_include)
  385. do_exec = !strcasecmp(cur, "exec");
  386. else
  387. do_exec = 0;
  388. if (do_exec && !option_exec_includes) {
  389. ast_log(LOG_WARNING, "Cannot perform #exec unless execincludes option is enabled in asterisk.conf (options section)!\n");
  390. do_exec = 0;
  391. }
  392. if (do_include || do_exec) {
  393. if (c) {
  394. /* Strip off leading and trailing "'s and <>'s */
  395. while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
  396. /* Get rid of leading mess */
  397. cur = c;
  398. while (!ast_strlen_zero(cur)) {
  399. c = cur + strlen(cur) - 1;
  400. if ((*c == '>') || (*c == '<') || (*c == '\"'))
  401. *c = '\0';
  402. else
  403. break;
  404. }
  405. /* #exec </path/to/executable>
  406. We create a tmp file, then we #include it, then we delete it. */
  407. if (do_exec) {
  408. snprintf(exec_file, sizeof(exec_file), "/var/tmp/exec.%d.%ld", (int)time(NULL), (long)pthread_self());
  409. snprintf(cmd, sizeof(cmd), "%s > %s 2>&1", cur, exec_file);
  410. ast_safe_system(cmd);
  411. cur = exec_file;
  412. } else
  413. exec_file[0] = '\0';
  414. /* A #include */
  415. do_include = ast_config_internal_load(cur, cfg) ? 1 : 0;
  416. if(!ast_strlen_zero(exec_file))
  417. unlink(exec_file);
  418. if(!do_include)
  419. return 0;
  420. } else {
  421. ast_log(LOG_WARNING, "Directive '#%s' needs an argument (%s) at line %d of %s\n",
  422. do_exec ? "exec" : "include",
  423. do_exec ? "/path/to/executable" : "filename",
  424. lineno,
  425. configfile);
  426. }
  427. }
  428. else
  429. ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
  430. } else {
  431. /* Just a line (variable = value) */
  432. if (!*cat) {
  433. ast_log(LOG_WARNING,
  434. "parse error: No category context for line %d of %s\n", lineno, configfile);
  435. return -1;
  436. }
  437. c = strchr(cur, '=');
  438. if (c) {
  439. *c = 0;
  440. c++;
  441. /* Ignore > in => */
  442. if (*c== '>') {
  443. object = 1;
  444. c++;
  445. } else
  446. object = 0;
  447. v = ast_variable_new(ast_strip(cur), ast_strip(c));
  448. if (v) {
  449. v->lineno = lineno;
  450. v->object = object;
  451. /* Put and reset comments */
  452. v->blanklines = 0;
  453. ast_variable_append(*cat, v);
  454. } else {
  455. ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
  456. return -1;
  457. }
  458. } else {
  459. ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
  460. }
  461. }
  462. return 0;
  463. }
  464. static struct ast_config *config_text_file_load(const char *database, const char *table, const char *filename, struct ast_config *cfg)
  465. {
  466. char fn[256];
  467. char buf[8192];
  468. char *new_buf, *comment_p, *process_buf;
  469. FILE *f;
  470. int lineno=0;
  471. int comment = 0, nest[MAX_NESTED_COMMENTS];
  472. struct ast_category *cat = NULL;
  473. int count = 0;
  474. struct stat statbuf;
  475. cat = ast_config_get_current_category(cfg);
  476. if (filename[0] == '/') {
  477. ast_copy_string(fn, filename, sizeof(fn));
  478. } else {
  479. snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, filename);
  480. }
  481. #ifdef AST_INCLUDE_GLOB
  482. {
  483. int glob_ret;
  484. glob_t globbuf;
  485. globbuf.gl_offs = 0; /* initialize it to silence gcc */
  486. #ifdef SOLARIS
  487. glob_ret = glob(fn, GLOB_NOCHECK, NULL, &globbuf);
  488. #else
  489. glob_ret = glob(fn, GLOB_NOMAGIC|GLOB_BRACE, NULL, &globbuf);
  490. #endif
  491. if (glob_ret == GLOB_NOSPACE)
  492. ast_log(LOG_WARNING,
  493. "Glob Expansion of pattern '%s' failed: Not enough memory\n", fn);
  494. else if (glob_ret == GLOB_ABORTED)
  495. ast_log(LOG_WARNING,
  496. "Glob Expansion of pattern '%s' failed: Read error\n", fn);
  497. else {
  498. /* loop over expanded files */
  499. int i;
  500. for (i=0; i<globbuf.gl_pathc; i++) {
  501. ast_copy_string(fn, globbuf.gl_pathv[i], sizeof(fn));
  502. #endif
  503. do {
  504. if (stat(fn, &statbuf))
  505. continue;
  506. if (!S_ISREG(statbuf.st_mode)) {
  507. ast_log(LOG_WARNING, "'%s' is not a regular file, ignoring\n", fn);
  508. continue;
  509. }
  510. if (option_verbose > 1) {
  511. ast_verbose(VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
  512. fflush(stdout);
  513. }
  514. if (!(f = fopen(fn, "r"))) {
  515. if (option_debug)
  516. ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
  517. if (option_verbose > 1)
  518. ast_verbose( "Not found (%s)\n", strerror(errno));
  519. continue;
  520. }
  521. count++;
  522. if (option_debug)
  523. ast_log(LOG_DEBUG, "Parsing %s\n", fn);
  524. if (option_verbose > 1)
  525. ast_verbose("Found\n");
  526. while(!feof(f)) {
  527. lineno++;
  528. if (fgets(buf, sizeof(buf), f)) {
  529. new_buf = buf;
  530. if (comment)
  531. process_buf = NULL;
  532. else
  533. process_buf = buf;
  534. while ((comment_p = strchr(new_buf, COMMENT_META))) {
  535. if ((comment_p > new_buf) && (*(comment_p-1) == '\\')) {
  536. /* Yuck, gotta memmove */
  537. memmove(comment_p - 1, comment_p, strlen(comment_p) + 1);
  538. new_buf = comment_p;
  539. } else if(comment_p[1] == COMMENT_TAG && comment_p[2] == COMMENT_TAG && (comment_p[3] != '-')) {
  540. /* Meta-Comment start detected ";--" */
  541. if (comment < MAX_NESTED_COMMENTS) {
  542. *comment_p = '\0';
  543. new_buf = comment_p + 3;
  544. comment++;
  545. nest[comment-1] = lineno;
  546. } else {
  547. ast_log(LOG_ERROR, "Maximum nest limit of %d reached.\n", MAX_NESTED_COMMENTS);
  548. }
  549. } else if ((comment_p >= new_buf + 2) &&
  550. (*(comment_p - 1) == COMMENT_TAG) &&
  551. (*(comment_p - 2) == COMMENT_TAG)) {
  552. /* Meta-Comment end detected */
  553. comment--;
  554. new_buf = comment_p + 1;
  555. if (!comment) {
  556. /* Back to non-comment now */
  557. if (process_buf) {
  558. /* Actually have to move what's left over the top, then continue */
  559. char *oldptr;
  560. oldptr = process_buf + strlen(process_buf);
  561. memmove(oldptr, new_buf, strlen(new_buf) + 1);
  562. new_buf = oldptr;
  563. } else
  564. process_buf = new_buf;
  565. }
  566. } else {
  567. if (!comment) {
  568. /* If ; is found, and we are not nested in a comment,
  569. we immediately stop all comment processing */
  570. *comment_p = '\0';
  571. new_buf = comment_p;
  572. } else
  573. new_buf = comment_p + 1;
  574. }
  575. }
  576. if (process_buf) {
  577. char *buf = ast_strip(process_buf);
  578. if (!ast_strlen_zero(buf)) {
  579. if (process_text_line(cfg, &cat, buf, lineno, fn)) {
  580. cfg = NULL;
  581. break;
  582. }
  583. }
  584. }
  585. }
  586. }
  587. fclose(f);
  588. } while(0);
  589. if (comment) {
  590. ast_log(LOG_WARNING,"Unterminated comment detected beginning on line %d\n", nest[comment - 1]);
  591. }
  592. #ifdef AST_INCLUDE_GLOB
  593. if (!cfg)
  594. break;
  595. }
  596. globfree(&globbuf);
  597. }
  598. }
  599. #endif
  600. if (count == 0)
  601. return NULL;
  602. return cfg;
  603. }
  604. int config_text_file_save(const char *configfile, const struct ast_config *cfg, const char *generator)
  605. {
  606. FILE *f;
  607. char fn[256];
  608. char date[256]="";
  609. time_t t;
  610. struct ast_variable *var;
  611. struct ast_category *cat;
  612. int blanklines = 0;
  613. if (configfile[0] == '/') {
  614. ast_copy_string(fn, configfile, sizeof(fn));
  615. } else {
  616. snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
  617. }
  618. time(&t);
  619. ast_copy_string(date, ctime(&t), sizeof(date));
  620. #ifdef __CYGWIN__
  621. if ((f = fopen(fn, "w+"))) {
  622. #else
  623. if ((f = fopen(fn, "w"))) {
  624. #endif
  625. if (option_verbose > 1)
  626. ast_verbose(VERBOSE_PREFIX_2 "Saving '%s': ", fn);
  627. fprintf(f, ";!\n");
  628. fprintf(f, ";! Automatically generated configuration file\n");
  629. if (strcmp(configfile, fn))
  630. fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
  631. else
  632. fprintf(f, ";! Filename: %s\n", configfile);
  633. fprintf(f, ";! Generator: %s\n", generator);
  634. fprintf(f, ";! Creation Date: %s", date);
  635. fprintf(f, ";!\n");
  636. cat = cfg->root;
  637. while(cat) {
  638. /* Dump section with any appropriate comment */
  639. fprintf(f, "[%s]\n", cat->name);
  640. var = cat->root;
  641. while(var) {
  642. if (var->sameline)
  643. fprintf(f, "%s %s %s ; %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
  644. else
  645. fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
  646. if (var->blanklines) {
  647. blanklines = var->blanklines;
  648. while (blanklines--)
  649. fprintf(f, "\n");
  650. }
  651. var = var->next;
  652. }
  653. #if 0
  654. /* Put an empty line */
  655. fprintf(f, "\n");
  656. #endif
  657. cat = cat->next;
  658. }
  659. } else {
  660. if (option_debug)
  661. ast_log(LOG_DEBUG, "Unable to open for writing: %s\n", fn);
  662. if (option_verbose > 1)
  663. ast_verbose(VERBOSE_PREFIX_2 "Unable to write (%s)", strerror(errno));
  664. return -1;
  665. }
  666. fclose(f);
  667. return 0;
  668. }
  669. static void clear_config_maps(void)
  670. {
  671. struct ast_config_map *map;
  672. ast_mutex_lock(&config_lock);
  673. while (config_maps) {
  674. map = config_maps;
  675. config_maps = config_maps->next;
  676. free(map);
  677. }
  678. ast_mutex_unlock(&config_lock);
  679. }
  680. static int append_mapping(char *name, char *driver, char *database, char *table)
  681. {
  682. struct ast_config_map *map;
  683. int length;
  684. length = sizeof(*map);
  685. length += strlen(name) + 1;
  686. length += strlen(driver) + 1;
  687. length += strlen(database) + 1;
  688. if (table)
  689. length += strlen(table) + 1;
  690. map = malloc(length);
  691. if (!map)
  692. return -1;
  693. memset(map, 0, length);
  694. map->name = map->stuff;
  695. strcpy(map->name, name);
  696. map->driver = map->name + strlen(map->name) + 1;
  697. strcpy(map->driver, driver);
  698. map->database = map->driver + strlen(map->driver) + 1;
  699. strcpy(map->database, database);
  700. if (table) {
  701. map->table = map->database + strlen(map->database) + 1;
  702. strcpy(map->table, table);
  703. }
  704. map->next = config_maps;
  705. if (option_verbose > 1)
  706. ast_verbose(VERBOSE_PREFIX_2 "Binding %s to %s/%s/%s\n",
  707. map->name, map->driver, map->database, map->table ? map->table : map->name);
  708. config_maps = map;
  709. return 0;
  710. }
  711. void read_config_maps(void)
  712. {
  713. struct ast_config *config, *configtmp;
  714. struct ast_variable *v;
  715. char *driver, *table, *database, *stringp;
  716. clear_config_maps();
  717. configtmp = ast_config_new();
  718. configtmp->max_include_level = 1;
  719. config = ast_config_internal_load(extconfig_conf, configtmp);
  720. if (!config) {
  721. ast_config_destroy(configtmp);
  722. return;
  723. }
  724. for (v = ast_variable_browse(config, "settings"); v; v = v->next) {
  725. stringp = v->value;
  726. driver = strsep(&stringp, ",");
  727. database = strsep(&stringp, ",");
  728. table = strsep(&stringp, ",");
  729. if (!strcmp(v->name, extconfig_conf)) {
  730. ast_log(LOG_WARNING, "Cannot bind '%s'!\n", extconfig_conf);
  731. continue;
  732. }
  733. if (!strcmp(v->name, "asterisk.conf")) {
  734. ast_log(LOG_WARNING, "Cannot bind 'asterisk.conf'!\n");
  735. continue;
  736. }
  737. if (!strcmp(v->name, "logger.conf")) {
  738. ast_log(LOG_WARNING, "Cannot bind 'logger.conf'!\n");
  739. continue;
  740. }
  741. if (!driver || !database)
  742. continue;
  743. if (!strcasecmp(v->name, "sipfriends")) {
  744. ast_log(LOG_WARNING, "The 'sipfriends' table is obsolete, update your config to use sipusers and sippeers, though they can point to the same table.\n");
  745. append_mapping("sipusers", driver, database, table ? table : "sipfriends");
  746. append_mapping("sippeers", driver, database, table ? table : "sipfriends");
  747. } else if (!strcasecmp(v->name, "iaxfriends")) {
  748. ast_log(LOG_WARNING, "The 'iaxfriends' table is obsolete, update your config to use iaxusers and iaxpeers, though they can point to the same table.\n");
  749. append_mapping("iaxusers", driver, database, table ? table : "iaxfriends");
  750. append_mapping("iaxpeers", driver, database, table ? table : "iaxfriends");
  751. } else
  752. append_mapping(v->name, driver, database, table);
  753. }
  754. ast_config_destroy(config);
  755. }
  756. int ast_config_engine_register(struct ast_config_engine *new)
  757. {
  758. struct ast_config_engine *ptr;
  759. ast_mutex_lock(&config_lock);
  760. if (!config_engine_list) {
  761. config_engine_list = new;
  762. } else {
  763. for (ptr = config_engine_list; ptr->next; ptr=ptr->next);
  764. ptr->next = new;
  765. }
  766. ast_mutex_unlock(&config_lock);
  767. ast_log(LOG_NOTICE,"Registered Config Engine %s\n", new->name);
  768. return 1;
  769. }
  770. int ast_config_engine_deregister(struct ast_config_engine *del)
  771. {
  772. struct ast_config_engine *ptr, *last=NULL;
  773. ast_mutex_lock(&config_lock);
  774. for (ptr = config_engine_list; ptr; ptr=ptr->next) {
  775. if (ptr == del) {
  776. if (last)
  777. last->next = ptr->next;
  778. else
  779. config_engine_list = ptr->next;
  780. break;
  781. }
  782. last = ptr;
  783. }
  784. ast_mutex_unlock(&config_lock);
  785. return 0;
  786. }
  787. /*--- find_engine: Find realtime engine for realtime family */
  788. static struct ast_config_engine *find_engine(const char *family, char *database, int dbsiz, char *table, int tabsiz)
  789. {
  790. struct ast_config_engine *eng, *ret = NULL;
  791. struct ast_config_map *map;
  792. ast_mutex_lock(&config_lock);
  793. for (map = config_maps; map; map = map->next) {
  794. if (!strcasecmp(family, map->name)) {
  795. if (database)
  796. ast_copy_string(database, map->database, dbsiz);
  797. if (table)
  798. ast_copy_string(table, map->table ? map->table : family, tabsiz);
  799. break;
  800. }
  801. }
  802. /* Check if the required driver (engine) exist */
  803. if (map) {
  804. for (eng = config_engine_list; !ret && eng; eng = eng->next) {
  805. if (!strcasecmp(eng->name, map->driver))
  806. ret = eng;
  807. }
  808. }
  809. ast_mutex_unlock(&config_lock);
  810. /* if we found a mapping, but the engine is not available, then issue a warning */
  811. if (map && !ret)
  812. ast_log(LOG_WARNING, "Realtime mapping for '%s' found to engine '%s', but the engine is not available\n", map->name, map->driver);
  813. return ret;
  814. }
  815. static struct ast_config_engine text_file_engine = {
  816. .name = "text",
  817. .load_func = config_text_file_load,
  818. };
  819. struct ast_config *ast_config_internal_load(const char *filename, struct ast_config *cfg)
  820. {
  821. char db[256];
  822. char table[256];
  823. struct ast_config_engine *loader = &text_file_engine;
  824. struct ast_config *result;
  825. if (cfg->include_level == cfg->max_include_level) {
  826. ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", cfg->max_include_level);
  827. return NULL;
  828. }
  829. cfg->include_level++;
  830. if (strcmp(filename, extconfig_conf) && strcmp(filename, "asterisk.conf") && config_engine_list) {
  831. struct ast_config_engine *eng;
  832. eng = find_engine(filename, db, sizeof(db), table, sizeof(table));
  833. if (eng && eng->load_func) {
  834. loader = eng;
  835. } else {
  836. eng = find_engine("global", db, sizeof(db), table, sizeof(table));
  837. if (eng && eng->load_func)
  838. loader = eng;
  839. }
  840. }
  841. result = loader->load_func(db, table, filename, cfg);
  842. if (result)
  843. result->include_level--;
  844. else
  845. cfg->include_level--;
  846. return result;
  847. }
  848. struct ast_config *ast_config_load(const char *filename)
  849. {
  850. struct ast_config *cfg;
  851. struct ast_config *result;
  852. cfg = ast_config_new();
  853. if (!cfg)
  854. return NULL;
  855. result = ast_config_internal_load(filename, cfg);
  856. if (!result)
  857. ast_config_destroy(cfg);
  858. return result;
  859. }
  860. struct ast_variable *ast_load_realtime(const char *family, ...)
  861. {
  862. struct ast_config_engine *eng;
  863. char db[256]="";
  864. char table[256]="";
  865. struct ast_variable *res=NULL;
  866. va_list ap;
  867. va_start(ap, family);
  868. eng = find_engine(family, db, sizeof(db), table, sizeof(table));
  869. if (eng && eng->realtime_func)
  870. res = eng->realtime_func(db, table, ap);
  871. va_end(ap);
  872. return res;
  873. }
  874. /*--- ast_check_realtime: Check if realtime engine is configured for family */
  875. int ast_check_realtime(const char *family)
  876. {
  877. struct ast_config_engine *eng;
  878. eng = find_engine(family, NULL, 0, NULL, 0);
  879. if (eng)
  880. return 1;
  881. return 0;
  882. }
  883. struct ast_config *ast_load_realtime_multientry(const char *family, ...)
  884. {
  885. struct ast_config_engine *eng;
  886. char db[256]="";
  887. char table[256]="";
  888. struct ast_config *res=NULL;
  889. va_list ap;
  890. va_start(ap, family);
  891. eng = find_engine(family, db, sizeof(db), table, sizeof(table));
  892. if (eng && eng->realtime_multi_func)
  893. res = eng->realtime_multi_func(db, table, ap);
  894. va_end(ap);
  895. return res;
  896. }
  897. int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
  898. {
  899. struct ast_config_engine *eng;
  900. int res = -1;
  901. char db[256]="";
  902. char table[256]="";
  903. va_list ap;
  904. va_start(ap, lookup);
  905. eng = find_engine(family, db, sizeof(db), table, sizeof(table));
  906. if (eng && eng->update_func)
  907. res = eng->update_func(db, table, keyfield, lookup, ap);
  908. va_end(ap);
  909. return res;
  910. }
  911. static int config_command(int fd, int argc, char **argv)
  912. {
  913. struct ast_config_engine *eng;
  914. struct ast_config_map *map;
  915. ast_mutex_lock(&config_lock);
  916. ast_cli(fd, "\n\n");
  917. for (eng = config_engine_list; eng; eng = eng->next) {
  918. ast_cli(fd, "\nConfig Engine: %s\n", eng->name);
  919. for (map = config_maps; map; map = map->next)
  920. if (!strcasecmp(map->driver, eng->name)) {
  921. ast_cli(fd, "===> %s (db=%s, table=%s)\n", map->name, map->database,
  922. map->table ? map->table : map->name);
  923. }
  924. }
  925. ast_cli(fd,"\n\n");
  926. ast_mutex_unlock(&config_lock);
  927. return 0;
  928. }
  929. static char show_config_help[] =
  930. "Usage: show config mappings\n"
  931. " Shows the filenames to config engines.\n";
  932. static struct ast_cli_entry config_command_struct = {
  933. { "show", "config", "mappings", NULL }, config_command, "Show Config mappings (file names to config engines)", show_config_help, NULL
  934. };
  935. int register_config_cli()
  936. {
  937. return ast_cli_register(&config_command_struct);
  938. }