file.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. /*
  2. * libuci - Library for the Unified Configuration Interface
  3. * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License version 2.1
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. */
  14. /*
  15. * This file contains the code for parsing uci config files
  16. */
  17. #define _GNU_SOURCE
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <sys/file.h>
  21. #include <stdbool.h>
  22. #include <unistd.h>
  23. #include <fcntl.h>
  24. #include <stdio.h>
  25. #include <ctype.h>
  26. #include <glob.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include "uci.h"
  30. #include "uci_internal.h"
  31. #define LINEBUF 32
  32. #define LINEBUF_MAX 4096
  33. /*
  34. * Fetch a new line from the input stream and resize buffer if necessary
  35. */
  36. __private void uci_getln(struct uci_context *ctx, int offset)
  37. {
  38. struct uci_parse_context *pctx = ctx->pctx;
  39. char *p;
  40. int ofs;
  41. if (pctx->buf == NULL) {
  42. pctx->buf = uci_malloc(ctx, LINEBUF);
  43. pctx->bufsz = LINEBUF;
  44. }
  45. /* It takes 2 slots for fgets to read 1 char. */
  46. if (offset >= pctx->bufsz - 1) {
  47. pctx->bufsz *= 2;
  48. pctx->buf = uci_realloc(ctx, pctx->buf, pctx->bufsz);
  49. }
  50. ofs = offset;
  51. do {
  52. p = &pctx->buf[ofs];
  53. p[0] = 0;
  54. p = fgets(p, pctx->bufsz - ofs, pctx->file);
  55. if (!p || !*p)
  56. return;
  57. ofs += strlen(p);
  58. if (pctx->buf[ofs - 1] == '\n') {
  59. pctx->line++;
  60. return;
  61. }
  62. if (pctx->bufsz > LINEBUF_MAX/2)
  63. uci_parse_error(ctx, "line too long");
  64. pctx->bufsz *= 2;
  65. pctx->buf = uci_realloc(ctx, pctx->buf, pctx->bufsz);
  66. } while (1);
  67. }
  68. /*
  69. * parse a character escaped by '\'
  70. * returns true if the escaped character is to be parsed
  71. * returns false if the escaped character is to be ignored
  72. */
  73. static bool parse_backslash(struct uci_context *ctx)
  74. {
  75. struct uci_parse_context *pctx = ctx->pctx;
  76. /* skip backslash */
  77. pctx->pos += 1;
  78. /* undecoded backslash at the end of line, fetch the next line */
  79. if (!pctx_cur_char(pctx) ||
  80. pctx_cur_char(pctx) == '\n' ||
  81. (pctx_cur_char(pctx) == '\r' &&
  82. pctx_char(pctx, pctx_pos(pctx) + 1) == '\n' &&
  83. !pctx_char(pctx, pctx_pos(pctx) + 2))) {
  84. uci_getln(ctx, pctx->pos);
  85. return false;
  86. }
  87. /* FIXME: decode escaped char, necessary? */
  88. return true;
  89. }
  90. /*
  91. * move the string pointer forward until a non-whitespace character or
  92. * EOL is reached
  93. */
  94. static void skip_whitespace(struct uci_context *ctx)
  95. {
  96. struct uci_parse_context *pctx = ctx->pctx;
  97. while (pctx_cur_char(pctx) && isspace(pctx_cur_char(pctx)))
  98. pctx->pos += 1;
  99. }
  100. static inline void addc(struct uci_context *ctx, int *pos_dest, int *pos_src)
  101. {
  102. struct uci_parse_context *pctx = ctx->pctx;
  103. pctx_char(pctx, *pos_dest) = pctx_char(pctx, *pos_src);
  104. *pos_dest += 1;
  105. *pos_src += 1;
  106. }
  107. /*
  108. * parse a double quoted string argument from the command line
  109. */
  110. static void parse_double_quote(struct uci_context *ctx, int *target)
  111. {
  112. struct uci_parse_context *pctx = ctx->pctx;
  113. char c;
  114. /* skip quote character */
  115. pctx->pos += 1;
  116. while (1) {
  117. c = pctx_cur_char(pctx);
  118. switch(c) {
  119. case '"':
  120. pctx->pos += 1;
  121. return;
  122. case 0:
  123. /* Multi-line str value */
  124. uci_getln(ctx, pctx->pos);
  125. if (!pctx_cur_char(pctx)) {
  126. uci_parse_error(ctx, "EOF with unterminated \"");
  127. }
  128. break;
  129. case '\\':
  130. if (!parse_backslash(ctx))
  131. continue;
  132. /* fall through */
  133. default:
  134. addc(ctx, target, &pctx->pos);
  135. break;
  136. }
  137. }
  138. }
  139. /*
  140. * parse a single quoted string argument from the command line
  141. */
  142. static void parse_single_quote(struct uci_context *ctx, int *target)
  143. {
  144. struct uci_parse_context *pctx = ctx->pctx;
  145. char c;
  146. /* skip quote character */
  147. pctx->pos += 1;
  148. while (1) {
  149. c = pctx_cur_char(pctx);
  150. switch(c) {
  151. case '\'':
  152. pctx->pos += 1;
  153. return;
  154. case 0:
  155. /* Multi-line str value */
  156. uci_getln(ctx, pctx->pos);
  157. if (!pctx_cur_char(pctx))
  158. uci_parse_error(ctx, "EOF with unterminated '");
  159. break;
  160. default:
  161. addc(ctx, target, &pctx->pos);
  162. }
  163. }
  164. }
  165. /*
  166. * parse a string from the command line and detect the quoting style
  167. */
  168. static void parse_str(struct uci_context *ctx, int *target)
  169. {
  170. struct uci_parse_context *pctx = ctx->pctx;
  171. bool next = true;
  172. do {
  173. switch(pctx_cur_char(pctx)) {
  174. case '\'':
  175. parse_single_quote(ctx, target);
  176. break;
  177. case '"':
  178. parse_double_quote(ctx, target);
  179. break;
  180. case '#':
  181. pctx_cur_char(pctx) = 0;
  182. /* fall through */
  183. case 0:
  184. goto done;
  185. case ';':
  186. next = false;
  187. goto done;
  188. case '\\':
  189. if (!parse_backslash(ctx))
  190. continue;
  191. /* fall through */
  192. default:
  193. addc(ctx, target, &pctx->pos);
  194. break;
  195. }
  196. } while (pctx_cur_char(pctx) && !isspace(pctx_cur_char(pctx)));
  197. done:
  198. /*
  199. * if the string was unquoted and we've stopped at a whitespace
  200. * character, skip to the next one, because the whitespace will
  201. * be overwritten by a null byte here
  202. */
  203. if (pctx_cur_char(pctx) && next)
  204. pctx->pos += 1;
  205. /* terminate the parsed string */
  206. pctx_char(pctx, *target) = 0;
  207. }
  208. /*
  209. * extract the next argument from the command line
  210. */
  211. static int next_arg(struct uci_context *ctx, bool required, bool name, bool package)
  212. {
  213. struct uci_parse_context *pctx = ctx->pctx;
  214. int val, ptr;
  215. skip_whitespace(ctx);
  216. val = ptr = pctx_pos(pctx);
  217. if (pctx_cur_char(pctx) == ';') {
  218. pctx_cur_char(pctx) = 0;
  219. pctx->pos += 1;
  220. } else {
  221. parse_str(ctx, &ptr);
  222. }
  223. if (!pctx_char(pctx, val)) {
  224. if (required)
  225. uci_parse_error(ctx, "insufficient arguments");
  226. goto done;
  227. }
  228. if (name && !uci_validate_str(pctx_str(pctx, val), name, package))
  229. uci_parse_error(ctx, "invalid character in name field");
  230. done:
  231. return val;
  232. }
  233. int uci_parse_argument(struct uci_context *ctx, FILE *stream, char **str, char **result)
  234. {
  235. int ofs_result;
  236. UCI_HANDLE_ERR(ctx);
  237. UCI_ASSERT(ctx, str != NULL);
  238. UCI_ASSERT(ctx, result != NULL);
  239. if (ctx->pctx && (ctx->pctx->file != stream))
  240. uci_cleanup(ctx);
  241. if (!ctx->pctx)
  242. uci_alloc_parse_context(ctx);
  243. ctx->pctx->file = stream;
  244. if (!*str)
  245. uci_getln(ctx, 0);
  246. /*FIXME do we need to skip empty lines? */
  247. ofs_result = next_arg(ctx, false, false, false);
  248. *result = pctx_str(ctx->pctx, ofs_result);
  249. *str = pctx_cur_str(ctx->pctx);
  250. return 0;
  251. }
  252. static int
  253. uci_fill_ptr(struct uci_context *ctx, struct uci_ptr *ptr, struct uci_element *e)
  254. {
  255. UCI_ASSERT(ctx, ptr != NULL);
  256. UCI_ASSERT(ctx, e != NULL);
  257. memset(ptr, 0, sizeof(struct uci_ptr));
  258. switch(e->type) {
  259. case UCI_TYPE_OPTION:
  260. ptr->o = uci_to_option(e);
  261. goto fill_option;
  262. case UCI_TYPE_SECTION:
  263. ptr->s = uci_to_section(e);
  264. goto fill_section;
  265. case UCI_TYPE_PACKAGE:
  266. ptr->p = uci_to_package(e);
  267. goto fill_package;
  268. default:
  269. UCI_THROW(ctx, UCI_ERR_INVAL);
  270. }
  271. fill_option:
  272. ptr->option = ptr->o->e.name;
  273. ptr->s = ptr->o->section;
  274. fill_section:
  275. ptr->section = ptr->s->e.name;
  276. ptr->p = ptr->s->package;
  277. fill_package:
  278. ptr->package = ptr->p->e.name;
  279. ptr->flags |= UCI_LOOKUP_DONE;
  280. return 0;
  281. }
  282. /*
  283. * verify that the end of the line or command is reached.
  284. * throw an error if extra arguments are given on the command line
  285. */
  286. static void assert_eol(struct uci_context *ctx)
  287. {
  288. char *tmp;
  289. int ofs_tmp;
  290. skip_whitespace(ctx);
  291. ofs_tmp = next_arg(ctx, false, false, false);
  292. tmp = pctx_str(ctx->pctx, ofs_tmp);
  293. if (*tmp && (ctx->flags & UCI_FLAG_STRICT))
  294. uci_parse_error(ctx, "too many arguments");
  295. }
  296. /*
  297. * switch to a different config, either triggered by uci_load, or by a
  298. * 'package <...>' statement in the import file
  299. */
  300. static void uci_switch_config(struct uci_context *ctx)
  301. {
  302. struct uci_parse_context *pctx;
  303. struct uci_element *e;
  304. const char *name;
  305. pctx = ctx->pctx;
  306. name = pctx->name;
  307. /* add the last config to main config file list */
  308. if (pctx->package) {
  309. pctx->package->backend = ctx->backend;
  310. uci_list_add(&ctx->root, &pctx->package->e.list);
  311. pctx->package = NULL;
  312. pctx->section = NULL;
  313. }
  314. if (!name)
  315. return;
  316. /*
  317. * if an older config under the same name exists, unload it
  318. * ignore errors here, e.g. if the config was not found
  319. */
  320. e = uci_lookup_list(&ctx->root, name);
  321. if (e)
  322. UCI_THROW(ctx, UCI_ERR_DUPLICATE);
  323. pctx->package = uci_alloc_package(ctx, name);
  324. }
  325. /*
  326. * parse the 'package' uci command (next config package)
  327. */
  328. static void uci_parse_package(struct uci_context *ctx, bool single)
  329. {
  330. struct uci_parse_context *pctx = ctx->pctx;
  331. int ofs_name;
  332. char *name;
  333. /* command string null-terminated by strtok */
  334. pctx->pos += strlen(pctx_cur_str(pctx)) + 1;
  335. ofs_name = next_arg(ctx, true, true, true);
  336. name = pctx_str(pctx, ofs_name);
  337. assert_eol(ctx);
  338. if (single)
  339. return;
  340. ctx->pctx->name = name;
  341. uci_switch_config(ctx);
  342. }
  343. /*
  344. * parse the 'config' uci command (open a section)
  345. */
  346. static void uci_parse_config(struct uci_context *ctx)
  347. {
  348. struct uci_parse_context *pctx = ctx->pctx;
  349. struct uci_element *e;
  350. struct uci_ptr ptr;
  351. int ofs_name, ofs_type;
  352. char *name;
  353. char *type;
  354. uci_fixup_section(ctx, ctx->pctx->section);
  355. if (!ctx->pctx->package) {
  356. if (!ctx->pctx->name)
  357. uci_parse_error(ctx, "attempting to import a file without a package name");
  358. uci_switch_config(ctx);
  359. }
  360. /* command string null-terminated by strtok */
  361. pctx->pos += strlen(pctx_cur_str(pctx)) + 1;
  362. ofs_type = next_arg(ctx, true, false, false);
  363. type = pctx_str(pctx, ofs_type);
  364. if (!uci_validate_type(type))
  365. uci_parse_error(ctx, "invalid character in type field");
  366. ofs_name = next_arg(ctx, false, true, false);
  367. type = pctx_str(pctx, ofs_type);
  368. name = pctx_str(pctx, ofs_name);
  369. assert_eol(ctx);
  370. if (!name || !name[0]) {
  371. ctx->internal = !pctx->merge;
  372. UCI_NESTED(uci_add_section, ctx, pctx->package, type, &pctx->section);
  373. } else {
  374. uci_fill_ptr(ctx, &ptr, &pctx->package->e);
  375. e = uci_lookup_list(&pctx->package->sections, name);
  376. if (e)
  377. ptr.s = uci_to_section(e);
  378. ptr.section = name;
  379. ptr.value = type;
  380. ctx->internal = !pctx->merge;
  381. UCI_NESTED(uci_set, ctx, &ptr);
  382. pctx->section = uci_to_section(ptr.last);
  383. }
  384. }
  385. /*
  386. * parse the 'option' uci command (open a value)
  387. */
  388. static void uci_parse_option(struct uci_context *ctx, bool list)
  389. {
  390. struct uci_parse_context *pctx = ctx->pctx;
  391. struct uci_element *e;
  392. struct uci_ptr ptr;
  393. int ofs_name, ofs_value;
  394. char *name = NULL;
  395. char *value = NULL;
  396. if (!pctx->section)
  397. uci_parse_error(ctx, "option/list command found before the first section");
  398. /* command string null-terminated by strtok */
  399. pctx->pos += strlen(pctx_cur_str(pctx)) + 1;
  400. ofs_name = next_arg(ctx, true, true, false);
  401. ofs_value = next_arg(ctx, false, false, false);
  402. name = pctx_str(pctx, ofs_name);
  403. value = pctx_str(pctx, ofs_value);
  404. assert_eol(ctx);
  405. uci_fill_ptr(ctx, &ptr, &pctx->section->e);
  406. e = uci_lookup_list(&pctx->section->options, name);
  407. if (e)
  408. ptr.o = uci_to_option(e);
  409. ptr.option = name;
  410. ptr.value = value;
  411. ctx->internal = !pctx->merge;
  412. if (list)
  413. UCI_NESTED(uci_add_list, ctx, &ptr);
  414. else
  415. UCI_NESTED(uci_set, ctx, &ptr);
  416. }
  417. /*
  418. * parse a complete input line, split up combined commands by ';'
  419. */
  420. static void uci_parse_line(struct uci_context *ctx, bool single)
  421. {
  422. struct uci_parse_context *pctx = ctx->pctx;
  423. char *word;
  424. /* Skip whitespace characters at the start of line */
  425. skip_whitespace(ctx);
  426. do {
  427. word = strtok(pctx_cur_str(pctx), " \t");
  428. if (!word)
  429. return;
  430. switch(word[0]) {
  431. case 0:
  432. case '#':
  433. return;
  434. case 'p':
  435. if ((word[1] == 0) || !strcmp(word + 1, "ackage"))
  436. uci_parse_package(ctx, single);
  437. else
  438. goto invalid;
  439. break;
  440. case 'c':
  441. if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
  442. uci_parse_config(ctx);
  443. else
  444. goto invalid;
  445. break;
  446. case 'o':
  447. if ((word[1] == 0) || !strcmp(word + 1, "ption"))
  448. uci_parse_option(ctx, false);
  449. else
  450. goto invalid;
  451. break;
  452. case 'l':
  453. if ((word[1] == 0) || !strcmp(word + 1, "ist"))
  454. uci_parse_option(ctx, true);
  455. else
  456. goto invalid;
  457. break;
  458. default:
  459. goto invalid;
  460. }
  461. continue;
  462. invalid:
  463. uci_parse_error(ctx, "invalid command");
  464. } while (1);
  465. }
  466. /* max number of characters that escaping adds to the string */
  467. #define UCI_QUOTE_ESCAPE "'\\''"
  468. /*
  469. * escape an uci string for export
  470. */
  471. static const char *uci_escape(struct uci_context *ctx, const char *str)
  472. {
  473. const char *end;
  474. int ofs = 0;
  475. if (!ctx->buf) {
  476. ctx->bufsz = LINEBUF;
  477. ctx->buf = malloc(LINEBUF);
  478. if (!ctx->buf)
  479. return str;
  480. }
  481. while (1) {
  482. int len;
  483. end = strchr(str, '\'');
  484. if (!end)
  485. end = str + strlen(str);
  486. len = end - str;
  487. /* make sure that we have enough room in the buffer */
  488. while (ofs + len + sizeof(UCI_QUOTE_ESCAPE) + 1 > ctx->bufsz) {
  489. ctx->bufsz *= 2;
  490. ctx->buf = uci_realloc(ctx, ctx->buf, ctx->bufsz);
  491. }
  492. /* copy the string until the character before the quote */
  493. memcpy(&ctx->buf[ofs], str, len);
  494. ofs += len;
  495. /* end of string? return the buffer */
  496. if (*end == 0)
  497. break;
  498. memcpy(&ctx->buf[ofs], UCI_QUOTE_ESCAPE, sizeof(UCI_QUOTE_ESCAPE));
  499. ofs += strlen(&ctx->buf[ofs]);
  500. str = end + 1;
  501. }
  502. ctx->buf[ofs] = 0;
  503. return ctx->buf;
  504. }
  505. /*
  506. * export a single config package to a file stream
  507. */
  508. static void uci_export_package(struct uci_package *p, FILE *stream, bool header)
  509. {
  510. struct uci_context *ctx = p->ctx;
  511. struct uci_element *s, *o, *i;
  512. if (header)
  513. fprintf(stream, "package %s\n", uci_escape(ctx, p->e.name));
  514. uci_foreach_element(&p->sections, s) {
  515. struct uci_section *sec = uci_to_section(s);
  516. fprintf(stream, "\nconfig %s", uci_escape(ctx, sec->type));
  517. if (!sec->anonymous || (ctx->flags & UCI_FLAG_EXPORT_NAME))
  518. fprintf(stream, " '%s'", uci_escape(ctx, sec->e.name));
  519. fprintf(stream, "\n");
  520. uci_foreach_element(&sec->options, o) {
  521. struct uci_option *opt = uci_to_option(o);
  522. switch(opt->type) {
  523. case UCI_TYPE_STRING:
  524. fprintf(stream, "\toption %s", uci_escape(ctx, opt->e.name));
  525. fprintf(stream, " '%s'\n", uci_escape(ctx, opt->v.string));
  526. break;
  527. case UCI_TYPE_LIST:
  528. uci_foreach_element(&opt->v.list, i) {
  529. fprintf(stream, "\tlist %s", uci_escape(ctx, opt->e.name));
  530. fprintf(stream, " '%s'\n", uci_escape(ctx, i->name));
  531. }
  532. break;
  533. default:
  534. fprintf(stream, "\t# unknown type for option '%s'\n", uci_escape(ctx, opt->e.name));
  535. break;
  536. }
  537. }
  538. }
  539. fprintf(stream, "\n");
  540. }
  541. int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package, bool header)
  542. {
  543. struct uci_element *e;
  544. UCI_HANDLE_ERR(ctx);
  545. UCI_ASSERT(ctx, stream != NULL);
  546. if (package)
  547. uci_export_package(package, stream, header);
  548. else {
  549. uci_foreach_element(&ctx->root, e) {
  550. uci_export_package(uci_to_package(e), stream, header);
  551. }
  552. }
  553. return 0;
  554. }
  555. int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package, bool single)
  556. {
  557. struct uci_parse_context *pctx;
  558. UCI_HANDLE_ERR(ctx);
  559. /* make sure no memory from previous parse attempts is leaked */
  560. uci_cleanup(ctx);
  561. uci_alloc_parse_context(ctx);
  562. pctx = ctx->pctx;
  563. pctx->file = stream;
  564. if (package && *package && single) {
  565. pctx->package = *package;
  566. pctx->merge = true;
  567. }
  568. /*
  569. * If 'name' was supplied, assume that the supplied stream does not contain
  570. * the appropriate 'package <name>' string to specify the config name
  571. * NB: the config file can still override the package name
  572. */
  573. if (name) {
  574. UCI_ASSERT(ctx, uci_validate_package(name));
  575. pctx->name = name;
  576. }
  577. while (!feof(pctx->file)) {
  578. pctx->pos = 0;
  579. uci_getln(ctx, 0);
  580. UCI_TRAP_SAVE(ctx, error);
  581. if (pctx->buf[0])
  582. uci_parse_line(ctx, single);
  583. UCI_TRAP_RESTORE(ctx);
  584. continue;
  585. error:
  586. if (ctx->flags & UCI_FLAG_PERROR)
  587. uci_perror(ctx, NULL);
  588. if ((ctx->err != UCI_ERR_PARSE) ||
  589. (ctx->flags & UCI_FLAG_STRICT))
  590. UCI_THROW(ctx, ctx->err);
  591. }
  592. uci_fixup_section(ctx, ctx->pctx->section);
  593. if (!pctx->package && name)
  594. uci_switch_config(ctx);
  595. if (package)
  596. *package = pctx->package;
  597. if (pctx->merge)
  598. pctx->package = NULL;
  599. pctx->name = NULL;
  600. uci_switch_config(ctx);
  601. /* no error happened, we can get rid of the parser context now */
  602. uci_cleanup(ctx);
  603. return 0;
  604. }
  605. static char *uci_config_path(struct uci_context *ctx, const char *name)
  606. {
  607. char *filename;
  608. UCI_ASSERT(ctx, uci_validate_package(name));
  609. filename = uci_malloc(ctx, strlen(name) + strlen(ctx->confdir) + 2);
  610. sprintf(filename, "%s/%s", ctx->confdir, name);
  611. return filename;
  612. }
  613. static void uci_file_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite)
  614. {
  615. struct uci_package *p = *package;
  616. FILE *f1, *f2 = NULL;
  617. char *name = NULL;
  618. char *path = NULL;
  619. char *filename = NULL;
  620. struct stat statbuf;
  621. bool do_rename = false;
  622. if (!p->path) {
  623. if (overwrite)
  624. p->path = uci_config_path(ctx, p->e.name);
  625. else
  626. UCI_THROW(ctx, UCI_ERR_INVAL);
  627. }
  628. if ((asprintf(&filename, "%s/.%s.uci-XXXXXX", ctx->confdir, p->e.name) < 0) || !filename)
  629. UCI_THROW(ctx, UCI_ERR_MEM);
  630. if (!mktemp(filename))
  631. *filename = 0;
  632. if (!*filename) {
  633. free(filename);
  634. UCI_THROW(ctx, UCI_ERR_IO);
  635. }
  636. if ((stat(filename, &statbuf) == 0) && ((statbuf.st_mode & S_IFMT) != S_IFREG))
  637. UCI_THROW(ctx, UCI_ERR_IO);
  638. /* open the config file for writing now, so that it is locked */
  639. f1 = uci_open_stream(ctx, p->path, NULL, SEEK_SET, true, true);
  640. /* flush unsaved changes and reload from delta file */
  641. UCI_TRAP_SAVE(ctx, done);
  642. if (p->has_delta) {
  643. if (!overwrite) {
  644. name = uci_strdup(ctx, p->e.name);
  645. path = uci_strdup(ctx, p->path);
  646. /* dump our own changes to the delta file */
  647. if (!uci_list_empty(&p->delta))
  648. UCI_INTERNAL(uci_save, ctx, p);
  649. /*
  650. * other processes might have modified the config
  651. * as well. dump and reload
  652. */
  653. uci_free_package(&p);
  654. uci_cleanup(ctx);
  655. UCI_INTERNAL(uci_import, ctx, f1, name, &p, true);
  656. p->path = path;
  657. p->has_delta = true;
  658. *package = p;
  659. /* freed together with the uci_package */
  660. path = NULL;
  661. }
  662. /* flush delta */
  663. if (!uci_load_delta(ctx, p, true))
  664. goto done;
  665. }
  666. f2 = uci_open_stream(ctx, filename, p->path, SEEK_SET, true, true);
  667. uci_export(ctx, f2, p, false);
  668. fflush(f2);
  669. fsync(fileno(f2));
  670. uci_close_stream(f2);
  671. do_rename = true;
  672. UCI_TRAP_RESTORE(ctx);
  673. done:
  674. free(name);
  675. free(path);
  676. uci_close_stream(f1);
  677. if (do_rename && rename(filename, p->path)) {
  678. unlink(filename);
  679. UCI_THROW(ctx, UCI_ERR_IO);
  680. }
  681. free(filename);
  682. sync();
  683. if (ctx->err)
  684. UCI_THROW(ctx, ctx->err);
  685. }
  686. /*
  687. * This function returns the filename by returning the string
  688. * after the last '/' character. By checking for a non-'\0'
  689. * character afterwards, directories are ignored (glob marks
  690. * those with a trailing '/'
  691. */
  692. static inline char *get_filename(char *path)
  693. {
  694. char *p;
  695. p = strrchr(path, '/');
  696. p++;
  697. if (!*p)
  698. return NULL;
  699. return p;
  700. }
  701. static char **uci_list_config_files(struct uci_context *ctx)
  702. {
  703. char **configs;
  704. glob_t globbuf;
  705. int size, i;
  706. char *buf;
  707. char *dir;
  708. dir = uci_malloc(ctx, strlen(ctx->confdir) + 1 + sizeof("/*"));
  709. sprintf(dir, "%s/*", ctx->confdir);
  710. if (glob(dir, GLOB_MARK, NULL, &globbuf) != 0) {
  711. free(dir);
  712. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  713. }
  714. size = sizeof(char *) * (globbuf.gl_pathc + 1);
  715. for(i = 0; i < globbuf.gl_pathc; i++) {
  716. char *p;
  717. p = get_filename(globbuf.gl_pathv[i]);
  718. if (!p)
  719. continue;
  720. size += strlen(p) + 1;
  721. }
  722. configs = uci_malloc(ctx, size);
  723. buf = (char *) &configs[globbuf.gl_pathc + 1];
  724. for(i = 0; i < globbuf.gl_pathc; i++) {
  725. char *p;
  726. p = get_filename(globbuf.gl_pathv[i]);
  727. if (!p)
  728. continue;
  729. if (!uci_validate_package(p))
  730. continue;
  731. configs[i] = buf;
  732. strcpy(buf, p);
  733. buf += strlen(buf) + 1;
  734. }
  735. free(dir);
  736. globfree(&globbuf);
  737. return configs;
  738. }
  739. static struct uci_package *uci_file_load(struct uci_context *ctx, const char *name)
  740. {
  741. struct uci_package *package = NULL;
  742. char *filename;
  743. bool confdir;
  744. FILE *file = NULL;
  745. switch (name[0]) {
  746. case '.':
  747. /* relative path outside of /etc/config */
  748. if (name[1] != '/')
  749. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  750. /* fall through */
  751. case '/':
  752. /* absolute path outside of /etc/config */
  753. filename = uci_strdup(ctx, name);
  754. name = strrchr(name, '/') + 1;
  755. confdir = false;
  756. break;
  757. default:
  758. /* config in /etc/config */
  759. filename = uci_config_path(ctx, name);
  760. confdir = true;
  761. break;
  762. }
  763. UCI_TRAP_SAVE(ctx, done);
  764. file = uci_open_stream(ctx, filename, NULL, SEEK_SET, false, false);
  765. ctx->err = 0;
  766. UCI_INTERNAL(uci_import, ctx, file, name, &package, true);
  767. UCI_TRAP_RESTORE(ctx);
  768. if (package) {
  769. package->path = filename;
  770. package->has_delta = confdir;
  771. uci_load_delta(ctx, package, false);
  772. }
  773. done:
  774. uci_close_stream(file);
  775. if (ctx->err) {
  776. free(filename);
  777. UCI_THROW(ctx, ctx->err);
  778. }
  779. return package;
  780. }
  781. __private UCI_BACKEND(uci_file_backend, "file",
  782. .load = uci_file_load,
  783. .commit = uci_file_commit,
  784. .list_configs = uci_list_config_files,
  785. );