util.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 == '\r') || (c == '\n') ||
  80. ((c < 32) && (c != '\t')))
  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 *pos, char *reason)
  138. {
  139. struct uci_parse_context *pctx = ctx->pctx;
  140. pctx->reason = reason;
  141. pctx->byte = pos - pctx->buf;
  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, 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. name = basename((char *) filename);
  162. if ((asprintf(&filename2, "%s/%s", ctx->confdir, name) < 0) || !filename2) {
  163. UCI_THROW(ctx, UCI_ERR_MEM);
  164. } else {
  165. if (stat(filename2,&statbuf) == 0)
  166. mode = statbuf.st_mode;
  167. free(filename2);
  168. }
  169. }
  170. if (!write && ((stat(filename, &statbuf) < 0) ||
  171. ((statbuf.st_mode & S_IFMT) != S_IFREG))) {
  172. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  173. }
  174. fd = open(filename, flags, mode);
  175. if (fd < 0)
  176. goto error;
  177. ret = flock(fd, (write ? LOCK_EX : LOCK_SH));
  178. if ((ret < 0) && (errno != ENOSYS))
  179. goto error;
  180. ret = lseek(fd, 0, pos);
  181. if (ret < 0)
  182. goto error;
  183. file = fdopen(fd, (write ? "w+" : "r"));
  184. if (file)
  185. goto done;
  186. error:
  187. UCI_THROW(ctx, UCI_ERR_IO);
  188. done:
  189. return file;
  190. }
  191. __private void uci_close_stream(FILE *stream)
  192. {
  193. int fd;
  194. if (!stream)
  195. return;
  196. fflush(stream);
  197. fd = fileno(stream);
  198. flock(fd, LOCK_UN);
  199. fclose(stream);
  200. }