autorid: introduce idmap_autorid_domsid_is_for_alloc()
[Samba.git] / lib / param / loadparm.c
blob97a06a0efd0e8b999a90bbdace926834028f01d7
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"
69 #include "tdb.h"
71 #define standard_sub_basic talloc_strdup
73 static bool do_parameter(const char *, const char *, void *);
74 static bool defaults_saved = false;
76 #include "lib/param/param_global.h"
78 #define NUMPARAMETERS (sizeof(parm_table) / sizeof(struct parm_struct))
80 /* we don't need a special handler for "dos charset" and "unix charset" */
81 #define handle_dos_charset NULL
82 #define handle_charset NULL
84 /* these are parameter handlers which are not needed in the
85 * non-source3 code
87 #define handle_netbios_aliases NULL
88 #define handle_printing NULL
89 #define handle_ldap_debug_level NULL
90 #define handle_idmap_backend NULL
91 #define handle_idmap_uid NULL
92 #define handle_idmap_gid NULL
94 #ifndef N_
95 #define N_(x) x
96 #endif
98 /* prototypes for the special type handlers */
99 static bool handle_include(struct loadparm_context *lp_ctx, int unused,
100 const char *pszParmValue, char **ptr);
101 static bool handle_realm(struct loadparm_context *lp_ctx, int unused,
102 const char *pszParmValue, char **ptr);
103 static bool handle_copy(struct loadparm_context *lp_ctx, int unused,
104 const char *pszParmValue, char **ptr);
105 static bool handle_debug_list(struct loadparm_context *lp_ctx, int unused,
106 const char *pszParmValue, char **ptr);
107 static bool handle_logfile(struct loadparm_context *lp_ctx, int unused,
108 const char *pszParmValue, char **ptr);
110 #include "lib/param/param_table.c"
112 /* local variables */
113 struct loadparm_context {
114 const char *szConfigFile;
115 struct loadparm_global *globals;
116 struct loadparm_service **services;
117 struct loadparm_service *sDefault;
118 struct smb_iconv_handle *iconv_handle;
119 int iNumServices;
120 struct loadparm_service *currentService;
121 bool bInGlobalSection;
122 struct file_lists {
123 struct file_lists *next;
124 char *name;
125 char *subfname;
126 time_t modtime;
127 } *file_lists;
128 unsigned int flags[NUMPARAMETERS];
129 bool loaded;
130 bool refuse_free;
131 bool global; /* Is this the global context, which may set
132 * global variables such as debug level etc? */
133 const struct loadparm_s3_helpers *s3_fns;
137 struct loadparm_service *lpcfg_default_service(struct loadparm_context *lp_ctx)
139 if (lp_ctx->s3_fns) {
140 return lp_ctx->s3_fns->get_default_loadparm_service();
142 return lp_ctx->sDefault;
146 * Convenience routine to grab string parameters into temporary memory
147 * and run standard_sub_basic on them.
149 * The buffers can be written to by
150 * callers without affecting the source string.
153 static const char *lpcfg_string(const char *s)
155 #if 0 /* until REWRITE done to make thread-safe */
156 size_t len = s ? strlen(s) : 0;
157 char *ret;
158 #endif
160 /* The follow debug is useful for tracking down memory problems
161 especially if you have an inner loop that is calling a lp_*()
162 function that returns a string. Perhaps this debug should be
163 present all the time? */
165 #if 0
166 DEBUG(10, ("lpcfg_string(%s)\n", s));
167 #endif
169 #if 0 /* until REWRITE done to make thread-safe */
170 if (!lp_talloc)
171 lp_talloc = talloc_init("lp_talloc");
173 ret = talloc_array(lp_talloc, char, len + 100); /* leave room for substitution */
175 if (!ret)
176 return NULL;
178 if (!s)
179 *ret = 0;
180 else
181 strlcpy(ret, s, len);
183 if (trim_string(ret, "\"", "\"")) {
184 if (strchr(ret,'"') != NULL)
185 strlcpy(ret, s, len);
188 standard_sub_basic(ret,len+100);
189 return (ret);
190 #endif
191 return s;
195 In this section all the functions that are used to access the
196 parameters from the rest of the program are defined
200 * the creation of separate lpcfg_*() and lp_*() functions is to allow
201 * for code compatibility between existing Samba4 and Samba3 code.
204 /* this global context supports the lp_*() function varients */
205 static struct loadparm_context *global_loadparm_context;
207 #define lpcfg_default_service global_loadparm_context->sDefault
208 #define lpcfg_global_service(i) global_loadparm_context->services[i]
210 #define FN_GLOBAL_STRING(fn_name,var_name) \
211 _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx, TALLOC_CTX *ctx) {\
212 if (lp_ctx == NULL) return NULL; \
213 if (lp_ctx->s3_fns) { \
214 return lp_ctx->globals->var_name ? lp_ctx->s3_fns->lp_string(ctx, lp_ctx->globals->var_name) : talloc_strdup(ctx, ""); \
216 return lp_ctx->globals->var_name ? talloc_strdup(ctx, lpcfg_string(lp_ctx->globals->var_name)) : talloc_strdup(ctx, ""); \
219 #define FN_GLOBAL_CONST_STRING(fn_name,var_name) \
220 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
221 if (lp_ctx == NULL) return NULL; \
222 return lp_ctx->globals->var_name ? lpcfg_string(lp_ctx->globals->var_name) : ""; \
225 #define FN_GLOBAL_LIST(fn_name,var_name) \
226 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
227 if (lp_ctx == NULL) return NULL; \
228 return lp_ctx->globals->var_name; \
231 #define FN_GLOBAL_BOOL(fn_name,var_name) \
232 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) {\
233 if (lp_ctx == NULL) return false; \
234 return lp_ctx->globals->var_name; \
237 #define FN_GLOBAL_INTEGER(fn_name,var_name) \
238 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
239 return lp_ctx->globals->var_name; \
242 /* Local parameters don't need the ->s3_fns because the struct
243 * loadparm_service is shared and lpcfg_service() checks the ->s3_fns
244 * hook */
245 #define FN_LOCAL_STRING(fn_name,val) \
246 _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_service *service, \
247 struct loadparm_service *sDefault, TALLOC_CTX *ctx) { \
248 return(talloc_strdup(ctx, lpcfg_string((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)))); \
251 #define FN_LOCAL_CONST_STRING(fn_name,val) \
252 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_service *service, \
253 struct loadparm_service *sDefault) { \
254 return((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)); \
257 #define FN_LOCAL_LIST(fn_name,val) \
258 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_service *service, \
259 struct loadparm_service *sDefault) {\
260 return(const char **)(service != NULL && service->val != NULL? service->val : sDefault->val); \
263 #define FN_LOCAL_PARM_BOOL(fn_name, val) FN_LOCAL_BOOL(fn_name, val)
265 #define FN_LOCAL_BOOL(fn_name,val) \
266 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_service *service, \
267 struct loadparm_service *sDefault) { \
268 return((service != NULL)? service->val : sDefault->val); \
271 #define FN_LOCAL_INTEGER(fn_name,val) \
272 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_service *service, \
273 struct loadparm_service *sDefault) { \
274 return((service != NULL)? service->val : sDefault->val); \
277 #define FN_LOCAL_PARM_INTEGER(fn_name, val) FN_LOCAL_INTEGER(fn_name, val)
279 #define FN_LOCAL_PARM_CHAR(fn_name,val) \
280 _PUBLIC_ char lpcfg_ ## fn_name(struct loadparm_service *service, \
281 struct loadparm_service *sDefault) { \
282 return((service != NULL)? service->val : sDefault->val); \
285 #include "lib/param/param_functions.c"
287 /* These functions cannot be auto-generated */
288 FN_LOCAL_BOOL(autoloaded, autoloaded)
289 FN_GLOBAL_CONST_STRING(dnsdomain, dnsdomain)
291 /* local prototypes */
292 static struct loadparm_service *getservicebyname(struct loadparm_context *lp_ctx,
293 const char *pszServiceName);
294 static void copy_service(struct loadparm_service *pserviceDest,
295 struct loadparm_service *pserviceSource,
296 struct bitmap *pcopymapDest);
297 static bool lpcfg_service_ok(struct loadparm_service *service);
298 static bool do_section(const char *pszSectionName, void *);
299 static void init_copymap(struct loadparm_service *pservice);
301 /* This is a helper function for parametrical options support. */
302 /* It returns a pointer to parametrical option value if it exists or NULL otherwise */
303 /* Actual parametrical functions are quite simple */
304 const char *lpcfg_get_parametric(struct loadparm_context *lp_ctx,
305 struct loadparm_service *service,
306 const char *type, const char *option)
308 char *vfskey_tmp = NULL;
309 char *vfskey = NULL;
310 struct parmlist_entry *data;
312 if (lp_ctx == NULL)
313 return NULL;
315 if (lp_ctx->s3_fns) {
316 return lp_ctx->s3_fns->get_parametric(service, type, option);
319 data = (service == NULL ? lp_ctx->globals->param_opt : service->param_opt);
321 vfskey_tmp = talloc_asprintf(NULL, "%s:%s", type, option);
322 if (vfskey_tmp == NULL) return NULL;
323 vfskey = strlower_talloc(NULL, vfskey_tmp);
324 talloc_free(vfskey_tmp);
326 while (data) {
327 if (strcmp(data->key, vfskey) == 0) {
328 talloc_free(vfskey);
329 return data->value;
331 data = data->next;
334 if (service != NULL) {
335 /* Try to fetch the same option but from globals */
336 /* but only if we are not already working with globals */
337 for (data = lp_ctx->globals->param_opt; data;
338 data = data->next) {
339 if (strcmp(data->key, vfskey) == 0) {
340 talloc_free(vfskey);
341 return data->value;
346 talloc_free(vfskey);
348 return NULL;
353 * convenience routine to return int parameters.
355 static int lp_int(const char *s)
358 if (!s) {
359 DEBUG(0,("lp_int(%s): is called with NULL!\n",s));
360 return -1;
363 return strtol(s, NULL, 0);
367 * convenience routine to return unsigned long parameters.
369 static unsigned long lp_ulong(const char *s)
372 if (!s) {
373 DEBUG(0,("lp_ulong(%s): is called with NULL!\n",s));
374 return -1;
377 return strtoul(s, NULL, 0);
381 * convenience routine to return unsigned long parameters.
383 static long lp_long(const char *s)
386 if (!s) {
387 DEBUG(0,("lp_long(%s): is called with NULL!\n",s));
388 return -1;
391 return strtol(s, NULL, 0);
395 * convenience routine to return unsigned long parameters.
397 static double lp_double(const char *s)
400 if (!s) {
401 DEBUG(0,("lp_double(%s): is called with NULL!\n",s));
402 return -1;
405 return strtod(s, NULL);
409 * convenience routine to return boolean parameters.
411 static bool lp_bool(const char *s)
413 bool ret = false;
415 if (!s) {
416 DEBUG(0,("lp_bool(%s): is called with NULL!\n",s));
417 return false;
420 if (!set_boolean(s, &ret)) {
421 DEBUG(0,("lp_bool(%s): value is not boolean!\n",s));
422 return false;
425 return ret;
430 * Return parametric option from a given service. Type is a part of option before ':'
431 * Parametric option has following syntax: 'Type: option = value'
432 * Returned value is allocated in 'lp_talloc' context
435 const char *lpcfg_parm_string(struct loadparm_context *lp_ctx,
436 struct loadparm_service *service, const char *type,
437 const char *option)
439 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
441 if (value)
442 return lpcfg_string(value);
444 return NULL;
448 * Return parametric option from a given service. Type is a part of option before ':'
449 * Parametric option has following syntax: 'Type: option = value'
450 * Returned value is allocated in 'lp_talloc' context
453 const char **lpcfg_parm_string_list(TALLOC_CTX *mem_ctx,
454 struct loadparm_context *lp_ctx,
455 struct loadparm_service *service,
456 const char *type,
457 const char *option, const char *separator)
459 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
461 if (value != NULL)
462 return (const char **)str_list_make(mem_ctx, value, separator);
464 return NULL;
468 * Return parametric option from a given service. Type is a part of option before ':'
469 * Parametric option has following syntax: 'Type: option = value'
472 int lpcfg_parm_int(struct loadparm_context *lp_ctx,
473 struct loadparm_service *service, const char *type,
474 const char *option, int default_v)
476 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
478 if (value)
479 return lp_int(value);
481 return default_v;
485 * Return parametric option from a given service. Type is a part of
486 * option before ':'.
487 * Parametric option has following syntax: 'Type: option = value'.
490 int lpcfg_parm_bytes(struct loadparm_context *lp_ctx,
491 struct loadparm_service *service, const char *type,
492 const char *option, int default_v)
494 uint64_t bval;
496 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
498 if (value && conv_str_size_error(value, &bval)) {
499 if (bval <= INT_MAX) {
500 return (int)bval;
504 return default_v;
508 * Return parametric option from a given service.
509 * Type is a part of option before ':'
510 * Parametric option has following syntax: 'Type: option = value'
512 unsigned long lpcfg_parm_ulong(struct loadparm_context *lp_ctx,
513 struct loadparm_service *service, const char *type,
514 const char *option, unsigned long default_v)
516 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
518 if (value)
519 return lp_ulong(value);
521 return default_v;
524 long lpcfg_parm_long(struct loadparm_context *lp_ctx,
525 struct loadparm_service *service, const char *type,
526 const char *option, long default_v)
528 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
530 if (value)
531 return lp_long(value);
533 return default_v;
536 double lpcfg_parm_double(struct loadparm_context *lp_ctx,
537 struct loadparm_service *service, const char *type,
538 const char *option, double default_v)
540 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
542 if (value != NULL)
543 return lp_double(value);
545 return default_v;
549 * Return parametric option from a given service. Type is a part of option before ':'
550 * Parametric option has following syntax: 'Type: option = value'
553 bool lpcfg_parm_bool(struct loadparm_context *lp_ctx,
554 struct loadparm_service *service, const char *type,
555 const char *option, bool default_v)
557 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
559 if (value != NULL)
560 return lp_bool(value);
562 return default_v;
567 * Initialise a service to the defaults.
570 static struct loadparm_service *init_service(TALLOC_CTX *mem_ctx, struct loadparm_service *sDefault)
572 struct loadparm_service *pservice =
573 talloc_zero(mem_ctx, struct loadparm_service);
574 copy_service(pservice, sDefault, NULL);
575 return pservice;
579 * Set a string value, deallocating any existing space, and allocing the space
580 * for the string
582 static bool lpcfg_string_set(TALLOC_CTX *mem_ctx, char **dest, const char *src)
584 talloc_free(*dest);
586 if (src == NULL)
587 src = "";
589 *dest = talloc_strdup(mem_ctx, src);
590 if ((*dest) == NULL) {
591 DEBUG(0,("Out of memory in string_set\n"));
592 return false;
595 return true;
599 * Set a string value, deallocating any existing space, and allocing the space
600 * for the string
602 static bool lpcfg_string_set_upper(TALLOC_CTX *mem_ctx, char **dest, const char *src)
604 talloc_free(*dest);
606 if (src == NULL)
607 src = "";
609 *dest = strupper_talloc(mem_ctx, src);
610 if ((*dest) == NULL) {
611 DEBUG(0,("Out of memory in string_set_upper\n"));
612 return false;
615 return true;
621 * Add a new service to the services array initialising it with the given
622 * service.
625 struct loadparm_service *lpcfg_add_service(struct loadparm_context *lp_ctx,
626 const struct loadparm_service *pservice,
627 const char *name)
629 int i;
630 struct loadparm_service tservice;
631 int num_to_alloc = lp_ctx->iNumServices + 1;
632 struct parmlist_entry *data, *pdata;
634 if (pservice == NULL) {
635 pservice = lp_ctx->sDefault;
638 tservice = *pservice;
640 /* it might already exist */
641 if (name) {
642 struct loadparm_service *service = getservicebyname(lp_ctx,
643 name);
644 if (service != NULL) {
645 /* Clean all parametric options for service */
646 /* They will be added during parsing again */
647 data = service->param_opt;
648 while (data) {
649 pdata = data->next;
650 talloc_free(data);
651 data = pdata;
653 service->param_opt = NULL;
654 return service;
658 /* find an invalid one */
659 for (i = 0; i < lp_ctx->iNumServices; i++)
660 if (lp_ctx->services[i] == NULL)
661 break;
663 /* if not, then create one */
664 if (i == lp_ctx->iNumServices) {
665 struct loadparm_service **tsp;
667 tsp = talloc_realloc(lp_ctx, lp_ctx->services, struct loadparm_service *, num_to_alloc);
669 if (!tsp) {
670 DEBUG(0,("lpcfg_add_service: failed to enlarge services!\n"));
671 return NULL;
672 } else {
673 lp_ctx->services = tsp;
674 lp_ctx->services[lp_ctx->iNumServices] = NULL;
677 lp_ctx->iNumServices++;
680 lp_ctx->services[i] = init_service(lp_ctx->services, lp_ctx->sDefault);
681 if (lp_ctx->services[i] == NULL) {
682 DEBUG(0,("lpcfg_add_service: out of memory!\n"));
683 return NULL;
685 copy_service(lp_ctx->services[i], &tservice, NULL);
686 if (name != NULL)
687 lpcfg_string_set(lp_ctx->services[i], &lp_ctx->services[i]->szService, name);
688 return lp_ctx->services[i];
692 * Add a new home service, with the specified home directory, defaults coming
693 * from service ifrom.
696 bool lpcfg_add_home(struct loadparm_context *lp_ctx,
697 const char *pszHomename,
698 struct loadparm_service *default_service,
699 const char *user, const char *pszHomedir)
701 struct loadparm_service *service;
703 service = lpcfg_add_service(lp_ctx, default_service, pszHomename);
705 if (service == NULL)
706 return false;
708 if (!(*(default_service->path))
709 || strequal(default_service->path, lp_ctx->sDefault->path)) {
710 service->path = talloc_strdup(service, pszHomedir);
711 } else {
712 service->path = string_sub_talloc(service, lpcfg_path(default_service, lp_ctx->sDefault, service), "%H", pszHomedir);
715 if (!(*(service->comment))) {
716 service->comment = talloc_asprintf(service, "Home directory of %s", user);
718 service->bAvailable = default_service->bAvailable;
719 service->browseable = default_service->browseable;
721 DEBUG(3, ("adding home's share [%s] for user '%s' at '%s'\n",
722 pszHomename, user, service->path));
724 return true;
728 * Add a new printer service, with defaults coming from service iFrom.
731 bool lpcfg_add_printer(struct loadparm_context *lp_ctx,
732 const char *pszPrintername,
733 struct loadparm_service *default_service)
735 const char *comment = "From Printcap";
736 struct loadparm_service *service;
737 service = lpcfg_add_service(lp_ctx, default_service, pszPrintername);
739 if (service == NULL)
740 return false;
742 /* note that we do NOT default the availability flag to True - */
743 /* we take it from the default service passed. This allows all */
744 /* dynamic printers to be disabled by disabling the [printers] */
745 /* entry (if/when the 'available' keyword is implemented!). */
747 /* the printer name is set to the service name. */
748 lpcfg_string_set(service, &service->_printername, pszPrintername);
749 lpcfg_string_set(service, &service->comment, comment);
750 service->browseable = default_service->browseable;
751 /* Printers cannot be read_only. */
752 service->read_only = false;
753 /* Printer services must be printable. */
754 service->printable = true;
756 DEBUG(3, ("adding printer service %s\n", pszPrintername));
758 return true;
762 * Map a parameter's string representation to something we can use.
763 * Returns False if the parameter string is not recognised, else TRUE.
766 int lpcfg_map_parameter(const char *pszParmName)
768 int iIndex;
770 for (iIndex = 0; parm_table[iIndex].label; iIndex++)
771 if (strwicmp(parm_table[iIndex].label, pszParmName) == 0)
772 return iIndex;
774 /* Warn only if it isn't parametric option */
775 if (strchr(pszParmName, ':') == NULL)
776 DEBUG(0, ("Unknown parameter encountered: \"%s\"\n", pszParmName));
777 /* We do return 'fail' for parametric options as well because they are
778 stored in different storage
780 return -1;
785 return the parameter structure for a parameter
787 struct parm_struct *lpcfg_parm_struct(struct loadparm_context *lp_ctx, const char *name)
789 int parmnum;
791 if (lp_ctx->s3_fns) {
792 return lp_ctx->s3_fns->get_parm_struct(name);
795 parmnum = lpcfg_map_parameter(name);
796 if (parmnum == -1) return NULL;
797 return &parm_table[parmnum];
801 return the parameter pointer for a parameter
803 void *lpcfg_parm_ptr(struct loadparm_context *lp_ctx,
804 struct loadparm_service *service, struct parm_struct *parm)
806 if (lp_ctx->s3_fns) {
807 return lp_ctx->s3_fns->get_parm_ptr(service, parm);
810 if (service == NULL) {
811 if (parm->p_class == P_LOCAL)
812 return ((char *)lp_ctx->sDefault)+parm->offset;
813 else if (parm->p_class == P_GLOBAL)
814 return ((char *)lp_ctx->globals)+parm->offset;
815 else return NULL;
816 } else {
817 return ((char *)service) + parm->offset;
822 return the parameter pointer for a parameter
824 bool lpcfg_parm_is_cmdline(struct loadparm_context *lp_ctx, const char *name)
826 int parmnum;
828 if (lp_ctx->s3_fns) {
829 struct parm_struct *parm = lp_ctx->s3_fns->get_parm_struct(name);
830 if (parm) {
831 return parm->flags & FLAG_CMDLINE;
833 return false;
836 parmnum = lpcfg_map_parameter(name);
837 if (parmnum == -1) return false;
839 return lp_ctx->flags[parmnum] & FLAG_CMDLINE;
843 * Find a service by name. Otherwise works like get_service.
846 static struct loadparm_service *getservicebyname(struct loadparm_context *lp_ctx,
847 const char *pszServiceName)
849 int iService;
851 if (lp_ctx->s3_fns) {
852 return lp_ctx->s3_fns->get_service(pszServiceName);
855 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--)
856 if (lp_ctx->services[iService] != NULL &&
857 strwicmp(lp_ctx->services[iService]->szService, pszServiceName) == 0) {
858 return lp_ctx->services[iService];
861 return NULL;
865 * Copy a service structure to another.
866 * If pcopymapDest is NULL then copy all fields
869 static void copy_service(struct loadparm_service *pserviceDest,
870 struct loadparm_service *pserviceSource,
871 struct bitmap *pcopymapDest)
873 int i;
874 bool bcopyall = (pcopymapDest == NULL);
875 struct parmlist_entry *data, *pdata, *paramo;
876 bool not_added;
878 for (i = 0; parm_table[i].label; i++)
879 if (parm_table[i].p_class == P_LOCAL &&
880 (bcopyall || bitmap_query(pcopymapDest, i))) {
881 void *src_ptr =
882 ((char *)pserviceSource) + parm_table[i].offset;
883 void *dest_ptr =
884 ((char *)pserviceDest) + parm_table[i].offset;
886 switch (parm_table[i].type) {
887 case P_BOOL:
888 case P_BOOLREV:
889 *(bool *)dest_ptr = *(bool *)src_ptr;
890 break;
892 case P_INTEGER:
893 case P_BYTES:
894 case P_OCTAL:
895 case P_ENUM:
896 *(int *)dest_ptr = *(int *)src_ptr;
897 break;
899 case P_CHAR:
900 *(char *)dest_ptr = *(char *)src_ptr;
901 break;
903 case P_STRING:
904 lpcfg_string_set(pserviceDest,
905 (char **)dest_ptr,
906 *(char **)src_ptr);
907 break;
909 case P_USTRING:
910 lpcfg_string_set_upper(pserviceDest,
911 (char **)dest_ptr,
912 *(char **)src_ptr);
913 break;
914 case P_LIST:
915 *(const char ***)dest_ptr = (const char **)str_list_copy(pserviceDest,
916 *(const char ***)src_ptr);
917 break;
918 default:
919 break;
923 if (bcopyall) {
924 init_copymap(pserviceDest);
925 if (pserviceSource->copymap)
926 bitmap_copy(pserviceDest->copymap,
927 pserviceSource->copymap);
930 data = pserviceSource->param_opt;
931 while (data) {
932 not_added = true;
933 pdata = pserviceDest->param_opt;
934 /* Traverse destination */
935 while (pdata) {
936 /* If we already have same option, override it */
937 if (strcmp(pdata->key, data->key) == 0) {
938 talloc_free(pdata->value);
939 pdata->value = talloc_strdup(pdata,
940 data->value);
941 not_added = false;
942 break;
944 pdata = pdata->next;
946 if (not_added) {
947 paramo = talloc_zero(pserviceDest, struct parmlist_entry);
948 if (paramo == NULL)
949 smb_panic("OOM");
950 paramo->key = talloc_strdup(paramo, data->key);
951 paramo->value = talloc_strdup(paramo, data->value);
952 DLIST_ADD(pserviceDest->param_opt, paramo);
954 data = data->next;
959 * Check a service for consistency. Return False if the service is in any way
960 * incomplete or faulty, else True.
962 static bool lpcfg_service_ok(struct loadparm_service *service)
964 bool bRetval;
966 bRetval = true;
967 if (service->szService[0] == '\0') {
968 DEBUG(0, ("The following message indicates an internal error:\n"));
969 DEBUG(0, ("No service name in service entry.\n"));
970 bRetval = false;
973 /* The [printers] entry MUST be printable. I'm all for flexibility, but */
974 /* I can't see why you'd want a non-printable printer service... */
975 if (strwicmp(service->szService, PRINTERS_NAME) == 0) {
976 if (!service->printable) {
977 DEBUG(0, ("WARNING: [%s] service MUST be printable!\n",
978 service->szService));
979 service->printable = true;
981 /* [printers] service must also be non-browsable. */
982 if (service->browseable)
983 service->browseable = false;
986 /* If a service is flagged unavailable, log the fact at level 0. */
987 if (!service->bAvailable)
988 DEBUG(1, ("NOTE: Service %s is flagged unavailable.\n",
989 service->szService));
991 return bRetval;
995 /*******************************************************************
996 Keep a linked list of all config files so we know when one has changed
997 it's date and needs to be reloaded.
998 ********************************************************************/
1000 static void add_to_file_list(struct loadparm_context *lp_ctx,
1001 const char *fname, const char *subfname)
1003 struct file_lists *f = lp_ctx->file_lists;
1005 while (f) {
1006 if (f->name && !strcmp(f->name, fname))
1007 break;
1008 f = f->next;
1011 if (!f) {
1012 f = talloc(lp_ctx, struct file_lists);
1013 if (!f)
1014 return;
1015 f->next = lp_ctx->file_lists;
1016 f->name = talloc_strdup(f, fname);
1017 if (!f->name) {
1018 talloc_free(f);
1019 return;
1021 f->subfname = talloc_strdup(f, subfname);
1022 if (!f->subfname) {
1023 talloc_free(f);
1024 return;
1026 lp_ctx->file_lists = f;
1027 f->modtime = file_modtime(subfname);
1028 } else {
1029 time_t t = file_modtime(subfname);
1030 if (t)
1031 f->modtime = t;
1035 /*******************************************************************
1036 Check if a config file has changed date.
1037 ********************************************************************/
1038 bool lpcfg_file_list_changed(struct loadparm_context *lp_ctx)
1040 struct file_lists *f;
1041 DEBUG(6, ("lpcfg_file_list_changed()\n"));
1043 for (f = lp_ctx->file_lists; f != NULL; f = f->next) {
1044 char *n2;
1045 time_t mod_time;
1047 n2 = standard_sub_basic(lp_ctx, f->name);
1049 DEBUGADD(6, ("file %s -> %s last mod_time: %s\n",
1050 f->name, n2, ctime(&f->modtime)));
1052 mod_time = file_modtime(n2);
1054 if (mod_time && ((f->modtime != mod_time) || (f->subfname == NULL) || (strcmp(n2, f->subfname) != 0))) {
1055 DEBUGADD(6, ("file %s modified: %s\n", n2,
1056 ctime(&mod_time)));
1057 f->modtime = mod_time;
1058 talloc_free(f->subfname);
1059 f->subfname = talloc_strdup(f, n2);
1060 return true;
1063 return false;
1066 /***************************************************************************
1067 Handle the "realm" parameter
1068 ***************************************************************************/
1070 static bool handle_realm(struct loadparm_context *lp_ctx, int unused,
1071 const char *pszParmValue, char **ptr)
1073 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1075 talloc_free(lp_ctx->globals->realm);
1076 talloc_free(lp_ctx->globals->dnsdomain);
1078 lp_ctx->globals->realm = strupper_talloc(lp_ctx, pszParmValue);
1079 lp_ctx->globals->dnsdomain = strlower_talloc(lp_ctx, pszParmValue);
1081 return true;
1084 /***************************************************************************
1085 Handle the include operation.
1086 ***************************************************************************/
1088 static bool handle_include(struct loadparm_context *lp_ctx, int unused,
1089 const char *pszParmValue, char **ptr)
1091 char *fname = standard_sub_basic(lp_ctx, pszParmValue);
1093 add_to_file_list(lp_ctx, pszParmValue, fname);
1095 lpcfg_string_set(lp_ctx, ptr, fname);
1097 if (file_exist(fname))
1098 return pm_process(fname, do_section, do_parameter, lp_ctx);
1100 DEBUG(2, ("Can't find include file %s\n", fname));
1102 return false;
1105 /***************************************************************************
1106 Handle the interpretation of the copy parameter.
1107 ***************************************************************************/
1109 static bool handle_copy(struct loadparm_context *lp_ctx, int unused,
1110 const char *pszParmValue, char **ptr)
1112 bool bRetval;
1113 struct loadparm_service *serviceTemp;
1115 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1117 bRetval = false;
1119 DEBUG(3, ("Copying service from service %s\n", pszParmValue));
1121 if ((serviceTemp = getservicebyname(lp_ctx, pszParmValue)) != NULL) {
1122 if (serviceTemp == lp_ctx->currentService) {
1123 DEBUG(0, ("Can't copy service %s - unable to copy self!\n", pszParmValue));
1124 } else {
1125 copy_service(lp_ctx->currentService,
1126 serviceTemp,
1127 lp_ctx->currentService->copymap);
1128 bRetval = true;
1130 } else {
1131 DEBUG(0, ("Unable to copy service - source not found: %s\n",
1132 pszParmValue));
1133 bRetval = false;
1136 return bRetval;
1139 static bool handle_debug_list(struct loadparm_context *lp_ctx, int unused,
1140 const char *pszParmValue, char **ptr)
1143 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1144 if (lp_ctx->global) {
1145 return debug_parse_levels(pszParmValue);
1147 return true;
1150 static bool handle_logfile(struct loadparm_context *lp_ctx, int unused,
1151 const char *pszParmValue, char **ptr)
1153 debug_set_logfile(pszParmValue);
1154 if (lp_ctx->global) {
1155 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1157 return true;
1160 /***************************************************************************
1161 Initialise a copymap.
1162 ***************************************************************************/
1164 static void init_copymap(struct loadparm_service *pservice)
1166 int i;
1168 TALLOC_FREE(pservice->copymap);
1170 pservice->copymap = bitmap_talloc(NULL, NUMPARAMETERS);
1171 if (!pservice->copymap)
1172 DEBUG(0,
1173 ("Couldn't allocate copymap!! (size %d)\n",
1174 (int)NUMPARAMETERS));
1175 else
1176 for (i = 0; i < NUMPARAMETERS; i++)
1177 bitmap_set(pservice->copymap, i);
1181 * Process a parametric option
1183 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
1184 struct loadparm_service *service,
1185 const char *pszParmName,
1186 const char *pszParmValue, int flags)
1188 struct parmlist_entry *paramo, *data;
1189 char *name;
1190 TALLOC_CTX *mem_ctx;
1192 while (isspace((unsigned char)*pszParmName)) {
1193 pszParmName++;
1196 name = strlower_talloc(lp_ctx, pszParmName);
1197 if (!name) return false;
1199 if (service == NULL) {
1200 data = lp_ctx->globals->param_opt;
1201 mem_ctx = lp_ctx->globals;
1202 } else {
1203 data = service->param_opt;
1204 mem_ctx = service;
1207 /* Traverse destination */
1208 for (paramo=data; paramo; paramo=paramo->next) {
1209 /* If we already have the option set, override it unless
1210 it was a command line option and the new one isn't */
1211 if (strcmp(paramo->key, name) == 0) {
1212 if ((paramo->priority & FLAG_CMDLINE) &&
1213 !(flags & FLAG_CMDLINE)) {
1214 talloc_free(name);
1215 return true;
1218 talloc_free(paramo->value);
1219 paramo->value = talloc_strdup(paramo, pszParmValue);
1220 paramo->priority = flags;
1221 talloc_free(name);
1222 return true;
1226 paramo = talloc_zero(mem_ctx, struct parmlist_entry);
1227 if (!paramo)
1228 smb_panic("OOM");
1229 paramo->key = talloc_strdup(paramo, name);
1230 paramo->value = talloc_strdup(paramo, pszParmValue);
1231 paramo->priority = flags;
1232 if (service == NULL) {
1233 DLIST_ADD(lp_ctx->globals->param_opt, paramo);
1234 } else {
1235 DLIST_ADD(service->param_opt, paramo);
1238 talloc_free(name);
1240 return true;
1243 static bool set_variable(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
1244 const char *pszParmName, const char *pszParmValue,
1245 struct loadparm_context *lp_ctx, bool on_globals)
1247 int i;
1248 /* if it is a special case then go ahead */
1249 if (parm_table[parmnum].special) {
1250 bool ret;
1251 ret = parm_table[parmnum].special(lp_ctx, -1, pszParmValue,
1252 (char **)parm_ptr);
1253 if (!ret) {
1254 return false;
1256 goto mark_non_default;
1259 /* now switch on the type of variable it is */
1260 switch (parm_table[parmnum].type)
1262 case P_BOOL: {
1263 bool b;
1264 if (!set_boolean(pszParmValue, &b)) {
1265 DEBUG(0, ("set_variable(%s): value is not "
1266 "boolean!\n", pszParmValue));
1267 return false;
1269 *(bool *)parm_ptr = b;
1271 break;
1273 case P_BOOLREV: {
1274 bool b;
1275 if (!set_boolean(pszParmValue, &b)) {
1276 DEBUG(0, ("set_variable(%s): value is not "
1277 "boolean!\n", pszParmValue));
1278 return false;
1280 *(bool *)parm_ptr = !b;
1282 break;
1284 case P_INTEGER:
1285 *(int *)parm_ptr = atoi(pszParmValue);
1286 break;
1288 case P_CHAR:
1289 *(char *)parm_ptr = *pszParmValue;
1290 break;
1292 case P_OCTAL:
1293 *(int *)parm_ptr = strtol(pszParmValue, NULL, 8);
1294 break;
1296 case P_BYTES:
1298 uint64_t val;
1299 if (conv_str_size_error(pszParmValue, &val)) {
1300 if (val <= INT_MAX) {
1301 *(int *)parm_ptr = (int)val;
1302 break;
1306 DEBUG(0, ("set_variable(%s): value is not "
1307 "a valid size specifier!\n", pszParmValue));
1308 return false;
1311 case P_CMDLIST:
1312 *(const char ***)parm_ptr = (const char **)str_list_make(mem_ctx,
1313 pszParmValue, NULL);
1314 break;
1315 case P_LIST:
1317 char **new_list = str_list_make(mem_ctx,
1318 pszParmValue, NULL);
1319 for (i=0; new_list[i]; i++) {
1320 if (*(const char ***)parm_ptr != NULL &&
1321 new_list[i][0] == '+' &&
1322 new_list[i][1])
1324 if (!str_list_check(*(const char ***)parm_ptr,
1325 &new_list[i][1])) {
1326 *(const char ***)parm_ptr = str_list_add(*(const char ***)parm_ptr,
1327 &new_list[i][1]);
1329 } else if (*(const char ***)parm_ptr != NULL &&
1330 new_list[i][0] == '-' &&
1331 new_list[i][1])
1333 str_list_remove(*(const char ***)parm_ptr,
1334 &new_list[i][1]);
1335 } else {
1336 if (i != 0) {
1337 DEBUG(0, ("Unsupported list syntax for: %s = %s\n",
1338 pszParmName, pszParmValue));
1339 return false;
1341 *(const char ***)parm_ptr = (const char **) new_list;
1342 break;
1345 break;
1347 case P_STRING:
1348 lpcfg_string_set(mem_ctx, (char **)parm_ptr, pszParmValue);
1349 break;
1351 case P_USTRING:
1352 lpcfg_string_set_upper(mem_ctx, (char **)parm_ptr, pszParmValue);
1353 break;
1355 case P_ENUM:
1356 for (i = 0; parm_table[parmnum].enum_list[i].name; i++) {
1357 if (strequal
1358 (pszParmValue,
1359 parm_table[parmnum].enum_list[i].name)) {
1360 *(int *)parm_ptr =
1361 parm_table[parmnum].
1362 enum_list[i].value;
1363 break;
1366 if (!parm_table[parmnum].enum_list[i].name) {
1367 DEBUG(0,("Unknown enumerated value '%s' for '%s'\n",
1368 pszParmValue, pszParmName));
1369 return false;
1371 break;
1373 case P_SEP:
1374 break;
1377 mark_non_default:
1378 if (on_globals && (lp_ctx->flags[parmnum] & FLAG_DEFAULT)) {
1379 lp_ctx->flags[parmnum] &= ~FLAG_DEFAULT;
1380 /* we have to also unset FLAG_DEFAULT on aliases */
1381 for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
1382 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1384 for (i=parmnum+1;i<NUMPARAMETERS && parm_table[i].offset == parm_table[parmnum].offset;i++) {
1385 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1388 return true;
1392 bool lpcfg_do_global_parameter(struct loadparm_context *lp_ctx,
1393 const char *pszParmName, const char *pszParmValue)
1395 int parmnum = lpcfg_map_parameter(pszParmName);
1396 void *parm_ptr;
1398 if (parmnum < 0) {
1399 if (strchr(pszParmName, ':')) {
1400 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName, pszParmValue, 0);
1402 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1403 return true;
1406 /* if the flag has been set on the command line, then don't allow override,
1407 but don't report an error */
1408 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1409 return true;
1412 parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[parmnum]);
1414 return set_variable(lp_ctx->globals, parmnum, parm_ptr,
1415 pszParmName, pszParmValue, lp_ctx, true);
1418 bool lpcfg_do_service_parameter(struct loadparm_context *lp_ctx,
1419 struct loadparm_service *service,
1420 const char *pszParmName, const char *pszParmValue)
1422 void *parm_ptr;
1423 int i;
1424 int parmnum = lpcfg_map_parameter(pszParmName);
1426 if (parmnum < 0) {
1427 if (strchr(pszParmName, ':')) {
1428 return lp_do_parameter_parametric(lp_ctx, service, pszParmName, pszParmValue, 0);
1430 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1431 return true;
1434 /* if the flag has been set on the command line, then don't allow override,
1435 but don't report an error */
1436 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1437 return true;
1440 if (parm_table[parmnum].p_class == P_GLOBAL) {
1441 DEBUG(0,
1442 ("Global parameter %s found in service section!\n",
1443 pszParmName));
1444 return true;
1446 parm_ptr = ((char *)service) + parm_table[parmnum].offset;
1448 if (!service->copymap)
1449 init_copymap(service);
1451 /* this handles the aliases - set the copymap for other
1452 * entries with the same data pointer */
1453 for (i = 0; parm_table[i].label; i++)
1454 if (parm_table[i].offset == parm_table[parmnum].offset &&
1455 parm_table[i].p_class == parm_table[parmnum].p_class)
1456 bitmap_clear(service->copymap, i);
1458 return set_variable(service, parmnum, parm_ptr, pszParmName,
1459 pszParmValue, lp_ctx, false);
1463 * Process a parameter.
1466 static bool do_parameter(const char *pszParmName, const char *pszParmValue,
1467 void *userdata)
1469 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1471 if (lp_ctx->bInGlobalSection)
1472 return lpcfg_do_global_parameter(lp_ctx, pszParmName,
1473 pszParmValue);
1474 else
1475 return lpcfg_do_service_parameter(lp_ctx, lp_ctx->currentService,
1476 pszParmName, pszParmValue);
1480 variable argument do parameter
1482 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx, const char *pszParmName, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
1483 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx,
1484 const char *pszParmName, const char *fmt, ...)
1486 char *s;
1487 bool ret;
1488 va_list ap;
1490 va_start(ap, fmt);
1491 s = talloc_vasprintf(NULL, fmt, ap);
1492 va_end(ap);
1493 ret = lpcfg_do_global_parameter(lp_ctx, pszParmName, s);
1494 talloc_free(s);
1495 return ret;
1500 set a parameter from the commandline - this is called from command line parameter
1501 parsing code. It sets the parameter then marks the parameter as unable to be modified
1502 by smb.conf processing
1504 bool lpcfg_set_cmdline(struct loadparm_context *lp_ctx, const char *pszParmName,
1505 const char *pszParmValue)
1507 int parmnum;
1508 int i;
1510 if (lp_ctx->s3_fns) {
1511 return lp_ctx->s3_fns->set_cmdline(pszParmName, pszParmValue);
1514 parmnum = lpcfg_map_parameter(pszParmName);
1516 while (isspace((unsigned char)*pszParmValue)) pszParmValue++;
1519 if (parmnum < 0 && strchr(pszParmName, ':')) {
1520 /* set a parametric option */
1521 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName,
1522 pszParmValue, FLAG_CMDLINE);
1525 if (parmnum < 0) {
1526 DEBUG(0,("Unknown option '%s'\n", pszParmName));
1527 return false;
1530 /* reset the CMDLINE flag in case this has been called before */
1531 lp_ctx->flags[parmnum] &= ~FLAG_CMDLINE;
1533 if (!lpcfg_do_global_parameter(lp_ctx, pszParmName, pszParmValue)) {
1534 return false;
1537 lp_ctx->flags[parmnum] |= FLAG_CMDLINE;
1539 /* we have to also set FLAG_CMDLINE on aliases */
1540 for (i=parmnum-1;
1541 i>=0 && parm_table[i].p_class == parm_table[parmnum].p_class &&
1542 parm_table[i].offset == parm_table[parmnum].offset;
1543 i--) {
1544 lp_ctx->flags[i] |= FLAG_CMDLINE;
1546 for (i=parmnum+1;
1547 i<NUMPARAMETERS &&
1548 parm_table[i].p_class == parm_table[parmnum].p_class &&
1549 parm_table[i].offset == parm_table[parmnum].offset;
1550 i++) {
1551 lp_ctx->flags[i] |= FLAG_CMDLINE;
1554 return true;
1558 set a option from the commandline in 'a=b' format. Use to support --option
1560 bool lpcfg_set_option(struct loadparm_context *lp_ctx, const char *option)
1562 char *p, *s;
1563 bool ret;
1565 s = talloc_strdup(NULL, option);
1566 if (!s) {
1567 return false;
1570 p = strchr(s, '=');
1571 if (!p) {
1572 talloc_free(s);
1573 return false;
1576 *p = 0;
1578 ret = lpcfg_set_cmdline(lp_ctx, s, p+1);
1579 talloc_free(s);
1580 return ret;
1584 #define BOOLSTR(b) ((b) ? "Yes" : "No")
1587 * Print a parameter of the specified type.
1590 void lpcfg_print_parameter(struct parm_struct *p, void *ptr, FILE * f)
1592 /* For the seperation of lists values that we print below */
1593 const char *list_sep = ", ";
1594 int i;
1595 switch (p->type)
1597 case P_ENUM:
1598 for (i = 0; p->enum_list[i].name; i++) {
1599 if (*(int *)ptr == p->enum_list[i].value) {
1600 fprintf(f, "%s",
1601 p->enum_list[i].name);
1602 break;
1605 break;
1607 case P_BOOL:
1608 fprintf(f, "%s", BOOLSTR(*(bool *)ptr));
1609 break;
1611 case P_BOOLREV:
1612 fprintf(f, "%s", BOOLSTR(!*(bool *)ptr));
1613 break;
1615 case P_INTEGER:
1616 case P_BYTES:
1617 fprintf(f, "%d", *(int *)ptr);
1618 break;
1620 case P_CHAR:
1621 fprintf(f, "%c", *(char *)ptr);
1622 break;
1624 case P_OCTAL: {
1625 int val = *(int *)ptr;
1626 if (val == -1) {
1627 fprintf(f, "-1");
1628 } else {
1629 fprintf(f, "0%03o", val);
1631 break;
1634 case P_CMDLIST:
1635 list_sep = " ";
1636 /* fall through */
1637 case P_LIST:
1638 if ((char ***)ptr && *(char ***)ptr) {
1639 char **list = *(char ***)ptr;
1640 for (; *list; list++) {
1641 /* surround strings with whitespace in double quotes */
1642 if (*(list+1) == NULL) {
1643 /* last item, no extra separator */
1644 list_sep = "";
1646 if ( strchr_m( *list, ' ' ) ) {
1647 fprintf(f, "\"%s\"%s", *list, list_sep);
1648 } else {
1649 fprintf(f, "%s%s", *list, list_sep);
1653 break;
1655 case P_STRING:
1656 case P_USTRING:
1657 if (*(char **)ptr) {
1658 fprintf(f, "%s", *(char **)ptr);
1660 break;
1661 case P_SEP:
1662 break;
1667 * Check if two parameters are equal.
1670 bool lpcfg_equal_parameter(parm_type type, void *ptr1, void *ptr2)
1672 switch (type) {
1673 case P_BOOL:
1674 case P_BOOLREV:
1675 return (*((bool *)ptr1) == *((bool *)ptr2));
1677 case P_INTEGER:
1678 case P_ENUM:
1679 case P_OCTAL:
1680 case P_BYTES:
1681 return (*((int *)ptr1) == *((int *)ptr2));
1683 case P_CHAR:
1684 return (*((char *)ptr1) == *((char *)ptr2));
1686 case P_LIST:
1687 case P_CMDLIST:
1688 return str_list_equal(*(const char ***)ptr1, *(const char ***)ptr2);
1690 case P_STRING:
1691 case P_USTRING:
1693 char *p1 = *(char **)ptr1, *p2 = *(char **)ptr2;
1694 if (p1 && !*p1)
1695 p1 = NULL;
1696 if (p2 && !*p2)
1697 p2 = NULL;
1698 return (p1 == p2 || strequal(p1, p2));
1700 case P_SEP:
1701 break;
1703 return false;
1707 * Process a new section (service).
1709 * At this stage all sections are services.
1710 * Later we'll have special sections that permit server parameters to be set.
1711 * Returns True on success, False on failure.
1714 static bool do_section(const char *pszSectionName, void *userdata)
1716 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1717 bool bRetval;
1718 bool isglobal = ((strwicmp(pszSectionName, GLOBAL_NAME) == 0) ||
1719 (strwicmp(pszSectionName, GLOBAL_NAME2) == 0));
1720 bRetval = false;
1722 /* if we've just struck a global section, note the fact. */
1723 lp_ctx->bInGlobalSection = isglobal;
1725 /* check for multiple global sections */
1726 if (lp_ctx->bInGlobalSection) {
1727 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1728 return true;
1731 /* if we have a current service, tidy it up before moving on */
1732 bRetval = true;
1734 if (lp_ctx->currentService != NULL)
1735 bRetval = lpcfg_service_ok(lp_ctx->currentService);
1737 /* if all is still well, move to the next record in the services array */
1738 if (bRetval) {
1739 /* We put this here to avoid an odd message order if messages are */
1740 /* issued by the post-processing of a previous section. */
1741 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1743 if ((lp_ctx->currentService = lpcfg_add_service(lp_ctx, lp_ctx->sDefault,
1744 pszSectionName))
1745 == NULL) {
1746 DEBUG(0, ("Failed to add a new service\n"));
1747 return false;
1751 return bRetval;
1756 * Determine if a particular base parameter is currently set to the default value.
1759 static bool is_default(struct loadparm_service *sDefault, int i)
1761 void *def_ptr = ((char *)sDefault) + parm_table[i].offset;
1762 switch (parm_table[i].type) {
1763 case P_CMDLIST:
1764 case P_LIST:
1765 return str_list_equal((const char **)parm_table[i].def.lvalue,
1766 (const char **)def_ptr);
1767 case P_STRING:
1768 case P_USTRING:
1769 return strequal(parm_table[i].def.svalue,
1770 *(char **)def_ptr);
1771 case P_BOOL:
1772 case P_BOOLREV:
1773 return parm_table[i].def.bvalue ==
1774 *(bool *)def_ptr;
1775 case P_INTEGER:
1776 case P_CHAR:
1777 case P_OCTAL:
1778 case P_BYTES:
1779 case P_ENUM:
1780 return parm_table[i].def.ivalue ==
1781 *(int *)def_ptr;
1782 case P_SEP:
1783 break;
1785 return false;
1789 *Display the contents of the global structure.
1792 static void dump_globals(struct loadparm_context *lp_ctx, FILE *f,
1793 bool show_defaults)
1795 int i;
1796 struct parmlist_entry *data;
1798 fprintf(f, "# Global parameters\n[global]\n");
1800 for (i = 0; parm_table[i].label; i++)
1801 if (parm_table[i].p_class == P_GLOBAL &&
1802 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset))) {
1803 if (!show_defaults && (lp_ctx->flags[i] & FLAG_DEFAULT))
1804 continue;
1805 fprintf(f, "\t%s = ", parm_table[i].label);
1806 lpcfg_print_parameter(&parm_table[i], lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[i]), f);
1807 fprintf(f, "\n");
1809 if (lp_ctx->globals->param_opt != NULL) {
1810 for (data = lp_ctx->globals->param_opt; data;
1811 data = data->next) {
1812 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
1813 continue;
1815 fprintf(f, "\t%s = %s\n", data->key, data->value);
1822 * Display the contents of a single services record.
1825 static void dump_a_service(struct loadparm_service * pService, struct loadparm_service *sDefault, FILE * f,
1826 unsigned int *flags)
1828 int i;
1829 struct parmlist_entry *data;
1831 if (pService != sDefault)
1832 fprintf(f, "\n[%s]\n", pService->szService);
1834 for (i = 0; parm_table[i].label; i++) {
1835 if (parm_table[i].p_class == P_LOCAL &&
1836 (*parm_table[i].label != '-') &&
1837 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset)))
1839 if (pService == sDefault) {
1840 if (flags && (flags[i] & FLAG_DEFAULT)) {
1841 continue;
1843 if (defaults_saved) {
1844 if (is_default(sDefault, i)) {
1845 continue;
1848 } else {
1849 if (lpcfg_equal_parameter(parm_table[i].type,
1850 ((char *)pService) +
1851 parm_table[i].offset,
1852 ((char *)sDefault) +
1853 parm_table[i].offset))
1854 continue;
1857 fprintf(f, "\t%s = ", parm_table[i].label);
1858 lpcfg_print_parameter(&parm_table[i],
1859 ((char *)pService) + parm_table[i].offset, f);
1860 fprintf(f, "\n");
1863 if (pService->param_opt != NULL) {
1864 for (data = pService->param_opt; data; data = data->next) {
1865 fprintf(f, "\t%s = %s\n", data->key, data->value);
1870 bool lpcfg_dump_a_parameter(struct loadparm_context *lp_ctx,
1871 struct loadparm_service *service,
1872 const char *parm_name, FILE * f)
1874 struct parm_struct *parm;
1875 void *ptr;
1877 parm = lpcfg_parm_struct(lp_ctx, parm_name);
1878 if (!parm) {
1879 return false;
1882 if (service != NULL && parm->p_class == P_GLOBAL) {
1883 return false;
1886 ptr = lpcfg_parm_ptr(lp_ctx, service,parm);
1888 lpcfg_print_parameter(parm, ptr, f);
1889 fprintf(f, "\n");
1890 return true;
1894 * Auto-load some home services.
1896 static void lpcfg_add_auto_services(struct loadparm_context *lp_ctx,
1897 const char *str)
1899 return;
1904 * Unload unused services.
1907 void lpcfg_killunused(struct loadparm_context *lp_ctx,
1908 struct smbsrv_connection *smb,
1909 bool (*snumused) (struct smbsrv_connection *, int))
1911 int i;
1912 for (i = 0; i < lp_ctx->iNumServices; i++) {
1913 if (lp_ctx->services[i] == NULL)
1914 continue;
1916 if (!snumused || !snumused(smb, i)) {
1917 talloc_free(lp_ctx->services[i]);
1918 lp_ctx->services[i] = NULL;
1924 static int lpcfg_destructor(struct loadparm_context *lp_ctx)
1926 struct parmlist_entry *data;
1928 if (lp_ctx->refuse_free) {
1929 /* someone is trying to free the
1930 global_loadparm_context.
1931 We can't allow that. */
1932 return -1;
1935 if (lp_ctx->globals->param_opt != NULL) {
1936 struct parmlist_entry *next;
1937 for (data = lp_ctx->globals->param_opt; data; data=next) {
1938 next = data->next;
1939 if (data->priority & FLAG_CMDLINE) continue;
1940 DLIST_REMOVE(lp_ctx->globals->param_opt, data);
1941 talloc_free(data);
1945 return 0;
1949 * Initialise the global parameter structure.
1951 * Note that most callers should use loadparm_init_global() instead
1953 struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
1955 int i;
1956 char *myname;
1957 struct loadparm_context *lp_ctx;
1958 struct parmlist_entry *parm;
1959 char *logfile;
1961 lp_ctx = talloc_zero(mem_ctx, struct loadparm_context);
1962 if (lp_ctx == NULL)
1963 return NULL;
1965 talloc_set_destructor(lp_ctx, lpcfg_destructor);
1966 lp_ctx->bInGlobalSection = true;
1967 lp_ctx->globals = talloc_zero(lp_ctx, struct loadparm_global);
1968 lp_ctx->sDefault = talloc_zero(lp_ctx, struct loadparm_service);
1970 lp_ctx->sDefault->iMaxPrintJobs = 1000;
1971 lp_ctx->sDefault->bAvailable = true;
1972 lp_ctx->sDefault->browseable = true;
1973 lp_ctx->sDefault->read_only = true;
1974 lp_ctx->sDefault->map_archive = true;
1975 lp_ctx->sDefault->strict_locking = true;
1976 lp_ctx->sDefault->oplocks = true;
1977 lp_ctx->sDefault->create_mask = 0744;
1978 lp_ctx->sDefault->force_create_mode = 0000;
1979 lp_ctx->sDefault->directory_mask = 0755;
1980 lp_ctx->sDefault->force_directory_mode = 0000;
1982 DEBUG(3, ("Initialising global parameters\n"));
1984 for (i = 0; parm_table[i].label; i++) {
1985 if ((parm_table[i].type == P_STRING ||
1986 parm_table[i].type == P_USTRING) &&
1987 !(lp_ctx->flags[i] & FLAG_CMDLINE)) {
1988 char **r;
1989 if (parm_table[i].p_class == P_LOCAL) {
1990 r = (char **)(((char *)lp_ctx->sDefault) + parm_table[i].offset);
1991 } else {
1992 r = (char **)(((char *)lp_ctx->globals) + parm_table[i].offset);
1994 *r = talloc_strdup(lp_ctx, "");
1998 logfile = talloc_asprintf(lp_ctx, "%s/log.samba", dyn_LOGFILEBASE);
1999 lpcfg_do_global_parameter(lp_ctx, "log file", logfile);
2000 talloc_free(logfile);
2002 lpcfg_do_global_parameter(lp_ctx, "log level", "0");
2004 lpcfg_do_global_parameter(lp_ctx, "syslog", "1");
2005 lpcfg_do_global_parameter(lp_ctx, "syslog only", "No");
2006 lpcfg_do_global_parameter(lp_ctx, "debug timestamp", "Yes");
2007 lpcfg_do_global_parameter(lp_ctx, "debug prefix timestamp", "No");
2008 lpcfg_do_global_parameter(lp_ctx, "debug hires timestamp", "Yes");
2009 lpcfg_do_global_parameter(lp_ctx, "debug pid", "No");
2010 lpcfg_do_global_parameter(lp_ctx, "debug uid", "No");
2011 lpcfg_do_global_parameter(lp_ctx, "debug class", "No");
2013 lpcfg_do_global_parameter(lp_ctx, "share backend", "classic");
2015 lpcfg_do_global_parameter(lp_ctx, "server role", "auto");
2016 lpcfg_do_global_parameter(lp_ctx, "domain logons", "No");
2017 lpcfg_do_global_parameter(lp_ctx, "domain master", "Auto");
2019 /* options that can be set on the command line must be initialised via
2020 the slower lpcfg_do_global_parameter() to ensure that FLAG_CMDLINE is obeyed */
2021 #ifdef TCP_NODELAY
2022 lpcfg_do_global_parameter(lp_ctx, "socket options", "TCP_NODELAY");
2023 #endif
2024 lpcfg_do_global_parameter(lp_ctx, "workgroup", DEFAULT_WORKGROUP);
2025 myname = get_myname(lp_ctx);
2026 lpcfg_do_global_parameter(lp_ctx, "netbios name", myname);
2027 talloc_free(myname);
2028 lpcfg_do_global_parameter(lp_ctx, "name resolve order", "lmhosts wins host bcast");
2030 lpcfg_do_global_parameter(lp_ctx, "fstype", "NTFS");
2032 lpcfg_do_global_parameter(lp_ctx, "ntvfs handler", "unixuid default");
2033 lpcfg_do_global_parameter(lp_ctx, "max connections", "0");
2035 lpcfg_do_global_parameter(lp_ctx, "dcerpc endpoint servers", "epmapper wkssvc rpcecho samr netlogon lsarpc spoolss drsuapi dssetup unixinfo browser eventlog6 backupkey dnsserver");
2036 lpcfg_do_global_parameter(lp_ctx, "server services", "s3fs rpc nbt wrepl ldap cldap kdc drepl winbind ntp_signd kcc dnsupdate dns");
2037 lpcfg_do_global_parameter(lp_ctx, "kccsrv:samba_kcc", "true");
2038 /* the winbind method for domain controllers is for both RODC
2039 auth forwarding and for trusted domains */
2040 lpcfg_do_global_parameter(lp_ctx, "private dir", dyn_PRIVATE_DIR);
2041 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_LOCAL_MACHINE", "hklm.ldb");
2043 /* This hive should be dynamically generated by Samba using
2044 data from the sam, but for the moment leave it in a tdb to
2045 keep regedt32 from popping up an annoying dialog. */
2046 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_USERS", "hku.ldb");
2048 /* using UTF8 by default allows us to support all chars */
2049 lpcfg_do_global_parameter(lp_ctx, "unix charset", "UTF-8");
2051 /* Use codepage 850 as a default for the dos character set */
2052 lpcfg_do_global_parameter(lp_ctx, "dos charset", "CP850");
2055 * Allow the default PASSWD_CHAT to be overridden in local.h.
2057 lpcfg_do_global_parameter(lp_ctx, "passwd chat", DEFAULT_PASSWD_CHAT);
2059 lpcfg_do_global_parameter(lp_ctx, "pid directory", dyn_PIDDIR);
2060 lpcfg_do_global_parameter(lp_ctx, "lock dir", dyn_LOCKDIR);
2061 lpcfg_do_global_parameter(lp_ctx, "state directory", dyn_STATEDIR);
2062 lpcfg_do_global_parameter(lp_ctx, "cache directory", dyn_CACHEDIR);
2063 lpcfg_do_global_parameter(lp_ctx, "ncalrpc dir", dyn_NCALRPCDIR);
2065 lpcfg_do_global_parameter(lp_ctx, "nbt client socket address", "0.0.0.0");
2066 lpcfg_do_global_parameter_var(lp_ctx, "server string",
2067 "Samba %s", SAMBA_VERSION_STRING);
2069 lpcfg_do_global_parameter(lp_ctx, "password server", "*");
2071 lpcfg_do_global_parameter(lp_ctx, "max mux", "50");
2072 lpcfg_do_global_parameter(lp_ctx, "max xmit", "16644");
2073 lpcfg_do_global_parameter(lp_ctx, "host msdfs", "true");
2075 lpcfg_do_global_parameter(lp_ctx, "LargeReadwrite", "True");
2076 lpcfg_do_global_parameter(lp_ctx, "server min protocol", "LANMAN1");
2077 lpcfg_do_global_parameter(lp_ctx, "server max protocol", "SMB3");
2078 lpcfg_do_global_parameter(lp_ctx, "client min protocol", "CORE");
2079 lpcfg_do_global_parameter(lp_ctx, "client max protocol", "NT1");
2080 lpcfg_do_global_parameter(lp_ctx, "security", "AUTO");
2081 lpcfg_do_global_parameter(lp_ctx, "EncryptPasswords", "True");
2082 lpcfg_do_global_parameter(lp_ctx, "ReadRaw", "True");
2083 lpcfg_do_global_parameter(lp_ctx, "WriteRaw", "True");
2084 lpcfg_do_global_parameter(lp_ctx, "NullPasswords", "False");
2085 lpcfg_do_global_parameter(lp_ctx, "old password allowed period", "60");
2086 lpcfg_do_global_parameter(lp_ctx, "ObeyPamRestrictions", "False");
2088 lpcfg_do_global_parameter(lp_ctx, "TimeServer", "False");
2089 lpcfg_do_global_parameter(lp_ctx, "BindInterfacesOnly", "False");
2090 lpcfg_do_global_parameter(lp_ctx, "Unicode", "True");
2091 lpcfg_do_global_parameter(lp_ctx, "ClientLanManAuth", "False");
2092 lpcfg_do_global_parameter(lp_ctx, "ClientNTLMv2Auth", "True");
2093 lpcfg_do_global_parameter(lp_ctx, "LanmanAuth", "False");
2094 lpcfg_do_global_parameter(lp_ctx, "NTLMAuth", "True");
2095 lpcfg_do_global_parameter(lp_ctx, "client use spnego principal", "False");
2097 lpcfg_do_global_parameter(lp_ctx, "UnixExtensions", "True");
2099 lpcfg_do_global_parameter(lp_ctx, "PreferredMaster", "Auto");
2100 lpcfg_do_global_parameter(lp_ctx, "LocalMaster", "True");
2102 lpcfg_do_global_parameter(lp_ctx, "wins support", "False");
2103 lpcfg_do_global_parameter(lp_ctx, "dns proxy", "True");
2105 lpcfg_do_global_parameter(lp_ctx, "winbind separator", "\\");
2106 lpcfg_do_global_parameter(lp_ctx, "winbind sealed pipes", "True");
2107 lpcfg_do_global_parameter(lp_ctx, "require strong key", "True");
2108 lpcfg_do_global_parameter(lp_ctx, "winbindd socket directory", dyn_WINBINDD_SOCKET_DIR);
2109 lpcfg_do_global_parameter(lp_ctx, "winbindd privileged socket directory", dyn_WINBINDD_PRIVILEGED_SOCKET_DIR);
2110 lpcfg_do_global_parameter(lp_ctx, "ntp signd socket directory", dyn_NTP_SIGND_SOCKET_DIR);
2111 lpcfg_do_global_parameter_var(lp_ctx, "dns update command", "%s/samba_dnsupdate", dyn_SCRIPTSBINDIR);
2112 lpcfg_do_global_parameter_var(lp_ctx, "spn update command", "%s/samba_spnupdate", dyn_SCRIPTSBINDIR);
2113 lpcfg_do_global_parameter_var(lp_ctx, "samba kcc command",
2114 "%s/samba_kcc", dyn_SCRIPTSBINDIR);
2115 lpcfg_do_global_parameter(lp_ctx, "template shell", "/bin/false");
2116 lpcfg_do_global_parameter(lp_ctx, "template homedir", "/home/%WORKGROUP%/%ACCOUNTNAME%");
2118 lpcfg_do_global_parameter(lp_ctx, "client signing", "default");
2119 lpcfg_do_global_parameter(lp_ctx, "server signing", "default");
2121 lpcfg_do_global_parameter(lp_ctx, "use spnego", "True");
2123 lpcfg_do_global_parameter(lp_ctx, "use mmap", "True");
2125 lpcfg_do_global_parameter(lp_ctx, "smb ports", "445 139");
2126 lpcfg_do_global_parameter(lp_ctx, "nbt port", "137");
2127 lpcfg_do_global_parameter(lp_ctx, "dgram port", "138");
2128 lpcfg_do_global_parameter(lp_ctx, "cldap port", "389");
2129 lpcfg_do_global_parameter(lp_ctx, "krb5 port", "88");
2130 lpcfg_do_global_parameter(lp_ctx, "kpasswd port", "464");
2131 lpcfg_do_global_parameter(lp_ctx, "web port", "901");
2133 lpcfg_do_global_parameter(lp_ctx, "nt status support", "True");
2135 lpcfg_do_global_parameter(lp_ctx, "max wins ttl", "518400"); /* 6 days */
2136 lpcfg_do_global_parameter(lp_ctx, "min wins ttl", "21600");
2138 lpcfg_do_global_parameter(lp_ctx, "tls enabled", "True");
2139 lpcfg_do_global_parameter(lp_ctx, "tls keyfile", "tls/key.pem");
2140 lpcfg_do_global_parameter(lp_ctx, "tls certfile", "tls/cert.pem");
2141 lpcfg_do_global_parameter(lp_ctx, "tls cafile", "tls/ca.pem");
2142 lpcfg_do_global_parameter(lp_ctx, "prefork children:smb", "4");
2144 lpcfg_do_global_parameter(lp_ctx, "rndc command", "/usr/sbin/rndc");
2145 lpcfg_do_global_parameter(lp_ctx, "nsupdate command", "/usr/bin/nsupdate -g");
2147 lpcfg_do_global_parameter(lp_ctx, "allow dns updates", "secure only");
2148 lpcfg_do_global_parameter(lp_ctx, "dns forwarder", "");
2150 lpcfg_do_global_parameter(lp_ctx, "algorithmic rid base", "1000");
2152 lpcfg_do_global_parameter(lp_ctx, "enhanced browsing", "True");
2154 lpcfg_do_global_parameter(lp_ctx, "winbind nss info", "template");
2156 lpcfg_do_global_parameter(lp_ctx, "server schannel", "Auto");
2158 lpcfg_do_global_parameter(lp_ctx, "short preserve case", "True");
2160 lpcfg_do_global_parameter(lp_ctx, "max open files", "16384");
2162 lpcfg_do_global_parameter(lp_ctx, "cups connection timeout", "30");
2164 lpcfg_do_global_parameter(lp_ctx, "locking", "True");
2166 lpcfg_do_global_parameter(lp_ctx, "block size", "1024");
2168 lpcfg_do_global_parameter(lp_ctx, "client use spnego", "True");
2170 lpcfg_do_global_parameter(lp_ctx, "change notify", "True");
2172 lpcfg_do_global_parameter(lp_ctx, "name cache timeout", "660");
2174 lpcfg_do_global_parameter(lp_ctx, "defer sharing violations", "True");
2176 lpcfg_do_global_parameter(lp_ctx, "ldap replication sleep", "1000");
2178 lpcfg_do_global_parameter(lp_ctx, "idmap backend", "tdb");
2180 lpcfg_do_global_parameter(lp_ctx, "enable privileges", "True");
2182 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max write", "%u", DEFAULT_SMB2_MAX_WRITE);
2184 lpcfg_do_global_parameter(lp_ctx, "passdb backend", "tdbsam");
2186 lpcfg_do_global_parameter(lp_ctx, "getwd cache", "True");
2188 lpcfg_do_global_parameter(lp_ctx, "winbind nested groups", "True");
2190 lpcfg_do_global_parameter(lp_ctx, "mangled names", "True");
2192 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max credits", "%u", DEFAULT_SMB2_MAX_CREDITS);
2194 lpcfg_do_global_parameter(lp_ctx, "ldap ssl", "start tls");
2196 lpcfg_do_global_parameter(lp_ctx, "ldap deref", "auto");
2198 lpcfg_do_global_parameter(lp_ctx, "lm interval", "60");
2200 lpcfg_do_global_parameter(lp_ctx, "mangling method", "hash2");
2202 lpcfg_do_global_parameter(lp_ctx, "hide dot files", "True");
2204 lpcfg_do_global_parameter(lp_ctx, "browse list", "True");
2206 lpcfg_do_global_parameter(lp_ctx, "passwd chat timeout", "2");
2208 lpcfg_do_global_parameter(lp_ctx, "guest account", GUEST_ACCOUNT);
2210 lpcfg_do_global_parameter(lp_ctx, "client schannel", "auto");
2212 lpcfg_do_global_parameter(lp_ctx, "smb encrypt", "default");
2214 lpcfg_do_global_parameter(lp_ctx, "max log size", "5000");
2216 lpcfg_do_global_parameter(lp_ctx, "idmap negative cache time", "120");
2218 lpcfg_do_global_parameter(lp_ctx, "ldap follow referral", "auto");
2220 lpcfg_do_global_parameter(lp_ctx, "multicast dns register", "yes");
2222 lpcfg_do_global_parameter(lp_ctx, "winbind reconnect delay", "30");
2224 lpcfg_do_global_parameter(lp_ctx, "nt acl support", "yes");
2226 lpcfg_do_global_parameter(lp_ctx, "acl check permissions", "yes");
2228 lpcfg_do_global_parameter(lp_ctx, "keepalive", "300");
2230 lpcfg_do_global_parameter(lp_ctx, "winbind cache time", "300");
2232 lpcfg_do_global_parameter(lp_ctx, "level2 oplocks", "yes");
2234 lpcfg_do_global_parameter(lp_ctx, "show add printer wizard", "yes");
2236 lpcfg_do_global_parameter(lp_ctx, "allocation roundup size", "1048576");
2238 lpcfg_do_global_parameter(lp_ctx, "ldap page size", "1024");
2240 lpcfg_do_global_parameter(lp_ctx, "kernel share modes", "yes");
2242 lpcfg_do_global_parameter(lp_ctx, "strict locking", "Auto");
2244 lpcfg_do_global_parameter(lp_ctx, "map readonly", "yes");
2246 lpcfg_do_global_parameter(lp_ctx, "allow trusted domains", "yes");
2248 lpcfg_do_global_parameter(lp_ctx, "default devmode", "yes");
2250 lpcfg_do_global_parameter(lp_ctx, "os level", "20");
2252 lpcfg_do_global_parameter(lp_ctx, "dos filetimes", "yes");
2254 lpcfg_do_global_parameter(lp_ctx, "mangling char", "~");
2256 lpcfg_do_global_parameter(lp_ctx, "printcap cache time", "750");
2258 lpcfg_do_global_parameter(lp_ctx, "create krb5 conf", "yes");
2260 lpcfg_do_global_parameter(lp_ctx, "winbind max clients", "200");
2262 lpcfg_do_global_parameter(lp_ctx, "acl map full control", "yes");
2264 lpcfg_do_global_parameter(lp_ctx, "nt pipe support", "yes");
2266 lpcfg_do_global_parameter(lp_ctx, "ldap debug threshold", "10");
2268 lpcfg_do_global_parameter(lp_ctx, "follow symlinks", "yes");
2270 lpcfg_do_global_parameter(lp_ctx, "machine password timeout", "604800");
2272 lpcfg_do_global_parameter(lp_ctx, "ldap connection timeout", "2");
2274 lpcfg_do_global_parameter(lp_ctx, "winbind expand groups", "1");
2276 lpcfg_do_global_parameter(lp_ctx, "stat cache", "yes");
2278 lpcfg_do_global_parameter(lp_ctx, "lpq cache time", "30");
2280 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max trans", "%u", DEFAULT_SMB2_MAX_TRANSACT);
2282 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max read", "%u", DEFAULT_SMB2_MAX_READ);
2284 lpcfg_do_global_parameter(lp_ctx, "durable handles", "yes");
2286 lpcfg_do_global_parameter(lp_ctx, "max stat cache size", "256");
2288 lpcfg_do_global_parameter(lp_ctx, "ldap passwd sync", "no");
2290 lpcfg_do_global_parameter(lp_ctx, "kernel change notify", "yes");
2292 lpcfg_do_global_parameter(lp_ctx, "max ttl", "259200");
2294 lpcfg_do_global_parameter(lp_ctx, "blocking locks", "yes");
2296 lpcfg_do_global_parameter(lp_ctx, "oplock contention limit", "2");
2298 lpcfg_do_global_parameter(lp_ctx, "load printers", "yes");
2300 lpcfg_do_global_parameter(lp_ctx, "idmap cache time", "604800");
2302 lpcfg_do_global_parameter(lp_ctx, "preserve case", "yes");
2304 lpcfg_do_global_parameter(lp_ctx, "lm announce", "auto");
2306 lpcfg_do_global_parameter(lp_ctx, "afs token lifetime", "604800");
2308 lpcfg_do_global_parameter(lp_ctx, "enable core files", "yes");
2310 lpcfg_do_global_parameter(lp_ctx, "winbind max domain connections", "1");
2312 lpcfg_do_global_parameter(lp_ctx, "case sensitive", "auto");
2314 lpcfg_do_global_parameter(lp_ctx, "ldap timeout", "15");
2316 lpcfg_do_global_parameter(lp_ctx, "mangle prefix", "1");
2318 lpcfg_do_global_parameter(lp_ctx, "posix locking", "yes");
2320 lpcfg_do_global_parameter(lp_ctx, "lock spin time", "200");
2322 lpcfg_do_global_parameter(lp_ctx, "directory name cache size", "100");
2324 lpcfg_do_global_parameter(lp_ctx, "nmbd bind explicit broadcast", "yes");
2326 lpcfg_do_global_parameter(lp_ctx, "init logon delay", "100");
2328 lpcfg_do_global_parameter(lp_ctx, "usershare owner only", "yes");
2330 lpcfg_do_global_parameter(lp_ctx, "-valid", "yes");
2332 lpcfg_do_global_parameter_var(lp_ctx, "usershare path", "%s/usershares", get_dyn_STATEDIR());
2334 #ifdef DEVELOPER
2335 lpcfg_do_global_parameter_var(lp_ctx, "panic action", "/bin/sleep 999999999");
2336 #endif
2338 lpcfg_do_global_parameter(lp_ctx, "smb passwd file", get_dyn_SMB_PASSWD_FILE());
2340 lpcfg_do_global_parameter(lp_ctx, "logon home", "\\\\%N\\%U");
2342 lpcfg_do_global_parameter(lp_ctx, "logon path", "\\\\%N\\%U\\profile");
2344 lpcfg_do_global_parameter(lp_ctx, "printjob username", "%U");
2346 for (i = 0; parm_table[i].label; i++) {
2347 if (!(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2348 lp_ctx->flags[i] |= FLAG_DEFAULT;
2352 for (parm=lp_ctx->globals->param_opt; parm; parm=parm->next) {
2353 if (!(parm->priority & FLAG_CMDLINE)) {
2354 parm->priority |= FLAG_DEFAULT;
2358 return lp_ctx;
2362 * Initialise the global parameter structure.
2364 struct loadparm_context *loadparm_init_global(bool load_default)
2366 if (global_loadparm_context == NULL) {
2367 global_loadparm_context = loadparm_init(NULL);
2369 if (global_loadparm_context == NULL) {
2370 return NULL;
2372 global_loadparm_context->global = true;
2373 if (load_default && !global_loadparm_context->loaded) {
2374 lpcfg_load_default(global_loadparm_context);
2376 global_loadparm_context->refuse_free = true;
2377 return global_loadparm_context;
2381 * Initialise the global parameter structure.
2383 struct loadparm_context *loadparm_init_s3(TALLOC_CTX *mem_ctx,
2384 const struct loadparm_s3_helpers *s3_fns)
2386 struct loadparm_context *loadparm_context = talloc_zero(mem_ctx, struct loadparm_context);
2387 if (!loadparm_context) {
2388 return NULL;
2390 loadparm_context->s3_fns = s3_fns;
2391 loadparm_context->globals = s3_fns->globals;
2392 return loadparm_context;
2395 const char *lpcfg_configfile(struct loadparm_context *lp_ctx)
2397 return lp_ctx->szConfigFile;
2400 const char *lp_default_path(void)
2402 if (getenv("SMB_CONF_PATH"))
2403 return getenv("SMB_CONF_PATH");
2404 else
2405 return dyn_CONFIGFILE;
2409 * Update the internal state of a loadparm context after settings
2410 * have changed.
2412 static bool lpcfg_update(struct loadparm_context *lp_ctx)
2414 struct debug_settings settings;
2415 TALLOC_CTX *tmp_ctx;
2417 tmp_ctx = talloc_new(lp_ctx);
2418 if (tmp_ctx == NULL) {
2419 return false;
2422 lpcfg_add_auto_services(lp_ctx, lpcfg_auto_services(lp_ctx, tmp_ctx));
2424 if (!lp_ctx->globals->wins_server_list && lp_ctx->globals->we_are_a_wins_server) {
2425 lpcfg_do_global_parameter(lp_ctx, "wins server", "127.0.0.1");
2428 if (!lp_ctx->global) {
2429 TALLOC_FREE(tmp_ctx);
2430 return true;
2433 panic_action = lp_ctx->globals->panic_action;
2435 reload_charcnv(lp_ctx);
2437 ZERO_STRUCT(settings);
2438 /* Add any more debug-related smb.conf parameters created in
2439 * future here */
2440 settings.syslog = lp_ctx->globals->syslog;
2441 settings.syslog_only = lp_ctx->globals->syslog_only;
2442 settings.timestamp_logs = lp_ctx->globals->timestamp_logs;
2443 settings.debug_prefix_timestamp = lp_ctx->globals->debug_prefix_timestamp;
2444 settings.debug_hires_timestamp = lp_ctx->globals->debug_hires_timestamp;
2445 settings.debug_pid = lp_ctx->globals->debug_pid;
2446 settings.debug_uid = lp_ctx->globals->debug_uid;
2447 settings.debug_class = lp_ctx->globals->debug_class;
2448 debug_set_settings(&settings);
2450 /* FIXME: This is a bit of a hack, but we can't use a global, since
2451 * not everything that uses lp also uses the socket library */
2452 if (lpcfg_parm_bool(lp_ctx, NULL, "socket", "testnonblock", false)) {
2453 setenv("SOCKET_TESTNONBLOCK", "1", 1);
2454 } else {
2455 unsetenv("SOCKET_TESTNONBLOCK");
2458 TALLOC_FREE(tmp_ctx);
2459 return true;
2462 bool lpcfg_load_default(struct loadparm_context *lp_ctx)
2464 const char *path;
2466 path = lp_default_path();
2468 if (!file_exist(path)) {
2469 /* We allow the default smb.conf file to not exist,
2470 * basically the equivalent of an empty file. */
2471 return lpcfg_update(lp_ctx);
2474 return lpcfg_load(lp_ctx, path);
2478 * Load the services array from the services file.
2480 * Return True on success, False on failure.
2482 bool lpcfg_load(struct loadparm_context *lp_ctx, const char *filename)
2484 char *n2;
2485 bool bRetval;
2487 filename = talloc_strdup(lp_ctx, filename);
2489 lp_ctx->szConfigFile = filename;
2491 if (lp_ctx->s3_fns) {
2492 return lp_ctx->s3_fns->load(filename);
2495 lp_ctx->bInGlobalSection = true;
2496 n2 = standard_sub_basic(lp_ctx, lp_ctx->szConfigFile);
2497 DEBUG(2, ("lpcfg_load: refreshing parameters from %s\n", n2));
2499 add_to_file_list(lp_ctx, lp_ctx->szConfigFile, n2);
2501 /* We get sections first, so have to start 'behind' to make up */
2502 lp_ctx->currentService = NULL;
2503 bRetval = pm_process(n2, do_section, do_parameter, lp_ctx);
2505 /* finish up the last section */
2506 DEBUG(4, ("pm_process() returned %s\n", BOOLSTR(bRetval)));
2507 if (bRetval)
2508 if (lp_ctx->currentService != NULL)
2509 bRetval = lpcfg_service_ok(lp_ctx->currentService);
2511 bRetval = bRetval && lpcfg_update(lp_ctx);
2513 /* we do this unconditionally, so that it happens even
2514 for a missing smb.conf */
2515 reload_charcnv(lp_ctx);
2517 if (bRetval == true) {
2518 /* set this up so that any child python tasks will
2519 find the right smb.conf */
2520 setenv("SMB_CONF_PATH", filename, 1);
2522 /* set the context used by the lp_*() function
2523 varients */
2524 global_loadparm_context = lp_ctx;
2525 lp_ctx->loaded = true;
2528 return bRetval;
2532 * Return the max number of services.
2535 int lpcfg_numservices(struct loadparm_context *lp_ctx)
2537 if (lp_ctx->s3_fns) {
2538 return lp_ctx->s3_fns->get_numservices();
2541 return lp_ctx->iNumServices;
2545 * Display the contents of the services array in human-readable form.
2548 void lpcfg_dump(struct loadparm_context *lp_ctx, FILE *f, bool show_defaults,
2549 int maxtoprint)
2551 int iService;
2553 if (lp_ctx->s3_fns) {
2554 lp_ctx->s3_fns->dump(f, show_defaults, maxtoprint);
2555 return;
2558 defaults_saved = !show_defaults;
2560 dump_globals(lp_ctx, f, show_defaults);
2562 dump_a_service(lp_ctx->sDefault, lp_ctx->sDefault, f, lp_ctx->flags);
2564 for (iService = 0; iService < maxtoprint; iService++)
2565 lpcfg_dump_one(f, show_defaults, lp_ctx->services[iService], lp_ctx->sDefault);
2569 * Display the contents of one service in human-readable form.
2571 void lpcfg_dump_one(FILE *f, bool show_defaults, struct loadparm_service *service, struct loadparm_service *sDefault)
2573 if (service != NULL) {
2574 if (service->szService[0] == '\0')
2575 return;
2576 dump_a_service(service, sDefault, f, NULL);
2580 struct loadparm_service *lpcfg_servicebynum(struct loadparm_context *lp_ctx,
2581 int snum)
2583 if (lp_ctx->s3_fns) {
2584 return lp_ctx->s3_fns->get_servicebynum(snum);
2587 return lp_ctx->services[snum];
2590 struct loadparm_service *lpcfg_service(struct loadparm_context *lp_ctx,
2591 const char *service_name)
2593 int iService;
2594 char *serviceName;
2596 if (lp_ctx->s3_fns) {
2597 return lp_ctx->s3_fns->get_service(service_name);
2600 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--) {
2601 if (lp_ctx->services[iService] &&
2602 lp_ctx->services[iService]->szService) {
2604 * The substitution here is used to support %U is
2605 * service names
2607 serviceName = standard_sub_basic(
2608 lp_ctx->services[iService],
2609 lp_ctx->services[iService]->szService);
2610 if (strequal(serviceName, service_name)) {
2611 talloc_free(serviceName);
2612 return lp_ctx->services[iService];
2614 talloc_free(serviceName);
2618 DEBUG(7,("lpcfg_servicenumber: couldn't find %s\n", service_name));
2619 return NULL;
2622 const char *lpcfg_servicename(const struct loadparm_service *service)
2624 return lpcfg_string((const char *)service->szService);
2628 * A useful volume label function.
2630 const char *lpcfg_volume_label(struct loadparm_service *service, struct loadparm_service *sDefault)
2632 const char *ret;
2633 ret = lpcfg_string((const char *)((service != NULL && service->volume != NULL) ?
2634 service->volume : sDefault->volume));
2635 if (!*ret)
2636 return lpcfg_servicename(service);
2637 return ret;
2641 * Return the correct printer name.
2643 const char *lpcfg_printername(struct loadparm_service *service, struct loadparm_service *sDefault)
2645 const char *ret;
2646 ret = lpcfg_string((const char *)((service != NULL && service->_printername != NULL) ?
2647 service->_printername : sDefault->_printername));
2648 if (ret == NULL || (ret != NULL && *ret == '\0'))
2649 ret = lpcfg_servicename(service);
2651 return ret;
2656 * Return the max print jobs per queue.
2658 int lpcfg_maxprintjobs(struct loadparm_service *service, struct loadparm_service *sDefault)
2660 int maxjobs = (service != NULL) ? service->iMaxPrintJobs : sDefault->iMaxPrintJobs;
2661 if (maxjobs <= 0 || maxjobs >= PRINT_MAX_JOBID)
2662 maxjobs = PRINT_MAX_JOBID - 1;
2664 return maxjobs;
2667 struct smb_iconv_handle *lpcfg_iconv_handle(struct loadparm_context *lp_ctx)
2669 if (lp_ctx == NULL) {
2670 return get_iconv_handle();
2672 return lp_ctx->iconv_handle;
2675 _PUBLIC_ void reload_charcnv(struct loadparm_context *lp_ctx)
2677 struct smb_iconv_handle *old_ic = lp_ctx->iconv_handle;
2678 if (!lp_ctx->global) {
2679 return;
2682 if (old_ic == NULL) {
2683 old_ic = global_iconv_handle;
2685 lp_ctx->iconv_handle = smb_iconv_handle_reinit_lp(lp_ctx, lp_ctx, old_ic);
2686 global_iconv_handle = lp_ctx->iconv_handle;
2689 _PUBLIC_ char *lpcfg_tls_keyfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2691 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_keyfile(lp_ctx));
2694 _PUBLIC_ char *lpcfg_tls_certfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2696 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_certfile(lp_ctx));
2699 _PUBLIC_ char *lpcfg_tls_cafile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2701 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_cafile(lp_ctx));
2704 _PUBLIC_ char *lpcfg_tls_crlfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2706 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_crlfile(lp_ctx));
2709 _PUBLIC_ char *lpcfg_tls_dhpfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2711 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_dhpfile(lp_ctx));
2714 struct gensec_settings *lpcfg_gensec_settings(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2716 struct gensec_settings *settings = talloc_zero(mem_ctx, struct gensec_settings);
2717 if (settings == NULL)
2718 return NULL;
2719 SMB_ASSERT(lp_ctx != NULL);
2720 settings->lp_ctx = talloc_reference(settings, lp_ctx);
2721 settings->target_hostname = lpcfg_parm_string(lp_ctx, NULL, "gensec", "target_hostname");
2722 return settings;
2725 int lpcfg_server_role(struct loadparm_context *lp_ctx)
2727 int domain_master = lpcfg__domain_master(lp_ctx);
2729 return lp_find_server_role(lpcfg__server_role(lp_ctx),
2730 lpcfg__security(lp_ctx),
2731 lpcfg__domain_logons(lp_ctx),
2732 (domain_master == true) ||
2733 (domain_master == Auto));
2736 int lpcfg_security(struct loadparm_context *lp_ctx)
2738 return lp_find_security(lpcfg__server_role(lp_ctx),
2739 lpcfg__security(lp_ctx));
2742 bool lpcfg_server_signing_allowed(struct loadparm_context *lp_ctx, bool *mandatory)
2744 bool allowed = true;
2745 enum smb_signing_setting signing_setting = lpcfg_server_signing(lp_ctx);
2747 *mandatory = false;
2749 if (signing_setting == SMB_SIGNING_DEFAULT) {
2751 * If we are a domain controller, SMB signing is
2752 * really important, as it can prevent a number of
2753 * attacks on communications between us and the
2754 * clients
2756 * However, it really sucks (no sendfile, CPU
2757 * overhead) performance-wise when used on a
2758 * file server, so disable it by default
2759 * on non-DCs
2762 if (lpcfg_server_role(lp_ctx) >= ROLE_ACTIVE_DIRECTORY_DC) {
2763 signing_setting = SMB_SIGNING_REQUIRED;
2764 } else {
2765 signing_setting = SMB_SIGNING_OFF;
2769 switch (signing_setting) {
2770 case SMB_SIGNING_REQUIRED:
2771 *mandatory = true;
2772 break;
2773 case SMB_SIGNING_IF_REQUIRED:
2774 break;
2775 case SMB_SIGNING_DEFAULT:
2776 case SMB_SIGNING_OFF:
2777 allowed = false;
2778 break;
2781 return allowed;
2784 int lpcfg_tdb_hash_size(struct loadparm_context *lp_ctx, const char *name)
2786 const char *base;
2788 if (name == NULL) {
2789 return 0;
2792 base = strrchr_m(name, '/');
2793 if (base != NULL) {
2794 base += 1;
2795 } else {
2796 base = name;
2798 return lpcfg_parm_int(lp_ctx, NULL, "tdb_hashsize", base, 0);
2802 int lpcfg_tdb_flags(struct loadparm_context *lp_ctx, int tdb_flags)
2804 if (!lpcfg_use_mmap(lp_ctx)) {
2805 tdb_flags |= TDB_NOMMAP;
2807 return tdb_flags;