file.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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. /*
  33. * Fetch a new line from the input stream and resize buffer if necessary
  34. */
  35. __private void uci_getln(struct uci_context *ctx, int offset)
  36. {
  37. struct uci_parse_context *pctx = ctx->pctx;
  38. char *p;
  39. int ofs;
  40. if (pctx->buf == NULL) {
  41. pctx->buf = uci_malloc(ctx, LINEBUF);
  42. pctx->bufsz = LINEBUF;
  43. }
  44. /* It takes 2 slots for fgets to read 1 char. */
  45. if (offset >= pctx->bufsz - 1) {
  46. pctx->bufsz *= 2;
  47. pctx->buf = uci_realloc(ctx, pctx->buf, pctx->bufsz);
  48. }
  49. ofs = offset;
  50. do {
  51. p = &pctx->buf[ofs];
  52. p[0] = 0;
  53. p = fgets(p, pctx->bufsz - ofs, pctx->file);
  54. if (!p || !*p)
  55. return;
  56. ofs += strlen(p);
  57. if (pctx->buf[ofs - 1] == '\n') {
  58. pctx->line++;
  59. return;
  60. }
  61. pctx->bufsz *= 2;
  62. pctx->buf = uci_realloc(ctx, pctx->buf, pctx->bufsz);
  63. } while (1);
  64. }
  65. /*
  66. * parse a character escaped by '\'
  67. * returns true if the escaped character is to be parsed
  68. * returns false if the escaped character is to be ignored
  69. */
  70. static bool parse_backslash(struct uci_context *ctx)
  71. {
  72. struct uci_parse_context *pctx = ctx->pctx;
  73. /* skip backslash */
  74. pctx->pos += 1;
  75. /* undecoded backslash at the end of line, fetch the next line */
  76. if (!pctx_cur_char(pctx) ||
  77. pctx_cur_char(pctx) == '\n' ||
  78. (pctx_cur_char(pctx) == '\r' &&
  79. pctx_char(pctx, pctx_pos(pctx) + 1) == '\n' &&
  80. !pctx_char(pctx, pctx_pos(pctx) + 2))) {
  81. uci_getln(ctx, pctx->pos);
  82. return false;
  83. }
  84. /* FIXME: decode escaped char, necessary? */
  85. return true;
  86. }
  87. /*
  88. * move the string pointer forward until a non-whitespace character or
  89. * EOL is reached
  90. */
  91. static void skip_whitespace(struct uci_context *ctx)
  92. {
  93. struct uci_parse_context *pctx = ctx->pctx;
  94. while (pctx_cur_char(pctx) && isspace(pctx_cur_char(pctx)))
  95. pctx->pos += 1;
  96. }
  97. static inline void addc(struct uci_context *ctx, int *pos_dest, int *pos_src)
  98. {
  99. struct uci_parse_context *pctx = ctx->pctx;
  100. pctx_char(pctx, *pos_dest) = pctx_char(pctx, *pos_src);
  101. *pos_dest += 1;
  102. *pos_src += 1;
  103. }
  104. /*
  105. * parse a double quoted string argument from the command line
  106. */
  107. static void parse_double_quote(struct uci_context *ctx, int *target)
  108. {
  109. struct uci_parse_context *pctx = ctx->pctx;
  110. char c;
  111. /* skip quote character */
  112. pctx->pos += 1;
  113. while (1) {
  114. c = pctx_cur_char(pctx);
  115. switch(c) {
  116. case '"':
  117. pctx->pos += 1;
  118. return;
  119. case 0:
  120. /* Multi-line str value */
  121. uci_getln(ctx, pctx->pos);
  122. if (!pctx_cur_char(pctx)) {
  123. uci_parse_error(ctx, "EOF with unterminated \"");
  124. }
  125. break;
  126. case '\\':
  127. if (!parse_backslash(ctx))
  128. continue;
  129. /* fall through */
  130. default:
  131. addc(ctx, target, &pctx->pos);
  132. break;
  133. }
  134. }
  135. }
  136. /*
  137. * parse a single quoted string argument from the command line
  138. */
  139. static void parse_single_quote(struct uci_context *ctx, int *target)
  140. {
  141. struct uci_parse_context *pctx = ctx->pctx;
  142. char c;
  143. /* skip quote character */
  144. pctx->pos += 1;
  145. while (1) {
  146. c = pctx_cur_char(pctx);
  147. switch(c) {
  148. case '\'':
  149. pctx->pos += 1;
  150. return;
  151. case 0:
  152. /* Multi-line str value */
  153. uci_getln(ctx, pctx->pos);
  154. if (!pctx_cur_char(pctx))
  155. uci_parse_error(ctx, "EOF with unterminated '");
  156. break;
  157. default:
  158. addc(ctx, target, &pctx->pos);
  159. }
  160. }
  161. }
  162. /*
  163. * parse a string from the command line and detect the quoting style
  164. */
  165. static void parse_str(struct uci_context *ctx, int *target)
  166. {
  167. struct uci_parse_context *pctx = ctx->pctx;
  168. bool next = true;
  169. do {
  170. switch(pctx_cur_char(pctx)) {
  171. case '\'':
  172. parse_single_quote(ctx, target);
  173. break;
  174. case '"':
  175. parse_double_quote(ctx, target);
  176. break;
  177. case '#':
  178. pctx_cur_char(pctx) = 0;
  179. /* fall through */
  180. case 0:
  181. goto done;
  182. case ';':
  183. next = false;
  184. goto done;
  185. case '\\':
  186. if (!parse_backslash(ctx))
  187. continue;
  188. /* fall through */
  189. default:
  190. addc(ctx, target, &pctx->pos);
  191. break;
  192. }
  193. } while (pctx_cur_char(pctx) && !isspace(pctx_cur_char(pctx)));
  194. done:
  195. /*
  196. * if the string was unquoted and we've stopped at a whitespace
  197. * character, skip to the next one, because the whitespace will
  198. * be overwritten by a null byte here
  199. */
  200. if (pctx_cur_char(pctx) && next)
  201. pctx->pos += 1;
  202. /* terminate the parsed string */
  203. pctx_char(pctx, *target) = 0;
  204. }
  205. /*
  206. * extract the next argument from the command line
  207. */
  208. static int next_arg(struct uci_context *ctx, bool required, bool name, bool package)
  209. {
  210. struct uci_parse_context *pctx = ctx->pctx;
  211. int val, ptr;
  212. skip_whitespace(ctx);
  213. val = ptr = pctx_pos(pctx);
  214. if (pctx_cur_char(pctx) == ';') {
  215. pctx_cur_char(pctx) = 0;
  216. pctx->pos += 1;
  217. } else {
  218. parse_str(ctx, &ptr);
  219. }
  220. if (!pctx_char(pctx, val)) {
  221. if (required)
  222. uci_parse_error(ctx, "insufficient arguments");
  223. goto done;
  224. }
  225. if (name && !uci_validate_str(pctx_str(pctx, val), name, package))
  226. uci_parse_error(ctx, "invalid character in name field");
  227. done:
  228. return val;
  229. }
  230. int uci_parse_argument(struct uci_context *ctx, FILE *stream, char **str, char **result)
  231. {
  232. int ofs_result;
  233. UCI_HANDLE_ERR(ctx);
  234. UCI_ASSERT(ctx, str != NULL);
  235. UCI_ASSERT(ctx, result != NULL);
  236. if (ctx->pctx && (ctx->pctx->file != stream))
  237. uci_cleanup(ctx);
  238. if (!ctx->pctx)
  239. uci_alloc_parse_context(ctx);
  240. ctx->pctx->file = stream;
  241. if (!*str) {
  242. ctx->pctx->pos = 0;
  243. uci_getln(ctx, 0);
  244. }
  245. ofs_result = next_arg(ctx, false, false, false);
  246. *result = pctx_str(ctx->pctx, ofs_result);
  247. *str = pctx_cur_str(ctx->pctx);
  248. return 0;
  249. }
  250. static int
  251. uci_fill_ptr(struct uci_context *ctx, struct uci_ptr *ptr, struct uci_element *e)
  252. {
  253. UCI_ASSERT(ctx, ptr != NULL);
  254. UCI_ASSERT(ctx, e != NULL);
  255. memset(ptr, 0, sizeof(struct uci_ptr));
  256. switch(e->type) {
  257. case UCI_TYPE_OPTION:
  258. ptr->o = uci_to_option(e);
  259. goto fill_option;
  260. case UCI_TYPE_SECTION:
  261. ptr->s = uci_to_section(e);
  262. goto fill_section;
  263. case UCI_TYPE_PACKAGE:
  264. ptr->p = uci_to_package(e);
  265. goto fill_package;
  266. default:
  267. UCI_THROW(ctx, UCI_ERR_INVAL);
  268. }
  269. fill_option:
  270. ptr->option = ptr->o->e.name;
  271. ptr->s = ptr->o->section;
  272. fill_section:
  273. ptr->section = ptr->s->e.name;
  274. ptr->p = ptr->s->package;
  275. fill_package:
  276. ptr->package = ptr->p->e.name;
  277. ptr->flags |= UCI_LOOKUP_DONE;
  278. return 0;
  279. }
  280. /*
  281. * verify that the end of the line or command is reached.
  282. * throw an error if extra arguments are given on the command line
  283. */
  284. static void assert_eol(struct uci_context *ctx)
  285. {
  286. char *tmp;
  287. int ofs_tmp;
  288. skip_whitespace(ctx);
  289. ofs_tmp = next_arg(ctx, false, false, false);
  290. tmp = pctx_str(ctx->pctx, ofs_tmp);
  291. if (*tmp && (ctx->flags & UCI_FLAG_STRICT))
  292. uci_parse_error(ctx, "too many arguments");
  293. }
  294. /*
  295. * switch to a different config, either triggered by uci_load, or by a
  296. * 'package <...>' statement in the import file
  297. */
  298. static void uci_switch_config(struct uci_context *ctx)
  299. {
  300. struct uci_parse_context *pctx;
  301. struct uci_element *e;
  302. const char *name;
  303. pctx = ctx->pctx;
  304. name = pctx->name;
  305. /* add the last config to main config file list */
  306. if (pctx->package) {
  307. pctx->package->backend = ctx->backend;
  308. uci_list_add(&ctx->root, &pctx->package->e.list);
  309. pctx->package = NULL;
  310. pctx->section = NULL;
  311. }
  312. if (!name)
  313. return;
  314. /*
  315. * if an older config under the same name exists, unload it
  316. * ignore errors here, e.g. if the config was not found
  317. */
  318. e = uci_lookup_list(&ctx->root, name);
  319. if (e)
  320. UCI_THROW(ctx, UCI_ERR_DUPLICATE);
  321. pctx->package = uci_alloc_package(ctx, name);
  322. }
  323. /*
  324. * parse the 'package' uci command (next config package)
  325. */
  326. static void uci_parse_package(struct uci_context *ctx, bool single)
  327. {
  328. struct uci_parse_context *pctx = ctx->pctx;
  329. int ofs_name;
  330. char *name;
  331. /* command string null-terminated by strtok */
  332. pctx->pos += strlen(pctx_cur_str(pctx)) + 1;
  333. ofs_name = next_arg(ctx, true, true, true);
  334. name = pctx_str(pctx, ofs_name);
  335. assert_eol(ctx);
  336. if (single)
  337. return;
  338. ctx->pctx->name = name;
  339. uci_switch_config(ctx);
  340. }
  341. /*
  342. * parse the 'config' uci command (open a section)
  343. */
  344. static void uci_parse_config(struct uci_context *ctx)
  345. {
  346. struct uci_parse_context *pctx = ctx->pctx;
  347. struct uci_element *e;
  348. struct uci_ptr ptr;
  349. int ofs_name, ofs_type;
  350. char *name;
  351. char *type;
  352. if (!ctx->pctx->package) {
  353. if (!ctx->pctx->name)
  354. uci_parse_error(ctx, "attempting to import a file without a package name");
  355. uci_switch_config(ctx);
  356. }
  357. /* command string null-terminated by strtok */
  358. pctx->pos += strlen(pctx_cur_str(pctx)) + 1;
  359. ofs_type = next_arg(ctx, true, false, false);
  360. type = pctx_str(pctx, ofs_type);
  361. if (!uci_validate_type(type))
  362. uci_parse_error(ctx, "invalid character in type field");
  363. ofs_name = next_arg(ctx, false, true, false);
  364. type = pctx_str(pctx, ofs_type);
  365. name = pctx_str(pctx, ofs_name);
  366. assert_eol(ctx);
  367. if (!name || !name[0]) {
  368. ctx->internal = !pctx->merge;
  369. UCI_NESTED(uci_add_section, ctx, pctx->package, type, &pctx->section);
  370. } else {
  371. uci_fill_ptr(ctx, &ptr, &pctx->package->e);
  372. e = uci_lookup_list(&pctx->package->sections, name);
  373. if (e) {
  374. ptr.s = uci_to_section(e);
  375. if ((ctx->flags & UCI_FLAG_STRICT) && strcmp(ptr.s->type, type))
  376. uci_parse_error(ctx, "section of different type overwrites prior section with same name");
  377. }
  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. if (!pctx->package && name)
  593. uci_switch_config(ctx);
  594. if (package)
  595. *package = pctx->package;
  596. if (pctx->merge)
  597. pctx->package = NULL;
  598. pctx->name = NULL;
  599. uci_switch_config(ctx);
  600. /* no error happened, we can get rid of the parser context now */
  601. uci_cleanup(ctx);
  602. return 0;
  603. }
  604. static char *uci_config_path(struct uci_context *ctx, const char *name)
  605. {
  606. char *filename;
  607. UCI_ASSERT(ctx, uci_validate_package(name));
  608. filename = uci_malloc(ctx, strlen(name) + strlen(ctx->confdir) + 2);
  609. sprintf(filename, "%s/%s", ctx->confdir, name);
  610. return filename;
  611. }
  612. static void uci_file_commit(struct uci_context *ctx, struct uci_package **package, bool overwrite)
  613. {
  614. struct uci_package *p = *package;
  615. FILE *f1, *f2 = NULL;
  616. char *name = NULL;
  617. char *path = NULL;
  618. char *filename = NULL;
  619. struct stat statbuf;
  620. bool do_rename = false;
  621. if (!p->path) {
  622. if (overwrite)
  623. p->path = uci_config_path(ctx, p->e.name);
  624. else
  625. UCI_THROW(ctx, UCI_ERR_INVAL);
  626. }
  627. if ((asprintf(&filename, "%s/.%s.uci-XXXXXX", ctx->confdir, p->e.name) < 0) || !filename)
  628. UCI_THROW(ctx, UCI_ERR_MEM);
  629. /* open the config file for writing now, so that it is locked */
  630. f1 = uci_open_stream(ctx, p->path, NULL, SEEK_SET, true, true);
  631. /* flush unsaved changes and reload from delta file */
  632. UCI_TRAP_SAVE(ctx, done);
  633. if (p->has_delta) {
  634. if (!overwrite) {
  635. name = uci_strdup(ctx, p->e.name);
  636. path = uci_strdup(ctx, p->path);
  637. /* dump our own changes to the delta file */
  638. if (!uci_list_empty(&p->delta))
  639. UCI_INTERNAL(uci_save, ctx, p);
  640. /*
  641. * other processes might have modified the config
  642. * as well. dump and reload
  643. */
  644. uci_free_package(&p);
  645. uci_cleanup(ctx);
  646. UCI_INTERNAL(uci_import, ctx, f1, name, &p, true);
  647. p->path = path;
  648. p->has_delta = true;
  649. *package = p;
  650. /* freed together with the uci_package */
  651. path = NULL;
  652. }
  653. /* flush delta */
  654. if (!uci_load_delta(ctx, p, true))
  655. goto done;
  656. }
  657. if (!mktemp(filename))
  658. *filename = 0;
  659. if (!*filename) {
  660. free(filename);
  661. UCI_THROW(ctx, UCI_ERR_IO);
  662. }
  663. if ((stat(filename, &statbuf) == 0) && ((statbuf.st_mode & S_IFMT) != S_IFREG))
  664. UCI_THROW(ctx, UCI_ERR_IO);
  665. f2 = uci_open_stream(ctx, filename, p->path, SEEK_SET, true, true);
  666. uci_export(ctx, f2, p, false);
  667. fflush(f2);
  668. fsync(fileno(f2));
  669. uci_close_stream(f2);
  670. do_rename = true;
  671. UCI_TRAP_RESTORE(ctx);
  672. done:
  673. free(name);
  674. free(path);
  675. uci_close_stream(f1);
  676. if (do_rename) {
  677. path = realpath(p->path, NULL);
  678. if (!path || rename(filename, path)) {
  679. unlink(filename);
  680. UCI_THROW(ctx, UCI_ERR_IO);
  681. }
  682. free(path);
  683. }
  684. free(filename);
  685. if (ctx->err)
  686. UCI_THROW(ctx, ctx->err);
  687. }
  688. /*
  689. * This function returns the filename by returning the string
  690. * after the last '/' character. By checking for a non-'\0'
  691. * character afterwards, directories are ignored (glob marks
  692. * those with a trailing '/'
  693. */
  694. static inline char *get_filename(char *path)
  695. {
  696. char *p;
  697. p = strrchr(path, '/');
  698. p++;
  699. if (!*p)
  700. return NULL;
  701. return p;
  702. }
  703. static char **uci_list_config_files(struct uci_context *ctx)
  704. {
  705. char **configs;
  706. glob_t globbuf;
  707. int size, i, j, skipped;
  708. char *buf;
  709. char *dir;
  710. dir = uci_malloc(ctx, strlen(ctx->confdir) + 1 + sizeof("/*"));
  711. sprintf(dir, "%s/*", ctx->confdir);
  712. if (glob(dir, GLOB_MARK, NULL, &globbuf) != 0) {
  713. free(dir);
  714. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  715. }
  716. size = sizeof(char *) * (globbuf.gl_pathc + 1);
  717. skipped = 0;
  718. for(i = 0; i < globbuf.gl_pathc; i++) {
  719. char *p;
  720. p = get_filename(globbuf.gl_pathv[i]);
  721. if (!p) {
  722. skipped++;
  723. continue;
  724. }
  725. size += strlen(p) + 1;
  726. }
  727. configs = uci_malloc(ctx, size - skipped);
  728. buf = (char *) &configs[globbuf.gl_pathc + 1 - skipped];
  729. j = 0;
  730. for(i = 0; i < globbuf.gl_pathc; i++) {
  731. char *p;
  732. p = get_filename(globbuf.gl_pathv[i]);
  733. if (!p)
  734. continue;
  735. if (!uci_validate_package(p))
  736. continue;
  737. configs[j++] = buf;
  738. strcpy(buf, p);
  739. buf += strlen(buf) + 1;
  740. }
  741. free(dir);
  742. globfree(&globbuf);
  743. return configs;
  744. }
  745. static struct uci_package *uci_file_load(struct uci_context *ctx, const char *name)
  746. {
  747. struct uci_package *package = NULL;
  748. char *filename;
  749. bool confdir;
  750. FILE *file = NULL;
  751. switch (name[0]) {
  752. case '.':
  753. /* relative path outside of /etc/config */
  754. if (name[1] != '/')
  755. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  756. /* fall through */
  757. case '/':
  758. /* absolute path outside of /etc/config */
  759. filename = uci_strdup(ctx, name);
  760. name = strrchr(name, '/') + 1;
  761. confdir = false;
  762. break;
  763. default:
  764. /* config in /etc/config */
  765. filename = uci_config_path(ctx, name);
  766. confdir = true;
  767. break;
  768. }
  769. UCI_TRAP_SAVE(ctx, done);
  770. file = uci_open_stream(ctx, filename, NULL, SEEK_SET, false, false);
  771. ctx->err = 0;
  772. UCI_INTERNAL(uci_import, ctx, file, name, &package, true);
  773. UCI_TRAP_RESTORE(ctx);
  774. if (package) {
  775. package->path = filename;
  776. package->has_delta = confdir;
  777. uci_load_delta(ctx, package, false);
  778. }
  779. done:
  780. uci_close_stream(file);
  781. if (ctx->err) {
  782. free(filename);
  783. UCI_THROW(ctx, ctx->err);
  784. }
  785. return package;
  786. }
  787. __private UCI_BACKEND(uci_file_backend, "file",
  788. .load = uci_file_load,
  789. .commit = uci_file_commit,
  790. .list_configs = uci_list_config_files,
  791. );