lib/param: remove some const warnings from using lists
[Samba.git] / lib / param / loadparm.c
blobb0dcf3d9a12b14b3a7b02de941d3ae7bbb4e0a6f
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_copy(struct loadparm_context *lp_ctx, int unused,
102 const char *pszParmValue, char **ptr);
104 #include "lib/param/param_table.c"
106 /* local variables */
107 struct loadparm_context {
108 const char *szConfigFile;
109 struct loadparm_global *globals;
110 struct loadparm_service **services;
111 struct loadparm_service *sDefault;
112 struct smb_iconv_handle *iconv_handle;
113 int iNumServices;
114 struct loadparm_service *currentService;
115 bool bInGlobalSection;
116 struct file_lists {
117 struct file_lists *next;
118 char *name;
119 char *subfname;
120 time_t modtime;
121 } *file_lists;
122 unsigned int flags[NUMPARAMETERS];
123 bool loaded;
124 bool refuse_free;
125 bool global; /* Is this the global context, which may set
126 * global variables such as debug level etc? */
127 const struct loadparm_s3_helpers *s3_fns;
131 struct loadparm_service *lpcfg_default_service(struct loadparm_context *lp_ctx)
133 if (lp_ctx->s3_fns) {
134 return lp_ctx->s3_fns->get_default_loadparm_service();
136 return lp_ctx->sDefault;
140 * Convenience routine to grab string parameters into temporary memory
141 * and run standard_sub_basic on them.
143 * The buffers can be written to by
144 * callers without affecting the source string.
147 static const char *lpcfg_string(const char *s)
149 #if 0 /* until REWRITE done to make thread-safe */
150 size_t len = s ? strlen(s) : 0;
151 char *ret;
152 #endif
154 /* The follow debug is useful for tracking down memory problems
155 especially if you have an inner loop that is calling a lp_*()
156 function that returns a string. Perhaps this debug should be
157 present all the time? */
159 #if 0
160 DEBUG(10, ("lpcfg_string(%s)\n", s));
161 #endif
163 #if 0 /* until REWRITE done to make thread-safe */
164 if (!lp_talloc)
165 lp_talloc = talloc_init("lp_talloc");
167 ret = talloc_array(lp_talloc, char, len + 100); /* leave room for substitution */
169 if (!ret)
170 return NULL;
172 if (!s)
173 *ret = 0;
174 else
175 strlcpy(ret, s, len);
177 if (trim_string(ret, "\"", "\"")) {
178 if (strchr(ret,'"') != NULL)
179 strlcpy(ret, s, len);
182 standard_sub_basic(ret,len+100);
183 return (ret);
184 #endif
185 return s;
189 In this section all the functions that are used to access the
190 parameters from the rest of the program are defined
194 * the creation of separate lpcfg_*() and lp_*() functions is to allow
195 * for code compatibility between existing Samba4 and Samba3 code.
198 /* this global context supports the lp_*() function varients */
199 static struct loadparm_context *global_loadparm_context;
201 #define lpcfg_default_service global_loadparm_context->sDefault
202 #define lpcfg_global_service(i) global_loadparm_context->services[i]
204 #define FN_GLOBAL_STRING(fn_name,var_name) \
205 _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx, TALLOC_CTX *ctx) {\
206 if (lp_ctx == NULL) return NULL; \
207 if (lp_ctx->s3_fns) { \
208 return lp_ctx->globals->var_name ? lp_ctx->s3_fns->lp_string(ctx, lp_ctx->globals->var_name) : talloc_strdup(ctx, ""); \
210 return lp_ctx->globals->var_name ? talloc_strdup(ctx, lpcfg_string(lp_ctx->globals->var_name)) : talloc_strdup(ctx, ""); \
213 #define FN_GLOBAL_CONST_STRING(fn_name,var_name) \
214 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
215 if (lp_ctx == NULL) return NULL; \
216 return lp_ctx->globals->var_name ? lpcfg_string(lp_ctx->globals->var_name) : ""; \
219 #define FN_GLOBAL_LIST(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; \
225 #define FN_GLOBAL_BOOL(fn_name,var_name) \
226 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) {\
227 if (lp_ctx == NULL) return false; \
228 return lp_ctx->globals->var_name; \
231 #define FN_GLOBAL_INTEGER(fn_name,var_name) \
232 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
233 return lp_ctx->globals->var_name; \
236 /* Local parameters don't need the ->s3_fns because the struct
237 * loadparm_service is shared and lpcfg_service() checks the ->s3_fns
238 * hook */
239 #define FN_LOCAL_STRING(fn_name,val) \
240 _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_service *service, \
241 struct loadparm_service *sDefault, TALLOC_CTX *ctx) { \
242 return(talloc_strdup(ctx, lpcfg_string((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)))); \
245 #define FN_LOCAL_CONST_STRING(fn_name,val) \
246 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_service *service, \
247 struct loadparm_service *sDefault) { \
248 return((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)); \
251 #define FN_LOCAL_LIST(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_PARM_BOOL(fn_name, val) FN_LOCAL_BOOL(fn_name, val)
259 #define FN_LOCAL_BOOL(fn_name,val) \
260 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_service *service, \
261 struct loadparm_service *sDefault) { \
262 return((service != NULL)? service->val : sDefault->val); \
265 #define FN_LOCAL_INTEGER(fn_name,val) \
266 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_service *service, \
267 struct loadparm_service *sDefault) { \
268 return((service != NULL)? service->val : sDefault->val); \
271 #define FN_LOCAL_PARM_INTEGER(fn_name, val) FN_LOCAL_INTEGER(fn_name, val)
273 #define FN_LOCAL_PARM_CHAR(fn_name,val) \
274 _PUBLIC_ char lpcfg_ ## fn_name(struct loadparm_service *service, \
275 struct loadparm_service *sDefault) { \
276 return((service != NULL)? service->val : sDefault->val); \
279 #include "lib/param/param_functions.c"
281 /* These functions cannot be auto-generated */
282 FN_LOCAL_BOOL(autoloaded, autoloaded)
283 FN_GLOBAL_CONST_STRING(dnsdomain, dnsdomain)
285 /* local prototypes */
286 static struct loadparm_service *getservicebyname(struct loadparm_context *lp_ctx,
287 const char *pszServiceName);
288 static void copy_service(struct loadparm_service *pserviceDest,
289 const struct loadparm_service *pserviceSource,
290 struct bitmap *pcopymapDest);
291 static bool lpcfg_service_ok(struct loadparm_service *service);
292 static bool do_section(const char *pszSectionName, void *);
293 static void init_copymap(struct loadparm_service *pservice);
295 /* This is a helper function for parametrical options support. */
296 /* It returns a pointer to parametrical option value if it exists or NULL otherwise */
297 /* Actual parametrical functions are quite simple */
298 const char *lpcfg_get_parametric(struct loadparm_context *lp_ctx,
299 struct loadparm_service *service,
300 const char *type, const char *option)
302 char *vfskey_tmp = NULL;
303 char *vfskey = NULL;
304 struct parmlist_entry *data;
306 if (lp_ctx == NULL)
307 return NULL;
309 if (lp_ctx->s3_fns) {
310 return lp_ctx->s3_fns->get_parametric(service, type, option);
313 data = (service == NULL ? lp_ctx->globals->param_opt : service->param_opt);
315 vfskey_tmp = talloc_asprintf(NULL, "%s:%s", type, option);
316 if (vfskey_tmp == NULL) return NULL;
317 vfskey = strlower_talloc(NULL, vfskey_tmp);
318 talloc_free(vfskey_tmp);
320 while (data) {
321 if (strcmp(data->key, vfskey) == 0) {
322 talloc_free(vfskey);
323 return data->value;
325 data = data->next;
328 if (service != NULL) {
329 /* Try to fetch the same option but from globals */
330 /* but only if we are not already working with globals */
331 for (data = lp_ctx->globals->param_opt; data;
332 data = data->next) {
333 if (strcmp(data->key, vfskey) == 0) {
334 talloc_free(vfskey);
335 return data->value;
340 talloc_free(vfskey);
342 return NULL;
347 * convenience routine to return int parameters.
349 static int lp_int(const char *s)
352 if (!s) {
353 DEBUG(0,("lp_int(%s): is called with NULL!\n",s));
354 return -1;
357 return strtol(s, NULL, 0);
361 * convenience routine to return unsigned long parameters.
363 static unsigned long lp_ulong(const char *s)
366 if (!s) {
367 DEBUG(0,("lp_ulong(%s): is called with NULL!\n",s));
368 return -1;
371 return strtoul(s, NULL, 0);
375 * convenience routine to return unsigned long parameters.
377 static long lp_long(const char *s)
380 if (!s) {
381 DEBUG(0,("lp_long(%s): is called with NULL!\n",s));
382 return -1;
385 return strtol(s, NULL, 0);
389 * convenience routine to return unsigned long parameters.
391 static double lp_double(const char *s)
394 if (!s) {
395 DEBUG(0,("lp_double(%s): is called with NULL!\n",s));
396 return -1;
399 return strtod(s, NULL);
403 * convenience routine to return boolean parameters.
405 static bool lp_bool(const char *s)
407 bool ret = false;
409 if (!s) {
410 DEBUG(0,("lp_bool(%s): is called with NULL!\n",s));
411 return false;
414 if (!set_boolean(s, &ret)) {
415 DEBUG(0,("lp_bool(%s): value is not boolean!\n",s));
416 return false;
419 return ret;
424 * Return parametric option from a given service. Type is a part of option before ':'
425 * Parametric option has following syntax: 'Type: option = value'
426 * Returned value is allocated in 'lp_talloc' context
429 const char *lpcfg_parm_string(struct loadparm_context *lp_ctx,
430 struct loadparm_service *service, const char *type,
431 const char *option)
433 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
435 if (value)
436 return lpcfg_string(value);
438 return NULL;
442 * Return parametric option from a given service. Type is a part of option before ':'
443 * Parametric option has following syntax: 'Type: option = value'
444 * Returned value is allocated in 'lp_talloc' context
447 const char **lpcfg_parm_string_list(TALLOC_CTX *mem_ctx,
448 struct loadparm_context *lp_ctx,
449 struct loadparm_service *service,
450 const char *type,
451 const char *option, const char *separator)
453 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
455 if (value != NULL)
456 return (const char **)str_list_make(mem_ctx, value, separator);
458 return NULL;
462 * Return parametric option from a given service. Type is a part of option before ':'
463 * Parametric option has following syntax: 'Type: option = value'
466 int lpcfg_parm_int(struct loadparm_context *lp_ctx,
467 struct loadparm_service *service, const char *type,
468 const char *option, int default_v)
470 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
472 if (value)
473 return lp_int(value);
475 return default_v;
479 * Return parametric option from a given service. Type is a part of
480 * option before ':'.
481 * Parametric option has following syntax: 'Type: option = value'.
484 int lpcfg_parm_bytes(struct loadparm_context *lp_ctx,
485 struct loadparm_service *service, const char *type,
486 const char *option, int default_v)
488 uint64_t bval;
490 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
492 if (value && conv_str_size_error(value, &bval)) {
493 if (bval <= INT_MAX) {
494 return (int)bval;
498 return default_v;
502 * Return parametric option from a given service.
503 * Type is a part of option before ':'
504 * Parametric option has following syntax: 'Type: option = value'
506 unsigned long lpcfg_parm_ulong(struct loadparm_context *lp_ctx,
507 struct loadparm_service *service, const char *type,
508 const char *option, unsigned long default_v)
510 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
512 if (value)
513 return lp_ulong(value);
515 return default_v;
518 long lpcfg_parm_long(struct loadparm_context *lp_ctx,
519 struct loadparm_service *service, const char *type,
520 const char *option, long default_v)
522 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
524 if (value)
525 return lp_long(value);
527 return default_v;
530 double lpcfg_parm_double(struct loadparm_context *lp_ctx,
531 struct loadparm_service *service, const char *type,
532 const char *option, double default_v)
534 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
536 if (value != NULL)
537 return lp_double(value);
539 return default_v;
543 * Return parametric option from a given service. Type is a part of option before ':'
544 * Parametric option has following syntax: 'Type: option = value'
547 bool lpcfg_parm_bool(struct loadparm_context *lp_ctx,
548 struct loadparm_service *service, const char *type,
549 const char *option, bool default_v)
551 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
553 if (value != NULL)
554 return lp_bool(value);
556 return default_v;
561 * Set a string value, deallocating any existing space, and allocing the space
562 * for the string
564 static bool lpcfg_string_set(TALLOC_CTX *mem_ctx, char **dest, const char *src)
566 talloc_free(*dest);
568 if (src == NULL)
569 src = "";
571 *dest = talloc_strdup(mem_ctx, src);
572 if ((*dest) == NULL) {
573 DEBUG(0,("Out of memory in string_set\n"));
574 return false;
577 return true;
581 * Set a string value, deallocating any existing space, and allocing the space
582 * for the string
584 static bool lpcfg_string_set_upper(TALLOC_CTX *mem_ctx, char **dest, const char *src)
586 talloc_free(*dest);
588 if (src == NULL)
589 src = "";
591 *dest = strupper_talloc(mem_ctx, src);
592 if ((*dest) == NULL) {
593 DEBUG(0,("Out of memory in string_set_upper\n"));
594 return false;
597 return true;
603 * Add a new service to the services array initialising it with the given
604 * service.
607 struct loadparm_service *lpcfg_add_service(struct loadparm_context *lp_ctx,
608 const struct loadparm_service *pservice,
609 const char *name)
611 int i;
612 int num_to_alloc = lp_ctx->iNumServices + 1;
613 struct parmlist_entry *data, *pdata;
615 if (pservice == NULL) {
616 pservice = lp_ctx->sDefault;
619 /* it might already exist */
620 if (name) {
621 struct loadparm_service *service = getservicebyname(lp_ctx,
622 name);
623 if (service != NULL) {
624 /* Clean all parametric options for service */
625 /* They will be added during parsing again */
626 data = service->param_opt;
627 while (data) {
628 pdata = data->next;
629 talloc_free(data);
630 data = pdata;
632 service->param_opt = NULL;
633 return service;
637 /* find an invalid one */
638 for (i = 0; i < lp_ctx->iNumServices; i++)
639 if (lp_ctx->services[i] == NULL)
640 break;
642 /* if not, then create one */
643 if (i == lp_ctx->iNumServices) {
644 struct loadparm_service **tsp;
646 tsp = talloc_realloc(lp_ctx, lp_ctx->services, struct loadparm_service *, num_to_alloc);
648 if (!tsp) {
649 DEBUG(0,("lpcfg_add_service: failed to enlarge services!\n"));
650 return NULL;
651 } else {
652 lp_ctx->services = tsp;
653 lp_ctx->services[lp_ctx->iNumServices] = NULL;
656 lp_ctx->iNumServices++;
659 lp_ctx->services[i] = talloc_zero(lp_ctx->services, struct loadparm_service);
660 if (lp_ctx->services[i] == NULL) {
661 DEBUG(0,("lpcfg_add_service: out of memory!\n"));
662 return NULL;
664 copy_service(lp_ctx->services[i], pservice, NULL);
665 if (name != NULL)
666 lpcfg_string_set(lp_ctx->services[i], &lp_ctx->services[i]->szService, name);
667 return lp_ctx->services[i];
671 * Add a new home service, with the specified home directory, defaults coming
672 * from service ifrom.
675 bool lpcfg_add_home(struct loadparm_context *lp_ctx,
676 const char *pszHomename,
677 struct loadparm_service *default_service,
678 const char *user, const char *pszHomedir)
680 struct loadparm_service *service;
682 service = lpcfg_add_service(lp_ctx, default_service, pszHomename);
684 if (service == NULL)
685 return false;
687 if (!(*(default_service->path))
688 || strequal(default_service->path, lp_ctx->sDefault->path)) {
689 service->path = talloc_strdup(service, pszHomedir);
690 } else {
691 service->path = string_sub_talloc(service, lpcfg_path(default_service, lp_ctx->sDefault, service), "%H", pszHomedir);
694 if (!(*(service->comment))) {
695 service->comment = talloc_asprintf(service, "Home directory of %s", user);
697 service->bAvailable = default_service->bAvailable;
698 service->browseable = default_service->browseable;
700 DEBUG(3, ("adding home's share [%s] for user '%s' at '%s'\n",
701 pszHomename, user, service->path));
703 return true;
707 * Add a new printer service, with defaults coming from service iFrom.
710 bool lpcfg_add_printer(struct loadparm_context *lp_ctx,
711 const char *pszPrintername,
712 struct loadparm_service *default_service)
714 const char *comment = "From Printcap";
715 struct loadparm_service *service;
716 service = lpcfg_add_service(lp_ctx, default_service, pszPrintername);
718 if (service == NULL)
719 return false;
721 /* note that we do NOT default the availability flag to True - */
722 /* we take it from the default service passed. This allows all */
723 /* dynamic printers to be disabled by disabling the [printers] */
724 /* entry (if/when the 'available' keyword is implemented!). */
726 /* the printer name is set to the service name. */
727 lpcfg_string_set(service, &service->_printername, pszPrintername);
728 lpcfg_string_set(service, &service->comment, comment);
729 service->browseable = default_service->browseable;
730 /* Printers cannot be read_only. */
731 service->read_only = false;
732 /* Printer services must be printable. */
733 service->printable = true;
735 DEBUG(3, ("adding printer service %s\n", pszPrintername));
737 return true;
741 * Map a parameter's string representation to something we can use.
742 * Returns False if the parameter string is not recognised, else TRUE.
745 int lpcfg_map_parameter(const char *pszParmName)
747 int iIndex;
749 for (iIndex = 0; parm_table[iIndex].label; iIndex++)
750 if (strwicmp(parm_table[iIndex].label, pszParmName) == 0)
751 return iIndex;
753 /* Warn only if it isn't parametric option */
754 if (strchr(pszParmName, ':') == NULL)
755 DEBUG(0, ("Unknown parameter encountered: \"%s\"\n", pszParmName));
756 /* We do return 'fail' for parametric options as well because they are
757 stored in different storage
759 return -1;
764 return the parameter structure for a parameter
766 struct parm_struct *lpcfg_parm_struct(struct loadparm_context *lp_ctx, const char *name)
768 int parmnum;
770 if (lp_ctx->s3_fns) {
771 return lp_ctx->s3_fns->get_parm_struct(name);
774 parmnum = lpcfg_map_parameter(name);
775 if (parmnum == -1) return NULL;
776 return &parm_table[parmnum];
780 return the parameter pointer for a parameter
782 void *lpcfg_parm_ptr(struct loadparm_context *lp_ctx,
783 struct loadparm_service *service, struct parm_struct *parm)
785 if (lp_ctx->s3_fns) {
786 return lp_ctx->s3_fns->get_parm_ptr(service, parm);
789 if (service == NULL) {
790 if (parm->p_class == P_LOCAL)
791 return ((char *)lp_ctx->sDefault)+parm->offset;
792 else if (parm->p_class == P_GLOBAL)
793 return ((char *)lp_ctx->globals)+parm->offset;
794 else return NULL;
795 } else {
796 return ((char *)service) + parm->offset;
801 return the parameter pointer for a parameter
803 bool lpcfg_parm_is_cmdline(struct loadparm_context *lp_ctx, const char *name)
805 int parmnum;
807 if (lp_ctx->s3_fns) {
808 struct parm_struct *parm = lp_ctx->s3_fns->get_parm_struct(name);
809 if (parm) {
810 return parm->flags & FLAG_CMDLINE;
812 return false;
815 parmnum = lpcfg_map_parameter(name);
816 if (parmnum == -1) return false;
818 return lp_ctx->flags[parmnum] & FLAG_CMDLINE;
822 * Find a service by name. Otherwise works like get_service.
825 static struct loadparm_service *getservicebyname(struct loadparm_context *lp_ctx,
826 const char *pszServiceName)
828 int iService;
830 if (lp_ctx->s3_fns) {
831 return lp_ctx->s3_fns->get_service(pszServiceName);
834 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--)
835 if (lp_ctx->services[iService] != NULL &&
836 strwicmp(lp_ctx->services[iService]->szService, pszServiceName) == 0) {
837 return lp_ctx->services[iService];
840 return NULL;
844 * Add a parametric option to a parmlist_entry,
845 * replacing old value, if already present.
847 void set_param_opt(TALLOC_CTX *mem_ctx,
848 struct parmlist_entry **opt_list,
849 const char *opt_name,
850 const char *opt_value,
851 unsigned priority)
853 struct parmlist_entry *new_opt, *opt;
854 bool not_added;
856 opt = *opt_list;
857 not_added = true;
859 /* Traverse destination */
860 while (opt) {
861 /* If we already have same option, override it */
862 if (strwicmp(opt->key, opt_name) == 0) {
863 if ((opt->priority & FLAG_CMDLINE) &&
864 !(priority & FLAG_CMDLINE)) {
865 /* it's been marked as not to be
866 overridden */
867 return;
869 TALLOC_FREE(opt->value);
870 TALLOC_FREE(opt->list);
871 opt->value = talloc_strdup(opt, opt_value);
872 opt->priority = priority;
873 not_added = false;
874 break;
876 opt = opt->next;
878 if (not_added) {
879 new_opt = talloc(mem_ctx, struct parmlist_entry);
880 if (new_opt == NULL) {
881 smb_panic("OOM");
884 new_opt->key = talloc_strdup(new_opt, opt_name);
885 if (new_opt->key == NULL) {
886 smb_panic("talloc_strdup failed");
889 new_opt->value = talloc_strdup(new_opt, opt_value);
890 if (new_opt->value == NULL) {
891 smb_panic("talloc_strdup failed");
894 new_opt->list = NULL;
895 new_opt->priority = priority;
896 DLIST_ADD(*opt_list, new_opt);
901 * Copy a service structure to another.
902 * If pcopymapDest is NULL then copy all fields
905 static void copy_service(struct loadparm_service *pserviceDest,
906 const struct loadparm_service *pserviceSource,
907 struct bitmap *pcopymapDest)
909 int i;
910 bool bcopyall = (pcopymapDest == NULL);
911 struct parmlist_entry *data;
913 for (i = 0; parm_table[i].label; i++)
914 if (parm_table[i].p_class == P_LOCAL &&
915 (bcopyall || bitmap_query(pcopymapDest, i))) {
916 const void *src_ptr =
917 ((const char *)pserviceSource) + parm_table[i].offset;
918 void *dest_ptr =
919 ((char *)pserviceDest) + parm_table[i].offset;
921 switch (parm_table[i].type) {
922 case P_BOOL:
923 case P_BOOLREV:
924 *(bool *)dest_ptr = *(const bool *)src_ptr;
925 break;
927 case P_INTEGER:
928 case P_BYTES:
929 case P_OCTAL:
930 case P_ENUM:
931 *(int *)dest_ptr = *(const int *)src_ptr;
932 break;
934 case P_CHAR:
935 *(char *)dest_ptr = *(const char *)src_ptr;
936 break;
938 case P_STRING:
939 lpcfg_string_set(pserviceDest,
940 (char **)dest_ptr,
941 *(const char * const *)src_ptr);
942 break;
944 case P_USTRING:
945 lpcfg_string_set_upper(pserviceDest,
946 (char **)dest_ptr,
947 *(const char * const *)src_ptr);
948 break;
949 case P_LIST:
950 *(const char * const **)dest_ptr = (const char * const *)str_list_copy(pserviceDest,
951 *(const char * * const *)src_ptr);
952 break;
953 default:
954 break;
958 if (bcopyall) {
959 init_copymap(pserviceDest);
960 if (pserviceSource->copymap)
961 bitmap_copy(pserviceDest->copymap,
962 pserviceSource->copymap);
965 for (data = pserviceSource->param_opt; data != NULL; data = data->next) {
966 set_param_opt(pserviceDest, &pserviceDest->param_opt,
967 data->key, data->value, data->priority);
972 * Check a service for consistency. Return False if the service is in any way
973 * incomplete or faulty, else True.
975 static bool lpcfg_service_ok(struct loadparm_service *service)
977 bool bRetval;
979 bRetval = true;
980 if (service->szService[0] == '\0') {
981 DEBUG(0, ("The following message indicates an internal error:\n"));
982 DEBUG(0, ("No service name in service entry.\n"));
983 bRetval = false;
986 /* The [printers] entry MUST be printable. I'm all for flexibility, but */
987 /* I can't see why you'd want a non-printable printer service... */
988 if (strwicmp(service->szService, PRINTERS_NAME) == 0) {
989 if (!service->printable) {
990 DEBUG(0, ("WARNING: [%s] service MUST be printable!\n",
991 service->szService));
992 service->printable = true;
994 /* [printers] service must also be non-browsable. */
995 if (service->browseable)
996 service->browseable = false;
999 /* If a service is flagged unavailable, log the fact at level 0. */
1000 if (!service->bAvailable)
1001 DEBUG(1, ("NOTE: Service %s is flagged unavailable.\n",
1002 service->szService));
1004 return bRetval;
1008 /*******************************************************************
1009 Keep a linked list of all config files so we know when one has changed
1010 it's date and needs to be reloaded.
1011 ********************************************************************/
1013 static void add_to_file_list(struct loadparm_context *lp_ctx,
1014 const char *fname, const char *subfname)
1016 struct file_lists *f = lp_ctx->file_lists;
1018 while (f) {
1019 if (f->name && !strcmp(f->name, fname))
1020 break;
1021 f = f->next;
1024 if (!f) {
1025 f = talloc(lp_ctx, struct file_lists);
1026 if (!f)
1027 return;
1028 f->next = lp_ctx->file_lists;
1029 f->name = talloc_strdup(f, fname);
1030 if (!f->name) {
1031 talloc_free(f);
1032 return;
1034 f->subfname = talloc_strdup(f, subfname);
1035 if (!f->subfname) {
1036 talloc_free(f);
1037 return;
1039 lp_ctx->file_lists = f;
1040 f->modtime = file_modtime(subfname);
1041 } else {
1042 time_t t = file_modtime(subfname);
1043 if (t)
1044 f->modtime = t;
1048 /*******************************************************************
1049 Check if a config file has changed date.
1050 ********************************************************************/
1051 bool lpcfg_file_list_changed(struct loadparm_context *lp_ctx)
1053 struct file_lists *f;
1054 DEBUG(6, ("lpcfg_file_list_changed()\n"));
1056 for (f = lp_ctx->file_lists; f != NULL; f = f->next) {
1057 char *n2;
1058 time_t mod_time;
1060 n2 = standard_sub_basic(lp_ctx, f->name);
1062 DEBUGADD(6, ("file %s -> %s last mod_time: %s\n",
1063 f->name, n2, ctime(&f->modtime)));
1065 mod_time = file_modtime(n2);
1067 if (mod_time && ((f->modtime != mod_time) || (f->subfname == NULL) || (strcmp(n2, f->subfname) != 0))) {
1068 DEBUGADD(6, ("file %s modified: %s\n", n2,
1069 ctime(&mod_time)));
1070 f->modtime = mod_time;
1071 talloc_free(f->subfname);
1072 f->subfname = talloc_strdup(f, n2);
1073 return true;
1076 return false;
1079 /***************************************************************************
1080 Handle the "realm" parameter
1081 ***************************************************************************/
1083 bool handle_realm(struct loadparm_context *lp_ctx, int unused,
1084 const char *pszParmValue, char **ptr)
1086 char *upper;
1087 char *lower;
1089 upper = strupper_talloc(lp_ctx, pszParmValue);
1090 if (upper == NULL) {
1091 return false;
1094 lower = strlower_talloc(lp_ctx, pszParmValue);
1095 if (lower == NULL) {
1096 TALLOC_FREE(upper);
1097 return false;
1100 if (lp_ctx->s3_fns != NULL) {
1101 lp_ctx->s3_fns->lp_string_set(ptr, pszParmValue);
1102 lp_ctx->s3_fns->lp_string_set(&lp_ctx->globals->realm, upper);
1103 lp_ctx->s3_fns->lp_string_set(&lp_ctx->globals->dnsdomain, lower);
1104 } else {
1105 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1106 lpcfg_string_set(lp_ctx, &lp_ctx->globals->realm, upper);
1107 lpcfg_string_set(lp_ctx, &lp_ctx->globals->dnsdomain, lower);
1110 return true;
1113 /***************************************************************************
1114 Handle the include operation.
1115 ***************************************************************************/
1117 static bool handle_include(struct loadparm_context *lp_ctx, int unused,
1118 const char *pszParmValue, char **ptr)
1120 char *fname = standard_sub_basic(lp_ctx, pszParmValue);
1122 add_to_file_list(lp_ctx, pszParmValue, fname);
1124 lpcfg_string_set(lp_ctx, ptr, fname);
1126 if (file_exist(fname))
1127 return pm_process(fname, do_section, do_parameter, lp_ctx);
1129 DEBUG(2, ("Can't find include file %s\n", fname));
1131 return false;
1134 /***************************************************************************
1135 Handle the interpretation of the copy parameter.
1136 ***************************************************************************/
1138 static bool handle_copy(struct loadparm_context *lp_ctx, int unused,
1139 const char *pszParmValue, char **ptr)
1141 bool bRetval;
1142 struct loadparm_service *serviceTemp;
1144 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1146 bRetval = false;
1148 DEBUG(3, ("Copying service from service %s\n", pszParmValue));
1150 if ((serviceTemp = getservicebyname(lp_ctx, pszParmValue)) != NULL) {
1151 if (serviceTemp == lp_ctx->currentService) {
1152 DEBUG(0, ("Can't copy service %s - unable to copy self!\n", pszParmValue));
1153 } else {
1154 copy_service(lp_ctx->currentService,
1155 serviceTemp,
1156 lp_ctx->currentService->copymap);
1157 bRetval = true;
1159 } else {
1160 DEBUG(0, ("Unable to copy service - source not found: %s\n",
1161 pszParmValue));
1162 bRetval = false;
1165 return bRetval;
1168 bool handle_debug_list(struct loadparm_context *lp_ctx, int unused,
1169 const char *pszParmValue, char **ptr)
1171 if (lp_ctx->s3_fns != NULL) {
1172 lp_ctx->s3_fns->lp_string_set(ptr, pszParmValue);
1173 } else {
1174 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1177 return debug_parse_levels(pszParmValue);
1180 bool handle_logfile(struct loadparm_context *lp_ctx, int unused,
1181 const char *pszParmValue, char **ptr)
1183 if (lp_ctx->s3_fns != NULL) {
1184 lp_ctx->s3_fns->lp_string_set(ptr, pszParmValue);
1185 } else {
1186 debug_set_logfile(pszParmValue);
1187 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1190 return true;
1193 /***************************************************************************
1194 Initialise a copymap.
1195 ***************************************************************************/
1197 static void init_copymap(struct loadparm_service *pservice)
1199 int i;
1201 TALLOC_FREE(pservice->copymap);
1203 pservice->copymap = bitmap_talloc(NULL, NUMPARAMETERS);
1204 if (!pservice->copymap)
1205 DEBUG(0,
1206 ("Couldn't allocate copymap!! (size %d)\n",
1207 (int)NUMPARAMETERS));
1208 else
1209 for (i = 0; i < NUMPARAMETERS; i++)
1210 bitmap_set(pservice->copymap, i);
1214 * Process a parametric option
1216 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
1217 struct loadparm_service *service,
1218 const char *pszParmName,
1219 const char *pszParmValue, int flags)
1221 struct parmlist_entry *paramo, *data;
1222 char *name;
1223 TALLOC_CTX *mem_ctx;
1225 while (isspace((unsigned char)*pszParmName)) {
1226 pszParmName++;
1229 name = strlower_talloc(lp_ctx, pszParmName);
1230 if (!name) return false;
1232 if (service == NULL) {
1233 data = lp_ctx->globals->param_opt;
1234 mem_ctx = lp_ctx->globals;
1235 } else {
1236 data = service->param_opt;
1237 mem_ctx = service;
1240 /* Traverse destination */
1241 for (paramo=data; paramo; paramo=paramo->next) {
1242 /* If we already have the option set, override it unless
1243 it was a command line option and the new one isn't */
1244 if (strcmp(paramo->key, name) == 0) {
1245 if ((paramo->priority & FLAG_CMDLINE) &&
1246 !(flags & FLAG_CMDLINE)) {
1247 talloc_free(name);
1248 return true;
1251 talloc_free(paramo->value);
1252 paramo->value = talloc_strdup(paramo, pszParmValue);
1253 paramo->priority = flags;
1254 talloc_free(name);
1255 return true;
1259 paramo = talloc_zero(mem_ctx, struct parmlist_entry);
1260 if (!paramo)
1261 smb_panic("OOM");
1262 paramo->key = talloc_strdup(paramo, name);
1263 paramo->value = talloc_strdup(paramo, pszParmValue);
1264 paramo->priority = flags;
1265 if (service == NULL) {
1266 DLIST_ADD(lp_ctx->globals->param_opt, paramo);
1267 } else {
1268 DLIST_ADD(service->param_opt, paramo);
1271 talloc_free(name);
1273 return true;
1276 static bool set_variable(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
1277 const char *pszParmName, const char *pszParmValue,
1278 struct loadparm_context *lp_ctx, bool on_globals)
1280 int i;
1281 /* if it is a special case then go ahead */
1282 if (parm_table[parmnum].special) {
1283 bool ret;
1284 ret = parm_table[parmnum].special(lp_ctx, -1, pszParmValue,
1285 (char **)parm_ptr);
1286 if (!ret) {
1287 return false;
1289 goto mark_non_default;
1292 /* now switch on the type of variable it is */
1293 switch (parm_table[parmnum].type)
1295 case P_BOOL: {
1296 bool b;
1297 if (!set_boolean(pszParmValue, &b)) {
1298 DEBUG(0, ("set_variable(%s): value is not "
1299 "boolean!\n", pszParmValue));
1300 return false;
1302 *(bool *)parm_ptr = b;
1304 break;
1306 case P_BOOLREV: {
1307 bool b;
1308 if (!set_boolean(pszParmValue, &b)) {
1309 DEBUG(0, ("set_variable(%s): value is not "
1310 "boolean!\n", pszParmValue));
1311 return false;
1313 *(bool *)parm_ptr = !b;
1315 break;
1317 case P_INTEGER:
1318 *(int *)parm_ptr = atoi(pszParmValue);
1319 break;
1321 case P_CHAR:
1322 *(char *)parm_ptr = *pszParmValue;
1323 break;
1325 case P_OCTAL:
1326 *(int *)parm_ptr = strtol(pszParmValue, NULL, 8);
1327 break;
1329 case P_BYTES:
1331 uint64_t val;
1332 if (conv_str_size_error(pszParmValue, &val)) {
1333 if (val <= INT_MAX) {
1334 *(int *)parm_ptr = (int)val;
1335 break;
1339 DEBUG(0, ("set_variable(%s): value is not "
1340 "a valid size specifier!\n", pszParmValue));
1341 return false;
1344 case P_CMDLIST:
1345 *(const char * const **)parm_ptr
1346 = (const char * const *)str_list_make(mem_ctx,
1347 pszParmValue, NULL);
1348 break;
1349 case P_LIST:
1351 char **new_list = str_list_make(mem_ctx,
1352 pszParmValue, NULL);
1353 for (i=0; new_list[i]; i++) {
1354 if (*(const char ***)parm_ptr != NULL &&
1355 new_list[i][0] == '+' &&
1356 new_list[i][1])
1358 if (!str_list_check(*(const char ***)parm_ptr,
1359 &new_list[i][1])) {
1360 *(const char ***)parm_ptr = str_list_add(*(const char ***)parm_ptr,
1361 &new_list[i][1]);
1363 } else if (*(const char ***)parm_ptr != NULL &&
1364 new_list[i][0] == '-' &&
1365 new_list[i][1])
1367 str_list_remove(*(const char ***)parm_ptr,
1368 &new_list[i][1]);
1369 } else {
1370 if (i != 0) {
1371 DEBUG(0, ("Unsupported list syntax for: %s = %s\n",
1372 pszParmName, pszParmValue));
1373 return false;
1375 *(const char * const **)parm_ptr = (const char * const *) new_list;
1376 break;
1379 break;
1381 case P_STRING:
1382 lpcfg_string_set(mem_ctx, (char **)parm_ptr, pszParmValue);
1383 break;
1385 case P_USTRING:
1386 lpcfg_string_set_upper(mem_ctx, (char **)parm_ptr, pszParmValue);
1387 break;
1389 case P_ENUM:
1390 for (i = 0; parm_table[parmnum].enum_list[i].name; i++) {
1391 if (strequal
1392 (pszParmValue,
1393 parm_table[parmnum].enum_list[i].name)) {
1394 *(int *)parm_ptr =
1395 parm_table[parmnum].
1396 enum_list[i].value;
1397 break;
1400 if (!parm_table[parmnum].enum_list[i].name) {
1401 DEBUG(0,("Unknown enumerated value '%s' for '%s'\n",
1402 pszParmValue, pszParmName));
1403 return false;
1405 break;
1407 case P_SEP:
1408 break;
1411 mark_non_default:
1412 if (on_globals && (lp_ctx->flags[parmnum] & FLAG_DEFAULT)) {
1413 lp_ctx->flags[parmnum] &= ~FLAG_DEFAULT;
1414 /* we have to also unset FLAG_DEFAULT on aliases */
1415 for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
1416 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1418 for (i=parmnum+1;i<NUMPARAMETERS && parm_table[i].offset == parm_table[parmnum].offset;i++) {
1419 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1422 return true;
1426 bool lpcfg_do_global_parameter(struct loadparm_context *lp_ctx,
1427 const char *pszParmName, const char *pszParmValue)
1429 int parmnum = lpcfg_map_parameter(pszParmName);
1430 void *parm_ptr;
1432 if (parmnum < 0) {
1433 if (strchr(pszParmName, ':')) {
1434 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName, pszParmValue, 0);
1436 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1437 return true;
1440 /* if the flag has been set on the command line, then don't allow override,
1441 but don't report an error */
1442 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1443 return true;
1446 parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[parmnum]);
1448 return set_variable(lp_ctx->globals, parmnum, parm_ptr,
1449 pszParmName, pszParmValue, lp_ctx, true);
1452 bool lpcfg_do_service_parameter(struct loadparm_context *lp_ctx,
1453 struct loadparm_service *service,
1454 const char *pszParmName, const char *pszParmValue)
1456 void *parm_ptr;
1457 int i;
1458 int parmnum = lpcfg_map_parameter(pszParmName);
1460 if (parmnum < 0) {
1461 if (strchr(pszParmName, ':')) {
1462 return lp_do_parameter_parametric(lp_ctx, service, pszParmName, pszParmValue, 0);
1464 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1465 return true;
1468 /* if the flag has been set on the command line, then don't allow override,
1469 but don't report an error */
1470 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1471 return true;
1474 if (parm_table[parmnum].p_class == P_GLOBAL) {
1475 DEBUG(0,
1476 ("Global parameter %s found in service section!\n",
1477 pszParmName));
1478 return true;
1480 parm_ptr = ((char *)service) + parm_table[parmnum].offset;
1482 if (!service->copymap)
1483 init_copymap(service);
1485 /* this handles the aliases - set the copymap for other
1486 * entries with the same data pointer */
1487 for (i = 0; parm_table[i].label; i++)
1488 if (parm_table[i].offset == parm_table[parmnum].offset &&
1489 parm_table[i].p_class == parm_table[parmnum].p_class)
1490 bitmap_clear(service->copymap, i);
1492 return set_variable(service, parmnum, parm_ptr, pszParmName,
1493 pszParmValue, lp_ctx, false);
1497 * Process a parameter.
1500 static bool do_parameter(const char *pszParmName, const char *pszParmValue,
1501 void *userdata)
1503 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1505 if (lp_ctx->bInGlobalSection)
1506 return lpcfg_do_global_parameter(lp_ctx, pszParmName,
1507 pszParmValue);
1508 else
1509 return lpcfg_do_service_parameter(lp_ctx, lp_ctx->currentService,
1510 pszParmName, pszParmValue);
1514 variable argument do parameter
1516 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx, const char *pszParmName, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
1517 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx,
1518 const char *pszParmName, const char *fmt, ...)
1520 char *s;
1521 bool ret;
1522 va_list ap;
1524 va_start(ap, fmt);
1525 s = talloc_vasprintf(NULL, fmt, ap);
1526 va_end(ap);
1527 ret = lpcfg_do_global_parameter(lp_ctx, pszParmName, s);
1528 talloc_free(s);
1529 return ret;
1534 set a parameter from the commandline - this is called from command line parameter
1535 parsing code. It sets the parameter then marks the parameter as unable to be modified
1536 by smb.conf processing
1538 bool lpcfg_set_cmdline(struct loadparm_context *lp_ctx, const char *pszParmName,
1539 const char *pszParmValue)
1541 int parmnum;
1542 int i;
1544 if (lp_ctx->s3_fns) {
1545 return lp_ctx->s3_fns->set_cmdline(pszParmName, pszParmValue);
1548 parmnum = lpcfg_map_parameter(pszParmName);
1550 while (isspace((unsigned char)*pszParmValue)) pszParmValue++;
1553 if (parmnum < 0 && strchr(pszParmName, ':')) {
1554 /* set a parametric option */
1555 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName,
1556 pszParmValue, FLAG_CMDLINE);
1559 if (parmnum < 0) {
1560 DEBUG(0,("Unknown option '%s'\n", pszParmName));
1561 return false;
1564 /* reset the CMDLINE flag in case this has been called before */
1565 lp_ctx->flags[parmnum] &= ~FLAG_CMDLINE;
1567 if (!lpcfg_do_global_parameter(lp_ctx, pszParmName, pszParmValue)) {
1568 return false;
1571 lp_ctx->flags[parmnum] |= FLAG_CMDLINE;
1573 /* we have to also set FLAG_CMDLINE on aliases */
1574 for (i=parmnum-1;
1575 i>=0 && parm_table[i].p_class == parm_table[parmnum].p_class &&
1576 parm_table[i].offset == parm_table[parmnum].offset;
1577 i--) {
1578 lp_ctx->flags[i] |= FLAG_CMDLINE;
1580 for (i=parmnum+1;
1581 i<NUMPARAMETERS &&
1582 parm_table[i].p_class == parm_table[parmnum].p_class &&
1583 parm_table[i].offset == parm_table[parmnum].offset;
1584 i++) {
1585 lp_ctx->flags[i] |= FLAG_CMDLINE;
1588 return true;
1592 set a option from the commandline in 'a=b' format. Use to support --option
1594 bool lpcfg_set_option(struct loadparm_context *lp_ctx, const char *option)
1596 char *p, *s;
1597 bool ret;
1599 s = talloc_strdup(NULL, option);
1600 if (!s) {
1601 return false;
1604 p = strchr(s, '=');
1605 if (!p) {
1606 talloc_free(s);
1607 return false;
1610 *p = 0;
1612 ret = lpcfg_set_cmdline(lp_ctx, s, p+1);
1613 talloc_free(s);
1614 return ret;
1618 #define BOOLSTR(b) ((b) ? "Yes" : "No")
1621 * Print a parameter of the specified type.
1624 void lpcfg_print_parameter(struct parm_struct *p, void *ptr, FILE * f)
1626 /* For the seperation of lists values that we print below */
1627 const char *list_sep = ", ";
1628 int i;
1629 switch (p->type)
1631 case P_ENUM:
1632 for (i = 0; p->enum_list[i].name; i++) {
1633 if (*(int *)ptr == p->enum_list[i].value) {
1634 fprintf(f, "%s",
1635 p->enum_list[i].name);
1636 break;
1639 break;
1641 case P_BOOL:
1642 fprintf(f, "%s", BOOLSTR(*(bool *)ptr));
1643 break;
1645 case P_BOOLREV:
1646 fprintf(f, "%s", BOOLSTR(!*(bool *)ptr));
1647 break;
1649 case P_INTEGER:
1650 case P_BYTES:
1651 fprintf(f, "%d", *(int *)ptr);
1652 break;
1654 case P_CHAR:
1655 fprintf(f, "%c", *(char *)ptr);
1656 break;
1658 case P_OCTAL: {
1659 int val = *(int *)ptr;
1660 if (val == -1) {
1661 fprintf(f, "-1");
1662 } else {
1663 fprintf(f, "0%03o", val);
1665 break;
1668 case P_CMDLIST:
1669 list_sep = " ";
1670 /* fall through */
1671 case P_LIST:
1672 if ((char ***)ptr && *(char ***)ptr) {
1673 char **list = *(char ***)ptr;
1674 for (; *list; list++) {
1675 /* surround strings with whitespace in double quotes */
1676 if (*(list+1) == NULL) {
1677 /* last item, no extra separator */
1678 list_sep = "";
1680 if ( strchr_m( *list, ' ' ) ) {
1681 fprintf(f, "\"%s\"%s", *list, list_sep);
1682 } else {
1683 fprintf(f, "%s%s", *list, list_sep);
1687 break;
1689 case P_STRING:
1690 case P_USTRING:
1691 if (*(char **)ptr) {
1692 fprintf(f, "%s", *(char **)ptr);
1694 break;
1695 case P_SEP:
1696 break;
1701 * Check if two parameters are equal.
1704 bool lpcfg_equal_parameter(parm_type type, void *ptr1, void *ptr2)
1706 switch (type) {
1707 case P_BOOL:
1708 case P_BOOLREV:
1709 return (*((bool *)ptr1) == *((bool *)ptr2));
1711 case P_INTEGER:
1712 case P_ENUM:
1713 case P_OCTAL:
1714 case P_BYTES:
1715 return (*((int *)ptr1) == *((int *)ptr2));
1717 case P_CHAR:
1718 return (*((char *)ptr1) == *((char *)ptr2));
1720 case P_LIST:
1721 case P_CMDLIST:
1722 return str_list_equal(*(const char ***)ptr1, *(const char ***)ptr2);
1724 case P_STRING:
1725 case P_USTRING:
1727 char *p1 = *(char **)ptr1, *p2 = *(char **)ptr2;
1728 if (p1 && !*p1)
1729 p1 = NULL;
1730 if (p2 && !*p2)
1731 p2 = NULL;
1732 return (p1 == p2 || strequal(p1, p2));
1734 case P_SEP:
1735 break;
1737 return false;
1741 * Process a new section (service).
1743 * At this stage all sections are services.
1744 * Later we'll have special sections that permit server parameters to be set.
1745 * Returns True on success, False on failure.
1748 static bool do_section(const char *pszSectionName, void *userdata)
1750 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1751 bool bRetval;
1752 bool isglobal = ((strwicmp(pszSectionName, GLOBAL_NAME) == 0) ||
1753 (strwicmp(pszSectionName, GLOBAL_NAME2) == 0));
1754 bRetval = false;
1756 /* if we've just struck a global section, note the fact. */
1757 lp_ctx->bInGlobalSection = isglobal;
1759 /* check for multiple global sections */
1760 if (lp_ctx->bInGlobalSection) {
1761 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1762 return true;
1765 /* if we have a current service, tidy it up before moving on */
1766 bRetval = true;
1768 if (lp_ctx->currentService != NULL)
1769 bRetval = lpcfg_service_ok(lp_ctx->currentService);
1771 /* if all is still well, move to the next record in the services array */
1772 if (bRetval) {
1773 /* We put this here to avoid an odd message order if messages are */
1774 /* issued by the post-processing of a previous section. */
1775 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1777 if ((lp_ctx->currentService = lpcfg_add_service(lp_ctx, lp_ctx->sDefault,
1778 pszSectionName))
1779 == NULL) {
1780 DEBUG(0, ("Failed to add a new service\n"));
1781 return false;
1785 return bRetval;
1790 * Determine if a particular base parameter is currently set to the default value.
1793 static bool is_default(struct loadparm_service *sDefault, int i)
1795 void *def_ptr = ((char *)sDefault) + parm_table[i].offset;
1796 switch (parm_table[i].type) {
1797 case P_CMDLIST:
1798 case P_LIST:
1799 return str_list_equal((const char * const *)parm_table[i].def.lvalue,
1800 (const char **)def_ptr);
1801 case P_STRING:
1802 case P_USTRING:
1803 return strequal(parm_table[i].def.svalue,
1804 *(char **)def_ptr);
1805 case P_BOOL:
1806 case P_BOOLREV:
1807 return parm_table[i].def.bvalue ==
1808 *(bool *)def_ptr;
1809 case P_INTEGER:
1810 case P_CHAR:
1811 case P_OCTAL:
1812 case P_BYTES:
1813 case P_ENUM:
1814 return parm_table[i].def.ivalue ==
1815 *(int *)def_ptr;
1816 case P_SEP:
1817 break;
1819 return false;
1823 *Display the contents of the global structure.
1826 static void dump_globals(struct loadparm_context *lp_ctx, FILE *f,
1827 bool show_defaults)
1829 int i;
1830 struct parmlist_entry *data;
1832 fprintf(f, "# Global parameters\n[global]\n");
1834 for (i = 0; parm_table[i].label; i++)
1835 if (parm_table[i].p_class == P_GLOBAL &&
1836 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset))) {
1837 if (!show_defaults && (lp_ctx->flags[i] & FLAG_DEFAULT))
1838 continue;
1839 fprintf(f, "\t%s = ", parm_table[i].label);
1840 lpcfg_print_parameter(&parm_table[i], lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[i]), f);
1841 fprintf(f, "\n");
1843 if (lp_ctx->globals->param_opt != NULL) {
1844 for (data = lp_ctx->globals->param_opt; data;
1845 data = data->next) {
1846 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
1847 continue;
1849 fprintf(f, "\t%s = %s\n", data->key, data->value);
1856 * Display the contents of a single services record.
1859 static void dump_a_service(struct loadparm_service * pService, struct loadparm_service *sDefault, FILE * f,
1860 unsigned int *flags)
1862 int i;
1863 struct parmlist_entry *data;
1865 if (pService != sDefault)
1866 fprintf(f, "\n[%s]\n", pService->szService);
1868 for (i = 0; parm_table[i].label; i++) {
1869 if (parm_table[i].p_class == P_LOCAL &&
1870 (*parm_table[i].label != '-') &&
1871 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset)))
1873 if (pService == sDefault) {
1874 if (flags && (flags[i] & FLAG_DEFAULT)) {
1875 continue;
1877 if (defaults_saved) {
1878 if (is_default(sDefault, i)) {
1879 continue;
1882 } else {
1883 if (lpcfg_equal_parameter(parm_table[i].type,
1884 ((char *)pService) +
1885 parm_table[i].offset,
1886 ((char *)sDefault) +
1887 parm_table[i].offset))
1888 continue;
1891 fprintf(f, "\t%s = ", parm_table[i].label);
1892 lpcfg_print_parameter(&parm_table[i],
1893 ((char *)pService) + parm_table[i].offset, f);
1894 fprintf(f, "\n");
1897 if (pService->param_opt != NULL) {
1898 for (data = pService->param_opt; data; data = data->next) {
1899 fprintf(f, "\t%s = %s\n", data->key, data->value);
1904 bool lpcfg_dump_a_parameter(struct loadparm_context *lp_ctx,
1905 struct loadparm_service *service,
1906 const char *parm_name, FILE * f)
1908 struct parm_struct *parm;
1909 void *ptr;
1911 parm = lpcfg_parm_struct(lp_ctx, parm_name);
1912 if (!parm) {
1913 return false;
1916 if (service != NULL && parm->p_class == P_GLOBAL) {
1917 return false;
1920 ptr = lpcfg_parm_ptr(lp_ctx, service,parm);
1922 lpcfg_print_parameter(parm, ptr, f);
1923 fprintf(f, "\n");
1924 return true;
1928 * Auto-load some home services.
1930 static void lpcfg_add_auto_services(struct loadparm_context *lp_ctx,
1931 const char *str)
1933 return;
1938 * Unload unused services.
1941 void lpcfg_killunused(struct loadparm_context *lp_ctx,
1942 struct smbsrv_connection *smb,
1943 bool (*snumused) (struct smbsrv_connection *, int))
1945 int i;
1946 for (i = 0; i < lp_ctx->iNumServices; i++) {
1947 if (lp_ctx->services[i] == NULL)
1948 continue;
1950 if (!snumused || !snumused(smb, i)) {
1951 talloc_free(lp_ctx->services[i]);
1952 lp_ctx->services[i] = NULL;
1958 static int lpcfg_destructor(struct loadparm_context *lp_ctx)
1960 struct parmlist_entry *data;
1962 if (lp_ctx->refuse_free) {
1963 /* someone is trying to free the
1964 global_loadparm_context.
1965 We can't allow that. */
1966 return -1;
1969 if (lp_ctx->globals->param_opt != NULL) {
1970 struct parmlist_entry *next;
1971 for (data = lp_ctx->globals->param_opt; data; data=next) {
1972 next = data->next;
1973 if (data->priority & FLAG_CMDLINE) continue;
1974 DLIST_REMOVE(lp_ctx->globals->param_opt, data);
1975 talloc_free(data);
1979 return 0;
1983 * Initialise the global parameter structure.
1985 * Note that most callers should use loadparm_init_global() instead
1987 struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
1989 int i;
1990 char *myname;
1991 struct loadparm_context *lp_ctx;
1992 struct parmlist_entry *parm;
1993 char *logfile;
1995 lp_ctx = talloc_zero(mem_ctx, struct loadparm_context);
1996 if (lp_ctx == NULL)
1997 return NULL;
1999 talloc_set_destructor(lp_ctx, lpcfg_destructor);
2000 lp_ctx->bInGlobalSection = true;
2001 lp_ctx->globals = talloc_zero(lp_ctx, struct loadparm_global);
2002 lp_ctx->sDefault = talloc_zero(lp_ctx, struct loadparm_service);
2004 lp_ctx->sDefault->iMaxPrintJobs = 1000;
2005 lp_ctx->sDefault->bAvailable = true;
2006 lp_ctx->sDefault->browseable = true;
2007 lp_ctx->sDefault->read_only = true;
2008 lp_ctx->sDefault->map_archive = true;
2009 lp_ctx->sDefault->strict_locking = true;
2010 lp_ctx->sDefault->oplocks = true;
2011 lp_ctx->sDefault->create_mask = 0744;
2012 lp_ctx->sDefault->force_create_mode = 0000;
2013 lp_ctx->sDefault->directory_mask = 0755;
2014 lp_ctx->sDefault->force_directory_mode = 0000;
2016 DEBUG(3, ("Initialising global parameters\n"));
2018 for (i = 0; parm_table[i].label; i++) {
2019 if ((parm_table[i].type == P_STRING ||
2020 parm_table[i].type == P_USTRING) &&
2021 !(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2022 char **r;
2023 if (parm_table[i].p_class == P_LOCAL) {
2024 r = (char **)(((char *)lp_ctx->sDefault) + parm_table[i].offset);
2025 } else {
2026 r = (char **)(((char *)lp_ctx->globals) + parm_table[i].offset);
2028 *r = talloc_strdup(lp_ctx, "");
2032 logfile = talloc_asprintf(lp_ctx, "%s/log.samba", dyn_LOGFILEBASE);
2033 lpcfg_do_global_parameter(lp_ctx, "log file", logfile);
2034 talloc_free(logfile);
2036 lpcfg_do_global_parameter(lp_ctx, "log level", "0");
2038 lpcfg_do_global_parameter(lp_ctx, "syslog", "1");
2039 lpcfg_do_global_parameter(lp_ctx, "syslog only", "No");
2040 lpcfg_do_global_parameter(lp_ctx, "debug timestamp", "Yes");
2041 lpcfg_do_global_parameter(lp_ctx, "debug prefix timestamp", "No");
2042 lpcfg_do_global_parameter(lp_ctx, "debug hires timestamp", "Yes");
2043 lpcfg_do_global_parameter(lp_ctx, "debug pid", "No");
2044 lpcfg_do_global_parameter(lp_ctx, "debug uid", "No");
2045 lpcfg_do_global_parameter(lp_ctx, "debug class", "No");
2047 lpcfg_do_global_parameter(lp_ctx, "share backend", "classic");
2049 lpcfg_do_global_parameter(lp_ctx, "server role", "auto");
2050 lpcfg_do_global_parameter(lp_ctx, "domain logons", "No");
2051 lpcfg_do_global_parameter(lp_ctx, "domain master", "Auto");
2053 /* options that can be set on the command line must be initialised via
2054 the slower lpcfg_do_global_parameter() to ensure that FLAG_CMDLINE is obeyed */
2055 #ifdef TCP_NODELAY
2056 lpcfg_do_global_parameter(lp_ctx, "socket options", "TCP_NODELAY");
2057 #endif
2058 lpcfg_do_global_parameter(lp_ctx, "workgroup", DEFAULT_WORKGROUP);
2059 myname = get_myname(lp_ctx);
2060 lpcfg_do_global_parameter(lp_ctx, "netbios name", myname);
2061 talloc_free(myname);
2062 lpcfg_do_global_parameter(lp_ctx, "name resolve order", "lmhosts wins host bcast");
2064 lpcfg_do_global_parameter(lp_ctx, "fstype", "NTFS");
2066 lpcfg_do_global_parameter(lp_ctx, "ntvfs handler", "unixuid default");
2067 lpcfg_do_global_parameter(lp_ctx, "max connections", "0");
2069 lpcfg_do_global_parameter(lp_ctx, "dcerpc endpoint servers", "epmapper wkssvc rpcecho samr netlogon lsarpc spoolss drsuapi dssetup unixinfo browser eventlog6 backupkey dnsserver");
2070 lpcfg_do_global_parameter(lp_ctx, "server services", "s3fs rpc nbt wrepl ldap cldap kdc drepl winbind ntp_signd kcc dnsupdate dns");
2071 lpcfg_do_global_parameter(lp_ctx, "kccsrv:samba_kcc", "true");
2072 /* the winbind method for domain controllers is for both RODC
2073 auth forwarding and for trusted domains */
2074 lpcfg_do_global_parameter(lp_ctx, "private dir", dyn_PRIVATE_DIR);
2075 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_LOCAL_MACHINE", "hklm.ldb");
2077 /* This hive should be dynamically generated by Samba using
2078 data from the sam, but for the moment leave it in a tdb to
2079 keep regedt32 from popping up an annoying dialog. */
2080 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_USERS", "hku.ldb");
2082 /* using UTF8 by default allows us to support all chars */
2083 lpcfg_do_global_parameter(lp_ctx, "unix charset", "UTF-8");
2085 /* Use codepage 850 as a default for the dos character set */
2086 lpcfg_do_global_parameter(lp_ctx, "dos charset", "CP850");
2089 * Allow the default PASSWD_CHAT to be overridden in local.h.
2091 lpcfg_do_global_parameter(lp_ctx, "passwd chat", DEFAULT_PASSWD_CHAT);
2093 lpcfg_do_global_parameter(lp_ctx, "pid directory", dyn_PIDDIR);
2094 lpcfg_do_global_parameter(lp_ctx, "lock dir", dyn_LOCKDIR);
2095 lpcfg_do_global_parameter(lp_ctx, "state directory", dyn_STATEDIR);
2096 lpcfg_do_global_parameter(lp_ctx, "cache directory", dyn_CACHEDIR);
2097 lpcfg_do_global_parameter(lp_ctx, "ncalrpc dir", dyn_NCALRPCDIR);
2099 lpcfg_do_global_parameter(lp_ctx, "nbt client socket address", "0.0.0.0");
2100 lpcfg_do_global_parameter_var(lp_ctx, "server string",
2101 "Samba %s", SAMBA_VERSION_STRING);
2103 lpcfg_do_global_parameter(lp_ctx, "password server", "*");
2105 lpcfg_do_global_parameter(lp_ctx, "max mux", "50");
2106 lpcfg_do_global_parameter(lp_ctx, "max xmit", "16644");
2107 lpcfg_do_global_parameter(lp_ctx, "host msdfs", "true");
2109 lpcfg_do_global_parameter(lp_ctx, "LargeReadwrite", "True");
2110 lpcfg_do_global_parameter(lp_ctx, "server min protocol", "LANMAN1");
2111 lpcfg_do_global_parameter(lp_ctx, "server max protocol", "SMB3");
2112 lpcfg_do_global_parameter(lp_ctx, "client min protocol", "CORE");
2113 lpcfg_do_global_parameter(lp_ctx, "client max protocol", "NT1");
2114 lpcfg_do_global_parameter(lp_ctx, "security", "AUTO");
2115 lpcfg_do_global_parameter(lp_ctx, "EncryptPasswords", "True");
2116 lpcfg_do_global_parameter(lp_ctx, "ReadRaw", "True");
2117 lpcfg_do_global_parameter(lp_ctx, "WriteRaw", "True");
2118 lpcfg_do_global_parameter(lp_ctx, "NullPasswords", "False");
2119 lpcfg_do_global_parameter(lp_ctx, "old password allowed period", "60");
2120 lpcfg_do_global_parameter(lp_ctx, "ObeyPamRestrictions", "False");
2122 lpcfg_do_global_parameter(lp_ctx, "TimeServer", "False");
2123 lpcfg_do_global_parameter(lp_ctx, "BindInterfacesOnly", "False");
2124 lpcfg_do_global_parameter(lp_ctx, "Unicode", "True");
2125 lpcfg_do_global_parameter(lp_ctx, "ClientLanManAuth", "False");
2126 lpcfg_do_global_parameter(lp_ctx, "ClientNTLMv2Auth", "True");
2127 lpcfg_do_global_parameter(lp_ctx, "LanmanAuth", "False");
2128 lpcfg_do_global_parameter(lp_ctx, "NTLMAuth", "True");
2129 lpcfg_do_global_parameter(lp_ctx, "client use spnego principal", "False");
2131 lpcfg_do_global_parameter(lp_ctx, "UnixExtensions", "True");
2133 lpcfg_do_global_parameter(lp_ctx, "PreferredMaster", "Auto");
2134 lpcfg_do_global_parameter(lp_ctx, "LocalMaster", "True");
2136 lpcfg_do_global_parameter(lp_ctx, "wins support", "False");
2137 lpcfg_do_global_parameter(lp_ctx, "dns proxy", "True");
2139 lpcfg_do_global_parameter(lp_ctx, "winbind separator", "\\");
2140 lpcfg_do_global_parameter(lp_ctx, "winbind sealed pipes", "True");
2141 lpcfg_do_global_parameter(lp_ctx, "require strong key", "True");
2142 lpcfg_do_global_parameter(lp_ctx, "winbindd socket directory", dyn_WINBINDD_SOCKET_DIR);
2143 lpcfg_do_global_parameter(lp_ctx, "winbindd privileged socket directory", dyn_WINBINDD_PRIVILEGED_SOCKET_DIR);
2144 lpcfg_do_global_parameter(lp_ctx, "ntp signd socket directory", dyn_NTP_SIGND_SOCKET_DIR);
2145 lpcfg_do_global_parameter_var(lp_ctx, "dns update command", "%s/samba_dnsupdate", dyn_SCRIPTSBINDIR);
2146 lpcfg_do_global_parameter_var(lp_ctx, "spn update command", "%s/samba_spnupdate", dyn_SCRIPTSBINDIR);
2147 lpcfg_do_global_parameter_var(lp_ctx, "samba kcc command",
2148 "%s/samba_kcc", dyn_SCRIPTSBINDIR);
2149 lpcfg_do_global_parameter(lp_ctx, "template shell", "/bin/false");
2150 lpcfg_do_global_parameter(lp_ctx, "template homedir", "/home/%WORKGROUP%/%ACCOUNTNAME%");
2152 lpcfg_do_global_parameter(lp_ctx, "client signing", "default");
2153 lpcfg_do_global_parameter(lp_ctx, "server signing", "default");
2155 lpcfg_do_global_parameter(lp_ctx, "use spnego", "True");
2157 lpcfg_do_global_parameter(lp_ctx, "use mmap", "True");
2159 lpcfg_do_global_parameter(lp_ctx, "smb ports", "445 139");
2160 lpcfg_do_global_parameter(lp_ctx, "nbt port", "137");
2161 lpcfg_do_global_parameter(lp_ctx, "dgram port", "138");
2162 lpcfg_do_global_parameter(lp_ctx, "cldap port", "389");
2163 lpcfg_do_global_parameter(lp_ctx, "krb5 port", "88");
2164 lpcfg_do_global_parameter(lp_ctx, "kpasswd port", "464");
2165 lpcfg_do_global_parameter(lp_ctx, "web port", "901");
2167 lpcfg_do_global_parameter(lp_ctx, "nt status support", "True");
2169 lpcfg_do_global_parameter(lp_ctx, "max wins ttl", "518400"); /* 6 days */
2170 lpcfg_do_global_parameter(lp_ctx, "min wins ttl", "21600");
2172 lpcfg_do_global_parameter(lp_ctx, "tls enabled", "True");
2173 lpcfg_do_global_parameter(lp_ctx, "tls keyfile", "tls/key.pem");
2174 lpcfg_do_global_parameter(lp_ctx, "tls certfile", "tls/cert.pem");
2175 lpcfg_do_global_parameter(lp_ctx, "tls cafile", "tls/ca.pem");
2176 lpcfg_do_global_parameter(lp_ctx, "prefork children:smb", "4");
2178 lpcfg_do_global_parameter(lp_ctx, "rndc command", "/usr/sbin/rndc");
2179 lpcfg_do_global_parameter(lp_ctx, "nsupdate command", "/usr/bin/nsupdate -g");
2181 lpcfg_do_global_parameter(lp_ctx, "allow dns updates", "secure only");
2182 lpcfg_do_global_parameter(lp_ctx, "dns forwarder", "");
2184 lpcfg_do_global_parameter(lp_ctx, "algorithmic rid base", "1000");
2186 lpcfg_do_global_parameter(lp_ctx, "enhanced browsing", "True");
2188 lpcfg_do_global_parameter(lp_ctx, "winbind nss info", "template");
2190 lpcfg_do_global_parameter(lp_ctx, "server schannel", "Auto");
2192 lpcfg_do_global_parameter(lp_ctx, "short preserve case", "True");
2194 lpcfg_do_global_parameter(lp_ctx, "max open files", "16384");
2196 lpcfg_do_global_parameter(lp_ctx, "cups connection timeout", "30");
2198 lpcfg_do_global_parameter(lp_ctx, "locking", "True");
2200 lpcfg_do_global_parameter(lp_ctx, "block size", "1024");
2202 lpcfg_do_global_parameter(lp_ctx, "client use spnego", "True");
2204 lpcfg_do_global_parameter(lp_ctx, "change notify", "True");
2206 lpcfg_do_global_parameter(lp_ctx, "name cache timeout", "660");
2208 lpcfg_do_global_parameter(lp_ctx, "defer sharing violations", "True");
2210 lpcfg_do_global_parameter(lp_ctx, "ldap replication sleep", "1000");
2212 lpcfg_do_global_parameter(lp_ctx, "idmap backend", "tdb");
2214 lpcfg_do_global_parameter(lp_ctx, "enable privileges", "True");
2216 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max write", "%u", DEFAULT_SMB2_MAX_WRITE);
2218 lpcfg_do_global_parameter(lp_ctx, "passdb backend", "tdbsam");
2220 lpcfg_do_global_parameter(lp_ctx, "getwd cache", "True");
2222 lpcfg_do_global_parameter(lp_ctx, "winbind nested groups", "True");
2224 lpcfg_do_global_parameter(lp_ctx, "mangled names", "True");
2226 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max credits", "%u", DEFAULT_SMB2_MAX_CREDITS);
2228 lpcfg_do_global_parameter(lp_ctx, "ldap ssl", "start tls");
2230 lpcfg_do_global_parameter(lp_ctx, "ldap deref", "auto");
2232 lpcfg_do_global_parameter(lp_ctx, "lm interval", "60");
2234 lpcfg_do_global_parameter(lp_ctx, "mangling method", "hash2");
2236 lpcfg_do_global_parameter(lp_ctx, "hide dot files", "True");
2238 lpcfg_do_global_parameter(lp_ctx, "browse list", "True");
2240 lpcfg_do_global_parameter(lp_ctx, "passwd chat timeout", "2");
2242 lpcfg_do_global_parameter(lp_ctx, "guest account", GUEST_ACCOUNT);
2244 lpcfg_do_global_parameter(lp_ctx, "client schannel", "auto");
2246 lpcfg_do_global_parameter(lp_ctx, "smb encrypt", "default");
2248 lpcfg_do_global_parameter(lp_ctx, "max log size", "5000");
2250 lpcfg_do_global_parameter(lp_ctx, "idmap negative cache time", "120");
2252 lpcfg_do_global_parameter(lp_ctx, "ldap follow referral", "auto");
2254 lpcfg_do_global_parameter(lp_ctx, "multicast dns register", "yes");
2256 lpcfg_do_global_parameter(lp_ctx, "winbind reconnect delay", "30");
2258 lpcfg_do_global_parameter(lp_ctx, "nt acl support", "yes");
2260 lpcfg_do_global_parameter(lp_ctx, "acl check permissions", "yes");
2262 lpcfg_do_global_parameter(lp_ctx, "keepalive", "300");
2264 lpcfg_do_global_parameter(lp_ctx, "winbind cache time", "300");
2266 lpcfg_do_global_parameter(lp_ctx, "level2 oplocks", "yes");
2268 lpcfg_do_global_parameter(lp_ctx, "show add printer wizard", "yes");
2270 lpcfg_do_global_parameter(lp_ctx, "allocation roundup size", "1048576");
2272 lpcfg_do_global_parameter(lp_ctx, "ldap page size", "1024");
2274 lpcfg_do_global_parameter(lp_ctx, "kernel share modes", "yes");
2276 lpcfg_do_global_parameter(lp_ctx, "strict locking", "Auto");
2278 lpcfg_do_global_parameter(lp_ctx, "map readonly", "yes");
2280 lpcfg_do_global_parameter(lp_ctx, "allow trusted domains", "yes");
2282 lpcfg_do_global_parameter(lp_ctx, "default devmode", "yes");
2284 lpcfg_do_global_parameter(lp_ctx, "os level", "20");
2286 lpcfg_do_global_parameter(lp_ctx, "dos filetimes", "yes");
2288 lpcfg_do_global_parameter(lp_ctx, "mangling char", "~");
2290 lpcfg_do_global_parameter(lp_ctx, "printcap cache time", "750");
2292 lpcfg_do_global_parameter(lp_ctx, "create krb5 conf", "yes");
2294 lpcfg_do_global_parameter(lp_ctx, "winbind max clients", "200");
2296 lpcfg_do_global_parameter(lp_ctx, "acl map full control", "yes");
2298 lpcfg_do_global_parameter(lp_ctx, "nt pipe support", "yes");
2300 lpcfg_do_global_parameter(lp_ctx, "ldap debug threshold", "10");
2302 lpcfg_do_global_parameter(lp_ctx, "follow symlinks", "yes");
2304 lpcfg_do_global_parameter(lp_ctx, "machine password timeout", "604800");
2306 lpcfg_do_global_parameter(lp_ctx, "ldap connection timeout", "2");
2308 lpcfg_do_global_parameter(lp_ctx, "winbind expand groups", "1");
2310 lpcfg_do_global_parameter(lp_ctx, "stat cache", "yes");
2312 lpcfg_do_global_parameter(lp_ctx, "lpq cache time", "30");
2314 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max trans", "%u", DEFAULT_SMB2_MAX_TRANSACT);
2316 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max read", "%u", DEFAULT_SMB2_MAX_READ);
2318 lpcfg_do_global_parameter(lp_ctx, "durable handles", "yes");
2320 lpcfg_do_global_parameter(lp_ctx, "max stat cache size", "256");
2322 lpcfg_do_global_parameter(lp_ctx, "ldap passwd sync", "no");
2324 lpcfg_do_global_parameter(lp_ctx, "kernel change notify", "yes");
2326 lpcfg_do_global_parameter(lp_ctx, "max ttl", "259200");
2328 lpcfg_do_global_parameter(lp_ctx, "blocking locks", "yes");
2330 lpcfg_do_global_parameter(lp_ctx, "oplock contention limit", "2");
2332 lpcfg_do_global_parameter(lp_ctx, "load printers", "yes");
2334 lpcfg_do_global_parameter(lp_ctx, "idmap cache time", "604800");
2336 lpcfg_do_global_parameter(lp_ctx, "preserve case", "yes");
2338 lpcfg_do_global_parameter(lp_ctx, "lm announce", "auto");
2340 lpcfg_do_global_parameter(lp_ctx, "afs token lifetime", "604800");
2342 lpcfg_do_global_parameter(lp_ctx, "enable core files", "yes");
2344 lpcfg_do_global_parameter(lp_ctx, "winbind max domain connections", "1");
2346 lpcfg_do_global_parameter(lp_ctx, "case sensitive", "auto");
2348 lpcfg_do_global_parameter(lp_ctx, "ldap timeout", "15");
2350 lpcfg_do_global_parameter(lp_ctx, "mangle prefix", "1");
2352 lpcfg_do_global_parameter(lp_ctx, "posix locking", "yes");
2354 lpcfg_do_global_parameter(lp_ctx, "lock spin time", "200");
2356 lpcfg_do_global_parameter(lp_ctx, "directory name cache size", "100");
2358 lpcfg_do_global_parameter(lp_ctx, "nmbd bind explicit broadcast", "yes");
2360 lpcfg_do_global_parameter(lp_ctx, "init logon delay", "100");
2362 lpcfg_do_global_parameter(lp_ctx, "usershare owner only", "yes");
2364 lpcfg_do_global_parameter(lp_ctx, "-valid", "yes");
2366 lpcfg_do_global_parameter_var(lp_ctx, "usershare path", "%s/usershares", get_dyn_STATEDIR());
2368 #ifdef DEVELOPER
2369 lpcfg_do_global_parameter_var(lp_ctx, "panic action", "/bin/sleep 999999999");
2370 #endif
2372 lpcfg_do_global_parameter(lp_ctx, "smb passwd file", get_dyn_SMB_PASSWD_FILE());
2374 lpcfg_do_global_parameter(lp_ctx, "logon home", "\\\\%N\\%U");
2376 lpcfg_do_global_parameter(lp_ctx, "logon path", "\\\\%N\\%U\\profile");
2378 lpcfg_do_global_parameter(lp_ctx, "printjob username", "%U");
2380 for (i = 0; parm_table[i].label; i++) {
2381 if (!(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2382 lp_ctx->flags[i] |= FLAG_DEFAULT;
2386 for (parm=lp_ctx->globals->param_opt; parm; parm=parm->next) {
2387 if (!(parm->priority & FLAG_CMDLINE)) {
2388 parm->priority |= FLAG_DEFAULT;
2392 return lp_ctx;
2396 * Initialise the global parameter structure.
2398 struct loadparm_context *loadparm_init_global(bool load_default)
2400 if (global_loadparm_context == NULL) {
2401 global_loadparm_context = loadparm_init(NULL);
2403 if (global_loadparm_context == NULL) {
2404 return NULL;
2406 global_loadparm_context->global = true;
2407 if (load_default && !global_loadparm_context->loaded) {
2408 lpcfg_load_default(global_loadparm_context);
2410 global_loadparm_context->refuse_free = true;
2411 return global_loadparm_context;
2415 * Initialise the global parameter structure.
2417 struct loadparm_context *loadparm_init_s3(TALLOC_CTX *mem_ctx,
2418 const struct loadparm_s3_helpers *s3_fns)
2420 struct loadparm_context *loadparm_context = talloc_zero(mem_ctx, struct loadparm_context);
2421 if (!loadparm_context) {
2422 return NULL;
2424 loadparm_context->s3_fns = s3_fns;
2425 loadparm_context->globals = s3_fns->globals;
2426 return loadparm_context;
2429 const char *lpcfg_configfile(struct loadparm_context *lp_ctx)
2431 return lp_ctx->szConfigFile;
2434 const char *lp_default_path(void)
2436 if (getenv("SMB_CONF_PATH"))
2437 return getenv("SMB_CONF_PATH");
2438 else
2439 return dyn_CONFIGFILE;
2443 * Update the internal state of a loadparm context after settings
2444 * have changed.
2446 static bool lpcfg_update(struct loadparm_context *lp_ctx)
2448 struct debug_settings settings;
2449 TALLOC_CTX *tmp_ctx;
2451 tmp_ctx = talloc_new(lp_ctx);
2452 if (tmp_ctx == NULL) {
2453 return false;
2456 lpcfg_add_auto_services(lp_ctx, lpcfg_auto_services(lp_ctx, tmp_ctx));
2458 if (!lp_ctx->globals->wins_server_list && lp_ctx->globals->we_are_a_wins_server) {
2459 lpcfg_do_global_parameter(lp_ctx, "wins server", "127.0.0.1");
2462 if (!lp_ctx->global) {
2463 TALLOC_FREE(tmp_ctx);
2464 return true;
2467 panic_action = lp_ctx->globals->panic_action;
2469 reload_charcnv(lp_ctx);
2471 ZERO_STRUCT(settings);
2472 /* Add any more debug-related smb.conf parameters created in
2473 * future here */
2474 settings.syslog = lp_ctx->globals->syslog;
2475 settings.syslog_only = lp_ctx->globals->syslog_only;
2476 settings.timestamp_logs = lp_ctx->globals->timestamp_logs;
2477 settings.debug_prefix_timestamp = lp_ctx->globals->debug_prefix_timestamp;
2478 settings.debug_hires_timestamp = lp_ctx->globals->debug_hires_timestamp;
2479 settings.debug_pid = lp_ctx->globals->debug_pid;
2480 settings.debug_uid = lp_ctx->globals->debug_uid;
2481 settings.debug_class = lp_ctx->globals->debug_class;
2482 debug_set_settings(&settings);
2484 /* FIXME: This is a bit of a hack, but we can't use a global, since
2485 * not everything that uses lp also uses the socket library */
2486 if (lpcfg_parm_bool(lp_ctx, NULL, "socket", "testnonblock", false)) {
2487 setenv("SOCKET_TESTNONBLOCK", "1", 1);
2488 } else {
2489 unsetenv("SOCKET_TESTNONBLOCK");
2492 TALLOC_FREE(tmp_ctx);
2493 return true;
2496 bool lpcfg_load_default(struct loadparm_context *lp_ctx)
2498 const char *path;
2500 path = lp_default_path();
2502 if (!file_exist(path)) {
2503 /* We allow the default smb.conf file to not exist,
2504 * basically the equivalent of an empty file. */
2505 return lpcfg_update(lp_ctx);
2508 return lpcfg_load(lp_ctx, path);
2512 * Load the services array from the services file.
2514 * Return True on success, False on failure.
2516 bool lpcfg_load(struct loadparm_context *lp_ctx, const char *filename)
2518 char *n2;
2519 bool bRetval;
2521 filename = talloc_strdup(lp_ctx, filename);
2523 lp_ctx->szConfigFile = filename;
2525 if (lp_ctx->s3_fns) {
2526 return lp_ctx->s3_fns->load(filename);
2529 lp_ctx->bInGlobalSection = true;
2530 n2 = standard_sub_basic(lp_ctx, lp_ctx->szConfigFile);
2531 DEBUG(2, ("lpcfg_load: refreshing parameters from %s\n", n2));
2533 add_to_file_list(lp_ctx, lp_ctx->szConfigFile, n2);
2535 /* We get sections first, so have to start 'behind' to make up */
2536 lp_ctx->currentService = NULL;
2537 bRetval = pm_process(n2, do_section, do_parameter, lp_ctx);
2539 /* finish up the last section */
2540 DEBUG(4, ("pm_process() returned %s\n", BOOLSTR(bRetval)));
2541 if (bRetval)
2542 if (lp_ctx->currentService != NULL)
2543 bRetval = lpcfg_service_ok(lp_ctx->currentService);
2545 bRetval = bRetval && lpcfg_update(lp_ctx);
2547 /* we do this unconditionally, so that it happens even
2548 for a missing smb.conf */
2549 reload_charcnv(lp_ctx);
2551 if (bRetval == true) {
2552 /* set this up so that any child python tasks will
2553 find the right smb.conf */
2554 setenv("SMB_CONF_PATH", filename, 1);
2556 /* set the context used by the lp_*() function
2557 varients */
2558 global_loadparm_context = lp_ctx;
2559 lp_ctx->loaded = true;
2562 return bRetval;
2566 * Return the max number of services.
2569 int lpcfg_numservices(struct loadparm_context *lp_ctx)
2571 if (lp_ctx->s3_fns) {
2572 return lp_ctx->s3_fns->get_numservices();
2575 return lp_ctx->iNumServices;
2579 * Display the contents of the services array in human-readable form.
2582 void lpcfg_dump(struct loadparm_context *lp_ctx, FILE *f, bool show_defaults,
2583 int maxtoprint)
2585 int iService;
2587 if (lp_ctx->s3_fns) {
2588 lp_ctx->s3_fns->dump(f, show_defaults, maxtoprint);
2589 return;
2592 defaults_saved = !show_defaults;
2594 dump_globals(lp_ctx, f, show_defaults);
2596 dump_a_service(lp_ctx->sDefault, lp_ctx->sDefault, f, lp_ctx->flags);
2598 for (iService = 0; iService < maxtoprint; iService++)
2599 lpcfg_dump_one(f, show_defaults, lp_ctx->services[iService], lp_ctx->sDefault);
2603 * Display the contents of one service in human-readable form.
2605 void lpcfg_dump_one(FILE *f, bool show_defaults, struct loadparm_service *service, struct loadparm_service *sDefault)
2607 if (service != NULL) {
2608 if (service->szService[0] == '\0')
2609 return;
2610 dump_a_service(service, sDefault, f, NULL);
2614 struct loadparm_service *lpcfg_servicebynum(struct loadparm_context *lp_ctx,
2615 int snum)
2617 if (lp_ctx->s3_fns) {
2618 return lp_ctx->s3_fns->get_servicebynum(snum);
2621 return lp_ctx->services[snum];
2624 struct loadparm_service *lpcfg_service(struct loadparm_context *lp_ctx,
2625 const char *service_name)
2627 int iService;
2628 char *serviceName;
2630 if (lp_ctx->s3_fns) {
2631 return lp_ctx->s3_fns->get_service(service_name);
2634 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--) {
2635 if (lp_ctx->services[iService] &&
2636 lp_ctx->services[iService]->szService) {
2638 * The substitution here is used to support %U is
2639 * service names
2641 serviceName = standard_sub_basic(
2642 lp_ctx->services[iService],
2643 lp_ctx->services[iService]->szService);
2644 if (strequal(serviceName, service_name)) {
2645 talloc_free(serviceName);
2646 return lp_ctx->services[iService];
2648 talloc_free(serviceName);
2652 DEBUG(7,("lpcfg_servicenumber: couldn't find %s\n", service_name));
2653 return NULL;
2656 const char *lpcfg_servicename(const struct loadparm_service *service)
2658 return lpcfg_string((const char *)service->szService);
2662 * A useful volume label function.
2664 const char *lpcfg_volume_label(struct loadparm_service *service, struct loadparm_service *sDefault)
2666 const char *ret;
2667 ret = lpcfg_string((const char *)((service != NULL && service->volume != NULL) ?
2668 service->volume : sDefault->volume));
2669 if (!*ret)
2670 return lpcfg_servicename(service);
2671 return ret;
2675 * Return the correct printer name.
2677 const char *lpcfg_printername(struct loadparm_service *service, struct loadparm_service *sDefault)
2679 const char *ret;
2680 ret = lpcfg_string((const char *)((service != NULL && service->_printername != NULL) ?
2681 service->_printername : sDefault->_printername));
2682 if (ret == NULL || (ret != NULL && *ret == '\0'))
2683 ret = lpcfg_servicename(service);
2685 return ret;
2690 * Return the max print jobs per queue.
2692 int lpcfg_maxprintjobs(struct loadparm_service *service, struct loadparm_service *sDefault)
2694 int maxjobs = (service != NULL) ? service->iMaxPrintJobs : sDefault->iMaxPrintJobs;
2695 if (maxjobs <= 0 || maxjobs >= PRINT_MAX_JOBID)
2696 maxjobs = PRINT_MAX_JOBID - 1;
2698 return maxjobs;
2701 struct smb_iconv_handle *lpcfg_iconv_handle(struct loadparm_context *lp_ctx)
2703 if (lp_ctx == NULL) {
2704 return get_iconv_handle();
2706 return lp_ctx->iconv_handle;
2709 _PUBLIC_ void reload_charcnv(struct loadparm_context *lp_ctx)
2711 struct smb_iconv_handle *old_ic = lp_ctx->iconv_handle;
2712 if (!lp_ctx->global) {
2713 return;
2716 if (old_ic == NULL) {
2717 old_ic = global_iconv_handle;
2719 lp_ctx->iconv_handle = smb_iconv_handle_reinit_lp(lp_ctx, lp_ctx, old_ic);
2720 global_iconv_handle = lp_ctx->iconv_handle;
2723 _PUBLIC_ char *lpcfg_tls_keyfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2725 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_keyfile(lp_ctx));
2728 _PUBLIC_ char *lpcfg_tls_certfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2730 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_certfile(lp_ctx));
2733 _PUBLIC_ char *lpcfg_tls_cafile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2735 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_cafile(lp_ctx));
2738 _PUBLIC_ char *lpcfg_tls_crlfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2740 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_crlfile(lp_ctx));
2743 _PUBLIC_ char *lpcfg_tls_dhpfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2745 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_dhpfile(lp_ctx));
2748 struct gensec_settings *lpcfg_gensec_settings(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2750 struct gensec_settings *settings = talloc_zero(mem_ctx, struct gensec_settings);
2751 if (settings == NULL)
2752 return NULL;
2753 SMB_ASSERT(lp_ctx != NULL);
2754 settings->lp_ctx = talloc_reference(settings, lp_ctx);
2755 settings->target_hostname = lpcfg_parm_string(lp_ctx, NULL, "gensec", "target_hostname");
2756 return settings;
2759 int lpcfg_server_role(struct loadparm_context *lp_ctx)
2761 int domain_master = lpcfg__domain_master(lp_ctx);
2763 return lp_find_server_role(lpcfg__server_role(lp_ctx),
2764 lpcfg__security(lp_ctx),
2765 lpcfg__domain_logons(lp_ctx),
2766 (domain_master == true) ||
2767 (domain_master == Auto));
2770 int lpcfg_security(struct loadparm_context *lp_ctx)
2772 return lp_find_security(lpcfg__server_role(lp_ctx),
2773 lpcfg__security(lp_ctx));
2776 bool lpcfg_server_signing_allowed(struct loadparm_context *lp_ctx, bool *mandatory)
2778 bool allowed = true;
2779 enum smb_signing_setting signing_setting = lpcfg_server_signing(lp_ctx);
2781 *mandatory = false;
2783 if (signing_setting == SMB_SIGNING_DEFAULT) {
2785 * If we are a domain controller, SMB signing is
2786 * really important, as it can prevent a number of
2787 * attacks on communications between us and the
2788 * clients
2790 * However, it really sucks (no sendfile, CPU
2791 * overhead) performance-wise when used on a
2792 * file server, so disable it by default
2793 * on non-DCs
2796 if (lpcfg_server_role(lp_ctx) >= ROLE_ACTIVE_DIRECTORY_DC) {
2797 signing_setting = SMB_SIGNING_REQUIRED;
2798 } else {
2799 signing_setting = SMB_SIGNING_OFF;
2803 switch (signing_setting) {
2804 case SMB_SIGNING_REQUIRED:
2805 *mandatory = true;
2806 break;
2807 case SMB_SIGNING_IF_REQUIRED:
2808 break;
2809 case SMB_SIGNING_DEFAULT:
2810 case SMB_SIGNING_OFF:
2811 allowed = false;
2812 break;
2815 return allowed;
2818 int lpcfg_tdb_hash_size(struct loadparm_context *lp_ctx, const char *name)
2820 const char *base;
2822 if (name == NULL) {
2823 return 0;
2826 base = strrchr_m(name, '/');
2827 if (base != NULL) {
2828 base += 1;
2829 } else {
2830 base = name;
2832 return lpcfg_parm_int(lp_ctx, NULL, "tdb_hashsize", base, 0);
2836 int lpcfg_tdb_flags(struct loadparm_context *lp_ctx, int tdb_flags)
2838 if (!lpcfg_use_mmap(lp_ctx)) {
2839 tdb_flags |= TDB_NOMMAP;
2841 return tdb_flags;