backupkey: Improve IDL
[Samba.git] / lib / param / loadparm.c
blob115575c9c3a9b086d6d508c8007de88c86255bb4
1 /*
2 Unix SMB/CIFS implementation.
3 Parameter loading functions
4 Copyright (C) Karl Auer 1993-1998
6 Largely re-written by Andrew Tridgell, September 1994
8 Copyright (C) Simo Sorce 2001
9 Copyright (C) Alexander Bokovoy 2002
10 Copyright (C) Stefan (metze) Metzmacher 2002
11 Copyright (C) Jim McDonough (jmcd@us.ibm.com) 2003.
12 Copyright (C) James Myers 2003 <myersjj@samba.org>
13 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
14 Copyright (C) Andrew Bartlett 2011-2012
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 * Load parameters.
33 * This module provides suitable callback functions for the params
34 * module. It builds the internal table of service details which is
35 * then used by the rest of the server.
37 * To add a parameter:
39 * 1) add it to the global or service structure definition
40 * 2) add it to the parm_table
41 * 3) add it to the list of available functions (eg: using FN_GLOBAL_STRING())
42 * 4) If it's a global then initialise it in init_globals. If a local
43 * (ie. service) parameter then initialise it in the sDefault structure
46 * Notes:
47 * The configuration file is processed sequentially for speed. It is NOT
48 * accessed randomly as happens in 'real' Windows. For this reason, there
49 * is a fair bit of sequence-dependent code here - ie., code which assumes
50 * that certain things happen before others. In particular, the code which
51 * happens at the boundary between sections is delicately poised, so be
52 * careful!
56 #include "includes.h"
57 #include "version.h"
58 #include "dynconfig/dynconfig.h"
59 #include "system/time.h"
60 #include "system/locale.h"
61 #include "system/network.h" /* needed for TCP_NODELAY */
62 #include "../lib/util/dlinklist.h"
63 #include "lib/param/param.h"
64 #include "lib/param/loadparm.h"
65 #include "auth/gensec/gensec.h"
66 #include "lib/param/s3_param.h"
67 #include "lib/util/bitmap.h"
68 #include "libcli/smb/smb_constants.h"
69 #include "tdb.h"
71 #define standard_sub_basic talloc_strdup
73 #include "lib/param/param_global.h"
75 struct loadparm_service *lpcfg_default_service(struct loadparm_context *lp_ctx)
77 return lp_ctx->sDefault;
80 /**
81 * Convenience routine to grab string parameters into temporary memory
82 * and run standard_sub_basic on them.
84 * The buffers can be written to by
85 * callers without affecting the source string.
88 static const char *lpcfg_string(const char *s)
90 #if 0 /* until REWRITE done to make thread-safe */
91 size_t len = s ? strlen(s) : 0;
92 char *ret;
93 #endif
95 /* The follow debug is useful for tracking down memory problems
96 especially if you have an inner loop that is calling a lp_*()
97 function that returns a string. Perhaps this debug should be
98 present all the time? */
100 #if 0
101 DEBUG(10, ("lpcfg_string(%s)\n", s));
102 #endif
104 #if 0 /* until REWRITE done to make thread-safe */
105 if (!lp_talloc)
106 lp_talloc = talloc_init("lp_talloc");
108 ret = talloc_array(lp_talloc, char, len + 100); /* leave room for substitution */
110 if (!ret)
111 return NULL;
113 if (!s)
114 *ret = 0;
115 else
116 strlcpy(ret, s, len);
118 if (trim_string(ret, "\"", "\"")) {
119 if (strchr(ret,'"') != NULL)
120 strlcpy(ret, s, len);
123 standard_sub_basic(ret,len+100);
124 return (ret);
125 #endif
126 return s;
130 In this section all the functions that are used to access the
131 parameters from the rest of the program are defined
135 * the creation of separate lpcfg_*() and lp_*() functions is to allow
136 * for code compatibility between existing Samba4 and Samba3 code.
139 /* this global context supports the lp_*() function varients */
140 static struct loadparm_context *global_loadparm_context;
142 #define lpcfg_default_service global_loadparm_context->sDefault
143 #define lpcfg_global_service(i) global_loadparm_context->services[i]
145 #define FN_GLOBAL_STRING(fn_name,var_name) \
146 _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx, TALLOC_CTX *ctx) {\
147 if (lp_ctx == NULL) return NULL; \
148 if (lp_ctx->s3_fns) { \
149 return lp_ctx->globals->var_name ? lp_ctx->s3_fns->lp_string(ctx, lp_ctx->globals->var_name) : talloc_strdup(ctx, ""); \
151 return lp_ctx->globals->var_name ? talloc_strdup(ctx, lpcfg_string(lp_ctx->globals->var_name)) : talloc_strdup(ctx, ""); \
154 #define FN_GLOBAL_CONST_STRING(fn_name,var_name) \
155 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
156 if (lp_ctx == NULL) return NULL; \
157 return lp_ctx->globals->var_name ? lpcfg_string(lp_ctx->globals->var_name) : ""; \
160 #define FN_GLOBAL_LIST(fn_name,var_name) \
161 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
162 if (lp_ctx == NULL) return NULL; \
163 return lp_ctx->globals->var_name; \
166 #define FN_GLOBAL_BOOL(fn_name,var_name) \
167 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) {\
168 if (lp_ctx == NULL) return false; \
169 return lp_ctx->globals->var_name; \
172 #define FN_GLOBAL_INTEGER(fn_name,var_name) \
173 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
174 return lp_ctx->globals->var_name; \
177 /* Local parameters don't need the ->s3_fns because the struct
178 * loadparm_service is shared and lpcfg_service() checks the ->s3_fns
179 * hook */
180 #define FN_LOCAL_STRING(fn_name,val) \
181 _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_service *service, \
182 struct loadparm_service *sDefault, TALLOC_CTX *ctx) { \
183 return(talloc_strdup(ctx, lpcfg_string((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)))); \
186 #define FN_LOCAL_CONST_STRING(fn_name,val) \
187 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_service *service, \
188 struct loadparm_service *sDefault) { \
189 return((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)); \
192 #define FN_LOCAL_LIST(fn_name,val) \
193 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_service *service, \
194 struct loadparm_service *sDefault) {\
195 return(const char **)(service != NULL && service->val != NULL? service->val : sDefault->val); \
198 #define FN_LOCAL_PARM_BOOL(fn_name, val) FN_LOCAL_BOOL(fn_name, val)
200 #define FN_LOCAL_BOOL(fn_name,val) \
201 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_service *service, \
202 struct loadparm_service *sDefault) { \
203 return((service != NULL)? service->val : sDefault->val); \
206 #define FN_LOCAL_INTEGER(fn_name,val) \
207 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_service *service, \
208 struct loadparm_service *sDefault) { \
209 return((service != NULL)? service->val : sDefault->val); \
212 #define FN_LOCAL_PARM_INTEGER(fn_name, val) FN_LOCAL_INTEGER(fn_name, val)
214 #define FN_LOCAL_PARM_CHAR(fn_name,val) \
215 _PUBLIC_ char lpcfg_ ## fn_name(struct loadparm_service *service, \
216 struct loadparm_service *sDefault) { \
217 return((service != NULL)? service->val : sDefault->val); \
220 #include "lib/param/param_functions.c"
222 /* These functions cannot be auto-generated */
223 FN_LOCAL_BOOL(autoloaded, autoloaded)
224 FN_GLOBAL_CONST_STRING(dnsdomain, dnsdomain)
226 /* local prototypes */
227 static struct loadparm_service *lpcfg_getservicebyname(struct loadparm_context *lp_ctx,
228 const char *pszServiceName);
229 static bool do_section(const char *pszSectionName, void *);
230 static bool set_variable_helper(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
231 const char *pszParmName, const char *pszParmValue);
232 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
233 struct loadparm_service *service,
234 const char *pszParmName,
235 const char *pszParmValue, int flags);
237 /* The following are helper functions for parametrical options support. */
238 /* It returns a pointer to parametrical option value if it exists or NULL otherwise */
239 /* Actual parametrical functions are quite simple */
240 struct parmlist_entry *get_parametric_helper(struct loadparm_service *service,
241 const char *type, const char *option,
242 struct parmlist_entry *global_opts)
244 size_t type_len = strlen(type);
245 size_t option_len = strlen(option);
246 char param_key[type_len + option_len + 2];
247 struct parmlist_entry *data = NULL;
249 snprintf(param_key, sizeof(param_key), "%s:%s", type, option);
252 * Try to fetch the option from the data.
254 if (service != NULL) {
255 data = service->param_opt;
256 while (data != NULL) {
257 if (strwicmp(data->key, param_key) == 0) {
258 return data;
260 data = data->next;
265 * Fall back to fetching from the globals.
267 data = global_opts;
268 while (data != NULL) {
269 if (strwicmp(data->key, param_key) == 0) {
270 return data;
272 data = data->next;
275 return NULL;
278 const char *lpcfg_get_parametric(struct loadparm_context *lp_ctx,
279 struct loadparm_service *service,
280 const char *type, const char *option)
282 struct parmlist_entry *data;
284 if (lp_ctx == NULL)
285 return NULL;
287 data = get_parametric_helper(service,
288 type, option, lp_ctx->globals->param_opt);
290 if (data == NULL) {
291 return NULL;
292 } else {
293 return data->value;
299 * convenience routine to return int parameters.
301 int lp_int(const char *s)
304 if (!s || !*s) {
305 DEBUG(0,("lp_int(%s): is called with NULL!\n",s));
306 return -1;
309 return strtol(s, NULL, 0);
313 * convenience routine to return unsigned long parameters.
315 unsigned long lp_ulong(const char *s)
318 if (!s || !*s) {
319 DEBUG(0,("lp_ulong(%s): is called with NULL!\n",s));
320 return -1;
323 return strtoul(s, NULL, 0);
327 * convenience routine to return unsigned long parameters.
329 static long lp_long(const char *s)
332 if (!s) {
333 DEBUG(0,("lp_long(%s): is called with NULL!\n",s));
334 return -1;
337 return strtol(s, NULL, 0);
341 * convenience routine to return unsigned long parameters.
343 static double lp_double(const char *s)
346 if (!s) {
347 DEBUG(0,("lp_double(%s): is called with NULL!\n",s));
348 return -1;
351 return strtod(s, NULL);
355 * convenience routine to return boolean parameters.
357 bool lp_bool(const char *s)
359 bool ret = false;
361 if (!s || !*s) {
362 DEBUG(0,("lp_bool(%s): is called with NULL!\n",s));
363 return false;
366 if (!set_boolean(s, &ret)) {
367 DEBUG(0,("lp_bool(%s): value is not boolean!\n",s));
368 return false;
371 return ret;
375 * Return parametric option from a given service. Type is a part of option before ':'
376 * Parametric option has following syntax: 'Type: option = value'
377 * Returned value is allocated in 'lp_talloc' context
380 const char *lpcfg_parm_string(struct loadparm_context *lp_ctx,
381 struct loadparm_service *service, const char *type,
382 const char *option)
384 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
386 if (value)
387 return lpcfg_string(value);
389 return NULL;
393 * Return parametric option from a given service. Type is a part of option before ':'
394 * Parametric option has following syntax: 'Type: option = value'
395 * Returned value is allocated in 'lp_talloc' context
398 const char **lpcfg_parm_string_list(TALLOC_CTX *mem_ctx,
399 struct loadparm_context *lp_ctx,
400 struct loadparm_service *service,
401 const char *type,
402 const char *option, const char *separator)
404 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
406 if (value != NULL) {
407 char **l = str_list_make(mem_ctx, value, separator);
408 return discard_const_p(const char *, l);
411 return NULL;
415 * Return parametric option from a given service. Type is a part of option before ':'
416 * Parametric option has following syntax: 'Type: option = value'
419 int lpcfg_parm_int(struct loadparm_context *lp_ctx,
420 struct loadparm_service *service, const char *type,
421 const char *option, int default_v)
423 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
425 if (value)
426 return lp_int(value);
428 return default_v;
432 * Return parametric option from a given service. Type is a part of
433 * option before ':'.
434 * Parametric option has following syntax: 'Type: option = value'.
437 int lpcfg_parm_bytes(struct loadparm_context *lp_ctx,
438 struct loadparm_service *service, const char *type,
439 const char *option, int default_v)
441 uint64_t bval;
443 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
445 if (value && conv_str_size_error(value, &bval)) {
446 if (bval <= INT_MAX) {
447 return (int)bval;
451 return default_v;
455 * Return parametric option from a given service.
456 * Type is a part of option before ':'
457 * Parametric option has following syntax: 'Type: option = value'
459 unsigned long lpcfg_parm_ulong(struct loadparm_context *lp_ctx,
460 struct loadparm_service *service, const char *type,
461 const char *option, unsigned long default_v)
463 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
465 if (value)
466 return lp_ulong(value);
468 return default_v;
471 long lpcfg_parm_long(struct loadparm_context *lp_ctx,
472 struct loadparm_service *service, const char *type,
473 const char *option, long default_v)
475 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
477 if (value)
478 return lp_long(value);
480 return default_v;
483 double lpcfg_parm_double(struct loadparm_context *lp_ctx,
484 struct loadparm_service *service, const char *type,
485 const char *option, double default_v)
487 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
489 if (value != NULL)
490 return lp_double(value);
492 return default_v;
496 * Return parametric option from a given service. Type is a part of option before ':'
497 * Parametric option has following syntax: 'Type: option = value'
500 bool lpcfg_parm_bool(struct loadparm_context *lp_ctx,
501 struct loadparm_service *service, const char *type,
502 const char *option, bool default_v)
504 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
506 if (value != NULL)
507 return lp_bool(value);
509 return default_v;
514 * Set a string value, deallocating any existing space, and allocing the space
515 * for the string
517 bool lpcfg_string_set(TALLOC_CTX *mem_ctx, char **dest, const char *src)
519 talloc_free(*dest);
521 if (src == NULL)
522 src = "";
524 *dest = talloc_strdup(mem_ctx, src);
525 if ((*dest) == NULL) {
526 DEBUG(0,("Out of memory in string_set\n"));
527 return false;
530 return true;
534 * Set a string value, deallocating any existing space, and allocing the space
535 * for the string
537 bool lpcfg_string_set_upper(TALLOC_CTX *mem_ctx, char **dest, const char *src)
539 talloc_free(*dest);
541 if (src == NULL)
542 src = "";
544 *dest = strupper_talloc(mem_ctx, src);
545 if ((*dest) == NULL) {
546 DEBUG(0,("Out of memory in string_set_upper\n"));
547 return false;
550 return true;
556 * Add a new service to the services array initialising it with the given
557 * service.
560 struct loadparm_service *lpcfg_add_service(struct loadparm_context *lp_ctx,
561 const struct loadparm_service *pservice,
562 const char *name)
564 int i;
565 int num_to_alloc = lp_ctx->iNumServices + 1;
566 struct parmlist_entry *data, *pdata;
568 if (lp_ctx->s3_fns != NULL) {
569 smb_panic("Add a service should not be called on an s3 loadparm ctx");
572 if (pservice == NULL) {
573 pservice = lp_ctx->sDefault;
576 /* it might already exist */
577 if (name) {
578 struct loadparm_service *service = lpcfg_getservicebyname(lp_ctx,
579 name);
580 if (service != NULL) {
581 /* Clean all parametric options for service */
582 /* They will be added during parsing again */
583 data = service->param_opt;
584 while (data) {
585 pdata = data->next;
586 talloc_free(data);
587 data = pdata;
589 service->param_opt = NULL;
590 return service;
594 /* find an invalid one */
595 for (i = 0; i < lp_ctx->iNumServices; i++)
596 if (lp_ctx->services[i] == NULL)
597 break;
599 /* if not, then create one */
600 if (i == lp_ctx->iNumServices) {
601 struct loadparm_service **tsp;
603 tsp = talloc_realloc(lp_ctx, lp_ctx->services, struct loadparm_service *, num_to_alloc);
605 if (!tsp) {
606 DEBUG(0,("lpcfg_add_service: failed to enlarge services!\n"));
607 return NULL;
608 } else {
609 lp_ctx->services = tsp;
610 lp_ctx->services[lp_ctx->iNumServices] = NULL;
613 lp_ctx->iNumServices++;
616 lp_ctx->services[i] = talloc_zero(lp_ctx->services, struct loadparm_service);
617 if (lp_ctx->services[i] == NULL) {
618 DEBUG(0,("lpcfg_add_service: out of memory!\n"));
619 return NULL;
621 copy_service(lp_ctx->services[i], pservice, NULL);
622 if (name != NULL)
623 lpcfg_string_set(lp_ctx->services[i], &lp_ctx->services[i]->szService, name);
624 return lp_ctx->services[i];
628 * Add a new home service, with the specified home directory, defaults coming
629 * from service ifrom.
632 bool lpcfg_add_home(struct loadparm_context *lp_ctx,
633 const char *pszHomename,
634 struct loadparm_service *default_service,
635 const char *user, const char *pszHomedir)
637 struct loadparm_service *service;
639 service = lpcfg_add_service(lp_ctx, default_service, pszHomename);
641 if (service == NULL)
642 return false;
644 if (!(*(default_service->path))
645 || strequal(default_service->path, lp_ctx->sDefault->path)) {
646 service->path = talloc_strdup(service, pszHomedir);
647 } else {
648 service->path = string_sub_talloc(service, lpcfg_path(default_service, lp_ctx->sDefault, service), "%H", pszHomedir);
651 if (!(*(service->comment))) {
652 service->comment = talloc_asprintf(service, "Home directory of %s", user);
654 service->bAvailable = default_service->bAvailable;
655 service->browseable = default_service->browseable;
657 DEBUG(3, ("adding home's share [%s] for user '%s' at '%s'\n",
658 pszHomename, user, service->path));
660 return true;
664 * Add a new printer service, with defaults coming from service iFrom.
667 bool lpcfg_add_printer(struct loadparm_context *lp_ctx,
668 const char *pszPrintername,
669 struct loadparm_service *default_service)
671 const char *comment = "From Printcap";
672 struct loadparm_service *service;
673 service = lpcfg_add_service(lp_ctx, default_service, pszPrintername);
675 if (service == NULL)
676 return false;
678 /* note that we do NOT default the availability flag to True - */
679 /* we take it from the default service passed. This allows all */
680 /* dynamic printers to be disabled by disabling the [printers] */
681 /* entry (if/when the 'available' keyword is implemented!). */
683 /* the printer name is set to the service name. */
684 lpcfg_string_set(service, &service->_printername, pszPrintername);
685 lpcfg_string_set(service, &service->comment, comment);
686 service->browseable = default_service->browseable;
687 /* Printers cannot be read_only. */
688 service->read_only = false;
689 /* Printer services must be printable. */
690 service->printable = true;
692 DEBUG(3, ("adding printer service %s\n", pszPrintername));
694 return true;
698 * Map a parameter's string representation to something we can use.
699 * Returns False if the parameter string is not recognised, else TRUE.
702 int lpcfg_map_parameter(const char *pszParmName)
704 int iIndex;
706 for (iIndex = 0; parm_table[iIndex].label; iIndex++)
707 if (strwicmp(parm_table[iIndex].label, pszParmName) == 0)
708 return iIndex;
710 /* Warn only if it isn't parametric option */
711 if (strchr(pszParmName, ':') == NULL)
712 DEBUG(0, ("Unknown parameter encountered: \"%s\"\n", pszParmName));
713 /* We do return 'fail' for parametric options as well because they are
714 stored in different storage
716 return -1;
721 return the parameter structure for a parameter
723 struct parm_struct *lpcfg_parm_struct(struct loadparm_context *lp_ctx, const char *name)
725 int num = lpcfg_map_parameter(name);
727 if (num < 0) {
728 return NULL;
731 return &parm_table[num];
735 return the parameter pointer for a parameter
737 void *lpcfg_parm_ptr(struct loadparm_context *lp_ctx,
738 struct loadparm_service *service, struct parm_struct *parm)
740 if (lp_ctx->s3_fns) {
741 return lp_ctx->s3_fns->get_parm_ptr(service, parm);
744 if (service == NULL) {
745 if (parm->p_class == P_LOCAL)
746 return ((char *)lp_ctx->sDefault)+parm->offset;
747 else if (parm->p_class == P_GLOBAL)
748 return ((char *)lp_ctx->globals)+parm->offset;
749 else return NULL;
750 } else {
751 return ((char *)service) + parm->offset;
756 return the parameter pointer for a parameter
758 bool lpcfg_parm_is_cmdline(struct loadparm_context *lp_ctx, const char *name)
760 int parmnum;
762 parmnum = lpcfg_map_parameter(name);
763 if (parmnum == -1) return false;
765 return lp_ctx->flags[parmnum] & FLAG_CMDLINE;
769 * Find a service by name. Otherwise works like get_service.
772 static struct loadparm_service *lpcfg_getservicebyname(struct loadparm_context *lp_ctx,
773 const char *pszServiceName)
775 int iService;
777 if (lp_ctx->s3_fns) {
778 return lp_ctx->s3_fns->get_service(pszServiceName);
781 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--)
782 if (lp_ctx->services[iService] != NULL &&
783 strwicmp(lp_ctx->services[iService]->szService, pszServiceName) == 0) {
784 return lp_ctx->services[iService];
787 return NULL;
791 * Add a parametric option to a parmlist_entry,
792 * replacing old value, if already present.
794 void set_param_opt(TALLOC_CTX *mem_ctx,
795 struct parmlist_entry **opt_list,
796 const char *opt_name,
797 const char *opt_value,
798 unsigned priority)
800 struct parmlist_entry *new_opt, *opt;
801 bool not_added;
803 opt = *opt_list;
804 not_added = true;
806 /* Traverse destination */
807 while (opt) {
808 /* If we already have same option, override it */
809 if (strwicmp(opt->key, opt_name) == 0) {
810 if ((opt->priority & FLAG_CMDLINE) &&
811 !(priority & FLAG_CMDLINE)) {
812 /* it's been marked as not to be
813 overridden */
814 return;
816 TALLOC_FREE(opt->value);
817 TALLOC_FREE(opt->list);
818 opt->value = talloc_strdup(opt, opt_value);
819 opt->priority = priority;
820 not_added = false;
821 break;
823 opt = opt->next;
825 if (not_added) {
826 new_opt = talloc(mem_ctx, struct parmlist_entry);
827 if (new_opt == NULL) {
828 smb_panic("OOM");
831 new_opt->key = talloc_strdup(new_opt, opt_name);
832 if (new_opt->key == NULL) {
833 smb_panic("talloc_strdup failed");
836 new_opt->value = talloc_strdup(new_opt, opt_value);
837 if (new_opt->value == NULL) {
838 smb_panic("talloc_strdup failed");
841 new_opt->list = NULL;
842 new_opt->priority = priority;
843 DLIST_ADD(*opt_list, new_opt);
848 * Copy a service structure to another.
849 * If pcopymapDest is NULL then copy all fields
852 void copy_service(struct loadparm_service *pserviceDest,
853 const struct loadparm_service *pserviceSource,
854 struct bitmap *pcopymapDest)
856 int i;
857 bool bcopyall = (pcopymapDest == NULL);
858 struct parmlist_entry *data;
860 for (i = 0; parm_table[i].label; i++)
861 if (parm_table[i].p_class == P_LOCAL &&
862 (bcopyall || bitmap_query(pcopymapDest, i))) {
863 const void *src_ptr =
864 ((const char *)pserviceSource) + parm_table[i].offset;
865 void *dest_ptr =
866 ((char *)pserviceDest) + parm_table[i].offset;
868 switch (parm_table[i].type) {
869 case P_BOOL:
870 case P_BOOLREV:
871 *(bool *)dest_ptr = *(const bool *)src_ptr;
872 break;
874 case P_INTEGER:
875 case P_BYTES:
876 case P_OCTAL:
877 case P_ENUM:
878 *(int *)dest_ptr = *(const int *)src_ptr;
879 break;
881 case P_CHAR:
882 *(char *)dest_ptr = *(const char *)src_ptr;
883 break;
885 case P_STRING:
886 lpcfg_string_set(pserviceDest,
887 (char **)dest_ptr,
888 *(const char * const *)src_ptr);
889 break;
891 case P_USTRING:
892 lpcfg_string_set_upper(pserviceDest,
893 (char **)dest_ptr,
894 *(const char * const *)src_ptr);
895 break;
896 case P_CMDLIST:
897 case P_LIST:
898 TALLOC_FREE(*((char ***)dest_ptr));
899 *(char ***)dest_ptr = str_list_copy(pserviceDest,
900 *discard_const_p(const char **, src_ptr));
901 break;
902 default:
903 break;
907 if (bcopyall) {
908 init_copymap(pserviceDest);
909 if (pserviceSource->copymap)
910 bitmap_copy(pserviceDest->copymap,
911 pserviceSource->copymap);
914 for (data = pserviceSource->param_opt; data != NULL; data = data->next) {
915 set_param_opt(pserviceDest, &pserviceDest->param_opt,
916 data->key, data->value, data->priority);
921 * Check a service for consistency. Return False if the service is in any way
922 * incomplete or faulty, else True.
924 bool lpcfg_service_ok(struct loadparm_service *service)
926 bool bRetval;
928 bRetval = true;
929 if (service->szService[0] == '\0') {
930 DEBUG(0, ("The following message indicates an internal error:\n"));
931 DEBUG(0, ("No service name in service entry.\n"));
932 bRetval = false;
935 /* The [printers] entry MUST be printable. I'm all for flexibility, but */
936 /* I can't see why you'd want a non-printable printer service... */
937 if (strwicmp(service->szService, PRINTERS_NAME) == 0) {
938 if (!service->printable) {
939 DEBUG(0, ("WARNING: [%s] service MUST be printable!\n",
940 service->szService));
941 service->printable = true;
943 /* [printers] service must also be non-browsable. */
944 if (service->browseable)
945 service->browseable = false;
948 if (service->path[0] == '\0' &&
949 strwicmp(service->szService, HOMES_NAME) != 0 &&
950 service->msdfs_proxy[0] == '\0')
952 DEBUG(0, ("WARNING: No path in service %s - making it unavailable!\n",
953 service->szService));
954 service->bAvailable = false;
957 if (!service->bAvailable)
958 DEBUG(1, ("NOTE: Service %s is flagged unavailable.\n",
959 service->szService));
961 return bRetval;
965 /*******************************************************************
966 Keep a linked list of all config files so we know when one has changed
967 it's date and needs to be reloaded.
968 ********************************************************************/
970 void add_to_file_list(TALLOC_CTX *mem_ctx, struct file_lists **list,
971 const char *fname, const char *subfname)
973 struct file_lists *f = *list;
975 while (f) {
976 if (f->name && !strcmp(f->name, fname))
977 break;
978 f = f->next;
981 if (!f) {
982 f = talloc(mem_ctx, struct file_lists);
983 if (!f)
984 goto fail;
985 f->next = *list;
986 f->name = talloc_strdup(f, fname);
987 if (!f->name) {
988 TALLOC_FREE(f);
989 goto fail;
991 f->subfname = talloc_strdup(f, subfname);
992 if (!f->subfname) {
993 TALLOC_FREE(f);
994 goto fail;
996 *list = f;
997 f->modtime = file_modtime(subfname);
998 } else {
999 time_t t = file_modtime(subfname);
1000 if (t)
1001 f->modtime = t;
1003 return;
1005 fail:
1006 DEBUG(0, ("Unable to add file to file list: %s\n", fname));
1010 /*******************************************************************
1011 Check if a config file has changed date.
1012 ********************************************************************/
1013 bool lpcfg_file_list_changed(struct loadparm_context *lp_ctx)
1015 struct file_lists *f;
1016 DEBUG(6, ("lpcfg_file_list_changed()\n"));
1018 for (f = lp_ctx->file_lists; f != NULL; f = f->next) {
1019 char *n2;
1020 time_t mod_time;
1022 n2 = standard_sub_basic(lp_ctx, f->name);
1024 DEBUGADD(6, ("file %s -> %s last mod_time: %s\n",
1025 f->name, n2, ctime(&f->modtime)));
1027 mod_time = file_modtime(n2);
1029 if (mod_time && ((f->modtime != mod_time) || (f->subfname == NULL) || (strcmp(n2, f->subfname) != 0))) {
1030 DEBUGADD(6, ("file %s modified: %s\n", n2,
1031 ctime(&mod_time)));
1032 f->modtime = mod_time;
1033 talloc_free(f->subfname);
1034 f->subfname = talloc_strdup(f, n2);
1035 TALLOC_FREE(n2);
1036 return true;
1038 TALLOC_FREE(n2);
1040 return false;
1044 * set the value for a P_ENUM
1046 bool lp_set_enum_parm( struct parm_struct *parm, const char *pszParmValue,
1047 int *ptr )
1049 int i;
1051 for (i = 0; parm->enum_list[i].name; i++) {
1052 if (strwicmp(pszParmValue, parm->enum_list[i].name) == 0) {
1053 *ptr = parm->enum_list[i].value;
1054 return true;
1057 DEBUG(0, ("WARNING: Ignoring invalid value '%s' for parameter '%s'\n",
1058 pszParmValue, parm->label));
1059 return false;
1063 /***************************************************************************
1064 Handle the "realm" parameter
1065 ***************************************************************************/
1067 bool handle_realm(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1068 const char *pszParmValue, char **ptr)
1070 char *upper;
1071 char *lower;
1073 upper = strupper_talloc(lp_ctx, pszParmValue);
1074 if (upper == NULL) {
1075 return false;
1078 lower = strlower_talloc(lp_ctx, pszParmValue);
1079 if (lower == NULL) {
1080 TALLOC_FREE(upper);
1081 return false;
1084 lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1085 lpcfg_string_set(lp_ctx->globals->ctx, &lp_ctx->globals->realm, upper);
1086 lpcfg_string_set(lp_ctx->globals->ctx, &lp_ctx->globals->dnsdomain, lower);
1088 return true;
1091 /***************************************************************************
1092 Handle the include operation.
1093 ***************************************************************************/
1095 bool handle_include(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1096 const char *pszParmValue, char **ptr)
1098 char *fname;
1100 if (lp_ctx->s3_fns) {
1101 return lp_ctx->s3_fns->lp_include(lp_ctx, service, pszParmValue, ptr);
1104 fname = standard_sub_basic(lp_ctx, pszParmValue);
1106 add_to_file_list(lp_ctx, &lp_ctx->file_lists, pszParmValue, fname);
1108 lpcfg_string_set(lp_ctx, ptr, fname);
1110 if (file_exist(fname))
1111 return pm_process(fname, do_section, lpcfg_do_parameter, lp_ctx);
1113 DEBUG(2, ("Can't find include file %s\n", fname));
1115 return false;
1118 /***************************************************************************
1119 Handle the interpretation of the copy parameter.
1120 ***************************************************************************/
1122 bool handle_copy(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1123 const char *pszParmValue, char **ptr)
1125 bool bRetval;
1126 struct loadparm_service *serviceTemp = NULL;
1128 bRetval = false;
1130 DEBUG(3, ("Copying service from service %s\n", pszParmValue));
1132 serviceTemp = lpcfg_getservicebyname(lp_ctx, pszParmValue);
1134 if (service == NULL) {
1135 DEBUG(0, ("Unable to copy service - invalid service destination.\n"));
1136 return false;
1139 if (serviceTemp != NULL) {
1140 if (serviceTemp == service) {
1141 DEBUG(0, ("Can't copy service %s - unable to copy self!\n", pszParmValue));
1142 } else {
1143 copy_service(service,
1144 serviceTemp,
1145 service->copymap);
1146 lpcfg_string_set(service, ptr, pszParmValue);
1148 bRetval = true;
1150 } else {
1151 DEBUG(0, ("Unable to copy service - source not found: %s\n",
1152 pszParmValue));
1153 bRetval = false;
1156 return bRetval;
1159 bool handle_debug_list(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1160 const char *pszParmValue, char **ptr)
1162 lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1164 return debug_parse_levels(pszParmValue);
1167 bool handle_logfile(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1168 const char *pszParmValue, char **ptr)
1170 if (lp_ctx->s3_fns == NULL) {
1171 debug_set_logfile(pszParmValue);
1174 lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1176 return true;
1180 * These special charset handling methods only run in the source3 code.
1183 bool handle_charset(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1184 const char *pszParmValue, char **ptr)
1186 if (lp_ctx->s3_fns) {
1187 if (*ptr == NULL || strcmp(*ptr, pszParmValue) != 0) {
1188 global_iconv_handle = smb_iconv_handle_reinit(NULL,
1189 lpcfg_dos_charset(lp_ctx),
1190 lpcfg_unix_charset(lp_ctx),
1191 true, global_iconv_handle);
1195 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1199 bool handle_dos_charset(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1200 const char *pszParmValue, char **ptr)
1202 bool is_utf8 = false;
1203 size_t len = strlen(pszParmValue);
1205 if (lp_ctx->s3_fns) {
1206 if (len == 4 || len == 5) {
1207 /* Don't use StrCaseCmp here as we don't want to
1208 initialize iconv. */
1209 if ((toupper_m(pszParmValue[0]) == 'U') &&
1210 (toupper_m(pszParmValue[1]) == 'T') &&
1211 (toupper_m(pszParmValue[2]) == 'F')) {
1212 if (len == 4) {
1213 if (pszParmValue[3] == '8') {
1214 is_utf8 = true;
1216 } else {
1217 if (pszParmValue[3] == '-' &&
1218 pszParmValue[4] == '8') {
1219 is_utf8 = true;
1225 if (*ptr == NULL || strcmp(*ptr, pszParmValue) != 0) {
1226 if (is_utf8) {
1227 DEBUG(0,("ERROR: invalid DOS charset: 'dos charset' must not "
1228 "be UTF8, using (default value) %s instead.\n",
1229 DEFAULT_DOS_CHARSET));
1230 pszParmValue = DEFAULT_DOS_CHARSET;
1232 global_iconv_handle = smb_iconv_handle_reinit(NULL,
1233 lpcfg_dos_charset(lp_ctx),
1234 lpcfg_unix_charset(lp_ctx),
1235 true, global_iconv_handle);
1239 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1242 bool handle_printing(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1243 const char *pszParmValue, char **ptr)
1245 static int parm_num = -1;
1247 if (parm_num == -1) {
1248 parm_num = lpcfg_map_parameter("printing");
1251 if (!lp_set_enum_parm(&parm_table[parm_num], pszParmValue, (int*)ptr)) {
1252 return false;
1255 if (lp_ctx->s3_fns) {
1256 if (service == NULL) {
1257 init_printer_values(lp_ctx, lp_ctx->globals->ctx, lp_ctx->sDefault);
1258 } else {
1259 init_printer_values(lp_ctx, service, service);
1263 return true;
1266 bool handle_ldap_debug_level(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1267 const char *pszParmValue, char **ptr)
1269 lp_ctx->globals->ldap_debug_level = lp_int(pszParmValue);
1271 if (lp_ctx->s3_fns) {
1272 lp_ctx->s3_fns->init_ldap_debugging();
1274 return true;
1277 bool handle_netbios_aliases(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1278 const char *pszParmValue, char **ptr)
1280 TALLOC_FREE(lp_ctx->globals->netbios_aliases);
1281 lp_ctx->globals->netbios_aliases = str_list_make_v3_const(lp_ctx->globals->ctx,
1282 pszParmValue, NULL);
1284 if (lp_ctx->s3_fns) {
1285 return lp_ctx->s3_fns->set_netbios_aliases(lp_ctx->globals->netbios_aliases);
1287 return true;
1291 * idmap related parameters
1294 bool handle_idmap_backend(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1295 const char *pszParmValue, char **ptr)
1297 if (lp_ctx->s3_fns) {
1298 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : backend",
1299 pszParmValue, 0);
1302 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1305 bool handle_idmap_uid(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1306 const char *pszParmValue, char **ptr)
1308 if (lp_ctx->s3_fns) {
1309 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : range",
1310 pszParmValue, 0);
1313 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1316 bool handle_idmap_gid(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1317 const char *pszParmValue, char **ptr)
1319 if (lp_ctx->s3_fns) {
1320 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : range",
1321 pszParmValue, 0);
1324 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1327 bool handle_smb_ports(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1328 const char *pszParmValue, char **ptr)
1330 static int parm_num = -1;
1331 int i;
1332 const char **list;
1334 if (!pszParmValue || !*pszParmValue) {
1335 return false;
1338 if (parm_num == -1) {
1339 parm_num = lpcfg_map_parameter("smb ports");
1342 if(!set_variable_helper(lp_ctx->globals->ctx, parm_num, ptr, "smb ports",
1343 pszParmValue)) {
1344 return false;
1347 list = lp_ctx->globals->smb_ports;
1348 if (list == NULL) {
1349 return false;
1352 /* Check that each port is a valid integer and within range */
1353 for (i = 0; list[i] != NULL; i++) {
1354 char *end = NULL;
1355 int port = 0;
1356 port = strtol(list[i], &end, 10);
1357 if (*end != '\0' || port <= 0 || port > 65535) {
1358 TALLOC_FREE(list);
1359 return false;
1363 return true;
1366 /***************************************************************************
1367 Initialise a copymap.
1368 ***************************************************************************/
1371 * Initializes service copymap
1372 * Note: pservice *must* be valid TALLOC_CTX
1374 void init_copymap(struct loadparm_service *pservice)
1376 int i;
1378 TALLOC_FREE(pservice->copymap);
1380 pservice->copymap = bitmap_talloc(pservice, num_parameters());
1381 if (!pservice->copymap) {
1382 DEBUG(0,
1383 ("Couldn't allocate copymap!! (size %d)\n",
1384 (int)num_parameters()));
1385 } else {
1386 for (i = 0; i < num_parameters(); i++) {
1387 bitmap_set(pservice->copymap, i);
1393 * Process a parametric option
1395 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
1396 struct loadparm_service *service,
1397 const char *pszParmName,
1398 const char *pszParmValue, int flags)
1400 struct parmlist_entry **data;
1401 char *name;
1402 TALLOC_CTX *mem_ctx;
1404 while (isspace((unsigned char)*pszParmName)) {
1405 pszParmName++;
1408 name = strlower_talloc(lp_ctx, pszParmName);
1409 if (!name) return false;
1411 if (service == NULL) {
1412 data = &lp_ctx->globals->param_opt;
1414 * s3 code cannot deal with parametric options stored on the globals ctx.
1416 if (lp_ctx->s3_fns != NULL) {
1417 mem_ctx = NULL;
1418 } else {
1419 mem_ctx = lp_ctx->globals->ctx;
1421 } else {
1422 data = &service->param_opt;
1423 mem_ctx = service;
1426 set_param_opt(mem_ctx, data, name, pszParmValue, flags);
1428 talloc_free(name);
1430 return true;
1433 static bool set_variable_helper(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
1434 const char *pszParmName, const char *pszParmValue)
1436 int i;
1438 /* switch on the type of variable it is */
1439 switch (parm_table[parmnum].type)
1441 case P_BOOL: {
1442 bool b;
1443 if (!set_boolean(pszParmValue, &b)) {
1444 DEBUG(0, ("set_variable_helper(%s): value is not "
1445 "boolean!\n", pszParmValue));
1446 return false;
1448 *(bool *)parm_ptr = b;
1450 break;
1452 case P_BOOLREV: {
1453 bool b;
1454 if (!set_boolean(pszParmValue, &b)) {
1455 DEBUG(0, ("set_variable_helper(%s): value is not "
1456 "boolean!\n", pszParmValue));
1457 return false;
1459 *(bool *)parm_ptr = !b;
1461 break;
1463 case P_INTEGER:
1464 *(int *)parm_ptr = lp_int(pszParmValue);
1465 break;
1467 case P_CHAR:
1468 *(char *)parm_ptr = *pszParmValue;
1469 break;
1471 case P_OCTAL:
1472 i = sscanf(pszParmValue, "%o", (int *)parm_ptr);
1473 if ( i != 1 ) {
1474 DEBUG ( 0, ("Invalid octal number %s\n", pszParmName ));
1475 return false;
1477 break;
1479 case P_BYTES:
1481 uint64_t val;
1482 if (conv_str_size_error(pszParmValue, &val)) {
1483 if (val <= INT_MAX) {
1484 *(int *)parm_ptr = (int)val;
1485 break;
1489 DEBUG(0, ("set_variable_helper(%s): value is not "
1490 "a valid size specifier!\n", pszParmValue));
1491 return false;
1494 case P_CMDLIST:
1495 TALLOC_FREE(*(char ***)parm_ptr);
1496 *(char ***)parm_ptr = str_list_make_v3(mem_ctx,
1497 pszParmValue, NULL);
1498 break;
1500 case P_LIST:
1502 char **new_list = str_list_make_v3(mem_ctx,
1503 pszParmValue, NULL);
1504 if (new_list == NULL) {
1505 break;
1508 for (i=0; new_list[i]; i++) {
1509 if (*(const char ***)parm_ptr != NULL &&
1510 new_list[i][0] == '+' &&
1511 new_list[i][1])
1513 if (!str_list_check(*(const char ***)parm_ptr,
1514 &new_list[i][1])) {
1515 *(const char ***)parm_ptr = str_list_add(*(const char ***)parm_ptr,
1516 &new_list[i][1]);
1518 } else if (*(const char ***)parm_ptr != NULL &&
1519 new_list[i][0] == '-' &&
1520 new_list[i][1])
1522 str_list_remove(*(const char ***)parm_ptr,
1523 &new_list[i][1]);
1524 } else {
1525 if (i != 0) {
1526 DEBUG(0, ("Unsupported list syntax for: %s = %s\n",
1527 pszParmName, pszParmValue));
1528 return false;
1530 *(char ***)parm_ptr = new_list;
1531 break;
1534 break;
1537 case P_STRING:
1538 lpcfg_string_set(mem_ctx, (char **)parm_ptr, pszParmValue);
1539 break;
1541 case P_USTRING:
1542 lpcfg_string_set_upper(mem_ctx, (char **)parm_ptr, pszParmValue);
1543 break;
1545 case P_ENUM:
1546 if (!lp_set_enum_parm(&parm_table[parmnum], pszParmValue, (int*)parm_ptr)) {
1547 return false;
1549 break;
1551 case P_SEP:
1552 break;
1555 return true;
1559 bool set_variable(TALLOC_CTX *mem_ctx, struct loadparm_service *service, int parmnum, void *parm_ptr,
1560 const char *pszParmName, const char *pszParmValue,
1561 struct loadparm_context *lp_ctx, bool on_globals)
1563 int i;
1564 bool ok;
1566 /* if it is a special case then go ahead */
1567 if (parm_table[parmnum].special) {
1568 ok = parm_table[parmnum].special(lp_ctx, service, pszParmValue,
1569 (char **)parm_ptr);
1570 } else {
1571 ok = set_variable_helper(mem_ctx, parmnum, parm_ptr,
1572 pszParmName, pszParmValue);
1575 if (!ok) {
1576 return false;
1579 if (on_globals && (lp_ctx->flags[parmnum] & FLAG_DEFAULT)) {
1580 lp_ctx->flags[parmnum] &= ~FLAG_DEFAULT;
1581 /* we have to also unset FLAG_DEFAULT on aliases */
1582 for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
1583 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1585 for (i=parmnum+1;i<num_parameters() && parm_table[i].offset == parm_table[parmnum].offset;i++) {
1586 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1589 return true;
1593 bool lpcfg_do_global_parameter(struct loadparm_context *lp_ctx,
1594 const char *pszParmName, const char *pszParmValue)
1596 int parmnum = lpcfg_map_parameter(pszParmName);
1597 void *parm_ptr;
1599 if (parmnum < 0) {
1600 if (strchr(pszParmName, ':')) {
1601 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName, pszParmValue, 0);
1603 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1604 return true;
1607 /* if the flag has been set on the command line, then don't allow override,
1608 but don't report an error */
1609 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1610 return true;
1613 if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
1614 DEBUG(1, ("WARNING: The \"%s\" option is deprecated\n",
1615 pszParmName));
1618 parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[parmnum]);
1620 return set_variable(lp_ctx->globals->ctx, NULL, parmnum, parm_ptr,
1621 pszParmName, pszParmValue, lp_ctx, true);
1624 bool lpcfg_do_service_parameter(struct loadparm_context *lp_ctx,
1625 struct loadparm_service *service,
1626 const char *pszParmName, const char *pszParmValue)
1628 void *parm_ptr;
1629 int i;
1630 int parmnum = lpcfg_map_parameter(pszParmName);
1632 if (parmnum < 0) {
1633 if (strchr(pszParmName, ':')) {
1634 return lp_do_parameter_parametric(lp_ctx, service, pszParmName, pszParmValue, 0);
1636 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1637 return true;
1640 /* if the flag has been set on the command line, then don't allow override,
1641 but don't report an error */
1642 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1643 return true;
1646 if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
1647 DEBUG(1, ("WARNING: The \"%s\" option is deprecated\n",
1648 pszParmName));
1651 if (parm_table[parmnum].p_class == P_GLOBAL) {
1652 DEBUG(0,
1653 ("Global parameter %s found in service section!\n",
1654 pszParmName));
1655 return true;
1657 parm_ptr = ((char *)service) + parm_table[parmnum].offset;
1659 if (!service->copymap)
1660 init_copymap(service);
1662 /* this handles the aliases - set the copymap for other
1663 * entries with the same data pointer */
1664 for (i = 0; parm_table[i].label; i++)
1665 if (parm_table[i].offset == parm_table[parmnum].offset &&
1666 parm_table[i].p_class == parm_table[parmnum].p_class)
1667 bitmap_clear(service->copymap, i);
1669 return set_variable(service, service, parmnum, parm_ptr, pszParmName,
1670 pszParmValue, lp_ctx, false);
1674 * Process a parameter.
1677 bool lpcfg_do_parameter(const char *pszParmName, const char *pszParmValue,
1678 void *userdata)
1680 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1682 if (lp_ctx->bInGlobalSection)
1683 return lpcfg_do_global_parameter(lp_ctx, pszParmName,
1684 pszParmValue);
1685 else
1686 return lpcfg_do_service_parameter(lp_ctx, lp_ctx->currentService,
1687 pszParmName, pszParmValue);
1691 variable argument do parameter
1693 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx, const char *pszParmName, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
1694 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx,
1695 const char *pszParmName, const char *fmt, ...)
1697 char *s;
1698 bool ret;
1699 va_list ap;
1701 va_start(ap, fmt);
1702 s = talloc_vasprintf(NULL, fmt, ap);
1703 va_end(ap);
1704 ret = lpcfg_do_global_parameter(lp_ctx, pszParmName, s);
1705 talloc_free(s);
1706 return ret;
1711 set a parameter from the commandline - this is called from command line parameter
1712 parsing code. It sets the parameter then marks the parameter as unable to be modified
1713 by smb.conf processing
1715 bool lpcfg_set_cmdline(struct loadparm_context *lp_ctx, const char *pszParmName,
1716 const char *pszParmValue)
1718 int parmnum;
1719 int i;
1721 while (isspace((unsigned char)*pszParmValue)) pszParmValue++;
1723 parmnum = lpcfg_map_parameter(pszParmName);
1725 if (parmnum < 0 && strchr(pszParmName, ':')) {
1726 /* set a parametric option */
1727 bool ok;
1728 ok = lp_do_parameter_parametric(lp_ctx, NULL, pszParmName,
1729 pszParmValue, FLAG_CMDLINE);
1730 if (lp_ctx->s3_fns != NULL) {
1731 if (ok) {
1732 lp_ctx->s3_fns->store_cmdline(pszParmName, pszParmValue);
1735 return ok;
1738 if (parmnum < 0) {
1739 DEBUG(0,("Unknown option '%s'\n", pszParmName));
1740 return false;
1743 /* reset the CMDLINE flag in case this has been called before */
1744 lp_ctx->flags[parmnum] &= ~FLAG_CMDLINE;
1746 if (!lpcfg_do_global_parameter(lp_ctx, pszParmName, pszParmValue)) {
1747 return false;
1750 lp_ctx->flags[parmnum] |= FLAG_CMDLINE;
1752 /* we have to also set FLAG_CMDLINE on aliases */
1753 for (i=parmnum-1;
1754 i>=0 && parm_table[i].p_class == parm_table[parmnum].p_class &&
1755 parm_table[i].offset == parm_table[parmnum].offset;
1756 i--) {
1757 lp_ctx->flags[i] |= FLAG_CMDLINE;
1759 for (i=parmnum+1;
1760 i<num_parameters() &&
1761 parm_table[i].p_class == parm_table[parmnum].p_class &&
1762 parm_table[i].offset == parm_table[parmnum].offset;
1763 i++) {
1764 lp_ctx->flags[i] |= FLAG_CMDLINE;
1767 if (lp_ctx->s3_fns != NULL) {
1768 lp_ctx->s3_fns->store_cmdline(pszParmName, pszParmValue);
1771 return true;
1775 set a option from the commandline in 'a=b' format. Use to support --option
1777 bool lpcfg_set_option(struct loadparm_context *lp_ctx, const char *option)
1779 char *p, *s;
1780 bool ret;
1782 s = talloc_strdup(NULL, option);
1783 if (!s) {
1784 return false;
1787 p = strchr(s, '=');
1788 if (!p) {
1789 talloc_free(s);
1790 return false;
1793 *p = 0;
1795 ret = lpcfg_set_cmdline(lp_ctx, s, p+1);
1796 talloc_free(s);
1797 return ret;
1801 #define BOOLSTR(b) ((b) ? "Yes" : "No")
1804 * Print a parameter of the specified type.
1807 void lpcfg_print_parameter(struct parm_struct *p, void *ptr, FILE * f)
1809 /* For the seperation of lists values that we print below */
1810 const char *list_sep = ", ";
1811 int i;
1812 switch (p->type)
1814 case P_ENUM:
1815 for (i = 0; p->enum_list[i].name; i++) {
1816 if (*(int *)ptr == p->enum_list[i].value) {
1817 fprintf(f, "%s",
1818 p->enum_list[i].name);
1819 break;
1822 break;
1824 case P_BOOL:
1825 fprintf(f, "%s", BOOLSTR(*(bool *)ptr));
1826 break;
1828 case P_BOOLREV:
1829 fprintf(f, "%s", BOOLSTR(!*(bool *)ptr));
1830 break;
1832 case P_INTEGER:
1833 case P_BYTES:
1834 fprintf(f, "%d", *(int *)ptr);
1835 break;
1837 case P_CHAR:
1838 fprintf(f, "%c", *(char *)ptr);
1839 break;
1841 case P_OCTAL: {
1842 int val = *(int *)ptr;
1843 if (val == -1) {
1844 fprintf(f, "-1");
1845 } else {
1846 fprintf(f, "0%03o", val);
1848 break;
1851 case P_CMDLIST:
1852 list_sep = " ";
1853 /* fall through */
1854 case P_LIST:
1855 if ((char ***)ptr && *(char ***)ptr) {
1856 char **list = *(char ***)ptr;
1857 for (; *list; list++) {
1858 /* surround strings with whitespace in double quotes */
1859 if (*(list+1) == NULL) {
1860 /* last item, no extra separator */
1861 list_sep = "";
1863 if ( strchr_m( *list, ' ' ) ) {
1864 fprintf(f, "\"%s\"%s", *list, list_sep);
1865 } else {
1866 fprintf(f, "%s%s", *list, list_sep);
1870 break;
1872 case P_STRING:
1873 case P_USTRING:
1874 if (*(char **)ptr) {
1875 fprintf(f, "%s", *(char **)ptr);
1877 break;
1878 case P_SEP:
1879 break;
1884 * Check if two parameters are equal.
1887 static bool lpcfg_equal_parameter(parm_type type, void *ptr1, void *ptr2)
1889 switch (type) {
1890 case P_BOOL:
1891 case P_BOOLREV:
1892 return (*((bool *)ptr1) == *((bool *)ptr2));
1894 case P_INTEGER:
1895 case P_ENUM:
1896 case P_OCTAL:
1897 case P_BYTES:
1898 return (*((int *)ptr1) == *((int *)ptr2));
1900 case P_CHAR:
1901 return (*((char *)ptr1) == *((char *)ptr2));
1903 case P_LIST:
1904 case P_CMDLIST:
1905 return str_list_equal(*(const char ***)ptr1, *(const char ***)ptr2);
1907 case P_STRING:
1908 case P_USTRING:
1910 char *p1 = *(char **)ptr1, *p2 = *(char **)ptr2;
1911 if (p1 && !*p1)
1912 p1 = NULL;
1913 if (p2 && !*p2)
1914 p2 = NULL;
1915 return (p1 == p2 || strequal(p1, p2));
1917 case P_SEP:
1918 break;
1920 return false;
1924 * Process a new section (service).
1926 * At this stage all sections are services.
1927 * Later we'll have special sections that permit server parameters to be set.
1928 * Returns True on success, False on failure.
1931 static bool do_section(const char *pszSectionName, void *userdata)
1933 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1934 bool bRetval;
1935 bool isglobal;
1937 if (lp_ctx->s3_fns != NULL) {
1938 return lp_ctx->s3_fns->do_section(pszSectionName, lp_ctx);
1941 isglobal = ((strwicmp(pszSectionName, GLOBAL_NAME) == 0) ||
1942 (strwicmp(pszSectionName, GLOBAL_NAME2) == 0));
1944 bRetval = false;
1946 /* if we've just struck a global section, note the fact. */
1947 lp_ctx->bInGlobalSection = isglobal;
1949 /* check for multiple global sections */
1950 if (lp_ctx->bInGlobalSection) {
1951 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1952 return true;
1955 /* if we have a current service, tidy it up before moving on */
1956 bRetval = true;
1958 if (lp_ctx->currentService != NULL)
1959 bRetval = lpcfg_service_ok(lp_ctx->currentService);
1961 /* if all is still well, move to the next record in the services array */
1962 if (bRetval) {
1963 /* We put this here to avoid an odd message order if messages are */
1964 /* issued by the post-processing of a previous section. */
1965 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1967 if ((lp_ctx->currentService = lpcfg_add_service(lp_ctx, lp_ctx->sDefault,
1968 pszSectionName))
1969 == NULL) {
1970 DEBUG(0, ("Failed to add a new service\n"));
1971 return false;
1975 return bRetval;
1980 * Determine if a particular base parameter is currently set to the default value.
1983 static bool is_default(void *base_structure, int i)
1985 void *def_ptr = ((char *)base_structure) + parm_table[i].offset;
1986 switch (parm_table[i].type) {
1987 case P_CMDLIST:
1988 case P_LIST:
1989 return str_list_equal((const char * const *)parm_table[i].def.lvalue,
1990 *(const char * const **)def_ptr);
1991 case P_STRING:
1992 case P_USTRING:
1993 return strequal(parm_table[i].def.svalue,
1994 *(char **)def_ptr);
1995 case P_BOOL:
1996 case P_BOOLREV:
1997 return parm_table[i].def.bvalue ==
1998 *(bool *)def_ptr;
1999 case P_INTEGER:
2000 case P_CHAR:
2001 case P_OCTAL:
2002 case P_BYTES:
2003 case P_ENUM:
2004 return parm_table[i].def.ivalue ==
2005 *(int *)def_ptr;
2006 case P_SEP:
2007 break;
2009 return false;
2013 *Display the contents of the global structure.
2016 void lpcfg_dump_globals(struct loadparm_context *lp_ctx, FILE *f,
2017 bool show_defaults)
2019 int i;
2020 struct parmlist_entry *data;
2022 fprintf(f, "# Global parameters\n[global]\n");
2024 for (i = 0; parm_table[i].label; i++)
2025 if (parm_table[i].p_class == P_GLOBAL &&
2026 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset))) {
2027 if (!show_defaults) {
2028 if (lp_ctx->flags && (lp_ctx->flags[i] & FLAG_DEFAULT)) {
2029 continue;
2032 if (is_default(lp_ctx->globals, i)) {
2033 continue;
2037 fprintf(f, "\t%s = ", parm_table[i].label);
2038 lpcfg_print_parameter(&parm_table[i], lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[i]), f);
2039 fprintf(f, "\n");
2041 if (lp_ctx->globals->param_opt != NULL) {
2042 for (data = lp_ctx->globals->param_opt; data;
2043 data = data->next) {
2044 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
2045 continue;
2047 fprintf(f, "\t%s = %s\n", data->key, data->value);
2054 * Display the contents of a single services record.
2057 void lpcfg_dump_a_service(struct loadparm_service * pService, struct loadparm_service *sDefault, FILE * f,
2058 unsigned int *flags, bool show_defaults)
2060 int i;
2061 struct parmlist_entry *data;
2063 if (pService != sDefault)
2064 fprintf(f, "\n[%s]\n", pService->szService);
2066 for (i = 0; parm_table[i].label; i++) {
2067 if (parm_table[i].p_class == P_LOCAL &&
2068 (*parm_table[i].label != '-') &&
2069 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset)))
2071 if (pService == sDefault) {
2072 if (!show_defaults) {
2073 if (flags && (flags[i] & FLAG_DEFAULT)) {
2074 continue;
2077 if (is_default(sDefault, i)) {
2078 continue;
2081 } else {
2082 if (lpcfg_equal_parameter(parm_table[i].type,
2083 ((char *)pService) +
2084 parm_table[i].offset,
2085 ((char *)sDefault) +
2086 parm_table[i].offset))
2087 continue;
2090 fprintf(f, "\t%s = ", parm_table[i].label);
2091 lpcfg_print_parameter(&parm_table[i],
2092 ((char *)pService) + parm_table[i].offset, f);
2093 fprintf(f, "\n");
2096 if (pService->param_opt != NULL) {
2097 for (data = pService->param_opt; data; data = data->next) {
2098 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
2099 continue;
2101 fprintf(f, "\t%s = %s\n", data->key, data->value);
2106 bool lpcfg_dump_a_parameter(struct loadparm_context *lp_ctx,
2107 struct loadparm_service *service,
2108 const char *parm_name, FILE * f)
2110 struct parm_struct *parm;
2111 void *ptr;
2112 char *local_parm_name;
2113 char *parm_opt;
2114 const char *parm_opt_value;
2116 /* check for parametrical option */
2117 local_parm_name = talloc_strdup(lp_ctx, parm_name);
2118 if (local_parm_name == NULL) {
2119 return false;
2122 parm_opt = strchr( local_parm_name, ':');
2124 if (parm_opt) {
2125 *parm_opt = '\0';
2126 parm_opt++;
2127 if (strlen(parm_opt)) {
2128 parm_opt_value = lpcfg_parm_string(lp_ctx, service,
2129 local_parm_name, parm_opt);
2130 if (parm_opt_value) {
2131 fprintf(f, "%s\n", parm_opt_value);
2132 return true;
2135 return false;
2138 /* parameter is not parametric, search the table */
2139 parm = lpcfg_parm_struct(lp_ctx, parm_name);
2140 if (!parm) {
2141 return false;
2144 if (service != NULL && parm->p_class == P_GLOBAL) {
2145 return false;
2148 ptr = lpcfg_parm_ptr(lp_ctx, service,parm);
2150 lpcfg_print_parameter(parm, ptr, f);
2151 fprintf(f, "\n");
2152 return true;
2156 * Auto-load some home services.
2158 static void lpcfg_add_auto_services(struct loadparm_context *lp_ctx,
2159 const char *str)
2161 return;
2164 /***************************************************************************
2165 Initialise the sDefault parameter structure for the printer values.
2166 ***************************************************************************/
2168 void init_printer_values(struct loadparm_context *lp_ctx, TALLOC_CTX *ctx,
2169 struct loadparm_service *pService)
2171 /* choose defaults depending on the type of printing */
2172 switch (pService->printing) {
2173 case PRINT_BSD:
2174 case PRINT_AIX:
2175 case PRINT_LPRNT:
2176 case PRINT_LPROS2:
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 break;
2182 case PRINT_LPRNG:
2183 case PRINT_PLP:
2184 lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P'%p'");
2185 lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P'%p' %j");
2186 lpcfg_string_set(ctx, &pService->print_command, "lpr -r -P'%p' %s");
2187 lpcfg_string_set(ctx, &pService->queuepause_command, "lpc stop '%p'");
2188 lpcfg_string_set(ctx, &pService->queueresume_command, "lpc start '%p'");
2189 lpcfg_string_set(ctx, &pService->lppause_command, "lpc hold '%p' %j");
2190 lpcfg_string_set(ctx, &pService->lpresume_command, "lpc release '%p' %j");
2191 break;
2193 case PRINT_CUPS:
2194 case PRINT_IPRINT:
2195 /* set the lpq command to contain the destination printer
2196 name only. This is used by cups_queue_get() */
2197 lpcfg_string_set(ctx, &pService->lpq_command, "%p");
2198 lpcfg_string_set(ctx, &pService->lprm_command, "");
2199 lpcfg_string_set(ctx, &pService->print_command, "");
2200 lpcfg_string_set(ctx, &pService->lppause_command, "");
2201 lpcfg_string_set(ctx, &pService->lpresume_command, "");
2202 lpcfg_string_set(ctx, &pService->queuepause_command, "");
2203 lpcfg_string_set(ctx, &pService->queueresume_command, "");
2204 break;
2206 case PRINT_SYSV:
2207 case PRINT_HPUX:
2208 lpcfg_string_set(ctx, &pService->lpq_command, "lpstat -o%p");
2209 lpcfg_string_set(ctx, &pService->lprm_command, "cancel %p-%j");
2210 lpcfg_string_set(ctx, &pService->print_command, "lp -c -d%p %s; rm %s");
2211 lpcfg_string_set(ctx, &pService->queuepause_command, "disable %p");
2212 lpcfg_string_set(ctx, &pService->queueresume_command, "enable %p");
2213 #ifndef HPUX
2214 lpcfg_string_set(ctx, &pService->lppause_command, "lp -i %p-%j -H hold");
2215 lpcfg_string_set(ctx, &pService->lpresume_command, "lp -i %p-%j -H resume");
2216 #endif /* HPUX */
2217 break;
2219 case PRINT_QNX:
2220 lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P%p");
2221 lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P%p %j");
2222 lpcfg_string_set(ctx, &pService->print_command, "lp -r -P%p %s");
2223 break;
2225 #if defined(DEVELOPER) || defined(ENABLE_SELFTEST)
2227 case PRINT_TEST:
2228 case PRINT_VLP: {
2229 const char *tdbfile;
2230 TALLOC_CTX *tmp_ctx = talloc_new(ctx);
2231 const char *tmp;
2233 tmp = lpcfg_parm_string(lp_ctx, NULL, "vlp", "tdbfile");
2234 if (tmp == NULL) {
2235 tmp = "/tmp/vlp.tdb";
2238 tdbfile = talloc_asprintf(tmp_ctx, "tdbfile=%s", tmp);
2239 if (tdbfile == NULL) {
2240 tdbfile="tdbfile=/tmp/vlp.tdb";
2243 tmp = talloc_asprintf(tmp_ctx, "vlp %s print %%p %%s",
2244 tdbfile);
2245 lpcfg_string_set(ctx, &pService->print_command,
2246 tmp ? tmp : "vlp print %p %s");
2248 tmp = talloc_asprintf(tmp_ctx, "vlp %s lpq %%p",
2249 tdbfile);
2250 lpcfg_string_set(ctx, &pService->lpq_command,
2251 tmp ? tmp : "vlp lpq %p");
2253 tmp = talloc_asprintf(tmp_ctx, "vlp %s lprm %%p %%j",
2254 tdbfile);
2255 lpcfg_string_set(ctx, &pService->lprm_command,
2256 tmp ? tmp : "vlp lprm %p %j");
2258 tmp = talloc_asprintf(tmp_ctx, "vlp %s lppause %%p %%j",
2259 tdbfile);
2260 lpcfg_string_set(ctx, &pService->lppause_command,
2261 tmp ? tmp : "vlp lppause %p %j");
2263 tmp = talloc_asprintf(tmp_ctx, "vlp %s lpresume %%p %%j",
2264 tdbfile);
2265 lpcfg_string_set(ctx, &pService->lpresume_command,
2266 tmp ? tmp : "vlp lpresume %p %j");
2268 tmp = talloc_asprintf(tmp_ctx, "vlp %s queuepause %%p",
2269 tdbfile);
2270 lpcfg_string_set(ctx, &pService->queuepause_command,
2271 tmp ? tmp : "vlp queuepause %p");
2273 tmp = talloc_asprintf(tmp_ctx, "vlp %s queueresume %%p",
2274 tdbfile);
2275 lpcfg_string_set(ctx, &pService->queueresume_command,
2276 tmp ? tmp : "vlp queueresume %p");
2277 TALLOC_FREE(tmp_ctx);
2279 break;
2281 #endif /* DEVELOPER */
2287 * Unload unused services.
2290 void lpcfg_killunused(struct loadparm_context *lp_ctx,
2291 struct smbsrv_connection *smb,
2292 bool (*snumused) (struct smbsrv_connection *, int))
2294 int i;
2296 if (lp_ctx->s3_fns != NULL) {
2297 smb_panic("Cannot be used from an s3 loadparm ctx");
2300 for (i = 0; i < lp_ctx->iNumServices; i++) {
2301 if (lp_ctx->services[i] == NULL)
2302 continue;
2304 if (!snumused || !snumused(smb, i)) {
2305 talloc_free(lp_ctx->services[i]);
2306 lp_ctx->services[i] = NULL;
2312 static int lpcfg_destructor(struct loadparm_context *lp_ctx)
2314 struct parmlist_entry *data;
2316 if (lp_ctx->refuse_free) {
2317 /* someone is trying to free the
2318 global_loadparm_context.
2319 We can't allow that. */
2320 return -1;
2323 if (lp_ctx->globals->param_opt != NULL) {
2324 struct parmlist_entry *next;
2325 for (data = lp_ctx->globals->param_opt; data; data=next) {
2326 next = data->next;
2327 if (data->priority & FLAG_CMDLINE) continue;
2328 DLIST_REMOVE(lp_ctx->globals->param_opt, data);
2329 talloc_free(data);
2333 return 0;
2337 * Initialise the global parameter structure.
2339 * Note that most callers should use loadparm_init_global() instead
2341 struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
2343 int i;
2344 char *myname;
2345 struct loadparm_context *lp_ctx;
2346 struct parmlist_entry *parm;
2347 char *logfile;
2349 lp_ctx = talloc_zero(mem_ctx, struct loadparm_context);
2350 if (lp_ctx == NULL)
2351 return NULL;
2353 talloc_set_destructor(lp_ctx, lpcfg_destructor);
2354 lp_ctx->bInGlobalSection = true;
2355 lp_ctx->globals = talloc_zero(lp_ctx, struct loadparm_global);
2356 /* This appears odd, but globals in s3 isn't a pointer */
2357 lp_ctx->globals->ctx = lp_ctx->globals;
2358 lp_ctx->sDefault = talloc_zero(lp_ctx, struct loadparm_service);
2359 lp_ctx->flags = talloc_zero_array(lp_ctx, unsigned int, num_parameters());
2361 lp_ctx->sDefault->iMaxPrintJobs = 1000;
2362 lp_ctx->sDefault->bAvailable = true;
2363 lp_ctx->sDefault->browseable = true;
2364 lp_ctx->sDefault->read_only = true;
2365 lp_ctx->sDefault->map_archive = true;
2366 lp_ctx->sDefault->strict_locking = true;
2367 lp_ctx->sDefault->oplocks = true;
2368 lp_ctx->sDefault->create_mask = 0744;
2369 lp_ctx->sDefault->force_create_mode = 0000;
2370 lp_ctx->sDefault->directory_mask = 0755;
2371 lp_ctx->sDefault->force_directory_mode = 0000;
2373 DEBUG(3, ("Initialising global parameters\n"));
2375 for (i = 0; parm_table[i].label; i++) {
2376 if ((parm_table[i].type == P_STRING ||
2377 parm_table[i].type == P_USTRING) &&
2378 !(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2379 char **r;
2380 if (parm_table[i].p_class == P_LOCAL) {
2381 r = (char **)(((char *)lp_ctx->sDefault) + parm_table[i].offset);
2382 } else {
2383 r = (char **)(((char *)lp_ctx->globals) + parm_table[i].offset);
2385 *r = talloc_strdup(lp_ctx, "");
2389 logfile = talloc_asprintf(lp_ctx, "%s/log.samba", dyn_LOGFILEBASE);
2390 lpcfg_do_global_parameter(lp_ctx, "log file", logfile);
2391 talloc_free(logfile);
2393 lpcfg_do_global_parameter(lp_ctx, "log level", "0");
2395 lpcfg_do_global_parameter(lp_ctx, "syslog", "1");
2396 lpcfg_do_global_parameter(lp_ctx, "syslog only", "No");
2397 lpcfg_do_global_parameter(lp_ctx, "debug timestamp", "Yes");
2398 lpcfg_do_global_parameter(lp_ctx, "debug prefix timestamp", "No");
2399 lpcfg_do_global_parameter(lp_ctx, "debug hires timestamp", "Yes");
2400 lpcfg_do_global_parameter(lp_ctx, "debug pid", "No");
2401 lpcfg_do_global_parameter(lp_ctx, "debug uid", "No");
2402 lpcfg_do_global_parameter(lp_ctx, "debug class", "No");
2404 lpcfg_do_global_parameter(lp_ctx, "share backend", "classic");
2406 lpcfg_do_global_parameter(lp_ctx, "server role", "auto");
2407 lpcfg_do_global_parameter(lp_ctx, "domain logons", "No");
2408 lpcfg_do_global_parameter(lp_ctx, "domain master", "Auto");
2410 /* options that can be set on the command line must be initialised via
2411 the slower lpcfg_do_global_parameter() to ensure that FLAG_CMDLINE is obeyed */
2412 #ifdef TCP_NODELAY
2413 lpcfg_do_global_parameter(lp_ctx, "socket options", "TCP_NODELAY");
2414 #endif
2415 lpcfg_do_global_parameter(lp_ctx, "workgroup", DEFAULT_WORKGROUP);
2416 myname = get_myname(lp_ctx);
2417 lpcfg_do_global_parameter(lp_ctx, "netbios name", myname);
2418 talloc_free(myname);
2419 lpcfg_do_global_parameter(lp_ctx, "name resolve order", "lmhosts wins host bcast");
2421 lpcfg_do_global_parameter(lp_ctx, "fstype", "NTFS");
2423 lpcfg_do_global_parameter(lp_ctx, "ntvfs handler", "unixuid default");
2424 lpcfg_do_global_parameter(lp_ctx, "max connections", "0");
2426 lpcfg_do_global_parameter(lp_ctx, "dcerpc endpoint servers", "epmapper wkssvc rpcecho samr netlogon lsarpc spoolss drsuapi dssetup unixinfo browser eventlog6 backupkey dnsserver");
2427 lpcfg_do_global_parameter(lp_ctx, "server services", "s3fs rpc nbt wrepl ldap cldap kdc drepl winbindd ntp_signd kcc dnsupdate dns");
2428 lpcfg_do_global_parameter(lp_ctx, "kccsrv:samba_kcc", "false");
2429 /* the winbind method for domain controllers is for both RODC
2430 auth forwarding and for trusted domains */
2431 lpcfg_do_global_parameter(lp_ctx, "private dir", dyn_PRIVATE_DIR);
2432 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_LOCAL_MACHINE", "hklm.ldb");
2434 /* This hive should be dynamically generated by Samba using
2435 data from the sam, but for the moment leave it in a tdb to
2436 keep regedt32 from popping up an annoying dialog. */
2437 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_USERS", "hku.ldb");
2439 /* using UTF8 by default allows us to support all chars */
2440 lpcfg_do_global_parameter(lp_ctx, "unix charset", "UTF-8");
2442 /* Use codepage 850 as a default for the dos character set */
2443 lpcfg_do_global_parameter(lp_ctx, "dos charset", "CP850");
2446 * Allow the default PASSWD_CHAT to be overridden in local.h.
2448 lpcfg_do_global_parameter(lp_ctx, "passwd chat", DEFAULT_PASSWD_CHAT);
2450 lpcfg_do_global_parameter(lp_ctx, "pid directory", dyn_PIDDIR);
2451 lpcfg_do_global_parameter(lp_ctx, "lock dir", dyn_LOCKDIR);
2452 lpcfg_do_global_parameter(lp_ctx, "state directory", dyn_STATEDIR);
2453 lpcfg_do_global_parameter(lp_ctx, "cache directory", dyn_CACHEDIR);
2454 lpcfg_do_global_parameter(lp_ctx, "ncalrpc dir", dyn_NCALRPCDIR);
2456 lpcfg_do_global_parameter(lp_ctx, "nbt client socket address", "0.0.0.0");
2457 lpcfg_do_global_parameter_var(lp_ctx, "server string",
2458 "Samba %s", SAMBA_VERSION_STRING);
2460 lpcfg_do_global_parameter(lp_ctx, "password server", "*");
2462 lpcfg_do_global_parameter(lp_ctx, "max mux", "50");
2463 lpcfg_do_global_parameter(lp_ctx, "max xmit", "16644");
2464 lpcfg_do_global_parameter(lp_ctx, "host msdfs", "true");
2466 lpcfg_do_global_parameter(lp_ctx, "LargeReadwrite", "True");
2467 lpcfg_do_global_parameter(lp_ctx, "server min protocol", "LANMAN1");
2468 lpcfg_do_global_parameter(lp_ctx, "server max protocol", "SMB3");
2469 lpcfg_do_global_parameter(lp_ctx, "client min protocol", "CORE");
2470 lpcfg_do_global_parameter(lp_ctx, "client max protocol", "default");
2471 lpcfg_do_global_parameter(lp_ctx, "security", "AUTO");
2472 lpcfg_do_global_parameter(lp_ctx, "EncryptPasswords", "True");
2473 lpcfg_do_global_parameter(lp_ctx, "ReadRaw", "True");
2474 lpcfg_do_global_parameter(lp_ctx, "WriteRaw", "True");
2475 lpcfg_do_global_parameter(lp_ctx, "NullPasswords", "False");
2476 lpcfg_do_global_parameter(lp_ctx, "old password allowed period", "60");
2477 lpcfg_do_global_parameter(lp_ctx, "ObeyPamRestrictions", "False");
2479 lpcfg_do_global_parameter(lp_ctx, "TimeServer", "False");
2480 lpcfg_do_global_parameter(lp_ctx, "BindInterfacesOnly", "False");
2481 lpcfg_do_global_parameter(lp_ctx, "Unicode", "True");
2482 lpcfg_do_global_parameter(lp_ctx, "ClientLanManAuth", "False");
2483 lpcfg_do_global_parameter(lp_ctx, "ClientNTLMv2Auth", "True");
2484 lpcfg_do_global_parameter(lp_ctx, "LanmanAuth", "False");
2485 lpcfg_do_global_parameter(lp_ctx, "NTLMAuth", "True");
2486 lpcfg_do_global_parameter(lp_ctx, "client use spnego principal", "False");
2488 lpcfg_do_global_parameter(lp_ctx, "UnixExtensions", "True");
2490 lpcfg_do_global_parameter(lp_ctx, "PreferredMaster", "Auto");
2491 lpcfg_do_global_parameter(lp_ctx, "LocalMaster", "True");
2493 lpcfg_do_global_parameter(lp_ctx, "wins support", "False");
2494 lpcfg_do_global_parameter(lp_ctx, "dns proxy", "True");
2496 lpcfg_do_global_parameter(lp_ctx, "winbind separator", "\\");
2497 lpcfg_do_global_parameter(lp_ctx, "winbind sealed pipes", "True");
2498 lpcfg_do_global_parameter(lp_ctx, "require strong key", "True");
2499 lpcfg_do_global_parameter(lp_ctx, "winbindd socket directory", dyn_WINBINDD_SOCKET_DIR);
2500 lpcfg_do_global_parameter(lp_ctx, "winbindd privileged socket directory", dyn_WINBINDD_PRIVILEGED_SOCKET_DIR);
2501 lpcfg_do_global_parameter(lp_ctx, "ntp signd socket directory", dyn_NTP_SIGND_SOCKET_DIR);
2502 lpcfg_do_global_parameter_var(lp_ctx, "dns update command", "%s/samba_dnsupdate", dyn_SCRIPTSBINDIR);
2503 lpcfg_do_global_parameter_var(lp_ctx, "spn update command", "%s/samba_spnupdate", dyn_SCRIPTSBINDIR);
2504 lpcfg_do_global_parameter_var(lp_ctx, "samba kcc command",
2505 "%s/samba_kcc", dyn_SCRIPTSBINDIR);
2506 lpcfg_do_global_parameter(lp_ctx, "template shell", "/bin/false");
2507 lpcfg_do_global_parameter(lp_ctx, "template homedir", "/home/%D/%U");
2509 lpcfg_do_global_parameter(lp_ctx, "client signing", "default");
2510 lpcfg_do_global_parameter(lp_ctx, "server signing", "default");
2512 lpcfg_do_global_parameter(lp_ctx, "use spnego", "True");
2514 lpcfg_do_global_parameter(lp_ctx, "use mmap", "True");
2516 lpcfg_do_global_parameter(lp_ctx, "smb ports", "445 139");
2517 lpcfg_do_global_parameter(lp_ctx, "nbt port", "137");
2518 lpcfg_do_global_parameter(lp_ctx, "dgram port", "138");
2519 lpcfg_do_global_parameter(lp_ctx, "cldap port", "389");
2520 lpcfg_do_global_parameter(lp_ctx, "krb5 port", "88");
2521 lpcfg_do_global_parameter(lp_ctx, "kpasswd port", "464");
2522 lpcfg_do_global_parameter(lp_ctx, "web port", "901");
2524 lpcfg_do_global_parameter(lp_ctx, "nt status support", "True");
2526 lpcfg_do_global_parameter(lp_ctx, "max wins ttl", "518400"); /* 6 days */
2527 lpcfg_do_global_parameter(lp_ctx, "min wins ttl", "21600");
2529 lpcfg_do_global_parameter(lp_ctx, "tls enabled", "True");
2530 lpcfg_do_global_parameter(lp_ctx, "tls keyfile", "tls/key.pem");
2531 lpcfg_do_global_parameter(lp_ctx, "tls certfile", "tls/cert.pem");
2532 lpcfg_do_global_parameter(lp_ctx, "tls cafile", "tls/ca.pem");
2533 lpcfg_do_global_parameter(lp_ctx, "prefork children:smb", "4");
2535 lpcfg_do_global_parameter(lp_ctx, "rndc command", "/usr/sbin/rndc");
2536 lpcfg_do_global_parameter(lp_ctx, "nsupdate command", "/usr/bin/nsupdate -g");
2538 lpcfg_do_global_parameter(lp_ctx, "allow dns updates", "secure only");
2539 lpcfg_do_global_parameter(lp_ctx, "dns forwarder", "");
2541 lpcfg_do_global_parameter(lp_ctx, "algorithmic rid base", "1000");
2543 lpcfg_do_global_parameter(lp_ctx, "enhanced browsing", "True");
2545 lpcfg_do_global_parameter(lp_ctx, "winbind nss info", "template");
2547 lpcfg_do_global_parameter(lp_ctx, "server schannel", "Auto");
2549 lpcfg_do_global_parameter(lp_ctx, "short preserve case", "True");
2551 lpcfg_do_global_parameter(lp_ctx, "max open files", "16384");
2553 lpcfg_do_global_parameter(lp_ctx, "cups connection timeout", "30");
2555 lpcfg_do_global_parameter(lp_ctx, "locking", "True");
2557 lpcfg_do_global_parameter(lp_ctx, "block size", "1024");
2559 lpcfg_do_global_parameter(lp_ctx, "client use spnego", "True");
2561 lpcfg_do_global_parameter(lp_ctx, "change notify", "True");
2563 lpcfg_do_global_parameter(lp_ctx, "name cache timeout", "660");
2565 lpcfg_do_global_parameter(lp_ctx, "defer sharing violations", "True");
2567 lpcfg_do_global_parameter(lp_ctx, "ldap replication sleep", "1000");
2569 lpcfg_do_global_parameter(lp_ctx, "idmap backend", "tdb");
2571 lpcfg_do_global_parameter(lp_ctx, "enable privileges", "True");
2573 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max write", "%u", DEFAULT_SMB2_MAX_WRITE);
2575 lpcfg_do_global_parameter(lp_ctx, "passdb backend", "tdbsam");
2577 lpcfg_do_global_parameter(lp_ctx, "getwd cache", "True");
2579 lpcfg_do_global_parameter(lp_ctx, "winbind nested groups", "True");
2581 lpcfg_do_global_parameter(lp_ctx, "mangled names", "True");
2583 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max credits", "%u", DEFAULT_SMB2_MAX_CREDITS);
2585 lpcfg_do_global_parameter(lp_ctx, "ldap ssl", "start tls");
2587 lpcfg_do_global_parameter(lp_ctx, "ldap deref", "auto");
2589 lpcfg_do_global_parameter(lp_ctx, "lm interval", "60");
2591 lpcfg_do_global_parameter(lp_ctx, "mangling method", "hash2");
2593 lpcfg_do_global_parameter(lp_ctx, "hide dot files", "True");
2595 lpcfg_do_global_parameter(lp_ctx, "browse list", "True");
2597 lpcfg_do_global_parameter(lp_ctx, "passwd chat timeout", "2");
2599 lpcfg_do_global_parameter(lp_ctx, "guest account", GUEST_ACCOUNT);
2601 lpcfg_do_global_parameter(lp_ctx, "client schannel", "auto");
2603 lpcfg_do_global_parameter(lp_ctx, "smb encrypt", "default");
2605 lpcfg_do_global_parameter(lp_ctx, "max log size", "5000");
2607 lpcfg_do_global_parameter(lp_ctx, "idmap negative cache time", "120");
2609 lpcfg_do_global_parameter(lp_ctx, "ldap follow referral", "auto");
2611 lpcfg_do_global_parameter(lp_ctx, "multicast dns register", "yes");
2613 lpcfg_do_global_parameter(lp_ctx, "winbind reconnect delay", "30");
2615 lpcfg_do_global_parameter(lp_ctx, "winbind request timeout", "60");
2617 lpcfg_do_global_parameter(lp_ctx, "nt acl support", "yes");
2619 lpcfg_do_global_parameter(lp_ctx, "acl check permissions", "yes");
2621 lpcfg_do_global_parameter(lp_ctx, "keepalive", "300");
2623 lpcfg_do_global_parameter(lp_ctx, "smbd profiling level", "off");
2625 lpcfg_do_global_parameter(lp_ctx, "winbind cache time", "300");
2627 lpcfg_do_global_parameter(lp_ctx, "level2 oplocks", "yes");
2629 lpcfg_do_global_parameter(lp_ctx, "show add printer wizard", "yes");
2631 lpcfg_do_global_parameter(lp_ctx, "allocation roundup size", "1048576");
2633 lpcfg_do_global_parameter(lp_ctx, "ldap page size", "1024");
2635 lpcfg_do_global_parameter(lp_ctx, "kernel share modes", "yes");
2637 lpcfg_do_global_parameter(lp_ctx, "strict locking", "Auto");
2639 lpcfg_do_global_parameter(lp_ctx, "map readonly", "yes");
2641 lpcfg_do_global_parameter(lp_ctx, "allow trusted domains", "yes");
2643 lpcfg_do_global_parameter(lp_ctx, "default devmode", "yes");
2645 lpcfg_do_global_parameter(lp_ctx, "os level", "20");
2647 lpcfg_do_global_parameter(lp_ctx, "dos filetimes", "yes");
2649 lpcfg_do_global_parameter(lp_ctx, "mangling char", "~");
2651 lpcfg_do_global_parameter(lp_ctx, "printcap cache time", "750");
2653 lpcfg_do_global_parameter(lp_ctx, "create krb5 conf", "yes");
2655 lpcfg_do_global_parameter(lp_ctx, "winbind max clients", "200");
2657 lpcfg_do_global_parameter(lp_ctx, "acl map full control", "yes");
2659 lpcfg_do_global_parameter(lp_ctx, "nt pipe support", "yes");
2661 lpcfg_do_global_parameter(lp_ctx, "ldap debug threshold", "10");
2663 lpcfg_do_global_parameter(lp_ctx, "client ldap sasl wrapping", "sign");
2665 lpcfg_do_global_parameter(lp_ctx, "follow symlinks", "yes");
2667 lpcfg_do_global_parameter(lp_ctx, "machine password timeout", "604800");
2669 lpcfg_do_global_parameter(lp_ctx, "ldap connection timeout", "2");
2671 lpcfg_do_global_parameter(lp_ctx, "winbind expand groups", "0");
2673 lpcfg_do_global_parameter(lp_ctx, "stat cache", "yes");
2675 lpcfg_do_global_parameter(lp_ctx, "lpq cache time", "30");
2677 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max trans", "%u", DEFAULT_SMB2_MAX_TRANSACT);
2679 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max read", "%u", DEFAULT_SMB2_MAX_READ);
2681 lpcfg_do_global_parameter(lp_ctx, "durable handles", "yes");
2683 lpcfg_do_global_parameter(lp_ctx, "max stat cache size", "256");
2685 lpcfg_do_global_parameter(lp_ctx, "ldap passwd sync", "no");
2687 lpcfg_do_global_parameter(lp_ctx, "kernel change notify", "yes");
2689 lpcfg_do_global_parameter(lp_ctx, "max ttl", "259200");
2691 lpcfg_do_global_parameter(lp_ctx, "blocking locks", "yes");
2693 lpcfg_do_global_parameter(lp_ctx, "oplock contention limit", "2");
2695 lpcfg_do_global_parameter(lp_ctx, "load printers", "yes");
2697 lpcfg_do_global_parameter(lp_ctx, "idmap cache time", "604800");
2699 lpcfg_do_global_parameter(lp_ctx, "preserve case", "yes");
2701 lpcfg_do_global_parameter(lp_ctx, "lm announce", "auto");
2703 lpcfg_do_global_parameter(lp_ctx, "afs token lifetime", "604800");
2705 lpcfg_do_global_parameter(lp_ctx, "enable core files", "yes");
2707 lpcfg_do_global_parameter(lp_ctx, "winbind max domain connections", "1");
2709 lpcfg_do_global_parameter(lp_ctx, "case sensitive", "auto");
2711 lpcfg_do_global_parameter(lp_ctx, "ldap timeout", "15");
2713 lpcfg_do_global_parameter(lp_ctx, "mangle prefix", "1");
2715 lpcfg_do_global_parameter(lp_ctx, "posix locking", "yes");
2717 lpcfg_do_global_parameter(lp_ctx, "lock spin time", "200");
2719 lpcfg_do_global_parameter(lp_ctx, "directory name cache size", "100");
2721 lpcfg_do_global_parameter(lp_ctx, "nmbd bind explicit broadcast", "yes");
2723 lpcfg_do_global_parameter(lp_ctx, "init logon delay", "100");
2725 lpcfg_do_global_parameter(lp_ctx, "usershare owner only", "yes");
2727 lpcfg_do_global_parameter(lp_ctx, "-valid", "yes");
2729 lpcfg_do_global_parameter_var(lp_ctx, "usershare path", "%s/usershares", get_dyn_STATEDIR());
2731 #ifdef DEVELOPER
2732 lpcfg_do_global_parameter_var(lp_ctx, "panic action", "/bin/sleep 999999999");
2733 #endif
2735 lpcfg_do_global_parameter(lp_ctx, "smb passwd file", get_dyn_SMB_PASSWD_FILE());
2737 lpcfg_do_global_parameter(lp_ctx, "logon home", "\\\\%N\\%U");
2739 lpcfg_do_global_parameter(lp_ctx, "logon path", "\\\\%N\\%U\\profile");
2741 lpcfg_do_global_parameter(lp_ctx, "printjob username", "%U");
2743 for (i = 0; parm_table[i].label; i++) {
2744 if (!(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2745 lp_ctx->flags[i] |= FLAG_DEFAULT;
2749 for (parm=lp_ctx->globals->param_opt; parm; parm=parm->next) {
2750 if (!(parm->priority & FLAG_CMDLINE)) {
2751 parm->priority |= FLAG_DEFAULT;
2755 for (parm=lp_ctx->sDefault->param_opt; parm; parm=parm->next) {
2756 if (!(parm->priority & FLAG_CMDLINE)) {
2757 parm->priority |= FLAG_DEFAULT;
2762 return lp_ctx;
2766 * Initialise the global parameter structure.
2768 struct loadparm_context *loadparm_init_global(bool load_default)
2770 if (global_loadparm_context == NULL) {
2771 global_loadparm_context = loadparm_init(NULL);
2773 if (global_loadparm_context == NULL) {
2774 return NULL;
2776 global_loadparm_context->global = true;
2777 if (load_default && !global_loadparm_context->loaded) {
2778 lpcfg_load_default(global_loadparm_context);
2780 global_loadparm_context->refuse_free = true;
2781 return global_loadparm_context;
2785 * Initialise the global parameter structure.
2787 struct loadparm_context *loadparm_init_s3(TALLOC_CTX *mem_ctx,
2788 const struct loadparm_s3_helpers *s3_fns)
2790 struct loadparm_context *loadparm_context = talloc_zero(mem_ctx, struct loadparm_context);
2791 if (!loadparm_context) {
2792 return NULL;
2794 loadparm_context->s3_fns = s3_fns;
2795 loadparm_context->globals = s3_fns->globals;
2796 loadparm_context->flags = s3_fns->flags;
2798 return loadparm_context;
2801 const char *lpcfg_configfile(struct loadparm_context *lp_ctx)
2803 return lp_ctx->szConfigFile;
2806 const char *lp_default_path(void)
2808 if (getenv("SMB_CONF_PATH"))
2809 return getenv("SMB_CONF_PATH");
2810 else
2811 return dyn_CONFIGFILE;
2815 * Update the internal state of a loadparm context after settings
2816 * have changed.
2818 static bool lpcfg_update(struct loadparm_context *lp_ctx)
2820 struct debug_settings settings;
2821 TALLOC_CTX *tmp_ctx;
2823 tmp_ctx = talloc_new(lp_ctx);
2824 if (tmp_ctx == NULL) {
2825 return false;
2828 lpcfg_add_auto_services(lp_ctx, lpcfg_auto_services(lp_ctx, tmp_ctx));
2830 if (!lp_ctx->globals->wins_server_list && lp_ctx->globals->we_are_a_wins_server) {
2831 lpcfg_do_global_parameter(lp_ctx, "wins server", "127.0.0.1");
2834 if (!lp_ctx->global) {
2835 TALLOC_FREE(tmp_ctx);
2836 return true;
2839 panic_action = lp_ctx->globals->panic_action;
2841 reload_charcnv(lp_ctx);
2843 ZERO_STRUCT(settings);
2844 /* Add any more debug-related smb.conf parameters created in
2845 * future here */
2846 settings.syslog = lp_ctx->globals->syslog;
2847 settings.syslog_only = lp_ctx->globals->syslog_only;
2848 settings.timestamp_logs = lp_ctx->globals->timestamp_logs;
2849 settings.debug_prefix_timestamp = lp_ctx->globals->debug_prefix_timestamp;
2850 settings.debug_hires_timestamp = lp_ctx->globals->debug_hires_timestamp;
2851 settings.debug_pid = lp_ctx->globals->debug_pid;
2852 settings.debug_uid = lp_ctx->globals->debug_uid;
2853 settings.debug_class = lp_ctx->globals->debug_class;
2854 debug_set_settings(&settings);
2856 /* FIXME: This is a bit of a hack, but we can't use a global, since
2857 * not everything that uses lp also uses the socket library */
2858 if (lpcfg_parm_bool(lp_ctx, NULL, "socket", "testnonblock", false)) {
2859 setenv("SOCKET_TESTNONBLOCK", "1", 1);
2860 } else {
2861 unsetenv("SOCKET_TESTNONBLOCK");
2864 TALLOC_FREE(tmp_ctx);
2865 return true;
2868 bool lpcfg_load_default(struct loadparm_context *lp_ctx)
2870 const char *path;
2872 path = lp_default_path();
2874 if (!file_exist(path)) {
2875 /* We allow the default smb.conf file to not exist,
2876 * basically the equivalent of an empty file. */
2877 return lpcfg_update(lp_ctx);
2880 return lpcfg_load(lp_ctx, path);
2884 * Load the services array from the services file.
2886 * Return True on success, False on failure.
2888 bool lpcfg_load(struct loadparm_context *lp_ctx, const char *filename)
2890 char *n2;
2891 bool bRetval;
2893 filename = talloc_strdup(lp_ctx, filename);
2895 lp_ctx->szConfigFile = filename;
2897 if (lp_ctx->s3_fns) {
2898 return lp_ctx->s3_fns->load(filename);
2901 lp_ctx->bInGlobalSection = true;
2902 n2 = standard_sub_basic(lp_ctx, lp_ctx->szConfigFile);
2903 DEBUG(2, ("lpcfg_load: refreshing parameters from %s\n", n2));
2905 add_to_file_list(lp_ctx, &lp_ctx->file_lists, lp_ctx->szConfigFile, n2);
2907 /* We get sections first, so have to start 'behind' to make up */
2908 lp_ctx->currentService = NULL;
2909 bRetval = pm_process(n2, do_section, lpcfg_do_parameter, lp_ctx);
2911 /* finish up the last section */
2912 DEBUG(4, ("pm_process() returned %s\n", BOOLSTR(bRetval)));
2913 if (bRetval)
2914 if (lp_ctx->currentService != NULL)
2915 bRetval = lpcfg_service_ok(lp_ctx->currentService);
2917 bRetval = bRetval && lpcfg_update(lp_ctx);
2919 /* we do this unconditionally, so that it happens even
2920 for a missing smb.conf */
2921 reload_charcnv(lp_ctx);
2923 if (bRetval == true) {
2924 /* set this up so that any child python tasks will
2925 find the right smb.conf */
2926 setenv("SMB_CONF_PATH", filename, 1);
2928 /* set the context used by the lp_*() function
2929 varients */
2930 global_loadparm_context = lp_ctx;
2931 lp_ctx->loaded = true;
2934 return bRetval;
2938 * Return the max number of services.
2941 int lpcfg_numservices(struct loadparm_context *lp_ctx)
2943 if (lp_ctx->s3_fns) {
2944 return lp_ctx->s3_fns->get_numservices();
2947 return lp_ctx->iNumServices;
2951 * Display the contents of the services array in human-readable form.
2954 void lpcfg_dump(struct loadparm_context *lp_ctx, FILE *f, bool show_defaults,
2955 int maxtoprint)
2957 int iService;
2959 if (lp_ctx->s3_fns) {
2960 lp_ctx->s3_fns->dump(f, show_defaults, maxtoprint);
2961 return;
2964 lpcfg_dump_globals(lp_ctx, f, show_defaults);
2966 lpcfg_dump_a_service(lp_ctx->sDefault, lp_ctx->sDefault, f, lp_ctx->flags, show_defaults);
2968 for (iService = 0; iService < maxtoprint; iService++)
2969 lpcfg_dump_one(f, show_defaults, lp_ctx->services[iService], lp_ctx->sDefault);
2973 * Display the contents of one service in human-readable form.
2975 void lpcfg_dump_one(FILE *f, bool show_defaults, struct loadparm_service *service, struct loadparm_service *sDefault)
2977 if (service != NULL) {
2978 if (service->szService[0] == '\0')
2979 return;
2980 lpcfg_dump_a_service(service, sDefault, f, NULL, show_defaults);
2984 struct loadparm_service *lpcfg_servicebynum(struct loadparm_context *lp_ctx,
2985 int snum)
2987 if (lp_ctx->s3_fns) {
2988 return lp_ctx->s3_fns->get_servicebynum(snum);
2991 return lp_ctx->services[snum];
2994 struct loadparm_service *lpcfg_service(struct loadparm_context *lp_ctx,
2995 const char *service_name)
2997 int iService;
2998 char *serviceName;
3000 if (lp_ctx->s3_fns) {
3001 return lp_ctx->s3_fns->get_service(service_name);
3004 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--) {
3005 if (lp_ctx->services[iService] &&
3006 lp_ctx->services[iService]->szService) {
3008 * The substitution here is used to support %U is
3009 * service names
3011 serviceName = standard_sub_basic(
3012 lp_ctx->services[iService],
3013 lp_ctx->services[iService]->szService);
3014 if (strequal(serviceName, service_name)) {
3015 talloc_free(serviceName);
3016 return lp_ctx->services[iService];
3018 talloc_free(serviceName);
3022 DEBUG(7,("lpcfg_servicenumber: couldn't find %s\n", service_name));
3023 return NULL;
3026 const char *lpcfg_servicename(const struct loadparm_service *service)
3028 return lpcfg_string((const char *)service->szService);
3032 * A useful volume label function.
3034 const char *lpcfg_volume_label(struct loadparm_service *service, struct loadparm_service *sDefault)
3036 const char *ret;
3037 ret = lpcfg_string((const char *)((service != NULL && service->volume != NULL) ?
3038 service->volume : sDefault->volume));
3039 if (!*ret)
3040 return lpcfg_servicename(service);
3041 return ret;
3045 * Return the correct printer name.
3047 const char *lpcfg_printername(struct loadparm_service *service, struct loadparm_service *sDefault)
3049 const char *ret;
3050 ret = lpcfg_string((const char *)((service != NULL && service->_printername != NULL) ?
3051 service->_printername : sDefault->_printername));
3052 if (ret == NULL || (ret != NULL && *ret == '\0'))
3053 ret = lpcfg_servicename(service);
3055 return ret;
3060 * Return the max print jobs per queue.
3062 int lpcfg_maxprintjobs(struct loadparm_service *service, struct loadparm_service *sDefault)
3064 int maxjobs = (service != NULL) ? service->iMaxPrintJobs : sDefault->iMaxPrintJobs;
3065 if (maxjobs <= 0 || maxjobs >= PRINT_MAX_JOBID)
3066 maxjobs = PRINT_MAX_JOBID - 1;
3068 return maxjobs;
3071 struct smb_iconv_handle *lpcfg_iconv_handle(struct loadparm_context *lp_ctx)
3073 if (lp_ctx == NULL) {
3074 return get_iconv_handle();
3076 return lp_ctx->iconv_handle;
3079 _PUBLIC_ void reload_charcnv(struct loadparm_context *lp_ctx)
3081 struct smb_iconv_handle *old_ic = lp_ctx->iconv_handle;
3082 if (!lp_ctx->global) {
3083 return;
3086 if (old_ic == NULL) {
3087 old_ic = global_iconv_handle;
3089 lp_ctx->iconv_handle = smb_iconv_handle_reinit_lp(lp_ctx, lp_ctx, old_ic);
3090 global_iconv_handle = lp_ctx->iconv_handle;
3093 _PUBLIC_ char *lpcfg_tls_keyfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3095 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_keyfile(lp_ctx));
3098 _PUBLIC_ char *lpcfg_tls_certfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3100 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_certfile(lp_ctx));
3103 _PUBLIC_ char *lpcfg_tls_cafile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3105 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_cafile(lp_ctx));
3108 _PUBLIC_ char *lpcfg_tls_crlfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3110 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_crlfile(lp_ctx));
3113 _PUBLIC_ char *lpcfg_tls_dhpfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3115 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_dhpfile(lp_ctx));
3118 struct gensec_settings *lpcfg_gensec_settings(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3120 struct gensec_settings *settings = talloc_zero(mem_ctx, struct gensec_settings);
3121 if (settings == NULL)
3122 return NULL;
3123 SMB_ASSERT(lp_ctx != NULL);
3124 settings->lp_ctx = talloc_reference(settings, lp_ctx);
3125 settings->target_hostname = lpcfg_parm_string(lp_ctx, NULL, "gensec", "target_hostname");
3126 return settings;
3129 int lpcfg_server_role(struct loadparm_context *lp_ctx)
3131 int domain_master = lpcfg__domain_master(lp_ctx);
3133 return lp_find_server_role(lpcfg__server_role(lp_ctx),
3134 lpcfg__security(lp_ctx),
3135 lpcfg__domain_logons(lp_ctx),
3136 (domain_master == true) ||
3137 (domain_master == Auto));
3140 int lpcfg_security(struct loadparm_context *lp_ctx)
3142 return lp_find_security(lpcfg__server_role(lp_ctx),
3143 lpcfg__security(lp_ctx));
3146 int lpcfg_client_max_protocol(struct loadparm_context *lp_ctx)
3148 int client_max_protocol = lpcfg__client_max_protocol(lp_ctx);
3149 if (client_max_protocol == PROTOCOL_DEFAULT) {
3150 return PROTOCOL_NT1;
3152 return client_max_protocol;
3155 bool lpcfg_server_signing_allowed(struct loadparm_context *lp_ctx, bool *mandatory)
3157 bool allowed = true;
3158 enum smb_signing_setting signing_setting = lpcfg_server_signing(lp_ctx);
3160 *mandatory = false;
3162 if (signing_setting == SMB_SIGNING_DEFAULT) {
3164 * If we are a domain controller, SMB signing is
3165 * really important, as it can prevent a number of
3166 * attacks on communications between us and the
3167 * clients
3169 * However, it really sucks (no sendfile, CPU
3170 * overhead) performance-wise when used on a
3171 * file server, so disable it by default
3172 * on non-DCs
3175 if (lpcfg_server_role(lp_ctx) >= ROLE_ACTIVE_DIRECTORY_DC) {
3176 signing_setting = SMB_SIGNING_REQUIRED;
3177 } else {
3178 signing_setting = SMB_SIGNING_OFF;
3182 switch (signing_setting) {
3183 case SMB_SIGNING_REQUIRED:
3184 *mandatory = true;
3185 break;
3186 case SMB_SIGNING_IF_REQUIRED:
3187 break;
3188 case SMB_SIGNING_DEFAULT:
3189 case SMB_SIGNING_OFF:
3190 allowed = false;
3191 break;
3194 return allowed;
3197 int lpcfg_tdb_hash_size(struct loadparm_context *lp_ctx, const char *name)
3199 const char *base;
3201 if (name == NULL) {
3202 return 0;
3205 base = strrchr_m(name, '/');
3206 if (base != NULL) {
3207 base += 1;
3208 } else {
3209 base = name;
3211 return lpcfg_parm_int(lp_ctx, NULL, "tdb_hashsize", base, 0);
3215 int lpcfg_tdb_flags(struct loadparm_context *lp_ctx, int tdb_flags)
3217 if (!lpcfg_use_mmap(lp_ctx)) {
3218 tdb_flags |= TDB_NOMMAP;
3220 return tdb_flags;