param: duplicate the copy service in lib/param into source3 loadparm
[Samba.git] / lib / param / loadparm.c
blob809793d3ca144305698347d8b88bdfb5cab77e8c
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 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 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 TALLOC_FREE(*((char ***)dest_ptr));
951 *(const char * const **)dest_ptr = (const char * const *)str_list_copy(pserviceDest,
952 *(const char * * const *)src_ptr);
953 break;
954 default:
955 break;
959 if (bcopyall) {
960 init_copymap(pserviceDest);
961 if (pserviceSource->copymap)
962 bitmap_copy(pserviceDest->copymap,
963 pserviceSource->copymap);
966 for (data = pserviceSource->param_opt; data != NULL; data = data->next) {
967 set_param_opt(pserviceDest, &pserviceDest->param_opt,
968 data->key, data->value, data->priority);
973 * Check a service for consistency. Return False if the service is in any way
974 * incomplete or faulty, else True.
976 static bool lpcfg_service_ok(struct loadparm_service *service)
978 bool bRetval;
980 bRetval = true;
981 if (service->szService[0] == '\0') {
982 DEBUG(0, ("The following message indicates an internal error:\n"));
983 DEBUG(0, ("No service name in service entry.\n"));
984 bRetval = false;
987 /* The [printers] entry MUST be printable. I'm all for flexibility, but */
988 /* I can't see why you'd want a non-printable printer service... */
989 if (strwicmp(service->szService, PRINTERS_NAME) == 0) {
990 if (!service->printable) {
991 DEBUG(0, ("WARNING: [%s] service MUST be printable!\n",
992 service->szService));
993 service->printable = true;
995 /* [printers] service must also be non-browsable. */
996 if (service->browseable)
997 service->browseable = false;
1000 /* If a service is flagged unavailable, log the fact at level 0. */
1001 if (!service->bAvailable)
1002 DEBUG(1, ("NOTE: Service %s is flagged unavailable.\n",
1003 service->szService));
1005 return bRetval;
1009 /*******************************************************************
1010 Keep a linked list of all config files so we know when one has changed
1011 it's date and needs to be reloaded.
1012 ********************************************************************/
1014 static void add_to_file_list(struct loadparm_context *lp_ctx,
1015 const char *fname, const char *subfname)
1017 struct file_lists *f = lp_ctx->file_lists;
1019 while (f) {
1020 if (f->name && !strcmp(f->name, fname))
1021 break;
1022 f = f->next;
1025 if (!f) {
1026 f = talloc(lp_ctx, struct file_lists);
1027 if (!f)
1028 return;
1029 f->next = lp_ctx->file_lists;
1030 f->name = talloc_strdup(f, fname);
1031 if (!f->name) {
1032 talloc_free(f);
1033 return;
1035 f->subfname = talloc_strdup(f, subfname);
1036 if (!f->subfname) {
1037 talloc_free(f);
1038 return;
1040 lp_ctx->file_lists = f;
1041 f->modtime = file_modtime(subfname);
1042 } else {
1043 time_t t = file_modtime(subfname);
1044 if (t)
1045 f->modtime = t;
1049 /*******************************************************************
1050 Check if a config file has changed date.
1051 ********************************************************************/
1052 bool lpcfg_file_list_changed(struct loadparm_context *lp_ctx)
1054 struct file_lists *f;
1055 DEBUG(6, ("lpcfg_file_list_changed()\n"));
1057 for (f = lp_ctx->file_lists; f != NULL; f = f->next) {
1058 char *n2;
1059 time_t mod_time;
1061 n2 = standard_sub_basic(lp_ctx, f->name);
1063 DEBUGADD(6, ("file %s -> %s last mod_time: %s\n",
1064 f->name, n2, ctime(&f->modtime)));
1066 mod_time = file_modtime(n2);
1068 if (mod_time && ((f->modtime != mod_time) || (f->subfname == NULL) || (strcmp(n2, f->subfname) != 0))) {
1069 DEBUGADD(6, ("file %s modified: %s\n", n2,
1070 ctime(&mod_time)));
1071 f->modtime = mod_time;
1072 talloc_free(f->subfname);
1073 f->subfname = talloc_strdup(f, n2);
1074 return true;
1077 return false;
1080 /***************************************************************************
1081 Handle the "realm" parameter
1082 ***************************************************************************/
1084 bool handle_realm(struct loadparm_context *lp_ctx, int unused,
1085 const char *pszParmValue, char **ptr)
1087 char *upper;
1088 char *lower;
1090 upper = strupper_talloc(lp_ctx, pszParmValue);
1091 if (upper == NULL) {
1092 return false;
1095 lower = strlower_talloc(lp_ctx, pszParmValue);
1096 if (lower == NULL) {
1097 TALLOC_FREE(upper);
1098 return false;
1101 if (lp_ctx->s3_fns != NULL) {
1102 lp_ctx->s3_fns->lp_string_set(ptr, pszParmValue);
1103 lp_ctx->s3_fns->lp_string_set(&lp_ctx->globals->realm, upper);
1104 lp_ctx->s3_fns->lp_string_set(&lp_ctx->globals->dnsdomain, lower);
1105 } else {
1106 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1107 lpcfg_string_set(lp_ctx, &lp_ctx->globals->realm, upper);
1108 lpcfg_string_set(lp_ctx, &lp_ctx->globals->dnsdomain, lower);
1111 return true;
1114 /***************************************************************************
1115 Handle the include operation.
1116 ***************************************************************************/
1118 static bool handle_include(struct loadparm_context *lp_ctx, int unused,
1119 const char *pszParmValue, char **ptr)
1121 char *fname = standard_sub_basic(lp_ctx, pszParmValue);
1123 add_to_file_list(lp_ctx, pszParmValue, fname);
1125 lpcfg_string_set(lp_ctx, ptr, fname);
1127 if (file_exist(fname))
1128 return pm_process(fname, do_section, do_parameter, lp_ctx);
1130 DEBUG(2, ("Can't find include file %s\n", fname));
1132 return false;
1135 /***************************************************************************
1136 Handle the interpretation of the copy parameter.
1137 ***************************************************************************/
1139 static bool handle_copy(struct loadparm_context *lp_ctx, int unused,
1140 const char *pszParmValue, char **ptr)
1142 bool bRetval;
1143 struct loadparm_service *serviceTemp;
1145 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1147 bRetval = false;
1149 DEBUG(3, ("Copying service from service %s\n", pszParmValue));
1151 if ((serviceTemp = getservicebyname(lp_ctx, pszParmValue)) != NULL) {
1152 if (serviceTemp == lp_ctx->currentService) {
1153 DEBUG(0, ("Can't copy service %s - unable to copy self!\n", pszParmValue));
1154 } else {
1155 copy_service(lp_ctx->currentService,
1156 serviceTemp,
1157 lp_ctx->currentService->copymap);
1158 bRetval = true;
1160 } else {
1161 DEBUG(0, ("Unable to copy service - source not found: %s\n",
1162 pszParmValue));
1163 bRetval = false;
1166 return bRetval;
1169 bool handle_debug_list(struct loadparm_context *lp_ctx, int unused,
1170 const char *pszParmValue, char **ptr)
1172 if (lp_ctx->s3_fns != NULL) {
1173 lp_ctx->s3_fns->lp_string_set(ptr, pszParmValue);
1174 } else {
1175 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1178 return debug_parse_levels(pszParmValue);
1181 bool handle_logfile(struct loadparm_context *lp_ctx, int unused,
1182 const char *pszParmValue, char **ptr)
1184 if (lp_ctx->s3_fns != NULL) {
1185 lp_ctx->s3_fns->lp_string_set(ptr, pszParmValue);
1186 } else {
1187 debug_set_logfile(pszParmValue);
1188 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1191 return true;
1194 /***************************************************************************
1195 Initialise a copymap.
1196 ***************************************************************************/
1198 static void init_copymap(struct loadparm_service *pservice)
1200 int i;
1202 TALLOC_FREE(pservice->copymap);
1204 pservice->copymap = bitmap_talloc(NULL, NUMPARAMETERS);
1205 if (!pservice->copymap)
1206 DEBUG(0,
1207 ("Couldn't allocate copymap!! (size %d)\n",
1208 (int)NUMPARAMETERS));
1209 else
1210 for (i = 0; i < NUMPARAMETERS; i++)
1211 bitmap_set(pservice->copymap, i);
1215 * Process a parametric option
1217 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
1218 struct loadparm_service *service,
1219 const char *pszParmName,
1220 const char *pszParmValue, int flags)
1222 struct parmlist_entry *paramo, *data;
1223 char *name;
1224 TALLOC_CTX *mem_ctx;
1226 while (isspace((unsigned char)*pszParmName)) {
1227 pszParmName++;
1230 name = strlower_talloc(lp_ctx, pszParmName);
1231 if (!name) return false;
1233 if (service == NULL) {
1234 data = lp_ctx->globals->param_opt;
1235 mem_ctx = lp_ctx->globals;
1236 } else {
1237 data = service->param_opt;
1238 mem_ctx = service;
1241 /* Traverse destination */
1242 for (paramo=data; paramo; paramo=paramo->next) {
1243 /* If we already have the option set, override it unless
1244 it was a command line option and the new one isn't */
1245 if (strcmp(paramo->key, name) == 0) {
1246 if ((paramo->priority & FLAG_CMDLINE) &&
1247 !(flags & FLAG_CMDLINE)) {
1248 talloc_free(name);
1249 return true;
1252 talloc_free(paramo->value);
1253 paramo->value = talloc_strdup(paramo, pszParmValue);
1254 paramo->priority = flags;
1255 talloc_free(name);
1256 return true;
1260 paramo = talloc_zero(mem_ctx, struct parmlist_entry);
1261 if (!paramo)
1262 smb_panic("OOM");
1263 paramo->key = talloc_strdup(paramo, name);
1264 paramo->value = talloc_strdup(paramo, pszParmValue);
1265 paramo->priority = flags;
1266 if (service == NULL) {
1267 DLIST_ADD(lp_ctx->globals->param_opt, paramo);
1268 } else {
1269 DLIST_ADD(service->param_opt, paramo);
1272 talloc_free(name);
1274 return true;
1277 static bool set_variable(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
1278 const char *pszParmName, const char *pszParmValue,
1279 struct loadparm_context *lp_ctx, bool on_globals)
1281 int i;
1282 /* if it is a special case then go ahead */
1283 if (parm_table[parmnum].special) {
1284 bool ret;
1285 ret = parm_table[parmnum].special(lp_ctx, -1, pszParmValue,
1286 (char **)parm_ptr);
1287 if (!ret) {
1288 return false;
1290 goto mark_non_default;
1293 /* now switch on the type of variable it is */
1294 switch (parm_table[parmnum].type)
1296 case P_BOOL: {
1297 bool b;
1298 if (!set_boolean(pszParmValue, &b)) {
1299 DEBUG(0, ("set_variable(%s): value is not "
1300 "boolean!\n", pszParmValue));
1301 return false;
1303 *(bool *)parm_ptr = b;
1305 break;
1307 case P_BOOLREV: {
1308 bool b;
1309 if (!set_boolean(pszParmValue, &b)) {
1310 DEBUG(0, ("set_variable(%s): value is not "
1311 "boolean!\n", pszParmValue));
1312 return false;
1314 *(bool *)parm_ptr = !b;
1316 break;
1318 case P_INTEGER:
1319 *(int *)parm_ptr = atoi(pszParmValue);
1320 break;
1322 case P_CHAR:
1323 *(char *)parm_ptr = *pszParmValue;
1324 break;
1326 case P_OCTAL:
1327 *(int *)parm_ptr = strtol(pszParmValue, NULL, 8);
1328 break;
1330 case P_BYTES:
1332 uint64_t val;
1333 if (conv_str_size_error(pszParmValue, &val)) {
1334 if (val <= INT_MAX) {
1335 *(int *)parm_ptr = (int)val;
1336 break;
1340 DEBUG(0, ("set_variable(%s): value is not "
1341 "a valid size specifier!\n", pszParmValue));
1342 return false;
1345 case P_CMDLIST:
1346 *(const char * const **)parm_ptr
1347 = (const char * const *)str_list_make(mem_ctx,
1348 pszParmValue, NULL);
1349 break;
1350 case P_LIST:
1352 char **new_list = str_list_make(mem_ctx,
1353 pszParmValue, NULL);
1354 for (i=0; new_list[i]; i++) {
1355 if (*(const char ***)parm_ptr != NULL &&
1356 new_list[i][0] == '+' &&
1357 new_list[i][1])
1359 if (!str_list_check(*(const char ***)parm_ptr,
1360 &new_list[i][1])) {
1361 *(const char ***)parm_ptr = str_list_add(*(const char ***)parm_ptr,
1362 &new_list[i][1]);
1364 } else if (*(const char ***)parm_ptr != NULL &&
1365 new_list[i][0] == '-' &&
1366 new_list[i][1])
1368 str_list_remove(*(const char ***)parm_ptr,
1369 &new_list[i][1]);
1370 } else {
1371 if (i != 0) {
1372 DEBUG(0, ("Unsupported list syntax for: %s = %s\n",
1373 pszParmName, pszParmValue));
1374 return false;
1376 *(const char * const **)parm_ptr = (const char * const *) new_list;
1377 break;
1380 break;
1382 case P_STRING:
1383 lpcfg_string_set(mem_ctx, (char **)parm_ptr, pszParmValue);
1384 break;
1386 case P_USTRING:
1387 lpcfg_string_set_upper(mem_ctx, (char **)parm_ptr, pszParmValue);
1388 break;
1390 case P_ENUM:
1391 for (i = 0; parm_table[parmnum].enum_list[i].name; i++) {
1392 if (strequal
1393 (pszParmValue,
1394 parm_table[parmnum].enum_list[i].name)) {
1395 *(int *)parm_ptr =
1396 parm_table[parmnum].
1397 enum_list[i].value;
1398 break;
1401 if (!parm_table[parmnum].enum_list[i].name) {
1402 DEBUG(0,("Unknown enumerated value '%s' for '%s'\n",
1403 pszParmValue, pszParmName));
1404 return false;
1406 break;
1408 case P_SEP:
1409 break;
1412 mark_non_default:
1413 if (on_globals && (lp_ctx->flags[parmnum] & FLAG_DEFAULT)) {
1414 lp_ctx->flags[parmnum] &= ~FLAG_DEFAULT;
1415 /* we have to also unset FLAG_DEFAULT on aliases */
1416 for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
1417 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1419 for (i=parmnum+1;i<NUMPARAMETERS && parm_table[i].offset == parm_table[parmnum].offset;i++) {
1420 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1423 return true;
1427 bool lpcfg_do_global_parameter(struct loadparm_context *lp_ctx,
1428 const char *pszParmName, const char *pszParmValue)
1430 int parmnum = lpcfg_map_parameter(pszParmName);
1431 void *parm_ptr;
1433 if (parmnum < 0) {
1434 if (strchr(pszParmName, ':')) {
1435 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName, pszParmValue, 0);
1437 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1438 return true;
1441 /* if the flag has been set on the command line, then don't allow override,
1442 but don't report an error */
1443 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1444 return true;
1447 parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[parmnum]);
1449 return set_variable(lp_ctx->globals, parmnum, parm_ptr,
1450 pszParmName, pszParmValue, lp_ctx, true);
1453 bool lpcfg_do_service_parameter(struct loadparm_context *lp_ctx,
1454 struct loadparm_service *service,
1455 const char *pszParmName, const char *pszParmValue)
1457 void *parm_ptr;
1458 int i;
1459 int parmnum = lpcfg_map_parameter(pszParmName);
1461 if (parmnum < 0) {
1462 if (strchr(pszParmName, ':')) {
1463 return lp_do_parameter_parametric(lp_ctx, service, pszParmName, pszParmValue, 0);
1465 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1466 return true;
1469 /* if the flag has been set on the command line, then don't allow override,
1470 but don't report an error */
1471 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1472 return true;
1475 if (parm_table[parmnum].p_class == P_GLOBAL) {
1476 DEBUG(0,
1477 ("Global parameter %s found in service section!\n",
1478 pszParmName));
1479 return true;
1481 parm_ptr = ((char *)service) + parm_table[parmnum].offset;
1483 if (!service->copymap)
1484 init_copymap(service);
1486 /* this handles the aliases - set the copymap for other
1487 * entries with the same data pointer */
1488 for (i = 0; parm_table[i].label; i++)
1489 if (parm_table[i].offset == parm_table[parmnum].offset &&
1490 parm_table[i].p_class == parm_table[parmnum].p_class)
1491 bitmap_clear(service->copymap, i);
1493 return set_variable(service, parmnum, parm_ptr, pszParmName,
1494 pszParmValue, lp_ctx, false);
1498 * Process a parameter.
1501 static bool do_parameter(const char *pszParmName, const char *pszParmValue,
1502 void *userdata)
1504 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1506 if (lp_ctx->bInGlobalSection)
1507 return lpcfg_do_global_parameter(lp_ctx, pszParmName,
1508 pszParmValue);
1509 else
1510 return lpcfg_do_service_parameter(lp_ctx, lp_ctx->currentService,
1511 pszParmName, pszParmValue);
1515 variable argument do parameter
1517 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx, const char *pszParmName, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
1518 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx,
1519 const char *pszParmName, const char *fmt, ...)
1521 char *s;
1522 bool ret;
1523 va_list ap;
1525 va_start(ap, fmt);
1526 s = talloc_vasprintf(NULL, fmt, ap);
1527 va_end(ap);
1528 ret = lpcfg_do_global_parameter(lp_ctx, pszParmName, s);
1529 talloc_free(s);
1530 return ret;
1535 set a parameter from the commandline - this is called from command line parameter
1536 parsing code. It sets the parameter then marks the parameter as unable to be modified
1537 by smb.conf processing
1539 bool lpcfg_set_cmdline(struct loadparm_context *lp_ctx, const char *pszParmName,
1540 const char *pszParmValue)
1542 int parmnum;
1543 int i;
1545 if (lp_ctx->s3_fns) {
1546 return lp_ctx->s3_fns->set_cmdline(pszParmName, pszParmValue);
1549 parmnum = lpcfg_map_parameter(pszParmName);
1551 while (isspace((unsigned char)*pszParmValue)) pszParmValue++;
1554 if (parmnum < 0 && strchr(pszParmName, ':')) {
1555 /* set a parametric option */
1556 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName,
1557 pszParmValue, FLAG_CMDLINE);
1560 if (parmnum < 0) {
1561 DEBUG(0,("Unknown option '%s'\n", pszParmName));
1562 return false;
1565 /* reset the CMDLINE flag in case this has been called before */
1566 lp_ctx->flags[parmnum] &= ~FLAG_CMDLINE;
1568 if (!lpcfg_do_global_parameter(lp_ctx, pszParmName, pszParmValue)) {
1569 return false;
1572 lp_ctx->flags[parmnum] |= FLAG_CMDLINE;
1574 /* we have to also set FLAG_CMDLINE on aliases */
1575 for (i=parmnum-1;
1576 i>=0 && parm_table[i].p_class == parm_table[parmnum].p_class &&
1577 parm_table[i].offset == parm_table[parmnum].offset;
1578 i--) {
1579 lp_ctx->flags[i] |= FLAG_CMDLINE;
1581 for (i=parmnum+1;
1582 i<NUMPARAMETERS &&
1583 parm_table[i].p_class == parm_table[parmnum].p_class &&
1584 parm_table[i].offset == parm_table[parmnum].offset;
1585 i++) {
1586 lp_ctx->flags[i] |= FLAG_CMDLINE;
1589 return true;
1593 set a option from the commandline in 'a=b' format. Use to support --option
1595 bool lpcfg_set_option(struct loadparm_context *lp_ctx, const char *option)
1597 char *p, *s;
1598 bool ret;
1600 s = talloc_strdup(NULL, option);
1601 if (!s) {
1602 return false;
1605 p = strchr(s, '=');
1606 if (!p) {
1607 talloc_free(s);
1608 return false;
1611 *p = 0;
1613 ret = lpcfg_set_cmdline(lp_ctx, s, p+1);
1614 talloc_free(s);
1615 return ret;
1619 #define BOOLSTR(b) ((b) ? "Yes" : "No")
1622 * Print a parameter of the specified type.
1625 void lpcfg_print_parameter(struct parm_struct *p, void *ptr, FILE * f)
1627 /* For the seperation of lists values that we print below */
1628 const char *list_sep = ", ";
1629 int i;
1630 switch (p->type)
1632 case P_ENUM:
1633 for (i = 0; p->enum_list[i].name; i++) {
1634 if (*(int *)ptr == p->enum_list[i].value) {
1635 fprintf(f, "%s",
1636 p->enum_list[i].name);
1637 break;
1640 break;
1642 case P_BOOL:
1643 fprintf(f, "%s", BOOLSTR(*(bool *)ptr));
1644 break;
1646 case P_BOOLREV:
1647 fprintf(f, "%s", BOOLSTR(!*(bool *)ptr));
1648 break;
1650 case P_INTEGER:
1651 case P_BYTES:
1652 fprintf(f, "%d", *(int *)ptr);
1653 break;
1655 case P_CHAR:
1656 fprintf(f, "%c", *(char *)ptr);
1657 break;
1659 case P_OCTAL: {
1660 int val = *(int *)ptr;
1661 if (val == -1) {
1662 fprintf(f, "-1");
1663 } else {
1664 fprintf(f, "0%03o", val);
1666 break;
1669 case P_CMDLIST:
1670 list_sep = " ";
1671 /* fall through */
1672 case P_LIST:
1673 if ((char ***)ptr && *(char ***)ptr) {
1674 char **list = *(char ***)ptr;
1675 for (; *list; list++) {
1676 /* surround strings with whitespace in double quotes */
1677 if (*(list+1) == NULL) {
1678 /* last item, no extra separator */
1679 list_sep = "";
1681 if ( strchr_m( *list, ' ' ) ) {
1682 fprintf(f, "\"%s\"%s", *list, list_sep);
1683 } else {
1684 fprintf(f, "%s%s", *list, list_sep);
1688 break;
1690 case P_STRING:
1691 case P_USTRING:
1692 if (*(char **)ptr) {
1693 fprintf(f, "%s", *(char **)ptr);
1695 break;
1696 case P_SEP:
1697 break;
1702 * Check if two parameters are equal.
1705 bool lpcfg_equal_parameter(parm_type type, void *ptr1, void *ptr2)
1707 switch (type) {
1708 case P_BOOL:
1709 case P_BOOLREV:
1710 return (*((bool *)ptr1) == *((bool *)ptr2));
1712 case P_INTEGER:
1713 case P_ENUM:
1714 case P_OCTAL:
1715 case P_BYTES:
1716 return (*((int *)ptr1) == *((int *)ptr2));
1718 case P_CHAR:
1719 return (*((char *)ptr1) == *((char *)ptr2));
1721 case P_LIST:
1722 case P_CMDLIST:
1723 return str_list_equal(*(const char ***)ptr1, *(const char ***)ptr2);
1725 case P_STRING:
1726 case P_USTRING:
1728 char *p1 = *(char **)ptr1, *p2 = *(char **)ptr2;
1729 if (p1 && !*p1)
1730 p1 = NULL;
1731 if (p2 && !*p2)
1732 p2 = NULL;
1733 return (p1 == p2 || strequal(p1, p2));
1735 case P_SEP:
1736 break;
1738 return false;
1742 * Process a new section (service).
1744 * At this stage all sections are services.
1745 * Later we'll have special sections that permit server parameters to be set.
1746 * Returns True on success, False on failure.
1749 static bool do_section(const char *pszSectionName, void *userdata)
1751 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1752 bool bRetval;
1753 bool isglobal = ((strwicmp(pszSectionName, GLOBAL_NAME) == 0) ||
1754 (strwicmp(pszSectionName, GLOBAL_NAME2) == 0));
1755 bRetval = false;
1757 /* if we've just struck a global section, note the fact. */
1758 lp_ctx->bInGlobalSection = isglobal;
1760 /* check for multiple global sections */
1761 if (lp_ctx->bInGlobalSection) {
1762 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1763 return true;
1766 /* if we have a current service, tidy it up before moving on */
1767 bRetval = true;
1769 if (lp_ctx->currentService != NULL)
1770 bRetval = lpcfg_service_ok(lp_ctx->currentService);
1772 /* if all is still well, move to the next record in the services array */
1773 if (bRetval) {
1774 /* We put this here to avoid an odd message order if messages are */
1775 /* issued by the post-processing of a previous section. */
1776 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1778 if ((lp_ctx->currentService = lpcfg_add_service(lp_ctx, lp_ctx->sDefault,
1779 pszSectionName))
1780 == NULL) {
1781 DEBUG(0, ("Failed to add a new service\n"));
1782 return false;
1786 return bRetval;
1791 * Determine if a particular base parameter is currently set to the default value.
1794 static bool is_default(struct loadparm_service *sDefault, int i)
1796 void *def_ptr = ((char *)sDefault) + parm_table[i].offset;
1797 switch (parm_table[i].type) {
1798 case P_CMDLIST:
1799 case P_LIST:
1800 return str_list_equal((const char * const *)parm_table[i].def.lvalue,
1801 (const char **)def_ptr);
1802 case P_STRING:
1803 case P_USTRING:
1804 return strequal(parm_table[i].def.svalue,
1805 *(char **)def_ptr);
1806 case P_BOOL:
1807 case P_BOOLREV:
1808 return parm_table[i].def.bvalue ==
1809 *(bool *)def_ptr;
1810 case P_INTEGER:
1811 case P_CHAR:
1812 case P_OCTAL:
1813 case P_BYTES:
1814 case P_ENUM:
1815 return parm_table[i].def.ivalue ==
1816 *(int *)def_ptr;
1817 case P_SEP:
1818 break;
1820 return false;
1824 *Display the contents of the global structure.
1827 static void dump_globals(struct loadparm_context *lp_ctx, FILE *f,
1828 bool show_defaults)
1830 int i;
1831 struct parmlist_entry *data;
1833 fprintf(f, "# Global parameters\n[global]\n");
1835 for (i = 0; parm_table[i].label; i++)
1836 if (parm_table[i].p_class == P_GLOBAL &&
1837 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset))) {
1838 if (!show_defaults && (lp_ctx->flags[i] & FLAG_DEFAULT))
1839 continue;
1840 fprintf(f, "\t%s = ", parm_table[i].label);
1841 lpcfg_print_parameter(&parm_table[i], lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[i]), f);
1842 fprintf(f, "\n");
1844 if (lp_ctx->globals->param_opt != NULL) {
1845 for (data = lp_ctx->globals->param_opt; data;
1846 data = data->next) {
1847 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
1848 continue;
1850 fprintf(f, "\t%s = %s\n", data->key, data->value);
1857 * Display the contents of a single services record.
1860 static void dump_a_service(struct loadparm_service * pService, struct loadparm_service *sDefault, FILE * f,
1861 unsigned int *flags)
1863 int i;
1864 struct parmlist_entry *data;
1866 if (pService != sDefault)
1867 fprintf(f, "\n[%s]\n", pService->szService);
1869 for (i = 0; parm_table[i].label; i++) {
1870 if (parm_table[i].p_class == P_LOCAL &&
1871 (*parm_table[i].label != '-') &&
1872 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset)))
1874 if (pService == sDefault) {
1875 if (flags && (flags[i] & FLAG_DEFAULT)) {
1876 continue;
1878 if (defaults_saved) {
1879 if (is_default(sDefault, i)) {
1880 continue;
1883 } else {
1884 if (lpcfg_equal_parameter(parm_table[i].type,
1885 ((char *)pService) +
1886 parm_table[i].offset,
1887 ((char *)sDefault) +
1888 parm_table[i].offset))
1889 continue;
1892 fprintf(f, "\t%s = ", parm_table[i].label);
1893 lpcfg_print_parameter(&parm_table[i],
1894 ((char *)pService) + parm_table[i].offset, f);
1895 fprintf(f, "\n");
1898 if (pService->param_opt != NULL) {
1899 for (data = pService->param_opt; data; data = data->next) {
1900 fprintf(f, "\t%s = %s\n", data->key, data->value);
1905 bool lpcfg_dump_a_parameter(struct loadparm_context *lp_ctx,
1906 struct loadparm_service *service,
1907 const char *parm_name, FILE * f)
1909 struct parm_struct *parm;
1910 void *ptr;
1912 parm = lpcfg_parm_struct(lp_ctx, parm_name);
1913 if (!parm) {
1914 return false;
1917 if (service != NULL && parm->p_class == P_GLOBAL) {
1918 return false;
1921 ptr = lpcfg_parm_ptr(lp_ctx, service,parm);
1923 lpcfg_print_parameter(parm, ptr, f);
1924 fprintf(f, "\n");
1925 return true;
1929 * Auto-load some home services.
1931 static void lpcfg_add_auto_services(struct loadparm_context *lp_ctx,
1932 const char *str)
1934 return;
1939 * Unload unused services.
1942 void lpcfg_killunused(struct loadparm_context *lp_ctx,
1943 struct smbsrv_connection *smb,
1944 bool (*snumused) (struct smbsrv_connection *, int))
1946 int i;
1947 for (i = 0; i < lp_ctx->iNumServices; i++) {
1948 if (lp_ctx->services[i] == NULL)
1949 continue;
1951 if (!snumused || !snumused(smb, i)) {
1952 talloc_free(lp_ctx->services[i]);
1953 lp_ctx->services[i] = NULL;
1959 static int lpcfg_destructor(struct loadparm_context *lp_ctx)
1961 struct parmlist_entry *data;
1963 if (lp_ctx->refuse_free) {
1964 /* someone is trying to free the
1965 global_loadparm_context.
1966 We can't allow that. */
1967 return -1;
1970 if (lp_ctx->globals->param_opt != NULL) {
1971 struct parmlist_entry *next;
1972 for (data = lp_ctx->globals->param_opt; data; data=next) {
1973 next = data->next;
1974 if (data->priority & FLAG_CMDLINE) continue;
1975 DLIST_REMOVE(lp_ctx->globals->param_opt, data);
1976 talloc_free(data);
1980 return 0;
1984 * Initialise the global parameter structure.
1986 * Note that most callers should use loadparm_init_global() instead
1988 struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
1990 int i;
1991 char *myname;
1992 struct loadparm_context *lp_ctx;
1993 struct parmlist_entry *parm;
1994 char *logfile;
1996 lp_ctx = talloc_zero(mem_ctx, struct loadparm_context);
1997 if (lp_ctx == NULL)
1998 return NULL;
2000 talloc_set_destructor(lp_ctx, lpcfg_destructor);
2001 lp_ctx->bInGlobalSection = true;
2002 lp_ctx->globals = talloc_zero(lp_ctx, struct loadparm_global);
2003 lp_ctx->sDefault = talloc_zero(lp_ctx, struct loadparm_service);
2005 lp_ctx->sDefault->iMaxPrintJobs = 1000;
2006 lp_ctx->sDefault->bAvailable = true;
2007 lp_ctx->sDefault->browseable = true;
2008 lp_ctx->sDefault->read_only = true;
2009 lp_ctx->sDefault->map_archive = true;
2010 lp_ctx->sDefault->strict_locking = true;
2011 lp_ctx->sDefault->oplocks = true;
2012 lp_ctx->sDefault->create_mask = 0744;
2013 lp_ctx->sDefault->force_create_mode = 0000;
2014 lp_ctx->sDefault->directory_mask = 0755;
2015 lp_ctx->sDefault->force_directory_mode = 0000;
2017 DEBUG(3, ("Initialising global parameters\n"));
2019 for (i = 0; parm_table[i].label; i++) {
2020 if ((parm_table[i].type == P_STRING ||
2021 parm_table[i].type == P_USTRING) &&
2022 !(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2023 char **r;
2024 if (parm_table[i].p_class == P_LOCAL) {
2025 r = (char **)(((char *)lp_ctx->sDefault) + parm_table[i].offset);
2026 } else {
2027 r = (char **)(((char *)lp_ctx->globals) + parm_table[i].offset);
2029 *r = talloc_strdup(lp_ctx, "");
2033 logfile = talloc_asprintf(lp_ctx, "%s/log.samba", dyn_LOGFILEBASE);
2034 lpcfg_do_global_parameter(lp_ctx, "log file", logfile);
2035 talloc_free(logfile);
2037 lpcfg_do_global_parameter(lp_ctx, "log level", "0");
2039 lpcfg_do_global_parameter(lp_ctx, "syslog", "1");
2040 lpcfg_do_global_parameter(lp_ctx, "syslog only", "No");
2041 lpcfg_do_global_parameter(lp_ctx, "debug timestamp", "Yes");
2042 lpcfg_do_global_parameter(lp_ctx, "debug prefix timestamp", "No");
2043 lpcfg_do_global_parameter(lp_ctx, "debug hires timestamp", "Yes");
2044 lpcfg_do_global_parameter(lp_ctx, "debug pid", "No");
2045 lpcfg_do_global_parameter(lp_ctx, "debug uid", "No");
2046 lpcfg_do_global_parameter(lp_ctx, "debug class", "No");
2048 lpcfg_do_global_parameter(lp_ctx, "share backend", "classic");
2050 lpcfg_do_global_parameter(lp_ctx, "server role", "auto");
2051 lpcfg_do_global_parameter(lp_ctx, "domain logons", "No");
2052 lpcfg_do_global_parameter(lp_ctx, "domain master", "Auto");
2054 /* options that can be set on the command line must be initialised via
2055 the slower lpcfg_do_global_parameter() to ensure that FLAG_CMDLINE is obeyed */
2056 #ifdef TCP_NODELAY
2057 lpcfg_do_global_parameter(lp_ctx, "socket options", "TCP_NODELAY");
2058 #endif
2059 lpcfg_do_global_parameter(lp_ctx, "workgroup", DEFAULT_WORKGROUP);
2060 myname = get_myname(lp_ctx);
2061 lpcfg_do_global_parameter(lp_ctx, "netbios name", myname);
2062 talloc_free(myname);
2063 lpcfg_do_global_parameter(lp_ctx, "name resolve order", "lmhosts wins host bcast");
2065 lpcfg_do_global_parameter(lp_ctx, "fstype", "NTFS");
2067 lpcfg_do_global_parameter(lp_ctx, "ntvfs handler", "unixuid default");
2068 lpcfg_do_global_parameter(lp_ctx, "max connections", "0");
2070 lpcfg_do_global_parameter(lp_ctx, "dcerpc endpoint servers", "epmapper wkssvc rpcecho samr netlogon lsarpc spoolss drsuapi dssetup unixinfo browser eventlog6 backupkey dnsserver");
2071 lpcfg_do_global_parameter(lp_ctx, "server services", "s3fs rpc nbt wrepl ldap cldap kdc drepl winbind ntp_signd kcc dnsupdate dns");
2072 lpcfg_do_global_parameter(lp_ctx, "kccsrv:samba_kcc", "true");
2073 /* the winbind method for domain controllers is for both RODC
2074 auth forwarding and for trusted domains */
2075 lpcfg_do_global_parameter(lp_ctx, "private dir", dyn_PRIVATE_DIR);
2076 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_LOCAL_MACHINE", "hklm.ldb");
2078 /* This hive should be dynamically generated by Samba using
2079 data from the sam, but for the moment leave it in a tdb to
2080 keep regedt32 from popping up an annoying dialog. */
2081 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_USERS", "hku.ldb");
2083 /* using UTF8 by default allows us to support all chars */
2084 lpcfg_do_global_parameter(lp_ctx, "unix charset", "UTF-8");
2086 /* Use codepage 850 as a default for the dos character set */
2087 lpcfg_do_global_parameter(lp_ctx, "dos charset", "CP850");
2090 * Allow the default PASSWD_CHAT to be overridden in local.h.
2092 lpcfg_do_global_parameter(lp_ctx, "passwd chat", DEFAULT_PASSWD_CHAT);
2094 lpcfg_do_global_parameter(lp_ctx, "pid directory", dyn_PIDDIR);
2095 lpcfg_do_global_parameter(lp_ctx, "lock dir", dyn_LOCKDIR);
2096 lpcfg_do_global_parameter(lp_ctx, "state directory", dyn_STATEDIR);
2097 lpcfg_do_global_parameter(lp_ctx, "cache directory", dyn_CACHEDIR);
2098 lpcfg_do_global_parameter(lp_ctx, "ncalrpc dir", dyn_NCALRPCDIR);
2100 lpcfg_do_global_parameter(lp_ctx, "nbt client socket address", "0.0.0.0");
2101 lpcfg_do_global_parameter_var(lp_ctx, "server string",
2102 "Samba %s", SAMBA_VERSION_STRING);
2104 lpcfg_do_global_parameter(lp_ctx, "password server", "*");
2106 lpcfg_do_global_parameter(lp_ctx, "max mux", "50");
2107 lpcfg_do_global_parameter(lp_ctx, "max xmit", "16644");
2108 lpcfg_do_global_parameter(lp_ctx, "host msdfs", "true");
2110 lpcfg_do_global_parameter(lp_ctx, "LargeReadwrite", "True");
2111 lpcfg_do_global_parameter(lp_ctx, "server min protocol", "LANMAN1");
2112 lpcfg_do_global_parameter(lp_ctx, "server max protocol", "SMB3");
2113 lpcfg_do_global_parameter(lp_ctx, "client min protocol", "CORE");
2114 lpcfg_do_global_parameter(lp_ctx, "client max protocol", "NT1");
2115 lpcfg_do_global_parameter(lp_ctx, "security", "AUTO");
2116 lpcfg_do_global_parameter(lp_ctx, "EncryptPasswords", "True");
2117 lpcfg_do_global_parameter(lp_ctx, "ReadRaw", "True");
2118 lpcfg_do_global_parameter(lp_ctx, "WriteRaw", "True");
2119 lpcfg_do_global_parameter(lp_ctx, "NullPasswords", "False");
2120 lpcfg_do_global_parameter(lp_ctx, "old password allowed period", "60");
2121 lpcfg_do_global_parameter(lp_ctx, "ObeyPamRestrictions", "False");
2123 lpcfg_do_global_parameter(lp_ctx, "TimeServer", "False");
2124 lpcfg_do_global_parameter(lp_ctx, "BindInterfacesOnly", "False");
2125 lpcfg_do_global_parameter(lp_ctx, "Unicode", "True");
2126 lpcfg_do_global_parameter(lp_ctx, "ClientLanManAuth", "False");
2127 lpcfg_do_global_parameter(lp_ctx, "ClientNTLMv2Auth", "True");
2128 lpcfg_do_global_parameter(lp_ctx, "LanmanAuth", "False");
2129 lpcfg_do_global_parameter(lp_ctx, "NTLMAuth", "True");
2130 lpcfg_do_global_parameter(lp_ctx, "client use spnego principal", "False");
2132 lpcfg_do_global_parameter(lp_ctx, "UnixExtensions", "True");
2134 lpcfg_do_global_parameter(lp_ctx, "PreferredMaster", "Auto");
2135 lpcfg_do_global_parameter(lp_ctx, "LocalMaster", "True");
2137 lpcfg_do_global_parameter(lp_ctx, "wins support", "False");
2138 lpcfg_do_global_parameter(lp_ctx, "dns proxy", "True");
2140 lpcfg_do_global_parameter(lp_ctx, "winbind separator", "\\");
2141 lpcfg_do_global_parameter(lp_ctx, "winbind sealed pipes", "True");
2142 lpcfg_do_global_parameter(lp_ctx, "require strong key", "True");
2143 lpcfg_do_global_parameter(lp_ctx, "winbindd socket directory", dyn_WINBINDD_SOCKET_DIR);
2144 lpcfg_do_global_parameter(lp_ctx, "winbindd privileged socket directory", dyn_WINBINDD_PRIVILEGED_SOCKET_DIR);
2145 lpcfg_do_global_parameter(lp_ctx, "ntp signd socket directory", dyn_NTP_SIGND_SOCKET_DIR);
2146 lpcfg_do_global_parameter_var(lp_ctx, "dns update command", "%s/samba_dnsupdate", dyn_SCRIPTSBINDIR);
2147 lpcfg_do_global_parameter_var(lp_ctx, "spn update command", "%s/samba_spnupdate", dyn_SCRIPTSBINDIR);
2148 lpcfg_do_global_parameter_var(lp_ctx, "samba kcc command",
2149 "%s/samba_kcc", dyn_SCRIPTSBINDIR);
2150 lpcfg_do_global_parameter(lp_ctx, "template shell", "/bin/false");
2151 lpcfg_do_global_parameter(lp_ctx, "template homedir", "/home/%WORKGROUP%/%ACCOUNTNAME%");
2153 lpcfg_do_global_parameter(lp_ctx, "client signing", "default");
2154 lpcfg_do_global_parameter(lp_ctx, "server signing", "default");
2156 lpcfg_do_global_parameter(lp_ctx, "use spnego", "True");
2158 lpcfg_do_global_parameter(lp_ctx, "use mmap", "True");
2160 lpcfg_do_global_parameter(lp_ctx, "smb ports", "445 139");
2161 lpcfg_do_global_parameter(lp_ctx, "nbt port", "137");
2162 lpcfg_do_global_parameter(lp_ctx, "dgram port", "138");
2163 lpcfg_do_global_parameter(lp_ctx, "cldap port", "389");
2164 lpcfg_do_global_parameter(lp_ctx, "krb5 port", "88");
2165 lpcfg_do_global_parameter(lp_ctx, "kpasswd port", "464");
2166 lpcfg_do_global_parameter(lp_ctx, "web port", "901");
2168 lpcfg_do_global_parameter(lp_ctx, "nt status support", "True");
2170 lpcfg_do_global_parameter(lp_ctx, "max wins ttl", "518400"); /* 6 days */
2171 lpcfg_do_global_parameter(lp_ctx, "min wins ttl", "21600");
2173 lpcfg_do_global_parameter(lp_ctx, "tls enabled", "True");
2174 lpcfg_do_global_parameter(lp_ctx, "tls keyfile", "tls/key.pem");
2175 lpcfg_do_global_parameter(lp_ctx, "tls certfile", "tls/cert.pem");
2176 lpcfg_do_global_parameter(lp_ctx, "tls cafile", "tls/ca.pem");
2177 lpcfg_do_global_parameter(lp_ctx, "prefork children:smb", "4");
2179 lpcfg_do_global_parameter(lp_ctx, "rndc command", "/usr/sbin/rndc");
2180 lpcfg_do_global_parameter(lp_ctx, "nsupdate command", "/usr/bin/nsupdate -g");
2182 lpcfg_do_global_parameter(lp_ctx, "allow dns updates", "secure only");
2183 lpcfg_do_global_parameter(lp_ctx, "dns forwarder", "");
2185 lpcfg_do_global_parameter(lp_ctx, "algorithmic rid base", "1000");
2187 lpcfg_do_global_parameter(lp_ctx, "enhanced browsing", "True");
2189 lpcfg_do_global_parameter(lp_ctx, "winbind nss info", "template");
2191 lpcfg_do_global_parameter(lp_ctx, "server schannel", "Auto");
2193 lpcfg_do_global_parameter(lp_ctx, "short preserve case", "True");
2195 lpcfg_do_global_parameter(lp_ctx, "max open files", "16384");
2197 lpcfg_do_global_parameter(lp_ctx, "cups connection timeout", "30");
2199 lpcfg_do_global_parameter(lp_ctx, "locking", "True");
2201 lpcfg_do_global_parameter(lp_ctx, "block size", "1024");
2203 lpcfg_do_global_parameter(lp_ctx, "client use spnego", "True");
2205 lpcfg_do_global_parameter(lp_ctx, "change notify", "True");
2207 lpcfg_do_global_parameter(lp_ctx, "name cache timeout", "660");
2209 lpcfg_do_global_parameter(lp_ctx, "defer sharing violations", "True");
2211 lpcfg_do_global_parameter(lp_ctx, "ldap replication sleep", "1000");
2213 lpcfg_do_global_parameter(lp_ctx, "idmap backend", "tdb");
2215 lpcfg_do_global_parameter(lp_ctx, "enable privileges", "True");
2217 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max write", "%u", DEFAULT_SMB2_MAX_WRITE);
2219 lpcfg_do_global_parameter(lp_ctx, "passdb backend", "tdbsam");
2221 lpcfg_do_global_parameter(lp_ctx, "getwd cache", "True");
2223 lpcfg_do_global_parameter(lp_ctx, "winbind nested groups", "True");
2225 lpcfg_do_global_parameter(lp_ctx, "mangled names", "True");
2227 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max credits", "%u", DEFAULT_SMB2_MAX_CREDITS);
2229 lpcfg_do_global_parameter(lp_ctx, "ldap ssl", "start tls");
2231 lpcfg_do_global_parameter(lp_ctx, "ldap deref", "auto");
2233 lpcfg_do_global_parameter(lp_ctx, "lm interval", "60");
2235 lpcfg_do_global_parameter(lp_ctx, "mangling method", "hash2");
2237 lpcfg_do_global_parameter(lp_ctx, "hide dot files", "True");
2239 lpcfg_do_global_parameter(lp_ctx, "browse list", "True");
2241 lpcfg_do_global_parameter(lp_ctx, "passwd chat timeout", "2");
2243 lpcfg_do_global_parameter(lp_ctx, "guest account", GUEST_ACCOUNT);
2245 lpcfg_do_global_parameter(lp_ctx, "client schannel", "auto");
2247 lpcfg_do_global_parameter(lp_ctx, "smb encrypt", "default");
2249 lpcfg_do_global_parameter(lp_ctx, "max log size", "5000");
2251 lpcfg_do_global_parameter(lp_ctx, "idmap negative cache time", "120");
2253 lpcfg_do_global_parameter(lp_ctx, "ldap follow referral", "auto");
2255 lpcfg_do_global_parameter(lp_ctx, "multicast dns register", "yes");
2257 lpcfg_do_global_parameter(lp_ctx, "winbind reconnect delay", "30");
2259 lpcfg_do_global_parameter(lp_ctx, "nt acl support", "yes");
2261 lpcfg_do_global_parameter(lp_ctx, "acl check permissions", "yes");
2263 lpcfg_do_global_parameter(lp_ctx, "keepalive", "300");
2265 lpcfg_do_global_parameter(lp_ctx, "winbind cache time", "300");
2267 lpcfg_do_global_parameter(lp_ctx, "level2 oplocks", "yes");
2269 lpcfg_do_global_parameter(lp_ctx, "show add printer wizard", "yes");
2271 lpcfg_do_global_parameter(lp_ctx, "allocation roundup size", "1048576");
2273 lpcfg_do_global_parameter(lp_ctx, "ldap page size", "1024");
2275 lpcfg_do_global_parameter(lp_ctx, "kernel share modes", "yes");
2277 lpcfg_do_global_parameter(lp_ctx, "strict locking", "Auto");
2279 lpcfg_do_global_parameter(lp_ctx, "map readonly", "yes");
2281 lpcfg_do_global_parameter(lp_ctx, "allow trusted domains", "yes");
2283 lpcfg_do_global_parameter(lp_ctx, "default devmode", "yes");
2285 lpcfg_do_global_parameter(lp_ctx, "os level", "20");
2287 lpcfg_do_global_parameter(lp_ctx, "dos filetimes", "yes");
2289 lpcfg_do_global_parameter(lp_ctx, "mangling char", "~");
2291 lpcfg_do_global_parameter(lp_ctx, "printcap cache time", "750");
2293 lpcfg_do_global_parameter(lp_ctx, "create krb5 conf", "yes");
2295 lpcfg_do_global_parameter(lp_ctx, "winbind max clients", "200");
2297 lpcfg_do_global_parameter(lp_ctx, "acl map full control", "yes");
2299 lpcfg_do_global_parameter(lp_ctx, "nt pipe support", "yes");
2301 lpcfg_do_global_parameter(lp_ctx, "ldap debug threshold", "10");
2303 lpcfg_do_global_parameter(lp_ctx, "follow symlinks", "yes");
2305 lpcfg_do_global_parameter(lp_ctx, "machine password timeout", "604800");
2307 lpcfg_do_global_parameter(lp_ctx, "ldap connection timeout", "2");
2309 lpcfg_do_global_parameter(lp_ctx, "winbind expand groups", "1");
2311 lpcfg_do_global_parameter(lp_ctx, "stat cache", "yes");
2313 lpcfg_do_global_parameter(lp_ctx, "lpq cache time", "30");
2315 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max trans", "%u", DEFAULT_SMB2_MAX_TRANSACT);
2317 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max read", "%u", DEFAULT_SMB2_MAX_READ);
2319 lpcfg_do_global_parameter(lp_ctx, "durable handles", "yes");
2321 lpcfg_do_global_parameter(lp_ctx, "max stat cache size", "256");
2323 lpcfg_do_global_parameter(lp_ctx, "ldap passwd sync", "no");
2325 lpcfg_do_global_parameter(lp_ctx, "kernel change notify", "yes");
2327 lpcfg_do_global_parameter(lp_ctx, "max ttl", "259200");
2329 lpcfg_do_global_parameter(lp_ctx, "blocking locks", "yes");
2331 lpcfg_do_global_parameter(lp_ctx, "oplock contention limit", "2");
2333 lpcfg_do_global_parameter(lp_ctx, "load printers", "yes");
2335 lpcfg_do_global_parameter(lp_ctx, "idmap cache time", "604800");
2337 lpcfg_do_global_parameter(lp_ctx, "preserve case", "yes");
2339 lpcfg_do_global_parameter(lp_ctx, "lm announce", "auto");
2341 lpcfg_do_global_parameter(lp_ctx, "afs token lifetime", "604800");
2343 lpcfg_do_global_parameter(lp_ctx, "enable core files", "yes");
2345 lpcfg_do_global_parameter(lp_ctx, "winbind max domain connections", "1");
2347 lpcfg_do_global_parameter(lp_ctx, "case sensitive", "auto");
2349 lpcfg_do_global_parameter(lp_ctx, "ldap timeout", "15");
2351 lpcfg_do_global_parameter(lp_ctx, "mangle prefix", "1");
2353 lpcfg_do_global_parameter(lp_ctx, "posix locking", "yes");
2355 lpcfg_do_global_parameter(lp_ctx, "lock spin time", "200");
2357 lpcfg_do_global_parameter(lp_ctx, "directory name cache size", "100");
2359 lpcfg_do_global_parameter(lp_ctx, "nmbd bind explicit broadcast", "yes");
2361 lpcfg_do_global_parameter(lp_ctx, "init logon delay", "100");
2363 lpcfg_do_global_parameter(lp_ctx, "usershare owner only", "yes");
2365 lpcfg_do_global_parameter(lp_ctx, "-valid", "yes");
2367 lpcfg_do_global_parameter_var(lp_ctx, "usershare path", "%s/usershares", get_dyn_STATEDIR());
2369 #ifdef DEVELOPER
2370 lpcfg_do_global_parameter_var(lp_ctx, "panic action", "/bin/sleep 999999999");
2371 #endif
2373 lpcfg_do_global_parameter(lp_ctx, "smb passwd file", get_dyn_SMB_PASSWD_FILE());
2375 lpcfg_do_global_parameter(lp_ctx, "logon home", "\\\\%N\\%U");
2377 lpcfg_do_global_parameter(lp_ctx, "logon path", "\\\\%N\\%U\\profile");
2379 lpcfg_do_global_parameter(lp_ctx, "printjob username", "%U");
2381 for (i = 0; parm_table[i].label; i++) {
2382 if (!(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2383 lp_ctx->flags[i] |= FLAG_DEFAULT;
2387 for (parm=lp_ctx->globals->param_opt; parm; parm=parm->next) {
2388 if (!(parm->priority & FLAG_CMDLINE)) {
2389 parm->priority |= FLAG_DEFAULT;
2393 return lp_ctx;
2397 * Initialise the global parameter structure.
2399 struct loadparm_context *loadparm_init_global(bool load_default)
2401 if (global_loadparm_context == NULL) {
2402 global_loadparm_context = loadparm_init(NULL);
2404 if (global_loadparm_context == NULL) {
2405 return NULL;
2407 global_loadparm_context->global = true;
2408 if (load_default && !global_loadparm_context->loaded) {
2409 lpcfg_load_default(global_loadparm_context);
2411 global_loadparm_context->refuse_free = true;
2412 return global_loadparm_context;
2416 * Initialise the global parameter structure.
2418 struct loadparm_context *loadparm_init_s3(TALLOC_CTX *mem_ctx,
2419 const struct loadparm_s3_helpers *s3_fns)
2421 struct loadparm_context *loadparm_context = talloc_zero(mem_ctx, struct loadparm_context);
2422 if (!loadparm_context) {
2423 return NULL;
2425 loadparm_context->s3_fns = s3_fns;
2426 loadparm_context->globals = s3_fns->globals;
2427 return loadparm_context;
2430 const char *lpcfg_configfile(struct loadparm_context *lp_ctx)
2432 return lp_ctx->szConfigFile;
2435 const char *lp_default_path(void)
2437 if (getenv("SMB_CONF_PATH"))
2438 return getenv("SMB_CONF_PATH");
2439 else
2440 return dyn_CONFIGFILE;
2444 * Update the internal state of a loadparm context after settings
2445 * have changed.
2447 static bool lpcfg_update(struct loadparm_context *lp_ctx)
2449 struct debug_settings settings;
2450 TALLOC_CTX *tmp_ctx;
2452 tmp_ctx = talloc_new(lp_ctx);
2453 if (tmp_ctx == NULL) {
2454 return false;
2457 lpcfg_add_auto_services(lp_ctx, lpcfg_auto_services(lp_ctx, tmp_ctx));
2459 if (!lp_ctx->globals->wins_server_list && lp_ctx->globals->we_are_a_wins_server) {
2460 lpcfg_do_global_parameter(lp_ctx, "wins server", "127.0.0.1");
2463 if (!lp_ctx->global) {
2464 TALLOC_FREE(tmp_ctx);
2465 return true;
2468 panic_action = lp_ctx->globals->panic_action;
2470 reload_charcnv(lp_ctx);
2472 ZERO_STRUCT(settings);
2473 /* Add any more debug-related smb.conf parameters created in
2474 * future here */
2475 settings.syslog = lp_ctx->globals->syslog;
2476 settings.syslog_only = lp_ctx->globals->syslog_only;
2477 settings.timestamp_logs = lp_ctx->globals->timestamp_logs;
2478 settings.debug_prefix_timestamp = lp_ctx->globals->debug_prefix_timestamp;
2479 settings.debug_hires_timestamp = lp_ctx->globals->debug_hires_timestamp;
2480 settings.debug_pid = lp_ctx->globals->debug_pid;
2481 settings.debug_uid = lp_ctx->globals->debug_uid;
2482 settings.debug_class = lp_ctx->globals->debug_class;
2483 debug_set_settings(&settings);
2485 /* FIXME: This is a bit of a hack, but we can't use a global, since
2486 * not everything that uses lp also uses the socket library */
2487 if (lpcfg_parm_bool(lp_ctx, NULL, "socket", "testnonblock", false)) {
2488 setenv("SOCKET_TESTNONBLOCK", "1", 1);
2489 } else {
2490 unsetenv("SOCKET_TESTNONBLOCK");
2493 TALLOC_FREE(tmp_ctx);
2494 return true;
2497 bool lpcfg_load_default(struct loadparm_context *lp_ctx)
2499 const char *path;
2501 path = lp_default_path();
2503 if (!file_exist(path)) {
2504 /* We allow the default smb.conf file to not exist,
2505 * basically the equivalent of an empty file. */
2506 return lpcfg_update(lp_ctx);
2509 return lpcfg_load(lp_ctx, path);
2513 * Load the services array from the services file.
2515 * Return True on success, False on failure.
2517 bool lpcfg_load(struct loadparm_context *lp_ctx, const char *filename)
2519 char *n2;
2520 bool bRetval;
2522 filename = talloc_strdup(lp_ctx, filename);
2524 lp_ctx->szConfigFile = filename;
2526 if (lp_ctx->s3_fns) {
2527 return lp_ctx->s3_fns->load(filename);
2530 lp_ctx->bInGlobalSection = true;
2531 n2 = standard_sub_basic(lp_ctx, lp_ctx->szConfigFile);
2532 DEBUG(2, ("lpcfg_load: refreshing parameters from %s\n", n2));
2534 add_to_file_list(lp_ctx, lp_ctx->szConfigFile, n2);
2536 /* We get sections first, so have to start 'behind' to make up */
2537 lp_ctx->currentService = NULL;
2538 bRetval = pm_process(n2, do_section, do_parameter, lp_ctx);
2540 /* finish up the last section */
2541 DEBUG(4, ("pm_process() returned %s\n", BOOLSTR(bRetval)));
2542 if (bRetval)
2543 if (lp_ctx->currentService != NULL)
2544 bRetval = lpcfg_service_ok(lp_ctx->currentService);
2546 bRetval = bRetval && lpcfg_update(lp_ctx);
2548 /* we do this unconditionally, so that it happens even
2549 for a missing smb.conf */
2550 reload_charcnv(lp_ctx);
2552 if (bRetval == true) {
2553 /* set this up so that any child python tasks will
2554 find the right smb.conf */
2555 setenv("SMB_CONF_PATH", filename, 1);
2557 /* set the context used by the lp_*() function
2558 varients */
2559 global_loadparm_context = lp_ctx;
2560 lp_ctx->loaded = true;
2563 return bRetval;
2567 * Return the max number of services.
2570 int lpcfg_numservices(struct loadparm_context *lp_ctx)
2572 if (lp_ctx->s3_fns) {
2573 return lp_ctx->s3_fns->get_numservices();
2576 return lp_ctx->iNumServices;
2580 * Display the contents of the services array in human-readable form.
2583 void lpcfg_dump(struct loadparm_context *lp_ctx, FILE *f, bool show_defaults,
2584 int maxtoprint)
2586 int iService;
2588 if (lp_ctx->s3_fns) {
2589 lp_ctx->s3_fns->dump(f, show_defaults, maxtoprint);
2590 return;
2593 defaults_saved = !show_defaults;
2595 dump_globals(lp_ctx, f, show_defaults);
2597 dump_a_service(lp_ctx->sDefault, lp_ctx->sDefault, f, lp_ctx->flags);
2599 for (iService = 0; iService < maxtoprint; iService++)
2600 lpcfg_dump_one(f, show_defaults, lp_ctx->services[iService], lp_ctx->sDefault);
2604 * Display the contents of one service in human-readable form.
2606 void lpcfg_dump_one(FILE *f, bool show_defaults, struct loadparm_service *service, struct loadparm_service *sDefault)
2608 if (service != NULL) {
2609 if (service->szService[0] == '\0')
2610 return;
2611 dump_a_service(service, sDefault, f, NULL);
2615 struct loadparm_service *lpcfg_servicebynum(struct loadparm_context *lp_ctx,
2616 int snum)
2618 if (lp_ctx->s3_fns) {
2619 return lp_ctx->s3_fns->get_servicebynum(snum);
2622 return lp_ctx->services[snum];
2625 struct loadparm_service *lpcfg_service(struct loadparm_context *lp_ctx,
2626 const char *service_name)
2628 int iService;
2629 char *serviceName;
2631 if (lp_ctx->s3_fns) {
2632 return lp_ctx->s3_fns->get_service(service_name);
2635 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--) {
2636 if (lp_ctx->services[iService] &&
2637 lp_ctx->services[iService]->szService) {
2639 * The substitution here is used to support %U is
2640 * service names
2642 serviceName = standard_sub_basic(
2643 lp_ctx->services[iService],
2644 lp_ctx->services[iService]->szService);
2645 if (strequal(serviceName, service_name)) {
2646 talloc_free(serviceName);
2647 return lp_ctx->services[iService];
2649 talloc_free(serviceName);
2653 DEBUG(7,("lpcfg_servicenumber: couldn't find %s\n", service_name));
2654 return NULL;
2657 const char *lpcfg_servicename(const struct loadparm_service *service)
2659 return lpcfg_string((const char *)service->szService);
2663 * A useful volume label function.
2665 const char *lpcfg_volume_label(struct loadparm_service *service, struct loadparm_service *sDefault)
2667 const char *ret;
2668 ret = lpcfg_string((const char *)((service != NULL && service->volume != NULL) ?
2669 service->volume : sDefault->volume));
2670 if (!*ret)
2671 return lpcfg_servicename(service);
2672 return ret;
2676 * Return the correct printer name.
2678 const char *lpcfg_printername(struct loadparm_service *service, struct loadparm_service *sDefault)
2680 const char *ret;
2681 ret = lpcfg_string((const char *)((service != NULL && service->_printername != NULL) ?
2682 service->_printername : sDefault->_printername));
2683 if (ret == NULL || (ret != NULL && *ret == '\0'))
2684 ret = lpcfg_servicename(service);
2686 return ret;
2691 * Return the max print jobs per queue.
2693 int lpcfg_maxprintjobs(struct loadparm_service *service, struct loadparm_service *sDefault)
2695 int maxjobs = (service != NULL) ? service->iMaxPrintJobs : sDefault->iMaxPrintJobs;
2696 if (maxjobs <= 0 || maxjobs >= PRINT_MAX_JOBID)
2697 maxjobs = PRINT_MAX_JOBID - 1;
2699 return maxjobs;
2702 struct smb_iconv_handle *lpcfg_iconv_handle(struct loadparm_context *lp_ctx)
2704 if (lp_ctx == NULL) {
2705 return get_iconv_handle();
2707 return lp_ctx->iconv_handle;
2710 _PUBLIC_ void reload_charcnv(struct loadparm_context *lp_ctx)
2712 struct smb_iconv_handle *old_ic = lp_ctx->iconv_handle;
2713 if (!lp_ctx->global) {
2714 return;
2717 if (old_ic == NULL) {
2718 old_ic = global_iconv_handle;
2720 lp_ctx->iconv_handle = smb_iconv_handle_reinit_lp(lp_ctx, lp_ctx, old_ic);
2721 global_iconv_handle = lp_ctx->iconv_handle;
2724 _PUBLIC_ char *lpcfg_tls_keyfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2726 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_keyfile(lp_ctx));
2729 _PUBLIC_ char *lpcfg_tls_certfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2731 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_certfile(lp_ctx));
2734 _PUBLIC_ char *lpcfg_tls_cafile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2736 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_cafile(lp_ctx));
2739 _PUBLIC_ char *lpcfg_tls_crlfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2741 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_crlfile(lp_ctx));
2744 _PUBLIC_ char *lpcfg_tls_dhpfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2746 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_dhpfile(lp_ctx));
2749 struct gensec_settings *lpcfg_gensec_settings(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2751 struct gensec_settings *settings = talloc_zero(mem_ctx, struct gensec_settings);
2752 if (settings == NULL)
2753 return NULL;
2754 SMB_ASSERT(lp_ctx != NULL);
2755 settings->lp_ctx = talloc_reference(settings, lp_ctx);
2756 settings->target_hostname = lpcfg_parm_string(lp_ctx, NULL, "gensec", "target_hostname");
2757 return settings;
2760 int lpcfg_server_role(struct loadparm_context *lp_ctx)
2762 int domain_master = lpcfg__domain_master(lp_ctx);
2764 return lp_find_server_role(lpcfg__server_role(lp_ctx),
2765 lpcfg__security(lp_ctx),
2766 lpcfg__domain_logons(lp_ctx),
2767 (domain_master == true) ||
2768 (domain_master == Auto));
2771 int lpcfg_security(struct loadparm_context *lp_ctx)
2773 return lp_find_security(lpcfg__server_role(lp_ctx),
2774 lpcfg__security(lp_ctx));
2777 bool lpcfg_server_signing_allowed(struct loadparm_context *lp_ctx, bool *mandatory)
2779 bool allowed = true;
2780 enum smb_signing_setting signing_setting = lpcfg_server_signing(lp_ctx);
2782 *mandatory = false;
2784 if (signing_setting == SMB_SIGNING_DEFAULT) {
2786 * If we are a domain controller, SMB signing is
2787 * really important, as it can prevent a number of
2788 * attacks on communications between us and the
2789 * clients
2791 * However, it really sucks (no sendfile, CPU
2792 * overhead) performance-wise when used on a
2793 * file server, so disable it by default
2794 * on non-DCs
2797 if (lpcfg_server_role(lp_ctx) >= ROLE_ACTIVE_DIRECTORY_DC) {
2798 signing_setting = SMB_SIGNING_REQUIRED;
2799 } else {
2800 signing_setting = SMB_SIGNING_OFF;
2804 switch (signing_setting) {
2805 case SMB_SIGNING_REQUIRED:
2806 *mandatory = true;
2807 break;
2808 case SMB_SIGNING_IF_REQUIRED:
2809 break;
2810 case SMB_SIGNING_DEFAULT:
2811 case SMB_SIGNING_OFF:
2812 allowed = false;
2813 break;
2816 return allowed;
2819 int lpcfg_tdb_hash_size(struct loadparm_context *lp_ctx, const char *name)
2821 const char *base;
2823 if (name == NULL) {
2824 return 0;
2827 base = strrchr_m(name, '/');
2828 if (base != NULL) {
2829 base += 1;
2830 } else {
2831 base = name;
2833 return lpcfg_parm_int(lp_ctx, NULL, "tdb_hashsize", base, 0);
2837 int lpcfg_tdb_flags(struct loadparm_context *lp_ctx, int tdb_flags)
2839 if (!lpcfg_use_mmap(lp_ctx)) {
2840 tdb_flags |= TDB_NOMMAP;
2842 return tdb_flags;