1 /* This is based on loadparm.c from Samba, written by Andrew Tridgell
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.
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.
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.
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.
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
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
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];
51 #define pstrcpy(a,b) strlcpy(a,b,sizeof(pstring))
53 /* the following are used by loadparm for option lists */
56 P_BOOL,P_BOOLREV,P_CHAR,P_INTEGER,P_OCTAL,
57 P_STRING,P_GSTRING,P_ENUM,P_SEP
62 P_LOCAL,P_GLOBAL,P_SEPARATOR,P_NONE
76 struct enum_list *enum_list;
80 static BOOL bLoaded = False;
83 #define GLOBAL_NAME "global"
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))
92 * This structure describes global (ie., server-wide) parameters.
100 char *socket_options;
103 static global Globals;
108 * This structure describes a single service.
119 BOOL transfer_logging;
131 char *refuse_options;
138 /* This is a default service used to prime a services structure */
139 static service sDefault =
144 DEFAULT_LOCK_FILE, /* lock file */
145 True, /* read only */
147 True, /* use chroot */
148 False, /* transfer logging */
151 NULL, /* hosts allow */
152 NULL, /* hosts deny */
153 NULL, /* auth users */
154 NULL, /* secrets file */
156 NULL, /* exclude from */
158 NULL, /* include from */
159 "%o %h [%a] %m (%u) %f %l", /* log format */
160 NULL, /* refuse options */
161 "*.gz *.tgz *.zip *.z *.rpm *.deb", /* dont compress */
163 0 /* max connections */
168 /* local variables */
169 static service **ServicePtrs = NULL;
170 static int iNumServices = 0;
171 static int iServiceIndex = 0;
172 static BOOL bInGlobalSection = True;
174 #define NUMPARAMETERS (sizeof(parm_table) / sizeof(struct parm_struct))
176 static struct enum_list enum_facilities[] = {
178 { LOG_AUTH, "auth" },
181 { LOG_AUTHPRIV, "authpriv" },
184 { LOG_CRON, "cron" },
187 { LOG_DAEMON, "daemon" },
193 { LOG_KERN, "kern" },
199 { LOG_MAIL, "mail" },
202 { LOG_NEWS, "news" },
205 { LOG_AUTH, "security" },
208 { LOG_SYSLOG, "syslog" },
211 { LOG_USER, "user" },
214 { LOG_UUCP, "uucp" },
217 { LOG_LOCAL0, "local0" },
220 { LOG_LOCAL1, "local1" },
223 { LOG_LOCAL2, "local2" },
226 { LOG_LOCAL3, "local3" },
229 { LOG_LOCAL4, "local4" },
232 { LOG_LOCAL5, "local5" },
235 { LOG_LOCAL6, "local6" },
238 { LOG_LOCAL7, "local7" },
243 /* note that we do not initialise the defaults union - it is not allowed in ANSI C */
244 static struct parm_struct parm_table[] =
246 {"motd file", P_STRING, P_GLOBAL, &Globals.motd_file, NULL, 0},
247 {"syslog facility", P_ENUM, P_GLOBAL, &Globals.syslog_facility, enum_facilities,0},
248 {"socket options", P_STRING, P_GLOBAL, &Globals.socket_options,NULL, 0},
249 {"log file", P_STRING, P_GLOBAL, &Globals.log_file, NULL, 0},
250 {"pid file", P_STRING, P_GLOBAL, &Globals.pid_file, NULL, 0},
252 {"timeout", P_INTEGER, P_LOCAL, &sDefault.timeout, NULL, 0},
253 {"max connections", P_INTEGER, P_LOCAL, &sDefault.max_connections,NULL, 0},
254 {"name", P_STRING, P_LOCAL, &sDefault.name, NULL, 0},
255 {"comment", P_STRING, P_LOCAL, &sDefault.comment, NULL, 0},
256 {"lock file", P_STRING, P_LOCAL, &sDefault.lock_file, NULL, 0},
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},
260 {"use chroot", P_BOOL, P_LOCAL, &sDefault.use_chroot, NULL, 0},
261 {"uid", P_STRING, P_LOCAL, &sDefault.uid, NULL, 0},
262 {"gid", P_STRING, P_LOCAL, &sDefault.gid, NULL, 0},
263 {"hosts allow", P_STRING, P_LOCAL, &sDefault.hosts_allow, NULL, 0},
264 {"hosts deny", P_STRING, P_LOCAL, &sDefault.hosts_deny, NULL, 0},
265 {"auth users", P_STRING, P_LOCAL, &sDefault.auth_users, NULL, 0},
266 {"secrets file", P_STRING, P_LOCAL, &sDefault.secrets_file,NULL, 0},
267 {"exclude", P_STRING, P_LOCAL, &sDefault.exclude, NULL, 0},
268 {"exclude from", P_STRING, P_LOCAL, &sDefault.exclude_from,NULL, 0},
269 {"include", P_STRING, P_LOCAL, &sDefault.include, NULL, 0},
270 {"include from", P_STRING, P_LOCAL, &sDefault.include_from,NULL, 0},
271 {"transfer logging", P_BOOL, P_LOCAL, &sDefault.transfer_logging,NULL,0},
272 {"log format", P_STRING, P_LOCAL, &sDefault.log_format, NULL, 0},
273 {"refuse options", P_STRING, P_LOCAL, &sDefault.refuse_options,NULL, 0},
274 {"dont compress", P_STRING, P_LOCAL, &sDefault.dont_compress,NULL, 0},
275 {NULL, P_BOOL, P_NONE, NULL, NULL, 0}
279 /***************************************************************************
280 Initialise the global parameter structure.
281 ***************************************************************************/
282 static void init_globals(void)
284 memset(&Globals, 0, sizeof(Globals));
286 Globals.syslog_facility = LOG_DAEMON;
290 /***************************************************************************
291 Initialise the sDefault parameter structure.
292 ***************************************************************************/
293 static void init_locals(void)
299 In this section all the functions that are used to access the
300 parameters from the rest of the program are defined
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));}
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);}
322 FN_GLOBAL_STRING(lp_motd_file, &Globals.motd_file)
323 FN_GLOBAL_STRING(lp_log_file, &Globals.log_file)
324 FN_GLOBAL_STRING(lp_pid_file, &Globals.pid_file)
325 FN_GLOBAL_STRING(lp_socket_options, &Globals.socket_options)
326 FN_GLOBAL_INTEGER(lp_syslog_facility, &Globals.syslog_facility)
328 FN_LOCAL_STRING(lp_name, name)
329 FN_LOCAL_STRING(lp_comment, comment)
330 FN_LOCAL_STRING(lp_path, path)
331 FN_LOCAL_STRING(lp_lock_file, lock_file)
332 FN_LOCAL_BOOL(lp_read_only, read_only)
333 FN_LOCAL_BOOL(lp_list, list)
334 FN_LOCAL_BOOL(lp_use_chroot, use_chroot)
335 FN_LOCAL_BOOL(lp_transfer_logging, transfer_logging)
336 FN_LOCAL_STRING(lp_uid, uid)
337 FN_LOCAL_STRING(lp_gid, gid)
338 FN_LOCAL_STRING(lp_hosts_allow, hosts_allow)
339 FN_LOCAL_STRING(lp_hosts_deny, hosts_deny)
340 FN_LOCAL_STRING(lp_auth_users, auth_users)
341 FN_LOCAL_STRING(lp_secrets_file, secrets_file)
342 FN_LOCAL_STRING(lp_exclude, exclude)
343 FN_LOCAL_STRING(lp_exclude_from, exclude_from)
344 FN_LOCAL_STRING(lp_include, include)
345 FN_LOCAL_STRING(lp_include_from, include_from)
346 FN_LOCAL_STRING(lp_log_format, log_format)
347 FN_LOCAL_STRING(lp_refuse_options, refuse_options)
348 FN_LOCAL_STRING(lp_dont_compress, dont_compress)
349 FN_LOCAL_INTEGER(lp_timeout, timeout)
350 FN_LOCAL_INTEGER(lp_max_connections, max_connections)
352 /* local prototypes */
353 static int strwicmp( char *psz1, char *psz2 );
354 static int map_parameter( char *parmname);
355 static BOOL set_boolean( BOOL *pb, char *parmvalue );
356 static int getservicebyname(char *name, service *pserviceDest);
357 static void copy_service( service *pserviceDest,
358 service *pserviceSource);
359 static BOOL do_parameter(char *parmname, char *parmvalue);
360 static BOOL do_section(char *sectionname);
363 /***************************************************************************
364 initialise a service to the defaults
365 ***************************************************************************/
366 static void init_service(service *pservice)
368 memset((char *)pservice,0,sizeof(service));
369 copy_service(pservice,&sDefault);
372 static void string_set(char **s, char *v)
379 if (!*s) exit_cleanup(RERR_MALLOC);
383 /***************************************************************************
384 add a new service to the services array initialising it with the given
386 ***************************************************************************/
387 static int add_a_service(service *pservice, char *name)
391 int num_to_alloc = iNumServices+1;
393 tservice = *pservice;
395 /* it might already exist */
398 i = getservicebyname(name,NULL);
405 ServicePtrs = (service **)Realloc(ServicePtrs,sizeof(service *)*num_to_alloc);
408 pSERVICE(iNumServices) = (service *)malloc(sizeof(service));
410 if (!ServicePtrs || !pSERVICE(iNumServices))
415 init_service(pSERVICE(i));
416 copy_service(pSERVICE(i),&tservice);
418 string_set(&iSERVICE(i).name,name);
423 /***************************************************************************
424 Do a case-insensitive, whitespace-ignoring string compare.
425 ***************************************************************************/
426 static int strwicmp(char *psz1, char *psz2)
428 /* if BOTH strings are NULL, return TRUE, if ONE is NULL return */
429 /* appropriate value. */
439 /* sync the strings on first non-whitespace */
442 while (isspace(*psz1))
444 while (isspace(*psz2))
446 if (toupper(*psz1) != toupper(*psz2) || *psz1 == '\0' || *psz2 == '\0')
451 return (*psz1 - *psz2);
454 /***************************************************************************
455 Map a parameter's string representation to something we can use.
456 Returns False if the parameter string is not recognised, else TRUE.
457 ***************************************************************************/
458 static int map_parameter(char *parmname)
462 if (*parmname == '-')
465 for (iIndex = 0; parm_table[iIndex].label; iIndex++)
466 if (strwicmp(parm_table[iIndex].label, parmname) == 0)
469 rprintf(FERROR, "Unknown Parameter encountered: \"%s\"\n", parmname);
474 /***************************************************************************
475 Set a boolean variable from the text value stored in the passed string.
476 Returns True in success, False if the passed string does not correctly
478 ***************************************************************************/
479 static BOOL set_boolean(BOOL *pb, char *parmvalue)
484 if (strwicmp(parmvalue, "yes") == 0 ||
485 strwicmp(parmvalue, "true") == 0 ||
486 strwicmp(parmvalue, "1") == 0)
489 if (strwicmp(parmvalue, "no") == 0 ||
490 strwicmp(parmvalue, "False") == 0 ||
491 strwicmp(parmvalue, "0") == 0)
495 rprintf(FERROR, "Badly formed boolean in configuration file: \"%s\".\n",
502 /***************************************************************************
503 Find a service by name. Otherwise works like get_service.
504 ***************************************************************************/
505 static int getservicebyname(char *name, service *pserviceDest)
509 for (iService = iNumServices - 1; iService >= 0; iService--)
510 if (strwicmp(iSERVICE(iService).name, name) == 0)
512 if (pserviceDest != NULL)
513 copy_service(pserviceDest, pSERVICE(iService));
522 /***************************************************************************
523 Copy a service structure to another
525 ***************************************************************************/
526 static void copy_service(service *pserviceDest,
527 service *pserviceSource)
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;
535 ((char *)pserviceSource) + PTR_DIFF(def_ptr,&sDefault);
537 ((char *)pserviceDest) + PTR_DIFF(def_ptr,&sDefault);
539 switch (parm_table[i].type)
543 *(BOOL *)dest_ptr = *(BOOL *)src_ptr;
549 *(int *)dest_ptr = *(int *)src_ptr;
553 *(char *)dest_ptr = *(char *)src_ptr;
557 string_set(dest_ptr,*(char **)src_ptr);
567 /***************************************************************************
568 Process a parameter for a particular service number. If snum < 0
569 then assume we are in the globals
570 ***************************************************************************/
571 static BOOL lp_do_parameter(int snum, char *parmname, char *parmvalue)
574 void *parm_ptr=NULL; /* where we are going to store the result */
577 parmnum = map_parameter(parmname);
581 rprintf(FERROR, "IGNORING unknown parameter \"%s\"\n", parmname);
585 def_ptr = parm_table[parmnum].ptr;
587 /* we might point at a service, the default service or a global */
591 if (parm_table[parmnum].class == P_GLOBAL) {
592 rprintf(FERROR, "Global parameter %s found in service section!\n",parmname);
595 parm_ptr = ((char *)pSERVICE(snum)) + PTR_DIFF(def_ptr,&sDefault);
598 /* now switch on the type of variable it is */
599 switch (parm_table[parmnum].type)
602 set_boolean(parm_ptr,parmvalue);
606 set_boolean(parm_ptr,parmvalue);
607 *(BOOL *)parm_ptr = ! *(BOOL *)parm_ptr;
611 *(int *)parm_ptr = atoi(parmvalue);
615 *(char *)parm_ptr = *parmvalue;
619 sscanf(parmvalue,"%o",(int *)parm_ptr);
623 string_set(parm_ptr,parmvalue);
627 strlcpy((char *)parm_ptr,parmvalue,sizeof(pstring));
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;
637 if (!parm_table[parmnum].enum_list[i].name) {
638 if (atoi(parmvalue) > 0)
639 *(int *)parm_ptr = atoi(parmvalue);
649 /***************************************************************************
651 ***************************************************************************/
652 static BOOL do_parameter(char *parmname, char *parmvalue)
654 return lp_do_parameter(bInGlobalSection?-2:iServiceIndex, parmname, parmvalue);
657 /***************************************************************************
658 Process a new section (service). At this stage all sections are services.
659 Later we'll have special sections that permit server parameters to be set.
660 Returns True on success, False on failure.
661 ***************************************************************************/
662 static BOOL do_section(char *sectionname)
665 BOOL isglobal = (strwicmp(sectionname, GLOBAL_NAME) == 0);
668 /* if we were in a global section then do the local inits */
669 if (bInGlobalSection && !isglobal)
672 /* if we've just struck a global section, note the fact. */
673 bInGlobalSection = isglobal;
675 /* check for multiple global sections */
676 if (bInGlobalSection)
681 /* if we have a current service, tidy it up before moving on */
684 if (iServiceIndex >= 0)
687 /* if all is still well, move to the next record in the services array */
690 /* We put this here to avoid an odd message order if messages are */
691 /* issued by the post-processing of a previous section. */
693 if ((iServiceIndex=add_a_service(&sDefault,sectionname)) < 0)
695 rprintf(FERROR,"Failed to add a new service\n");
704 /***************************************************************************
705 Load the services array from the services file. Return True on success,
707 ***************************************************************************/
708 BOOL lp_load(char *pszFname, int globals_only)
715 bInGlobalSection = True;
719 pstrcpy(n2,pszFname);
721 /* We get sections first, so have to start 'behind' to make up */
723 bRetval = pm_process(n2, globals_only?NULL:do_section, do_parameter);
731 /***************************************************************************
732 return the max number of services
733 ***************************************************************************/
734 int lp_numservices(void)
736 return(iNumServices);
739 /***************************************************************************
740 Return the number of the service with the given name, or -1 if it doesn't
741 exist. Note that this is a DIFFERENT ANIMAL from the internal function
742 getservicebyname()! This works ONLY if all services have been loaded, and
743 does not copy the found service.
744 ***************************************************************************/
745 int lp_number(char *name)
749 for (iService = iNumServices - 1; iService >= 0; iService--)
750 if (strequal(lp_name(iService), name))