config.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2010, 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 http://wiki.asterisk.org
  26. */
  27. /*** MODULEINFO
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include "asterisk/paths.h" /* use ast_config_AST_CONFIG_DIR */
  33. #include "asterisk/network.h" /* we do some sockaddr manipulation here */
  34. #include <time.h>
  35. #include <sys/stat.h>
  36. #include <math.h> /* HUGE_VAL */
  37. #define AST_INCLUDE_GLOB 1
  38. #include "asterisk/config.h"
  39. #include "asterisk/cli.h"
  40. #include "asterisk/lock.h"
  41. #include "asterisk/utils.h"
  42. #include "asterisk/channel.h"
  43. #include "asterisk/app.h"
  44. #include "asterisk/astobj2.h"
  45. #include "asterisk/strings.h" /* for the ast_str_*() API */
  46. #include "asterisk/netsock2.h"
  47. #define MAX_NESTED_COMMENTS 128
  48. #define COMMENT_START ";--"
  49. #define COMMENT_END "--;"
  50. #define COMMENT_META ';'
  51. #define COMMENT_TAG '-'
  52. /*!
  53. * Define the minimum filename space to reserve for each
  54. * ast_variable in case the filename is renamed later by
  55. * ast_include_rename().
  56. */
  57. #define MIN_VARIABLE_FNAME_SPACE 40
  58. static char *extconfig_conf = "extconfig.conf";
  59. /*! \brief Structure to keep comments for rewriting configuration files */
  60. struct ast_comment {
  61. struct ast_comment *next;
  62. /*! Comment body allocated after struct. */
  63. char cmt[0];
  64. };
  65. /*! \brief Hold the mtime for config files, so if we don't need to reread our config, don't. */
  66. struct cache_file_include {
  67. AST_LIST_ENTRY(cache_file_include) list;
  68. /*! Filename or wildcard pattern as specified by the including file. */
  69. char include[0];
  70. };
  71. struct cache_file_mtime {
  72. AST_LIST_ENTRY(cache_file_mtime) list;
  73. AST_LIST_HEAD_NOLOCK(includes, cache_file_include) includes;
  74. unsigned int has_exec:1;
  75. /*! stat() file size */
  76. unsigned long stat_size;
  77. /*! stat() file modtime nanoseconds */
  78. unsigned long stat_mtime_nsec;
  79. /*! stat() file modtime seconds since epoc */
  80. time_t stat_mtime;
  81. /*! String stuffed in filename[] after the filename string. */
  82. const char *who_asked;
  83. /*! Filename and who_asked stuffed after it. */
  84. char filename[0];
  85. };
  86. /*! Cached file mtime list. */
  87. static AST_LIST_HEAD_STATIC(cfmtime_head, cache_file_mtime);
  88. static int init_appendbuf(void *data)
  89. {
  90. struct ast_str **str = data;
  91. *str = ast_str_create(16);
  92. return *str ? 0 : -1;
  93. }
  94. AST_THREADSTORAGE_CUSTOM(appendbuf, init_appendbuf, ast_free_ptr);
  95. /* comment buffers are better implemented using the ast_str_*() API */
  96. #define CB_SIZE 250 /* initial size of comment buffers */
  97. static void CB_ADD(struct ast_str **cb, const char *str)
  98. {
  99. ast_str_append(cb, 0, "%s", str);
  100. }
  101. static void CB_ADD_LEN(struct ast_str **cb, const char *str, int len)
  102. {
  103. char *s = ast_alloca(len + 1);
  104. memcpy(s, str, len);
  105. s[len] = '\0';
  106. ast_str_append(cb, 0, "%s", s);
  107. }
  108. static void CB_RESET(struct ast_str *cb, struct ast_str *llb)
  109. {
  110. if (cb) {
  111. ast_str_reset(cb);
  112. }
  113. if (llb) {
  114. ast_str_reset(llb);
  115. }
  116. }
  117. static struct ast_comment *ALLOC_COMMENT(struct ast_str *buffer)
  118. {
  119. struct ast_comment *x = NULL;
  120. if (!buffer || !ast_str_strlen(buffer)) {
  121. return NULL;
  122. }
  123. if ((x = ast_calloc(1, sizeof(*x) + ast_str_strlen(buffer) + 1))) {
  124. strcpy(x->cmt, ast_str_buffer(buffer)); /* SAFE */
  125. }
  126. return x;
  127. }
  128. /* I need to keep track of each config file, and all its inclusions,
  129. so that we can track blank lines in each */
  130. struct inclfile {
  131. char *fname;
  132. int lineno;
  133. };
  134. static int hash_string(const void *obj, const int flags)
  135. {
  136. char *str = ((struct inclfile *) obj)->fname;
  137. int total;
  138. for (total = 0; *str; str++) {
  139. unsigned int tmp = total;
  140. total <<= 1; /* multiply by 2 */
  141. total += tmp; /* multiply by 3 */
  142. total <<= 2; /* multiply by 12 */
  143. total += tmp; /* multiply by 13 */
  144. total += ((unsigned int) (*str));
  145. }
  146. if (total < 0) {
  147. total = -total;
  148. }
  149. return total;
  150. }
  151. static int hashtab_compare_strings(void *a, void *b, int flags)
  152. {
  153. const struct inclfile *ae = a, *be = b;
  154. return !strcmp(ae->fname, be->fname) ? CMP_MATCH | CMP_STOP : 0;
  155. }
  156. static struct ast_config_map {
  157. struct ast_config_map *next;
  158. int priority;
  159. /*! Stored in stuff[] at struct end. */
  160. const char *name;
  161. /*! Stored in stuff[] at struct end. */
  162. const char *driver;
  163. /*! Stored in stuff[] at struct end. */
  164. const char *database;
  165. /*! Stored in stuff[] at struct end. */
  166. const char *table;
  167. /*! Contents of name, driver, database, and table in that order stuffed here. */
  168. char stuff[0];
  169. } *config_maps = NULL;
  170. AST_MUTEX_DEFINE_STATIC(config_lock);
  171. static struct ast_config_engine *config_engine_list;
  172. #define MAX_INCLUDE_LEVEL 10
  173. struct ast_category_template_instance {
  174. char name[80]; /* redundant? */
  175. const struct ast_category *inst;
  176. AST_LIST_ENTRY(ast_category_template_instance) next;
  177. };
  178. struct ast_category {
  179. char name[80];
  180. int ignored; /*!< do not let user of the config see this category -- set by (!) after the category decl; a template */
  181. int include_level;
  182. /*!
  183. * \brief The file name from whence this declaration was read
  184. * \note Will never be NULL
  185. */
  186. char *file;
  187. int lineno;
  188. AST_LIST_HEAD_NOLOCK(template_instance_list, ast_category_template_instance) template_instances;
  189. struct ast_comment *precomments;
  190. struct ast_comment *sameline;
  191. struct ast_comment *trailing; /*!< the last object in the list will get assigned any trailing comments when EOF is hit */
  192. /*! First category variable in the list. */
  193. struct ast_variable *root;
  194. /*! Last category variable in the list. */
  195. struct ast_variable *last;
  196. /*! Next node in the list. */
  197. struct ast_category *next;
  198. };
  199. struct ast_config {
  200. /*! First config category in the list. */
  201. struct ast_category *root;
  202. /*! Last config category in the list. */
  203. struct ast_category *last;
  204. struct ast_category *current;
  205. struct ast_category *last_browse; /*!< used to cache the last category supplied via category_browse */
  206. int include_level;
  207. int max_include_level;
  208. struct ast_config_include *includes; /*!< a list of inclusions, which should describe the entire tree */
  209. };
  210. struct ast_config_include {
  211. /*!
  212. * \brief file name in which the include occurs
  213. * \note Will never be NULL
  214. */
  215. char *include_location_file;
  216. int include_location_lineno; /*!< lineno where include occurred */
  217. int exec; /*!< set to non-zero if its a #exec statement */
  218. /*!
  219. * \brief if it's an exec, you'll have both the /var/tmp to read, and the original script
  220. * \note Will never be NULL if exec is non-zero
  221. */
  222. char *exec_file;
  223. /*!
  224. * \brief file name included
  225. * \note Will never be NULL
  226. */
  227. char *included_file;
  228. int inclusion_count; /*!< if the file is included more than once, a running count thereof -- but, worry not,
  229. we explode the instances and will include those-- so all entries will be unique */
  230. int output; /*!< a flag to indicate if the inclusion has been output */
  231. struct ast_config_include *next; /*!< ptr to next inclusion in the list */
  232. };
  233. static void ast_variable_destroy(struct ast_variable *doomed);
  234. static void ast_includes_destroy(struct ast_config_include *incls);
  235. #ifdef MALLOC_DEBUG
  236. struct ast_variable *_ast_variable_new(const char *name, const char *value, const char *filename, const char *file, const char *func, int lineno)
  237. #else
  238. struct ast_variable *ast_variable_new(const char *name, const char *value, const char *filename)
  239. #endif
  240. {
  241. struct ast_variable *variable;
  242. int name_len = strlen(name) + 1;
  243. int val_len = strlen(value) + 1;
  244. int fn_len = strlen(filename) + 1;
  245. /* Ensure a minimum length in case the filename is changed later. */
  246. if (fn_len < MIN_VARIABLE_FNAME_SPACE) {
  247. fn_len = MIN_VARIABLE_FNAME_SPACE;
  248. }
  249. if (
  250. #ifdef MALLOC_DEBUG
  251. (variable = __ast_calloc(1, fn_len + name_len + val_len + sizeof(*variable), file, lineno, func))
  252. #else
  253. (variable = ast_calloc(1, fn_len + name_len + val_len + sizeof(*variable)))
  254. #endif
  255. ) {
  256. char *dst = variable->stuff; /* writable space starts here */
  257. /* Put file first so ast_include_rename() can calculate space available. */
  258. variable->file = strcpy(dst, filename);
  259. dst += fn_len;
  260. variable->name = strcpy(dst, name);
  261. dst += name_len;
  262. variable->value = strcpy(dst, value);
  263. }
  264. return variable;
  265. }
  266. /*!
  267. * \internal
  268. * \brief Move the contents from the source to the destination variable.
  269. *
  270. * \param dst_var Destination variable node
  271. * \param src_var Source variable node
  272. *
  273. * \return Nothing
  274. */
  275. static void ast_variable_move(struct ast_variable *dst_var, struct ast_variable *src_var)
  276. {
  277. dst_var->lineno = src_var->lineno;
  278. dst_var->object = src_var->object;
  279. dst_var->blanklines = src_var->blanklines;
  280. dst_var->precomments = src_var->precomments;
  281. src_var->precomments = NULL;
  282. dst_var->sameline = src_var->sameline;
  283. src_var->sameline = NULL;
  284. dst_var->trailing = src_var->trailing;
  285. src_var->trailing = NULL;
  286. }
  287. 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)
  288. {
  289. /* a file should be included ONCE. Otherwise, if one of the instances is changed,
  290. * then all be changed. -- how do we know to include it? -- Handling modified
  291. * instances is possible, I'd have
  292. * to create a new master for each instance. */
  293. struct ast_config_include *inc;
  294. struct stat statbuf;
  295. inc = ast_include_find(conf, included_file);
  296. if (inc) {
  297. do {
  298. inc->inclusion_count++;
  299. snprintf(real_included_file_name, real_included_file_name_size, "%s~~%d", included_file, inc->inclusion_count);
  300. } while (stat(real_included_file_name, &statbuf) == 0);
  301. 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);
  302. } else
  303. *real_included_file_name = 0;
  304. inc = ast_calloc(1,sizeof(struct ast_config_include));
  305. if (!inc) {
  306. return NULL;
  307. }
  308. inc->include_location_file = ast_strdup(from_file);
  309. inc->include_location_lineno = from_lineno;
  310. if (!ast_strlen_zero(real_included_file_name))
  311. inc->included_file = ast_strdup(real_included_file_name);
  312. else
  313. inc->included_file = ast_strdup(included_file);
  314. inc->exec = is_exec;
  315. if (is_exec)
  316. inc->exec_file = ast_strdup(exec_file);
  317. if (!inc->include_location_file
  318. || !inc->included_file
  319. || (is_exec && !inc->exec_file)) {
  320. ast_includes_destroy(inc);
  321. return NULL;
  322. }
  323. /* attach this new struct to the conf struct */
  324. inc->next = conf->includes;
  325. conf->includes = inc;
  326. return inc;
  327. }
  328. void ast_include_rename(struct ast_config *conf, const char *from_file, const char *to_file)
  329. {
  330. struct ast_config_include *incl;
  331. struct ast_category *cat;
  332. char *str;
  333. int from_len = strlen(from_file);
  334. int to_len = strlen(to_file);
  335. if (strcmp(from_file, to_file) == 0) /* no use wasting time if the name is the same */
  336. return;
  337. /* the manager code allows you to read in one config file, then
  338. * write it back out under a different name. But, the new arrangement
  339. * ties output lines to the file name. So, before you try to write
  340. * the config file to disk, better riffle thru the data and make sure
  341. * the file names are changed.
  342. */
  343. /* file names are on categories, includes (of course), and on variables. So,
  344. * traverse all this and swap names */
  345. for (incl = conf->includes; incl; incl=incl->next) {
  346. if (strcmp(incl->include_location_file,from_file) == 0) {
  347. if (from_len >= to_len)
  348. strcpy(incl->include_location_file, to_file);
  349. else {
  350. /* Keep the old filename if the allocation fails. */
  351. str = ast_strdup(to_file);
  352. if (str) {
  353. ast_free(incl->include_location_file);
  354. incl->include_location_file = str;
  355. }
  356. }
  357. }
  358. }
  359. for (cat = conf->root; cat; cat = cat->next) {
  360. struct ast_variable **prev;
  361. struct ast_variable *v;
  362. struct ast_variable *new_var;
  363. if (strcmp(cat->file,from_file) == 0) {
  364. if (from_len >= to_len)
  365. strcpy(cat->file, to_file);
  366. else {
  367. /* Keep the old filename if the allocation fails. */
  368. str = ast_strdup(to_file);
  369. if (str) {
  370. ast_free(cat->file);
  371. cat->file = str;
  372. }
  373. }
  374. }
  375. for (prev = &cat->root, v = cat->root; v; prev = &v->next, v = v->next) {
  376. if (strcmp(v->file, from_file)) {
  377. continue;
  378. }
  379. /*
  380. * Calculate actual space available. The file string is
  381. * intentionally stuffed before the name string just so we can
  382. * do this.
  383. */
  384. if (to_len < v->name - v->file) {
  385. /* The new name will fit in the available space. */
  386. str = (char *) v->file;/* Stupid compiler complains about discarding qualifiers even though I used a cast. */
  387. strcpy(str, to_file);/* SAFE */
  388. continue;
  389. }
  390. /* Keep the old filename if the allocation fails. */
  391. new_var = ast_variable_new(v->name, v->value, to_file);
  392. if (!new_var) {
  393. continue;
  394. }
  395. /* Move items from the old list node to the replacement node. */
  396. ast_variable_move(new_var, v);
  397. /* Replace the old node in the list with the new node. */
  398. new_var->next = v->next;
  399. if (cat->last == v) {
  400. cat->last = new_var;
  401. }
  402. *prev = new_var;
  403. ast_variable_destroy(v);
  404. v = new_var;
  405. }
  406. }
  407. }
  408. struct ast_config_include *ast_include_find(struct ast_config *conf, const char *included_file)
  409. {
  410. struct ast_config_include *x;
  411. for (x=conf->includes;x;x=x->next) {
  412. if (strcmp(x->included_file,included_file) == 0)
  413. return x;
  414. }
  415. return 0;
  416. }
  417. void ast_variable_append(struct ast_category *category, struct ast_variable *variable)
  418. {
  419. if (!variable)
  420. return;
  421. if (category->last)
  422. category->last->next = variable;
  423. else
  424. category->root = variable;
  425. category->last = variable;
  426. while (category->last->next)
  427. category->last = category->last->next;
  428. }
  429. void ast_variable_insert(struct ast_category *category, struct ast_variable *variable, const char *line)
  430. {
  431. struct ast_variable *cur = category->root;
  432. int lineno;
  433. int insertline;
  434. if (!variable || sscanf(line, "%30d", &insertline) != 1) {
  435. return;
  436. }
  437. if (!insertline) {
  438. variable->next = category->root;
  439. category->root = variable;
  440. } else {
  441. for (lineno = 1; lineno < insertline; lineno++) {
  442. cur = cur->next;
  443. if (!cur->next) {
  444. break;
  445. }
  446. }
  447. variable->next = cur->next;
  448. cur->next = variable;
  449. }
  450. }
  451. static void ast_comment_destroy(struct ast_comment **comment)
  452. {
  453. struct ast_comment *n, *p;
  454. for (p = *comment; p; p = n) {
  455. n = p->next;
  456. ast_free(p);
  457. }
  458. *comment = NULL;
  459. }
  460. static void ast_variable_destroy(struct ast_variable *doomed)
  461. {
  462. ast_comment_destroy(&doomed->precomments);
  463. ast_comment_destroy(&doomed->sameline);
  464. ast_comment_destroy(&doomed->trailing);
  465. ast_free(doomed);
  466. }
  467. struct ast_variable *ast_variables_dup(struct ast_variable *var)
  468. {
  469. struct ast_variable *cloned;
  470. struct ast_variable *tmp;
  471. if (!(cloned = ast_variable_new(var->name, var->value, var->file))) {
  472. return NULL;
  473. }
  474. tmp = cloned;
  475. while ((var = var->next)) {
  476. if (!(tmp->next = ast_variable_new(var->name, var->value, var->file))) {
  477. ast_variables_destroy(cloned);
  478. return NULL;
  479. }
  480. tmp = tmp->next;
  481. }
  482. return cloned;
  483. }
  484. struct ast_variable *ast_variables_reverse(struct ast_variable *var)
  485. {
  486. struct ast_variable *var1, *var2;
  487. var1 = var;
  488. if (!var1 || !var1->next) {
  489. return var1;
  490. }
  491. var2 = var1->next;
  492. var1->next = NULL;
  493. while (var2) {
  494. struct ast_variable *next = var2->next;
  495. var2->next = var1;
  496. var1 = var2;
  497. var2 = next;
  498. }
  499. return var1;
  500. }
  501. void ast_variables_destroy(struct ast_variable *v)
  502. {
  503. struct ast_variable *vn;
  504. while (v) {
  505. vn = v;
  506. v = v->next;
  507. ast_variable_destroy(vn);
  508. }
  509. }
  510. struct ast_variable *ast_variable_browse(const struct ast_config *config, const char *category)
  511. {
  512. struct ast_category *cat = NULL;
  513. if (!category) {
  514. return NULL;
  515. }
  516. if (config->last_browse && (config->last_browse->name == category)) {
  517. cat = config->last_browse;
  518. } else {
  519. cat = ast_category_get(config, category);
  520. }
  521. return (cat) ? cat->root : NULL;
  522. }
  523. const char *ast_config_option(struct ast_config *cfg, const char *cat, const char *var)
  524. {
  525. const char *tmp;
  526. tmp = ast_variable_retrieve(cfg, cat, var);
  527. if (!tmp) {
  528. tmp = ast_variable_retrieve(cfg, "general", var);
  529. }
  530. return tmp;
  531. }
  532. const char *ast_variable_retrieve(const struct ast_config *config, const char *category, const char *variable)
  533. {
  534. struct ast_variable *v;
  535. if (category) {
  536. for (v = ast_variable_browse(config, category); v; v = v->next) {
  537. if (!strcasecmp(variable, v->name)) {
  538. return v->value;
  539. }
  540. }
  541. } else {
  542. struct ast_category *cat;
  543. for (cat = config->root; cat; cat = cat->next) {
  544. for (v = cat->root; v; v = v->next) {
  545. if (!strcasecmp(variable, v->name)) {
  546. return v->value;
  547. }
  548. }
  549. }
  550. }
  551. return NULL;
  552. }
  553. static struct ast_variable *variable_clone(const struct ast_variable *old)
  554. {
  555. struct ast_variable *new = ast_variable_new(old->name, old->value, old->file);
  556. if (new) {
  557. new->lineno = old->lineno;
  558. new->object = old->object;
  559. new->blanklines = old->blanklines;
  560. /* TODO: clone comments? */
  561. }
  562. return new;
  563. }
  564. static void move_variables(struct ast_category *old, struct ast_category *new)
  565. {
  566. struct ast_variable *var = old->root;
  567. old->root = NULL;
  568. /* we can just move the entire list in a single op */
  569. ast_variable_append(new, var);
  570. }
  571. struct ast_category *ast_category_new(const char *name, const char *in_file, int lineno)
  572. {
  573. struct ast_category *category;
  574. category = ast_calloc(1, sizeof(*category));
  575. if (!category) {
  576. return NULL;
  577. }
  578. category->file = ast_strdup(in_file);
  579. if (!category->file) {
  580. ast_category_destroy(category);
  581. return NULL;
  582. }
  583. ast_copy_string(category->name, name, sizeof(category->name));
  584. category->lineno = lineno; /* if you don't know the lineno, set it to 999999 or something real big */
  585. return category;
  586. }
  587. static struct ast_category *category_get(const struct ast_config *config, const char *category_name, int ignored)
  588. {
  589. struct ast_category *cat;
  590. /* try exact match first, then case-insensitive match */
  591. for (cat = config->root; cat; cat = cat->next) {
  592. if (cat->name == category_name && (ignored || !cat->ignored))
  593. return cat;
  594. }
  595. for (cat = config->root; cat; cat = cat->next) {
  596. if (!strcasecmp(cat->name, category_name) && (ignored || !cat->ignored))
  597. return cat;
  598. }
  599. return NULL;
  600. }
  601. struct ast_category *ast_category_get(const struct ast_config *config, const char *category_name)
  602. {
  603. return category_get(config, category_name, 0);
  604. }
  605. int ast_category_exist(const struct ast_config *config, const char *category_name)
  606. {
  607. return !!ast_category_get(config, category_name);
  608. }
  609. void ast_category_append(struct ast_config *config, struct ast_category *category)
  610. {
  611. if (config->last)
  612. config->last->next = category;
  613. else
  614. config->root = category;
  615. category->include_level = config->include_level;
  616. config->last = category;
  617. config->current = category;
  618. }
  619. int ast_category_insert(struct ast_config *config, struct ast_category *cat, const char *match)
  620. {
  621. struct ast_category *cur_category;
  622. if (!config || !cat || !match) {
  623. return -1;
  624. }
  625. if (!strcasecmp(config->root->name, match)) {
  626. cat->next = config->root;
  627. config->root = cat;
  628. return 0;
  629. }
  630. for (cur_category = config->root; cur_category && cur_category->next;
  631. cur_category = cur_category->next) {
  632. if (!strcasecmp(cur_category->next->name, match)) {
  633. cat->next = cur_category->next;
  634. cur_category->next = cat;
  635. return 0;
  636. }
  637. }
  638. return -1;
  639. }
  640. static void ast_destroy_template_list(struct ast_category *cat)
  641. {
  642. struct ast_category_template_instance *x;
  643. while ((x = AST_LIST_REMOVE_HEAD(&cat->template_instances, next)))
  644. ast_free(x);
  645. }
  646. void ast_category_destroy(struct ast_category *cat)
  647. {
  648. ast_variables_destroy(cat->root);
  649. cat->root = NULL;
  650. cat->last = NULL;
  651. ast_comment_destroy(&cat->precomments);
  652. ast_comment_destroy(&cat->sameline);
  653. ast_comment_destroy(&cat->trailing);
  654. ast_destroy_template_list(cat);
  655. ast_free(cat->file);
  656. ast_free(cat);
  657. }
  658. static void ast_includes_destroy(struct ast_config_include *incls)
  659. {
  660. struct ast_config_include *incl,*inclnext;
  661. for (incl=incls; incl; incl = inclnext) {
  662. inclnext = incl->next;
  663. ast_free(incl->include_location_file);
  664. ast_free(incl->exec_file);
  665. ast_free(incl->included_file);
  666. ast_free(incl);
  667. }
  668. }
  669. static struct ast_category *next_available_category(struct ast_category *cat)
  670. {
  671. for (; cat && cat->ignored; cat = cat->next);
  672. return cat;
  673. }
  674. /*! return the first var of a category */
  675. struct ast_variable *ast_category_first(struct ast_category *cat)
  676. {
  677. return (cat) ? cat->root : NULL;
  678. }
  679. struct ast_variable *ast_category_root(struct ast_config *config, char *cat)
  680. {
  681. struct ast_category *category = ast_category_get(config, cat);
  682. if (category)
  683. return category->root;
  684. return NULL;
  685. }
  686. char *ast_category_browse(struct ast_config *config, const char *prev)
  687. {
  688. struct ast_category *cat;
  689. if (!prev) {
  690. /* First time browse. */
  691. cat = config->root;
  692. } else if (config->last_browse && (config->last_browse->name == prev)) {
  693. /* Simple last browse found. */
  694. cat = config->last_browse->next;
  695. } else {
  696. /*
  697. * Config changed since last browse.
  698. *
  699. * First try cheap last browse search. (Rebrowsing a different
  700. * previous category?)
  701. */
  702. for (cat = config->root; cat; cat = cat->next) {
  703. if (cat->name == prev) {
  704. /* Found it. */
  705. cat = cat->next;
  706. break;
  707. }
  708. }
  709. if (!cat) {
  710. /*
  711. * Have to do it the hard way. (Last category was deleted and
  712. * re-added?)
  713. */
  714. for (cat = config->root; cat; cat = cat->next) {
  715. if (!strcasecmp(cat->name, prev)) {
  716. /* Found it. */
  717. cat = cat->next;
  718. break;
  719. }
  720. }
  721. }
  722. }
  723. if (cat)
  724. cat = next_available_category(cat);
  725. config->last_browse = cat;
  726. return (cat) ? cat->name : NULL;
  727. }
  728. struct ast_variable *ast_category_detach_variables(struct ast_category *cat)
  729. {
  730. struct ast_variable *v;
  731. v = cat->root;
  732. cat->root = NULL;
  733. cat->last = NULL;
  734. return v;
  735. }
  736. void ast_category_rename(struct ast_category *cat, const char *name)
  737. {
  738. ast_copy_string(cat->name, name, sizeof(cat->name));
  739. }
  740. static void inherit_category(struct ast_category *new, const struct ast_category *base)
  741. {
  742. struct ast_variable *var;
  743. struct ast_category_template_instance *x;
  744. x = ast_calloc(1, sizeof(*x));
  745. if (!x) {
  746. return;
  747. }
  748. strcpy(x->name, base->name);
  749. x->inst = base;
  750. AST_LIST_INSERT_TAIL(&new->template_instances, x, next);
  751. for (var = base->root; var; var = var->next)
  752. ast_variable_append(new, variable_clone(var));
  753. }
  754. struct ast_config *ast_config_new(void)
  755. {
  756. struct ast_config *config;
  757. if ((config = ast_calloc(1, sizeof(*config))))
  758. config->max_include_level = MAX_INCLUDE_LEVEL;
  759. return config;
  760. }
  761. int ast_variable_delete(struct ast_category *category, const char *variable, const char *match, const char *line)
  762. {
  763. struct ast_variable *cur, *prev=NULL, *curn;
  764. int res = -1;
  765. int num_item = 0;
  766. int req_item;
  767. req_item = -1;
  768. if (!ast_strlen_zero(line)) {
  769. /* Requesting to delete by item number. */
  770. if (sscanf(line, "%30d", &req_item) != 1
  771. || req_item < 0) {
  772. /* Invalid item number to delete. */
  773. return -1;
  774. }
  775. }
  776. prev = NULL;
  777. cur = category->root;
  778. while (cur) {
  779. curn = cur->next;
  780. /* Delete by item number or by variable name with optional value. */
  781. if ((0 <= req_item && num_item == req_item)
  782. || (req_item < 0 && !strcasecmp(cur->name, variable)
  783. && (ast_strlen_zero(match) || !strcasecmp(cur->value, match)))) {
  784. if (prev) {
  785. prev->next = cur->next;
  786. if (cur == category->last)
  787. category->last = prev;
  788. } else {
  789. category->root = cur->next;
  790. if (cur == category->last)
  791. category->last = NULL;
  792. }
  793. ast_variable_destroy(cur);
  794. res = 0;
  795. } else
  796. prev = cur;
  797. cur = curn;
  798. ++num_item;
  799. }
  800. return res;
  801. }
  802. int ast_variable_update(struct ast_category *category, const char *variable,
  803. const char *value, const char *match, unsigned int object)
  804. {
  805. struct ast_variable *cur, *prev=NULL, *newer=NULL;
  806. for (cur = category->root; cur; prev = cur, cur = cur->next) {
  807. if (strcasecmp(cur->name, variable) ||
  808. (!ast_strlen_zero(match) && strcasecmp(cur->value, match)))
  809. continue;
  810. if (!(newer = ast_variable_new(variable, value, cur->file)))
  811. return -1;
  812. ast_variable_move(newer, cur);
  813. newer->object = newer->object || object;
  814. /* Replace the old node in the list with the new node. */
  815. newer->next = cur->next;
  816. if (prev)
  817. prev->next = newer;
  818. else
  819. category->root = newer;
  820. if (category->last == cur)
  821. category->last = newer;
  822. ast_variable_destroy(cur);
  823. return 0;
  824. }
  825. /* Could not find variable to update */
  826. return -1;
  827. }
  828. int ast_category_delete(struct ast_config *cfg, const char *category)
  829. {
  830. struct ast_category *prev=NULL, *cat;
  831. cat = cfg->root;
  832. while (cat) {
  833. if (cat->name == category) {
  834. if (prev) {
  835. prev->next = cat->next;
  836. if (cat == cfg->last)
  837. cfg->last = prev;
  838. } else {
  839. cfg->root = cat->next;
  840. if (cat == cfg->last)
  841. cfg->last = NULL;
  842. }
  843. ast_category_destroy(cat);
  844. return 0;
  845. }
  846. prev = cat;
  847. cat = cat->next;
  848. }
  849. prev = NULL;
  850. cat = cfg->root;
  851. while (cat) {
  852. if (!strcasecmp(cat->name, category)) {
  853. if (prev) {
  854. prev->next = cat->next;
  855. if (cat == cfg->last)
  856. cfg->last = prev;
  857. } else {
  858. cfg->root = cat->next;
  859. if (cat == cfg->last)
  860. cfg->last = NULL;
  861. }
  862. ast_category_destroy(cat);
  863. return 0;
  864. }
  865. prev = cat;
  866. cat = cat->next;
  867. }
  868. return -1;
  869. }
  870. int ast_category_empty(struct ast_config *cfg, const char *category)
  871. {
  872. struct ast_category *cat;
  873. for (cat = cfg->root; cat; cat = cat->next) {
  874. if (strcasecmp(cat->name, category))
  875. continue;
  876. ast_variables_destroy(cat->root);
  877. cat->root = NULL;
  878. cat->last = NULL;
  879. return 0;
  880. }
  881. return -1;
  882. }
  883. void ast_config_destroy(struct ast_config *cfg)
  884. {
  885. struct ast_category *cat, *catn;
  886. if (!cfg)
  887. return;
  888. ast_includes_destroy(cfg->includes);
  889. cat = cfg->root;
  890. while (cat) {
  891. catn = cat;
  892. cat = cat->next;
  893. ast_category_destroy(catn);
  894. }
  895. ast_free(cfg);
  896. }
  897. struct ast_category *ast_config_get_current_category(const struct ast_config *cfg)
  898. {
  899. return cfg->current;
  900. }
  901. void ast_config_set_current_category(struct ast_config *cfg, const struct ast_category *cat)
  902. {
  903. /* cast below is just to silence compiler warning about dropping "const" */
  904. cfg->current = (struct ast_category *) cat;
  905. }
  906. /*!
  907. * \internal
  908. * \brief Create a new cfmtime list node.
  909. *
  910. * \param filename Config filename caching.
  911. * \param who_asked Who wanted to know.
  912. *
  913. * \retval cfmtime New node on success.
  914. * \retval NULL on error.
  915. */
  916. static struct cache_file_mtime *cfmtime_new(const char *filename, const char *who_asked)
  917. {
  918. struct cache_file_mtime *cfmtime;
  919. char *dst;
  920. cfmtime = ast_calloc(1,
  921. sizeof(*cfmtime) + strlen(filename) + 1 + strlen(who_asked) + 1);
  922. if (!cfmtime) {
  923. return NULL;
  924. }
  925. dst = cfmtime->filename; /* writable space starts here */
  926. strcpy(dst, filename); /* Safe */
  927. dst += strlen(dst) + 1;
  928. cfmtime->who_asked = strcpy(dst, who_asked); /* Safe */
  929. return cfmtime;
  930. }
  931. enum config_cache_attribute_enum {
  932. ATTRIBUTE_INCLUDE = 0,
  933. ATTRIBUTE_EXEC = 1,
  934. };
  935. /*!
  936. * \internal
  937. * \brief Save the stat() data to the cached file modtime struct.
  938. *
  939. * \param cfmtime Cached file modtime.
  940. * \param statbuf Buffer filled in by stat().
  941. *
  942. * \return Nothing
  943. */
  944. static void cfmstat_save(struct cache_file_mtime *cfmtime, struct stat *statbuf)
  945. {
  946. cfmtime->stat_size = statbuf->st_size;
  947. #if defined(HAVE_STRUCT_STAT_ST_MTIM)
  948. cfmtime->stat_mtime_nsec = statbuf->st_mtim.tv_nsec;
  949. #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
  950. cfmtime->stat_mtime_nsec = statbuf->st_mtimensec;
  951. #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
  952. cfmtime->stat_mtime_nsec = statbuf->st_mtimespec.tv_nsec;
  953. #else
  954. cfmtime->stat_mtime_nsec = 0;
  955. #endif
  956. cfmtime->stat_mtime = statbuf->st_mtime;
  957. }
  958. /*!
  959. * \internal
  960. * \brief Compare the stat() data with the cached file modtime struct.
  961. *
  962. * \param cfmtime Cached file modtime.
  963. * \param statbuf Buffer filled in by stat().
  964. *
  965. * \retval non-zero if different.
  966. */
  967. static int cfmstat_cmp(struct cache_file_mtime *cfmtime, struct stat *statbuf)
  968. {
  969. struct cache_file_mtime cfm_buf;
  970. cfmstat_save(&cfm_buf, statbuf);
  971. return cfmtime->stat_size != cfm_buf.stat_size
  972. || cfmtime->stat_mtime != cfm_buf.stat_mtime
  973. || cfmtime->stat_mtime_nsec != cfm_buf.stat_mtime_nsec;
  974. }
  975. /*!
  976. * \internal
  977. * \brief Clear the cached file modtime include list.
  978. *
  979. * \param cfmtime Cached file modtime.
  980. *
  981. * \note cfmtime_head is assumed already locked.
  982. *
  983. * \return Nothing
  984. */
  985. static void config_cache_flush_includes(struct cache_file_mtime *cfmtime)
  986. {
  987. struct cache_file_include *cfinclude;
  988. while ((cfinclude = AST_LIST_REMOVE_HEAD(&cfmtime->includes, list))) {
  989. ast_free(cfinclude);
  990. }
  991. }
  992. /*!
  993. * \internal
  994. * \brief Destroy the given cached file modtime entry.
  995. *
  996. * \param cfmtime Cached file modtime.
  997. *
  998. * \note cfmtime_head is assumed already locked.
  999. *
  1000. * \return Nothing
  1001. */
  1002. static void config_cache_destroy_entry(struct cache_file_mtime *cfmtime)
  1003. {
  1004. config_cache_flush_includes(cfmtime);
  1005. ast_free(cfmtime);
  1006. }
  1007. /*!
  1008. * \internal
  1009. * \brief Remove and destroy the config cache entry for the filename and who_asked.
  1010. *
  1011. * \param filename Config filename.
  1012. * \param who_asked Which module asked.
  1013. *
  1014. * \return Nothing
  1015. */
  1016. static void config_cache_remove(const char *filename, const char *who_asked)
  1017. {
  1018. struct cache_file_mtime *cfmtime;
  1019. AST_LIST_LOCK(&cfmtime_head);
  1020. AST_LIST_TRAVERSE_SAFE_BEGIN(&cfmtime_head, cfmtime, list) {
  1021. if (!strcmp(cfmtime->filename, filename)
  1022. && !strcmp(cfmtime->who_asked, who_asked)) {
  1023. AST_LIST_REMOVE_CURRENT(list);
  1024. config_cache_destroy_entry(cfmtime);
  1025. break;
  1026. }
  1027. }
  1028. AST_LIST_TRAVERSE_SAFE_END;
  1029. AST_LIST_UNLOCK(&cfmtime_head);
  1030. }
  1031. static void config_cache_attribute(const char *configfile, enum config_cache_attribute_enum attrtype, const char *filename, const char *who_asked)
  1032. {
  1033. struct cache_file_mtime *cfmtime;
  1034. struct cache_file_include *cfinclude;
  1035. /* Find our cached entry for this configuration file */
  1036. AST_LIST_LOCK(&cfmtime_head);
  1037. AST_LIST_TRAVERSE(&cfmtime_head, cfmtime, list) {
  1038. if (!strcmp(cfmtime->filename, configfile) && !strcmp(cfmtime->who_asked, who_asked))
  1039. break;
  1040. }
  1041. if (!cfmtime) {
  1042. cfmtime = cfmtime_new(configfile, who_asked);
  1043. if (!cfmtime) {
  1044. AST_LIST_UNLOCK(&cfmtime_head);
  1045. return;
  1046. }
  1047. /* Note that the file mtime is initialized to 0, i.e. 1970 */
  1048. AST_LIST_INSERT_SORTALPHA(&cfmtime_head, cfmtime, list, filename);
  1049. }
  1050. switch (attrtype) {
  1051. case ATTRIBUTE_INCLUDE:
  1052. AST_LIST_TRAVERSE(&cfmtime->includes, cfinclude, list) {
  1053. if (!strcmp(cfinclude->include, filename)) {
  1054. AST_LIST_UNLOCK(&cfmtime_head);
  1055. return;
  1056. }
  1057. }
  1058. cfinclude = ast_calloc(1, sizeof(*cfinclude) + strlen(filename) + 1);
  1059. if (!cfinclude) {
  1060. AST_LIST_UNLOCK(&cfmtime_head);
  1061. return;
  1062. }
  1063. strcpy(cfinclude->include, filename); /* Safe */
  1064. AST_LIST_INSERT_TAIL(&cfmtime->includes, cfinclude, list);
  1065. break;
  1066. case ATTRIBUTE_EXEC:
  1067. cfmtime->has_exec = 1;
  1068. break;
  1069. }
  1070. AST_LIST_UNLOCK(&cfmtime_head);
  1071. }
  1072. /*! \brief parse one line in the configuration.
  1073. * \verbatim
  1074. * We can have a category header [foo](...)
  1075. * a directive #include / #exec
  1076. * or a regular line name = value
  1077. * \endverbatim
  1078. */
  1079. static int process_text_line(struct ast_config *cfg, struct ast_category **cat,
  1080. char *buf, int lineno, const char *configfile, struct ast_flags flags,
  1081. struct ast_str *comment_buffer,
  1082. struct ast_str *lline_buffer,
  1083. const char *suggested_include_file,
  1084. struct ast_category **last_cat, struct ast_variable **last_var, const char *who_asked)
  1085. {
  1086. char *c;
  1087. char *cur = buf;
  1088. struct ast_variable *v;
  1089. char cmd[512], exec_file[512];
  1090. /* Actually parse the entry */
  1091. if (cur[0] == '[') { /* A category header */
  1092. /* format is one of the following:
  1093. * [foo] define a new category named 'foo'
  1094. * [foo](!) define a new template category named 'foo'
  1095. * [foo](+) append to category 'foo', error if foo does not exist.
  1096. * [foo](a) define a new category and inherit from template a.
  1097. * You can put a comma-separated list of templates and '!' and '+'
  1098. * between parentheses, with obvious meaning.
  1099. */
  1100. struct ast_category *newcat = NULL;
  1101. char *catname;
  1102. c = strchr(cur, ']');
  1103. if (!c) {
  1104. ast_log(LOG_WARNING, "parse error: no closing ']', line %d of %s\n", lineno, configfile);
  1105. return -1;
  1106. }
  1107. *c++ = '\0';
  1108. cur++;
  1109. if (*c++ != '(')
  1110. c = NULL;
  1111. catname = cur;
  1112. if (!(*cat = newcat = ast_category_new(catname,
  1113. S_OR(suggested_include_file, cfg->include_level == 1 ? "" : configfile),
  1114. lineno))) {
  1115. return -1;
  1116. }
  1117. (*cat)->lineno = lineno;
  1118. *last_var = 0;
  1119. *last_cat = newcat;
  1120. /* add comments */
  1121. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS))
  1122. newcat->precomments = ALLOC_COMMENT(comment_buffer);
  1123. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS))
  1124. newcat->sameline = ALLOC_COMMENT(lline_buffer);
  1125. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS))
  1126. CB_RESET(comment_buffer, lline_buffer);
  1127. /* If there are options or categories to inherit from, process them now */
  1128. if (c) {
  1129. if (!(cur = strchr(c, ')'))) {
  1130. ast_log(LOG_WARNING, "parse error: no closing ')', line %d of %s\n", lineno, configfile);
  1131. return -1;
  1132. }
  1133. *cur = '\0';
  1134. while ((cur = strsep(&c, ","))) {
  1135. if (!strcasecmp(cur, "!")) {
  1136. (*cat)->ignored = 1;
  1137. } else if (!strcasecmp(cur, "+")) {
  1138. *cat = category_get(cfg, catname, 1);
  1139. if (!(*cat)) {
  1140. if (newcat)
  1141. ast_category_destroy(newcat);
  1142. ast_log(LOG_WARNING, "Category addition requested, but category '%s' does not exist, line %d of %s\n", catname, lineno, configfile);
  1143. return -1;
  1144. }
  1145. if (newcat) {
  1146. move_variables(newcat, *cat);
  1147. ast_category_destroy(newcat);
  1148. newcat = NULL;
  1149. }
  1150. } else {
  1151. struct ast_category *base;
  1152. base = category_get(cfg, cur, 1);
  1153. if (!base) {
  1154. ast_log(LOG_WARNING, "Inheritance requested, but category '%s' does not exist, line %d of %s\n", cur, lineno, configfile);
  1155. return -1;
  1156. }
  1157. inherit_category(*cat, base);
  1158. }
  1159. }
  1160. }
  1161. if (newcat)
  1162. ast_category_append(cfg, *cat);
  1163. } else if (cur[0] == '#') { /* A directive - #include or #exec */
  1164. char *cur2;
  1165. char real_inclusion_name[256];
  1166. int do_include = 0; /* otherwise, it is exec */
  1167. cur++;
  1168. c = cur;
  1169. while (*c && (*c > 32)) {
  1170. c++;
  1171. }
  1172. if (*c) {
  1173. *c = '\0';
  1174. /* Find real argument */
  1175. c = ast_strip(c + 1);
  1176. if (!(*c)) {
  1177. c = NULL;
  1178. }
  1179. } else {
  1180. c = NULL;
  1181. }
  1182. if (!strcasecmp(cur, "include")) {
  1183. do_include = 1;
  1184. } else if (!strcasecmp(cur, "exec")) {
  1185. if (!ast_opt_exec_includes) {
  1186. ast_log(LOG_WARNING, "Cannot perform #exec unless execincludes option is enabled in asterisk.conf (options section)!\n");
  1187. return 0; /* XXX is this correct ? or we should return -1 ? */
  1188. }
  1189. } else {
  1190. ast_log(LOG_WARNING, "Unknown directive '#%s' at line %d of %s\n", cur, lineno, configfile);
  1191. return 0; /* XXX is this correct ? or we should return -1 ? */
  1192. }
  1193. if (c == NULL) {
  1194. ast_log(LOG_WARNING, "Directive '#%s' needs an argument (%s) at line %d of %s\n",
  1195. do_include ? "include" : "exec",
  1196. do_include ? "filename" : "/path/to/executable",
  1197. lineno,
  1198. configfile);
  1199. return 0; /* XXX is this correct ? or we should return -1 ? */
  1200. }
  1201. cur = c;
  1202. /* Strip off leading and trailing "'s and <>'s */
  1203. /* Dequote */
  1204. if ((*c == '"') || (*c == '<')) {
  1205. char quote_char = *c;
  1206. if (quote_char == '<') {
  1207. quote_char = '>';
  1208. }
  1209. if (*(c + strlen(c) - 1) == quote_char) {
  1210. cur++;
  1211. *(c + strlen(c) - 1) = '\0';
  1212. }
  1213. }
  1214. cur2 = cur;
  1215. /* #exec </path/to/executable>
  1216. We create a tmp file, then we #include it, then we delete it. */
  1217. if (!do_include) {
  1218. struct timeval now = ast_tvnow();
  1219. if (!ast_test_flag(&flags, CONFIG_FLAG_NOCACHE))
  1220. config_cache_attribute(configfile, ATTRIBUTE_EXEC, NULL, who_asked);
  1221. snprintf(exec_file, sizeof(exec_file), "/var/tmp/exec.%d%d.%ld", (int)now.tv_sec, (int)now.tv_usec, (long)pthread_self());
  1222. snprintf(cmd, sizeof(cmd), "%s > %s 2>&1", cur, exec_file);
  1223. ast_safe_system(cmd);
  1224. cur = exec_file;
  1225. } else {
  1226. if (!ast_test_flag(&flags, CONFIG_FLAG_NOCACHE))
  1227. config_cache_attribute(configfile, ATTRIBUTE_INCLUDE, cur, who_asked);
  1228. exec_file[0] = '\0';
  1229. }
  1230. /* A #include */
  1231. /* record this inclusion */
  1232. ast_include_new(cfg, cfg->include_level == 1 ? "" : configfile, cur, !do_include, cur2, lineno, real_inclusion_name, sizeof(real_inclusion_name));
  1233. do_include = ast_config_internal_load(cur, cfg, flags, real_inclusion_name, who_asked) ? 1 : 0;
  1234. if (!ast_strlen_zero(exec_file))
  1235. unlink(exec_file);
  1236. if (!do_include) {
  1237. ast_log(LOG_ERROR, "The file '%s' was listed as a #include but it does not exist.\n", cur);
  1238. return -1;
  1239. }
  1240. /* XXX otherwise what ? the default return is 0 anyways */
  1241. } else {
  1242. /* Just a line (variable = value) */
  1243. int object = 0;
  1244. if (!(*cat)) {
  1245. ast_log(LOG_WARNING,
  1246. "parse error: No category context for line %d of %s\n", lineno, configfile);
  1247. return -1;
  1248. }
  1249. c = strchr(cur, '=');
  1250. if (c && c > cur && (*(c - 1) == '+')) {
  1251. struct ast_variable *var, *replace = NULL;
  1252. struct ast_str **str = ast_threadstorage_get(&appendbuf, sizeof(*str));
  1253. if (!str || !*str) {
  1254. return -1;
  1255. }
  1256. *(c - 1) = '\0';
  1257. c++;
  1258. cur = ast_strip(cur);
  1259. /* Must iterate through category until we find last variable of same name (since there could be multiple) */
  1260. for (var = ast_category_first(*cat); var; var = var->next) {
  1261. if (!strcmp(var->name, cur)) {
  1262. replace = var;
  1263. }
  1264. }
  1265. if (!replace) {
  1266. /* Nothing to replace; just set a variable normally. */
  1267. goto set_new_variable;
  1268. }
  1269. ast_str_set(str, 0, "%s", replace->value);
  1270. ast_str_append(str, 0, "%s", c);
  1271. ast_str_trim_blanks(*str);
  1272. ast_variable_update(*cat, replace->name, ast_skip_blanks(ast_str_buffer(*str)), replace->value, object);
  1273. } else if (c) {
  1274. *c = 0;
  1275. c++;
  1276. /* Ignore > in => */
  1277. if (*c== '>') {
  1278. object = 1;
  1279. c++;
  1280. }
  1281. set_new_variable:
  1282. if ((v = ast_variable_new(ast_strip(cur), ast_strip(c), S_OR(suggested_include_file, cfg->include_level == 1 ? "" : configfile)))) {
  1283. v->lineno = lineno;
  1284. v->object = object;
  1285. *last_cat = 0;
  1286. *last_var = v;
  1287. /* Put and reset comments */
  1288. v->blanklines = 0;
  1289. ast_variable_append(*cat, v);
  1290. /* add comments */
  1291. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS))
  1292. v->precomments = ALLOC_COMMENT(comment_buffer);
  1293. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS))
  1294. v->sameline = ALLOC_COMMENT(lline_buffer);
  1295. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS))
  1296. CB_RESET(comment_buffer, lline_buffer);
  1297. } else {
  1298. return -1;
  1299. }
  1300. } else {
  1301. ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
  1302. }
  1303. }
  1304. return 0;
  1305. }
  1306. 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)
  1307. {
  1308. char fn[256];
  1309. #if defined(LOW_MEMORY)
  1310. char buf[512];
  1311. #else
  1312. char buf[8192];
  1313. #endif
  1314. char *new_buf, *comment_p, *process_buf;
  1315. FILE *f;
  1316. int lineno=0;
  1317. int comment = 0, nest[MAX_NESTED_COMMENTS];
  1318. struct ast_category *cat = NULL;
  1319. int count = 0;
  1320. struct stat statbuf;
  1321. struct cache_file_mtime *cfmtime = NULL;
  1322. struct cache_file_include *cfinclude;
  1323. struct ast_variable *last_var = 0;
  1324. struct ast_category *last_cat = 0;
  1325. /*! Growable string buffer */
  1326. struct ast_str *comment_buffer = NULL; /*!< this will be a comment collector.*/
  1327. struct ast_str *lline_buffer = NULL; /*!< A buffer for stuff behind the ; */
  1328. if (cfg)
  1329. cat = ast_config_get_current_category(cfg);
  1330. if (filename[0] == '/') {
  1331. ast_copy_string(fn, filename, sizeof(fn));
  1332. } else {
  1333. snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, filename);
  1334. }
  1335. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS)) {
  1336. comment_buffer = ast_str_create(CB_SIZE);
  1337. if (comment_buffer)
  1338. lline_buffer = ast_str_create(CB_SIZE);
  1339. if (!lline_buffer) {
  1340. ast_free(comment_buffer);
  1341. ast_log(LOG_ERROR, "Failed to initialize the comment buffer!\n");
  1342. return NULL;
  1343. }
  1344. }
  1345. #ifdef AST_INCLUDE_GLOB
  1346. {
  1347. int glob_ret;
  1348. glob_t globbuf;
  1349. globbuf.gl_offs = 0; /* initialize it to silence gcc */
  1350. glob_ret = glob(fn, MY_GLOB_FLAGS, NULL, &globbuf);
  1351. if (glob_ret == GLOB_NOSPACE) {
  1352. ast_log(LOG_WARNING,
  1353. "Glob Expansion of pattern '%s' failed: Not enough memory\n", fn);
  1354. } else if (glob_ret == GLOB_ABORTED) {
  1355. ast_log(LOG_WARNING,
  1356. "Glob Expansion of pattern '%s' failed: Read error\n", fn);
  1357. } else {
  1358. /* loop over expanded files */
  1359. int i;
  1360. if (!cfg && (globbuf.gl_pathc != 1 || strcmp(fn, globbuf.gl_pathv[0]))) {
  1361. /*
  1362. * We just want a file changed answer and since we cannot
  1363. * tell if a file was deleted with wildcard matching we will
  1364. * assume that something has always changed. Also without
  1365. * a lot of refactoring we couldn't check more than one file
  1366. * for changes in the glob loop anyway.
  1367. */
  1368. globfree(&globbuf);
  1369. ast_free(comment_buffer);
  1370. ast_free(lline_buffer);
  1371. return NULL;
  1372. }
  1373. for (i=0; i<globbuf.gl_pathc; i++) {
  1374. ast_copy_string(fn, globbuf.gl_pathv[i], sizeof(fn));
  1375. #endif
  1376. /*
  1377. * The following is not a loop, but just a convenient way to define a block
  1378. * (using do { } while(0) ), and be able to exit from it with 'continue'
  1379. * or 'break' in case of errors. Nice trick.
  1380. */
  1381. do {
  1382. if (stat(fn, &statbuf)) {
  1383. if (!ast_test_flag(&flags, CONFIG_FLAG_NOCACHE)) {
  1384. config_cache_remove(fn, who_asked);
  1385. }
  1386. continue;
  1387. }
  1388. if (!S_ISREG(statbuf.st_mode)) {
  1389. ast_log(LOG_WARNING, "'%s' is not a regular file, ignoring\n", fn);
  1390. if (!ast_test_flag(&flags, CONFIG_FLAG_NOCACHE)) {
  1391. config_cache_remove(fn, who_asked);
  1392. }
  1393. continue;
  1394. }
  1395. if (!ast_test_flag(&flags, CONFIG_FLAG_NOCACHE)) {
  1396. /* Find our cached entry for this configuration file */
  1397. AST_LIST_LOCK(&cfmtime_head);
  1398. AST_LIST_TRAVERSE(&cfmtime_head, cfmtime, list) {
  1399. if (!strcmp(cfmtime->filename, fn) && !strcmp(cfmtime->who_asked, who_asked))
  1400. break;
  1401. }
  1402. if (!cfmtime) {
  1403. cfmtime = cfmtime_new(fn, who_asked);
  1404. if (!cfmtime) {
  1405. AST_LIST_UNLOCK(&cfmtime_head);
  1406. continue;
  1407. }
  1408. /* Note that the file mtime is initialized to 0, i.e. 1970 */
  1409. AST_LIST_INSERT_SORTALPHA(&cfmtime_head, cfmtime, list, filename);
  1410. }
  1411. }
  1412. if (cfmtime
  1413. && !cfmtime->has_exec
  1414. && !cfmstat_cmp(cfmtime, &statbuf)
  1415. && ast_test_flag(&flags, CONFIG_FLAG_FILEUNCHANGED)) {
  1416. int unchanged = 1;
  1417. /* File is unchanged, what about the (cached) includes (if any)? */
  1418. AST_LIST_TRAVERSE(&cfmtime->includes, cfinclude, list) {
  1419. if (!config_text_file_load(NULL, NULL, cfinclude->include,
  1420. NULL, flags, "", who_asked)) {
  1421. /* One change is enough to short-circuit and reload the whole shebang */
  1422. unchanged = 0;
  1423. break;
  1424. }
  1425. }
  1426. if (unchanged) {
  1427. AST_LIST_UNLOCK(&cfmtime_head);
  1428. #ifdef AST_INCLUDE_GLOB
  1429. globfree(&globbuf);
  1430. #endif
  1431. ast_free(comment_buffer);
  1432. ast_free(lline_buffer);
  1433. return CONFIG_STATUS_FILEUNCHANGED;
  1434. }
  1435. }
  1436. /* If cfg is NULL, then we just want a file changed answer. */
  1437. if (cfg == NULL) {
  1438. if (cfmtime) {
  1439. AST_LIST_UNLOCK(&cfmtime_head);
  1440. }
  1441. continue;
  1442. }
  1443. if (cfmtime) {
  1444. /* Forget about what we thought we knew about this file's includes. */
  1445. cfmtime->has_exec = 0;
  1446. config_cache_flush_includes(cfmtime);
  1447. cfmstat_save(cfmtime, &statbuf);
  1448. AST_LIST_UNLOCK(&cfmtime_head);
  1449. }
  1450. ast_verb(2, "Parsing '%s': ", fn);
  1451. fflush(stdout);
  1452. if (!(f = fopen(fn, "r"))) {
  1453. ast_debug(1, "No file to parse: %s\n", fn);
  1454. ast_verb(2, "Not found (%s)\n", strerror(errno));
  1455. continue;
  1456. }
  1457. count++;
  1458. /* If we get to this point, then we're loading regardless */
  1459. ast_clear_flag(&flags, CONFIG_FLAG_FILEUNCHANGED);
  1460. ast_debug(1, "Parsing %s\n", fn);
  1461. ast_verb(2, "Found\n");
  1462. while (!feof(f)) {
  1463. lineno++;
  1464. if (fgets(buf, sizeof(buf), f)) {
  1465. /* Skip lines that are too long */
  1466. if (strlen(buf) == sizeof(buf) - 1 && buf[sizeof(buf) - 1] != '\n') {
  1467. ast_log(LOG_WARNING, "Line %d too long, skipping. It begins with: %.32s...\n", lineno, buf);
  1468. while (fgets(buf, sizeof(buf), f)) {
  1469. if (strlen(buf) != sizeof(buf) - 1 || buf[sizeof(buf) - 1] == '\n') {
  1470. break;
  1471. }
  1472. }
  1473. continue;
  1474. }
  1475. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS) && lline_buffer && ast_str_strlen(lline_buffer)) {
  1476. CB_ADD(&comment_buffer, ast_str_buffer(lline_buffer)); /* add the current lline buffer to the comment buffer */
  1477. ast_str_reset(lline_buffer); /* erase the lline buffer */
  1478. }
  1479. new_buf = buf;
  1480. if (comment)
  1481. process_buf = NULL;
  1482. else
  1483. process_buf = buf;
  1484. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS) && comment_buffer && ast_str_strlen(comment_buffer) && (ast_strlen_zero(buf) || strlen(buf) == strspn(buf," \t\n\r"))) {
  1485. /* blank line? really? Can we add it to an existing comment and maybe preserve inter- and post- comment spacing? */
  1486. CB_ADD(&comment_buffer, "\n"); /* add a newline to the comment buffer */
  1487. continue; /* go get a new line, then */
  1488. }
  1489. while ((comment_p = strchr(new_buf, COMMENT_META))) {
  1490. if ((comment_p > new_buf) && (*(comment_p - 1) == '\\')) {
  1491. /* Escaped semicolons aren't comments. */
  1492. new_buf = comment_p;
  1493. /* write over the \ and bring the null terminator with us */
  1494. memmove(comment_p - 1, comment_p, strlen(comment_p) + 1);
  1495. } else if (comment_p[1] == COMMENT_TAG && comment_p[2] == COMMENT_TAG && (comment_p[3] != '-')) {
  1496. /* Meta-Comment start detected ";--" */
  1497. if (comment < MAX_NESTED_COMMENTS) {
  1498. *comment_p = '\0';
  1499. new_buf = comment_p + 3;
  1500. comment++;
  1501. nest[comment-1] = lineno;
  1502. } else {
  1503. ast_log(LOG_ERROR, "Maximum nest limit of %d reached.\n", MAX_NESTED_COMMENTS);
  1504. }
  1505. } else if ((comment_p >= new_buf + 2) &&
  1506. (*(comment_p - 1) == COMMENT_TAG) &&
  1507. (*(comment_p - 2) == COMMENT_TAG)) {
  1508. /* Meta-Comment end detected "--;" */
  1509. comment--;
  1510. new_buf = comment_p + 1;
  1511. if (!comment) {
  1512. /* Back to non-comment now */
  1513. if (process_buf) {
  1514. /* Actually have to move what's left over the top, then continue */
  1515. char *oldptr;
  1516. oldptr = process_buf + strlen(process_buf);
  1517. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS)) {
  1518. CB_ADD(&comment_buffer, ";");
  1519. CB_ADD_LEN(&comment_buffer, oldptr+1, new_buf-oldptr-1);
  1520. }
  1521. memmove(oldptr, new_buf, strlen(new_buf) + 1);
  1522. new_buf = oldptr;
  1523. } else
  1524. process_buf = new_buf;
  1525. }
  1526. } else {
  1527. if (!comment) {
  1528. /* If ; is found, and we are not nested in a comment,
  1529. we immediately stop all comment processing */
  1530. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS)) {
  1531. CB_ADD(&lline_buffer, comment_p);
  1532. }
  1533. *comment_p = '\0';
  1534. new_buf = comment_p;
  1535. } else
  1536. new_buf = comment_p + 1;
  1537. }
  1538. }
  1539. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS) && comment && !process_buf ) {
  1540. CB_ADD(&comment_buffer, buf); /* the whole line is a comment, store it */
  1541. }
  1542. if (process_buf) {
  1543. char *buffer = ast_strip(process_buf);
  1544. if (!ast_strlen_zero(buffer)) {
  1545. if (process_text_line(cfg, &cat, buffer, lineno, fn, flags, comment_buffer, lline_buffer, suggested_include_file, &last_cat, &last_var, who_asked)) {
  1546. cfg = CONFIG_STATUS_FILEINVALID;
  1547. break;
  1548. }
  1549. }
  1550. }
  1551. }
  1552. }
  1553. /* end of file-- anything in a comment buffer? */
  1554. if (last_cat) {
  1555. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS) && comment_buffer && ast_str_strlen(comment_buffer)) {
  1556. if (lline_buffer && ast_str_strlen(lline_buffer)) {
  1557. CB_ADD(&comment_buffer, ast_str_buffer(lline_buffer)); /* add the current lline buffer to the comment buffer */
  1558. ast_str_reset(lline_buffer); /* erase the lline buffer */
  1559. }
  1560. last_cat->trailing = ALLOC_COMMENT(comment_buffer);
  1561. }
  1562. } else if (last_var) {
  1563. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS) && comment_buffer && ast_str_strlen(comment_buffer)) {
  1564. if (lline_buffer && ast_str_strlen(lline_buffer)) {
  1565. CB_ADD(&comment_buffer, ast_str_buffer(lline_buffer)); /* add the current lline buffer to the comment buffer */
  1566. ast_str_reset(lline_buffer); /* erase the lline buffer */
  1567. }
  1568. last_var->trailing = ALLOC_COMMENT(comment_buffer);
  1569. }
  1570. } else {
  1571. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS) && comment_buffer && ast_str_strlen(comment_buffer)) {
  1572. ast_debug(1, "Nothing to attach comments to, discarded: %s\n", ast_str_buffer(comment_buffer));
  1573. }
  1574. }
  1575. if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS))
  1576. CB_RESET(comment_buffer, lline_buffer);
  1577. fclose(f);
  1578. } while (0);
  1579. if (comment) {
  1580. ast_log(LOG_WARNING,"Unterminated comment detected beginning on line %d\n", nest[comment - 1]);
  1581. }
  1582. #ifdef AST_INCLUDE_GLOB
  1583. if (cfg == NULL || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID) {
  1584. break;
  1585. }
  1586. }
  1587. globfree(&globbuf);
  1588. }
  1589. }
  1590. #endif
  1591. ast_free(comment_buffer);
  1592. ast_free(lline_buffer);
  1593. if (count == 0)
  1594. return NULL;
  1595. return cfg;
  1596. }
  1597. /* NOTE: categories and variables each have a file and lineno attribute. On a save operation, these are used to determine
  1598. which file and line number to write out to. Thus, an entire hierarchy of config files (via #include statements) can be
  1599. recreated. BUT, care must be taken to make sure that every cat and var has the proper file name stored, or you may
  1600. be shocked and mystified as to why things are not showing up in the files!
  1601. Also, All #include/#exec statements are recorded in the "includes" LL in the ast_config structure. The file name
  1602. and line number are stored for each include, plus the name of the file included, so that these statements may be
  1603. included in the output files on a file_save operation.
  1604. The lineno's are really just for relative placement in the file. There is no attempt to make sure that blank lines
  1605. are included to keep the lineno's the same between input and output. The lineno fields are used mainly to determine
  1606. the position of the #include and #exec directives. So, blank lines tend to disappear from a read/rewrite operation,
  1607. and a header gets added.
  1608. vars and category heads are output in the order they are stored in the config file. So, if the software
  1609. shuffles these at all, then the placement of #include directives might get a little mixed up, because the
  1610. file/lineno data probably won't get changed.
  1611. */
  1612. static void gen_header(FILE *f1, const char *configfile, const char *fn, const char *generator)
  1613. {
  1614. char date[256]="";
  1615. time_t t;
  1616. time(&t);
  1617. ast_copy_string(date, ctime(&t), sizeof(date));
  1618. fprintf(f1, ";!\n");
  1619. fprintf(f1, ";! Automatically generated configuration file\n");
  1620. if (strcmp(configfile, fn))
  1621. fprintf(f1, ";! Filename: %s (%s)\n", configfile, fn);
  1622. else
  1623. fprintf(f1, ";! Filename: %s\n", configfile);
  1624. fprintf(f1, ";! Generator: %s\n", generator);
  1625. fprintf(f1, ";! Creation Date: %s", date);
  1626. fprintf(f1, ";!\n");
  1627. }
  1628. static void inclfile_destroy(void *obj)
  1629. {
  1630. const struct inclfile *o = obj;
  1631. ast_free(o->fname);
  1632. }
  1633. static void make_fn(char *fn, size_t fn_size, const char *file, const char *configfile)
  1634. {
  1635. if (ast_strlen_zero(file)) {
  1636. if (configfile[0] == '/') {
  1637. ast_copy_string(fn, configfile, fn_size);
  1638. } else {
  1639. snprintf(fn, fn_size, "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
  1640. }
  1641. } else if (file[0] == '/') {
  1642. ast_copy_string(fn, file, fn_size);
  1643. } else {
  1644. snprintf(fn, fn_size, "%s/%s", ast_config_AST_CONFIG_DIR, file);
  1645. }
  1646. }
  1647. static struct inclfile *set_fn(char *fn, size_t fn_size, const char *file, const char *configfile, struct ao2_container *fileset)
  1648. {
  1649. struct inclfile lookup;
  1650. struct inclfile *fi;
  1651. make_fn(fn, fn_size, file, configfile);
  1652. lookup.fname = fn;
  1653. fi = ao2_find(fileset, &lookup, OBJ_POINTER);
  1654. if (fi) {
  1655. /* Found existing include file scratch pad. */
  1656. return fi;
  1657. }
  1658. /* set up a file scratch pad */
  1659. fi = ao2_alloc(sizeof(struct inclfile), inclfile_destroy);
  1660. if (!fi) {
  1661. /* Scratch pad creation failed. */
  1662. return NULL;
  1663. }
  1664. fi->fname = ast_strdup(fn);
  1665. if (!fi->fname) {
  1666. /* Scratch pad creation failed. */
  1667. ao2_ref(fi, -1);
  1668. return NULL;
  1669. }
  1670. fi->lineno = 1;
  1671. ao2_link(fileset, fi);
  1672. return fi;
  1673. }
  1674. static int count_linefeeds(char *str)
  1675. {
  1676. int count = 0;
  1677. while (*str) {
  1678. if (*str =='\n')
  1679. count++;
  1680. str++;
  1681. }
  1682. return count;
  1683. }
  1684. static int count_linefeeds_in_comments(struct ast_comment *x)
  1685. {
  1686. int count = 0;
  1687. while (x) {
  1688. count += count_linefeeds(x->cmt);
  1689. x = x->next;
  1690. }
  1691. return count;
  1692. }
  1693. static void insert_leading_blank_lines(FILE *fp, struct inclfile *fi, struct ast_comment *precomments, int lineno)
  1694. {
  1695. int precomment_lines;
  1696. int i;
  1697. if (!fi) {
  1698. /* No file scratch pad object so insert no blank lines. */
  1699. return;
  1700. }
  1701. precomment_lines = count_linefeeds_in_comments(precomments);
  1702. /* I don't have to worry about those ;! comments, they are
  1703. stored in the precomments, but not printed back out.
  1704. I did have to make sure that comments following
  1705. the ;! header comments were not also deleted in the process */
  1706. if (lineno - precomment_lines - fi->lineno < 0) { /* insertions can mess up the line numbering and produce negative numbers that mess things up */
  1707. return;
  1708. } else if (lineno == 0) {
  1709. /* Line replacements also mess things up */
  1710. return;
  1711. } else if (lineno - precomment_lines - fi->lineno < 5) {
  1712. /* Only insert less than 5 blank lines; if anything more occurs,
  1713. * it's probably due to context deletion. */
  1714. for (i = fi->lineno; i < lineno - precomment_lines; i++) {
  1715. fprintf(fp, "\n");
  1716. }
  1717. } else {
  1718. /* Deletion occurred - insert a single blank line, for separation of
  1719. * contexts. */
  1720. fprintf(fp, "\n");
  1721. }
  1722. fi->lineno = lineno + 1; /* Advance the file lineno */
  1723. }
  1724. int config_text_file_save(const char *configfile, const struct ast_config *cfg, const char *generator)
  1725. {
  1726. return ast_config_text_file_save(configfile, cfg, generator);
  1727. }
  1728. int ast_config_text_file_save(const char *configfile, const struct ast_config *cfg, const char *generator)
  1729. {
  1730. FILE *f;
  1731. char fn[PATH_MAX];
  1732. struct ast_variable *var;
  1733. struct ast_category *cat;
  1734. struct ast_comment *cmt;
  1735. struct ast_config_include *incl;
  1736. int blanklines = 0;
  1737. struct ao2_container *fileset;
  1738. struct inclfile *fi;
  1739. fileset = ao2_container_alloc(1023, hash_string, hashtab_compare_strings);
  1740. if (!fileset) {
  1741. /* Container creation failed. */
  1742. return -1;
  1743. }
  1744. /* Check all the files for write access before attempting to modify any of them */
  1745. for (incl = cfg->includes; incl; incl = incl->next) {
  1746. /* reset all the output flags in case this isn't our first time saving this data */
  1747. incl->output = 0;
  1748. /* now make sure we have write access */
  1749. if (!incl->exec) {
  1750. make_fn(fn, sizeof(fn), incl->included_file, configfile);
  1751. if (access(fn, R_OK | W_OK)) {
  1752. ast_log(LOG_ERROR, "Unable to write %s (%s)\n", fn, strerror(errno));
  1753. return -1;
  1754. }
  1755. }
  1756. }
  1757. /* now make sure we have write access to the main config file */
  1758. make_fn(fn, sizeof(fn), 0, configfile);
  1759. if (access(fn, R_OK | W_OK)) {
  1760. ast_log(LOG_ERROR, "Unable to write %s (%s)\n", fn, strerror(errno));
  1761. return -1;
  1762. }
  1763. /* Now that we know we have write access to all files, it's safe to start truncating them */
  1764. /* go thru all the inclusions and make sure all the files involved (configfile plus all its inclusions)
  1765. are all truncated to zero bytes and have that nice header*/
  1766. for (incl = cfg->includes; incl; incl = incl->next) {
  1767. 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*/
  1768. /* normally, fn is just set to incl->included_file, prepended with config dir if relative */
  1769. fi = set_fn(fn, sizeof(fn), incl->included_file, configfile, fileset);
  1770. f = fopen(fn, "w");
  1771. if (f) {
  1772. gen_header(f, configfile, fn, generator);
  1773. fclose(f); /* this should zero out the file */
  1774. } else {
  1775. ast_log(LOG_ERROR, "Unable to write %s (%s)\n", fn, strerror(errno));
  1776. }
  1777. if (fi) {
  1778. ao2_ref(fi, -1);
  1779. }
  1780. }
  1781. }
  1782. /* just set fn to absolute ver of configfile */
  1783. fi = set_fn(fn, sizeof(fn), 0, configfile, fileset);
  1784. if (
  1785. #ifdef __CYGWIN__
  1786. (f = fopen(fn, "w+"))
  1787. #else
  1788. (f = fopen(fn, "w"))
  1789. #endif
  1790. ) {
  1791. ast_verb(2, "Saving '%s': ", fn);
  1792. gen_header(f, configfile, fn, generator);
  1793. cat = cfg->root;
  1794. fclose(f);
  1795. if (fi) {
  1796. ao2_ref(fi, -1);
  1797. }
  1798. /* from here out, we open each involved file and concat the stuff we need to add to the end and immediately close... */
  1799. /* since each var, cat, and associated comments can come from any file, we have to be
  1800. mobile, and open each file, print, and close it on an entry-by-entry basis */
  1801. while (cat) {
  1802. fi = set_fn(fn, sizeof(fn), cat->file, configfile, fileset);
  1803. f = fopen(fn, "a");
  1804. if (!f) {
  1805. ast_log(LOG_ERROR, "Unable to write %s (%s)\n", fn, strerror(errno));
  1806. if (fi) {
  1807. ao2_ref(fi, -1);
  1808. }
  1809. ao2_ref(fileset, -1);
  1810. return -1;
  1811. }
  1812. /* dump any includes that happen before this category header */
  1813. for (incl=cfg->includes; incl; incl = incl->next) {
  1814. if (strcmp(incl->include_location_file, cat->file) == 0){
  1815. if (cat->lineno > incl->include_location_lineno && !incl->output) {
  1816. if (incl->exec)
  1817. fprintf(f,"#exec \"%s\"\n", incl->exec_file);
  1818. else
  1819. fprintf(f,"#include \"%s\"\n", incl->included_file);
  1820. incl->output = 1;
  1821. }
  1822. }
  1823. }
  1824. insert_leading_blank_lines(f, fi, cat->precomments, cat->lineno);
  1825. /* Dump section with any appropriate comment */
  1826. for (cmt = cat->precomments; cmt; cmt=cmt->next) {
  1827. char *cmtp = cmt->cmt;
  1828. while (cmtp && *cmtp == ';' && *(cmtp+1) == '!') {
  1829. char *cmtp2 = strchr(cmtp+1, '\n');
  1830. if (cmtp2)
  1831. cmtp = cmtp2+1;
  1832. else cmtp = 0;
  1833. }
  1834. if (cmtp)
  1835. fprintf(f,"%s", cmtp);
  1836. }
  1837. fprintf(f, "[%s]", cat->name);
  1838. if (cat->ignored || !AST_LIST_EMPTY(&cat->template_instances)) {
  1839. fprintf(f, "(");
  1840. if (cat->ignored) {
  1841. fprintf(f, "!");
  1842. }
  1843. if (cat->ignored && !AST_LIST_EMPTY(&cat->template_instances)) {
  1844. fprintf(f, ",");
  1845. }
  1846. if (!AST_LIST_EMPTY(&cat->template_instances)) {
  1847. struct ast_category_template_instance *x;
  1848. AST_LIST_TRAVERSE(&cat->template_instances, x, next) {
  1849. fprintf(f,"%s",x->name);
  1850. if (x != AST_LIST_LAST(&cat->template_instances))
  1851. fprintf(f,",");
  1852. }
  1853. }
  1854. fprintf(f, ")");
  1855. }
  1856. for(cmt = cat->sameline; cmt; cmt=cmt->next)
  1857. {
  1858. fprintf(f,"%s", cmt->cmt);
  1859. }
  1860. if (!cat->sameline)
  1861. fprintf(f,"\n");
  1862. for (cmt = cat->trailing; cmt; cmt=cmt->next) {
  1863. if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
  1864. fprintf(f,"%s", cmt->cmt);
  1865. }
  1866. fclose(f);
  1867. if (fi) {
  1868. ao2_ref(fi, -1);
  1869. }
  1870. var = cat->root;
  1871. while (var) {
  1872. struct ast_category_template_instance *x;
  1873. int found = 0;
  1874. AST_LIST_TRAVERSE(&cat->template_instances, x, next) {
  1875. struct ast_variable *v;
  1876. for (v = x->inst->root; v; v = v->next) {
  1877. if (!strcasecmp(var->name, v->name) && !strcmp(var->value, v->value)) {
  1878. found = 1;
  1879. break;
  1880. }
  1881. }
  1882. if (found)
  1883. break;
  1884. }
  1885. if (found) {
  1886. var = var->next;
  1887. continue;
  1888. }
  1889. fi = set_fn(fn, sizeof(fn), var->file, configfile, fileset);
  1890. f = fopen(fn, "a");
  1891. if (!f) {
  1892. ast_debug(1, "Unable to open for writing: %s\n", fn);
  1893. ast_verb(2, "Unable to write %s (%s)", fn, strerror(errno));
  1894. if (fi) {
  1895. ao2_ref(fi, -1);
  1896. }
  1897. ao2_ref(fileset, -1);
  1898. return -1;
  1899. }
  1900. /* dump any includes that happen before this category header */
  1901. for (incl=cfg->includes; incl; incl = incl->next) {
  1902. if (strcmp(incl->include_location_file, var->file) == 0){
  1903. if (var->lineno > incl->include_location_lineno && !incl->output) {
  1904. if (incl->exec)
  1905. fprintf(f,"#exec \"%s\"\n", incl->exec_file);
  1906. else
  1907. fprintf(f,"#include \"%s\"\n", incl->included_file);
  1908. incl->output = 1;
  1909. }
  1910. }
  1911. }
  1912. insert_leading_blank_lines(f, fi, var->precomments, var->lineno);
  1913. for (cmt = var->precomments; cmt; cmt=cmt->next) {
  1914. if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
  1915. fprintf(f,"%s", cmt->cmt);
  1916. }
  1917. if (var->sameline)
  1918. fprintf(f, "%s %s %s %s", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
  1919. else
  1920. fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
  1921. for (cmt = var->trailing; cmt; cmt=cmt->next) {
  1922. if (cmt->cmt[0] != ';' || cmt->cmt[1] != '!')
  1923. fprintf(f,"%s", cmt->cmt);
  1924. }
  1925. if (var->blanklines) {
  1926. blanklines = var->blanklines;
  1927. while (blanklines--)
  1928. fprintf(f, "\n");
  1929. }
  1930. fclose(f);
  1931. if (fi) {
  1932. ao2_ref(fi, -1);
  1933. }
  1934. var = var->next;
  1935. }
  1936. cat = cat->next;
  1937. }
  1938. if (!option_debug)
  1939. ast_verb(2, "Saved\n");
  1940. } else {
  1941. ast_debug(1, "Unable to open for writing: %s\n", fn);
  1942. ast_verb(2, "Unable to write (%s)", strerror(errno));
  1943. if (fi) {
  1944. ao2_ref(fi, -1);
  1945. }
  1946. ao2_ref(fileset, -1);
  1947. return -1;
  1948. }
  1949. /* Now, for files with trailing #include/#exec statements,
  1950. we have to make sure every entry is output */
  1951. for (incl=cfg->includes; incl; incl = incl->next) {
  1952. if (!incl->output) {
  1953. /* open the respective file */
  1954. fi = set_fn(fn, sizeof(fn), incl->include_location_file, configfile, fileset);
  1955. f = fopen(fn, "a");
  1956. if (!f) {
  1957. ast_debug(1, "Unable to open for writing: %s\n", fn);
  1958. ast_verb(2, "Unable to write %s (%s)", fn, strerror(errno));
  1959. if (fi) {
  1960. ao2_ref(fi, -1);
  1961. }
  1962. ao2_ref(fileset, -1);
  1963. return -1;
  1964. }
  1965. /* output the respective include */
  1966. if (incl->exec)
  1967. fprintf(f,"#exec \"%s\"\n", incl->exec_file);
  1968. else
  1969. fprintf(f,"#include \"%s\"\n", incl->included_file);
  1970. fclose(f);
  1971. incl->output = 1;
  1972. if (fi) {
  1973. ao2_ref(fi, -1);
  1974. }
  1975. }
  1976. }
  1977. ao2_ref(fileset, -1); /* this should destroy the hash container */
  1978. return 0;
  1979. }
  1980. static void clear_config_maps(void)
  1981. {
  1982. struct ast_config_map *map;
  1983. ast_mutex_lock(&config_lock);
  1984. while (config_maps) {
  1985. map = config_maps;
  1986. config_maps = config_maps->next;
  1987. ast_free(map);
  1988. }
  1989. ast_mutex_unlock(&config_lock);
  1990. }
  1991. static int append_mapping(const char *name, const char *driver, const char *database, const char *table, int priority)
  1992. {
  1993. struct ast_config_map *map;
  1994. char *dst;
  1995. int length;
  1996. length = sizeof(*map);
  1997. length += strlen(name) + 1;
  1998. length += strlen(driver) + 1;
  1999. length += strlen(database) + 1;
  2000. if (table)
  2001. length += strlen(table) + 1;
  2002. if (!(map = ast_calloc(1, length)))
  2003. return -1;
  2004. dst = map->stuff; /* writable space starts here */
  2005. map->name = strcpy(dst, name);
  2006. dst += strlen(dst) + 1;
  2007. map->driver = strcpy(dst, driver);
  2008. dst += strlen(dst) + 1;
  2009. map->database = strcpy(dst, database);
  2010. if (table) {
  2011. dst += strlen(dst) + 1;
  2012. map->table = strcpy(dst, table);
  2013. }
  2014. map->priority = priority;
  2015. map->next = config_maps;
  2016. config_maps = map;
  2017. ast_verb(2, "Binding %s to %s/%s/%s\n", map->name, map->driver, map->database, map->table ? map->table : map->name);
  2018. return 0;
  2019. }
  2020. int read_config_maps(void)
  2021. {
  2022. struct ast_config *config, *configtmp;
  2023. struct ast_variable *v;
  2024. char *driver, *table, *database, *textpri, *stringp, *tmp;
  2025. struct ast_flags flags = { CONFIG_FLAG_NOREALTIME };
  2026. int pri;
  2027. clear_config_maps();
  2028. configtmp = ast_config_new();
  2029. if (!configtmp) {
  2030. ast_log(LOG_ERROR, "Unable to allocate memory for new config\n");
  2031. return -1;
  2032. }
  2033. config = ast_config_internal_load(extconfig_conf, configtmp, flags, "", "extconfig");
  2034. if (config == CONFIG_STATUS_FILEINVALID) {
  2035. return -1;
  2036. } else if (!config) {
  2037. ast_config_destroy(configtmp);
  2038. return 0;
  2039. }
  2040. for (v = ast_variable_browse(config, "settings"); v; v = v->next) {
  2041. char buf[512];
  2042. ast_copy_string(buf, v->value, sizeof(buf));
  2043. stringp = buf;
  2044. driver = strsep(&stringp, ",");
  2045. if ((tmp = strchr(stringp, '\"')))
  2046. stringp = tmp;
  2047. /* check if the database text starts with a double quote */
  2048. if (*stringp == '"') {
  2049. stringp++;
  2050. database = strsep(&stringp, "\"");
  2051. strsep(&stringp, ",");
  2052. } else {
  2053. /* apparently this text has no quotes */
  2054. database = strsep(&stringp, ",");
  2055. }
  2056. table = strsep(&stringp, ",");
  2057. textpri = strsep(&stringp, ",");
  2058. if (!textpri || !(pri = atoi(textpri))) {
  2059. pri = 1;
  2060. }
  2061. if (!strcmp(v->name, extconfig_conf)) {
  2062. ast_log(LOG_WARNING, "Cannot bind '%s'!\n", extconfig_conf);
  2063. continue;
  2064. }
  2065. if (!strcmp(v->name, "asterisk.conf")) {
  2066. ast_log(LOG_WARNING, "Cannot bind 'asterisk.conf'!\n");
  2067. continue;
  2068. }
  2069. if (!strcmp(v->name, "logger.conf")) {
  2070. ast_log(LOG_WARNING, "Cannot bind 'logger.conf'!\n");
  2071. continue;
  2072. }
  2073. if (!driver || !database)
  2074. continue;
  2075. if (!strcasecmp(v->name, "sipfriends")) {
  2076. ast_log(LOG_WARNING, "The 'sipfriends' table is obsolete, update your config to use sippeers instead.\n");
  2077. append_mapping("sippeers", driver, database, table ? table : "sipfriends", pri);
  2078. } else if (!strcasecmp(v->name, "iaxfriends")) {
  2079. 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");
  2080. append_mapping("iaxusers", driver, database, table ? table : "iaxfriends", pri);
  2081. append_mapping("iaxpeers", driver, database, table ? table : "iaxfriends", pri);
  2082. } else
  2083. append_mapping(v->name, driver, database, table, pri);
  2084. }
  2085. ast_config_destroy(config);
  2086. return 0;
  2087. }
  2088. int ast_config_engine_register(struct ast_config_engine *new)
  2089. {
  2090. struct ast_config_engine *ptr;
  2091. ast_mutex_lock(&config_lock);
  2092. if (!config_engine_list) {
  2093. config_engine_list = new;
  2094. } else {
  2095. for (ptr = config_engine_list; ptr->next; ptr=ptr->next);
  2096. ptr->next = new;
  2097. }
  2098. ast_mutex_unlock(&config_lock);
  2099. ast_log(LOG_NOTICE,"Registered Config Engine %s\n", new->name);
  2100. return 1;
  2101. }
  2102. int ast_config_engine_deregister(struct ast_config_engine *del)
  2103. {
  2104. struct ast_config_engine *ptr, *last=NULL;
  2105. ast_mutex_lock(&config_lock);
  2106. for (ptr = config_engine_list; ptr; ptr=ptr->next) {
  2107. if (ptr == del) {
  2108. if (last)
  2109. last->next = ptr->next;
  2110. else
  2111. config_engine_list = ptr->next;
  2112. break;
  2113. }
  2114. last = ptr;
  2115. }
  2116. ast_mutex_unlock(&config_lock);
  2117. return 0;
  2118. }
  2119. /*! \brief Find realtime engine for realtime family */
  2120. static struct ast_config_engine *find_engine(const char *family, int priority, char *database, int dbsiz, char *table, int tabsiz)
  2121. {
  2122. struct ast_config_engine *eng, *ret = NULL;
  2123. struct ast_config_map *map;
  2124. ast_mutex_lock(&config_lock);
  2125. for (map = config_maps; map; map = map->next) {
  2126. if (!strcasecmp(family, map->name) && (priority == map->priority)) {
  2127. if (database)
  2128. ast_copy_string(database, map->database, dbsiz);
  2129. if (table)
  2130. ast_copy_string(table, map->table ? map->table : family, tabsiz);
  2131. break;
  2132. }
  2133. }
  2134. /* Check if the required driver (engine) exist */
  2135. if (map) {
  2136. for (eng = config_engine_list; !ret && eng; eng = eng->next) {
  2137. if (!strcasecmp(eng->name, map->driver))
  2138. ret = eng;
  2139. }
  2140. }
  2141. ast_mutex_unlock(&config_lock);
  2142. /* if we found a mapping, but the engine is not available, then issue a warning */
  2143. if (map && !ret)
  2144. ast_log(LOG_WARNING, "Realtime mapping for '%s' found to engine '%s', but the engine is not available\n", map->name, map->driver);
  2145. return ret;
  2146. }
  2147. static struct ast_config_engine text_file_engine = {
  2148. .name = "text",
  2149. .load_func = config_text_file_load,
  2150. };
  2151. 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)
  2152. {
  2153. char db[256];
  2154. char table[256];
  2155. struct ast_config_engine *loader = &text_file_engine;
  2156. struct ast_config *result;
  2157. /* The config file itself bumps include_level by 1 */
  2158. if (cfg->max_include_level > 0 && cfg->include_level == cfg->max_include_level + 1) {
  2159. ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", cfg->max_include_level);
  2160. return NULL;
  2161. }
  2162. cfg->include_level++;
  2163. if (!ast_test_flag(&flags, CONFIG_FLAG_NOREALTIME) && config_engine_list) {
  2164. struct ast_config_engine *eng;
  2165. eng = find_engine(filename, 1, db, sizeof(db), table, sizeof(table));
  2166. if (eng && eng->load_func) {
  2167. loader = eng;
  2168. } else {
  2169. eng = find_engine("global", 1, db, sizeof(db), table, sizeof(table));
  2170. if (eng && eng->load_func)
  2171. loader = eng;
  2172. }
  2173. }
  2174. result = loader->load_func(db, table, filename, cfg, flags, suggested_include_file, who_asked);
  2175. if (result && result != CONFIG_STATUS_FILEINVALID && result != CONFIG_STATUS_FILEUNCHANGED)
  2176. result->include_level--;
  2177. else if (result != CONFIG_STATUS_FILEINVALID)
  2178. cfg->include_level--;
  2179. return result;
  2180. }
  2181. struct ast_config *ast_config_load2(const char *filename, const char *who_asked, struct ast_flags flags)
  2182. {
  2183. struct ast_config *cfg;
  2184. struct ast_config *result;
  2185. cfg = ast_config_new();
  2186. if (!cfg)
  2187. return NULL;
  2188. result = ast_config_internal_load(filename, cfg, flags, "", who_asked);
  2189. if (!result || result == CONFIG_STATUS_FILEUNCHANGED || result == CONFIG_STATUS_FILEINVALID)
  2190. ast_config_destroy(cfg);
  2191. return result;
  2192. }
  2193. static struct ast_variable *ast_load_realtime_helper(const char *family, va_list ap)
  2194. {
  2195. struct ast_config_engine *eng;
  2196. char db[256];
  2197. char table[256];
  2198. struct ast_variable *res=NULL;
  2199. int i;
  2200. for (i = 1; ; i++) {
  2201. if ((eng = find_engine(family, i, db, sizeof(db), table, sizeof(table)))) {
  2202. if (eng->realtime_func && (res = eng->realtime_func(db, table, ap))) {
  2203. return res;
  2204. }
  2205. } else {
  2206. return NULL;
  2207. }
  2208. }
  2209. return res;
  2210. }
  2211. struct ast_variable *ast_load_realtime_all(const char *family, ...)
  2212. {
  2213. struct ast_variable *res;
  2214. va_list ap;
  2215. va_start(ap, family);
  2216. res = ast_load_realtime_helper(family, ap);
  2217. va_end(ap);
  2218. return res;
  2219. }
  2220. struct ast_variable *ast_load_realtime(const char *family, ...)
  2221. {
  2222. struct ast_variable *res;
  2223. struct ast_variable *cur;
  2224. struct ast_variable **prev;
  2225. va_list ap;
  2226. va_start(ap, family);
  2227. res = ast_load_realtime_helper(family, ap);
  2228. va_end(ap);
  2229. /* Filter the list. */
  2230. prev = &res;
  2231. cur = res;
  2232. while (cur) {
  2233. if (ast_strlen_zero(cur->value)) {
  2234. /* Eliminate empty entries */
  2235. struct ast_variable *next;
  2236. next = cur->next;
  2237. *prev = next;
  2238. ast_variable_destroy(cur);
  2239. cur = next;
  2240. } else {
  2241. /* Make blank entries empty and keep them. */
  2242. if (cur->value[0] == ' ' && cur->value[1] == '\0') {
  2243. char *vptr = (char *) cur->value;
  2244. vptr[0] = '\0';
  2245. }
  2246. prev = &cur->next;
  2247. cur = cur->next;
  2248. }
  2249. }
  2250. return res;
  2251. }
  2252. /*! \brief Check if realtime engine is configured for family */
  2253. int ast_check_realtime(const char *family)
  2254. {
  2255. struct ast_config_engine *eng;
  2256. if (!ast_realtime_enabled()) {
  2257. return 0; /* There are no engines at all so fail early */
  2258. }
  2259. eng = find_engine(family, 1, NULL, 0, NULL, 0);
  2260. if (eng)
  2261. return 1;
  2262. return 0;
  2263. }
  2264. /*! \brief Check if there's any realtime engines loaded */
  2265. int ast_realtime_enabled(void)
  2266. {
  2267. return config_maps ? 1 : 0;
  2268. }
  2269. int ast_realtime_require_field(const char *family, ...)
  2270. {
  2271. struct ast_config_engine *eng;
  2272. char db[256];
  2273. char table[256];
  2274. va_list ap;
  2275. int res = -1, i;
  2276. va_start(ap, family);
  2277. for (i = 1; ; i++) {
  2278. if ((eng = find_engine(family, i, db, sizeof(db), table, sizeof(table)))) {
  2279. /* If the require succeeds, it returns 0. */
  2280. if (eng->require_func && !(res = eng->require_func(db, table, ap))) {
  2281. break;
  2282. }
  2283. } else {
  2284. break;
  2285. }
  2286. }
  2287. va_end(ap);
  2288. return res;
  2289. }
  2290. int ast_unload_realtime(const char *family)
  2291. {
  2292. struct ast_config_engine *eng;
  2293. char db[256];
  2294. char table[256];
  2295. int res = -1, i;
  2296. for (i = 1; ; i++) {
  2297. if ((eng = find_engine(family, i, db, sizeof(db), table, sizeof(table)))) {
  2298. if (eng->unload_func) {
  2299. /* Do this for ALL engines */
  2300. res = eng->unload_func(db, table);
  2301. }
  2302. } else {
  2303. break;
  2304. }
  2305. }
  2306. return res;
  2307. }
  2308. struct ast_config *ast_load_realtime_multientry(const char *family, ...)
  2309. {
  2310. struct ast_config_engine *eng;
  2311. char db[256];
  2312. char table[256];
  2313. struct ast_config *res = NULL;
  2314. va_list ap;
  2315. int i;
  2316. va_start(ap, family);
  2317. for (i = 1; ; i++) {
  2318. if ((eng = find_engine(family, i, db, sizeof(db), table, sizeof(table)))) {
  2319. if (eng->realtime_multi_func && (res = eng->realtime_multi_func(db, table, ap))) {
  2320. /* If we were returned an empty cfg, destroy it and return NULL */
  2321. if (!res->root) {
  2322. ast_config_destroy(res);
  2323. res = NULL;
  2324. }
  2325. break;
  2326. }
  2327. } else {
  2328. break;
  2329. }
  2330. }
  2331. va_end(ap);
  2332. return res;
  2333. }
  2334. int ast_update_realtime(const char *family, const char *keyfield, const char *lookup, ...)
  2335. {
  2336. struct ast_config_engine *eng;
  2337. int res = -1, i;
  2338. char db[256];
  2339. char table[256];
  2340. va_list ap;
  2341. va_start(ap, lookup);
  2342. for (i = 1; ; i++) {
  2343. if ((eng = find_engine(family, i, db, sizeof(db), table, sizeof(table)))) {
  2344. /* If the update succeeds, it returns 0. */
  2345. if (eng->update_func && !(res = eng->update_func(db, table, keyfield, lookup, ap))) {
  2346. break;
  2347. }
  2348. } else {
  2349. break;
  2350. }
  2351. }
  2352. va_end(ap);
  2353. return res;
  2354. }
  2355. int ast_update2_realtime(const char *family, ...)
  2356. {
  2357. struct ast_config_engine *eng;
  2358. int res = -1, i;
  2359. char db[256];
  2360. char table[256];
  2361. va_list ap;
  2362. va_start(ap, family);
  2363. for (i = 1; ; i++) {
  2364. if ((eng = find_engine(family, i, db, sizeof(db), table, sizeof(table)))) {
  2365. if (eng->update2_func && !(res = eng->update2_func(db, table, ap))) {
  2366. break;
  2367. }
  2368. } else {
  2369. break;
  2370. }
  2371. }
  2372. va_end(ap);
  2373. return res;
  2374. }
  2375. int ast_store_realtime(const char *family, ...)
  2376. {
  2377. struct ast_config_engine *eng;
  2378. int res = -1, i;
  2379. char db[256];
  2380. char table[256];
  2381. va_list ap;
  2382. va_start(ap, family);
  2383. for (i = 1; ; i++) {
  2384. if ((eng = find_engine(family, i, db, sizeof(db), table, sizeof(table)))) {
  2385. /* If the store succeeds, it returns 0. */
  2386. if (eng->store_func && !(res = eng->store_func(db, table, ap))) {
  2387. break;
  2388. }
  2389. } else {
  2390. break;
  2391. }
  2392. }
  2393. va_end(ap);
  2394. return res;
  2395. }
  2396. int ast_destroy_realtime(const char *family, const char *keyfield, const char *lookup, ...)
  2397. {
  2398. struct ast_config_engine *eng;
  2399. int res = -1, i;
  2400. char db[256];
  2401. char table[256];
  2402. va_list ap;
  2403. va_start(ap, lookup);
  2404. for (i = 1; ; i++) {
  2405. if ((eng = find_engine(family, i, db, sizeof(db), table, sizeof(table)))) {
  2406. if (eng->destroy_func && !(res = eng->destroy_func(db, table, keyfield, lookup, ap))) {
  2407. break;
  2408. }
  2409. } else {
  2410. break;
  2411. }
  2412. }
  2413. va_end(ap);
  2414. return res;
  2415. }
  2416. char *ast_realtime_decode_chunk(char *chunk)
  2417. {
  2418. char *orig = chunk;
  2419. for (; *chunk; chunk++) {
  2420. if (*chunk == '^' && strchr("0123456789ABCDEFabcdef", chunk[1]) && strchr("0123456789ABCDEFabcdef", chunk[2])) {
  2421. sscanf(chunk + 1, "%02hhX", (unsigned char *)chunk);
  2422. memmove(chunk + 1, chunk + 3, strlen(chunk + 3) + 1);
  2423. }
  2424. }
  2425. return orig;
  2426. }
  2427. char *ast_realtime_encode_chunk(struct ast_str **dest, ssize_t maxlen, const char *chunk)
  2428. {
  2429. if (!strchr(chunk, ';') && !strchr(chunk, '^')) {
  2430. ast_str_set(dest, maxlen, "%s", chunk);
  2431. } else {
  2432. ast_str_reset(*dest);
  2433. for (; *chunk; chunk++) {
  2434. if (strchr(";^", *chunk)) {
  2435. ast_str_append(dest, maxlen, "^%02hhX", *chunk);
  2436. } else {
  2437. ast_str_append(dest, maxlen, "%c", *chunk);
  2438. }
  2439. }
  2440. }
  2441. return ast_str_buffer(*dest);
  2442. }
  2443. /*! \brief Helper function to parse arguments
  2444. * See documentation in config.h
  2445. */
  2446. int ast_parse_arg(const char *arg, enum ast_parse_flags flags,
  2447. void *p_result, ...)
  2448. {
  2449. va_list ap;
  2450. int error = 0;
  2451. va_start(ap, p_result);
  2452. switch (flags & PARSE_TYPE) {
  2453. case PARSE_INT32:
  2454. {
  2455. long int x = 0;
  2456. int32_t *result = p_result;
  2457. int32_t def = result ? *result : 0, high = INT32_MAX, low = INT32_MIN;
  2458. char *endptr = NULL;
  2459. /* optional arguments: default value and/or (low, high) */
  2460. if (flags & PARSE_DEFAULT) {
  2461. def = va_arg(ap, int32_t);
  2462. }
  2463. if (flags & (PARSE_IN_RANGE | PARSE_OUT_RANGE)) {
  2464. low = va_arg(ap, int32_t);
  2465. high = va_arg(ap, int32_t);
  2466. }
  2467. if (ast_strlen_zero(arg)) {
  2468. error = 1;
  2469. goto int32_done;
  2470. }
  2471. errno = 0;
  2472. x = strtol(arg, &endptr, 0);
  2473. if (*endptr || errno || x < INT32_MIN || x > INT32_MAX) {
  2474. /* Parse error, or type out of int32_t bounds */
  2475. error = 1;
  2476. goto int32_done;
  2477. }
  2478. error = (x < low) || (x > high);
  2479. if (flags & PARSE_OUT_RANGE) {
  2480. error = !error;
  2481. }
  2482. int32_done:
  2483. if (result) {
  2484. *result = error ? def : x;
  2485. }
  2486. ast_debug(3, "extract int from [%s] in [%d, %d] gives [%ld](%d)\n",
  2487. arg, low, high, result ? *result : x, error);
  2488. break;
  2489. }
  2490. case PARSE_UINT32:
  2491. {
  2492. unsigned long int x = 0;
  2493. uint32_t *result = p_result;
  2494. uint32_t def = result ? *result : 0, low = 0, high = UINT32_MAX;
  2495. char *endptr = NULL;
  2496. /* optional argument: first default value, then range */
  2497. if (flags & PARSE_DEFAULT) {
  2498. def = va_arg(ap, uint32_t);
  2499. }
  2500. if (flags & (PARSE_IN_RANGE|PARSE_OUT_RANGE)) {
  2501. /* range requested, update bounds */
  2502. low = va_arg(ap, uint32_t);
  2503. high = va_arg(ap, uint32_t);
  2504. }
  2505. if (ast_strlen_zero(arg)) {
  2506. error = 1;
  2507. goto uint32_done;
  2508. }
  2509. /* strtoul will happilly and silently negate negative numbers */
  2510. arg = ast_skip_blanks(arg);
  2511. if (*arg == '-') {
  2512. error = 1;
  2513. goto uint32_done;
  2514. }
  2515. errno = 0;
  2516. x = strtoul(arg, &endptr, 0);
  2517. if (*endptr || errno || x > UINT32_MAX) {
  2518. error = 1;
  2519. goto uint32_done;
  2520. }
  2521. error = (x < low) || (x > high);
  2522. if (flags & PARSE_OUT_RANGE) {
  2523. error = !error;
  2524. }
  2525. uint32_done:
  2526. if (result) {
  2527. *result = error ? def : x;
  2528. }
  2529. ast_debug(3, "extract uint from [%s] in [%u, %u] gives [%lu](%d)\n",
  2530. arg, low, high, result ? *result : x, error);
  2531. break;
  2532. }
  2533. case PARSE_DOUBLE:
  2534. {
  2535. double *result = p_result;
  2536. double x = 0, def = result ? *result : 0, low = -HUGE_VAL, high = HUGE_VAL;
  2537. char *endptr = NULL;
  2538. /* optional argument: first default value, then range */
  2539. if (flags & PARSE_DEFAULT) {
  2540. def = va_arg(ap, double);
  2541. }
  2542. if (flags & (PARSE_IN_RANGE | PARSE_OUT_RANGE)) {
  2543. /* range requested, update bounds */
  2544. low = va_arg(ap, double);
  2545. high = va_arg(ap, double);
  2546. }
  2547. if (ast_strlen_zero(arg)) {
  2548. error = 1;
  2549. goto double_done;
  2550. }
  2551. errno = 0;
  2552. x = strtod(arg, &endptr);
  2553. if (*endptr || errno == ERANGE) {
  2554. error = 1;
  2555. goto double_done;
  2556. }
  2557. error = (x < low) || (x > high);
  2558. if (flags & PARSE_OUT_RANGE) {
  2559. error = !error;
  2560. }
  2561. double_done:
  2562. if (result) {
  2563. *result = error ? def : x;
  2564. }
  2565. ast_debug(3, "extract double from [%s] in [%f, %f] gives [%f](%d)\n",
  2566. arg, low, high, result ? *result : x, error);
  2567. break;
  2568. }
  2569. case PARSE_ADDR:
  2570. {
  2571. struct ast_sockaddr *addr = (struct ast_sockaddr *)p_result;
  2572. if (!ast_sockaddr_parse(addr, arg, flags & PARSE_PORT_MASK)) {
  2573. error = 1;
  2574. }
  2575. ast_debug(3, "extract addr from %s gives %s(%d)\n",
  2576. arg, ast_sockaddr_stringify(addr), error);
  2577. break;
  2578. }
  2579. case PARSE_INADDR: /* TODO Remove this (use PARSE_ADDR instead). */
  2580. {
  2581. char *port, *buf;
  2582. struct sockaddr_in _sa_buf; /* buffer for the result */
  2583. struct sockaddr_in *sa = p_result ?
  2584. (struct sockaddr_in *)p_result : &_sa_buf;
  2585. /* default is either the supplied value or the result itself */
  2586. struct sockaddr_in *def = (flags & PARSE_DEFAULT) ?
  2587. va_arg(ap, struct sockaddr_in *) : sa;
  2588. struct hostent *hp;
  2589. struct ast_hostent ahp;
  2590. memset(&_sa_buf, '\0', sizeof(_sa_buf)); /* clear buffer */
  2591. /* duplicate the string to strip away the :port */
  2592. port = ast_strdupa(arg);
  2593. buf = strsep(&port, ":");
  2594. sa->sin_family = AF_INET; /* assign family */
  2595. /*
  2596. * honor the ports flag setting, assign default value
  2597. * in case of errors or field unset.
  2598. */
  2599. flags &= PARSE_PORT_MASK; /* the only flags left to process */
  2600. if (port) {
  2601. if (flags == PARSE_PORT_FORBID) {
  2602. error = 1; /* port was forbidden */
  2603. sa->sin_port = def->sin_port;
  2604. } else if (flags == PARSE_PORT_IGNORE)
  2605. sa->sin_port = def->sin_port;
  2606. else /* accept or require */
  2607. sa->sin_port = htons(strtol(port, NULL, 0));
  2608. } else {
  2609. sa->sin_port = def->sin_port;
  2610. if (flags == PARSE_PORT_REQUIRE)
  2611. error = 1;
  2612. }
  2613. /* Now deal with host part, even if we have errors before. */
  2614. hp = ast_gethostbyname(buf, &ahp);
  2615. if (hp) /* resolved successfully */
  2616. memcpy(&sa->sin_addr, hp->h_addr, sizeof(sa->sin_addr));
  2617. else {
  2618. error = 1;
  2619. sa->sin_addr = def->sin_addr;
  2620. }
  2621. ast_debug(3,
  2622. "extract inaddr from [%s] gives [%s:%d](%d)\n",
  2623. arg, ast_inet_ntoa(sa->sin_addr),
  2624. ntohs(sa->sin_port), error);
  2625. break;
  2626. }
  2627. }
  2628. va_end(ap);
  2629. return error;
  2630. }
  2631. static char *handle_cli_core_show_config_mappings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2632. {
  2633. struct ast_config_engine *eng;
  2634. struct ast_config_map *map;
  2635. switch (cmd) {
  2636. case CLI_INIT:
  2637. e->command = "core show config mappings";
  2638. e->usage =
  2639. "Usage: core show config mappings\n"
  2640. " Shows the filenames to config engines.\n";
  2641. return NULL;
  2642. case CLI_GENERATE:
  2643. return NULL;
  2644. }
  2645. ast_mutex_lock(&config_lock);
  2646. if (!config_engine_list) {
  2647. ast_cli(a->fd, "No config mappings found.\n");
  2648. } else {
  2649. for (eng = config_engine_list; eng; eng = eng->next) {
  2650. ast_cli(a->fd, "Config Engine: %s\n", eng->name);
  2651. for (map = config_maps; map; map = map->next) {
  2652. if (!strcasecmp(map->driver, eng->name)) {
  2653. ast_cli(a->fd, "===> %s (db=%s, table=%s)\n", map->name, map->database,
  2654. map->table ? map->table : map->name);
  2655. }
  2656. }
  2657. }
  2658. }
  2659. ast_mutex_unlock(&config_lock);
  2660. return CLI_SUCCESS;
  2661. }
  2662. static char *handle_cli_config_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2663. {
  2664. struct cache_file_mtime *cfmtime;
  2665. char *prev = "", *completion_value = NULL;
  2666. int wordlen, which = 0;
  2667. switch (cmd) {
  2668. case CLI_INIT:
  2669. e->command = "config reload";
  2670. e->usage =
  2671. "Usage: config reload <filename.conf>\n"
  2672. " Reloads all modules that reference <filename.conf>\n";
  2673. return NULL;
  2674. case CLI_GENERATE:
  2675. if (a->pos > 2) {
  2676. return NULL;
  2677. }
  2678. wordlen = strlen(a->word);
  2679. AST_LIST_LOCK(&cfmtime_head);
  2680. AST_LIST_TRAVERSE(&cfmtime_head, cfmtime, list) {
  2681. /* Skip duplicates - this only works because the list is sorted by filename */
  2682. if (strcmp(cfmtime->filename, prev) == 0) {
  2683. continue;
  2684. }
  2685. /* Core configs cannot be reloaded */
  2686. if (ast_strlen_zero(cfmtime->who_asked)) {
  2687. continue;
  2688. }
  2689. if (++which > a->n && strncmp(cfmtime->filename, a->word, wordlen) == 0) {
  2690. completion_value = ast_strdup(cfmtime->filename);
  2691. break;
  2692. }
  2693. /* Otherwise save that we've seen this filename */
  2694. prev = cfmtime->filename;
  2695. }
  2696. AST_LIST_UNLOCK(&cfmtime_head);
  2697. return completion_value;
  2698. }
  2699. if (a->argc != 3) {
  2700. return CLI_SHOWUSAGE;
  2701. }
  2702. AST_LIST_LOCK(&cfmtime_head);
  2703. AST_LIST_TRAVERSE(&cfmtime_head, cfmtime, list) {
  2704. if (!strcmp(cfmtime->filename, a->argv[2])) {
  2705. char *buf = ast_alloca(strlen("module reload ") + strlen(cfmtime->who_asked) + 1);
  2706. sprintf(buf, "module reload %s", cfmtime->who_asked);
  2707. ast_cli_command(a->fd, buf);
  2708. }
  2709. }
  2710. AST_LIST_UNLOCK(&cfmtime_head);
  2711. return CLI_SUCCESS;
  2712. }
  2713. static char *handle_cli_config_list(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2714. {
  2715. struct cache_file_mtime *cfmtime;
  2716. switch (cmd) {
  2717. case CLI_INIT:
  2718. e->command = "config list";
  2719. e->usage =
  2720. "Usage: config list\n"
  2721. " Show all modules that have loaded a configuration file\n";
  2722. return NULL;
  2723. case CLI_GENERATE:
  2724. return NULL;
  2725. }
  2726. AST_LIST_LOCK(&cfmtime_head);
  2727. AST_LIST_TRAVERSE(&cfmtime_head, cfmtime, list) {
  2728. ast_cli(a->fd, "%-20.20s %-50s\n", S_OR(cfmtime->who_asked, "core"), cfmtime->filename);
  2729. }
  2730. AST_LIST_UNLOCK(&cfmtime_head);
  2731. return CLI_SUCCESS;
  2732. }
  2733. static struct ast_cli_entry cli_config[] = {
  2734. AST_CLI_DEFINE(handle_cli_core_show_config_mappings, "Display config mappings (file names to config engines)"),
  2735. AST_CLI_DEFINE(handle_cli_config_reload, "Force a reload on modules using a particular configuration file"),
  2736. AST_CLI_DEFINE(handle_cli_config_list, "Show all files that have loaded a configuration file"),
  2737. };
  2738. static void config_shutdown(void)
  2739. {
  2740. struct cache_file_mtime *cfmtime;
  2741. AST_LIST_LOCK(&cfmtime_head);
  2742. while ((cfmtime = AST_LIST_REMOVE_HEAD(&cfmtime_head, list))) {
  2743. config_cache_destroy_entry(cfmtime);
  2744. }
  2745. AST_LIST_UNLOCK(&cfmtime_head);
  2746. ast_cli_unregister_multiple(cli_config, ARRAY_LEN(cli_config));
  2747. }
  2748. int register_config_cli(void)
  2749. {
  2750. ast_cli_register_multiple(cli_config, ARRAY_LEN(cli_config));
  2751. ast_register_atexit(config_shutdown);
  2752. return 0;
  2753. }