backupkey: Handle more clearly the case where we find the secret, but it has no value
[Samba.git] / lib / util / tini.c
blob6cd301a0b1b3cb2a2de69e53d673d517b2a67d60
1 /*
2 * Trivial smb.conf parsing code
4 * Copyright Volker Lendecke <vl@samba.org> 2014
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.
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.)
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.
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"
46 static int next_content(FILE *f)
48 int c;
50 for (c = fgetc(f); c != EOF; c = fgetc(f)) {
51 if (!isspace(c)) {
52 break;
54 if (c == '\n') {
55 break;
59 return c;
62 static int next_end_of_line(FILE *f)
64 int c;
66 for (c = fgetc(f); c != EOF; c = fgetc(f)) {
67 if (c == '\n') {
68 break;
71 return c;
74 static bool make_space(char **buf, size_t *buflen, size_t position)
76 char *tmp;
78 if (position < *buflen) {
79 return true;
81 tmp = realloc(*buf, (*buflen) * 2);
82 if (tmp == NULL) {
83 return false;
85 *buf = tmp;
86 *buflen *= 2;
87 return true;
91 * Get a conf line into *pbuf (which must be a malloc'ed buffer already).
93 * Ignores leading spaces
94 * Ignores comment lines
95 * Ignores empty lines
96 * Takes care of continuation lines
97 * Zaps multiple spaces into one
100 static int get_line(FILE *f, char **pbuf, size_t *pbuflen)
102 int c;
103 char *buf;
104 size_t buflen, pos;
106 buf = *pbuf;
107 buflen = *pbuflen;
108 pos = 0;
110 next_line:
112 c = next_content(f);
113 if (c == EOF) {
114 return ENOENT;
117 if ((c == '#') || (c == ';')) {
119 * Line starting with a comment, skip
121 c = next_end_of_line(f);
122 if (c == EOF) {
123 return ENOENT;
125 goto next_line;
128 if (c == '\n') {
130 * Blank line, skip
132 goto next_line;
135 for ( ; c != EOF ; c = fgetc(f)) {
137 if (c == '\n') {
139 if ((pos > 0) && (buf[pos-1] == '\\')) {
141 * Line ends in "\". Continuation.
143 pos -= 1;
144 continue;
147 if ((pos > 1) && (buf[pos-2] == '\\') &&
148 isspace(buf[pos-1])) {
150 * Line ends in "\ ". Mind that we zap
151 * multiple spaces into one. Continuation.
153 pos -= 2;
154 continue;
158 * No continuation, done with the line
160 break;
163 if ((pos > 0) && isspace(buf[pos-1]) && isspace(c)) {
165 * Zap multiple spaces to one
167 continue;
170 if (!make_space(&buf, &buflen, pos)) {
171 return ENOMEM;
173 buf[pos++] = c;
176 if (!make_space(&buf, &buflen, pos)) {
177 return ENOMEM;
179 buf[pos++] = '\0';
181 *pbuf = buf;
182 return 0;
185 static bool parse_section(
186 char *buf, bool (*sfunc)(const char *section, void *private_data),
187 void *private_data)
189 char *p, *q;
191 p = buf+1; /* skip [ */
193 q = strchr(p, ']');
194 if (q == NULL) {
195 return false;
197 *q = '\0';
199 return sfunc(p, private_data);
202 static char *trim_one_space(char *buf)
204 size_t len;
206 if (isspace(buf[0])) {
207 buf += 1;
209 len = strlen(buf);
210 if (len == 0) {
211 return buf;
213 if (isspace(buf[len-1])) {
214 buf[len-1] = '\0';
217 return buf;
220 static bool parse_param(char *buf,
221 bool (*pfunc)(const char *name, const char *value,
222 void *private_data),
223 void *private_data)
225 char *equals;
226 char *name, *value;
227 size_t len;
229 equals = strchr(buf, '=');
230 if (equals == NULL) {
231 return true;
233 *equals = '\0';
235 name = trim_one_space(buf);
236 len = strlen(buf);
237 if (len == 0) {
238 return false;
241 value = trim_one_space(equals+1);
243 return pfunc(name, value, private_data);
246 bool tini_parse(FILE *f,
247 bool (*sfunc)(const char *section, void *private_data),
248 bool (*pfunc)(const char *name, const char *value,
249 void *private_data),
250 void *private_data)
252 char *buf;
253 size_t buflen;
255 buflen = 256;
257 buf = malloc(buflen);
258 if (buf == NULL) {
259 return false;
262 while (true) {
263 int ret;
264 bool ok;
266 ret = get_line(f, &buf, &buflen);
268 if (ret == ENOENT) {
269 /* No lines anymore */
270 break;
273 if (ret != 0) {
274 /* Real error */
275 free(buf);
276 return false;
279 switch(buf[0]) {
280 case 0:
281 continue;
282 break;
283 case '[':
284 ok = parse_section(buf, sfunc, private_data);
285 break;
286 default:
287 ok = parse_param(buf, pfunc, private_data);
288 break;
291 if (!ok) {
292 free(buf);
293 return false;
296 free(buf);
297 return true;