param: add missing talloc_free in lpcfg_file_list_changed which are present in s3
[Samba.git] / lib / param / loadparm.c
blobd975359a4b41f8b16201b99697e6a14eea8a5ede
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 if (lp_ctx->s3_fns) {
78 return lp_ctx->s3_fns->get_default_loadparm_service();
80 return lp_ctx->sDefault;
83 /**
84 * Convenience routine to grab string parameters into temporary memory
85 * and run standard_sub_basic on them.
87 * The buffers can be written to by
88 * callers without affecting the source string.
91 static const char *lpcfg_string(const char *s)
93 #if 0 /* until REWRITE done to make thread-safe */
94 size_t len = s ? strlen(s) : 0;
95 char *ret;
96 #endif
98 /* The follow debug is useful for tracking down memory problems
99 especially if you have an inner loop that is calling a lp_*()
100 function that returns a string. Perhaps this debug should be
101 present all the time? */
103 #if 0
104 DEBUG(10, ("lpcfg_string(%s)\n", s));
105 #endif
107 #if 0 /* until REWRITE done to make thread-safe */
108 if (!lp_talloc)
109 lp_talloc = talloc_init("lp_talloc");
111 ret = talloc_array(lp_talloc, char, len + 100); /* leave room for substitution */
113 if (!ret)
114 return NULL;
116 if (!s)
117 *ret = 0;
118 else
119 strlcpy(ret, s, len);
121 if (trim_string(ret, "\"", "\"")) {
122 if (strchr(ret,'"') != NULL)
123 strlcpy(ret, s, len);
126 standard_sub_basic(ret,len+100);
127 return (ret);
128 #endif
129 return s;
133 In this section all the functions that are used to access the
134 parameters from the rest of the program are defined
138 * the creation of separate lpcfg_*() and lp_*() functions is to allow
139 * for code compatibility between existing Samba4 and Samba3 code.
142 /* this global context supports the lp_*() function varients */
143 static struct loadparm_context *global_loadparm_context;
145 #define lpcfg_default_service global_loadparm_context->sDefault
146 #define lpcfg_global_service(i) global_loadparm_context->services[i]
148 #define FN_GLOBAL_STRING(fn_name,var_name) \
149 _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx, TALLOC_CTX *ctx) {\
150 if (lp_ctx == NULL) return NULL; \
151 if (lp_ctx->s3_fns) { \
152 return lp_ctx->globals->var_name ? lp_ctx->s3_fns->lp_string(ctx, lp_ctx->globals->var_name) : talloc_strdup(ctx, ""); \
154 return lp_ctx->globals->var_name ? talloc_strdup(ctx, lpcfg_string(lp_ctx->globals->var_name)) : talloc_strdup(ctx, ""); \
157 #define FN_GLOBAL_CONST_STRING(fn_name,var_name) \
158 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
159 if (lp_ctx == NULL) return NULL; \
160 return lp_ctx->globals->var_name ? lpcfg_string(lp_ctx->globals->var_name) : ""; \
163 #define FN_GLOBAL_LIST(fn_name,var_name) \
164 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
165 if (lp_ctx == NULL) return NULL; \
166 return lp_ctx->globals->var_name; \
169 #define FN_GLOBAL_BOOL(fn_name,var_name) \
170 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) {\
171 if (lp_ctx == NULL) return false; \
172 return lp_ctx->globals->var_name; \
175 #define FN_GLOBAL_INTEGER(fn_name,var_name) \
176 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
177 return lp_ctx->globals->var_name; \
180 /* Local parameters don't need the ->s3_fns because the struct
181 * loadparm_service is shared and lpcfg_service() checks the ->s3_fns
182 * hook */
183 #define FN_LOCAL_STRING(fn_name,val) \
184 _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_service *service, \
185 struct loadparm_service *sDefault, TALLOC_CTX *ctx) { \
186 return(talloc_strdup(ctx, lpcfg_string((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)))); \
189 #define FN_LOCAL_CONST_STRING(fn_name,val) \
190 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_service *service, \
191 struct loadparm_service *sDefault) { \
192 return((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)); \
195 #define FN_LOCAL_LIST(fn_name,val) \
196 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_service *service, \
197 struct loadparm_service *sDefault) {\
198 return(const char **)(service != NULL && service->val != NULL? service->val : sDefault->val); \
201 #define FN_LOCAL_PARM_BOOL(fn_name, val) FN_LOCAL_BOOL(fn_name, val)
203 #define FN_LOCAL_BOOL(fn_name,val) \
204 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_service *service, \
205 struct loadparm_service *sDefault) { \
206 return((service != NULL)? service->val : sDefault->val); \
209 #define FN_LOCAL_INTEGER(fn_name,val) \
210 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_service *service, \
211 struct loadparm_service *sDefault) { \
212 return((service != NULL)? service->val : sDefault->val); \
215 #define FN_LOCAL_PARM_INTEGER(fn_name, val) FN_LOCAL_INTEGER(fn_name, val)
217 #define FN_LOCAL_PARM_CHAR(fn_name,val) \
218 _PUBLIC_ char lpcfg_ ## fn_name(struct loadparm_service *service, \
219 struct loadparm_service *sDefault) { \
220 return((service != NULL)? service->val : sDefault->val); \
223 #include "lib/param/param_functions.c"
225 /* These functions cannot be auto-generated */
226 FN_LOCAL_BOOL(autoloaded, autoloaded)
227 FN_GLOBAL_CONST_STRING(dnsdomain, dnsdomain)
229 /* local prototypes */
230 static struct loadparm_service *lpcfg_getservicebyname(struct loadparm_context *lp_ctx,
231 const char *pszServiceName);
232 static bool do_section(const char *pszSectionName, void *);
233 static bool set_variable_helper(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
234 const char *pszParmName, const char *pszParmValue);
235 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
236 struct loadparm_service *service,
237 const char *pszParmName,
238 const char *pszParmValue, int flags);
240 /* The following are helper functions for parametrical options support. */
241 /* It returns a pointer to parametrical option value if it exists or NULL otherwise */
242 /* Actual parametrical functions are quite simple */
243 struct parmlist_entry *get_parametric_helper(struct loadparm_service *service,
244 const char *type, const char *option,
245 struct parmlist_entry *global_opts)
247 char* param_key;
248 struct parmlist_entry *data = NULL;
249 TALLOC_CTX *mem_ctx = talloc_stackframe();
251 param_key = talloc_asprintf(mem_ctx, "%s:%s", type, option);
252 if (param_key == NULL) {
253 DEBUG(0,("asprintf failed!\n"));
254 TALLOC_FREE(mem_ctx);
255 return NULL;
259 * Try to fetch the option from the data.
261 if (service != NULL) {
262 data = service->param_opt;
263 while (data != NULL) {
264 if (strwicmp(data->key, param_key) == 0) {
265 TALLOC_FREE(mem_ctx);
266 return data;
268 data = data->next;
273 * Fall back to fetching from the globals.
275 data = global_opts;
276 while (data != NULL) {
277 if (strwicmp(data->key, param_key) == 0) {
278 TALLOC_FREE(mem_ctx);
279 return data;
281 data = data->next;
285 TALLOC_FREE(mem_ctx);
287 return NULL;
292 const char *lpcfg_get_parametric(struct loadparm_context *lp_ctx,
293 struct loadparm_service *service,
294 const char *type, const char *option)
296 struct parmlist_entry *data;
298 if (lp_ctx == NULL)
299 return NULL;
301 data = get_parametric_helper(service,
302 type, option, lp_ctx->globals->param_opt);
304 if (data == NULL) {
305 return NULL;
306 } else {
307 return data->value;
313 * convenience routine to return int parameters.
315 int lp_int(const char *s)
318 if (!s || !*s) {
319 DEBUG(0,("lp_int(%s): is called with NULL!\n",s));
320 return -1;
323 return strtol(s, NULL, 0);
327 * convenience routine to return unsigned long parameters.
329 unsigned long lp_ulong(const char *s)
332 if (!s || !*s) {
333 DEBUG(0,("lp_ulong(%s): is called with NULL!\n",s));
334 return -1;
337 return strtoul(s, NULL, 0);
341 * convenience routine to return unsigned long parameters.
343 static long lp_long(const char *s)
346 if (!s) {
347 DEBUG(0,("lp_long(%s): is called with NULL!\n",s));
348 return -1;
351 return strtol(s, NULL, 0);
355 * convenience routine to return unsigned long parameters.
357 static double lp_double(const char *s)
360 if (!s) {
361 DEBUG(0,("lp_double(%s): is called with NULL!\n",s));
362 return -1;
365 return strtod(s, NULL);
369 * convenience routine to return boolean parameters.
371 bool lp_bool(const char *s)
373 bool ret = false;
375 if (!s || !*s) {
376 DEBUG(0,("lp_bool(%s): is called with NULL!\n",s));
377 return false;
380 if (!set_boolean(s, &ret)) {
381 DEBUG(0,("lp_bool(%s): value is not boolean!\n",s));
382 return false;
385 return ret;
389 * Return parametric option from a given service. Type is a part of option before ':'
390 * Parametric option has following syntax: 'Type: option = value'
391 * Returned value is allocated in 'lp_talloc' context
394 const char *lpcfg_parm_string(struct loadparm_context *lp_ctx,
395 struct loadparm_service *service, const char *type,
396 const char *option)
398 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
400 if (value)
401 return lpcfg_string(value);
403 return NULL;
407 * Return parametric option from a given service. Type is a part of option before ':'
408 * Parametric option has following syntax: 'Type: option = value'
409 * Returned value is allocated in 'lp_talloc' context
412 const char **lpcfg_parm_string_list(TALLOC_CTX *mem_ctx,
413 struct loadparm_context *lp_ctx,
414 struct loadparm_service *service,
415 const char *type,
416 const char *option, const char *separator)
418 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
420 if (value != NULL)
421 return (const char **)str_list_make(mem_ctx, value, separator);
423 return NULL;
427 * Return parametric option from a given service. Type is a part of option before ':'
428 * Parametric option has following syntax: 'Type: option = value'
431 int lpcfg_parm_int(struct loadparm_context *lp_ctx,
432 struct loadparm_service *service, const char *type,
433 const char *option, int default_v)
435 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
437 if (value)
438 return lp_int(value);
440 return default_v;
444 * Return parametric option from a given service. Type is a part of
445 * option before ':'.
446 * Parametric option has following syntax: 'Type: option = value'.
449 int lpcfg_parm_bytes(struct loadparm_context *lp_ctx,
450 struct loadparm_service *service, const char *type,
451 const char *option, int default_v)
453 uint64_t bval;
455 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
457 if (value && conv_str_size_error(value, &bval)) {
458 if (bval <= INT_MAX) {
459 return (int)bval;
463 return default_v;
467 * Return parametric option from a given service.
468 * Type is a part of option before ':'
469 * Parametric option has following syntax: 'Type: option = value'
471 unsigned long lpcfg_parm_ulong(struct loadparm_context *lp_ctx,
472 struct loadparm_service *service, const char *type,
473 const char *option, unsigned long default_v)
475 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
477 if (value)
478 return lp_ulong(value);
480 return default_v;
483 long lpcfg_parm_long(struct loadparm_context *lp_ctx,
484 struct loadparm_service *service, const char *type,
485 const char *option, long default_v)
487 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
489 if (value)
490 return lp_long(value);
492 return default_v;
495 double lpcfg_parm_double(struct loadparm_context *lp_ctx,
496 struct loadparm_service *service, const char *type,
497 const char *option, double default_v)
499 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
501 if (value != NULL)
502 return lp_double(value);
504 return default_v;
508 * Return parametric option from a given service. Type is a part of option before ':'
509 * Parametric option has following syntax: 'Type: option = value'
512 bool lpcfg_parm_bool(struct loadparm_context *lp_ctx,
513 struct loadparm_service *service, const char *type,
514 const char *option, bool default_v)
516 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
518 if (value != NULL)
519 return lp_bool(value);
521 return default_v;
526 * Set a string value, deallocating any existing space, and allocing the space
527 * for the string
529 bool lpcfg_string_set(TALLOC_CTX *mem_ctx, char **dest, const char *src)
531 talloc_free(*dest);
533 if (src == NULL)
534 src = "";
536 *dest = talloc_strdup(mem_ctx, src);
537 if ((*dest) == NULL) {
538 DEBUG(0,("Out of memory in string_set\n"));
539 return false;
542 return true;
546 * Set a string value, deallocating any existing space, and allocing the space
547 * for the string
549 bool lpcfg_string_set_upper(TALLOC_CTX *mem_ctx, char **dest, const char *src)
551 talloc_free(*dest);
553 if (src == NULL)
554 src = "";
556 *dest = strupper_talloc(mem_ctx, src);
557 if ((*dest) == NULL) {
558 DEBUG(0,("Out of memory in string_set_upper\n"));
559 return false;
562 return true;
568 * Add a new service to the services array initialising it with the given
569 * service.
572 struct loadparm_service *lpcfg_add_service(struct loadparm_context *lp_ctx,
573 const struct loadparm_service *pservice,
574 const char *name)
576 int i;
577 int num_to_alloc = lp_ctx->iNumServices + 1;
578 struct parmlist_entry *data, *pdata;
580 if (pservice == NULL) {
581 pservice = lp_ctx->sDefault;
584 /* it might already exist */
585 if (name) {
586 struct loadparm_service *service = lpcfg_getservicebyname(lp_ctx,
587 name);
588 if (service != NULL) {
589 /* Clean all parametric options for service */
590 /* They will be added during parsing again */
591 data = service->param_opt;
592 while (data) {
593 pdata = data->next;
594 talloc_free(data);
595 data = pdata;
597 service->param_opt = NULL;
598 return service;
602 /* find an invalid one */
603 for (i = 0; i < lp_ctx->iNumServices; i++)
604 if (lp_ctx->services[i] == NULL)
605 break;
607 /* if not, then create one */
608 if (i == lp_ctx->iNumServices) {
609 struct loadparm_service **tsp;
611 tsp = talloc_realloc(lp_ctx, lp_ctx->services, struct loadparm_service *, num_to_alloc);
613 if (!tsp) {
614 DEBUG(0,("lpcfg_add_service: failed to enlarge services!\n"));
615 return NULL;
616 } else {
617 lp_ctx->services = tsp;
618 lp_ctx->services[lp_ctx->iNumServices] = NULL;
621 lp_ctx->iNumServices++;
624 lp_ctx->services[i] = talloc_zero(lp_ctx->services, struct loadparm_service);
625 if (lp_ctx->services[i] == NULL) {
626 DEBUG(0,("lpcfg_add_service: out of memory!\n"));
627 return NULL;
629 copy_service(lp_ctx->services[i], pservice, NULL);
630 if (name != NULL)
631 lpcfg_string_set(lp_ctx->services[i], &lp_ctx->services[i]->szService, name);
632 return lp_ctx->services[i];
636 * Add a new home service, with the specified home directory, defaults coming
637 * from service ifrom.
640 bool lpcfg_add_home(struct loadparm_context *lp_ctx,
641 const char *pszHomename,
642 struct loadparm_service *default_service,
643 const char *user, const char *pszHomedir)
645 struct loadparm_service *service;
647 service = lpcfg_add_service(lp_ctx, default_service, pszHomename);
649 if (service == NULL)
650 return false;
652 if (!(*(default_service->path))
653 || strequal(default_service->path, lp_ctx->sDefault->path)) {
654 service->path = talloc_strdup(service, pszHomedir);
655 } else {
656 service->path = string_sub_talloc(service, lpcfg_path(default_service, lp_ctx->sDefault, service), "%H", pszHomedir);
659 if (!(*(service->comment))) {
660 service->comment = talloc_asprintf(service, "Home directory of %s", user);
662 service->bAvailable = default_service->bAvailable;
663 service->browseable = default_service->browseable;
665 DEBUG(3, ("adding home's share [%s] for user '%s' at '%s'\n",
666 pszHomename, user, service->path));
668 return true;
672 * Add a new printer service, with defaults coming from service iFrom.
675 bool lpcfg_add_printer(struct loadparm_context *lp_ctx,
676 const char *pszPrintername,
677 struct loadparm_service *default_service)
679 const char *comment = "From Printcap";
680 struct loadparm_service *service;
681 service = lpcfg_add_service(lp_ctx, default_service, pszPrintername);
683 if (service == NULL)
684 return false;
686 /* note that we do NOT default the availability flag to True - */
687 /* we take it from the default service passed. This allows all */
688 /* dynamic printers to be disabled by disabling the [printers] */
689 /* entry (if/when the 'available' keyword is implemented!). */
691 /* the printer name is set to the service name. */
692 lpcfg_string_set(service, &service->_printername, pszPrintername);
693 lpcfg_string_set(service, &service->comment, comment);
694 service->browseable = default_service->browseable;
695 /* Printers cannot be read_only. */
696 service->read_only = false;
697 /* Printer services must be printable. */
698 service->printable = true;
700 DEBUG(3, ("adding printer service %s\n", pszPrintername));
702 return true;
706 * Map a parameter's string representation to something we can use.
707 * Returns False if the parameter string is not recognised, else TRUE.
710 int lpcfg_map_parameter(const char *pszParmName)
712 int iIndex;
714 for (iIndex = 0; parm_table[iIndex].label; iIndex++)
715 if (strwicmp(parm_table[iIndex].label, pszParmName) == 0)
716 return iIndex;
718 /* Warn only if it isn't parametric option */
719 if (strchr(pszParmName, ':') == NULL)
720 DEBUG(0, ("Unknown parameter encountered: \"%s\"\n", pszParmName));
721 /* We do return 'fail' for parametric options as well because they are
722 stored in different storage
724 return -1;
729 return the parameter structure for a parameter
731 struct parm_struct *lpcfg_parm_struct(struct loadparm_context *lp_ctx, const char *name)
733 int parmnum;
735 if (lp_ctx->s3_fns) {
736 return lp_ctx->s3_fns->get_parm_struct(name);
739 parmnum = lpcfg_map_parameter(name);
740 if (parmnum == -1) return NULL;
741 return &parm_table[parmnum];
745 return the parameter pointer for a parameter
747 void *lpcfg_parm_ptr(struct loadparm_context *lp_ctx,
748 struct loadparm_service *service, struct parm_struct *parm)
750 if (lp_ctx->s3_fns) {
751 return lp_ctx->s3_fns->get_parm_ptr(service, parm);
754 if (service == NULL) {
755 if (parm->p_class == P_LOCAL)
756 return ((char *)lp_ctx->sDefault)+parm->offset;
757 else if (parm->p_class == P_GLOBAL)
758 return ((char *)lp_ctx->globals)+parm->offset;
759 else return NULL;
760 } else {
761 return ((char *)service) + parm->offset;
766 return the parameter pointer for a parameter
768 bool lpcfg_parm_is_cmdline(struct loadparm_context *lp_ctx, const char *name)
770 int parmnum;
772 parmnum = lpcfg_map_parameter(name);
773 if (parmnum == -1) return false;
775 return lp_ctx->flags[parmnum] & FLAG_CMDLINE;
779 * Find a service by name. Otherwise works like get_service.
782 static struct loadparm_service *lpcfg_getservicebyname(struct loadparm_context *lp_ctx,
783 const char *pszServiceName)
785 int iService;
787 if (lp_ctx->s3_fns) {
788 return lp_ctx->s3_fns->get_service(pszServiceName);
791 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--)
792 if (lp_ctx->services[iService] != NULL &&
793 strwicmp(lp_ctx->services[iService]->szService, pszServiceName) == 0) {
794 return lp_ctx->services[iService];
797 return NULL;
801 * Add a parametric option to a parmlist_entry,
802 * replacing old value, if already present.
804 void set_param_opt(TALLOC_CTX *mem_ctx,
805 struct parmlist_entry **opt_list,
806 const char *opt_name,
807 const char *opt_value,
808 unsigned priority)
810 struct parmlist_entry *new_opt, *opt;
811 bool not_added;
813 opt = *opt_list;
814 not_added = true;
816 /* Traverse destination */
817 while (opt) {
818 /* If we already have same option, override it */
819 if (strwicmp(opt->key, opt_name) == 0) {
820 if ((opt->priority & FLAG_CMDLINE) &&
821 !(priority & FLAG_CMDLINE)) {
822 /* it's been marked as not to be
823 overridden */
824 return;
826 TALLOC_FREE(opt->value);
827 TALLOC_FREE(opt->list);
828 opt->value = talloc_strdup(opt, opt_value);
829 opt->priority = priority;
830 not_added = false;
831 break;
833 opt = opt->next;
835 if (not_added) {
836 new_opt = talloc(mem_ctx, struct parmlist_entry);
837 if (new_opt == NULL) {
838 smb_panic("OOM");
841 new_opt->key = talloc_strdup(new_opt, opt_name);
842 if (new_opt->key == NULL) {
843 smb_panic("talloc_strdup failed");
846 new_opt->value = talloc_strdup(new_opt, opt_value);
847 if (new_opt->value == NULL) {
848 smb_panic("talloc_strdup failed");
851 new_opt->list = NULL;
852 new_opt->priority = priority;
853 DLIST_ADD(*opt_list, new_opt);
858 * Copy a service structure to another.
859 * If pcopymapDest is NULL then copy all fields
862 void copy_service(struct loadparm_service *pserviceDest,
863 const struct loadparm_service *pserviceSource,
864 struct bitmap *pcopymapDest)
866 int i;
867 bool bcopyall = (pcopymapDest == NULL);
868 struct parmlist_entry *data;
870 for (i = 0; parm_table[i].label; i++)
871 if (parm_table[i].p_class == P_LOCAL &&
872 (bcopyall || bitmap_query(pcopymapDest, i))) {
873 const void *src_ptr =
874 ((const char *)pserviceSource) + parm_table[i].offset;
875 void *dest_ptr =
876 ((char *)pserviceDest) + parm_table[i].offset;
878 switch (parm_table[i].type) {
879 case P_BOOL:
880 case P_BOOLREV:
881 *(bool *)dest_ptr = *(const bool *)src_ptr;
882 break;
884 case P_INTEGER:
885 case P_BYTES:
886 case P_OCTAL:
887 case P_ENUM:
888 *(int *)dest_ptr = *(const int *)src_ptr;
889 break;
891 case P_CHAR:
892 *(char *)dest_ptr = *(const char *)src_ptr;
893 break;
895 case P_STRING:
896 lpcfg_string_set(pserviceDest,
897 (char **)dest_ptr,
898 *(const char * const *)src_ptr);
899 break;
901 case P_USTRING:
902 lpcfg_string_set_upper(pserviceDest,
903 (char **)dest_ptr,
904 *(const char * const *)src_ptr);
905 break;
906 case P_CMDLIST:
907 case P_LIST:
908 TALLOC_FREE(*((char ***)dest_ptr));
909 *(const char * const **)dest_ptr = (const char * const *)str_list_copy(pserviceDest,
910 *(const char * * const *)src_ptr);
911 break;
912 default:
913 break;
917 if (bcopyall) {
918 init_copymap(pserviceDest);
919 if (pserviceSource->copymap)
920 bitmap_copy(pserviceDest->copymap,
921 pserviceSource->copymap);
924 for (data = pserviceSource->param_opt; data != NULL; data = data->next) {
925 set_param_opt(pserviceDest, &pserviceDest->param_opt,
926 data->key, data->value, data->priority);
931 * Check a service for consistency. Return False if the service is in any way
932 * incomplete or faulty, else True.
934 bool lpcfg_service_ok(struct loadparm_service *service)
936 bool bRetval;
938 bRetval = true;
939 if (service->szService[0] == '\0') {
940 DEBUG(0, ("The following message indicates an internal error:\n"));
941 DEBUG(0, ("No service name in service entry.\n"));
942 bRetval = false;
945 /* The [printers] entry MUST be printable. I'm all for flexibility, but */
946 /* I can't see why you'd want a non-printable printer service... */
947 if (strwicmp(service->szService, PRINTERS_NAME) == 0) {
948 if (!service->printable) {
949 DEBUG(0, ("WARNING: [%s] service MUST be printable!\n",
950 service->szService));
951 service->printable = true;
953 /* [printers] service must also be non-browsable. */
954 if (service->browseable)
955 service->browseable = false;
958 if (service->path[0] == '\0' &&
959 strwicmp(service->szService, HOMES_NAME) != 0 &&
960 service->msdfs_proxy[0] == '\0')
962 DEBUG(0, ("WARNING: No path in service %s - making it unavailable!\n",
963 service->szService));
964 service->bAvailable = false;
967 /* If a service is flagged unavailable, log the fact at level 0. */
968 if (!service->bAvailable)
969 DEBUG(1, ("NOTE: Service %s is flagged unavailable.\n",
970 service->szService));
972 return bRetval;
976 /*******************************************************************
977 Keep a linked list of all config files so we know when one has changed
978 it's date and needs to be reloaded.
979 ********************************************************************/
981 void add_to_file_list(TALLOC_CTX *mem_ctx, struct file_lists **list,
982 const char *fname, const char *subfname)
984 struct file_lists *f = *list;
986 while (f) {
987 if (f->name && !strcmp(f->name, fname))
988 break;
989 f = f->next;
992 if (!f) {
993 f = talloc(mem_ctx, struct file_lists);
994 if (!f)
995 goto fail;
996 f->next = *list;
997 f->name = talloc_strdup(f, fname);
998 if (!f->name) {
999 TALLOC_FREE(f);
1000 goto fail;
1002 f->subfname = talloc_strdup(f, subfname);
1003 if (!f->subfname) {
1004 TALLOC_FREE(f);
1005 goto fail;
1007 *list = f;
1008 f->modtime = file_modtime(subfname);
1009 } else {
1010 time_t t = file_modtime(subfname);
1011 if (t)
1012 f->modtime = t;
1014 return;
1016 fail:
1017 DEBUG(0, ("Unable to add file to file list: %s\n", fname));
1021 /*******************************************************************
1022 Check if a config file has changed date.
1023 ********************************************************************/
1024 bool lpcfg_file_list_changed(struct loadparm_context *lp_ctx)
1026 struct file_lists *f;
1027 DEBUG(6, ("lpcfg_file_list_changed()\n"));
1029 for (f = lp_ctx->file_lists; f != NULL; f = f->next) {
1030 char *n2;
1031 time_t mod_time;
1033 n2 = standard_sub_basic(lp_ctx, f->name);
1035 DEBUGADD(6, ("file %s -> %s last mod_time: %s\n",
1036 f->name, n2, ctime(&f->modtime)));
1038 mod_time = file_modtime(n2);
1040 if (mod_time && ((f->modtime != mod_time) || (f->subfname == NULL) || (strcmp(n2, f->subfname) != 0))) {
1041 DEBUGADD(6, ("file %s modified: %s\n", n2,
1042 ctime(&mod_time)));
1043 f->modtime = mod_time;
1044 talloc_free(f->subfname);
1045 f->subfname = talloc_strdup(f, n2);
1046 TALLOC_FREE(n2);
1047 return true;
1049 TALLOC_FREE(n2);
1051 return false;
1055 * set the value for a P_ENUM
1057 bool lp_set_enum_parm( struct parm_struct *parm, const char *pszParmValue,
1058 int *ptr )
1060 int i;
1062 for (i = 0; parm->enum_list[i].name; i++) {
1063 if ( strequal(pszParmValue, parm->enum_list[i].name)) {
1064 *ptr = parm->enum_list[i].value;
1065 return true;
1068 DEBUG(0, ("WARNING: Ignoring invalid value '%s' for parameter '%s'\n",
1069 pszParmValue, parm->label));
1070 return false;
1074 /***************************************************************************
1075 Handle the "realm" parameter
1076 ***************************************************************************/
1078 bool handle_realm(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1079 const char *pszParmValue, char **ptr)
1081 char *upper;
1082 char *lower;
1084 upper = strupper_talloc(lp_ctx, pszParmValue);
1085 if (upper == NULL) {
1086 return false;
1089 lower = strlower_talloc(lp_ctx, pszParmValue);
1090 if (lower == NULL) {
1091 TALLOC_FREE(upper);
1092 return false;
1095 lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1096 lpcfg_string_set(lp_ctx->globals->ctx, &lp_ctx->globals->realm, upper);
1097 lpcfg_string_set(lp_ctx->globals->ctx, &lp_ctx->globals->dnsdomain, lower);
1099 return true;
1102 /***************************************************************************
1103 Handle the include operation.
1104 ***************************************************************************/
1106 bool handle_include(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1107 const char *pszParmValue, char **ptr)
1109 char *fname;
1111 if (lp_ctx->s3_fns) {
1112 return lp_ctx->s3_fns->lp_include(lp_ctx, service, pszParmValue, ptr);
1115 fname = standard_sub_basic(lp_ctx, pszParmValue);
1117 add_to_file_list(lp_ctx, &lp_ctx->file_lists, pszParmValue, fname);
1119 lpcfg_string_set(lp_ctx, ptr, fname);
1121 if (file_exist(fname))
1122 return pm_process(fname, do_section, lpcfg_do_parameter, lp_ctx);
1124 DEBUG(2, ("Can't find include file %s\n", fname));
1126 return false;
1129 /***************************************************************************
1130 Handle the interpretation of the copy parameter.
1131 ***************************************************************************/
1133 bool handle_copy(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1134 const char *pszParmValue, char **ptr)
1136 bool bRetval;
1137 struct loadparm_service *serviceTemp = NULL;
1139 bRetval = false;
1141 DEBUG(3, ("Copying service from service %s\n", pszParmValue));
1143 serviceTemp = lpcfg_getservicebyname(lp_ctx, pszParmValue);
1145 if (service == NULL) {
1146 DEBUG(0, ("Unable to copy service - invalid service destination"));
1147 return false;
1150 if (serviceTemp != NULL) {
1151 if (serviceTemp == service) {
1152 DEBUG(0, ("Can't copy service %s - unable to copy self!\n", pszParmValue));
1153 } else {
1154 copy_service(service,
1155 serviceTemp,
1156 service->copymap);
1157 lpcfg_string_set(service, ptr, pszParmValue);
1159 bRetval = true;
1161 } else {
1162 DEBUG(0, ("Unable to copy service - source not found: %s\n",
1163 pszParmValue));
1164 bRetval = false;
1167 return bRetval;
1170 bool handle_debug_list(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1171 const char *pszParmValue, char **ptr)
1173 lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1175 return debug_parse_levels(pszParmValue);
1178 bool handle_logfile(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1179 const char *pszParmValue, char **ptr)
1181 if (lp_ctx->s3_fns == NULL) {
1182 debug_set_logfile(pszParmValue);
1185 lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1187 return true;
1191 * These special charset handling methods only run in the source3 code.
1194 bool handle_charset(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1195 const char *pszParmValue, char **ptr)
1197 if (lp_ctx->s3_fns) {
1198 if (*ptr == NULL || strcmp(*ptr, pszParmValue) != 0) {
1199 global_iconv_handle = smb_iconv_handle_reinit(NULL,
1200 lpcfg_dos_charset(lp_ctx),
1201 lpcfg_unix_charset(lp_ctx),
1202 true, global_iconv_handle);
1206 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1210 bool handle_dos_charset(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1211 const char *pszParmValue, char **ptr)
1213 bool is_utf8 = false;
1214 size_t len = strlen(pszParmValue);
1216 if (lp_ctx->s3_fns) {
1217 if (len == 4 || len == 5) {
1218 /* Don't use StrCaseCmp here as we don't want to
1219 initialize iconv. */
1220 if ((toupper_m(pszParmValue[0]) == 'U') &&
1221 (toupper_m(pszParmValue[1]) == 'T') &&
1222 (toupper_m(pszParmValue[2]) == 'F')) {
1223 if (len == 4) {
1224 if (pszParmValue[3] == '8') {
1225 is_utf8 = true;
1227 } else {
1228 if (pszParmValue[3] == '-' &&
1229 pszParmValue[4] == '8') {
1230 is_utf8 = true;
1236 if (*ptr == NULL || strcmp(*ptr, pszParmValue) != 0) {
1237 if (is_utf8) {
1238 DEBUG(0,("ERROR: invalid DOS charset: 'dos charset' must not "
1239 "be UTF8, using (default value) %s instead.\n",
1240 DEFAULT_DOS_CHARSET));
1241 pszParmValue = DEFAULT_DOS_CHARSET;
1243 global_iconv_handle = smb_iconv_handle_reinit(NULL,
1244 lpcfg_dos_charset(lp_ctx),
1245 lpcfg_unix_charset(lp_ctx),
1246 true, global_iconv_handle);
1250 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1253 bool handle_printing(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1254 const char *pszParmValue, char **ptr)
1256 static int parm_num = -1;
1257 struct loadparm_service *s;
1259 if (parm_num == -1) {
1260 parm_num = lpcfg_map_parameter("printing");
1263 if (!lp_set_enum_parm(&parm_table[parm_num], pszParmValue, (int*)ptr)) {
1264 return false;
1267 if (lp_ctx->s3_fns) {
1268 if (service == NULL) {
1269 s = lp_ctx->sDefault;
1270 lp_ctx->s3_fns->init_printer_values(lp_ctx->globals->ctx, s);
1271 } else {
1272 s = service;
1273 lp_ctx->s3_fns->init_printer_values(s, s);
1277 return true;
1280 bool handle_ldap_debug_level(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1281 const char *pszParmValue, char **ptr)
1283 lp_ctx->globals->ldap_debug_level = lp_int(pszParmValue);
1285 if (lp_ctx->s3_fns) {
1286 lp_ctx->s3_fns->init_ldap_debugging();
1288 return true;
1291 bool handle_netbios_aliases(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1292 const char *pszParmValue, char **ptr)
1294 TALLOC_FREE(lp_ctx->globals->netbios_aliases);
1295 lp_ctx->globals->netbios_aliases = (const char **)str_list_make_v3(lp_ctx->globals->ctx,
1296 pszParmValue, NULL);
1298 if (lp_ctx->s3_fns) {
1299 return lp_ctx->s3_fns->set_netbios_aliases(lp_ctx->globals->netbios_aliases);
1301 return true;
1305 * idmap related parameters
1308 bool handle_idmap_backend(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1309 const char *pszParmValue, char **ptr)
1311 if (lp_ctx->s3_fns) {
1312 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : backend",
1313 pszParmValue, 0);
1316 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1319 bool handle_idmap_uid(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1320 const char *pszParmValue, char **ptr)
1322 if (lp_ctx->s3_fns) {
1323 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : range",
1324 pszParmValue, 0);
1327 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1330 bool handle_idmap_gid(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1331 const char *pszParmValue, char **ptr)
1333 if (lp_ctx->s3_fns) {
1334 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : range",
1335 pszParmValue, 0);
1338 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1341 bool handle_smb_ports(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1342 const char *pszParmValue, char **ptr)
1344 static int parm_num = -1;
1345 int i;
1346 const char **list;
1348 if (!pszParmValue || !*pszParmValue) {
1349 return false;
1352 if (parm_num == -1) {
1353 parm_num = lpcfg_map_parameter("smb ports");
1356 if(!set_variable_helper(lp_ctx->globals->ctx, parm_num, ptr, "smb ports",
1357 pszParmValue)) {
1358 return false;
1361 list = lp_ctx->globals->smb_ports;
1362 if (list == NULL) {
1363 return false;
1366 /* Check that each port is a valid integer and within range */
1367 for (i = 0; list[i] != NULL; i++) {
1368 char *end = NULL;
1369 int port = 0;
1370 port = strtol(list[i], &end, 10);
1371 if (*end != '\0' || port <= 0 || port > 65535) {
1372 TALLOC_FREE(list);
1373 return false;
1377 return true;
1380 /***************************************************************************
1381 Initialise a copymap.
1382 ***************************************************************************/
1384 void init_copymap(struct loadparm_service *pservice)
1386 int i;
1388 TALLOC_FREE(pservice->copymap);
1390 pservice->copymap = bitmap_talloc(NULL, num_parameters());
1391 if (!pservice->copymap)
1392 DEBUG(0,
1393 ("Couldn't allocate copymap!! (size %d)\n",
1394 (int)num_parameters()));
1395 else
1396 for (i = 0; i < num_parameters(); i++)
1397 bitmap_set(pservice->copymap, i);
1401 * Process a parametric option
1403 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
1404 struct loadparm_service *service,
1405 const char *pszParmName,
1406 const char *pszParmValue, int flags)
1408 struct parmlist_entry **data;
1409 char *name;
1410 TALLOC_CTX *mem_ctx;
1412 while (isspace((unsigned char)*pszParmName)) {
1413 pszParmName++;
1416 name = strlower_talloc(lp_ctx, pszParmName);
1417 if (!name) return false;
1419 if (service == NULL) {
1420 data = &lp_ctx->globals->param_opt;
1422 * s3 code cannot deal with parametric options stored on the globals ctx.
1424 if (lp_ctx->s3_fns != NULL) {
1425 mem_ctx = NULL;
1426 } else {
1427 mem_ctx = lp_ctx->globals->ctx;
1429 } else {
1430 data = &service->param_opt;
1431 mem_ctx = service;
1434 set_param_opt(mem_ctx, data, name, pszParmValue, flags);
1436 talloc_free(name);
1438 return true;
1441 static bool set_variable_helper(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
1442 const char *pszParmName, const char *pszParmValue)
1444 int i;
1446 /* switch on the type of variable it is */
1447 switch (parm_table[parmnum].type)
1449 case P_BOOL: {
1450 bool b;
1451 if (!set_boolean(pszParmValue, &b)) {
1452 DEBUG(0, ("set_variable_helper(%s): value is not "
1453 "boolean!\n", pszParmValue));
1454 return false;
1456 *(bool *)parm_ptr = b;
1458 break;
1460 case P_BOOLREV: {
1461 bool b;
1462 if (!set_boolean(pszParmValue, &b)) {
1463 DEBUG(0, ("set_variable_helper(%s): value is not "
1464 "boolean!\n", pszParmValue));
1465 return false;
1467 *(bool *)parm_ptr = !b;
1469 break;
1471 case P_INTEGER:
1472 *(int *)parm_ptr = lp_int(pszParmValue);
1473 break;
1475 case P_CHAR:
1476 *(char *)parm_ptr = *pszParmValue;
1477 break;
1479 case P_OCTAL:
1480 i = sscanf(pszParmValue, "%o", (int *)parm_ptr);
1481 if ( i != 1 ) {
1482 DEBUG ( 0, ("Invalid octal number %s\n", pszParmName ));
1483 return false;
1485 break;
1487 case P_BYTES:
1489 uint64_t val;
1490 if (conv_str_size_error(pszParmValue, &val)) {
1491 if (val <= INT_MAX) {
1492 *(int *)parm_ptr = (int)val;
1493 break;
1497 DEBUG(0, ("set_variable_helper(%s): value is not "
1498 "a valid size specifier!\n", pszParmValue));
1499 return false;
1502 case P_CMDLIST:
1503 TALLOC_FREE(*(char ***)parm_ptr);
1504 *(const char * const **)parm_ptr
1505 = (const char * const *)str_list_make_v3(mem_ctx,
1506 pszParmValue, NULL);
1507 break;
1509 case P_LIST:
1511 char **new_list = str_list_make_v3(mem_ctx,
1512 pszParmValue, NULL);
1513 if (new_list == NULL) {
1514 break;
1517 for (i=0; new_list[i]; i++) {
1518 if (*(const char ***)parm_ptr != NULL &&
1519 new_list[i][0] == '+' &&
1520 new_list[i][1])
1522 if (!str_list_check(*(const char ***)parm_ptr,
1523 &new_list[i][1])) {
1524 *(const char ***)parm_ptr = str_list_add(*(const char ***)parm_ptr,
1525 &new_list[i][1]);
1527 } else if (*(const char ***)parm_ptr != NULL &&
1528 new_list[i][0] == '-' &&
1529 new_list[i][1])
1531 str_list_remove(*(const char ***)parm_ptr,
1532 &new_list[i][1]);
1533 } else {
1534 if (i != 0) {
1535 DEBUG(0, ("Unsupported list syntax for: %s = %s\n",
1536 pszParmName, pszParmValue));
1537 return false;
1539 *(const char * const **)parm_ptr = (const char * const *) new_list;
1540 break;
1543 break;
1546 case P_STRING:
1547 lpcfg_string_set(mem_ctx, (char **)parm_ptr, pszParmValue);
1548 break;
1550 case P_USTRING:
1551 lpcfg_string_set_upper(mem_ctx, (char **)parm_ptr, pszParmValue);
1552 break;
1554 case P_ENUM:
1555 if (!lp_set_enum_parm(&parm_table[parmnum], pszParmValue, (int*)parm_ptr)) {
1556 return false;
1558 break;
1560 case P_SEP:
1561 break;
1564 return true;
1568 bool set_variable(TALLOC_CTX *mem_ctx, struct loadparm_service *service, int parmnum, void *parm_ptr,
1569 const char *pszParmName, const char *pszParmValue,
1570 struct loadparm_context *lp_ctx, bool on_globals)
1572 int i;
1573 bool ok;
1575 /* if it is a special case then go ahead */
1576 if (parm_table[parmnum].special) {
1577 ok = parm_table[parmnum].special(lp_ctx, service, pszParmValue,
1578 (char **)parm_ptr);
1579 if (!ok) {
1580 return false;
1582 goto mark_non_default;
1585 ok = set_variable_helper(mem_ctx, parmnum, parm_ptr, pszParmName, pszParmValue);
1587 if (!ok) {
1588 return false;
1591 mark_non_default:
1592 if (on_globals && (lp_ctx->flags[parmnum] & FLAG_DEFAULT)) {
1593 lp_ctx->flags[parmnum] &= ~FLAG_DEFAULT;
1594 /* we have to also unset FLAG_DEFAULT on aliases */
1595 for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
1596 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1598 for (i=parmnum+1;i<num_parameters() && parm_table[i].offset == parm_table[parmnum].offset;i++) {
1599 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1602 return true;
1606 bool lpcfg_do_global_parameter(struct loadparm_context *lp_ctx,
1607 const char *pszParmName, const char *pszParmValue)
1609 int parmnum = lpcfg_map_parameter(pszParmName);
1610 void *parm_ptr;
1612 if (parmnum < 0) {
1613 if (strchr(pszParmName, ':')) {
1614 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName, pszParmValue, 0);
1616 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1617 return true;
1620 /* if the flag has been set on the command line, then don't allow override,
1621 but don't report an error */
1622 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1623 return true;
1626 if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
1627 DEBUG(1, ("WARNING: The \"%s\" option is deprecated\n",
1628 pszParmName));
1631 parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[parmnum]);
1633 return set_variable(lp_ctx->globals->ctx, NULL, parmnum, parm_ptr,
1634 pszParmName, pszParmValue, lp_ctx, true);
1637 bool lpcfg_do_service_parameter(struct loadparm_context *lp_ctx,
1638 struct loadparm_service *service,
1639 const char *pszParmName, const char *pszParmValue)
1641 void *parm_ptr;
1642 int i;
1643 int parmnum = lpcfg_map_parameter(pszParmName);
1645 if (parmnum < 0) {
1646 if (strchr(pszParmName, ':')) {
1647 return lp_do_parameter_parametric(lp_ctx, service, pszParmName, pszParmValue, 0);
1649 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1650 return true;
1653 /* if the flag has been set on the command line, then don't allow override,
1654 but don't report an error */
1655 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1656 return true;
1659 if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
1660 DEBUG(1, ("WARNING: The \"%s\" option is deprecated\n",
1661 pszParmName));
1664 if (parm_table[parmnum].p_class == P_GLOBAL) {
1665 DEBUG(0,
1666 ("Global parameter %s found in service section!\n",
1667 pszParmName));
1668 return true;
1670 parm_ptr = ((char *)service) + parm_table[parmnum].offset;
1672 if (!service->copymap)
1673 init_copymap(service);
1675 /* this handles the aliases - set the copymap for other
1676 * entries with the same data pointer */
1677 for (i = 0; parm_table[i].label; i++)
1678 if (parm_table[i].offset == parm_table[parmnum].offset &&
1679 parm_table[i].p_class == parm_table[parmnum].p_class)
1680 bitmap_clear(service->copymap, i);
1682 return set_variable(service, service, parmnum, parm_ptr, pszParmName,
1683 pszParmValue, lp_ctx, false);
1687 * Process a parameter.
1690 bool lpcfg_do_parameter(const char *pszParmName, const char *pszParmValue,
1691 void *userdata)
1693 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1695 if (lp_ctx->bInGlobalSection)
1696 return lpcfg_do_global_parameter(lp_ctx, pszParmName,
1697 pszParmValue);
1698 else
1699 return lpcfg_do_service_parameter(lp_ctx, lp_ctx->currentService,
1700 pszParmName, pszParmValue);
1704 variable argument do parameter
1706 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx, const char *pszParmName, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
1707 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx,
1708 const char *pszParmName, const char *fmt, ...)
1710 char *s;
1711 bool ret;
1712 va_list ap;
1714 va_start(ap, fmt);
1715 s = talloc_vasprintf(NULL, fmt, ap);
1716 va_end(ap);
1717 ret = lpcfg_do_global_parameter(lp_ctx, pszParmName, s);
1718 talloc_free(s);
1719 return ret;
1724 set a parameter from the commandline - this is called from command line parameter
1725 parsing code. It sets the parameter then marks the parameter as unable to be modified
1726 by smb.conf processing
1728 bool lpcfg_set_cmdline(struct loadparm_context *lp_ctx, const char *pszParmName,
1729 const char *pszParmValue)
1731 int parmnum;
1732 int i;
1734 while (isspace((unsigned char)*pszParmValue)) pszParmValue++;
1736 parmnum = lpcfg_map_parameter(pszParmName);
1738 if (parmnum < 0 && strchr(pszParmName, ':')) {
1739 /* set a parametric option */
1740 bool ok;
1741 ok = lp_do_parameter_parametric(lp_ctx, NULL, pszParmName,
1742 pszParmValue, FLAG_CMDLINE);
1743 if (lp_ctx->s3_fns != NULL) {
1744 if (ok) {
1745 lp_ctx->s3_fns->store_cmdline(pszParmName, pszParmValue);
1748 return ok;
1751 if (parmnum < 0) {
1752 DEBUG(0,("Unknown option '%s'\n", pszParmName));
1753 return false;
1756 /* reset the CMDLINE flag in case this has been called before */
1757 lp_ctx->flags[parmnum] &= ~FLAG_CMDLINE;
1759 if (lp_ctx->s3_fns != NULL) {
1760 if (!lp_ctx->s3_fns->lp_do_parameter(-1, pszParmName, pszParmValue)) {
1761 return false;
1763 } else {
1764 if (!lpcfg_do_global_parameter(lp_ctx, pszParmName, pszParmValue)) {
1765 return false;
1769 lp_ctx->flags[parmnum] |= FLAG_CMDLINE;
1771 /* we have to also set FLAG_CMDLINE on aliases */
1772 for (i=parmnum-1;
1773 i>=0 && parm_table[i].p_class == parm_table[parmnum].p_class &&
1774 parm_table[i].offset == parm_table[parmnum].offset;
1775 i--) {
1776 lp_ctx->flags[i] |= FLAG_CMDLINE;
1778 for (i=parmnum+1;
1779 i<num_parameters() &&
1780 parm_table[i].p_class == parm_table[parmnum].p_class &&
1781 parm_table[i].offset == parm_table[parmnum].offset;
1782 i++) {
1783 lp_ctx->flags[i] |= FLAG_CMDLINE;
1786 if (lp_ctx->s3_fns != NULL) {
1787 lp_ctx->s3_fns->store_cmdline(pszParmName, pszParmValue);
1790 return true;
1794 set a option from the commandline in 'a=b' format. Use to support --option
1796 bool lpcfg_set_option(struct loadparm_context *lp_ctx, const char *option)
1798 char *p, *s;
1799 bool ret;
1801 s = talloc_strdup(NULL, option);
1802 if (!s) {
1803 return false;
1806 p = strchr(s, '=');
1807 if (!p) {
1808 talloc_free(s);
1809 return false;
1812 *p = 0;
1814 ret = lpcfg_set_cmdline(lp_ctx, s, p+1);
1815 talloc_free(s);
1816 return ret;
1820 #define BOOLSTR(b) ((b) ? "Yes" : "No")
1823 * Print a parameter of the specified type.
1826 void lpcfg_print_parameter(struct parm_struct *p, void *ptr, FILE * f)
1828 /* For the seperation of lists values that we print below */
1829 const char *list_sep = ", ";
1830 int i;
1831 switch (p->type)
1833 case P_ENUM:
1834 for (i = 0; p->enum_list[i].name; i++) {
1835 if (*(int *)ptr == p->enum_list[i].value) {
1836 fprintf(f, "%s",
1837 p->enum_list[i].name);
1838 break;
1841 break;
1843 case P_BOOL:
1844 fprintf(f, "%s", BOOLSTR(*(bool *)ptr));
1845 break;
1847 case P_BOOLREV:
1848 fprintf(f, "%s", BOOLSTR(!*(bool *)ptr));
1849 break;
1851 case P_INTEGER:
1852 case P_BYTES:
1853 fprintf(f, "%d", *(int *)ptr);
1854 break;
1856 case P_CHAR:
1857 fprintf(f, "%c", *(char *)ptr);
1858 break;
1860 case P_OCTAL: {
1861 int val = *(int *)ptr;
1862 if (val == -1) {
1863 fprintf(f, "-1");
1864 } else {
1865 fprintf(f, "0%03o", val);
1867 break;
1870 case P_CMDLIST:
1871 list_sep = " ";
1872 /* fall through */
1873 case P_LIST:
1874 if ((char ***)ptr && *(char ***)ptr) {
1875 char **list = *(char ***)ptr;
1876 for (; *list; list++) {
1877 /* surround strings with whitespace in double quotes */
1878 if (*(list+1) == NULL) {
1879 /* last item, no extra separator */
1880 list_sep = "";
1882 if ( strchr_m( *list, ' ' ) ) {
1883 fprintf(f, "\"%s\"%s", *list, list_sep);
1884 } else {
1885 fprintf(f, "%s%s", *list, list_sep);
1889 break;
1891 case P_STRING:
1892 case P_USTRING:
1893 if (*(char **)ptr) {
1894 fprintf(f, "%s", *(char **)ptr);
1896 break;
1897 case P_SEP:
1898 break;
1903 * Check if two parameters are equal.
1906 static bool lpcfg_equal_parameter(parm_type type, void *ptr1, void *ptr2)
1908 switch (type) {
1909 case P_BOOL:
1910 case P_BOOLREV:
1911 return (*((bool *)ptr1) == *((bool *)ptr2));
1913 case P_INTEGER:
1914 case P_ENUM:
1915 case P_OCTAL:
1916 case P_BYTES:
1917 return (*((int *)ptr1) == *((int *)ptr2));
1919 case P_CHAR:
1920 return (*((char *)ptr1) == *((char *)ptr2));
1922 case P_LIST:
1923 case P_CMDLIST:
1924 return str_list_equal(*(const char ***)ptr1, *(const char ***)ptr2);
1926 case P_STRING:
1927 case P_USTRING:
1929 char *p1 = *(char **)ptr1, *p2 = *(char **)ptr2;
1930 if (p1 && !*p1)
1931 p1 = NULL;
1932 if (p2 && !*p2)
1933 p2 = NULL;
1934 return (p1 == p2 || strequal(p1, p2));
1936 case P_SEP:
1937 break;
1939 return false;
1943 * Process a new section (service).
1945 * At this stage all sections are services.
1946 * Later we'll have special sections that permit server parameters to be set.
1947 * Returns True on success, False on failure.
1950 static bool do_section(const char *pszSectionName, void *userdata)
1952 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1953 bool bRetval;
1954 bool isglobal;
1956 if (lp_ctx->s3_fns != NULL) {
1957 return lp_ctx->s3_fns->do_section(pszSectionName, lp_ctx);
1960 isglobal = ((strwicmp(pszSectionName, GLOBAL_NAME) == 0) ||
1961 (strwicmp(pszSectionName, GLOBAL_NAME2) == 0));
1963 bRetval = false;
1965 /* if we've just struck a global section, note the fact. */
1966 lp_ctx->bInGlobalSection = isglobal;
1968 /* check for multiple global sections */
1969 if (lp_ctx->bInGlobalSection) {
1970 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1971 return true;
1974 /* if we have a current service, tidy it up before moving on */
1975 bRetval = true;
1977 if (lp_ctx->currentService != NULL)
1978 bRetval = lpcfg_service_ok(lp_ctx->currentService);
1980 /* if all is still well, move to the next record in the services array */
1981 if (bRetval) {
1982 /* We put this here to avoid an odd message order if messages are */
1983 /* issued by the post-processing of a previous section. */
1984 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1986 if ((lp_ctx->currentService = lpcfg_add_service(lp_ctx, lp_ctx->sDefault,
1987 pszSectionName))
1988 == NULL) {
1989 DEBUG(0, ("Failed to add a new service\n"));
1990 return false;
1994 return bRetval;
1999 * Determine if a particular base parameter is currently set to the default value.
2002 static bool is_default(void *base_structure, int i)
2004 void *def_ptr = ((char *)base_structure) + parm_table[i].offset;
2005 switch (parm_table[i].type) {
2006 case P_CMDLIST:
2007 case P_LIST:
2008 return str_list_equal((const char * const *)parm_table[i].def.lvalue,
2009 *(const char ***)def_ptr);
2010 case P_STRING:
2011 case P_USTRING:
2012 return strequal(parm_table[i].def.svalue,
2013 *(char **)def_ptr);
2014 case P_BOOL:
2015 case P_BOOLREV:
2016 return parm_table[i].def.bvalue ==
2017 *(bool *)def_ptr;
2018 case P_INTEGER:
2019 case P_CHAR:
2020 case P_OCTAL:
2021 case P_BYTES:
2022 case P_ENUM:
2023 return parm_table[i].def.ivalue ==
2024 *(int *)def_ptr;
2025 case P_SEP:
2026 break;
2028 return false;
2032 *Display the contents of the global structure.
2035 void lpcfg_dump_globals(struct loadparm_context *lp_ctx, FILE *f,
2036 bool show_defaults)
2038 int i;
2039 struct parmlist_entry *data;
2041 fprintf(f, "# Global parameters\n[global]\n");
2043 for (i = 0; parm_table[i].label; i++)
2044 if (parm_table[i].p_class == P_GLOBAL &&
2045 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset))) {
2046 if (!show_defaults) {
2047 if (lp_ctx->flags && (lp_ctx->flags[i] & FLAG_DEFAULT)) {
2048 continue;
2051 if (is_default(lp_ctx->globals, i)) {
2052 continue;
2056 fprintf(f, "\t%s = ", parm_table[i].label);
2057 lpcfg_print_parameter(&parm_table[i], lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[i]), f);
2058 fprintf(f, "\n");
2060 if (lp_ctx->globals->param_opt != NULL) {
2061 for (data = lp_ctx->globals->param_opt; data;
2062 data = data->next) {
2063 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
2064 continue;
2066 fprintf(f, "\t%s = %s\n", data->key, data->value);
2073 * Display the contents of a single services record.
2076 void lpcfg_dump_a_service(struct loadparm_service * pService, struct loadparm_service *sDefault, FILE * f,
2077 unsigned int *flags, bool show_defaults)
2079 int i;
2080 struct parmlist_entry *data;
2082 if (pService != sDefault)
2083 fprintf(f, "\n[%s]\n", pService->szService);
2085 for (i = 0; parm_table[i].label; i++) {
2086 if (parm_table[i].p_class == P_LOCAL &&
2087 (*parm_table[i].label != '-') &&
2088 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset)))
2090 if (pService == sDefault) {
2091 if (flags && (flags[i] & FLAG_DEFAULT)) {
2092 continue;
2094 if (!show_defaults) {
2095 if (is_default(sDefault, i)) {
2096 continue;
2099 } else {
2100 if (lpcfg_equal_parameter(parm_table[i].type,
2101 ((char *)pService) +
2102 parm_table[i].offset,
2103 ((char *)sDefault) +
2104 parm_table[i].offset))
2105 continue;
2108 fprintf(f, "\t%s = ", parm_table[i].label);
2109 lpcfg_print_parameter(&parm_table[i],
2110 ((char *)pService) + parm_table[i].offset, f);
2111 fprintf(f, "\n");
2114 if (pService->param_opt != NULL) {
2115 for (data = pService->param_opt; data; data = data->next) {
2116 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
2117 continue;
2119 fprintf(f, "\t%s = %s\n", data->key, data->value);
2124 bool lpcfg_dump_a_parameter(struct loadparm_context *lp_ctx,
2125 struct loadparm_service *service,
2126 const char *parm_name, FILE * f)
2128 struct parm_struct *parm;
2129 void *ptr;
2130 char *local_parm_name;
2131 char *parm_opt;
2132 const char *parm_opt_value;
2134 /* check for parametrical option */
2135 local_parm_name = talloc_strdup(lp_ctx, parm_name);
2136 if (local_parm_name == NULL) {
2137 return false;
2140 parm_opt = strchr( local_parm_name, ':');
2142 if (parm_opt) {
2143 *parm_opt = '\0';
2144 parm_opt++;
2145 if (strlen(parm_opt)) {
2146 parm_opt_value = lpcfg_parm_string(lp_ctx, service,
2147 local_parm_name, parm_opt);
2148 if (parm_opt_value) {
2149 fprintf(f, "%s\n", parm_opt_value);
2150 return true;
2153 return false;
2156 /* parameter is not parametric, search the table */
2157 parm = lpcfg_parm_struct(lp_ctx, parm_name);
2158 if (!parm) {
2159 return false;
2162 if (service != NULL && parm->p_class == P_GLOBAL) {
2163 return false;
2166 ptr = lpcfg_parm_ptr(lp_ctx, service,parm);
2168 lpcfg_print_parameter(parm, ptr, f);
2169 fprintf(f, "\n");
2170 return true;
2174 * Auto-load some home services.
2176 static void lpcfg_add_auto_services(struct loadparm_context *lp_ctx,
2177 const char *str)
2179 return;
2184 * Unload unused services.
2187 void lpcfg_killunused(struct loadparm_context *lp_ctx,
2188 struct smbsrv_connection *smb,
2189 bool (*snumused) (struct smbsrv_connection *, int))
2191 int i;
2192 for (i = 0; i < lp_ctx->iNumServices; i++) {
2193 if (lp_ctx->services[i] == NULL)
2194 continue;
2196 if (!snumused || !snumused(smb, i)) {
2197 talloc_free(lp_ctx->services[i]);
2198 lp_ctx->services[i] = NULL;
2204 static int lpcfg_destructor(struct loadparm_context *lp_ctx)
2206 struct parmlist_entry *data;
2208 if (lp_ctx->refuse_free) {
2209 /* someone is trying to free the
2210 global_loadparm_context.
2211 We can't allow that. */
2212 return -1;
2215 if (lp_ctx->globals->param_opt != NULL) {
2216 struct parmlist_entry *next;
2217 for (data = lp_ctx->globals->param_opt; data; data=next) {
2218 next = data->next;
2219 if (data->priority & FLAG_CMDLINE) continue;
2220 DLIST_REMOVE(lp_ctx->globals->param_opt, data);
2221 talloc_free(data);
2225 return 0;
2229 * Initialise the global parameter structure.
2231 * Note that most callers should use loadparm_init_global() instead
2233 struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
2235 int i;
2236 char *myname;
2237 struct loadparm_context *lp_ctx;
2238 struct parmlist_entry *parm;
2239 char *logfile;
2241 lp_ctx = talloc_zero(mem_ctx, struct loadparm_context);
2242 if (lp_ctx == NULL)
2243 return NULL;
2245 talloc_set_destructor(lp_ctx, lpcfg_destructor);
2246 lp_ctx->bInGlobalSection = true;
2247 lp_ctx->globals = talloc_zero(lp_ctx, struct loadparm_global);
2248 /* This appears odd, but globals in s3 isn't a pointer */
2249 lp_ctx->globals->ctx = lp_ctx->globals;
2250 lp_ctx->sDefault = talloc_zero(lp_ctx, struct loadparm_service);
2251 lp_ctx->flags = talloc_zero_array(lp_ctx, unsigned int, num_parameters());
2253 lp_ctx->sDefault->iMaxPrintJobs = 1000;
2254 lp_ctx->sDefault->bAvailable = true;
2255 lp_ctx->sDefault->browseable = true;
2256 lp_ctx->sDefault->read_only = true;
2257 lp_ctx->sDefault->map_archive = true;
2258 lp_ctx->sDefault->strict_locking = true;
2259 lp_ctx->sDefault->oplocks = true;
2260 lp_ctx->sDefault->create_mask = 0744;
2261 lp_ctx->sDefault->force_create_mode = 0000;
2262 lp_ctx->sDefault->directory_mask = 0755;
2263 lp_ctx->sDefault->force_directory_mode = 0000;
2265 DEBUG(3, ("Initialising global parameters\n"));
2267 for (i = 0; parm_table[i].label; i++) {
2268 if ((parm_table[i].type == P_STRING ||
2269 parm_table[i].type == P_USTRING) &&
2270 !(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2271 char **r;
2272 if (parm_table[i].p_class == P_LOCAL) {
2273 r = (char **)(((char *)lp_ctx->sDefault) + parm_table[i].offset);
2274 } else {
2275 r = (char **)(((char *)lp_ctx->globals) + parm_table[i].offset);
2277 *r = talloc_strdup(lp_ctx, "");
2281 logfile = talloc_asprintf(lp_ctx, "%s/log.samba", dyn_LOGFILEBASE);
2282 lpcfg_do_global_parameter(lp_ctx, "log file", logfile);
2283 talloc_free(logfile);
2285 lpcfg_do_global_parameter(lp_ctx, "log level", "0");
2287 lpcfg_do_global_parameter(lp_ctx, "syslog", "1");
2288 lpcfg_do_global_parameter(lp_ctx, "syslog only", "No");
2289 lpcfg_do_global_parameter(lp_ctx, "debug timestamp", "Yes");
2290 lpcfg_do_global_parameter(lp_ctx, "debug prefix timestamp", "No");
2291 lpcfg_do_global_parameter(lp_ctx, "debug hires timestamp", "Yes");
2292 lpcfg_do_global_parameter(lp_ctx, "debug pid", "No");
2293 lpcfg_do_global_parameter(lp_ctx, "debug uid", "No");
2294 lpcfg_do_global_parameter(lp_ctx, "debug class", "No");
2296 lpcfg_do_global_parameter(lp_ctx, "share backend", "classic");
2298 lpcfg_do_global_parameter(lp_ctx, "server role", "auto");
2299 lpcfg_do_global_parameter(lp_ctx, "domain logons", "No");
2300 lpcfg_do_global_parameter(lp_ctx, "domain master", "Auto");
2302 /* options that can be set on the command line must be initialised via
2303 the slower lpcfg_do_global_parameter() to ensure that FLAG_CMDLINE is obeyed */
2304 #ifdef TCP_NODELAY
2305 lpcfg_do_global_parameter(lp_ctx, "socket options", "TCP_NODELAY");
2306 #endif
2307 lpcfg_do_global_parameter(lp_ctx, "workgroup", DEFAULT_WORKGROUP);
2308 myname = get_myname(lp_ctx);
2309 lpcfg_do_global_parameter(lp_ctx, "netbios name", myname);
2310 talloc_free(myname);
2311 lpcfg_do_global_parameter(lp_ctx, "name resolve order", "lmhosts wins host bcast");
2313 lpcfg_do_global_parameter(lp_ctx, "fstype", "NTFS");
2315 lpcfg_do_global_parameter(lp_ctx, "ntvfs handler", "unixuid default");
2316 lpcfg_do_global_parameter(lp_ctx, "max connections", "0");
2318 lpcfg_do_global_parameter(lp_ctx, "dcerpc endpoint servers", "epmapper wkssvc rpcecho samr netlogon lsarpc spoolss drsuapi dssetup unixinfo browser eventlog6 backupkey dnsserver");
2319 lpcfg_do_global_parameter(lp_ctx, "server services", "s3fs rpc nbt wrepl ldap cldap kdc drepl winbindd ntp_signd kcc dnsupdate dns");
2320 lpcfg_do_global_parameter(lp_ctx, "kccsrv:samba_kcc", "true");
2321 /* the winbind method for domain controllers is for both RODC
2322 auth forwarding and for trusted domains */
2323 lpcfg_do_global_parameter(lp_ctx, "private dir", dyn_PRIVATE_DIR);
2324 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_LOCAL_MACHINE", "hklm.ldb");
2326 /* This hive should be dynamically generated by Samba using
2327 data from the sam, but for the moment leave it in a tdb to
2328 keep regedt32 from popping up an annoying dialog. */
2329 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_USERS", "hku.ldb");
2331 /* using UTF8 by default allows us to support all chars */
2332 lpcfg_do_global_parameter(lp_ctx, "unix charset", "UTF-8");
2334 /* Use codepage 850 as a default for the dos character set */
2335 lpcfg_do_global_parameter(lp_ctx, "dos charset", "CP850");
2338 * Allow the default PASSWD_CHAT to be overridden in local.h.
2340 lpcfg_do_global_parameter(lp_ctx, "passwd chat", DEFAULT_PASSWD_CHAT);
2342 lpcfg_do_global_parameter(lp_ctx, "pid directory", dyn_PIDDIR);
2343 lpcfg_do_global_parameter(lp_ctx, "lock dir", dyn_LOCKDIR);
2344 lpcfg_do_global_parameter(lp_ctx, "state directory", dyn_STATEDIR);
2345 lpcfg_do_global_parameter(lp_ctx, "cache directory", dyn_CACHEDIR);
2346 lpcfg_do_global_parameter(lp_ctx, "ncalrpc dir", dyn_NCALRPCDIR);
2348 lpcfg_do_global_parameter(lp_ctx, "nbt client socket address", "0.0.0.0");
2349 lpcfg_do_global_parameter_var(lp_ctx, "server string",
2350 "Samba %s", SAMBA_VERSION_STRING);
2352 lpcfg_do_global_parameter(lp_ctx, "password server", "*");
2354 lpcfg_do_global_parameter(lp_ctx, "max mux", "50");
2355 lpcfg_do_global_parameter(lp_ctx, "max xmit", "16644");
2356 lpcfg_do_global_parameter(lp_ctx, "host msdfs", "true");
2358 lpcfg_do_global_parameter(lp_ctx, "LargeReadwrite", "True");
2359 lpcfg_do_global_parameter(lp_ctx, "server min protocol", "LANMAN1");
2360 lpcfg_do_global_parameter(lp_ctx, "server max protocol", "SMB3");
2361 lpcfg_do_global_parameter(lp_ctx, "client min protocol", "CORE");
2362 lpcfg_do_global_parameter(lp_ctx, "client max protocol", "NT1");
2363 lpcfg_do_global_parameter(lp_ctx, "security", "AUTO");
2364 lpcfg_do_global_parameter(lp_ctx, "EncryptPasswords", "True");
2365 lpcfg_do_global_parameter(lp_ctx, "ReadRaw", "True");
2366 lpcfg_do_global_parameter(lp_ctx, "WriteRaw", "True");
2367 lpcfg_do_global_parameter(lp_ctx, "NullPasswords", "False");
2368 lpcfg_do_global_parameter(lp_ctx, "old password allowed period", "60");
2369 lpcfg_do_global_parameter(lp_ctx, "ObeyPamRestrictions", "False");
2371 lpcfg_do_global_parameter(lp_ctx, "TimeServer", "False");
2372 lpcfg_do_global_parameter(lp_ctx, "BindInterfacesOnly", "False");
2373 lpcfg_do_global_parameter(lp_ctx, "Unicode", "True");
2374 lpcfg_do_global_parameter(lp_ctx, "ClientLanManAuth", "False");
2375 lpcfg_do_global_parameter(lp_ctx, "ClientNTLMv2Auth", "True");
2376 lpcfg_do_global_parameter(lp_ctx, "LanmanAuth", "False");
2377 lpcfg_do_global_parameter(lp_ctx, "NTLMAuth", "True");
2378 lpcfg_do_global_parameter(lp_ctx, "client use spnego principal", "False");
2380 lpcfg_do_global_parameter(lp_ctx, "UnixExtensions", "True");
2382 lpcfg_do_global_parameter(lp_ctx, "PreferredMaster", "Auto");
2383 lpcfg_do_global_parameter(lp_ctx, "LocalMaster", "True");
2385 lpcfg_do_global_parameter(lp_ctx, "wins support", "False");
2386 lpcfg_do_global_parameter(lp_ctx, "dns proxy", "True");
2388 lpcfg_do_global_parameter(lp_ctx, "winbind separator", "\\");
2389 lpcfg_do_global_parameter(lp_ctx, "winbind sealed pipes", "True");
2390 lpcfg_do_global_parameter(lp_ctx, "require strong key", "True");
2391 lpcfg_do_global_parameter(lp_ctx, "winbindd socket directory", dyn_WINBINDD_SOCKET_DIR);
2392 lpcfg_do_global_parameter(lp_ctx, "winbindd privileged socket directory", dyn_WINBINDD_PRIVILEGED_SOCKET_DIR);
2393 lpcfg_do_global_parameter(lp_ctx, "ntp signd socket directory", dyn_NTP_SIGND_SOCKET_DIR);
2394 lpcfg_do_global_parameter_var(lp_ctx, "dns update command", "%s/samba_dnsupdate", dyn_SCRIPTSBINDIR);
2395 lpcfg_do_global_parameter_var(lp_ctx, "spn update command", "%s/samba_spnupdate", dyn_SCRIPTSBINDIR);
2396 lpcfg_do_global_parameter_var(lp_ctx, "samba kcc command",
2397 "%s/samba_kcc", dyn_SCRIPTSBINDIR);
2398 lpcfg_do_global_parameter(lp_ctx, "template shell", "/bin/false");
2399 lpcfg_do_global_parameter(lp_ctx, "template homedir", "/home/%D/%U");
2401 lpcfg_do_global_parameter(lp_ctx, "client signing", "default");
2402 lpcfg_do_global_parameter(lp_ctx, "server signing", "default");
2404 lpcfg_do_global_parameter(lp_ctx, "use spnego", "True");
2406 lpcfg_do_global_parameter(lp_ctx, "use mmap", "True");
2408 lpcfg_do_global_parameter(lp_ctx, "smb ports", "445 139");
2409 lpcfg_do_global_parameter(lp_ctx, "nbt port", "137");
2410 lpcfg_do_global_parameter(lp_ctx, "dgram port", "138");
2411 lpcfg_do_global_parameter(lp_ctx, "cldap port", "389");
2412 lpcfg_do_global_parameter(lp_ctx, "krb5 port", "88");
2413 lpcfg_do_global_parameter(lp_ctx, "kpasswd port", "464");
2414 lpcfg_do_global_parameter(lp_ctx, "web port", "901");
2416 lpcfg_do_global_parameter(lp_ctx, "nt status support", "True");
2418 lpcfg_do_global_parameter(lp_ctx, "max wins ttl", "518400"); /* 6 days */
2419 lpcfg_do_global_parameter(lp_ctx, "min wins ttl", "21600");
2421 lpcfg_do_global_parameter(lp_ctx, "tls enabled", "True");
2422 lpcfg_do_global_parameter(lp_ctx, "tls keyfile", "tls/key.pem");
2423 lpcfg_do_global_parameter(lp_ctx, "tls certfile", "tls/cert.pem");
2424 lpcfg_do_global_parameter(lp_ctx, "tls cafile", "tls/ca.pem");
2425 lpcfg_do_global_parameter(lp_ctx, "prefork children:smb", "4");
2427 lpcfg_do_global_parameter(lp_ctx, "rndc command", "/usr/sbin/rndc");
2428 lpcfg_do_global_parameter(lp_ctx, "nsupdate command", "/usr/bin/nsupdate -g");
2430 lpcfg_do_global_parameter(lp_ctx, "allow dns updates", "secure only");
2431 lpcfg_do_global_parameter(lp_ctx, "dns forwarder", "");
2433 lpcfg_do_global_parameter(lp_ctx, "algorithmic rid base", "1000");
2435 lpcfg_do_global_parameter(lp_ctx, "enhanced browsing", "True");
2437 lpcfg_do_global_parameter(lp_ctx, "winbind nss info", "template");
2439 lpcfg_do_global_parameter(lp_ctx, "server schannel", "Auto");
2441 lpcfg_do_global_parameter(lp_ctx, "short preserve case", "True");
2443 lpcfg_do_global_parameter(lp_ctx, "max open files", "16384");
2445 lpcfg_do_global_parameter(lp_ctx, "cups connection timeout", "30");
2447 lpcfg_do_global_parameter(lp_ctx, "locking", "True");
2449 lpcfg_do_global_parameter(lp_ctx, "block size", "1024");
2451 lpcfg_do_global_parameter(lp_ctx, "client use spnego", "True");
2453 lpcfg_do_global_parameter(lp_ctx, "change notify", "True");
2455 lpcfg_do_global_parameter(lp_ctx, "name cache timeout", "660");
2457 lpcfg_do_global_parameter(lp_ctx, "defer sharing violations", "True");
2459 lpcfg_do_global_parameter(lp_ctx, "ldap replication sleep", "1000");
2461 lpcfg_do_global_parameter(lp_ctx, "idmap backend", "tdb");
2463 lpcfg_do_global_parameter(lp_ctx, "enable privileges", "True");
2465 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max write", "%u", DEFAULT_SMB2_MAX_WRITE);
2467 lpcfg_do_global_parameter(lp_ctx, "passdb backend", "tdbsam");
2469 lpcfg_do_global_parameter(lp_ctx, "getwd cache", "True");
2471 lpcfg_do_global_parameter(lp_ctx, "winbind nested groups", "True");
2473 lpcfg_do_global_parameter(lp_ctx, "mangled names", "True");
2475 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max credits", "%u", DEFAULT_SMB2_MAX_CREDITS);
2477 lpcfg_do_global_parameter(lp_ctx, "ldap ssl", "start tls");
2479 lpcfg_do_global_parameter(lp_ctx, "ldap deref", "auto");
2481 lpcfg_do_global_parameter(lp_ctx, "lm interval", "60");
2483 lpcfg_do_global_parameter(lp_ctx, "mangling method", "hash2");
2485 lpcfg_do_global_parameter(lp_ctx, "hide dot files", "True");
2487 lpcfg_do_global_parameter(lp_ctx, "browse list", "True");
2489 lpcfg_do_global_parameter(lp_ctx, "passwd chat timeout", "2");
2491 lpcfg_do_global_parameter(lp_ctx, "guest account", GUEST_ACCOUNT);
2493 lpcfg_do_global_parameter(lp_ctx, "client schannel", "auto");
2495 lpcfg_do_global_parameter(lp_ctx, "smb encrypt", "default");
2497 lpcfg_do_global_parameter(lp_ctx, "max log size", "5000");
2499 lpcfg_do_global_parameter(lp_ctx, "idmap negative cache time", "120");
2501 lpcfg_do_global_parameter(lp_ctx, "ldap follow referral", "auto");
2503 lpcfg_do_global_parameter(lp_ctx, "multicast dns register", "yes");
2505 lpcfg_do_global_parameter(lp_ctx, "winbind reconnect delay", "30");
2507 lpcfg_do_global_parameter(lp_ctx, "winbind request timeout", "60");
2509 lpcfg_do_global_parameter(lp_ctx, "nt acl support", "yes");
2511 lpcfg_do_global_parameter(lp_ctx, "acl check permissions", "yes");
2513 lpcfg_do_global_parameter(lp_ctx, "keepalive", "300");
2515 lpcfg_do_global_parameter(lp_ctx, "winbind cache time", "300");
2517 lpcfg_do_global_parameter(lp_ctx, "level2 oplocks", "yes");
2519 lpcfg_do_global_parameter(lp_ctx, "show add printer wizard", "yes");
2521 lpcfg_do_global_parameter(lp_ctx, "allocation roundup size", "1048576");
2523 lpcfg_do_global_parameter(lp_ctx, "ldap page size", "1024");
2525 lpcfg_do_global_parameter(lp_ctx, "kernel share modes", "yes");
2527 lpcfg_do_global_parameter(lp_ctx, "strict locking", "Auto");
2529 lpcfg_do_global_parameter(lp_ctx, "map readonly", "yes");
2531 lpcfg_do_global_parameter(lp_ctx, "allow trusted domains", "yes");
2533 lpcfg_do_global_parameter(lp_ctx, "default devmode", "yes");
2535 lpcfg_do_global_parameter(lp_ctx, "os level", "20");
2537 lpcfg_do_global_parameter(lp_ctx, "dos filetimes", "yes");
2539 lpcfg_do_global_parameter(lp_ctx, "mangling char", "~");
2541 lpcfg_do_global_parameter(lp_ctx, "printcap cache time", "750");
2543 lpcfg_do_global_parameter(lp_ctx, "create krb5 conf", "yes");
2545 lpcfg_do_global_parameter(lp_ctx, "winbind max clients", "200");
2547 lpcfg_do_global_parameter(lp_ctx, "acl map full control", "yes");
2549 lpcfg_do_global_parameter(lp_ctx, "nt pipe support", "yes");
2551 lpcfg_do_global_parameter(lp_ctx, "ldap debug threshold", "10");
2553 lpcfg_do_global_parameter(lp_ctx, "follow symlinks", "yes");
2555 lpcfg_do_global_parameter(lp_ctx, "machine password timeout", "604800");
2557 lpcfg_do_global_parameter(lp_ctx, "ldap connection timeout", "2");
2559 lpcfg_do_global_parameter(lp_ctx, "winbind expand groups", "1");
2561 lpcfg_do_global_parameter(lp_ctx, "stat cache", "yes");
2563 lpcfg_do_global_parameter(lp_ctx, "lpq cache time", "30");
2565 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max trans", "%u", DEFAULT_SMB2_MAX_TRANSACT);
2567 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max read", "%u", DEFAULT_SMB2_MAX_READ);
2569 lpcfg_do_global_parameter(lp_ctx, "durable handles", "yes");
2571 lpcfg_do_global_parameter(lp_ctx, "max stat cache size", "256");
2573 lpcfg_do_global_parameter(lp_ctx, "ldap passwd sync", "no");
2575 lpcfg_do_global_parameter(lp_ctx, "kernel change notify", "yes");
2577 lpcfg_do_global_parameter(lp_ctx, "max ttl", "259200");
2579 lpcfg_do_global_parameter(lp_ctx, "blocking locks", "yes");
2581 lpcfg_do_global_parameter(lp_ctx, "oplock contention limit", "2");
2583 lpcfg_do_global_parameter(lp_ctx, "load printers", "yes");
2585 lpcfg_do_global_parameter(lp_ctx, "idmap cache time", "604800");
2587 lpcfg_do_global_parameter(lp_ctx, "preserve case", "yes");
2589 lpcfg_do_global_parameter(lp_ctx, "lm announce", "auto");
2591 lpcfg_do_global_parameter(lp_ctx, "afs token lifetime", "604800");
2593 lpcfg_do_global_parameter(lp_ctx, "enable core files", "yes");
2595 lpcfg_do_global_parameter(lp_ctx, "winbind max domain connections", "1");
2597 lpcfg_do_global_parameter(lp_ctx, "case sensitive", "auto");
2599 lpcfg_do_global_parameter(lp_ctx, "ldap timeout", "15");
2601 lpcfg_do_global_parameter(lp_ctx, "mangle prefix", "1");
2603 lpcfg_do_global_parameter(lp_ctx, "posix locking", "yes");
2605 lpcfg_do_global_parameter(lp_ctx, "lock spin time", "200");
2607 lpcfg_do_global_parameter(lp_ctx, "directory name cache size", "100");
2609 lpcfg_do_global_parameter(lp_ctx, "nmbd bind explicit broadcast", "yes");
2611 lpcfg_do_global_parameter(lp_ctx, "init logon delay", "100");
2613 lpcfg_do_global_parameter(lp_ctx, "usershare owner only", "yes");
2615 lpcfg_do_global_parameter(lp_ctx, "-valid", "yes");
2617 lpcfg_do_global_parameter_var(lp_ctx, "usershare path", "%s/usershares", get_dyn_STATEDIR());
2619 #ifdef DEVELOPER
2620 lpcfg_do_global_parameter_var(lp_ctx, "panic action", "/bin/sleep 999999999");
2621 #endif
2623 lpcfg_do_global_parameter(lp_ctx, "smb passwd file", get_dyn_SMB_PASSWD_FILE());
2625 lpcfg_do_global_parameter(lp_ctx, "logon home", "\\\\%N\\%U");
2627 lpcfg_do_global_parameter(lp_ctx, "logon path", "\\\\%N\\%U\\profile");
2629 lpcfg_do_global_parameter(lp_ctx, "printjob username", "%U");
2631 for (i = 0; parm_table[i].label; i++) {
2632 if (!(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2633 lp_ctx->flags[i] |= FLAG_DEFAULT;
2637 for (parm=lp_ctx->globals->param_opt; parm; parm=parm->next) {
2638 if (!(parm->priority & FLAG_CMDLINE)) {
2639 parm->priority |= FLAG_DEFAULT;
2643 for (parm=lp_ctx->sDefault->param_opt; parm; parm=parm->next) {
2644 if (!(parm->priority & FLAG_CMDLINE)) {
2645 parm->priority |= FLAG_DEFAULT;
2650 return lp_ctx;
2654 * Initialise the global parameter structure.
2656 struct loadparm_context *loadparm_init_global(bool load_default)
2658 if (global_loadparm_context == NULL) {
2659 global_loadparm_context = loadparm_init(NULL);
2661 if (global_loadparm_context == NULL) {
2662 return NULL;
2664 global_loadparm_context->global = true;
2665 if (load_default && !global_loadparm_context->loaded) {
2666 lpcfg_load_default(global_loadparm_context);
2668 global_loadparm_context->refuse_free = true;
2669 return global_loadparm_context;
2673 * Initialise the global parameter structure.
2675 struct loadparm_context *loadparm_init_s3(TALLOC_CTX *mem_ctx,
2676 const struct loadparm_s3_helpers *s3_fns)
2678 struct loadparm_context *loadparm_context = talloc_zero(mem_ctx, struct loadparm_context);
2679 if (!loadparm_context) {
2680 return NULL;
2682 loadparm_context->s3_fns = s3_fns;
2683 loadparm_context->globals = s3_fns->globals;
2684 loadparm_context->flags = s3_fns->flags;
2686 return loadparm_context;
2689 const char *lpcfg_configfile(struct loadparm_context *lp_ctx)
2691 return lp_ctx->szConfigFile;
2694 const char *lp_default_path(void)
2696 if (getenv("SMB_CONF_PATH"))
2697 return getenv("SMB_CONF_PATH");
2698 else
2699 return dyn_CONFIGFILE;
2703 * Update the internal state of a loadparm context after settings
2704 * have changed.
2706 static bool lpcfg_update(struct loadparm_context *lp_ctx)
2708 struct debug_settings settings;
2709 TALLOC_CTX *tmp_ctx;
2711 tmp_ctx = talloc_new(lp_ctx);
2712 if (tmp_ctx == NULL) {
2713 return false;
2716 lpcfg_add_auto_services(lp_ctx, lpcfg_auto_services(lp_ctx, tmp_ctx));
2718 if (!lp_ctx->globals->wins_server_list && lp_ctx->globals->we_are_a_wins_server) {
2719 lpcfg_do_global_parameter(lp_ctx, "wins server", "127.0.0.1");
2722 if (!lp_ctx->global) {
2723 TALLOC_FREE(tmp_ctx);
2724 return true;
2727 panic_action = lp_ctx->globals->panic_action;
2729 reload_charcnv(lp_ctx);
2731 ZERO_STRUCT(settings);
2732 /* Add any more debug-related smb.conf parameters created in
2733 * future here */
2734 settings.syslog = lp_ctx->globals->syslog;
2735 settings.syslog_only = lp_ctx->globals->syslog_only;
2736 settings.timestamp_logs = lp_ctx->globals->timestamp_logs;
2737 settings.debug_prefix_timestamp = lp_ctx->globals->debug_prefix_timestamp;
2738 settings.debug_hires_timestamp = lp_ctx->globals->debug_hires_timestamp;
2739 settings.debug_pid = lp_ctx->globals->debug_pid;
2740 settings.debug_uid = lp_ctx->globals->debug_uid;
2741 settings.debug_class = lp_ctx->globals->debug_class;
2742 debug_set_settings(&settings);
2744 /* FIXME: This is a bit of a hack, but we can't use a global, since
2745 * not everything that uses lp also uses the socket library */
2746 if (lpcfg_parm_bool(lp_ctx, NULL, "socket", "testnonblock", false)) {
2747 setenv("SOCKET_TESTNONBLOCK", "1", 1);
2748 } else {
2749 unsetenv("SOCKET_TESTNONBLOCK");
2752 TALLOC_FREE(tmp_ctx);
2753 return true;
2756 bool lpcfg_load_default(struct loadparm_context *lp_ctx)
2758 const char *path;
2760 path = lp_default_path();
2762 if (!file_exist(path)) {
2763 /* We allow the default smb.conf file to not exist,
2764 * basically the equivalent of an empty file. */
2765 return lpcfg_update(lp_ctx);
2768 return lpcfg_load(lp_ctx, path);
2772 * Load the services array from the services file.
2774 * Return True on success, False on failure.
2776 bool lpcfg_load(struct loadparm_context *lp_ctx, const char *filename)
2778 char *n2;
2779 bool bRetval;
2781 filename = talloc_strdup(lp_ctx, filename);
2783 lp_ctx->szConfigFile = filename;
2785 if (lp_ctx->s3_fns) {
2786 return lp_ctx->s3_fns->load(filename);
2789 lp_ctx->bInGlobalSection = true;
2790 n2 = standard_sub_basic(lp_ctx, lp_ctx->szConfigFile);
2791 DEBUG(2, ("lpcfg_load: refreshing parameters from %s\n", n2));
2793 add_to_file_list(lp_ctx, &lp_ctx->file_lists, lp_ctx->szConfigFile, n2);
2795 /* We get sections first, so have to start 'behind' to make up */
2796 lp_ctx->currentService = NULL;
2797 bRetval = pm_process(n2, do_section, lpcfg_do_parameter, lp_ctx);
2799 /* finish up the last section */
2800 DEBUG(4, ("pm_process() returned %s\n", BOOLSTR(bRetval)));
2801 if (bRetval)
2802 if (lp_ctx->currentService != NULL)
2803 bRetval = lpcfg_service_ok(lp_ctx->currentService);
2805 bRetval = bRetval && lpcfg_update(lp_ctx);
2807 /* we do this unconditionally, so that it happens even
2808 for a missing smb.conf */
2809 reload_charcnv(lp_ctx);
2811 if (bRetval == true) {
2812 /* set this up so that any child python tasks will
2813 find the right smb.conf */
2814 setenv("SMB_CONF_PATH", filename, 1);
2816 /* set the context used by the lp_*() function
2817 varients */
2818 global_loadparm_context = lp_ctx;
2819 lp_ctx->loaded = true;
2822 return bRetval;
2826 * Return the max number of services.
2829 int lpcfg_numservices(struct loadparm_context *lp_ctx)
2831 if (lp_ctx->s3_fns) {
2832 return lp_ctx->s3_fns->get_numservices();
2835 return lp_ctx->iNumServices;
2839 * Display the contents of the services array in human-readable form.
2842 void lpcfg_dump(struct loadparm_context *lp_ctx, FILE *f, bool show_defaults,
2843 int maxtoprint)
2845 int iService;
2847 if (lp_ctx->s3_fns) {
2848 lp_ctx->s3_fns->dump(f, show_defaults, maxtoprint);
2849 return;
2852 lpcfg_dump_globals(lp_ctx, f, show_defaults);
2854 lpcfg_dump_a_service(lp_ctx->sDefault, lp_ctx->sDefault, f, lp_ctx->flags, show_defaults);
2856 for (iService = 0; iService < maxtoprint; iService++)
2857 lpcfg_dump_one(f, show_defaults, lp_ctx->services[iService], lp_ctx->sDefault);
2861 * Display the contents of one service in human-readable form.
2863 void lpcfg_dump_one(FILE *f, bool show_defaults, struct loadparm_service *service, struct loadparm_service *sDefault)
2865 if (service != NULL) {
2866 if (service->szService[0] == '\0')
2867 return;
2868 lpcfg_dump_a_service(service, sDefault, f, NULL, show_defaults);
2872 struct loadparm_service *lpcfg_servicebynum(struct loadparm_context *lp_ctx,
2873 int snum)
2875 if (lp_ctx->s3_fns) {
2876 return lp_ctx->s3_fns->get_servicebynum(snum);
2879 return lp_ctx->services[snum];
2882 struct loadparm_service *lpcfg_service(struct loadparm_context *lp_ctx,
2883 const char *service_name)
2885 int iService;
2886 char *serviceName;
2888 if (lp_ctx->s3_fns) {
2889 return lp_ctx->s3_fns->get_service(service_name);
2892 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--) {
2893 if (lp_ctx->services[iService] &&
2894 lp_ctx->services[iService]->szService) {
2896 * The substitution here is used to support %U is
2897 * service names
2899 serviceName = standard_sub_basic(
2900 lp_ctx->services[iService],
2901 lp_ctx->services[iService]->szService);
2902 if (strequal(serviceName, service_name)) {
2903 talloc_free(serviceName);
2904 return lp_ctx->services[iService];
2906 talloc_free(serviceName);
2910 DEBUG(7,("lpcfg_servicenumber: couldn't find %s\n", service_name));
2911 return NULL;
2914 const char *lpcfg_servicename(const struct loadparm_service *service)
2916 return lpcfg_string((const char *)service->szService);
2920 * A useful volume label function.
2922 const char *lpcfg_volume_label(struct loadparm_service *service, struct loadparm_service *sDefault)
2924 const char *ret;
2925 ret = lpcfg_string((const char *)((service != NULL && service->volume != NULL) ?
2926 service->volume : sDefault->volume));
2927 if (!*ret)
2928 return lpcfg_servicename(service);
2929 return ret;
2933 * Return the correct printer name.
2935 const char *lpcfg_printername(struct loadparm_service *service, struct loadparm_service *sDefault)
2937 const char *ret;
2938 ret = lpcfg_string((const char *)((service != NULL && service->_printername != NULL) ?
2939 service->_printername : sDefault->_printername));
2940 if (ret == NULL || (ret != NULL && *ret == '\0'))
2941 ret = lpcfg_servicename(service);
2943 return ret;
2948 * Return the max print jobs per queue.
2950 int lpcfg_maxprintjobs(struct loadparm_service *service, struct loadparm_service *sDefault)
2952 int maxjobs = (service != NULL) ? service->iMaxPrintJobs : sDefault->iMaxPrintJobs;
2953 if (maxjobs <= 0 || maxjobs >= PRINT_MAX_JOBID)
2954 maxjobs = PRINT_MAX_JOBID - 1;
2956 return maxjobs;
2959 struct smb_iconv_handle *lpcfg_iconv_handle(struct loadparm_context *lp_ctx)
2961 if (lp_ctx == NULL) {
2962 return get_iconv_handle();
2964 return lp_ctx->iconv_handle;
2967 _PUBLIC_ void reload_charcnv(struct loadparm_context *lp_ctx)
2969 struct smb_iconv_handle *old_ic = lp_ctx->iconv_handle;
2970 if (!lp_ctx->global) {
2971 return;
2974 if (old_ic == NULL) {
2975 old_ic = global_iconv_handle;
2977 lp_ctx->iconv_handle = smb_iconv_handle_reinit_lp(lp_ctx, lp_ctx, old_ic);
2978 global_iconv_handle = lp_ctx->iconv_handle;
2981 _PUBLIC_ char *lpcfg_tls_keyfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2983 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_keyfile(lp_ctx));
2986 _PUBLIC_ char *lpcfg_tls_certfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2988 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_certfile(lp_ctx));
2991 _PUBLIC_ char *lpcfg_tls_cafile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2993 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_cafile(lp_ctx));
2996 _PUBLIC_ char *lpcfg_tls_crlfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2998 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_crlfile(lp_ctx));
3001 _PUBLIC_ char *lpcfg_tls_dhpfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3003 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_dhpfile(lp_ctx));
3006 struct gensec_settings *lpcfg_gensec_settings(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3008 struct gensec_settings *settings = talloc_zero(mem_ctx, struct gensec_settings);
3009 if (settings == NULL)
3010 return NULL;
3011 SMB_ASSERT(lp_ctx != NULL);
3012 settings->lp_ctx = talloc_reference(settings, lp_ctx);
3013 settings->target_hostname = lpcfg_parm_string(lp_ctx, NULL, "gensec", "target_hostname");
3014 return settings;
3017 int lpcfg_server_role(struct loadparm_context *lp_ctx)
3019 int domain_master = lpcfg__domain_master(lp_ctx);
3021 return lp_find_server_role(lpcfg__server_role(lp_ctx),
3022 lpcfg__security(lp_ctx),
3023 lpcfg__domain_logons(lp_ctx),
3024 (domain_master == true) ||
3025 (domain_master == Auto));
3028 int lpcfg_security(struct loadparm_context *lp_ctx)
3030 return lp_find_security(lpcfg__server_role(lp_ctx),
3031 lpcfg__security(lp_ctx));
3034 bool lpcfg_server_signing_allowed(struct loadparm_context *lp_ctx, bool *mandatory)
3036 bool allowed = true;
3037 enum smb_signing_setting signing_setting = lpcfg_server_signing(lp_ctx);
3039 *mandatory = false;
3041 if (signing_setting == SMB_SIGNING_DEFAULT) {
3043 * If we are a domain controller, SMB signing is
3044 * really important, as it can prevent a number of
3045 * attacks on communications between us and the
3046 * clients
3048 * However, it really sucks (no sendfile, CPU
3049 * overhead) performance-wise when used on a
3050 * file server, so disable it by default
3051 * on non-DCs
3054 if (lpcfg_server_role(lp_ctx) >= ROLE_ACTIVE_DIRECTORY_DC) {
3055 signing_setting = SMB_SIGNING_REQUIRED;
3056 } else {
3057 signing_setting = SMB_SIGNING_OFF;
3061 switch (signing_setting) {
3062 case SMB_SIGNING_REQUIRED:
3063 *mandatory = true;
3064 break;
3065 case SMB_SIGNING_IF_REQUIRED:
3066 break;
3067 case SMB_SIGNING_DEFAULT:
3068 case SMB_SIGNING_OFF:
3069 allowed = false;
3070 break;
3073 return allowed;
3076 int lpcfg_tdb_hash_size(struct loadparm_context *lp_ctx, const char *name)
3078 const char *base;
3080 if (name == NULL) {
3081 return 0;
3084 base = strrchr_m(name, '/');
3085 if (base != NULL) {
3086 base += 1;
3087 } else {
3088 base = name;
3090 return lpcfg_parm_int(lp_ctx, NULL, "tdb_hashsize", base, 0);
3094 int lpcfg_tdb_flags(struct loadparm_context *lp_ctx, int tdb_flags)
3096 if (!lpcfg_use_mmap(lp_ctx)) {
3097 tdb_flags |= TDB_NOMMAP;
3099 return tdb_flags;