Introduce PTR_SUB
[rsync.git] / loadparm.c
blob3906bc0f419faea3c4d4d666ce77fa560b5c3a60
1 /*
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
22 /* Load parameters.
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.
28 * To add a parameter:
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
35 * Notes:
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,
40 * so be careful!
43 #include "rsync.h"
44 #include "itypes.h"
45 #include "ifuncs.h"
46 #include "default-dont-compress.h"
48 extern item_list dparam_list;
50 #define strequal(a, b) (strcasecmp(a, b)==0)
52 #ifndef LOG_DAEMON
53 #define LOG_DAEMON 0
54 #endif
56 /* the following are used by loadparm for option lists */
57 typedef enum {
58 P_BOOL, P_BOOLREV, P_BOOL3, P_CHAR, P_INTEGER,
59 P_OCTAL, P_PATH, P_STRING, P_ENUM
60 } parm_type;
62 typedef enum {
63 P_LOCAL, P_GLOBAL, P_NONE
64 } parm_class;
66 struct enum_list {
67 int value;
68 char *name;
71 struct parm_struct {
72 char *label;
73 parm_type type;
74 parm_class class;
75 void *ptr;
76 struct enum_list *enum_list;
77 unsigned flags;
80 #ifndef GLOBAL_NAME
81 #define GLOBAL_NAME "global"
82 #endif
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[] = {
99 #ifdef LOG_AUTH
100 { LOG_AUTH, "auth" },
101 #endif
102 #ifdef LOG_AUTHPRIV
103 { LOG_AUTHPRIV, "authpriv" },
104 #endif
105 #ifdef LOG_CRON
106 { LOG_CRON, "cron" },
107 #endif
108 #ifdef LOG_DAEMON
109 { LOG_DAEMON, "daemon" },
110 #endif
111 #ifdef LOG_FTP
112 { LOG_FTP, "ftp" },
113 #endif
114 #ifdef LOG_KERN
115 { LOG_KERN, "kern" },
116 #endif
117 #ifdef LOG_LPR
118 { LOG_LPR, "lpr" },
119 #endif
120 #ifdef LOG_MAIL
121 { LOG_MAIL, "mail" },
122 #endif
123 #ifdef LOG_NEWS
124 { LOG_NEWS, "news" },
125 #endif
126 #ifdef LOG_AUTH
127 { LOG_AUTH, "security" },
128 #endif
129 #ifdef LOG_SYSLOG
130 { LOG_SYSLOG, "syslog" },
131 #endif
132 #ifdef LOG_USER
133 { LOG_USER, "user" },
134 #endif
135 #ifdef LOG_UUCP
136 { LOG_UUCP, "uucp" },
137 #endif
138 #ifdef LOG_LOCAL0
139 { LOG_LOCAL0, "local0" },
140 #endif
141 #ifdef LOG_LOCAL1
142 { LOG_LOCAL1, "local1" },
143 #endif
144 #ifdef LOG_LOCAL2
145 { LOG_LOCAL2, "local2" },
146 #endif
147 #ifdef LOG_LOCAL3
148 { LOG_LOCAL3, "local3" },
149 #endif
150 #ifdef LOG_LOCAL4
151 { LOG_LOCAL4, "local4" },
152 #endif
153 #ifdef LOG_LOCAL5
154 { LOG_LOCAL5, "local5" },
155 #endif
156 #ifdef LOG_LOCAL6
157 { LOG_LOCAL6, "local6" },
158 #endif
159 #ifdef LOG_LOCAL7
160 { LOG_LOCAL7, "local7" },
161 #endif
162 { -1, NULL }
165 /* Expand %VAR% references. Any unknown vars or unrecognized
166 * syntax leaves the raw chars unchanged. */
167 static char *expand_vars(const char *str)
169 char *buf, *t;
170 const char *f;
171 int bufsize;
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) {
183 char *val;
184 strlcpy(t, f+1, percent - f);
185 val = getenv(t);
186 if (val) {
187 int len = strlcpy(t, val, bufsize+1);
188 if (len > bufsize)
189 break;
190 bufsize -= len;
191 t += len;
192 f = percent + 1;
193 continue;
197 *t++ = *f++;
198 bufsize--;
200 *t = '\0';
202 if (*f) {
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");
210 return buf;
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. */
285 if (psz1 == psz2)
286 return 1;
287 if (psz1 == NULL || psz2 == NULL)
288 return 0;
290 /* sync the strings on first non-whitespace */
291 while (1) {
292 while (isSpace(psz1))
293 psz1++;
294 while (isSpace(psz2))
295 psz2++;
296 if (*psz1 == '\0' || *psz2 == '\0')
297 break;
298 if (toUpper(psz1) != toUpper(psz2))
299 break;
300 psz1++;
301 psz2++;
303 return *psz1 == *psz2;
306 /* Find a section by name. Otherwise works like get_section. */
307 static int getsectionbyname(char *name)
309 int i;
311 for (i = section_list.count - 1; i >= 0; i--) {
312 if (strwiEQ(iSECTION(i).name, name))
313 break;
316 return i;
319 /* Add a new section to the sections array w/the default values. */
320 static int add_a_section(char *name)
322 int i;
323 local_vars *s;
325 /* it might already exist */
326 if (name) {
327 i = getsectionbyname(name);
328 if (i >= 0)
329 return i;
332 i = section_list.count;
333 s = EXPAND_ITEM_LIST(&section_list, local_vars, 2);
335 init_section(s);
336 if (name)
337 string_set(&s->name, name);
339 return i;
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)
346 int iIndex;
348 if (*parmname == '-')
349 return -1;
351 for (iIndex = 0; parm_table[iIndex].label; iIndex++) {
352 if (strwiEQ(parm_table[iIndex].label, parmname))
353 return iIndex;
356 rprintf(FLOG, "Unknown Parameter encountered: \"%s\"\n", parmname);
357 return -1;
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"))
366 *pb = True;
367 else if (strwiEQ(parmvalue, "no") || strwiEQ(parmvalue, "false") || strwiEQ(parmvalue, "0"))
368 *pb = False;
369 else if (allow_unset && (strwiEQ(parmvalue, "unset") || strwiEQ(parmvalue, "-1")))
370 *pb = Unset;
371 else {
372 rprintf(FLOG, "Badly formed boolean in configuration file: \"%s\".\n", parmvalue);
373 return False;
375 return True;
378 /* Process a parameter. */
379 static BOOL do_parameter(char *parmname, char *parmvalue)
381 int parmnum, i;
382 void *parm_ptr; /* where we are going to store the result */
383 void *def_ptr;
384 char *cp;
386 parmnum = map_parameter(parmname);
388 if (parmnum < 0) {
389 rprintf(FLOG, "IGNORING unknown parameter \"%s\"\n", parmname);
390 return True;
393 def_ptr = parm_table[parmnum].ptr;
395 if (bInGlobalSection)
396 parm_ptr = def_ptr;
397 else {
398 if (parm_table[parmnum].class == P_GLOBAL) {
399 rprintf(FLOG, "Global parameter %s found in module section!\n", parmname);
400 return True;
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) {
407 case P_PATH:
408 case P_STRING:
409 /* delay expansion of %VAR% strings */
410 break;
411 default:
412 /* expand any %VAR% strings now */
413 parmvalue = expand_vars(parmvalue);
414 break;
417 switch (parm_table[parmnum].type) {
418 case P_BOOL:
419 set_boolean(parm_ptr, parmvalue, False);
420 break;
422 case P_BOOL3:
423 set_boolean(parm_ptr, parmvalue, True);
424 break;
426 case P_BOOLREV:
427 set_boolean(parm_ptr, parmvalue, False);
428 *(BOOL *)parm_ptr = ! *(BOOL *)parm_ptr;
429 break;
431 case P_INTEGER:
432 *(int *)parm_ptr = atoi(parmvalue);
433 break;
435 case P_CHAR:
436 *(char *)parm_ptr = *parmvalue;
437 break;
439 case P_OCTAL:
440 sscanf(parmvalue, "%o", (unsigned int *)parm_ptr);
441 break;
443 case P_PATH:
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--;
448 cp[len] = '\0';
450 break;
452 case P_STRING:
453 string_set(parm_ptr, parmvalue);
454 break;
456 case P_ENUM:
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;
460 break;
463 if (!parm_table[parmnum].enum_list[i].name) {
464 if (atoi(parmvalue) > 0)
465 *(int *)parm_ptr = atoi(parmvalue);
467 break;
470 return True;
473 /* Process a new section (rsync module).
474 * Returns True on success, False on failure. */
475 static BOOL do_section(char *sectionname)
477 BOOL isglobal;
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)
488 return False;
489 memcpy(&Vars, vp, sizeof Vars);
490 if (sectionname[1] == 'p')
491 Vars_stack.count--;
492 } else
493 return False;
494 return True;
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)
502 set_dparams(0);
505 /* if we've just struck a global section, note the fact. */
506 bInGlobalSection = isglobal;
508 /* check for multiple global sections */
509 if (bInGlobalSection)
510 return True;
512 #if 0
513 /* If we have a current section, tidy it up before moving on. */
514 if (iSectionIndex >= 0) {
515 /* Add any tidy work as needed ... */
516 if (problem)
517 return False;
519 #endif
521 if (strchr(sectionname, '/') != NULL) {
522 rprintf(FLOG, "Warning: invalid section name in configuration file: %s\n", sectionname);
523 return False;
526 if ((iSectionIndex = add_a_section(sectionname)) < 0) {
527 rprintf(FLOG, "Failed to add a new module\n");
528 bInGlobalSection = True;
529 return False;
532 return 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;
541 reset_daemon_vars();
543 /* We get sections first, so have to start 'behind' to make up. */
544 iSectionIndex = -1;
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;
551 unsigned j;
553 for (j = 0; j < dparam_list.count; j++) {
554 equal = strchr(params[j], '='); /* options.c verified this */
555 *equal = '\0';
556 if (syntax_check_only) {
557 if (map_parameter(params[j]) < 0) {
558 rprintf(FERROR, "Unknown parameter \"%s\"\n", params[j]);
559 *equal = '=';
560 return False;
562 } else {
563 for (val = equal+1; isSpace(val); val++) {}
564 do_parameter(params[j], val);
566 *equal = '=';
569 return True;
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)
584 int i;
586 for (i = section_list.count - 1; i >= 0; i--) {
587 if (strcmp(lp_name(i), name) == 0)
588 break;
591 return i;