util.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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)
  62. {
  63. if (!*str)
  64. return false;
  65. while (*str) {
  66. unsigned char c = *str;
  67. if (!isalnum(c) && c != '_') {
  68. if (name || (c < 33) || (c > 126))
  69. return false;
  70. }
  71. str++;
  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) &&
  80. (c != '\t') &&
  81. (c != '\n') &&
  82. (c != '\r'))) {
  83. return false;
  84. }
  85. str++;
  86. }
  87. return true;
  88. }
  89. __private void uci_alloc_parse_context(struct uci_context *ctx)
  90. {
  91. ctx->pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
  92. }
  93. int uci_parse_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str)
  94. {
  95. char *last = NULL;
  96. char *tmp;
  97. UCI_HANDLE_ERR(ctx);
  98. UCI_ASSERT(ctx, str);
  99. UCI_ASSERT(ctx, ptr);
  100. memset(ptr, 0, sizeof(struct uci_ptr));
  101. /* value */
  102. last = strchr(str, '=');
  103. if (last) {
  104. *last = 0;
  105. last++;
  106. ptr->value = last;
  107. }
  108. ptr->package = strsep(&str, ".");
  109. if (!ptr->package)
  110. goto error;
  111. ptr->section = strsep(&str, ".");
  112. if (!ptr->section) {
  113. ptr->target = UCI_TYPE_PACKAGE;
  114. goto lastval;
  115. }
  116. ptr->option = strsep(&str, ".");
  117. if (!ptr->option) {
  118. ptr->target = UCI_TYPE_SECTION;
  119. goto lastval;
  120. } else {
  121. ptr->target = UCI_TYPE_OPTION;
  122. }
  123. tmp = strsep(&str, ".");
  124. if (tmp)
  125. goto error;
  126. lastval:
  127. if (ptr->package && !uci_validate_package(ptr->package))
  128. goto error;
  129. if (ptr->section && !uci_validate_name(ptr->section))
  130. ptr->flags |= UCI_LOOKUP_EXTENDED;
  131. if (ptr->option && !uci_validate_name(ptr->option))
  132. goto error;
  133. if (ptr->value && !uci_validate_text(ptr->value))
  134. goto error;
  135. return 0;
  136. error:
  137. memset(ptr, 0, sizeof(struct uci_ptr));
  138. UCI_THROW(ctx, UCI_ERR_PARSE);
  139. }
  140. __private void uci_parse_error(struct uci_context *ctx, char *reason)
  141. {
  142. struct uci_parse_context *pctx = ctx->pctx;
  143. pctx->reason = reason;
  144. pctx->byte = pctx_pos(pctx);
  145. UCI_THROW(ctx, UCI_ERR_PARSE);
  146. }
  147. /*
  148. * open a stream and go to the right position
  149. *
  150. * note: when opening for write and seeking to the beginning of
  151. * the stream, truncate the file
  152. */
  153. __private FILE *uci_open_stream(struct uci_context *ctx, const char *filename, const char *origfilename, int pos, bool write, bool create)
  154. {
  155. struct stat statbuf;
  156. FILE *file = NULL;
  157. int fd, ret;
  158. int flags = (write ? O_RDWR : O_RDONLY);
  159. mode_t mode = UCI_FILEMODE;
  160. char *name = NULL;
  161. char *filename2 = NULL;
  162. if (create) {
  163. flags |= O_CREAT;
  164. if (origfilename) {
  165. name = basename((char *) origfilename);
  166. } else {
  167. name = basename((char *) filename);
  168. }
  169. if ((asprintf(&filename2, "%s/%s", ctx->confdir, name) < 0) || !filename2) {
  170. UCI_THROW(ctx, UCI_ERR_MEM);
  171. } else {
  172. if (stat(filename2, &statbuf) == 0)
  173. mode = statbuf.st_mode;
  174. free(filename2);
  175. }
  176. }
  177. if (!write && ((stat(filename, &statbuf) < 0) ||
  178. ((statbuf.st_mode & S_IFMT) != S_IFREG))) {
  179. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  180. }
  181. fd = open(filename, flags, mode);
  182. if (fd < 0)
  183. goto error;
  184. ret = flock(fd, (write ? LOCK_EX : LOCK_SH));
  185. if ((ret < 0) && (errno != ENOSYS))
  186. goto error;
  187. ret = lseek(fd, 0, pos);
  188. if (ret < 0)
  189. goto error;
  190. file = fdopen(fd, (write ? "w+" : "r"));
  191. if (file)
  192. goto done;
  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. }