blob: f50cb5395c1815375dfe6af6020595a35cafbeda [file] [log] [blame]
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001/*
2 * profile.c -- A simple configuration file parsing "library in a file"
3 *
4 * The profile library was originally written by Theodore Ts'o in 1995
5 * for use in the MIT Kerberos v5 library. It has been
6 * modified/enhanced/bug-fixed over time by other members of the MIT
7 * Kerberos team. This version was originally taken from the Kerberos
8 * v5 distribution, version 1.4.2, and radically simplified for use in
9 * e2fsprogs. (Support for locking for multi-threaded operations,
10 * being able to modify and update the configuration file
11 * programmatically, and Mac/Windows portability have been removed.
12 * It has been folded into a single C source file to make it easier to
13 * fold into an application program.)
14 *
Theodore Ts'od45544c2006-01-05 01:23:48 -050015 * Copyright (C) 2005, 2006 by Theodore Ts'o.
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -050016 *
17 * %Begin-Header%
18 * This file may be redistributed under the terms of the GNU Public
19 * License.
20 * %End-Header%
21 *
22 * Copyright (C) 1985-2005 by the Massachusetts Institute of Technology.
23 *
24 * All rights reserved.
25 *
26 * Export of this software from the United States of America may require
27 * a specific license from the United States Government. It is the
28 * responsibility of any person or organization contemplating export to
29 * obtain such a license before exporting.
30 *
31 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
32 * distribute this software and its documentation for any purpose and
33 * without fee is hereby granted, provided that the above copyright
34 * notice appear in all copies and that both that copyright notice and
35 * this permission notice appear in supporting documentation, and that
36 * the name of M.I.T. not be used in advertising or publicity pertaining
37 * to distribution of the software without specific, written prior
38 * permission. Furthermore if you modify this software you must label
39 * your software as modified software and not distribute it in such a
40 * fashion that it might be confused with the original MIT software.
41 * M.I.T. makes no representations about the suitability of this software
42 * for any purpose. It is provided "as is" without express or implied
43 * warranty.
44 *
45 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
46 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
47 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
48 *
49 */
50
51#ifdef HAVE_UNISTD_H
52#include <unistd.h>
53#endif
54#include <stdio.h>
55#ifdef HAVE_STDLIB_H
56#include <stdlib.h>
57#endif
58#include <time.h>
59#include <string.h>
60#include <errno.h>
61#include <ctype.h>
62#include <limits.h>
63#include <stddef.h>
64#include <sys/types.h>
65#include <sys/stat.h>
Theodore Ts'o7d922f82006-01-05 03:56:19 -050066#include <dirent.h>
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -050067#ifdef HAVE_PWD_H
68#include <pwd.h>
69#endif
70
Theodore Ts'ocec71032006-01-01 21:31:45 -050071#include <et/com_err.h>
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -050072#include "profile.h"
Theodore Ts'ofd7ac1f2005-12-31 01:28:33 -050073#include "prof_err.h"
74
Theodore Ts'o55822752005-12-31 16:24:07 -050075#undef STAT_ONCE_PER_SECOND
76#undef HAVE_STAT
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -050077
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -050078/*
79 * prof_int.h
80 */
81
82typedef long prf_magic_t;
83
84/*
85 * This is the structure which stores the profile information for a
86 * particular configuration file.
87 */
Theodore Ts'od45544c2006-01-05 01:23:48 -050088struct _prf_file_t {
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -050089 prf_magic_t magic;
Theodore Ts'od45544c2006-01-05 01:23:48 -050090 char *filespec;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -050091#ifdef STAT_ONCE_PER_SECOND
92 time_t last_stat;
93#endif
94 time_t timestamp; /* time tree was last updated from file */
95 int flags; /* r/w, dirty */
96 int upd_serial; /* incremented when data changes */
Theodore Ts'od45544c2006-01-05 01:23:48 -050097 struct profile_node *root;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -050098 struct _prf_file_t *next;
99};
100
101typedef struct _prf_file_t *prf_file_t;
102
103/*
104 * The profile flags
105 */
106#define PROFILE_FILE_RW 0x0001
107#define PROFILE_FILE_DIRTY 0x0002
108
109/*
110 * This structure defines the high-level, user visible profile_t
111 * object, which is used as a handle by users who need to query some
112 * configuration file(s)
113 */
114struct _profile_t {
115 prf_magic_t magic;
116 prf_file_t first_file;
117};
118
119/*
120 * Used by the profile iterator in prof_get.c
121 */
122#define PROFILE_ITER_LIST_SECTION 0x0001
123#define PROFILE_ITER_SECTIONS_ONLY 0x0002
124#define PROFILE_ITER_RELATIONS_ONLY 0x0004
125
126#define PROFILE_ITER_FINAL_SEEN 0x0100
127
128/*
129 * Check if a filespec is last in a list (NULL on UNIX, invalid FSSpec on MacOS
130 */
131
132#define PROFILE_LAST_FILESPEC(x) (((x) == NULL) || ((x)[0] == '\0'))
133
Theodore Ts'o9a4c2092006-01-02 22:04:41 -0500134struct profile_node {
135 errcode_t magic;
136 char *name;
137 char *value;
138 int group_level;
139 int final:1; /* Indicate don't search next file */
140 int deleted:1;
141 struct profile_node *first_child;
142 struct profile_node *parent;
143 struct profile_node *next, *prev;
144};
145
146#define CHECK_MAGIC(node) \
147 if ((node)->magic != PROF_MAGIC_NODE) \
148 return PROF_MAGIC_NODE;
149
Theodore Ts'o204ae372006-03-22 10:01:24 -0500150/* profile parser declarations */
151struct parse_state {
152 int state;
153 int group_level;
154 int line_num;
155 struct profile_node *root_section;
156 struct profile_node *current_section;
157};
158
159static profile_syntax_err_cb_t syntax_err_cb;
160
161static errcode_t parse_line(char *line, struct parse_state *state);
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500162
163#ifdef DEBUG_PROGRAM
164static errcode_t profile_write_tree_file
165 (struct profile_node *root, FILE *dstfile);
166
167static errcode_t profile_write_tree_to_buffer
168 (struct profile_node *root, char **buf);
169#endif
170
171
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500172static void profile_free_node
173 (struct profile_node *relation);
174
175static errcode_t profile_create_node
176 (const char *name, const char *value,
177 struct profile_node **ret_node);
178
179#ifdef DEBUG_PROGRAM
180static errcode_t profile_verify_node
181 (struct profile_node *node);
182#endif
183
184static errcode_t profile_add_node
185 (struct profile_node *section,
186 const char *name, const char *value,
187 struct profile_node **ret_node);
188
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500189static errcode_t profile_find_node
190 (struct profile_node *section,
191 const char *name, const char *value,
192 int section_flag, void **state,
193 struct profile_node **node);
194
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500195static errcode_t profile_node_iterator
196 (void **iter_p, struct profile_node **ret_node,
197 char **ret_name, char **ret_value);
198
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500199static errcode_t profile_open_file
200 (const char * file, prf_file_t *ret_prof);
201
Theodore Ts'od45544c2006-01-05 01:23:48 -0500202static errcode_t profile_update_file
203 (prf_file_t prf);
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500204
205static void profile_free_file
206 (prf_file_t profile);
207
Theodore Ts'o9a4c2092006-01-02 22:04:41 -0500208static errcode_t profile_get_value(profile_t profile, const char *name,
209 const char *subname, const char *subsubname,
210 const char **ret_value);
211
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500212
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500213/*
214 * prof_init.c --- routines that manipulate the user-visible profile_t
215 * object.
216 */
217
Theodore Ts'o7d922f82006-01-05 03:56:19 -0500218static int compstr(const void *m1, const void *m2)
219{
220 const char *s1 = *((const char **) m1);
221 const char *s2 = *((const char **) m2);
222
223 return strcmp(s1, s2);
224}
225
226static void free_list(char **list)
227{
228 char **cp;
229
230 if (list == 0)
231 return;
232
233 for (cp = list; *cp; cp++)
234 free(*cp);
235 free(list);
236}
237
238static errcode_t get_dirlist(const char *dirname, char***ret_array)
239{
240 DIR *dir;
241 struct dirent *de;
242 struct stat st;
243 errcode_t retval;
244 char *fn, *cp;
245 char **array = 0, **new_array;
246 int max = 0, num = 0;
247
248 dir = opendir(dirname);
249 if (!dir)
250 return errno;
251
252 while ((de = readdir(dir)) != NULL) {
253 for (cp = de->d_name; *cp; cp++) {
254 if (!isalnum(*cp) &&
255 (*cp != '-') &&
256 (*cp != '_'))
257 break;
258 }
259 if (*cp)
260 continue;
261 fn = malloc(strlen(dirname) + strlen(de->d_name) + 2);
262 if (!fn) {
263 retval = ENOMEM;
264 goto errout;
265 }
266 sprintf(fn, "%s/%s", dirname, de->d_name);
267 if ((stat(fn, &st) < 0) || !S_ISREG(st.st_mode)) {
268 free(fn);
269 continue;
270 }
271 if (num >= max) {
272 max += 10;
273 new_array = realloc(array, sizeof(char *) * (max+1));
274 if (!new_array) {
275 retval = ENOMEM;
276 goto errout;
277 }
278 array = new_array;
279 }
280 array[num++] = fn;
281 }
282 qsort(array, num, sizeof(char *), compstr);
283 array[num++] = 0;
284 *ret_array = array;
285 closedir(dir);
286 return 0;
287errout:
288 closedir(dir);
289 free_list(array);
290 return retval;
291}
292
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500293errcode_t
294profile_init(const char **files, profile_t *ret_profile)
295{
296 const char **fs;
297 profile_t profile;
Theodore Ts'o7d922f82006-01-05 03:56:19 -0500298 prf_file_t new_file, *last;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500299 errcode_t retval = 0;
Theodore Ts'o7d922f82006-01-05 03:56:19 -0500300 char **cpp, *cp, **array = 0;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500301
302 profile = malloc(sizeof(struct _profile_t));
303 if (!profile)
304 return ENOMEM;
305 memset(profile, 0, sizeof(struct _profile_t));
306 profile->magic = PROF_MAGIC_PROFILE;
Theodore Ts'o7d922f82006-01-05 03:56:19 -0500307 last = &profile->first_file;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500308
309 /* if the filenames list is not specified return an empty profile */
310 if ( files ) {
311 for (fs = files; !PROFILE_LAST_FILESPEC(*fs); fs++) {
Theodore Ts'o7d922f82006-01-05 03:56:19 -0500312 retval = get_dirlist(*fs, &array);
313 if (retval == 0) {
314 for (cpp = array; (cp = *cpp); cpp++) {
315 retval = profile_open_file(cp, &new_file);
316 if (retval == EACCES)
317 continue;
318 if (retval)
319 goto errout;
320 *last = new_file;
321 last = &new_file->next;
322 }
323 } else if (retval != ENOTDIR)
324 goto errout;
325
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500326 retval = profile_open_file(*fs, &new_file);
327 /* if this file is missing, skip to the next */
328 if (retval == ENOENT || retval == EACCES) {
329 continue;
330 }
Theodore Ts'o7d922f82006-01-05 03:56:19 -0500331 if (retval)
332 goto errout;
333 *last = new_file;
334 last = &new_file->next;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500335 }
336 /*
Theodore Ts'o7d922f82006-01-05 03:56:19 -0500337 * If all the files were not found, return the appropriate error.
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500338 */
Theodore Ts'o7d922f82006-01-05 03:56:19 -0500339 if (!profile->first_file) {
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500340 profile_release(profile);
341 return ENOENT;
342 }
343 }
344
Theodore Ts'o7d922f82006-01-05 03:56:19 -0500345 free_list(array);
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500346 *ret_profile = profile;
347 return 0;
Theodore Ts'o7d922f82006-01-05 03:56:19 -0500348errout:
349 free_list(array);
350 profile_release(profile);
351 return retval;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500352}
353
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500354void
355profile_release(profile_t profile)
356{
357 prf_file_t p, next;
358
359 if (!profile || profile->magic != PROF_MAGIC_PROFILE)
360 return;
361
362 for (p = profile->first_file; p; p = next) {
363 next = p->next;
364 profile_free_file(p);
365 }
366 profile->magic = 0;
367 free(profile);
368}
369
370
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500371/*
372 * prof_file.c ---- routines that manipulate an individual profile file.
373 */
374
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500375errcode_t profile_open_file(const char * filespec,
376 prf_file_t *ret_prof)
377{
378 prf_file_t prf;
379 errcode_t retval;
380 char *home_env = 0;
381 unsigned int len;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500382 char *expanded_filename;
383
384 prf = malloc(sizeof(struct _prf_file_t));
385 if (!prf)
386 return ENOMEM;
387 memset(prf, 0, sizeof(struct _prf_file_t));
388 prf->magic = PROF_MAGIC_FILE;
389
390 len = strlen(filespec)+1;
391 if (filespec[0] == '~' && filespec[1] == '/') {
392 home_env = getenv("HOME");
393#ifdef HAVE_PWD_H
394 if (home_env == NULL) {
Theodore Ts'o95a8d1d2006-01-05 00:47:34 -0500395#ifdef HAVE_GETWUID_R
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500396 struct passwd *pw, pwx;
Theodore Ts'o95a8d1d2006-01-05 00:47:34 -0500397 uid_t uid;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500398 char pwbuf[BUFSIZ];
399
400 uid = getuid();
Theodore Ts'o95a8d1d2006-01-05 00:47:34 -0500401 if (!getpwuid_r(uid, &pwx, pwbuf, sizeof(pwbuf), &pw)
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500402 && pw != NULL && pw->pw_dir[0] != 0)
403 home_env = pw->pw_dir;
Theodore Ts'o95a8d1d2006-01-05 00:47:34 -0500404#else
405 struct passwd *pw;
406
407 pw = getpwuid(getuid());
408 home_env = pw->pw_dir;
409#endif
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500410 }
411#endif
412 if (home_env)
413 len += strlen(home_env);
414 }
415 expanded_filename = malloc(len);
416 if (expanded_filename == 0)
417 return errno;
418 if (home_env) {
419 strcpy(expanded_filename, home_env);
420 strcat(expanded_filename, filespec+1);
421 } else
422 memcpy(expanded_filename, filespec, len);
423
Theodore Ts'od45544c2006-01-05 01:23:48 -0500424 prf->filespec = expanded_filename;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500425
426 retval = profile_update_file(prf);
427 if (retval) {
428 profile_free_file(prf);
429 return retval;
430 }
431
432 *ret_prof = prf;
433 return 0;
434}
435
Theodore Ts'od45544c2006-01-05 01:23:48 -0500436errcode_t profile_update_file(prf_file_t prf)
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500437{
438 errcode_t retval;
439#ifdef HAVE_STAT
440 struct stat st;
441#ifdef STAT_ONCE_PER_SECOND
442 time_t now;
443#endif
444#endif
445 FILE *f;
Theodore Ts'o204ae372006-03-22 10:01:24 -0500446 char buf[2048];
447 struct parse_state state;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500448
449#ifdef HAVE_STAT
450#ifdef STAT_ONCE_PER_SECOND
451 now = time(0);
Theodore Ts'od45544c2006-01-05 01:23:48 -0500452 if (now == prf->last_stat && prf->root != NULL) {
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500453 return 0;
454 }
455#endif
Theodore Ts'od45544c2006-01-05 01:23:48 -0500456 if (stat(prf->filespec, &st)) {
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500457 retval = errno;
458 return retval;
459 }
460#ifdef STAT_ONCE_PER_SECOND
Theodore Ts'od45544c2006-01-05 01:23:48 -0500461 prf->last_stat = now;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500462#endif
Theodore Ts'od45544c2006-01-05 01:23:48 -0500463 if (st.st_mtime == prf->timestamp && prf->root != NULL) {
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500464 return 0;
465 }
Theodore Ts'od45544c2006-01-05 01:23:48 -0500466 if (prf->root) {
467 profile_free_node(prf->root);
468 prf->root = 0;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500469 }
470#else
471 /*
472 * If we don't have the stat() call, assume that our in-core
473 * memory image is correct. That is, we won't reread the
474 * profile file if it changes.
475 */
Theodore Ts'od45544c2006-01-05 01:23:48 -0500476 if (prf->root) {
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500477 return 0;
478 }
479#endif
Theodore Ts'o204ae372006-03-22 10:01:24 -0500480 memset(&state, 0, sizeof(struct parse_state));
481 retval = profile_create_node("(root)", 0, &state.root_section);
482 if (retval)
483 return retval;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500484 errno = 0;
Theodore Ts'od45544c2006-01-05 01:23:48 -0500485 f = fopen(prf->filespec, "r");
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500486 if (f == NULL) {
487 retval = errno;
488 if (retval == 0)
489 retval = ENOENT;
490 return retval;
491 }
Theodore Ts'od45544c2006-01-05 01:23:48 -0500492 prf->upd_serial++;
Theodore Ts'o204ae372006-03-22 10:01:24 -0500493 while (!feof(f)) {
494 if (fgets(buf, sizeof(buf), f) == NULL)
495 break;
496 retval = parse_line(buf, &state);
497 if (retval) {
498 if (syntax_err_cb)
499 (syntax_err_cb)(prf->filespec, retval,
500 state.line_num);
501 fclose(f);
502 return retval;
503 }
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500504 }
Theodore Ts'o204ae372006-03-22 10:01:24 -0500505 prf->root = state.root_section;
506
507 fclose(f);
508
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500509#ifdef HAVE_STAT
Theodore Ts'od45544c2006-01-05 01:23:48 -0500510 prf->timestamp = st.st_mtime;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500511#endif
512 return 0;
513}
514
515void profile_free_file(prf_file_t prf)
516{
Theodore Ts'od45544c2006-01-05 01:23:48 -0500517 if (prf->root)
518 profile_free_node(prf->root);
519 if (prf->filespec)
520 free(prf->filespec);
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500521 free(prf);
522}
523
Theodore Ts'od45544c2006-01-05 01:23:48 -0500524/* Begin the profile parser */
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500525
Theodore Ts'of5f14fc2006-01-04 10:32:16 -0500526profile_syntax_err_cb_t profile_set_syntax_err_cb(profile_syntax_err_cb_t hook)
527{
528 profile_syntax_err_cb_t old;
529
530 old = syntax_err_cb;
531 syntax_err_cb = hook;
532 return(old);
533}
534
Theodore Ts'of5f14fc2006-01-04 10:32:16 -0500535#define STATE_INIT_COMMENT 0
536#define STATE_STD_LINE 1
537#define STATE_GET_OBRACE 2
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500538
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500539static char *skip_over_blanks(char *cp)
540{
541 while (*cp && isspace((int) (*cp)))
542 cp++;
543 return cp;
544}
545
Theodore Ts'o95a8d1d2006-01-05 00:47:34 -0500546static int end_or_comment(char ch)
547{
548 return (ch == 0 || ch == '#' || ch == ';');
549}
550
551static char *skip_over_nonblanks(char *cp)
552{
553 while (!end_or_comment(*cp) && !isspace(*cp))
554 cp++;
555 return cp;
556}
557
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500558static void strip_line(char *line)
559{
560 char *p = line + strlen(line);
561 while (p > line && (p[-1] == '\n' || p[-1] == '\r'))
562 *p-- = 0;
563}
564
565static void parse_quoted_string(char *str)
566{
567 char *to, *from;
568
569 to = from = str;
570
571 for (to = from = str; *from && *from != '"'; to++, from++) {
572 if (*from == '\\') {
573 from++;
574 switch (*from) {
575 case 'n':
576 *to = '\n';
577 break;
578 case 't':
579 *to = '\t';
580 break;
581 case 'b':
582 *to = '\b';
583 break;
584 default:
585 *to = *from;
586 }
587 continue;
588 }
589 *to = *from;
590 }
591 *to = '\0';
592}
593
Theodore Ts'o204ae372006-03-22 10:01:24 -0500594static errcode_t parse_line(char *line, struct parse_state *state)
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500595{
596 char *cp, ch, *tag, *value;
597 char *p;
598 errcode_t retval;
599 struct profile_node *node;
600 int do_subsection = 0;
601 void *iter = 0;
602
Theodore Ts'o204ae372006-03-22 10:01:24 -0500603 state->line_num++;
604 if (state->state == STATE_GET_OBRACE) {
605 cp = skip_over_blanks(line);
606 if (*cp != '{')
607 return PROF_MISSING_OBRACE;
608 state->state = STATE_STD_LINE;
609 return 0;
610 }
611 if (state->state == STATE_INIT_COMMENT) {
612 if (line[0] != '[')
613 return 0;
614 state->state = STATE_STD_LINE;
615 }
616
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500617 if (*line == 0)
618 return 0;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500619 strip_line(line);
620 cp = skip_over_blanks(line);
621 ch = *cp;
Theodore Ts'o95a8d1d2006-01-05 00:47:34 -0500622 if (end_or_comment(ch))
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500623 return 0;
624 if (ch == '[') {
625 if (state->group_level > 0)
626 return PROF_SECTION_NOTOP;
627 cp++;
Theodore Ts'o22fe6742006-01-06 15:04:39 -0500628 cp = skip_over_blanks(cp);
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500629 p = strchr(cp, ']');
630 if (p == NULL)
631 return PROF_SECTION_SYNTAX;
Theodore Ts'o22fe6742006-01-06 15:04:39 -0500632 if (*cp == '"') {
633 cp++;
634 parse_quoted_string(cp);
635 } else {
636 *p-- = '\0';
637 while (isspace(*p) && (p > cp))
638 *p-- = '\0';
639 if (*cp == 0)
640 return PROF_SECTION_SYNTAX;
641 }
Theodore Ts'o9a4c2092006-01-02 22:04:41 -0500642 retval = profile_find_node(state->root_section, cp, 0, 1,
643 &iter, &state->current_section);
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500644 if (retval == PROF_NO_SECTION) {
645 retval = profile_add_node(state->root_section,
646 cp, 0,
647 &state->current_section);
648 if (retval)
649 return retval;
650 } else if (retval)
651 return retval;
652
653 /*
654 * Finish off the rest of the line.
655 */
656 cp = p+1;
657 if (*cp == '*') {
Theodore Ts'o9a4c2092006-01-02 22:04:41 -0500658 state->current_section->final = 1;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500659 cp++;
660 }
661 /*
Theodore Ts'o95a8d1d2006-01-05 00:47:34 -0500662 * Spaces or comments after ']' should not be fatal
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500663 */
664 cp = skip_over_blanks(cp);
Theodore Ts'o95a8d1d2006-01-05 00:47:34 -0500665 if (!end_or_comment(*cp))
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500666 return PROF_SECTION_SYNTAX;
667 return 0;
668 }
669 if (ch == '}') {
670 if (state->group_level == 0)
671 return PROF_EXTRA_CBRACE;
672 if (*(cp+1) == '*')
Theodore Ts'o9a4c2092006-01-02 22:04:41 -0500673 state->current_section->final = 1;
674 state->current_section = state->current_section->parent;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500675 state->group_level--;
676 return 0;
677 }
678 /*
679 * Parse the relations
680 */
681 tag = cp;
682 cp = strchr(cp, '=');
683 if (!cp)
684 return PROF_RELATION_SYNTAX;
685 if (cp == tag)
686 return PROF_RELATION_SYNTAX;
687 *cp = '\0';
Theodore Ts'o22fe6742006-01-06 15:04:39 -0500688 if (*tag == '"') {
689 tag++;
690 parse_quoted_string(tag);
691 } else {
692 /* Look for whitespace on left-hand side. */
693 p = skip_over_nonblanks(tag);
694 if (*p)
695 *p++ = 0;
696 p = skip_over_blanks(p);
697 /* If we have more non-whitespace, it's an error. */
698 if (*p)
699 return PROF_RELATION_SYNTAX;
700 }
Theodore Ts'o95a8d1d2006-01-05 00:47:34 -0500701
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500702 cp = skip_over_blanks(cp+1);
703 value = cp;
Theodore Ts'o95a8d1d2006-01-05 00:47:34 -0500704 ch = value[0];
705 if (ch == '"') {
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500706 value++;
707 parse_quoted_string(value);
Theodore Ts'o95a8d1d2006-01-05 00:47:34 -0500708 } else if (end_or_comment(ch)) {
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500709 do_subsection++;
710 state->state = STATE_GET_OBRACE;
Theodore Ts'o95a8d1d2006-01-05 00:47:34 -0500711 } else if (value[0] == '{') {
712 cp = skip_over_blanks(value+1);
713 ch = *cp;
714 if (end_or_comment(ch))
715 do_subsection++;
716 else
717 return PROF_RELATION_SYNTAX;
718 } else {
719 cp = skip_over_nonblanks(value);
720 p = skip_over_blanks(cp);
721 ch = *p;
722 *cp = 0;
723 if (!end_or_comment(ch))
724 return PROF_RELATION_SYNTAX;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500725 }
726 if (do_subsection) {
727 p = strchr(tag, '*');
728 if (p)
729 *p = '\0';
730 retval = profile_add_node(state->current_section,
731 tag, 0, &state->current_section);
732 if (retval)
733 return retval;
734 if (p)
Theodore Ts'o9a4c2092006-01-02 22:04:41 -0500735 state->current_section->final = 1;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500736 state->group_level++;
737 return 0;
738 }
739 p = strchr(tag, '*');
740 if (p)
741 *p = '\0';
742 profile_add_node(state->current_section, tag, value, &node);
743 if (p)
Theodore Ts'o9a4c2092006-01-02 22:04:41 -0500744 node->final = 1;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500745 return 0;
746}
747
Theodore Ts'o44dc5f82006-01-02 12:25:03 -0500748#ifdef DEBUG_PROGRAM
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500749/*
750 * Return TRUE if the string begins or ends with whitespace
751 */
752static int need_double_quotes(char *str)
753{
754 if (!str || !*str)
755 return 0;
756 if (isspace((int) (*str)) ||isspace((int) (*(str + strlen(str) - 1))))
757 return 1;
Theodore Ts'o95a8d1d2006-01-05 00:47:34 -0500758 if (strchr(str, '\n') || strchr(str, '\t') || strchr(str, '\b') ||
759 strchr(str, ' ') || strchr(str, '#') || strchr(str, ';'))
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500760 return 1;
761 return 0;
762}
763
764/*
765 * Output a string with double quotes, doing appropriate backquoting
766 * of characters as necessary.
767 */
768static void output_quoted_string(char *str, void (*cb)(const char *,void *),
769 void *data)
770{
771 char ch;
772 char buf[2];
773
774 cb("\"", data);
775 if (!str) {
776 cb("\"", data);
777 return;
778 }
779 buf[1] = 0;
780 while ((ch = *str++)) {
781 switch (ch) {
782 case '\\':
783 cb("\\\\", data);
784 break;
785 case '\n':
786 cb("\\n", data);
787 break;
788 case '\t':
789 cb("\\t", data);
790 break;
791 case '\b':
792 cb("\\b", data);
793 break;
794 default:
795 /* This would be a lot faster if we scanned
796 forward for the next "interesting"
797 character. */
798 buf[0] = ch;
799 cb(buf, data);
800 break;
801 }
802 }
803 cb("\"", data);
804}
805
806#ifndef EOL
807#define EOL "\n"
808#endif
809
810/* Errors should be returned, not ignored! */
811static void dump_profile(struct profile_node *root, int level,
812 void (*cb)(const char *, void *), void *data)
813{
814 int i;
815 struct profile_node *p;
816 void *iter;
817 long retval;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500818
819 iter = 0;
820 do {
Theodore Ts'o9a4c2092006-01-02 22:04:41 -0500821 retval = profile_find_node(root, 0, 0, 0, &iter, &p);
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500822 if (retval)
823 break;
824 for (i=0; i < level; i++)
825 cb("\t", data);
Theodore Ts'o22fe6742006-01-06 15:04:39 -0500826 if (need_double_quotes(p->name))
827 output_quoted_string(p->name, cb, data);
828 else
Theodore Ts'o9a4c2092006-01-02 22:04:41 -0500829 cb(p->name, data);
Theodore Ts'o22fe6742006-01-06 15:04:39 -0500830 cb(" = ", data);
831 if (need_double_quotes(p->value))
Theodore Ts'o9a4c2092006-01-02 22:04:41 -0500832 output_quoted_string(p->value, cb, data);
Theodore Ts'o22fe6742006-01-06 15:04:39 -0500833 else
Theodore Ts'o9a4c2092006-01-02 22:04:41 -0500834 cb(p->value, data);
Theodore Ts'o22fe6742006-01-06 15:04:39 -0500835 cb(EOL, data);
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500836 } while (iter != 0);
837
838 iter = 0;
839 do {
Theodore Ts'o9a4c2092006-01-02 22:04:41 -0500840 retval = profile_find_node(root, 0, 0, 1, &iter, &p);
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500841 if (retval)
842 break;
843 if (level == 0) { /* [xxx] */
844 cb("[", data);
Theodore Ts'o22fe6742006-01-06 15:04:39 -0500845 if (need_double_quotes(p->name))
846 output_quoted_string(p->name, cb, data);
847 else
848 cb(p->name, data);
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500849 cb("]", data);
Theodore Ts'o9a4c2092006-01-02 22:04:41 -0500850 cb(p->final ? "*" : "", data);
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500851 cb(EOL, data);
852 dump_profile(p, level+1, cb, data);
853 cb(EOL, data);
854 } else { /* xxx = { ... } */
855 for (i=0; i < level; i++)
856 cb("\t", data);
Theodore Ts'o22fe6742006-01-06 15:04:39 -0500857 if (need_double_quotes(p->name))
858 output_quoted_string(p->name, cb, data);
859 else
860 cb(p->name, data);
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500861 cb(" = {", data);
862 cb(EOL, data);
863 dump_profile(p, level+1, cb, data);
864 for (i=0; i < level; i++)
865 cb("\t", data);
866 cb("}", data);
Theodore Ts'o9a4c2092006-01-02 22:04:41 -0500867 cb(p->final ? "*" : "", data);
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500868 cb(EOL, data);
869 }
870 } while (iter != 0);
871}
872
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500873static void dump_profile_to_file_cb(const char *str, void *data)
874{
875 fputs(str, data);
876}
877
878errcode_t profile_write_tree_file(struct profile_node *root, FILE *dstfile)
879{
880 dump_profile(root, 0, dump_profile_to_file_cb, dstfile);
881 return 0;
882}
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500883
884struct prof_buf {
885 char *base;
886 size_t cur, max;
887 int err;
888};
889
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500890static void add_data_to_buffer(struct prof_buf *b, const void *d, size_t len)
891{
892 if (b->err)
893 return;
894 if (b->max - b->cur < len) {
895 size_t newsize;
896 char *newptr;
897
898 newsize = b->max + (b->max >> 1) + len + 1024;
899 newptr = realloc(b->base, newsize);
900 if (newptr == NULL) {
901 b->err = 1;
902 return;
903 }
904 b->base = newptr;
905 b->max = newsize;
906 }
907 memcpy(b->base + b->cur, d, len);
908 b->cur += len; /* ignore overflow */
909}
910
911static void dump_profile_to_buffer_cb(const char *str, void *data)
912{
913 add_data_to_buffer((struct prof_buf *)data, str, strlen(str));
914}
915
916errcode_t profile_write_tree_to_buffer(struct profile_node *root,
917 char **buf)
918{
919 struct prof_buf prof_buf = { 0, 0, 0, 0 };
920
921 dump_profile(root, 0, dump_profile_to_buffer_cb, &prof_buf);
922 if (prof_buf.err) {
923 *buf = NULL;
924 return ENOMEM;
925 }
926 add_data_to_buffer(&prof_buf, "", 1); /* append nul */
927 if (prof_buf.max - prof_buf.cur > (prof_buf.max >> 3)) {
928 char *newptr = realloc(prof_buf.base, prof_buf.cur);
929 if (newptr)
930 prof_buf.base = newptr;
931 }
932 *buf = prof_buf.base;
933 return 0;
934}
935#endif
936
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500937/*
938 * prof_tree.c --- these routines maintain the parse tree of the
939 * config file.
940 *
941 * All of the details of how the tree is stored is abstracted away in
942 * this file; all of the other profile routines build, access, and
943 * modify the tree via the accessor functions found in this file.
944 *
945 * Each node may represent either a relation or a section header.
946 *
947 * A section header must have its value field set to 0, and may a one
948 * or more child nodes, pointed to by first_child.
949 *
950 * A relation has as its value a pointer to allocated memory
951 * containing a string. Its first_child pointer must be null.
952 *
953 */
954
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -0500955/*
956 * Free a node, and any children
957 */
958void profile_free_node(struct profile_node *node)
959{
960 struct profile_node *child, *next;
961
962 if (node->magic != PROF_MAGIC_NODE)
963 return;
964
965 if (node->name)
966 free(node->name);
967 if (node->value)
968 free(node->value);
969
970 for (child=node->first_child; child; child = next) {
971 next = child->next;
972 profile_free_node(child);
973 }
974 node->magic = 0;
975
976 free(node);
977}
978
979#ifndef HAVE_STRDUP
980#undef strdup
981#define strdup MYstrdup
982static char *MYstrdup (const char *s)
983{
984 size_t sz = strlen(s) + 1;
985 char *p = malloc(sz);
986 if (p != 0)
987 memcpy(p, s, sz);
988 return p;
989}
990#endif
991
992/*
993 * Create a node
994 */
995errcode_t profile_create_node(const char *name, const char *value,
996 struct profile_node **ret_node)
997{
998 struct profile_node *new;
999
1000 new = malloc(sizeof(struct profile_node));
1001 if (!new)
1002 return ENOMEM;
1003 memset(new, 0, sizeof(struct profile_node));
1004 new->name = strdup(name);
1005 if (new->name == 0) {
1006 profile_free_node(new);
1007 return ENOMEM;
1008 }
1009 if (value) {
1010 new->value = strdup(value);
1011 if (new->value == 0) {
1012 profile_free_node(new);
1013 return ENOMEM;
1014 }
1015 }
1016 new->magic = PROF_MAGIC_NODE;
1017
1018 *ret_node = new;
1019 return 0;
1020}
1021
1022/*
1023 * This function verifies that all of the representation invarients of
1024 * the profile are true. If not, we have a programming bug somewhere,
1025 * probably in this file.
1026 */
1027#ifdef DEBUG_PROGRAM
1028errcode_t profile_verify_node(struct profile_node *node)
1029{
1030 struct profile_node *p, *last;
1031 errcode_t retval;
1032
1033 CHECK_MAGIC(node);
1034
1035 if (node->value && node->first_child)
1036 return PROF_SECTION_WITH_VALUE;
1037
1038 last = 0;
1039 for (p = node->first_child; p; last = p, p = p->next) {
1040 if (p->prev != last)
1041 return PROF_BAD_LINK_LIST;
1042 if (last && (last->next != p))
1043 return PROF_BAD_LINK_LIST;
1044 if (node->group_level+1 != p->group_level)
1045 return PROF_BAD_GROUP_LVL;
1046 if (p->parent != node)
1047 return PROF_BAD_PARENT_PTR;
1048 retval = profile_verify_node(p);
1049 if (retval)
1050 return retval;
1051 }
1052 return 0;
1053}
1054#endif
1055
1056/*
1057 * Add a node to a particular section
1058 */
1059errcode_t profile_add_node(struct profile_node *section, const char *name,
1060 const char *value, struct profile_node **ret_node)
1061{
1062 errcode_t retval;
1063 struct profile_node *p, *last, *new;
1064
1065 CHECK_MAGIC(section);
1066
1067 if (section->value)
1068 return PROF_ADD_NOT_SECTION;
1069
1070 /*
1071 * Find the place to insert the new node. We look for the
1072 * place *after* the last match of the node name, since
1073 * order matters.
1074 */
1075 for (p=section->first_child, last = 0; p; last = p, p = p->next) {
1076 int cmp;
1077 cmp = strcmp(p->name, name);
1078 if (cmp > 0)
1079 break;
1080 }
1081 retval = profile_create_node(name, value, &new);
1082 if (retval)
1083 return retval;
1084 new->group_level = section->group_level+1;
1085 new->deleted = 0;
1086 new->parent = section;
1087 new->prev = last;
1088 new->next = p;
1089 if (p)
1090 p->prev = new;
1091 if (last)
1092 last->next = new;
1093 else
1094 section->first_child = new;
1095 if (ret_node)
1096 *ret_node = new;
1097 return 0;
1098}
1099
1100/*
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001101 * Iterate through the section, returning the nodes which match
1102 * the given name. If name is NULL, then interate through all the
1103 * nodes in the section. If section_flag is non-zero, only return the
1104 * section which matches the name; don't return relations. If value
1105 * is non-NULL, then only return relations which match the requested
1106 * value. (The value argument is ignored if section_flag is non-zero.)
1107 *
1108 * The first time this routine is called, the state pointer must be
1109 * null. When this profile_find_node_relation() returns, if the state
1110 * pointer is non-NULL, then this routine should be called again.
1111 * (This won't happen if section_flag is non-zero, obviously.)
1112 *
1113 */
1114errcode_t profile_find_node(struct profile_node *section, const char *name,
1115 const char *value, int section_flag, void **state,
1116 struct profile_node **node)
1117{
1118 struct profile_node *p;
1119
1120 CHECK_MAGIC(section);
1121 p = *state;
1122 if (p) {
1123 CHECK_MAGIC(p);
1124 } else
1125 p = section->first_child;
1126
1127 for (; p; p = p->next) {
1128 if (name && (strcmp(p->name, name)))
1129 continue;
1130 if (section_flag) {
1131 if (p->value)
1132 continue;
1133 } else {
1134 if (!p->value)
1135 continue;
1136 if (value && (strcmp(p->value, value)))
1137 continue;
1138 }
1139 if (p->deleted)
1140 continue;
1141 /* A match! */
1142 if (node)
1143 *node = p;
1144 break;
1145 }
1146 if (p == 0) {
1147 *state = 0;
1148 return section_flag ? PROF_NO_SECTION : PROF_NO_RELATION;
1149 }
1150 /*
1151 * OK, we've found one match; now let's try to find another
1152 * one. This way, if we return a non-zero state pointer,
1153 * there's guaranteed to be another match that's returned.
1154 */
1155 for (p = p->next; p; p = p->next) {
1156 if (name && (strcmp(p->name, name)))
1157 continue;
1158 if (section_flag) {
1159 if (p->value)
1160 continue;
1161 } else {
1162 if (!p->value)
1163 continue;
1164 if (value && (strcmp(p->value, value)))
1165 continue;
1166 }
1167 /* A match! */
1168 break;
1169 }
1170 *state = p;
1171 return 0;
1172}
1173
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001174/*
1175 * This is a general-purpose iterator for returning all nodes that
1176 * match the specified name array.
1177 */
1178struct profile_iterator {
1179 prf_magic_t magic;
1180 profile_t profile;
1181 int flags;
1182 const char *const *names;
1183 const char *name;
1184 prf_file_t file;
1185 int file_serial;
1186 int done_idx;
1187 struct profile_node *node;
1188 int num;
1189};
1190
Theodore Ts'o9a4c2092006-01-02 22:04:41 -05001191errcode_t
1192profile_iterator_create(profile_t profile, const char *const *names, int flags,
1193 void **ret_iter)
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001194{
1195 struct profile_iterator *iter;
1196 int done_idx = 0;
1197
1198 if (profile == 0)
1199 return PROF_NO_PROFILE;
1200 if (profile->magic != PROF_MAGIC_PROFILE)
1201 return PROF_MAGIC_PROFILE;
1202 if (!names)
1203 return PROF_BAD_NAMESET;
1204 if (!(flags & PROFILE_ITER_LIST_SECTION)) {
1205 if (!names[0])
1206 return PROF_BAD_NAMESET;
1207 done_idx = 1;
1208 }
1209
1210 if ((iter = malloc(sizeof(struct profile_iterator))) == NULL)
1211 return ENOMEM;
1212
1213 iter->magic = PROF_MAGIC_ITERATOR;
1214 iter->profile = profile;
1215 iter->names = names;
1216 iter->flags = flags;
1217 iter->file = profile->first_file;
1218 iter->done_idx = done_idx;
1219 iter->node = 0;
1220 iter->num = 0;
1221 *ret_iter = iter;
1222 return 0;
1223}
1224
Theodore Ts'o9a4c2092006-01-02 22:04:41 -05001225void profile_iterator_free(void **iter_p)
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001226{
1227 struct profile_iterator *iter;
1228
1229 if (!iter_p)
1230 return;
1231 iter = *iter_p;
1232 if (!iter || iter->magic != PROF_MAGIC_ITERATOR)
1233 return;
1234 free(iter);
1235 *iter_p = 0;
1236}
1237
1238/*
1239 * Note: the returned character strings in ret_name and ret_value
1240 * points to the stored character string in the parse string. Before
1241 * this string value is returned to a calling application
1242 * (profile_node_iterator is not an exported interface), it should be
1243 * strdup()'ed.
1244 */
1245errcode_t profile_node_iterator(void **iter_p, struct profile_node **ret_node,
1246 char **ret_name, char **ret_value)
1247{
1248 struct profile_iterator *iter = *iter_p;
1249 struct profile_node *section, *p;
1250 const char *const *cpp;
1251 errcode_t retval;
1252 int skip_num = 0;
1253
1254 if (!iter || iter->magic != PROF_MAGIC_ITERATOR)
1255 return PROF_MAGIC_ITERATOR;
1256 if (iter->file && iter->file->magic != PROF_MAGIC_FILE)
1257 return PROF_MAGIC_FILE;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001258 /*
1259 * If the file has changed, then the node pointer is invalid,
1260 * so we'll have search the file again looking for it.
1261 */
Theodore Ts'od45544c2006-01-05 01:23:48 -05001262 if (iter->node && (iter->file->upd_serial != iter->file_serial)) {
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001263 iter->flags &= ~PROFILE_ITER_FINAL_SEEN;
1264 skip_num = iter->num;
1265 iter->node = 0;
1266 }
1267 if (iter->node && iter->node->magic != PROF_MAGIC_NODE) {
1268 return PROF_MAGIC_NODE;
1269 }
1270get_new_file:
1271 if (iter->node == 0) {
1272 if (iter->file == 0 ||
1273 (iter->flags & PROFILE_ITER_FINAL_SEEN)) {
Theodore Ts'o9a4c2092006-01-02 22:04:41 -05001274 profile_iterator_free(iter_p);
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001275 if (ret_node)
1276 *ret_node = 0;
1277 if (ret_name)
1278 *ret_name = 0;
1279 if (ret_value)
1280 *ret_value =0;
1281 return 0;
1282 }
1283 if ((retval = profile_update_file(iter->file))) {
1284 if (retval == ENOENT || retval == EACCES) {
1285 /* XXX memory leak? */
1286 iter->file = iter->file->next;
1287 skip_num = 0;
1288 retval = 0;
1289 goto get_new_file;
1290 } else {
Theodore Ts'o9a4c2092006-01-02 22:04:41 -05001291 profile_iterator_free(iter_p);
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001292 return retval;
1293 }
1294 }
Theodore Ts'od45544c2006-01-05 01:23:48 -05001295 iter->file_serial = iter->file->upd_serial;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001296 /*
1297 * Find the section to list if we are a LIST_SECTION,
1298 * or find the containing section if not.
1299 */
Theodore Ts'od45544c2006-01-05 01:23:48 -05001300 section = iter->file->root;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001301 for (cpp = iter->names; cpp[iter->done_idx]; cpp++) {
1302 for (p=section->first_child; p; p = p->next) {
1303 if (!strcmp(p->name, *cpp) && !p->value)
1304 break;
1305 }
1306 if (!p) {
1307 section = 0;
1308 break;
1309 }
1310 section = p;
1311 if (p->final)
1312 iter->flags |= PROFILE_ITER_FINAL_SEEN;
1313 }
1314 if (!section) {
1315 iter->file = iter->file->next;
1316 skip_num = 0;
1317 goto get_new_file;
1318 }
1319 iter->name = *cpp;
1320 iter->node = section->first_child;
1321 }
1322 /*
1323 * OK, now we know iter->node is set up correctly. Let's do
1324 * the search.
1325 */
1326 for (p = iter->node; p; p = p->next) {
1327 if (iter->name && strcmp(p->name, iter->name))
1328 continue;
1329 if ((iter->flags & PROFILE_ITER_SECTIONS_ONLY) &&
1330 p->value)
1331 continue;
1332 if ((iter->flags & PROFILE_ITER_RELATIONS_ONLY) &&
1333 !p->value)
1334 continue;
1335 if (skip_num > 0) {
1336 skip_num--;
1337 continue;
1338 }
1339 if (p->deleted)
1340 continue;
1341 break;
1342 }
1343 iter->num++;
1344 if (!p) {
1345 iter->file = iter->file->next;
1346 if (iter->file) {
1347 }
1348 iter->node = 0;
1349 skip_num = 0;
1350 goto get_new_file;
1351 }
1352 if ((iter->node = p->next) == NULL)
1353 iter->file = iter->file->next;
1354 if (ret_node)
1355 *ret_node = p;
1356 if (ret_name)
1357 *ret_name = p->name;
1358 if (ret_value)
1359 *ret_value = p->value;
1360 return 0;
1361}
1362
1363
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001364/*
1365 * prof_get.c --- routines that expose the public interfaces for
1366 * querying items from the profile.
1367 *
1368 */
1369
1370/*
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001371 * This function only gets the first value from the file; it is a
1372 * helper function for profile_get_string, profile_get_integer, etc.
1373 */
Theodore Ts'o9a4c2092006-01-02 22:04:41 -05001374errcode_t profile_get_value(profile_t profile, const char *name,
1375 const char *subname, const char *subsubname,
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001376 const char **ret_value)
1377{
1378 errcode_t retval;
1379 void *state;
1380 char *value;
Theodore Ts'o9a4c2092006-01-02 22:04:41 -05001381 const char *names[4];
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001382
Theodore Ts'o9a4c2092006-01-02 22:04:41 -05001383 names[0] = name;
1384 names[1] = subname;
1385 names[2] = subsubname;
1386 names[3] = 0;
1387
1388 if ((retval = profile_iterator_create(profile, names,
1389 PROFILE_ITER_RELATIONS_ONLY,
1390 &state)))
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001391 return retval;
1392
1393 if ((retval = profile_node_iterator(&state, 0, 0, &value)))
1394 goto cleanup;
1395
1396 if (value)
1397 *ret_value = value;
1398 else
1399 retval = PROF_NO_RELATION;
1400
1401cleanup:
Theodore Ts'o9a4c2092006-01-02 22:04:41 -05001402 profile_iterator_free(&state);
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001403 return retval;
1404}
1405
1406errcode_t
1407profile_get_string(profile_t profile, const char *name, const char *subname,
1408 const char *subsubname, const char *def_val,
1409 char **ret_string)
1410{
1411 const char *value;
1412 errcode_t retval;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001413
1414 if (profile) {
Theodore Ts'o9a4c2092006-01-02 22:04:41 -05001415 retval = profile_get_value(profile, name, subname,
1416 subsubname, &value);
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001417 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION)
1418 value = def_val;
1419 else if (retval)
1420 return retval;
1421 } else
1422 value = def_val;
1423
1424 if (value) {
1425 *ret_string = malloc(strlen(value)+1);
1426 if (*ret_string == 0)
1427 return ENOMEM;
1428 strcpy(*ret_string, value);
1429 } else
1430 *ret_string = 0;
1431 return 0;
1432}
1433
1434errcode_t
1435profile_get_integer(profile_t profile, const char *name, const char *subname,
1436 const char *subsubname, int def_val, int *ret_int)
1437{
1438 const char *value;
1439 errcode_t retval;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001440 char *end_value;
1441 long ret_long;
1442
1443 *ret_int = def_val;
1444 if (profile == 0)
1445 return 0;
1446
Theodore Ts'o9a4c2092006-01-02 22:04:41 -05001447 retval = profile_get_value(profile, name, subname, subsubname, &value);
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001448 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
1449 *ret_int = def_val;
1450 return 0;
1451 } else if (retval)
1452 return retval;
1453
1454 if (value[0] == 0)
1455 /* Empty string is no good. */
1456 return PROF_BAD_INTEGER;
1457 errno = 0;
1458 ret_long = strtol (value, &end_value, 10);
1459
1460 /* Overflow or underflow. */
1461 if ((ret_long == LONG_MIN || ret_long == LONG_MAX) && errno != 0)
1462 return PROF_BAD_INTEGER;
1463 /* Value outside "int" range. */
1464 if ((long) (int) ret_long != ret_long)
1465 return PROF_BAD_INTEGER;
1466 /* Garbage in string. */
1467 if (end_value != value + strlen (value))
1468 return PROF_BAD_INTEGER;
1469
1470
1471 *ret_int = ret_long;
1472 return 0;
1473}
1474
1475static const char *const conf_yes[] = {
1476 "y", "yes", "true", "t", "1", "on",
1477 0,
1478};
1479
1480static const char *const conf_no[] = {
1481 "n", "no", "false", "nil", "0", "off",
1482 0,
1483};
1484
1485static errcode_t
1486profile_parse_boolean(const char *s, int *ret_boolean)
1487{
1488 const char *const *p;
1489
1490 if (ret_boolean == NULL)
1491 return PROF_EINVAL;
1492
1493 for(p=conf_yes; *p; p++) {
1494 if (!strcasecmp(*p,s)) {
1495 *ret_boolean = 1;
1496 return 0;
1497 }
1498 }
1499
1500 for(p=conf_no; *p; p++) {
1501 if (!strcasecmp(*p,s)) {
1502 *ret_boolean = 0;
1503 return 0;
1504 }
1505 }
1506
1507 return PROF_BAD_BOOLEAN;
1508}
1509
1510errcode_t
1511profile_get_boolean(profile_t profile, const char *name, const char *subname,
1512 const char *subsubname, int def_val, int *ret_boolean)
1513{
1514 const char *value;
1515 errcode_t retval;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001516
1517 if (profile == 0) {
1518 *ret_boolean = def_val;
1519 return 0;
1520 }
1521
Theodore Ts'o9a4c2092006-01-02 22:04:41 -05001522 retval = profile_get_value(profile, name, subname, subsubname, &value);
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001523 if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
1524 *ret_boolean = def_val;
1525 return 0;
1526 } else if (retval)
1527 return retval;
1528
1529 return profile_parse_boolean (value, ret_boolean);
1530}
1531
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001532errcode_t
1533profile_iterator(void **iter_p, char **ret_name, char **ret_value)
1534{
1535 char *name, *value;
1536 errcode_t retval;
1537
1538 retval = profile_node_iterator(iter_p, 0, &name, &value);
1539 if (retval)
1540 return retval;
1541
1542 if (ret_name) {
1543 if (name) {
1544 *ret_name = malloc(strlen(name)+1);
1545 if (!*ret_name)
1546 return ENOMEM;
1547 strcpy(*ret_name, name);
1548 } else
1549 *ret_name = 0;
1550 }
1551 if (ret_value) {
1552 if (value) {
1553 *ret_value = malloc(strlen(value)+1);
1554 if (!*ret_value) {
1555 if (ret_name) {
1556 free(*ret_name);
1557 *ret_name = 0;
1558 }
1559 return ENOMEM;
1560 }
1561 strcpy(*ret_value, value);
1562 } else
1563 *ret_value = 0;
1564 }
1565 return 0;
1566}
1567
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001568#ifdef DEBUG_PROGRAM
1569
1570/*
1571 * test_profile.c --- testing program for the profile routine
1572 */
1573
1574#include "argv_parse.h"
Theodore Ts'o9a4c2092006-01-02 22:04:41 -05001575#include "profile_helpers.h"
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001576
1577const char *program_name = "test_profile";
1578
1579#define PRINT_VALUE 1
1580#define PRINT_VALUES 2
1581
Theodore Ts'o44dc5f82006-01-02 12:25:03 -05001582static void do_cmd(profile_t profile, char **argv)
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001583{
1584 errcode_t retval;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001585 const char **names, *value;
Theodore Ts'o44dc5f82006-01-02 12:25:03 -05001586 char **values, **cpp;
1587 char *cmd;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001588 int print_status;
1589
Theodore Ts'o44dc5f82006-01-02 12:25:03 -05001590 cmd = *(argv);
1591 names = (const char **) argv + 1;
1592 print_status = 0;
1593 retval = 0;
1594 if (cmd == 0)
1595 return;
1596 if (!strcmp(cmd, "query")) {
1597 retval = profile_get_values(profile, names, &values);
1598 print_status = PRINT_VALUES;
1599 } else if (!strcmp(cmd, "query1")) {
Theodore Ts'o9a4c2092006-01-02 22:04:41 -05001600 const char *name = 0;
1601 const char *subname = 0;
1602 const char *subsubname = 0;
1603
1604 name = names[0];
1605 if (name)
1606 subname = names[1];
1607 if (subname)
1608 subsubname = names[2];
1609 if (subsubname && names[3]) {
1610 fprintf(stderr,
1611 "Only 3 levels are allowed with query1\n");
1612 retval = EINVAL;
1613 } else
1614 retval = profile_get_value(profile, name, subname,
1615 subsubname, &value);
Theodore Ts'o44dc5f82006-01-02 12:25:03 -05001616 print_status = PRINT_VALUE;
1617 } else if (!strcmp(cmd, "list_sections")) {
1618 retval = profile_get_subsection_names(profile, names,
1619 &values);
1620 print_status = PRINT_VALUES;
1621 } else if (!strcmp(cmd, "list_relations")) {
1622 retval = profile_get_relation_names(profile, names,
1623 &values);
1624 print_status = PRINT_VALUES;
1625 } else if (!strcmp(cmd, "dump")) {
1626 retval = profile_write_tree_file
Theodore Ts'od45544c2006-01-05 01:23:48 -05001627 (profile->first_file->root, stdout);
Theodore Ts'o44dc5f82006-01-02 12:25:03 -05001628#if 0
1629 } else if (!strcmp(cmd, "clear")) {
1630 retval = profile_clear_relation(profile, names);
1631 } else if (!strcmp(cmd, "update")) {
1632 retval = profile_update_relation(profile, names+2,
1633 *names, *(names+1));
1634#endif
1635 } else if (!strcmp(cmd, "verify")) {
1636 retval = profile_verify_node
Theodore Ts'od45544c2006-01-05 01:23:48 -05001637 (profile->first_file->root);
Theodore Ts'o44dc5f82006-01-02 12:25:03 -05001638#if 0
1639 } else if (!strcmp(cmd, "rename_section")) {
1640 retval = profile_rename_section(profile, names+1, *names);
1641 } else if (!strcmp(cmd, "add")) {
1642 value = *names;
1643 if (strcmp(value, "NULL") == 0)
1644 value = NULL;
1645 retval = profile_add_relation(profile, names+1, value);
1646 } else if (!strcmp(cmd, "flush")) {
1647 retval = profile_flush(profile);
1648#endif
1649 } else {
1650 printf("Invalid command.\n");
1651 }
1652 if (retval) {
1653 com_err(cmd, retval, "");
1654 print_status = 0;
1655 }
1656 switch (print_status) {
1657 case PRINT_VALUE:
1658 printf("%s\n", value);
1659 break;
1660 case PRINT_VALUES:
1661 for (cpp = values; *cpp; cpp++)
1662 printf("%s\n", *cpp);
1663 profile_free_list(values);
1664 break;
1665 }
1666}
1667
1668static void do_batchmode(profile_t profile)
1669{
1670 int argc, ret;
1671 char **argv;
1672 char buf[256];
1673
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001674 while (!feof(stdin)) {
1675 if (fgets(buf, sizeof(buf), stdin) == NULL)
1676 break;
1677 printf(">%s", buf);
1678 ret = argv_parse(buf, &argc, &argv);
1679 if (ret != 0) {
1680 printf("Argv_parse returned %d!\n", ret);
1681 continue;
1682 }
Theodore Ts'o44dc5f82006-01-02 12:25:03 -05001683 do_cmd(profile, argv);
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001684 printf("\n");
1685 argv_free(argv);
1686 }
1687 profile_release(profile);
1688 exit(0);
1689
1690}
1691
Theodore Ts'of5f14fc2006-01-04 10:32:16 -05001692void syntax_err_report(const char *filename, long err, int line_num)
1693{
1694 fprintf(stderr, "Syntax error in %s, line number %d: %s\n",
1695 filename, line_num, error_message(err));
1696 exit(1);
1697}
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001698
Theodore Ts'o44dc5f82006-01-02 12:25:03 -05001699int main(int argc, char **argv)
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001700{
1701 profile_t profile;
1702 long retval;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001703 char *cmd;
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001704
1705 if (argc < 2) {
1706 fprintf(stderr, "Usage: %s filename [cmd argset]\n", program_name);
1707 exit(1);
1708 }
1709
1710 initialize_prof_error_table();
Theodore Ts'of5f14fc2006-01-04 10:32:16 -05001711
1712 profile_set_syntax_err_cb(syntax_err_report);
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001713
1714 retval = profile_init_path(argv[1], &profile);
1715 if (retval) {
1716 com_err(program_name, retval, "while initializing profile");
1717 exit(1);
1718 }
1719 cmd = *(argv+2);
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001720 if (!cmd || !strcmp(cmd, "batch"))
1721 do_batchmode(profile);
Theodore Ts'o44dc5f82006-01-02 12:25:03 -05001722 else
1723 do_cmd(profile, argv+2);
Theodore Ts'o2fa9ba92005-12-30 23:57:32 -05001724 profile_release(profile);
1725
1726 return 0;
1727}
1728
1729#endif