lib: iov_buf does not need talloc.h anymore
[Samba.git] / lib / param / loadparm.c
blobbfa9c7b61fcee47b59865417b2d765d3ce6d68b5
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 if (!ok) {
1571 return false;
1573 goto mark_non_default;
1576 ok = set_variable_helper(mem_ctx, parmnum, parm_ptr, pszParmName, pszParmValue);
1578 if (!ok) {
1579 return false;
1582 mark_non_default:
1583 if (on_globals && (lp_ctx->flags[parmnum] & FLAG_DEFAULT)) {
1584 lp_ctx->flags[parmnum] &= ~FLAG_DEFAULT;
1585 /* we have to also unset FLAG_DEFAULT on aliases */
1586 for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
1587 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1589 for (i=parmnum+1;i<num_parameters() && parm_table[i].offset == parm_table[parmnum].offset;i++) {
1590 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1593 return true;
1597 bool lpcfg_do_global_parameter(struct loadparm_context *lp_ctx,
1598 const char *pszParmName, const char *pszParmValue)
1600 int parmnum = lpcfg_map_parameter(pszParmName);
1601 void *parm_ptr;
1603 if (parmnum < 0) {
1604 if (strchr(pszParmName, ':')) {
1605 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName, pszParmValue, 0);
1607 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1608 return true;
1611 /* if the flag has been set on the command line, then don't allow override,
1612 but don't report an error */
1613 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1614 return true;
1617 if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
1618 DEBUG(1, ("WARNING: The \"%s\" option is deprecated\n",
1619 pszParmName));
1622 parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[parmnum]);
1624 return set_variable(lp_ctx->globals->ctx, NULL, parmnum, parm_ptr,
1625 pszParmName, pszParmValue, lp_ctx, true);
1628 bool lpcfg_do_service_parameter(struct loadparm_context *lp_ctx,
1629 struct loadparm_service *service,
1630 const char *pszParmName, const char *pszParmValue)
1632 void *parm_ptr;
1633 int i;
1634 int parmnum = lpcfg_map_parameter(pszParmName);
1636 if (parmnum < 0) {
1637 if (strchr(pszParmName, ':')) {
1638 return lp_do_parameter_parametric(lp_ctx, service, pszParmName, pszParmValue, 0);
1640 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1641 return true;
1644 /* if the flag has been set on the command line, then don't allow override,
1645 but don't report an error */
1646 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1647 return true;
1650 if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
1651 DEBUG(1, ("WARNING: The \"%s\" option is deprecated\n",
1652 pszParmName));
1655 if (parm_table[parmnum].p_class == P_GLOBAL) {
1656 DEBUG(0,
1657 ("Global parameter %s found in service section!\n",
1658 pszParmName));
1659 return true;
1661 parm_ptr = ((char *)service) + parm_table[parmnum].offset;
1663 if (!service->copymap)
1664 init_copymap(service);
1666 /* this handles the aliases - set the copymap for other
1667 * entries with the same data pointer */
1668 for (i = 0; parm_table[i].label; i++)
1669 if (parm_table[i].offset == parm_table[parmnum].offset &&
1670 parm_table[i].p_class == parm_table[parmnum].p_class)
1671 bitmap_clear(service->copymap, i);
1673 return set_variable(service, service, parmnum, parm_ptr, pszParmName,
1674 pszParmValue, lp_ctx, false);
1678 * Process a parameter.
1681 bool lpcfg_do_parameter(const char *pszParmName, const char *pszParmValue,
1682 void *userdata)
1684 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1686 if (lp_ctx->bInGlobalSection)
1687 return lpcfg_do_global_parameter(lp_ctx, pszParmName,
1688 pszParmValue);
1689 else
1690 return lpcfg_do_service_parameter(lp_ctx, lp_ctx->currentService,
1691 pszParmName, pszParmValue);
1695 variable argument do parameter
1697 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx, const char *pszParmName, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
1698 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx,
1699 const char *pszParmName, const char *fmt, ...)
1701 char *s;
1702 bool ret;
1703 va_list ap;
1705 va_start(ap, fmt);
1706 s = talloc_vasprintf(NULL, fmt, ap);
1707 va_end(ap);
1708 ret = lpcfg_do_global_parameter(lp_ctx, pszParmName, s);
1709 talloc_free(s);
1710 return ret;
1715 set a parameter from the commandline - this is called from command line parameter
1716 parsing code. It sets the parameter then marks the parameter as unable to be modified
1717 by smb.conf processing
1719 bool lpcfg_set_cmdline(struct loadparm_context *lp_ctx, const char *pszParmName,
1720 const char *pszParmValue)
1722 int parmnum;
1723 int i;
1725 while (isspace((unsigned char)*pszParmValue)) pszParmValue++;
1727 parmnum = lpcfg_map_parameter(pszParmName);
1729 if (parmnum < 0 && strchr(pszParmName, ':')) {
1730 /* set a parametric option */
1731 bool ok;
1732 ok = lp_do_parameter_parametric(lp_ctx, NULL, pszParmName,
1733 pszParmValue, FLAG_CMDLINE);
1734 if (lp_ctx->s3_fns != NULL) {
1735 if (ok) {
1736 lp_ctx->s3_fns->store_cmdline(pszParmName, pszParmValue);
1739 return ok;
1742 if (parmnum < 0) {
1743 DEBUG(0,("Unknown option '%s'\n", pszParmName));
1744 return false;
1747 /* reset the CMDLINE flag in case this has been called before */
1748 lp_ctx->flags[parmnum] &= ~FLAG_CMDLINE;
1750 if (!lpcfg_do_global_parameter(lp_ctx, pszParmName, pszParmValue)) {
1751 return false;
1754 lp_ctx->flags[parmnum] |= FLAG_CMDLINE;
1756 /* we have to also set FLAG_CMDLINE on aliases */
1757 for (i=parmnum-1;
1758 i>=0 && parm_table[i].p_class == parm_table[parmnum].p_class &&
1759 parm_table[i].offset == parm_table[parmnum].offset;
1760 i--) {
1761 lp_ctx->flags[i] |= FLAG_CMDLINE;
1763 for (i=parmnum+1;
1764 i<num_parameters() &&
1765 parm_table[i].p_class == parm_table[parmnum].p_class &&
1766 parm_table[i].offset == parm_table[parmnum].offset;
1767 i++) {
1768 lp_ctx->flags[i] |= FLAG_CMDLINE;
1771 if (lp_ctx->s3_fns != NULL) {
1772 lp_ctx->s3_fns->store_cmdline(pszParmName, pszParmValue);
1775 return true;
1779 set a option from the commandline in 'a=b' format. Use to support --option
1781 bool lpcfg_set_option(struct loadparm_context *lp_ctx, const char *option)
1783 char *p, *s;
1784 bool ret;
1786 s = talloc_strdup(NULL, option);
1787 if (!s) {
1788 return false;
1791 p = strchr(s, '=');
1792 if (!p) {
1793 talloc_free(s);
1794 return false;
1797 *p = 0;
1799 ret = lpcfg_set_cmdline(lp_ctx, s, p+1);
1800 talloc_free(s);
1801 return ret;
1805 #define BOOLSTR(b) ((b) ? "Yes" : "No")
1808 * Print a parameter of the specified type.
1811 void lpcfg_print_parameter(struct parm_struct *p, void *ptr, FILE * f)
1813 /* For the seperation of lists values that we print below */
1814 const char *list_sep = ", ";
1815 int i;
1816 switch (p->type)
1818 case P_ENUM:
1819 for (i = 0; p->enum_list[i].name; i++) {
1820 if (*(int *)ptr == p->enum_list[i].value) {
1821 fprintf(f, "%s",
1822 p->enum_list[i].name);
1823 break;
1826 break;
1828 case P_BOOL:
1829 fprintf(f, "%s", BOOLSTR(*(bool *)ptr));
1830 break;
1832 case P_BOOLREV:
1833 fprintf(f, "%s", BOOLSTR(!*(bool *)ptr));
1834 break;
1836 case P_INTEGER:
1837 case P_BYTES:
1838 fprintf(f, "%d", *(int *)ptr);
1839 break;
1841 case P_CHAR:
1842 fprintf(f, "%c", *(char *)ptr);
1843 break;
1845 case P_OCTAL: {
1846 int val = *(int *)ptr;
1847 if (val == -1) {
1848 fprintf(f, "-1");
1849 } else {
1850 fprintf(f, "0%03o", val);
1852 break;
1855 case P_CMDLIST:
1856 list_sep = " ";
1857 /* fall through */
1858 case P_LIST:
1859 if ((char ***)ptr && *(char ***)ptr) {
1860 char **list = *(char ***)ptr;
1861 for (; *list; list++) {
1862 /* surround strings with whitespace in double quotes */
1863 if (*(list+1) == NULL) {
1864 /* last item, no extra separator */
1865 list_sep = "";
1867 if ( strchr_m( *list, ' ' ) ) {
1868 fprintf(f, "\"%s\"%s", *list, list_sep);
1869 } else {
1870 fprintf(f, "%s%s", *list, list_sep);
1874 break;
1876 case P_STRING:
1877 case P_USTRING:
1878 if (*(char **)ptr) {
1879 fprintf(f, "%s", *(char **)ptr);
1881 break;
1882 case P_SEP:
1883 break;
1888 * Check if two parameters are equal.
1891 static bool lpcfg_equal_parameter(parm_type type, void *ptr1, void *ptr2)
1893 switch (type) {
1894 case P_BOOL:
1895 case P_BOOLREV:
1896 return (*((bool *)ptr1) == *((bool *)ptr2));
1898 case P_INTEGER:
1899 case P_ENUM:
1900 case P_OCTAL:
1901 case P_BYTES:
1902 return (*((int *)ptr1) == *((int *)ptr2));
1904 case P_CHAR:
1905 return (*((char *)ptr1) == *((char *)ptr2));
1907 case P_LIST:
1908 case P_CMDLIST:
1909 return str_list_equal(*(const char ***)ptr1, *(const char ***)ptr2);
1911 case P_STRING:
1912 case P_USTRING:
1914 char *p1 = *(char **)ptr1, *p2 = *(char **)ptr2;
1915 if (p1 && !*p1)
1916 p1 = NULL;
1917 if (p2 && !*p2)
1918 p2 = NULL;
1919 return (p1 == p2 || strequal(p1, p2));
1921 case P_SEP:
1922 break;
1924 return false;
1928 * Process a new section (service).
1930 * At this stage all sections are services.
1931 * Later we'll have special sections that permit server parameters to be set.
1932 * Returns True on success, False on failure.
1935 static bool do_section(const char *pszSectionName, void *userdata)
1937 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1938 bool bRetval;
1939 bool isglobal;
1941 if (lp_ctx->s3_fns != NULL) {
1942 return lp_ctx->s3_fns->do_section(pszSectionName, lp_ctx);
1945 isglobal = ((strwicmp(pszSectionName, GLOBAL_NAME) == 0) ||
1946 (strwicmp(pszSectionName, GLOBAL_NAME2) == 0));
1948 bRetval = false;
1950 /* if we've just struck a global section, note the fact. */
1951 lp_ctx->bInGlobalSection = isglobal;
1953 /* check for multiple global sections */
1954 if (lp_ctx->bInGlobalSection) {
1955 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1956 return true;
1959 /* if we have a current service, tidy it up before moving on */
1960 bRetval = true;
1962 if (lp_ctx->currentService != NULL)
1963 bRetval = lpcfg_service_ok(lp_ctx->currentService);
1965 /* if all is still well, move to the next record in the services array */
1966 if (bRetval) {
1967 /* We put this here to avoid an odd message order if messages are */
1968 /* issued by the post-processing of a previous section. */
1969 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1971 if ((lp_ctx->currentService = lpcfg_add_service(lp_ctx, lp_ctx->sDefault,
1972 pszSectionName))
1973 == NULL) {
1974 DEBUG(0, ("Failed to add a new service\n"));
1975 return false;
1979 return bRetval;
1984 * Determine if a particular base parameter is currently set to the default value.
1987 static bool is_default(void *base_structure, int i)
1989 void *def_ptr = ((char *)base_structure) + parm_table[i].offset;
1990 switch (parm_table[i].type) {
1991 case P_CMDLIST:
1992 case P_LIST:
1993 return str_list_equal((const char * const *)parm_table[i].def.lvalue,
1994 *(const char * const **)def_ptr);
1995 case P_STRING:
1996 case P_USTRING:
1997 return strequal(parm_table[i].def.svalue,
1998 *(char **)def_ptr);
1999 case P_BOOL:
2000 case P_BOOLREV:
2001 return parm_table[i].def.bvalue ==
2002 *(bool *)def_ptr;
2003 case P_INTEGER:
2004 case P_CHAR:
2005 case P_OCTAL:
2006 case P_BYTES:
2007 case P_ENUM:
2008 return parm_table[i].def.ivalue ==
2009 *(int *)def_ptr;
2010 case P_SEP:
2011 break;
2013 return false;
2017 *Display the contents of the global structure.
2020 void lpcfg_dump_globals(struct loadparm_context *lp_ctx, FILE *f,
2021 bool show_defaults)
2023 int i;
2024 struct parmlist_entry *data;
2026 fprintf(f, "# Global parameters\n[global]\n");
2028 for (i = 0; parm_table[i].label; i++)
2029 if (parm_table[i].p_class == P_GLOBAL &&
2030 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset))) {
2031 if (!show_defaults) {
2032 if (lp_ctx->flags && (lp_ctx->flags[i] & FLAG_DEFAULT)) {
2033 continue;
2036 if (is_default(lp_ctx->globals, i)) {
2037 continue;
2041 fprintf(f, "\t%s = ", parm_table[i].label);
2042 lpcfg_print_parameter(&parm_table[i], lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[i]), f);
2043 fprintf(f, "\n");
2045 if (lp_ctx->globals->param_opt != NULL) {
2046 for (data = lp_ctx->globals->param_opt; data;
2047 data = data->next) {
2048 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
2049 continue;
2051 fprintf(f, "\t%s = %s\n", data->key, data->value);
2058 * Display the contents of a single services record.
2061 void lpcfg_dump_a_service(struct loadparm_service * pService, struct loadparm_service *sDefault, FILE * f,
2062 unsigned int *flags, bool show_defaults)
2064 int i;
2065 struct parmlist_entry *data;
2067 if (pService != sDefault)
2068 fprintf(f, "\n[%s]\n", pService->szService);
2070 for (i = 0; parm_table[i].label; i++) {
2071 if (parm_table[i].p_class == P_LOCAL &&
2072 (*parm_table[i].label != '-') &&
2073 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset)))
2075 if (pService == sDefault) {
2076 if (!show_defaults) {
2077 if (flags && (flags[i] & FLAG_DEFAULT)) {
2078 continue;
2081 if (is_default(sDefault, i)) {
2082 continue;
2085 } else {
2086 if (lpcfg_equal_parameter(parm_table[i].type,
2087 ((char *)pService) +
2088 parm_table[i].offset,
2089 ((char *)sDefault) +
2090 parm_table[i].offset))
2091 continue;
2094 fprintf(f, "\t%s = ", parm_table[i].label);
2095 lpcfg_print_parameter(&parm_table[i],
2096 ((char *)pService) + parm_table[i].offset, f);
2097 fprintf(f, "\n");
2100 if (pService->param_opt != NULL) {
2101 for (data = pService->param_opt; data; data = data->next) {
2102 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
2103 continue;
2105 fprintf(f, "\t%s = %s\n", data->key, data->value);
2110 bool lpcfg_dump_a_parameter(struct loadparm_context *lp_ctx,
2111 struct loadparm_service *service,
2112 const char *parm_name, FILE * f)
2114 struct parm_struct *parm;
2115 void *ptr;
2116 char *local_parm_name;
2117 char *parm_opt;
2118 const char *parm_opt_value;
2120 /* check for parametrical option */
2121 local_parm_name = talloc_strdup(lp_ctx, parm_name);
2122 if (local_parm_name == NULL) {
2123 return false;
2126 parm_opt = strchr( local_parm_name, ':');
2128 if (parm_opt) {
2129 *parm_opt = '\0';
2130 parm_opt++;
2131 if (strlen(parm_opt)) {
2132 parm_opt_value = lpcfg_parm_string(lp_ctx, service,
2133 local_parm_name, parm_opt);
2134 if (parm_opt_value) {
2135 fprintf(f, "%s\n", parm_opt_value);
2136 return true;
2139 return false;
2142 /* parameter is not parametric, search the table */
2143 parm = lpcfg_parm_struct(lp_ctx, parm_name);
2144 if (!parm) {
2145 return false;
2148 if (service != NULL && parm->p_class == P_GLOBAL) {
2149 return false;
2152 ptr = lpcfg_parm_ptr(lp_ctx, service,parm);
2154 lpcfg_print_parameter(parm, ptr, f);
2155 fprintf(f, "\n");
2156 return true;
2160 * Auto-load some home services.
2162 static void lpcfg_add_auto_services(struct loadparm_context *lp_ctx,
2163 const char *str)
2165 return;
2168 /***************************************************************************
2169 Initialise the sDefault parameter structure for the printer values.
2170 ***************************************************************************/
2172 void init_printer_values(struct loadparm_context *lp_ctx, TALLOC_CTX *ctx,
2173 struct loadparm_service *pService)
2175 /* choose defaults depending on the type of printing */
2176 switch (pService->printing) {
2177 case PRINT_BSD:
2178 case PRINT_AIX:
2179 case PRINT_LPRNT:
2180 case PRINT_LPROS2:
2181 lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P'%p'");
2182 lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P'%p' %j");
2183 lpcfg_string_set(ctx, &pService->print_command, "lpr -r -P'%p' %s");
2184 break;
2186 case PRINT_LPRNG:
2187 case PRINT_PLP:
2188 lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P'%p'");
2189 lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P'%p' %j");
2190 lpcfg_string_set(ctx, &pService->print_command, "lpr -r -P'%p' %s");
2191 lpcfg_string_set(ctx, &pService->queuepause_command, "lpc stop '%p'");
2192 lpcfg_string_set(ctx, &pService->queueresume_command, "lpc start '%p'");
2193 lpcfg_string_set(ctx, &pService->lppause_command, "lpc hold '%p' %j");
2194 lpcfg_string_set(ctx, &pService->lpresume_command, "lpc release '%p' %j");
2195 break;
2197 case PRINT_CUPS:
2198 case PRINT_IPRINT:
2199 /* set the lpq command to contain the destination printer
2200 name only. This is used by cups_queue_get() */
2201 lpcfg_string_set(ctx, &pService->lpq_command, "%p");
2202 lpcfg_string_set(ctx, &pService->lprm_command, "");
2203 lpcfg_string_set(ctx, &pService->print_command, "");
2204 lpcfg_string_set(ctx, &pService->lppause_command, "");
2205 lpcfg_string_set(ctx, &pService->lpresume_command, "");
2206 lpcfg_string_set(ctx, &pService->queuepause_command, "");
2207 lpcfg_string_set(ctx, &pService->queueresume_command, "");
2208 break;
2210 case PRINT_SYSV:
2211 case PRINT_HPUX:
2212 lpcfg_string_set(ctx, &pService->lpq_command, "lpstat -o%p");
2213 lpcfg_string_set(ctx, &pService->lprm_command, "cancel %p-%j");
2214 lpcfg_string_set(ctx, &pService->print_command, "lp -c -d%p %s; rm %s");
2215 lpcfg_string_set(ctx, &pService->queuepause_command, "disable %p");
2216 lpcfg_string_set(ctx, &pService->queueresume_command, "enable %p");
2217 #ifndef HPUX
2218 lpcfg_string_set(ctx, &pService->lppause_command, "lp -i %p-%j -H hold");
2219 lpcfg_string_set(ctx, &pService->lpresume_command, "lp -i %p-%j -H resume");
2220 #endif /* HPUX */
2221 break;
2223 case PRINT_QNX:
2224 lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P%p");
2225 lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P%p %j");
2226 lpcfg_string_set(ctx, &pService->print_command, "lp -r -P%p %s");
2227 break;
2229 #if defined(DEVELOPER) || defined(ENABLE_SELFTEST)
2231 case PRINT_TEST:
2232 case PRINT_VLP: {
2233 const char *tdbfile;
2234 TALLOC_CTX *tmp_ctx = talloc_new(ctx);
2235 const char *tmp;
2237 tmp = lpcfg_parm_string(lp_ctx, NULL, "vlp", "tdbfile");
2238 if (tmp == NULL) {
2239 tmp = "/tmp/vlp.tdb";
2242 tdbfile = talloc_asprintf(tmp_ctx, "tdbfile=%s", tmp);
2243 if (tdbfile == NULL) {
2244 tdbfile="tdbfile=/tmp/vlp.tdb";
2247 tmp = talloc_asprintf(tmp_ctx, "vlp %s print %%p %%s",
2248 tdbfile);
2249 lpcfg_string_set(ctx, &pService->print_command,
2250 tmp ? tmp : "vlp print %p %s");
2252 tmp = talloc_asprintf(tmp_ctx, "vlp %s lpq %%p",
2253 tdbfile);
2254 lpcfg_string_set(ctx, &pService->lpq_command,
2255 tmp ? tmp : "vlp lpq %p");
2257 tmp = talloc_asprintf(tmp_ctx, "vlp %s lprm %%p %%j",
2258 tdbfile);
2259 lpcfg_string_set(ctx, &pService->lprm_command,
2260 tmp ? tmp : "vlp lprm %p %j");
2262 tmp = talloc_asprintf(tmp_ctx, "vlp %s lppause %%p %%j",
2263 tdbfile);
2264 lpcfg_string_set(ctx, &pService->lppause_command,
2265 tmp ? tmp : "vlp lppause %p %j");
2267 tmp = talloc_asprintf(tmp_ctx, "vlp %s lpresume %%p %%j",
2268 tdbfile);
2269 lpcfg_string_set(ctx, &pService->lpresume_command,
2270 tmp ? tmp : "vlp lpresume %p %j");
2272 tmp = talloc_asprintf(tmp_ctx, "vlp %s queuepause %%p",
2273 tdbfile);
2274 lpcfg_string_set(ctx, &pService->queuepause_command,
2275 tmp ? tmp : "vlp queuepause %p");
2277 tmp = talloc_asprintf(tmp_ctx, "vlp %s queueresume %%p",
2278 tdbfile);
2279 lpcfg_string_set(ctx, &pService->queueresume_command,
2280 tmp ? tmp : "vlp queueresume %p");
2281 TALLOC_FREE(tmp_ctx);
2283 break;
2285 #endif /* DEVELOPER */
2291 * Unload unused services.
2294 void lpcfg_killunused(struct loadparm_context *lp_ctx,
2295 struct smbsrv_connection *smb,
2296 bool (*snumused) (struct smbsrv_connection *, int))
2298 int i;
2300 if (lp_ctx->s3_fns != NULL) {
2301 smb_panic("Cannot be used from an s3 loadparm ctx");
2304 for (i = 0; i < lp_ctx->iNumServices; i++) {
2305 if (lp_ctx->services[i] == NULL)
2306 continue;
2308 if (!snumused || !snumused(smb, i)) {
2309 talloc_free(lp_ctx->services[i]);
2310 lp_ctx->services[i] = NULL;
2316 static int lpcfg_destructor(struct loadparm_context *lp_ctx)
2318 struct parmlist_entry *data;
2320 if (lp_ctx->refuse_free) {
2321 /* someone is trying to free the
2322 global_loadparm_context.
2323 We can't allow that. */
2324 return -1;
2327 if (lp_ctx->globals->param_opt != NULL) {
2328 struct parmlist_entry *next;
2329 for (data = lp_ctx->globals->param_opt; data; data=next) {
2330 next = data->next;
2331 if (data->priority & FLAG_CMDLINE) continue;
2332 DLIST_REMOVE(lp_ctx->globals->param_opt, data);
2333 talloc_free(data);
2337 return 0;
2341 * Initialise the global parameter structure.
2343 * Note that most callers should use loadparm_init_global() instead
2345 struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
2347 int i;
2348 char *myname;
2349 struct loadparm_context *lp_ctx;
2350 struct parmlist_entry *parm;
2351 char *logfile;
2353 lp_ctx = talloc_zero(mem_ctx, struct loadparm_context);
2354 if (lp_ctx == NULL)
2355 return NULL;
2357 talloc_set_destructor(lp_ctx, lpcfg_destructor);
2358 lp_ctx->bInGlobalSection = true;
2359 lp_ctx->globals = talloc_zero(lp_ctx, struct loadparm_global);
2360 /* This appears odd, but globals in s3 isn't a pointer */
2361 lp_ctx->globals->ctx = lp_ctx->globals;
2362 lp_ctx->sDefault = talloc_zero(lp_ctx, struct loadparm_service);
2363 lp_ctx->flags = talloc_zero_array(lp_ctx, unsigned int, num_parameters());
2365 lp_ctx->sDefault->iMaxPrintJobs = 1000;
2366 lp_ctx->sDefault->bAvailable = true;
2367 lp_ctx->sDefault->browseable = true;
2368 lp_ctx->sDefault->read_only = true;
2369 lp_ctx->sDefault->map_archive = true;
2370 lp_ctx->sDefault->strict_locking = true;
2371 lp_ctx->sDefault->oplocks = true;
2372 lp_ctx->sDefault->create_mask = 0744;
2373 lp_ctx->sDefault->force_create_mode = 0000;
2374 lp_ctx->sDefault->directory_mask = 0755;
2375 lp_ctx->sDefault->force_directory_mode = 0000;
2377 DEBUG(3, ("Initialising global parameters\n"));
2379 for (i = 0; parm_table[i].label; i++) {
2380 if ((parm_table[i].type == P_STRING ||
2381 parm_table[i].type == P_USTRING) &&
2382 !(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2383 char **r;
2384 if (parm_table[i].p_class == P_LOCAL) {
2385 r = (char **)(((char *)lp_ctx->sDefault) + parm_table[i].offset);
2386 } else {
2387 r = (char **)(((char *)lp_ctx->globals) + parm_table[i].offset);
2389 *r = talloc_strdup(lp_ctx, "");
2393 logfile = talloc_asprintf(lp_ctx, "%s/log.samba", dyn_LOGFILEBASE);
2394 lpcfg_do_global_parameter(lp_ctx, "log file", logfile);
2395 talloc_free(logfile);
2397 lpcfg_do_global_parameter(lp_ctx, "log level", "0");
2399 lpcfg_do_global_parameter(lp_ctx, "syslog", "1");
2400 lpcfg_do_global_parameter(lp_ctx, "syslog only", "No");
2401 lpcfg_do_global_parameter(lp_ctx, "debug timestamp", "Yes");
2402 lpcfg_do_global_parameter(lp_ctx, "debug prefix timestamp", "No");
2403 lpcfg_do_global_parameter(lp_ctx, "debug hires timestamp", "Yes");
2404 lpcfg_do_global_parameter(lp_ctx, "debug pid", "No");
2405 lpcfg_do_global_parameter(lp_ctx, "debug uid", "No");
2406 lpcfg_do_global_parameter(lp_ctx, "debug class", "No");
2408 lpcfg_do_global_parameter(lp_ctx, "share backend", "classic");
2410 lpcfg_do_global_parameter(lp_ctx, "server role", "auto");
2411 lpcfg_do_global_parameter(lp_ctx, "domain logons", "No");
2412 lpcfg_do_global_parameter(lp_ctx, "domain master", "Auto");
2414 /* options that can be set on the command line must be initialised via
2415 the slower lpcfg_do_global_parameter() to ensure that FLAG_CMDLINE is obeyed */
2416 #ifdef TCP_NODELAY
2417 lpcfg_do_global_parameter(lp_ctx, "socket options", "TCP_NODELAY");
2418 #endif
2419 lpcfg_do_global_parameter(lp_ctx, "workgroup", DEFAULT_WORKGROUP);
2420 myname = get_myname(lp_ctx);
2421 lpcfg_do_global_parameter(lp_ctx, "netbios name", myname);
2422 talloc_free(myname);
2423 lpcfg_do_global_parameter(lp_ctx, "name resolve order", "lmhosts wins host bcast");
2425 lpcfg_do_global_parameter(lp_ctx, "fstype", "NTFS");
2427 lpcfg_do_global_parameter(lp_ctx, "ntvfs handler", "unixuid default");
2428 lpcfg_do_global_parameter(lp_ctx, "max connections", "0");
2430 lpcfg_do_global_parameter(lp_ctx, "dcerpc endpoint servers", "epmapper wkssvc rpcecho samr netlogon lsarpc spoolss drsuapi dssetup unixinfo browser eventlog6 backupkey dnsserver");
2431 lpcfg_do_global_parameter(lp_ctx, "server services", "s3fs rpc nbt wrepl ldap cldap kdc drepl winbindd ntp_signd kcc dnsupdate dns");
2432 lpcfg_do_global_parameter(lp_ctx, "kccsrv:samba_kcc", "false");
2433 /* the winbind method for domain controllers is for both RODC
2434 auth forwarding and for trusted domains */
2435 lpcfg_do_global_parameter(lp_ctx, "private dir", dyn_PRIVATE_DIR);
2436 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_LOCAL_MACHINE", "hklm.ldb");
2438 /* This hive should be dynamically generated by Samba using
2439 data from the sam, but for the moment leave it in a tdb to
2440 keep regedt32 from popping up an annoying dialog. */
2441 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_USERS", "hku.ldb");
2443 /* using UTF8 by default allows us to support all chars */
2444 lpcfg_do_global_parameter(lp_ctx, "unix charset", "UTF-8");
2446 /* Use codepage 850 as a default for the dos character set */
2447 lpcfg_do_global_parameter(lp_ctx, "dos charset", "CP850");
2450 * Allow the default PASSWD_CHAT to be overridden in local.h.
2452 lpcfg_do_global_parameter(lp_ctx, "passwd chat", DEFAULT_PASSWD_CHAT);
2454 lpcfg_do_global_parameter(lp_ctx, "pid directory", dyn_PIDDIR);
2455 lpcfg_do_global_parameter(lp_ctx, "lock dir", dyn_LOCKDIR);
2456 lpcfg_do_global_parameter(lp_ctx, "state directory", dyn_STATEDIR);
2457 lpcfg_do_global_parameter(lp_ctx, "cache directory", dyn_CACHEDIR);
2458 lpcfg_do_global_parameter(lp_ctx, "ncalrpc dir", dyn_NCALRPCDIR);
2460 lpcfg_do_global_parameter(lp_ctx, "nbt client socket address", "0.0.0.0");
2461 lpcfg_do_global_parameter_var(lp_ctx, "server string",
2462 "Samba %s", SAMBA_VERSION_STRING);
2464 lpcfg_do_global_parameter(lp_ctx, "password server", "*");
2466 lpcfg_do_global_parameter(lp_ctx, "max mux", "50");
2467 lpcfg_do_global_parameter(lp_ctx, "max xmit", "16644");
2468 lpcfg_do_global_parameter(lp_ctx, "host msdfs", "true");
2470 lpcfg_do_global_parameter(lp_ctx, "LargeReadwrite", "True");
2471 lpcfg_do_global_parameter(lp_ctx, "server min protocol", "LANMAN1");
2472 lpcfg_do_global_parameter(lp_ctx, "server max protocol", "SMB3");
2473 lpcfg_do_global_parameter(lp_ctx, "client min protocol", "CORE");
2474 lpcfg_do_global_parameter(lp_ctx, "client max protocol", "default");
2475 lpcfg_do_global_parameter(lp_ctx, "security", "AUTO");
2476 lpcfg_do_global_parameter(lp_ctx, "EncryptPasswords", "True");
2477 lpcfg_do_global_parameter(lp_ctx, "ReadRaw", "True");
2478 lpcfg_do_global_parameter(lp_ctx, "WriteRaw", "True");
2479 lpcfg_do_global_parameter(lp_ctx, "NullPasswords", "False");
2480 lpcfg_do_global_parameter(lp_ctx, "old password allowed period", "60");
2481 lpcfg_do_global_parameter(lp_ctx, "ObeyPamRestrictions", "False");
2483 lpcfg_do_global_parameter(lp_ctx, "TimeServer", "False");
2484 lpcfg_do_global_parameter(lp_ctx, "BindInterfacesOnly", "False");
2485 lpcfg_do_global_parameter(lp_ctx, "Unicode", "True");
2486 lpcfg_do_global_parameter(lp_ctx, "ClientLanManAuth", "False");
2487 lpcfg_do_global_parameter(lp_ctx, "ClientNTLMv2Auth", "True");
2488 lpcfg_do_global_parameter(lp_ctx, "LanmanAuth", "False");
2489 lpcfg_do_global_parameter(lp_ctx, "NTLMAuth", "True");
2490 lpcfg_do_global_parameter(lp_ctx, "client use spnego principal", "False");
2492 lpcfg_do_global_parameter(lp_ctx, "UnixExtensions", "True");
2494 lpcfg_do_global_parameter(lp_ctx, "PreferredMaster", "Auto");
2495 lpcfg_do_global_parameter(lp_ctx, "LocalMaster", "True");
2497 lpcfg_do_global_parameter(lp_ctx, "wins support", "False");
2498 lpcfg_do_global_parameter(lp_ctx, "dns proxy", "True");
2500 lpcfg_do_global_parameter(lp_ctx, "winbind separator", "\\");
2501 lpcfg_do_global_parameter(lp_ctx, "winbind sealed pipes", "True");
2502 lpcfg_do_global_parameter(lp_ctx, "require strong key", "True");
2503 lpcfg_do_global_parameter(lp_ctx, "winbindd socket directory", dyn_WINBINDD_SOCKET_DIR);
2504 lpcfg_do_global_parameter(lp_ctx, "winbindd privileged socket directory", dyn_WINBINDD_PRIVILEGED_SOCKET_DIR);
2505 lpcfg_do_global_parameter(lp_ctx, "ntp signd socket directory", dyn_NTP_SIGND_SOCKET_DIR);
2506 lpcfg_do_global_parameter_var(lp_ctx, "dns update command", "%s/samba_dnsupdate", dyn_SCRIPTSBINDIR);
2507 lpcfg_do_global_parameter_var(lp_ctx, "spn update command", "%s/samba_spnupdate", dyn_SCRIPTSBINDIR);
2508 lpcfg_do_global_parameter_var(lp_ctx, "samba kcc command",
2509 "%s/samba_kcc", dyn_SCRIPTSBINDIR);
2510 lpcfg_do_global_parameter(lp_ctx, "template shell", "/bin/false");
2511 lpcfg_do_global_parameter(lp_ctx, "template homedir", "/home/%D/%U");
2513 lpcfg_do_global_parameter(lp_ctx, "client signing", "default");
2514 lpcfg_do_global_parameter(lp_ctx, "server signing", "default");
2516 lpcfg_do_global_parameter(lp_ctx, "use spnego", "True");
2518 lpcfg_do_global_parameter(lp_ctx, "use mmap", "True");
2520 lpcfg_do_global_parameter(lp_ctx, "smb ports", "445 139");
2521 lpcfg_do_global_parameter(lp_ctx, "nbt port", "137");
2522 lpcfg_do_global_parameter(lp_ctx, "dgram port", "138");
2523 lpcfg_do_global_parameter(lp_ctx, "cldap port", "389");
2524 lpcfg_do_global_parameter(lp_ctx, "krb5 port", "88");
2525 lpcfg_do_global_parameter(lp_ctx, "kpasswd port", "464");
2526 lpcfg_do_global_parameter(lp_ctx, "web port", "901");
2528 lpcfg_do_global_parameter(lp_ctx, "nt status support", "True");
2530 lpcfg_do_global_parameter(lp_ctx, "max wins ttl", "518400"); /* 6 days */
2531 lpcfg_do_global_parameter(lp_ctx, "min wins ttl", "21600");
2533 lpcfg_do_global_parameter(lp_ctx, "tls enabled", "True");
2534 lpcfg_do_global_parameter(lp_ctx, "tls keyfile", "tls/key.pem");
2535 lpcfg_do_global_parameter(lp_ctx, "tls certfile", "tls/cert.pem");
2536 lpcfg_do_global_parameter(lp_ctx, "tls cafile", "tls/ca.pem");
2537 lpcfg_do_global_parameter(lp_ctx, "prefork children:smb", "4");
2539 lpcfg_do_global_parameter(lp_ctx, "rndc command", "/usr/sbin/rndc");
2540 lpcfg_do_global_parameter(lp_ctx, "nsupdate command", "/usr/bin/nsupdate -g");
2542 lpcfg_do_global_parameter(lp_ctx, "allow dns updates", "secure only");
2543 lpcfg_do_global_parameter(lp_ctx, "dns forwarder", "");
2545 lpcfg_do_global_parameter(lp_ctx, "algorithmic rid base", "1000");
2547 lpcfg_do_global_parameter(lp_ctx, "enhanced browsing", "True");
2549 lpcfg_do_global_parameter(lp_ctx, "winbind nss info", "template");
2551 lpcfg_do_global_parameter(lp_ctx, "server schannel", "Auto");
2553 lpcfg_do_global_parameter(lp_ctx, "short preserve case", "True");
2555 lpcfg_do_global_parameter(lp_ctx, "max open files", "16384");
2557 lpcfg_do_global_parameter(lp_ctx, "cups connection timeout", "30");
2559 lpcfg_do_global_parameter(lp_ctx, "locking", "True");
2561 lpcfg_do_global_parameter(lp_ctx, "block size", "1024");
2563 lpcfg_do_global_parameter(lp_ctx, "client use spnego", "True");
2565 lpcfg_do_global_parameter(lp_ctx, "change notify", "True");
2567 lpcfg_do_global_parameter(lp_ctx, "name cache timeout", "660");
2569 lpcfg_do_global_parameter(lp_ctx, "defer sharing violations", "True");
2571 lpcfg_do_global_parameter(lp_ctx, "ldap replication sleep", "1000");
2573 lpcfg_do_global_parameter(lp_ctx, "idmap backend", "tdb");
2575 lpcfg_do_global_parameter(lp_ctx, "enable privileges", "True");
2577 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max write", "%u", DEFAULT_SMB2_MAX_WRITE);
2579 lpcfg_do_global_parameter(lp_ctx, "passdb backend", "tdbsam");
2581 lpcfg_do_global_parameter(lp_ctx, "getwd cache", "True");
2583 lpcfg_do_global_parameter(lp_ctx, "winbind nested groups", "True");
2585 lpcfg_do_global_parameter(lp_ctx, "mangled names", "True");
2587 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max credits", "%u", DEFAULT_SMB2_MAX_CREDITS);
2589 lpcfg_do_global_parameter(lp_ctx, "ldap ssl", "start tls");
2591 lpcfg_do_global_parameter(lp_ctx, "ldap deref", "auto");
2593 lpcfg_do_global_parameter(lp_ctx, "lm interval", "60");
2595 lpcfg_do_global_parameter(lp_ctx, "mangling method", "hash2");
2597 lpcfg_do_global_parameter(lp_ctx, "hide dot files", "True");
2599 lpcfg_do_global_parameter(lp_ctx, "browse list", "True");
2601 lpcfg_do_global_parameter(lp_ctx, "passwd chat timeout", "2");
2603 lpcfg_do_global_parameter(lp_ctx, "guest account", GUEST_ACCOUNT);
2605 lpcfg_do_global_parameter(lp_ctx, "client schannel", "auto");
2607 lpcfg_do_global_parameter(lp_ctx, "smb encrypt", "default");
2609 lpcfg_do_global_parameter(lp_ctx, "max log size", "5000");
2611 lpcfg_do_global_parameter(lp_ctx, "idmap negative cache time", "120");
2613 lpcfg_do_global_parameter(lp_ctx, "ldap follow referral", "auto");
2615 lpcfg_do_global_parameter(lp_ctx, "multicast dns register", "yes");
2617 lpcfg_do_global_parameter(lp_ctx, "winbind reconnect delay", "30");
2619 lpcfg_do_global_parameter(lp_ctx, "winbind request timeout", "60");
2621 lpcfg_do_global_parameter(lp_ctx, "nt acl support", "yes");
2623 lpcfg_do_global_parameter(lp_ctx, "acl check permissions", "yes");
2625 lpcfg_do_global_parameter(lp_ctx, "keepalive", "300");
2627 lpcfg_do_global_parameter(lp_ctx, "smbd profiling level", "off");
2629 lpcfg_do_global_parameter(lp_ctx, "winbind cache time", "300");
2631 lpcfg_do_global_parameter(lp_ctx, "level2 oplocks", "yes");
2633 lpcfg_do_global_parameter(lp_ctx, "show add printer wizard", "yes");
2635 lpcfg_do_global_parameter(lp_ctx, "allocation roundup size", "1048576");
2637 lpcfg_do_global_parameter(lp_ctx, "ldap page size", "1024");
2639 lpcfg_do_global_parameter(lp_ctx, "kernel share modes", "yes");
2641 lpcfg_do_global_parameter(lp_ctx, "strict locking", "Auto");
2643 lpcfg_do_global_parameter(lp_ctx, "map readonly", "yes");
2645 lpcfg_do_global_parameter(lp_ctx, "allow trusted domains", "yes");
2647 lpcfg_do_global_parameter(lp_ctx, "default devmode", "yes");
2649 lpcfg_do_global_parameter(lp_ctx, "os level", "20");
2651 lpcfg_do_global_parameter(lp_ctx, "dos filetimes", "yes");
2653 lpcfg_do_global_parameter(lp_ctx, "mangling char", "~");
2655 lpcfg_do_global_parameter(lp_ctx, "printcap cache time", "750");
2657 lpcfg_do_global_parameter(lp_ctx, "create krb5 conf", "yes");
2659 lpcfg_do_global_parameter(lp_ctx, "winbind max clients", "200");
2661 lpcfg_do_global_parameter(lp_ctx, "acl map full control", "yes");
2663 lpcfg_do_global_parameter(lp_ctx, "nt pipe support", "yes");
2665 lpcfg_do_global_parameter(lp_ctx, "ldap debug threshold", "10");
2667 lpcfg_do_global_parameter(lp_ctx, "client ldap sasl wrapping", "sign");
2669 lpcfg_do_global_parameter(lp_ctx, "follow symlinks", "yes");
2671 lpcfg_do_global_parameter(lp_ctx, "machine password timeout", "604800");
2673 lpcfg_do_global_parameter(lp_ctx, "ldap connection timeout", "2");
2675 lpcfg_do_global_parameter(lp_ctx, "winbind expand groups", "0");
2677 lpcfg_do_global_parameter(lp_ctx, "stat cache", "yes");
2679 lpcfg_do_global_parameter(lp_ctx, "lpq cache time", "30");
2681 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max trans", "%u", DEFAULT_SMB2_MAX_TRANSACT);
2683 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max read", "%u", DEFAULT_SMB2_MAX_READ);
2685 lpcfg_do_global_parameter(lp_ctx, "durable handles", "yes");
2687 lpcfg_do_global_parameter(lp_ctx, "max stat cache size", "256");
2689 lpcfg_do_global_parameter(lp_ctx, "ldap passwd sync", "no");
2691 lpcfg_do_global_parameter(lp_ctx, "kernel change notify", "yes");
2693 lpcfg_do_global_parameter(lp_ctx, "max ttl", "259200");
2695 lpcfg_do_global_parameter(lp_ctx, "blocking locks", "yes");
2697 lpcfg_do_global_parameter(lp_ctx, "oplock contention limit", "2");
2699 lpcfg_do_global_parameter(lp_ctx, "load printers", "yes");
2701 lpcfg_do_global_parameter(lp_ctx, "idmap cache time", "604800");
2703 lpcfg_do_global_parameter(lp_ctx, "preserve case", "yes");
2705 lpcfg_do_global_parameter(lp_ctx, "lm announce", "auto");
2707 lpcfg_do_global_parameter(lp_ctx, "afs token lifetime", "604800");
2709 lpcfg_do_global_parameter(lp_ctx, "enable core files", "yes");
2711 lpcfg_do_global_parameter(lp_ctx, "winbind max domain connections", "1");
2713 lpcfg_do_global_parameter(lp_ctx, "case sensitive", "auto");
2715 lpcfg_do_global_parameter(lp_ctx, "ldap timeout", "15");
2717 lpcfg_do_global_parameter(lp_ctx, "mangle prefix", "1");
2719 lpcfg_do_global_parameter(lp_ctx, "posix locking", "yes");
2721 lpcfg_do_global_parameter(lp_ctx, "lock spin time", "200");
2723 lpcfg_do_global_parameter(lp_ctx, "directory name cache size", "100");
2725 lpcfg_do_global_parameter(lp_ctx, "nmbd bind explicit broadcast", "yes");
2727 lpcfg_do_global_parameter(lp_ctx, "init logon delay", "100");
2729 lpcfg_do_global_parameter(lp_ctx, "usershare owner only", "yes");
2731 lpcfg_do_global_parameter(lp_ctx, "-valid", "yes");
2733 lpcfg_do_global_parameter_var(lp_ctx, "usershare path", "%s/usershares", get_dyn_STATEDIR());
2735 #ifdef DEVELOPER
2736 lpcfg_do_global_parameter_var(lp_ctx, "panic action", "/bin/sleep 999999999");
2737 #endif
2739 lpcfg_do_global_parameter(lp_ctx, "smb passwd file", get_dyn_SMB_PASSWD_FILE());
2741 lpcfg_do_global_parameter(lp_ctx, "logon home", "\\\\%N\\%U");
2743 lpcfg_do_global_parameter(lp_ctx, "logon path", "\\\\%N\\%U\\profile");
2745 lpcfg_do_global_parameter(lp_ctx, "printjob username", "%U");
2747 for (i = 0; parm_table[i].label; i++) {
2748 if (!(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2749 lp_ctx->flags[i] |= FLAG_DEFAULT;
2753 for (parm=lp_ctx->globals->param_opt; parm; parm=parm->next) {
2754 if (!(parm->priority & FLAG_CMDLINE)) {
2755 parm->priority |= FLAG_DEFAULT;
2759 for (parm=lp_ctx->sDefault->param_opt; parm; parm=parm->next) {
2760 if (!(parm->priority & FLAG_CMDLINE)) {
2761 parm->priority |= FLAG_DEFAULT;
2766 return lp_ctx;
2770 * Initialise the global parameter structure.
2772 struct loadparm_context *loadparm_init_global(bool load_default)
2774 if (global_loadparm_context == NULL) {
2775 global_loadparm_context = loadparm_init(NULL);
2777 if (global_loadparm_context == NULL) {
2778 return NULL;
2780 global_loadparm_context->global = true;
2781 if (load_default && !global_loadparm_context->loaded) {
2782 lpcfg_load_default(global_loadparm_context);
2784 global_loadparm_context->refuse_free = true;
2785 return global_loadparm_context;
2789 * Initialise the global parameter structure.
2791 struct loadparm_context *loadparm_init_s3(TALLOC_CTX *mem_ctx,
2792 const struct loadparm_s3_helpers *s3_fns)
2794 struct loadparm_context *loadparm_context = talloc_zero(mem_ctx, struct loadparm_context);
2795 if (!loadparm_context) {
2796 return NULL;
2798 loadparm_context->s3_fns = s3_fns;
2799 loadparm_context->globals = s3_fns->globals;
2800 loadparm_context->flags = s3_fns->flags;
2802 return loadparm_context;
2805 const char *lpcfg_configfile(struct loadparm_context *lp_ctx)
2807 return lp_ctx->szConfigFile;
2810 const char *lp_default_path(void)
2812 if (getenv("SMB_CONF_PATH"))
2813 return getenv("SMB_CONF_PATH");
2814 else
2815 return dyn_CONFIGFILE;
2819 * Update the internal state of a loadparm context after settings
2820 * have changed.
2822 static bool lpcfg_update(struct loadparm_context *lp_ctx)
2824 struct debug_settings settings;
2825 TALLOC_CTX *tmp_ctx;
2827 tmp_ctx = talloc_new(lp_ctx);
2828 if (tmp_ctx == NULL) {
2829 return false;
2832 lpcfg_add_auto_services(lp_ctx, lpcfg_auto_services(lp_ctx, tmp_ctx));
2834 if (!lp_ctx->globals->wins_server_list && lp_ctx->globals->we_are_a_wins_server) {
2835 lpcfg_do_global_parameter(lp_ctx, "wins server", "127.0.0.1");
2838 if (!lp_ctx->global) {
2839 TALLOC_FREE(tmp_ctx);
2840 return true;
2843 panic_action = lp_ctx->globals->panic_action;
2845 reload_charcnv(lp_ctx);
2847 ZERO_STRUCT(settings);
2848 /* Add any more debug-related smb.conf parameters created in
2849 * future here */
2850 settings.syslog = lp_ctx->globals->syslog;
2851 settings.syslog_only = lp_ctx->globals->syslog_only;
2852 settings.timestamp_logs = lp_ctx->globals->timestamp_logs;
2853 settings.debug_prefix_timestamp = lp_ctx->globals->debug_prefix_timestamp;
2854 settings.debug_hires_timestamp = lp_ctx->globals->debug_hires_timestamp;
2855 settings.debug_pid = lp_ctx->globals->debug_pid;
2856 settings.debug_uid = lp_ctx->globals->debug_uid;
2857 settings.debug_class = lp_ctx->globals->debug_class;
2858 debug_set_settings(&settings);
2860 /* FIXME: This is a bit of a hack, but we can't use a global, since
2861 * not everything that uses lp also uses the socket library */
2862 if (lpcfg_parm_bool(lp_ctx, NULL, "socket", "testnonblock", false)) {
2863 setenv("SOCKET_TESTNONBLOCK", "1", 1);
2864 } else {
2865 unsetenv("SOCKET_TESTNONBLOCK");
2868 TALLOC_FREE(tmp_ctx);
2869 return true;
2872 bool lpcfg_load_default(struct loadparm_context *lp_ctx)
2874 const char *path;
2876 path = lp_default_path();
2878 if (!file_exist(path)) {
2879 /* We allow the default smb.conf file to not exist,
2880 * basically the equivalent of an empty file. */
2881 return lpcfg_update(lp_ctx);
2884 return lpcfg_load(lp_ctx, path);
2888 * Load the services array from the services file.
2890 * Return True on success, False on failure.
2892 bool lpcfg_load(struct loadparm_context *lp_ctx, const char *filename)
2894 char *n2;
2895 bool bRetval;
2897 filename = talloc_strdup(lp_ctx, filename);
2899 lp_ctx->szConfigFile = filename;
2901 if (lp_ctx->s3_fns) {
2902 return lp_ctx->s3_fns->load(filename);
2905 lp_ctx->bInGlobalSection = true;
2906 n2 = standard_sub_basic(lp_ctx, lp_ctx->szConfigFile);
2907 DEBUG(2, ("lpcfg_load: refreshing parameters from %s\n", n2));
2909 add_to_file_list(lp_ctx, &lp_ctx->file_lists, lp_ctx->szConfigFile, n2);
2911 /* We get sections first, so have to start 'behind' to make up */
2912 lp_ctx->currentService = NULL;
2913 bRetval = pm_process(n2, do_section, lpcfg_do_parameter, lp_ctx);
2915 /* finish up the last section */
2916 DEBUG(4, ("pm_process() returned %s\n", BOOLSTR(bRetval)));
2917 if (bRetval)
2918 if (lp_ctx->currentService != NULL)
2919 bRetval = lpcfg_service_ok(lp_ctx->currentService);
2921 bRetval = bRetval && lpcfg_update(lp_ctx);
2923 /* we do this unconditionally, so that it happens even
2924 for a missing smb.conf */
2925 reload_charcnv(lp_ctx);
2927 if (bRetval == true) {
2928 /* set this up so that any child python tasks will
2929 find the right smb.conf */
2930 setenv("SMB_CONF_PATH", filename, 1);
2932 /* set the context used by the lp_*() function
2933 varients */
2934 global_loadparm_context = lp_ctx;
2935 lp_ctx->loaded = true;
2938 return bRetval;
2942 * Return the max number of services.
2945 int lpcfg_numservices(struct loadparm_context *lp_ctx)
2947 if (lp_ctx->s3_fns) {
2948 return lp_ctx->s3_fns->get_numservices();
2951 return lp_ctx->iNumServices;
2955 * Display the contents of the services array in human-readable form.
2958 void lpcfg_dump(struct loadparm_context *lp_ctx, FILE *f, bool show_defaults,
2959 int maxtoprint)
2961 int iService;
2963 if (lp_ctx->s3_fns) {
2964 lp_ctx->s3_fns->dump(f, show_defaults, maxtoprint);
2965 return;
2968 lpcfg_dump_globals(lp_ctx, f, show_defaults);
2970 lpcfg_dump_a_service(lp_ctx->sDefault, lp_ctx->sDefault, f, lp_ctx->flags, show_defaults);
2972 for (iService = 0; iService < maxtoprint; iService++)
2973 lpcfg_dump_one(f, show_defaults, lp_ctx->services[iService], lp_ctx->sDefault);
2977 * Display the contents of one service in human-readable form.
2979 void lpcfg_dump_one(FILE *f, bool show_defaults, struct loadparm_service *service, struct loadparm_service *sDefault)
2981 if (service != NULL) {
2982 if (service->szService[0] == '\0')
2983 return;
2984 lpcfg_dump_a_service(service, sDefault, f, NULL, show_defaults);
2988 struct loadparm_service *lpcfg_servicebynum(struct loadparm_context *lp_ctx,
2989 int snum)
2991 if (lp_ctx->s3_fns) {
2992 return lp_ctx->s3_fns->get_servicebynum(snum);
2995 return lp_ctx->services[snum];
2998 struct loadparm_service *lpcfg_service(struct loadparm_context *lp_ctx,
2999 const char *service_name)
3001 int iService;
3002 char *serviceName;
3004 if (lp_ctx->s3_fns) {
3005 return lp_ctx->s3_fns->get_service(service_name);
3008 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--) {
3009 if (lp_ctx->services[iService] &&
3010 lp_ctx->services[iService]->szService) {
3012 * The substitution here is used to support %U is
3013 * service names
3015 serviceName = standard_sub_basic(
3016 lp_ctx->services[iService],
3017 lp_ctx->services[iService]->szService);
3018 if (strequal(serviceName, service_name)) {
3019 talloc_free(serviceName);
3020 return lp_ctx->services[iService];
3022 talloc_free(serviceName);
3026 DEBUG(7,("lpcfg_servicenumber: couldn't find %s\n", service_name));
3027 return NULL;
3030 const char *lpcfg_servicename(const struct loadparm_service *service)
3032 return lpcfg_string((const char *)service->szService);
3036 * A useful volume label function.
3038 const char *lpcfg_volume_label(struct loadparm_service *service, struct loadparm_service *sDefault)
3040 const char *ret;
3041 ret = lpcfg_string((const char *)((service != NULL && service->volume != NULL) ?
3042 service->volume : sDefault->volume));
3043 if (!*ret)
3044 return lpcfg_servicename(service);
3045 return ret;
3049 * Return the correct printer name.
3051 const char *lpcfg_printername(struct loadparm_service *service, struct loadparm_service *sDefault)
3053 const char *ret;
3054 ret = lpcfg_string((const char *)((service != NULL && service->_printername != NULL) ?
3055 service->_printername : sDefault->_printername));
3056 if (ret == NULL || (ret != NULL && *ret == '\0'))
3057 ret = lpcfg_servicename(service);
3059 return ret;
3064 * Return the max print jobs per queue.
3066 int lpcfg_maxprintjobs(struct loadparm_service *service, struct loadparm_service *sDefault)
3068 int maxjobs = (service != NULL) ? service->iMaxPrintJobs : sDefault->iMaxPrintJobs;
3069 if (maxjobs <= 0 || maxjobs >= PRINT_MAX_JOBID)
3070 maxjobs = PRINT_MAX_JOBID - 1;
3072 return maxjobs;
3075 struct smb_iconv_handle *lpcfg_iconv_handle(struct loadparm_context *lp_ctx)
3077 if (lp_ctx == NULL) {
3078 return get_iconv_handle();
3080 return lp_ctx->iconv_handle;
3083 _PUBLIC_ void reload_charcnv(struct loadparm_context *lp_ctx)
3085 struct smb_iconv_handle *old_ic = lp_ctx->iconv_handle;
3086 if (!lp_ctx->global) {
3087 return;
3090 if (old_ic == NULL) {
3091 old_ic = global_iconv_handle;
3093 lp_ctx->iconv_handle = smb_iconv_handle_reinit_lp(lp_ctx, lp_ctx, old_ic);
3094 global_iconv_handle = lp_ctx->iconv_handle;
3097 _PUBLIC_ char *lpcfg_tls_keyfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3099 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_keyfile(lp_ctx));
3102 _PUBLIC_ char *lpcfg_tls_certfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3104 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_certfile(lp_ctx));
3107 _PUBLIC_ char *lpcfg_tls_cafile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3109 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_cafile(lp_ctx));
3112 _PUBLIC_ char *lpcfg_tls_crlfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3114 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_crlfile(lp_ctx));
3117 _PUBLIC_ char *lpcfg_tls_dhpfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3119 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_dhpfile(lp_ctx));
3122 struct gensec_settings *lpcfg_gensec_settings(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3124 struct gensec_settings *settings = talloc_zero(mem_ctx, struct gensec_settings);
3125 if (settings == NULL)
3126 return NULL;
3127 SMB_ASSERT(lp_ctx != NULL);
3128 settings->lp_ctx = talloc_reference(settings, lp_ctx);
3129 settings->target_hostname = lpcfg_parm_string(lp_ctx, NULL, "gensec", "target_hostname");
3130 return settings;
3133 int lpcfg_server_role(struct loadparm_context *lp_ctx)
3135 int domain_master = lpcfg__domain_master(lp_ctx);
3137 return lp_find_server_role(lpcfg__server_role(lp_ctx),
3138 lpcfg__security(lp_ctx),
3139 lpcfg__domain_logons(lp_ctx),
3140 (domain_master == true) ||
3141 (domain_master == Auto));
3144 int lpcfg_security(struct loadparm_context *lp_ctx)
3146 return lp_find_security(lpcfg__server_role(lp_ctx),
3147 lpcfg__security(lp_ctx));
3150 int lpcfg_client_max_protocol(struct loadparm_context *lp_ctx)
3152 int client_max_protocol = lpcfg__client_max_protocol(lp_ctx);
3153 if (client_max_protocol == PROTOCOL_DEFAULT) {
3154 return PROTOCOL_NT1;
3156 return client_max_protocol;
3159 bool lpcfg_server_signing_allowed(struct loadparm_context *lp_ctx, bool *mandatory)
3161 bool allowed = true;
3162 enum smb_signing_setting signing_setting = lpcfg_server_signing(lp_ctx);
3164 *mandatory = false;
3166 if (signing_setting == SMB_SIGNING_DEFAULT) {
3168 * If we are a domain controller, SMB signing is
3169 * really important, as it can prevent a number of
3170 * attacks on communications between us and the
3171 * clients
3173 * However, it really sucks (no sendfile, CPU
3174 * overhead) performance-wise when used on a
3175 * file server, so disable it by default
3176 * on non-DCs
3179 if (lpcfg_server_role(lp_ctx) >= ROLE_ACTIVE_DIRECTORY_DC) {
3180 signing_setting = SMB_SIGNING_REQUIRED;
3181 } else {
3182 signing_setting = SMB_SIGNING_OFF;
3186 switch (signing_setting) {
3187 case SMB_SIGNING_REQUIRED:
3188 *mandatory = true;
3189 break;
3190 case SMB_SIGNING_IF_REQUIRED:
3191 break;
3192 case SMB_SIGNING_DEFAULT:
3193 case SMB_SIGNING_OFF:
3194 allowed = false;
3195 break;
3198 return allowed;
3201 int lpcfg_tdb_hash_size(struct loadparm_context *lp_ctx, const char *name)
3203 const char *base;
3205 if (name == NULL) {
3206 return 0;
3209 base = strrchr_m(name, '/');
3210 if (base != NULL) {
3211 base += 1;
3212 } else {
3213 base = name;
3215 return lpcfg_parm_int(lp_ctx, NULL, "tdb_hashsize", base, 0);
3219 int lpcfg_tdb_flags(struct loadparm_context *lp_ctx, int tdb_flags)
3221 if (!lpcfg_use_mmap(lp_ctx)) {
3222 tdb_flags |= TDB_NOMMAP;
3224 return tdb_flags;