Commit | Line | Data |
---|---|---|
0b76cd63 AT |
1 | /* This is based on loadparm.c from Samba, written by Andrew Tridgell |
2 | and Karl Auer */ | |
3 | ||
4 | /* | |
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 2 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program; if not, write to the Free Software | |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 | */ | |
19 | ||
20 | /* | |
21 | * Load parameters. | |
22 | * | |
23 | * This module provides suitable callback functions for the params | |
24 | * module. It builds the internal table of service details which is | |
25 | * then used by the rest of the server. | |
26 | * | |
27 | * To add a parameter: | |
28 | * | |
29 | * 1) add it to the global or service structure definition | |
30 | * 2) add it to the parm_table | |
31 | * 3) add it to the list of available functions (eg: using FN_GLOBAL_STRING()) | |
32 | * 4) If it's a global then initialise it in init_globals. If a local | |
33 | * (ie. service) parameter then initialise it in the sDefault structure | |
34 | * | |
35 | * | |
36 | * Notes: | |
37 | * The configuration file is processed sequentially for speed. It is NOT | |
38 | * accessed randomly as happens in 'real' Windows. For this reason, there | |
39 | * is a fair bit of sequence-dependent code here - ie., code which assumes | |
40 | * that certain things happen before others. In particular, the code which | |
41 | * happens at the boundary between sections is delicately poised, so be | |
42 | * careful! | |
43 | * | |
44 | */ | |
45 | ||
46 | #include "rsync.h" | |
0b76cd63 AT |
47 | #define PTR_DIFF(p1,p2) ((ptrdiff_t)(((char *)(p1)) - (char *)(p2))) |
48 | #define strequal(a,b) (strcasecmp(a,b)==0) | |
49 | #define BOOLSTR(b) ((b) ? "Yes" : "No") | |
50 | typedef char pstring[1024]; | |
37f9805d | 51 | #define pstrcpy(a,b) strlcpy(a,b,sizeof(pstring)) |
0b76cd63 AT |
52 | |
53 | /* the following are used by loadparm for option lists */ | |
54 | typedef enum | |
55 | { | |
31593dd6 AT |
56 | P_BOOL,P_BOOLREV,P_CHAR,P_INTEGER,P_OCTAL, |
57 | P_STRING,P_GSTRING,P_ENUM,P_SEP | |
0b76cd63 AT |
58 | } parm_type; |
59 | ||
60 | typedef enum | |
61 | { | |
31593dd6 | 62 | P_LOCAL,P_GLOBAL,P_SEPARATOR,P_NONE |
0b76cd63 AT |
63 | } parm_class; |
64 | ||
65 | struct enum_list { | |
66 | int value; | |
67 | char *name; | |
68 | }; | |
69 | ||
70 | struct parm_struct | |
71 | { | |
72 | char *label; | |
73 | parm_type type; | |
74 | parm_class class; | |
75 | void *ptr; | |
76 | struct enum_list *enum_list; | |
77 | unsigned flags; | |
78 | }; | |
79 | ||
80 | static BOOL bLoaded = False; | |
81 | ||
82 | #ifndef GLOBAL_NAME | |
83 | #define GLOBAL_NAME "global" | |
84 | #endif | |
85 | ||
86 | /* some helpful bits */ | |
87 | #define pSERVICE(i) ServicePtrs[i] | |
88 | #define iSERVICE(i) (*pSERVICE(i)) | |
89 | #define LP_SNUM_OK(iService) (((iService) >= 0) && ((iService) < iNumServices)) | |
90 | ||
91 | /* | |
92 | * This structure describes global (ie., server-wide) parameters. | |
93 | */ | |
94 | typedef struct | |
95 | { | |
96 | char *motd_file; | |
4f6325c3 | 97 | char *log_file; |
8638dd48 | 98 | char *pid_file; |
851dbdb7 | 99 | int syslog_facility; |
a6801c39 | 100 | char *socket_options; |
0b76cd63 AT |
101 | } global; |
102 | ||
103 | static global Globals; | |
104 | ||
105 | ||
106 | ||
107 | /* | |
108 | * This structure describes a single service. | |
109 | */ | |
110 | typedef struct | |
111 | { | |
112 | char *name; | |
113 | char *path; | |
114 | char *comment; | |
5e71c444 | 115 | char *lock_file; |
0b76cd63 AT |
116 | BOOL read_only; |
117 | BOOL list; | |
8638dd48 | 118 | BOOL use_chroot; |
11a5a3c7 | 119 | BOOL transfer_logging; |
8ef4ffd6 AT |
120 | char *uid; |
121 | char *gid; | |
56c473b7 AT |
122 | char *hosts_allow; |
123 | char *hosts_deny; | |
31593dd6 AT |
124 | char *auth_users; |
125 | char *secrets_file; | |
3ca8e68f | 126 | BOOL strict_modes; |
8f3a2d54 AT |
127 | char *exclude; |
128 | char *exclude_from; | |
cd64343a DD |
129 | char *include; |
130 | char *include_from; | |
e08bfe12 | 131 | char *log_format; |
cd8185f2 | 132 | char *refuse_options; |
83fff1aa | 133 | char *dont_compress; |
81791cfc | 134 | int timeout; |
5e71c444 | 135 | int max_connections; |
0b76cd63 AT |
136 | } service; |
137 | ||
138 | ||
139 | /* This is a default service used to prime a services structure */ | |
140 | static service sDefault = | |
141 | { | |
142 | NULL, /* name */ | |
143 | NULL, /* path */ | |
144 | NULL, /* comment */ | |
5e71c444 | 145 | DEFAULT_LOCK_FILE, /* lock file */ |
0b76cd63 AT |
146 | True, /* read only */ |
147 | True, /* list */ | |
8638dd48 | 148 | True, /* use chroot */ |
11a5a3c7 | 149 | False, /* transfer logging */ |
8ef4ffd6 AT |
150 | "nobody",/* uid */ |
151 | "nobody",/* gid */ | |
56c473b7 AT |
152 | NULL, /* hosts allow */ |
153 | NULL, /* hosts deny */ | |
31593dd6 AT |
154 | NULL, /* auth users */ |
155 | NULL, /* secrets file */ | |
3ca8e68f | 156 | True, /* strict modes */ |
8f3a2d54 AT |
157 | NULL, /* exclude */ |
158 | NULL, /* exclude from */ | |
cd64343a DD |
159 | NULL, /* include */ |
160 | NULL, /* include from */ | |
2c51d5de | 161 | "%o %h [%a] %m (%u) %f %l", /* log format */ |
cd8185f2 | 162 | NULL, /* refuse options */ |
83fff1aa | 163 | "*.gz *.tgz *.zip *.z *.rpm *.deb", /* dont compress */ |
5e71c444 AT |
164 | 0, /* timeout */ |
165 | 0 /* max connections */ | |
0b76cd63 AT |
166 | }; |
167 | ||
168 | ||
169 | ||
170 | /* local variables */ | |
171 | static service **ServicePtrs = NULL; | |
172 | static int iNumServices = 0; | |
173 | static int iServiceIndex = 0; | |
174 | static BOOL bInGlobalSection = True; | |
175 | ||
176 | #define NUMPARAMETERS (sizeof(parm_table) / sizeof(struct parm_struct)) | |
177 | ||
09e13ae2 AT |
178 | static struct enum_list enum_facilities[] = { |
179 | #ifdef LOG_AUTH | |
180 | { LOG_AUTH, "auth" }, | |
181 | #endif | |
182 | #ifdef LOG_AUTHPRIV | |
183 | { LOG_AUTHPRIV, "authpriv" }, | |
184 | #endif | |
185 | #ifdef LOG_CRON | |
186 | { LOG_CRON, "cron" }, | |
187 | #endif | |
188 | #ifdef LOG_DAEMON | |
189 | { LOG_DAEMON, "daemon" }, | |
190 | #endif | |
191 | #ifdef LOG_FTP | |
192 | { LOG_FTP, "ftp" }, | |
193 | #endif | |
194 | #ifdef LOG_KERN | |
195 | { LOG_KERN, "kern" }, | |
196 | #endif | |
197 | #ifdef LOG_LPR | |
198 | { LOG_LPR, "lpr" }, | |
199 | #endif | |
200 | #ifdef LOG_MAIL | |
201 | { LOG_MAIL, "mail" }, | |
202 | #endif | |
203 | #ifdef LOG_NEWS | |
204 | { LOG_NEWS, "news" }, | |
205 | #endif | |
206 | #ifdef LOG_AUTH | |
207 | { LOG_AUTH, "security" }, | |
208 | #endif | |
209 | #ifdef LOG_SYSLOG | |
210 | { LOG_SYSLOG, "syslog" }, | |
211 | #endif | |
212 | #ifdef LOG_USER | |
213 | { LOG_USER, "user" }, | |
214 | #endif | |
215 | #ifdef LOG_UUCP | |
216 | { LOG_UUCP, "uucp" }, | |
217 | #endif | |
218 | #ifdef LOG_LOCAL0 | |
219 | { LOG_LOCAL0, "local0" }, | |
220 | #endif | |
221 | #ifdef LOG_LOCAL1 | |
222 | { LOG_LOCAL1, "local1" }, | |
223 | #endif | |
224 | #ifdef LOG_LOCAL2 | |
225 | { LOG_LOCAL2, "local2" }, | |
226 | #endif | |
227 | #ifdef LOG_LOCAL3 | |
228 | { LOG_LOCAL3, "local3" }, | |
229 | #endif | |
230 | #ifdef LOG_LOCAL4 | |
231 | { LOG_LOCAL4, "local4" }, | |
232 | #endif | |
233 | #ifdef LOG_LOCAL5 | |
234 | { LOG_LOCAL5, "local5" }, | |
235 | #endif | |
236 | #ifdef LOG_LOCAL6 | |
237 | { LOG_LOCAL6, "local6" }, | |
238 | #endif | |
239 | #ifdef LOG_LOCAL7 | |
240 | { LOG_LOCAL7, "local7" }, | |
241 | #endif | |
242 | { -1, NULL }}; | |
243 | ||
0b76cd63 AT |
244 | |
245 | /* note that we do not initialise the defaults union - it is not allowed in ANSI C */ | |
246 | static struct parm_struct parm_table[] = | |
247 | { | |
248 | {"motd file", P_STRING, P_GLOBAL, &Globals.motd_file, NULL, 0}, | |
09e13ae2 | 249 | {"syslog facility", P_ENUM, P_GLOBAL, &Globals.syslog_facility, enum_facilities,0}, |
a6801c39 | 250 | {"socket options", P_STRING, P_GLOBAL, &Globals.socket_options,NULL, 0}, |
4f6325c3 | 251 | {"log file", P_STRING, P_GLOBAL, &Globals.log_file, NULL, 0}, |
8638dd48 | 252 | {"pid file", P_STRING, P_GLOBAL, &Globals.pid_file, NULL, 0}, |
41059f75 | 253 | |
81791cfc | 254 | {"timeout", P_INTEGER, P_LOCAL, &sDefault.timeout, NULL, 0}, |
5e71c444 | 255 | {"max connections", P_INTEGER, P_LOCAL, &sDefault.max_connections,NULL, 0}, |
0b76cd63 AT |
256 | {"name", P_STRING, P_LOCAL, &sDefault.name, NULL, 0}, |
257 | {"comment", P_STRING, P_LOCAL, &sDefault.comment, NULL, 0}, | |
5e71c444 | 258 | {"lock file", P_STRING, P_LOCAL, &sDefault.lock_file, NULL, 0}, |
0b76cd63 AT |
259 | {"path", P_STRING, P_LOCAL, &sDefault.path, NULL, 0}, |
260 | {"read only", P_BOOL, P_LOCAL, &sDefault.read_only, NULL, 0}, | |
261 | {"list", P_BOOL, P_LOCAL, &sDefault.list, NULL, 0}, | |
8638dd48 | 262 | {"use chroot", P_BOOL, P_LOCAL, &sDefault.use_chroot, NULL, 0}, |
8ef4ffd6 AT |
263 | {"uid", P_STRING, P_LOCAL, &sDefault.uid, NULL, 0}, |
264 | {"gid", P_STRING, P_LOCAL, &sDefault.gid, NULL, 0}, | |
56c473b7 AT |
265 | {"hosts allow", P_STRING, P_LOCAL, &sDefault.hosts_allow, NULL, 0}, |
266 | {"hosts deny", P_STRING, P_LOCAL, &sDefault.hosts_deny, NULL, 0}, | |
31593dd6 AT |
267 | {"auth users", P_STRING, P_LOCAL, &sDefault.auth_users, NULL, 0}, |
268 | {"secrets file", P_STRING, P_LOCAL, &sDefault.secrets_file,NULL, 0}, | |
3ca8e68f | 269 | {"strict modes", P_BOOL, P_LOCAL, &sDefault.strict_modes,NULL, 0}, |
8f3a2d54 AT |
270 | {"exclude", P_STRING, P_LOCAL, &sDefault.exclude, NULL, 0}, |
271 | {"exclude from", P_STRING, P_LOCAL, &sDefault.exclude_from,NULL, 0}, | |
cd64343a DD |
272 | {"include", P_STRING, P_LOCAL, &sDefault.include, NULL, 0}, |
273 | {"include from", P_STRING, P_LOCAL, &sDefault.include_from,NULL, 0}, | |
11a5a3c7 | 274 | {"transfer logging", P_BOOL, P_LOCAL, &sDefault.transfer_logging,NULL,0}, |
e08bfe12 | 275 | {"log format", P_STRING, P_LOCAL, &sDefault.log_format, NULL, 0}, |
cd8185f2 | 276 | {"refuse options", P_STRING, P_LOCAL, &sDefault.refuse_options,NULL, 0}, |
83fff1aa | 277 | {"dont compress", P_STRING, P_LOCAL, &sDefault.dont_compress,NULL, 0}, |
0b76cd63 AT |
278 | {NULL, P_BOOL, P_NONE, NULL, NULL, 0} |
279 | }; | |
280 | ||
281 | ||
282 | /*************************************************************************** | |
283 | Initialise the global parameter structure. | |
284 | ***************************************************************************/ | |
285 | static void init_globals(void) | |
286 | { | |
f9e940ef | 287 | memset(&Globals, 0, sizeof(Globals)); |
851dbdb7 AT |
288 | #ifdef LOG_DAEMON |
289 | Globals.syslog_facility = LOG_DAEMON; | |
290 | #endif | |
0b76cd63 AT |
291 | } |
292 | ||
293 | /*************************************************************************** | |
294 | Initialise the sDefault parameter structure. | |
295 | ***************************************************************************/ | |
296 | static void init_locals(void) | |
297 | { | |
298 | } | |
299 | ||
300 | ||
301 | /* | |
302 | In this section all the functions that are used to access the | |
303 | parameters from the rest of the program are defined | |
304 | */ | |
305 | ||
306 | #define FN_GLOBAL_STRING(fn_name,ptr) \ | |
307 | char *fn_name(void) {return(*(char **)(ptr) ? *(char **)(ptr) : "");} | |
308 | #define FN_GLOBAL_BOOL(fn_name,ptr) \ | |
309 | BOOL fn_name(void) {return(*(BOOL *)(ptr));} | |
310 | #define FN_GLOBAL_CHAR(fn_name,ptr) \ | |
311 | char fn_name(void) {return(*(char *)(ptr));} | |
312 | #define FN_GLOBAL_INTEGER(fn_name,ptr) \ | |
313 | int fn_name(void) {return(*(int *)(ptr));} | |
314 | ||
315 | #define FN_LOCAL_STRING(fn_name,val) \ | |
316 | char *fn_name(int i) {return((LP_SNUM_OK(i)&&pSERVICE(i)->val)?pSERVICE(i)->val : (sDefault.val?sDefault.val:""));} | |
317 | #define FN_LOCAL_BOOL(fn_name,val) \ | |
318 | BOOL fn_name(int i) {return(LP_SNUM_OK(i)? pSERVICE(i)->val : sDefault.val);} | |
319 | #define FN_LOCAL_CHAR(fn_name,val) \ | |
320 | char fn_name(int i) {return(LP_SNUM_OK(i)? pSERVICE(i)->val : sDefault.val);} | |
321 | #define FN_LOCAL_INTEGER(fn_name,val) \ | |
322 | int fn_name(int i) {return(LP_SNUM_OK(i)? pSERVICE(i)->val : sDefault.val);} | |
323 | ||
324 | ||
325 | FN_GLOBAL_STRING(lp_motd_file, &Globals.motd_file) | |
4f6325c3 | 326 | FN_GLOBAL_STRING(lp_log_file, &Globals.log_file) |
8638dd48 | 327 | FN_GLOBAL_STRING(lp_pid_file, &Globals.pid_file) |
a6801c39 | 328 | FN_GLOBAL_STRING(lp_socket_options, &Globals.socket_options) |
851dbdb7 | 329 | FN_GLOBAL_INTEGER(lp_syslog_facility, &Globals.syslog_facility) |
a6801c39 | 330 | |
0b76cd63 AT |
331 | FN_LOCAL_STRING(lp_name, name) |
332 | FN_LOCAL_STRING(lp_comment, comment) | |
333 | FN_LOCAL_STRING(lp_path, path) | |
5e71c444 | 334 | FN_LOCAL_STRING(lp_lock_file, lock_file) |
0b76cd63 AT |
335 | FN_LOCAL_BOOL(lp_read_only, read_only) |
336 | FN_LOCAL_BOOL(lp_list, list) | |
8638dd48 | 337 | FN_LOCAL_BOOL(lp_use_chroot, use_chroot) |
11a5a3c7 | 338 | FN_LOCAL_BOOL(lp_transfer_logging, transfer_logging) |
8ef4ffd6 AT |
339 | FN_LOCAL_STRING(lp_uid, uid) |
340 | FN_LOCAL_STRING(lp_gid, gid) | |
56c473b7 AT |
341 | FN_LOCAL_STRING(lp_hosts_allow, hosts_allow) |
342 | FN_LOCAL_STRING(lp_hosts_deny, hosts_deny) | |
31593dd6 AT |
343 | FN_LOCAL_STRING(lp_auth_users, auth_users) |
344 | FN_LOCAL_STRING(lp_secrets_file, secrets_file) | |
3ca8e68f | 345 | FN_LOCAL_BOOL(lp_strict_modes, strict_modes) |
8f3a2d54 AT |
346 | FN_LOCAL_STRING(lp_exclude, exclude) |
347 | FN_LOCAL_STRING(lp_exclude_from, exclude_from) | |
cd64343a DD |
348 | FN_LOCAL_STRING(lp_include, include) |
349 | FN_LOCAL_STRING(lp_include_from, include_from) | |
e08bfe12 | 350 | FN_LOCAL_STRING(lp_log_format, log_format) |
cd8185f2 | 351 | FN_LOCAL_STRING(lp_refuse_options, refuse_options) |
83fff1aa | 352 | FN_LOCAL_STRING(lp_dont_compress, dont_compress) |
81791cfc | 353 | FN_LOCAL_INTEGER(lp_timeout, timeout) |
5e71c444 | 354 | FN_LOCAL_INTEGER(lp_max_connections, max_connections) |
0b76cd63 AT |
355 | |
356 | /* local prototypes */ | |
357 | static int strwicmp( char *psz1, char *psz2 ); | |
358 | static int map_parameter( char *parmname); | |
359 | static BOOL set_boolean( BOOL *pb, char *parmvalue ); | |
360 | static int getservicebyname(char *name, service *pserviceDest); | |
361 | static void copy_service( service *pserviceDest, | |
362 | service *pserviceSource); | |
363 | static BOOL do_parameter(char *parmname, char *parmvalue); | |
364 | static BOOL do_section(char *sectionname); | |
365 | ||
366 | ||
367 | /*************************************************************************** | |
368 | initialise a service to the defaults | |
369 | ***************************************************************************/ | |
370 | static void init_service(service *pservice) | |
371 | { | |
f5780433 | 372 | memset((char *)pservice,0,sizeof(service)); |
45ccc5c0 | 373 | copy_service(pservice,&sDefault); |
0b76cd63 AT |
374 | } |
375 | ||
376 | static void string_set(char **s, char *v) | |
377 | { | |
0b76cd63 AT |
378 | if (!v) { |
379 | *s = NULL; | |
380 | return; | |
381 | } | |
382 | *s = strdup(v); | |
65417579 | 383 | if (!*s) exit_cleanup(RERR_MALLOC); |
0b76cd63 AT |
384 | } |
385 | ||
386 | ||
387 | /*************************************************************************** | |
388 | add a new service to the services array initialising it with the given | |
389 | service | |
390 | ***************************************************************************/ | |
391 | static int add_a_service(service *pservice, char *name) | |
392 | { | |
393 | int i; | |
394 | service tservice; | |
395 | int num_to_alloc = iNumServices+1; | |
396 | ||
397 | tservice = *pservice; | |
398 | ||
399 | /* it might already exist */ | |
400 | if (name) | |
401 | { | |
402 | i = getservicebyname(name,NULL); | |
403 | if (i >= 0) | |
404 | return(i); | |
405 | } | |
406 | ||
407 | i = iNumServices; | |
408 | ||
fe8c0a98 | 409 | ServicePtrs = (service **)Realloc(ServicePtrs,sizeof(service *)*num_to_alloc); |
6cdc6b13 | 410 | |
0b76cd63 AT |
411 | if (ServicePtrs) |
412 | pSERVICE(iNumServices) = (service *)malloc(sizeof(service)); | |
413 | ||
414 | if (!ServicePtrs || !pSERVICE(iNumServices)) | |
415 | return(-1); | |
416 | ||
417 | iNumServices++; | |
418 | ||
419 | init_service(pSERVICE(i)); | |
420 | copy_service(pSERVICE(i),&tservice); | |
421 | if (name) | |
422 | string_set(&iSERVICE(i).name,name); | |
423 | ||
424 | return(i); | |
425 | } | |
426 | ||
427 | /*************************************************************************** | |
428 | Do a case-insensitive, whitespace-ignoring string compare. | |
429 | ***************************************************************************/ | |
430 | static int strwicmp(char *psz1, char *psz2) | |
431 | { | |
432 | /* if BOTH strings are NULL, return TRUE, if ONE is NULL return */ | |
433 | /* appropriate value. */ | |
434 | if (psz1 == psz2) | |
435 | return (0); | |
436 | else | |
437 | if (psz1 == NULL) | |
438 | return (-1); | |
439 | else | |
440 | if (psz2 == NULL) | |
441 | return (1); | |
442 | ||
443 | /* sync the strings on first non-whitespace */ | |
444 | while (1) | |
445 | { | |
446 | while (isspace(*psz1)) | |
447 | psz1++; | |
448 | while (isspace(*psz2)) | |
449 | psz2++; | |
450 | if (toupper(*psz1) != toupper(*psz2) || *psz1 == '\0' || *psz2 == '\0') | |
451 | break; | |
452 | psz1++; | |
453 | psz2++; | |
454 | } | |
455 | return (*psz1 - *psz2); | |
456 | } | |
457 | ||
458 | /*************************************************************************** | |
459 | Map a parameter's string representation to something we can use. | |
460 | Returns False if the parameter string is not recognised, else TRUE. | |
461 | ***************************************************************************/ | |
462 | static int map_parameter(char *parmname) | |
463 | { | |
464 | int iIndex; | |
465 | ||
466 | if (*parmname == '-') | |
467 | return(-1); | |
468 | ||
469 | for (iIndex = 0; parm_table[iIndex].label; iIndex++) | |
470 | if (strwicmp(parm_table[iIndex].label, parmname) == 0) | |
471 | return(iIndex); | |
472 | ||
cd64343a | 473 | rprintf(FERROR, "Unknown Parameter encountered: \"%s\"\n", parmname); |
0b76cd63 AT |
474 | return(-1); |
475 | } | |
476 | ||
477 | ||
478 | /*************************************************************************** | |
479 | Set a boolean variable from the text value stored in the passed string. | |
480 | Returns True in success, False if the passed string does not correctly | |
481 | represent a boolean. | |
482 | ***************************************************************************/ | |
483 | static BOOL set_boolean(BOOL *pb, char *parmvalue) | |
484 | { | |
485 | BOOL bRetval; | |
486 | ||
487 | bRetval = True; | |
488 | if (strwicmp(parmvalue, "yes") == 0 || | |
489 | strwicmp(parmvalue, "true") == 0 || | |
490 | strwicmp(parmvalue, "1") == 0) | |
491 | *pb = True; | |
492 | else | |
493 | if (strwicmp(parmvalue, "no") == 0 || | |
494 | strwicmp(parmvalue, "False") == 0 || | |
495 | strwicmp(parmvalue, "0") == 0) | |
496 | *pb = False; | |
497 | else | |
498 | { | |
499 | rprintf(FERROR, "Badly formed boolean in configuration file: \"%s\".\n", | |
500 | parmvalue); | |
501 | bRetval = False; | |
502 | } | |
503 | return (bRetval); | |
504 | } | |
505 | ||
506 | /*************************************************************************** | |
507 | Find a service by name. Otherwise works like get_service. | |
508 | ***************************************************************************/ | |
509 | static int getservicebyname(char *name, service *pserviceDest) | |
510 | { | |
511 | int iService; | |
512 | ||
513 | for (iService = iNumServices - 1; iService >= 0; iService--) | |
514 | if (strwicmp(iSERVICE(iService).name, name) == 0) | |
515 | { | |
516 | if (pserviceDest != NULL) | |
517 | copy_service(pserviceDest, pSERVICE(iService)); | |
518 | break; | |
519 | } | |
520 | ||
521 | return (iService); | |
522 | } | |
523 | ||
524 | ||
525 | ||
526 | /*************************************************************************** | |
527 | Copy a service structure to another | |
528 | ||
529 | ***************************************************************************/ | |
530 | static void copy_service(service *pserviceDest, | |
531 | service *pserviceSource) | |
532 | { | |
533 | int i; | |
534 | ||
535 | for (i=0;parm_table[i].label;i++) | |
536 | if (parm_table[i].ptr && parm_table[i].class == P_LOCAL) { | |
537 | void *def_ptr = parm_table[i].ptr; | |
538 | void *src_ptr = | |
539 | ((char *)pserviceSource) + PTR_DIFF(def_ptr,&sDefault); | |
540 | void *dest_ptr = | |
541 | ((char *)pserviceDest) + PTR_DIFF(def_ptr,&sDefault); | |
542 | ||
543 | switch (parm_table[i].type) | |
544 | { | |
545 | case P_BOOL: | |
546 | case P_BOOLREV: | |
547 | *(BOOL *)dest_ptr = *(BOOL *)src_ptr; | |
548 | break; | |
549 | ||
550 | case P_INTEGER: | |
551 | case P_ENUM: | |
552 | case P_OCTAL: | |
553 | *(int *)dest_ptr = *(int *)src_ptr; | |
554 | break; | |
555 | ||
556 | case P_CHAR: | |
557 | *(char *)dest_ptr = *(char *)src_ptr; | |
558 | break; | |
559 | ||
560 | case P_STRING: | |
561 | string_set(dest_ptr,*(char **)src_ptr); | |
562 | break; | |
563 | ||
564 | default: | |
565 | break; | |
566 | } | |
567 | } | |
568 | } | |
569 | ||
570 | ||
571 | /*************************************************************************** | |
572 | Process a parameter for a particular service number. If snum < 0 | |
573 | then assume we are in the globals | |
574 | ***************************************************************************/ | |
575 | static BOOL lp_do_parameter(int snum, char *parmname, char *parmvalue) | |
576 | { | |
577 | int parmnum, i; | |
578 | void *parm_ptr=NULL; /* where we are going to store the result */ | |
579 | void *def_ptr=NULL; | |
580 | ||
581 | parmnum = map_parameter(parmname); | |
582 | ||
583 | if (parmnum < 0) | |
584 | { | |
cd64343a | 585 | rprintf(FERROR, "IGNORING unknown parameter \"%s\"\n", parmname); |
0b76cd63 AT |
586 | return(True); |
587 | } | |
588 | ||
589 | def_ptr = parm_table[parmnum].ptr; | |
590 | ||
591 | /* we might point at a service, the default service or a global */ | |
592 | if (snum < 0) { | |
593 | parm_ptr = def_ptr; | |
594 | } else { | |
595 | if (parm_table[parmnum].class == P_GLOBAL) { | |
596 | rprintf(FERROR, "Global parameter %s found in service section!\n",parmname); | |
597 | return(True); | |
598 | } | |
599 | parm_ptr = ((char *)pSERVICE(snum)) + PTR_DIFF(def_ptr,&sDefault); | |
600 | } | |
601 | ||
602 | /* now switch on the type of variable it is */ | |
603 | switch (parm_table[parmnum].type) | |
604 | { | |
605 | case P_BOOL: | |
606 | set_boolean(parm_ptr,parmvalue); | |
607 | break; | |
608 | ||
609 | case P_BOOLREV: | |
610 | set_boolean(parm_ptr,parmvalue); | |
611 | *(BOOL *)parm_ptr = ! *(BOOL *)parm_ptr; | |
612 | break; | |
613 | ||
614 | case P_INTEGER: | |
615 | *(int *)parm_ptr = atoi(parmvalue); | |
616 | break; | |
617 | ||
618 | case P_CHAR: | |
619 | *(char *)parm_ptr = *parmvalue; | |
620 | break; | |
621 | ||
622 | case P_OCTAL: | |
623 | sscanf(parmvalue,"%o",(int *)parm_ptr); | |
624 | break; | |
625 | ||
626 | case P_STRING: | |
627 | string_set(parm_ptr,parmvalue); | |
628 | break; | |
629 | ||
630 | case P_GSTRING: | |
37f9805d | 631 | strlcpy((char *)parm_ptr,parmvalue,sizeof(pstring)); |
0b76cd63 AT |
632 | break; |
633 | ||
634 | case P_ENUM: | |
635 | for (i=0;parm_table[parmnum].enum_list[i].name;i++) { | |
636 | if (strequal(parmvalue, parm_table[parmnum].enum_list[i].name)) { | |
637 | *(int *)parm_ptr = parm_table[parmnum].enum_list[i].value; | |
638 | break; | |
639 | } | |
640 | } | |
fabf5ea7 | 641 | if (!parm_table[parmnum].enum_list[i].name) { |
41059f75 AT |
642 | if (atoi(parmvalue) > 0) |
643 | *(int *)parm_ptr = atoi(parmvalue); | |
fabf5ea7 | 644 | } |
0b76cd63 AT |
645 | break; |
646 | case P_SEP: | |
647 | break; | |
648 | } | |
649 | ||
650 | return(True); | |
651 | } | |
652 | ||
653 | /*************************************************************************** | |
654 | Process a parameter. | |
655 | ***************************************************************************/ | |
656 | static BOOL do_parameter(char *parmname, char *parmvalue) | |
657 | { | |
658 | return lp_do_parameter(bInGlobalSection?-2:iServiceIndex, parmname, parmvalue); | |
659 | } | |
660 | ||
661 | /*************************************************************************** | |
662 | Process a new section (service). At this stage all sections are services. | |
663 | Later we'll have special sections that permit server parameters to be set. | |
664 | Returns True on success, False on failure. | |
665 | ***************************************************************************/ | |
666 | static BOOL do_section(char *sectionname) | |
667 | { | |
668 | BOOL bRetval; | |
669 | BOOL isglobal = (strwicmp(sectionname, GLOBAL_NAME) == 0); | |
670 | bRetval = False; | |
671 | ||
672 | /* if we were in a global section then do the local inits */ | |
673 | if (bInGlobalSection && !isglobal) | |
674 | init_locals(); | |
675 | ||
676 | /* if we've just struck a global section, note the fact. */ | |
677 | bInGlobalSection = isglobal; | |
678 | ||
679 | /* check for multiple global sections */ | |
680 | if (bInGlobalSection) | |
681 | { | |
682 | return(True); | |
683 | } | |
684 | ||
685 | /* if we have a current service, tidy it up before moving on */ | |
686 | bRetval = True; | |
687 | ||
688 | if (iServiceIndex >= 0) | |
689 | bRetval = True; | |
690 | ||
691 | /* if all is still well, move to the next record in the services array */ | |
692 | if (bRetval) | |
693 | { | |
694 | /* We put this here to avoid an odd message order if messages are */ | |
695 | /* issued by the post-processing of a previous section. */ | |
696 | ||
697 | if ((iServiceIndex=add_a_service(&sDefault,sectionname)) < 0) | |
698 | { | |
699 | rprintf(FERROR,"Failed to add a new service\n"); | |
700 | return(False); | |
701 | } | |
702 | } | |
703 | ||
704 | return (bRetval); | |
705 | } | |
706 | ||
707 | ||
708 | /*************************************************************************** | |
709 | Load the services array from the services file. Return True on success, | |
710 | False on failure. | |
711 | ***************************************************************************/ | |
f9e940ef | 712 | BOOL lp_load(char *pszFname, int globals_only) |
0b76cd63 | 713 | { |
f9e940ef AT |
714 | pstring n2; |
715 | BOOL bRetval; | |
0b76cd63 | 716 | |
f9e940ef | 717 | bRetval = False; |
0b76cd63 | 718 | |
f9e940ef | 719 | bInGlobalSection = True; |
0b76cd63 | 720 | |
f9e940ef | 721 | init_globals(); |
0b76cd63 | 722 | |
f9e940ef | 723 | pstrcpy(n2,pszFname); |
0b76cd63 | 724 | |
f9e940ef AT |
725 | /* We get sections first, so have to start 'behind' to make up */ |
726 | iServiceIndex = -1; | |
727 | bRetval = pm_process(n2, globals_only?NULL:do_section, do_parameter); | |
0b76cd63 | 728 | |
f9e940ef | 729 | bLoaded = True; |
0b76cd63 | 730 | |
f9e940ef | 731 | return (bRetval); |
0b76cd63 AT |
732 | } |
733 | ||
734 | ||
735 | /*************************************************************************** | |
736 | return the max number of services | |
737 | ***************************************************************************/ | |
738 | int lp_numservices(void) | |
739 | { | |
740 | return(iNumServices); | |
741 | } | |
742 | ||
743 | /*************************************************************************** | |
744 | Return the number of the service with the given name, or -1 if it doesn't | |
745 | exist. Note that this is a DIFFERENT ANIMAL from the internal function | |
746 | getservicebyname()! This works ONLY if all services have been loaded, and | |
747 | does not copy the found service. | |
748 | ***************************************************************************/ | |
749 | int lp_number(char *name) | |
750 | { | |
751 | int iService; | |
752 | ||
753 | for (iService = iNumServices - 1; iService >= 0; iService--) | |
754 | if (strequal(lp_name(iService), name)) | |
755 | break; | |
756 | ||
757 | return (iService); | |
758 | } | |
759 |