param: Rename variable used for lp_logon_path szLogonPath
[Samba.git] / lib / param / loadparm.c
blob96c699f7244c99e609865e61e39eca76162c9c85
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 *szRealm; \
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; \
99 char *panic_action;
101 #include "lib/param/param_global.h"
103 #define NUMPARAMETERS (sizeof(parm_table) / sizeof(struct parm_struct))
105 /* we don't need a special handler for "dos charset" and "unix charset" */
106 #define handle_dos_charset NULL
107 #define handle_charset NULL
109 /* these are parameter handlers which are not needed in the
110 * non-source3 code
112 #define handle_netbios_aliases NULL
113 #define handle_printing NULL
114 #define handle_ldap_debug_level NULL
115 #define handle_idmap_backend NULL
116 #define handle_idmap_uid NULL
117 #define handle_idmap_gid NULL
119 #ifndef N_
120 #define N_(x) x
121 #endif
123 /* prototypes for the special type handlers */
124 static bool handle_include(struct loadparm_context *lp_ctx, int unused,
125 const char *pszParmValue, char **ptr);
126 static bool handle_realm(struct loadparm_context *lp_ctx, int unused,
127 const char *pszParmValue, char **ptr);
128 static bool handle_copy(struct loadparm_context *lp_ctx, int unused,
129 const char *pszParmValue, char **ptr);
130 static bool handle_debug_list(struct loadparm_context *lp_ctx, int unused,
131 const char *pszParmValue, char **ptr);
132 static bool handle_logfile(struct loadparm_context *lp_ctx, int unused,
133 const char *pszParmValue, char **ptr);
135 #include "lib/param/param_table.c"
137 /* local variables */
138 struct loadparm_context {
139 const char *szConfigFile;
140 struct loadparm_global *globals;
141 struct loadparm_service **services;
142 struct loadparm_service *sDefault;
143 struct smb_iconv_handle *iconv_handle;
144 int iNumServices;
145 struct loadparm_service *currentService;
146 bool bInGlobalSection;
147 struct file_lists {
148 struct file_lists *next;
149 char *name;
150 char *subfname;
151 time_t modtime;
152 } *file_lists;
153 unsigned int flags[NUMPARAMETERS];
154 bool loaded;
155 bool refuse_free;
156 bool global; /* Is this the global context, which may set
157 * global variables such as debug level etc? */
158 const struct loadparm_s3_helpers *s3_fns;
162 struct loadparm_service *lpcfg_default_service(struct loadparm_context *lp_ctx)
164 if (lp_ctx->s3_fns) {
165 return lp_ctx->s3_fns->get_default_loadparm_service();
167 return lp_ctx->sDefault;
171 * Convenience routine to grab string parameters into temporary memory
172 * and run standard_sub_basic on them.
174 * The buffers can be written to by
175 * callers without affecting the source string.
178 static const char *lp_string(const char *s)
180 #if 0 /* until REWRITE done to make thread-safe */
181 size_t len = s ? strlen(s) : 0;
182 char *ret;
183 #endif
185 /* The follow debug is useful for tracking down memory problems
186 especially if you have an inner loop that is calling a lp_*()
187 function that returns a string. Perhaps this debug should be
188 present all the time? */
190 #if 0
191 DEBUG(10, ("lp_string(%s)\n", s));
192 #endif
194 #if 0 /* until REWRITE done to make thread-safe */
195 if (!lp_talloc)
196 lp_talloc = talloc_init("lp_talloc");
198 ret = talloc_array(lp_talloc, char, len + 100); /* leave room for substitution */
200 if (!ret)
201 return NULL;
203 if (!s)
204 *ret = 0;
205 else
206 strlcpy(ret, s, len);
208 if (trim_string(ret, "\"", "\"")) {
209 if (strchr(ret,'"') != NULL)
210 strlcpy(ret, s, len);
213 standard_sub_basic(ret,len+100);
214 return (ret);
215 #endif
216 return s;
220 In this section all the functions that are used to access the
221 parameters from the rest of the program are defined
225 * the creation of separate lpcfg_*() and lp_*() functions is to allow
226 * for code compatibility between existing Samba4 and Samba3 code.
229 /* this global context supports the lp_*() function varients */
230 static struct loadparm_context *global_loadparm_context;
232 #define lpcfg_default_service global_loadparm_context->sDefault
233 #define lpcfg_global_service(i) global_loadparm_context->services[i]
235 #define FN_GLOBAL_STRING(fn_name,var_name) \
236 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) {\
237 if (lp_ctx == NULL) return NULL; \
238 if (lp_ctx->s3_fns) { \
239 smb_panic( __location__ ": " #fn_name " not implemented because it is an allocated and substiuted string"); \
241 return lp_ctx->globals->var_name ? lp_string(lp_ctx->globals->var_name) : ""; \
244 #define FN_GLOBAL_CONST_STRING(fn_name,var_name) \
245 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
246 if (lp_ctx == NULL) return NULL; \
247 if (lp_ctx->s3_fns) { \
248 SMB_ASSERT(lp_ctx->s3_fns->fn_name); \
249 return lp_ctx->s3_fns->fn_name(); \
251 return lp_ctx->globals->var_name ? lp_string(lp_ctx->globals->var_name) : ""; \
254 #define FN_GLOBAL_LIST(fn_name,var_name) \
255 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
256 if (lp_ctx == NULL) return NULL; \
257 if (lp_ctx->s3_fns) { \
258 SMB_ASSERT(lp_ctx->s3_fns->fn_name); \
259 return lp_ctx->s3_fns->fn_name(); \
261 return lp_ctx->globals->var_name; \
264 #define FN_GLOBAL_BOOL(fn_name,var_name) \
265 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) {\
266 if (lp_ctx == NULL) return false; \
267 if (lp_ctx->s3_fns) { \
268 SMB_ASSERT(lp_ctx->s3_fns->fn_name); \
269 return lp_ctx->s3_fns->fn_name(); \
271 return lp_ctx->globals->var_name; \
274 #define FN_GLOBAL_INTEGER(fn_name,var_name) \
275 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
276 if (lp_ctx->s3_fns) { \
277 SMB_ASSERT(lp_ctx->s3_fns->fn_name); \
278 return lp_ctx->s3_fns->fn_name(); \
280 return lp_ctx->globals->var_name; \
283 /* Local parameters don't need the ->s3_fns because the struct
284 * loadparm_service is shared and lpcfg_service() checks the ->s3_fns
285 * hook */
286 #define FN_LOCAL_STRING(fn_name,val) \
287 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_service *service, \
288 struct loadparm_service *sDefault) { \
289 return(lp_string((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val))); \
292 #define FN_LOCAL_CONST_STRING(fn_name,val) FN_LOCAL_STRING(fn_name, val)
294 #define FN_LOCAL_LIST(fn_name,val) \
295 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_service *service, \
296 struct loadparm_service *sDefault) {\
297 return(const char **)(service != NULL && service->val != NULL? service->val : sDefault->val); \
300 #define FN_LOCAL_PARM_BOOL(fn_name, val) FN_LOCAL_BOOL(fn_name, val)
302 #define FN_LOCAL_BOOL(fn_name,val) \
303 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_service *service, \
304 struct loadparm_service *sDefault) { \
305 return((service != NULL)? service->val : sDefault->val); \
308 #define FN_LOCAL_INTEGER(fn_name,val) \
309 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_service *service, \
310 struct loadparm_service *sDefault) { \
311 return((service != NULL)? service->val : sDefault->val); \
314 #define FN_LOCAL_PARM_INTEGER(fn_name, val) FN_LOCAL_INTEGER(fn_name, val)
316 #define FN_LOCAL_PARM_CHAR(fn_name,val) \
317 _PUBLIC_ char lpcfg_ ## fn_name(struct loadparm_service *service, \
318 struct loadparm_service *sDefault) { \
319 return((service != NULL)? service->val : sDefault->val); \
322 #include "lib/param/param_functions.c"
324 /* These functions remain only in lib/param for now */
325 FN_GLOBAL_BOOL(readraw, bReadRaw)
326 FN_GLOBAL_BOOL(writeraw, bWriteRaw)
327 FN_GLOBAL_CONST_STRING(cachedir, szCacheDir)
328 FN_GLOBAL_CONST_STRING(statedir, szStateDir)
330 /* local prototypes */
331 static int map_parameter(const char *pszParmName);
332 static struct loadparm_service *getservicebyname(struct loadparm_context *lp_ctx,
333 const char *pszServiceName);
334 static void copy_service(struct loadparm_service *pserviceDest,
335 struct loadparm_service *pserviceSource,
336 struct bitmap *pcopymapDest);
337 static bool lpcfg_service_ok(struct loadparm_service *service);
338 static bool do_section(const char *pszSectionName, void *);
339 static void init_copymap(struct loadparm_service *pservice);
341 /* This is a helper function for parametrical options support. */
342 /* It returns a pointer to parametrical option value if it exists or NULL otherwise */
343 /* Actual parametrical functions are quite simple */
344 const char *lpcfg_get_parametric(struct loadparm_context *lp_ctx,
345 struct loadparm_service *service,
346 const char *type, const char *option)
348 char *vfskey_tmp = NULL;
349 char *vfskey = NULL;
350 struct parmlist_entry *data;
352 if (lp_ctx == NULL)
353 return NULL;
355 if (lp_ctx->s3_fns) {
356 return lp_ctx->s3_fns->get_parametric(service, type, option);
359 data = (service == NULL ? lp_ctx->globals->param_opt : service->param_opt);
361 vfskey_tmp = talloc_asprintf(NULL, "%s:%s", type, option);
362 if (vfskey_tmp == NULL) return NULL;
363 vfskey = strlower_talloc(NULL, vfskey_tmp);
364 talloc_free(vfskey_tmp);
366 while (data) {
367 if (strcmp(data->key, vfskey) == 0) {
368 talloc_free(vfskey);
369 return data->value;
371 data = data->next;
374 if (service != NULL) {
375 /* Try to fetch the same option but from globals */
376 /* but only if we are not already working with globals */
377 for (data = lp_ctx->globals->param_opt; data;
378 data = data->next) {
379 if (strcmp(data->key, vfskey) == 0) {
380 talloc_free(vfskey);
381 return data->value;
386 talloc_free(vfskey);
388 return NULL;
393 * convenience routine to return int parameters.
395 static int lp_int(const char *s)
398 if (!s) {
399 DEBUG(0,("lp_int(%s): is called with NULL!\n",s));
400 return -1;
403 return strtol(s, NULL, 0);
407 * convenience routine to return unsigned long parameters.
409 static unsigned long lp_ulong(const char *s)
412 if (!s) {
413 DEBUG(0,("lp_ulong(%s): is called with NULL!\n",s));
414 return -1;
417 return strtoul(s, NULL, 0);
421 * convenience routine to return unsigned long parameters.
423 static long lp_long(const char *s)
426 if (!s) {
427 DEBUG(0,("lp_long(%s): is called with NULL!\n",s));
428 return -1;
431 return strtol(s, NULL, 0);
435 * convenience routine to return unsigned long parameters.
437 static double lp_double(const char *s)
440 if (!s) {
441 DEBUG(0,("lp_double(%s): is called with NULL!\n",s));
442 return -1;
445 return strtod(s, NULL);
449 * convenience routine to return boolean parameters.
451 static bool lp_bool(const char *s)
453 bool ret = false;
455 if (!s) {
456 DEBUG(0,("lp_bool(%s): is called with NULL!\n",s));
457 return false;
460 if (!set_boolean(s, &ret)) {
461 DEBUG(0,("lp_bool(%s): value is not boolean!\n",s));
462 return false;
465 return ret;
470 * Return parametric option from a given service. Type is a part of option before ':'
471 * Parametric option has following syntax: 'Type: option = value'
472 * Returned value is allocated in 'lp_talloc' context
475 const char *lpcfg_parm_string(struct loadparm_context *lp_ctx,
476 struct loadparm_service *service, const char *type,
477 const char *option)
479 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
481 if (value)
482 return lp_string(value);
484 return NULL;
488 * Return parametric option from a given service. Type is a part of option before ':'
489 * Parametric option has following syntax: 'Type: option = value'
490 * Returned value is allocated in 'lp_talloc' context
493 const char **lpcfg_parm_string_list(TALLOC_CTX *mem_ctx,
494 struct loadparm_context *lp_ctx,
495 struct loadparm_service *service,
496 const char *type,
497 const char *option, const char *separator)
499 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
501 if (value != NULL)
502 return (const char **)str_list_make(mem_ctx, value, separator);
504 return NULL;
508 * Return parametric option from a given service. Type is a part of option before ':'
509 * Parametric option has following syntax: 'Type: option = value'
512 int lpcfg_parm_int(struct loadparm_context *lp_ctx,
513 struct loadparm_service *service, const char *type,
514 const char *option, int default_v)
516 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
518 if (value)
519 return lp_int(value);
521 return default_v;
525 * Return parametric option from a given service. Type is a part of
526 * option before ':'.
527 * Parametric option has following syntax: 'Type: option = value'.
530 int lpcfg_parm_bytes(struct loadparm_context *lp_ctx,
531 struct loadparm_service *service, const char *type,
532 const char *option, int default_v)
534 uint64_t bval;
536 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
538 if (value && conv_str_size_error(value, &bval)) {
539 if (bval <= INT_MAX) {
540 return (int)bval;
544 return default_v;
548 * Return parametric option from a given service.
549 * Type is a part of option before ':'
550 * Parametric option has following syntax: 'Type: option = value'
552 unsigned long lpcfg_parm_ulong(struct loadparm_context *lp_ctx,
553 struct loadparm_service *service, const char *type,
554 const char *option, unsigned long default_v)
556 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
558 if (value)
559 return lp_ulong(value);
561 return default_v;
564 long lpcfg_parm_long(struct loadparm_context *lp_ctx,
565 struct loadparm_service *service, const char *type,
566 const char *option, long default_v)
568 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
570 if (value)
571 return lp_long(value);
573 return default_v;
576 double lpcfg_parm_double(struct loadparm_context *lp_ctx,
577 struct loadparm_service *service, const char *type,
578 const char *option, double default_v)
580 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
582 if (value != NULL)
583 return lp_double(value);
585 return default_v;
589 * Return parametric option from a given service. Type is a part of option before ':'
590 * Parametric option has following syntax: 'Type: option = value'
593 bool lpcfg_parm_bool(struct loadparm_context *lp_ctx,
594 struct loadparm_service *service, const char *type,
595 const char *option, bool default_v)
597 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
599 if (value != NULL)
600 return lp_bool(value);
602 return default_v;
607 * Initialise a service to the defaults.
610 static struct loadparm_service *init_service(TALLOC_CTX *mem_ctx, struct loadparm_service *sDefault)
612 struct loadparm_service *pservice =
613 talloc_zero(mem_ctx, struct loadparm_service);
614 copy_service(pservice, sDefault, NULL);
615 return pservice;
619 * Set a string value, deallocating any existing space, and allocing the space
620 * for the string
622 static bool lpcfg_string_set(TALLOC_CTX *mem_ctx, char **dest, const char *src)
624 talloc_free(*dest);
626 if (src == NULL)
627 src = "";
629 *dest = talloc_strdup(mem_ctx, src);
630 if ((*dest) == NULL) {
631 DEBUG(0,("Out of memory in string_set\n"));
632 return false;
635 return true;
639 * Set a string value, deallocating any existing space, and allocing the space
640 * for the string
642 static bool lpcfg_string_set_upper(TALLOC_CTX *mem_ctx, char **dest, const char *src)
644 talloc_free(*dest);
646 if (src == NULL)
647 src = "";
649 *dest = strupper_talloc(mem_ctx, src);
650 if ((*dest) == NULL) {
651 DEBUG(0,("Out of memory in string_set_upper\n"));
652 return false;
655 return true;
661 * Add a new service to the services array initialising it with the given
662 * service.
665 struct loadparm_service *lpcfg_add_service(struct loadparm_context *lp_ctx,
666 const struct loadparm_service *pservice,
667 const char *name)
669 int i;
670 struct loadparm_service tservice;
671 int num_to_alloc = lp_ctx->iNumServices + 1;
672 struct parmlist_entry *data, *pdata;
674 if (pservice == NULL) {
675 pservice = lp_ctx->sDefault;
678 tservice = *pservice;
680 /* it might already exist */
681 if (name) {
682 struct loadparm_service *service = getservicebyname(lp_ctx,
683 name);
684 if (service != NULL) {
685 /* Clean all parametric options for service */
686 /* They will be added during parsing again */
687 data = service->param_opt;
688 while (data) {
689 pdata = data->next;
690 talloc_free(data);
691 data = pdata;
693 service->param_opt = NULL;
694 return service;
698 /* find an invalid one */
699 for (i = 0; i < lp_ctx->iNumServices; i++)
700 if (lp_ctx->services[i] == NULL)
701 break;
703 /* if not, then create one */
704 if (i == lp_ctx->iNumServices) {
705 struct loadparm_service **tsp;
707 tsp = talloc_realloc(lp_ctx, lp_ctx->services, struct loadparm_service *, num_to_alloc);
709 if (!tsp) {
710 DEBUG(0,("lpcfg_add_service: failed to enlarge services!\n"));
711 return NULL;
712 } else {
713 lp_ctx->services = tsp;
714 lp_ctx->services[lp_ctx->iNumServices] = NULL;
717 lp_ctx->iNumServices++;
720 lp_ctx->services[i] = init_service(lp_ctx->services, lp_ctx->sDefault);
721 if (lp_ctx->services[i] == NULL) {
722 DEBUG(0,("lpcfg_add_service: out of memory!\n"));
723 return NULL;
725 copy_service(lp_ctx->services[i], &tservice, NULL);
726 if (name != NULL)
727 lpcfg_string_set(lp_ctx->services[i], &lp_ctx->services[i]->szService, name);
728 return lp_ctx->services[i];
732 * Add a new home service, with the specified home directory, defaults coming
733 * from service ifrom.
736 bool lpcfg_add_home(struct loadparm_context *lp_ctx,
737 const char *pszHomename,
738 struct loadparm_service *default_service,
739 const char *user, const char *pszHomedir)
741 struct loadparm_service *service;
743 service = lpcfg_add_service(lp_ctx, default_service, pszHomename);
745 if (service == NULL)
746 return false;
748 if (!(*(default_service->pathname))
749 || strequal(default_service->pathname, lp_ctx->sDefault->pathname)) {
750 service->pathname = talloc_strdup(service, pszHomedir);
751 } else {
752 service->pathname = string_sub_talloc(service, lpcfg_pathname(default_service, lp_ctx->sDefault), "%H", pszHomedir);
755 if (!(*(service->comment))) {
756 service->comment = talloc_asprintf(service, "Home directory of %s", user);
758 service->bAvailable = default_service->bAvailable;
759 service->browseable = default_service->browseable;
761 DEBUG(3, ("adding home's share [%s] for user '%s' at '%s'\n",
762 pszHomename, user, service->pathname));
764 return true;
768 * Add a new printer service, with defaults coming from service iFrom.
771 bool lpcfg_add_printer(struct loadparm_context *lp_ctx,
772 const char *pszPrintername,
773 struct loadparm_service *default_service)
775 const char *comment = "From Printcap";
776 struct loadparm_service *service;
777 service = lpcfg_add_service(lp_ctx, default_service, pszPrintername);
779 if (service == NULL)
780 return false;
782 /* note that we do NOT default the availability flag to True - */
783 /* we take it from the default service passed. This allows all */
784 /* dynamic printers to be disabled by disabling the [printers] */
785 /* entry (if/when the 'available' keyword is implemented!). */
787 /* the printer name is set to the service name. */
788 lpcfg_string_set(service, &service->szPrintername, pszPrintername);
789 lpcfg_string_set(service, &service->comment, comment);
790 service->browseable = default_service->browseable;
791 /* Printers cannot be read_only. */
792 service->readonly = false;
793 /* Printer services must be printable. */
794 service->print_ok = true;
796 DEBUG(3, ("adding printer service %s\n", pszPrintername));
798 return true;
802 * Map a parameter's string representation to something we can use.
803 * Returns False if the parameter string is not recognised, else TRUE.
806 static int map_parameter(const char *pszParmName)
808 int iIndex;
810 if (*pszParmName == '-')
811 return -1;
813 for (iIndex = 0; parm_table[iIndex].label; iIndex++)
814 if (strwicmp(parm_table[iIndex].label, pszParmName) == 0)
815 return iIndex;
817 /* Warn only if it isn't parametric option */
818 if (strchr(pszParmName, ':') == NULL)
819 DEBUG(0, ("Unknown parameter encountered: \"%s\"\n", pszParmName));
820 /* We do return 'fail' for parametric options as well because they are
821 stored in different storage
823 return -1;
828 return the parameter structure for a parameter
830 struct parm_struct *lpcfg_parm_struct(struct loadparm_context *lp_ctx, const char *name)
832 int parmnum;
834 if (lp_ctx->s3_fns) {
835 return lp_ctx->s3_fns->get_parm_struct(name);
838 parmnum = map_parameter(name);
839 if (parmnum == -1) return NULL;
840 return &parm_table[parmnum];
844 return the parameter pointer for a parameter
846 void *lpcfg_parm_ptr(struct loadparm_context *lp_ctx,
847 struct loadparm_service *service, struct parm_struct *parm)
849 if (lp_ctx->s3_fns) {
850 return lp_ctx->s3_fns->get_parm_ptr(service, parm);
853 if (service == NULL) {
854 if (parm->p_class == P_LOCAL)
855 return ((char *)lp_ctx->sDefault)+parm->offset;
856 else if (parm->p_class == P_GLOBAL)
857 return ((char *)lp_ctx->globals)+parm->offset;
858 else return NULL;
859 } else {
860 return ((char *)service) + parm->offset;
865 return the parameter pointer for a parameter
867 bool lpcfg_parm_is_cmdline(struct loadparm_context *lp_ctx, const char *name)
869 int parmnum;
871 if (lp_ctx->s3_fns) {
872 struct parm_struct *parm = lp_ctx->s3_fns->get_parm_struct(name);
873 if (parm) {
874 return parm->flags & FLAG_CMDLINE;
876 return false;
879 parmnum = map_parameter(name);
880 if (parmnum == -1) return false;
882 return lp_ctx->flags[parmnum] & FLAG_CMDLINE;
886 * Find a service by name. Otherwise works like get_service.
889 static struct loadparm_service *getservicebyname(struct loadparm_context *lp_ctx,
890 const char *pszServiceName)
892 int iService;
894 if (lp_ctx->s3_fns) {
895 return lp_ctx->s3_fns->get_service(pszServiceName);
898 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--)
899 if (lp_ctx->services[iService] != NULL &&
900 strwicmp(lp_ctx->services[iService]->szService, pszServiceName) == 0) {
901 return lp_ctx->services[iService];
904 return NULL;
908 * Copy a service structure to another.
909 * If pcopymapDest is NULL then copy all fields
912 static void copy_service(struct loadparm_service *pserviceDest,
913 struct loadparm_service *pserviceSource,
914 struct bitmap *pcopymapDest)
916 int i;
917 bool bcopyall = (pcopymapDest == NULL);
918 struct parmlist_entry *data, *pdata, *paramo;
919 bool not_added;
921 for (i = 0; parm_table[i].label; i++)
922 if (parm_table[i].p_class == P_LOCAL &&
923 (bcopyall || bitmap_query(pcopymapDest, i))) {
924 void *src_ptr =
925 ((char *)pserviceSource) + parm_table[i].offset;
926 void *dest_ptr =
927 ((char *)pserviceDest) + parm_table[i].offset;
929 switch (parm_table[i].type) {
930 case P_BOOL:
931 *(bool *)dest_ptr = *(bool *)src_ptr;
932 break;
934 case P_INTEGER:
935 case P_BYTES:
936 case P_OCTAL:
937 case P_ENUM:
938 *(int *)dest_ptr = *(int *)src_ptr;
939 break;
941 case P_STRING:
942 lpcfg_string_set(pserviceDest,
943 (char **)dest_ptr,
944 *(char **)src_ptr);
945 break;
947 case P_USTRING:
948 lpcfg_string_set_upper(pserviceDest,
949 (char **)dest_ptr,
950 *(char **)src_ptr);
951 break;
952 case P_LIST:
953 *(const char ***)dest_ptr = (const char **)str_list_copy(pserviceDest,
954 *(const char ***)src_ptr);
955 break;
956 default:
957 break;
961 if (bcopyall) {
962 init_copymap(pserviceDest);
963 if (pserviceSource->copymap)
964 bitmap_copy(pserviceDest->copymap,
965 pserviceSource->copymap);
968 data = pserviceSource->param_opt;
969 while (data) {
970 not_added = true;
971 pdata = pserviceDest->param_opt;
972 /* Traverse destination */
973 while (pdata) {
974 /* If we already have same option, override it */
975 if (strcmp(pdata->key, data->key) == 0) {
976 talloc_free(pdata->value);
977 pdata->value = talloc_strdup(pdata,
978 data->value);
979 not_added = false;
980 break;
982 pdata = pdata->next;
984 if (not_added) {
985 paramo = talloc_zero(pserviceDest, struct parmlist_entry);
986 if (paramo == NULL)
987 smb_panic("OOM");
988 paramo->key = talloc_strdup(paramo, data->key);
989 paramo->value = talloc_strdup(paramo, data->value);
990 DLIST_ADD(pserviceDest->param_opt, paramo);
992 data = data->next;
997 * Check a service for consistency. Return False if the service is in any way
998 * incomplete or faulty, else True.
1000 static bool lpcfg_service_ok(struct loadparm_service *service)
1002 bool bRetval;
1004 bRetval = true;
1005 if (service->szService[0] == '\0') {
1006 DEBUG(0, ("The following message indicates an internal error:\n"));
1007 DEBUG(0, ("No service name in service entry.\n"));
1008 bRetval = false;
1011 /* The [printers] entry MUST be printable. I'm all for flexibility, but */
1012 /* I can't see why you'd want a non-printable printer service... */
1013 if (strwicmp(service->szService, PRINTERS_NAME) == 0) {
1014 if (!service->print_ok) {
1015 DEBUG(0, ("WARNING: [%s] service MUST be printable!\n",
1016 service->szService));
1017 service->print_ok = true;
1019 /* [printers] service must also be non-browsable. */
1020 if (service->browseable)
1021 service->browseable = false;
1024 /* If a service is flagged unavailable, log the fact at level 0. */
1025 if (!service->bAvailable)
1026 DEBUG(1, ("NOTE: Service %s is flagged unavailable.\n",
1027 service->szService));
1029 return bRetval;
1033 /*******************************************************************
1034 Keep a linked list of all config files so we know when one has changed
1035 it's date and needs to be reloaded.
1036 ********************************************************************/
1038 static void add_to_file_list(struct loadparm_context *lp_ctx,
1039 const char *fname, const char *subfname)
1041 struct file_lists *f = lp_ctx->file_lists;
1043 while (f) {
1044 if (f->name && !strcmp(f->name, fname))
1045 break;
1046 f = f->next;
1049 if (!f) {
1050 f = talloc(lp_ctx, struct file_lists);
1051 if (!f)
1052 return;
1053 f->next = lp_ctx->file_lists;
1054 f->name = talloc_strdup(f, fname);
1055 if (!f->name) {
1056 talloc_free(f);
1057 return;
1059 f->subfname = talloc_strdup(f, subfname);
1060 if (!f->subfname) {
1061 talloc_free(f);
1062 return;
1064 lp_ctx->file_lists = f;
1065 f->modtime = file_modtime(subfname);
1066 } else {
1067 time_t t = file_modtime(subfname);
1068 if (t)
1069 f->modtime = t;
1073 /*******************************************************************
1074 Check if a config file has changed date.
1075 ********************************************************************/
1076 bool lpcfg_file_list_changed(struct loadparm_context *lp_ctx)
1078 struct file_lists *f;
1079 DEBUG(6, ("lpcfg_file_list_changed()\n"));
1081 for (f = lp_ctx->file_lists; f != NULL; f = f->next) {
1082 char *n2;
1083 time_t mod_time;
1085 n2 = standard_sub_basic(lp_ctx, f->name);
1087 DEBUGADD(6, ("file %s -> %s last mod_time: %s\n",
1088 f->name, n2, ctime(&f->modtime)));
1090 mod_time = file_modtime(n2);
1092 if (mod_time && ((f->modtime != mod_time) || (f->subfname == NULL) || (strcmp(n2, f->subfname) != 0))) {
1093 DEBUGADD(6, ("file %s modified: %s\n", n2,
1094 ctime(&mod_time)));
1095 f->modtime = mod_time;
1096 talloc_free(f->subfname);
1097 f->subfname = talloc_strdup(f, n2);
1098 return true;
1101 return false;
1104 /***************************************************************************
1105 Handle the "realm" parameter
1106 ***************************************************************************/
1108 static bool handle_realm(struct loadparm_context *lp_ctx, int unused,
1109 const char *pszParmValue, char **ptr)
1111 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1113 talloc_free(lp_ctx->globals->szRealm_upper);
1114 talloc_free(lp_ctx->globals->dnsdomain);
1116 lp_ctx->globals->szRealm_upper = strupper_talloc(lp_ctx, pszParmValue);
1117 lp_ctx->globals->dnsdomain = strlower_talloc(lp_ctx, pszParmValue);
1119 return true;
1122 /***************************************************************************
1123 Handle the include operation.
1124 ***************************************************************************/
1126 static bool handle_include(struct loadparm_context *lp_ctx, int unused,
1127 const char *pszParmValue, char **ptr)
1129 char *fname = standard_sub_basic(lp_ctx, pszParmValue);
1131 add_to_file_list(lp_ctx, pszParmValue, fname);
1133 lpcfg_string_set(lp_ctx, ptr, fname);
1135 if (file_exist(fname))
1136 return pm_process(fname, do_section, do_parameter, lp_ctx);
1138 DEBUG(2, ("Can't find include file %s\n", fname));
1140 return false;
1143 /***************************************************************************
1144 Handle the interpretation of the copy parameter.
1145 ***************************************************************************/
1147 static bool handle_copy(struct loadparm_context *lp_ctx, int unused,
1148 const char *pszParmValue, char **ptr)
1150 bool bRetval;
1151 struct loadparm_service *serviceTemp;
1153 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1155 bRetval = false;
1157 DEBUG(3, ("Copying service from service %s\n", pszParmValue));
1159 if ((serviceTemp = getservicebyname(lp_ctx, pszParmValue)) != NULL) {
1160 if (serviceTemp == lp_ctx->currentService) {
1161 DEBUG(0, ("Can't copy service %s - unable to copy self!\n", pszParmValue));
1162 } else {
1163 copy_service(lp_ctx->currentService,
1164 serviceTemp,
1165 lp_ctx->currentService->copymap);
1166 bRetval = true;
1168 } else {
1169 DEBUG(0, ("Unable to copy service - source not found: %s\n",
1170 pszParmValue));
1171 bRetval = false;
1174 return bRetval;
1177 static bool handle_debug_list(struct loadparm_context *lp_ctx, int unused,
1178 const char *pszParmValue, char **ptr)
1181 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1182 if (lp_ctx->global) {
1183 return debug_parse_levels(pszParmValue);
1185 return true;
1188 static bool handle_logfile(struct loadparm_context *lp_ctx, int unused,
1189 const char *pszParmValue, char **ptr)
1191 debug_set_logfile(pszParmValue);
1192 if (lp_ctx->global) {
1193 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1195 return true;
1198 /***************************************************************************
1199 Initialise a copymap.
1200 ***************************************************************************/
1202 static void init_copymap(struct loadparm_service *pservice)
1204 int i;
1206 TALLOC_FREE(pservice->copymap);
1208 pservice->copymap = bitmap_talloc(NULL, NUMPARAMETERS);
1209 if (!pservice->copymap)
1210 DEBUG(0,
1211 ("Couldn't allocate copymap!! (size %d)\n",
1212 (int)NUMPARAMETERS));
1213 else
1214 for (i = 0; i < NUMPARAMETERS; i++)
1215 bitmap_set(pservice->copymap, i);
1219 * Process a parametric option
1221 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
1222 struct loadparm_service *service,
1223 const char *pszParmName,
1224 const char *pszParmValue, int flags)
1226 struct parmlist_entry *paramo, *data;
1227 char *name;
1228 TALLOC_CTX *mem_ctx;
1230 while (isspace((unsigned char)*pszParmName)) {
1231 pszParmName++;
1234 name = strlower_talloc(lp_ctx, pszParmName);
1235 if (!name) return false;
1237 if (service == NULL) {
1238 data = lp_ctx->globals->param_opt;
1239 mem_ctx = lp_ctx->globals;
1240 } else {
1241 data = service->param_opt;
1242 mem_ctx = service;
1245 /* Traverse destination */
1246 for (paramo=data; paramo; paramo=paramo->next) {
1247 /* If we already have the option set, override it unless
1248 it was a command line option and the new one isn't */
1249 if (strcmp(paramo->key, name) == 0) {
1250 if ((paramo->priority & FLAG_CMDLINE) &&
1251 !(flags & FLAG_CMDLINE)) {
1252 talloc_free(name);
1253 return true;
1256 talloc_free(paramo->value);
1257 paramo->value = talloc_strdup(paramo, pszParmValue);
1258 paramo->priority = flags;
1259 talloc_free(name);
1260 return true;
1264 paramo = talloc_zero(mem_ctx, struct parmlist_entry);
1265 if (!paramo)
1266 smb_panic("OOM");
1267 paramo->key = talloc_strdup(paramo, name);
1268 paramo->value = talloc_strdup(paramo, pszParmValue);
1269 paramo->priority = flags;
1270 if (service == NULL) {
1271 DLIST_ADD(lp_ctx->globals->param_opt, paramo);
1272 } else {
1273 DLIST_ADD(service->param_opt, paramo);
1276 talloc_free(name);
1278 return true;
1281 static bool set_variable(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
1282 const char *pszParmName, const char *pszParmValue,
1283 struct loadparm_context *lp_ctx, bool on_globals)
1285 int i;
1286 /* if it is a special case then go ahead */
1287 if (parm_table[parmnum].special) {
1288 bool ret;
1289 ret = parm_table[parmnum].special(lp_ctx, -1, pszParmValue,
1290 (char **)parm_ptr);
1291 if (!ret) {
1292 return false;
1294 goto mark_non_default;
1297 /* now switch on the type of variable it is */
1298 switch (parm_table[parmnum].type)
1300 case P_BOOL: {
1301 bool b;
1302 if (!set_boolean(pszParmValue, &b)) {
1303 DEBUG(0, ("set_variable(%s): value is not "
1304 "boolean!\n", pszParmValue));
1305 return false;
1307 *(bool *)parm_ptr = b;
1309 break;
1311 case P_BOOLREV: {
1312 bool b;
1313 if (!set_boolean(pszParmValue, &b)) {
1314 DEBUG(0, ("set_variable(%s): value is not "
1315 "boolean!\n", pszParmValue));
1316 return false;
1318 *(bool *)parm_ptr = !b;
1320 break;
1322 case P_INTEGER:
1323 *(int *)parm_ptr = atoi(pszParmValue);
1324 break;
1326 case P_CHAR:
1327 *(char *)parm_ptr = *pszParmValue;
1328 break;
1330 case P_OCTAL:
1331 *(int *)parm_ptr = strtol(pszParmValue, NULL, 8);
1332 break;
1334 case P_BYTES:
1336 uint64_t val;
1337 if (conv_str_size_error(pszParmValue, &val)) {
1338 if (val <= INT_MAX) {
1339 *(int *)parm_ptr = (int)val;
1340 break;
1344 DEBUG(0, ("set_variable(%s): value is not "
1345 "a valid size specifier!\n", pszParmValue));
1346 return false;
1349 case P_CMDLIST:
1350 *(const char ***)parm_ptr = (const char **)str_list_make(mem_ctx,
1351 pszParmValue, NULL);
1352 break;
1353 case P_LIST:
1355 char **new_list = str_list_make(mem_ctx,
1356 pszParmValue, NULL);
1357 for (i=0; new_list[i]; i++) {
1358 if (*(const char ***)parm_ptr != NULL &&
1359 new_list[i][0] == '+' &&
1360 new_list[i][1])
1362 if (!str_list_check(*(const char ***)parm_ptr,
1363 &new_list[i][1])) {
1364 *(const char ***)parm_ptr = str_list_add(*(const char ***)parm_ptr,
1365 &new_list[i][1]);
1367 } else if (*(const char ***)parm_ptr != NULL &&
1368 new_list[i][0] == '-' &&
1369 new_list[i][1])
1371 str_list_remove(*(const char ***)parm_ptr,
1372 &new_list[i][1]);
1373 } else {
1374 if (i != 0) {
1375 DEBUG(0, ("Unsupported list syntax for: %s = %s\n",
1376 pszParmName, pszParmValue));
1377 return false;
1379 *(const char ***)parm_ptr = (const char **) new_list;
1380 break;
1383 break;
1385 case P_STRING:
1386 lpcfg_string_set(mem_ctx, (char **)parm_ptr, pszParmValue);
1387 break;
1389 case P_USTRING:
1390 lpcfg_string_set_upper(mem_ctx, (char **)parm_ptr, pszParmValue);
1391 break;
1393 case P_ENUM:
1394 for (i = 0; parm_table[parmnum].enum_list[i].name; i++) {
1395 if (strequal
1396 (pszParmValue,
1397 parm_table[parmnum].enum_list[i].name)) {
1398 *(int *)parm_ptr =
1399 parm_table[parmnum].
1400 enum_list[i].value;
1401 break;
1404 if (!parm_table[parmnum].enum_list[i].name) {
1405 DEBUG(0,("Unknown enumerated value '%s' for '%s'\n",
1406 pszParmValue, pszParmName));
1407 return false;
1409 break;
1411 case P_SEP:
1412 break;
1415 mark_non_default:
1416 if (on_globals && (lp_ctx->flags[parmnum] & FLAG_DEFAULT)) {
1417 lp_ctx->flags[parmnum] &= ~FLAG_DEFAULT;
1418 /* we have to also unset FLAG_DEFAULT on aliases */
1419 for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
1420 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1422 for (i=parmnum+1;i<NUMPARAMETERS && parm_table[i].offset == parm_table[parmnum].offset;i++) {
1423 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1426 return true;
1430 bool lpcfg_do_global_parameter(struct loadparm_context *lp_ctx,
1431 const char *pszParmName, const char *pszParmValue)
1433 int parmnum = map_parameter(pszParmName);
1434 void *parm_ptr;
1436 if (parmnum < 0) {
1437 if (strchr(pszParmName, ':')) {
1438 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName, pszParmValue, 0);
1440 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1441 return true;
1444 /* if the flag has been set on the command line, then don't allow override,
1445 but don't report an error */
1446 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1447 return true;
1450 parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[parmnum]);
1452 return set_variable(lp_ctx->globals, parmnum, parm_ptr,
1453 pszParmName, pszParmValue, lp_ctx, true);
1456 bool lpcfg_do_service_parameter(struct loadparm_context *lp_ctx,
1457 struct loadparm_service *service,
1458 const char *pszParmName, const char *pszParmValue)
1460 void *parm_ptr;
1461 int i;
1462 int parmnum = map_parameter(pszParmName);
1464 if (parmnum < 0) {
1465 if (strchr(pszParmName, ':')) {
1466 return lp_do_parameter_parametric(lp_ctx, service, pszParmName, pszParmValue, 0);
1468 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1469 return true;
1472 /* if the flag has been set on the command line, then don't allow override,
1473 but don't report an error */
1474 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1475 return true;
1478 if (parm_table[parmnum].p_class == P_GLOBAL) {
1479 DEBUG(0,
1480 ("Global parameter %s found in service section!\n",
1481 pszParmName));
1482 return true;
1484 parm_ptr = ((char *)service) + parm_table[parmnum].offset;
1486 if (!service->copymap)
1487 init_copymap(service);
1489 /* this handles the aliases - set the copymap for other
1490 * entries with the same data pointer */
1491 for (i = 0; parm_table[i].label; i++)
1492 if (parm_table[i].offset == parm_table[parmnum].offset &&
1493 parm_table[i].p_class == parm_table[parmnum].p_class)
1494 bitmap_clear(service->copymap, i);
1496 return set_variable(service, parmnum, parm_ptr, pszParmName,
1497 pszParmValue, lp_ctx, false);
1501 * Process a parameter.
1504 static bool do_parameter(const char *pszParmName, const char *pszParmValue,
1505 void *userdata)
1507 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1509 if (lp_ctx->bInGlobalSection)
1510 return lpcfg_do_global_parameter(lp_ctx, pszParmName,
1511 pszParmValue);
1512 else
1513 return lpcfg_do_service_parameter(lp_ctx, lp_ctx->currentService,
1514 pszParmName, pszParmValue);
1518 variable argument do parameter
1520 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx, const char *pszParmName, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
1521 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx,
1522 const char *pszParmName, const char *fmt, ...)
1524 char *s;
1525 bool ret;
1526 va_list ap;
1528 va_start(ap, fmt);
1529 s = talloc_vasprintf(NULL, fmt, ap);
1530 va_end(ap);
1531 ret = lpcfg_do_global_parameter(lp_ctx, pszParmName, s);
1532 talloc_free(s);
1533 return ret;
1538 set a parameter from the commandline - this is called from command line parameter
1539 parsing code. It sets the parameter then marks the parameter as unable to be modified
1540 by smb.conf processing
1542 bool lpcfg_set_cmdline(struct loadparm_context *lp_ctx, const char *pszParmName,
1543 const char *pszParmValue)
1545 int parmnum;
1546 int i;
1548 if (lp_ctx->s3_fns) {
1549 return lp_ctx->s3_fns->set_cmdline(pszParmName, pszParmValue);
1552 parmnum = map_parameter(pszParmName);
1554 while (isspace((unsigned char)*pszParmValue)) pszParmValue++;
1557 if (parmnum < 0 && strchr(pszParmName, ':')) {
1558 /* set a parametric option */
1559 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName,
1560 pszParmValue, FLAG_CMDLINE);
1563 if (parmnum < 0) {
1564 DEBUG(0,("Unknown option '%s'\n", pszParmName));
1565 return false;
1568 /* reset the CMDLINE flag in case this has been called before */
1569 lp_ctx->flags[parmnum] &= ~FLAG_CMDLINE;
1571 if (!lpcfg_do_global_parameter(lp_ctx, pszParmName, pszParmValue)) {
1572 return false;
1575 lp_ctx->flags[parmnum] |= FLAG_CMDLINE;
1577 /* we have to also set FLAG_CMDLINE on aliases */
1578 for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
1579 lp_ctx->flags[i] |= FLAG_CMDLINE;
1581 for (i=parmnum+1;i<NUMPARAMETERS && parm_table[i].offset == parm_table[parmnum].offset;i++) {
1582 lp_ctx->flags[i] |= FLAG_CMDLINE;
1585 return true;
1589 set a option from the commandline in 'a=b' format. Use to support --option
1591 bool lpcfg_set_option(struct loadparm_context *lp_ctx, const char *option)
1593 char *p, *s;
1594 bool ret;
1596 s = talloc_strdup(NULL, option);
1597 if (!s) {
1598 return false;
1601 p = strchr(s, '=');
1602 if (!p) {
1603 talloc_free(s);
1604 return false;
1607 *p = 0;
1609 ret = lpcfg_set_cmdline(lp_ctx, s, p+1);
1610 talloc_free(s);
1611 return ret;
1615 #define BOOLSTR(b) ((b) ? "Yes" : "No")
1618 * Print a parameter of the specified type.
1621 static void print_parameter(struct parm_struct *p, void *ptr, FILE * f)
1623 /* For the seperation of lists values that we print below */
1624 const char *list_sep = ", ";
1625 int i;
1626 switch (p->type)
1628 case P_ENUM:
1629 for (i = 0; p->enum_list[i].name; i++) {
1630 if (*(int *)ptr == p->enum_list[i].value) {
1631 fprintf(f, "%s",
1632 p->enum_list[i].name);
1633 break;
1636 break;
1638 case P_BOOL:
1639 fprintf(f, "%s", BOOLSTR(*(bool *)ptr));
1640 break;
1642 case P_BOOLREV:
1643 fprintf(f, "%s", BOOLSTR(!*(bool *)ptr));
1644 break;
1646 case P_INTEGER:
1647 case P_BYTES:
1648 fprintf(f, "%d", *(int *)ptr);
1649 break;
1651 case P_CHAR:
1652 fprintf(f, "%c", *(char *)ptr);
1653 break;
1655 case P_OCTAL: {
1656 int val = *(int *)ptr;
1657 if (val == -1) {
1658 fprintf(f, "-1");
1659 } else {
1660 fprintf(f, "0%o", val);
1662 break;
1665 case P_CMDLIST:
1666 list_sep = " ";
1667 /* fall through */
1668 case P_LIST:
1669 if ((char ***)ptr && *(char ***)ptr) {
1670 char **list = *(char ***)ptr;
1671 for (; *list; list++) {
1672 /* surround strings with whitespace in double quotes */
1673 if (*(list+1) == NULL) {
1674 /* last item, no extra separator */
1675 list_sep = "";
1677 if ( strchr_m( *list, ' ' ) ) {
1678 fprintf(f, "\"%s\"%s", *list, list_sep);
1679 } else {
1680 fprintf(f, "%s%s", *list, list_sep);
1684 break;
1686 case P_STRING:
1687 case P_USTRING:
1688 if (*(char **)ptr) {
1689 fprintf(f, "%s", *(char **)ptr);
1691 break;
1692 case P_SEP:
1693 break;
1698 * Check if two parameters are equal.
1701 static bool equal_parameter(parm_type type, void *ptr1, void *ptr2)
1703 switch (type) {
1704 case P_BOOL:
1705 case P_BOOLREV:
1706 return (*((bool *)ptr1) == *((bool *)ptr2));
1708 case P_INTEGER:
1709 case P_ENUM:
1710 case P_OCTAL:
1711 case P_BYTES:
1712 return (*((int *)ptr1) == *((int *)ptr2));
1714 case P_CHAR:
1715 return (*((char *)ptr1) == *((char *)ptr2));
1717 case P_LIST:
1718 case P_CMDLIST:
1719 return str_list_equal(*(const char ***)ptr1, *(const char ***)ptr2);
1721 case P_STRING:
1722 case P_USTRING:
1724 char *p1 = *(char **)ptr1, *p2 = *(char **)ptr2;
1725 if (p1 && !*p1)
1726 p1 = NULL;
1727 if (p2 && !*p2)
1728 p2 = NULL;
1729 return (p1 == p2 || strequal(p1, p2));
1731 case P_SEP:
1732 break;
1734 return false;
1738 * Process a new section (service).
1740 * At this stage all sections are services.
1741 * Later we'll have special sections that permit server parameters to be set.
1742 * Returns True on success, False on failure.
1745 static bool do_section(const char *pszSectionName, void *userdata)
1747 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1748 bool bRetval;
1749 bool isglobal = ((strwicmp(pszSectionName, GLOBAL_NAME) == 0) ||
1750 (strwicmp(pszSectionName, GLOBAL_NAME2) == 0));
1751 bRetval = false;
1753 /* if we've just struck a global section, note the fact. */
1754 lp_ctx->bInGlobalSection = isglobal;
1756 /* check for multiple global sections */
1757 if (lp_ctx->bInGlobalSection) {
1758 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1759 return true;
1762 /* if we have a current service, tidy it up before moving on */
1763 bRetval = true;
1765 if (lp_ctx->currentService != NULL)
1766 bRetval = lpcfg_service_ok(lp_ctx->currentService);
1768 /* if all is still well, move to the next record in the services array */
1769 if (bRetval) {
1770 /* We put this here to avoid an odd message order if messages are */
1771 /* issued by the post-processing of a previous section. */
1772 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1774 if ((lp_ctx->currentService = lpcfg_add_service(lp_ctx, lp_ctx->sDefault,
1775 pszSectionName))
1776 == NULL) {
1777 DEBUG(0, ("Failed to add a new service\n"));
1778 return false;
1782 return bRetval;
1787 * Determine if a particular base parameter is currently set to the default value.
1790 static bool is_default(struct loadparm_service *sDefault, int i)
1792 void *def_ptr = ((char *)sDefault) + parm_table[i].offset;
1793 if (!defaults_saved)
1794 return false;
1795 switch (parm_table[i].type) {
1796 case P_CMDLIST:
1797 case P_LIST:
1798 return str_list_equal((const char **)parm_table[i].def.lvalue,
1799 (const char **)def_ptr);
1800 case P_STRING:
1801 case P_USTRING:
1802 return strequal(parm_table[i].def.svalue,
1803 *(char **)def_ptr);
1804 case P_BOOL:
1805 case P_BOOLREV:
1806 return parm_table[i].def.bvalue ==
1807 *(bool *)def_ptr;
1808 case P_INTEGER:
1809 case P_CHAR:
1810 case P_OCTAL:
1811 case P_BYTES:
1812 case P_ENUM:
1813 return parm_table[i].def.ivalue ==
1814 *(int *)def_ptr;
1815 case P_SEP:
1816 break;
1818 return false;
1822 *Display the contents of the global structure.
1825 static void dump_globals(struct loadparm_context *lp_ctx, FILE *f,
1826 bool show_defaults)
1828 int i;
1829 struct parmlist_entry *data;
1831 fprintf(f, "# Global parameters\n[global]\n");
1833 for (i = 0; parm_table[i].label; i++)
1834 if (parm_table[i].p_class == P_GLOBAL &&
1835 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset))) {
1836 if (!show_defaults && (lp_ctx->flags[i] & FLAG_DEFAULT))
1837 continue;
1838 fprintf(f, "\t%s = ", parm_table[i].label);
1839 print_parameter(&parm_table[i], lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[i]), f);
1840 fprintf(f, "\n");
1842 if (lp_ctx->globals->param_opt != NULL) {
1843 for (data = lp_ctx->globals->param_opt; data;
1844 data = data->next) {
1845 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
1846 continue;
1848 fprintf(f, "\t%s = %s\n", data->key, data->value);
1855 * Display the contents of a single services record.
1858 static void dump_a_service(struct loadparm_service * pService, struct loadparm_service *sDefault, FILE * f,
1859 unsigned int *flags)
1861 int i;
1862 struct parmlist_entry *data;
1864 if (pService != sDefault)
1865 fprintf(f, "\n[%s]\n", pService->szService);
1867 for (i = 0; parm_table[i].label; i++) {
1868 if (parm_table[i].p_class == P_LOCAL &&
1869 (*parm_table[i].label != '-') &&
1870 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset)))
1872 if (pService == sDefault) {
1873 if (flags && (flags[i] & FLAG_DEFAULT)) {
1874 continue;
1876 if (defaults_saved) {
1877 if (is_default(sDefault, i)) {
1878 continue;
1881 } else {
1882 if (equal_parameter(parm_table[i].type,
1883 ((char *)pService) +
1884 parm_table[i].offset,
1885 ((char *)sDefault) +
1886 parm_table[i].offset))
1887 continue;
1890 fprintf(f, "\t%s = ", parm_table[i].label);
1891 print_parameter(&parm_table[i],
1892 ((char *)pService) + parm_table[i].offset, f);
1893 fprintf(f, "\n");
1896 if (pService->param_opt != NULL) {
1897 for (data = pService->param_opt; data; data = data->next) {
1898 fprintf(f, "\t%s = %s\n", data->key, data->value);
1903 bool lpcfg_dump_a_parameter(struct loadparm_context *lp_ctx,
1904 struct loadparm_service *service,
1905 const char *parm_name, FILE * f)
1907 struct parm_struct *parm;
1908 void *ptr;
1910 parm = lpcfg_parm_struct(lp_ctx, parm_name);
1911 if (!parm) {
1912 return false;
1915 ptr = lpcfg_parm_ptr(lp_ctx, service,parm);
1917 print_parameter(parm, ptr, f);
1918 fprintf(f, "\n");
1919 return true;
1923 * Return info about the next parameter in a service.
1924 * snum==-1 gives the globals.
1925 * Return NULL when out of parameters.
1929 struct parm_struct *lpcfg_next_parameter(struct loadparm_context *lp_ctx, int snum, int *i,
1930 int allparameters)
1932 if (snum == -1) {
1933 /* do the globals */
1934 for (; parm_table[*i].label; (*i)++) {
1935 if ((*parm_table[*i].label == '-'))
1936 continue;
1938 if ((*i) > 0
1939 && (parm_table[*i].offset ==
1940 parm_table[(*i) - 1].offset)
1941 && (parm_table[*i].p_class ==
1942 parm_table[(*i) - 1].p_class))
1943 continue;
1945 return &parm_table[(*i)++];
1947 } else {
1948 struct loadparm_service *pService = lp_ctx->services[snum];
1950 for (; parm_table[*i].label; (*i)++) {
1951 if (parm_table[*i].p_class == P_LOCAL &&
1952 (*parm_table[*i].label != '-') &&
1953 ((*i) == 0 ||
1954 (parm_table[*i].offset !=
1955 parm_table[(*i) - 1].offset)))
1957 if (allparameters ||
1958 !equal_parameter(parm_table[*i].type,
1959 ((char *)pService) +
1960 parm_table[*i].offset,
1961 ((char *)lp_ctx->sDefault) +
1962 parm_table[*i].offset))
1964 return &parm_table[(*i)++];
1970 return NULL;
1975 * Auto-load some home services.
1977 static void lpcfg_add_auto_services(struct loadparm_context *lp_ctx,
1978 const char *str)
1980 return;
1985 * Unload unused services.
1988 void lpcfg_killunused(struct loadparm_context *lp_ctx,
1989 struct smbsrv_connection *smb,
1990 bool (*snumused) (struct smbsrv_connection *, int))
1992 int i;
1993 for (i = 0; i < lp_ctx->iNumServices; i++) {
1994 if (lp_ctx->services[i] == NULL)
1995 continue;
1997 if (!snumused || !snumused(smb, i)) {
1998 talloc_free(lp_ctx->services[i]);
1999 lp_ctx->services[i] = NULL;
2005 static int lpcfg_destructor(struct loadparm_context *lp_ctx)
2007 struct parmlist_entry *data;
2009 if (lp_ctx->refuse_free) {
2010 /* someone is trying to free the
2011 global_loadparm_context.
2012 We can't allow that. */
2013 return -1;
2016 if (lp_ctx->globals->param_opt != NULL) {
2017 struct parmlist_entry *next;
2018 for (data = lp_ctx->globals->param_opt; data; data=next) {
2019 next = data->next;
2020 if (data->priority & FLAG_CMDLINE) continue;
2021 DLIST_REMOVE(lp_ctx->globals->param_opt, data);
2022 talloc_free(data);
2026 return 0;
2030 * Initialise the global parameter structure.
2032 * Note that most callers should use loadparm_init_global() instead
2034 struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
2036 int i;
2037 char *myname;
2038 struct loadparm_context *lp_ctx;
2039 struct parmlist_entry *parm;
2040 char *logfile;
2042 lp_ctx = talloc_zero(mem_ctx, struct loadparm_context);
2043 if (lp_ctx == NULL)
2044 return NULL;
2046 talloc_set_destructor(lp_ctx, lpcfg_destructor);
2047 lp_ctx->bInGlobalSection = true;
2048 lp_ctx->globals = talloc_zero(lp_ctx, struct loadparm_global);
2049 lp_ctx->sDefault = talloc_zero(lp_ctx, struct loadparm_service);
2051 lp_ctx->sDefault->iMaxPrintJobs = 1000;
2052 lp_ctx->sDefault->bAvailable = true;
2053 lp_ctx->sDefault->browseable = true;
2054 lp_ctx->sDefault->readonly = true;
2055 lp_ctx->sDefault->map_archive = true;
2056 lp_ctx->sDefault->strict_locking = true;
2057 lp_ctx->sDefault->oplocks = true;
2058 lp_ctx->sDefault->create_mask = 0744;
2059 lp_ctx->sDefault->force_create_mode = 0000;
2060 lp_ctx->sDefault->dir_mask = 0755;
2061 lp_ctx->sDefault->force_dir_mode = 0000;
2063 DEBUG(3, ("Initialising global parameters\n"));
2065 for (i = 0; parm_table[i].label; i++) {
2066 if ((parm_table[i].type == P_STRING ||
2067 parm_table[i].type == P_USTRING) &&
2068 !(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2069 char **r;
2070 if (parm_table[i].p_class == P_LOCAL) {
2071 r = (char **)(((char *)lp_ctx->sDefault) + parm_table[i].offset);
2072 } else {
2073 r = (char **)(((char *)lp_ctx->globals) + parm_table[i].offset);
2075 *r = talloc_strdup(lp_ctx, "");
2079 logfile = talloc_asprintf(lp_ctx, "%s/log.samba", dyn_LOGFILEBASE);
2080 lpcfg_do_global_parameter(lp_ctx, "log file", logfile);
2081 talloc_free(logfile);
2083 lpcfg_do_global_parameter(lp_ctx, "log level", "0");
2085 lpcfg_do_global_parameter(lp_ctx, "syslog", "1");
2086 lpcfg_do_global_parameter(lp_ctx, "syslog only", "No");
2087 lpcfg_do_global_parameter(lp_ctx, "debug timestamp", "Yes");
2088 lpcfg_do_global_parameter(lp_ctx, "debug prefix timestamp", "No");
2089 lpcfg_do_global_parameter(lp_ctx, "debug hires timestamp", "Yes");
2090 lpcfg_do_global_parameter(lp_ctx, "debug pid", "No");
2091 lpcfg_do_global_parameter(lp_ctx, "debug uid", "No");
2092 lpcfg_do_global_parameter(lp_ctx, "debug class", "No");
2094 lpcfg_do_global_parameter(lp_ctx, "share backend", "classic");
2096 lpcfg_do_global_parameter(lp_ctx, "server role", "auto");
2097 lpcfg_do_global_parameter(lp_ctx, "domain logons", "No");
2098 lpcfg_do_global_parameter(lp_ctx, "domain master", "Auto");
2100 /* options that can be set on the command line must be initialised via
2101 the slower lpcfg_do_global_parameter() to ensure that FLAG_CMDLINE is obeyed */
2102 #ifdef TCP_NODELAY
2103 lpcfg_do_global_parameter(lp_ctx, "socket options", "TCP_NODELAY");
2104 #endif
2105 lpcfg_do_global_parameter(lp_ctx, "workgroup", DEFAULT_WORKGROUP);
2106 myname = get_myname(lp_ctx);
2107 lpcfg_do_global_parameter(lp_ctx, "netbios name", myname);
2108 talloc_free(myname);
2109 lpcfg_do_global_parameter(lp_ctx, "name resolve order", "wins host bcast");
2111 lpcfg_do_global_parameter(lp_ctx, "fstype", "NTFS");
2113 lpcfg_do_global_parameter(lp_ctx, "ntvfs handler", "unixuid default");
2114 lpcfg_do_global_parameter(lp_ctx, "max connections", "-1");
2116 lpcfg_do_global_parameter(lp_ctx, "dcerpc endpoint servers", "epmapper wkssvc rpcecho samr netlogon lsarpc spoolss drsuapi dssetup unixinfo browser eventlog6 backupkey dnsserver");
2117 lpcfg_do_global_parameter(lp_ctx, "server services", "s3fs rpc nbt wrepl ldap cldap kdc drepl winbind ntp_signd kcc dnsupdate dns");
2118 lpcfg_do_global_parameter(lp_ctx, "kccsrv:samba_kcc", "true");
2119 /* the winbind method for domain controllers is for both RODC
2120 auth forwarding and for trusted domains */
2121 lpcfg_do_global_parameter(lp_ctx, "private dir", dyn_PRIVATE_DIR);
2122 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_LOCAL_MACHINE", "hklm.ldb");
2124 /* This hive should be dynamically generated by Samba using
2125 data from the sam, but for the moment leave it in a tdb to
2126 keep regedt32 from popping up an annoying dialog. */
2127 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_USERS", "hku.ldb");
2129 /* using UTF8 by default allows us to support all chars */
2130 lpcfg_do_global_parameter(lp_ctx, "unix charset", "UTF8");
2132 /* Use codepage 850 as a default for the dos character set */
2133 lpcfg_do_global_parameter(lp_ctx, "dos charset", "CP850");
2136 * Allow the default PASSWD_CHAT to be overridden in local.h.
2138 lpcfg_do_global_parameter(lp_ctx, "passwd chat", DEFAULT_PASSWD_CHAT);
2140 lpcfg_do_global_parameter(lp_ctx, "pid directory", dyn_PIDDIR);
2141 lpcfg_do_global_parameter(lp_ctx, "lock dir", dyn_LOCKDIR);
2142 lpcfg_do_global_parameter(lp_ctx, "state directory", dyn_STATEDIR);
2143 lpcfg_do_global_parameter(lp_ctx, "cache directory", dyn_CACHEDIR);
2144 lpcfg_do_global_parameter(lp_ctx, "ncalrpc dir", dyn_NCALRPCDIR);
2146 lpcfg_do_global_parameter(lp_ctx, "nbt client socket address", "");
2147 lpcfg_do_global_parameter_var(lp_ctx, "server string",
2148 "Samba %s", SAMBA_VERSION_STRING);
2150 lpcfg_do_global_parameter(lp_ctx, "password server", "*");
2152 lpcfg_do_global_parameter(lp_ctx, "max mux", "50");
2153 lpcfg_do_global_parameter(lp_ctx, "max xmit", "12288");
2154 lpcfg_do_global_parameter(lp_ctx, "host msdfs", "true");
2156 lpcfg_do_global_parameter(lp_ctx, "LargeReadwrite", "True");
2157 lpcfg_do_global_parameter(lp_ctx, "server min protocol", "CORE");
2158 lpcfg_do_global_parameter(lp_ctx, "server max protocol", "NT1");
2159 lpcfg_do_global_parameter(lp_ctx, "client min protocol", "CORE");
2160 lpcfg_do_global_parameter(lp_ctx, "client max protocol", "NT1");
2161 lpcfg_do_global_parameter(lp_ctx, "security", "AUTO");
2162 lpcfg_do_global_parameter(lp_ctx, "EncryptPasswords", "True");
2163 lpcfg_do_global_parameter(lp_ctx, "ReadRaw", "True");
2164 lpcfg_do_global_parameter(lp_ctx, "WriteRaw", "True");
2165 lpcfg_do_global_parameter(lp_ctx, "NullPasswords", "False");
2166 lpcfg_do_global_parameter(lp_ctx, "ObeyPamRestrictions", "False");
2168 lpcfg_do_global_parameter(lp_ctx, "TimeServer", "False");
2169 lpcfg_do_global_parameter(lp_ctx, "BindInterfacesOnly", "False");
2170 lpcfg_do_global_parameter(lp_ctx, "Unicode", "True");
2171 lpcfg_do_global_parameter(lp_ctx, "ClientLanManAuth", "False");
2172 lpcfg_do_global_parameter(lp_ctx, "ClientNTLMv2Auth", "True");
2173 lpcfg_do_global_parameter(lp_ctx, "LanmanAuth", "False");
2174 lpcfg_do_global_parameter(lp_ctx, "NTLMAuth", "True");
2175 lpcfg_do_global_parameter(lp_ctx, "client use spnego principal", "False");
2177 lpcfg_do_global_parameter(lp_ctx, "UnixExtensions", "True");
2179 lpcfg_do_global_parameter(lp_ctx, "PreferredMaster", "Auto");
2180 lpcfg_do_global_parameter(lp_ctx, "LocalMaster", "True");
2182 lpcfg_do_global_parameter(lp_ctx, "wins support", "False");
2183 lpcfg_do_global_parameter(lp_ctx, "dns proxy", "True");
2185 lpcfg_do_global_parameter(lp_ctx, "winbind separator", "\\");
2186 lpcfg_do_global_parameter(lp_ctx, "winbind sealed pipes", "True");
2187 lpcfg_do_global_parameter(lp_ctx, "require strong key", "True");
2188 lpcfg_do_global_parameter(lp_ctx, "winbindd socket directory", dyn_WINBINDD_SOCKET_DIR);
2189 lpcfg_do_global_parameter(lp_ctx, "winbindd privileged socket directory", dyn_WINBINDD_PRIVILEGED_SOCKET_DIR);
2190 lpcfg_do_global_parameter(lp_ctx, "ntp signd socket directory", dyn_NTP_SIGND_SOCKET_DIR);
2191 lpcfg_do_global_parameter_var(lp_ctx, "dns update command", "%s/samba_dnsupdate", dyn_SCRIPTSBINDIR);
2192 lpcfg_do_global_parameter_var(lp_ctx, "spn update command", "%s/samba_spnupdate", dyn_SCRIPTSBINDIR);
2193 lpcfg_do_global_parameter_var(lp_ctx, "samba kcc command",
2194 "%s/samba_kcc", dyn_SCRIPTSBINDIR);
2195 lpcfg_do_global_parameter(lp_ctx, "template shell", "/bin/false");
2196 lpcfg_do_global_parameter(lp_ctx, "template homedir", "/home/%WORKGROUP%/%ACCOUNTNAME%");
2198 lpcfg_do_global_parameter(lp_ctx, "client signing", "default");
2199 lpcfg_do_global_parameter(lp_ctx, "server signing", "default");
2201 lpcfg_do_global_parameter(lp_ctx, "use spnego", "True");
2203 lpcfg_do_global_parameter(lp_ctx, "use mmap", "True");
2205 lpcfg_do_global_parameter(lp_ctx, "smb ports", "445 139");
2206 lpcfg_do_global_parameter(lp_ctx, "nbt port", "137");
2207 lpcfg_do_global_parameter(lp_ctx, "dgram port", "138");
2208 lpcfg_do_global_parameter(lp_ctx, "cldap port", "389");
2209 lpcfg_do_global_parameter(lp_ctx, "krb5 port", "88");
2210 lpcfg_do_global_parameter(lp_ctx, "kpasswd port", "464");
2211 lpcfg_do_global_parameter(lp_ctx, "web port", "901");
2213 lpcfg_do_global_parameter(lp_ctx, "nt status support", "True");
2215 lpcfg_do_global_parameter(lp_ctx, "max wins ttl", "518400"); /* 6 days */
2216 lpcfg_do_global_parameter(lp_ctx, "min wins ttl", "10");
2218 lpcfg_do_global_parameter(lp_ctx, "tls enabled", "True");
2219 lpcfg_do_global_parameter(lp_ctx, "tls keyfile", "tls/key.pem");
2220 lpcfg_do_global_parameter(lp_ctx, "tls certfile", "tls/cert.pem");
2221 lpcfg_do_global_parameter(lp_ctx, "tls cafile", "tls/ca.pem");
2222 lpcfg_do_global_parameter(lp_ctx, "prefork children:smb", "4");
2224 lpcfg_do_global_parameter(lp_ctx, "rndc command", "/usr/sbin/rndc");
2225 lpcfg_do_global_parameter(lp_ctx, "nsupdate command", "/usr/bin/nsupdate -g");
2227 lpcfg_do_global_parameter(lp_ctx, "allow dns updates", "secure only");
2228 lpcfg_do_global_parameter(lp_ctx, "dns forwarder", "");
2230 for (i = 0; parm_table[i].label; i++) {
2231 if (!(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2232 lp_ctx->flags[i] |= FLAG_DEFAULT;
2236 for (parm=lp_ctx->globals->param_opt; parm; parm=parm->next) {
2237 if (!(parm->priority & FLAG_CMDLINE)) {
2238 parm->priority |= FLAG_DEFAULT;
2242 return lp_ctx;
2246 * Initialise the global parameter structure.
2248 struct loadparm_context *loadparm_init_global(bool load_default)
2250 if (global_loadparm_context == NULL) {
2251 global_loadparm_context = loadparm_init(NULL);
2253 if (global_loadparm_context == NULL) {
2254 return NULL;
2256 global_loadparm_context->global = true;
2257 if (load_default && !global_loadparm_context->loaded) {
2258 lpcfg_load_default(global_loadparm_context);
2260 global_loadparm_context->refuse_free = true;
2261 return global_loadparm_context;
2265 * Initialise the global parameter structure.
2267 struct loadparm_context *loadparm_init_s3(TALLOC_CTX *mem_ctx,
2268 const struct loadparm_s3_helpers *s3_fns)
2270 struct loadparm_context *loadparm_context = talloc_zero(mem_ctx, struct loadparm_context);
2271 if (!loadparm_context) {
2272 return NULL;
2274 loadparm_context->s3_fns = s3_fns;
2275 return loadparm_context;
2278 const char *lpcfg_configfile(struct loadparm_context *lp_ctx)
2280 return lp_ctx->szConfigFile;
2283 const char *lp_default_path(void)
2285 if (getenv("SMB_CONF_PATH"))
2286 return getenv("SMB_CONF_PATH");
2287 else
2288 return dyn_CONFIGFILE;
2292 * Update the internal state of a loadparm context after settings
2293 * have changed.
2295 static bool lpcfg_update(struct loadparm_context *lp_ctx)
2297 struct debug_settings settings;
2298 lpcfg_add_auto_services(lp_ctx, lpcfg_auto_services(lp_ctx));
2300 if (!lp_ctx->globals->szWINSservers && lp_ctx->globals->we_are_a_wins_server) {
2301 lpcfg_do_global_parameter(lp_ctx, "wins server", "127.0.0.1");
2304 if (!lp_ctx->global) {
2305 return true;
2308 panic_action = lp_ctx->globals->szPanicAction;
2310 reload_charcnv(lp_ctx);
2312 ZERO_STRUCT(settings);
2313 /* Add any more debug-related smb.conf parameters created in
2314 * future here */
2315 settings.syslog = lp_ctx->globals->syslog;
2316 settings.syslog_only = lp_ctx->globals->syslog_only;
2317 settings.timestamp_logs = lp_ctx->globals->timestamp_logs;
2318 settings.debug_prefix_timestamp = lp_ctx->globals->debug_prefix_timestamp;
2319 settings.debug_hires_timestamp = lp_ctx->globals->debug_hires_timestamp;
2320 settings.debug_pid = lp_ctx->globals->debug_pid;
2321 settings.debug_uid = lp_ctx->globals->debug_uid;
2322 settings.debug_class = lp_ctx->globals->debug_class;
2323 debug_set_settings(&settings);
2325 /* FIXME: This is a bit of a hack, but we can't use a global, since
2326 * not everything that uses lp also uses the socket library */
2327 if (lpcfg_parm_bool(lp_ctx, NULL, "socket", "testnonblock", false)) {
2328 setenv("SOCKET_TESTNONBLOCK", "1", 1);
2329 } else {
2330 unsetenv("SOCKET_TESTNONBLOCK");
2333 return true;
2336 bool lpcfg_load_default(struct loadparm_context *lp_ctx)
2338 const char *path;
2340 path = lp_default_path();
2342 if (!file_exist(path)) {
2343 /* We allow the default smb.conf file to not exist,
2344 * basically the equivalent of an empty file. */
2345 return lpcfg_update(lp_ctx);
2348 return lpcfg_load(lp_ctx, path);
2352 * Load the services array from the services file.
2354 * Return True on success, False on failure.
2356 bool lpcfg_load(struct loadparm_context *lp_ctx, const char *filename)
2358 char *n2;
2359 bool bRetval;
2361 filename = talloc_strdup(lp_ctx, filename);
2363 lp_ctx->szConfigFile = filename;
2365 if (lp_ctx->s3_fns) {
2366 return lp_ctx->s3_fns->load(filename);
2369 lp_ctx->bInGlobalSection = true;
2370 n2 = standard_sub_basic(lp_ctx, lp_ctx->szConfigFile);
2371 DEBUG(2, ("lpcfg_load: refreshing parameters from %s\n", n2));
2373 add_to_file_list(lp_ctx, lp_ctx->szConfigFile, n2);
2375 /* We get sections first, so have to start 'behind' to make up */
2376 lp_ctx->currentService = NULL;
2377 bRetval = pm_process(n2, do_section, do_parameter, lp_ctx);
2379 /* finish up the last section */
2380 DEBUG(4, ("pm_process() returned %s\n", BOOLSTR(bRetval)));
2381 if (bRetval)
2382 if (lp_ctx->currentService != NULL)
2383 bRetval = lpcfg_service_ok(lp_ctx->currentService);
2385 bRetval = bRetval && lpcfg_update(lp_ctx);
2387 /* we do this unconditionally, so that it happens even
2388 for a missing smb.conf */
2389 reload_charcnv(lp_ctx);
2391 if (bRetval == true) {
2392 /* set this up so that any child python tasks will
2393 find the right smb.conf */
2394 setenv("SMB_CONF_PATH", filename, 1);
2396 /* set the context used by the lp_*() function
2397 varients */
2398 global_loadparm_context = lp_ctx;
2399 lp_ctx->loaded = true;
2402 return bRetval;
2406 * Return the max number of services.
2409 int lpcfg_numservices(struct loadparm_context *lp_ctx)
2411 if (lp_ctx->s3_fns) {
2412 return lp_ctx->s3_fns->get_numservices();
2415 return lp_ctx->iNumServices;
2419 * Display the contents of the services array in human-readable form.
2422 void lpcfg_dump(struct loadparm_context *lp_ctx, FILE *f, bool show_defaults,
2423 int maxtoprint)
2425 int iService;
2427 if (lp_ctx->s3_fns) {
2428 lp_ctx->s3_fns->dump(f, show_defaults, maxtoprint);
2429 return;
2432 defaults_saved = !show_defaults;
2434 dump_globals(lp_ctx, f, show_defaults);
2436 dump_a_service(lp_ctx->sDefault, lp_ctx->sDefault, f, lp_ctx->flags);
2438 for (iService = 0; iService < maxtoprint; iService++)
2439 lpcfg_dump_one(f, show_defaults, lp_ctx->services[iService], lp_ctx->sDefault);
2443 * Display the contents of one service in human-readable form.
2445 void lpcfg_dump_one(FILE *f, bool show_defaults, struct loadparm_service *service, struct loadparm_service *sDefault)
2447 if (service != NULL) {
2448 if (service->szService[0] == '\0')
2449 return;
2450 dump_a_service(service, sDefault, f, NULL);
2454 struct loadparm_service *lpcfg_servicebynum(struct loadparm_context *lp_ctx,
2455 int snum)
2457 if (lp_ctx->s3_fns) {
2458 return lp_ctx->s3_fns->get_servicebynum(snum);
2461 return lp_ctx->services[snum];
2464 struct loadparm_service *lpcfg_service(struct loadparm_context *lp_ctx,
2465 const char *service_name)
2467 int iService;
2468 char *serviceName;
2470 if (lp_ctx->s3_fns) {
2471 return lp_ctx->s3_fns->get_service(service_name);
2474 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--) {
2475 if (lp_ctx->services[iService] &&
2476 lp_ctx->services[iService]->szService) {
2478 * The substitution here is used to support %U is
2479 * service names
2481 serviceName = standard_sub_basic(
2482 lp_ctx->services[iService],
2483 lp_ctx->services[iService]->szService);
2484 if (strequal(serviceName, service_name)) {
2485 talloc_free(serviceName);
2486 return lp_ctx->services[iService];
2488 talloc_free(serviceName);
2492 DEBUG(7,("lpcfg_servicenumber: couldn't find %s\n", service_name));
2493 return NULL;
2496 const char *lpcfg_servicename(const struct loadparm_service *service)
2498 return lp_string((const char *)service->szService);
2502 * A useful volume label function.
2504 const char *lpcfg_volume_label(struct loadparm_service *service, struct loadparm_service *sDefault)
2506 const char *ret;
2507 ret = lp_string((const char *)((service != NULL && service->volume != NULL) ?
2508 service->volume : sDefault->volume));
2509 if (!*ret)
2510 return lpcfg_servicename(service);
2511 return ret;
2515 * If we are PDC then prefer us as DMB
2517 const char *lpcfg_printername(struct loadparm_service *service, struct loadparm_service *sDefault)
2519 const char *ret;
2520 ret = lp_string((const char *)((service != NULL && service->szPrintername != NULL) ?
2521 service->szPrintername : sDefault->szPrintername));
2522 if (ret == NULL || (ret != NULL && *ret == '\0'))
2523 ret = lpcfg_servicename(service);
2525 return ret;
2530 * Return the max print jobs per queue.
2532 int lpcfg_maxprintjobs(struct loadparm_service *service, struct loadparm_service *sDefault)
2534 int maxjobs = (service != NULL) ? service->iMaxPrintJobs : sDefault->iMaxPrintJobs;
2535 if (maxjobs <= 0 || maxjobs >= PRINT_MAX_JOBID)
2536 maxjobs = PRINT_MAX_JOBID - 1;
2538 return maxjobs;
2541 struct smb_iconv_handle *lpcfg_iconv_handle(struct loadparm_context *lp_ctx)
2543 if (lp_ctx == NULL) {
2544 return get_iconv_handle();
2546 return lp_ctx->iconv_handle;
2549 _PUBLIC_ void reload_charcnv(struct loadparm_context *lp_ctx)
2551 struct smb_iconv_handle *old_ic = lp_ctx->iconv_handle;
2552 if (!lp_ctx->global) {
2553 return;
2556 if (old_ic == NULL) {
2557 old_ic = global_iconv_handle;
2559 lp_ctx->iconv_handle = smb_iconv_handle_reinit_lp(lp_ctx, lp_ctx, old_ic);
2560 global_iconv_handle = lp_ctx->iconv_handle;
2563 _PUBLIC_ char *lpcfg_tls_keyfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2565 return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_keyfile);
2568 _PUBLIC_ char *lpcfg_tls_certfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2570 return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_certfile);
2573 _PUBLIC_ char *lpcfg_tls_cafile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2575 return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_cafile);
2578 _PUBLIC_ char *lpcfg_tls_crlfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2580 return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_crlfile);
2583 _PUBLIC_ char *lpcfg_tls_dhpfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2585 return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_dhpfile);
2588 struct gensec_settings *lpcfg_gensec_settings(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2590 struct gensec_settings *settings = talloc_zero(mem_ctx, struct gensec_settings);
2591 if (settings == NULL)
2592 return NULL;
2593 SMB_ASSERT(lp_ctx != NULL);
2594 settings->lp_ctx = talloc_reference(settings, lp_ctx);
2595 settings->target_hostname = lpcfg_parm_string(lp_ctx, NULL, "gensec", "target_hostname");
2596 return settings;
2599 int lpcfg_server_role(struct loadparm_context *lp_ctx)
2601 int domain_master = lpcfg__domain_master(lp_ctx);
2603 return lp_find_server_role(lpcfg__server_role(lp_ctx),
2604 lpcfg__security(lp_ctx),
2605 lpcfg__domain_logons(lp_ctx),
2606 (domain_master == true) ||
2607 (domain_master == Auto));
2610 int lpcfg_security(struct loadparm_context *lp_ctx)
2612 return lp_find_security(lpcfg__server_role(lp_ctx),
2613 lpcfg__security(lp_ctx));
2616 bool lpcfg_server_signing_allowed(struct loadparm_context *lp_ctx, bool *mandatory)
2618 bool allowed = true;
2619 enum smb_signing_setting signing_setting = lpcfg_server_signing(lp_ctx);
2621 *mandatory = false;
2623 if (signing_setting == SMB_SIGNING_DEFAULT) {
2625 * If we are a domain controller, SMB signing is
2626 * really important, as it can prevent a number of
2627 * attacks on communications between us and the
2628 * clients
2630 * However, it really sucks (no sendfile, CPU
2631 * overhead) performance-wise when used on a
2632 * file server, so disable it by default
2633 * on non-DCs
2636 if (lpcfg_server_role(lp_ctx) >= ROLE_ACTIVE_DIRECTORY_DC) {
2637 signing_setting = SMB_SIGNING_REQUIRED;
2638 } else {
2639 signing_setting = SMB_SIGNING_OFF;
2643 switch (signing_setting) {
2644 case SMB_SIGNING_REQUIRED:
2645 *mandatory = true;
2646 break;
2647 case SMB_SIGNING_IF_REQUIRED:
2648 break;
2649 case SMB_SIGNING_DEFAULT:
2650 case SMB_SIGNING_OFF:
2651 allowed = false;
2652 break;
2655 return allowed;