/src/samba/lib/util/tini.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Trivial smb.conf parsing code |
3 | | * |
4 | | * Copyright Volker Lendecke <vl@samba.org> 2014 |
5 | | * |
6 | | * Redistribution and use in source and binary forms, with or without |
7 | | * modification, are permitted provided that the following conditions |
8 | | * are met: |
9 | | * 1. Redistributions of source code must retain the above copyright |
10 | | * notice, and the entire permission notice in its entirety, |
11 | | * including the disclaimer of warranties. |
12 | | * 2. Redistributions in binary form must reproduce the above copyright |
13 | | * notice, this list of conditions and the following disclaimer in the |
14 | | * documentation and/or other materials provided with the distribution. |
15 | | * 3. The name of the author may not be used to endorse or promote |
16 | | * products derived from this software without specific prior |
17 | | * written permission. |
18 | | * |
19 | | * ALTERNATIVELY, this product may be distributed under the terms of |
20 | | * the GNU Public License Version 3 or later, in which case the |
21 | | * provisions of the GPL are required INSTEAD OF the above restrictions. |
22 | | * (This clause is necessary due to a potential bad interaction between the |
23 | | * GPL and the restrictions contained in a BSD-style copyright.) |
24 | | * |
25 | | * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED |
26 | | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
27 | | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
28 | | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
29 | | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
30 | | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
31 | | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
32 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
33 | | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
34 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
35 | | * OF THE POSSIBILITY OF SUCH DAMAGE. |
36 | | */ |
37 | | |
38 | | #include <stdio.h> |
39 | | #include <stdlib.h> |
40 | | #include <stdbool.h> |
41 | | #include <ctype.h> |
42 | | #include <errno.h> |
43 | | #include <string.h> |
44 | | #include "tini.h" |
45 | | |
46 | | static bool c_isspace(char c) |
47 | 20.0M | { |
48 | 20.0M | unsigned char uc = c; |
49 | 20.0M | if (c != uc) { |
50 | 4.95M | return false; |
51 | 4.95M | } |
52 | 15.1M | return isspace(uc); |
53 | 20.0M | } |
54 | | |
55 | | static int next_content(FILE *f) |
56 | 8.16k | { |
57 | 8.16k | int c; |
58 | | |
59 | 8.37k | for (c = fgetc(f); c != EOF; c = fgetc(f)) { |
60 | 7.75k | if (!c_isspace(c)) { |
61 | 7.34k | break; |
62 | 7.34k | } |
63 | 405 | if (c == '\n') { |
64 | 196 | break; |
65 | 196 | } |
66 | 405 | } |
67 | | |
68 | 8.16k | return c; |
69 | 8.16k | } |
70 | | |
71 | | static int next_end_of_line(FILE *f) |
72 | 804 | { |
73 | 804 | int c; |
74 | | |
75 | 1.23k | for (c = fgetc(f); c != EOF; c = fgetc(f)) { |
76 | 1.17k | if (c == '\n') { |
77 | 753 | break; |
78 | 753 | } |
79 | 1.17k | } |
80 | 804 | return c; |
81 | 804 | } |
82 | | |
83 | | static bool make_space(char **buf, size_t *buflen, size_t position) |
84 | 20.0M | { |
85 | 20.0M | char *tmp; |
86 | | |
87 | 20.0M | if (position < *buflen) { |
88 | 20.0M | return true; |
89 | 20.0M | } |
90 | 1.05k | tmp = realloc(*buf, (*buflen) * 2); |
91 | 1.05k | if (tmp == NULL) { |
92 | 0 | return false; |
93 | 0 | } |
94 | 1.05k | *buf = tmp; |
95 | 1.05k | *buflen *= 2; |
96 | 1.05k | return true; |
97 | 1.05k | } |
98 | | |
99 | | /* |
100 | | * Get a conf line into *pbuf (which must be a malloc'ed buffer already). |
101 | | * |
102 | | * Ignores leading spaces |
103 | | * Ignores comment lines |
104 | | * Ignores empty lines |
105 | | * Takes care of continuation lines |
106 | | * Zaps multiple spaces into one |
107 | | */ |
108 | | |
109 | | static int get_line(FILE *f, char **pbuf, size_t *pbuflen) |
110 | 7.21k | { |
111 | 7.21k | int c; |
112 | 7.21k | char *buf; |
113 | 7.21k | size_t buflen, pos; |
114 | | |
115 | 7.21k | buf = *pbuf; |
116 | 7.21k | buflen = *pbuflen; |
117 | 7.21k | pos = 0; |
118 | | |
119 | 8.16k | next_line: |
120 | | |
121 | 8.16k | c = next_content(f); |
122 | 8.16k | if (c == EOF) { |
123 | 625 | return ENOENT; |
124 | 625 | } |
125 | | |
126 | 7.54k | if ((c == '#') || (c == ';')) { |
127 | | /* |
128 | | * Line starting with a comment, skip |
129 | | */ |
130 | 804 | c = next_end_of_line(f); |
131 | 804 | if (c == EOF) { |
132 | 51 | return ENOENT; |
133 | 51 | } |
134 | 753 | goto next_line; |
135 | 804 | } |
136 | | |
137 | 6.73k | if (c == '\n') { |
138 | | /* |
139 | | * Blank line, skip |
140 | | */ |
141 | 196 | goto next_line; |
142 | 196 | } |
143 | | |
144 | 20.0M | for ( ; c != EOF ; c = fgetc(f)) { |
145 | | |
146 | 20.0M | if (c == '\n') { |
147 | | |
148 | 6.76k | if ((pos > 0) && (buf[pos-1] == '\\')) { |
149 | | /* |
150 | | * Line ends in "\". Continuation. |
151 | | */ |
152 | 646 | pos -= 1; |
153 | 646 | continue; |
154 | 646 | } |
155 | | |
156 | 6.11k | if ((pos > 1) && (buf[pos-2] == '\\') && |
157 | 6.11k | c_isspace(buf[pos-1])) { |
158 | | /* |
159 | | * Line ends in "\ ". Mind that we zap |
160 | | * multiple spaces into one. Continuation. |
161 | | */ |
162 | 238 | pos -= 2; |
163 | 238 | continue; |
164 | 238 | } |
165 | | |
166 | | /* |
167 | | * No continuation, done with the line |
168 | | */ |
169 | 5.87k | break; |
170 | 6.11k | } |
171 | | |
172 | 20.0M | if ((pos > 0) && c_isspace(buf[pos-1]) && c_isspace(c)) { |
173 | | /* |
174 | | * Zap multiple spaces to one |
175 | | */ |
176 | 198 | continue; |
177 | 198 | } |
178 | | |
179 | 20.0M | if (!make_space(&buf, &buflen, pos)) { |
180 | 0 | return ENOMEM; |
181 | 0 | } |
182 | 20.0M | buf[pos++] = c; |
183 | 20.0M | } |
184 | | |
185 | 6.54k | if (!make_space(&buf, &buflen, pos)) { |
186 | 0 | return ENOMEM; |
187 | 0 | } |
188 | 6.54k | buf[pos++] = '\0'; |
189 | | |
190 | 6.54k | *pbuf = buf; |
191 | 6.54k | return 0; |
192 | 6.54k | } |
193 | | |
194 | | static bool parse_section( |
195 | | char *buf, bool (*sfunc)(const char *section, void *private_data), |
196 | | void *private_data) |
197 | 2.08k | { |
198 | 2.08k | char *p, *q; |
199 | | |
200 | 2.08k | p = buf+1; /* skip [ */ |
201 | | |
202 | 2.08k | q = strchr(p, ']'); |
203 | 2.08k | if (q == NULL) { |
204 | 32 | return false; |
205 | 32 | } |
206 | 2.04k | *q = '\0'; |
207 | | |
208 | 2.04k | return sfunc(p, private_data); |
209 | 2.08k | } |
210 | | |
211 | | static char *trim_one_space(char *buf) |
212 | 6.04k | { |
213 | 6.04k | size_t len; |
214 | | |
215 | 6.04k | if (c_isspace(buf[0])) { |
216 | 449 | buf += 1; |
217 | 449 | } |
218 | 6.04k | len = strlen(buf); |
219 | 6.04k | if (len == 0) { |
220 | 2.62k | return buf; |
221 | 2.62k | } |
222 | 3.42k | if (c_isspace(buf[len-1])) { |
223 | 395 | buf[len-1] = '\0'; |
224 | 395 | } |
225 | | |
226 | 3.42k | return buf; |
227 | 6.04k | } |
228 | | |
229 | | static bool parse_param(char *buf, |
230 | | bool allow_empty_value, |
231 | | bool (*pfunc)(const char *name, const char *value, |
232 | | void *private_data), |
233 | | void *private_data) |
234 | 3.96k | { |
235 | 3.96k | char *equals; |
236 | 3.96k | char *name; |
237 | 3.96k | const char *value; |
238 | 3.96k | size_t len; |
239 | 3.96k | bool no_value = false; |
240 | | |
241 | 3.96k | equals = strchr(buf, '='); |
242 | 3.96k | if (equals != NULL) { |
243 | 3.03k | *equals = '\0'; |
244 | 3.03k | } else { |
245 | 933 | if (allow_empty_value) { |
246 | 0 | no_value = true; |
247 | 933 | } else { |
248 | 933 | return true; |
249 | 933 | } |
250 | 933 | } |
251 | | |
252 | 3.03k | name = trim_one_space(buf); |
253 | 3.03k | len = strlen(buf); |
254 | 3.03k | if (len == 0) { |
255 | 24 | return false; |
256 | 24 | } |
257 | | |
258 | 3.00k | if (no_value) { |
259 | 0 | value = ""; |
260 | 3.00k | } else { |
261 | 3.00k | value = trim_one_space(equals+1); |
262 | 3.00k | } |
263 | | |
264 | 3.00k | return pfunc(name, value, private_data); |
265 | 3.03k | } |
266 | | |
267 | | bool tini_parse(FILE *f, |
268 | | bool allow_empty_value, |
269 | | bool (*sfunc)(const char *section, void *private_data), |
270 | | bool (*pfunc)(const char *name, const char *value, |
271 | | void *private_data), |
272 | | void *private_data) |
273 | 809 | { |
274 | 809 | char *buf; |
275 | 809 | size_t buflen; |
276 | | |
277 | 809 | buflen = 256; |
278 | | |
279 | 809 | buf = malloc(buflen); |
280 | 809 | if (buf == NULL) { |
281 | 0 | return false; |
282 | 0 | } |
283 | | |
284 | 7.21k | while (true) { |
285 | 7.21k | int ret; |
286 | 7.21k | bool ok; |
287 | | |
288 | 7.21k | ret = get_line(f, &buf, &buflen); |
289 | | |
290 | 7.21k | if (ret == ENOENT) { |
291 | | /* No lines anymore */ |
292 | 676 | break; |
293 | 676 | } |
294 | | |
295 | 6.54k | if (ret != 0) { |
296 | | /* Real error */ |
297 | 0 | free(buf); |
298 | 0 | return false; |
299 | 0 | } |
300 | | |
301 | 6.54k | switch(buf[0]) { |
302 | 494 | case 0: |
303 | 494 | continue; |
304 | 0 | break; |
305 | 2.08k | case '[': |
306 | 2.08k | ok = parse_section(buf, sfunc, private_data); |
307 | 2.08k | break; |
308 | 3.96k | default: |
309 | 3.96k | ok = parse_param(buf, allow_empty_value, pfunc, private_data); |
310 | 3.96k | break; |
311 | 6.54k | } |
312 | | |
313 | 6.04k | if (!ok) { |
314 | 133 | free(buf); |
315 | 133 | return false; |
316 | 133 | } |
317 | 6.04k | } |
318 | 676 | free(buf); |
319 | 676 | return true; |
320 | 809 | } |