util.c 5.1 KB

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