util.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 misc utility functions and wrappers to standard
  16. * functions, which throw exceptions upon failure.
  17. */
  18. #define _GNU_SOURCE
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <sys/file.h>
  22. #include <stdbool.h>
  23. #include <unistd.h>
  24. #include <ctype.h>
  25. #include <fcntl.h>
  26. #include <errno.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <libgen.h>
  30. #include "uci.h"
  31. #include "uci_internal.h"
  32. __private void *uci_malloc(struct uci_context *ctx, size_t size)
  33. {
  34. void *ptr;
  35. ptr = malloc(size);
  36. if (!ptr)
  37. UCI_THROW(ctx, UCI_ERR_MEM);
  38. memset(ptr, 0, size);
  39. return ptr;
  40. }
  41. __private void *uci_realloc(struct uci_context *ctx, void *ptr, size_t size)
  42. {
  43. ptr = realloc(ptr, size);
  44. if (!ptr)
  45. UCI_THROW(ctx, UCI_ERR_MEM);
  46. return ptr;
  47. }
  48. __private char *uci_strdup(struct uci_context *ctx, const char *str)
  49. {
  50. char *ptr;
  51. ptr = strdup(str);
  52. if (!ptr)
  53. UCI_THROW(ctx, UCI_ERR_MEM);
  54. return ptr;
  55. }
  56. /*
  57. * validate strings for names and types, reject special characters
  58. * for names, only alphanum and _ is allowed (shell compatibility)
  59. * for types, we allow more characters
  60. */
  61. __private bool uci_validate_str(const char *str, bool name, bool package)
  62. {
  63. if (!*str)
  64. return false;
  65. for (; *str; str++) {
  66. unsigned char c = *str;
  67. if (isalnum(c) || c == '_')
  68. continue;
  69. if (c == '-' && package)
  70. continue;
  71. if (name || (c < 33) || (c > 126))
  72. return false;
  73. }
  74. return true;
  75. }
  76. bool uci_validate_text(const char *str)
  77. {
  78. while (*str) {
  79. unsigned char c = *str;
  80. if (c < 32 && c != '\t' && c != '\n' && c != '\r')
  81. return false;
  82. str++;
  83. }
  84. return true;
  85. }
  86. __private void uci_alloc_parse_context(struct uci_context *ctx)
  87. {
  88. ctx->pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
  89. }
  90. int uci_parse_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str)
  91. {
  92. char *last = NULL;
  93. char *tmp;
  94. UCI_HANDLE_ERR(ctx);
  95. UCI_ASSERT(ctx, str);
  96. UCI_ASSERT(ctx, ptr);
  97. memset(ptr, 0, sizeof(struct uci_ptr));
  98. /* value */
  99. last = strchr(str, '=');
  100. if (last) {
  101. *last = 0;
  102. last++;
  103. ptr->value = last;
  104. }
  105. ptr->package = strsep(&str, ".");
  106. if (!ptr->package)
  107. goto error;
  108. ptr->section = strsep(&str, ".");
  109. if (!ptr->section) {
  110. ptr->target = UCI_TYPE_PACKAGE;
  111. goto lastval;
  112. }
  113. ptr->option = strsep(&str, ".");
  114. if (!ptr->option) {
  115. ptr->target = UCI_TYPE_SECTION;
  116. goto lastval;
  117. } else {
  118. ptr->target = UCI_TYPE_OPTION;
  119. }
  120. tmp = strsep(&str, ".");
  121. if (tmp)
  122. goto error;
  123. lastval:
  124. if (ptr->package && !uci_validate_package(ptr->package))
  125. goto error;
  126. if (ptr->section && !uci_validate_name(ptr->section))
  127. ptr->flags |= UCI_LOOKUP_EXTENDED;
  128. if (ptr->option && !uci_validate_name(ptr->option))
  129. goto error;
  130. if (ptr->value && !uci_validate_text(ptr->value))
  131. goto error;
  132. return 0;
  133. error:
  134. memset(ptr, 0, sizeof(struct uci_ptr));
  135. UCI_THROW(ctx, UCI_ERR_PARSE);
  136. }
  137. __private void uci_parse_error(struct uci_context *ctx, char *reason)
  138. {
  139. struct uci_parse_context *pctx = ctx->pctx;
  140. pctx->reason = reason;
  141. pctx->byte = pctx_pos(pctx);
  142. UCI_THROW(ctx, UCI_ERR_PARSE);
  143. }
  144. /*
  145. * open a stream and go to the right position
  146. *
  147. * note: when opening for write and seeking to the beginning of
  148. * the stream, truncate the file
  149. */
  150. __private FILE *uci_open_stream(struct uci_context *ctx, const char *filename, const char *origfilename, int pos, bool write, bool create)
  151. {
  152. struct stat statbuf;
  153. FILE *file = NULL;
  154. int fd, ret;
  155. int flags = (write ? O_RDWR : O_RDONLY);
  156. mode_t mode = UCI_FILEMODE;
  157. char *name = NULL;
  158. char *filename2 = NULL;
  159. if (create) {
  160. flags |= O_CREAT;
  161. if (origfilename) {
  162. name = basename((char *) origfilename);
  163. } else {
  164. name = basename((char *) filename);
  165. }
  166. if ((asprintf(&filename2, "%s/%s", ctx->confdir, name) < 0) || !filename2) {
  167. UCI_THROW(ctx, UCI_ERR_MEM);
  168. } else {
  169. if (stat(filename2, &statbuf) == 0)
  170. mode = statbuf.st_mode;
  171. free(filename2);
  172. }
  173. }
  174. if (!write && ((stat(filename, &statbuf) < 0) ||
  175. ((statbuf.st_mode & S_IFMT) != S_IFREG))) {
  176. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  177. }
  178. fd = open(filename, flags, mode);
  179. if (fd < 0)
  180. goto error;
  181. ret = flock(fd, (write ? LOCK_EX : LOCK_SH));
  182. if ((ret < 0) && (errno != ENOSYS))
  183. goto error;
  184. ret = lseek(fd, 0, pos);
  185. if (ret < 0)
  186. goto error;
  187. file = fdopen(fd, (write ? "w+" : "r"));
  188. if (file)
  189. goto done;
  190. error:
  191. UCI_THROW(ctx, UCI_ERR_IO);
  192. done:
  193. return file;
  194. }
  195. __private void uci_close_stream(FILE *stream)
  196. {
  197. int fd;
  198. if (!stream)
  199. return;
  200. fflush(stream);
  201. fd = fileno(stream);
  202. flock(fd, LOCK_UN);
  203. fclose(stream);
  204. }