util.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 General Public License for more details.
  13. */
  14. /*
  15. * This file contains misc utility functions and wrappers to standard
  16. * functions, which throw exceptions upon failure.
  17. */
  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 <ctype.h>
  24. #include <fcntl.h>
  25. #define LINEBUF 32
  26. #define LINEBUF_MAX 4096
  27. __plugin void *uci_malloc(struct uci_context *ctx, size_t size)
  28. {
  29. void *ptr;
  30. ptr = malloc(size);
  31. if (!ptr)
  32. UCI_THROW(ctx, UCI_ERR_MEM);
  33. memset(ptr, 0, size);
  34. return ptr;
  35. }
  36. __plugin void *uci_realloc(struct uci_context *ctx, void *ptr, size_t size)
  37. {
  38. ptr = realloc(ptr, size);
  39. if (!ptr)
  40. UCI_THROW(ctx, UCI_ERR_MEM);
  41. return ptr;
  42. }
  43. __plugin char *uci_strdup(struct uci_context *ctx, const char *str)
  44. {
  45. char *ptr;
  46. ptr = strdup(str);
  47. if (!ptr)
  48. UCI_THROW(ctx, UCI_ERR_MEM);
  49. return ptr;
  50. }
  51. /* Based on an efficient hash function published by D. J. Bernstein */
  52. static unsigned int djbhash(unsigned int hash, char *str)
  53. {
  54. int len = strlen(str);
  55. int i;
  56. /* initial value */
  57. if (hash == ~0)
  58. hash = 5381;
  59. for(i = 0; i < len; i++) {
  60. hash = ((hash << 5) + hash) + str[i];
  61. }
  62. return (hash & 0x7FFFFFFF);
  63. }
  64. /*
  65. * validate strings for names and types, reject special characters
  66. * for names, only alphanum and _ is allowed (shell compatibility)
  67. * for types, we allow more characters
  68. */
  69. __plugin bool uci_validate_str(const char *str, bool name)
  70. {
  71. if (!*str)
  72. return false;
  73. while (*str) {
  74. char c = *str;
  75. if (!isalnum(c) && c != '_') {
  76. if (name || (c < 33) || (c > 126))
  77. return false;
  78. }
  79. str++;
  80. }
  81. return true;
  82. }
  83. static inline bool uci_validate_name(const char *str)
  84. {
  85. return uci_validate_str(str, true);
  86. }
  87. bool uci_validate_text(const char *str)
  88. {
  89. while (*str) {
  90. if ((*str == '\r') || (*str == '\n') ||
  91. ((*str < 32) && (*str != '\t')))
  92. return false;
  93. str++;
  94. }
  95. return true;
  96. }
  97. static void uci_alloc_parse_context(struct uci_context *ctx)
  98. {
  99. ctx->pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
  100. }
  101. int uci_parse_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str)
  102. {
  103. char *last = NULL;
  104. char *tmp;
  105. UCI_HANDLE_ERR(ctx);
  106. UCI_ASSERT(ctx, str);
  107. UCI_ASSERT(ctx, ptr);
  108. memset(ptr, 0, sizeof(struct uci_ptr));
  109. /* value */
  110. last = strchr(str, '=');
  111. if (last) {
  112. *last = 0;
  113. last++;
  114. ptr->value = last;
  115. }
  116. ptr->package = strsep(&str, ".");
  117. if (!ptr->package)
  118. goto error;
  119. ptr->section = strsep(&str, ".");
  120. if (!ptr->section) {
  121. ptr->target = UCI_TYPE_PACKAGE;
  122. goto lastval;
  123. }
  124. ptr->option = strsep(&str, ".");
  125. if (!ptr->option) {
  126. ptr->target = UCI_TYPE_SECTION;
  127. goto lastval;
  128. } else {
  129. ptr->target = UCI_TYPE_OPTION;
  130. }
  131. tmp = strsep(&str, ".");
  132. if (tmp)
  133. goto error;
  134. lastval:
  135. if (ptr->package && !uci_validate_str(ptr->package, false))
  136. goto error;
  137. if (ptr->section && !uci_validate_name(ptr->section))
  138. ptr->flags |= UCI_LOOKUP_EXTENDED;
  139. if (ptr->option && !uci_validate_name(ptr->option))
  140. goto error;
  141. if (ptr->value && !uci_validate_text(ptr->value))
  142. goto error;
  143. return 0;
  144. error:
  145. memset(ptr, 0, sizeof(struct uci_ptr));
  146. UCI_THROW(ctx, UCI_ERR_PARSE);
  147. }
  148. static void uci_parse_error(struct uci_context *ctx, char *pos, char *reason)
  149. {
  150. struct uci_parse_context *pctx = ctx->pctx;
  151. pctx->reason = reason;
  152. pctx->byte = pos - pctx->buf;
  153. UCI_THROW(ctx, UCI_ERR_PARSE);
  154. }
  155. /*
  156. * Fetch a new line from the input stream and resize buffer if necessary
  157. */
  158. static void uci_getln(struct uci_context *ctx, int offset)
  159. {
  160. struct uci_parse_context *pctx = ctx->pctx;
  161. char *p;
  162. int ofs;
  163. if (pctx->buf == NULL) {
  164. pctx->buf = uci_malloc(ctx, LINEBUF);
  165. pctx->bufsz = LINEBUF;
  166. }
  167. ofs = offset;
  168. do {
  169. p = &pctx->buf[ofs];
  170. p[ofs] = 0;
  171. p = fgets(p, pctx->bufsz - ofs, pctx->file);
  172. if (!p || !*p)
  173. return;
  174. ofs += strlen(p);
  175. if (pctx->buf[ofs - 1] == '\n') {
  176. pctx->line++;
  177. pctx->buf[ofs - 1] = 0;
  178. return;
  179. }
  180. if (pctx->bufsz > LINEBUF_MAX/2)
  181. uci_parse_error(ctx, p, "line too long");
  182. pctx->bufsz *= 2;
  183. pctx->buf = uci_realloc(ctx, pctx->buf, pctx->bufsz);
  184. } while (1);
  185. }
  186. /*
  187. * parse a character escaped by '\'
  188. * returns true if the escaped character is to be parsed
  189. * returns false if the escaped character is to be ignored
  190. */
  191. static inline bool parse_backslash(struct uci_context *ctx, char **str)
  192. {
  193. /* skip backslash */
  194. *str += 1;
  195. /* undecoded backslash at the end of line, fetch the next line */
  196. if (!**str) {
  197. *str += 1;
  198. uci_getln(ctx, *str - ctx->pctx->buf);
  199. return false;
  200. }
  201. /* FIXME: decode escaped char, necessary? */
  202. return true;
  203. }
  204. /*
  205. * move the string pointer forward until a non-whitespace character or
  206. * EOL is reached
  207. */
  208. static void skip_whitespace(struct uci_context *ctx, char **str)
  209. {
  210. restart:
  211. while (**str && isspace(**str))
  212. *str += 1;
  213. if (**str == '\\') {
  214. if (!parse_backslash(ctx, str))
  215. goto restart;
  216. }
  217. }
  218. static inline void addc(char **dest, char **src)
  219. {
  220. **dest = **src;
  221. *dest += 1;
  222. *src += 1;
  223. }
  224. /*
  225. * parse a double quoted string argument from the command line
  226. */
  227. static void parse_double_quote(struct uci_context *ctx, char **str, char **target)
  228. {
  229. char c;
  230. /* skip quote character */
  231. *str += 1;
  232. while ((c = **str)) {
  233. switch(c) {
  234. case '"':
  235. **target = 0;
  236. *str += 1;
  237. return;
  238. case '\\':
  239. if (!parse_backslash(ctx, str))
  240. continue;
  241. /* fall through */
  242. default:
  243. addc(target, str);
  244. break;
  245. }
  246. }
  247. uci_parse_error(ctx, *str, "unterminated \"");
  248. }
  249. /*
  250. * parse a single quoted string argument from the command line
  251. */
  252. static void parse_single_quote(struct uci_context *ctx, char **str, char **target)
  253. {
  254. char c;
  255. /* skip quote character */
  256. *str += 1;
  257. while ((c = **str)) {
  258. switch(c) {
  259. case '\'':
  260. **target = 0;
  261. *str += 1;
  262. return;
  263. default:
  264. addc(target, str);
  265. }
  266. }
  267. uci_parse_error(ctx, *str, "unterminated '");
  268. }
  269. /*
  270. * parse a string from the command line and detect the quoting style
  271. */
  272. static void parse_str(struct uci_context *ctx, char **str, char **target)
  273. {
  274. bool next = true;
  275. do {
  276. switch(**str) {
  277. case '\'':
  278. parse_single_quote(ctx, str, target);
  279. break;
  280. case '"':
  281. parse_double_quote(ctx, str, target);
  282. break;
  283. case '#':
  284. **str = 0;
  285. /* fall through */
  286. case 0:
  287. goto done;
  288. case ';':
  289. next = false;
  290. goto done;
  291. case '\\':
  292. if (!parse_backslash(ctx, str))
  293. continue;
  294. /* fall through */
  295. default:
  296. addc(target, str);
  297. break;
  298. }
  299. } while (**str && !isspace(**str));
  300. done:
  301. /*
  302. * if the string was unquoted and we've stopped at a whitespace
  303. * character, skip to the next one, because the whitespace will
  304. * be overwritten by a null byte here
  305. */
  306. if (**str && next)
  307. *str += 1;
  308. /* terminate the parsed string */
  309. **target = 0;
  310. }
  311. /*
  312. * extract the next argument from the command line
  313. */
  314. static char *next_arg(struct uci_context *ctx, char **str, bool required, bool name)
  315. {
  316. char *val;
  317. char *ptr;
  318. val = ptr = *str;
  319. skip_whitespace(ctx, str);
  320. if(*str[0] == ';') {
  321. *str[0] = 0;
  322. *str += 1;
  323. } else {
  324. parse_str(ctx, str, &ptr);
  325. }
  326. if (!*val) {
  327. if (required)
  328. uci_parse_error(ctx, *str, "insufficient arguments");
  329. goto done;
  330. }
  331. if (name && !uci_validate_name(val))
  332. uci_parse_error(ctx, val, "invalid character in field");
  333. done:
  334. return val;
  335. }
  336. int uci_parse_argument(struct uci_context *ctx, FILE *stream, char **str, char **result)
  337. {
  338. UCI_HANDLE_ERR(ctx);
  339. UCI_ASSERT(ctx, str != NULL);
  340. UCI_ASSERT(ctx, result != NULL);
  341. if (ctx->pctx && (ctx->pctx->file != stream))
  342. uci_cleanup(ctx);
  343. if (!ctx->pctx)
  344. uci_alloc_parse_context(ctx);
  345. ctx->pctx->file = stream;
  346. if (!*str) {
  347. uci_getln(ctx, 0);
  348. *str = ctx->pctx->buf;
  349. }
  350. *result = next_arg(ctx, str, false, false);
  351. return 0;
  352. }
  353. /*
  354. * open a stream and go to the right position
  355. *
  356. * note: when opening for write and seeking to the beginning of
  357. * the stream, truncate the file
  358. */
  359. static FILE *uci_open_stream(struct uci_context *ctx, const char *filename, int pos, bool write, bool create)
  360. {
  361. struct stat statbuf;
  362. FILE *file = NULL;
  363. int fd, ret;
  364. int mode = (write ? O_RDWR : O_RDONLY);
  365. if (create)
  366. mode |= O_CREAT;
  367. if (!write && ((stat(filename, &statbuf) < 0) ||
  368. ((statbuf.st_mode & S_IFMT) != S_IFREG))) {
  369. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  370. }
  371. fd = open(filename, mode, UCI_FILEMODE);
  372. if (fd < 0)
  373. goto error;
  374. if (flock(fd, (write ? LOCK_EX : LOCK_SH)) < 0)
  375. goto error;
  376. ret = lseek(fd, 0, pos);
  377. if (ret < 0)
  378. goto error;
  379. file = fdopen(fd, (write ? "w+" : "r"));
  380. if (file)
  381. goto done;
  382. error:
  383. UCI_THROW(ctx, UCI_ERR_IO);
  384. done:
  385. return file;
  386. }
  387. static void uci_close_stream(FILE *stream)
  388. {
  389. int fd;
  390. if (!stream)
  391. return;
  392. fd = fileno(stream);
  393. flock(fd, LOCK_UN);
  394. fclose(stream);
  395. }