2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 3 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License along
13 * with this program; if not, visit the http://fsf.org website.
15 * This is based on loadparm.c from Samba, written by Andrew Tridgell
16 * and Karl Auer. Some of the changes are:
18 * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
19 * Copyright (C) 2003-2020 Wayne Davison
24 * This module provides suitable callback functions for the params
25 * module. It builds the internal table of section details which is
26 * then used by the rest of the server.
30 * 1) add it to the global_vars or local_vars structure definition
31 * 2) add it to the parm_table
32 * 3) add it to the list of available functions (eg: using FN_GLOBAL_STRING())
33 * 4) initialise it in the Defaults static structure
36 * The configuration file is processed sequentially for speed. For this
37 * reason, there is a fair bit of sequence-dependent code here - ie., code
38 * which assumes that certain things happen before others. In particular, the
39 * code which happens at the boundary between sections is delicately poised,
46 #include "default-dont-compress.h"
48 extern item_list dparam_list;
50 #define strequal(a, b) (strcasecmp(a, b)==0)
56 /* the following are used by loadparm for option lists */
58 P_BOOL, P_BOOLREV, P_BOOL3, P_CHAR, P_INTEGER,
59 P_OCTAL, P_PATH, P_STRING, P_ENUM
63 P_LOCAL, P_GLOBAL, P_NONE
76 struct enum_list *enum_list;
81 #define GLOBAL_NAME "global"
84 /* some helpful bits */
85 #define iSECTION(i) ((local_vars*)section_list.items)[i]
86 #define LP_SNUM_OK(i) ((i) >= 0 && (i) < (int)section_list.count)
87 #define SECTION_PTR(s, p) (((char*)(s)) + (ptrdiff_t)(((char*)(p))-(char*)&Vars.l))
89 /* Stack of "Vars" values used by the &include directive. */
90 static item_list Vars_stack = EMPTY_ITEM_LIST;
92 /* The array of section values that holds all the defined modules. */
93 static item_list section_list = EMPTY_ITEM_LIST;
95 static int iSectionIndex = -1;
96 static BOOL bInGlobalSection = True;
98 static struct enum_list enum_syslog_facility[] = {
100 { LOG_AUTH, "auth" },
103 { LOG_AUTHPRIV, "authpriv" },
106 { LOG_CRON, "cron" },
109 { LOG_DAEMON, "daemon" },
115 { LOG_KERN, "kern" },
121 { LOG_MAIL, "mail" },
124 { LOG_NEWS, "news" },
127 { LOG_AUTH, "security" },
130 { LOG_SYSLOG, "syslog" },
133 { LOG_USER, "user" },
136 { LOG_UUCP, "uucp" },
139 { LOG_LOCAL0, "local0" },
142 { LOG_LOCAL1, "local1" },
145 { LOG_LOCAL2, "local2" },
148 { LOG_LOCAL3, "local3" },
151 { LOG_LOCAL4, "local4" },
154 { LOG_LOCAL5, "local5" },
157 { LOG_LOCAL6, "local6" },
160 { LOG_LOCAL7, "local7" },
165 /* Expand %VAR% references. Any unknown vars or unrecognized
166 * syntax leaves the raw chars unchanged. */
167 static char *expand_vars(const char *str)
173 if (!str || !strchr(str, '%'))
174 return (char *)str; /* TODO change return value to const char* at some point. */
176 bufsize = strlen(str) + 2048;
177 buf = new_array(char, bufsize+1); /* +1 for trailing '\0' */
179 for (t = buf, f = str; bufsize && *f; ) {
180 if (*f == '%' && isUpper(f+1)) {
181 char *percent = strchr(f+1, '%');
182 if (percent && percent - f < bufsize) {
184 strlcpy(t, f+1, percent - f);
187 int len = strlcpy(t, val, bufsize+1);
203 rprintf(FLOG, "Overflowed buf in expand_vars() trying to expand: %s\n", str);
204 exit_cleanup(RERR_MALLOC);
207 if (bufsize && (buf = realloc(buf, t - buf + 1)) == NULL)
208 out_of_memory("expand_vars");
213 /* Each "char* foo" has an associated "BOOL foo_EXP" that tracks if the string has been expanded yet or not. */
215 /* NOTE: use this function and all the FN_{GLOBAL,LOCAL} ones WITHOUT a trailing semicolon! */
216 #define RETURN_EXPANDED(val) {if (!val ## _EXP) {val = expand_vars(val); val ## _EXP = True;} return val ? val : "";}
218 /* In this section all the functions that are used to access the
219 * parameters from the rest of the program are defined. */
221 #define FN_GLOBAL_STRING(fn_name, val) \
222 char *fn_name(void) RETURN_EXPANDED(Vars.g.val)
223 #define FN_GLOBAL_BOOL(fn_name, val) \
224 BOOL fn_name(void) {return Vars.g.val;}
225 #define FN_GLOBAL_CHAR(fn_name, val) \
226 char fn_name(void) {return Vars.g.val;}
227 #define FN_GLOBAL_INTEGER(fn_name, val) \
228 int fn_name(void) {return Vars.g.val;}
230 #define FN_LOCAL_STRING(fn_name, val) \
231 char *fn_name(int i) {if (LP_SNUM_OK(i) && iSECTION(i).val) RETURN_EXPANDED(iSECTION(i).val) else RETURN_EXPANDED(Vars.l.val)}
232 #define FN_LOCAL_BOOL(fn_name, val) \
233 BOOL fn_name(int i) {return LP_SNUM_OK(i)? iSECTION(i).val : Vars.l.val;}
234 #define FN_LOCAL_CHAR(fn_name, val) \
235 char fn_name(int i) {return LP_SNUM_OK(i)? iSECTION(i).val : Vars.l.val;}
236 #define FN_LOCAL_INTEGER(fn_name, val) \
237 int fn_name(int i) {return LP_SNUM_OK(i)? iSECTION(i).val : Vars.l.val;}
239 /* The following include file contains:
241 * typedef global_vars - describes global (ie., server-wide) parameters.
242 * typedef local_vars - describes a single section.
243 * typedef all_vars - a combination of global_vars & local_vars.
244 * all_vars Defaults - the default values for all the variables.
245 * all_vars Vars - the currently configured values for all the variables.
246 * struct parm_struct parm_table - the strings & variables for the parser.
247 * FN_{LOCAL,GLOBAL}_{TYPE}() definition for all the lp_var_name() accessors.
250 #include "daemon-parm.h"
252 /* Initialise the Default all_vars structure. */
253 void reset_daemon_vars(void)
255 memcpy(&Vars, &Defaults, sizeof Vars);
258 /* Assign a copy of v to *s. Handles NULL strings. We don't worry
259 * about overwriting a malloc'd string because the long-running
260 * (port-listening) daemon only loads the config file once, and the
261 * per-job (forked or xinitd-ran) daemon only re-reads the file at
262 * the start, so any lost memory is inconsequential. */
263 static inline void string_set(char **s, const char *v)
265 *s = v ? strdup(v) : NULL;
268 /* Copy local_vars into a new section. No need to strdup since we don't free. */
269 static void copy_section(local_vars *psectionDest, local_vars *psectionSource)
271 memcpy(psectionDest, psectionSource, sizeof psectionDest[0]);
274 /* Initialise a section to the defaults. */
275 static void init_section(local_vars *psection)
277 memset(psection, 0, sizeof (local_vars));
278 copy_section(psection, &Vars.l);
281 /* Do a case-insensitive, whitespace-ignoring string equality check. */
282 static int strwiEQ(char *psz1, char *psz2)
284 /* If one or both strings are NULL, we return equality right away. */
287 if (psz1 == NULL || psz2 == NULL)
290 /* sync the strings on first non-whitespace */
292 while (isSpace(psz1))
294 while (isSpace(psz2))
296 if (*psz1 == '\0' || *psz2 == '\0')
298 if (toUpper(psz1) != toUpper(psz2))
303 return *psz1 == *psz2;
306 /* Find a section by name. Otherwise works like get_section. */
307 static int getsectionbyname(char *name)
311 for (i = section_list.count - 1; i >= 0; i--) {
312 if (strwiEQ(iSECTION(i).name, name))
319 /* Add a new section to the sections array w/the default values. */
320 static int add_a_section(char *name)
325 /* it might already exist */
327 i = getsectionbyname(name);
332 i = section_list.count;
333 s = EXPAND_ITEM_LIST(§ion_list, local_vars, 2);
337 string_set(&s->name, name);
342 /* Map a parameter's string representation to something we can use.
343 * Returns False if the parameter string is not recognised, else TRUE. */
344 static int map_parameter(char *parmname)
348 if (*parmname == '-')
351 for (iIndex = 0; parm_table[iIndex].label; iIndex++) {
352 if (strwiEQ(parm_table[iIndex].label, parmname))
356 rprintf(FLOG, "Unknown Parameter encountered: \"%s\"\n", parmname);
360 /* Set a boolean variable from the text value stored in the passed string.
361 * Returns True in success, False if the passed string does not correctly
362 * represent a boolean. */
363 static BOOL set_boolean(BOOL *pb, char *parmvalue, int allow_unset)
365 if (strwiEQ(parmvalue, "yes") || strwiEQ(parmvalue, "true") || strwiEQ(parmvalue, "1"))
367 else if (strwiEQ(parmvalue, "no") || strwiEQ(parmvalue, "false") || strwiEQ(parmvalue, "0"))
369 else if (allow_unset && (strwiEQ(parmvalue, "unset") || strwiEQ(parmvalue, "-1")))
372 rprintf(FLOG, "Badly formed boolean in configuration file: \"%s\".\n", parmvalue);
378 /* Process a parameter. */
379 static BOOL do_parameter(char *parmname, char *parmvalue)
382 void *parm_ptr; /* where we are going to store the result */
386 parmnum = map_parameter(parmname);
389 rprintf(FLOG, "IGNORING unknown parameter \"%s\"\n", parmname);
393 def_ptr = parm_table[parmnum].ptr;
395 if (bInGlobalSection)
398 if (parm_table[parmnum].class == P_GLOBAL) {
399 rprintf(FLOG, "Global parameter %s found in module section!\n", parmname);
402 parm_ptr = SECTION_PTR(&iSECTION(iSectionIndex), def_ptr);
405 /* now switch on the type of variable it is */
406 switch (parm_table[parmnum].type) {
409 /* delay expansion of %VAR% strings */
412 /* expand any %VAR% strings now */
413 parmvalue = expand_vars(parmvalue);
417 switch (parm_table[parmnum].type) {
419 set_boolean(parm_ptr, parmvalue, False);
423 set_boolean(parm_ptr, parmvalue, True);
427 set_boolean(parm_ptr, parmvalue, False);
428 *(BOOL *)parm_ptr = ! *(BOOL *)parm_ptr;
432 *(int *)parm_ptr = atoi(parmvalue);
436 *(char *)parm_ptr = *parmvalue;
440 sscanf(parmvalue, "%o", (unsigned int *)parm_ptr);
444 string_set(parm_ptr, parmvalue);
445 if ((cp = *(char**)parm_ptr) != NULL) {
446 int len = strlen(cp);
447 while (len > 1 && cp[len-1] == '/') len--;
453 string_set(parm_ptr, parmvalue);
457 for (i=0; parm_table[parmnum].enum_list[i].name; i++) {
458 if (strequal(parmvalue, parm_table[parmnum].enum_list[i].name)) {
459 *(int *)parm_ptr = parm_table[parmnum].enum_list[i].value;
463 if (!parm_table[parmnum].enum_list[i].name) {
464 if (atoi(parmvalue) > 0)
465 *(int *)parm_ptr = atoi(parmvalue);
473 /* Process a new section (rsync module).
474 * Returns True on success, False on failure. */
475 static BOOL do_section(char *sectionname)
479 if (*sectionname == ']') { /* A special push/pop/reset directive from params.c */
480 bInGlobalSection = 1;
481 if (strcmp(sectionname+1, "push") == 0) {
482 all_vars *vp = EXPAND_ITEM_LIST(&Vars_stack, all_vars, 2);
483 memcpy(vp, &Vars, sizeof Vars);
484 } else if (strcmp(sectionname+1, "pop") == 0
485 || strcmp(sectionname+1, "reset") == 0) {
486 all_vars *vp = ((all_vars*)Vars_stack.items) + Vars_stack.count - 1;
487 if (!Vars_stack.count)
489 memcpy(&Vars, vp, sizeof Vars);
490 if (sectionname[1] == 'p')
497 isglobal = strwiEQ(sectionname, GLOBAL_NAME);
499 /* At the end of the global section, add any --dparam items. */
500 if (bInGlobalSection && !isglobal) {
501 if (!section_list.count)
505 /* if we've just struck a global section, note the fact. */
506 bInGlobalSection = isglobal;
508 /* check for multiple global sections */
509 if (bInGlobalSection)
513 /* If we have a current section, tidy it up before moving on. */
514 if (iSectionIndex >= 0) {
515 /* Add any tidy work as needed ... */
521 if (strchr(sectionname, '/') != NULL) {
522 rprintf(FLOG, "Warning: invalid section name in configuration file: %s\n", sectionname);
526 if ((iSectionIndex = add_a_section(sectionname)) < 0) {
527 rprintf(FLOG, "Failed to add a new module\n");
528 bInGlobalSection = True;
535 /* Load the modules from the config file. Return True on success,
536 * False on failure. */
537 int lp_load(char *pszFname, int globals_only)
539 bInGlobalSection = True;
543 /* We get sections first, so have to start 'behind' to make up. */
545 return pm_process(pszFname, globals_only ? NULL : do_section, do_parameter);
548 BOOL set_dparams(int syntax_check_only)
550 char *equal, *val, **params = dparam_list.items;
553 for (j = 0; j < dparam_list.count; j++) {
554 equal = strchr(params[j], '='); /* options.c verified this */
556 if (syntax_check_only) {
557 if (map_parameter(params[j]) < 0) {
558 rprintf(FERROR, "Unknown parameter \"%s\"\n", params[j]);
563 for (val = equal+1; isSpace(val); val++) {}
564 do_parameter(params[j], val);
572 /* Return the max number of modules (sections). */
573 int lp_num_modules(void)
575 return section_list.count;
578 /* Return the number of the module with the given name, or -1 if it doesn't
579 * exist. Note that this is a DIFFERENT ANIMAL from the internal function
580 * getsectionbyname()! This works ONLY if all sections have been loaded,
581 * and does not copy the found section. */
582 int lp_number(char *name)
586 for (i = section_list.count - 1; i >= 0; i--) {
587 if (strcmp(lp_name(i), name) == 0)