s4:gensec/gssapi: use gensec_gssapi_max_{input,wrapped}_size() for all backends
[Samba.git] / lib / param / loadparm.c
blobbb215b26992a17f8ed5197fa935e6f29c904d72a
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 FN_GLOBAL_STRING(fn_name,var_name) \
144 _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx, TALLOC_CTX *ctx) {\
145 if (lp_ctx == NULL) return NULL; \
146 if (lp_ctx->s3_fns) { \
147 return lp_ctx->globals->var_name ? lp_ctx->s3_fns->lp_string(ctx, lp_ctx->globals->var_name) : talloc_strdup(ctx, ""); \
149 return lp_ctx->globals->var_name ? talloc_strdup(ctx, lpcfg_string(lp_ctx->globals->var_name)) : talloc_strdup(ctx, ""); \
152 #define FN_GLOBAL_CONST_STRING(fn_name,var_name) \
153 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
154 if (lp_ctx == NULL) return NULL; \
155 return lp_ctx->globals->var_name ? lpcfg_string(lp_ctx->globals->var_name) : ""; \
158 #define FN_GLOBAL_LIST(fn_name,var_name) \
159 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
160 if (lp_ctx == NULL) return NULL; \
161 return lp_ctx->globals->var_name; \
164 #define FN_GLOBAL_BOOL(fn_name,var_name) \
165 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) {\
166 if (lp_ctx == NULL) return false; \
167 return lp_ctx->globals->var_name; \
170 #define FN_GLOBAL_INTEGER(fn_name,var_name) \
171 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
172 return lp_ctx->globals->var_name; \
175 /* Local parameters don't need the ->s3_fns because the struct
176 * loadparm_service is shared and lpcfg_service() checks the ->s3_fns
177 * hook */
178 #define FN_LOCAL_STRING(fn_name,val) \
179 _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_service *service, \
180 struct loadparm_service *sDefault, TALLOC_CTX *ctx) { \
181 return(talloc_strdup(ctx, lpcfg_string((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)))); \
184 #define FN_LOCAL_CONST_STRING(fn_name,val) \
185 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_service *service, \
186 struct loadparm_service *sDefault) { \
187 return((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)); \
190 #define FN_LOCAL_LIST(fn_name,val) \
191 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_service *service, \
192 struct loadparm_service *sDefault) {\
193 return(const char **)(service != NULL && service->val != NULL? service->val : sDefault->val); \
196 #define FN_LOCAL_PARM_BOOL(fn_name, val) FN_LOCAL_BOOL(fn_name, val)
198 #define FN_LOCAL_BOOL(fn_name,val) \
199 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_service *service, \
200 struct loadparm_service *sDefault) { \
201 return((service != NULL)? service->val : sDefault->val); \
204 #define FN_LOCAL_INTEGER(fn_name,val) \
205 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_service *service, \
206 struct loadparm_service *sDefault) { \
207 return((service != NULL)? service->val : sDefault->val); \
210 #define FN_LOCAL_PARM_INTEGER(fn_name, val) FN_LOCAL_INTEGER(fn_name, val)
212 #define FN_LOCAL_PARM_CHAR(fn_name,val) \
213 _PUBLIC_ char lpcfg_ ## fn_name(struct loadparm_service *service, \
214 struct loadparm_service *sDefault) { \
215 return((service != NULL)? service->val : sDefault->val); \
218 #include "lib/param/param_functions.c"
220 /* These functions cannot be auto-generated */
221 FN_LOCAL_BOOL(autoloaded, autoloaded)
222 FN_GLOBAL_CONST_STRING(dnsdomain, dnsdomain)
224 /* local prototypes */
225 static struct loadparm_service *lpcfg_getservicebyname(struct loadparm_context *lp_ctx,
226 const char *pszServiceName);
227 static bool do_section(const char *pszSectionName, void *);
228 static bool set_variable_helper(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
229 const char *pszParmName, const char *pszParmValue);
230 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
231 struct loadparm_service *service,
232 const char *pszParmName,
233 const char *pszParmValue, int flags);
235 /* The following are helper functions for parametrical options support. */
236 /* It returns a pointer to parametrical option value if it exists or NULL otherwise */
237 /* Actual parametrical functions are quite simple */
238 struct parmlist_entry *get_parametric_helper(struct loadparm_service *service,
239 const char *type, const char *option,
240 struct parmlist_entry *global_opts)
242 size_t type_len = strlen(type);
243 size_t option_len = strlen(option);
244 char param_key[type_len + option_len + 2];
245 struct parmlist_entry *data = NULL;
247 snprintf(param_key, sizeof(param_key), "%s:%s", type, option);
250 * Try to fetch the option from the data.
252 if (service != NULL) {
253 data = service->param_opt;
254 while (data != NULL) {
255 if (strwicmp(data->key, param_key) == 0) {
256 return data;
258 data = data->next;
263 * Fall back to fetching from the globals.
265 data = global_opts;
266 while (data != NULL) {
267 if (strwicmp(data->key, param_key) == 0) {
268 return data;
270 data = data->next;
273 return NULL;
276 const char *lpcfg_get_parametric(struct loadparm_context *lp_ctx,
277 struct loadparm_service *service,
278 const char *type, const char *option)
280 struct parmlist_entry *data;
282 if (lp_ctx == NULL)
283 return NULL;
285 data = get_parametric_helper(service,
286 type, option, lp_ctx->globals->param_opt);
288 if (data == NULL) {
289 return NULL;
290 } else {
291 return data->value;
297 * convenience routine to return int parameters.
299 int lp_int(const char *s)
302 if (!s || !*s) {
303 DEBUG(0,("lp_int(%s): is called with NULL!\n",s));
304 return -1;
307 return strtol(s, NULL, 0);
311 * convenience routine to return unsigned long parameters.
313 unsigned long lp_ulong(const char *s)
316 if (!s || !*s) {
317 DEBUG(0,("lp_ulong(%s): is called with NULL!\n",s));
318 return -1;
321 return strtoul(s, NULL, 0);
325 * convenience routine to return unsigned long parameters.
327 static long lp_long(const char *s)
330 if (!s) {
331 DEBUG(0,("lp_long(%s): is called with NULL!\n",s));
332 return -1;
335 return strtol(s, NULL, 0);
339 * convenience routine to return unsigned long parameters.
341 static double lp_double(const char *s)
344 if (!s) {
345 DEBUG(0,("lp_double(%s): is called with NULL!\n",s));
346 return -1;
349 return strtod(s, NULL);
353 * convenience routine to return boolean parameters.
355 bool lp_bool(const char *s)
357 bool ret = false;
359 if (!s || !*s) {
360 DEBUG(0,("lp_bool(%s): is called with NULL!\n",s));
361 return false;
364 if (!set_boolean(s, &ret)) {
365 DEBUG(0,("lp_bool(%s): value is not boolean!\n",s));
366 return false;
369 return ret;
373 * Return parametric option from a given service. Type is a part of option before ':'
374 * Parametric option has following syntax: 'Type: option = value'
375 * Returned value is allocated in 'lp_talloc' context
378 const char *lpcfg_parm_string(struct loadparm_context *lp_ctx,
379 struct loadparm_service *service, const char *type,
380 const char *option)
382 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
384 if (value)
385 return lpcfg_string(value);
387 return NULL;
391 * Return parametric option from a given service. Type is a part of option before ':'
392 * Parametric option has following syntax: 'Type: option = value'
393 * Returned value is allocated in 'lp_talloc' context
396 const char **lpcfg_parm_string_list(TALLOC_CTX *mem_ctx,
397 struct loadparm_context *lp_ctx,
398 struct loadparm_service *service,
399 const char *type,
400 const char *option, const char *separator)
402 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
404 if (value != NULL) {
405 char **l = str_list_make(mem_ctx, value, separator);
406 return discard_const_p(const char *, l);
409 return NULL;
413 * Return parametric option from a given service. Type is a part of option before ':'
414 * Parametric option has following syntax: 'Type: option = value'
417 int lpcfg_parm_int(struct loadparm_context *lp_ctx,
418 struct loadparm_service *service, const char *type,
419 const char *option, int default_v)
421 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
423 if (value)
424 return lp_int(value);
426 return default_v;
430 * Return parametric option from a given service. Type is a part of
431 * option before ':'.
432 * Parametric option has following syntax: 'Type: option = value'.
435 int lpcfg_parm_bytes(struct loadparm_context *lp_ctx,
436 struct loadparm_service *service, const char *type,
437 const char *option, int default_v)
439 uint64_t bval;
441 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
443 if (value && conv_str_size_error(value, &bval)) {
444 if (bval <= INT_MAX) {
445 return (int)bval;
449 return default_v;
453 * Return parametric option from a given service.
454 * Type is a part of option before ':'
455 * Parametric option has following syntax: 'Type: option = value'
457 unsigned long lpcfg_parm_ulong(struct loadparm_context *lp_ctx,
458 struct loadparm_service *service, const char *type,
459 const char *option, unsigned long default_v)
461 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
463 if (value)
464 return lp_ulong(value);
466 return default_v;
469 long lpcfg_parm_long(struct loadparm_context *lp_ctx,
470 struct loadparm_service *service, const char *type,
471 const char *option, long default_v)
473 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
475 if (value)
476 return lp_long(value);
478 return default_v;
481 double lpcfg_parm_double(struct loadparm_context *lp_ctx,
482 struct loadparm_service *service, const char *type,
483 const char *option, double default_v)
485 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
487 if (value != NULL)
488 return lp_double(value);
490 return default_v;
494 * Return parametric option from a given service. Type is a part of option before ':'
495 * Parametric option has following syntax: 'Type: option = value'
498 bool lpcfg_parm_bool(struct loadparm_context *lp_ctx,
499 struct loadparm_service *service, const char *type,
500 const char *option, bool default_v)
502 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
504 if (value != NULL)
505 return lp_bool(value);
507 return default_v;
512 * Set a string value, deallocating any existing space, and allocing the space
513 * for the string
515 bool lpcfg_string_set(TALLOC_CTX *mem_ctx, char **dest, const char *src)
517 talloc_free(*dest);
519 if (src == NULL)
520 src = "";
522 *dest = talloc_strdup(mem_ctx, src);
523 if ((*dest) == NULL) {
524 DEBUG(0,("Out of memory in string_set\n"));
525 return false;
528 return true;
532 * Set a string value, deallocating any existing space, and allocing the space
533 * for the string
535 bool lpcfg_string_set_upper(TALLOC_CTX *mem_ctx, char **dest, const char *src)
537 talloc_free(*dest);
539 if (src == NULL)
540 src = "";
542 *dest = strupper_talloc(mem_ctx, src);
543 if ((*dest) == NULL) {
544 DEBUG(0,("Out of memory in string_set_upper\n"));
545 return false;
548 return true;
554 * Add a new service to the services array initialising it with the given
555 * service.
558 struct loadparm_service *lpcfg_add_service(struct loadparm_context *lp_ctx,
559 const struct loadparm_service *pservice,
560 const char *name)
562 int i;
563 int num_to_alloc = lp_ctx->iNumServices + 1;
564 struct parmlist_entry *data, *pdata;
566 if (lp_ctx->s3_fns != NULL) {
567 smb_panic("Add a service should not be called on an s3 loadparm ctx");
570 if (pservice == NULL) {
571 pservice = lp_ctx->sDefault;
574 /* it might already exist */
575 if (name) {
576 struct loadparm_service *service = lpcfg_getservicebyname(lp_ctx,
577 name);
578 if (service != NULL) {
579 /* Clean all parametric options for service */
580 /* They will be added during parsing again */
581 data = service->param_opt;
582 while (data) {
583 pdata = data->next;
584 talloc_free(data);
585 data = pdata;
587 service->param_opt = NULL;
588 return service;
592 /* find an invalid one */
593 for (i = 0; i < lp_ctx->iNumServices; i++)
594 if (lp_ctx->services[i] == NULL)
595 break;
597 /* if not, then create one */
598 if (i == lp_ctx->iNumServices) {
599 struct loadparm_service **tsp;
601 tsp = talloc_realloc(lp_ctx, lp_ctx->services, struct loadparm_service *, num_to_alloc);
603 if (!tsp) {
604 DEBUG(0,("lpcfg_add_service: failed to enlarge services!\n"));
605 return NULL;
606 } else {
607 lp_ctx->services = tsp;
608 lp_ctx->services[lp_ctx->iNumServices] = NULL;
611 lp_ctx->iNumServices++;
614 lp_ctx->services[i] = talloc_zero(lp_ctx->services, struct loadparm_service);
615 if (lp_ctx->services[i] == NULL) {
616 DEBUG(0,("lpcfg_add_service: out of memory!\n"));
617 return NULL;
619 copy_service(lp_ctx->services[i], pservice, NULL);
620 if (name != NULL)
621 lpcfg_string_set(lp_ctx->services[i], &lp_ctx->services[i]->szService, name);
622 return lp_ctx->services[i];
626 * Add a new home service, with the specified home directory, defaults coming
627 * from service ifrom.
630 bool lpcfg_add_home(struct loadparm_context *lp_ctx,
631 const char *pszHomename,
632 struct loadparm_service *default_service,
633 const char *user, const char *pszHomedir)
635 struct loadparm_service *service;
637 service = lpcfg_add_service(lp_ctx, default_service, pszHomename);
639 if (service == NULL)
640 return false;
642 if (!(*(default_service->path))
643 || strequal(default_service->path, lp_ctx->sDefault->path)) {
644 service->path = talloc_strdup(service, pszHomedir);
645 } else {
646 service->path = string_sub_talloc(service, lpcfg_path(default_service, lp_ctx->sDefault, service), "%H", pszHomedir);
649 if (!(*(service->comment))) {
650 service->comment = talloc_asprintf(service, "Home directory of %s", user);
652 service->bAvailable = default_service->bAvailable;
653 service->browseable = default_service->browseable;
655 DEBUG(3, ("adding home's share [%s] for user '%s' at '%s'\n",
656 pszHomename, user, service->path));
658 return true;
662 * Add a new printer service, with defaults coming from service iFrom.
665 bool lpcfg_add_printer(struct loadparm_context *lp_ctx,
666 const char *pszPrintername,
667 struct loadparm_service *default_service)
669 const char *comment = "From Printcap";
670 struct loadparm_service *service;
671 service = lpcfg_add_service(lp_ctx, default_service, pszPrintername);
673 if (service == NULL)
674 return false;
676 /* note that we do NOT default the availability flag to True - */
677 /* we take it from the default service passed. This allows all */
678 /* dynamic printers to be disabled by disabling the [printers] */
679 /* entry (if/when the 'available' keyword is implemented!). */
681 /* the printer name is set to the service name. */
682 lpcfg_string_set(service, &service->_printername, pszPrintername);
683 lpcfg_string_set(service, &service->comment, comment);
684 service->browseable = default_service->browseable;
685 /* Printers cannot be read_only. */
686 service->read_only = false;
687 /* Printer services must be printable. */
688 service->printable = true;
690 DEBUG(3, ("adding printer service %s\n", pszPrintername));
692 return true;
696 * Map a parameter's string representation to something we can use.
697 * Returns False if the parameter string is not recognised, else TRUE.
700 int lpcfg_map_parameter(const char *pszParmName)
702 int iIndex;
704 for (iIndex = 0; parm_table[iIndex].label; iIndex++)
705 if (strwicmp(parm_table[iIndex].label, pszParmName) == 0)
706 return iIndex;
708 /* Warn only if it isn't parametric option */
709 if (strchr(pszParmName, ':') == NULL)
710 DEBUG(0, ("Unknown parameter encountered: \"%s\"\n", pszParmName));
711 /* We do return 'fail' for parametric options as well because they are
712 stored in different storage
714 return -1;
719 return the parameter structure for a parameter
721 struct parm_struct *lpcfg_parm_struct(struct loadparm_context *lp_ctx, const char *name)
723 int num = lpcfg_map_parameter(name);
725 if (num < 0) {
726 return NULL;
729 return &parm_table[num];
733 return the parameter pointer for a parameter
735 void *lpcfg_parm_ptr(struct loadparm_context *lp_ctx,
736 struct loadparm_service *service, struct parm_struct *parm)
738 if (lp_ctx->s3_fns) {
739 return lp_ctx->s3_fns->get_parm_ptr(service, parm);
742 if (service == NULL) {
743 if (parm->p_class == P_LOCAL)
744 return ((char *)lp_ctx->sDefault)+parm->offset;
745 else if (parm->p_class == P_GLOBAL)
746 return ((char *)lp_ctx->globals)+parm->offset;
747 else return NULL;
748 } else {
749 return ((char *)service) + parm->offset;
754 return the parameter pointer for a parameter
756 bool lpcfg_parm_is_cmdline(struct loadparm_context *lp_ctx, const char *name)
758 int parmnum;
760 parmnum = lpcfg_map_parameter(name);
761 if (parmnum == -1) return false;
763 return lp_ctx->flags[parmnum] & FLAG_CMDLINE;
767 * Find a service by name. Otherwise works like get_service.
770 static struct loadparm_service *lpcfg_getservicebyname(struct loadparm_context *lp_ctx,
771 const char *pszServiceName)
773 int iService;
775 if (lp_ctx->s3_fns) {
776 return lp_ctx->s3_fns->get_service(pszServiceName);
779 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--)
780 if (lp_ctx->services[iService] != NULL &&
781 strwicmp(lp_ctx->services[iService]->szService, pszServiceName) == 0) {
782 return lp_ctx->services[iService];
785 return NULL;
789 * Add a parametric option to a parmlist_entry,
790 * replacing old value, if already present.
792 void set_param_opt(TALLOC_CTX *mem_ctx,
793 struct parmlist_entry **opt_list,
794 const char *opt_name,
795 const char *opt_value,
796 unsigned priority)
798 struct parmlist_entry *new_opt, *opt;
799 bool not_added;
801 opt = *opt_list;
802 not_added = true;
804 /* Traverse destination */
805 while (opt) {
806 /* If we already have same option, override it */
807 if (strwicmp(opt->key, opt_name) == 0) {
808 if ((opt->priority & FLAG_CMDLINE) &&
809 !(priority & FLAG_CMDLINE)) {
810 /* it's been marked as not to be
811 overridden */
812 return;
814 TALLOC_FREE(opt->value);
815 TALLOC_FREE(opt->list);
816 opt->value = talloc_strdup(opt, opt_value);
817 opt->priority = priority;
818 not_added = false;
819 break;
821 opt = opt->next;
823 if (not_added) {
824 new_opt = talloc(mem_ctx, struct parmlist_entry);
825 if (new_opt == NULL) {
826 smb_panic("OOM");
829 new_opt->key = talloc_strdup(new_opt, opt_name);
830 if (new_opt->key == NULL) {
831 smb_panic("talloc_strdup failed");
834 new_opt->value = talloc_strdup(new_opt, opt_value);
835 if (new_opt->value == NULL) {
836 smb_panic("talloc_strdup failed");
839 new_opt->list = NULL;
840 new_opt->priority = priority;
841 DLIST_ADD(*opt_list, new_opt);
846 * Copy a service structure to another.
847 * If pcopymapDest is NULL then copy all fields
850 void copy_service(struct loadparm_service *pserviceDest,
851 const struct loadparm_service *pserviceSource,
852 struct bitmap *pcopymapDest)
854 int i;
855 bool bcopyall = (pcopymapDest == NULL);
856 struct parmlist_entry *data;
858 for (i = 0; parm_table[i].label; i++)
859 if (parm_table[i].p_class == P_LOCAL &&
860 (bcopyall || bitmap_query(pcopymapDest, i))) {
861 const void *src_ptr =
862 ((const char *)pserviceSource) + parm_table[i].offset;
863 void *dest_ptr =
864 ((char *)pserviceDest) + parm_table[i].offset;
866 switch (parm_table[i].type) {
867 case P_BOOL:
868 case P_BOOLREV:
869 *(bool *)dest_ptr = *(const bool *)src_ptr;
870 break;
872 case P_INTEGER:
873 case P_BYTES:
874 case P_OCTAL:
875 case P_ENUM:
876 *(int *)dest_ptr = *(const int *)src_ptr;
877 break;
879 case P_CHAR:
880 *(char *)dest_ptr = *(const char *)src_ptr;
881 break;
883 case P_STRING:
884 lpcfg_string_set(pserviceDest,
885 (char **)dest_ptr,
886 *(const char * const *)src_ptr);
887 break;
889 case P_USTRING:
890 lpcfg_string_set_upper(pserviceDest,
891 (char **)dest_ptr,
892 *(const char * const *)src_ptr);
893 break;
894 case P_CMDLIST:
895 case P_LIST:
896 TALLOC_FREE(*((char ***)dest_ptr));
897 *(char ***)dest_ptr = str_list_copy(pserviceDest,
898 *discard_const_p(const char **, src_ptr));
899 break;
900 default:
901 break;
905 if (bcopyall) {
906 init_copymap(pserviceDest);
907 if (pserviceSource->copymap)
908 bitmap_copy(pserviceDest->copymap,
909 pserviceSource->copymap);
912 for (data = pserviceSource->param_opt; data != NULL; data = data->next) {
913 set_param_opt(pserviceDest, &pserviceDest->param_opt,
914 data->key, data->value, data->priority);
919 * Check a service for consistency. Return False if the service is in any way
920 * incomplete or faulty, else True.
922 bool lpcfg_service_ok(struct loadparm_service *service)
924 bool bRetval;
926 bRetval = true;
927 if (service->szService[0] == '\0') {
928 DEBUG(0, ("The following message indicates an internal error:\n"));
929 DEBUG(0, ("No service name in service entry.\n"));
930 bRetval = false;
933 /* The [printers] entry MUST be printable. I'm all for flexibility, but */
934 /* I can't see why you'd want a non-printable printer service... */
935 if (strwicmp(service->szService, PRINTERS_NAME) == 0) {
936 if (!service->printable) {
937 DEBUG(0, ("WARNING: [%s] service MUST be printable!\n",
938 service->szService));
939 service->printable = true;
941 /* [printers] service must also be non-browsable. */
942 if (service->browseable)
943 service->browseable = false;
946 if (service->path[0] == '\0' &&
947 strwicmp(service->szService, HOMES_NAME) != 0 &&
948 service->msdfs_proxy[0] == '\0')
950 DEBUG(0, ("WARNING: No path in service %s - making it unavailable!\n",
951 service->szService));
952 service->bAvailable = false;
955 if (!service->bAvailable)
956 DEBUG(1, ("NOTE: Service %s is flagged unavailable.\n",
957 service->szService));
959 return bRetval;
963 /*******************************************************************
964 Keep a linked list of all config files so we know when one has changed
965 it's date and needs to be reloaded.
966 ********************************************************************/
968 void add_to_file_list(TALLOC_CTX *mem_ctx, struct file_lists **list,
969 const char *fname, const char *subfname)
971 struct file_lists *f = *list;
973 while (f) {
974 if (f->name && !strcmp(f->name, fname))
975 break;
976 f = f->next;
979 if (!f) {
980 f = talloc(mem_ctx, struct file_lists);
981 if (!f)
982 goto fail;
983 f->next = *list;
984 f->name = talloc_strdup(f, fname);
985 if (!f->name) {
986 TALLOC_FREE(f);
987 goto fail;
989 f->subfname = talloc_strdup(f, subfname);
990 if (!f->subfname) {
991 TALLOC_FREE(f);
992 goto fail;
994 *list = f;
995 f->modtime = file_modtime(subfname);
996 } else {
997 time_t t = file_modtime(subfname);
998 if (t)
999 f->modtime = t;
1001 return;
1003 fail:
1004 DEBUG(0, ("Unable to add file to file list: %s\n", fname));
1008 /*******************************************************************
1009 Check if a config file has changed date.
1010 ********************************************************************/
1011 bool lpcfg_file_list_changed(struct loadparm_context *lp_ctx)
1013 struct file_lists *f;
1014 DEBUG(6, ("lpcfg_file_list_changed()\n"));
1016 for (f = lp_ctx->file_lists; f != NULL; f = f->next) {
1017 char *n2;
1018 time_t mod_time;
1020 n2 = standard_sub_basic(lp_ctx, f->name);
1022 DEBUGADD(6, ("file %s -> %s last mod_time: %s\n",
1023 f->name, n2, ctime(&f->modtime)));
1025 mod_time = file_modtime(n2);
1027 if (mod_time && ((f->modtime != mod_time) || (f->subfname == NULL) || (strcmp(n2, f->subfname) != 0))) {
1028 DEBUGADD(6, ("file %s modified: %s\n", n2,
1029 ctime(&mod_time)));
1030 f->modtime = mod_time;
1031 talloc_free(f->subfname);
1032 f->subfname = talloc_strdup(f, n2);
1033 TALLOC_FREE(n2);
1034 return true;
1036 TALLOC_FREE(n2);
1038 return false;
1042 * set the value for a P_ENUM
1044 bool lp_set_enum_parm( struct parm_struct *parm, const char *pszParmValue,
1045 int *ptr )
1047 int i;
1049 for (i = 0; parm->enum_list[i].name; i++) {
1050 if (strwicmp(pszParmValue, parm->enum_list[i].name) == 0) {
1051 *ptr = parm->enum_list[i].value;
1052 return true;
1055 DEBUG(0, ("WARNING: Ignoring invalid value '%s' for parameter '%s'\n",
1056 pszParmValue, parm->label));
1057 return false;
1061 /***************************************************************************
1062 Handle the "realm" parameter
1063 ***************************************************************************/
1065 bool handle_realm(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1066 const char *pszParmValue, char **ptr)
1068 char *upper;
1069 char *lower;
1071 upper = strupper_talloc(lp_ctx, pszParmValue);
1072 if (upper == NULL) {
1073 return false;
1076 lower = strlower_talloc(lp_ctx, pszParmValue);
1077 if (lower == NULL) {
1078 TALLOC_FREE(upper);
1079 return false;
1082 lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1083 lpcfg_string_set(lp_ctx->globals->ctx, &lp_ctx->globals->realm, upper);
1084 lpcfg_string_set(lp_ctx->globals->ctx, &lp_ctx->globals->dnsdomain, lower);
1086 return true;
1089 /***************************************************************************
1090 Handle the include operation.
1091 ***************************************************************************/
1093 bool handle_include(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1094 const char *pszParmValue, char **ptr)
1096 char *fname;
1098 if (lp_ctx->s3_fns) {
1099 return lp_ctx->s3_fns->lp_include(lp_ctx, service, pszParmValue, ptr);
1102 fname = standard_sub_basic(lp_ctx, pszParmValue);
1104 add_to_file_list(lp_ctx, &lp_ctx->file_lists, pszParmValue, fname);
1106 lpcfg_string_set(lp_ctx, ptr, fname);
1108 if (file_exist(fname))
1109 return pm_process(fname, do_section, lpcfg_do_parameter, lp_ctx);
1111 DEBUG(2, ("Can't find include file %s\n", fname));
1113 return false;
1116 /***************************************************************************
1117 Handle the interpretation of the copy parameter.
1118 ***************************************************************************/
1120 bool handle_copy(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1121 const char *pszParmValue, char **ptr)
1123 bool bRetval;
1124 struct loadparm_service *serviceTemp = NULL;
1126 bRetval = false;
1128 DEBUG(3, ("Copying service from service %s\n", pszParmValue));
1130 serviceTemp = lpcfg_getservicebyname(lp_ctx, pszParmValue);
1132 if (service == NULL) {
1133 DEBUG(0, ("Unable to copy service - invalid service destination.\n"));
1134 return false;
1137 if (serviceTemp != NULL) {
1138 if (serviceTemp == service) {
1139 DEBUG(0, ("Can't copy service %s - unable to copy self!\n", pszParmValue));
1140 } else {
1141 copy_service(service,
1142 serviceTemp,
1143 service->copymap);
1144 lpcfg_string_set(service, ptr, pszParmValue);
1146 bRetval = true;
1148 } else {
1149 DEBUG(0, ("Unable to copy service - source not found: %s\n",
1150 pszParmValue));
1151 bRetval = false;
1154 return bRetval;
1157 bool handle_debug_list(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1158 const char *pszParmValue, char **ptr)
1160 lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1162 return debug_parse_levels(pszParmValue);
1165 bool handle_logfile(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1166 const char *pszParmValue, char **ptr)
1168 if (lp_ctx->s3_fns == NULL) {
1169 debug_set_logfile(pszParmValue);
1172 lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1174 return true;
1178 * These special charset handling methods only run in the source3 code.
1181 bool handle_charset(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1182 const char *pszParmValue, char **ptr)
1184 if (lp_ctx->s3_fns) {
1185 if (*ptr == NULL || strcmp(*ptr, pszParmValue) != 0) {
1186 global_iconv_handle = smb_iconv_handle_reinit(NULL,
1187 lpcfg_dos_charset(lp_ctx),
1188 lpcfg_unix_charset(lp_ctx),
1189 true, global_iconv_handle);
1193 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1197 bool handle_dos_charset(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1198 const char *pszParmValue, char **ptr)
1200 bool is_utf8 = false;
1201 size_t len = strlen(pszParmValue);
1203 if (lp_ctx->s3_fns) {
1204 if (len == 4 || len == 5) {
1205 /* Don't use StrCaseCmp here as we don't want to
1206 initialize iconv. */
1207 if ((toupper_m(pszParmValue[0]) == 'U') &&
1208 (toupper_m(pszParmValue[1]) == 'T') &&
1209 (toupper_m(pszParmValue[2]) == 'F')) {
1210 if (len == 4) {
1211 if (pszParmValue[3] == '8') {
1212 is_utf8 = true;
1214 } else {
1215 if (pszParmValue[3] == '-' &&
1216 pszParmValue[4] == '8') {
1217 is_utf8 = true;
1223 if (*ptr == NULL || strcmp(*ptr, pszParmValue) != 0) {
1224 if (is_utf8) {
1225 DEBUG(0,("ERROR: invalid DOS charset: 'dos charset' must not "
1226 "be UTF8, using (default value) %s instead.\n",
1227 DEFAULT_DOS_CHARSET));
1228 pszParmValue = DEFAULT_DOS_CHARSET;
1230 global_iconv_handle = smb_iconv_handle_reinit(NULL,
1231 lpcfg_dos_charset(lp_ctx),
1232 lpcfg_unix_charset(lp_ctx),
1233 true, global_iconv_handle);
1237 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1240 bool handle_printing(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1241 const char *pszParmValue, char **ptr)
1243 static int parm_num = -1;
1245 if (parm_num == -1) {
1246 parm_num = lpcfg_map_parameter("printing");
1249 if (!lp_set_enum_parm(&parm_table[parm_num], pszParmValue, (int*)ptr)) {
1250 return false;
1253 if (lp_ctx->s3_fns) {
1254 if (service == NULL) {
1255 init_printer_values(lp_ctx, lp_ctx->globals->ctx, lp_ctx->sDefault);
1256 } else {
1257 init_printer_values(lp_ctx, service, service);
1261 return true;
1264 bool handle_ldap_debug_level(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1265 const char *pszParmValue, char **ptr)
1267 lp_ctx->globals->ldap_debug_level = lp_int(pszParmValue);
1269 if (lp_ctx->s3_fns) {
1270 lp_ctx->s3_fns->init_ldap_debugging();
1272 return true;
1275 bool handle_netbios_aliases(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1276 const char *pszParmValue, char **ptr)
1278 TALLOC_FREE(lp_ctx->globals->netbios_aliases);
1279 lp_ctx->globals->netbios_aliases = str_list_make_v3_const(lp_ctx->globals->ctx,
1280 pszParmValue, NULL);
1282 if (lp_ctx->s3_fns) {
1283 return lp_ctx->s3_fns->set_netbios_aliases(lp_ctx->globals->netbios_aliases);
1285 return true;
1289 * idmap related parameters
1292 bool handle_idmap_backend(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1293 const char *pszParmValue, char **ptr)
1295 if (lp_ctx->s3_fns) {
1296 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : backend",
1297 pszParmValue, 0);
1300 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1303 bool handle_idmap_uid(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1304 const char *pszParmValue, char **ptr)
1306 if (lp_ctx->s3_fns) {
1307 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : range",
1308 pszParmValue, 0);
1311 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1314 bool handle_idmap_gid(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1315 const char *pszParmValue, char **ptr)
1317 if (lp_ctx->s3_fns) {
1318 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : range",
1319 pszParmValue, 0);
1322 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1325 bool handle_smb_ports(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1326 const char *pszParmValue, char **ptr)
1328 static int parm_num = -1;
1329 int i;
1330 const char **list;
1332 if (!pszParmValue || !*pszParmValue) {
1333 return false;
1336 if (parm_num == -1) {
1337 parm_num = lpcfg_map_parameter("smb ports");
1338 if (parm_num == -1) {
1339 return false;
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;
1554 return true;
1558 bool set_variable(TALLOC_CTX *mem_ctx, struct loadparm_service *service, int parmnum, void *parm_ptr,
1559 const char *pszParmName, const char *pszParmValue,
1560 struct loadparm_context *lp_ctx, bool on_globals)
1562 int i;
1563 bool ok;
1565 /* if it is a special case then go ahead */
1566 if (parm_table[parmnum].special) {
1567 ok = parm_table[parmnum].special(lp_ctx, service, pszParmValue,
1568 (char **)parm_ptr);
1569 } else {
1570 ok = set_variable_helper(mem_ctx, parmnum, parm_ptr,
1571 pszParmName, pszParmValue);
1574 if (!ok) {
1575 return false;
1578 if (on_globals && (lp_ctx->flags[parmnum] & FLAG_DEFAULT)) {
1579 lp_ctx->flags[parmnum] &= ~FLAG_DEFAULT;
1580 /* we have to also unset FLAG_DEFAULT on aliases */
1581 for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
1582 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1584 for (i=parmnum+1;i<num_parameters() && parm_table[i].offset == parm_table[parmnum].offset;i++) {
1585 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1588 return true;
1592 bool lpcfg_do_global_parameter(struct loadparm_context *lp_ctx,
1593 const char *pszParmName, const char *pszParmValue)
1595 int parmnum = lpcfg_map_parameter(pszParmName);
1596 void *parm_ptr;
1598 if (parmnum < 0) {
1599 if (strchr(pszParmName, ':')) {
1600 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName, pszParmValue, 0);
1602 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1603 return true;
1606 /* if the flag has been set on the command line, then don't allow override,
1607 but don't report an error */
1608 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1609 return true;
1612 if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
1613 DEBUG(1, ("WARNING: The \"%s\" option is deprecated\n",
1614 pszParmName));
1617 parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[parmnum]);
1619 return set_variable(lp_ctx->globals->ctx, NULL, parmnum, parm_ptr,
1620 pszParmName, pszParmValue, lp_ctx, true);
1623 bool lpcfg_do_service_parameter(struct loadparm_context *lp_ctx,
1624 struct loadparm_service *service,
1625 const char *pszParmName, const char *pszParmValue)
1627 void *parm_ptr;
1628 int i;
1629 int parmnum = lpcfg_map_parameter(pszParmName);
1631 if (parmnum < 0) {
1632 if (strchr(pszParmName, ':')) {
1633 return lp_do_parameter_parametric(lp_ctx, service, pszParmName, pszParmValue, 0);
1635 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1636 return true;
1639 /* if the flag has been set on the command line, then don't allow override,
1640 but don't report an error */
1641 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1642 return true;
1645 if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
1646 DEBUG(1, ("WARNING: The \"%s\" option is deprecated\n",
1647 pszParmName));
1650 if (parm_table[parmnum].p_class == P_GLOBAL) {
1651 DEBUG(0,
1652 ("Global parameter %s found in service section!\n",
1653 pszParmName));
1654 return true;
1656 parm_ptr = ((char *)service) + parm_table[parmnum].offset;
1658 if (!service->copymap)
1659 init_copymap(service);
1661 /* this handles the aliases - set the copymap for other
1662 * entries with the same data pointer */
1663 for (i = 0; parm_table[i].label; i++)
1664 if (parm_table[i].offset == parm_table[parmnum].offset &&
1665 parm_table[i].p_class == parm_table[parmnum].p_class)
1666 bitmap_clear(service->copymap, i);
1668 return set_variable(service, service, parmnum, parm_ptr, pszParmName,
1669 pszParmValue, lp_ctx, false);
1673 * Process a parameter.
1676 bool lpcfg_do_parameter(const char *pszParmName, const char *pszParmValue,
1677 void *userdata)
1679 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1681 if (lp_ctx->bInGlobalSection)
1682 return lpcfg_do_global_parameter(lp_ctx, pszParmName,
1683 pszParmValue);
1684 else
1685 return lpcfg_do_service_parameter(lp_ctx, lp_ctx->currentService,
1686 pszParmName, pszParmValue);
1690 variable argument do parameter
1692 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx, const char *pszParmName, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
1693 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx,
1694 const char *pszParmName, const char *fmt, ...)
1696 char *s;
1697 bool ret;
1698 va_list ap;
1700 va_start(ap, fmt);
1701 s = talloc_vasprintf(NULL, fmt, ap);
1702 va_end(ap);
1703 ret = lpcfg_do_global_parameter(lp_ctx, pszParmName, s);
1704 talloc_free(s);
1705 return ret;
1710 set a parameter from the commandline - this is called from command line parameter
1711 parsing code. It sets the parameter then marks the parameter as unable to be modified
1712 by smb.conf processing
1714 bool lpcfg_set_cmdline(struct loadparm_context *lp_ctx, const char *pszParmName,
1715 const char *pszParmValue)
1717 int parmnum;
1718 int i;
1720 while (isspace((unsigned char)*pszParmValue)) pszParmValue++;
1722 parmnum = lpcfg_map_parameter(pszParmName);
1724 if (parmnum < 0 && strchr(pszParmName, ':')) {
1725 /* set a parametric option */
1726 bool ok;
1727 ok = lp_do_parameter_parametric(lp_ctx, NULL, pszParmName,
1728 pszParmValue, FLAG_CMDLINE);
1729 if (lp_ctx->s3_fns != NULL) {
1730 if (ok) {
1731 lp_ctx->s3_fns->store_cmdline(pszParmName, pszParmValue);
1734 return ok;
1737 if (parmnum < 0) {
1738 DEBUG(0,("Unknown option '%s'\n", pszParmName));
1739 return false;
1742 /* reset the CMDLINE flag in case this has been called before */
1743 lp_ctx->flags[parmnum] &= ~FLAG_CMDLINE;
1745 if (!lpcfg_do_global_parameter(lp_ctx, pszParmName, pszParmValue)) {
1746 return false;
1749 lp_ctx->flags[parmnum] |= FLAG_CMDLINE;
1751 /* we have to also set FLAG_CMDLINE on aliases */
1752 for (i=parmnum-1;
1753 i>=0 && parm_table[i].p_class == parm_table[parmnum].p_class &&
1754 parm_table[i].offset == parm_table[parmnum].offset;
1755 i--) {
1756 lp_ctx->flags[i] |= FLAG_CMDLINE;
1758 for (i=parmnum+1;
1759 i<num_parameters() &&
1760 parm_table[i].p_class == parm_table[parmnum].p_class &&
1761 parm_table[i].offset == parm_table[parmnum].offset;
1762 i++) {
1763 lp_ctx->flags[i] |= FLAG_CMDLINE;
1766 if (lp_ctx->s3_fns != NULL) {
1767 lp_ctx->s3_fns->store_cmdline(pszParmName, pszParmValue);
1770 return true;
1774 set a option from the commandline in 'a=b' format. Use to support --option
1776 bool lpcfg_set_option(struct loadparm_context *lp_ctx, const char *option)
1778 char *p, *s;
1779 bool ret;
1781 s = talloc_strdup(NULL, option);
1782 if (!s) {
1783 return false;
1786 p = strchr(s, '=');
1787 if (!p) {
1788 talloc_free(s);
1789 return false;
1792 *p = 0;
1794 ret = lpcfg_set_cmdline(lp_ctx, s, p+1);
1795 talloc_free(s);
1796 return ret;
1800 #define BOOLSTR(b) ((b) ? "Yes" : "No")
1803 * Print a parameter of the specified type.
1806 void lpcfg_print_parameter(struct parm_struct *p, void *ptr, FILE * f)
1808 /* For the seperation of lists values that we print below */
1809 const char *list_sep = ", ";
1810 int i;
1811 switch (p->type)
1813 case P_ENUM:
1814 for (i = 0; p->enum_list[i].name; i++) {
1815 if (*(int *)ptr == p->enum_list[i].value) {
1816 fprintf(f, "%s",
1817 p->enum_list[i].name);
1818 break;
1821 break;
1823 case P_BOOL:
1824 fprintf(f, "%s", BOOLSTR(*(bool *)ptr));
1825 break;
1827 case P_BOOLREV:
1828 fprintf(f, "%s", BOOLSTR(!*(bool *)ptr));
1829 break;
1831 case P_INTEGER:
1832 case P_BYTES:
1833 fprintf(f, "%d", *(int *)ptr);
1834 break;
1836 case P_CHAR:
1837 fprintf(f, "%c", *(char *)ptr);
1838 break;
1840 case P_OCTAL: {
1841 int val = *(int *)ptr;
1842 if (val == -1) {
1843 fprintf(f, "-1");
1844 } else {
1845 fprintf(f, "0%03o", val);
1847 break;
1850 case P_CMDLIST:
1851 list_sep = " ";
1852 /* fall through */
1853 case P_LIST:
1854 if ((char ***)ptr && *(char ***)ptr) {
1855 char **list = *(char ***)ptr;
1856 for (; *list; list++) {
1857 /* surround strings with whitespace in double quotes */
1858 if (*(list+1) == NULL) {
1859 /* last item, no extra separator */
1860 list_sep = "";
1862 if ( strchr_m( *list, ' ' ) ) {
1863 fprintf(f, "\"%s\"%s", *list, list_sep);
1864 } else {
1865 fprintf(f, "%s%s", *list, list_sep);
1869 break;
1871 case P_STRING:
1872 case P_USTRING:
1873 if (*(char **)ptr) {
1874 fprintf(f, "%s", *(char **)ptr);
1876 break;
1881 * Check if two parameters are equal.
1884 static bool lpcfg_equal_parameter(parm_type type, void *ptr1, void *ptr2)
1886 switch (type) {
1887 case P_BOOL:
1888 case P_BOOLREV:
1889 return (*((bool *)ptr1) == *((bool *)ptr2));
1891 case P_INTEGER:
1892 case P_ENUM:
1893 case P_OCTAL:
1894 case P_BYTES:
1895 return (*((int *)ptr1) == *((int *)ptr2));
1897 case P_CHAR:
1898 return (*((char *)ptr1) == *((char *)ptr2));
1900 case P_LIST:
1901 case P_CMDLIST:
1902 return str_list_equal(*(const char ***)ptr1, *(const char ***)ptr2);
1904 case P_STRING:
1905 case P_USTRING:
1907 char *p1 = *(char **)ptr1, *p2 = *(char **)ptr2;
1908 if (p1 && !*p1)
1909 p1 = NULL;
1910 if (p2 && !*p2)
1911 p2 = NULL;
1912 return (p1 == p2 || strequal(p1, p2));
1915 return false;
1919 * Process a new section (service).
1921 * At this stage all sections are services.
1922 * Later we'll have special sections that permit server parameters to be set.
1923 * Returns True on success, False on failure.
1926 static bool do_section(const char *pszSectionName, void *userdata)
1928 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1929 bool bRetval;
1930 bool isglobal;
1932 if (lp_ctx->s3_fns != NULL) {
1933 return lp_ctx->s3_fns->do_section(pszSectionName, lp_ctx);
1936 isglobal = ((strwicmp(pszSectionName, GLOBAL_NAME) == 0) ||
1937 (strwicmp(pszSectionName, GLOBAL_NAME2) == 0));
1939 bRetval = false;
1941 /* if we've just struck a global section, note the fact. */
1942 lp_ctx->bInGlobalSection = isglobal;
1944 /* check for multiple global sections */
1945 if (lp_ctx->bInGlobalSection) {
1946 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1947 return true;
1950 /* if we have a current service, tidy it up before moving on */
1951 bRetval = true;
1953 if (lp_ctx->currentService != NULL)
1954 bRetval = lpcfg_service_ok(lp_ctx->currentService);
1956 /* if all is still well, move to the next record in the services array */
1957 if (bRetval) {
1958 /* We put this here to avoid an odd message order if messages are */
1959 /* issued by the post-processing of a previous section. */
1960 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1962 if ((lp_ctx->currentService = lpcfg_add_service(lp_ctx, lp_ctx->sDefault,
1963 pszSectionName))
1964 == NULL) {
1965 DEBUG(0, ("Failed to add a new service\n"));
1966 return false;
1970 return bRetval;
1975 * Determine if a particular base parameter is currently set to the default value.
1978 static bool is_default(void *base_structure, int i)
1980 void *def_ptr = ((char *)base_structure) + parm_table[i].offset;
1981 switch (parm_table[i].type) {
1982 case P_CMDLIST:
1983 case P_LIST:
1984 return str_list_equal((const char * const *)parm_table[i].def.lvalue,
1985 *(const char * const **)def_ptr);
1986 case P_STRING:
1987 case P_USTRING:
1988 return strequal(parm_table[i].def.svalue,
1989 *(char **)def_ptr);
1990 case P_BOOL:
1991 case P_BOOLREV:
1992 return parm_table[i].def.bvalue ==
1993 *(bool *)def_ptr;
1994 case P_INTEGER:
1995 case P_CHAR:
1996 case P_OCTAL:
1997 case P_BYTES:
1998 case P_ENUM:
1999 return parm_table[i].def.ivalue ==
2000 *(int *)def_ptr;
2002 return false;
2006 *Display the contents of the global structure.
2009 void lpcfg_dump_globals(struct loadparm_context *lp_ctx, FILE *f,
2010 bool show_defaults)
2012 int i;
2013 struct parmlist_entry *data;
2015 fprintf(f, "# Global parameters\n[global]\n");
2017 for (i = 0; parm_table[i].label; i++)
2018 if (parm_table[i].p_class == P_GLOBAL &&
2019 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset))) {
2020 if (!show_defaults) {
2021 if (lp_ctx->flags && (lp_ctx->flags[i] & FLAG_DEFAULT)) {
2022 continue;
2025 if (is_default(lp_ctx->globals, i)) {
2026 continue;
2030 fprintf(f, "\t%s = ", parm_table[i].label);
2031 lpcfg_print_parameter(&parm_table[i], lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[i]), f);
2032 fprintf(f, "\n");
2034 if (lp_ctx->globals->param_opt != NULL) {
2035 for (data = lp_ctx->globals->param_opt; data;
2036 data = data->next) {
2037 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
2038 continue;
2040 fprintf(f, "\t%s = %s\n", data->key, data->value);
2047 * Display the contents of a single services record.
2050 void lpcfg_dump_a_service(struct loadparm_service * pService, struct loadparm_service *sDefault, FILE * f,
2051 unsigned int *flags, bool show_defaults)
2053 int i;
2054 struct parmlist_entry *data;
2056 if (pService != sDefault)
2057 fprintf(f, "\n[%s]\n", pService->szService);
2059 for (i = 0; parm_table[i].label; i++) {
2060 if (parm_table[i].p_class == P_LOCAL &&
2061 (*parm_table[i].label != '-') &&
2062 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset)))
2064 if (pService == sDefault) {
2065 if (!show_defaults) {
2066 if (flags && (flags[i] & FLAG_DEFAULT)) {
2067 continue;
2070 if (is_default(sDefault, i)) {
2071 continue;
2074 } else {
2075 if (lpcfg_equal_parameter(parm_table[i].type,
2076 ((char *)pService) +
2077 parm_table[i].offset,
2078 ((char *)sDefault) +
2079 parm_table[i].offset))
2080 continue;
2083 fprintf(f, "\t%s = ", parm_table[i].label);
2084 lpcfg_print_parameter(&parm_table[i],
2085 ((char *)pService) + parm_table[i].offset, f);
2086 fprintf(f, "\n");
2089 if (pService->param_opt != NULL) {
2090 for (data = pService->param_opt; data; data = data->next) {
2091 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
2092 continue;
2094 fprintf(f, "\t%s = %s\n", data->key, data->value);
2099 bool lpcfg_dump_a_parameter(struct loadparm_context *lp_ctx,
2100 struct loadparm_service *service,
2101 const char *parm_name, FILE * f)
2103 struct parm_struct *parm;
2104 void *ptr;
2105 char *local_parm_name;
2106 char *parm_opt;
2107 const char *parm_opt_value;
2109 /* check for parametrical option */
2110 local_parm_name = talloc_strdup(lp_ctx, parm_name);
2111 if (local_parm_name == NULL) {
2112 return false;
2115 parm_opt = strchr( local_parm_name, ':');
2117 if (parm_opt) {
2118 *parm_opt = '\0';
2119 parm_opt++;
2120 if (strlen(parm_opt)) {
2121 parm_opt_value = lpcfg_parm_string(lp_ctx, service,
2122 local_parm_name, parm_opt);
2123 if (parm_opt_value) {
2124 fprintf(f, "%s\n", parm_opt_value);
2125 return true;
2128 return false;
2131 /* parameter is not parametric, search the table */
2132 parm = lpcfg_parm_struct(lp_ctx, parm_name);
2133 if (!parm) {
2134 return false;
2137 if (service != NULL && parm->p_class == P_GLOBAL) {
2138 return false;
2141 ptr = lpcfg_parm_ptr(lp_ctx, service,parm);
2143 lpcfg_print_parameter(parm, ptr, f);
2144 fprintf(f, "\n");
2145 return true;
2149 * Auto-load some home services.
2151 static void lpcfg_add_auto_services(struct loadparm_context *lp_ctx,
2152 const char *str)
2154 return;
2157 /***************************************************************************
2158 Initialise the sDefault parameter structure for the printer values.
2159 ***************************************************************************/
2161 void init_printer_values(struct loadparm_context *lp_ctx, TALLOC_CTX *ctx,
2162 struct loadparm_service *pService)
2164 /* choose defaults depending on the type of printing */
2165 switch (pService->printing) {
2166 case PRINT_BSD:
2167 case PRINT_AIX:
2168 case PRINT_LPRNT:
2169 case PRINT_LPROS2:
2170 lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P'%p'");
2171 lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P'%p' %j");
2172 lpcfg_string_set(ctx, &pService->print_command, "lpr -r -P'%p' %s");
2173 break;
2175 case PRINT_LPRNG:
2176 case PRINT_PLP:
2177 lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P'%p'");
2178 lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P'%p' %j");
2179 lpcfg_string_set(ctx, &pService->print_command, "lpr -r -P'%p' %s");
2180 lpcfg_string_set(ctx, &pService->queuepause_command, "lpc stop '%p'");
2181 lpcfg_string_set(ctx, &pService->queueresume_command, "lpc start '%p'");
2182 lpcfg_string_set(ctx, &pService->lppause_command, "lpc hold '%p' %j");
2183 lpcfg_string_set(ctx, &pService->lpresume_command, "lpc release '%p' %j");
2184 break;
2186 case PRINT_CUPS:
2187 case PRINT_IPRINT:
2188 /* set the lpq command to contain the destination printer
2189 name only. This is used by cups_queue_get() */
2190 lpcfg_string_set(ctx, &pService->lpq_command, "%p");
2191 lpcfg_string_set(ctx, &pService->lprm_command, "");
2192 lpcfg_string_set(ctx, &pService->print_command, "");
2193 lpcfg_string_set(ctx, &pService->lppause_command, "");
2194 lpcfg_string_set(ctx, &pService->lpresume_command, "");
2195 lpcfg_string_set(ctx, &pService->queuepause_command, "");
2196 lpcfg_string_set(ctx, &pService->queueresume_command, "");
2197 break;
2199 case PRINT_SYSV:
2200 case PRINT_HPUX:
2201 lpcfg_string_set(ctx, &pService->lpq_command, "lpstat -o%p");
2202 lpcfg_string_set(ctx, &pService->lprm_command, "cancel %p-%j");
2203 lpcfg_string_set(ctx, &pService->print_command, "lp -c -d%p %s; rm %s");
2204 lpcfg_string_set(ctx, &pService->queuepause_command, "disable %p");
2205 lpcfg_string_set(ctx, &pService->queueresume_command, "enable %p");
2206 #ifndef HPUX
2207 lpcfg_string_set(ctx, &pService->lppause_command, "lp -i %p-%j -H hold");
2208 lpcfg_string_set(ctx, &pService->lpresume_command, "lp -i %p-%j -H resume");
2209 #endif /* HPUX */
2210 break;
2212 case PRINT_QNX:
2213 lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P%p");
2214 lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P%p %j");
2215 lpcfg_string_set(ctx, &pService->print_command, "lp -r -P%p %s");
2216 break;
2218 #if defined(DEVELOPER) || defined(ENABLE_SELFTEST)
2220 case PRINT_TEST:
2221 case PRINT_VLP: {
2222 const char *tdbfile;
2223 TALLOC_CTX *tmp_ctx = talloc_new(ctx);
2224 const char *tmp;
2226 tmp = lpcfg_parm_string(lp_ctx, NULL, "vlp", "tdbfile");
2227 if (tmp == NULL) {
2228 tmp = "/tmp/vlp.tdb";
2231 tdbfile = talloc_asprintf(tmp_ctx, "tdbfile=%s", tmp);
2232 if (tdbfile == NULL) {
2233 tdbfile="tdbfile=/tmp/vlp.tdb";
2236 tmp = talloc_asprintf(tmp_ctx, "vlp %s print %%p %%s",
2237 tdbfile);
2238 lpcfg_string_set(ctx, &pService->print_command,
2239 tmp ? tmp : "vlp print %p %s");
2241 tmp = talloc_asprintf(tmp_ctx, "vlp %s lpq %%p",
2242 tdbfile);
2243 lpcfg_string_set(ctx, &pService->lpq_command,
2244 tmp ? tmp : "vlp lpq %p");
2246 tmp = talloc_asprintf(tmp_ctx, "vlp %s lprm %%p %%j",
2247 tdbfile);
2248 lpcfg_string_set(ctx, &pService->lprm_command,
2249 tmp ? tmp : "vlp lprm %p %j");
2251 tmp = talloc_asprintf(tmp_ctx, "vlp %s lppause %%p %%j",
2252 tdbfile);
2253 lpcfg_string_set(ctx, &pService->lppause_command,
2254 tmp ? tmp : "vlp lppause %p %j");
2256 tmp = talloc_asprintf(tmp_ctx, "vlp %s lpresume %%p %%j",
2257 tdbfile);
2258 lpcfg_string_set(ctx, &pService->lpresume_command,
2259 tmp ? tmp : "vlp lpresume %p %j");
2261 tmp = talloc_asprintf(tmp_ctx, "vlp %s queuepause %%p",
2262 tdbfile);
2263 lpcfg_string_set(ctx, &pService->queuepause_command,
2264 tmp ? tmp : "vlp queuepause %p");
2266 tmp = talloc_asprintf(tmp_ctx, "vlp %s queueresume %%p",
2267 tdbfile);
2268 lpcfg_string_set(ctx, &pService->queueresume_command,
2269 tmp ? tmp : "vlp queueresume %p");
2270 TALLOC_FREE(tmp_ctx);
2272 break;
2274 #endif /* DEVELOPER */
2280 * Unload unused services.
2283 void lpcfg_killunused(struct loadparm_context *lp_ctx,
2284 struct smbsrv_connection *smb,
2285 bool (*snumused) (struct smbsrv_connection *, int))
2287 int i;
2289 if (lp_ctx->s3_fns != NULL) {
2290 smb_panic("Cannot be used from an s3 loadparm ctx");
2293 for (i = 0; i < lp_ctx->iNumServices; i++) {
2294 if (lp_ctx->services[i] == NULL)
2295 continue;
2297 if (!snumused || !snumused(smb, i)) {
2298 talloc_free(lp_ctx->services[i]);
2299 lp_ctx->services[i] = NULL;
2305 static int lpcfg_destructor(struct loadparm_context *lp_ctx)
2307 struct parmlist_entry *data;
2309 if (lp_ctx->refuse_free) {
2310 /* someone is trying to free the
2311 global_loadparm_context.
2312 We can't allow that. */
2313 return -1;
2316 if (lp_ctx->globals->param_opt != NULL) {
2317 struct parmlist_entry *next;
2318 for (data = lp_ctx->globals->param_opt; data; data=next) {
2319 next = data->next;
2320 if (data->priority & FLAG_CMDLINE) continue;
2321 DLIST_REMOVE(lp_ctx->globals->param_opt, data);
2322 talloc_free(data);
2326 return 0;
2329 struct defaults_hook_data {
2330 const char *name;
2331 lpcfg_defaults_hook hook;
2332 struct defaults_hook_data *prev, *next;
2333 } *defaults_hooks = NULL;
2336 bool lpcfg_register_defaults_hook(const char *name, lpcfg_defaults_hook hook)
2338 struct defaults_hook_data *hook_data = talloc(talloc_autofree_context(),
2339 struct defaults_hook_data);
2340 hook_data->name = talloc_strdup(hook_data, name);
2341 hook_data->hook = hook;
2342 DLIST_ADD(defaults_hooks, hook_data);
2343 return false;
2347 * Initialise the global parameter structure.
2349 * Note that most callers should use loadparm_init_global() instead
2351 struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
2353 int i;
2354 char *myname;
2355 struct loadparm_context *lp_ctx;
2356 struct parmlist_entry *parm;
2357 char *logfile;
2358 struct defaults_hook_data *defaults_hook;
2360 lp_ctx = talloc_zero(mem_ctx, struct loadparm_context);
2361 if (lp_ctx == NULL)
2362 return NULL;
2364 talloc_set_destructor(lp_ctx, lpcfg_destructor);
2365 lp_ctx->bInGlobalSection = true;
2366 lp_ctx->globals = talloc_zero(lp_ctx, struct loadparm_global);
2367 /* This appears odd, but globals in s3 isn't a pointer */
2368 lp_ctx->globals->ctx = lp_ctx->globals;
2369 lp_ctx->sDefault = talloc_zero(lp_ctx, struct loadparm_service);
2370 lp_ctx->flags = talloc_zero_array(lp_ctx, unsigned int, num_parameters());
2372 lp_ctx->sDefault->iMaxPrintJobs = 1000;
2373 lp_ctx->sDefault->bAvailable = true;
2374 lp_ctx->sDefault->browseable = true;
2375 lp_ctx->sDefault->read_only = true;
2376 lp_ctx->sDefault->map_archive = true;
2377 lp_ctx->sDefault->strict_locking = true;
2378 lp_ctx->sDefault->oplocks = true;
2379 lp_ctx->sDefault->create_mask = 0744;
2380 lp_ctx->sDefault->force_create_mode = 0000;
2381 lp_ctx->sDefault->directory_mask = 0755;
2382 lp_ctx->sDefault->force_directory_mode = 0000;
2384 DEBUG(3, ("Initialising global parameters\n"));
2386 for (i = 0; parm_table[i].label; i++) {
2387 if ((parm_table[i].type == P_STRING ||
2388 parm_table[i].type == P_USTRING) &&
2389 !(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2390 char **r;
2391 if (parm_table[i].p_class == P_LOCAL) {
2392 r = (char **)(((char *)lp_ctx->sDefault) + parm_table[i].offset);
2393 } else {
2394 r = (char **)(((char *)lp_ctx->globals) + parm_table[i].offset);
2396 *r = talloc_strdup(lp_ctx, "");
2400 logfile = talloc_asprintf(lp_ctx, "%s/log.samba", dyn_LOGFILEBASE);
2401 lpcfg_do_global_parameter(lp_ctx, "log file", logfile);
2402 talloc_free(logfile);
2404 lpcfg_do_global_parameter(lp_ctx, "log level", "0");
2406 lpcfg_do_global_parameter(lp_ctx, "syslog", "1");
2407 lpcfg_do_global_parameter(lp_ctx, "syslog only", "No");
2408 lpcfg_do_global_parameter(lp_ctx, "debug timestamp", "Yes");
2409 lpcfg_do_global_parameter(lp_ctx, "debug prefix timestamp", "No");
2410 lpcfg_do_global_parameter(lp_ctx, "debug hires timestamp", "Yes");
2411 lpcfg_do_global_parameter(lp_ctx, "debug pid", "No");
2412 lpcfg_do_global_parameter(lp_ctx, "debug uid", "No");
2413 lpcfg_do_global_parameter(lp_ctx, "debug class", "No");
2415 lpcfg_do_global_parameter(lp_ctx, "share backend", "classic");
2417 lpcfg_do_global_parameter(lp_ctx, "server role", "auto");
2418 lpcfg_do_global_parameter(lp_ctx, "domain logons", "No");
2419 lpcfg_do_global_parameter(lp_ctx, "domain master", "Auto");
2421 /* options that can be set on the command line must be initialised via
2422 the slower lpcfg_do_global_parameter() to ensure that FLAG_CMDLINE is obeyed */
2423 #ifdef TCP_NODELAY
2424 lpcfg_do_global_parameter(lp_ctx, "socket options", "TCP_NODELAY");
2425 #endif
2426 lpcfg_do_global_parameter(lp_ctx, "workgroup", DEFAULT_WORKGROUP);
2427 myname = get_myname(lp_ctx);
2428 lpcfg_do_global_parameter(lp_ctx, "netbios name", myname);
2429 talloc_free(myname);
2430 lpcfg_do_global_parameter(lp_ctx, "name resolve order", "lmhosts wins host bcast");
2432 lpcfg_do_global_parameter(lp_ctx, "fstype", "NTFS");
2434 lpcfg_do_global_parameter(lp_ctx, "ntvfs handler", "unixuid default");
2435 lpcfg_do_global_parameter(lp_ctx, "max connections", "0");
2437 lpcfg_do_global_parameter(lp_ctx, "dcerpc endpoint servers", "epmapper wkssvc rpcecho samr netlogon lsarpc spoolss drsuapi dssetup unixinfo browser eventlog6 backupkey dnsserver");
2438 lpcfg_do_global_parameter(lp_ctx, "server services", "s3fs rpc nbt wrepl ldap cldap kdc drepl winbindd ntp_signd kcc dnsupdate dns");
2439 lpcfg_do_global_parameter(lp_ctx, "kccsrv:samba_kcc", "false");
2440 /* the winbind method for domain controllers is for both RODC
2441 auth forwarding and for trusted domains */
2442 lpcfg_do_global_parameter(lp_ctx, "private dir", dyn_PRIVATE_DIR);
2443 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_LOCAL_MACHINE", "hklm.ldb");
2445 /* This hive should be dynamically generated by Samba using
2446 data from the sam, but for the moment leave it in a tdb to
2447 keep regedt32 from popping up an annoying dialog. */
2448 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_USERS", "hku.ldb");
2450 /* using UTF8 by default allows us to support all chars */
2451 lpcfg_do_global_parameter(lp_ctx, "unix charset", "UTF-8");
2453 /* Use codepage 850 as a default for the dos character set */
2454 lpcfg_do_global_parameter(lp_ctx, "dos charset", "CP850");
2457 * Allow the default PASSWD_CHAT to be overridden in local.h.
2459 lpcfg_do_global_parameter(lp_ctx, "passwd chat", DEFAULT_PASSWD_CHAT);
2461 lpcfg_do_global_parameter(lp_ctx, "pid directory", dyn_PIDDIR);
2462 lpcfg_do_global_parameter(lp_ctx, "lock dir", dyn_LOCKDIR);
2463 lpcfg_do_global_parameter(lp_ctx, "state directory", dyn_STATEDIR);
2464 lpcfg_do_global_parameter(lp_ctx, "cache directory", dyn_CACHEDIR);
2465 lpcfg_do_global_parameter(lp_ctx, "ncalrpc dir", dyn_NCALRPCDIR);
2467 lpcfg_do_global_parameter(lp_ctx, "nbt client socket address", "0.0.0.0");
2468 lpcfg_do_global_parameter_var(lp_ctx, "server string",
2469 "Samba %s", SAMBA_VERSION_STRING);
2471 lpcfg_do_global_parameter(lp_ctx, "password server", "*");
2473 lpcfg_do_global_parameter(lp_ctx, "max mux", "50");
2474 lpcfg_do_global_parameter(lp_ctx, "max xmit", "16644");
2475 lpcfg_do_global_parameter(lp_ctx, "host msdfs", "true");
2477 lpcfg_do_global_parameter(lp_ctx, "LargeReadwrite", "True");
2478 lpcfg_do_global_parameter(lp_ctx, "server min protocol", "LANMAN1");
2479 lpcfg_do_global_parameter(lp_ctx, "server max protocol", "SMB3");
2480 lpcfg_do_global_parameter(lp_ctx, "client min protocol", "CORE");
2481 lpcfg_do_global_parameter(lp_ctx, "client max protocol", "default");
2482 lpcfg_do_global_parameter(lp_ctx, "security", "AUTO");
2483 lpcfg_do_global_parameter(lp_ctx, "EncryptPasswords", "True");
2484 lpcfg_do_global_parameter(lp_ctx, "ReadRaw", "True");
2485 lpcfg_do_global_parameter(lp_ctx, "WriteRaw", "True");
2486 lpcfg_do_global_parameter(lp_ctx, "NullPasswords", "False");
2487 lpcfg_do_global_parameter(lp_ctx, "old password allowed period", "60");
2488 lpcfg_do_global_parameter(lp_ctx, "ObeyPamRestrictions", "False");
2490 lpcfg_do_global_parameter(lp_ctx, "TimeServer", "False");
2491 lpcfg_do_global_parameter(lp_ctx, "BindInterfacesOnly", "False");
2492 lpcfg_do_global_parameter(lp_ctx, "Unicode", "True");
2493 lpcfg_do_global_parameter(lp_ctx, "ClientLanManAuth", "False");
2494 lpcfg_do_global_parameter(lp_ctx, "ClientNTLMv2Auth", "True");
2495 lpcfg_do_global_parameter(lp_ctx, "LanmanAuth", "False");
2496 lpcfg_do_global_parameter(lp_ctx, "NTLMAuth", "True");
2497 lpcfg_do_global_parameter(lp_ctx, "client use spnego principal", "False");
2499 lpcfg_do_global_parameter(lp_ctx, "UnixExtensions", "True");
2501 lpcfg_do_global_parameter(lp_ctx, "PreferredMaster", "Auto");
2502 lpcfg_do_global_parameter(lp_ctx, "LocalMaster", "True");
2504 lpcfg_do_global_parameter(lp_ctx, "wins support", "False");
2505 lpcfg_do_global_parameter(lp_ctx, "dns proxy", "True");
2507 lpcfg_do_global_parameter(lp_ctx, "winbind separator", "\\");
2508 lpcfg_do_global_parameter(lp_ctx, "winbind sealed pipes", "True");
2509 lpcfg_do_global_parameter(lp_ctx, "require strong key", "True");
2510 lpcfg_do_global_parameter(lp_ctx, "winbindd socket directory", dyn_WINBINDD_SOCKET_DIR);
2511 lpcfg_do_global_parameter(lp_ctx, "winbindd privileged socket directory", dyn_WINBINDD_PRIVILEGED_SOCKET_DIR);
2512 lpcfg_do_global_parameter(lp_ctx, "ntp signd socket directory", dyn_NTP_SIGND_SOCKET_DIR);
2513 lpcfg_do_global_parameter_var(lp_ctx, "dns update command", "%s/samba_dnsupdate", dyn_SCRIPTSBINDIR);
2514 lpcfg_do_global_parameter_var(lp_ctx, "spn update command", "%s/samba_spnupdate", dyn_SCRIPTSBINDIR);
2515 lpcfg_do_global_parameter_var(lp_ctx, "samba kcc command",
2516 "%s/samba_kcc", dyn_SCRIPTSBINDIR);
2517 lpcfg_do_global_parameter(lp_ctx, "template shell", "/bin/false");
2518 lpcfg_do_global_parameter(lp_ctx, "template homedir", "/home/%D/%U");
2520 lpcfg_do_global_parameter(lp_ctx, "client signing", "default");
2521 lpcfg_do_global_parameter(lp_ctx, "server signing", "default");
2523 lpcfg_do_global_parameter(lp_ctx, "use spnego", "True");
2525 lpcfg_do_global_parameter(lp_ctx, "use mmap", "True");
2527 lpcfg_do_global_parameter(lp_ctx, "smb ports", "445 139");
2528 lpcfg_do_global_parameter_var(lp_ctx, "nbt port", "%d", NBT_NAME_SERVICE_PORT);
2529 lpcfg_do_global_parameter_var(lp_ctx, "dgram port", "%d", NBT_DGRAM_SERVICE_PORT);
2530 lpcfg_do_global_parameter(lp_ctx, "cldap port", "389");
2531 lpcfg_do_global_parameter(lp_ctx, "krb5 port", "88");
2532 lpcfg_do_global_parameter(lp_ctx, "kpasswd port", "464");
2533 lpcfg_do_global_parameter(lp_ctx, "web port", "901");
2535 lpcfg_do_global_parameter(lp_ctx, "nt status support", "True");
2537 lpcfg_do_global_parameter(lp_ctx, "max wins ttl", "518400"); /* 6 days */
2538 lpcfg_do_global_parameter(lp_ctx, "min wins ttl", "21600");
2540 lpcfg_do_global_parameter(lp_ctx, "tls enabled", "True");
2541 lpcfg_do_global_parameter(lp_ctx, "tls keyfile", "tls/key.pem");
2542 lpcfg_do_global_parameter(lp_ctx, "tls certfile", "tls/cert.pem");
2543 lpcfg_do_global_parameter(lp_ctx, "tls cafile", "tls/ca.pem");
2544 lpcfg_do_global_parameter(lp_ctx, "prefork children:smb", "4");
2546 lpcfg_do_global_parameter(lp_ctx, "rndc command", "/usr/sbin/rndc");
2547 lpcfg_do_global_parameter(lp_ctx, "nsupdate command", "/usr/bin/nsupdate -g");
2549 lpcfg_do_global_parameter(lp_ctx, "allow dns updates", "secure only");
2550 lpcfg_do_global_parameter(lp_ctx, "dns forwarder", "");
2552 lpcfg_do_global_parameter(lp_ctx, "algorithmic rid base", "1000");
2554 lpcfg_do_global_parameter(lp_ctx, "enhanced browsing", "True");
2556 lpcfg_do_global_parameter(lp_ctx, "winbind nss info", "template");
2558 lpcfg_do_global_parameter(lp_ctx, "server schannel", "Auto");
2560 lpcfg_do_global_parameter(lp_ctx, "short preserve case", "True");
2562 lpcfg_do_global_parameter(lp_ctx, "max open files", "16384");
2564 lpcfg_do_global_parameter(lp_ctx, "cups connection timeout", "30");
2566 lpcfg_do_global_parameter(lp_ctx, "locking", "True");
2568 lpcfg_do_global_parameter(lp_ctx, "block size", "1024");
2570 lpcfg_do_global_parameter(lp_ctx, "client use spnego", "True");
2572 lpcfg_do_global_parameter(lp_ctx, "change notify", "True");
2574 lpcfg_do_global_parameter(lp_ctx, "name cache timeout", "660");
2576 lpcfg_do_global_parameter(lp_ctx, "defer sharing violations", "True");
2578 lpcfg_do_global_parameter(lp_ctx, "ldap replication sleep", "1000");
2580 lpcfg_do_global_parameter(lp_ctx, "idmap backend", "tdb");
2582 lpcfg_do_global_parameter(lp_ctx, "enable privileges", "True");
2584 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max write", "%u", DEFAULT_SMB2_MAX_WRITE);
2586 lpcfg_do_global_parameter(lp_ctx, "passdb backend", "tdbsam");
2588 lpcfg_do_global_parameter(lp_ctx, "getwd cache", "True");
2590 lpcfg_do_global_parameter(lp_ctx, "winbind nested groups", "True");
2592 lpcfg_do_global_parameter(lp_ctx, "mangled names", "True");
2594 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max credits", "%u", DEFAULT_SMB2_MAX_CREDITS);
2596 lpcfg_do_global_parameter(lp_ctx, "ldap ssl", "start tls");
2598 lpcfg_do_global_parameter(lp_ctx, "ldap deref", "auto");
2600 lpcfg_do_global_parameter(lp_ctx, "lm interval", "60");
2602 lpcfg_do_global_parameter(lp_ctx, "mangling method", "hash2");
2604 lpcfg_do_global_parameter(lp_ctx, "hide dot files", "True");
2606 lpcfg_do_global_parameter(lp_ctx, "browse list", "True");
2608 lpcfg_do_global_parameter(lp_ctx, "passwd chat timeout", "2");
2610 lpcfg_do_global_parameter(lp_ctx, "guest account", GUEST_ACCOUNT);
2612 lpcfg_do_global_parameter(lp_ctx, "client schannel", "auto");
2614 lpcfg_do_global_parameter(lp_ctx, "smb encrypt", "default");
2616 lpcfg_do_global_parameter(lp_ctx, "max log size", "5000");
2618 lpcfg_do_global_parameter(lp_ctx, "idmap negative cache time", "120");
2620 lpcfg_do_global_parameter(lp_ctx, "ldap follow referral", "auto");
2622 lpcfg_do_global_parameter(lp_ctx, "multicast dns register", "yes");
2624 lpcfg_do_global_parameter(lp_ctx, "winbind reconnect delay", "30");
2626 lpcfg_do_global_parameter(lp_ctx, "winbind request timeout", "60");
2628 lpcfg_do_global_parameter(lp_ctx, "nt acl support", "yes");
2630 lpcfg_do_global_parameter(lp_ctx, "acl check permissions", "yes");
2632 lpcfg_do_global_parameter(lp_ctx, "keepalive", "300");
2634 lpcfg_do_global_parameter(lp_ctx, "smbd profiling level", "off");
2636 lpcfg_do_global_parameter(lp_ctx, "winbind cache time", "300");
2638 lpcfg_do_global_parameter(lp_ctx, "level2 oplocks", "yes");
2640 lpcfg_do_global_parameter(lp_ctx, "show add printer wizard", "yes");
2642 lpcfg_do_global_parameter(lp_ctx, "allocation roundup size", "1048576");
2644 lpcfg_do_global_parameter(lp_ctx, "ldap page size", "1024");
2646 lpcfg_do_global_parameter(lp_ctx, "kernel share modes", "yes");
2648 lpcfg_do_global_parameter(lp_ctx, "strict locking", "Auto");
2650 lpcfg_do_global_parameter(lp_ctx, "map readonly", "yes");
2652 lpcfg_do_global_parameter(lp_ctx, "allow trusted domains", "yes");
2654 lpcfg_do_global_parameter(lp_ctx, "default devmode", "yes");
2656 lpcfg_do_global_parameter(lp_ctx, "os level", "20");
2658 lpcfg_do_global_parameter(lp_ctx, "dos filetimes", "yes");
2660 lpcfg_do_global_parameter(lp_ctx, "mangling char", "~");
2662 lpcfg_do_global_parameter(lp_ctx, "printcap cache time", "750");
2664 lpcfg_do_global_parameter(lp_ctx, "create krb5 conf", "yes");
2666 lpcfg_do_global_parameter(lp_ctx, "winbind max clients", "200");
2668 lpcfg_do_global_parameter(lp_ctx, "acl map full control", "yes");
2670 lpcfg_do_global_parameter(lp_ctx, "nt pipe support", "yes");
2672 lpcfg_do_global_parameter(lp_ctx, "ldap debug threshold", "10");
2674 lpcfg_do_global_parameter(lp_ctx, "client ldap sasl wrapping", "sign");
2676 lpcfg_do_global_parameter(lp_ctx, "follow symlinks", "yes");
2678 lpcfg_do_global_parameter(lp_ctx, "machine password timeout", "604800");
2680 lpcfg_do_global_parameter(lp_ctx, "ldap connection timeout", "2");
2682 lpcfg_do_global_parameter(lp_ctx, "winbind expand groups", "0");
2684 lpcfg_do_global_parameter(lp_ctx, "stat cache", "yes");
2686 lpcfg_do_global_parameter(lp_ctx, "lpq cache time", "30");
2688 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max trans", "%u", DEFAULT_SMB2_MAX_TRANSACT);
2690 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max read", "%u", DEFAULT_SMB2_MAX_READ);
2692 lpcfg_do_global_parameter(lp_ctx, "durable handles", "yes");
2694 lpcfg_do_global_parameter(lp_ctx, "max stat cache size", "256");
2696 lpcfg_do_global_parameter(lp_ctx, "ldap passwd sync", "no");
2698 lpcfg_do_global_parameter(lp_ctx, "kernel change notify", "yes");
2700 lpcfg_do_global_parameter(lp_ctx, "max ttl", "259200");
2702 lpcfg_do_global_parameter(lp_ctx, "blocking locks", "yes");
2704 lpcfg_do_global_parameter(lp_ctx, "oplock contention limit", "2");
2706 lpcfg_do_global_parameter(lp_ctx, "load printers", "yes");
2708 lpcfg_do_global_parameter(lp_ctx, "idmap cache time", "604800");
2710 lpcfg_do_global_parameter(lp_ctx, "preserve case", "yes");
2712 lpcfg_do_global_parameter(lp_ctx, "lm announce", "auto");
2714 lpcfg_do_global_parameter(lp_ctx, "afs token lifetime", "604800");
2716 lpcfg_do_global_parameter(lp_ctx, "enable core files", "yes");
2718 lpcfg_do_global_parameter(lp_ctx, "winbind max domain connections", "1");
2720 lpcfg_do_global_parameter(lp_ctx, "case sensitive", "auto");
2722 lpcfg_do_global_parameter(lp_ctx, "ldap timeout", "15");
2724 lpcfg_do_global_parameter(lp_ctx, "mangle prefix", "1");
2726 lpcfg_do_global_parameter(lp_ctx, "posix locking", "yes");
2728 lpcfg_do_global_parameter(lp_ctx, "lock spin time", "200");
2730 lpcfg_do_global_parameter(lp_ctx, "directory name cache size", "100");
2732 lpcfg_do_global_parameter(lp_ctx, "nmbd bind explicit broadcast", "yes");
2734 lpcfg_do_global_parameter(lp_ctx, "init logon delay", "100");
2736 lpcfg_do_global_parameter(lp_ctx, "usershare owner only", "yes");
2738 lpcfg_do_global_parameter(lp_ctx, "-valid", "yes");
2740 lpcfg_do_global_parameter_var(lp_ctx, "usershare path", "%s/usershares", get_dyn_STATEDIR());
2742 #ifdef DEVELOPER
2743 lpcfg_do_global_parameter_var(lp_ctx, "panic action", "/bin/sleep 999999999");
2744 #endif
2746 lpcfg_do_global_parameter(lp_ctx, "smb passwd file", get_dyn_SMB_PASSWD_FILE());
2748 lpcfg_do_global_parameter(lp_ctx, "logon home", "\\\\%N\\%U");
2750 lpcfg_do_global_parameter(lp_ctx, "logon path", "\\\\%N\\%U\\profile");
2752 lpcfg_do_global_parameter(lp_ctx, "printjob username", "%U");
2754 /* Allow modules to adjust defaults */
2755 for (defaults_hook = defaults_hooks; defaults_hook;
2756 defaults_hook = defaults_hook->next) {
2757 bool ret;
2759 ret = defaults_hook->hook(lp_ctx);
2760 if (!ret) {
2761 DEBUG(1, ("Defaults hook %s failed to run.",
2762 defaults_hook->name));
2763 talloc_free(lp_ctx);
2764 return NULL;
2768 for (i = 0; parm_table[i].label; i++) {
2769 if (!(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2770 lp_ctx->flags[i] |= FLAG_DEFAULT;
2774 for (parm=lp_ctx->globals->param_opt; parm; parm=parm->next) {
2775 if (!(parm->priority & FLAG_CMDLINE)) {
2776 parm->priority |= FLAG_DEFAULT;
2780 for (parm=lp_ctx->sDefault->param_opt; parm; parm=parm->next) {
2781 if (!(parm->priority & FLAG_CMDLINE)) {
2782 parm->priority |= FLAG_DEFAULT;
2786 return lp_ctx;
2790 * Initialise the global parameter structure.
2792 struct loadparm_context *loadparm_init_global(bool load_default)
2794 if (global_loadparm_context == NULL) {
2795 global_loadparm_context = loadparm_init(NULL);
2797 if (global_loadparm_context == NULL) {
2798 return NULL;
2800 global_loadparm_context->global = true;
2801 if (load_default && !global_loadparm_context->loaded) {
2802 lpcfg_load_default(global_loadparm_context);
2804 global_loadparm_context->refuse_free = true;
2805 return global_loadparm_context;
2809 * Initialise the global parameter structure.
2811 struct loadparm_context *loadparm_init_s3(TALLOC_CTX *mem_ctx,
2812 const struct loadparm_s3_helpers *s3_fns)
2814 struct loadparm_context *loadparm_context = talloc_zero(mem_ctx, struct loadparm_context);
2815 if (!loadparm_context) {
2816 return NULL;
2818 loadparm_context->s3_fns = s3_fns;
2819 loadparm_context->globals = s3_fns->globals;
2820 loadparm_context->flags = s3_fns->flags;
2822 return loadparm_context;
2825 const char *lpcfg_configfile(struct loadparm_context *lp_ctx)
2827 return lp_ctx->szConfigFile;
2830 const char *lp_default_path(void)
2832 if (getenv("SMB_CONF_PATH"))
2833 return getenv("SMB_CONF_PATH");
2834 else
2835 return dyn_CONFIGFILE;
2839 * Update the internal state of a loadparm context after settings
2840 * have changed.
2842 static bool lpcfg_update(struct loadparm_context *lp_ctx)
2844 struct debug_settings settings;
2845 TALLOC_CTX *tmp_ctx;
2847 tmp_ctx = talloc_new(lp_ctx);
2848 if (tmp_ctx == NULL) {
2849 return false;
2852 lpcfg_add_auto_services(lp_ctx, lpcfg_auto_services(lp_ctx, tmp_ctx));
2854 if (!lp_ctx->globals->wins_server_list && lp_ctx->globals->we_are_a_wins_server) {
2855 lpcfg_do_global_parameter(lp_ctx, "wins server", "127.0.0.1");
2858 if (!lp_ctx->global) {
2859 TALLOC_FREE(tmp_ctx);
2860 return true;
2863 panic_action = lp_ctx->globals->panic_action;
2865 reload_charcnv(lp_ctx);
2867 ZERO_STRUCT(settings);
2868 /* Add any more debug-related smb.conf parameters created in
2869 * future here */
2870 settings.timestamp_logs = lp_ctx->globals->timestamp_logs;
2871 settings.debug_prefix_timestamp = lp_ctx->globals->debug_prefix_timestamp;
2872 settings.debug_hires_timestamp = lp_ctx->globals->debug_hires_timestamp;
2873 settings.debug_pid = lp_ctx->globals->debug_pid;
2874 settings.debug_uid = lp_ctx->globals->debug_uid;
2875 settings.debug_class = lp_ctx->globals->debug_class;
2876 debug_set_settings(&settings, lp_ctx->globals->logging,
2877 lp_ctx->globals->syslog,
2878 lp_ctx->globals->syslog_only);
2880 /* FIXME: This is a bit of a hack, but we can't use a global, since
2881 * not everything that uses lp also uses the socket library */
2882 if (lpcfg_parm_bool(lp_ctx, NULL, "socket", "testnonblock", false)) {
2883 setenv("SOCKET_TESTNONBLOCK", "1", 1);
2884 } else {
2885 unsetenv("SOCKET_TESTNONBLOCK");
2888 TALLOC_FREE(tmp_ctx);
2889 return true;
2892 bool lpcfg_load_default(struct loadparm_context *lp_ctx)
2894 const char *path;
2896 path = lp_default_path();
2898 if (!file_exist(path)) {
2899 /* We allow the default smb.conf file to not exist,
2900 * basically the equivalent of an empty file. */
2901 return lpcfg_update(lp_ctx);
2904 return lpcfg_load(lp_ctx, path);
2908 * Load the services array from the services file.
2910 * Return True on success, False on failure.
2912 bool lpcfg_load(struct loadparm_context *lp_ctx, const char *filename)
2914 char *n2;
2915 bool bRetval;
2917 filename = talloc_strdup(lp_ctx, filename);
2919 lp_ctx->szConfigFile = filename;
2921 if (lp_ctx->s3_fns) {
2922 return lp_ctx->s3_fns->load(filename);
2925 lp_ctx->bInGlobalSection = true;
2926 n2 = standard_sub_basic(lp_ctx, lp_ctx->szConfigFile);
2927 DEBUG(2, ("lpcfg_load: refreshing parameters from %s\n", n2));
2929 add_to_file_list(lp_ctx, &lp_ctx->file_lists, lp_ctx->szConfigFile, n2);
2931 /* We get sections first, so have to start 'behind' to make up */
2932 lp_ctx->currentService = NULL;
2933 bRetval = pm_process(n2, do_section, lpcfg_do_parameter, lp_ctx);
2935 /* finish up the last section */
2936 DEBUG(4, ("pm_process() returned %s\n", BOOLSTR(bRetval)));
2937 if (bRetval)
2938 if (lp_ctx->currentService != NULL)
2939 bRetval = lpcfg_service_ok(lp_ctx->currentService);
2941 bRetval = bRetval && lpcfg_update(lp_ctx);
2943 /* we do this unconditionally, so that it happens even
2944 for a missing smb.conf */
2945 reload_charcnv(lp_ctx);
2947 if (bRetval == true) {
2948 /* set this up so that any child python tasks will
2949 find the right smb.conf */
2950 setenv("SMB_CONF_PATH", filename, 1);
2952 /* set the context used by the lp_*() function
2953 varients */
2954 global_loadparm_context = lp_ctx;
2955 lp_ctx->loaded = true;
2958 return bRetval;
2962 * Return the max number of services.
2965 int lpcfg_numservices(struct loadparm_context *lp_ctx)
2967 if (lp_ctx->s3_fns) {
2968 return lp_ctx->s3_fns->get_numservices();
2971 return lp_ctx->iNumServices;
2975 * Display the contents of the services array in human-readable form.
2978 void lpcfg_dump(struct loadparm_context *lp_ctx, FILE *f, bool show_defaults,
2979 int maxtoprint)
2981 int iService;
2983 if (lp_ctx->s3_fns) {
2984 lp_ctx->s3_fns->dump(f, show_defaults, maxtoprint);
2985 return;
2988 lpcfg_dump_globals(lp_ctx, f, show_defaults);
2990 lpcfg_dump_a_service(lp_ctx->sDefault, lp_ctx->sDefault, f, lp_ctx->flags, show_defaults);
2992 for (iService = 0; iService < maxtoprint; iService++)
2993 lpcfg_dump_one(f, show_defaults, lp_ctx->services[iService], lp_ctx->sDefault);
2997 * Display the contents of one service in human-readable form.
2999 void lpcfg_dump_one(FILE *f, bool show_defaults, struct loadparm_service *service, struct loadparm_service *sDefault)
3001 if (service != NULL) {
3002 if (service->szService[0] == '\0')
3003 return;
3004 lpcfg_dump_a_service(service, sDefault, f, NULL, show_defaults);
3008 struct loadparm_service *lpcfg_servicebynum(struct loadparm_context *lp_ctx,
3009 int snum)
3011 if (lp_ctx->s3_fns) {
3012 return lp_ctx->s3_fns->get_servicebynum(snum);
3015 return lp_ctx->services[snum];
3018 struct loadparm_service *lpcfg_service(struct loadparm_context *lp_ctx,
3019 const char *service_name)
3021 int iService;
3022 char *serviceName;
3024 if (lp_ctx->s3_fns) {
3025 return lp_ctx->s3_fns->get_service(service_name);
3028 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--) {
3029 if (lp_ctx->services[iService] &&
3030 lp_ctx->services[iService]->szService) {
3032 * The substitution here is used to support %U is
3033 * service names
3035 serviceName = standard_sub_basic(
3036 lp_ctx->services[iService],
3037 lp_ctx->services[iService]->szService);
3038 if (strequal(serviceName, service_name)) {
3039 talloc_free(serviceName);
3040 return lp_ctx->services[iService];
3042 talloc_free(serviceName);
3046 DEBUG(7,("lpcfg_servicenumber: couldn't find %s\n", service_name));
3047 return NULL;
3050 const char *lpcfg_servicename(const struct loadparm_service *service)
3052 return lpcfg_string((const char *)service->szService);
3056 * A useful volume label function.
3058 const char *lpcfg_volume_label(struct loadparm_service *service, struct loadparm_service *sDefault)
3060 const char *ret;
3061 ret = lpcfg_string((const char *)((service != NULL && service->volume != NULL) ?
3062 service->volume : sDefault->volume));
3063 if (!*ret)
3064 return lpcfg_servicename(service);
3065 return ret;
3069 * Return the correct printer name.
3071 const char *lpcfg_printername(struct loadparm_service *service, struct loadparm_service *sDefault)
3073 const char *ret;
3074 ret = lpcfg_string((const char *)((service != NULL && service->_printername != NULL) ?
3075 service->_printername : sDefault->_printername));
3076 if (ret == NULL || (ret != NULL && *ret == '\0'))
3077 ret = lpcfg_servicename(service);
3079 return ret;
3084 * Return the max print jobs per queue.
3086 int lpcfg_maxprintjobs(struct loadparm_service *service, struct loadparm_service *sDefault)
3088 int maxjobs = (service != NULL) ? service->iMaxPrintJobs : sDefault->iMaxPrintJobs;
3089 if (maxjobs <= 0 || maxjobs >= PRINT_MAX_JOBID)
3090 maxjobs = PRINT_MAX_JOBID - 1;
3092 return maxjobs;
3095 struct smb_iconv_handle *lpcfg_iconv_handle(struct loadparm_context *lp_ctx)
3097 if (lp_ctx == NULL) {
3098 return get_iconv_handle();
3100 return lp_ctx->iconv_handle;
3103 _PUBLIC_ void reload_charcnv(struct loadparm_context *lp_ctx)
3105 struct smb_iconv_handle *old_ic = lp_ctx->iconv_handle;
3106 if (!lp_ctx->global) {
3107 return;
3110 if (old_ic == NULL) {
3111 old_ic = global_iconv_handle;
3113 lp_ctx->iconv_handle = smb_iconv_handle_reinit_lp(lp_ctx, lp_ctx, old_ic);
3114 global_iconv_handle = lp_ctx->iconv_handle;
3117 _PUBLIC_ char *lpcfg_tls_keyfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3119 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_keyfile(lp_ctx));
3122 _PUBLIC_ char *lpcfg_tls_certfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3124 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_certfile(lp_ctx));
3127 _PUBLIC_ char *lpcfg_tls_cafile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3129 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_cafile(lp_ctx));
3132 _PUBLIC_ char *lpcfg_tls_crlfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3134 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_crlfile(lp_ctx));
3137 _PUBLIC_ char *lpcfg_tls_dhpfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3139 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_dhpfile(lp_ctx));
3142 struct gensec_settings *lpcfg_gensec_settings(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3144 struct gensec_settings *settings = talloc_zero(mem_ctx, struct gensec_settings);
3145 if (settings == NULL)
3146 return NULL;
3147 SMB_ASSERT(lp_ctx != NULL);
3148 settings->lp_ctx = talloc_reference(settings, lp_ctx);
3149 settings->target_hostname = lpcfg_parm_string(lp_ctx, NULL, "gensec", "target_hostname");
3150 return settings;
3153 int lpcfg_server_role(struct loadparm_context *lp_ctx)
3155 int domain_master = lpcfg__domain_master(lp_ctx);
3157 return lp_find_server_role(lpcfg__server_role(lp_ctx),
3158 lpcfg__security(lp_ctx),
3159 lpcfg__domain_logons(lp_ctx),
3160 (domain_master == true) ||
3161 (domain_master == Auto));
3164 int lpcfg_security(struct loadparm_context *lp_ctx)
3166 return lp_find_security(lpcfg__server_role(lp_ctx),
3167 lpcfg__security(lp_ctx));
3170 int lpcfg_client_max_protocol(struct loadparm_context *lp_ctx)
3172 int client_max_protocol = lpcfg__client_max_protocol(lp_ctx);
3173 if (client_max_protocol == PROTOCOL_DEFAULT) {
3174 return PROTOCOL_NT1;
3176 return client_max_protocol;
3179 bool lpcfg_server_signing_allowed(struct loadparm_context *lp_ctx, bool *mandatory)
3181 bool allowed = true;
3182 enum smb_signing_setting signing_setting = lpcfg_server_signing(lp_ctx);
3184 *mandatory = false;
3186 if (signing_setting == SMB_SIGNING_DEFAULT) {
3188 * If we are a domain controller, SMB signing is
3189 * really important, as it can prevent a number of
3190 * attacks on communications between us and the
3191 * clients
3193 * However, it really sucks (no sendfile, CPU
3194 * overhead) performance-wise when used on a
3195 * file server, so disable it by default
3196 * on non-DCs
3199 if (lpcfg_server_role(lp_ctx) >= ROLE_ACTIVE_DIRECTORY_DC) {
3200 signing_setting = SMB_SIGNING_REQUIRED;
3201 } else {
3202 signing_setting = SMB_SIGNING_OFF;
3206 switch (signing_setting) {
3207 case SMB_SIGNING_REQUIRED:
3208 *mandatory = true;
3209 break;
3210 case SMB_SIGNING_IF_REQUIRED:
3211 break;
3212 case SMB_SIGNING_DEFAULT:
3213 case SMB_SIGNING_OFF:
3214 allowed = false;
3215 break;
3218 return allowed;
3221 int lpcfg_tdb_hash_size(struct loadparm_context *lp_ctx, const char *name)
3223 const char *base;
3225 if (name == NULL) {
3226 return 0;
3229 base = strrchr_m(name, '/');
3230 if (base != NULL) {
3231 base += 1;
3232 } else {
3233 base = name;
3235 return lpcfg_parm_int(lp_ctx, NULL, "tdb_hashsize", base, 0);
3239 int lpcfg_tdb_flags(struct loadparm_context *lp_ctx, int tdb_flags)
3241 if (!lpcfg_use_mmap(lp_ctx)) {
3242 tdb_flags |= TDB_NOMMAP;
3244 return tdb_flags;