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