lib/param: fix parameter dumping to detect share and global parameters
[Samba.git] / lib / param / loadparm.c
blob8ade5623b987cb53674bf2f3b3a761bd6974b903
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_STRING:
941 lpcfg_string_set(pserviceDest,
942 (char **)dest_ptr,
943 *(char **)src_ptr);
944 break;
946 case P_USTRING:
947 lpcfg_string_set_upper(pserviceDest,
948 (char **)dest_ptr,
949 *(char **)src_ptr);
950 break;
951 case P_LIST:
952 *(const char ***)dest_ptr = (const char **)str_list_copy(pserviceDest,
953 *(const char ***)src_ptr);
954 break;
955 default:
956 break;
960 if (bcopyall) {
961 init_copymap(pserviceDest);
962 if (pserviceSource->copymap)
963 bitmap_copy(pserviceDest->copymap,
964 pserviceSource->copymap);
967 data = pserviceSource->param_opt;
968 while (data) {
969 not_added = true;
970 pdata = pserviceDest->param_opt;
971 /* Traverse destination */
972 while (pdata) {
973 /* If we already have same option, override it */
974 if (strcmp(pdata->key, data->key) == 0) {
975 talloc_free(pdata->value);
976 pdata->value = talloc_strdup(pdata,
977 data->value);
978 not_added = false;
979 break;
981 pdata = pdata->next;
983 if (not_added) {
984 paramo = talloc_zero(pserviceDest, struct parmlist_entry);
985 if (paramo == NULL)
986 smb_panic("OOM");
987 paramo->key = talloc_strdup(paramo, data->key);
988 paramo->value = talloc_strdup(paramo, data->value);
989 DLIST_ADD(pserviceDest->param_opt, paramo);
991 data = data->next;
996 * Check a service for consistency. Return False if the service is in any way
997 * incomplete or faulty, else True.
999 static bool lpcfg_service_ok(struct loadparm_service *service)
1001 bool bRetval;
1003 bRetval = true;
1004 if (service->szService[0] == '\0') {
1005 DEBUG(0, ("The following message indicates an internal error:\n"));
1006 DEBUG(0, ("No service name in service entry.\n"));
1007 bRetval = false;
1010 /* The [printers] entry MUST be printable. I'm all for flexibility, but */
1011 /* I can't see why you'd want a non-printable printer service... */
1012 if (strwicmp(service->szService, PRINTERS_NAME) == 0) {
1013 if (!service->print_ok) {
1014 DEBUG(0, ("WARNING: [%s] service MUST be printable!\n",
1015 service->szService));
1016 service->print_ok = true;
1018 /* [printers] service must also be non-browsable. */
1019 if (service->browseable)
1020 service->browseable = false;
1023 /* If a service is flagged unavailable, log the fact at level 0. */
1024 if (!service->bAvailable)
1025 DEBUG(1, ("NOTE: Service %s is flagged unavailable.\n",
1026 service->szService));
1028 return bRetval;
1032 /*******************************************************************
1033 Keep a linked list of all config files so we know when one has changed
1034 it's date and needs to be reloaded.
1035 ********************************************************************/
1037 static void add_to_file_list(struct loadparm_context *lp_ctx,
1038 const char *fname, const char *subfname)
1040 struct file_lists *f = lp_ctx->file_lists;
1042 while (f) {
1043 if (f->name && !strcmp(f->name, fname))
1044 break;
1045 f = f->next;
1048 if (!f) {
1049 f = talloc(lp_ctx, struct file_lists);
1050 if (!f)
1051 return;
1052 f->next = lp_ctx->file_lists;
1053 f->name = talloc_strdup(f, fname);
1054 if (!f->name) {
1055 talloc_free(f);
1056 return;
1058 f->subfname = talloc_strdup(f, subfname);
1059 if (!f->subfname) {
1060 talloc_free(f);
1061 return;
1063 lp_ctx->file_lists = f;
1064 f->modtime = file_modtime(subfname);
1065 } else {
1066 time_t t = file_modtime(subfname);
1067 if (t)
1068 f->modtime = t;
1072 /*******************************************************************
1073 Check if a config file has changed date.
1074 ********************************************************************/
1075 bool lpcfg_file_list_changed(struct loadparm_context *lp_ctx)
1077 struct file_lists *f;
1078 DEBUG(6, ("lpcfg_file_list_changed()\n"));
1080 for (f = lp_ctx->file_lists; f != NULL; f = f->next) {
1081 char *n2;
1082 time_t mod_time;
1084 n2 = standard_sub_basic(lp_ctx, f->name);
1086 DEBUGADD(6, ("file %s -> %s last mod_time: %s\n",
1087 f->name, n2, ctime(&f->modtime)));
1089 mod_time = file_modtime(n2);
1091 if (mod_time && ((f->modtime != mod_time) || (f->subfname == NULL) || (strcmp(n2, f->subfname) != 0))) {
1092 DEBUGADD(6, ("file %s modified: %s\n", n2,
1093 ctime(&mod_time)));
1094 f->modtime = mod_time;
1095 talloc_free(f->subfname);
1096 f->subfname = talloc_strdup(f, n2);
1097 return true;
1100 return false;
1103 /***************************************************************************
1104 Handle the "realm" parameter
1105 ***************************************************************************/
1107 static bool handle_realm(struct loadparm_context *lp_ctx, int unused,
1108 const char *pszParmValue, char **ptr)
1110 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1112 talloc_free(lp_ctx->globals->realm);
1113 talloc_free(lp_ctx->globals->dnsdomain);
1115 lp_ctx->globals->realm = strupper_talloc(lp_ctx, pszParmValue);
1116 lp_ctx->globals->dnsdomain = strlower_talloc(lp_ctx, pszParmValue);
1118 return true;
1121 /***************************************************************************
1122 Handle the include operation.
1123 ***************************************************************************/
1125 static bool handle_include(struct loadparm_context *lp_ctx, int unused,
1126 const char *pszParmValue, char **ptr)
1128 char *fname = standard_sub_basic(lp_ctx, pszParmValue);
1130 add_to_file_list(lp_ctx, pszParmValue, fname);
1132 lpcfg_string_set(lp_ctx, ptr, fname);
1134 if (file_exist(fname))
1135 return pm_process(fname, do_section, do_parameter, lp_ctx);
1137 DEBUG(2, ("Can't find include file %s\n", fname));
1139 return false;
1142 /***************************************************************************
1143 Handle the interpretation of the copy parameter.
1144 ***************************************************************************/
1146 static bool handle_copy(struct loadparm_context *lp_ctx, int unused,
1147 const char *pszParmValue, char **ptr)
1149 bool bRetval;
1150 struct loadparm_service *serviceTemp;
1152 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1154 bRetval = false;
1156 DEBUG(3, ("Copying service from service %s\n", pszParmValue));
1158 if ((serviceTemp = getservicebyname(lp_ctx, pszParmValue)) != NULL) {
1159 if (serviceTemp == lp_ctx->currentService) {
1160 DEBUG(0, ("Can't copy service %s - unable to copy self!\n", pszParmValue));
1161 } else {
1162 copy_service(lp_ctx->currentService,
1163 serviceTemp,
1164 lp_ctx->currentService->copymap);
1165 bRetval = true;
1167 } else {
1168 DEBUG(0, ("Unable to copy service - source not found: %s\n",
1169 pszParmValue));
1170 bRetval = false;
1173 return bRetval;
1176 static bool handle_debug_list(struct loadparm_context *lp_ctx, int unused,
1177 const char *pszParmValue, char **ptr)
1180 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1181 if (lp_ctx->global) {
1182 return debug_parse_levels(pszParmValue);
1184 return true;
1187 static bool handle_logfile(struct loadparm_context *lp_ctx, int unused,
1188 const char *pszParmValue, char **ptr)
1190 debug_set_logfile(pszParmValue);
1191 if (lp_ctx->global) {
1192 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1194 return true;
1197 /***************************************************************************
1198 Initialise a copymap.
1199 ***************************************************************************/
1201 static void init_copymap(struct loadparm_service *pservice)
1203 int i;
1205 TALLOC_FREE(pservice->copymap);
1207 pservice->copymap = bitmap_talloc(NULL, NUMPARAMETERS);
1208 if (!pservice->copymap)
1209 DEBUG(0,
1210 ("Couldn't allocate copymap!! (size %d)\n",
1211 (int)NUMPARAMETERS));
1212 else
1213 for (i = 0; i < NUMPARAMETERS; i++)
1214 bitmap_set(pservice->copymap, i);
1218 * Process a parametric option
1220 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
1221 struct loadparm_service *service,
1222 const char *pszParmName,
1223 const char *pszParmValue, int flags)
1225 struct parmlist_entry *paramo, *data;
1226 char *name;
1227 TALLOC_CTX *mem_ctx;
1229 while (isspace((unsigned char)*pszParmName)) {
1230 pszParmName++;
1233 name = strlower_talloc(lp_ctx, pszParmName);
1234 if (!name) return false;
1236 if (service == NULL) {
1237 data = lp_ctx->globals->param_opt;
1238 mem_ctx = lp_ctx->globals;
1239 } else {
1240 data = service->param_opt;
1241 mem_ctx = service;
1244 /* Traverse destination */
1245 for (paramo=data; paramo; paramo=paramo->next) {
1246 /* If we already have the option set, override it unless
1247 it was a command line option and the new one isn't */
1248 if (strcmp(paramo->key, name) == 0) {
1249 if ((paramo->priority & FLAG_CMDLINE) &&
1250 !(flags & FLAG_CMDLINE)) {
1251 talloc_free(name);
1252 return true;
1255 talloc_free(paramo->value);
1256 paramo->value = talloc_strdup(paramo, pszParmValue);
1257 paramo->priority = flags;
1258 talloc_free(name);
1259 return true;
1263 paramo = talloc_zero(mem_ctx, struct parmlist_entry);
1264 if (!paramo)
1265 smb_panic("OOM");
1266 paramo->key = talloc_strdup(paramo, name);
1267 paramo->value = talloc_strdup(paramo, pszParmValue);
1268 paramo->priority = flags;
1269 if (service == NULL) {
1270 DLIST_ADD(lp_ctx->globals->param_opt, paramo);
1271 } else {
1272 DLIST_ADD(service->param_opt, paramo);
1275 talloc_free(name);
1277 return true;
1280 static bool set_variable(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
1281 const char *pszParmName, const char *pszParmValue,
1282 struct loadparm_context *lp_ctx, bool on_globals)
1284 int i;
1285 /* if it is a special case then go ahead */
1286 if (parm_table[parmnum].special) {
1287 bool ret;
1288 ret = parm_table[parmnum].special(lp_ctx, -1, pszParmValue,
1289 (char **)parm_ptr);
1290 if (!ret) {
1291 return false;
1293 goto mark_non_default;
1296 /* now switch on the type of variable it is */
1297 switch (parm_table[parmnum].type)
1299 case P_BOOL: {
1300 bool b;
1301 if (!set_boolean(pszParmValue, &b)) {
1302 DEBUG(0, ("set_variable(%s): value is not "
1303 "boolean!\n", pszParmValue));
1304 return false;
1306 *(bool *)parm_ptr = b;
1308 break;
1310 case P_BOOLREV: {
1311 bool b;
1312 if (!set_boolean(pszParmValue, &b)) {
1313 DEBUG(0, ("set_variable(%s): value is not "
1314 "boolean!\n", pszParmValue));
1315 return false;
1317 *(bool *)parm_ptr = !b;
1319 break;
1321 case P_INTEGER:
1322 *(int *)parm_ptr = atoi(pszParmValue);
1323 break;
1325 case P_CHAR:
1326 *(char *)parm_ptr = *pszParmValue;
1327 break;
1329 case P_OCTAL:
1330 *(int *)parm_ptr = strtol(pszParmValue, NULL, 8);
1331 break;
1333 case P_BYTES:
1335 uint64_t val;
1336 if (conv_str_size_error(pszParmValue, &val)) {
1337 if (val <= INT_MAX) {
1338 *(int *)parm_ptr = (int)val;
1339 break;
1343 DEBUG(0, ("set_variable(%s): value is not "
1344 "a valid size specifier!\n", pszParmValue));
1345 return false;
1348 case P_CMDLIST:
1349 *(const char ***)parm_ptr = (const char **)str_list_make(mem_ctx,
1350 pszParmValue, NULL);
1351 break;
1352 case P_LIST:
1354 char **new_list = str_list_make(mem_ctx,
1355 pszParmValue, NULL);
1356 for (i=0; new_list[i]; i++) {
1357 if (*(const char ***)parm_ptr != NULL &&
1358 new_list[i][0] == '+' &&
1359 new_list[i][1])
1361 if (!str_list_check(*(const char ***)parm_ptr,
1362 &new_list[i][1])) {
1363 *(const char ***)parm_ptr = str_list_add(*(const char ***)parm_ptr,
1364 &new_list[i][1]);
1366 } else if (*(const char ***)parm_ptr != NULL &&
1367 new_list[i][0] == '-' &&
1368 new_list[i][1])
1370 str_list_remove(*(const char ***)parm_ptr,
1371 &new_list[i][1]);
1372 } else {
1373 if (i != 0) {
1374 DEBUG(0, ("Unsupported list syntax for: %s = %s\n",
1375 pszParmName, pszParmValue));
1376 return false;
1378 *(const char ***)parm_ptr = (const char **) new_list;
1379 break;
1382 break;
1384 case P_STRING:
1385 lpcfg_string_set(mem_ctx, (char **)parm_ptr, pszParmValue);
1386 break;
1388 case P_USTRING:
1389 lpcfg_string_set_upper(mem_ctx, (char **)parm_ptr, pszParmValue);
1390 break;
1392 case P_ENUM:
1393 for (i = 0; parm_table[parmnum].enum_list[i].name; i++) {
1394 if (strequal
1395 (pszParmValue,
1396 parm_table[parmnum].enum_list[i].name)) {
1397 *(int *)parm_ptr =
1398 parm_table[parmnum].
1399 enum_list[i].value;
1400 break;
1403 if (!parm_table[parmnum].enum_list[i].name) {
1404 DEBUG(0,("Unknown enumerated value '%s' for '%s'\n",
1405 pszParmValue, pszParmName));
1406 return false;
1408 break;
1410 case P_SEP:
1411 break;
1414 mark_non_default:
1415 if (on_globals && (lp_ctx->flags[parmnum] & FLAG_DEFAULT)) {
1416 lp_ctx->flags[parmnum] &= ~FLAG_DEFAULT;
1417 /* we have to also unset FLAG_DEFAULT on aliases */
1418 for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
1419 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1421 for (i=parmnum+1;i<NUMPARAMETERS && parm_table[i].offset == parm_table[parmnum].offset;i++) {
1422 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1425 return true;
1429 bool lpcfg_do_global_parameter(struct loadparm_context *lp_ctx,
1430 const char *pszParmName, const char *pszParmValue)
1432 int parmnum = map_parameter(pszParmName);
1433 void *parm_ptr;
1435 if (parmnum < 0) {
1436 if (strchr(pszParmName, ':')) {
1437 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName, pszParmValue, 0);
1439 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1440 return true;
1443 /* if the flag has been set on the command line, then don't allow override,
1444 but don't report an error */
1445 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1446 return true;
1449 parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[parmnum]);
1451 return set_variable(lp_ctx->globals, parmnum, parm_ptr,
1452 pszParmName, pszParmValue, lp_ctx, true);
1455 bool lpcfg_do_service_parameter(struct loadparm_context *lp_ctx,
1456 struct loadparm_service *service,
1457 const char *pszParmName, const char *pszParmValue)
1459 void *parm_ptr;
1460 int i;
1461 int parmnum = map_parameter(pszParmName);
1463 if (parmnum < 0) {
1464 if (strchr(pszParmName, ':')) {
1465 return lp_do_parameter_parametric(lp_ctx, service, pszParmName, pszParmValue, 0);
1467 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1468 return true;
1471 /* if the flag has been set on the command line, then don't allow override,
1472 but don't report an error */
1473 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1474 return true;
1477 if (parm_table[parmnum].p_class == P_GLOBAL) {
1478 DEBUG(0,
1479 ("Global parameter %s found in service section!\n",
1480 pszParmName));
1481 return true;
1483 parm_ptr = ((char *)service) + parm_table[parmnum].offset;
1485 if (!service->copymap)
1486 init_copymap(service);
1488 /* this handles the aliases - set the copymap for other
1489 * entries with the same data pointer */
1490 for (i = 0; parm_table[i].label; i++)
1491 if (parm_table[i].offset == parm_table[parmnum].offset &&
1492 parm_table[i].p_class == parm_table[parmnum].p_class)
1493 bitmap_clear(service->copymap, i);
1495 return set_variable(service, parmnum, parm_ptr, pszParmName,
1496 pszParmValue, lp_ctx, false);
1500 * Process a parameter.
1503 static bool do_parameter(const char *pszParmName, const char *pszParmValue,
1504 void *userdata)
1506 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1508 if (lp_ctx->bInGlobalSection)
1509 return lpcfg_do_global_parameter(lp_ctx, pszParmName,
1510 pszParmValue);
1511 else
1512 return lpcfg_do_service_parameter(lp_ctx, lp_ctx->currentService,
1513 pszParmName, pszParmValue);
1517 variable argument do parameter
1519 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx, const char *pszParmName, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
1520 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx,
1521 const char *pszParmName, const char *fmt, ...)
1523 char *s;
1524 bool ret;
1525 va_list ap;
1527 va_start(ap, fmt);
1528 s = talloc_vasprintf(NULL, fmt, ap);
1529 va_end(ap);
1530 ret = lpcfg_do_global_parameter(lp_ctx, pszParmName, s);
1531 talloc_free(s);
1532 return ret;
1537 set a parameter from the commandline - this is called from command line parameter
1538 parsing code. It sets the parameter then marks the parameter as unable to be modified
1539 by smb.conf processing
1541 bool lpcfg_set_cmdline(struct loadparm_context *lp_ctx, const char *pszParmName,
1542 const char *pszParmValue)
1544 int parmnum;
1545 int i;
1547 if (lp_ctx->s3_fns) {
1548 return lp_ctx->s3_fns->set_cmdline(pszParmName, pszParmValue);
1551 parmnum = map_parameter(pszParmName);
1553 while (isspace((unsigned char)*pszParmValue)) pszParmValue++;
1556 if (parmnum < 0 && strchr(pszParmName, ':')) {
1557 /* set a parametric option */
1558 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName,
1559 pszParmValue, FLAG_CMDLINE);
1562 if (parmnum < 0) {
1563 DEBUG(0,("Unknown option '%s'\n", pszParmName));
1564 return false;
1567 /* reset the CMDLINE flag in case this has been called before */
1568 lp_ctx->flags[parmnum] &= ~FLAG_CMDLINE;
1570 if (!lpcfg_do_global_parameter(lp_ctx, pszParmName, pszParmValue)) {
1571 return false;
1574 lp_ctx->flags[parmnum] |= FLAG_CMDLINE;
1576 /* we have to also set FLAG_CMDLINE on aliases */
1577 for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
1578 lp_ctx->flags[i] |= FLAG_CMDLINE;
1580 for (i=parmnum+1;i<NUMPARAMETERS && parm_table[i].offset == parm_table[parmnum].offset;i++) {
1581 lp_ctx->flags[i] |= FLAG_CMDLINE;
1584 return true;
1588 set a option from the commandline in 'a=b' format. Use to support --option
1590 bool lpcfg_set_option(struct loadparm_context *lp_ctx, const char *option)
1592 char *p, *s;
1593 bool ret;
1595 s = talloc_strdup(NULL, option);
1596 if (!s) {
1597 return false;
1600 p = strchr(s, '=');
1601 if (!p) {
1602 talloc_free(s);
1603 return false;
1606 *p = 0;
1608 ret = lpcfg_set_cmdline(lp_ctx, s, p+1);
1609 talloc_free(s);
1610 return ret;
1614 #define BOOLSTR(b) ((b) ? "Yes" : "No")
1617 * Print a parameter of the specified type.
1620 static void print_parameter(struct parm_struct *p, void *ptr, FILE * f)
1622 /* For the seperation of lists values that we print below */
1623 const char *list_sep = ", ";
1624 int i;
1625 switch (p->type)
1627 case P_ENUM:
1628 for (i = 0; p->enum_list[i].name; i++) {
1629 if (*(int *)ptr == p->enum_list[i].value) {
1630 fprintf(f, "%s",
1631 p->enum_list[i].name);
1632 break;
1635 break;
1637 case P_BOOL:
1638 fprintf(f, "%s", BOOLSTR(*(bool *)ptr));
1639 break;
1641 case P_BOOLREV:
1642 fprintf(f, "%s", BOOLSTR(!*(bool *)ptr));
1643 break;
1645 case P_INTEGER:
1646 case P_BYTES:
1647 fprintf(f, "%d", *(int *)ptr);
1648 break;
1650 case P_CHAR:
1651 fprintf(f, "%c", *(char *)ptr);
1652 break;
1654 case P_OCTAL: {
1655 int val = *(int *)ptr;
1656 if (val == -1) {
1657 fprintf(f, "-1");
1658 } else {
1659 fprintf(f, "0%o", val);
1661 break;
1664 case P_CMDLIST:
1665 list_sep = " ";
1666 /* fall through */
1667 case P_LIST:
1668 if ((char ***)ptr && *(char ***)ptr) {
1669 char **list = *(char ***)ptr;
1670 for (; *list; list++) {
1671 /* surround strings with whitespace in double quotes */
1672 if (*(list+1) == NULL) {
1673 /* last item, no extra separator */
1674 list_sep = "";
1676 if ( strchr_m( *list, ' ' ) ) {
1677 fprintf(f, "\"%s\"%s", *list, list_sep);
1678 } else {
1679 fprintf(f, "%s%s", *list, list_sep);
1683 break;
1685 case P_STRING:
1686 case P_USTRING:
1687 if (*(char **)ptr) {
1688 fprintf(f, "%s", *(char **)ptr);
1690 break;
1691 case P_SEP:
1692 break;
1697 * Check if two parameters are equal.
1700 static bool equal_parameter(parm_type type, void *ptr1, void *ptr2)
1702 switch (type) {
1703 case P_BOOL:
1704 case P_BOOLREV:
1705 return (*((bool *)ptr1) == *((bool *)ptr2));
1707 case P_INTEGER:
1708 case P_ENUM:
1709 case P_OCTAL:
1710 case P_BYTES:
1711 return (*((int *)ptr1) == *((int *)ptr2));
1713 case P_CHAR:
1714 return (*((char *)ptr1) == *((char *)ptr2));
1716 case P_LIST:
1717 case P_CMDLIST:
1718 return str_list_equal(*(const char ***)ptr1, *(const char ***)ptr2);
1720 case P_STRING:
1721 case P_USTRING:
1723 char *p1 = *(char **)ptr1, *p2 = *(char **)ptr2;
1724 if (p1 && !*p1)
1725 p1 = NULL;
1726 if (p2 && !*p2)
1727 p2 = NULL;
1728 return (p1 == p2 || strequal(p1, p2));
1730 case P_SEP:
1731 break;
1733 return false;
1737 * Process a new section (service).
1739 * At this stage all sections are services.
1740 * Later we'll have special sections that permit server parameters to be set.
1741 * Returns True on success, False on failure.
1744 static bool do_section(const char *pszSectionName, void *userdata)
1746 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1747 bool bRetval;
1748 bool isglobal = ((strwicmp(pszSectionName, GLOBAL_NAME) == 0) ||
1749 (strwicmp(pszSectionName, GLOBAL_NAME2) == 0));
1750 bRetval = false;
1752 /* if we've just struck a global section, note the fact. */
1753 lp_ctx->bInGlobalSection = isglobal;
1755 /* check for multiple global sections */
1756 if (lp_ctx->bInGlobalSection) {
1757 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1758 return true;
1761 /* if we have a current service, tidy it up before moving on */
1762 bRetval = true;
1764 if (lp_ctx->currentService != NULL)
1765 bRetval = lpcfg_service_ok(lp_ctx->currentService);
1767 /* if all is still well, move to the next record in the services array */
1768 if (bRetval) {
1769 /* We put this here to avoid an odd message order if messages are */
1770 /* issued by the post-processing of a previous section. */
1771 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1773 if ((lp_ctx->currentService = lpcfg_add_service(lp_ctx, lp_ctx->sDefault,
1774 pszSectionName))
1775 == NULL) {
1776 DEBUG(0, ("Failed to add a new service\n"));
1777 return false;
1781 return bRetval;
1786 * Determine if a particular base parameter is currently set to the default value.
1789 static bool is_default(struct loadparm_service *sDefault, int i)
1791 void *def_ptr = ((char *)sDefault) + parm_table[i].offset;
1792 if (!defaults_saved)
1793 return false;
1794 switch (parm_table[i].type) {
1795 case P_CMDLIST:
1796 case P_LIST:
1797 return str_list_equal((const char **)parm_table[i].def.lvalue,
1798 (const char **)def_ptr);
1799 case P_STRING:
1800 case P_USTRING:
1801 return strequal(parm_table[i].def.svalue,
1802 *(char **)def_ptr);
1803 case P_BOOL:
1804 case P_BOOLREV:
1805 return parm_table[i].def.bvalue ==
1806 *(bool *)def_ptr;
1807 case P_INTEGER:
1808 case P_CHAR:
1809 case P_OCTAL:
1810 case P_BYTES:
1811 case P_ENUM:
1812 return parm_table[i].def.ivalue ==
1813 *(int *)def_ptr;
1814 case P_SEP:
1815 break;
1817 return false;
1821 *Display the contents of the global structure.
1824 static void dump_globals(struct loadparm_context *lp_ctx, FILE *f,
1825 bool show_defaults)
1827 int i;
1828 struct parmlist_entry *data;
1830 fprintf(f, "# Global parameters\n[global]\n");
1832 for (i = 0; parm_table[i].label; i++)
1833 if (parm_table[i].p_class == P_GLOBAL &&
1834 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset))) {
1835 if (!show_defaults && (lp_ctx->flags[i] & FLAG_DEFAULT))
1836 continue;
1837 fprintf(f, "\t%s = ", parm_table[i].label);
1838 print_parameter(&parm_table[i], lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[i]), f);
1839 fprintf(f, "\n");
1841 if (lp_ctx->globals->param_opt != NULL) {
1842 for (data = lp_ctx->globals->param_opt; data;
1843 data = data->next) {
1844 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
1845 continue;
1847 fprintf(f, "\t%s = %s\n", data->key, data->value);
1854 * Display the contents of a single services record.
1857 static void dump_a_service(struct loadparm_service * pService, struct loadparm_service *sDefault, FILE * f,
1858 unsigned int *flags)
1860 int i;
1861 struct parmlist_entry *data;
1863 if (pService != sDefault)
1864 fprintf(f, "\n[%s]\n", pService->szService);
1866 for (i = 0; parm_table[i].label; i++) {
1867 if (parm_table[i].p_class == P_LOCAL &&
1868 (*parm_table[i].label != '-') &&
1869 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset)))
1871 if (pService == sDefault) {
1872 if (flags && (flags[i] & FLAG_DEFAULT)) {
1873 continue;
1875 if (defaults_saved) {
1876 if (is_default(sDefault, i)) {
1877 continue;
1880 } else {
1881 if (equal_parameter(parm_table[i].type,
1882 ((char *)pService) +
1883 parm_table[i].offset,
1884 ((char *)sDefault) +
1885 parm_table[i].offset))
1886 continue;
1889 fprintf(f, "\t%s = ", parm_table[i].label);
1890 print_parameter(&parm_table[i],
1891 ((char *)pService) + parm_table[i].offset, f);
1892 fprintf(f, "\n");
1895 if (pService->param_opt != NULL) {
1896 for (data = pService->param_opt; data; data = data->next) {
1897 fprintf(f, "\t%s = %s\n", data->key, data->value);
1902 bool lpcfg_dump_a_parameter(struct loadparm_context *lp_ctx,
1903 struct loadparm_service *service,
1904 const char *parm_name, FILE * f)
1906 struct parm_struct *parm;
1907 void *ptr;
1909 parm = lpcfg_parm_struct(lp_ctx, parm_name);
1910 if (!parm) {
1911 return false;
1914 if (service != NULL && parm->p_class == P_GLOBAL) {
1915 return false;
1918 ptr = lpcfg_parm_ptr(lp_ctx, service,parm);
1920 print_parameter(parm, ptr, f);
1921 fprintf(f, "\n");
1922 return true;
1926 * Return info about the next parameter in a service.
1927 * snum==-1 gives the globals.
1928 * Return NULL when out of parameters.
1932 struct parm_struct *lpcfg_next_parameter(struct loadparm_context *lp_ctx, int snum, int *i,
1933 int allparameters)
1935 if (snum == -1) {
1936 /* do the globals */
1937 for (; parm_table[*i].label; (*i)++) {
1938 if ((*parm_table[*i].label == '-'))
1939 continue;
1941 if ((*i) > 0
1942 && (parm_table[*i].offset ==
1943 parm_table[(*i) - 1].offset)
1944 && (parm_table[*i].p_class ==
1945 parm_table[(*i) - 1].p_class))
1946 continue;
1948 return &parm_table[(*i)++];
1950 } else {
1951 struct loadparm_service *pService = lp_ctx->services[snum];
1953 for (; parm_table[*i].label; (*i)++) {
1954 if (parm_table[*i].p_class == P_LOCAL &&
1955 (*parm_table[*i].label != '-') &&
1956 ((*i) == 0 ||
1957 (parm_table[*i].offset !=
1958 parm_table[(*i) - 1].offset)))
1960 if (allparameters ||
1961 !equal_parameter(parm_table[*i].type,
1962 ((char *)pService) +
1963 parm_table[*i].offset,
1964 ((char *)lp_ctx->sDefault) +
1965 parm_table[*i].offset))
1967 return &parm_table[(*i)++];
1973 return NULL;
1978 * Auto-load some home services.
1980 static void lpcfg_add_auto_services(struct loadparm_context *lp_ctx,
1981 const char *str)
1983 return;
1988 * Unload unused services.
1991 void lpcfg_killunused(struct loadparm_context *lp_ctx,
1992 struct smbsrv_connection *smb,
1993 bool (*snumused) (struct smbsrv_connection *, int))
1995 int i;
1996 for (i = 0; i < lp_ctx->iNumServices; i++) {
1997 if (lp_ctx->services[i] == NULL)
1998 continue;
2000 if (!snumused || !snumused(smb, i)) {
2001 talloc_free(lp_ctx->services[i]);
2002 lp_ctx->services[i] = NULL;
2008 static int lpcfg_destructor(struct loadparm_context *lp_ctx)
2010 struct parmlist_entry *data;
2012 if (lp_ctx->refuse_free) {
2013 /* someone is trying to free the
2014 global_loadparm_context.
2015 We can't allow that. */
2016 return -1;
2019 if (lp_ctx->globals->param_opt != NULL) {
2020 struct parmlist_entry *next;
2021 for (data = lp_ctx->globals->param_opt; data; data=next) {
2022 next = data->next;
2023 if (data->priority & FLAG_CMDLINE) continue;
2024 DLIST_REMOVE(lp_ctx->globals->param_opt, data);
2025 talloc_free(data);
2029 return 0;
2033 * Initialise the global parameter structure.
2035 * Note that most callers should use loadparm_init_global() instead
2037 struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
2039 int i;
2040 char *myname;
2041 struct loadparm_context *lp_ctx;
2042 struct parmlist_entry *parm;
2043 char *logfile;
2045 lp_ctx = talloc_zero(mem_ctx, struct loadparm_context);
2046 if (lp_ctx == NULL)
2047 return NULL;
2049 talloc_set_destructor(lp_ctx, lpcfg_destructor);
2050 lp_ctx->bInGlobalSection = true;
2051 lp_ctx->globals = talloc_zero(lp_ctx, struct loadparm_global);
2052 lp_ctx->sDefault = talloc_zero(lp_ctx, struct loadparm_service);
2054 lp_ctx->sDefault->iMaxPrintJobs = 1000;
2055 lp_ctx->sDefault->bAvailable = true;
2056 lp_ctx->sDefault->browseable = true;
2057 lp_ctx->sDefault->readonly = true;
2058 lp_ctx->sDefault->map_archive = true;
2059 lp_ctx->sDefault->strict_locking = true;
2060 lp_ctx->sDefault->oplocks = true;
2061 lp_ctx->sDefault->create_mask = 0744;
2062 lp_ctx->sDefault->force_create_mode = 0000;
2063 lp_ctx->sDefault->dir_mask = 0755;
2064 lp_ctx->sDefault->force_dir_mode = 0000;
2066 DEBUG(3, ("Initialising global parameters\n"));
2068 for (i = 0; parm_table[i].label; i++) {
2069 if ((parm_table[i].type == P_STRING ||
2070 parm_table[i].type == P_USTRING) &&
2071 !(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2072 char **r;
2073 if (parm_table[i].p_class == P_LOCAL) {
2074 r = (char **)(((char *)lp_ctx->sDefault) + parm_table[i].offset);
2075 } else {
2076 r = (char **)(((char *)lp_ctx->globals) + parm_table[i].offset);
2078 *r = talloc_strdup(lp_ctx, "");
2082 logfile = talloc_asprintf(lp_ctx, "%s/log.samba", dyn_LOGFILEBASE);
2083 lpcfg_do_global_parameter(lp_ctx, "log file", logfile);
2084 talloc_free(logfile);
2086 lpcfg_do_global_parameter(lp_ctx, "log level", "0");
2088 lpcfg_do_global_parameter(lp_ctx, "syslog", "1");
2089 lpcfg_do_global_parameter(lp_ctx, "syslog only", "No");
2090 lpcfg_do_global_parameter(lp_ctx, "debug timestamp", "Yes");
2091 lpcfg_do_global_parameter(lp_ctx, "debug prefix timestamp", "No");
2092 lpcfg_do_global_parameter(lp_ctx, "debug hires timestamp", "Yes");
2093 lpcfg_do_global_parameter(lp_ctx, "debug pid", "No");
2094 lpcfg_do_global_parameter(lp_ctx, "debug uid", "No");
2095 lpcfg_do_global_parameter(lp_ctx, "debug class", "No");
2097 lpcfg_do_global_parameter(lp_ctx, "share backend", "classic");
2099 lpcfg_do_global_parameter(lp_ctx, "server role", "auto");
2100 lpcfg_do_global_parameter(lp_ctx, "domain logons", "No");
2101 lpcfg_do_global_parameter(lp_ctx, "domain master", "Auto");
2103 /* options that can be set on the command line must be initialised via
2104 the slower lpcfg_do_global_parameter() to ensure that FLAG_CMDLINE is obeyed */
2105 #ifdef TCP_NODELAY
2106 lpcfg_do_global_parameter(lp_ctx, "socket options", "TCP_NODELAY");
2107 #endif
2108 lpcfg_do_global_parameter(lp_ctx, "workgroup", DEFAULT_WORKGROUP);
2109 myname = get_myname(lp_ctx);
2110 lpcfg_do_global_parameter(lp_ctx, "netbios name", myname);
2111 talloc_free(myname);
2112 lpcfg_do_global_parameter(lp_ctx, "name resolve order", "wins host bcast");
2114 lpcfg_do_global_parameter(lp_ctx, "fstype", "NTFS");
2116 lpcfg_do_global_parameter(lp_ctx, "ntvfs handler", "unixuid default");
2117 lpcfg_do_global_parameter(lp_ctx, "max connections", "-1");
2119 lpcfg_do_global_parameter(lp_ctx, "dcerpc endpoint servers", "epmapper wkssvc rpcecho samr netlogon lsarpc spoolss drsuapi dssetup unixinfo browser eventlog6 backupkey dnsserver");
2120 lpcfg_do_global_parameter(lp_ctx, "server services", "s3fs rpc nbt wrepl ldap cldap kdc drepl winbind ntp_signd kcc dnsupdate dns");
2121 lpcfg_do_global_parameter(lp_ctx, "kccsrv:samba_kcc", "true");
2122 /* the winbind method for domain controllers is for both RODC
2123 auth forwarding and for trusted domains */
2124 lpcfg_do_global_parameter(lp_ctx, "private dir", dyn_PRIVATE_DIR);
2125 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_LOCAL_MACHINE", "hklm.ldb");
2127 /* This hive should be dynamically generated by Samba using
2128 data from the sam, but for the moment leave it in a tdb to
2129 keep regedt32 from popping up an annoying dialog. */
2130 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_USERS", "hku.ldb");
2132 /* using UTF8 by default allows us to support all chars */
2133 lpcfg_do_global_parameter(lp_ctx, "unix charset", "UTF8");
2135 /* Use codepage 850 as a default for the dos character set */
2136 lpcfg_do_global_parameter(lp_ctx, "dos charset", "CP850");
2139 * Allow the default PASSWD_CHAT to be overridden in local.h.
2141 lpcfg_do_global_parameter(lp_ctx, "passwd chat", DEFAULT_PASSWD_CHAT);
2143 lpcfg_do_global_parameter(lp_ctx, "pid directory", dyn_PIDDIR);
2144 lpcfg_do_global_parameter(lp_ctx, "lock dir", dyn_LOCKDIR);
2145 lpcfg_do_global_parameter(lp_ctx, "state directory", dyn_STATEDIR);
2146 lpcfg_do_global_parameter(lp_ctx, "cache directory", dyn_CACHEDIR);
2147 lpcfg_do_global_parameter(lp_ctx, "ncalrpc dir", dyn_NCALRPCDIR);
2149 lpcfg_do_global_parameter(lp_ctx, "nbt client socket address", "");
2150 lpcfg_do_global_parameter_var(lp_ctx, "server string",
2151 "Samba %s", SAMBA_VERSION_STRING);
2153 lpcfg_do_global_parameter(lp_ctx, "password server", "*");
2155 lpcfg_do_global_parameter(lp_ctx, "max mux", "50");
2156 lpcfg_do_global_parameter(lp_ctx, "max xmit", "12288");
2157 lpcfg_do_global_parameter(lp_ctx, "host msdfs", "true");
2159 lpcfg_do_global_parameter(lp_ctx, "LargeReadwrite", "True");
2160 lpcfg_do_global_parameter(lp_ctx, "server min protocol", "CORE");
2161 lpcfg_do_global_parameter(lp_ctx, "server max protocol", "NT1");
2162 lpcfg_do_global_parameter(lp_ctx, "client min protocol", "CORE");
2163 lpcfg_do_global_parameter(lp_ctx, "client max protocol", "NT1");
2164 lpcfg_do_global_parameter(lp_ctx, "security", "AUTO");
2165 lpcfg_do_global_parameter(lp_ctx, "EncryptPasswords", "True");
2166 lpcfg_do_global_parameter(lp_ctx, "ReadRaw", "True");
2167 lpcfg_do_global_parameter(lp_ctx, "WriteRaw", "True");
2168 lpcfg_do_global_parameter(lp_ctx, "NullPasswords", "False");
2169 lpcfg_do_global_parameter(lp_ctx, "ObeyPamRestrictions", "False");
2171 lpcfg_do_global_parameter(lp_ctx, "TimeServer", "False");
2172 lpcfg_do_global_parameter(lp_ctx, "BindInterfacesOnly", "False");
2173 lpcfg_do_global_parameter(lp_ctx, "Unicode", "True");
2174 lpcfg_do_global_parameter(lp_ctx, "ClientLanManAuth", "False");
2175 lpcfg_do_global_parameter(lp_ctx, "ClientNTLMv2Auth", "True");
2176 lpcfg_do_global_parameter(lp_ctx, "LanmanAuth", "False");
2177 lpcfg_do_global_parameter(lp_ctx, "NTLMAuth", "True");
2178 lpcfg_do_global_parameter(lp_ctx, "client use spnego principal", "False");
2180 lpcfg_do_global_parameter(lp_ctx, "UnixExtensions", "True");
2182 lpcfg_do_global_parameter(lp_ctx, "PreferredMaster", "Auto");
2183 lpcfg_do_global_parameter(lp_ctx, "LocalMaster", "True");
2185 lpcfg_do_global_parameter(lp_ctx, "wins support", "False");
2186 lpcfg_do_global_parameter(lp_ctx, "dns proxy", "True");
2188 lpcfg_do_global_parameter(lp_ctx, "winbind separator", "\\");
2189 lpcfg_do_global_parameter(lp_ctx, "winbind sealed pipes", "True");
2190 lpcfg_do_global_parameter(lp_ctx, "require strong key", "True");
2191 lpcfg_do_global_parameter(lp_ctx, "winbindd socket directory", dyn_WINBINDD_SOCKET_DIR);
2192 lpcfg_do_global_parameter(lp_ctx, "winbindd privileged socket directory", dyn_WINBINDD_PRIVILEGED_SOCKET_DIR);
2193 lpcfg_do_global_parameter(lp_ctx, "ntp signd socket directory", dyn_NTP_SIGND_SOCKET_DIR);
2194 lpcfg_do_global_parameter_var(lp_ctx, "dns update command", "%s/samba_dnsupdate", dyn_SCRIPTSBINDIR);
2195 lpcfg_do_global_parameter_var(lp_ctx, "spn update command", "%s/samba_spnupdate", dyn_SCRIPTSBINDIR);
2196 lpcfg_do_global_parameter_var(lp_ctx, "samba kcc command",
2197 "%s/samba_kcc", dyn_SCRIPTSBINDIR);
2198 lpcfg_do_global_parameter(lp_ctx, "template shell", "/bin/false");
2199 lpcfg_do_global_parameter(lp_ctx, "template homedir", "/home/%WORKGROUP%/%ACCOUNTNAME%");
2201 lpcfg_do_global_parameter(lp_ctx, "client signing", "default");
2202 lpcfg_do_global_parameter(lp_ctx, "server signing", "default");
2204 lpcfg_do_global_parameter(lp_ctx, "use spnego", "True");
2206 lpcfg_do_global_parameter(lp_ctx, "use mmap", "True");
2208 lpcfg_do_global_parameter(lp_ctx, "smb ports", "445 139");
2209 lpcfg_do_global_parameter(lp_ctx, "nbt port", "137");
2210 lpcfg_do_global_parameter(lp_ctx, "dgram port", "138");
2211 lpcfg_do_global_parameter(lp_ctx, "cldap port", "389");
2212 lpcfg_do_global_parameter(lp_ctx, "krb5 port", "88");
2213 lpcfg_do_global_parameter(lp_ctx, "kpasswd port", "464");
2214 lpcfg_do_global_parameter(lp_ctx, "web port", "901");
2216 lpcfg_do_global_parameter(lp_ctx, "nt status support", "True");
2218 lpcfg_do_global_parameter(lp_ctx, "max wins ttl", "518400"); /* 6 days */
2219 lpcfg_do_global_parameter(lp_ctx, "min wins ttl", "10");
2221 lpcfg_do_global_parameter(lp_ctx, "tls enabled", "True");
2222 lpcfg_do_global_parameter(lp_ctx, "tls keyfile", "tls/key.pem");
2223 lpcfg_do_global_parameter(lp_ctx, "tls certfile", "tls/cert.pem");
2224 lpcfg_do_global_parameter(lp_ctx, "tls cafile", "tls/ca.pem");
2225 lpcfg_do_global_parameter(lp_ctx, "prefork children:smb", "4");
2227 lpcfg_do_global_parameter(lp_ctx, "rndc command", "/usr/sbin/rndc");
2228 lpcfg_do_global_parameter(lp_ctx, "nsupdate command", "/usr/bin/nsupdate -g");
2230 lpcfg_do_global_parameter(lp_ctx, "allow dns updates", "secure only");
2231 lpcfg_do_global_parameter(lp_ctx, "dns forwarder", "");
2233 for (i = 0; parm_table[i].label; i++) {
2234 if (!(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2235 lp_ctx->flags[i] |= FLAG_DEFAULT;
2239 for (parm=lp_ctx->globals->param_opt; parm; parm=parm->next) {
2240 if (!(parm->priority & FLAG_CMDLINE)) {
2241 parm->priority |= FLAG_DEFAULT;
2245 return lp_ctx;
2249 * Initialise the global parameter structure.
2251 struct loadparm_context *loadparm_init_global(bool load_default)
2253 if (global_loadparm_context == NULL) {
2254 global_loadparm_context = loadparm_init(NULL);
2256 if (global_loadparm_context == NULL) {
2257 return NULL;
2259 global_loadparm_context->global = true;
2260 if (load_default && !global_loadparm_context->loaded) {
2261 lpcfg_load_default(global_loadparm_context);
2263 global_loadparm_context->refuse_free = true;
2264 return global_loadparm_context;
2268 * Initialise the global parameter structure.
2270 struct loadparm_context *loadparm_init_s3(TALLOC_CTX *mem_ctx,
2271 const struct loadparm_s3_helpers *s3_fns)
2273 struct loadparm_context *loadparm_context = talloc_zero(mem_ctx, struct loadparm_context);
2274 if (!loadparm_context) {
2275 return NULL;
2277 loadparm_context->s3_fns = s3_fns;
2278 return loadparm_context;
2281 const char *lpcfg_configfile(struct loadparm_context *lp_ctx)
2283 return lp_ctx->szConfigFile;
2286 const char *lp_default_path(void)
2288 if (getenv("SMB_CONF_PATH"))
2289 return getenv("SMB_CONF_PATH");
2290 else
2291 return dyn_CONFIGFILE;
2295 * Update the internal state of a loadparm context after settings
2296 * have changed.
2298 static bool lpcfg_update(struct loadparm_context *lp_ctx)
2300 struct debug_settings settings;
2301 lpcfg_add_auto_services(lp_ctx, lpcfg_auto_services(lp_ctx));
2303 if (!lp_ctx->globals->wins_server_list && lp_ctx->globals->we_are_a_wins_server) {
2304 lpcfg_do_global_parameter(lp_ctx, "wins server", "127.0.0.1");
2307 if (!lp_ctx->global) {
2308 return true;
2311 panic_action = lp_ctx->globals->panic_action;
2313 reload_charcnv(lp_ctx);
2315 ZERO_STRUCT(settings);
2316 /* Add any more debug-related smb.conf parameters created in
2317 * future here */
2318 settings.syslog = lp_ctx->globals->syslog;
2319 settings.syslog_only = lp_ctx->globals->syslog_only;
2320 settings.timestamp_logs = lp_ctx->globals->timestamp_logs;
2321 settings.debug_prefix_timestamp = lp_ctx->globals->debug_prefix_timestamp;
2322 settings.debug_hires_timestamp = lp_ctx->globals->debug_hires_timestamp;
2323 settings.debug_pid = lp_ctx->globals->debug_pid;
2324 settings.debug_uid = lp_ctx->globals->debug_uid;
2325 settings.debug_class = lp_ctx->globals->debug_class;
2326 debug_set_settings(&settings);
2328 /* FIXME: This is a bit of a hack, but we can't use a global, since
2329 * not everything that uses lp also uses the socket library */
2330 if (lpcfg_parm_bool(lp_ctx, NULL, "socket", "testnonblock", false)) {
2331 setenv("SOCKET_TESTNONBLOCK", "1", 1);
2332 } else {
2333 unsetenv("SOCKET_TESTNONBLOCK");
2336 return true;
2339 bool lpcfg_load_default(struct loadparm_context *lp_ctx)
2341 const char *path;
2343 path = lp_default_path();
2345 if (!file_exist(path)) {
2346 /* We allow the default smb.conf file to not exist,
2347 * basically the equivalent of an empty file. */
2348 return lpcfg_update(lp_ctx);
2351 return lpcfg_load(lp_ctx, path);
2355 * Load the services array from the services file.
2357 * Return True on success, False on failure.
2359 bool lpcfg_load(struct loadparm_context *lp_ctx, const char *filename)
2361 char *n2;
2362 bool bRetval;
2364 filename = talloc_strdup(lp_ctx, filename);
2366 lp_ctx->szConfigFile = filename;
2368 if (lp_ctx->s3_fns) {
2369 return lp_ctx->s3_fns->load(filename);
2372 lp_ctx->bInGlobalSection = true;
2373 n2 = standard_sub_basic(lp_ctx, lp_ctx->szConfigFile);
2374 DEBUG(2, ("lpcfg_load: refreshing parameters from %s\n", n2));
2376 add_to_file_list(lp_ctx, lp_ctx->szConfigFile, n2);
2378 /* We get sections first, so have to start 'behind' to make up */
2379 lp_ctx->currentService = NULL;
2380 bRetval = pm_process(n2, do_section, do_parameter, lp_ctx);
2382 /* finish up the last section */
2383 DEBUG(4, ("pm_process() returned %s\n", BOOLSTR(bRetval)));
2384 if (bRetval)
2385 if (lp_ctx->currentService != NULL)
2386 bRetval = lpcfg_service_ok(lp_ctx->currentService);
2388 bRetval = bRetval && lpcfg_update(lp_ctx);
2390 /* we do this unconditionally, so that it happens even
2391 for a missing smb.conf */
2392 reload_charcnv(lp_ctx);
2394 if (bRetval == true) {
2395 /* set this up so that any child python tasks will
2396 find the right smb.conf */
2397 setenv("SMB_CONF_PATH", filename, 1);
2399 /* set the context used by the lp_*() function
2400 varients */
2401 global_loadparm_context = lp_ctx;
2402 lp_ctx->loaded = true;
2405 return bRetval;
2409 * Return the max number of services.
2412 int lpcfg_numservices(struct loadparm_context *lp_ctx)
2414 if (lp_ctx->s3_fns) {
2415 return lp_ctx->s3_fns->get_numservices();
2418 return lp_ctx->iNumServices;
2422 * Display the contents of the services array in human-readable form.
2425 void lpcfg_dump(struct loadparm_context *lp_ctx, FILE *f, bool show_defaults,
2426 int maxtoprint)
2428 int iService;
2430 if (lp_ctx->s3_fns) {
2431 lp_ctx->s3_fns->dump(f, show_defaults, maxtoprint);
2432 return;
2435 defaults_saved = !show_defaults;
2437 dump_globals(lp_ctx, f, show_defaults);
2439 dump_a_service(lp_ctx->sDefault, lp_ctx->sDefault, f, lp_ctx->flags);
2441 for (iService = 0; iService < maxtoprint; iService++)
2442 lpcfg_dump_one(f, show_defaults, lp_ctx->services[iService], lp_ctx->sDefault);
2446 * Display the contents of one service in human-readable form.
2448 void lpcfg_dump_one(FILE *f, bool show_defaults, struct loadparm_service *service, struct loadparm_service *sDefault)
2450 if (service != NULL) {
2451 if (service->szService[0] == '\0')
2452 return;
2453 dump_a_service(service, sDefault, f, NULL);
2457 struct loadparm_service *lpcfg_servicebynum(struct loadparm_context *lp_ctx,
2458 int snum)
2460 if (lp_ctx->s3_fns) {
2461 return lp_ctx->s3_fns->get_servicebynum(snum);
2464 return lp_ctx->services[snum];
2467 struct loadparm_service *lpcfg_service(struct loadparm_context *lp_ctx,
2468 const char *service_name)
2470 int iService;
2471 char *serviceName;
2473 if (lp_ctx->s3_fns) {
2474 return lp_ctx->s3_fns->get_service(service_name);
2477 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--) {
2478 if (lp_ctx->services[iService] &&
2479 lp_ctx->services[iService]->szService) {
2481 * The substitution here is used to support %U is
2482 * service names
2484 serviceName = standard_sub_basic(
2485 lp_ctx->services[iService],
2486 lp_ctx->services[iService]->szService);
2487 if (strequal(serviceName, service_name)) {
2488 talloc_free(serviceName);
2489 return lp_ctx->services[iService];
2491 talloc_free(serviceName);
2495 DEBUG(7,("lpcfg_servicenumber: couldn't find %s\n", service_name));
2496 return NULL;
2499 const char *lpcfg_servicename(const struct loadparm_service *service)
2501 return lp_string((const char *)service->szService);
2505 * A useful volume label function.
2507 const char *lpcfg_volume_label(struct loadparm_service *service, struct loadparm_service *sDefault)
2509 const char *ret;
2510 ret = lp_string((const char *)((service != NULL && service->volume != NULL) ?
2511 service->volume : sDefault->volume));
2512 if (!*ret)
2513 return lpcfg_servicename(service);
2514 return ret;
2518 * If we are PDC then prefer us as DMB
2520 const char *lpcfg_printername(struct loadparm_service *service, struct loadparm_service *sDefault)
2522 const char *ret;
2523 ret = lp_string((const char *)((service != NULL && service->szPrintername != NULL) ?
2524 service->szPrintername : sDefault->szPrintername));
2525 if (ret == NULL || (ret != NULL && *ret == '\0'))
2526 ret = lpcfg_servicename(service);
2528 return ret;
2533 * Return the max print jobs per queue.
2535 int lpcfg_maxprintjobs(struct loadparm_service *service, struct loadparm_service *sDefault)
2537 int maxjobs = (service != NULL) ? service->iMaxPrintJobs : sDefault->iMaxPrintJobs;
2538 if (maxjobs <= 0 || maxjobs >= PRINT_MAX_JOBID)
2539 maxjobs = PRINT_MAX_JOBID - 1;
2541 return maxjobs;
2544 struct smb_iconv_handle *lpcfg_iconv_handle(struct loadparm_context *lp_ctx)
2546 if (lp_ctx == NULL) {
2547 return get_iconv_handle();
2549 return lp_ctx->iconv_handle;
2552 _PUBLIC_ void reload_charcnv(struct loadparm_context *lp_ctx)
2554 struct smb_iconv_handle *old_ic = lp_ctx->iconv_handle;
2555 if (!lp_ctx->global) {
2556 return;
2559 if (old_ic == NULL) {
2560 old_ic = global_iconv_handle;
2562 lp_ctx->iconv_handle = smb_iconv_handle_reinit_lp(lp_ctx, lp_ctx, old_ic);
2563 global_iconv_handle = lp_ctx->iconv_handle;
2566 _PUBLIC_ char *lpcfg_tls_keyfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2568 return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_keyfile);
2571 _PUBLIC_ char *lpcfg_tls_certfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2573 return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_certfile);
2576 _PUBLIC_ char *lpcfg_tls_cafile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2578 return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_cafile);
2581 _PUBLIC_ char *lpcfg_tls_crlfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2583 return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_crlfile);
2586 _PUBLIC_ char *lpcfg_tls_dhpfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2588 return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_dhpfile);
2591 struct gensec_settings *lpcfg_gensec_settings(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2593 struct gensec_settings *settings = talloc_zero(mem_ctx, struct gensec_settings);
2594 if (settings == NULL)
2595 return NULL;
2596 SMB_ASSERT(lp_ctx != NULL);
2597 settings->lp_ctx = talloc_reference(settings, lp_ctx);
2598 settings->target_hostname = lpcfg_parm_string(lp_ctx, NULL, "gensec", "target_hostname");
2599 return settings;
2602 int lpcfg_server_role(struct loadparm_context *lp_ctx)
2604 int domain_master = lpcfg__domain_master(lp_ctx);
2606 return lp_find_server_role(lpcfg__server_role(lp_ctx),
2607 lpcfg__security(lp_ctx),
2608 lpcfg__domain_logons(lp_ctx),
2609 (domain_master == true) ||
2610 (domain_master == Auto));
2613 int lpcfg_security(struct loadparm_context *lp_ctx)
2615 return lp_find_security(lpcfg__server_role(lp_ctx),
2616 lpcfg__security(lp_ctx));
2619 bool lpcfg_server_signing_allowed(struct loadparm_context *lp_ctx, bool *mandatory)
2621 bool allowed = true;
2622 enum smb_signing_setting signing_setting = lpcfg_server_signing(lp_ctx);
2624 *mandatory = false;
2626 if (signing_setting == SMB_SIGNING_DEFAULT) {
2628 * If we are a domain controller, SMB signing is
2629 * really important, as it can prevent a number of
2630 * attacks on communications between us and the
2631 * clients
2633 * However, it really sucks (no sendfile, CPU
2634 * overhead) performance-wise when used on a
2635 * file server, so disable it by default
2636 * on non-DCs
2639 if (lpcfg_server_role(lp_ctx) >= ROLE_ACTIVE_DIRECTORY_DC) {
2640 signing_setting = SMB_SIGNING_REQUIRED;
2641 } else {
2642 signing_setting = SMB_SIGNING_OFF;
2646 switch (signing_setting) {
2647 case SMB_SIGNING_REQUIRED:
2648 *mandatory = true;
2649 break;
2650 case SMB_SIGNING_IF_REQUIRED:
2651 break;
2652 case SMB_SIGNING_DEFAULT:
2653 case SMB_SIGNING_OFF:
2654 allowed = false;
2655 break;
2658 return allowed;