lib/param: fix copy service to include the case for P_CHAR
[Samba.git] / lib / param / loadparm.c
blob669e42db73853ad8ada10221417ad3c369438eb2
1 /*
2 Unix SMB/CIFS implementation.
3 Parameter loading functions
4 Copyright (C) Karl Auer 1993-1998
6 Largely re-written by Andrew Tridgell, September 1994
8 Copyright (C) Simo Sorce 2001
9 Copyright (C) Alexander Bokovoy 2002
10 Copyright (C) Stefan (metze) Metzmacher 2002
11 Copyright (C) Jim McDonough (jmcd@us.ibm.com) 2003.
12 Copyright (C) James Myers 2003 <myersjj@samba.org>
13 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
14 Copyright (C) Andrew Bartlett 2011-2012
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 * Load parameters.
33 * This module provides suitable callback functions for the params
34 * module. It builds the internal table of service details which is
35 * then used by the rest of the server.
37 * To add a parameter:
39 * 1) add it to the global or service structure definition
40 * 2) add it to the parm_table
41 * 3) add it to the list of available functions (eg: using FN_GLOBAL_STRING())
42 * 4) If it's a global then initialise it in init_globals. If a local
43 * (ie. service) parameter then initialise it in the sDefault structure
46 * Notes:
47 * The configuration file is processed sequentially for speed. It is NOT
48 * accessed randomly as happens in 'real' Windows. For this reason, there
49 * is a fair bit of sequence-dependent code here - ie., code which assumes
50 * that certain things happen before others. In particular, the code which
51 * happens at the boundary between sections is delicately poised, so be
52 * careful!
56 #include "includes.h"
57 #include "version.h"
58 #include "dynconfig/dynconfig.h"
59 #include "system/time.h"
60 #include "system/locale.h"
61 #include "system/network.h" /* needed for TCP_NODELAY */
62 #include "../lib/util/dlinklist.h"
63 #include "lib/param/param.h"
64 #include "lib/param/loadparm.h"
65 #include "auth/gensec/gensec.h"
66 #include "lib/param/s3_param.h"
67 #include "lib/util/bitmap.h"
68 #include "libcli/smb/smb_constants.h"
70 #define standard_sub_basic talloc_strdup
72 static bool do_parameter(const char *, const char *, void *);
73 static bool defaults_saved = false;
75 #define LOADPARM_EXTRA_GLOBALS \
76 struct parmlist_entry *param_opt; \
77 char *realm_original; \
78 char *szConfigFile; \
79 int iminreceivefile; \
80 char *szPrintcapname; \
81 int CupsEncrypt; \
82 int iPreferredMaster; \
83 char *szLdapMachineSuffix; \
84 char *szLdapUserSuffix; \
85 char *szLdapIdmapSuffix; \
86 char *szLdapGroupSuffix; \
87 char *szUsershareTemplateShare; \
88 char *szIdmapUID; \
89 char *szIdmapGID; \
90 char *szIdmapBackend; \
91 int winbindMaxDomainConnections; \
92 int ismb2_max_credits; \
93 char *tls_keyfile; \
94 char *tls_certfile; \
95 char *tls_cafile; \
96 char *tls_crlfile; \
97 char *tls_dhpfile; \
98 char *loglevel;
100 #include "lib/param/param_global.h"
102 #define NUMPARAMETERS (sizeof(parm_table) / sizeof(struct parm_struct))
104 /* we don't need a special handler for "dos charset" and "unix charset" */
105 #define handle_dos_charset NULL
106 #define handle_charset NULL
108 /* these are parameter handlers which are not needed in the
109 * non-source3 code
111 #define handle_netbios_aliases NULL
112 #define handle_printing NULL
113 #define handle_ldap_debug_level NULL
114 #define handle_idmap_backend NULL
115 #define handle_idmap_uid NULL
116 #define handle_idmap_gid NULL
118 #ifndef N_
119 #define N_(x) x
120 #endif
122 /* prototypes for the special type handlers */
123 static bool handle_include(struct loadparm_context *lp_ctx, int unused,
124 const char *pszParmValue, char **ptr);
125 static bool handle_realm(struct loadparm_context *lp_ctx, int unused,
126 const char *pszParmValue, char **ptr);
127 static bool handle_copy(struct loadparm_context *lp_ctx, int unused,
128 const char *pszParmValue, char **ptr);
129 static bool handle_debug_list(struct loadparm_context *lp_ctx, int unused,
130 const char *pszParmValue, char **ptr);
131 static bool handle_logfile(struct loadparm_context *lp_ctx, int unused,
132 const char *pszParmValue, char **ptr);
134 #include "lib/param/param_table.c"
136 /* local variables */
137 struct loadparm_context {
138 const char *szConfigFile;
139 struct loadparm_global *globals;
140 struct loadparm_service **services;
141 struct loadparm_service *sDefault;
142 struct smb_iconv_handle *iconv_handle;
143 int iNumServices;
144 struct loadparm_service *currentService;
145 bool bInGlobalSection;
146 struct file_lists {
147 struct file_lists *next;
148 char *name;
149 char *subfname;
150 time_t modtime;
151 } *file_lists;
152 unsigned int flags[NUMPARAMETERS];
153 bool loaded;
154 bool refuse_free;
155 bool global; /* Is this the global context, which may set
156 * global variables such as debug level etc? */
157 const struct loadparm_s3_helpers *s3_fns;
161 struct loadparm_service *lpcfg_default_service(struct loadparm_context *lp_ctx)
163 if (lp_ctx->s3_fns) {
164 return lp_ctx->s3_fns->get_default_loadparm_service();
166 return lp_ctx->sDefault;
170 * Convenience routine to grab string parameters into temporary memory
171 * and run standard_sub_basic on them.
173 * The buffers can be written to by
174 * callers without affecting the source string.
177 static const char *lp_string(const char *s)
179 #if 0 /* until REWRITE done to make thread-safe */
180 size_t len = s ? strlen(s) : 0;
181 char *ret;
182 #endif
184 /* The follow debug is useful for tracking down memory problems
185 especially if you have an inner loop that is calling a lp_*()
186 function that returns a string. Perhaps this debug should be
187 present all the time? */
189 #if 0
190 DEBUG(10, ("lp_string(%s)\n", s));
191 #endif
193 #if 0 /* until REWRITE done to make thread-safe */
194 if (!lp_talloc)
195 lp_talloc = talloc_init("lp_talloc");
197 ret = talloc_array(lp_talloc, char, len + 100); /* leave room for substitution */
199 if (!ret)
200 return NULL;
202 if (!s)
203 *ret = 0;
204 else
205 strlcpy(ret, s, len);
207 if (trim_string(ret, "\"", "\"")) {
208 if (strchr(ret,'"') != NULL)
209 strlcpy(ret, s, len);
212 standard_sub_basic(ret,len+100);
213 return (ret);
214 #endif
215 return s;
219 In this section all the functions that are used to access the
220 parameters from the rest of the program are defined
224 * the creation of separate lpcfg_*() and lp_*() functions is to allow
225 * for code compatibility between existing Samba4 and Samba3 code.
228 /* this global context supports the lp_*() function varients */
229 static struct loadparm_context *global_loadparm_context;
231 #define lpcfg_default_service global_loadparm_context->sDefault
232 #define lpcfg_global_service(i) global_loadparm_context->services[i]
234 #define FN_GLOBAL_STRING(fn_name,var_name) \
235 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) {\
236 if (lp_ctx == NULL) return NULL; \
237 if (lp_ctx->s3_fns) { \
238 smb_panic( __location__ ": " #fn_name " not implemented because it is an allocated and substiuted string"); \
240 return lp_ctx->globals->var_name ? lp_string(lp_ctx->globals->var_name) : ""; \
243 #define FN_GLOBAL_CONST_STRING(fn_name,var_name) \
244 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
245 if (lp_ctx == NULL) return NULL; \
246 if (lp_ctx->s3_fns) { \
247 SMB_ASSERT(lp_ctx->s3_fns->fn_name); \
248 return lp_ctx->s3_fns->fn_name(); \
250 return lp_ctx->globals->var_name ? lp_string(lp_ctx->globals->var_name) : ""; \
253 #define FN_GLOBAL_LIST(fn_name,var_name) \
254 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
255 if (lp_ctx == NULL) return NULL; \
256 if (lp_ctx->s3_fns) { \
257 SMB_ASSERT(lp_ctx->s3_fns->fn_name); \
258 return lp_ctx->s3_fns->fn_name(); \
260 return lp_ctx->globals->var_name; \
263 #define FN_GLOBAL_BOOL(fn_name,var_name) \
264 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) {\
265 if (lp_ctx == NULL) return false; \
266 if (lp_ctx->s3_fns) { \
267 SMB_ASSERT(lp_ctx->s3_fns->fn_name); \
268 return lp_ctx->s3_fns->fn_name(); \
270 return lp_ctx->globals->var_name; \
273 #define FN_GLOBAL_INTEGER(fn_name,var_name) \
274 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
275 if (lp_ctx->s3_fns) { \
276 SMB_ASSERT(lp_ctx->s3_fns->fn_name); \
277 return lp_ctx->s3_fns->fn_name(); \
279 return lp_ctx->globals->var_name; \
282 /* Local parameters don't need the ->s3_fns because the struct
283 * loadparm_service is shared and lpcfg_service() checks the ->s3_fns
284 * hook */
285 #define FN_LOCAL_STRING(fn_name,val) \
286 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_service *service, \
287 struct loadparm_service *sDefault) { \
288 return(lp_string((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val))); \
291 #define FN_LOCAL_CONST_STRING(fn_name,val) FN_LOCAL_STRING(fn_name, val)
293 #define FN_LOCAL_LIST(fn_name,val) \
294 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_service *service, \
295 struct loadparm_service *sDefault) {\
296 return(const char **)(service != NULL && service->val != NULL? service->val : sDefault->val); \
299 #define FN_LOCAL_PARM_BOOL(fn_name, val) FN_LOCAL_BOOL(fn_name, val)
301 #define FN_LOCAL_BOOL(fn_name,val) \
302 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_service *service, \
303 struct loadparm_service *sDefault) { \
304 return((service != NULL)? service->val : sDefault->val); \
307 #define FN_LOCAL_INTEGER(fn_name,val) \
308 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_service *service, \
309 struct loadparm_service *sDefault) { \
310 return((service != NULL)? service->val : sDefault->val); \
313 #define FN_LOCAL_PARM_INTEGER(fn_name, val) FN_LOCAL_INTEGER(fn_name, val)
315 #define FN_LOCAL_PARM_CHAR(fn_name,val) \
316 _PUBLIC_ char lpcfg_ ## fn_name(struct loadparm_service *service, \
317 struct loadparm_service *sDefault) { \
318 return((service != NULL)? service->val : sDefault->val); \
321 #include "lib/param/param_functions.c"
323 /* These functions remain only in lib/param for now */
324 FN_GLOBAL_BOOL(readraw, bReadRaw)
325 FN_GLOBAL_BOOL(writeraw, bWriteRaw)
326 FN_GLOBAL_CONST_STRING(cachedir, szCacheDir)
327 FN_GLOBAL_CONST_STRING(statedir, szStateDir)
329 /* local prototypes */
330 static int map_parameter(const char *pszParmName);
331 static struct loadparm_service *getservicebyname(struct loadparm_context *lp_ctx,
332 const char *pszServiceName);
333 static void copy_service(struct loadparm_service *pserviceDest,
334 struct loadparm_service *pserviceSource,
335 struct bitmap *pcopymapDest);
336 static bool lpcfg_service_ok(struct loadparm_service *service);
337 static bool do_section(const char *pszSectionName, void *);
338 static void init_copymap(struct loadparm_service *pservice);
340 /* This is a helper function for parametrical options support. */
341 /* It returns a pointer to parametrical option value if it exists or NULL otherwise */
342 /* Actual parametrical functions are quite simple */
343 const char *lpcfg_get_parametric(struct loadparm_context *lp_ctx,
344 struct loadparm_service *service,
345 const char *type, const char *option)
347 char *vfskey_tmp = NULL;
348 char *vfskey = NULL;
349 struct parmlist_entry *data;
351 if (lp_ctx == NULL)
352 return NULL;
354 if (lp_ctx->s3_fns) {
355 return lp_ctx->s3_fns->get_parametric(service, type, option);
358 data = (service == NULL ? lp_ctx->globals->param_opt : service->param_opt);
360 vfskey_tmp = talloc_asprintf(NULL, "%s:%s", type, option);
361 if (vfskey_tmp == NULL) return NULL;
362 vfskey = strlower_talloc(NULL, vfskey_tmp);
363 talloc_free(vfskey_tmp);
365 while (data) {
366 if (strcmp(data->key, vfskey) == 0) {
367 talloc_free(vfskey);
368 return data->value;
370 data = data->next;
373 if (service != NULL) {
374 /* Try to fetch the same option but from globals */
375 /* but only if we are not already working with globals */
376 for (data = lp_ctx->globals->param_opt; data;
377 data = data->next) {
378 if (strcmp(data->key, vfskey) == 0) {
379 talloc_free(vfskey);
380 return data->value;
385 talloc_free(vfskey);
387 return NULL;
392 * convenience routine to return int parameters.
394 static int lp_int(const char *s)
397 if (!s) {
398 DEBUG(0,("lp_int(%s): is called with NULL!\n",s));
399 return -1;
402 return strtol(s, NULL, 0);
406 * convenience routine to return unsigned long parameters.
408 static unsigned long lp_ulong(const char *s)
411 if (!s) {
412 DEBUG(0,("lp_ulong(%s): is called with NULL!\n",s));
413 return -1;
416 return strtoul(s, NULL, 0);
420 * convenience routine to return unsigned long parameters.
422 static long lp_long(const char *s)
425 if (!s) {
426 DEBUG(0,("lp_long(%s): is called with NULL!\n",s));
427 return -1;
430 return strtol(s, NULL, 0);
434 * convenience routine to return unsigned long parameters.
436 static double lp_double(const char *s)
439 if (!s) {
440 DEBUG(0,("lp_double(%s): is called with NULL!\n",s));
441 return -1;
444 return strtod(s, NULL);
448 * convenience routine to return boolean parameters.
450 static bool lp_bool(const char *s)
452 bool ret = false;
454 if (!s) {
455 DEBUG(0,("lp_bool(%s): is called with NULL!\n",s));
456 return false;
459 if (!set_boolean(s, &ret)) {
460 DEBUG(0,("lp_bool(%s): value is not boolean!\n",s));
461 return false;
464 return ret;
469 * Return parametric option from a given service. Type is a part of option before ':'
470 * Parametric option has following syntax: 'Type: option = value'
471 * Returned value is allocated in 'lp_talloc' context
474 const char *lpcfg_parm_string(struct loadparm_context *lp_ctx,
475 struct loadparm_service *service, const char *type,
476 const char *option)
478 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
480 if (value)
481 return lp_string(value);
483 return NULL;
487 * Return parametric option from a given service. Type is a part of option before ':'
488 * Parametric option has following syntax: 'Type: option = value'
489 * Returned value is allocated in 'lp_talloc' context
492 const char **lpcfg_parm_string_list(TALLOC_CTX *mem_ctx,
493 struct loadparm_context *lp_ctx,
494 struct loadparm_service *service,
495 const char *type,
496 const char *option, const char *separator)
498 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
500 if (value != NULL)
501 return (const char **)str_list_make(mem_ctx, value, separator);
503 return NULL;
507 * Return parametric option from a given service. Type is a part of option before ':'
508 * Parametric option has following syntax: 'Type: option = value'
511 int lpcfg_parm_int(struct loadparm_context *lp_ctx,
512 struct loadparm_service *service, const char *type,
513 const char *option, int default_v)
515 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
517 if (value)
518 return lp_int(value);
520 return default_v;
524 * Return parametric option from a given service. Type is a part of
525 * option before ':'.
526 * Parametric option has following syntax: 'Type: option = value'.
529 int lpcfg_parm_bytes(struct loadparm_context *lp_ctx,
530 struct loadparm_service *service, const char *type,
531 const char *option, int default_v)
533 uint64_t bval;
535 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
537 if (value && conv_str_size_error(value, &bval)) {
538 if (bval <= INT_MAX) {
539 return (int)bval;
543 return default_v;
547 * Return parametric option from a given service.
548 * Type is a part of option before ':'
549 * Parametric option has following syntax: 'Type: option = value'
551 unsigned long lpcfg_parm_ulong(struct loadparm_context *lp_ctx,
552 struct loadparm_service *service, const char *type,
553 const char *option, unsigned long default_v)
555 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
557 if (value)
558 return lp_ulong(value);
560 return default_v;
563 long lpcfg_parm_long(struct loadparm_context *lp_ctx,
564 struct loadparm_service *service, const char *type,
565 const char *option, long default_v)
567 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
569 if (value)
570 return lp_long(value);
572 return default_v;
575 double lpcfg_parm_double(struct loadparm_context *lp_ctx,
576 struct loadparm_service *service, const char *type,
577 const char *option, double default_v)
579 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
581 if (value != NULL)
582 return lp_double(value);
584 return default_v;
588 * Return parametric option from a given service. Type is a part of option before ':'
589 * Parametric option has following syntax: 'Type: option = value'
592 bool lpcfg_parm_bool(struct loadparm_context *lp_ctx,
593 struct loadparm_service *service, const char *type,
594 const char *option, bool default_v)
596 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
598 if (value != NULL)
599 return lp_bool(value);
601 return default_v;
606 * Initialise a service to the defaults.
609 static struct loadparm_service *init_service(TALLOC_CTX *mem_ctx, struct loadparm_service *sDefault)
611 struct loadparm_service *pservice =
612 talloc_zero(mem_ctx, struct loadparm_service);
613 copy_service(pservice, sDefault, NULL);
614 return pservice;
618 * Set a string value, deallocating any existing space, and allocing the space
619 * for the string
621 static bool lpcfg_string_set(TALLOC_CTX *mem_ctx, char **dest, const char *src)
623 talloc_free(*dest);
625 if (src == NULL)
626 src = "";
628 *dest = talloc_strdup(mem_ctx, src);
629 if ((*dest) == NULL) {
630 DEBUG(0,("Out of memory in string_set\n"));
631 return false;
634 return true;
638 * Set a string value, deallocating any existing space, and allocing the space
639 * for the string
641 static bool lpcfg_string_set_upper(TALLOC_CTX *mem_ctx, char **dest, const char *src)
643 talloc_free(*dest);
645 if (src == NULL)
646 src = "";
648 *dest = strupper_talloc(mem_ctx, src);
649 if ((*dest) == NULL) {
650 DEBUG(0,("Out of memory in string_set_upper\n"));
651 return false;
654 return true;
660 * Add a new service to the services array initialising it with the given
661 * service.
664 struct loadparm_service *lpcfg_add_service(struct loadparm_context *lp_ctx,
665 const struct loadparm_service *pservice,
666 const char *name)
668 int i;
669 struct loadparm_service tservice;
670 int num_to_alloc = lp_ctx->iNumServices + 1;
671 struct parmlist_entry *data, *pdata;
673 if (pservice == NULL) {
674 pservice = lp_ctx->sDefault;
677 tservice = *pservice;
679 /* it might already exist */
680 if (name) {
681 struct loadparm_service *service = getservicebyname(lp_ctx,
682 name);
683 if (service != NULL) {
684 /* Clean all parametric options for service */
685 /* They will be added during parsing again */
686 data = service->param_opt;
687 while (data) {
688 pdata = data->next;
689 talloc_free(data);
690 data = pdata;
692 service->param_opt = NULL;
693 return service;
697 /* find an invalid one */
698 for (i = 0; i < lp_ctx->iNumServices; i++)
699 if (lp_ctx->services[i] == NULL)
700 break;
702 /* if not, then create one */
703 if (i == lp_ctx->iNumServices) {
704 struct loadparm_service **tsp;
706 tsp = talloc_realloc(lp_ctx, lp_ctx->services, struct loadparm_service *, num_to_alloc);
708 if (!tsp) {
709 DEBUG(0,("lpcfg_add_service: failed to enlarge services!\n"));
710 return NULL;
711 } else {
712 lp_ctx->services = tsp;
713 lp_ctx->services[lp_ctx->iNumServices] = NULL;
716 lp_ctx->iNumServices++;
719 lp_ctx->services[i] = init_service(lp_ctx->services, lp_ctx->sDefault);
720 if (lp_ctx->services[i] == NULL) {
721 DEBUG(0,("lpcfg_add_service: out of memory!\n"));
722 return NULL;
724 copy_service(lp_ctx->services[i], &tservice, NULL);
725 if (name != NULL)
726 lpcfg_string_set(lp_ctx->services[i], &lp_ctx->services[i]->szService, name);
727 return lp_ctx->services[i];
731 * Add a new home service, with the specified home directory, defaults coming
732 * from service ifrom.
735 bool lpcfg_add_home(struct loadparm_context *lp_ctx,
736 const char *pszHomename,
737 struct loadparm_service *default_service,
738 const char *user, const char *pszHomedir)
740 struct loadparm_service *service;
742 service = lpcfg_add_service(lp_ctx, default_service, pszHomename);
744 if (service == NULL)
745 return false;
747 if (!(*(default_service->pathname))
748 || strequal(default_service->pathname, lp_ctx->sDefault->pathname)) {
749 service->pathname = talloc_strdup(service, pszHomedir);
750 } else {
751 service->pathname = string_sub_talloc(service, lpcfg_pathname(default_service, lp_ctx->sDefault), "%H", pszHomedir);
754 if (!(*(service->comment))) {
755 service->comment = talloc_asprintf(service, "Home directory of %s", user);
757 service->bAvailable = default_service->bAvailable;
758 service->browseable = default_service->browseable;
760 DEBUG(3, ("adding home's share [%s] for user '%s' at '%s'\n",
761 pszHomename, user, service->pathname));
763 return true;
767 * Add a new printer service, with defaults coming from service iFrom.
770 bool lpcfg_add_printer(struct loadparm_context *lp_ctx,
771 const char *pszPrintername,
772 struct loadparm_service *default_service)
774 const char *comment = "From Printcap";
775 struct loadparm_service *service;
776 service = lpcfg_add_service(lp_ctx, default_service, pszPrintername);
778 if (service == NULL)
779 return false;
781 /* note that we do NOT default the availability flag to True - */
782 /* we take it from the default service passed. This allows all */
783 /* dynamic printers to be disabled by disabling the [printers] */
784 /* entry (if/when the 'available' keyword is implemented!). */
786 /* the printer name is set to the service name. */
787 lpcfg_string_set(service, &service->szPrintername, pszPrintername);
788 lpcfg_string_set(service, &service->comment, comment);
789 service->browseable = default_service->browseable;
790 /* Printers cannot be read_only. */
791 service->readonly = false;
792 /* Printer services must be printable. */
793 service->print_ok = true;
795 DEBUG(3, ("adding printer service %s\n", pszPrintername));
797 return true;
801 * Map a parameter's string representation to something we can use.
802 * Returns False if the parameter string is not recognised, else TRUE.
805 static int map_parameter(const char *pszParmName)
807 int iIndex;
809 if (*pszParmName == '-')
810 return -1;
812 for (iIndex = 0; parm_table[iIndex].label; iIndex++)
813 if (strwicmp(parm_table[iIndex].label, pszParmName) == 0)
814 return iIndex;
816 /* Warn only if it isn't parametric option */
817 if (strchr(pszParmName, ':') == NULL)
818 DEBUG(0, ("Unknown parameter encountered: \"%s\"\n", pszParmName));
819 /* We do return 'fail' for parametric options as well because they are
820 stored in different storage
822 return -1;
827 return the parameter structure for a parameter
829 struct parm_struct *lpcfg_parm_struct(struct loadparm_context *lp_ctx, const char *name)
831 int parmnum;
833 if (lp_ctx->s3_fns) {
834 return lp_ctx->s3_fns->get_parm_struct(name);
837 parmnum = map_parameter(name);
838 if (parmnum == -1) return NULL;
839 return &parm_table[parmnum];
843 return the parameter pointer for a parameter
845 void *lpcfg_parm_ptr(struct loadparm_context *lp_ctx,
846 struct loadparm_service *service, struct parm_struct *parm)
848 if (lp_ctx->s3_fns) {
849 return lp_ctx->s3_fns->get_parm_ptr(service, parm);
852 if (service == NULL) {
853 if (parm->p_class == P_LOCAL)
854 return ((char *)lp_ctx->sDefault)+parm->offset;
855 else if (parm->p_class == P_GLOBAL)
856 return ((char *)lp_ctx->globals)+parm->offset;
857 else return NULL;
858 } else {
859 return ((char *)service) + parm->offset;
864 return the parameter pointer for a parameter
866 bool lpcfg_parm_is_cmdline(struct loadparm_context *lp_ctx, const char *name)
868 int parmnum;
870 if (lp_ctx->s3_fns) {
871 struct parm_struct *parm = lp_ctx->s3_fns->get_parm_struct(name);
872 if (parm) {
873 return parm->flags & FLAG_CMDLINE;
875 return false;
878 parmnum = map_parameter(name);
879 if (parmnum == -1) return false;
881 return lp_ctx->flags[parmnum] & FLAG_CMDLINE;
885 * Find a service by name. Otherwise works like get_service.
888 static struct loadparm_service *getservicebyname(struct loadparm_context *lp_ctx,
889 const char *pszServiceName)
891 int iService;
893 if (lp_ctx->s3_fns) {
894 return lp_ctx->s3_fns->get_service(pszServiceName);
897 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--)
898 if (lp_ctx->services[iService] != NULL &&
899 strwicmp(lp_ctx->services[iService]->szService, pszServiceName) == 0) {
900 return lp_ctx->services[iService];
903 return NULL;
907 * Copy a service structure to another.
908 * If pcopymapDest is NULL then copy all fields
911 static void copy_service(struct loadparm_service *pserviceDest,
912 struct loadparm_service *pserviceSource,
913 struct bitmap *pcopymapDest)
915 int i;
916 bool bcopyall = (pcopymapDest == NULL);
917 struct parmlist_entry *data, *pdata, *paramo;
918 bool not_added;
920 for (i = 0; parm_table[i].label; i++)
921 if (parm_table[i].p_class == P_LOCAL &&
922 (bcopyall || bitmap_query(pcopymapDest, i))) {
923 void *src_ptr =
924 ((char *)pserviceSource) + parm_table[i].offset;
925 void *dest_ptr =
926 ((char *)pserviceDest) + parm_table[i].offset;
928 switch (parm_table[i].type) {
929 case P_BOOL:
930 *(bool *)dest_ptr = *(bool *)src_ptr;
931 break;
933 case P_INTEGER:
934 case P_BYTES:
935 case P_OCTAL:
936 case P_ENUM:
937 *(int *)dest_ptr = *(int *)src_ptr;
938 break;
940 case P_CHAR:
941 *(char *)dest_ptr = *(char *)src_ptr;
942 break;
944 case P_STRING:
945 lpcfg_string_set(pserviceDest,
946 (char **)dest_ptr,
947 *(char **)src_ptr);
948 break;
950 case P_USTRING:
951 lpcfg_string_set_upper(pserviceDest,
952 (char **)dest_ptr,
953 *(char **)src_ptr);
954 break;
955 case P_LIST:
956 *(const char ***)dest_ptr = (const char **)str_list_copy(pserviceDest,
957 *(const char ***)src_ptr);
958 break;
959 default:
960 break;
964 if (bcopyall) {
965 init_copymap(pserviceDest);
966 if (pserviceSource->copymap)
967 bitmap_copy(pserviceDest->copymap,
968 pserviceSource->copymap);
971 data = pserviceSource->param_opt;
972 while (data) {
973 not_added = true;
974 pdata = pserviceDest->param_opt;
975 /* Traverse destination */
976 while (pdata) {
977 /* If we already have same option, override it */
978 if (strcmp(pdata->key, data->key) == 0) {
979 talloc_free(pdata->value);
980 pdata->value = talloc_strdup(pdata,
981 data->value);
982 not_added = false;
983 break;
985 pdata = pdata->next;
987 if (not_added) {
988 paramo = talloc_zero(pserviceDest, struct parmlist_entry);
989 if (paramo == NULL)
990 smb_panic("OOM");
991 paramo->key = talloc_strdup(paramo, data->key);
992 paramo->value = talloc_strdup(paramo, data->value);
993 DLIST_ADD(pserviceDest->param_opt, paramo);
995 data = data->next;
1000 * Check a service for consistency. Return False if the service is in any way
1001 * incomplete or faulty, else True.
1003 static bool lpcfg_service_ok(struct loadparm_service *service)
1005 bool bRetval;
1007 bRetval = true;
1008 if (service->szService[0] == '\0') {
1009 DEBUG(0, ("The following message indicates an internal error:\n"));
1010 DEBUG(0, ("No service name in service entry.\n"));
1011 bRetval = false;
1014 /* The [printers] entry MUST be printable. I'm all for flexibility, but */
1015 /* I can't see why you'd want a non-printable printer service... */
1016 if (strwicmp(service->szService, PRINTERS_NAME) == 0) {
1017 if (!service->print_ok) {
1018 DEBUG(0, ("WARNING: [%s] service MUST be printable!\n",
1019 service->szService));
1020 service->print_ok = true;
1022 /* [printers] service must also be non-browsable. */
1023 if (service->browseable)
1024 service->browseable = false;
1027 /* If a service is flagged unavailable, log the fact at level 0. */
1028 if (!service->bAvailable)
1029 DEBUG(1, ("NOTE: Service %s is flagged unavailable.\n",
1030 service->szService));
1032 return bRetval;
1036 /*******************************************************************
1037 Keep a linked list of all config files so we know when one has changed
1038 it's date and needs to be reloaded.
1039 ********************************************************************/
1041 static void add_to_file_list(struct loadparm_context *lp_ctx,
1042 const char *fname, const char *subfname)
1044 struct file_lists *f = lp_ctx->file_lists;
1046 while (f) {
1047 if (f->name && !strcmp(f->name, fname))
1048 break;
1049 f = f->next;
1052 if (!f) {
1053 f = talloc(lp_ctx, struct file_lists);
1054 if (!f)
1055 return;
1056 f->next = lp_ctx->file_lists;
1057 f->name = talloc_strdup(f, fname);
1058 if (!f->name) {
1059 talloc_free(f);
1060 return;
1062 f->subfname = talloc_strdup(f, subfname);
1063 if (!f->subfname) {
1064 talloc_free(f);
1065 return;
1067 lp_ctx->file_lists = f;
1068 f->modtime = file_modtime(subfname);
1069 } else {
1070 time_t t = file_modtime(subfname);
1071 if (t)
1072 f->modtime = t;
1076 /*******************************************************************
1077 Check if a config file has changed date.
1078 ********************************************************************/
1079 bool lpcfg_file_list_changed(struct loadparm_context *lp_ctx)
1081 struct file_lists *f;
1082 DEBUG(6, ("lpcfg_file_list_changed()\n"));
1084 for (f = lp_ctx->file_lists; f != NULL; f = f->next) {
1085 char *n2;
1086 time_t mod_time;
1088 n2 = standard_sub_basic(lp_ctx, f->name);
1090 DEBUGADD(6, ("file %s -> %s last mod_time: %s\n",
1091 f->name, n2, ctime(&f->modtime)));
1093 mod_time = file_modtime(n2);
1095 if (mod_time && ((f->modtime != mod_time) || (f->subfname == NULL) || (strcmp(n2, f->subfname) != 0))) {
1096 DEBUGADD(6, ("file %s modified: %s\n", n2,
1097 ctime(&mod_time)));
1098 f->modtime = mod_time;
1099 talloc_free(f->subfname);
1100 f->subfname = talloc_strdup(f, n2);
1101 return true;
1104 return false;
1107 /***************************************************************************
1108 Handle the "realm" parameter
1109 ***************************************************************************/
1111 static bool handle_realm(struct loadparm_context *lp_ctx, int unused,
1112 const char *pszParmValue, char **ptr)
1114 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1116 talloc_free(lp_ctx->globals->realm);
1117 talloc_free(lp_ctx->globals->dnsdomain);
1119 lp_ctx->globals->realm = strupper_talloc(lp_ctx, pszParmValue);
1120 lp_ctx->globals->dnsdomain = strlower_talloc(lp_ctx, pszParmValue);
1122 return true;
1125 /***************************************************************************
1126 Handle the include operation.
1127 ***************************************************************************/
1129 static bool handle_include(struct loadparm_context *lp_ctx, int unused,
1130 const char *pszParmValue, char **ptr)
1132 char *fname = standard_sub_basic(lp_ctx, pszParmValue);
1134 add_to_file_list(lp_ctx, pszParmValue, fname);
1136 lpcfg_string_set(lp_ctx, ptr, fname);
1138 if (file_exist(fname))
1139 return pm_process(fname, do_section, do_parameter, lp_ctx);
1141 DEBUG(2, ("Can't find include file %s\n", fname));
1143 return false;
1146 /***************************************************************************
1147 Handle the interpretation of the copy parameter.
1148 ***************************************************************************/
1150 static bool handle_copy(struct loadparm_context *lp_ctx, int unused,
1151 const char *pszParmValue, char **ptr)
1153 bool bRetval;
1154 struct loadparm_service *serviceTemp;
1156 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1158 bRetval = false;
1160 DEBUG(3, ("Copying service from service %s\n", pszParmValue));
1162 if ((serviceTemp = getservicebyname(lp_ctx, pszParmValue)) != NULL) {
1163 if (serviceTemp == lp_ctx->currentService) {
1164 DEBUG(0, ("Can't copy service %s - unable to copy self!\n", pszParmValue));
1165 } else {
1166 copy_service(lp_ctx->currentService,
1167 serviceTemp,
1168 lp_ctx->currentService->copymap);
1169 bRetval = true;
1171 } else {
1172 DEBUG(0, ("Unable to copy service - source not found: %s\n",
1173 pszParmValue));
1174 bRetval = false;
1177 return bRetval;
1180 static bool handle_debug_list(struct loadparm_context *lp_ctx, int unused,
1181 const char *pszParmValue, char **ptr)
1184 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1185 if (lp_ctx->global) {
1186 return debug_parse_levels(pszParmValue);
1188 return true;
1191 static bool handle_logfile(struct loadparm_context *lp_ctx, int unused,
1192 const char *pszParmValue, char **ptr)
1194 debug_set_logfile(pszParmValue);
1195 if (lp_ctx->global) {
1196 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1198 return true;
1201 /***************************************************************************
1202 Initialise a copymap.
1203 ***************************************************************************/
1205 static void init_copymap(struct loadparm_service *pservice)
1207 int i;
1209 TALLOC_FREE(pservice->copymap);
1211 pservice->copymap = bitmap_talloc(NULL, NUMPARAMETERS);
1212 if (!pservice->copymap)
1213 DEBUG(0,
1214 ("Couldn't allocate copymap!! (size %d)\n",
1215 (int)NUMPARAMETERS));
1216 else
1217 for (i = 0; i < NUMPARAMETERS; i++)
1218 bitmap_set(pservice->copymap, i);
1222 * Process a parametric option
1224 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
1225 struct loadparm_service *service,
1226 const char *pszParmName,
1227 const char *pszParmValue, int flags)
1229 struct parmlist_entry *paramo, *data;
1230 char *name;
1231 TALLOC_CTX *mem_ctx;
1233 while (isspace((unsigned char)*pszParmName)) {
1234 pszParmName++;
1237 name = strlower_talloc(lp_ctx, pszParmName);
1238 if (!name) return false;
1240 if (service == NULL) {
1241 data = lp_ctx->globals->param_opt;
1242 mem_ctx = lp_ctx->globals;
1243 } else {
1244 data = service->param_opt;
1245 mem_ctx = service;
1248 /* Traverse destination */
1249 for (paramo=data; paramo; paramo=paramo->next) {
1250 /* If we already have the option set, override it unless
1251 it was a command line option and the new one isn't */
1252 if (strcmp(paramo->key, name) == 0) {
1253 if ((paramo->priority & FLAG_CMDLINE) &&
1254 !(flags & FLAG_CMDLINE)) {
1255 talloc_free(name);
1256 return true;
1259 talloc_free(paramo->value);
1260 paramo->value = talloc_strdup(paramo, pszParmValue);
1261 paramo->priority = flags;
1262 talloc_free(name);
1263 return true;
1267 paramo = talloc_zero(mem_ctx, struct parmlist_entry);
1268 if (!paramo)
1269 smb_panic("OOM");
1270 paramo->key = talloc_strdup(paramo, name);
1271 paramo->value = talloc_strdup(paramo, pszParmValue);
1272 paramo->priority = flags;
1273 if (service == NULL) {
1274 DLIST_ADD(lp_ctx->globals->param_opt, paramo);
1275 } else {
1276 DLIST_ADD(service->param_opt, paramo);
1279 talloc_free(name);
1281 return true;
1284 static bool set_variable(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
1285 const char *pszParmName, const char *pszParmValue,
1286 struct loadparm_context *lp_ctx, bool on_globals)
1288 int i;
1289 /* if it is a special case then go ahead */
1290 if (parm_table[parmnum].special) {
1291 bool ret;
1292 ret = parm_table[parmnum].special(lp_ctx, -1, pszParmValue,
1293 (char **)parm_ptr);
1294 if (!ret) {
1295 return false;
1297 goto mark_non_default;
1300 /* now switch on the type of variable it is */
1301 switch (parm_table[parmnum].type)
1303 case P_BOOL: {
1304 bool b;
1305 if (!set_boolean(pszParmValue, &b)) {
1306 DEBUG(0, ("set_variable(%s): value is not "
1307 "boolean!\n", pszParmValue));
1308 return false;
1310 *(bool *)parm_ptr = b;
1312 break;
1314 case P_BOOLREV: {
1315 bool b;
1316 if (!set_boolean(pszParmValue, &b)) {
1317 DEBUG(0, ("set_variable(%s): value is not "
1318 "boolean!\n", pszParmValue));
1319 return false;
1321 *(bool *)parm_ptr = !b;
1323 break;
1325 case P_INTEGER:
1326 *(int *)parm_ptr = atoi(pszParmValue);
1327 break;
1329 case P_CHAR:
1330 *(char *)parm_ptr = *pszParmValue;
1331 break;
1333 case P_OCTAL:
1334 *(int *)parm_ptr = strtol(pszParmValue, NULL, 8);
1335 break;
1337 case P_BYTES:
1339 uint64_t val;
1340 if (conv_str_size_error(pszParmValue, &val)) {
1341 if (val <= INT_MAX) {
1342 *(int *)parm_ptr = (int)val;
1343 break;
1347 DEBUG(0, ("set_variable(%s): value is not "
1348 "a valid size specifier!\n", pszParmValue));
1349 return false;
1352 case P_CMDLIST:
1353 *(const char ***)parm_ptr = (const char **)str_list_make(mem_ctx,
1354 pszParmValue, NULL);
1355 break;
1356 case P_LIST:
1358 char **new_list = str_list_make(mem_ctx,
1359 pszParmValue, NULL);
1360 for (i=0; new_list[i]; i++) {
1361 if (*(const char ***)parm_ptr != NULL &&
1362 new_list[i][0] == '+' &&
1363 new_list[i][1])
1365 if (!str_list_check(*(const char ***)parm_ptr,
1366 &new_list[i][1])) {
1367 *(const char ***)parm_ptr = str_list_add(*(const char ***)parm_ptr,
1368 &new_list[i][1]);
1370 } else if (*(const char ***)parm_ptr != NULL &&
1371 new_list[i][0] == '-' &&
1372 new_list[i][1])
1374 str_list_remove(*(const char ***)parm_ptr,
1375 &new_list[i][1]);
1376 } else {
1377 if (i != 0) {
1378 DEBUG(0, ("Unsupported list syntax for: %s = %s\n",
1379 pszParmName, pszParmValue));
1380 return false;
1382 *(const char ***)parm_ptr = (const char **) new_list;
1383 break;
1386 break;
1388 case P_STRING:
1389 lpcfg_string_set(mem_ctx, (char **)parm_ptr, pszParmValue);
1390 break;
1392 case P_USTRING:
1393 lpcfg_string_set_upper(mem_ctx, (char **)parm_ptr, pszParmValue);
1394 break;
1396 case P_ENUM:
1397 for (i = 0; parm_table[parmnum].enum_list[i].name; i++) {
1398 if (strequal
1399 (pszParmValue,
1400 parm_table[parmnum].enum_list[i].name)) {
1401 *(int *)parm_ptr =
1402 parm_table[parmnum].
1403 enum_list[i].value;
1404 break;
1407 if (!parm_table[parmnum].enum_list[i].name) {
1408 DEBUG(0,("Unknown enumerated value '%s' for '%s'\n",
1409 pszParmValue, pszParmName));
1410 return false;
1412 break;
1414 case P_SEP:
1415 break;
1418 mark_non_default:
1419 if (on_globals && (lp_ctx->flags[parmnum] & FLAG_DEFAULT)) {
1420 lp_ctx->flags[parmnum] &= ~FLAG_DEFAULT;
1421 /* we have to also unset FLAG_DEFAULT on aliases */
1422 for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
1423 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1425 for (i=parmnum+1;i<NUMPARAMETERS && parm_table[i].offset == parm_table[parmnum].offset;i++) {
1426 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1429 return true;
1433 bool lpcfg_do_global_parameter(struct loadparm_context *lp_ctx,
1434 const char *pszParmName, const char *pszParmValue)
1436 int parmnum = map_parameter(pszParmName);
1437 void *parm_ptr;
1439 if (parmnum < 0) {
1440 if (strchr(pszParmName, ':')) {
1441 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName, pszParmValue, 0);
1443 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1444 return true;
1447 /* if the flag has been set on the command line, then don't allow override,
1448 but don't report an error */
1449 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1450 return true;
1453 parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[parmnum]);
1455 return set_variable(lp_ctx->globals, parmnum, parm_ptr,
1456 pszParmName, pszParmValue, lp_ctx, true);
1459 bool lpcfg_do_service_parameter(struct loadparm_context *lp_ctx,
1460 struct loadparm_service *service,
1461 const char *pszParmName, const char *pszParmValue)
1463 void *parm_ptr;
1464 int i;
1465 int parmnum = map_parameter(pszParmName);
1467 if (parmnum < 0) {
1468 if (strchr(pszParmName, ':')) {
1469 return lp_do_parameter_parametric(lp_ctx, service, pszParmName, pszParmValue, 0);
1471 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1472 return true;
1475 /* if the flag has been set on the command line, then don't allow override,
1476 but don't report an error */
1477 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1478 return true;
1481 if (parm_table[parmnum].p_class == P_GLOBAL) {
1482 DEBUG(0,
1483 ("Global parameter %s found in service section!\n",
1484 pszParmName));
1485 return true;
1487 parm_ptr = ((char *)service) + parm_table[parmnum].offset;
1489 if (!service->copymap)
1490 init_copymap(service);
1492 /* this handles the aliases - set the copymap for other
1493 * entries with the same data pointer */
1494 for (i = 0; parm_table[i].label; i++)
1495 if (parm_table[i].offset == parm_table[parmnum].offset &&
1496 parm_table[i].p_class == parm_table[parmnum].p_class)
1497 bitmap_clear(service->copymap, i);
1499 return set_variable(service, parmnum, parm_ptr, pszParmName,
1500 pszParmValue, lp_ctx, false);
1504 * Process a parameter.
1507 static bool do_parameter(const char *pszParmName, const char *pszParmValue,
1508 void *userdata)
1510 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1512 if (lp_ctx->bInGlobalSection)
1513 return lpcfg_do_global_parameter(lp_ctx, pszParmName,
1514 pszParmValue);
1515 else
1516 return lpcfg_do_service_parameter(lp_ctx, lp_ctx->currentService,
1517 pszParmName, pszParmValue);
1521 variable argument do parameter
1523 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx, const char *pszParmName, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
1524 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx,
1525 const char *pszParmName, const char *fmt, ...)
1527 char *s;
1528 bool ret;
1529 va_list ap;
1531 va_start(ap, fmt);
1532 s = talloc_vasprintf(NULL, fmt, ap);
1533 va_end(ap);
1534 ret = lpcfg_do_global_parameter(lp_ctx, pszParmName, s);
1535 talloc_free(s);
1536 return ret;
1541 set a parameter from the commandline - this is called from command line parameter
1542 parsing code. It sets the parameter then marks the parameter as unable to be modified
1543 by smb.conf processing
1545 bool lpcfg_set_cmdline(struct loadparm_context *lp_ctx, const char *pszParmName,
1546 const char *pszParmValue)
1548 int parmnum;
1549 int i;
1551 if (lp_ctx->s3_fns) {
1552 return lp_ctx->s3_fns->set_cmdline(pszParmName, pszParmValue);
1555 parmnum = map_parameter(pszParmName);
1557 while (isspace((unsigned char)*pszParmValue)) pszParmValue++;
1560 if (parmnum < 0 && strchr(pszParmName, ':')) {
1561 /* set a parametric option */
1562 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName,
1563 pszParmValue, FLAG_CMDLINE);
1566 if (parmnum < 0) {
1567 DEBUG(0,("Unknown option '%s'\n", pszParmName));
1568 return false;
1571 /* reset the CMDLINE flag in case this has been called before */
1572 lp_ctx->flags[parmnum] &= ~FLAG_CMDLINE;
1574 if (!lpcfg_do_global_parameter(lp_ctx, pszParmName, pszParmValue)) {
1575 return false;
1578 lp_ctx->flags[parmnum] |= FLAG_CMDLINE;
1580 /* we have to also set FLAG_CMDLINE on aliases */
1581 for (i=parmnum-1;
1582 i>=0 && parm_table[i].p_class == parm_table[parmnum].p_class &&
1583 parm_table[i].offset == parm_table[parmnum].offset;
1584 i--) {
1585 lp_ctx->flags[i] |= FLAG_CMDLINE;
1587 for (i=parmnum+1;
1588 i<NUMPARAMETERS &&
1589 parm_table[i].p_class == parm_table[parmnum].p_class &&
1590 parm_table[i].offset == parm_table[parmnum].offset;
1591 i++) {
1592 lp_ctx->flags[i] |= FLAG_CMDLINE;
1595 return true;
1599 set a option from the commandline in 'a=b' format. Use to support --option
1601 bool lpcfg_set_option(struct loadparm_context *lp_ctx, const char *option)
1603 char *p, *s;
1604 bool ret;
1606 s = talloc_strdup(NULL, option);
1607 if (!s) {
1608 return false;
1611 p = strchr(s, '=');
1612 if (!p) {
1613 talloc_free(s);
1614 return false;
1617 *p = 0;
1619 ret = lpcfg_set_cmdline(lp_ctx, s, p+1);
1620 talloc_free(s);
1621 return ret;
1625 #define BOOLSTR(b) ((b) ? "Yes" : "No")
1628 * Print a parameter of the specified type.
1631 static void print_parameter(struct parm_struct *p, void *ptr, FILE * f)
1633 /* For the seperation of lists values that we print below */
1634 const char *list_sep = ", ";
1635 int i;
1636 switch (p->type)
1638 case P_ENUM:
1639 for (i = 0; p->enum_list[i].name; i++) {
1640 if (*(int *)ptr == p->enum_list[i].value) {
1641 fprintf(f, "%s",
1642 p->enum_list[i].name);
1643 break;
1646 break;
1648 case P_BOOL:
1649 fprintf(f, "%s", BOOLSTR(*(bool *)ptr));
1650 break;
1652 case P_BOOLREV:
1653 fprintf(f, "%s", BOOLSTR(!*(bool *)ptr));
1654 break;
1656 case P_INTEGER:
1657 case P_BYTES:
1658 fprintf(f, "%d", *(int *)ptr);
1659 break;
1661 case P_CHAR:
1662 fprintf(f, "%c", *(char *)ptr);
1663 break;
1665 case P_OCTAL: {
1666 int val = *(int *)ptr;
1667 if (val == -1) {
1668 fprintf(f, "-1");
1669 } else {
1670 fprintf(f, "0%o", val);
1672 break;
1675 case P_CMDLIST:
1676 list_sep = " ";
1677 /* fall through */
1678 case P_LIST:
1679 if ((char ***)ptr && *(char ***)ptr) {
1680 char **list = *(char ***)ptr;
1681 for (; *list; list++) {
1682 /* surround strings with whitespace in double quotes */
1683 if (*(list+1) == NULL) {
1684 /* last item, no extra separator */
1685 list_sep = "";
1687 if ( strchr_m( *list, ' ' ) ) {
1688 fprintf(f, "\"%s\"%s", *list, list_sep);
1689 } else {
1690 fprintf(f, "%s%s", *list, list_sep);
1694 break;
1696 case P_STRING:
1697 case P_USTRING:
1698 if (*(char **)ptr) {
1699 fprintf(f, "%s", *(char **)ptr);
1701 break;
1702 case P_SEP:
1703 break;
1708 * Check if two parameters are equal.
1711 static bool equal_parameter(parm_type type, void *ptr1, void *ptr2)
1713 switch (type) {
1714 case P_BOOL:
1715 case P_BOOLREV:
1716 return (*((bool *)ptr1) == *((bool *)ptr2));
1718 case P_INTEGER:
1719 case P_ENUM:
1720 case P_OCTAL:
1721 case P_BYTES:
1722 return (*((int *)ptr1) == *((int *)ptr2));
1724 case P_CHAR:
1725 return (*((char *)ptr1) == *((char *)ptr2));
1727 case P_LIST:
1728 case P_CMDLIST:
1729 return str_list_equal(*(const char ***)ptr1, *(const char ***)ptr2);
1731 case P_STRING:
1732 case P_USTRING:
1734 char *p1 = *(char **)ptr1, *p2 = *(char **)ptr2;
1735 if (p1 && !*p1)
1736 p1 = NULL;
1737 if (p2 && !*p2)
1738 p2 = NULL;
1739 return (p1 == p2 || strequal(p1, p2));
1741 case P_SEP:
1742 break;
1744 return false;
1748 * Process a new section (service).
1750 * At this stage all sections are services.
1751 * Later we'll have special sections that permit server parameters to be set.
1752 * Returns True on success, False on failure.
1755 static bool do_section(const char *pszSectionName, void *userdata)
1757 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1758 bool bRetval;
1759 bool isglobal = ((strwicmp(pszSectionName, GLOBAL_NAME) == 0) ||
1760 (strwicmp(pszSectionName, GLOBAL_NAME2) == 0));
1761 bRetval = false;
1763 /* if we've just struck a global section, note the fact. */
1764 lp_ctx->bInGlobalSection = isglobal;
1766 /* check for multiple global sections */
1767 if (lp_ctx->bInGlobalSection) {
1768 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1769 return true;
1772 /* if we have a current service, tidy it up before moving on */
1773 bRetval = true;
1775 if (lp_ctx->currentService != NULL)
1776 bRetval = lpcfg_service_ok(lp_ctx->currentService);
1778 /* if all is still well, move to the next record in the services array */
1779 if (bRetval) {
1780 /* We put this here to avoid an odd message order if messages are */
1781 /* issued by the post-processing of a previous section. */
1782 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1784 if ((lp_ctx->currentService = lpcfg_add_service(lp_ctx, lp_ctx->sDefault,
1785 pszSectionName))
1786 == NULL) {
1787 DEBUG(0, ("Failed to add a new service\n"));
1788 return false;
1792 return bRetval;
1797 * Determine if a particular base parameter is currently set to the default value.
1800 static bool is_default(struct loadparm_service *sDefault, int i)
1802 void *def_ptr = ((char *)sDefault) + parm_table[i].offset;
1803 if (!defaults_saved)
1804 return false;
1805 switch (parm_table[i].type) {
1806 case P_CMDLIST:
1807 case P_LIST:
1808 return str_list_equal((const char **)parm_table[i].def.lvalue,
1809 (const char **)def_ptr);
1810 case P_STRING:
1811 case P_USTRING:
1812 return strequal(parm_table[i].def.svalue,
1813 *(char **)def_ptr);
1814 case P_BOOL:
1815 case P_BOOLREV:
1816 return parm_table[i].def.bvalue ==
1817 *(bool *)def_ptr;
1818 case P_INTEGER:
1819 case P_CHAR:
1820 case P_OCTAL:
1821 case P_BYTES:
1822 case P_ENUM:
1823 return parm_table[i].def.ivalue ==
1824 *(int *)def_ptr;
1825 case P_SEP:
1826 break;
1828 return false;
1832 *Display the contents of the global structure.
1835 static void dump_globals(struct loadparm_context *lp_ctx, FILE *f,
1836 bool show_defaults)
1838 int i;
1839 struct parmlist_entry *data;
1841 fprintf(f, "# Global parameters\n[global]\n");
1843 for (i = 0; parm_table[i].label; i++)
1844 if (parm_table[i].p_class == P_GLOBAL &&
1845 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset))) {
1846 if (!show_defaults && (lp_ctx->flags[i] & FLAG_DEFAULT))
1847 continue;
1848 fprintf(f, "\t%s = ", parm_table[i].label);
1849 print_parameter(&parm_table[i], lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[i]), f);
1850 fprintf(f, "\n");
1852 if (lp_ctx->globals->param_opt != NULL) {
1853 for (data = lp_ctx->globals->param_opt; data;
1854 data = data->next) {
1855 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
1856 continue;
1858 fprintf(f, "\t%s = %s\n", data->key, data->value);
1865 * Display the contents of a single services record.
1868 static void dump_a_service(struct loadparm_service * pService, struct loadparm_service *sDefault, FILE * f,
1869 unsigned int *flags)
1871 int i;
1872 struct parmlist_entry *data;
1874 if (pService != sDefault)
1875 fprintf(f, "\n[%s]\n", pService->szService);
1877 for (i = 0; parm_table[i].label; i++) {
1878 if (parm_table[i].p_class == P_LOCAL &&
1879 (*parm_table[i].label != '-') &&
1880 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset)))
1882 if (pService == sDefault) {
1883 if (flags && (flags[i] & FLAG_DEFAULT)) {
1884 continue;
1886 if (defaults_saved) {
1887 if (is_default(sDefault, i)) {
1888 continue;
1891 } else {
1892 if (equal_parameter(parm_table[i].type,
1893 ((char *)pService) +
1894 parm_table[i].offset,
1895 ((char *)sDefault) +
1896 parm_table[i].offset))
1897 continue;
1900 fprintf(f, "\t%s = ", parm_table[i].label);
1901 print_parameter(&parm_table[i],
1902 ((char *)pService) + parm_table[i].offset, f);
1903 fprintf(f, "\n");
1906 if (pService->param_opt != NULL) {
1907 for (data = pService->param_opt; data; data = data->next) {
1908 fprintf(f, "\t%s = %s\n", data->key, data->value);
1913 bool lpcfg_dump_a_parameter(struct loadparm_context *lp_ctx,
1914 struct loadparm_service *service,
1915 const char *parm_name, FILE * f)
1917 struct parm_struct *parm;
1918 void *ptr;
1920 parm = lpcfg_parm_struct(lp_ctx, parm_name);
1921 if (!parm) {
1922 return false;
1925 if (service != NULL && parm->p_class == P_GLOBAL) {
1926 return false;
1929 ptr = lpcfg_parm_ptr(lp_ctx, service,parm);
1931 print_parameter(parm, ptr, f);
1932 fprintf(f, "\n");
1933 return true;
1937 * Return info about the next parameter in a service.
1938 * snum==-1 gives the globals.
1939 * Return NULL when out of parameters.
1943 struct parm_struct *lpcfg_next_parameter(struct loadparm_context *lp_ctx, int snum, int *i,
1944 int allparameters)
1946 if (snum == -1) {
1947 /* do the globals */
1948 for (; parm_table[*i].label; (*i)++) {
1949 if ((*parm_table[*i].label == '-'))
1950 continue;
1952 if ((*i) > 0
1953 && (parm_table[*i].offset ==
1954 parm_table[(*i) - 1].offset)
1955 && (parm_table[*i].p_class ==
1956 parm_table[(*i) - 1].p_class))
1957 continue;
1959 return &parm_table[(*i)++];
1961 } else {
1962 struct loadparm_service *pService = lp_ctx->services[snum];
1964 for (; parm_table[*i].label; (*i)++) {
1965 if (parm_table[*i].p_class == P_LOCAL &&
1966 (*parm_table[*i].label != '-') &&
1967 ((*i) == 0 ||
1968 (parm_table[*i].offset !=
1969 parm_table[(*i) - 1].offset)))
1971 if (allparameters ||
1972 !equal_parameter(parm_table[*i].type,
1973 ((char *)pService) +
1974 parm_table[*i].offset,
1975 ((char *)lp_ctx->sDefault) +
1976 parm_table[*i].offset))
1978 return &parm_table[(*i)++];
1984 return NULL;
1989 * Auto-load some home services.
1991 static void lpcfg_add_auto_services(struct loadparm_context *lp_ctx,
1992 const char *str)
1994 return;
1999 * Unload unused services.
2002 void lpcfg_killunused(struct loadparm_context *lp_ctx,
2003 struct smbsrv_connection *smb,
2004 bool (*snumused) (struct smbsrv_connection *, int))
2006 int i;
2007 for (i = 0; i < lp_ctx->iNumServices; i++) {
2008 if (lp_ctx->services[i] == NULL)
2009 continue;
2011 if (!snumused || !snumused(smb, i)) {
2012 talloc_free(lp_ctx->services[i]);
2013 lp_ctx->services[i] = NULL;
2019 static int lpcfg_destructor(struct loadparm_context *lp_ctx)
2021 struct parmlist_entry *data;
2023 if (lp_ctx->refuse_free) {
2024 /* someone is trying to free the
2025 global_loadparm_context.
2026 We can't allow that. */
2027 return -1;
2030 if (lp_ctx->globals->param_opt != NULL) {
2031 struct parmlist_entry *next;
2032 for (data = lp_ctx->globals->param_opt; data; data=next) {
2033 next = data->next;
2034 if (data->priority & FLAG_CMDLINE) continue;
2035 DLIST_REMOVE(lp_ctx->globals->param_opt, data);
2036 talloc_free(data);
2040 return 0;
2044 * Initialise the global parameter structure.
2046 * Note that most callers should use loadparm_init_global() instead
2048 struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
2050 int i;
2051 char *myname;
2052 struct loadparm_context *lp_ctx;
2053 struct parmlist_entry *parm;
2054 char *logfile;
2056 lp_ctx = talloc_zero(mem_ctx, struct loadparm_context);
2057 if (lp_ctx == NULL)
2058 return NULL;
2060 talloc_set_destructor(lp_ctx, lpcfg_destructor);
2061 lp_ctx->bInGlobalSection = true;
2062 lp_ctx->globals = talloc_zero(lp_ctx, struct loadparm_global);
2063 lp_ctx->sDefault = talloc_zero(lp_ctx, struct loadparm_service);
2065 lp_ctx->sDefault->iMaxPrintJobs = 1000;
2066 lp_ctx->sDefault->bAvailable = true;
2067 lp_ctx->sDefault->browseable = true;
2068 lp_ctx->sDefault->readonly = true;
2069 lp_ctx->sDefault->map_archive = true;
2070 lp_ctx->sDefault->strict_locking = true;
2071 lp_ctx->sDefault->oplocks = true;
2072 lp_ctx->sDefault->create_mask = 0744;
2073 lp_ctx->sDefault->force_create_mode = 0000;
2074 lp_ctx->sDefault->dir_mask = 0755;
2075 lp_ctx->sDefault->force_dir_mode = 0000;
2077 DEBUG(3, ("Initialising global parameters\n"));
2079 for (i = 0; parm_table[i].label; i++) {
2080 if ((parm_table[i].type == P_STRING ||
2081 parm_table[i].type == P_USTRING) &&
2082 !(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2083 char **r;
2084 if (parm_table[i].p_class == P_LOCAL) {
2085 r = (char **)(((char *)lp_ctx->sDefault) + parm_table[i].offset);
2086 } else {
2087 r = (char **)(((char *)lp_ctx->globals) + parm_table[i].offset);
2089 *r = talloc_strdup(lp_ctx, "");
2093 logfile = talloc_asprintf(lp_ctx, "%s/log.samba", dyn_LOGFILEBASE);
2094 lpcfg_do_global_parameter(lp_ctx, "log file", logfile);
2095 talloc_free(logfile);
2097 lpcfg_do_global_parameter(lp_ctx, "log level", "0");
2099 lpcfg_do_global_parameter(lp_ctx, "syslog", "1");
2100 lpcfg_do_global_parameter(lp_ctx, "syslog only", "No");
2101 lpcfg_do_global_parameter(lp_ctx, "debug timestamp", "Yes");
2102 lpcfg_do_global_parameter(lp_ctx, "debug prefix timestamp", "No");
2103 lpcfg_do_global_parameter(lp_ctx, "debug hires timestamp", "Yes");
2104 lpcfg_do_global_parameter(lp_ctx, "debug pid", "No");
2105 lpcfg_do_global_parameter(lp_ctx, "debug uid", "No");
2106 lpcfg_do_global_parameter(lp_ctx, "debug class", "No");
2108 lpcfg_do_global_parameter(lp_ctx, "share backend", "classic");
2110 lpcfg_do_global_parameter(lp_ctx, "server role", "auto");
2111 lpcfg_do_global_parameter(lp_ctx, "domain logons", "No");
2112 lpcfg_do_global_parameter(lp_ctx, "domain master", "Auto");
2114 /* options that can be set on the command line must be initialised via
2115 the slower lpcfg_do_global_parameter() to ensure that FLAG_CMDLINE is obeyed */
2116 #ifdef TCP_NODELAY
2117 lpcfg_do_global_parameter(lp_ctx, "socket options", "TCP_NODELAY");
2118 #endif
2119 lpcfg_do_global_parameter(lp_ctx, "workgroup", DEFAULT_WORKGROUP);
2120 myname = get_myname(lp_ctx);
2121 lpcfg_do_global_parameter(lp_ctx, "netbios name", myname);
2122 talloc_free(myname);
2123 lpcfg_do_global_parameter(lp_ctx, "name resolve order", "wins host bcast");
2125 lpcfg_do_global_parameter(lp_ctx, "fstype", "NTFS");
2127 lpcfg_do_global_parameter(lp_ctx, "ntvfs handler", "unixuid default");
2128 lpcfg_do_global_parameter(lp_ctx, "max connections", "0");
2130 lpcfg_do_global_parameter(lp_ctx, "dcerpc endpoint servers", "epmapper wkssvc rpcecho samr netlogon lsarpc spoolss drsuapi dssetup unixinfo browser eventlog6 backupkey dnsserver");
2131 lpcfg_do_global_parameter(lp_ctx, "server services", "s3fs rpc nbt wrepl ldap cldap kdc drepl winbind ntp_signd kcc dnsupdate dns");
2132 lpcfg_do_global_parameter(lp_ctx, "kccsrv:samba_kcc", "true");
2133 /* the winbind method for domain controllers is for both RODC
2134 auth forwarding and for trusted domains */
2135 lpcfg_do_global_parameter(lp_ctx, "private dir", dyn_PRIVATE_DIR);
2136 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_LOCAL_MACHINE", "hklm.ldb");
2138 /* This hive should be dynamically generated by Samba using
2139 data from the sam, but for the moment leave it in a tdb to
2140 keep regedt32 from popping up an annoying dialog. */
2141 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_USERS", "hku.ldb");
2143 /* using UTF8 by default allows us to support all chars */
2144 lpcfg_do_global_parameter(lp_ctx, "unix charset", "UTF-8");
2146 /* Use codepage 850 as a default for the dos character set */
2147 lpcfg_do_global_parameter(lp_ctx, "dos charset", "CP850");
2150 * Allow the default PASSWD_CHAT to be overridden in local.h.
2152 lpcfg_do_global_parameter(lp_ctx, "passwd chat", DEFAULT_PASSWD_CHAT);
2154 lpcfg_do_global_parameter(lp_ctx, "pid directory", dyn_PIDDIR);
2155 lpcfg_do_global_parameter(lp_ctx, "lock dir", dyn_LOCKDIR);
2156 lpcfg_do_global_parameter(lp_ctx, "state directory", dyn_STATEDIR);
2157 lpcfg_do_global_parameter(lp_ctx, "cache directory", dyn_CACHEDIR);
2158 lpcfg_do_global_parameter(lp_ctx, "ncalrpc dir", dyn_NCALRPCDIR);
2160 lpcfg_do_global_parameter(lp_ctx, "nbt client socket address", "0.0.0.0");
2161 lpcfg_do_global_parameter_var(lp_ctx, "server string",
2162 "Samba %s", SAMBA_VERSION_STRING);
2164 lpcfg_do_global_parameter(lp_ctx, "password server", "*");
2166 lpcfg_do_global_parameter(lp_ctx, "max mux", "50");
2167 lpcfg_do_global_parameter(lp_ctx, "max xmit", "16644");
2168 lpcfg_do_global_parameter(lp_ctx, "host msdfs", "true");
2170 lpcfg_do_global_parameter(lp_ctx, "LargeReadwrite", "True");
2171 lpcfg_do_global_parameter(lp_ctx, "server min protocol", "LANMAN1");
2172 lpcfg_do_global_parameter(lp_ctx, "server max protocol", "NT1");
2173 lpcfg_do_global_parameter(lp_ctx, "client min protocol", "CORE");
2174 lpcfg_do_global_parameter(lp_ctx, "client max protocol", "NT1");
2175 lpcfg_do_global_parameter(lp_ctx, "security", "AUTO");
2176 lpcfg_do_global_parameter(lp_ctx, "EncryptPasswords", "True");
2177 lpcfg_do_global_parameter(lp_ctx, "ReadRaw", "True");
2178 lpcfg_do_global_parameter(lp_ctx, "WriteRaw", "True");
2179 lpcfg_do_global_parameter(lp_ctx, "NullPasswords", "False");
2180 lpcfg_do_global_parameter(lp_ctx, "ObeyPamRestrictions", "False");
2182 lpcfg_do_global_parameter(lp_ctx, "TimeServer", "False");
2183 lpcfg_do_global_parameter(lp_ctx, "BindInterfacesOnly", "False");
2184 lpcfg_do_global_parameter(lp_ctx, "Unicode", "True");
2185 lpcfg_do_global_parameter(lp_ctx, "ClientLanManAuth", "False");
2186 lpcfg_do_global_parameter(lp_ctx, "ClientNTLMv2Auth", "True");
2187 lpcfg_do_global_parameter(lp_ctx, "LanmanAuth", "False");
2188 lpcfg_do_global_parameter(lp_ctx, "NTLMAuth", "True");
2189 lpcfg_do_global_parameter(lp_ctx, "client use spnego principal", "False");
2191 lpcfg_do_global_parameter(lp_ctx, "UnixExtensions", "True");
2193 lpcfg_do_global_parameter(lp_ctx, "PreferredMaster", "Auto");
2194 lpcfg_do_global_parameter(lp_ctx, "LocalMaster", "True");
2196 lpcfg_do_global_parameter(lp_ctx, "wins support", "False");
2197 lpcfg_do_global_parameter(lp_ctx, "dns proxy", "True");
2199 lpcfg_do_global_parameter(lp_ctx, "winbind separator", "\\");
2200 lpcfg_do_global_parameter(lp_ctx, "winbind sealed pipes", "True");
2201 lpcfg_do_global_parameter(lp_ctx, "require strong key", "True");
2202 lpcfg_do_global_parameter(lp_ctx, "winbindd socket directory", dyn_WINBINDD_SOCKET_DIR);
2203 lpcfg_do_global_parameter(lp_ctx, "winbindd privileged socket directory", dyn_WINBINDD_PRIVILEGED_SOCKET_DIR);
2204 lpcfg_do_global_parameter(lp_ctx, "ntp signd socket directory", dyn_NTP_SIGND_SOCKET_DIR);
2205 lpcfg_do_global_parameter_var(lp_ctx, "dns update command", "%s/samba_dnsupdate", dyn_SCRIPTSBINDIR);
2206 lpcfg_do_global_parameter_var(lp_ctx, "spn update command", "%s/samba_spnupdate", dyn_SCRIPTSBINDIR);
2207 lpcfg_do_global_parameter_var(lp_ctx, "samba kcc command",
2208 "%s/samba_kcc", dyn_SCRIPTSBINDIR);
2209 lpcfg_do_global_parameter(lp_ctx, "template shell", "/bin/false");
2210 lpcfg_do_global_parameter(lp_ctx, "template homedir", "/home/%WORKGROUP%/%ACCOUNTNAME%");
2212 lpcfg_do_global_parameter(lp_ctx, "client signing", "default");
2213 lpcfg_do_global_parameter(lp_ctx, "server signing", "default");
2215 lpcfg_do_global_parameter(lp_ctx, "use spnego", "True");
2217 lpcfg_do_global_parameter(lp_ctx, "use mmap", "True");
2219 lpcfg_do_global_parameter(lp_ctx, "smb ports", "445 139");
2220 lpcfg_do_global_parameter(lp_ctx, "nbt port", "137");
2221 lpcfg_do_global_parameter(lp_ctx, "dgram port", "138");
2222 lpcfg_do_global_parameter(lp_ctx, "cldap port", "389");
2223 lpcfg_do_global_parameter(lp_ctx, "krb5 port", "88");
2224 lpcfg_do_global_parameter(lp_ctx, "kpasswd port", "464");
2225 lpcfg_do_global_parameter(lp_ctx, "web port", "901");
2227 lpcfg_do_global_parameter(lp_ctx, "nt status support", "True");
2229 lpcfg_do_global_parameter(lp_ctx, "max wins ttl", "518400"); /* 6 days */
2230 lpcfg_do_global_parameter(lp_ctx, "min wins ttl", "10");
2232 lpcfg_do_global_parameter(lp_ctx, "tls enabled", "True");
2233 lpcfg_do_global_parameter(lp_ctx, "tls keyfile", "tls/key.pem");
2234 lpcfg_do_global_parameter(lp_ctx, "tls certfile", "tls/cert.pem");
2235 lpcfg_do_global_parameter(lp_ctx, "tls cafile", "tls/ca.pem");
2236 lpcfg_do_global_parameter(lp_ctx, "prefork children:smb", "4");
2238 lpcfg_do_global_parameter(lp_ctx, "rndc command", "/usr/sbin/rndc");
2239 lpcfg_do_global_parameter(lp_ctx, "nsupdate command", "/usr/bin/nsupdate -g");
2241 lpcfg_do_global_parameter(lp_ctx, "allow dns updates", "secure only");
2242 lpcfg_do_global_parameter(lp_ctx, "dns forwarder", "");
2244 lpcfg_do_global_parameter(lp_ctx, "algorithmic rid base", "1000");
2246 lpcfg_do_global_parameter(lp_ctx, "enhanced browsing", "True");
2248 lpcfg_do_global_parameter(lp_ctx, "winbind nss info", "template");
2250 lpcfg_do_global_parameter(lp_ctx, "server schannel", "Auto");
2252 lpcfg_do_global_parameter(lp_ctx, "short preserve case", "True");
2254 lpcfg_do_global_parameter(lp_ctx, "max open files", "16384");
2256 lpcfg_do_global_parameter(lp_ctx, "cups connection timeout", "30");
2258 lpcfg_do_global_parameter(lp_ctx, "locking", "True");
2260 lpcfg_do_global_parameter(lp_ctx, "block size", "1024");
2262 lpcfg_do_global_parameter(lp_ctx, "client use spnego", "True");
2264 lpcfg_do_global_parameter(lp_ctx, "change notify", "True");
2266 lpcfg_do_global_parameter(lp_ctx, "name cache timeout", "660");
2268 lpcfg_do_global_parameter(lp_ctx, "defer sharing violations", "True");
2270 lpcfg_do_global_parameter(lp_ctx, "ldap replication sleep", "1000");
2272 lpcfg_do_global_parameter(lp_ctx, "idmap backend", "tdb");
2274 lpcfg_do_global_parameter(lp_ctx, "enable privileges", "True");
2276 lpcfg_do_global_parameter(lp_ctx, "smb2 max write", "1048576");
2278 lpcfg_do_global_parameter(lp_ctx, "passdb backend", "tdbsam");
2280 lpcfg_do_global_parameter(lp_ctx, "getwd cache", "True");
2282 lpcfg_do_global_parameter(lp_ctx, "winbind nested groups", "True");
2284 lpcfg_do_global_parameter(lp_ctx, "mangled names", "True");
2286 lpcfg_do_global_parameter(lp_ctx, "smb2 max credits", "8192");
2288 lpcfg_do_global_parameter(lp_ctx, "ldap ssl", "start tls");
2290 lpcfg_do_global_parameter(lp_ctx, "ldap deref", "auto");
2292 lpcfg_do_global_parameter(lp_ctx, "lm interval", "60");
2294 lpcfg_do_global_parameter(lp_ctx, "mangling method", "hash2");
2296 lpcfg_do_global_parameter(lp_ctx, "hide dot files", "True");
2298 lpcfg_do_global_parameter(lp_ctx, "browse list", "True");
2300 lpcfg_do_global_parameter(lp_ctx, "passwd chat timeout", "2");
2302 lpcfg_do_global_parameter(lp_ctx, "guest account", GUEST_ACCOUNT);
2304 lpcfg_do_global_parameter(lp_ctx, "client schannel", "auto");
2306 lpcfg_do_global_parameter(lp_ctx, "smb encrypt", "default");
2308 lpcfg_do_global_parameter(lp_ctx, "max log size", "5000");
2310 lpcfg_do_global_parameter(lp_ctx, "idmap negative cache time", "120");
2312 lpcfg_do_global_parameter(lp_ctx, "ldap follow referral", "auto");
2314 lpcfg_do_global_parameter(lp_ctx, "multicast dns register", "yes");
2316 lpcfg_do_global_parameter(lp_ctx, "winbind reconnect delay", "30");
2318 lpcfg_do_global_parameter(lp_ctx, "nt acl support", "yes");
2320 lpcfg_do_global_parameter(lp_ctx, "acl check permissions", "yes");
2322 lpcfg_do_global_parameter(lp_ctx, "keepalive", "300");
2324 lpcfg_do_global_parameter(lp_ctx, "winbind cache time", "300");
2326 lpcfg_do_global_parameter(lp_ctx, "level2 oplocks", "yes");
2328 lpcfg_do_global_parameter(lp_ctx, "show add printer wizard", "yes");
2330 lpcfg_do_global_parameter(lp_ctx, "allocation roundup size", "1048576");
2332 lpcfg_do_global_parameter(lp_ctx, "ldap page size", "1024");
2334 lpcfg_do_global_parameter(lp_ctx, "kernel share modes", "yes");
2336 lpcfg_do_global_parameter(lp_ctx, "strict locking", "Auto");
2338 lpcfg_do_global_parameter(lp_ctx, "map readonly", "yes");
2340 lpcfg_do_global_parameter(lp_ctx, "allow trusted domains", "yes");
2342 lpcfg_do_global_parameter(lp_ctx, "default devmode", "yes");
2344 lpcfg_do_global_parameter(lp_ctx, "os level", "20");
2346 lpcfg_do_global_parameter(lp_ctx, "dos filetimes", "yes");
2348 lpcfg_do_global_parameter(lp_ctx, "mangling char", "~");
2350 lpcfg_do_global_parameter(lp_ctx, "printcap cache time", "750");
2352 lpcfg_do_global_parameter(lp_ctx, "create krb5 conf", "yes");
2354 lpcfg_do_global_parameter(lp_ctx, "winbind max clients", "200");
2356 lpcfg_do_global_parameter(lp_ctx, "acl map full control", "yes");
2358 lpcfg_do_global_parameter(lp_ctx, "nt pipe support", "yes");
2360 lpcfg_do_global_parameter(lp_ctx, "ldap debug threshold", "10");
2362 lpcfg_do_global_parameter(lp_ctx, "follow symlinks", "yes");
2364 lpcfg_do_global_parameter(lp_ctx, "machine password timeout", "604800");
2366 lpcfg_do_global_parameter(lp_ctx, "ldap connection timeout", "2");
2368 lpcfg_do_global_parameter(lp_ctx, "winbind expand groups", "1");
2370 lpcfg_do_global_parameter(lp_ctx, "stat cache", "yes");
2372 lpcfg_do_global_parameter(lp_ctx, "lpq cache time", "30");
2374 lpcfg_do_global_parameter(lp_ctx, "smb2 max trans", "1048576");
2376 lpcfg_do_global_parameter(lp_ctx, "smb2 max read", "1048576");
2378 lpcfg_do_global_parameter(lp_ctx, "durable handles", "yes");
2380 lpcfg_do_global_parameter(lp_ctx, "max stat cache size", "256");
2382 lpcfg_do_global_parameter(lp_ctx, "ldap passwd sync", "no");
2384 lpcfg_do_global_parameter(lp_ctx, "kernel change notify", "yes");
2386 lpcfg_do_global_parameter(lp_ctx, "max ttl", "259200");
2388 lpcfg_do_global_parameter(lp_ctx, "blocking locks", "yes");
2390 lpcfg_do_global_parameter(lp_ctx, "oplock contention limit", "2");
2392 lpcfg_do_global_parameter(lp_ctx, "load printers", "yes");
2394 lpcfg_do_global_parameter(lp_ctx, "idmap cache time", "604800");
2396 lpcfg_do_global_parameter(lp_ctx, "preserve case", "yes");
2398 lpcfg_do_global_parameter(lp_ctx, "lm announce", "auto");
2400 lpcfg_do_global_parameter(lp_ctx, "afs token lifetime", "604800");
2402 lpcfg_do_global_parameter(lp_ctx, "enable core files", "yes");
2404 lpcfg_do_global_parameter(lp_ctx, "winbind max domain connections", "1");
2406 lpcfg_do_global_parameter(lp_ctx, "case sensitive", "auto");
2408 lpcfg_do_global_parameter(lp_ctx, "ldap timeout", "15");
2410 lpcfg_do_global_parameter(lp_ctx, "mangle prefix", "1");
2412 lpcfg_do_global_parameter(lp_ctx, "posix locking", "yes");
2414 lpcfg_do_global_parameter(lp_ctx, "lock spin time", "200");
2416 lpcfg_do_global_parameter(lp_ctx, "directory name cache size", "100");
2418 lpcfg_do_global_parameter(lp_ctx, "nmbd bind explicit broadcast", "yes");
2420 lpcfg_do_global_parameter(lp_ctx, "init logon delay", "100");
2422 lpcfg_do_global_parameter(lp_ctx, "usershare owner only", "yes");
2424 for (i = 0; parm_table[i].label; i++) {
2425 if (!(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2426 lp_ctx->flags[i] |= FLAG_DEFAULT;
2430 for (parm=lp_ctx->globals->param_opt; parm; parm=parm->next) {
2431 if (!(parm->priority & FLAG_CMDLINE)) {
2432 parm->priority |= FLAG_DEFAULT;
2436 return lp_ctx;
2440 * Initialise the global parameter structure.
2442 struct loadparm_context *loadparm_init_global(bool load_default)
2444 if (global_loadparm_context == NULL) {
2445 global_loadparm_context = loadparm_init(NULL);
2447 if (global_loadparm_context == NULL) {
2448 return NULL;
2450 global_loadparm_context->global = true;
2451 if (load_default && !global_loadparm_context->loaded) {
2452 lpcfg_load_default(global_loadparm_context);
2454 global_loadparm_context->refuse_free = true;
2455 return global_loadparm_context;
2459 * Initialise the global parameter structure.
2461 struct loadparm_context *loadparm_init_s3(TALLOC_CTX *mem_ctx,
2462 const struct loadparm_s3_helpers *s3_fns)
2464 struct loadparm_context *loadparm_context = talloc_zero(mem_ctx, struct loadparm_context);
2465 if (!loadparm_context) {
2466 return NULL;
2468 loadparm_context->s3_fns = s3_fns;
2469 return loadparm_context;
2472 const char *lpcfg_configfile(struct loadparm_context *lp_ctx)
2474 return lp_ctx->szConfigFile;
2477 const char *lp_default_path(void)
2479 if (getenv("SMB_CONF_PATH"))
2480 return getenv("SMB_CONF_PATH");
2481 else
2482 return dyn_CONFIGFILE;
2486 * Update the internal state of a loadparm context after settings
2487 * have changed.
2489 static bool lpcfg_update(struct loadparm_context *lp_ctx)
2491 struct debug_settings settings;
2492 lpcfg_add_auto_services(lp_ctx, lpcfg_auto_services(lp_ctx));
2494 if (!lp_ctx->globals->wins_server_list && lp_ctx->globals->we_are_a_wins_server) {
2495 lpcfg_do_global_parameter(lp_ctx, "wins server", "127.0.0.1");
2498 if (!lp_ctx->global) {
2499 return true;
2502 panic_action = lp_ctx->globals->panic_action;
2504 reload_charcnv(lp_ctx);
2506 ZERO_STRUCT(settings);
2507 /* Add any more debug-related smb.conf parameters created in
2508 * future here */
2509 settings.syslog = lp_ctx->globals->syslog;
2510 settings.syslog_only = lp_ctx->globals->syslog_only;
2511 settings.timestamp_logs = lp_ctx->globals->timestamp_logs;
2512 settings.debug_prefix_timestamp = lp_ctx->globals->debug_prefix_timestamp;
2513 settings.debug_hires_timestamp = lp_ctx->globals->debug_hires_timestamp;
2514 settings.debug_pid = lp_ctx->globals->debug_pid;
2515 settings.debug_uid = lp_ctx->globals->debug_uid;
2516 settings.debug_class = lp_ctx->globals->debug_class;
2517 debug_set_settings(&settings);
2519 /* FIXME: This is a bit of a hack, but we can't use a global, since
2520 * not everything that uses lp also uses the socket library */
2521 if (lpcfg_parm_bool(lp_ctx, NULL, "socket", "testnonblock", false)) {
2522 setenv("SOCKET_TESTNONBLOCK", "1", 1);
2523 } else {
2524 unsetenv("SOCKET_TESTNONBLOCK");
2527 return true;
2530 bool lpcfg_load_default(struct loadparm_context *lp_ctx)
2532 const char *path;
2534 path = lp_default_path();
2536 if (!file_exist(path)) {
2537 /* We allow the default smb.conf file to not exist,
2538 * basically the equivalent of an empty file. */
2539 return lpcfg_update(lp_ctx);
2542 return lpcfg_load(lp_ctx, path);
2546 * Load the services array from the services file.
2548 * Return True on success, False on failure.
2550 bool lpcfg_load(struct loadparm_context *lp_ctx, const char *filename)
2552 char *n2;
2553 bool bRetval;
2555 filename = talloc_strdup(lp_ctx, filename);
2557 lp_ctx->szConfigFile = filename;
2559 if (lp_ctx->s3_fns) {
2560 return lp_ctx->s3_fns->load(filename);
2563 lp_ctx->bInGlobalSection = true;
2564 n2 = standard_sub_basic(lp_ctx, lp_ctx->szConfigFile);
2565 DEBUG(2, ("lpcfg_load: refreshing parameters from %s\n", n2));
2567 add_to_file_list(lp_ctx, lp_ctx->szConfigFile, n2);
2569 /* We get sections first, so have to start 'behind' to make up */
2570 lp_ctx->currentService = NULL;
2571 bRetval = pm_process(n2, do_section, do_parameter, lp_ctx);
2573 /* finish up the last section */
2574 DEBUG(4, ("pm_process() returned %s\n", BOOLSTR(bRetval)));
2575 if (bRetval)
2576 if (lp_ctx->currentService != NULL)
2577 bRetval = lpcfg_service_ok(lp_ctx->currentService);
2579 bRetval = bRetval && lpcfg_update(lp_ctx);
2581 /* we do this unconditionally, so that it happens even
2582 for a missing smb.conf */
2583 reload_charcnv(lp_ctx);
2585 if (bRetval == true) {
2586 /* set this up so that any child python tasks will
2587 find the right smb.conf */
2588 setenv("SMB_CONF_PATH", filename, 1);
2590 /* set the context used by the lp_*() function
2591 varients */
2592 global_loadparm_context = lp_ctx;
2593 lp_ctx->loaded = true;
2596 return bRetval;
2600 * Return the max number of services.
2603 int lpcfg_numservices(struct loadparm_context *lp_ctx)
2605 if (lp_ctx->s3_fns) {
2606 return lp_ctx->s3_fns->get_numservices();
2609 return lp_ctx->iNumServices;
2613 * Display the contents of the services array in human-readable form.
2616 void lpcfg_dump(struct loadparm_context *lp_ctx, FILE *f, bool show_defaults,
2617 int maxtoprint)
2619 int iService;
2621 if (lp_ctx->s3_fns) {
2622 lp_ctx->s3_fns->dump(f, show_defaults, maxtoprint);
2623 return;
2626 defaults_saved = !show_defaults;
2628 dump_globals(lp_ctx, f, show_defaults);
2630 dump_a_service(lp_ctx->sDefault, lp_ctx->sDefault, f, lp_ctx->flags);
2632 for (iService = 0; iService < maxtoprint; iService++)
2633 lpcfg_dump_one(f, show_defaults, lp_ctx->services[iService], lp_ctx->sDefault);
2637 * Display the contents of one service in human-readable form.
2639 void lpcfg_dump_one(FILE *f, bool show_defaults, struct loadparm_service *service, struct loadparm_service *sDefault)
2641 if (service != NULL) {
2642 if (service->szService[0] == '\0')
2643 return;
2644 dump_a_service(service, sDefault, f, NULL);
2648 struct loadparm_service *lpcfg_servicebynum(struct loadparm_context *lp_ctx,
2649 int snum)
2651 if (lp_ctx->s3_fns) {
2652 return lp_ctx->s3_fns->get_servicebynum(snum);
2655 return lp_ctx->services[snum];
2658 struct loadparm_service *lpcfg_service(struct loadparm_context *lp_ctx,
2659 const char *service_name)
2661 int iService;
2662 char *serviceName;
2664 if (lp_ctx->s3_fns) {
2665 return lp_ctx->s3_fns->get_service(service_name);
2668 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--) {
2669 if (lp_ctx->services[iService] &&
2670 lp_ctx->services[iService]->szService) {
2672 * The substitution here is used to support %U is
2673 * service names
2675 serviceName = standard_sub_basic(
2676 lp_ctx->services[iService],
2677 lp_ctx->services[iService]->szService);
2678 if (strequal(serviceName, service_name)) {
2679 talloc_free(serviceName);
2680 return lp_ctx->services[iService];
2682 talloc_free(serviceName);
2686 DEBUG(7,("lpcfg_servicenumber: couldn't find %s\n", service_name));
2687 return NULL;
2690 const char *lpcfg_servicename(const struct loadparm_service *service)
2692 return lp_string((const char *)service->szService);
2696 * A useful volume label function.
2698 const char *lpcfg_volume_label(struct loadparm_service *service, struct loadparm_service *sDefault)
2700 const char *ret;
2701 ret = lp_string((const char *)((service != NULL && service->volume != NULL) ?
2702 service->volume : sDefault->volume));
2703 if (!*ret)
2704 return lpcfg_servicename(service);
2705 return ret;
2709 * If we are PDC then prefer us as DMB
2711 const char *lpcfg_printername(struct loadparm_service *service, struct loadparm_service *sDefault)
2713 const char *ret;
2714 ret = lp_string((const char *)((service != NULL && service->szPrintername != NULL) ?
2715 service->szPrintername : sDefault->szPrintername));
2716 if (ret == NULL || (ret != NULL && *ret == '\0'))
2717 ret = lpcfg_servicename(service);
2719 return ret;
2724 * Return the max print jobs per queue.
2726 int lpcfg_maxprintjobs(struct loadparm_service *service, struct loadparm_service *sDefault)
2728 int maxjobs = (service != NULL) ? service->iMaxPrintJobs : sDefault->iMaxPrintJobs;
2729 if (maxjobs <= 0 || maxjobs >= PRINT_MAX_JOBID)
2730 maxjobs = PRINT_MAX_JOBID - 1;
2732 return maxjobs;
2735 struct smb_iconv_handle *lpcfg_iconv_handle(struct loadparm_context *lp_ctx)
2737 if (lp_ctx == NULL) {
2738 return get_iconv_handle();
2740 return lp_ctx->iconv_handle;
2743 _PUBLIC_ void reload_charcnv(struct loadparm_context *lp_ctx)
2745 struct smb_iconv_handle *old_ic = lp_ctx->iconv_handle;
2746 if (!lp_ctx->global) {
2747 return;
2750 if (old_ic == NULL) {
2751 old_ic = global_iconv_handle;
2753 lp_ctx->iconv_handle = smb_iconv_handle_reinit_lp(lp_ctx, lp_ctx, old_ic);
2754 global_iconv_handle = lp_ctx->iconv_handle;
2757 _PUBLIC_ char *lpcfg_tls_keyfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2759 return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_keyfile);
2762 _PUBLIC_ char *lpcfg_tls_certfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2764 return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_certfile);
2767 _PUBLIC_ char *lpcfg_tls_cafile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2769 return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_cafile);
2772 _PUBLIC_ char *lpcfg_tls_crlfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2774 return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_crlfile);
2777 _PUBLIC_ char *lpcfg_tls_dhpfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2779 return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_dhpfile);
2782 struct gensec_settings *lpcfg_gensec_settings(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2784 struct gensec_settings *settings = talloc_zero(mem_ctx, struct gensec_settings);
2785 if (settings == NULL)
2786 return NULL;
2787 SMB_ASSERT(lp_ctx != NULL);
2788 settings->lp_ctx = talloc_reference(settings, lp_ctx);
2789 settings->target_hostname = lpcfg_parm_string(lp_ctx, NULL, "gensec", "target_hostname");
2790 return settings;
2793 int lpcfg_server_role(struct loadparm_context *lp_ctx)
2795 int domain_master = lpcfg__domain_master(lp_ctx);
2797 return lp_find_server_role(lpcfg__server_role(lp_ctx),
2798 lpcfg__security(lp_ctx),
2799 lpcfg__domain_logons(lp_ctx),
2800 (domain_master == true) ||
2801 (domain_master == Auto));
2804 int lpcfg_security(struct loadparm_context *lp_ctx)
2806 return lp_find_security(lpcfg__server_role(lp_ctx),
2807 lpcfg__security(lp_ctx));
2810 bool lpcfg_server_signing_allowed(struct loadparm_context *lp_ctx, bool *mandatory)
2812 bool allowed = true;
2813 enum smb_signing_setting signing_setting = lpcfg_server_signing(lp_ctx);
2815 *mandatory = false;
2817 if (signing_setting == SMB_SIGNING_DEFAULT) {
2819 * If we are a domain controller, SMB signing is
2820 * really important, as it can prevent a number of
2821 * attacks on communications between us and the
2822 * clients
2824 * However, it really sucks (no sendfile, CPU
2825 * overhead) performance-wise when used on a
2826 * file server, so disable it by default
2827 * on non-DCs
2830 if (lpcfg_server_role(lp_ctx) >= ROLE_ACTIVE_DIRECTORY_DC) {
2831 signing_setting = SMB_SIGNING_REQUIRED;
2832 } else {
2833 signing_setting = SMB_SIGNING_OFF;
2837 switch (signing_setting) {
2838 case SMB_SIGNING_REQUIRED:
2839 *mandatory = true;
2840 break;
2841 case SMB_SIGNING_IF_REQUIRED:
2842 break;
2843 case SMB_SIGNING_DEFAULT:
2844 case SMB_SIGNING_OFF:
2845 allowed = false;
2846 break;
2849 return allowed;