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