config.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376
  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. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * Includes the Asterisk Realtime API - ARA
  25. * See doc/realtime.txt and doc/extconfig.txt
  26. */
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/paths.h" /* use ast_config_AST_CONFIG_DIR */
  30. #include "asterisk/network.h" /* we do some sockaddr manipulation here */
  31. #include <time.h>
  32. #include <sys/stat.h>
  33. #include <math.h> /* HUGE_VAL */
  34. #define AST_INCLUDE_GLOB 1
  35. #include "asterisk/config.h"
  36. #include "asterisk/cli.h"
  37. #include "asterisk/lock.h"
  38. #include "asterisk/utils.h"
  39. #include "asterisk/channel.h"
  40. #include "asterisk/app.h"
  41. #include "asterisk/astobj2.h"
  42. #include "asterisk/strings.h" /* for the ast_str_*() API */
  43. #define MAX_NESTED_COMMENTS 128
  44. #define COMMENT_START ";--"
  45. #define COMMENT_END "--;"
  46. #define COMMENT_META ';'
  47. #define COMMENT_TAG '-'
  48. static char *extconfig_conf = "extconfig.conf";
  49. /*! \brief Structure to keep comments for rewriting configuration files */
  50. struct ast_comment {
  51. struct ast_comment *next;
  52. char cmt[0];
  53. };
  54. /*! \brief Hold the mtime for config files, so if we don't need to reread our config, don't. */
  55. struct cache_file_include {
  56. AST_LIST_ENTRY(cache_file_include) list;
  57. char include[0];
  58. };
  59. struct cache_file_mtime {
  60. AST_LIST_ENTRY(cache_file_mtime) list;
  61. AST_LIST_HEAD(includes, cache_file_include) includes;
  62. unsigned int has_exec:1;
  63. time_t mtime;
  64. char *who_asked;
  65. char filename[0];
  66. };
  67. static AST_LIST_HEAD_STATIC(cfmtime_head, cache_file_mtime);
  68. /* comment buffers are better implemented using the ast_str_*() API */
  69. #define CB_SIZE 250 /* initial size of comment buffers */
  70. static void CB_ADD(struct ast_str **cb, const char *str)
  71. {
  72. ast_str_append(cb, 0, "%s", str);
  73. }
  74. static void CB_ADD_LEN(struct ast_str **cb, const char *str, int len)
  75. {
  76. char *s = alloca(len + 1);
  77. ast_copy_string(s, str, len);
  78. ast_str_append(cb, 0, "%s", str);
  79. }
  80. static void CB_RESET(struct ast_str *cb, struct ast_str *llb)
  81. {
  82. if (cb)
  83. cb->used = 0;
  84. if (llb)
  85. llb->used = 0;
  86. }
  87. static struct ast_comment *ALLOC_COMMENT(const struct ast_str *buffer)
  88. {
  89. struct ast_comment *x = NULL;
  90. if (buffer && buffer->used)
  91. x = ast_calloc(1, sizeof(*x) + buffer->used + 1);
  92. if (x)
  93. strcpy(x->cmt, buffer->str);
  94. return x;
  95. }
  96. /* I need to keep track of each config file, and all its inclusions,
  97. so that we can track blank lines in each */
  98. struct inclfile {
  99. char *fname;
  100. int lineno;
  101. };
  102. static int hash_string(const void *obj, const int flags)
  103. {
  104. char *str = ((struct inclfile*)obj)->fname;
  105. int total;
  106. for (total=0; *str; str++) {
  107. unsigned int tmp = total;
  108. total <<= 1; /* multiply by 2 */
  109. total += tmp; /* multiply by 3 */
  110. total <<= 2; /* multiply by 12 */
  111. total += tmp; /* multiply by 13 */
  112. total += ((unsigned int)(*str));
  113. }
  114. if (total < 0)
  115. total = -total;
  116. return total;
  117. }
  118. static int hashtab_compare_strings(void *a, void *b, int flags)
  119. {
  120. const struct inclfile *ae = a, *be = b;
  121. return !strcmp(ae->fname, be->fname) ? CMP_MATCH : 0;
  122. }
  123. static struct ast_config_map {
  124. struct ast_config_map *next;
  125. char *name;
  126. char *driver;
  127. char *database;
  128. char *table;
  129. char stuff[0];
  130. } *config_maps = NULL;
  131. AST_MUTEX_DEFINE_STATIC(config_lock);
  132. static struct ast_config_engine *config_engine_list;
  133. #define MAX_INCLUDE_LEVEL 10
  134. struct ast_category_template_instance {
  135. char name[80]; /* redundant? */
  136. const struct ast_category *inst;
  137. AST_LIST_ENTRY(ast_category_template_instance) next;
  138. };
  139. struct ast_category {
  140. char name[80];
  141. int ignored; /*!< do not let user of the config see this category -- set by (!) after the category decl; a template */
  142. int include_level;
  143. char *file; /*!< the file name from whence this declaration was read */
  144. int lineno;
  145. AST_LIST_HEAD_NOLOCK(template_instance_list, ast_category_template_instance) template_instances;
  146. struct ast_comment *precomments;
  147. struct ast_comment *sameline;
  148. struct ast_comment *trailing; /*!< the last object in the list will get assigned any trailing comments when EOF is hit */
  149. struct ast_variable *root;
  150. struct ast_variable *last;
  151. struct ast_category *next;
  152. };
  153. struct ast_config {
  154. struct ast_category *root;
  155. struct ast_category *last;
  156. struct ast_category *current;
  157. struct ast_category *last_browse; /*!< used to cache the last category supplied via category_browse */
  158. int include_level;
  159. int max_include_level;
  160. struct ast_config_include *includes; /*!< a list of inclusions, which should describe the entire tree */
  161. };
  162. struct ast_config_include {
  163. char *include_location_file; /*!< file name in which the include occurs */
  164. int include_location_lineno; /*!< lineno where include occurred */
  165. int exec; /*!< set to non-zero if itsa #exec statement */
  166. char *exec_file; /*!< if it's an exec, you'll have both the /var/tmp to read, and the original script */
  167. char *included_file; /*!< file name included */
  168. int inclusion_count; /*!< if the file is included more than once, a running count thereof -- but, worry not,
  169. we explode the instances and will include those-- so all entries will be unique */
  170. int output; /*!< a flag to indicate if the inclusion has been output */
  171. struct ast_config_include *next; /*!< ptr to next inclusion in the list */
  172. };
  173. struct ast_variable *ast_variable_new(const char *name, const char *value, const char *filename)
  174. {
  175. struct ast_variable *variable;
  176. int name_len = strlen(name) + 1;
  177. int val_len = strlen(value) + 1;
  178. int fn_len = strlen(filename) + 1;
  179. if ((variable = ast_calloc(1, name_len + val_len + fn_len + sizeof(*variable)))) {
  180. char *dst = variable->stuff; /* writable space starts here */
  181. variable->name = strcpy(dst, name);
  182. dst += name_len;
  183. variable->value = strcpy(dst, value);
  184. dst += val_len;
  185. variable->file = strcpy(dst, filename);
  186. }
  187. return variable;
  188. }
  189. struct ast_config_include *ast_include_new(struct ast_config *conf, const char *from_file, const char *included_file, int is_exec, const char *exec_file, int from_lineno, char *real_included_file_name, int real_included_file_name_size)
  190. {
  191. /* a file should be included ONCE. Otherwise, if one of the instances is changed,
  192. * then all be changed. -- how do we know to include it? -- Handling modified
  193. * instances is possible, I'd have
  194. * to create a new master for each instance. */
  195. struct ast_config_include *inc;
  196. struct stat statbuf;
  197. inc = ast_include_find(conf, included_file);
  198. if (inc) {
  199. do {
  200. inc->inclusion_count++;
  201. snprintf(real_included_file_name, real_included_file_name_size, "%s~~%d", included_file, inc->inclusion_count);
  202. } while (stat(real_included_file_name, &statbuf) == 0);
  203. ast_log(LOG_WARNING,"'%s', line %d: Same File included more than once! This data will be saved in %s if saved back to disk.\n", from_file, from_lineno, real_included_file_name);
  204. } else
  205. *real_included_file_name = 0;
  206. inc = ast_calloc(1,sizeof(struct ast_config_include));
  207. inc->include_location_file = ast_strdup(from_file);
  208. inc->include_location_lineno = from_lineno;
  209. if (!ast_strlen_zero(real_included_file_name))
  210. inc->included_file = ast_strdup(real_included_file_name);
  211. else
  212. inc->included_file = ast_strdup(included_file);
  213. inc->exec = is_exec;
  214. if (is_exec)
  215. inc->exec_file = ast_strdup(exec_file);
  216. /* attach this new struct to the conf struct */
  217. inc->next = conf->includes;
  218. conf->includes = inc;
  219. return inc;
  220. }
  221. void ast_include_rename(struct ast_config *conf, const char *from_file, const char *to_file)
  222. {
  223. struct ast_config_include *incl;
  224. struct ast_category *cat;
  225. struct ast_variable *v;
  226. int from_len = strlen(from_file);
  227. int to_len = strlen(to_file);
  228. if (strcmp(from_file, to_file) == 0) /* no use wasting time if the name is the same */
  229. return;
  230. /* the manager code allows you to read in one config file, then
  231. * write it back out under a different name. But, the new arrangement
  232. * ties output lines to the file name. So, before you try to write
  233. * the config file to disk, better riffle thru the data and make sure
  234. * the file names are changed.
  235. */
  236. /* file names are on categories, includes (of course), and on variables. So,
  237. * traverse all this and swap names */
  238. for (incl = conf->includes; incl; incl=incl->next) {
  239. if (strcmp(incl->include_location_file,from_file) == 0) {
  240. if (from_len >= to_len)
  241. strcpy(incl->include_location_file, to_file);
  242. else {
  243. free(incl->include_location_file);
  244. incl->include_location_file = strdup(to_file);
  245. }
  246. }
  247. }
  248. for (cat = conf->root; cat; cat = cat->next) {
  249. if (strcmp(cat->file,from_file) == 0) {
  250. if (from_len >= to_len)
  251. strcpy(cat->file, to_file);
  252. else {
  253. free(cat->file);
  254. cat->file = strdup(to_file);
  255. }
  256. }
  257. for (v = cat->root; v; v = v->next) {
  258. if (strcmp(v->file,from_file) == 0) {
  259. if (from_len >= to_len)
  260. strcpy(v->file, to_file);
  261. else {
  262. free(v->file);
  263. v->file = strdup(to_file);
  264. }
  265. }
  266. }
  267. }
  268. }
  269. struct ast_config_include *ast_include_find(struct ast_config *conf, const char *included_file)
  270. {
  271. struct ast_config_include *x;
  272. for (x=conf->includes;x;x=x->next) {
  273. if (strcmp(x->included_file,included_file) == 0)
  274. return x;
  275. }
  276. return 0;
  277. }
  278. void ast_variable_append(struct ast_category *category, struct ast_variable *variable)
  279. {
  280. if (!variable)
  281. return;
  282. if (category->last)
  283. category->last->next = variable;
  284. else
  285. category->root = variable;
  286. category->last = variable;
  287. while (category->last->next)
  288. category->last = category->last->next;
  289. }
  290. void ast_variable_insert(struct ast_category *category, struct ast_variable *variable, const char *line)
  291. {
  292. struct ast_variable *cur = category->root;
  293. int lineno;
  294. int insertline;
  295. if (!variable || sscanf(line, "%30d", &insertline) != 1)
  296. return;
  297. if (!insertline) {
  298. variable->next = category->root;
  299. category->root = variable;
  300. } else {
  301. for (lineno = 1; lineno < insertline; lineno++) {
  302. cur = cur->next;
  303. if (!cur->next)
  304. break;
  305. }
  306. variable->next = cur->next;
  307. cur->next = variable;
  308. }
  309. }
  310. static void ast_comment_destroy(struct ast_comment **comment)
  311. {
  312. struct ast_comment *n, *p;
  313. for (p = *comment; p; p = n) {
  314. n = p->next;
  315. ast_free(p);
  316. }
  317. *comment = NULL;
  318. }
  319. void ast_variables_destroy(struct ast_variable *v)
  320. {
  321. struct ast_variable *vn;
  322. while (v) {
  323. vn = v;
  324. v = v->next;
  325. ast_comment_destroy(&vn->precomments);
  326. ast_comment_destroy(&vn->sameline);
  327. ast_comment_destroy(&vn->trailing);
  328. ast_free(vn);
  329. }
  330. }
  331. struct ast_variable *ast_variable_browse(const struct ast_config *config, const char *category)
  332. {
  333. struct ast_category *cat = NULL;
  334. if (category && config->last_browse && (config->last_browse->name == category))
  335. cat = config->last_browse;
  336. else
  337. cat = ast_category_get(config, category);
  338. return (cat) ? cat->root : NULL;
  339. }
  340. const char *ast_config_option(struct ast_config *cfg, const char *cat, const char *var)
  341. {
  342. const char *tmp;
  343. tmp = ast_variable_retrieve(cfg, cat, var);
  344. if (!tmp)
  345. tmp = ast_variable_retrieve(cfg, "general", var);
  346. return tmp;
  347. }
  348. const char *ast_variable_retrieve(const struct ast_config *config, const char *category, const char *variable)
  349. {
  350. struct ast_variable *v;
  351. if (category) {
  352. for (v = ast_variable_browse(config, category); v; v = v->next) {
  353. if (!strcasecmp(variable, v->name))
  354. return v->value;
  355. }
  356. } else {
  357. struct ast_category *cat;
  358. for (cat = config->root; cat; cat = cat->next)
  359. for (v = cat->root; v; v = v->next)
  360. if (!strcasecmp(variable, v->name))
  361. return v->value;
  362. }
  363. return NULL;
  364. }
  365. static struct ast_variable *variable_clone(const struct ast_variable *old)
  366. {
  367. struct ast_variable *new = ast_variable_new(old->name, old->value, old->file);
  368. if (new) {
  369. new->lineno = old->lineno;
  370. new->object = old->object;
  371. new->blanklines = old->blanklines;
  372. /* TODO: clone comments? */
  373. }
  374. return new;
  375. }
  376. static void move_variables(struct ast_category *old, struct ast_category *new)
  377. {
  378. struct ast_variable *var = old->root;
  379. old->root = NULL;
  380. /* we can just move the entire list in a single op */
  381. ast_variable_append(new, var);
  382. }
  383. struct ast_category *ast_category_new(const char *name, const char *in_file, int lineno)
  384. {
  385. struct ast_category *category;
  386. if ((category = ast_calloc(1, sizeof(*category))))
  387. ast_copy_string(category->name, name, sizeof(category->name));
  388. category->file = strdup(in_file);
  389. category->lineno = lineno; /* if you don't know the lineno, set it to 999999 or something real big */
  390. return category;
  391. }
  392. static struct ast_category *category_get(const struct ast_config *config, const char *category_name, int ignored)
  393. {
  394. struct ast_category *cat;
  395. /* try exact match first, then case-insensitive match */
  396. for (cat = config->root; cat; cat = cat->next) {
  397. if (cat->name == category_name && (ignored || !cat->ignored))
  398. return cat;
  399. }
  400. for (cat = config->root; cat; cat = cat->next) {
  401. if (!strcasecmp(cat->name, category_name) && (ignored || !cat->ignored))
  402. return cat;
  403. }
  404. return NULL;
  405. }
  406. struct ast_category *ast_category_get(const struct ast_config *config, const char *category_name)
  407. {
  408. return category_get(config, category_name, 0);
  409. }
  410. int ast_category_exist(const struct ast_config *config, const char *category_name)
  411. {
  412. return !!ast_category_get(config, category_name);
  413. }
  414. void ast_category_append(struct ast_config *config, struct ast_category *category)
  415. {
  416. if (config->last)
  417. config->last->next = category;
  418. else
  419. config->root = category;
  420. category->include_level = config->include_level;
  421. config->last = category;
  422. config->current = category;
  423. }
  424. void ast_category_insert(struct ast_config *config, struct ast_category *cat, const char *match)
  425. {
  426. struct ast_category *cur_category;
  427. if (!cat || !match)
  428. return;
  429. if (!strcasecmp(config->root->name, match)) {
  430. cat->next = config->root;
  431. config->root = cat;
  432. return;
  433. }
  434. for (cur_category = config->root; cur_category; cur_category = cur_category->next) {
  435. if (!strcasecmp(cur_category->next->name, match)) {
  436. cat->next = cur_category->next;
  437. cur_category->next = cat;
  438. break;
  439. }
  440. }
  441. }
  442. static void ast_destroy_template_list(struct ast_category *cat)
  443. {
  444. struct ast_category_template_instance *x;
  445. while ((x = AST_LIST_REMOVE_HEAD(&cat->template_instances, next)))
  446. free(x);
  447. }
  448. void ast_category_destroy(struct ast_category *cat)
  449. {
  450. ast_variables_destroy(cat->root);
  451. if (cat->file) {
  452. free(cat->file);
  453. cat->file = 0;
  454. }
  455. ast_comment_destroy(&cat->precomments);
  456. ast_comment_destroy(&cat->sameline);
  457. ast_comment_destroy(&cat->trailing);
  458. ast_destroy_template_list(cat);
  459. ast_free(cat);
  460. }
  461. static void ast_includes_destroy(struct ast_config_include *incls)
  462. {
  463. struct ast_config_include *incl,*inclnext;
  464. for (incl=incls; incl; incl = inclnext) {
  465. inclnext = incl->next;
  466. if (incl->include_location_file)
  467. free(incl->include_location_file);
  468. if (incl->exec_file)
  469. free(incl->exec_file);
  470. if (incl->included_file)
  471. free(incl->included_file);
  472. free(incl);
  473. }
  474. }
  475. static struct ast_category *next_available_category(struct ast_category *cat)
  476. {
  477. for (; cat && cat->ignored; cat = cat->next);
  478. return cat;
  479. }
  480. /*! return the first var of a category */
  481. struct ast_variable *ast_category_first(struct ast_category *cat)
  482. {
  483. return (cat) ? cat->root : NULL;
  484. }
  485. struct ast_variable *ast_category_root(struct ast_config *config, char *cat)
  486. {
  487. struct ast_category *category = ast_category_get(config, cat);
  488. if (category)
  489. return category->root;
  490. return NULL;
  491. }
  492. char *ast_category_browse(struct ast_config *config, const char *prev)
  493. {
  494. struct ast_category *cat = NULL;
  495. if (prev && config->last_browse && (config->last_browse->name == prev))
  496. cat = config->last_browse->next;
  497. else if (!prev && config->root)
  498. cat = config->root;
  499. else if (prev) {
  500. for (cat = config->root; cat; cat = cat->next) {
  501. if (cat->name == prev) {
  502. cat = cat->next;
  503. break;
  504. }
  505. }
  506. if (!cat) {
  507. for (cat = config->root; cat; cat = cat->next) {
  508. if (!strcasecmp(cat->name, prev)) {
  509. cat = cat->next;
  510. break;
  511. }
  512. }
  513. }
  514. }
  515. if (cat)
  516. cat = next_available_category(cat);
  517. config->last_browse = cat;
  518. return (cat) ? cat->name : NULL;
  519. }
  520. struct ast_variable *ast_category_detach_variables(struct ast_category *cat)
  521. {
  522. struct ast_variable *v;
  523. v = cat->root;
  524. cat->root = NULL;
  525. cat->last = NULL;
  526. return v;
  527. }
  528. void ast_category_rename(struct ast_category *cat, const char *name)
  529. {
  530. ast_copy_string(cat->name, name, sizeof(cat->name));
  531. }
  532. static void inherit_category(struct ast_category *new, const struct ast_category *base)
  533. {
  534. struct ast_variable *var;
  535. struct ast_category_template_instance *x = ast_calloc(1,sizeof(struct ast_category_template_instance));
  536. strcpy(x->name, base->name);
  537. x->inst = base;
  538. AST_LIST_INSERT_TAIL(&new->template_instances, x, next);
  539. for (var = base->root; var; var = var->next)
  540. ast_variable_append(new, variable_clone(var));
  541. }
  542. struct ast_config *ast_config_new(void)
  543. {
  544. struct ast_config *config;
  545. if ((config = ast_calloc(1, sizeof(*config))))
  546. config->max_include_level = MAX_INCLUDE_LEVEL;
  547. return config;
  548. }
  549. int ast_variable_delete(struct ast_category *category, const char *variable, const char *match, const char *line)
  550. {
  551. struct ast_variable *cur, *prev=NULL, *curn;
  552. int res = -1;
  553. int lineno = 0;
  554. cur = category->root;
  555. while (cur) {
  556. if (cur->name == variable) {
  557. if (prev) {
  558. prev->next = cur->next;
  559. if (cur == category->last)
  560. category->last = prev;
  561. } else {
  562. category->root = cur->next;
  563. if (cur == category->last)
  564. category->last = NULL;
  565. }
  566. cur->next = NULL;
  567. ast_variables_destroy(cur);
  568. return 0;
  569. }
  570. prev = cur;
  571. cur = cur->next;
  572. }
  573. prev = NULL;
  574. cur = category->root;
  575. while (cur) {
  576. curn = cur->next;
  577. if ((!ast_strlen_zero(line) && lineno == atoi(line)) || (ast_strlen_zero(line) && !strcasecmp(cur->name, variable) && (ast_strlen_zero(match) || !strcasecmp(cur->value, match)))) {
  578. if (prev) {
  579. prev->next = cur->next;
  580. if (cur == category->last)
  581. category->last = prev;
  582. } else {
  583. category->root = cur->next;
  584. if (cur == category->last)
  585. category->last = NULL;
  586. }
  587. cur->next = NULL;
  588. ast_variables_destroy(cur);
  589. res = 0;
  590. } else
  591. prev = cur;
  592. cur = curn;
  593. lineno++;
  594. }
  595. return res;
  596. }
  597. int ast_variable_update(struct ast_category *category, const char *variable,
  598. const char *value, const char *match, unsigned int object)
  599. {
  600. struct ast_variable *cur, *prev=NULL, *newer=NULL;
  601. for (cur = category->root; cur; prev = cur, cur = cur->next) {
  602. if (strcasecmp(cur->name, variable) ||
  603. (!ast_strlen_zero(match) && strcasecmp(cur->value, match)))
  604. continue;
  605. if (!(newer = ast_variable_new(variable, value, cur->file)))
  606. return -1;
  607. newer->next = cur->next;
  608. newer->object = cur->object || object;
  609. /* Preserve everything */
  610. newer->lineno = cur->lineno;
  611. newer->blanklines = cur->blanklines;
  612. newer->precomments = cur->precomments; cur->precomments = NULL;
  613. newer->sameline = cur->sameline; cur->sameline = NULL;
  614. newer->trailing = cur->trailing; cur->trailing = NULL;
  615. if (prev)
  616. prev->next = newer;
  617. else
  618. category->root = newer;
  619. if (category->last == cur)
  620. category->last = newer;
  621. cur->next = NULL;
  622. ast_variables_destroy(cur);
  623. return 0;
  624. }
  625. if (prev)
  626. prev->next = newer;
  627. else
  628. category->root = newer;
  629. return 0;
  630. }
  631. int ast_category_delete(struct ast_config *cfg, const char *category)
  632. {
  633. struct ast_category *prev=NULL, *cat;
  634. cat = cfg->root;
  635. while (cat) {
  636. if (cat->name == category) {
  637. if (prev) {
  638. prev->next = cat->next;
  639. if (cat == cfg->last)
  640. cfg->last = prev;
  641. } else {
  642. cfg->root = cat->next;
  643. if (cat == cfg->last)
  644. cfg->last = NULL;
  645. }
  646. ast_category_destroy(cat);
  647. return 0;
  648. }
  649. prev = cat;
  650. cat = cat->next;
  651. }
  652. prev = NULL;
  653. cat = cfg->root;
  654. while (cat) {
  655. if (!strcasecmp(cat->name, category)) {
  656. if (prev) {
  657. prev->next = cat->next;
  658. if (cat == cfg->last)
  659. cfg->last = prev;
  660. } else {
  661. cfg->root = cat->next;
  662. if (cat == cfg->last)
  663. cfg->last = NULL;
  664. }
  665. ast_category_destroy(cat);
  666. return 0;
  667. }
  668. prev = cat;
  669. cat = cat->next;
  670. }
  671. return -1;
  672. }
  673. int ast_category_empty(struct ast_config *cfg, const char *category)
  674. {
  675. struct ast_category *cat;
  676. for (cat = cfg->root; cat; cat = cat->next) {
  677. if (!strcasecmp(cat->name, category))
  678. continue;
  679. ast_variables_destroy(cat->root);
  680. cat->root = NULL;
  681. cat->last = NULL;
  682. return 0;
  683. }
  684. return -1;
  685. }
  686. void ast_config_destroy(struct ast_config *cfg)
  687. {
  688. struct ast_category *cat, *catn;
  689. if (!cfg)
  690. return;
  691. ast_includes_destroy(cfg->includes);
  692. cat = cfg->root;
  693. while (cat) {
  694. catn = cat;
  695. cat = cat->next;
  696. ast_category_destroy(catn);
  697. }
  698. ast_free(cfg);
  699. }
  700. struct ast_category *ast_config_get_current_category(const struct ast_config *cfg)
  701. {
  702. return cfg->current;
  703. }
  704. void ast_config_set_current_category(struct ast_config *cfg, const struct ast_category *cat)
  705. {
  706. /* cast below is just to silence compiler warning about dropping "const" */
  707. cfg->current = (struct ast_category *) cat;
  708. }
  709. enum config_cache_attribute_enum {
  710. ATTRIBUTE_INCLUDE = 0,
  711. ATTRIBUTE_EXEC = 1,
  712. };
  713. static void config_cache_attribute(const char *configfile, enum config_cache_attribute_enum attrtype, const char *filename, const char *who_asked)
  714. {
  715. struct cache_file_mtime *cfmtime;
  716. struct cache_file_include *cfinclude;
  717. struct stat statbuf = { 0, };
  718. /* Find our cached entry for this configuration file */
  719. AST_LIST_LOCK(&cfmtime_head);
  720. AST_LIST_TRAVERSE(&cfmtime_head, cfmtime, list) {
  721. if (!strcmp(cfmtime->filename, configfile) && !strcmp(cfmtime->who_asked, who_asked))
  722. break;
  723. }
  724. if (!cfmtime) {
  725. cfmtime = ast_calloc(1, sizeof(*cfmtime) + strlen(configfile) + 1 + strlen(who_asked) + 1);
  726. if (!cfmtime) {
  727. AST_LIST_UNLOCK(&cfmtime_head);
  728. return;
  729. }
  730. AST_LIST_HEAD_INIT(&cfmtime->includes);
  731. strcpy(cfmtime->filename, configfile);
  732. cfmtime->who_asked = cfmtime->filename + strlen(configfile) + 1;
  733. strcpy(cfmtime->who_asked, who_asked);
  734. /* Note that the file mtime is initialized to 0, i.e. 1970 */
  735. AST_LIST_INSERT_TAIL(&cfmtime_head, cfmtime, list);
  736. }
  737. if (!stat(configfile, &statbuf))
  738. cfmtime->mtime = 0;
  739. else
  740. cfmtime->mtime = statbuf.st_mtime;
  741. switch (attrtype) {
  742. case ATTRIBUTE_INCLUDE:
  743. AST_LIST_TRAVERSE(&cfmtime->includes, cfinclude, list) {
  744. if (!strcmp(cfinclude->include, filename)) {
  745. AST_LIST_UNLOCK(&cfmtime_head);
  746. return;
  747. }
  748. }
  749. cfinclude = ast_calloc(1, sizeof(*cfinclude) + strlen(filename) + 1);
  750. if (!cfinclude) {
  751. AST_LIST_UNLOCK(&cfmtime_head);
  752. return;
  753. }
  754. strcpy(cfinclude->include, filename);
  755. AST_LIST_INSERT_TAIL(&cfmtime->includes, cfinclude, list);
  756. break;
  757. case ATTRIBUTE_EXEC:
  758. cfmtime->has_exec = 1;
  759. break;
  760. }
  761. AST_LIST_UNLOCK(&cfmtime_head);
  762. }
  763. /*! \brief parse one line in the configuration.
  764. * \verbatim
  765. * We can have a category header [foo](...)
  766. * a directive #include / #exec
  767. * or a regular line name = value
  768. * \endverbatim
  769. */
  770. static int process_text_line(struct ast_config *cfg, struct ast_category **cat,
  771. char *buf, int lineno, const char *configfile, struct ast_flags flags,
  772. struct ast_str *comment_buffer,
  773. struct ast_str *lline_buffer,
  774. const char *suggested_include_file,
  775. struct ast_category **last_cat, struct ast_variable **last_var, const char *who_asked)
  776. {
  777. char *c;
  778. char *cur = buf;
  779. struct ast_variable *v;
  780. char cmd[512], exec_file[512];
  781. /* Actually parse the entry */
  782. if (cur[0] == '[') { /* A category header */
  783. /* format is one of the following:
  784. * [foo] define a new category named 'foo'
  785. * [foo](!) define a new template category named 'foo'
  786. * [foo](+) append to category 'foo', error if foo does not exist.
  787. * [foo](a) define a new category and inherit from template a.
  788. * You can put a comma-separated list of templates and '!' and '+'
  789. * between parentheses, with obvious meaning.
  790. */
  791. struct ast_category *newcat = NULL;
  792. char *catname;
  793. c = strchr(cur, ']');
  794. if (!c) {
  795. ast_log(LOG_WARNING, "parse error: no closing ']', line %d of %s\n", lineno, configfile);
  796. return -1;
  797. }
  798. *c++ = '\0';
  799. cur++;
  800. if (*c++ != '(')
  801. c = NULL;
  802. catname = cur;
  803. if (!(*cat = newcat = ast_category_new(catname,
  804. S_OR(suggested_include_file, cfg->include_level == 1 ? "" : configfile),
  805. lineno))) {
  806. return -1;
  807. }
  808. (*cat)->lineno = lineno;
  809. *last_var = 0;
  810. *last_cat = newcat;
  811. /* add comments */
  812. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS))
  813. newcat->precomments = ALLOC_COMMENT(comment_buffer);
  814. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS))
  815. newcat->sameline = ALLOC_COMMENT(lline_buffer);
  816. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS))
  817. CB_RESET(comment_buffer, lline_buffer);
  818. /* If there are options or categories to inherit from, process them now */
  819. if (c) {
  820. if (!(cur = strchr(c, ')'))) {
  821. ast_log(LOG_WARNING, "parse error: no closing ')', line %d of %s\n", lineno, configfile);
  822. return -1;
  823. }
  824. *cur = '\0';
  825. while ((cur = strsep(&c, ","))) {
  826. if (!strcasecmp(cur, "!")) {
  827. (*cat)->ignored = 1;
  828. } else if (!strcasecmp(cur, "+")) {
  829. *cat = category_get(cfg, catname, 1);
  830. if (!(*cat)) {
  831. if (newcat)
  832. ast_category_destroy(newcat);
  833. ast_log(LOG_WARNING, "Category addition requested, but category '%s' does not exist, line %d of %s\n", catname, lineno, configfile);
  834. return -1;
  835. }
  836. if (newcat) {
  837. move_variables(newcat, *cat);
  838. ast_category_destroy(newcat);
  839. newcat = NULL;
  840. }
  841. } else {
  842. struct ast_category *base;
  843. base = category_get(cfg, cur, 1);
  844. if (!base) {
  845. ast_log(LOG_WARNING, "Inheritance requested, but category '%s' does not exist, line %d of %s\n", cur, lineno, configfile);
  846. return -1;
  847. }
  848. inherit_category(*cat, base);
  849. }
  850. }
  851. }
  852. if (newcat)
  853. ast_category_append(cfg, *cat);
  854. } else if (cur[0] == '#') { /* A directive - #include or #exec */
  855. char *cur2;
  856. char real_inclusion_name[256];
  857. struct ast_config_include *inclu;
  858. int do_include = 0; /* otherwise, it is exec */
  859. cur++;
  860. c = cur;
  861. while (*c && (*c > 32)) c++;
  862. if (*c) {
  863. *c = '\0';
  864. /* Find real argument */
  865. c = ast_skip_blanks(c + 1);
  866. if (!(*c))
  867. c = NULL;
  868. } else
  869. c = NULL;
  870. if (!strcasecmp(cur, "include")) {
  871. do_include = 1;
  872. } else if (!strcasecmp(cur, "exec")) {
  873. if (!ast_opt_exec_includes) {
  874. ast_log(LOG_WARNING, "Cannot perform #exec unless execincludes option is enabled in asterisk.conf (options section)!\n");
  875. return 0; /* XXX is this correct ? or we should return -1 ? */
  876. }
  877. } else {
  878. ast_log(LOG_WARNING, "Unknown directive '#%s' at line %d of %s\n", cur, lineno, configfile);
  879. return 0; /* XXX is this correct ? or we should return -1 ? */
  880. }
  881. if (c == NULL) {
  882. ast_log(LOG_WARNING, "Directive '#%s' needs an argument (%s) at line %d of %s\n",
  883. do_include ? "include" : "exec",
  884. do_include ? "filename" : "/path/to/executable",
  885. lineno,
  886. configfile);
  887. return 0; /* XXX is this correct ? or we should return -1 ? */
  888. }
  889. cur = c;
  890. /* Strip off leading and trailing "'s and <>'s */
  891. if (*c == '"') {
  892. /* Dequote */
  893. while (*c) {
  894. if (*c == '"') {
  895. strcpy(c, c + 1); /* SAFE */
  896. c--;
  897. } else if (*c == '\\') {
  898. strcpy(c, c + 1); /* SAFE */
  899. }
  900. c++;
  901. }
  902. } else if (*c == '<') {
  903. /* C-style include */
  904. if (*(c + strlen(c) - 1) == '>') {
  905. cur++;
  906. *(c + strlen(c) - 1) = '\0';
  907. }
  908. }
  909. cur2 = cur;
  910. /* #exec </path/to/executable>
  911. We create a tmp file, then we #include it, then we delete it. */
  912. if (!do_include) {
  913. struct timeval tv = ast_tvnow();
  914. if (!ast_test_flag(&flags, CONFIG_FLAG_NOCACHE))
  915. config_cache_attribute(configfile, ATTRIBUTE_EXEC, NULL, who_asked);
  916. snprintf(exec_file, sizeof(exec_file), "/var/tmp/exec.%d%d.%ld", (int)tv.tv_sec, (int)tv.tv_usec, (long)pthread_self());
  917. snprintf(cmd, sizeof(cmd), "%s > %s 2>&1", cur, exec_file);
  918. ast_safe_system(cmd);
  919. cur = exec_file;
  920. } else {
  921. if (!ast_test_flag(&flags, CONFIG_FLAG_NOCACHE))
  922. config_cache_attribute(configfile, ATTRIBUTE_INCLUDE, cur, who_asked);
  923. exec_file[0] = '\0';
  924. }
  925. /* A #include */
  926. /* record this inclusion */
  927. inclu = ast_include_new(cfg, cfg->include_level == 1 ? "" : configfile, cur, !do_include, cur2, lineno, real_inclusion_name, sizeof(real_inclusion_name));
  928. do_include = ast_config_internal_load(cur, cfg, flags, real_inclusion_name, who_asked) ? 1 : 0;
  929. if (!ast_strlen_zero(exec_file))
  930. unlink(exec_file);
  931. if (!do_include) {
  932. ast_log(LOG_ERROR, "The file '%s' was listed as a #include but it does not exist.\n", cur);
  933. return -1;
  934. }
  935. /* XXX otherwise what ? the default return is 0 anyways */
  936. } else {
  937. /* Just a line (variable = value) */
  938. if (!(*cat)) {
  939. ast_log(LOG_WARNING,
  940. "parse error: No category context for line %d of %s\n", lineno, configfile);
  941. return -1;
  942. }
  943. c = strchr(cur, '=');
  944. if (c) {
  945. int object;
  946. *c = 0;
  947. c++;
  948. /* Ignore > in => */
  949. if (*c== '>') {
  950. object = 1;
  951. c++;
  952. } else
  953. object = 0;
  954. if ((v = ast_variable_new(ast_strip(cur), ast_strip(c), S_OR(suggested_include_file, cfg->include_level == 1 ? "" : configfile)))) {
  955. v->lineno = lineno;
  956. v->object = object;
  957. *last_cat = 0;
  958. *last_var = v;
  959. /* Put and reset comments */
  960. v->blanklines = 0;
  961. ast_variable_append(*cat, v);
  962. /* add comments */
  963. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS))
  964. v->precomments = ALLOC_COMMENT(comment_buffer);
  965. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS))
  966. v->sameline = ALLOC_COMMENT(lline_buffer);
  967. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS))
  968. CB_RESET(comment_buffer, lline_buffer);
  969. } else {
  970. return -1;
  971. }
  972. } else {
  973. ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
  974. }
  975. }
  976. return 0;
  977. }
  978. static struct ast_config *config_text_file_load(const char *database, const char *table, const char *filename, struct ast_config *cfg, struct ast_flags flags, const char *suggested_include_file, const char *who_asked)
  979. {
  980. char fn[256];
  981. #if defined(LOW_MEMORY)
  982. char buf[512];
  983. #else
  984. char buf[8192];
  985. #endif
  986. char *new_buf, *comment_p, *process_buf;
  987. FILE *f;
  988. int lineno=0;
  989. int comment = 0, nest[MAX_NESTED_COMMENTS];
  990. struct ast_category *cat = NULL;
  991. int count = 0;
  992. struct stat statbuf;
  993. struct cache_file_mtime *cfmtime = NULL;
  994. struct cache_file_include *cfinclude;
  995. struct ast_variable *last_var = 0;
  996. struct ast_category *last_cat = 0;
  997. /*! Growable string buffer */
  998. struct ast_str *comment_buffer = NULL; /*!< this will be a comment collector.*/
  999. struct ast_str *lline_buffer = NULL; /*!< A buffer for stuff behind the ; */
  1000. if (cfg)
  1001. cat = ast_config_get_current_category(cfg);
  1002. if (filename[0] == '/') {
  1003. ast_copy_string(fn, filename, sizeof(fn));
  1004. } else {
  1005. snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, filename);
  1006. }
  1007. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS)) {
  1008. comment_buffer = ast_str_create(CB_SIZE);
  1009. if (comment_buffer)
  1010. lline_buffer = ast_str_create(CB_SIZE);
  1011. if (!lline_buffer) {
  1012. if (comment_buffer)
  1013. ast_free(comment_buffer);
  1014. ast_log(LOG_ERROR, "Failed to initialize the comment buffer!\n");
  1015. return NULL;
  1016. }
  1017. }
  1018. #ifdef AST_INCLUDE_GLOB
  1019. {
  1020. int glob_ret;
  1021. glob_t globbuf;
  1022. globbuf.gl_offs = 0; /* initialize it to silence gcc */
  1023. glob_ret = glob(fn, MY_GLOB_FLAGS, NULL, &globbuf);
  1024. if (glob_ret == GLOB_NOSPACE)
  1025. ast_log(LOG_WARNING,
  1026. "Glob Expansion of pattern '%s' failed: Not enough memory\n", fn);
  1027. else if (glob_ret == GLOB_ABORTED)
  1028. ast_log(LOG_WARNING,
  1029. "Glob Expansion of pattern '%s' failed: Read error\n", fn);
  1030. else {
  1031. /* loop over expanded files */
  1032. int i;
  1033. for (i=0; i<globbuf.gl_pathc; i++) {
  1034. ast_copy_string(fn, globbuf.gl_pathv[i], sizeof(fn));
  1035. #endif
  1036. /*
  1037. * The following is not a loop, but just a convenient way to define a block
  1038. * (using do { } while(0) ), and be able to exit from it with 'continue'
  1039. * or 'break' in case of errors. Nice trick.
  1040. */
  1041. do {
  1042. if (stat(fn, &statbuf))
  1043. continue;
  1044. if (!S_ISREG(statbuf.st_mode)) {
  1045. ast_log(LOG_WARNING, "'%s' is not a regular file, ignoring\n", fn);
  1046. continue;
  1047. }
  1048. if (!ast_test_flag(&flags, CONFIG_FLAG_NOCACHE)) {
  1049. /* Find our cached entry for this configuration file */
  1050. AST_LIST_LOCK(&cfmtime_head);
  1051. AST_LIST_TRAVERSE(&cfmtime_head, cfmtime, list) {
  1052. if (!strcmp(cfmtime->filename, fn) && !strcmp(cfmtime->who_asked, who_asked))
  1053. break;
  1054. }
  1055. if (!cfmtime) {
  1056. cfmtime = ast_calloc(1, sizeof(*cfmtime) + strlen(fn) + 1 + strlen(who_asked) + 1);
  1057. if (!cfmtime)
  1058. continue;
  1059. AST_LIST_HEAD_INIT(&cfmtime->includes);
  1060. strcpy(cfmtime->filename, fn);
  1061. cfmtime->who_asked = cfmtime->filename + strlen(fn) + 1;
  1062. strcpy(cfmtime->who_asked, who_asked);
  1063. /* Note that the file mtime is initialized to 0, i.e. 1970 */
  1064. AST_LIST_INSERT_TAIL(&cfmtime_head, cfmtime, list);
  1065. }
  1066. }
  1067. if (cfmtime && (!cfmtime->has_exec) && (cfmtime->mtime == statbuf.st_mtime) && ast_test_flag(&flags, CONFIG_FLAG_FILEUNCHANGED)) {
  1068. /* File is unchanged, what about the (cached) includes (if any)? */
  1069. int unchanged = 1;
  1070. AST_LIST_TRAVERSE(&cfmtime->includes, cfinclude, list) {
  1071. /* We must glob here, because if we did not, then adding a file to globbed directory would
  1072. * incorrectly cause no reload to be necessary. */
  1073. char fn2[256];
  1074. #ifdef AST_INCLUDE_GLOB
  1075. int glob_ret;
  1076. glob_t globbuf = { .gl_offs = 0 };
  1077. glob_ret = glob(cfinclude->include, MY_GLOB_FLAGS, NULL, &globbuf);
  1078. /* On error, we reparse */
  1079. if (glob_ret == GLOB_NOSPACE || glob_ret == GLOB_ABORTED)
  1080. unchanged = 0;
  1081. else {
  1082. /* loop over expanded files */
  1083. int j;
  1084. for (j = 0; j < globbuf.gl_pathc; j++) {
  1085. ast_copy_string(fn2, globbuf.gl_pathv[j], sizeof(fn2));
  1086. #else
  1087. ast_copy_string(fn2, cfinclude->include);
  1088. #endif
  1089. if (config_text_file_load(NULL, NULL, fn2, NULL, flags, "", who_asked) == NULL) {
  1090. /* that second-to-last field needs to be looked at in this case... TODO */
  1091. unchanged = 0;
  1092. /* One change is enough to short-circuit and reload the whole shebang */
  1093. break;
  1094. }
  1095. #ifdef AST_INCLUDE_GLOB
  1096. }
  1097. }
  1098. #endif
  1099. }
  1100. if (unchanged) {
  1101. AST_LIST_UNLOCK(&cfmtime_head);
  1102. return CONFIG_STATUS_FILEUNCHANGED;
  1103. }
  1104. }
  1105. if (!ast_test_flag(&flags, CONFIG_FLAG_NOCACHE))
  1106. AST_LIST_UNLOCK(&cfmtime_head);
  1107. /* If cfg is NULL, then we just want an answer */
  1108. if (cfg == NULL)
  1109. return NULL;
  1110. if (cfmtime)
  1111. cfmtime->mtime = statbuf.st_mtime;
  1112. ast_verb(2, "Parsing '%s': ", fn);
  1113. fflush(stdout);
  1114. if (!(f = fopen(fn, "r"))) {
  1115. ast_debug(1, "No file to parse: %s\n", fn);
  1116. ast_verb(2, "Not found (%s)\n", strerror(errno));
  1117. continue;
  1118. }
  1119. count++;
  1120. /* If we get to this point, then we're loading regardless */
  1121. ast_clear_flag(&flags, CONFIG_FLAG_FILEUNCHANGED);
  1122. ast_debug(1, "Parsing %s\n", fn);
  1123. ast_verb(2, "Found\n");
  1124. while (!feof(f)) {
  1125. lineno++;
  1126. if (fgets(buf, sizeof(buf), f)) {
  1127. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS) && lline_buffer && lline_buffer->used) {
  1128. CB_ADD(&comment_buffer, lline_buffer->str); /* add the current lline buffer to the comment buffer */
  1129. lline_buffer->used = 0; /* erase the lline buffer */
  1130. }
  1131. new_buf = buf;
  1132. if (comment)
  1133. process_buf = NULL;
  1134. else
  1135. process_buf = buf;
  1136. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS) && comment_buffer && comment_buffer->used && (ast_strlen_zero(buf) || strlen(buf) == strspn(buf," \t\n\r"))) {
  1137. /* blank line? really? Can we add it to an existing comment and maybe preserve inter- and post- comment spacing? */
  1138. CB_ADD(&comment_buffer, "\n"); /* add a newline to the comment buffer */
  1139. continue; /* go get a new line, then */
  1140. }
  1141. while ((comment_p = strchr(new_buf, COMMENT_META))) {
  1142. if ((comment_p > new_buf) && (*(comment_p-1) == '\\')) {
  1143. /* Escaped semicolons aren't comments. */
  1144. new_buf = comment_p + 1;
  1145. } else if (comment_p[1] == COMMENT_TAG && comment_p[2] == COMMENT_TAG && (comment_p[3] != '-')) {
  1146. /* Meta-Comment start detected ";--" */
  1147. if (comment < MAX_NESTED_COMMENTS) {
  1148. *comment_p = '\0';
  1149. new_buf = comment_p + 3;
  1150. comment++;
  1151. nest[comment-1] = lineno;
  1152. } else {
  1153. ast_log(LOG_ERROR, "Maximum nest limit of %d reached.\n", MAX_NESTED_COMMENTS);
  1154. }
  1155. } else if ((comment_p >= new_buf + 2) &&
  1156. (*(comment_p - 1) == COMMENT_TAG) &&
  1157. (*(comment_p - 2) == COMMENT_TAG)) {
  1158. /* Meta-Comment end detected */
  1159. comment--;
  1160. new_buf = comment_p + 1;
  1161. if (!comment) {
  1162. /* Back to non-comment now */
  1163. if (process_buf) {
  1164. /* Actually have to move what's left over the top, then continue */
  1165. char *oldptr;
  1166. oldptr = process_buf + strlen(process_buf);
  1167. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS)) {
  1168. CB_ADD(&comment_buffer, ";");
  1169. CB_ADD_LEN(&comment_buffer, oldptr+1, new_buf-oldptr-1);
  1170. }
  1171. memmove(oldptr, new_buf, strlen(new_buf) + 1);
  1172. new_buf = oldptr;
  1173. } else
  1174. process_buf = new_buf;
  1175. }
  1176. } else {
  1177. if (!comment) {
  1178. /* If ; is found, and we are not nested in a comment,
  1179. we immediately stop all comment processing */
  1180. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS)) {
  1181. CB_ADD(&lline_buffer, comment_p);
  1182. }
  1183. *comment_p = '\0';
  1184. new_buf = comment_p;
  1185. } else
  1186. new_buf = comment_p + 1;
  1187. }
  1188. }
  1189. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS) && comment && !process_buf ) {
  1190. CB_ADD(&comment_buffer, buf); /* the whole line is a comment, store it */
  1191. }
  1192. if (process_buf) {
  1193. char *buf = ast_strip(process_buf);
  1194. if (!ast_strlen_zero(buf)) {
  1195. if (process_text_line(cfg, &cat, buf, lineno, fn, flags, comment_buffer, lline_buffer, suggested_include_file, &last_cat, &last_var, who_asked)) {
  1196. cfg = NULL;
  1197. break;
  1198. }
  1199. }
  1200. }
  1201. }
  1202. }
  1203. /* end of file-- anything in a comment buffer? */
  1204. if (last_cat) {
  1205. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS) && comment_buffer && comment_buffer->used ) {
  1206. if (lline_buffer && lline_buffer->used) {
  1207. CB_ADD(&comment_buffer, lline_buffer->str); /* add the current lline buffer to the comment buffer */
  1208. lline_buffer->used = 0; /* erase the lline buffer */
  1209. }
  1210. last_cat->trailing = ALLOC_COMMENT(comment_buffer);
  1211. }
  1212. } else if (last_var) {
  1213. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS) && comment_buffer && comment_buffer->used ) {
  1214. if (lline_buffer && lline_buffer->used) {
  1215. CB_ADD(&comment_buffer, lline_buffer->str); /* add the current lline buffer to the comment buffer */
  1216. lline_buffer->used = 0; /* erase the lline buffer */
  1217. }
  1218. last_var->trailing = ALLOC_COMMENT(comment_buffer);
  1219. }
  1220. } else {
  1221. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS) && comment_buffer && comment_buffer->used) {
  1222. ast_debug(1, "Nothing to attach comments to, discarded: %s\n", comment_buffer->str);
  1223. }
  1224. }
  1225. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS))
  1226. CB_RESET(comment_buffer, lline_buffer);
  1227. fclose(f);
  1228. } while (0);
  1229. if (comment) {
  1230. ast_log(LOG_WARNING,"Unterminated comment detected beginning on line %d\n", nest[comment - 1]);
  1231. }
  1232. #ifdef AST_INCLUDE_GLOB
  1233. if (cfg == NULL || cfg == CONFIG_STATUS_FILEUNCHANGED)
  1234. break;
  1235. }
  1236. globfree(&globbuf);
  1237. }
  1238. }
  1239. #endif
  1240. if (cfg && cfg != CONFIG_STATUS_FILEUNCHANGED && cfg->include_level == 1 && ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS)) {
  1241. if (comment_buffer)
  1242. ast_free(comment_buffer);
  1243. if (lline_buffer)
  1244. ast_free(lline_buffer);
  1245. comment_buffer = NULL;
  1246. lline_buffer = NULL;
  1247. }
  1248. if (count == 0)
  1249. return NULL;
  1250. return cfg;
  1251. }
  1252. /* NOTE: categories and variables each have a file and lineno attribute. On a save operation, these are used to determine
  1253. which file and line number to write out to. Thus, an entire hierarchy of config files (via #include statements) can be
  1254. recreated. BUT, care must be taken to make sure that every cat and var has the proper file name stored, or you may
  1255. be shocked and mystified as to why things are not showing up in the files!
  1256. Also, All #include/#exec statements are recorded in the "includes" LL in the ast_config structure. The file name
  1257. and line number are stored for each include, plus the name of the file included, so that these statements may be
  1258. included in the output files on a file_save operation.
  1259. The lineno's are really just for relative placement in the file. There is no attempt to make sure that blank lines
  1260. are included to keep the lineno's the same between input and output. The lineno fields are used mainly to determine
  1261. the position of the #include and #exec directives. So, blank lines tend to disappear from a read/rewrite operation,
  1262. and a header gets added.
  1263. vars and category heads are output in the order they are stored in the config file. So, if the software
  1264. shuffles these at all, then the placement of #include directives might get a little mixed up, because the
  1265. file/lineno data probably won't get changed.
  1266. */
  1267. static void gen_header(FILE *f1, const char *configfile, const char *fn, const char *generator)
  1268. {
  1269. char date[256]="";
  1270. time_t t;
  1271. time(&t);
  1272. ast_copy_string(date, ctime(&t), sizeof(date));
  1273. fprintf(f1, ";!\n");
  1274. fprintf(f1, ";! Automatically generated configuration file\n");
  1275. if (strcmp(configfile, fn))
  1276. fprintf(f1, ";! Filename: %s (%s)\n", configfile, fn);
  1277. else
  1278. fprintf(f1, ";! Filename: %s\n", configfile);
  1279. fprintf(f1, ";! Generator: %s\n", generator);
  1280. fprintf(f1, ";! Creation Date: %s", date);
  1281. fprintf(f1, ";!\n");
  1282. }
  1283. static void inclfile_destroy(void *obj)
  1284. {
  1285. const struct inclfile *o = obj;
  1286. if (o->fname)
  1287. free(o->fname);
  1288. }
  1289. static void set_fn(char *fn, int fn_size, const char *file, const char *configfile, struct ao2_container *fileset, struct inclfile **fi)
  1290. {
  1291. struct inclfile lookup;
  1292. if (!file || file[0] == 0) {
  1293. if (configfile[0] == '/')
  1294. ast_copy_string(fn, configfile, fn_size);
  1295. else
  1296. snprintf(fn, fn_size, "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
  1297. } else if (file[0] == '/')
  1298. ast_copy_string(fn, file, fn_size);
  1299. else
  1300. snprintf(fn, fn_size, "%s/%s", ast_config_AST_CONFIG_DIR, file);
  1301. lookup.fname = fn;
  1302. *fi = ao2_find(fileset, &lookup, OBJ_POINTER);
  1303. if (!(*fi)) {
  1304. /* set up a file scratch pad */
  1305. struct inclfile *fx = ao2_alloc(sizeof(struct inclfile), inclfile_destroy);
  1306. fx->fname = ast_strdup(fn);
  1307. fx->lineno = 1;
  1308. *fi = fx;
  1309. ao2_link(fileset, fx);
  1310. }
  1311. }
  1312. static int count_linefeeds(char *str)
  1313. {
  1314. int count = 0;
  1315. while (*str) {
  1316. if (*str =='\n')
  1317. count++;
  1318. str++;
  1319. }
  1320. return count;
  1321. }
  1322. static int count_linefeeds_in_comments(struct ast_comment *x)
  1323. {
  1324. int count = 0;
  1325. while (x) {
  1326. count += count_linefeeds(x->cmt);
  1327. x = x->next;
  1328. }
  1329. return count;
  1330. }
  1331. static void insert_leading_blank_lines(FILE *fp, struct inclfile *fi, struct ast_comment *precomments, int lineno)
  1332. {
  1333. int precomment_lines = count_linefeeds_in_comments(precomments);
  1334. int i;
  1335. /* I don't have to worry about those ;! comments, they are
  1336. stored in the precomments, but not printed back out.
  1337. I did have to make sure that comments following
  1338. the ;! header comments were not also deleted in the process */
  1339. if (lineno - precomment_lines - fi->lineno < 0) { /* insertions can mess up the line numbering and produce negative numbers that mess things up */
  1340. return;
  1341. } else if (lineno == 0) {
  1342. /* Line replacements also mess things up */
  1343. return;
  1344. } else if (lineno - precomment_lines - fi->lineno < 5) {
  1345. /* Only insert less than 5 blank lines; if anything more occurs,
  1346. * it's probably due to context deletion. */
  1347. for (i = fi->lineno; i < lineno - precomment_lines; i++) {
  1348. fprintf(fp, "\n");
  1349. }
  1350. } else {
  1351. /* Deletion occurred - insert a single blank line, for separation of
  1352. * contexts. */
  1353. fprintf(fp, "\n");
  1354. }
  1355. fi->lineno = lineno + 1; /* Advance the file lineno */
  1356. }
  1357. int config_text_file_save(const char *configfile, const struct ast_config *cfg, const char *generator)
  1358. {
  1359. FILE *f;
  1360. char fn[256];
  1361. struct ast_variable *var;
  1362. struct ast_category *cat;
  1363. struct ast_comment *cmt;
  1364. struct ast_config_include *incl;
  1365. int blanklines = 0;
  1366. struct ao2_container *fileset = ao2_container_alloc(180000, hash_string, hashtab_compare_strings);
  1367. struct inclfile *fi = 0;
  1368. /* reset all the output flags, in case this isn't our first time saving this data */
  1369. for (incl=cfg->includes; incl; incl = incl->next)
  1370. incl->output = 0;
  1371. /* go thru all the inclusions and make sure all the files involved (configfile plus all its inclusions)
  1372. are all truncated to zero bytes and have that nice header*/
  1373. for (incl=cfg->includes; incl; incl = incl->next)
  1374. {
  1375. if (!incl->exec) { /* leave the execs alone -- we'll write out the #exec directives, but won't zero out the include files or exec files*/
  1376. FILE *f1;
  1377. set_fn(fn, sizeof(fn), incl->included_file, configfile, fileset, &fi); /* normally, fn is just set to incl->included_file, prepended with config dir if relative */
  1378. f1 = fopen(fn,"w");
  1379. if (f1) {
  1380. gen_header(f1, configfile, fn, generator);
  1381. fclose(f1); /* this should zero out the file */
  1382. } else {
  1383. ast_debug(1, "Unable to open for writing: %s\n", fn);
  1384. ast_verb(2, "Unable to write %s (%s)", fn, strerror(errno));
  1385. }
  1386. ao2_ref(fi,-1); /* we are giving up this reference to the object ptd to by fi */
  1387. fi = 0;
  1388. }
  1389. }
  1390. set_fn(fn, sizeof(fn), 0, configfile, fileset, &fi); /* just set fn to absolute ver of configfile */
  1391. #ifdef __CYGWIN__
  1392. if ((f = fopen(fn, "w+"))) {
  1393. #else
  1394. if ((f = fopen(fn, "w"))) {
  1395. #endif
  1396. ast_verb(2, "Saving '%s': ", fn);
  1397. gen_header(f, configfile, fn, generator);
  1398. cat = cfg->root;
  1399. fclose(f);
  1400. ao2_ref(fi,-1); /* we are giving up this reference to the object ptd to by fi */
  1401. /* from here out, we open each involved file and concat the stuff we need to add to the end and immediately close... */
  1402. /* since each var, cat, and associated comments can come from any file, we have to be
  1403. mobile, and open each file, print, and close it on an entry-by-entry basis */
  1404. while (cat) {
  1405. set_fn(fn, sizeof(fn), cat->file, configfile, fileset, &fi);
  1406. f = fopen(fn, "a");
  1407. if (!f)
  1408. {
  1409. ast_debug(1, "Unable to open for writing: %s\n", fn);
  1410. ast_verb(2, "Unable to write %s (%s)", fn, strerror(errno));
  1411. ao2_ref(fileset, -1);
  1412. return -1;
  1413. }
  1414. /* dump any includes that happen before this category header */
  1415. for (incl=cfg->includes; incl; incl = incl->next) {
  1416. if (strcmp(incl->include_location_file, cat->file) == 0){
  1417. if (cat->lineno > incl->include_location_lineno && !incl->output) {
  1418. if (incl->exec)
  1419. fprintf(f,"#exec \"%s\"\n", incl->exec_file);
  1420. else
  1421. fprintf(f,"#include \"%s\"\n", incl->included_file);
  1422. incl->output = 1;
  1423. }
  1424. }
  1425. }
  1426. insert_leading_blank_lines(f, fi, cat->precomments, cat->lineno);
  1427. /* Dump section with any appropriate comment */
  1428. for (cmt = cat->precomments; cmt; cmt=cmt->next) {
  1429. char *cmtp = cmt->cmt;
  1430. while (*cmtp == ';' && *(cmtp+1) == '!') {
  1431. char *cmtp2 = strchr(cmtp+1, '\n');
  1432. if (cmtp2)
  1433. cmtp = cmtp2+1;
  1434. else cmtp = 0;
  1435. }
  1436. if (cmtp)
  1437. fprintf(f,"%s", cmtp);
  1438. }
  1439. fprintf(f, "[%s]", cat->name);
  1440. if (cat->ignored || !AST_LIST_EMPTY(&cat->template_instances)) {
  1441. fprintf(f, "(");
  1442. if (cat->ignored) {
  1443. fprintf(f, "!");
  1444. }
  1445. if (cat->ignored && !AST_LIST_EMPTY(&cat->template_instances)) {
  1446. fprintf(f, ",");
  1447. }
  1448. if (!AST_LIST_EMPTY(&cat->template_instances)) {
  1449. struct ast_category_template_instance *x;
  1450. AST_LIST_TRAVERSE(&cat->template_instances, x, next) {
  1451. fprintf(f,"%s",x->name);
  1452. if (x != AST_LIST_LAST(&cat->template_instances))
  1453. fprintf(f,",");
  1454. }
  1455. }
  1456. fprintf(f, ")");
  1457. }
  1458. for(cmt = cat->sameline; cmt; cmt=cmt->next)
  1459. {
  1460. fprintf(f,"%s", cmt->cmt);
  1461. }
  1462. if (!cat->sameline)
  1463. fprintf(f,"\n");
  1464. for (cmt = cat->trailing; cmt; cmt=cmt->next) {
  1465. if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
  1466. fprintf(f,"%s", cmt->cmt);
  1467. }
  1468. fclose(f);
  1469. ao2_ref(fi,-1); /* we are giving up this reference to the object ptd to by fi */
  1470. fi = 0;
  1471. var = cat->root;
  1472. while (var) {
  1473. struct ast_category_template_instance *x;
  1474. int found = 0;
  1475. AST_LIST_TRAVERSE(&cat->template_instances, x, next) {
  1476. struct ast_variable *v;
  1477. for (v = x->inst->root; v; v = v->next) {
  1478. if (!strcasecmp(var->name, v->name) && !strcmp(var->value, v->value)) {
  1479. found = 1;
  1480. break;
  1481. }
  1482. }
  1483. if (found)
  1484. break;
  1485. }
  1486. if (found) {
  1487. var = var->next;
  1488. continue;
  1489. }
  1490. set_fn(fn, sizeof(fn), var->file, configfile, fileset, &fi);
  1491. f = fopen(fn, "a");
  1492. if (!f)
  1493. {
  1494. ast_debug(1, "Unable to open for writing: %s\n", fn);
  1495. ast_verb(2, "Unable to write %s (%s)", fn, strerror(errno));
  1496. ao2_ref(fi,-1); /* we are giving up this reference to the object ptd to by fi */
  1497. fi = 0;
  1498. ao2_ref(fileset, -1);
  1499. return -1;
  1500. }
  1501. /* dump any includes that happen before this category header */
  1502. for (incl=cfg->includes; incl; incl = incl->next) {
  1503. if (strcmp(incl->include_location_file, var->file) == 0){
  1504. if (var->lineno > incl->include_location_lineno && !incl->output) {
  1505. if (incl->exec)
  1506. fprintf(f,"#exec \"%s\"\n", incl->exec_file);
  1507. else
  1508. fprintf(f,"#include \"%s\"\n", incl->included_file);
  1509. incl->output = 1;
  1510. }
  1511. }
  1512. }
  1513. insert_leading_blank_lines(f, fi, var->precomments, var->lineno);
  1514. for (cmt = var->precomments; cmt; cmt=cmt->next) {
  1515. if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
  1516. fprintf(f,"%s", cmt->cmt);
  1517. }
  1518. if (var->sameline)
  1519. fprintf(f, "%s %s %s %s", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
  1520. else
  1521. fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
  1522. for (cmt = var->trailing; cmt; cmt=cmt->next) {
  1523. if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
  1524. fprintf(f,"%s", cmt->cmt);
  1525. }
  1526. if (var->blanklines) {
  1527. blanklines = var->blanklines;
  1528. while (blanklines--)
  1529. fprintf(f, "\n");
  1530. }
  1531. fclose(f);
  1532. ao2_ref(fi,-1); /* we are giving up this reference to the object ptd to by fi */
  1533. fi = 0;
  1534. var = var->next;
  1535. }
  1536. cat = cat->next;
  1537. }
  1538. if (!option_debug)
  1539. ast_verb(2, "Saved\n");
  1540. } else {
  1541. ast_debug(1, "Unable to open for writing: %s\n", fn);
  1542. ast_verb(2, "Unable to write (%s)", strerror(errno));
  1543. ao2_ref(fi,-1); /* we are giving up this reference to the object ptd to by fi */
  1544. ao2_ref(fileset, -1);
  1545. return -1;
  1546. }
  1547. /* Now, for files with trailing #include/#exec statements,
  1548. we have to make sure every entry is output */
  1549. for (incl=cfg->includes; incl; incl = incl->next) {
  1550. if (!incl->output) {
  1551. /* open the respective file */
  1552. set_fn(fn, sizeof(fn), incl->include_location_file, configfile, fileset, &fi);
  1553. f = fopen(fn, "a");
  1554. if (!f)
  1555. {
  1556. ast_debug(1, "Unable to open for writing: %s\n", fn);
  1557. ast_verb(2, "Unable to write %s (%s)", fn, strerror(errno));
  1558. ao2_ref(fi,-1); /* we are giving up this reference to the object ptd to by fi */
  1559. fi = 0;
  1560. ao2_ref(fileset, -1);
  1561. return -1;
  1562. }
  1563. /* output the respective include */
  1564. if (incl->exec)
  1565. fprintf(f,"#exec \"%s\"\n", incl->exec_file);
  1566. else
  1567. fprintf(f,"#include \"%s\"\n", incl->included_file);
  1568. fclose(f);
  1569. incl->output = 1;
  1570. ao2_ref(fi,-1); /* we are giving up this reference to the object ptd to by fi */
  1571. fi = 0;
  1572. }
  1573. }
  1574. ao2_ref(fileset, -1); /* this should destroy the hash container */
  1575. return 0;
  1576. }
  1577. static void clear_config_maps(void)
  1578. {
  1579. struct ast_config_map *map;
  1580. ast_mutex_lock(&config_lock);
  1581. while (config_maps) {
  1582. map = config_maps;
  1583. config_maps = config_maps->next;
  1584. ast_free(map);
  1585. }
  1586. ast_mutex_unlock(&config_lock);
  1587. }
  1588. static int append_mapping(const char *name, const char *driver, const char *database, const char *table)
  1589. {
  1590. struct ast_config_map *map;
  1591. int length;
  1592. length = sizeof(*map);
  1593. length += strlen(name) + 1;
  1594. length += strlen(driver) + 1;
  1595. length += strlen(database) + 1;
  1596. if (table)
  1597. length += strlen(table) + 1;
  1598. if (!(map = ast_calloc(1, length)))
  1599. return -1;
  1600. map->name = map->stuff;
  1601. strcpy(map->name, name);
  1602. map->driver = map->name + strlen(map->name) + 1;
  1603. strcpy(map->driver, driver);
  1604. map->database = map->driver + strlen(map->driver) + 1;
  1605. strcpy(map->database, database);
  1606. if (table) {
  1607. map->table = map->database + strlen(map->database) + 1;
  1608. strcpy(map->table, table);
  1609. }
  1610. map->next = config_maps;
  1611. ast_verb(2, "Binding %s to %s/%s/%s\n", map->name, map->driver, map->database, map->table ? map->table : map->name);
  1612. config_maps = map;
  1613. return 0;
  1614. }
  1615. int read_config_maps(void)
  1616. {
  1617. struct ast_config *config, *configtmp;
  1618. struct ast_variable *v;
  1619. char *driver, *table, *database, *stringp, *tmp;
  1620. struct ast_flags flags = { 0 };
  1621. clear_config_maps();
  1622. configtmp = ast_config_new();
  1623. configtmp->max_include_level = 1;
  1624. config = ast_config_internal_load(extconfig_conf, configtmp, flags, "", "config.c");
  1625. if (!config) {
  1626. ast_config_destroy(configtmp);
  1627. return 0;
  1628. }
  1629. for (v = ast_variable_browse(config, "settings"); v; v = v->next) {
  1630. char buf[512];
  1631. ast_copy_string(buf, v->value, sizeof(buf));
  1632. stringp = buf;
  1633. driver = strsep(&stringp, ",");
  1634. if ((tmp = strchr(stringp, '\"')))
  1635. stringp = tmp;
  1636. /* check if the database text starts with a double quote */
  1637. if (*stringp == '"') {
  1638. stringp++;
  1639. database = strsep(&stringp, "\"");
  1640. strsep(&stringp, ",");
  1641. } else {
  1642. /* apparently this text has no quotes */
  1643. database = strsep(&stringp, ",");
  1644. }
  1645. table = strsep(&stringp, ",");
  1646. if (!strcmp(v->name, extconfig_conf)) {
  1647. ast_log(LOG_WARNING, "Cannot bind '%s'!\n", extconfig_conf);
  1648. continue;
  1649. }
  1650. if (!strcmp(v->name, "asterisk.conf")) {
  1651. ast_log(LOG_WARNING, "Cannot bind 'asterisk.conf'!\n");
  1652. continue;
  1653. }
  1654. if (!strcmp(v->name, "logger.conf")) {
  1655. ast_log(LOG_WARNING, "Cannot bind 'logger.conf'!\n");
  1656. continue;
  1657. }
  1658. if (!driver || !database)
  1659. continue;
  1660. if (!strcasecmp(v->name, "sipfriends")) {
  1661. 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");
  1662. append_mapping("sipusers", driver, database, table ? table : "sipfriends");
  1663. append_mapping("sippeers", driver, database, table ? table : "sipfriends");
  1664. } else if (!strcasecmp(v->name, "iaxfriends")) {
  1665. 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");
  1666. append_mapping("iaxusers", driver, database, table ? table : "iaxfriends");
  1667. append_mapping("iaxpeers", driver, database, table ? table : "iaxfriends");
  1668. } else
  1669. append_mapping(v->name, driver, database, table);
  1670. }
  1671. ast_config_destroy(config);
  1672. return 0;
  1673. }
  1674. int ast_config_engine_register(struct ast_config_engine *new)
  1675. {
  1676. struct ast_config_engine *ptr;
  1677. ast_mutex_lock(&config_lock);
  1678. if (!config_engine_list) {
  1679. config_engine_list = new;
  1680. } else {
  1681. for (ptr = config_engine_list; ptr->next; ptr=ptr->next);
  1682. ptr->next = new;
  1683. }
  1684. ast_mutex_unlock(&config_lock);
  1685. ast_log(LOG_NOTICE,"Registered Config Engine %s\n", new->name);
  1686. return 1;
  1687. }
  1688. int ast_config_engine_deregister(struct ast_config_engine *del)
  1689. {
  1690. struct ast_config_engine *ptr, *last=NULL;
  1691. ast_mutex_lock(&config_lock);
  1692. for (ptr = config_engine_list; ptr; ptr=ptr->next) {
  1693. if (ptr == del) {
  1694. if (last)
  1695. last->next = ptr->next;
  1696. else
  1697. config_engine_list = ptr->next;
  1698. break;
  1699. }
  1700. last = ptr;
  1701. }
  1702. ast_mutex_unlock(&config_lock);
  1703. return 0;
  1704. }
  1705. /*! \brief Find realtime engine for realtime family */
  1706. static struct ast_config_engine *find_engine(const char *family, char *database, int dbsiz, char *table, int tabsiz)
  1707. {
  1708. struct ast_config_engine *eng, *ret = NULL;
  1709. struct ast_config_map *map;
  1710. ast_mutex_lock(&config_lock);
  1711. for (map = config_maps; map; map = map->next) {
  1712. if (!strcasecmp(family, map->name)) {
  1713. if (database)
  1714. ast_copy_string(database, map->database, dbsiz);
  1715. if (table)
  1716. ast_copy_string(table, map->table ? map->table : family, tabsiz);
  1717. break;
  1718. }
  1719. }
  1720. /* Check if the required driver (engine) exist */
  1721. if (map) {
  1722. for (eng = config_engine_list; !ret && eng; eng = eng->next) {
  1723. if (!strcasecmp(eng->name, map->driver))
  1724. ret = eng;
  1725. }
  1726. }
  1727. ast_mutex_unlock(&config_lock);
  1728. /* if we found a mapping, but the engine is not available, then issue a warning */
  1729. if (map && !ret)
  1730. ast_log(LOG_WARNING, "Realtime mapping for '%s' found to engine '%s', but the engine is not available\n", map->name, map->driver);
  1731. return ret;
  1732. }
  1733. static struct ast_config_engine text_file_engine = {
  1734. .name = "text",
  1735. .load_func = config_text_file_load,
  1736. };
  1737. struct ast_config *ast_config_internal_load(const char *filename, struct ast_config *cfg, struct ast_flags flags, const char *suggested_include_file, const char *who_asked)
  1738. {
  1739. char db[256];
  1740. char table[256];
  1741. struct ast_config_engine *loader = &text_file_engine;
  1742. struct ast_config *result;
  1743. /* The config file itself bumps include_level by 1 */
  1744. if (cfg->max_include_level > 0 && cfg->include_level == cfg->max_include_level + 1) {
  1745. ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", cfg->max_include_level);
  1746. return NULL;
  1747. }
  1748. cfg->include_level++;
  1749. if (strcmp(filename, extconfig_conf) && strcmp(filename, "asterisk.conf") && config_engine_list) {
  1750. struct ast_config_engine *eng;
  1751. eng = find_engine(filename, db, sizeof(db), table, sizeof(table));
  1752. if (eng && eng->load_func) {
  1753. loader = eng;
  1754. } else {
  1755. eng = find_engine("global", db, sizeof(db), table, sizeof(table));
  1756. if (eng && eng->load_func)
  1757. loader = eng;
  1758. }
  1759. }
  1760. result = loader->load_func(db, table, filename, cfg, flags, suggested_include_file, who_asked);
  1761. if (result && result != CONFIG_STATUS_FILEUNCHANGED)
  1762. result->include_level--;
  1763. else
  1764. cfg->include_level--;
  1765. return result;
  1766. }
  1767. struct ast_config *ast_config_load2(const char *filename, const char *who_asked, struct ast_flags flags)
  1768. {
  1769. struct ast_config *cfg;
  1770. struct ast_config *result;
  1771. cfg = ast_config_new();
  1772. if (!cfg)
  1773. return NULL;
  1774. result = ast_config_internal_load(filename, cfg, flags, "", who_asked);
  1775. if (!result || result == CONFIG_STATUS_FILEUNCHANGED)
  1776. ast_config_destroy(cfg);
  1777. return result;
  1778. }
  1779. static struct ast_variable *ast_load_realtime_helper(const char *family, va_list ap)
  1780. {
  1781. struct ast_config_engine *eng;
  1782. char db[256]="";
  1783. char table[256]="";
  1784. struct ast_variable *res=NULL;
  1785. eng = find_engine(family, db, sizeof(db), table, sizeof(table));
  1786. if (eng && eng->realtime_func)
  1787. res = eng->realtime_func(db, table, ap);
  1788. return res;
  1789. }
  1790. struct ast_variable *ast_load_realtime_all(const char *family, ...)
  1791. {
  1792. struct ast_variable *res;
  1793. va_list ap;
  1794. va_start(ap, family);
  1795. res = ast_load_realtime_helper(family, ap);
  1796. va_end(ap);
  1797. return res;
  1798. }
  1799. struct ast_variable *ast_load_realtime(const char *family, ...)
  1800. {
  1801. struct ast_variable *res, *cur, *prev = NULL, *freeme = NULL;
  1802. va_list ap;
  1803. va_start(ap, family);
  1804. res = ast_load_realtime_helper(family, ap);
  1805. va_end(ap);
  1806. /* Eliminate blank entries */
  1807. for (cur = res; cur; cur = cur->next) {
  1808. if (freeme) {
  1809. ast_free(freeme);
  1810. freeme = NULL;
  1811. }
  1812. if (ast_strlen_zero(cur->value)) {
  1813. if (prev)
  1814. prev->next = cur->next;
  1815. else
  1816. res = cur->next;
  1817. freeme = cur;
  1818. } else if (cur->value[0] == ' ' && cur->value[1] == '\0') {
  1819. char *vptr = (char *) cur->value;
  1820. vptr[0] = '\0';
  1821. prev = cur;
  1822. } else {
  1823. prev = cur;
  1824. }
  1825. }
  1826. return res;
  1827. }
  1828. /*! \brief Check if realtime engine is configured for family */
  1829. int ast_check_realtime(const char *family)
  1830. {
  1831. struct ast_config_engine *eng;
  1832. eng = find_engine(family, NULL, 0, NULL, 0);
  1833. if (eng)
  1834. return 1;
  1835. return 0;
  1836. }
  1837. /*! \brief Check if there's any realtime engines loaded */
  1838. int ast_realtime_enabled()
  1839. {
  1840. return config_maps ? 1 : 0;
  1841. }
  1842. struct ast_config *ast_load_realtime_multientry(const char *family, ...)
  1843. {
  1844. struct ast_config_engine *eng;
  1845. char db[256]="";
  1846. char table[256]="";
  1847. struct ast_config *res=NULL;
  1848. va_list ap;
  1849. va_start(ap, family);
  1850. eng = find_engine(family, db, sizeof(db), table, sizeof(table));
  1851. if (eng && eng->realtime_multi_func)
  1852. res = eng->realtime_multi_func(db, table, ap);
  1853. va_end(ap);
  1854. return res;
  1855. }
  1856. int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
  1857. {
  1858. struct ast_config_engine *eng;
  1859. int res = -1;
  1860. char db[256]="";
  1861. char table[256]="";
  1862. va_list ap;
  1863. va_start(ap, lookup);
  1864. eng = find_engine(family, db, sizeof(db), table, sizeof(table));
  1865. if (eng && eng->update_func)
  1866. res = eng->update_func(db, table, keyfield, lookup, ap);
  1867. va_end(ap);
  1868. return res;
  1869. }
  1870. int ast_store_realtime(const char *family, ...) {
  1871. struct ast_config_engine *eng;
  1872. int res = -1;
  1873. char db[256]="";
  1874. char table[256]="";
  1875. va_list ap;
  1876. va_start(ap, family);
  1877. eng = find_engine(family, db, sizeof(db), table, sizeof(table));
  1878. if (eng && eng->store_func)
  1879. res = eng->store_func(db, table, ap);
  1880. va_end(ap);
  1881. return res;
  1882. }
  1883. int ast_destroy_realtime(const char *family, const char *keyfield, const char *lookup, ...) {
  1884. struct ast_config_engine *eng;
  1885. int res = -1;
  1886. char db[256]="";
  1887. char table[256]="";
  1888. va_list ap;
  1889. va_start(ap, lookup);
  1890. eng = find_engine(family, db, sizeof(db), table, sizeof(table));
  1891. if (eng && eng->destroy_func)
  1892. res = eng->destroy_func(db, table, keyfield, lookup, ap);
  1893. va_end(ap);
  1894. return res;
  1895. }
  1896. /*! \brief Helper function to parse arguments
  1897. * See documentation in config.h
  1898. */
  1899. int ast_parse_arg(const char *arg, enum ast_parse_flags flags,
  1900. void *p_result, ...)
  1901. {
  1902. va_list ap;
  1903. int error = 0;
  1904. va_start(ap, p_result);
  1905. switch (flags & PARSE_TYPE) {
  1906. case PARSE_INT32:
  1907. {
  1908. int32_t *result = p_result;
  1909. int32_t x, def = result ? *result : 0,
  1910. high = (int32_t)0x7fffffff,
  1911. low = (int32_t)0x80000000;
  1912. /* optional argument: first default value, then range */
  1913. if (flags & PARSE_DEFAULT)
  1914. def = va_arg(ap, int32_t);
  1915. if (flags & (PARSE_IN_RANGE|PARSE_OUT_RANGE)) {
  1916. /* range requested, update bounds */
  1917. low = va_arg(ap, int32_t);
  1918. high = va_arg(ap, int32_t);
  1919. }
  1920. x = strtol(arg, NULL, 0);
  1921. error = (x < low) || (x > high);
  1922. if (flags & PARSE_OUT_RANGE)
  1923. error = !error;
  1924. if (result)
  1925. *result = error ? def : x;
  1926. ast_debug(3,
  1927. "extract int from [%s] in [%d, %d] gives [%d](%d)\n",
  1928. arg, low, high,
  1929. result ? *result : x, error);
  1930. break;
  1931. }
  1932. case PARSE_UINT32:
  1933. {
  1934. uint32_t *result = p_result;
  1935. uint32_t x, def = result ? *result : 0,
  1936. low = 0, high = (uint32_t)~0;
  1937. /* optional argument: first default value, then range */
  1938. if (flags & PARSE_DEFAULT)
  1939. def = va_arg(ap, uint32_t);
  1940. if (flags & (PARSE_IN_RANGE|PARSE_OUT_RANGE)) {
  1941. /* range requested, update bounds */
  1942. low = va_arg(ap, uint32_t);
  1943. high = va_arg(ap, uint32_t);
  1944. }
  1945. x = strtoul(arg, NULL, 0);
  1946. error = (x < low) || (x > high);
  1947. if (flags & PARSE_OUT_RANGE)
  1948. error = !error;
  1949. if (result)
  1950. *result = error ? def : x;
  1951. ast_debug(3,
  1952. "extract uint from [%s] in [%u, %u] gives [%u](%d)\n",
  1953. arg, low, high,
  1954. result ? *result : x, error);
  1955. break;
  1956. }
  1957. case PARSE_DOUBLE:
  1958. {
  1959. double *result = p_result;
  1960. double x, def = result ? *result : 0,
  1961. low = -HUGE_VAL, high = HUGE_VAL;
  1962. /* optional argument: first default value, then range */
  1963. if (flags & PARSE_DEFAULT)
  1964. def = va_arg(ap, double);
  1965. if (flags & (PARSE_IN_RANGE|PARSE_OUT_RANGE)) {
  1966. /* range requested, update bounds */
  1967. low = va_arg(ap, double);
  1968. high = va_arg(ap, double);
  1969. }
  1970. x = strtod(arg, NULL);
  1971. error = (x < low) || (x > high);
  1972. if (flags & PARSE_OUT_RANGE)
  1973. error = !error;
  1974. if (result)
  1975. *result = error ? def : x;
  1976. ast_debug(3,
  1977. "extract double from [%s] in [%f, %f] gives [%f](%d)\n",
  1978. arg, low, high,
  1979. result ? *result : x, error);
  1980. break;
  1981. }
  1982. case PARSE_INADDR:
  1983. {
  1984. char *port, *buf;
  1985. struct sockaddr_in _sa_buf; /* buffer for the result */
  1986. struct sockaddr_in *sa = p_result ?
  1987. (struct sockaddr_in *)p_result : &_sa_buf;
  1988. /* default is either the supplied value or the result itself */
  1989. struct sockaddr_in *def = (flags & PARSE_DEFAULT) ?
  1990. va_arg(ap, struct sockaddr_in *) : sa;
  1991. struct hostent *hp;
  1992. struct ast_hostent ahp;
  1993. memset(&_sa_buf, '\0', sizeof(_sa_buf)); /* clear buffer */
  1994. /* duplicate the string to strip away the :port */
  1995. port = ast_strdupa(arg);
  1996. buf = strsep(&port, ":");
  1997. sa->sin_family = AF_INET; /* assign family */
  1998. /*
  1999. * honor the ports flag setting, assign default value
  2000. * in case of errors or field unset.
  2001. */
  2002. flags &= PARSE_PORT_MASK; /* the only flags left to process */
  2003. if (port) {
  2004. if (flags == PARSE_PORT_FORBID) {
  2005. error = 1; /* port was forbidden */
  2006. sa->sin_port = def->sin_port;
  2007. } else if (flags == PARSE_PORT_IGNORE)
  2008. sa->sin_port = def->sin_port;
  2009. else /* accept or require */
  2010. sa->sin_port = htons(strtol(port, NULL, 0));
  2011. } else {
  2012. sa->sin_port = def->sin_port;
  2013. if (flags == PARSE_PORT_REQUIRE)
  2014. error = 1;
  2015. }
  2016. /* Now deal with host part, even if we have errors before. */
  2017. hp = ast_gethostbyname(buf, &ahp);
  2018. if (hp) /* resolved successfully */
  2019. memcpy(&sa->sin_addr, hp->h_addr, sizeof(sa->sin_addr));
  2020. else {
  2021. error = 1;
  2022. sa->sin_addr = def->sin_addr;
  2023. }
  2024. ast_debug(3,
  2025. "extract inaddr from [%s] gives [%s:%d](%d)\n",
  2026. arg, ast_inet_ntoa(sa->sin_addr),
  2027. ntohs(sa->sin_port), error);
  2028. break;
  2029. }
  2030. }
  2031. va_end(ap);
  2032. return error;
  2033. }
  2034. static char *handle_cli_core_show_config_mappings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2035. {
  2036. struct ast_config_engine *eng;
  2037. struct ast_config_map *map;
  2038. switch (cmd) {
  2039. case CLI_INIT:
  2040. e->command = "core show config mappings";
  2041. e->usage =
  2042. "Usage: core show config mappings\n"
  2043. " Shows the filenames to config engines.\n";
  2044. return NULL;
  2045. case CLI_GENERATE:
  2046. return NULL;
  2047. }
  2048. ast_mutex_lock(&config_lock);
  2049. if (!config_engine_list) {
  2050. ast_cli(a->fd, "No config mappings found.\n");
  2051. } else {
  2052. ast_cli(a->fd, "\n\n");
  2053. for (eng = config_engine_list; eng; eng = eng->next) {
  2054. ast_cli(a->fd, "\nConfig Engine: %s\n", eng->name);
  2055. for (map = config_maps; map; map = map->next) {
  2056. if (!strcasecmp(map->driver, eng->name)) {
  2057. ast_cli(a->fd, "===> %s (db=%s, table=%s)\n", map->name, map->database,
  2058. map->table ? map->table : map->name);
  2059. }
  2060. }
  2061. }
  2062. ast_cli(a->fd,"\n\n");
  2063. }
  2064. ast_mutex_unlock(&config_lock);
  2065. return CLI_SUCCESS;
  2066. }
  2067. static struct ast_cli_entry cli_config[] = {
  2068. AST_CLI_DEFINE(handle_cli_core_show_config_mappings, "Display config mappings (file names to config engines)"),
  2069. };
  2070. int register_config_cli()
  2071. {
  2072. ast_cli_register_multiple(cli_config, sizeof(cli_config) / sizeof(struct ast_cli_entry));
  2073. return 0;
  2074. }