s3:libsmb: let cli_tree_connect_creds() only call cli_credentials_get_password()...
[samba.git] / lib / util / tini.c
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 int next_content(FILE *f)
47 {
48         int c;
49
50         for (c = fgetc(f); c != EOF; c = fgetc(f)) {
51                 if (!isspace(c)) {
52                         break;
53                 }
54                 if (c == '\n') {
55                         break;
56                 }
57         }
58
59         return c;
60 }
61
62 static int next_end_of_line(FILE *f)
63 {
64         int c;
65
66         for (c = fgetc(f); c != EOF; c = fgetc(f)) {
67                 if (c == '\n') {
68                         break;
69                 }
70         }
71         return c;
72 }
73
74 static bool make_space(char **buf, size_t *buflen, size_t position)
75 {
76         char *tmp;
77
78         if (position < *buflen) {
79                 return true;
80         }
81         tmp = realloc(*buf, (*buflen) * 2);
82         if (tmp == NULL) {
83                 return false;
84         }
85         *buf = tmp;
86         *buflen *= 2;
87         return true;
88 }
89
90 /*
91  * Get a conf line into *pbuf (which must be a malloc'ed buffer already).
92  *
93  * Ignores leading spaces
94  * Ignores comment lines
95  * Ignores empty lines
96  * Takes care of continuation lines
97  * Zaps multiple spaces into one
98  */
99
100 static int get_line(FILE *f, char **pbuf, size_t *pbuflen)
101 {
102         int c;
103         char *buf;
104         size_t buflen, pos;
105
106         buf = *pbuf;
107         buflen = *pbuflen;
108         pos = 0;
109
110 next_line:
111
112         c = next_content(f);
113         if (c == EOF) {
114                 return ENOENT;
115         }
116
117         if ((c == '#') || (c == ';')) {
118                 /*
119                  * Line starting with a comment, skip
120                  */
121                 c = next_end_of_line(f);
122                 if (c == EOF) {
123                         return ENOENT;
124                 }
125                 goto next_line;
126         }
127
128         if (c == '\n') {
129                 /*
130                  * Blank line, skip
131                  */
132                 goto next_line;
133         }
134
135         for ( ; c != EOF ; c = fgetc(f)) {
136
137                 if (c == '\n') {
138
139                         if ((pos > 0) && (buf[pos-1] == '\\')) {
140                                 /*
141                                  * Line ends in "\". Continuation.
142                                  */
143                                 pos -= 1;
144                                 continue;
145                         }
146
147                         if ((pos > 1) && (buf[pos-2] == '\\') &&
148                             isspace(buf[pos-1])) {
149                                 /*
150                                  * Line ends in "\ ". Mind that we zap
151                                  * multiple spaces into one. Continuation.
152                                  */
153                                 pos -= 2;
154                                 continue;
155                         }
156
157                         /*
158                          * No continuation, done with the line
159                          */
160                         break;
161                 }
162
163                 if ((pos > 0) && isspace(buf[pos-1]) && isspace(c)) {
164                         /*
165                          * Zap multiple spaces to one
166                          */
167                         continue;
168                 }
169
170                 if (!make_space(&buf, &buflen, pos)) {
171                         return ENOMEM;
172                 }
173                 buf[pos++] = c;
174         }
175
176         if (!make_space(&buf, &buflen, pos)) {
177                 return ENOMEM;
178         }
179         buf[pos++] = '\0';
180
181         *pbuf = buf;
182         return 0;
183 }
184
185 static bool parse_section(
186         char *buf, bool (*sfunc)(const char *section, void *private_data),
187         void *private_data)
188 {
189         char *p, *q;
190
191         p = buf+1;              /* skip [ */
192
193         q = strchr(p, ']');
194         if (q == NULL) {
195                 return false;
196         }
197         *q = '\0';
198
199         return sfunc(p, private_data);
200 }
201
202 static char *trim_one_space(char *buf)
203 {
204         size_t len;
205
206         if (isspace(buf[0])) {
207                 buf += 1;
208         }
209         len = strlen(buf);
210         if (len == 0) {
211                 return buf;
212         }
213         if (isspace(buf[len-1])) {
214                 buf[len-1] = '\0';
215         }
216
217         return buf;
218 }
219
220 static bool parse_param(char *buf,
221                         bool (*pfunc)(const char *name, const char *value,
222                                       void *private_data),
223                         void *private_data)
224 {
225         char *equals;
226         char *name, *value;
227         size_t len;
228
229         equals = strchr(buf, '=');
230         if (equals == NULL) {
231                 return true;
232         }
233         *equals = '\0';
234
235         name = trim_one_space(buf);
236         len = strlen(buf);
237         if (len == 0) {
238                 return false;
239         }
240
241         value = trim_one_space(equals+1);
242
243         return pfunc(name, value, private_data);
244 }
245
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)
251 {
252         char *buf;
253         size_t buflen;
254
255         buflen = 256;
256
257         buf = malloc(buflen);
258         if (buf == NULL) {
259                 return false;
260         }
261
262         while (true) {
263                 int ret;
264                 bool ok;
265
266                 ret = get_line(f, &buf, &buflen);
267
268                 if (ret == ENOENT) {
269                         /* No lines anymore */
270                         break;
271                 }
272
273                 if (ret != 0) {
274                         /* Real error */
275                         free(buf);
276                         return false;
277                 }
278
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;
289                 }
290
291                 if (!ok) {
292                         free(buf);
293                         return false;
294                 }
295         }
296         free(buf);
297         return true;
298 }