util.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. static void uci_alloc_parse_context(struct uci_context *ctx)
  88. {
  89. ctx->pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
  90. }
  91. int uci_parse_tuple(struct uci_context *ctx, char *str, char **package, char **section, char **option, char **value)
  92. {
  93. char *last = NULL;
  94. UCI_HANDLE_ERR(ctx);
  95. UCI_ASSERT(ctx, str && package && section && option);
  96. last = strchr(str, '=');
  97. if (last) {
  98. *last = 0;
  99. last++;
  100. }
  101. *package = strsep(&str, ".");
  102. if (!*package || !uci_validate_name(*package))
  103. goto error;
  104. *section = strsep(&str, ".");
  105. if (!*section)
  106. goto lastval;
  107. *option = strsep(&str, ".");
  108. if (!*option)
  109. goto lastval;
  110. lastval:
  111. if (last) {
  112. if (!value)
  113. goto error;
  114. if (!*last)
  115. goto error;
  116. *value = last;
  117. }
  118. if (*section && *section[0] && !uci_validate_name(*section))
  119. goto error;
  120. if (*option && !uci_validate_name(*option))
  121. goto error;
  122. goto done;
  123. error:
  124. UCI_THROW(ctx, UCI_ERR_PARSE);
  125. done:
  126. return 0;
  127. }
  128. static void uci_parse_error(struct uci_context *ctx, char *pos, char *reason)
  129. {
  130. struct uci_parse_context *pctx = ctx->pctx;
  131. pctx->reason = reason;
  132. pctx->byte = pos - pctx->buf;
  133. UCI_THROW(ctx, UCI_ERR_PARSE);
  134. }
  135. /*
  136. * Fetch a new line from the input stream and resize buffer if necessary
  137. */
  138. static void uci_getln(struct uci_context *ctx, int offset)
  139. {
  140. struct uci_parse_context *pctx = ctx->pctx;
  141. char *p;
  142. int ofs;
  143. if (pctx->buf == NULL) {
  144. pctx->buf = uci_malloc(ctx, LINEBUF);
  145. pctx->bufsz = LINEBUF;
  146. }
  147. ofs = offset;
  148. do {
  149. p = &pctx->buf[ofs];
  150. p[ofs] = 0;
  151. p = fgets(p, pctx->bufsz - ofs, pctx->file);
  152. if (!p || !*p)
  153. return;
  154. ofs += strlen(p);
  155. if (pctx->buf[ofs - 1] == '\n') {
  156. pctx->line++;
  157. pctx->buf[ofs - 1] = 0;
  158. return;
  159. }
  160. if (pctx->bufsz > LINEBUF_MAX/2)
  161. uci_parse_error(ctx, p, "line too long");
  162. pctx->bufsz *= 2;
  163. pctx->buf = uci_realloc(ctx, pctx->buf, pctx->bufsz);
  164. } while (1);
  165. }
  166. /*
  167. * parse a character escaped by '\'
  168. * returns true if the escaped character is to be parsed
  169. * returns false if the escaped character is to be ignored
  170. */
  171. static inline bool parse_backslash(struct uci_context *ctx, char **str)
  172. {
  173. /* skip backslash */
  174. *str += 1;
  175. /* undecoded backslash at the end of line, fetch the next line */
  176. if (!**str) {
  177. *str += 1;
  178. uci_getln(ctx, *str - ctx->pctx->buf);
  179. return false;
  180. }
  181. /* FIXME: decode escaped char, necessary? */
  182. return true;
  183. }
  184. /*
  185. * move the string pointer forward until a non-whitespace character or
  186. * EOL is reached
  187. */
  188. static void skip_whitespace(struct uci_context *ctx, char **str)
  189. {
  190. restart:
  191. while (**str && isspace(**str))
  192. *str += 1;
  193. if (**str == '\\') {
  194. if (!parse_backslash(ctx, str))
  195. goto restart;
  196. }
  197. }
  198. static inline void addc(char **dest, char **src)
  199. {
  200. **dest = **src;
  201. *dest += 1;
  202. *src += 1;
  203. }
  204. /*
  205. * parse a double quoted string argument from the command line
  206. */
  207. static void parse_double_quote(struct uci_context *ctx, char **str, char **target)
  208. {
  209. char c;
  210. /* skip quote character */
  211. *str += 1;
  212. while ((c = **str)) {
  213. switch(c) {
  214. case '"':
  215. **target = 0;
  216. *str += 1;
  217. return;
  218. case '\\':
  219. if (!parse_backslash(ctx, str))
  220. continue;
  221. /* fall through */
  222. default:
  223. addc(target, str);
  224. break;
  225. }
  226. }
  227. uci_parse_error(ctx, *str, "unterminated \"");
  228. }
  229. /*
  230. * parse a single quoted string argument from the command line
  231. */
  232. static void parse_single_quote(struct uci_context *ctx, char **str, char **target)
  233. {
  234. char c;
  235. /* skip quote character */
  236. *str += 1;
  237. while ((c = **str)) {
  238. switch(c) {
  239. case '\'':
  240. **target = 0;
  241. *str += 1;
  242. return;
  243. default:
  244. addc(target, str);
  245. }
  246. }
  247. uci_parse_error(ctx, *str, "unterminated '");
  248. }
  249. /*
  250. * parse a string from the command line and detect the quoting style
  251. */
  252. static void parse_str(struct uci_context *ctx, char **str, char **target)
  253. {
  254. do {
  255. switch(**str) {
  256. case '\'':
  257. parse_single_quote(ctx, str, target);
  258. break;
  259. case '"':
  260. parse_double_quote(ctx, str, target);
  261. break;
  262. case '#':
  263. **str = 0;
  264. /* fall through */
  265. case 0:
  266. goto done;
  267. case '\\':
  268. if (!parse_backslash(ctx, str))
  269. continue;
  270. /* fall through */
  271. default:
  272. addc(target, str);
  273. break;
  274. }
  275. } while (**str && !isspace(**str));
  276. done:
  277. /*
  278. * if the string was unquoted and we've stopped at a whitespace
  279. * character, skip to the next one, because the whitespace will
  280. * be overwritten by a null byte here
  281. */
  282. if (**str)
  283. *str += 1;
  284. /* terminate the parsed string */
  285. **target = 0;
  286. }
  287. /*
  288. * extract the next argument from the command line
  289. */
  290. static char *next_arg(struct uci_context *ctx, char **str, bool required, bool name)
  291. {
  292. char *val;
  293. char *ptr;
  294. val = ptr = *str;
  295. skip_whitespace(ctx, str);
  296. parse_str(ctx, str, &ptr);
  297. if (!*val) {
  298. if (required)
  299. uci_parse_error(ctx, *str, "insufficient arguments");
  300. goto done;
  301. }
  302. if (name && !uci_validate_name(val))
  303. uci_parse_error(ctx, val, "invalid character in field");
  304. done:
  305. return val;
  306. }
  307. int uci_parse_argument(struct uci_context *ctx, FILE *stream, char **str, char **result)
  308. {
  309. UCI_HANDLE_ERR(ctx);
  310. UCI_ASSERT(ctx, str != NULL);
  311. UCI_ASSERT(ctx, result != NULL);
  312. if (ctx->pctx) {
  313. if (ctx->pctx->file != stream) {
  314. uci_cleanup(ctx);
  315. }
  316. } else {
  317. uci_alloc_parse_context(ctx);
  318. ctx->pctx->file = stream;
  319. }
  320. if (!*str) {
  321. uci_getln(ctx, 0);
  322. *str = ctx->pctx->buf;
  323. }
  324. *result = next_arg(ctx, str, false, false);
  325. return 0;
  326. }
  327. /*
  328. * open a stream and go to the right position
  329. *
  330. * note: when opening for write and seeking to the beginning of
  331. * the stream, truncate the file
  332. */
  333. static FILE *uci_open_stream(struct uci_context *ctx, const char *filename, int pos, bool write, bool create)
  334. {
  335. struct stat statbuf;
  336. FILE *file = NULL;
  337. int fd, ret;
  338. int mode = (write ? O_RDWR : O_RDONLY);
  339. if (create)
  340. mode |= O_CREAT;
  341. if (!write && ((stat(filename, &statbuf) < 0) ||
  342. ((statbuf.st_mode & S_IFMT) != S_IFREG))) {
  343. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  344. }
  345. fd = open(filename, mode, UCI_FILEMODE);
  346. if (fd < 0)
  347. goto error;
  348. if (flock(fd, (write ? LOCK_EX : LOCK_SH)) < 0)
  349. goto error;
  350. ret = lseek(fd, 0, pos);
  351. if (ret < 0)
  352. goto error;
  353. file = fdopen(fd, (write ? "w+" : "r"));
  354. if (file)
  355. goto done;
  356. error:
  357. UCI_THROW(ctx, UCI_ERR_IO);
  358. done:
  359. return file;
  360. }
  361. static void uci_close_stream(FILE *stream)
  362. {
  363. int fd;
  364. if (!stream)
  365. return;
  366. fd = fileno(stream);
  367. flock(fd, LOCK_UN);
  368. fclose(stream);
  369. }