dbwrap: Remove dbwrap_ntdb
[Samba.git] / lib / param / loadparm.c
blobc619ad2a3a8b9951124ee0e178970c0512488971
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"
70 #include "librpc/gen_ndr/nbt.h"
72 #define standard_sub_basic talloc_strdup
74 #include "lib/param/param_global.h"
76 struct loadparm_service *lpcfg_default_service(struct loadparm_context *lp_ctx)
78 return lp_ctx->sDefault;
81 /**
82 * Convenience routine to grab string parameters into temporary memory
83 * and run standard_sub_basic on them.
85 * The buffers can be written to by
86 * callers without affecting the source string.
89 static const char *lpcfg_string(const char *s)
91 #if 0 /* until REWRITE done to make thread-safe */
92 size_t len = s ? strlen(s) : 0;
93 char *ret;
94 #endif
96 /* The follow debug is useful for tracking down memory problems
97 especially if you have an inner loop that is calling a lp_*()
98 function that returns a string. Perhaps this debug should be
99 present all the time? */
101 #if 0
102 DEBUG(10, ("lpcfg_string(%s)\n", s));
103 #endif
105 #if 0 /* until REWRITE done to make thread-safe */
106 if (!lp_talloc)
107 lp_talloc = talloc_init("lp_talloc");
109 ret = talloc_array(lp_talloc, char, len + 100); /* leave room for substitution */
111 if (!ret)
112 return NULL;
114 if (!s)
115 *ret = 0;
116 else
117 strlcpy(ret, s, len);
119 if (trim_string(ret, "\"", "\"")) {
120 if (strchr(ret,'"') != NULL)
121 strlcpy(ret, s, len);
124 standard_sub_basic(ret,len+100);
125 return (ret);
126 #endif
127 return s;
131 In this section all the functions that are used to access the
132 parameters from the rest of the program are defined
136 * the creation of separate lpcfg_*() and lp_*() functions is to allow
137 * for code compatibility between existing Samba4 and Samba3 code.
140 /* this global context supports the lp_*() function varients */
141 static struct loadparm_context *global_loadparm_context;
143 #define lpcfg_default_service global_loadparm_context->sDefault
144 #define lpcfg_global_service(i) global_loadparm_context->services[i]
146 #define FN_GLOBAL_STRING(fn_name,var_name) \
147 _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx, TALLOC_CTX *ctx) {\
148 if (lp_ctx == NULL) return NULL; \
149 if (lp_ctx->s3_fns) { \
150 return lp_ctx->globals->var_name ? lp_ctx->s3_fns->lp_string(ctx, lp_ctx->globals->var_name) : talloc_strdup(ctx, ""); \
152 return lp_ctx->globals->var_name ? talloc_strdup(ctx, lpcfg_string(lp_ctx->globals->var_name)) : talloc_strdup(ctx, ""); \
155 #define FN_GLOBAL_CONST_STRING(fn_name,var_name) \
156 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
157 if (lp_ctx == NULL) return NULL; \
158 return lp_ctx->globals->var_name ? lpcfg_string(lp_ctx->globals->var_name) : ""; \
161 #define FN_GLOBAL_LIST(fn_name,var_name) \
162 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
163 if (lp_ctx == NULL) return NULL; \
164 return lp_ctx->globals->var_name; \
167 #define FN_GLOBAL_BOOL(fn_name,var_name) \
168 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) {\
169 if (lp_ctx == NULL) return false; \
170 return lp_ctx->globals->var_name; \
173 #define FN_GLOBAL_INTEGER(fn_name,var_name) \
174 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
175 return lp_ctx->globals->var_name; \
178 /* Local parameters don't need the ->s3_fns because the struct
179 * loadparm_service is shared and lpcfg_service() checks the ->s3_fns
180 * hook */
181 #define FN_LOCAL_STRING(fn_name,val) \
182 _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_service *service, \
183 struct loadparm_service *sDefault, TALLOC_CTX *ctx) { \
184 return(talloc_strdup(ctx, lpcfg_string((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)))); \
187 #define FN_LOCAL_CONST_STRING(fn_name,val) \
188 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_service *service, \
189 struct loadparm_service *sDefault) { \
190 return((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)); \
193 #define FN_LOCAL_LIST(fn_name,val) \
194 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_service *service, \
195 struct loadparm_service *sDefault) {\
196 return(const char **)(service != NULL && service->val != NULL? service->val : sDefault->val); \
199 #define FN_LOCAL_PARM_BOOL(fn_name, val) FN_LOCAL_BOOL(fn_name, val)
201 #define FN_LOCAL_BOOL(fn_name,val) \
202 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_service *service, \
203 struct loadparm_service *sDefault) { \
204 return((service != NULL)? service->val : sDefault->val); \
207 #define FN_LOCAL_INTEGER(fn_name,val) \
208 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_service *service, \
209 struct loadparm_service *sDefault) { \
210 return((service != NULL)? service->val : sDefault->val); \
213 #define FN_LOCAL_PARM_INTEGER(fn_name, val) FN_LOCAL_INTEGER(fn_name, val)
215 #define FN_LOCAL_PARM_CHAR(fn_name,val) \
216 _PUBLIC_ char lpcfg_ ## fn_name(struct loadparm_service *service, \
217 struct loadparm_service *sDefault) { \
218 return((service != NULL)? service->val : sDefault->val); \
221 #include "lib/param/param_functions.c"
223 /* These functions cannot be auto-generated */
224 FN_LOCAL_BOOL(autoloaded, autoloaded)
225 FN_GLOBAL_CONST_STRING(dnsdomain, dnsdomain)
227 /* local prototypes */
228 static struct loadparm_service *lpcfg_getservicebyname(struct loadparm_context *lp_ctx,
229 const char *pszServiceName);
230 static bool do_section(const char *pszSectionName, void *);
231 static bool set_variable_helper(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
232 const char *pszParmName, const char *pszParmValue);
233 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
234 struct loadparm_service *service,
235 const char *pszParmName,
236 const char *pszParmValue, int flags);
238 /* The following are helper functions for parametrical options support. */
239 /* It returns a pointer to parametrical option value if it exists or NULL otherwise */
240 /* Actual parametrical functions are quite simple */
241 struct parmlist_entry *get_parametric_helper(struct loadparm_service *service,
242 const char *type, const char *option,
243 struct parmlist_entry *global_opts)
245 size_t type_len = strlen(type);
246 size_t option_len = strlen(option);
247 char param_key[type_len + option_len + 2];
248 struct parmlist_entry *data = NULL;
250 snprintf(param_key, sizeof(param_key), "%s:%s", type, option);
253 * Try to fetch the option from the data.
255 if (service != NULL) {
256 data = service->param_opt;
257 while (data != NULL) {
258 if (strwicmp(data->key, param_key) == 0) {
259 return data;
261 data = data->next;
266 * Fall back to fetching from the globals.
268 data = global_opts;
269 while (data != NULL) {
270 if (strwicmp(data->key, param_key) == 0) {
271 return data;
273 data = data->next;
276 return NULL;
279 const char *lpcfg_get_parametric(struct loadparm_context *lp_ctx,
280 struct loadparm_service *service,
281 const char *type, const char *option)
283 struct parmlist_entry *data;
285 if (lp_ctx == NULL)
286 return NULL;
288 data = get_parametric_helper(service,
289 type, option, lp_ctx->globals->param_opt);
291 if (data == NULL) {
292 return NULL;
293 } else {
294 return data->value;
300 * convenience routine to return int parameters.
302 int lp_int(const char *s)
305 if (!s || !*s) {
306 DEBUG(0,("lp_int(%s): is called with NULL!\n",s));
307 return -1;
310 return strtol(s, NULL, 0);
314 * convenience routine to return unsigned long parameters.
316 unsigned long lp_ulong(const char *s)
319 if (!s || !*s) {
320 DEBUG(0,("lp_ulong(%s): is called with NULL!\n",s));
321 return -1;
324 return strtoul(s, NULL, 0);
328 * convenience routine to return unsigned long parameters.
330 static long lp_long(const char *s)
333 if (!s) {
334 DEBUG(0,("lp_long(%s): is called with NULL!\n",s));
335 return -1;
338 return strtol(s, NULL, 0);
342 * convenience routine to return unsigned long parameters.
344 static double lp_double(const char *s)
347 if (!s) {
348 DEBUG(0,("lp_double(%s): is called with NULL!\n",s));
349 return -1;
352 return strtod(s, NULL);
356 * convenience routine to return boolean parameters.
358 bool lp_bool(const char *s)
360 bool ret = false;
362 if (!s || !*s) {
363 DEBUG(0,("lp_bool(%s): is called with NULL!\n",s));
364 return false;
367 if (!set_boolean(s, &ret)) {
368 DEBUG(0,("lp_bool(%s): value is not boolean!\n",s));
369 return false;
372 return ret;
376 * Return parametric option from a given service. Type is a part of option before ':'
377 * Parametric option has following syntax: 'Type: option = value'
378 * Returned value is allocated in 'lp_talloc' context
381 const char *lpcfg_parm_string(struct loadparm_context *lp_ctx,
382 struct loadparm_service *service, const char *type,
383 const char *option)
385 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
387 if (value)
388 return lpcfg_string(value);
390 return NULL;
394 * Return parametric option from a given service. Type is a part of option before ':'
395 * Parametric option has following syntax: 'Type: option = value'
396 * Returned value is allocated in 'lp_talloc' context
399 const char **lpcfg_parm_string_list(TALLOC_CTX *mem_ctx,
400 struct loadparm_context *lp_ctx,
401 struct loadparm_service *service,
402 const char *type,
403 const char *option, const char *separator)
405 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
407 if (value != NULL) {
408 char **l = str_list_make(mem_ctx, value, separator);
409 return discard_const_p(const char *, l);
412 return NULL;
416 * Return parametric option from a given service. Type is a part of option before ':'
417 * Parametric option has following syntax: 'Type: option = value'
420 int lpcfg_parm_int(struct loadparm_context *lp_ctx,
421 struct loadparm_service *service, const char *type,
422 const char *option, int default_v)
424 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
426 if (value)
427 return lp_int(value);
429 return default_v;
433 * Return parametric option from a given service. Type is a part of
434 * option before ':'.
435 * Parametric option has following syntax: 'Type: option = value'.
438 int lpcfg_parm_bytes(struct loadparm_context *lp_ctx,
439 struct loadparm_service *service, const char *type,
440 const char *option, int default_v)
442 uint64_t bval;
444 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
446 if (value && conv_str_size_error(value, &bval)) {
447 if (bval <= INT_MAX) {
448 return (int)bval;
452 return default_v;
456 * Return parametric option from a given service.
457 * Type is a part of option before ':'
458 * Parametric option has following syntax: 'Type: option = value'
460 unsigned long lpcfg_parm_ulong(struct loadparm_context *lp_ctx,
461 struct loadparm_service *service, const char *type,
462 const char *option, unsigned long default_v)
464 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
466 if (value)
467 return lp_ulong(value);
469 return default_v;
472 long lpcfg_parm_long(struct loadparm_context *lp_ctx,
473 struct loadparm_service *service, const char *type,
474 const char *option, long default_v)
476 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
478 if (value)
479 return lp_long(value);
481 return default_v;
484 double lpcfg_parm_double(struct loadparm_context *lp_ctx,
485 struct loadparm_service *service, const char *type,
486 const char *option, double default_v)
488 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
490 if (value != NULL)
491 return lp_double(value);
493 return default_v;
497 * Return parametric option from a given service. Type is a part of option before ':'
498 * Parametric option has following syntax: 'Type: option = value'
501 bool lpcfg_parm_bool(struct loadparm_context *lp_ctx,
502 struct loadparm_service *service, const char *type,
503 const char *option, bool default_v)
505 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
507 if (value != NULL)
508 return lp_bool(value);
510 return default_v;
515 * Set a string value, deallocating any existing space, and allocing the space
516 * for the string
518 bool lpcfg_string_set(TALLOC_CTX *mem_ctx, char **dest, const char *src)
520 talloc_free(*dest);
522 if (src == NULL)
523 src = "";
525 *dest = talloc_strdup(mem_ctx, src);
526 if ((*dest) == NULL) {
527 DEBUG(0,("Out of memory in string_set\n"));
528 return false;
531 return true;
535 * Set a string value, deallocating any existing space, and allocing the space
536 * for the string
538 bool lpcfg_string_set_upper(TALLOC_CTX *mem_ctx, char **dest, const char *src)
540 talloc_free(*dest);
542 if (src == NULL)
543 src = "";
545 *dest = strupper_talloc(mem_ctx, src);
546 if ((*dest) == NULL) {
547 DEBUG(0,("Out of memory in string_set_upper\n"));
548 return false;
551 return true;
557 * Add a new service to the services array initialising it with the given
558 * service.
561 struct loadparm_service *lpcfg_add_service(struct loadparm_context *lp_ctx,
562 const struct loadparm_service *pservice,
563 const char *name)
565 int i;
566 int num_to_alloc = lp_ctx->iNumServices + 1;
567 struct parmlist_entry *data, *pdata;
569 if (lp_ctx->s3_fns != NULL) {
570 smb_panic("Add a service should not be called on an s3 loadparm ctx");
573 if (pservice == NULL) {
574 pservice = lp_ctx->sDefault;
577 /* it might already exist */
578 if (name) {
579 struct loadparm_service *service = lpcfg_getservicebyname(lp_ctx,
580 name);
581 if (service != NULL) {
582 /* Clean all parametric options for service */
583 /* They will be added during parsing again */
584 data = service->param_opt;
585 while (data) {
586 pdata = data->next;
587 talloc_free(data);
588 data = pdata;
590 service->param_opt = NULL;
591 return service;
595 /* find an invalid one */
596 for (i = 0; i < lp_ctx->iNumServices; i++)
597 if (lp_ctx->services[i] == NULL)
598 break;
600 /* if not, then create one */
601 if (i == lp_ctx->iNumServices) {
602 struct loadparm_service **tsp;
604 tsp = talloc_realloc(lp_ctx, lp_ctx->services, struct loadparm_service *, num_to_alloc);
606 if (!tsp) {
607 DEBUG(0,("lpcfg_add_service: failed to enlarge services!\n"));
608 return NULL;
609 } else {
610 lp_ctx->services = tsp;
611 lp_ctx->services[lp_ctx->iNumServices] = NULL;
614 lp_ctx->iNumServices++;
617 lp_ctx->services[i] = talloc_zero(lp_ctx->services, struct loadparm_service);
618 if (lp_ctx->services[i] == NULL) {
619 DEBUG(0,("lpcfg_add_service: out of memory!\n"));
620 return NULL;
622 copy_service(lp_ctx->services[i], pservice, NULL);
623 if (name != NULL)
624 lpcfg_string_set(lp_ctx->services[i], &lp_ctx->services[i]->szService, name);
625 return lp_ctx->services[i];
629 * Add a new home service, with the specified home directory, defaults coming
630 * from service ifrom.
633 bool lpcfg_add_home(struct loadparm_context *lp_ctx,
634 const char *pszHomename,
635 struct loadparm_service *default_service,
636 const char *user, const char *pszHomedir)
638 struct loadparm_service *service;
640 service = lpcfg_add_service(lp_ctx, default_service, pszHomename);
642 if (service == NULL)
643 return false;
645 if (!(*(default_service->path))
646 || strequal(default_service->path, lp_ctx->sDefault->path)) {
647 service->path = talloc_strdup(service, pszHomedir);
648 } else {
649 service->path = string_sub_talloc(service, lpcfg_path(default_service, lp_ctx->sDefault, service), "%H", pszHomedir);
652 if (!(*(service->comment))) {
653 service->comment = talloc_asprintf(service, "Home directory of %s", user);
655 service->bAvailable = default_service->bAvailable;
656 service->browseable = default_service->browseable;
658 DEBUG(3, ("adding home's share [%s] for user '%s' at '%s'\n",
659 pszHomename, user, service->path));
661 return true;
665 * Add a new printer service, with defaults coming from service iFrom.
668 bool lpcfg_add_printer(struct loadparm_context *lp_ctx,
669 const char *pszPrintername,
670 struct loadparm_service *default_service)
672 const char *comment = "From Printcap";
673 struct loadparm_service *service;
674 service = lpcfg_add_service(lp_ctx, default_service, pszPrintername);
676 if (service == NULL)
677 return false;
679 /* note that we do NOT default the availability flag to True - */
680 /* we take it from the default service passed. This allows all */
681 /* dynamic printers to be disabled by disabling the [printers] */
682 /* entry (if/when the 'available' keyword is implemented!). */
684 /* the printer name is set to the service name. */
685 lpcfg_string_set(service, &service->_printername, pszPrintername);
686 lpcfg_string_set(service, &service->comment, comment);
687 service->browseable = default_service->browseable;
688 /* Printers cannot be read_only. */
689 service->read_only = false;
690 /* Printer services must be printable. */
691 service->printable = true;
693 DEBUG(3, ("adding printer service %s\n", pszPrintername));
695 return true;
699 * Map a parameter's string representation to something we can use.
700 * Returns False if the parameter string is not recognised, else TRUE.
703 int lpcfg_map_parameter(const char *pszParmName)
705 int iIndex;
707 for (iIndex = 0; parm_table[iIndex].label; iIndex++)
708 if (strwicmp(parm_table[iIndex].label, pszParmName) == 0)
709 return iIndex;
711 /* Warn only if it isn't parametric option */
712 if (strchr(pszParmName, ':') == NULL)
713 DEBUG(0, ("Unknown parameter encountered: \"%s\"\n", pszParmName));
714 /* We do return 'fail' for parametric options as well because they are
715 stored in different storage
717 return -1;
722 return the parameter structure for a parameter
724 struct parm_struct *lpcfg_parm_struct(struct loadparm_context *lp_ctx, const char *name)
726 int num = lpcfg_map_parameter(name);
728 if (num < 0) {
729 return NULL;
732 return &parm_table[num];
736 return the parameter pointer for a parameter
738 void *lpcfg_parm_ptr(struct loadparm_context *lp_ctx,
739 struct loadparm_service *service, struct parm_struct *parm)
741 if (lp_ctx->s3_fns) {
742 return lp_ctx->s3_fns->get_parm_ptr(service, parm);
745 if (service == NULL) {
746 if (parm->p_class == P_LOCAL)
747 return ((char *)lp_ctx->sDefault)+parm->offset;
748 else if (parm->p_class == P_GLOBAL)
749 return ((char *)lp_ctx->globals)+parm->offset;
750 else return NULL;
751 } else {
752 return ((char *)service) + parm->offset;
757 return the parameter pointer for a parameter
759 bool lpcfg_parm_is_cmdline(struct loadparm_context *lp_ctx, const char *name)
761 int parmnum;
763 parmnum = lpcfg_map_parameter(name);
764 if (parmnum == -1) return false;
766 return lp_ctx->flags[parmnum] & FLAG_CMDLINE;
770 * Find a service by name. Otherwise works like get_service.
773 static struct loadparm_service *lpcfg_getservicebyname(struct loadparm_context *lp_ctx,
774 const char *pszServiceName)
776 int iService;
778 if (lp_ctx->s3_fns) {
779 return lp_ctx->s3_fns->get_service(pszServiceName);
782 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--)
783 if (lp_ctx->services[iService] != NULL &&
784 strwicmp(lp_ctx->services[iService]->szService, pszServiceName) == 0) {
785 return lp_ctx->services[iService];
788 return NULL;
792 * Add a parametric option to a parmlist_entry,
793 * replacing old value, if already present.
795 void set_param_opt(TALLOC_CTX *mem_ctx,
796 struct parmlist_entry **opt_list,
797 const char *opt_name,
798 const char *opt_value,
799 unsigned priority)
801 struct parmlist_entry *new_opt, *opt;
802 bool not_added;
804 opt = *opt_list;
805 not_added = true;
807 /* Traverse destination */
808 while (opt) {
809 /* If we already have same option, override it */
810 if (strwicmp(opt->key, opt_name) == 0) {
811 if ((opt->priority & FLAG_CMDLINE) &&
812 !(priority & FLAG_CMDLINE)) {
813 /* it's been marked as not to be
814 overridden */
815 return;
817 TALLOC_FREE(opt->value);
818 TALLOC_FREE(opt->list);
819 opt->value = talloc_strdup(opt, opt_value);
820 opt->priority = priority;
821 not_added = false;
822 break;
824 opt = opt->next;
826 if (not_added) {
827 new_opt = talloc(mem_ctx, struct parmlist_entry);
828 if (new_opt == NULL) {
829 smb_panic("OOM");
832 new_opt->key = talloc_strdup(new_opt, opt_name);
833 if (new_opt->key == NULL) {
834 smb_panic("talloc_strdup failed");
837 new_opt->value = talloc_strdup(new_opt, opt_value);
838 if (new_opt->value == NULL) {
839 smb_panic("talloc_strdup failed");
842 new_opt->list = NULL;
843 new_opt->priority = priority;
844 DLIST_ADD(*opt_list, new_opt);
849 * Copy a service structure to another.
850 * If pcopymapDest is NULL then copy all fields
853 void copy_service(struct loadparm_service *pserviceDest,
854 const struct loadparm_service *pserviceSource,
855 struct bitmap *pcopymapDest)
857 int i;
858 bool bcopyall = (pcopymapDest == NULL);
859 struct parmlist_entry *data;
861 for (i = 0; parm_table[i].label; i++)
862 if (parm_table[i].p_class == P_LOCAL &&
863 (bcopyall || bitmap_query(pcopymapDest, i))) {
864 const void *src_ptr =
865 ((const char *)pserviceSource) + parm_table[i].offset;
866 void *dest_ptr =
867 ((char *)pserviceDest) + parm_table[i].offset;
869 switch (parm_table[i].type) {
870 case P_BOOL:
871 case P_BOOLREV:
872 *(bool *)dest_ptr = *(const bool *)src_ptr;
873 break;
875 case P_INTEGER:
876 case P_BYTES:
877 case P_OCTAL:
878 case P_ENUM:
879 *(int *)dest_ptr = *(const int *)src_ptr;
880 break;
882 case P_CHAR:
883 *(char *)dest_ptr = *(const char *)src_ptr;
884 break;
886 case P_STRING:
887 lpcfg_string_set(pserviceDest,
888 (char **)dest_ptr,
889 *(const char * const *)src_ptr);
890 break;
892 case P_USTRING:
893 lpcfg_string_set_upper(pserviceDest,
894 (char **)dest_ptr,
895 *(const char * const *)src_ptr);
896 break;
897 case P_CMDLIST:
898 case P_LIST:
899 TALLOC_FREE(*((char ***)dest_ptr));
900 *(char ***)dest_ptr = str_list_copy(pserviceDest,
901 *discard_const_p(const char **, src_ptr));
902 break;
903 default:
904 break;
908 if (bcopyall) {
909 init_copymap(pserviceDest);
910 if (pserviceSource->copymap)
911 bitmap_copy(pserviceDest->copymap,
912 pserviceSource->copymap);
915 for (data = pserviceSource->param_opt; data != NULL; data = data->next) {
916 set_param_opt(pserviceDest, &pserviceDest->param_opt,
917 data->key, data->value, data->priority);
922 * Check a service for consistency. Return False if the service is in any way
923 * incomplete or faulty, else True.
925 bool lpcfg_service_ok(struct loadparm_service *service)
927 bool bRetval;
929 bRetval = true;
930 if (service->szService[0] == '\0') {
931 DEBUG(0, ("The following message indicates an internal error:\n"));
932 DEBUG(0, ("No service name in service entry.\n"));
933 bRetval = false;
936 /* The [printers] entry MUST be printable. I'm all for flexibility, but */
937 /* I can't see why you'd want a non-printable printer service... */
938 if (strwicmp(service->szService, PRINTERS_NAME) == 0) {
939 if (!service->printable) {
940 DEBUG(0, ("WARNING: [%s] service MUST be printable!\n",
941 service->szService));
942 service->printable = true;
944 /* [printers] service must also be non-browsable. */
945 if (service->browseable)
946 service->browseable = false;
949 if (service->path[0] == '\0' &&
950 strwicmp(service->szService, HOMES_NAME) != 0 &&
951 service->msdfs_proxy[0] == '\0')
953 DEBUG(0, ("WARNING: No path in service %s - making it unavailable!\n",
954 service->szService));
955 service->bAvailable = false;
958 if (!service->bAvailable)
959 DEBUG(1, ("NOTE: Service %s is flagged unavailable.\n",
960 service->szService));
962 return bRetval;
966 /*******************************************************************
967 Keep a linked list of all config files so we know when one has changed
968 it's date and needs to be reloaded.
969 ********************************************************************/
971 void add_to_file_list(TALLOC_CTX *mem_ctx, struct file_lists **list,
972 const char *fname, const char *subfname)
974 struct file_lists *f = *list;
976 while (f) {
977 if (f->name && !strcmp(f->name, fname))
978 break;
979 f = f->next;
982 if (!f) {
983 f = talloc(mem_ctx, struct file_lists);
984 if (!f)
985 goto fail;
986 f->next = *list;
987 f->name = talloc_strdup(f, fname);
988 if (!f->name) {
989 TALLOC_FREE(f);
990 goto fail;
992 f->subfname = talloc_strdup(f, subfname);
993 if (!f->subfname) {
994 TALLOC_FREE(f);
995 goto fail;
997 *list = f;
998 f->modtime = file_modtime(subfname);
999 } else {
1000 time_t t = file_modtime(subfname);
1001 if (t)
1002 f->modtime = t;
1004 return;
1006 fail:
1007 DEBUG(0, ("Unable to add file to file list: %s\n", fname));
1011 /*******************************************************************
1012 Check if a config file has changed date.
1013 ********************************************************************/
1014 bool lpcfg_file_list_changed(struct loadparm_context *lp_ctx)
1016 struct file_lists *f;
1017 DEBUG(6, ("lpcfg_file_list_changed()\n"));
1019 for (f = lp_ctx->file_lists; f != NULL; f = f->next) {
1020 char *n2;
1021 time_t mod_time;
1023 n2 = standard_sub_basic(lp_ctx, f->name);
1025 DEBUGADD(6, ("file %s -> %s last mod_time: %s\n",
1026 f->name, n2, ctime(&f->modtime)));
1028 mod_time = file_modtime(n2);
1030 if (mod_time && ((f->modtime != mod_time) || (f->subfname == NULL) || (strcmp(n2, f->subfname) != 0))) {
1031 DEBUGADD(6, ("file %s modified: %s\n", n2,
1032 ctime(&mod_time)));
1033 f->modtime = mod_time;
1034 talloc_free(f->subfname);
1035 f->subfname = talloc_strdup(f, n2);
1036 TALLOC_FREE(n2);
1037 return true;
1039 TALLOC_FREE(n2);
1041 return false;
1045 * set the value for a P_ENUM
1047 bool lp_set_enum_parm( struct parm_struct *parm, const char *pszParmValue,
1048 int *ptr )
1050 int i;
1052 for (i = 0; parm->enum_list[i].name; i++) {
1053 if (strwicmp(pszParmValue, parm->enum_list[i].name) == 0) {
1054 *ptr = parm->enum_list[i].value;
1055 return true;
1058 DEBUG(0, ("WARNING: Ignoring invalid value '%s' for parameter '%s'\n",
1059 pszParmValue, parm->label));
1060 return false;
1064 /***************************************************************************
1065 Handle the "realm" parameter
1066 ***************************************************************************/
1068 bool handle_realm(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1069 const char *pszParmValue, char **ptr)
1071 char *upper;
1072 char *lower;
1074 upper = strupper_talloc(lp_ctx, pszParmValue);
1075 if (upper == NULL) {
1076 return false;
1079 lower = strlower_talloc(lp_ctx, pszParmValue);
1080 if (lower == NULL) {
1081 TALLOC_FREE(upper);
1082 return false;
1085 lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1086 lpcfg_string_set(lp_ctx->globals->ctx, &lp_ctx->globals->realm, upper);
1087 lpcfg_string_set(lp_ctx->globals->ctx, &lp_ctx->globals->dnsdomain, lower);
1089 return true;
1092 /***************************************************************************
1093 Handle the include operation.
1094 ***************************************************************************/
1096 bool handle_include(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1097 const char *pszParmValue, char **ptr)
1099 char *fname;
1101 if (lp_ctx->s3_fns) {
1102 return lp_ctx->s3_fns->lp_include(lp_ctx, service, pszParmValue, ptr);
1105 fname = standard_sub_basic(lp_ctx, pszParmValue);
1107 add_to_file_list(lp_ctx, &lp_ctx->file_lists, pszParmValue, fname);
1109 lpcfg_string_set(lp_ctx, ptr, fname);
1111 if (file_exist(fname))
1112 return pm_process(fname, do_section, lpcfg_do_parameter, lp_ctx);
1114 DEBUG(2, ("Can't find include file %s\n", fname));
1116 return false;
1119 /***************************************************************************
1120 Handle the interpretation of the copy parameter.
1121 ***************************************************************************/
1123 bool handle_copy(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1124 const char *pszParmValue, char **ptr)
1126 bool bRetval;
1127 struct loadparm_service *serviceTemp = NULL;
1129 bRetval = false;
1131 DEBUG(3, ("Copying service from service %s\n", pszParmValue));
1133 serviceTemp = lpcfg_getservicebyname(lp_ctx, pszParmValue);
1135 if (service == NULL) {
1136 DEBUG(0, ("Unable to copy service - invalid service destination.\n"));
1137 return false;
1140 if (serviceTemp != NULL) {
1141 if (serviceTemp == service) {
1142 DEBUG(0, ("Can't copy service %s - unable to copy self!\n", pszParmValue));
1143 } else {
1144 copy_service(service,
1145 serviceTemp,
1146 service->copymap);
1147 lpcfg_string_set(service, ptr, pszParmValue);
1149 bRetval = true;
1151 } else {
1152 DEBUG(0, ("Unable to copy service - source not found: %s\n",
1153 pszParmValue));
1154 bRetval = false;
1157 return bRetval;
1160 bool handle_debug_list(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1161 const char *pszParmValue, char **ptr)
1163 lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1165 return debug_parse_levels(pszParmValue);
1168 bool handle_logfile(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1169 const char *pszParmValue, char **ptr)
1171 if (lp_ctx->s3_fns == NULL) {
1172 debug_set_logfile(pszParmValue);
1175 lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1177 return true;
1181 * These special charset handling methods only run in the source3 code.
1184 bool handle_charset(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1185 const char *pszParmValue, char **ptr)
1187 if (lp_ctx->s3_fns) {
1188 if (*ptr == NULL || strcmp(*ptr, pszParmValue) != 0) {
1189 global_iconv_handle = smb_iconv_handle_reinit(NULL,
1190 lpcfg_dos_charset(lp_ctx),
1191 lpcfg_unix_charset(lp_ctx),
1192 true, global_iconv_handle);
1196 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1200 bool handle_dos_charset(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1201 const char *pszParmValue, char **ptr)
1203 bool is_utf8 = false;
1204 size_t len = strlen(pszParmValue);
1206 if (lp_ctx->s3_fns) {
1207 if (len == 4 || len == 5) {
1208 /* Don't use StrCaseCmp here as we don't want to
1209 initialize iconv. */
1210 if ((toupper_m(pszParmValue[0]) == 'U') &&
1211 (toupper_m(pszParmValue[1]) == 'T') &&
1212 (toupper_m(pszParmValue[2]) == 'F')) {
1213 if (len == 4) {
1214 if (pszParmValue[3] == '8') {
1215 is_utf8 = true;
1217 } else {
1218 if (pszParmValue[3] == '-' &&
1219 pszParmValue[4] == '8') {
1220 is_utf8 = true;
1226 if (*ptr == NULL || strcmp(*ptr, pszParmValue) != 0) {
1227 if (is_utf8) {
1228 DEBUG(0,("ERROR: invalid DOS charset: 'dos charset' must not "
1229 "be UTF8, using (default value) %s instead.\n",
1230 DEFAULT_DOS_CHARSET));
1231 pszParmValue = DEFAULT_DOS_CHARSET;
1233 global_iconv_handle = smb_iconv_handle_reinit(NULL,
1234 lpcfg_dos_charset(lp_ctx),
1235 lpcfg_unix_charset(lp_ctx),
1236 true, global_iconv_handle);
1240 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1243 bool handle_printing(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1244 const char *pszParmValue, char **ptr)
1246 static int parm_num = -1;
1248 if (parm_num == -1) {
1249 parm_num = lpcfg_map_parameter("printing");
1252 if (!lp_set_enum_parm(&parm_table[parm_num], pszParmValue, (int*)ptr)) {
1253 return false;
1256 if (lp_ctx->s3_fns) {
1257 if (service == NULL) {
1258 init_printer_values(lp_ctx, lp_ctx->globals->ctx, lp_ctx->sDefault);
1259 } else {
1260 init_printer_values(lp_ctx, service, service);
1264 return true;
1267 bool handle_ldap_debug_level(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1268 const char *pszParmValue, char **ptr)
1270 lp_ctx->globals->ldap_debug_level = lp_int(pszParmValue);
1272 if (lp_ctx->s3_fns) {
1273 lp_ctx->s3_fns->init_ldap_debugging();
1275 return true;
1278 bool handle_netbios_aliases(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1279 const char *pszParmValue, char **ptr)
1281 TALLOC_FREE(lp_ctx->globals->netbios_aliases);
1282 lp_ctx->globals->netbios_aliases = str_list_make_v3_const(lp_ctx->globals->ctx,
1283 pszParmValue, NULL);
1285 if (lp_ctx->s3_fns) {
1286 return lp_ctx->s3_fns->set_netbios_aliases(lp_ctx->globals->netbios_aliases);
1288 return true;
1292 * idmap related parameters
1295 bool handle_idmap_backend(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1296 const char *pszParmValue, char **ptr)
1298 if (lp_ctx->s3_fns) {
1299 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : backend",
1300 pszParmValue, 0);
1303 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1306 bool handle_idmap_uid(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1307 const char *pszParmValue, char **ptr)
1309 if (lp_ctx->s3_fns) {
1310 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : range",
1311 pszParmValue, 0);
1314 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1317 bool handle_idmap_gid(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1318 const char *pszParmValue, char **ptr)
1320 if (lp_ctx->s3_fns) {
1321 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : range",
1322 pszParmValue, 0);
1325 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1328 bool handle_smb_ports(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1329 const char *pszParmValue, char **ptr)
1331 static int parm_num = -1;
1332 int i;
1333 const char **list;
1335 if (!pszParmValue || !*pszParmValue) {
1336 return false;
1339 if (parm_num == -1) {
1340 parm_num = lpcfg_map_parameter("smb ports");
1343 if(!set_variable_helper(lp_ctx->globals->ctx, parm_num, ptr, "smb ports",
1344 pszParmValue)) {
1345 return false;
1348 list = lp_ctx->globals->smb_ports;
1349 if (list == NULL) {
1350 return false;
1353 /* Check that each port is a valid integer and within range */
1354 for (i = 0; list[i] != NULL; i++) {
1355 char *end = NULL;
1356 int port = 0;
1357 port = strtol(list[i], &end, 10);
1358 if (*end != '\0' || port <= 0 || port > 65535) {
1359 TALLOC_FREE(list);
1360 return false;
1364 return true;
1367 /***************************************************************************
1368 Initialise a copymap.
1369 ***************************************************************************/
1372 * Initializes service copymap
1373 * Note: pservice *must* be valid TALLOC_CTX
1375 void init_copymap(struct loadparm_service *pservice)
1377 int i;
1379 TALLOC_FREE(pservice->copymap);
1381 pservice->copymap = bitmap_talloc(pservice, num_parameters());
1382 if (!pservice->copymap) {
1383 DEBUG(0,
1384 ("Couldn't allocate copymap!! (size %d)\n",
1385 (int)num_parameters()));
1386 } else {
1387 for (i = 0; i < num_parameters(); i++) {
1388 bitmap_set(pservice->copymap, i);
1394 * Process a parametric option
1396 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
1397 struct loadparm_service *service,
1398 const char *pszParmName,
1399 const char *pszParmValue, int flags)
1401 struct parmlist_entry **data;
1402 char *name;
1403 TALLOC_CTX *mem_ctx;
1405 while (isspace((unsigned char)*pszParmName)) {
1406 pszParmName++;
1409 name = strlower_talloc(lp_ctx, pszParmName);
1410 if (!name) return false;
1412 if (service == NULL) {
1413 data = &lp_ctx->globals->param_opt;
1415 * s3 code cannot deal with parametric options stored on the globals ctx.
1417 if (lp_ctx->s3_fns != NULL) {
1418 mem_ctx = NULL;
1419 } else {
1420 mem_ctx = lp_ctx->globals->ctx;
1422 } else {
1423 data = &service->param_opt;
1424 mem_ctx = service;
1427 set_param_opt(mem_ctx, data, name, pszParmValue, flags);
1429 talloc_free(name);
1431 return true;
1434 static bool set_variable_helper(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
1435 const char *pszParmName, const char *pszParmValue)
1437 int i;
1439 /* switch on the type of variable it is */
1440 switch (parm_table[parmnum].type)
1442 case P_BOOL: {
1443 bool b;
1444 if (!set_boolean(pszParmValue, &b)) {
1445 DEBUG(0, ("set_variable_helper(%s): value is not "
1446 "boolean!\n", pszParmValue));
1447 return false;
1449 *(bool *)parm_ptr = b;
1451 break;
1453 case P_BOOLREV: {
1454 bool b;
1455 if (!set_boolean(pszParmValue, &b)) {
1456 DEBUG(0, ("set_variable_helper(%s): value is not "
1457 "boolean!\n", pszParmValue));
1458 return false;
1460 *(bool *)parm_ptr = !b;
1462 break;
1464 case P_INTEGER:
1465 *(int *)parm_ptr = lp_int(pszParmValue);
1466 break;
1468 case P_CHAR:
1469 *(char *)parm_ptr = *pszParmValue;
1470 break;
1472 case P_OCTAL:
1473 i = sscanf(pszParmValue, "%o", (int *)parm_ptr);
1474 if ( i != 1 ) {
1475 DEBUG ( 0, ("Invalid octal number %s\n", pszParmName ));
1476 return false;
1478 break;
1480 case P_BYTES:
1482 uint64_t val;
1483 if (conv_str_size_error(pszParmValue, &val)) {
1484 if (val <= INT_MAX) {
1485 *(int *)parm_ptr = (int)val;
1486 break;
1490 DEBUG(0, ("set_variable_helper(%s): value is not "
1491 "a valid size specifier!\n", pszParmValue));
1492 return false;
1495 case P_CMDLIST:
1496 TALLOC_FREE(*(char ***)parm_ptr);
1497 *(char ***)parm_ptr = str_list_make_v3(mem_ctx,
1498 pszParmValue, NULL);
1499 break;
1501 case P_LIST:
1503 char **new_list = str_list_make_v3(mem_ctx,
1504 pszParmValue, NULL);
1505 if (new_list == NULL) {
1506 break;
1509 for (i=0; new_list[i]; i++) {
1510 if (*(const char ***)parm_ptr != NULL &&
1511 new_list[i][0] == '+' &&
1512 new_list[i][1])
1514 if (!str_list_check(*(const char ***)parm_ptr,
1515 &new_list[i][1])) {
1516 *(const char ***)parm_ptr = str_list_add(*(const char ***)parm_ptr,
1517 &new_list[i][1]);
1519 } else if (*(const char ***)parm_ptr != NULL &&
1520 new_list[i][0] == '-' &&
1521 new_list[i][1])
1523 str_list_remove(*(const char ***)parm_ptr,
1524 &new_list[i][1]);
1525 } else {
1526 if (i != 0) {
1527 DEBUG(0, ("Unsupported list syntax for: %s = %s\n",
1528 pszParmName, pszParmValue));
1529 return false;
1531 *(char ***)parm_ptr = new_list;
1532 break;
1535 break;
1538 case P_STRING:
1539 lpcfg_string_set(mem_ctx, (char **)parm_ptr, pszParmValue);
1540 break;
1542 case P_USTRING:
1543 lpcfg_string_set_upper(mem_ctx, (char **)parm_ptr, pszParmValue);
1544 break;
1546 case P_ENUM:
1547 if (!lp_set_enum_parm(&parm_table[parmnum], pszParmValue, (int*)parm_ptr)) {
1548 return false;
1550 break;
1552 case P_SEP:
1553 break;
1556 return true;
1560 bool set_variable(TALLOC_CTX *mem_ctx, struct loadparm_service *service, int parmnum, void *parm_ptr,
1561 const char *pszParmName, const char *pszParmValue,
1562 struct loadparm_context *lp_ctx, bool on_globals)
1564 int i;
1565 bool ok;
1567 /* if it is a special case then go ahead */
1568 if (parm_table[parmnum].special) {
1569 ok = parm_table[parmnum].special(lp_ctx, service, pszParmValue,
1570 (char **)parm_ptr);
1571 } else {
1572 ok = set_variable_helper(mem_ctx, parmnum, parm_ptr,
1573 pszParmName, pszParmValue);
1576 if (!ok) {
1577 return false;
1580 if (on_globals && (lp_ctx->flags[parmnum] & FLAG_DEFAULT)) {
1581 lp_ctx->flags[parmnum] &= ~FLAG_DEFAULT;
1582 /* we have to also unset FLAG_DEFAULT on aliases */
1583 for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
1584 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1586 for (i=parmnum+1;i<num_parameters() && parm_table[i].offset == parm_table[parmnum].offset;i++) {
1587 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1590 return true;
1594 bool lpcfg_do_global_parameter(struct loadparm_context *lp_ctx,
1595 const char *pszParmName, const char *pszParmValue)
1597 int parmnum = lpcfg_map_parameter(pszParmName);
1598 void *parm_ptr;
1600 if (parmnum < 0) {
1601 if (strchr(pszParmName, ':')) {
1602 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName, pszParmValue, 0);
1604 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1605 return true;
1608 /* if the flag has been set on the command line, then don't allow override,
1609 but don't report an error */
1610 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1611 return true;
1614 if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
1615 DEBUG(1, ("WARNING: The \"%s\" option is deprecated\n",
1616 pszParmName));
1619 parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[parmnum]);
1621 return set_variable(lp_ctx->globals->ctx, NULL, parmnum, parm_ptr,
1622 pszParmName, pszParmValue, lp_ctx, true);
1625 bool lpcfg_do_service_parameter(struct loadparm_context *lp_ctx,
1626 struct loadparm_service *service,
1627 const char *pszParmName, const char *pszParmValue)
1629 void *parm_ptr;
1630 int i;
1631 int parmnum = lpcfg_map_parameter(pszParmName);
1633 if (parmnum < 0) {
1634 if (strchr(pszParmName, ':')) {
1635 return lp_do_parameter_parametric(lp_ctx, service, pszParmName, pszParmValue, 0);
1637 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1638 return true;
1641 /* if the flag has been set on the command line, then don't allow override,
1642 but don't report an error */
1643 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1644 return true;
1647 if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
1648 DEBUG(1, ("WARNING: The \"%s\" option is deprecated\n",
1649 pszParmName));
1652 if (parm_table[parmnum].p_class == P_GLOBAL) {
1653 DEBUG(0,
1654 ("Global parameter %s found in service section!\n",
1655 pszParmName));
1656 return true;
1658 parm_ptr = ((char *)service) + parm_table[parmnum].offset;
1660 if (!service->copymap)
1661 init_copymap(service);
1663 /* this handles the aliases - set the copymap for other
1664 * entries with the same data pointer */
1665 for (i = 0; parm_table[i].label; i++)
1666 if (parm_table[i].offset == parm_table[parmnum].offset &&
1667 parm_table[i].p_class == parm_table[parmnum].p_class)
1668 bitmap_clear(service->copymap, i);
1670 return set_variable(service, service, parmnum, parm_ptr, pszParmName,
1671 pszParmValue, lp_ctx, false);
1675 * Process a parameter.
1678 bool lpcfg_do_parameter(const char *pszParmName, const char *pszParmValue,
1679 void *userdata)
1681 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1683 if (lp_ctx->bInGlobalSection)
1684 return lpcfg_do_global_parameter(lp_ctx, pszParmName,
1685 pszParmValue);
1686 else
1687 return lpcfg_do_service_parameter(lp_ctx, lp_ctx->currentService,
1688 pszParmName, pszParmValue);
1692 variable argument do parameter
1694 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx, const char *pszParmName, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
1695 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx,
1696 const char *pszParmName, const char *fmt, ...)
1698 char *s;
1699 bool ret;
1700 va_list ap;
1702 va_start(ap, fmt);
1703 s = talloc_vasprintf(NULL, fmt, ap);
1704 va_end(ap);
1705 ret = lpcfg_do_global_parameter(lp_ctx, pszParmName, s);
1706 talloc_free(s);
1707 return ret;
1712 set a parameter from the commandline - this is called from command line parameter
1713 parsing code. It sets the parameter then marks the parameter as unable to be modified
1714 by smb.conf processing
1716 bool lpcfg_set_cmdline(struct loadparm_context *lp_ctx, const char *pszParmName,
1717 const char *pszParmValue)
1719 int parmnum;
1720 int i;
1722 while (isspace((unsigned char)*pszParmValue)) pszParmValue++;
1724 parmnum = lpcfg_map_parameter(pszParmName);
1726 if (parmnum < 0 && strchr(pszParmName, ':')) {
1727 /* set a parametric option */
1728 bool ok;
1729 ok = lp_do_parameter_parametric(lp_ctx, NULL, pszParmName,
1730 pszParmValue, FLAG_CMDLINE);
1731 if (lp_ctx->s3_fns != NULL) {
1732 if (ok) {
1733 lp_ctx->s3_fns->store_cmdline(pszParmName, pszParmValue);
1736 return ok;
1739 if (parmnum < 0) {
1740 DEBUG(0,("Unknown option '%s'\n", pszParmName));
1741 return false;
1744 /* reset the CMDLINE flag in case this has been called before */
1745 lp_ctx->flags[parmnum] &= ~FLAG_CMDLINE;
1747 if (!lpcfg_do_global_parameter(lp_ctx, pszParmName, pszParmValue)) {
1748 return false;
1751 lp_ctx->flags[parmnum] |= FLAG_CMDLINE;
1753 /* we have to also set FLAG_CMDLINE on aliases */
1754 for (i=parmnum-1;
1755 i>=0 && parm_table[i].p_class == parm_table[parmnum].p_class &&
1756 parm_table[i].offset == parm_table[parmnum].offset;
1757 i--) {
1758 lp_ctx->flags[i] |= FLAG_CMDLINE;
1760 for (i=parmnum+1;
1761 i<num_parameters() &&
1762 parm_table[i].p_class == parm_table[parmnum].p_class &&
1763 parm_table[i].offset == parm_table[parmnum].offset;
1764 i++) {
1765 lp_ctx->flags[i] |= FLAG_CMDLINE;
1768 if (lp_ctx->s3_fns != NULL) {
1769 lp_ctx->s3_fns->store_cmdline(pszParmName, pszParmValue);
1772 return true;
1776 set a option from the commandline in 'a=b' format. Use to support --option
1778 bool lpcfg_set_option(struct loadparm_context *lp_ctx, const char *option)
1780 char *p, *s;
1781 bool ret;
1783 s = talloc_strdup(NULL, option);
1784 if (!s) {
1785 return false;
1788 p = strchr(s, '=');
1789 if (!p) {
1790 talloc_free(s);
1791 return false;
1794 *p = 0;
1796 ret = lpcfg_set_cmdline(lp_ctx, s, p+1);
1797 talloc_free(s);
1798 return ret;
1802 #define BOOLSTR(b) ((b) ? "Yes" : "No")
1805 * Print a parameter of the specified type.
1808 void lpcfg_print_parameter(struct parm_struct *p, void *ptr, FILE * f)
1810 /* For the seperation of lists values that we print below */
1811 const char *list_sep = ", ";
1812 int i;
1813 switch (p->type)
1815 case P_ENUM:
1816 for (i = 0; p->enum_list[i].name; i++) {
1817 if (*(int *)ptr == p->enum_list[i].value) {
1818 fprintf(f, "%s",
1819 p->enum_list[i].name);
1820 break;
1823 break;
1825 case P_BOOL:
1826 fprintf(f, "%s", BOOLSTR(*(bool *)ptr));
1827 break;
1829 case P_BOOLREV:
1830 fprintf(f, "%s", BOOLSTR(!*(bool *)ptr));
1831 break;
1833 case P_INTEGER:
1834 case P_BYTES:
1835 fprintf(f, "%d", *(int *)ptr);
1836 break;
1838 case P_CHAR:
1839 fprintf(f, "%c", *(char *)ptr);
1840 break;
1842 case P_OCTAL: {
1843 int val = *(int *)ptr;
1844 if (val == -1) {
1845 fprintf(f, "-1");
1846 } else {
1847 fprintf(f, "0%03o", val);
1849 break;
1852 case P_CMDLIST:
1853 list_sep = " ";
1854 /* fall through */
1855 case P_LIST:
1856 if ((char ***)ptr && *(char ***)ptr) {
1857 char **list = *(char ***)ptr;
1858 for (; *list; list++) {
1859 /* surround strings with whitespace in double quotes */
1860 if (*(list+1) == NULL) {
1861 /* last item, no extra separator */
1862 list_sep = "";
1864 if ( strchr_m( *list, ' ' ) ) {
1865 fprintf(f, "\"%s\"%s", *list, list_sep);
1866 } else {
1867 fprintf(f, "%s%s", *list, list_sep);
1871 break;
1873 case P_STRING:
1874 case P_USTRING:
1875 if (*(char **)ptr) {
1876 fprintf(f, "%s", *(char **)ptr);
1878 break;
1879 case P_SEP:
1880 break;
1885 * Check if two parameters are equal.
1888 static bool lpcfg_equal_parameter(parm_type type, void *ptr1, void *ptr2)
1890 switch (type) {
1891 case P_BOOL:
1892 case P_BOOLREV:
1893 return (*((bool *)ptr1) == *((bool *)ptr2));
1895 case P_INTEGER:
1896 case P_ENUM:
1897 case P_OCTAL:
1898 case P_BYTES:
1899 return (*((int *)ptr1) == *((int *)ptr2));
1901 case P_CHAR:
1902 return (*((char *)ptr1) == *((char *)ptr2));
1904 case P_LIST:
1905 case P_CMDLIST:
1906 return str_list_equal(*(const char ***)ptr1, *(const char ***)ptr2);
1908 case P_STRING:
1909 case P_USTRING:
1911 char *p1 = *(char **)ptr1, *p2 = *(char **)ptr2;
1912 if (p1 && !*p1)
1913 p1 = NULL;
1914 if (p2 && !*p2)
1915 p2 = NULL;
1916 return (p1 == p2 || strequal(p1, p2));
1918 case P_SEP:
1919 break;
1921 return false;
1925 * Process a new section (service).
1927 * At this stage all sections are services.
1928 * Later we'll have special sections that permit server parameters to be set.
1929 * Returns True on success, False on failure.
1932 static bool do_section(const char *pszSectionName, void *userdata)
1934 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1935 bool bRetval;
1936 bool isglobal;
1938 if (lp_ctx->s3_fns != NULL) {
1939 return lp_ctx->s3_fns->do_section(pszSectionName, lp_ctx);
1942 isglobal = ((strwicmp(pszSectionName, GLOBAL_NAME) == 0) ||
1943 (strwicmp(pszSectionName, GLOBAL_NAME2) == 0));
1945 bRetval = false;
1947 /* if we've just struck a global section, note the fact. */
1948 lp_ctx->bInGlobalSection = isglobal;
1950 /* check for multiple global sections */
1951 if (lp_ctx->bInGlobalSection) {
1952 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1953 return true;
1956 /* if we have a current service, tidy it up before moving on */
1957 bRetval = true;
1959 if (lp_ctx->currentService != NULL)
1960 bRetval = lpcfg_service_ok(lp_ctx->currentService);
1962 /* if all is still well, move to the next record in the services array */
1963 if (bRetval) {
1964 /* We put this here to avoid an odd message order if messages are */
1965 /* issued by the post-processing of a previous section. */
1966 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1968 if ((lp_ctx->currentService = lpcfg_add_service(lp_ctx, lp_ctx->sDefault,
1969 pszSectionName))
1970 == NULL) {
1971 DEBUG(0, ("Failed to add a new service\n"));
1972 return false;
1976 return bRetval;
1981 * Determine if a particular base parameter is currently set to the default value.
1984 static bool is_default(void *base_structure, int i)
1986 void *def_ptr = ((char *)base_structure) + parm_table[i].offset;
1987 switch (parm_table[i].type) {
1988 case P_CMDLIST:
1989 case P_LIST:
1990 return str_list_equal((const char * const *)parm_table[i].def.lvalue,
1991 *(const char * const **)def_ptr);
1992 case P_STRING:
1993 case P_USTRING:
1994 return strequal(parm_table[i].def.svalue,
1995 *(char **)def_ptr);
1996 case P_BOOL:
1997 case P_BOOLREV:
1998 return parm_table[i].def.bvalue ==
1999 *(bool *)def_ptr;
2000 case P_INTEGER:
2001 case P_CHAR:
2002 case P_OCTAL:
2003 case P_BYTES:
2004 case P_ENUM:
2005 return parm_table[i].def.ivalue ==
2006 *(int *)def_ptr;
2007 case P_SEP:
2008 break;
2010 return false;
2014 *Display the contents of the global structure.
2017 void lpcfg_dump_globals(struct loadparm_context *lp_ctx, FILE *f,
2018 bool show_defaults)
2020 int i;
2021 struct parmlist_entry *data;
2023 fprintf(f, "# Global parameters\n[global]\n");
2025 for (i = 0; parm_table[i].label; i++)
2026 if (parm_table[i].p_class == P_GLOBAL &&
2027 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset))) {
2028 if (!show_defaults) {
2029 if (lp_ctx->flags && (lp_ctx->flags[i] & FLAG_DEFAULT)) {
2030 continue;
2033 if (is_default(lp_ctx->globals, i)) {
2034 continue;
2038 fprintf(f, "\t%s = ", parm_table[i].label);
2039 lpcfg_print_parameter(&parm_table[i], lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[i]), f);
2040 fprintf(f, "\n");
2042 if (lp_ctx->globals->param_opt != NULL) {
2043 for (data = lp_ctx->globals->param_opt; data;
2044 data = data->next) {
2045 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
2046 continue;
2048 fprintf(f, "\t%s = %s\n", data->key, data->value);
2055 * Display the contents of a single services record.
2058 void lpcfg_dump_a_service(struct loadparm_service * pService, struct loadparm_service *sDefault, FILE * f,
2059 unsigned int *flags, bool show_defaults)
2061 int i;
2062 struct parmlist_entry *data;
2064 if (pService != sDefault)
2065 fprintf(f, "\n[%s]\n", pService->szService);
2067 for (i = 0; parm_table[i].label; i++) {
2068 if (parm_table[i].p_class == P_LOCAL &&
2069 (*parm_table[i].label != '-') &&
2070 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset)))
2072 if (pService == sDefault) {
2073 if (!show_defaults) {
2074 if (flags && (flags[i] & FLAG_DEFAULT)) {
2075 continue;
2078 if (is_default(sDefault, i)) {
2079 continue;
2082 } else {
2083 if (lpcfg_equal_parameter(parm_table[i].type,
2084 ((char *)pService) +
2085 parm_table[i].offset,
2086 ((char *)sDefault) +
2087 parm_table[i].offset))
2088 continue;
2091 fprintf(f, "\t%s = ", parm_table[i].label);
2092 lpcfg_print_parameter(&parm_table[i],
2093 ((char *)pService) + parm_table[i].offset, f);
2094 fprintf(f, "\n");
2097 if (pService->param_opt != NULL) {
2098 for (data = pService->param_opt; data; data = data->next) {
2099 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
2100 continue;
2102 fprintf(f, "\t%s = %s\n", data->key, data->value);
2107 bool lpcfg_dump_a_parameter(struct loadparm_context *lp_ctx,
2108 struct loadparm_service *service,
2109 const char *parm_name, FILE * f)
2111 struct parm_struct *parm;
2112 void *ptr;
2113 char *local_parm_name;
2114 char *parm_opt;
2115 const char *parm_opt_value;
2117 /* check for parametrical option */
2118 local_parm_name = talloc_strdup(lp_ctx, parm_name);
2119 if (local_parm_name == NULL) {
2120 return false;
2123 parm_opt = strchr( local_parm_name, ':');
2125 if (parm_opt) {
2126 *parm_opt = '\0';
2127 parm_opt++;
2128 if (strlen(parm_opt)) {
2129 parm_opt_value = lpcfg_parm_string(lp_ctx, service,
2130 local_parm_name, parm_opt);
2131 if (parm_opt_value) {
2132 fprintf(f, "%s\n", parm_opt_value);
2133 return true;
2136 return false;
2139 /* parameter is not parametric, search the table */
2140 parm = lpcfg_parm_struct(lp_ctx, parm_name);
2141 if (!parm) {
2142 return false;
2145 if (service != NULL && parm->p_class == P_GLOBAL) {
2146 return false;
2149 ptr = lpcfg_parm_ptr(lp_ctx, service,parm);
2151 lpcfg_print_parameter(parm, ptr, f);
2152 fprintf(f, "\n");
2153 return true;
2157 * Auto-load some home services.
2159 static void lpcfg_add_auto_services(struct loadparm_context *lp_ctx,
2160 const char *str)
2162 return;
2165 /***************************************************************************
2166 Initialise the sDefault parameter structure for the printer values.
2167 ***************************************************************************/
2169 void init_printer_values(struct loadparm_context *lp_ctx, TALLOC_CTX *ctx,
2170 struct loadparm_service *pService)
2172 /* choose defaults depending on the type of printing */
2173 switch (pService->printing) {
2174 case PRINT_BSD:
2175 case PRINT_AIX:
2176 case PRINT_LPRNT:
2177 case PRINT_LPROS2:
2178 lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P'%p'");
2179 lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P'%p' %j");
2180 lpcfg_string_set(ctx, &pService->print_command, "lpr -r -P'%p' %s");
2181 break;
2183 case PRINT_LPRNG:
2184 case PRINT_PLP:
2185 lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P'%p'");
2186 lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P'%p' %j");
2187 lpcfg_string_set(ctx, &pService->print_command, "lpr -r -P'%p' %s");
2188 lpcfg_string_set(ctx, &pService->queuepause_command, "lpc stop '%p'");
2189 lpcfg_string_set(ctx, &pService->queueresume_command, "lpc start '%p'");
2190 lpcfg_string_set(ctx, &pService->lppause_command, "lpc hold '%p' %j");
2191 lpcfg_string_set(ctx, &pService->lpresume_command, "lpc release '%p' %j");
2192 break;
2194 case PRINT_CUPS:
2195 case PRINT_IPRINT:
2196 /* set the lpq command to contain the destination printer
2197 name only. This is used by cups_queue_get() */
2198 lpcfg_string_set(ctx, &pService->lpq_command, "%p");
2199 lpcfg_string_set(ctx, &pService->lprm_command, "");
2200 lpcfg_string_set(ctx, &pService->print_command, "");
2201 lpcfg_string_set(ctx, &pService->lppause_command, "");
2202 lpcfg_string_set(ctx, &pService->lpresume_command, "");
2203 lpcfg_string_set(ctx, &pService->queuepause_command, "");
2204 lpcfg_string_set(ctx, &pService->queueresume_command, "");
2205 break;
2207 case PRINT_SYSV:
2208 case PRINT_HPUX:
2209 lpcfg_string_set(ctx, &pService->lpq_command, "lpstat -o%p");
2210 lpcfg_string_set(ctx, &pService->lprm_command, "cancel %p-%j");
2211 lpcfg_string_set(ctx, &pService->print_command, "lp -c -d%p %s; rm %s");
2212 lpcfg_string_set(ctx, &pService->queuepause_command, "disable %p");
2213 lpcfg_string_set(ctx, &pService->queueresume_command, "enable %p");
2214 #ifndef HPUX
2215 lpcfg_string_set(ctx, &pService->lppause_command, "lp -i %p-%j -H hold");
2216 lpcfg_string_set(ctx, &pService->lpresume_command, "lp -i %p-%j -H resume");
2217 #endif /* HPUX */
2218 break;
2220 case PRINT_QNX:
2221 lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P%p");
2222 lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P%p %j");
2223 lpcfg_string_set(ctx, &pService->print_command, "lp -r -P%p %s");
2224 break;
2226 #if defined(DEVELOPER) || defined(ENABLE_SELFTEST)
2228 case PRINT_TEST:
2229 case PRINT_VLP: {
2230 const char *tdbfile;
2231 TALLOC_CTX *tmp_ctx = talloc_new(ctx);
2232 const char *tmp;
2234 tmp = lpcfg_parm_string(lp_ctx, NULL, "vlp", "tdbfile");
2235 if (tmp == NULL) {
2236 tmp = "/tmp/vlp.tdb";
2239 tdbfile = talloc_asprintf(tmp_ctx, "tdbfile=%s", tmp);
2240 if (tdbfile == NULL) {
2241 tdbfile="tdbfile=/tmp/vlp.tdb";
2244 tmp = talloc_asprintf(tmp_ctx, "vlp %s print %%p %%s",
2245 tdbfile);
2246 lpcfg_string_set(ctx, &pService->print_command,
2247 tmp ? tmp : "vlp print %p %s");
2249 tmp = talloc_asprintf(tmp_ctx, "vlp %s lpq %%p",
2250 tdbfile);
2251 lpcfg_string_set(ctx, &pService->lpq_command,
2252 tmp ? tmp : "vlp lpq %p");
2254 tmp = talloc_asprintf(tmp_ctx, "vlp %s lprm %%p %%j",
2255 tdbfile);
2256 lpcfg_string_set(ctx, &pService->lprm_command,
2257 tmp ? tmp : "vlp lprm %p %j");
2259 tmp = talloc_asprintf(tmp_ctx, "vlp %s lppause %%p %%j",
2260 tdbfile);
2261 lpcfg_string_set(ctx, &pService->lppause_command,
2262 tmp ? tmp : "vlp lppause %p %j");
2264 tmp = talloc_asprintf(tmp_ctx, "vlp %s lpresume %%p %%j",
2265 tdbfile);
2266 lpcfg_string_set(ctx, &pService->lpresume_command,
2267 tmp ? tmp : "vlp lpresume %p %j");
2269 tmp = talloc_asprintf(tmp_ctx, "vlp %s queuepause %%p",
2270 tdbfile);
2271 lpcfg_string_set(ctx, &pService->queuepause_command,
2272 tmp ? tmp : "vlp queuepause %p");
2274 tmp = talloc_asprintf(tmp_ctx, "vlp %s queueresume %%p",
2275 tdbfile);
2276 lpcfg_string_set(ctx, &pService->queueresume_command,
2277 tmp ? tmp : "vlp queueresume %p");
2278 TALLOC_FREE(tmp_ctx);
2280 break;
2282 #endif /* DEVELOPER */
2288 * Unload unused services.
2291 void lpcfg_killunused(struct loadparm_context *lp_ctx,
2292 struct smbsrv_connection *smb,
2293 bool (*snumused) (struct smbsrv_connection *, int))
2295 int i;
2297 if (lp_ctx->s3_fns != NULL) {
2298 smb_panic("Cannot be used from an s3 loadparm ctx");
2301 for (i = 0; i < lp_ctx->iNumServices; i++) {
2302 if (lp_ctx->services[i] == NULL)
2303 continue;
2305 if (!snumused || !snumused(smb, i)) {
2306 talloc_free(lp_ctx->services[i]);
2307 lp_ctx->services[i] = NULL;
2313 static int lpcfg_destructor(struct loadparm_context *lp_ctx)
2315 struct parmlist_entry *data;
2317 if (lp_ctx->refuse_free) {
2318 /* someone is trying to free the
2319 global_loadparm_context.
2320 We can't allow that. */
2321 return -1;
2324 if (lp_ctx->globals->param_opt != NULL) {
2325 struct parmlist_entry *next;
2326 for (data = lp_ctx->globals->param_opt; data; data=next) {
2327 next = data->next;
2328 if (data->priority & FLAG_CMDLINE) continue;
2329 DLIST_REMOVE(lp_ctx->globals->param_opt, data);
2330 talloc_free(data);
2334 return 0;
2337 struct defaults_hook_data {
2338 const char *name;
2339 lpcfg_defaults_hook hook;
2340 struct defaults_hook_data *prev, *next;
2341 } *defaults_hooks = NULL;
2344 bool lpcfg_register_defaults_hook(const char *name, lpcfg_defaults_hook hook)
2346 struct defaults_hook_data *hook_data = talloc(talloc_autofree_context(),
2347 struct defaults_hook_data);
2348 hook_data->name = talloc_strdup(hook_data, name);
2349 hook_data->hook = hook;
2350 DLIST_ADD(defaults_hooks, hook_data);
2351 return false;
2355 * Initialise the global parameter structure.
2357 * Note that most callers should use loadparm_init_global() instead
2359 struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
2361 int i;
2362 char *myname;
2363 struct loadparm_context *lp_ctx;
2364 struct parmlist_entry *parm;
2365 char *logfile;
2366 struct defaults_hook_data *defaults_hook;
2368 lp_ctx = talloc_zero(mem_ctx, struct loadparm_context);
2369 if (lp_ctx == NULL)
2370 return NULL;
2372 talloc_set_destructor(lp_ctx, lpcfg_destructor);
2373 lp_ctx->bInGlobalSection = true;
2374 lp_ctx->globals = talloc_zero(lp_ctx, struct loadparm_global);
2375 /* This appears odd, but globals in s3 isn't a pointer */
2376 lp_ctx->globals->ctx = lp_ctx->globals;
2377 lp_ctx->sDefault = talloc_zero(lp_ctx, struct loadparm_service);
2378 lp_ctx->flags = talloc_zero_array(lp_ctx, unsigned int, num_parameters());
2380 lp_ctx->sDefault->iMaxPrintJobs = 1000;
2381 lp_ctx->sDefault->bAvailable = true;
2382 lp_ctx->sDefault->browseable = true;
2383 lp_ctx->sDefault->read_only = true;
2384 lp_ctx->sDefault->map_archive = true;
2385 lp_ctx->sDefault->strict_locking = true;
2386 lp_ctx->sDefault->oplocks = true;
2387 lp_ctx->sDefault->create_mask = 0744;
2388 lp_ctx->sDefault->force_create_mode = 0000;
2389 lp_ctx->sDefault->directory_mask = 0755;
2390 lp_ctx->sDefault->force_directory_mode = 0000;
2392 DEBUG(3, ("Initialising global parameters\n"));
2394 for (i = 0; parm_table[i].label; i++) {
2395 if ((parm_table[i].type == P_STRING ||
2396 parm_table[i].type == P_USTRING) &&
2397 !(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2398 char **r;
2399 if (parm_table[i].p_class == P_LOCAL) {
2400 r = (char **)(((char *)lp_ctx->sDefault) + parm_table[i].offset);
2401 } else {
2402 r = (char **)(((char *)lp_ctx->globals) + parm_table[i].offset);
2404 *r = talloc_strdup(lp_ctx, "");
2408 logfile = talloc_asprintf(lp_ctx, "%s/log.samba", dyn_LOGFILEBASE);
2409 lpcfg_do_global_parameter(lp_ctx, "log file", logfile);
2410 talloc_free(logfile);
2412 lpcfg_do_global_parameter(lp_ctx, "log level", "0");
2414 lpcfg_do_global_parameter(lp_ctx, "syslog", "1");
2415 lpcfg_do_global_parameter(lp_ctx, "syslog only", "No");
2416 lpcfg_do_global_parameter(lp_ctx, "debug timestamp", "Yes");
2417 lpcfg_do_global_parameter(lp_ctx, "debug prefix timestamp", "No");
2418 lpcfg_do_global_parameter(lp_ctx, "debug hires timestamp", "Yes");
2419 lpcfg_do_global_parameter(lp_ctx, "debug pid", "No");
2420 lpcfg_do_global_parameter(lp_ctx, "debug uid", "No");
2421 lpcfg_do_global_parameter(lp_ctx, "debug class", "No");
2423 lpcfg_do_global_parameter(lp_ctx, "share backend", "classic");
2425 lpcfg_do_global_parameter(lp_ctx, "server role", "auto");
2426 lpcfg_do_global_parameter(lp_ctx, "domain logons", "No");
2427 lpcfg_do_global_parameter(lp_ctx, "domain master", "Auto");
2429 /* options that can be set on the command line must be initialised via
2430 the slower lpcfg_do_global_parameter() to ensure that FLAG_CMDLINE is obeyed */
2431 #ifdef TCP_NODELAY
2432 lpcfg_do_global_parameter(lp_ctx, "socket options", "TCP_NODELAY");
2433 #endif
2434 lpcfg_do_global_parameter(lp_ctx, "workgroup", DEFAULT_WORKGROUP);
2435 myname = get_myname(lp_ctx);
2436 lpcfg_do_global_parameter(lp_ctx, "netbios name", myname);
2437 talloc_free(myname);
2438 lpcfg_do_global_parameter(lp_ctx, "name resolve order", "lmhosts wins host bcast");
2440 lpcfg_do_global_parameter(lp_ctx, "fstype", "NTFS");
2442 lpcfg_do_global_parameter(lp_ctx, "ntvfs handler", "unixuid default");
2443 lpcfg_do_global_parameter(lp_ctx, "max connections", "0");
2445 lpcfg_do_global_parameter(lp_ctx, "dcerpc endpoint servers", "epmapper wkssvc rpcecho samr netlogon lsarpc spoolss drsuapi dssetup unixinfo browser eventlog6 backupkey dnsserver");
2446 lpcfg_do_global_parameter(lp_ctx, "server services", "s3fs rpc nbt wrepl ldap cldap kdc drepl winbindd ntp_signd kcc dnsupdate dns");
2447 lpcfg_do_global_parameter(lp_ctx, "kccsrv:samba_kcc", "false");
2448 /* the winbind method for domain controllers is for both RODC
2449 auth forwarding and for trusted domains */
2450 lpcfg_do_global_parameter(lp_ctx, "private dir", dyn_PRIVATE_DIR);
2451 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_LOCAL_MACHINE", "hklm.ldb");
2453 /* This hive should be dynamically generated by Samba using
2454 data from the sam, but for the moment leave it in a tdb to
2455 keep regedt32 from popping up an annoying dialog. */
2456 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_USERS", "hku.ldb");
2458 /* using UTF8 by default allows us to support all chars */
2459 lpcfg_do_global_parameter(lp_ctx, "unix charset", "UTF-8");
2461 /* Use codepage 850 as a default for the dos character set */
2462 lpcfg_do_global_parameter(lp_ctx, "dos charset", "CP850");
2465 * Allow the default PASSWD_CHAT to be overridden in local.h.
2467 lpcfg_do_global_parameter(lp_ctx, "passwd chat", DEFAULT_PASSWD_CHAT);
2469 lpcfg_do_global_parameter(lp_ctx, "pid directory", dyn_PIDDIR);
2470 lpcfg_do_global_parameter(lp_ctx, "lock dir", dyn_LOCKDIR);
2471 lpcfg_do_global_parameter(lp_ctx, "state directory", dyn_STATEDIR);
2472 lpcfg_do_global_parameter(lp_ctx, "cache directory", dyn_CACHEDIR);
2473 lpcfg_do_global_parameter(lp_ctx, "ncalrpc dir", dyn_NCALRPCDIR);
2475 lpcfg_do_global_parameter(lp_ctx, "nbt client socket address", "0.0.0.0");
2476 lpcfg_do_global_parameter_var(lp_ctx, "server string",
2477 "Samba %s", SAMBA_VERSION_STRING);
2479 lpcfg_do_global_parameter(lp_ctx, "password server", "*");
2481 lpcfg_do_global_parameter(lp_ctx, "max mux", "50");
2482 lpcfg_do_global_parameter(lp_ctx, "max xmit", "16644");
2483 lpcfg_do_global_parameter(lp_ctx, "host msdfs", "true");
2485 lpcfg_do_global_parameter(lp_ctx, "LargeReadwrite", "True");
2486 lpcfg_do_global_parameter(lp_ctx, "server min protocol", "LANMAN1");
2487 lpcfg_do_global_parameter(lp_ctx, "server max protocol", "SMB3");
2488 lpcfg_do_global_parameter(lp_ctx, "client min protocol", "CORE");
2489 lpcfg_do_global_parameter(lp_ctx, "client max protocol", "default");
2490 lpcfg_do_global_parameter(lp_ctx, "security", "AUTO");
2491 lpcfg_do_global_parameter(lp_ctx, "EncryptPasswords", "True");
2492 lpcfg_do_global_parameter(lp_ctx, "ReadRaw", "True");
2493 lpcfg_do_global_parameter(lp_ctx, "WriteRaw", "True");
2494 lpcfg_do_global_parameter(lp_ctx, "NullPasswords", "False");
2495 lpcfg_do_global_parameter(lp_ctx, "old password allowed period", "60");
2496 lpcfg_do_global_parameter(lp_ctx, "ObeyPamRestrictions", "False");
2498 lpcfg_do_global_parameter(lp_ctx, "TimeServer", "False");
2499 lpcfg_do_global_parameter(lp_ctx, "BindInterfacesOnly", "False");
2500 lpcfg_do_global_parameter(lp_ctx, "Unicode", "True");
2501 lpcfg_do_global_parameter(lp_ctx, "ClientLanManAuth", "False");
2502 lpcfg_do_global_parameter(lp_ctx, "ClientNTLMv2Auth", "True");
2503 lpcfg_do_global_parameter(lp_ctx, "LanmanAuth", "False");
2504 lpcfg_do_global_parameter(lp_ctx, "NTLMAuth", "True");
2505 lpcfg_do_global_parameter(lp_ctx, "client use spnego principal", "False");
2507 lpcfg_do_global_parameter(lp_ctx, "UnixExtensions", "True");
2509 lpcfg_do_global_parameter(lp_ctx, "PreferredMaster", "Auto");
2510 lpcfg_do_global_parameter(lp_ctx, "LocalMaster", "True");
2512 lpcfg_do_global_parameter(lp_ctx, "wins support", "False");
2513 lpcfg_do_global_parameter(lp_ctx, "dns proxy", "True");
2515 lpcfg_do_global_parameter(lp_ctx, "winbind separator", "\\");
2516 lpcfg_do_global_parameter(lp_ctx, "winbind sealed pipes", "True");
2517 lpcfg_do_global_parameter(lp_ctx, "require strong key", "True");
2518 lpcfg_do_global_parameter(lp_ctx, "winbindd socket directory", dyn_WINBINDD_SOCKET_DIR);
2519 lpcfg_do_global_parameter(lp_ctx, "winbindd privileged socket directory", dyn_WINBINDD_PRIVILEGED_SOCKET_DIR);
2520 lpcfg_do_global_parameter(lp_ctx, "ntp signd socket directory", dyn_NTP_SIGND_SOCKET_DIR);
2521 lpcfg_do_global_parameter_var(lp_ctx, "dns update command", "%s/samba_dnsupdate", dyn_SCRIPTSBINDIR);
2522 lpcfg_do_global_parameter_var(lp_ctx, "spn update command", "%s/samba_spnupdate", dyn_SCRIPTSBINDIR);
2523 lpcfg_do_global_parameter_var(lp_ctx, "samba kcc command",
2524 "%s/samba_kcc", dyn_SCRIPTSBINDIR);
2525 lpcfg_do_global_parameter(lp_ctx, "template shell", "/bin/false");
2526 lpcfg_do_global_parameter(lp_ctx, "template homedir", "/home/%D/%U");
2528 lpcfg_do_global_parameter(lp_ctx, "client signing", "default");
2529 lpcfg_do_global_parameter(lp_ctx, "server signing", "default");
2531 lpcfg_do_global_parameter(lp_ctx, "use spnego", "True");
2533 lpcfg_do_global_parameter(lp_ctx, "use mmap", "True");
2535 lpcfg_do_global_parameter(lp_ctx, "smb ports", "445 139");
2536 lpcfg_do_global_parameter_var(lp_ctx, "nbt port", "%d", NBT_NAME_SERVICE_PORT);
2537 lpcfg_do_global_parameter_var(lp_ctx, "dgram port", "%d", NBT_DGRAM_SERVICE_PORT);
2538 lpcfg_do_global_parameter(lp_ctx, "cldap port", "389");
2539 lpcfg_do_global_parameter(lp_ctx, "krb5 port", "88");
2540 lpcfg_do_global_parameter(lp_ctx, "kpasswd port", "464");
2541 lpcfg_do_global_parameter(lp_ctx, "web port", "901");
2543 lpcfg_do_global_parameter(lp_ctx, "nt status support", "True");
2545 lpcfg_do_global_parameter(lp_ctx, "max wins ttl", "518400"); /* 6 days */
2546 lpcfg_do_global_parameter(lp_ctx, "min wins ttl", "21600");
2548 lpcfg_do_global_parameter(lp_ctx, "tls enabled", "True");
2549 lpcfg_do_global_parameter(lp_ctx, "tls keyfile", "tls/key.pem");
2550 lpcfg_do_global_parameter(lp_ctx, "tls certfile", "tls/cert.pem");
2551 lpcfg_do_global_parameter(lp_ctx, "tls cafile", "tls/ca.pem");
2552 lpcfg_do_global_parameter(lp_ctx, "prefork children:smb", "4");
2554 lpcfg_do_global_parameter(lp_ctx, "rndc command", "/usr/sbin/rndc");
2555 lpcfg_do_global_parameter(lp_ctx, "nsupdate command", "/usr/bin/nsupdate -g");
2557 lpcfg_do_global_parameter(lp_ctx, "allow dns updates", "secure only");
2558 lpcfg_do_global_parameter(lp_ctx, "dns forwarder", "");
2560 lpcfg_do_global_parameter(lp_ctx, "algorithmic rid base", "1000");
2562 lpcfg_do_global_parameter(lp_ctx, "enhanced browsing", "True");
2564 lpcfg_do_global_parameter(lp_ctx, "winbind nss info", "template");
2566 lpcfg_do_global_parameter(lp_ctx, "server schannel", "Auto");
2568 lpcfg_do_global_parameter(lp_ctx, "short preserve case", "True");
2570 lpcfg_do_global_parameter(lp_ctx, "max open files", "16384");
2572 lpcfg_do_global_parameter(lp_ctx, "cups connection timeout", "30");
2574 lpcfg_do_global_parameter(lp_ctx, "locking", "True");
2576 lpcfg_do_global_parameter(lp_ctx, "block size", "1024");
2578 lpcfg_do_global_parameter(lp_ctx, "client use spnego", "True");
2580 lpcfg_do_global_parameter(lp_ctx, "change notify", "True");
2582 lpcfg_do_global_parameter(lp_ctx, "name cache timeout", "660");
2584 lpcfg_do_global_parameter(lp_ctx, "defer sharing violations", "True");
2586 lpcfg_do_global_parameter(lp_ctx, "ldap replication sleep", "1000");
2588 lpcfg_do_global_parameter(lp_ctx, "idmap backend", "tdb");
2590 lpcfg_do_global_parameter(lp_ctx, "enable privileges", "True");
2592 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max write", "%u", DEFAULT_SMB2_MAX_WRITE);
2594 lpcfg_do_global_parameter(lp_ctx, "passdb backend", "tdbsam");
2596 lpcfg_do_global_parameter(lp_ctx, "getwd cache", "True");
2598 lpcfg_do_global_parameter(lp_ctx, "winbind nested groups", "True");
2600 lpcfg_do_global_parameter(lp_ctx, "mangled names", "True");
2602 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max credits", "%u", DEFAULT_SMB2_MAX_CREDITS);
2604 lpcfg_do_global_parameter(lp_ctx, "ldap ssl", "start tls");
2606 lpcfg_do_global_parameter(lp_ctx, "ldap deref", "auto");
2608 lpcfg_do_global_parameter(lp_ctx, "lm interval", "60");
2610 lpcfg_do_global_parameter(lp_ctx, "mangling method", "hash2");
2612 lpcfg_do_global_parameter(lp_ctx, "hide dot files", "True");
2614 lpcfg_do_global_parameter(lp_ctx, "browse list", "True");
2616 lpcfg_do_global_parameter(lp_ctx, "passwd chat timeout", "2");
2618 lpcfg_do_global_parameter(lp_ctx, "guest account", GUEST_ACCOUNT);
2620 lpcfg_do_global_parameter(lp_ctx, "client schannel", "auto");
2622 lpcfg_do_global_parameter(lp_ctx, "smb encrypt", "default");
2624 lpcfg_do_global_parameter(lp_ctx, "max log size", "5000");
2626 lpcfg_do_global_parameter(lp_ctx, "idmap negative cache time", "120");
2628 lpcfg_do_global_parameter(lp_ctx, "ldap follow referral", "auto");
2630 lpcfg_do_global_parameter(lp_ctx, "multicast dns register", "yes");
2632 lpcfg_do_global_parameter(lp_ctx, "winbind reconnect delay", "30");
2634 lpcfg_do_global_parameter(lp_ctx, "winbind request timeout", "60");
2636 lpcfg_do_global_parameter(lp_ctx, "nt acl support", "yes");
2638 lpcfg_do_global_parameter(lp_ctx, "acl check permissions", "yes");
2640 lpcfg_do_global_parameter(lp_ctx, "keepalive", "300");
2642 lpcfg_do_global_parameter(lp_ctx, "smbd profiling level", "off");
2644 lpcfg_do_global_parameter(lp_ctx, "winbind cache time", "300");
2646 lpcfg_do_global_parameter(lp_ctx, "level2 oplocks", "yes");
2648 lpcfg_do_global_parameter(lp_ctx, "show add printer wizard", "yes");
2650 lpcfg_do_global_parameter(lp_ctx, "allocation roundup size", "1048576");
2652 lpcfg_do_global_parameter(lp_ctx, "ldap page size", "1024");
2654 lpcfg_do_global_parameter(lp_ctx, "kernel share modes", "yes");
2656 lpcfg_do_global_parameter(lp_ctx, "strict locking", "Auto");
2658 lpcfg_do_global_parameter(lp_ctx, "map readonly", "yes");
2660 lpcfg_do_global_parameter(lp_ctx, "allow trusted domains", "yes");
2662 lpcfg_do_global_parameter(lp_ctx, "default devmode", "yes");
2664 lpcfg_do_global_parameter(lp_ctx, "os level", "20");
2666 lpcfg_do_global_parameter(lp_ctx, "dos filetimes", "yes");
2668 lpcfg_do_global_parameter(lp_ctx, "mangling char", "~");
2670 lpcfg_do_global_parameter(lp_ctx, "printcap cache time", "750");
2672 lpcfg_do_global_parameter(lp_ctx, "create krb5 conf", "yes");
2674 lpcfg_do_global_parameter(lp_ctx, "winbind max clients", "200");
2676 lpcfg_do_global_parameter(lp_ctx, "acl map full control", "yes");
2678 lpcfg_do_global_parameter(lp_ctx, "nt pipe support", "yes");
2680 lpcfg_do_global_parameter(lp_ctx, "ldap debug threshold", "10");
2682 lpcfg_do_global_parameter(lp_ctx, "client ldap sasl wrapping", "sign");
2684 lpcfg_do_global_parameter(lp_ctx, "follow symlinks", "yes");
2686 lpcfg_do_global_parameter(lp_ctx, "machine password timeout", "604800");
2688 lpcfg_do_global_parameter(lp_ctx, "ldap connection timeout", "2");
2690 lpcfg_do_global_parameter(lp_ctx, "winbind expand groups", "0");
2692 lpcfg_do_global_parameter(lp_ctx, "stat cache", "yes");
2694 lpcfg_do_global_parameter(lp_ctx, "lpq cache time", "30");
2696 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max trans", "%u", DEFAULT_SMB2_MAX_TRANSACT);
2698 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max read", "%u", DEFAULT_SMB2_MAX_READ);
2700 lpcfg_do_global_parameter(lp_ctx, "durable handles", "yes");
2702 lpcfg_do_global_parameter(lp_ctx, "max stat cache size", "256");
2704 lpcfg_do_global_parameter(lp_ctx, "ldap passwd sync", "no");
2706 lpcfg_do_global_parameter(lp_ctx, "kernel change notify", "yes");
2708 lpcfg_do_global_parameter(lp_ctx, "max ttl", "259200");
2710 lpcfg_do_global_parameter(lp_ctx, "blocking locks", "yes");
2712 lpcfg_do_global_parameter(lp_ctx, "oplock contention limit", "2");
2714 lpcfg_do_global_parameter(lp_ctx, "load printers", "yes");
2716 lpcfg_do_global_parameter(lp_ctx, "idmap cache time", "604800");
2718 lpcfg_do_global_parameter(lp_ctx, "preserve case", "yes");
2720 lpcfg_do_global_parameter(lp_ctx, "lm announce", "auto");
2722 lpcfg_do_global_parameter(lp_ctx, "afs token lifetime", "604800");
2724 lpcfg_do_global_parameter(lp_ctx, "enable core files", "yes");
2726 lpcfg_do_global_parameter(lp_ctx, "winbind max domain connections", "1");
2728 lpcfg_do_global_parameter(lp_ctx, "case sensitive", "auto");
2730 lpcfg_do_global_parameter(lp_ctx, "ldap timeout", "15");
2732 lpcfg_do_global_parameter(lp_ctx, "mangle prefix", "1");
2734 lpcfg_do_global_parameter(lp_ctx, "posix locking", "yes");
2736 lpcfg_do_global_parameter(lp_ctx, "lock spin time", "200");
2738 lpcfg_do_global_parameter(lp_ctx, "directory name cache size", "100");
2740 lpcfg_do_global_parameter(lp_ctx, "nmbd bind explicit broadcast", "yes");
2742 lpcfg_do_global_parameter(lp_ctx, "init logon delay", "100");
2744 lpcfg_do_global_parameter(lp_ctx, "usershare owner only", "yes");
2746 lpcfg_do_global_parameter(lp_ctx, "-valid", "yes");
2748 lpcfg_do_global_parameter_var(lp_ctx, "usershare path", "%s/usershares", get_dyn_STATEDIR());
2750 #ifdef DEVELOPER
2751 lpcfg_do_global_parameter_var(lp_ctx, "panic action", "/bin/sleep 999999999");
2752 #endif
2754 lpcfg_do_global_parameter(lp_ctx, "smb passwd file", get_dyn_SMB_PASSWD_FILE());
2756 lpcfg_do_global_parameter(lp_ctx, "logon home", "\\\\%N\\%U");
2758 lpcfg_do_global_parameter(lp_ctx, "logon path", "\\\\%N\\%U\\profile");
2760 lpcfg_do_global_parameter(lp_ctx, "printjob username", "%U");
2762 /* Allow modules to adjust defaults */
2763 for (defaults_hook = defaults_hooks; defaults_hook;
2764 defaults_hook = defaults_hook->next) {
2765 bool ret;
2767 ret = defaults_hook->hook(lp_ctx);
2768 if (!ret) {
2769 DEBUG(1, ("Defaults hook %s failed to run.",
2770 defaults_hook->name));
2771 talloc_free(lp_ctx);
2772 return NULL;
2776 for (i = 0; parm_table[i].label; i++) {
2777 if (!(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2778 lp_ctx->flags[i] |= FLAG_DEFAULT;
2782 for (parm=lp_ctx->globals->param_opt; parm; parm=parm->next) {
2783 if (!(parm->priority & FLAG_CMDLINE)) {
2784 parm->priority |= FLAG_DEFAULT;
2788 for (parm=lp_ctx->sDefault->param_opt; parm; parm=parm->next) {
2789 if (!(parm->priority & FLAG_CMDLINE)) {
2790 parm->priority |= FLAG_DEFAULT;
2794 return lp_ctx;
2798 * Initialise the global parameter structure.
2800 struct loadparm_context *loadparm_init_global(bool load_default)
2802 if (global_loadparm_context == NULL) {
2803 global_loadparm_context = loadparm_init(NULL);
2805 if (global_loadparm_context == NULL) {
2806 return NULL;
2808 global_loadparm_context->global = true;
2809 if (load_default && !global_loadparm_context->loaded) {
2810 lpcfg_load_default(global_loadparm_context);
2812 global_loadparm_context->refuse_free = true;
2813 return global_loadparm_context;
2817 * Initialise the global parameter structure.
2819 struct loadparm_context *loadparm_init_s3(TALLOC_CTX *mem_ctx,
2820 const struct loadparm_s3_helpers *s3_fns)
2822 struct loadparm_context *loadparm_context = talloc_zero(mem_ctx, struct loadparm_context);
2823 if (!loadparm_context) {
2824 return NULL;
2826 loadparm_context->s3_fns = s3_fns;
2827 loadparm_context->globals = s3_fns->globals;
2828 loadparm_context->flags = s3_fns->flags;
2830 return loadparm_context;
2833 const char *lpcfg_configfile(struct loadparm_context *lp_ctx)
2835 return lp_ctx->szConfigFile;
2838 const char *lp_default_path(void)
2840 if (getenv("SMB_CONF_PATH"))
2841 return getenv("SMB_CONF_PATH");
2842 else
2843 return dyn_CONFIGFILE;
2847 * Update the internal state of a loadparm context after settings
2848 * have changed.
2850 static bool lpcfg_update(struct loadparm_context *lp_ctx)
2852 struct debug_settings settings;
2853 TALLOC_CTX *tmp_ctx;
2855 tmp_ctx = talloc_new(lp_ctx);
2856 if (tmp_ctx == NULL) {
2857 return false;
2860 lpcfg_add_auto_services(lp_ctx, lpcfg_auto_services(lp_ctx, tmp_ctx));
2862 if (!lp_ctx->globals->wins_server_list && lp_ctx->globals->we_are_a_wins_server) {
2863 lpcfg_do_global_parameter(lp_ctx, "wins server", "127.0.0.1");
2866 if (!lp_ctx->global) {
2867 TALLOC_FREE(tmp_ctx);
2868 return true;
2871 panic_action = lp_ctx->globals->panic_action;
2873 reload_charcnv(lp_ctx);
2875 ZERO_STRUCT(settings);
2876 /* Add any more debug-related smb.conf parameters created in
2877 * future here */
2878 settings.syslog = lp_ctx->globals->syslog;
2879 settings.syslog_only = lp_ctx->globals->syslog_only;
2880 settings.timestamp_logs = lp_ctx->globals->timestamp_logs;
2881 settings.debug_prefix_timestamp = lp_ctx->globals->debug_prefix_timestamp;
2882 settings.debug_hires_timestamp = lp_ctx->globals->debug_hires_timestamp;
2883 settings.debug_pid = lp_ctx->globals->debug_pid;
2884 settings.debug_uid = lp_ctx->globals->debug_uid;
2885 settings.debug_class = lp_ctx->globals->debug_class;
2886 debug_set_settings(&settings);
2888 /* FIXME: This is a bit of a hack, but we can't use a global, since
2889 * not everything that uses lp also uses the socket library */
2890 if (lpcfg_parm_bool(lp_ctx, NULL, "socket", "testnonblock", false)) {
2891 setenv("SOCKET_TESTNONBLOCK", "1", 1);
2892 } else {
2893 unsetenv("SOCKET_TESTNONBLOCK");
2896 TALLOC_FREE(tmp_ctx);
2897 return true;
2900 bool lpcfg_load_default(struct loadparm_context *lp_ctx)
2902 const char *path;
2904 path = lp_default_path();
2906 if (!file_exist(path)) {
2907 /* We allow the default smb.conf file to not exist,
2908 * basically the equivalent of an empty file. */
2909 return lpcfg_update(lp_ctx);
2912 return lpcfg_load(lp_ctx, path);
2916 * Load the services array from the services file.
2918 * Return True on success, False on failure.
2920 bool lpcfg_load(struct loadparm_context *lp_ctx, const char *filename)
2922 char *n2;
2923 bool bRetval;
2925 filename = talloc_strdup(lp_ctx, filename);
2927 lp_ctx->szConfigFile = filename;
2929 if (lp_ctx->s3_fns) {
2930 return lp_ctx->s3_fns->load(filename);
2933 lp_ctx->bInGlobalSection = true;
2934 n2 = standard_sub_basic(lp_ctx, lp_ctx->szConfigFile);
2935 DEBUG(2, ("lpcfg_load: refreshing parameters from %s\n", n2));
2937 add_to_file_list(lp_ctx, &lp_ctx->file_lists, lp_ctx->szConfigFile, n2);
2939 /* We get sections first, so have to start 'behind' to make up */
2940 lp_ctx->currentService = NULL;
2941 bRetval = pm_process(n2, do_section, lpcfg_do_parameter, lp_ctx);
2943 /* finish up the last section */
2944 DEBUG(4, ("pm_process() returned %s\n", BOOLSTR(bRetval)));
2945 if (bRetval)
2946 if (lp_ctx->currentService != NULL)
2947 bRetval = lpcfg_service_ok(lp_ctx->currentService);
2949 bRetval = bRetval && lpcfg_update(lp_ctx);
2951 /* we do this unconditionally, so that it happens even
2952 for a missing smb.conf */
2953 reload_charcnv(lp_ctx);
2955 if (bRetval == true) {
2956 /* set this up so that any child python tasks will
2957 find the right smb.conf */
2958 setenv("SMB_CONF_PATH", filename, 1);
2960 /* set the context used by the lp_*() function
2961 varients */
2962 global_loadparm_context = lp_ctx;
2963 lp_ctx->loaded = true;
2966 return bRetval;
2970 * Return the max number of services.
2973 int lpcfg_numservices(struct loadparm_context *lp_ctx)
2975 if (lp_ctx->s3_fns) {
2976 return lp_ctx->s3_fns->get_numservices();
2979 return lp_ctx->iNumServices;
2983 * Display the contents of the services array in human-readable form.
2986 void lpcfg_dump(struct loadparm_context *lp_ctx, FILE *f, bool show_defaults,
2987 int maxtoprint)
2989 int iService;
2991 if (lp_ctx->s3_fns) {
2992 lp_ctx->s3_fns->dump(f, show_defaults, maxtoprint);
2993 return;
2996 lpcfg_dump_globals(lp_ctx, f, show_defaults);
2998 lpcfg_dump_a_service(lp_ctx->sDefault, lp_ctx->sDefault, f, lp_ctx->flags, show_defaults);
3000 for (iService = 0; iService < maxtoprint; iService++)
3001 lpcfg_dump_one(f, show_defaults, lp_ctx->services[iService], lp_ctx->sDefault);
3005 * Display the contents of one service in human-readable form.
3007 void lpcfg_dump_one(FILE *f, bool show_defaults, struct loadparm_service *service, struct loadparm_service *sDefault)
3009 if (service != NULL) {
3010 if (service->szService[0] == '\0')
3011 return;
3012 lpcfg_dump_a_service(service, sDefault, f, NULL, show_defaults);
3016 struct loadparm_service *lpcfg_servicebynum(struct loadparm_context *lp_ctx,
3017 int snum)
3019 if (lp_ctx->s3_fns) {
3020 return lp_ctx->s3_fns->get_servicebynum(snum);
3023 return lp_ctx->services[snum];
3026 struct loadparm_service *lpcfg_service(struct loadparm_context *lp_ctx,
3027 const char *service_name)
3029 int iService;
3030 char *serviceName;
3032 if (lp_ctx->s3_fns) {
3033 return lp_ctx->s3_fns->get_service(service_name);
3036 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--) {
3037 if (lp_ctx->services[iService] &&
3038 lp_ctx->services[iService]->szService) {
3040 * The substitution here is used to support %U is
3041 * service names
3043 serviceName = standard_sub_basic(
3044 lp_ctx->services[iService],
3045 lp_ctx->services[iService]->szService);
3046 if (strequal(serviceName, service_name)) {
3047 talloc_free(serviceName);
3048 return lp_ctx->services[iService];
3050 talloc_free(serviceName);
3054 DEBUG(7,("lpcfg_servicenumber: couldn't find %s\n", service_name));
3055 return NULL;
3058 const char *lpcfg_servicename(const struct loadparm_service *service)
3060 return lpcfg_string((const char *)service->szService);
3064 * A useful volume label function.
3066 const char *lpcfg_volume_label(struct loadparm_service *service, struct loadparm_service *sDefault)
3068 const char *ret;
3069 ret = lpcfg_string((const char *)((service != NULL && service->volume != NULL) ?
3070 service->volume : sDefault->volume));
3071 if (!*ret)
3072 return lpcfg_servicename(service);
3073 return ret;
3077 * Return the correct printer name.
3079 const char *lpcfg_printername(struct loadparm_service *service, struct loadparm_service *sDefault)
3081 const char *ret;
3082 ret = lpcfg_string((const char *)((service != NULL && service->_printername != NULL) ?
3083 service->_printername : sDefault->_printername));
3084 if (ret == NULL || (ret != NULL && *ret == '\0'))
3085 ret = lpcfg_servicename(service);
3087 return ret;
3092 * Return the max print jobs per queue.
3094 int lpcfg_maxprintjobs(struct loadparm_service *service, struct loadparm_service *sDefault)
3096 int maxjobs = (service != NULL) ? service->iMaxPrintJobs : sDefault->iMaxPrintJobs;
3097 if (maxjobs <= 0 || maxjobs >= PRINT_MAX_JOBID)
3098 maxjobs = PRINT_MAX_JOBID - 1;
3100 return maxjobs;
3103 struct smb_iconv_handle *lpcfg_iconv_handle(struct loadparm_context *lp_ctx)
3105 if (lp_ctx == NULL) {
3106 return get_iconv_handle();
3108 return lp_ctx->iconv_handle;
3111 _PUBLIC_ void reload_charcnv(struct loadparm_context *lp_ctx)
3113 struct smb_iconv_handle *old_ic = lp_ctx->iconv_handle;
3114 if (!lp_ctx->global) {
3115 return;
3118 if (old_ic == NULL) {
3119 old_ic = global_iconv_handle;
3121 lp_ctx->iconv_handle = smb_iconv_handle_reinit_lp(lp_ctx, lp_ctx, old_ic);
3122 global_iconv_handle = lp_ctx->iconv_handle;
3125 _PUBLIC_ char *lpcfg_tls_keyfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3127 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_keyfile(lp_ctx));
3130 _PUBLIC_ char *lpcfg_tls_certfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3132 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_certfile(lp_ctx));
3135 _PUBLIC_ char *lpcfg_tls_cafile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3137 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_cafile(lp_ctx));
3140 _PUBLIC_ char *lpcfg_tls_crlfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3142 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_crlfile(lp_ctx));
3145 _PUBLIC_ char *lpcfg_tls_dhpfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3147 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_dhpfile(lp_ctx));
3150 struct gensec_settings *lpcfg_gensec_settings(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3152 struct gensec_settings *settings = talloc_zero(mem_ctx, struct gensec_settings);
3153 if (settings == NULL)
3154 return NULL;
3155 SMB_ASSERT(lp_ctx != NULL);
3156 settings->lp_ctx = talloc_reference(settings, lp_ctx);
3157 settings->target_hostname = lpcfg_parm_string(lp_ctx, NULL, "gensec", "target_hostname");
3158 return settings;
3161 int lpcfg_server_role(struct loadparm_context *lp_ctx)
3163 int domain_master = lpcfg__domain_master(lp_ctx);
3165 return lp_find_server_role(lpcfg__server_role(lp_ctx),
3166 lpcfg__security(lp_ctx),
3167 lpcfg__domain_logons(lp_ctx),
3168 (domain_master == true) ||
3169 (domain_master == Auto));
3172 int lpcfg_security(struct loadparm_context *lp_ctx)
3174 return lp_find_security(lpcfg__server_role(lp_ctx),
3175 lpcfg__security(lp_ctx));
3178 int lpcfg_client_max_protocol(struct loadparm_context *lp_ctx)
3180 int client_max_protocol = lpcfg__client_max_protocol(lp_ctx);
3181 if (client_max_protocol == PROTOCOL_DEFAULT) {
3182 return PROTOCOL_NT1;
3184 return client_max_protocol;
3187 bool lpcfg_server_signing_allowed(struct loadparm_context *lp_ctx, bool *mandatory)
3189 bool allowed = true;
3190 enum smb_signing_setting signing_setting = lpcfg_server_signing(lp_ctx);
3192 *mandatory = false;
3194 if (signing_setting == SMB_SIGNING_DEFAULT) {
3196 * If we are a domain controller, SMB signing is
3197 * really important, as it can prevent a number of
3198 * attacks on communications between us and the
3199 * clients
3201 * However, it really sucks (no sendfile, CPU
3202 * overhead) performance-wise when used on a
3203 * file server, so disable it by default
3204 * on non-DCs
3207 if (lpcfg_server_role(lp_ctx) >= ROLE_ACTIVE_DIRECTORY_DC) {
3208 signing_setting = SMB_SIGNING_REQUIRED;
3209 } else {
3210 signing_setting = SMB_SIGNING_OFF;
3214 switch (signing_setting) {
3215 case SMB_SIGNING_REQUIRED:
3216 *mandatory = true;
3217 break;
3218 case SMB_SIGNING_IF_REQUIRED:
3219 break;
3220 case SMB_SIGNING_DEFAULT:
3221 case SMB_SIGNING_OFF:
3222 allowed = false;
3223 break;
3226 return allowed;
3229 int lpcfg_tdb_hash_size(struct loadparm_context *lp_ctx, const char *name)
3231 const char *base;
3233 if (name == NULL) {
3234 return 0;
3237 base = strrchr_m(name, '/');
3238 if (base != NULL) {
3239 base += 1;
3240 } else {
3241 base = name;
3243 return lpcfg_parm_int(lp_ctx, NULL, "tdb_hashsize", base, 0);
3247 int lpcfg_tdb_flags(struct loadparm_context *lp_ctx, int tdb_flags)
3249 if (!lpcfg_use_mmap(lp_ctx)) {
3250 tdb_flags |= TDB_NOMMAP;
3252 return tdb_flags;