3 #include "libconfig_private.h"
4 #include "conf_section.h"
5 #include "conf_apache.h"
6 #include "conf_colon.h"
7 #include "conf_equal.h"
8 #include "conf_space.h"
31 #ifdef HAVE_SYS_TYPES_H
32 #include <sys/types.h>
39 struct lc_varhandler_st *varhandlers = NULL;
40 lc_err_t lc_errno = LC_ERR_NONE;
41 const char *lc_errfile = NULL;
45 extern char **environ;
47 static int lc_process_var_string(void *data, const char *value, const char **endptr) {
51 *dataval = strdup(value);
58 static int lc_process_var_cidr(void *data, const char *value, const char **endptr) {
62 static int lc_process_var_hostname6(void *data, const char *value, const char **endptr) {
66 static int lc_process_var_hostname4(void *data, const char *value, const char **endptr) {
70 static int lc_process_var_ip6(void *data, const char *value, const char **endptr) {
74 static int lc_process_var_ip4(void *data, const char *value, const char **endptr) {
75 uint32_t *dataval, retval = 0;
76 const char *dotptr = NULL;
85 tmpval = atoi(dotptr);
93 dotptr = strpbrk(dotptr, "./ \t");
105 *endptr = (char *) dotptr;
110 static int lc_process_var_longlong(void *data, const char *value, const char **endptr) {
114 *dataval = strtoll(value, (char **) endptr, 10);
119 static int lc_process_var_long(void *data, const char *value, const char **endptr) {
123 *dataval = strtoll(value, (char **) endptr, 10);
128 static int lc_process_var_int(void *data, const char *value, const char **endptr) {
132 *dataval = strtoll(value, (char **) endptr, 10);
137 static int lc_process_var_short(void *data, const char *value, const char **endptr) {
141 *dataval = strtoll(value, (char **) endptr, 10);
146 static int lc_process_var_bool_byexistance(void *data, const char *value, const char **endptr) {
158 static int lc_process_var_bool(void *data, const char *value, const char **endptr) {
159 char *trueval[] = {"enable", "true", "yes", "on", "y", "1"};
160 char *falseval[] = {"disable", "false", "no", "off", "n", "0"};
161 size_t chkvallen, vallen;
169 vallen = strlen(value);
171 for (i = 0; i < (sizeof(trueval) / sizeof(*trueval)); i++) {
172 chkvallen = strlen(trueval[i]);
175 * Skip if there's no way we could find a match here.
177 if (chkvallen > vallen) {
182 * Skip if there is no partial match.
184 if (strncasecmp(value, trueval[i], chkvallen) != 0) {
188 if (value[chkvallen] == '\0' || value[chkvallen] == ',' || \
189 value[chkvallen] == ' ') {
190 /* Declare a winner and set the next token. */
191 *endptr = value + chkvallen;
197 for (i = 0; i < (sizeof(falseval) / sizeof(*falseval)); i++) {
198 chkvallen = strlen(falseval[i]);
201 * Skip if there's no way we could find a match here.
203 if (chkvallen > vallen) {
208 * Skip if there is no partial match.
210 if (strncasecmp(value, falseval[i], chkvallen) != 0) {
214 if (value[chkvallen] == '\0' || value[chkvallen] == ',' || \
215 value[chkvallen] == ' ') {
216 /* Declare a winner and set the next token. */
217 *endptr = value + chkvallen;
223 lc_errno = LC_ERR_BADFORMAT;
227 static unsigned long long lc_process_size(const char *value, const char **endptr) {
228 unsigned long long retval = 0;
231 retval = strtoll(value, &mult, 10);
233 switch (tolower(mult[0])) {
235 retval *= 1125899906842624LLU;
238 retval *= 1958505086976LLU;
241 retval *= 1073741824;
257 static int lc_process_var_sizelonglong(void *data, const char *value, const char **endptr) {
261 *dataval = lc_process_size(value, endptr);
266 static int lc_process_var_sizelong(void *data, const char *value, const char **endptr) {
270 *dataval = lc_process_size(value, endptr);
275 static int lc_process_var_sizeint(void *data, const char *value, const char **endptr) {
279 *dataval = lc_process_size(value, endptr);
284 static int lc_process_var_sizeshort(void *data, const char *value, const char **endptr) {
288 *dataval = lc_process_size(value, endptr);
293 static int lc_process_var_sizesizet(void *data, const char *value, const char **endptr) {
297 *dataval = lc_process_size(value, endptr);
303 static int lc_handle_type(lc_var_type_t type, const char *value, void *data) {
307 is_list = type & LC_VAR_LIST;
309 if (is_list == LC_VAR_LIST) {
314 return(lc_process_var_string(data, value, &next));
316 case LC_VAR_LONG_LONG:
317 return(lc_process_var_longlong(data, value, &next));
320 return(lc_process_var_long(data, value, &next));
323 return(lc_process_var_int(data, value, &next));
326 return(lc_process_var_short(data, value, &next));
329 return(lc_process_var_bool(data, value, &next));
331 case LC_VAR_SIZE_LONG_LONG:
332 return(lc_process_var_sizelonglong(data, value, &next));
334 case LC_VAR_SIZE_LONG:
335 return(lc_process_var_sizelong(data, value, &next));
337 case LC_VAR_SIZE_INT:
338 return(lc_process_var_sizeint(data, value, &next));
340 case LC_VAR_SIZE_SHORT:
341 return(lc_process_var_sizeshort(data, value, &next));
343 case LC_VAR_BOOL_BY_EXISTANCE:
344 return(lc_process_var_bool_byexistance(data, value, &next));
346 case LC_VAR_SIZE_SIZE_T:
347 return(lc_process_var_sizesizet(data, value, &next));
351 return(lc_process_var_ip4(data, value, &next));
354 return(lc_process_var_ip6(data, value, &next));
356 case LC_VAR_HOSTNAME4:
357 return(lc_process_var_hostname4(data, value, &next));
359 case LC_VAR_HOSTNAME6:
360 return(lc_process_var_hostname6(data, value, &next));
363 return(lc_process_var_cidr(data, value, &next));
367 case LC_VAR_FILENAME:
368 case LC_VAR_DIRECTORY:
370 fprintf(stderr, "Not implemented yet!\n");
376 case LC_VAR_SECTIONSTART:
377 case LC_VAR_SECTIONEND:
385 static int lc_handle(struct lc_varhandler_st *handler, const char *var, const char *varargs, const char *value, lc_flags_t flags) {
386 const char *localvar = NULL;
390 localvar = strrchr(var, '.');
391 if (localvar == NULL) {
400 switch (handler->mode) {
401 case LC_MODE_CALLBACK:
402 if (handler->callback != NULL) {
403 retval = handler->callback(localvar, var, varargs, value, flags, handler->extra);
405 lc_errno = LC_ERR_CALLBACK;
411 return(lc_handle_type(handler->type, value, handler->data));
418 static int lc_process_environment(const char *appname) {
420 struct lc_varhandler_st *handler = NULL;
421 size_t appnamelen = 0;
422 char varnamebuf[128] = {0};
424 char *sep = NULL, *value = NULL, *cmd = NULL;
425 char *ucase_appname = NULL, *ucase_appname_itr = NULL;
426 char *lastcomponent_handler = NULL;
428 char *local_lc_errfile;
429 int local_lc_errline;
431 /* Make sure we have an environment to screw with, if not,
432 no arguments were found to be in error */
433 if (environ == NULL || appname == NULL) {
437 local_lc_errfile = "<environment>";
438 local_lc_errline = 0;
440 /* Allocate and create our uppercase appname. */
441 ucase_appname = strdup(appname);
442 if (ucase_appname == NULL) {
443 lc_errfile = local_lc_errfile;
444 lc_errline = local_lc_errline;
445 lc_errno = LC_ERR_ENOMEM;
448 for (ucase_appname_itr = ucase_appname; *ucase_appname_itr != '\0'; ucase_appname_itr++) {
449 *ucase_appname_itr = toupper(*ucase_appname_itr);
452 appnamelen = strlen(ucase_appname);
454 for (currvar = environ; *currvar != NULL; currvar++) {
455 /* If it doesn't begin with our appname ignore it completely. */
456 if (strncmp(*currvar, ucase_appname, appnamelen) != 0) {
460 /* Find our seperator. */
461 sep = strchr(*currvar, '=');
466 varnamelen = sep - *currvar;
468 /* Skip variables that would overflow our buffer. */
469 if (varnamelen >= sizeof(varnamebuf)) {
473 strncpy(varnamebuf, *currvar, varnamelen);
475 varnamebuf[varnamelen] = '\0';
478 /* We ignore APPNAME by itself. */
479 if (strlen(varnamebuf) <= appnamelen) {
483 /* Further it must be <APPNAME>_ */
484 if (varnamebuf[appnamelen] != '_') {
488 cmd = varnamebuf + appnamelen + 1;
490 /* We don't allow section specifiers, for reasons see notes in
491 the cmdline processor (below). */
492 if (strchr(cmd, '.') != NULL) {
496 for (handler = varhandlers; handler != NULL; handler = handler->_next) {
497 if (handler->var == NULL) {
501 /* Skip handlers which don't agree with being
502 processed outside a config file */
503 if (handler->type == LC_VAR_SECTION ||
504 handler->type == LC_VAR_SECTIONSTART ||
505 handler->type == LC_VAR_SECTIONEND ||
506 handler->type == LC_VAR_UNKNOWN) {
510 /* Find the last part of the variable and compare it with
511 the option being processed, if a wildcard is given. */
512 if (handler->var[0] == '*' && handler->var[1] == '.') {
513 lastcomponent_handler = strrchr(handler->var, '.');
514 if (lastcomponent_handler == NULL) {
515 lastcomponent_handler = handler->var;
517 lastcomponent_handler++;
520 lastcomponent_handler = handler->var;
523 /* Ignore this handler if they don't match. */
524 if (strcasecmp(lastcomponent_handler, cmd) != 0) {
528 if (handler->type == LC_VAR_NONE || handler->type == LC_VAR_BOOL_BY_EXISTANCE) {
532 /* We ignore errors from the environment variables,
533 they're mostly insignificant. */
534 lc_handle(handler, cmd, NULL, value, LC_FLAGS_ENVIRON);
546 static int lc_process_cmdline(int argc, char **argv) {
547 struct lc_varhandler_st *handler = NULL;
548 char *cmdarg = NULL, *cmdoptarg = NULL;
549 char *lastcomponent_handler = NULL;
550 char **newargv = NULL;
551 char *usedargv = NULL;
554 int retval = 0, chkretval = 0;
556 char *local_lc_errfile;
557 int local_lc_errline;
559 local_lc_errfile = "<cmdline>";
560 local_lc_errline = 0;
562 /* Allocate "argc + 1" (+1 for the NULL terminator) elements. */
563 newargv = malloc((argc + 1) * sizeof(*newargv));
564 if (newargv == NULL) {
565 lc_errfile = local_lc_errfile;
566 lc_errline = local_lc_errline;
567 lc_errno = LC_ERR_ENOMEM;
570 newargv[newargvidx++] = argv[0];
571 newargv[argc] = NULL;
573 /* Allocate space to indicate which arguments have been used. */
574 usedargv = malloc(argc * sizeof(*usedargv));
575 if (usedargv == NULL) {
576 lc_errfile = local_lc_errfile;
577 lc_errline = local_lc_errline;
578 lc_errno = LC_ERR_ENOMEM;
582 for (cmdargidx = 0; cmdargidx < argc; cmdargidx++) {
583 usedargv[cmdargidx] = 0;
586 for (cmdargidx = 1; cmdargidx < argc; cmdargidx++) {
587 cmdarg = argv[cmdargidx];
589 /* Make sure we have an argument here. */
590 if (cmdarg == NULL) {
594 /* If the argument isn't an option, skip. */
595 if (cmdarg[0] != '-') {
599 /* Setup a pointer in the new array for the actual argument. */
600 newargv[newargvidx++] = cmdarg;
601 usedargv[cmdargidx] = 1;
603 /* Then shift the argument past the '-' so we can ignore it. */
606 /* Handle long options. */
607 if (cmdarg[0] == '-') {
610 /* Don't process arguments after the '--' option. */
611 if (cmdarg[0] == '\0') {
615 /* Look for a variable name that matches */
616 for (handler = varhandlers; handler != NULL; handler = handler->_next) {
617 /* Skip handlers with no variable name. */
618 if (handler->var == NULL) {
621 /* Skip handlers which don't agree with being
622 processed on the command line. */
623 if (handler->type == LC_VAR_SECTION ||
624 handler->type == LC_VAR_SECTIONSTART ||
625 handler->type == LC_VAR_SECTIONEND ||
626 handler->type == LC_VAR_UNKNOWN) {
630 /* Find the last part of the variable and compare it with
631 the option being processed, if a wildcard is given. */
632 if (handler->var[0] == '*' && handler->var[1] == '.') {
633 lastcomponent_handler = strrchr(handler->var, '.');
634 if (lastcomponent_handler == NULL) {
635 lastcomponent_handler = handler->var;
637 lastcomponent_handler++;
640 /* Disallow use of the fully qualified name
641 since there was no sectionstart portion
642 we cannot allow it to handle children of it. */
643 if (strchr(cmdarg, '.') != NULL) {
646 lastcomponent_handler = handler->var;
649 /* Ignore this handler if they don't match. */
650 if (strcasecmp(lastcomponent_handler, cmdarg) != 0) {
654 if (handler->type == LC_VAR_NONE || handler->type == LC_VAR_BOOL_BY_EXISTANCE) {
658 if (cmdargidx >= argc) {
659 fprintf(stderr, "Argument required.\n");
660 lc_errfile = local_lc_errfile;
661 lc_errline = local_lc_errline;
662 lc_errno = LC_ERR_BADFORMAT;
667 cmdoptarg = argv[cmdargidx];
668 newargv[newargvidx++] = cmdoptarg;
669 usedargv[cmdargidx] = 1;
672 chkretval = lc_handle(handler, handler->var, NULL, cmdoptarg, LC_FLAGS_CMDLINE);
680 if (handler == NULL) {
681 fprintf(stderr, "Unknown option: --%s\n", cmdarg);
682 lc_errfile = local_lc_errfile;
683 lc_errline = local_lc_errline;
684 lc_errno = LC_ERR_INVCMD;
690 for (; *cmdarg != '\0'; cmdarg++) {
693 for (handler = varhandlers; handler != NULL; handler = handler->_next) {
694 if (handler->opt != ch || handler->opt == '\0') {
697 /* Skip handlers which don't agree with being
698 processed on the command line. */
699 if (handler->type == LC_VAR_SECTION ||
700 handler->type == LC_VAR_SECTIONSTART ||
701 handler->type == LC_VAR_SECTIONEND ||
702 handler->type == LC_VAR_UNKNOWN) {
706 if (handler->type == LC_VAR_NONE || handler->type == LC_VAR_BOOL_BY_EXISTANCE) {
710 if (cmdargidx >= argc) {
711 fprintf(stderr, "Argument required.\n");
712 lc_errfile = local_lc_errfile;
713 lc_errline = local_lc_errline;
714 lc_errno = LC_ERR_BADFORMAT;
719 cmdoptarg = argv[cmdargidx];
720 newargv[newargvidx++] = cmdoptarg;
721 usedargv[cmdargidx] = 1;
724 chkretval = lc_handle(handler, handler->var, NULL, cmdoptarg, LC_FLAGS_CMDLINE);
726 lc_errfile = local_lc_errfile;
727 lc_errline = local_lc_errline;
734 if (handler == NULL) {
735 fprintf(stderr, "Unknown option: -%c\n", ch);
736 lc_errfile = local_lc_errfile;
737 lc_errline = local_lc_errline;
738 lc_errno = LC_ERR_INVCMD;
748 lc_optind = newargvidx;
749 for (cmdargidx = 1; cmdargidx < argc; cmdargidx++) {
750 if (usedargv[cmdargidx] != 0) {
754 cmdarg = argv[cmdargidx];
756 newargv[newargvidx++] = cmdarg;
758 for (cmdargidx = 1; cmdargidx < argc; cmdargidx++) {
759 argv[cmdargidx] = newargv[cmdargidx];
770 int lc_process_var(const char *var, const char *varargs, const char *value, lc_flags_t flags) {
771 struct lc_varhandler_st *handler = NULL;
772 const char *lastcomponent_handler = NULL, *lastcomponent_var = NULL;
774 lastcomponent_var = strrchr(var, '.');
775 if (lastcomponent_var == NULL) {
776 lastcomponent_var = var;
781 for (handler = varhandlers; handler != NULL; handler = handler->_next) {
782 /* If either handler->var or var is NULL, skip, unless both are NULL. */
783 if (handler->var != var && (handler->var == NULL || var == NULL)) {
787 /* If both are not-NULL, compare them. */
788 if (handler->var != NULL) {
789 /* Wild-card-ish match. */
790 if (handler->var[0] == '*' && handler->var[1] == '.') {
791 /* Only compare the last components */
793 lastcomponent_handler = strrchr(handler->var, '.') + 1; /* strrchr() won't return NULL, because we already checked it. */
795 if (strcasecmp(lastcomponent_handler, lastcomponent_var) != 0) {
798 } else if (strcasecmp(handler->var, var) != 0) {
799 /* Exact (case-insensitive comparison) failed. */
805 handler->type != LC_VAR_NONE &&
806 handler->type != LC_VAR_BOOL_BY_EXISTANCE &&
807 handler->type != LC_VAR_SECTION &&
808 handler->type != LC_VAR_SECTIONSTART &&
809 handler->type != LC_VAR_SECTIONEND) {
810 lc_errno = LC_ERR_BADFORMAT;
814 return(lc_handle(handler, var, varargs, value, flags));
820 int lc_register_callback(const char *var, char opt, lc_var_type_t type, int (*callback)(const char *, const char *, const char *, const char *, lc_flags_t, void *), void *extra) {
821 struct lc_varhandler_st *newhandler = NULL;
823 newhandler = malloc(sizeof(*newhandler));
825 if (newhandler == NULL) {
830 newhandler->var = NULL;
832 newhandler->var = strdup(var);
834 newhandler->mode = LC_MODE_CALLBACK;
835 newhandler->type = type;
836 newhandler->callback = callback;
837 newhandler->opt = opt;
838 newhandler->extra = extra;
839 newhandler->_next = varhandlers;
841 varhandlers = newhandler;
846 int lc_register_var(const char *var, lc_var_type_t type, void *data, char opt) {
847 struct lc_varhandler_st *newhandler = NULL;
849 newhandler = malloc(sizeof(*newhandler));
851 if (newhandler == NULL) {
856 newhandler->var = NULL;
858 newhandler->var = strdup(var);
860 newhandler->mode = LC_MODE_VAR;
861 newhandler->type = type;
862 newhandler->data = data;
863 newhandler->opt = opt;
864 newhandler->extra = NULL;
865 newhandler->_next = varhandlers;
867 varhandlers = newhandler;
872 int lc_process_file(const char *appname, const char *pathname, lc_conf_type_t type) {
876 case LC_CONF_SECTION:
877 chkretval = lc_process_conf_section(appname, pathname);
880 chkretval = lc_process_conf_apache(appname, pathname);
883 chkretval = lc_process_conf_colon(appname, pathname);
886 chkretval = lc_process_conf_equal(appname, pathname);
889 chkretval = lc_process_conf_space(appname, pathname);
892 chkretval = lc_process_conf_xml(appname, pathname);
896 lc_errno = LC_ERR_INVDATA;
903 static int lc_process_files(const char *appname, lc_conf_type_t type, const char *extraconfig) {
905 struct passwd *pwinfo = NULL;
907 char configfiles[3][13][512] = {{{0}}};
908 char *configfile = NULL;
909 char *homedir = NULL;
910 int configsetidx = 0, configidx = 0;
911 int chkretval = 0, retval = 0;
913 if (extraconfig != NULL) {
914 snprintf(configfiles[0][0], sizeof(**configfiles) - 1, "%s", extraconfig);
916 snprintf(configfiles[1][0], sizeof(**configfiles) - 1, "/etc/%s.cfg", appname);
917 snprintf(configfiles[1][1], sizeof(**configfiles) - 1, "/etc/%s.conf", appname);
918 snprintf(configfiles[1][2], sizeof(**configfiles) - 1, "/etc/%s/%s.cfg", appname, appname);
919 snprintf(configfiles[1][3], sizeof(**configfiles) - 1, "/etc/%s/%s.conf", appname, appname);
920 snprintf(configfiles[1][4], sizeof(**configfiles) - 1, "/usr/etc/%s.cfg", appname);
921 snprintf(configfiles[1][5], sizeof(**configfiles) - 1, "/usr/etc/%s.conf", appname);
922 snprintf(configfiles[1][6], sizeof(**configfiles) - 1, "/usr/etc/%s/%s.cfg", appname, appname);
923 snprintf(configfiles[1][7], sizeof(**configfiles) - 1, "/usr/etc/%s/%s.conf", appname, appname);
924 snprintf(configfiles[1][8], sizeof(**configfiles) - 1, "/usr/local/etc/%s.cfg", appname);
925 snprintf(configfiles[1][9], sizeof(**configfiles) - 1, "/usr/local/etc/%s.conf", appname);
926 snprintf(configfiles[1][10], sizeof(**configfiles) - 1, "/usr/local/etc/%s/%s.cfg", appname, appname);
927 snprintf(configfiles[1][11], sizeof(**configfiles) - 1, "/usr/local/etc/%s/%s.conf", appname, appname);
929 homedir = getenv("HOME");
931 if (homedir == NULL) {
932 pwinfo = getpwuid(getuid());
933 if (pwinfo != NULL) {
934 homedir = pwinfo->pw_dir;
938 if (homedir != NULL) {
939 if (strcmp(homedir, "/") != 0 && access(homedir, R_OK|W_OK|X_OK) == 0) {
940 snprintf(configfiles[2][0], sizeof(**configfiles) - 1, "%s/.%src", homedir, appname);
941 snprintf(configfiles[2][1], sizeof(**configfiles) - 1, "%s/.%s.cfg", homedir, appname);
942 snprintf(configfiles[2][2], sizeof(**configfiles) - 1, "%s/.%s.conf", homedir, appname);
943 snprintf(configfiles[2][3], sizeof(**configfiles) - 1, "%s/.%s/%s.cfg", homedir, appname, appname);
944 snprintf(configfiles[2][4], sizeof(**configfiles) - 1, "%s/.%s/%s.conf", homedir, appname, appname);
945 snprintf(configfiles[2][5], sizeof(**configfiles) - 1, "%s/.%s/config", homedir, appname);
950 for (configsetidx = 0; configsetidx < 3; configsetidx++) {
951 for (configidx = 0; configidx < 13; configidx++) {
952 configfile = configfiles[configsetidx][configidx];
953 if (configfile[0] == '\0') {
956 if (access(configfile, R_OK) == 0) {
957 chkretval = lc_process_file(appname, configfile, type);
969 void lc_cleanup(void) {
970 struct lc_varhandler_st *handler = NULL, *next = NULL;
972 handler = varhandlers;
973 while (handler != NULL) {
974 if (handler->var != NULL) {
978 next = handler->_next;
990 int lc_process(int argc, char **argv, const char *appname, lc_conf_type_t type, const char *extra) {
991 int retval = 0, chkretval = 0;
993 /* Handle config files. */
994 chkretval = lc_process_files(appname, type, extra);
999 /* Handle environment variables.*/
1000 chkretval = lc_process_environment(appname);
1001 if (chkretval < 0) {
1005 /* Handle command line arguments */
1006 chkretval = lc_process_cmdline(argc, argv);
1007 if (chkretval < 0) {
1015 lc_err_t lc_geterrno(void) {
1020 lc_errno = LC_ERR_NONE;
1025 char *lc_geterrstr(void) {
1026 static char retval[512];
1027 char *errmsg = NULL;
1034 errmsg = "Invalid command or option";
1036 case LC_ERR_INVSECTION:
1037 errmsg = "Invalid section";
1039 case LC_ERR_INVDATA:
1040 errmsg = "Invalid application data (internal error)";
1042 case LC_ERR_BADFORMAT:
1043 errmsg = "Bad data specified or incorrect format.";
1045 case LC_ERR_CANTOPEN:
1046 errmsg = "Can't open file.";
1048 case LC_ERR_CALLBACK:
1049 errmsg = "Error return from application handler.";
1052 errmsg = "Insuffcient memory.";
1057 * This is not part of the switch statement so we will get warnings
1058 * about unhandled enum values.
1060 if (errmsg == NULL) {
1061 errmsg = "Unknown error";
1064 if (lc_errfile == NULL) {
1065 snprintf(retval, sizeof(retval), "%s:%i: %s", "<no file>", lc_errline, errmsg);
1067 snprintf(retval, sizeof(retval), "%s:%i: %s", lc_errfile, lc_errline, errmsg);
1070 retval[sizeof(retval) - 1] = '\0';