param: add extra condition in lpcfg_service_ok
[Samba.git] / lib / param / loadparm.c
blob2d9bee2ab6d97824232a2afe64e064fed78e0b6c
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 lpcfg_service_ok(struct loadparm_service *service);
233 static bool do_section(const char *pszSectionName, void *);
234 static bool set_variable_helper(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
235 const char *pszParmName, const char *pszParmValue);
236 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
237 struct loadparm_service *service,
238 const char *pszParmName,
239 const char *pszParmValue, int flags);
241 /* The following are helper functions for parametrical options support. */
242 /* It returns a pointer to parametrical option value if it exists or NULL otherwise */
243 /* Actual parametrical functions are quite simple */
244 struct parmlist_entry *get_parametric_helper(struct loadparm_service *service,
245 const char *type, const char *option,
246 struct parmlist_entry *global_opts)
248 char* param_key;
249 struct parmlist_entry *data = NULL;
250 TALLOC_CTX *mem_ctx = talloc_stackframe();
252 param_key = talloc_asprintf(mem_ctx, "%s:%s", type, option);
253 if (param_key == NULL) {
254 DEBUG(0,("asprintf failed!\n"));
255 TALLOC_FREE(mem_ctx);
256 return NULL;
260 * Try to fetch the option from the data.
262 if (service != NULL) {
263 data = service->param_opt;
264 while (data != NULL) {
265 if (strwicmp(data->key, param_key) == 0) {
266 TALLOC_FREE(mem_ctx);
267 return data;
269 data = data->next;
274 * Fall back to fetching from the globals.
276 data = global_opts;
277 while (data != NULL) {
278 if (strwicmp(data->key, param_key) == 0) {
279 TALLOC_FREE(mem_ctx);
280 return data;
282 data = data->next;
286 TALLOC_FREE(mem_ctx);
288 return NULL;
293 const char *lpcfg_get_parametric(struct loadparm_context *lp_ctx,
294 struct loadparm_service *service,
295 const char *type, const char *option)
297 struct parmlist_entry *data;
299 if (lp_ctx == NULL)
300 return NULL;
302 data = get_parametric_helper(service,
303 type, option, lp_ctx->globals->param_opt);
305 if (data == NULL) {
306 return NULL;
307 } else {
308 return data->value;
314 * convenience routine to return int parameters.
316 int lp_int(const char *s)
319 if (!s || !*s) {
320 DEBUG(0,("lp_int(%s): is called with NULL!\n",s));
321 return -1;
324 return strtol(s, NULL, 0);
328 * convenience routine to return unsigned long parameters.
330 unsigned long lp_ulong(const char *s)
333 if (!s || !*s) {
334 DEBUG(0,("lp_ulong(%s): is called with NULL!\n",s));
335 return -1;
338 return strtoul(s, NULL, 0);
342 * convenience routine to return unsigned long parameters.
344 static long lp_long(const char *s)
347 if (!s) {
348 DEBUG(0,("lp_long(%s): is called with NULL!\n",s));
349 return -1;
352 return strtol(s, NULL, 0);
356 * convenience routine to return unsigned long parameters.
358 static double lp_double(const char *s)
361 if (!s) {
362 DEBUG(0,("lp_double(%s): is called with NULL!\n",s));
363 return -1;
366 return strtod(s, NULL);
370 * convenience routine to return boolean parameters.
372 bool lp_bool(const char *s)
374 bool ret = false;
376 if (!s || !*s) {
377 DEBUG(0,("lp_bool(%s): is called with NULL!\n",s));
378 return false;
381 if (!set_boolean(s, &ret)) {
382 DEBUG(0,("lp_bool(%s): value is not boolean!\n",s));
383 return false;
386 return ret;
390 * Return parametric option from a given service. Type is a part of option before ':'
391 * Parametric option has following syntax: 'Type: option = value'
392 * Returned value is allocated in 'lp_talloc' context
395 const char *lpcfg_parm_string(struct loadparm_context *lp_ctx,
396 struct loadparm_service *service, const char *type,
397 const char *option)
399 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
401 if (value)
402 return lpcfg_string(value);
404 return NULL;
408 * Return parametric option from a given service. Type is a part of option before ':'
409 * Parametric option has following syntax: 'Type: option = value'
410 * Returned value is allocated in 'lp_talloc' context
413 const char **lpcfg_parm_string_list(TALLOC_CTX *mem_ctx,
414 struct loadparm_context *lp_ctx,
415 struct loadparm_service *service,
416 const char *type,
417 const char *option, const char *separator)
419 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
421 if (value != NULL)
422 return (const char **)str_list_make(mem_ctx, value, separator);
424 return NULL;
428 * Return parametric option from a given service. Type is a part of option before ':'
429 * Parametric option has following syntax: 'Type: option = value'
432 int lpcfg_parm_int(struct loadparm_context *lp_ctx,
433 struct loadparm_service *service, const char *type,
434 const char *option, int default_v)
436 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
438 if (value)
439 return lp_int(value);
441 return default_v;
445 * Return parametric option from a given service. Type is a part of
446 * option before ':'.
447 * Parametric option has following syntax: 'Type: option = value'.
450 int lpcfg_parm_bytes(struct loadparm_context *lp_ctx,
451 struct loadparm_service *service, const char *type,
452 const char *option, int default_v)
454 uint64_t bval;
456 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
458 if (value && conv_str_size_error(value, &bval)) {
459 if (bval <= INT_MAX) {
460 return (int)bval;
464 return default_v;
468 * Return parametric option from a given service.
469 * Type is a part of option before ':'
470 * Parametric option has following syntax: 'Type: option = value'
472 unsigned long lpcfg_parm_ulong(struct loadparm_context *lp_ctx,
473 struct loadparm_service *service, const char *type,
474 const char *option, unsigned long default_v)
476 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
478 if (value)
479 return lp_ulong(value);
481 return default_v;
484 long lpcfg_parm_long(struct loadparm_context *lp_ctx,
485 struct loadparm_service *service, const char *type,
486 const char *option, long default_v)
488 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
490 if (value)
491 return lp_long(value);
493 return default_v;
496 double lpcfg_parm_double(struct loadparm_context *lp_ctx,
497 struct loadparm_service *service, const char *type,
498 const char *option, double default_v)
500 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
502 if (value != NULL)
503 return lp_double(value);
505 return default_v;
509 * Return parametric option from a given service. Type is a part of option before ':'
510 * Parametric option has following syntax: 'Type: option = value'
513 bool lpcfg_parm_bool(struct loadparm_context *lp_ctx,
514 struct loadparm_service *service, const char *type,
515 const char *option, bool default_v)
517 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
519 if (value != NULL)
520 return lp_bool(value);
522 return default_v;
527 * Set a string value, deallocating any existing space, and allocing the space
528 * for the string
530 bool lpcfg_string_set(TALLOC_CTX *mem_ctx, char **dest, const char *src)
532 talloc_free(*dest);
534 if (src == NULL)
535 src = "";
537 *dest = talloc_strdup(mem_ctx, src);
538 if ((*dest) == NULL) {
539 DEBUG(0,("Out of memory in string_set\n"));
540 return false;
543 return true;
547 * Set a string value, deallocating any existing space, and allocing the space
548 * for the string
550 bool lpcfg_string_set_upper(TALLOC_CTX *mem_ctx, char **dest, const char *src)
552 talloc_free(*dest);
554 if (src == NULL)
555 src = "";
557 *dest = strupper_talloc(mem_ctx, src);
558 if ((*dest) == NULL) {
559 DEBUG(0,("Out of memory in string_set_upper\n"));
560 return false;
563 return true;
569 * Add a new service to the services array initialising it with the given
570 * service.
573 struct loadparm_service *lpcfg_add_service(struct loadparm_context *lp_ctx,
574 const struct loadparm_service *pservice,
575 const char *name)
577 int i;
578 int num_to_alloc = lp_ctx->iNumServices + 1;
579 struct parmlist_entry *data, *pdata;
581 if (pservice == NULL) {
582 pservice = lp_ctx->sDefault;
585 /* it might already exist */
586 if (name) {
587 struct loadparm_service *service = lpcfg_getservicebyname(lp_ctx,
588 name);
589 if (service != NULL) {
590 /* Clean all parametric options for service */
591 /* They will be added during parsing again */
592 data = service->param_opt;
593 while (data) {
594 pdata = data->next;
595 talloc_free(data);
596 data = pdata;
598 service->param_opt = NULL;
599 return service;
603 /* find an invalid one */
604 for (i = 0; i < lp_ctx->iNumServices; i++)
605 if (lp_ctx->services[i] == NULL)
606 break;
608 /* if not, then create one */
609 if (i == lp_ctx->iNumServices) {
610 struct loadparm_service **tsp;
612 tsp = talloc_realloc(lp_ctx, lp_ctx->services, struct loadparm_service *, num_to_alloc);
614 if (!tsp) {
615 DEBUG(0,("lpcfg_add_service: failed to enlarge services!\n"));
616 return NULL;
617 } else {
618 lp_ctx->services = tsp;
619 lp_ctx->services[lp_ctx->iNumServices] = NULL;
622 lp_ctx->iNumServices++;
625 lp_ctx->services[i] = talloc_zero(lp_ctx->services, struct loadparm_service);
626 if (lp_ctx->services[i] == NULL) {
627 DEBUG(0,("lpcfg_add_service: out of memory!\n"));
628 return NULL;
630 copy_service(lp_ctx->services[i], pservice, NULL);
631 if (name != NULL)
632 lpcfg_string_set(lp_ctx->services[i], &lp_ctx->services[i]->szService, name);
633 return lp_ctx->services[i];
637 * Add a new home service, with the specified home directory, defaults coming
638 * from service ifrom.
641 bool lpcfg_add_home(struct loadparm_context *lp_ctx,
642 const char *pszHomename,
643 struct loadparm_service *default_service,
644 const char *user, const char *pszHomedir)
646 struct loadparm_service *service;
648 service = lpcfg_add_service(lp_ctx, default_service, pszHomename);
650 if (service == NULL)
651 return false;
653 if (!(*(default_service->path))
654 || strequal(default_service->path, lp_ctx->sDefault->path)) {
655 service->path = talloc_strdup(service, pszHomedir);
656 } else {
657 service->path = string_sub_talloc(service, lpcfg_path(default_service, lp_ctx->sDefault, service), "%H", pszHomedir);
660 if (!(*(service->comment))) {
661 service->comment = talloc_asprintf(service, "Home directory of %s", user);
663 service->bAvailable = default_service->bAvailable;
664 service->browseable = default_service->browseable;
666 DEBUG(3, ("adding home's share [%s] for user '%s' at '%s'\n",
667 pszHomename, user, service->path));
669 return true;
673 * Add a new printer service, with defaults coming from service iFrom.
676 bool lpcfg_add_printer(struct loadparm_context *lp_ctx,
677 const char *pszPrintername,
678 struct loadparm_service *default_service)
680 const char *comment = "From Printcap";
681 struct loadparm_service *service;
682 service = lpcfg_add_service(lp_ctx, default_service, pszPrintername);
684 if (service == NULL)
685 return false;
687 /* note that we do NOT default the availability flag to True - */
688 /* we take it from the default service passed. This allows all */
689 /* dynamic printers to be disabled by disabling the [printers] */
690 /* entry (if/when the 'available' keyword is implemented!). */
692 /* the printer name is set to the service name. */
693 lpcfg_string_set(service, &service->_printername, pszPrintername);
694 lpcfg_string_set(service, &service->comment, comment);
695 service->browseable = default_service->browseable;
696 /* Printers cannot be read_only. */
697 service->read_only = false;
698 /* Printer services must be printable. */
699 service->printable = true;
701 DEBUG(3, ("adding printer service %s\n", pszPrintername));
703 return true;
707 * Map a parameter's string representation to something we can use.
708 * Returns False if the parameter string is not recognised, else TRUE.
711 int lpcfg_map_parameter(const char *pszParmName)
713 int iIndex;
715 for (iIndex = 0; parm_table[iIndex].label; iIndex++)
716 if (strwicmp(parm_table[iIndex].label, pszParmName) == 0)
717 return iIndex;
719 /* Warn only if it isn't parametric option */
720 if (strchr(pszParmName, ':') == NULL)
721 DEBUG(0, ("Unknown parameter encountered: \"%s\"\n", pszParmName));
722 /* We do return 'fail' for parametric options as well because they are
723 stored in different storage
725 return -1;
730 return the parameter structure for a parameter
732 struct parm_struct *lpcfg_parm_struct(struct loadparm_context *lp_ctx, const char *name)
734 int parmnum;
736 if (lp_ctx->s3_fns) {
737 return lp_ctx->s3_fns->get_parm_struct(name);
740 parmnum = lpcfg_map_parameter(name);
741 if (parmnum == -1) return NULL;
742 return &parm_table[parmnum];
746 return the parameter pointer for a parameter
748 void *lpcfg_parm_ptr(struct loadparm_context *lp_ctx,
749 struct loadparm_service *service, struct parm_struct *parm)
751 if (lp_ctx->s3_fns) {
752 return lp_ctx->s3_fns->get_parm_ptr(service, parm);
755 if (service == NULL) {
756 if (parm->p_class == P_LOCAL)
757 return ((char *)lp_ctx->sDefault)+parm->offset;
758 else if (parm->p_class == P_GLOBAL)
759 return ((char *)lp_ctx->globals)+parm->offset;
760 else return NULL;
761 } else {
762 return ((char *)service) + parm->offset;
767 return the parameter pointer for a parameter
769 bool lpcfg_parm_is_cmdline(struct loadparm_context *lp_ctx, const char *name)
771 int parmnum;
773 parmnum = lpcfg_map_parameter(name);
774 if (parmnum == -1) return false;
776 return lp_ctx->flags[parmnum] & FLAG_CMDLINE;
780 * Find a service by name. Otherwise works like get_service.
783 static struct loadparm_service *lpcfg_getservicebyname(struct loadparm_context *lp_ctx,
784 const char *pszServiceName)
786 int iService;
788 if (lp_ctx->s3_fns) {
789 return lp_ctx->s3_fns->get_service(pszServiceName);
792 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--)
793 if (lp_ctx->services[iService] != NULL &&
794 strwicmp(lp_ctx->services[iService]->szService, pszServiceName) == 0) {
795 return lp_ctx->services[iService];
798 return NULL;
802 * Add a parametric option to a parmlist_entry,
803 * replacing old value, if already present.
805 void set_param_opt(TALLOC_CTX *mem_ctx,
806 struct parmlist_entry **opt_list,
807 const char *opt_name,
808 const char *opt_value,
809 unsigned priority)
811 struct parmlist_entry *new_opt, *opt;
812 bool not_added;
814 opt = *opt_list;
815 not_added = true;
817 /* Traverse destination */
818 while (opt) {
819 /* If we already have same option, override it */
820 if (strwicmp(opt->key, opt_name) == 0) {
821 if ((opt->priority & FLAG_CMDLINE) &&
822 !(priority & FLAG_CMDLINE)) {
823 /* it's been marked as not to be
824 overridden */
825 return;
827 TALLOC_FREE(opt->value);
828 TALLOC_FREE(opt->list);
829 opt->value = talloc_strdup(opt, opt_value);
830 opt->priority = priority;
831 not_added = false;
832 break;
834 opt = opt->next;
836 if (not_added) {
837 new_opt = talloc(mem_ctx, struct parmlist_entry);
838 if (new_opt == NULL) {
839 smb_panic("OOM");
842 new_opt->key = talloc_strdup(new_opt, opt_name);
843 if (new_opt->key == NULL) {
844 smb_panic("talloc_strdup failed");
847 new_opt->value = talloc_strdup(new_opt, opt_value);
848 if (new_opt->value == NULL) {
849 smb_panic("talloc_strdup failed");
852 new_opt->list = NULL;
853 new_opt->priority = priority;
854 DLIST_ADD(*opt_list, new_opt);
859 * Copy a service structure to another.
860 * If pcopymapDest is NULL then copy all fields
863 void copy_service(struct loadparm_service *pserviceDest,
864 const struct loadparm_service *pserviceSource,
865 struct bitmap *pcopymapDest)
867 int i;
868 bool bcopyall = (pcopymapDest == NULL);
869 struct parmlist_entry *data;
871 for (i = 0; parm_table[i].label; i++)
872 if (parm_table[i].p_class == P_LOCAL &&
873 (bcopyall || bitmap_query(pcopymapDest, i))) {
874 const void *src_ptr =
875 ((const char *)pserviceSource) + parm_table[i].offset;
876 void *dest_ptr =
877 ((char *)pserviceDest) + parm_table[i].offset;
879 switch (parm_table[i].type) {
880 case P_BOOL:
881 case P_BOOLREV:
882 *(bool *)dest_ptr = *(const bool *)src_ptr;
883 break;
885 case P_INTEGER:
886 case P_BYTES:
887 case P_OCTAL:
888 case P_ENUM:
889 *(int *)dest_ptr = *(const int *)src_ptr;
890 break;
892 case P_CHAR:
893 *(char *)dest_ptr = *(const char *)src_ptr;
894 break;
896 case P_STRING:
897 lpcfg_string_set(pserviceDest,
898 (char **)dest_ptr,
899 *(const char * const *)src_ptr);
900 break;
902 case P_USTRING:
903 lpcfg_string_set_upper(pserviceDest,
904 (char **)dest_ptr,
905 *(const char * const *)src_ptr);
906 break;
907 case P_CMDLIST:
908 case P_LIST:
909 TALLOC_FREE(*((char ***)dest_ptr));
910 *(const char * const **)dest_ptr = (const char * const *)str_list_copy(pserviceDest,
911 *(const char * * const *)src_ptr);
912 break;
913 default:
914 break;
918 if (bcopyall) {
919 init_copymap(pserviceDest);
920 if (pserviceSource->copymap)
921 bitmap_copy(pserviceDest->copymap,
922 pserviceSource->copymap);
925 for (data = pserviceSource->param_opt; data != NULL; data = data->next) {
926 set_param_opt(pserviceDest, &pserviceDest->param_opt,
927 data->key, data->value, data->priority);
932 * Check a service for consistency. Return False if the service is in any way
933 * incomplete or faulty, else True.
935 static bool lpcfg_service_ok(struct loadparm_service *service)
937 bool bRetval;
939 bRetval = true;
940 if (service->szService[0] == '\0') {
941 DEBUG(0, ("The following message indicates an internal error:\n"));
942 DEBUG(0, ("No service name in service entry.\n"));
943 bRetval = false;
946 /* The [printers] entry MUST be printable. I'm all for flexibility, but */
947 /* I can't see why you'd want a non-printable printer service... */
948 if (strwicmp(service->szService, PRINTERS_NAME) == 0) {
949 if (!service->printable) {
950 DEBUG(0, ("WARNING: [%s] service MUST be printable!\n",
951 service->szService));
952 service->printable = true;
954 /* [printers] service must also be non-browsable. */
955 if (service->browseable)
956 service->browseable = false;
959 if (service->path[0] == '\0' &&
960 strwicmp(service->szService, HOMES_NAME) != 0 &&
961 service->msdfs_proxy[0] == '\0')
963 DEBUG(0, ("WARNING: No path in service %s - making it unavailable!\n",
964 service->szService));
965 service->bAvailable = false;
968 /* If a service is flagged unavailable, log the fact at level 0. */
969 if (!service->bAvailable)
970 DEBUG(1, ("NOTE: Service %s is flagged unavailable.\n",
971 service->szService));
973 return bRetval;
977 /*******************************************************************
978 Keep a linked list of all config files so we know when one has changed
979 it's date and needs to be reloaded.
980 ********************************************************************/
982 void add_to_file_list(TALLOC_CTX *mem_ctx, struct file_lists **list,
983 const char *fname, const char *subfname)
985 struct file_lists *f = *list;
987 while (f) {
988 if (f->name && !strcmp(f->name, fname))
989 break;
990 f = f->next;
993 if (!f) {
994 f = talloc(mem_ctx, struct file_lists);
995 if (!f)
996 goto fail;
997 f->next = *list;
998 f->name = talloc_strdup(f, fname);
999 if (!f->name) {
1000 TALLOC_FREE(f);
1001 goto fail;
1003 f->subfname = talloc_strdup(f, subfname);
1004 if (!f->subfname) {
1005 TALLOC_FREE(f);
1006 goto fail;
1008 *list = f;
1009 f->modtime = file_modtime(subfname);
1010 } else {
1011 time_t t = file_modtime(subfname);
1012 if (t)
1013 f->modtime = t;
1015 return;
1017 fail:
1018 DEBUG(0, ("Unable to add file to file list: %s\n", fname));
1022 /*******************************************************************
1023 Check if a config file has changed date.
1024 ********************************************************************/
1025 bool lpcfg_file_list_changed(struct loadparm_context *lp_ctx)
1027 struct file_lists *f;
1028 DEBUG(6, ("lpcfg_file_list_changed()\n"));
1030 for (f = lp_ctx->file_lists; f != NULL; f = f->next) {
1031 char *n2;
1032 time_t mod_time;
1034 n2 = standard_sub_basic(lp_ctx, f->name);
1036 DEBUGADD(6, ("file %s -> %s last mod_time: %s\n",
1037 f->name, n2, ctime(&f->modtime)));
1039 mod_time = file_modtime(n2);
1041 if (mod_time && ((f->modtime != mod_time) || (f->subfname == NULL) || (strcmp(n2, f->subfname) != 0))) {
1042 DEBUGADD(6, ("file %s modified: %s\n", n2,
1043 ctime(&mod_time)));
1044 f->modtime = mod_time;
1045 talloc_free(f->subfname);
1046 f->subfname = talloc_strdup(f, n2);
1047 return true;
1050 return false;
1054 * set the value for a P_ENUM
1056 bool lp_set_enum_parm( struct parm_struct *parm, const char *pszParmValue,
1057 int *ptr )
1059 int i;
1061 for (i = 0; parm->enum_list[i].name; i++) {
1062 if ( strequal(pszParmValue, parm->enum_list[i].name)) {
1063 *ptr = parm->enum_list[i].value;
1064 return true;
1067 DEBUG(0, ("WARNING: Ignoring invalid value '%s' for parameter '%s'\n",
1068 pszParmValue, parm->label));
1069 return false;
1073 /***************************************************************************
1074 Handle the "realm" parameter
1075 ***************************************************************************/
1077 bool handle_realm(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1078 const char *pszParmValue, char **ptr)
1080 char *upper;
1081 char *lower;
1083 upper = strupper_talloc(lp_ctx, pszParmValue);
1084 if (upper == NULL) {
1085 return false;
1088 lower = strlower_talloc(lp_ctx, pszParmValue);
1089 if (lower == NULL) {
1090 TALLOC_FREE(upper);
1091 return false;
1094 lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1095 lpcfg_string_set(lp_ctx->globals->ctx, &lp_ctx->globals->realm, upper);
1096 lpcfg_string_set(lp_ctx->globals->ctx, &lp_ctx->globals->dnsdomain, lower);
1098 return true;
1101 /***************************************************************************
1102 Handle the include operation.
1103 ***************************************************************************/
1105 bool handle_include(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1106 const char *pszParmValue, char **ptr)
1108 char *fname;
1110 if (lp_ctx->s3_fns) {
1111 return lp_ctx->s3_fns->lp_include(lp_ctx, service, pszParmValue, ptr);
1114 fname = standard_sub_basic(lp_ctx, pszParmValue);
1116 add_to_file_list(lp_ctx, &lp_ctx->file_lists, pszParmValue, fname);
1118 lpcfg_string_set(lp_ctx, ptr, fname);
1120 if (file_exist(fname))
1121 return pm_process(fname, do_section, lpcfg_do_parameter, lp_ctx);
1123 DEBUG(2, ("Can't find include file %s\n", fname));
1125 return false;
1128 /***************************************************************************
1129 Handle the interpretation of the copy parameter.
1130 ***************************************************************************/
1132 bool handle_copy(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1133 const char *pszParmValue, char **ptr)
1135 bool bRetval;
1136 struct loadparm_service *serviceTemp = NULL;
1138 bRetval = false;
1140 DEBUG(3, ("Copying service from service %s\n", pszParmValue));
1142 serviceTemp = lpcfg_getservicebyname(lp_ctx, pszParmValue);
1144 if (service == NULL) {
1145 DEBUG(0, ("Unable to copy service - invalid service destination"));
1146 return false;
1149 if (serviceTemp != NULL) {
1150 if (serviceTemp == service) {
1151 DEBUG(0, ("Can't copy service %s - unable to copy self!\n", pszParmValue));
1152 } else {
1153 copy_service(service,
1154 serviceTemp,
1155 service->copymap);
1156 lpcfg_string_set(service, ptr, pszParmValue);
1158 bRetval = true;
1160 } else {
1161 DEBUG(0, ("Unable to copy service - source not found: %s\n",
1162 pszParmValue));
1163 bRetval = false;
1166 return bRetval;
1169 bool handle_debug_list(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1170 const char *pszParmValue, char **ptr)
1172 lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1174 return debug_parse_levels(pszParmValue);
1177 bool handle_logfile(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1178 const char *pszParmValue, char **ptr)
1180 if (lp_ctx->s3_fns == NULL) {
1181 debug_set_logfile(pszParmValue);
1184 lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1186 return true;
1190 * These special charset handling methods only run in the source3 code.
1193 bool handle_charset(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1194 const char *pszParmValue, char **ptr)
1196 if (lp_ctx->s3_fns) {
1197 if (*ptr == NULL || strcmp(*ptr, pszParmValue) != 0) {
1198 global_iconv_handle = smb_iconv_handle_reinit(NULL,
1199 lpcfg_dos_charset(lp_ctx),
1200 lpcfg_unix_charset(lp_ctx),
1201 true, global_iconv_handle);
1205 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1209 bool handle_dos_charset(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1210 const char *pszParmValue, char **ptr)
1212 bool is_utf8 = false;
1213 size_t len = strlen(pszParmValue);
1215 if (lp_ctx->s3_fns) {
1216 if (len == 4 || len == 5) {
1217 /* Don't use StrCaseCmp here as we don't want to
1218 initialize iconv. */
1219 if ((toupper_m(pszParmValue[0]) == 'U') &&
1220 (toupper_m(pszParmValue[1]) == 'T') &&
1221 (toupper_m(pszParmValue[2]) == 'F')) {
1222 if (len == 4) {
1223 if (pszParmValue[3] == '8') {
1224 is_utf8 = true;
1226 } else {
1227 if (pszParmValue[3] == '-' &&
1228 pszParmValue[4] == '8') {
1229 is_utf8 = true;
1235 if (*ptr == NULL || strcmp(*ptr, pszParmValue) != 0) {
1236 if (is_utf8) {
1237 DEBUG(0,("ERROR: invalid DOS charset: 'dos charset' must not "
1238 "be UTF8, using (default value) %s instead.\n",
1239 DEFAULT_DOS_CHARSET));
1240 pszParmValue = DEFAULT_DOS_CHARSET;
1242 global_iconv_handle = smb_iconv_handle_reinit(NULL,
1243 lpcfg_dos_charset(lp_ctx),
1244 lpcfg_unix_charset(lp_ctx),
1245 true, global_iconv_handle);
1249 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1252 bool handle_printing(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1253 const char *pszParmValue, char **ptr)
1255 static int parm_num = -1;
1256 struct loadparm_service *s;
1258 if (parm_num == -1) {
1259 parm_num = lpcfg_map_parameter("printing");
1262 if (!lp_set_enum_parm(&parm_table[parm_num], pszParmValue, (int*)ptr)) {
1263 return false;
1266 if (lp_ctx->s3_fns) {
1267 if (service == NULL) {
1268 s = lp_ctx->sDefault;
1269 lp_ctx->s3_fns->init_printer_values(lp_ctx->globals->ctx, s);
1270 } else {
1271 s = service;
1272 lp_ctx->s3_fns->init_printer_values(s, s);
1276 return true;
1279 bool handle_ldap_debug_level(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1280 const char *pszParmValue, char **ptr)
1282 lp_ctx->globals->ldap_debug_level = lp_int(pszParmValue);
1284 if (lp_ctx->s3_fns) {
1285 lp_ctx->s3_fns->init_ldap_debugging();
1287 return true;
1290 bool handle_netbios_aliases(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1291 const char *pszParmValue, char **ptr)
1293 TALLOC_FREE(lp_ctx->globals->netbios_aliases);
1294 lp_ctx->globals->netbios_aliases = (const char **)str_list_make_v3(lp_ctx->globals->ctx,
1295 pszParmValue, NULL);
1297 if (lp_ctx->s3_fns) {
1298 return lp_ctx->s3_fns->set_netbios_aliases(lp_ctx->globals->netbios_aliases);
1300 return true;
1304 * idmap related parameters
1307 bool handle_idmap_backend(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1308 const char *pszParmValue, char **ptr)
1310 if (lp_ctx->s3_fns) {
1311 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : backend",
1312 pszParmValue, 0);
1315 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1318 bool handle_idmap_uid(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1319 const char *pszParmValue, char **ptr)
1321 if (lp_ctx->s3_fns) {
1322 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : range",
1323 pszParmValue, 0);
1326 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1329 bool handle_idmap_gid(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1330 const char *pszParmValue, char **ptr)
1332 if (lp_ctx->s3_fns) {
1333 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : range",
1334 pszParmValue, 0);
1337 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1340 bool handle_smb_ports(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1341 const char *pszParmValue, char **ptr)
1343 static int parm_num = -1;
1344 int i;
1345 const char **list;
1347 if (!pszParmValue || !*pszParmValue) {
1348 return false;
1351 if (parm_num == -1) {
1352 parm_num = lpcfg_map_parameter("smb ports");
1355 if(!set_variable_helper(lp_ctx->globals->ctx, parm_num, ptr, "smb ports",
1356 pszParmValue)) {
1357 return false;
1360 list = lp_ctx->globals->smb_ports;
1361 if (list == NULL) {
1362 return false;
1365 /* Check that each port is a valid integer and within range */
1366 for (i = 0; list[i] != NULL; i++) {
1367 char *end = NULL;
1368 int port = 0;
1369 port = strtol(list[i], &end, 10);
1370 if (*end != '\0' || port <= 0 || port > 65535) {
1371 TALLOC_FREE(list);
1372 return false;
1376 return true;
1379 /***************************************************************************
1380 Initialise a copymap.
1381 ***************************************************************************/
1383 void init_copymap(struct loadparm_service *pservice)
1385 int i;
1387 TALLOC_FREE(pservice->copymap);
1389 pservice->copymap = bitmap_talloc(NULL, num_parameters());
1390 if (!pservice->copymap)
1391 DEBUG(0,
1392 ("Couldn't allocate copymap!! (size %d)\n",
1393 (int)num_parameters()));
1394 else
1395 for (i = 0; i < num_parameters(); i++)
1396 bitmap_set(pservice->copymap, i);
1400 * Process a parametric option
1402 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
1403 struct loadparm_service *service,
1404 const char *pszParmName,
1405 const char *pszParmValue, int flags)
1407 struct parmlist_entry **data;
1408 char *name;
1409 TALLOC_CTX *mem_ctx;
1411 while (isspace((unsigned char)*pszParmName)) {
1412 pszParmName++;
1415 name = strlower_talloc(lp_ctx, pszParmName);
1416 if (!name) return false;
1418 if (service == NULL) {
1419 data = &lp_ctx->globals->param_opt;
1421 * s3 code cannot deal with parametric options stored on the globals ctx.
1423 if (lp_ctx->s3_fns != NULL) {
1424 mem_ctx = NULL;
1425 } else {
1426 mem_ctx = lp_ctx->globals->ctx;
1428 } else {
1429 data = &service->param_opt;
1430 mem_ctx = service;
1433 set_param_opt(mem_ctx, data, name, pszParmValue, flags);
1435 talloc_free(name);
1437 return true;
1440 static bool set_variable_helper(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
1441 const char *pszParmName, const char *pszParmValue)
1443 int i;
1445 /* switch on the type of variable it is */
1446 switch (parm_table[parmnum].type)
1448 case P_BOOL: {
1449 bool b;
1450 if (!set_boolean(pszParmValue, &b)) {
1451 DEBUG(0, ("set_variable_helper(%s): value is not "
1452 "boolean!\n", pszParmValue));
1453 return false;
1455 *(bool *)parm_ptr = b;
1457 break;
1459 case P_BOOLREV: {
1460 bool b;
1461 if (!set_boolean(pszParmValue, &b)) {
1462 DEBUG(0, ("set_variable_helper(%s): value is not "
1463 "boolean!\n", pszParmValue));
1464 return false;
1466 *(bool *)parm_ptr = !b;
1468 break;
1470 case P_INTEGER:
1471 *(int *)parm_ptr = lp_int(pszParmValue);
1472 break;
1474 case P_CHAR:
1475 *(char *)parm_ptr = *pszParmValue;
1476 break;
1478 case P_OCTAL:
1479 i = sscanf(pszParmValue, "%o", (int *)parm_ptr);
1480 if ( i != 1 ) {
1481 DEBUG ( 0, ("Invalid octal number %s\n", pszParmName ));
1482 return false;
1484 break;
1486 case P_BYTES:
1488 uint64_t val;
1489 if (conv_str_size_error(pszParmValue, &val)) {
1490 if (val <= INT_MAX) {
1491 *(int *)parm_ptr = (int)val;
1492 break;
1496 DEBUG(0, ("set_variable_helper(%s): value is not "
1497 "a valid size specifier!\n", pszParmValue));
1498 return false;
1501 case P_CMDLIST:
1502 TALLOC_FREE(*(char ***)parm_ptr);
1503 *(const char * const **)parm_ptr
1504 = (const char * const *)str_list_make_v3(mem_ctx,
1505 pszParmValue, NULL);
1506 break;
1508 case P_LIST:
1510 char **new_list = str_list_make_v3(mem_ctx,
1511 pszParmValue, NULL);
1512 if (new_list == NULL) {
1513 break;
1516 for (i=0; new_list[i]; i++) {
1517 if (*(const char ***)parm_ptr != NULL &&
1518 new_list[i][0] == '+' &&
1519 new_list[i][1])
1521 if (!str_list_check(*(const char ***)parm_ptr,
1522 &new_list[i][1])) {
1523 *(const char ***)parm_ptr = str_list_add(*(const char ***)parm_ptr,
1524 &new_list[i][1]);
1526 } else if (*(const char ***)parm_ptr != NULL &&
1527 new_list[i][0] == '-' &&
1528 new_list[i][1])
1530 str_list_remove(*(const char ***)parm_ptr,
1531 &new_list[i][1]);
1532 } else {
1533 if (i != 0) {
1534 DEBUG(0, ("Unsupported list syntax for: %s = %s\n",
1535 pszParmName, pszParmValue));
1536 return false;
1538 *(const char * const **)parm_ptr = (const char * const *) new_list;
1539 break;
1542 break;
1545 case P_STRING:
1546 lpcfg_string_set(mem_ctx, (char **)parm_ptr, pszParmValue);
1547 break;
1549 case P_USTRING:
1550 lpcfg_string_set_upper(mem_ctx, (char **)parm_ptr, pszParmValue);
1551 break;
1553 case P_ENUM:
1554 if (!lp_set_enum_parm(&parm_table[parmnum], pszParmValue, (int*)parm_ptr)) {
1555 return false;
1557 break;
1559 case P_SEP:
1560 break;
1563 return true;
1567 bool set_variable(TALLOC_CTX *mem_ctx, struct loadparm_service *service, int parmnum, void *parm_ptr,
1568 const char *pszParmName, const char *pszParmValue,
1569 struct loadparm_context *lp_ctx, bool on_globals)
1571 int i;
1572 bool ok;
1574 /* if it is a special case then go ahead */
1575 if (parm_table[parmnum].special) {
1576 ok = parm_table[parmnum].special(lp_ctx, service, pszParmValue,
1577 (char **)parm_ptr);
1578 if (!ok) {
1579 return false;
1581 goto mark_non_default;
1584 ok = set_variable_helper(mem_ctx, parmnum, parm_ptr, pszParmName, pszParmValue);
1586 if (!ok) {
1587 return false;
1590 mark_non_default:
1591 if (on_globals && (lp_ctx->flags[parmnum] & FLAG_DEFAULT)) {
1592 lp_ctx->flags[parmnum] &= ~FLAG_DEFAULT;
1593 /* we have to also unset FLAG_DEFAULT on aliases */
1594 for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
1595 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1597 for (i=parmnum+1;i<num_parameters() && parm_table[i].offset == parm_table[parmnum].offset;i++) {
1598 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1601 return true;
1605 bool lpcfg_do_global_parameter(struct loadparm_context *lp_ctx,
1606 const char *pszParmName, const char *pszParmValue)
1608 int parmnum = lpcfg_map_parameter(pszParmName);
1609 void *parm_ptr;
1611 if (parmnum < 0) {
1612 if (strchr(pszParmName, ':')) {
1613 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName, pszParmValue, 0);
1615 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1616 return true;
1619 /* if the flag has been set on the command line, then don't allow override,
1620 but don't report an error */
1621 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1622 return true;
1625 if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
1626 DEBUG(1, ("WARNING: The \"%s\" option is deprecated\n",
1627 pszParmName));
1630 parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[parmnum]);
1632 return set_variable(lp_ctx->globals->ctx, NULL, parmnum, parm_ptr,
1633 pszParmName, pszParmValue, lp_ctx, true);
1636 bool lpcfg_do_service_parameter(struct loadparm_context *lp_ctx,
1637 struct loadparm_service *service,
1638 const char *pszParmName, const char *pszParmValue)
1640 void *parm_ptr;
1641 int i;
1642 int parmnum = lpcfg_map_parameter(pszParmName);
1644 if (parmnum < 0) {
1645 if (strchr(pszParmName, ':')) {
1646 return lp_do_parameter_parametric(lp_ctx, service, pszParmName, pszParmValue, 0);
1648 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1649 return true;
1652 /* if the flag has been set on the command line, then don't allow override,
1653 but don't report an error */
1654 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1655 return true;
1658 if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
1659 DEBUG(1, ("WARNING: The \"%s\" option is deprecated\n",
1660 pszParmName));
1663 if (parm_table[parmnum].p_class == P_GLOBAL) {
1664 DEBUG(0,
1665 ("Global parameter %s found in service section!\n",
1666 pszParmName));
1667 return true;
1669 parm_ptr = ((char *)service) + parm_table[parmnum].offset;
1671 if (!service->copymap)
1672 init_copymap(service);
1674 /* this handles the aliases - set the copymap for other
1675 * entries with the same data pointer */
1676 for (i = 0; parm_table[i].label; i++)
1677 if (parm_table[i].offset == parm_table[parmnum].offset &&
1678 parm_table[i].p_class == parm_table[parmnum].p_class)
1679 bitmap_clear(service->copymap, i);
1681 return set_variable(service, service, parmnum, parm_ptr, pszParmName,
1682 pszParmValue, lp_ctx, false);
1686 * Process a parameter.
1689 bool lpcfg_do_parameter(const char *pszParmName, const char *pszParmValue,
1690 void *userdata)
1692 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1694 if (lp_ctx->bInGlobalSection)
1695 return lpcfg_do_global_parameter(lp_ctx, pszParmName,
1696 pszParmValue);
1697 else
1698 return lpcfg_do_service_parameter(lp_ctx, lp_ctx->currentService,
1699 pszParmName, pszParmValue);
1703 variable argument do parameter
1705 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx, const char *pszParmName, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
1706 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx,
1707 const char *pszParmName, const char *fmt, ...)
1709 char *s;
1710 bool ret;
1711 va_list ap;
1713 va_start(ap, fmt);
1714 s = talloc_vasprintf(NULL, fmt, ap);
1715 va_end(ap);
1716 ret = lpcfg_do_global_parameter(lp_ctx, pszParmName, s);
1717 talloc_free(s);
1718 return ret;
1723 set a parameter from the commandline - this is called from command line parameter
1724 parsing code. It sets the parameter then marks the parameter as unable to be modified
1725 by smb.conf processing
1727 bool lpcfg_set_cmdline(struct loadparm_context *lp_ctx, const char *pszParmName,
1728 const char *pszParmValue)
1730 int parmnum;
1731 int i;
1733 while (isspace((unsigned char)*pszParmValue)) pszParmValue++;
1735 parmnum = lpcfg_map_parameter(pszParmName);
1737 if (parmnum < 0 && strchr(pszParmName, ':')) {
1738 /* set a parametric option */
1739 bool ok;
1740 ok = lp_do_parameter_parametric(lp_ctx, NULL, pszParmName,
1741 pszParmValue, FLAG_CMDLINE);
1742 if (lp_ctx->s3_fns != NULL) {
1743 if (ok) {
1744 lp_ctx->s3_fns->store_cmdline(pszParmName, pszParmValue);
1747 return ok;
1750 if (parmnum < 0) {
1751 DEBUG(0,("Unknown option '%s'\n", pszParmName));
1752 return false;
1755 /* reset the CMDLINE flag in case this has been called before */
1756 lp_ctx->flags[parmnum] &= ~FLAG_CMDLINE;
1758 if (lp_ctx->s3_fns != NULL) {
1759 if (!lp_ctx->s3_fns->lp_do_parameter(-1, pszParmName, pszParmValue)) {
1760 return false;
1762 } else {
1763 if (!lpcfg_do_global_parameter(lp_ctx, pszParmName, pszParmValue)) {
1764 return false;
1768 lp_ctx->flags[parmnum] |= FLAG_CMDLINE;
1770 /* we have to also set FLAG_CMDLINE on aliases */
1771 for (i=parmnum-1;
1772 i>=0 && parm_table[i].p_class == parm_table[parmnum].p_class &&
1773 parm_table[i].offset == parm_table[parmnum].offset;
1774 i--) {
1775 lp_ctx->flags[i] |= FLAG_CMDLINE;
1777 for (i=parmnum+1;
1778 i<num_parameters() &&
1779 parm_table[i].p_class == parm_table[parmnum].p_class &&
1780 parm_table[i].offset == parm_table[parmnum].offset;
1781 i++) {
1782 lp_ctx->flags[i] |= FLAG_CMDLINE;
1785 if (lp_ctx->s3_fns != NULL) {
1786 lp_ctx->s3_fns->store_cmdline(pszParmName, pszParmValue);
1789 return true;
1793 set a option from the commandline in 'a=b' format. Use to support --option
1795 bool lpcfg_set_option(struct loadparm_context *lp_ctx, const char *option)
1797 char *p, *s;
1798 bool ret;
1800 s = talloc_strdup(NULL, option);
1801 if (!s) {
1802 return false;
1805 p = strchr(s, '=');
1806 if (!p) {
1807 talloc_free(s);
1808 return false;
1811 *p = 0;
1813 ret = lpcfg_set_cmdline(lp_ctx, s, p+1);
1814 talloc_free(s);
1815 return ret;
1819 #define BOOLSTR(b) ((b) ? "Yes" : "No")
1822 * Print a parameter of the specified type.
1825 void lpcfg_print_parameter(struct parm_struct *p, void *ptr, FILE * f)
1827 /* For the seperation of lists values that we print below */
1828 const char *list_sep = ", ";
1829 int i;
1830 switch (p->type)
1832 case P_ENUM:
1833 for (i = 0; p->enum_list[i].name; i++) {
1834 if (*(int *)ptr == p->enum_list[i].value) {
1835 fprintf(f, "%s",
1836 p->enum_list[i].name);
1837 break;
1840 break;
1842 case P_BOOL:
1843 fprintf(f, "%s", BOOLSTR(*(bool *)ptr));
1844 break;
1846 case P_BOOLREV:
1847 fprintf(f, "%s", BOOLSTR(!*(bool *)ptr));
1848 break;
1850 case P_INTEGER:
1851 case P_BYTES:
1852 fprintf(f, "%d", *(int *)ptr);
1853 break;
1855 case P_CHAR:
1856 fprintf(f, "%c", *(char *)ptr);
1857 break;
1859 case P_OCTAL: {
1860 int val = *(int *)ptr;
1861 if (val == -1) {
1862 fprintf(f, "-1");
1863 } else {
1864 fprintf(f, "0%03o", val);
1866 break;
1869 case P_CMDLIST:
1870 list_sep = " ";
1871 /* fall through */
1872 case P_LIST:
1873 if ((char ***)ptr && *(char ***)ptr) {
1874 char **list = *(char ***)ptr;
1875 for (; *list; list++) {
1876 /* surround strings with whitespace in double quotes */
1877 if (*(list+1) == NULL) {
1878 /* last item, no extra separator */
1879 list_sep = "";
1881 if ( strchr_m( *list, ' ' ) ) {
1882 fprintf(f, "\"%s\"%s", *list, list_sep);
1883 } else {
1884 fprintf(f, "%s%s", *list, list_sep);
1888 break;
1890 case P_STRING:
1891 case P_USTRING:
1892 if (*(char **)ptr) {
1893 fprintf(f, "%s", *(char **)ptr);
1895 break;
1896 case P_SEP:
1897 break;
1902 * Check if two parameters are equal.
1905 static bool lpcfg_equal_parameter(parm_type type, void *ptr1, void *ptr2)
1907 switch (type) {
1908 case P_BOOL:
1909 case P_BOOLREV:
1910 return (*((bool *)ptr1) == *((bool *)ptr2));
1912 case P_INTEGER:
1913 case P_ENUM:
1914 case P_OCTAL:
1915 case P_BYTES:
1916 return (*((int *)ptr1) == *((int *)ptr2));
1918 case P_CHAR:
1919 return (*((char *)ptr1) == *((char *)ptr2));
1921 case P_LIST:
1922 case P_CMDLIST:
1923 return str_list_equal(*(const char ***)ptr1, *(const char ***)ptr2);
1925 case P_STRING:
1926 case P_USTRING:
1928 char *p1 = *(char **)ptr1, *p2 = *(char **)ptr2;
1929 if (p1 && !*p1)
1930 p1 = NULL;
1931 if (p2 && !*p2)
1932 p2 = NULL;
1933 return (p1 == p2 || strequal(p1, p2));
1935 case P_SEP:
1936 break;
1938 return false;
1942 * Process a new section (service).
1944 * At this stage all sections are services.
1945 * Later we'll have special sections that permit server parameters to be set.
1946 * Returns True on success, False on failure.
1949 static bool do_section(const char *pszSectionName, void *userdata)
1951 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1952 bool bRetval;
1953 bool isglobal;
1955 if (lp_ctx->s3_fns != NULL) {
1956 return lp_ctx->s3_fns->do_section(pszSectionName, lp_ctx);
1959 isglobal = ((strwicmp(pszSectionName, GLOBAL_NAME) == 0) ||
1960 (strwicmp(pszSectionName, GLOBAL_NAME2) == 0));
1962 bRetval = false;
1964 /* if we've just struck a global section, note the fact. */
1965 lp_ctx->bInGlobalSection = isglobal;
1967 /* check for multiple global sections */
1968 if (lp_ctx->bInGlobalSection) {
1969 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1970 return true;
1973 /* if we have a current service, tidy it up before moving on */
1974 bRetval = true;
1976 if (lp_ctx->currentService != NULL)
1977 bRetval = lpcfg_service_ok(lp_ctx->currentService);
1979 /* if all is still well, move to the next record in the services array */
1980 if (bRetval) {
1981 /* We put this here to avoid an odd message order if messages are */
1982 /* issued by the post-processing of a previous section. */
1983 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1985 if ((lp_ctx->currentService = lpcfg_add_service(lp_ctx, lp_ctx->sDefault,
1986 pszSectionName))
1987 == NULL) {
1988 DEBUG(0, ("Failed to add a new service\n"));
1989 return false;
1993 return bRetval;
1998 * Determine if a particular base parameter is currently set to the default value.
2001 static bool is_default(struct loadparm_service *sDefault, int i)
2003 void *def_ptr = ((char *)sDefault) + parm_table[i].offset;
2004 switch (parm_table[i].type) {
2005 case P_CMDLIST:
2006 case P_LIST:
2007 return str_list_equal((const char * const *)parm_table[i].def.lvalue,
2008 *(const char ***)def_ptr);
2009 case P_STRING:
2010 case P_USTRING:
2011 return strequal(parm_table[i].def.svalue,
2012 *(char **)def_ptr);
2013 case P_BOOL:
2014 case P_BOOLREV:
2015 return parm_table[i].def.bvalue ==
2016 *(bool *)def_ptr;
2017 case P_INTEGER:
2018 case P_CHAR:
2019 case P_OCTAL:
2020 case P_BYTES:
2021 case P_ENUM:
2022 return parm_table[i].def.ivalue ==
2023 *(int *)def_ptr;
2024 case P_SEP:
2025 break;
2027 return false;
2031 *Display the contents of the global structure.
2034 static void dump_globals(struct loadparm_context *lp_ctx, FILE *f,
2035 bool show_defaults)
2037 int i;
2038 struct parmlist_entry *data;
2040 fprintf(f, "# Global parameters\n[global]\n");
2042 for (i = 0; parm_table[i].label; i++)
2043 if (parm_table[i].p_class == P_GLOBAL &&
2044 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset))) {
2045 if (!show_defaults && (lp_ctx->flags[i] & FLAG_DEFAULT))
2046 continue;
2047 fprintf(f, "\t%s = ", parm_table[i].label);
2048 lpcfg_print_parameter(&parm_table[i], lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[i]), f);
2049 fprintf(f, "\n");
2051 if (lp_ctx->globals->param_opt != NULL) {
2052 for (data = lp_ctx->globals->param_opt; data;
2053 data = data->next) {
2054 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
2055 continue;
2057 fprintf(f, "\t%s = %s\n", data->key, data->value);
2064 * Display the contents of a single services record.
2067 void lpcfg_dump_a_service(struct loadparm_service * pService, struct loadparm_service *sDefault, FILE * f,
2068 unsigned int *flags, bool show_defaults)
2070 int i;
2071 struct parmlist_entry *data;
2073 if (pService != sDefault)
2074 fprintf(f, "\n[%s]\n", pService->szService);
2076 for (i = 0; parm_table[i].label; i++) {
2077 if (parm_table[i].p_class == P_LOCAL &&
2078 (*parm_table[i].label != '-') &&
2079 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset)))
2081 if (pService == sDefault) {
2082 if (flags && (flags[i] & FLAG_DEFAULT)) {
2083 continue;
2085 if (!show_defaults) {
2086 if (is_default(sDefault, i)) {
2087 continue;
2090 } else {
2091 if (lpcfg_equal_parameter(parm_table[i].type,
2092 ((char *)pService) +
2093 parm_table[i].offset,
2094 ((char *)sDefault) +
2095 parm_table[i].offset))
2096 continue;
2099 fprintf(f, "\t%s = ", parm_table[i].label);
2100 lpcfg_print_parameter(&parm_table[i],
2101 ((char *)pService) + parm_table[i].offset, f);
2102 fprintf(f, "\n");
2105 if (pService->param_opt != NULL) {
2106 for (data = pService->param_opt; data; data = data->next) {
2107 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
2108 continue;
2110 fprintf(f, "\t%s = %s\n", data->key, data->value);
2115 bool lpcfg_dump_a_parameter(struct loadparm_context *lp_ctx,
2116 struct loadparm_service *service,
2117 const char *parm_name, FILE * f)
2119 struct parm_struct *parm;
2120 void *ptr;
2121 char *local_parm_name;
2122 char *parm_opt;
2123 const char *parm_opt_value;
2125 /* check for parametrical option */
2126 local_parm_name = talloc_strdup(lp_ctx, parm_name);
2127 if (local_parm_name == NULL) {
2128 return false;
2131 parm_opt = strchr( local_parm_name, ':');
2133 if (parm_opt) {
2134 *parm_opt = '\0';
2135 parm_opt++;
2136 if (strlen(parm_opt)) {
2137 parm_opt_value = lpcfg_parm_string(lp_ctx, service,
2138 local_parm_name, parm_opt);
2139 if (parm_opt_value) {
2140 fprintf(f, "%s\n", parm_opt_value);
2141 return true;
2144 return false;
2147 /* parameter is not parametric, search the table */
2148 parm = lpcfg_parm_struct(lp_ctx, parm_name);
2149 if (!parm) {
2150 return false;
2153 if (service != NULL && parm->p_class == P_GLOBAL) {
2154 return false;
2157 ptr = lpcfg_parm_ptr(lp_ctx, service,parm);
2159 lpcfg_print_parameter(parm, ptr, f);
2160 fprintf(f, "\n");
2161 return true;
2165 * Auto-load some home services.
2167 static void lpcfg_add_auto_services(struct loadparm_context *lp_ctx,
2168 const char *str)
2170 return;
2175 * Unload unused services.
2178 void lpcfg_killunused(struct loadparm_context *lp_ctx,
2179 struct smbsrv_connection *smb,
2180 bool (*snumused) (struct smbsrv_connection *, int))
2182 int i;
2183 for (i = 0; i < lp_ctx->iNumServices; i++) {
2184 if (lp_ctx->services[i] == NULL)
2185 continue;
2187 if (!snumused || !snumused(smb, i)) {
2188 talloc_free(lp_ctx->services[i]);
2189 lp_ctx->services[i] = NULL;
2195 static int lpcfg_destructor(struct loadparm_context *lp_ctx)
2197 struct parmlist_entry *data;
2199 if (lp_ctx->refuse_free) {
2200 /* someone is trying to free the
2201 global_loadparm_context.
2202 We can't allow that. */
2203 return -1;
2206 if (lp_ctx->globals->param_opt != NULL) {
2207 struct parmlist_entry *next;
2208 for (data = lp_ctx->globals->param_opt; data; data=next) {
2209 next = data->next;
2210 if (data->priority & FLAG_CMDLINE) continue;
2211 DLIST_REMOVE(lp_ctx->globals->param_opt, data);
2212 talloc_free(data);
2216 return 0;
2220 * Initialise the global parameter structure.
2222 * Note that most callers should use loadparm_init_global() instead
2224 struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
2226 int i;
2227 char *myname;
2228 struct loadparm_context *lp_ctx;
2229 struct parmlist_entry *parm;
2230 char *logfile;
2232 lp_ctx = talloc_zero(mem_ctx, struct loadparm_context);
2233 if (lp_ctx == NULL)
2234 return NULL;
2236 talloc_set_destructor(lp_ctx, lpcfg_destructor);
2237 lp_ctx->bInGlobalSection = true;
2238 lp_ctx->globals = talloc_zero(lp_ctx, struct loadparm_global);
2239 /* This appears odd, but globals in s3 isn't a pointer */
2240 lp_ctx->globals->ctx = lp_ctx->globals;
2241 lp_ctx->sDefault = talloc_zero(lp_ctx, struct loadparm_service);
2242 lp_ctx->flags = talloc_zero_array(lp_ctx, unsigned int, num_parameters());
2244 lp_ctx->sDefault->iMaxPrintJobs = 1000;
2245 lp_ctx->sDefault->bAvailable = true;
2246 lp_ctx->sDefault->browseable = true;
2247 lp_ctx->sDefault->read_only = true;
2248 lp_ctx->sDefault->map_archive = true;
2249 lp_ctx->sDefault->strict_locking = true;
2250 lp_ctx->sDefault->oplocks = true;
2251 lp_ctx->sDefault->create_mask = 0744;
2252 lp_ctx->sDefault->force_create_mode = 0000;
2253 lp_ctx->sDefault->directory_mask = 0755;
2254 lp_ctx->sDefault->force_directory_mode = 0000;
2256 DEBUG(3, ("Initialising global parameters\n"));
2258 for (i = 0; parm_table[i].label; i++) {
2259 if ((parm_table[i].type == P_STRING ||
2260 parm_table[i].type == P_USTRING) &&
2261 !(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2262 char **r;
2263 if (parm_table[i].p_class == P_LOCAL) {
2264 r = (char **)(((char *)lp_ctx->sDefault) + parm_table[i].offset);
2265 } else {
2266 r = (char **)(((char *)lp_ctx->globals) + parm_table[i].offset);
2268 *r = talloc_strdup(lp_ctx, "");
2272 logfile = talloc_asprintf(lp_ctx, "%s/log.samba", dyn_LOGFILEBASE);
2273 lpcfg_do_global_parameter(lp_ctx, "log file", logfile);
2274 talloc_free(logfile);
2276 lpcfg_do_global_parameter(lp_ctx, "log level", "0");
2278 lpcfg_do_global_parameter(lp_ctx, "syslog", "1");
2279 lpcfg_do_global_parameter(lp_ctx, "syslog only", "No");
2280 lpcfg_do_global_parameter(lp_ctx, "debug timestamp", "Yes");
2281 lpcfg_do_global_parameter(lp_ctx, "debug prefix timestamp", "No");
2282 lpcfg_do_global_parameter(lp_ctx, "debug hires timestamp", "Yes");
2283 lpcfg_do_global_parameter(lp_ctx, "debug pid", "No");
2284 lpcfg_do_global_parameter(lp_ctx, "debug uid", "No");
2285 lpcfg_do_global_parameter(lp_ctx, "debug class", "No");
2287 lpcfg_do_global_parameter(lp_ctx, "share backend", "classic");
2289 lpcfg_do_global_parameter(lp_ctx, "server role", "auto");
2290 lpcfg_do_global_parameter(lp_ctx, "domain logons", "No");
2291 lpcfg_do_global_parameter(lp_ctx, "domain master", "Auto");
2293 /* options that can be set on the command line must be initialised via
2294 the slower lpcfg_do_global_parameter() to ensure that FLAG_CMDLINE is obeyed */
2295 #ifdef TCP_NODELAY
2296 lpcfg_do_global_parameter(lp_ctx, "socket options", "TCP_NODELAY");
2297 #endif
2298 lpcfg_do_global_parameter(lp_ctx, "workgroup", DEFAULT_WORKGROUP);
2299 myname = get_myname(lp_ctx);
2300 lpcfg_do_global_parameter(lp_ctx, "netbios name", myname);
2301 talloc_free(myname);
2302 lpcfg_do_global_parameter(lp_ctx, "name resolve order", "lmhosts wins host bcast");
2304 lpcfg_do_global_parameter(lp_ctx, "fstype", "NTFS");
2306 lpcfg_do_global_parameter(lp_ctx, "ntvfs handler", "unixuid default");
2307 lpcfg_do_global_parameter(lp_ctx, "max connections", "0");
2309 lpcfg_do_global_parameter(lp_ctx, "dcerpc endpoint servers", "epmapper wkssvc rpcecho samr netlogon lsarpc spoolss drsuapi dssetup unixinfo browser eventlog6 backupkey dnsserver");
2310 lpcfg_do_global_parameter(lp_ctx, "server services", "s3fs rpc nbt wrepl ldap cldap kdc drepl winbindd ntp_signd kcc dnsupdate dns");
2311 lpcfg_do_global_parameter(lp_ctx, "kccsrv:samba_kcc", "true");
2312 /* the winbind method for domain controllers is for both RODC
2313 auth forwarding and for trusted domains */
2314 lpcfg_do_global_parameter(lp_ctx, "private dir", dyn_PRIVATE_DIR);
2315 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_LOCAL_MACHINE", "hklm.ldb");
2317 /* This hive should be dynamically generated by Samba using
2318 data from the sam, but for the moment leave it in a tdb to
2319 keep regedt32 from popping up an annoying dialog. */
2320 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_USERS", "hku.ldb");
2322 /* using UTF8 by default allows us to support all chars */
2323 lpcfg_do_global_parameter(lp_ctx, "unix charset", "UTF-8");
2325 /* Use codepage 850 as a default for the dos character set */
2326 lpcfg_do_global_parameter(lp_ctx, "dos charset", "CP850");
2329 * Allow the default PASSWD_CHAT to be overridden in local.h.
2331 lpcfg_do_global_parameter(lp_ctx, "passwd chat", DEFAULT_PASSWD_CHAT);
2333 lpcfg_do_global_parameter(lp_ctx, "pid directory", dyn_PIDDIR);
2334 lpcfg_do_global_parameter(lp_ctx, "lock dir", dyn_LOCKDIR);
2335 lpcfg_do_global_parameter(lp_ctx, "state directory", dyn_STATEDIR);
2336 lpcfg_do_global_parameter(lp_ctx, "cache directory", dyn_CACHEDIR);
2337 lpcfg_do_global_parameter(lp_ctx, "ncalrpc dir", dyn_NCALRPCDIR);
2339 lpcfg_do_global_parameter(lp_ctx, "nbt client socket address", "0.0.0.0");
2340 lpcfg_do_global_parameter_var(lp_ctx, "server string",
2341 "Samba %s", SAMBA_VERSION_STRING);
2343 lpcfg_do_global_parameter(lp_ctx, "password server", "*");
2345 lpcfg_do_global_parameter(lp_ctx, "max mux", "50");
2346 lpcfg_do_global_parameter(lp_ctx, "max xmit", "16644");
2347 lpcfg_do_global_parameter(lp_ctx, "host msdfs", "true");
2349 lpcfg_do_global_parameter(lp_ctx, "LargeReadwrite", "True");
2350 lpcfg_do_global_parameter(lp_ctx, "server min protocol", "LANMAN1");
2351 lpcfg_do_global_parameter(lp_ctx, "server max protocol", "SMB3");
2352 lpcfg_do_global_parameter(lp_ctx, "client min protocol", "CORE");
2353 lpcfg_do_global_parameter(lp_ctx, "client max protocol", "NT1");
2354 lpcfg_do_global_parameter(lp_ctx, "security", "AUTO");
2355 lpcfg_do_global_parameter(lp_ctx, "EncryptPasswords", "True");
2356 lpcfg_do_global_parameter(lp_ctx, "ReadRaw", "True");
2357 lpcfg_do_global_parameter(lp_ctx, "WriteRaw", "True");
2358 lpcfg_do_global_parameter(lp_ctx, "NullPasswords", "False");
2359 lpcfg_do_global_parameter(lp_ctx, "old password allowed period", "60");
2360 lpcfg_do_global_parameter(lp_ctx, "ObeyPamRestrictions", "False");
2362 lpcfg_do_global_parameter(lp_ctx, "TimeServer", "False");
2363 lpcfg_do_global_parameter(lp_ctx, "BindInterfacesOnly", "False");
2364 lpcfg_do_global_parameter(lp_ctx, "Unicode", "True");
2365 lpcfg_do_global_parameter(lp_ctx, "ClientLanManAuth", "False");
2366 lpcfg_do_global_parameter(lp_ctx, "ClientNTLMv2Auth", "True");
2367 lpcfg_do_global_parameter(lp_ctx, "LanmanAuth", "False");
2368 lpcfg_do_global_parameter(lp_ctx, "NTLMAuth", "True");
2369 lpcfg_do_global_parameter(lp_ctx, "client use spnego principal", "False");
2371 lpcfg_do_global_parameter(lp_ctx, "UnixExtensions", "True");
2373 lpcfg_do_global_parameter(lp_ctx, "PreferredMaster", "Auto");
2374 lpcfg_do_global_parameter(lp_ctx, "LocalMaster", "True");
2376 lpcfg_do_global_parameter(lp_ctx, "wins support", "False");
2377 lpcfg_do_global_parameter(lp_ctx, "dns proxy", "True");
2379 lpcfg_do_global_parameter(lp_ctx, "winbind separator", "\\");
2380 lpcfg_do_global_parameter(lp_ctx, "winbind sealed pipes", "True");
2381 lpcfg_do_global_parameter(lp_ctx, "require strong key", "True");
2382 lpcfg_do_global_parameter(lp_ctx, "winbindd socket directory", dyn_WINBINDD_SOCKET_DIR);
2383 lpcfg_do_global_parameter(lp_ctx, "winbindd privileged socket directory", dyn_WINBINDD_PRIVILEGED_SOCKET_DIR);
2384 lpcfg_do_global_parameter(lp_ctx, "ntp signd socket directory", dyn_NTP_SIGND_SOCKET_DIR);
2385 lpcfg_do_global_parameter_var(lp_ctx, "dns update command", "%s/samba_dnsupdate", dyn_SCRIPTSBINDIR);
2386 lpcfg_do_global_parameter_var(lp_ctx, "spn update command", "%s/samba_spnupdate", dyn_SCRIPTSBINDIR);
2387 lpcfg_do_global_parameter_var(lp_ctx, "samba kcc command",
2388 "%s/samba_kcc", dyn_SCRIPTSBINDIR);
2389 lpcfg_do_global_parameter(lp_ctx, "template shell", "/bin/false");
2390 lpcfg_do_global_parameter(lp_ctx, "template homedir", "/home/%D/%U");
2392 lpcfg_do_global_parameter(lp_ctx, "client signing", "default");
2393 lpcfg_do_global_parameter(lp_ctx, "server signing", "default");
2395 lpcfg_do_global_parameter(lp_ctx, "use spnego", "True");
2397 lpcfg_do_global_parameter(lp_ctx, "use mmap", "True");
2399 lpcfg_do_global_parameter(lp_ctx, "smb ports", "445 139");
2400 lpcfg_do_global_parameter(lp_ctx, "nbt port", "137");
2401 lpcfg_do_global_parameter(lp_ctx, "dgram port", "138");
2402 lpcfg_do_global_parameter(lp_ctx, "cldap port", "389");
2403 lpcfg_do_global_parameter(lp_ctx, "krb5 port", "88");
2404 lpcfg_do_global_parameter(lp_ctx, "kpasswd port", "464");
2405 lpcfg_do_global_parameter(lp_ctx, "web port", "901");
2407 lpcfg_do_global_parameter(lp_ctx, "nt status support", "True");
2409 lpcfg_do_global_parameter(lp_ctx, "max wins ttl", "518400"); /* 6 days */
2410 lpcfg_do_global_parameter(lp_ctx, "min wins ttl", "21600");
2412 lpcfg_do_global_parameter(lp_ctx, "tls enabled", "True");
2413 lpcfg_do_global_parameter(lp_ctx, "tls keyfile", "tls/key.pem");
2414 lpcfg_do_global_parameter(lp_ctx, "tls certfile", "tls/cert.pem");
2415 lpcfg_do_global_parameter(lp_ctx, "tls cafile", "tls/ca.pem");
2416 lpcfg_do_global_parameter(lp_ctx, "prefork children:smb", "4");
2418 lpcfg_do_global_parameter(lp_ctx, "rndc command", "/usr/sbin/rndc");
2419 lpcfg_do_global_parameter(lp_ctx, "nsupdate command", "/usr/bin/nsupdate -g");
2421 lpcfg_do_global_parameter(lp_ctx, "allow dns updates", "secure only");
2422 lpcfg_do_global_parameter(lp_ctx, "dns forwarder", "");
2424 lpcfg_do_global_parameter(lp_ctx, "algorithmic rid base", "1000");
2426 lpcfg_do_global_parameter(lp_ctx, "enhanced browsing", "True");
2428 lpcfg_do_global_parameter(lp_ctx, "winbind nss info", "template");
2430 lpcfg_do_global_parameter(lp_ctx, "server schannel", "Auto");
2432 lpcfg_do_global_parameter(lp_ctx, "short preserve case", "True");
2434 lpcfg_do_global_parameter(lp_ctx, "max open files", "16384");
2436 lpcfg_do_global_parameter(lp_ctx, "cups connection timeout", "30");
2438 lpcfg_do_global_parameter(lp_ctx, "locking", "True");
2440 lpcfg_do_global_parameter(lp_ctx, "block size", "1024");
2442 lpcfg_do_global_parameter(lp_ctx, "client use spnego", "True");
2444 lpcfg_do_global_parameter(lp_ctx, "change notify", "True");
2446 lpcfg_do_global_parameter(lp_ctx, "name cache timeout", "660");
2448 lpcfg_do_global_parameter(lp_ctx, "defer sharing violations", "True");
2450 lpcfg_do_global_parameter(lp_ctx, "ldap replication sleep", "1000");
2452 lpcfg_do_global_parameter(lp_ctx, "idmap backend", "tdb");
2454 lpcfg_do_global_parameter(lp_ctx, "enable privileges", "True");
2456 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max write", "%u", DEFAULT_SMB2_MAX_WRITE);
2458 lpcfg_do_global_parameter(lp_ctx, "passdb backend", "tdbsam");
2460 lpcfg_do_global_parameter(lp_ctx, "getwd cache", "True");
2462 lpcfg_do_global_parameter(lp_ctx, "winbind nested groups", "True");
2464 lpcfg_do_global_parameter(lp_ctx, "mangled names", "True");
2466 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max credits", "%u", DEFAULT_SMB2_MAX_CREDITS);
2468 lpcfg_do_global_parameter(lp_ctx, "ldap ssl", "start tls");
2470 lpcfg_do_global_parameter(lp_ctx, "ldap deref", "auto");
2472 lpcfg_do_global_parameter(lp_ctx, "lm interval", "60");
2474 lpcfg_do_global_parameter(lp_ctx, "mangling method", "hash2");
2476 lpcfg_do_global_parameter(lp_ctx, "hide dot files", "True");
2478 lpcfg_do_global_parameter(lp_ctx, "browse list", "True");
2480 lpcfg_do_global_parameter(lp_ctx, "passwd chat timeout", "2");
2482 lpcfg_do_global_parameter(lp_ctx, "guest account", GUEST_ACCOUNT);
2484 lpcfg_do_global_parameter(lp_ctx, "client schannel", "auto");
2486 lpcfg_do_global_parameter(lp_ctx, "smb encrypt", "default");
2488 lpcfg_do_global_parameter(lp_ctx, "max log size", "5000");
2490 lpcfg_do_global_parameter(lp_ctx, "idmap negative cache time", "120");
2492 lpcfg_do_global_parameter(lp_ctx, "ldap follow referral", "auto");
2494 lpcfg_do_global_parameter(lp_ctx, "multicast dns register", "yes");
2496 lpcfg_do_global_parameter(lp_ctx, "winbind reconnect delay", "30");
2498 lpcfg_do_global_parameter(lp_ctx, "winbind request timeout", "60");
2500 lpcfg_do_global_parameter(lp_ctx, "nt acl support", "yes");
2502 lpcfg_do_global_parameter(lp_ctx, "acl check permissions", "yes");
2504 lpcfg_do_global_parameter(lp_ctx, "keepalive", "300");
2506 lpcfg_do_global_parameter(lp_ctx, "winbind cache time", "300");
2508 lpcfg_do_global_parameter(lp_ctx, "level2 oplocks", "yes");
2510 lpcfg_do_global_parameter(lp_ctx, "show add printer wizard", "yes");
2512 lpcfg_do_global_parameter(lp_ctx, "allocation roundup size", "1048576");
2514 lpcfg_do_global_parameter(lp_ctx, "ldap page size", "1024");
2516 lpcfg_do_global_parameter(lp_ctx, "kernel share modes", "yes");
2518 lpcfg_do_global_parameter(lp_ctx, "strict locking", "Auto");
2520 lpcfg_do_global_parameter(lp_ctx, "map readonly", "yes");
2522 lpcfg_do_global_parameter(lp_ctx, "allow trusted domains", "yes");
2524 lpcfg_do_global_parameter(lp_ctx, "default devmode", "yes");
2526 lpcfg_do_global_parameter(lp_ctx, "os level", "20");
2528 lpcfg_do_global_parameter(lp_ctx, "dos filetimes", "yes");
2530 lpcfg_do_global_parameter(lp_ctx, "mangling char", "~");
2532 lpcfg_do_global_parameter(lp_ctx, "printcap cache time", "750");
2534 lpcfg_do_global_parameter(lp_ctx, "create krb5 conf", "yes");
2536 lpcfg_do_global_parameter(lp_ctx, "winbind max clients", "200");
2538 lpcfg_do_global_parameter(lp_ctx, "acl map full control", "yes");
2540 lpcfg_do_global_parameter(lp_ctx, "nt pipe support", "yes");
2542 lpcfg_do_global_parameter(lp_ctx, "ldap debug threshold", "10");
2544 lpcfg_do_global_parameter(lp_ctx, "follow symlinks", "yes");
2546 lpcfg_do_global_parameter(lp_ctx, "machine password timeout", "604800");
2548 lpcfg_do_global_parameter(lp_ctx, "ldap connection timeout", "2");
2550 lpcfg_do_global_parameter(lp_ctx, "winbind expand groups", "1");
2552 lpcfg_do_global_parameter(lp_ctx, "stat cache", "yes");
2554 lpcfg_do_global_parameter(lp_ctx, "lpq cache time", "30");
2556 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max trans", "%u", DEFAULT_SMB2_MAX_TRANSACT);
2558 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max read", "%u", DEFAULT_SMB2_MAX_READ);
2560 lpcfg_do_global_parameter(lp_ctx, "durable handles", "yes");
2562 lpcfg_do_global_parameter(lp_ctx, "max stat cache size", "256");
2564 lpcfg_do_global_parameter(lp_ctx, "ldap passwd sync", "no");
2566 lpcfg_do_global_parameter(lp_ctx, "kernel change notify", "yes");
2568 lpcfg_do_global_parameter(lp_ctx, "max ttl", "259200");
2570 lpcfg_do_global_parameter(lp_ctx, "blocking locks", "yes");
2572 lpcfg_do_global_parameter(lp_ctx, "oplock contention limit", "2");
2574 lpcfg_do_global_parameter(lp_ctx, "load printers", "yes");
2576 lpcfg_do_global_parameter(lp_ctx, "idmap cache time", "604800");
2578 lpcfg_do_global_parameter(lp_ctx, "preserve case", "yes");
2580 lpcfg_do_global_parameter(lp_ctx, "lm announce", "auto");
2582 lpcfg_do_global_parameter(lp_ctx, "afs token lifetime", "604800");
2584 lpcfg_do_global_parameter(lp_ctx, "enable core files", "yes");
2586 lpcfg_do_global_parameter(lp_ctx, "winbind max domain connections", "1");
2588 lpcfg_do_global_parameter(lp_ctx, "case sensitive", "auto");
2590 lpcfg_do_global_parameter(lp_ctx, "ldap timeout", "15");
2592 lpcfg_do_global_parameter(lp_ctx, "mangle prefix", "1");
2594 lpcfg_do_global_parameter(lp_ctx, "posix locking", "yes");
2596 lpcfg_do_global_parameter(lp_ctx, "lock spin time", "200");
2598 lpcfg_do_global_parameter(lp_ctx, "directory name cache size", "100");
2600 lpcfg_do_global_parameter(lp_ctx, "nmbd bind explicit broadcast", "yes");
2602 lpcfg_do_global_parameter(lp_ctx, "init logon delay", "100");
2604 lpcfg_do_global_parameter(lp_ctx, "usershare owner only", "yes");
2606 lpcfg_do_global_parameter(lp_ctx, "-valid", "yes");
2608 lpcfg_do_global_parameter_var(lp_ctx, "usershare path", "%s/usershares", get_dyn_STATEDIR());
2610 #ifdef DEVELOPER
2611 lpcfg_do_global_parameter_var(lp_ctx, "panic action", "/bin/sleep 999999999");
2612 #endif
2614 lpcfg_do_global_parameter(lp_ctx, "smb passwd file", get_dyn_SMB_PASSWD_FILE());
2616 lpcfg_do_global_parameter(lp_ctx, "logon home", "\\\\%N\\%U");
2618 lpcfg_do_global_parameter(lp_ctx, "logon path", "\\\\%N\\%U\\profile");
2620 lpcfg_do_global_parameter(lp_ctx, "printjob username", "%U");
2622 for (i = 0; parm_table[i].label; i++) {
2623 if (!(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2624 lp_ctx->flags[i] |= FLAG_DEFAULT;
2628 for (parm=lp_ctx->globals->param_opt; parm; parm=parm->next) {
2629 if (!(parm->priority & FLAG_CMDLINE)) {
2630 parm->priority |= FLAG_DEFAULT;
2634 for (parm=lp_ctx->sDefault->param_opt; parm; parm=parm->next) {
2635 if (!(parm->priority & FLAG_CMDLINE)) {
2636 parm->priority |= FLAG_DEFAULT;
2641 return lp_ctx;
2645 * Initialise the global parameter structure.
2647 struct loadparm_context *loadparm_init_global(bool load_default)
2649 if (global_loadparm_context == NULL) {
2650 global_loadparm_context = loadparm_init(NULL);
2652 if (global_loadparm_context == NULL) {
2653 return NULL;
2655 global_loadparm_context->global = true;
2656 if (load_default && !global_loadparm_context->loaded) {
2657 lpcfg_load_default(global_loadparm_context);
2659 global_loadparm_context->refuse_free = true;
2660 return global_loadparm_context;
2664 * Initialise the global parameter structure.
2666 struct loadparm_context *loadparm_init_s3(TALLOC_CTX *mem_ctx,
2667 const struct loadparm_s3_helpers *s3_fns)
2669 struct loadparm_context *loadparm_context = talloc_zero(mem_ctx, struct loadparm_context);
2670 if (!loadparm_context) {
2671 return NULL;
2673 loadparm_context->s3_fns = s3_fns;
2674 loadparm_context->globals = s3_fns->globals;
2675 loadparm_context->flags = s3_fns->flags;
2677 return loadparm_context;
2680 const char *lpcfg_configfile(struct loadparm_context *lp_ctx)
2682 return lp_ctx->szConfigFile;
2685 const char *lp_default_path(void)
2687 if (getenv("SMB_CONF_PATH"))
2688 return getenv("SMB_CONF_PATH");
2689 else
2690 return dyn_CONFIGFILE;
2694 * Update the internal state of a loadparm context after settings
2695 * have changed.
2697 static bool lpcfg_update(struct loadparm_context *lp_ctx)
2699 struct debug_settings settings;
2700 TALLOC_CTX *tmp_ctx;
2702 tmp_ctx = talloc_new(lp_ctx);
2703 if (tmp_ctx == NULL) {
2704 return false;
2707 lpcfg_add_auto_services(lp_ctx, lpcfg_auto_services(lp_ctx, tmp_ctx));
2709 if (!lp_ctx->globals->wins_server_list && lp_ctx->globals->we_are_a_wins_server) {
2710 lpcfg_do_global_parameter(lp_ctx, "wins server", "127.0.0.1");
2713 if (!lp_ctx->global) {
2714 TALLOC_FREE(tmp_ctx);
2715 return true;
2718 panic_action = lp_ctx->globals->panic_action;
2720 reload_charcnv(lp_ctx);
2722 ZERO_STRUCT(settings);
2723 /* Add any more debug-related smb.conf parameters created in
2724 * future here */
2725 settings.syslog = lp_ctx->globals->syslog;
2726 settings.syslog_only = lp_ctx->globals->syslog_only;
2727 settings.timestamp_logs = lp_ctx->globals->timestamp_logs;
2728 settings.debug_prefix_timestamp = lp_ctx->globals->debug_prefix_timestamp;
2729 settings.debug_hires_timestamp = lp_ctx->globals->debug_hires_timestamp;
2730 settings.debug_pid = lp_ctx->globals->debug_pid;
2731 settings.debug_uid = lp_ctx->globals->debug_uid;
2732 settings.debug_class = lp_ctx->globals->debug_class;
2733 debug_set_settings(&settings);
2735 /* FIXME: This is a bit of a hack, but we can't use a global, since
2736 * not everything that uses lp also uses the socket library */
2737 if (lpcfg_parm_bool(lp_ctx, NULL, "socket", "testnonblock", false)) {
2738 setenv("SOCKET_TESTNONBLOCK", "1", 1);
2739 } else {
2740 unsetenv("SOCKET_TESTNONBLOCK");
2743 TALLOC_FREE(tmp_ctx);
2744 return true;
2747 bool lpcfg_load_default(struct loadparm_context *lp_ctx)
2749 const char *path;
2751 path = lp_default_path();
2753 if (!file_exist(path)) {
2754 /* We allow the default smb.conf file to not exist,
2755 * basically the equivalent of an empty file. */
2756 return lpcfg_update(lp_ctx);
2759 return lpcfg_load(lp_ctx, path);
2763 * Load the services array from the services file.
2765 * Return True on success, False on failure.
2767 bool lpcfg_load(struct loadparm_context *lp_ctx, const char *filename)
2769 char *n2;
2770 bool bRetval;
2772 filename = talloc_strdup(lp_ctx, filename);
2774 lp_ctx->szConfigFile = filename;
2776 if (lp_ctx->s3_fns) {
2777 return lp_ctx->s3_fns->load(filename);
2780 lp_ctx->bInGlobalSection = true;
2781 n2 = standard_sub_basic(lp_ctx, lp_ctx->szConfigFile);
2782 DEBUG(2, ("lpcfg_load: refreshing parameters from %s\n", n2));
2784 add_to_file_list(lp_ctx, &lp_ctx->file_lists, lp_ctx->szConfigFile, n2);
2786 /* We get sections first, so have to start 'behind' to make up */
2787 lp_ctx->currentService = NULL;
2788 bRetval = pm_process(n2, do_section, lpcfg_do_parameter, lp_ctx);
2790 /* finish up the last section */
2791 DEBUG(4, ("pm_process() returned %s\n", BOOLSTR(bRetval)));
2792 if (bRetval)
2793 if (lp_ctx->currentService != NULL)
2794 bRetval = lpcfg_service_ok(lp_ctx->currentService);
2796 bRetval = bRetval && lpcfg_update(lp_ctx);
2798 /* we do this unconditionally, so that it happens even
2799 for a missing smb.conf */
2800 reload_charcnv(lp_ctx);
2802 if (bRetval == true) {
2803 /* set this up so that any child python tasks will
2804 find the right smb.conf */
2805 setenv("SMB_CONF_PATH", filename, 1);
2807 /* set the context used by the lp_*() function
2808 varients */
2809 global_loadparm_context = lp_ctx;
2810 lp_ctx->loaded = true;
2813 return bRetval;
2817 * Return the max number of services.
2820 int lpcfg_numservices(struct loadparm_context *lp_ctx)
2822 if (lp_ctx->s3_fns) {
2823 return lp_ctx->s3_fns->get_numservices();
2826 return lp_ctx->iNumServices;
2830 * Display the contents of the services array in human-readable form.
2833 void lpcfg_dump(struct loadparm_context *lp_ctx, FILE *f, bool show_defaults,
2834 int maxtoprint)
2836 int iService;
2838 if (lp_ctx->s3_fns) {
2839 lp_ctx->s3_fns->dump(f, show_defaults, maxtoprint);
2840 return;
2843 dump_globals(lp_ctx, f, show_defaults);
2845 lpcfg_dump_a_service(lp_ctx->sDefault, lp_ctx->sDefault, f, lp_ctx->flags, show_defaults);
2847 for (iService = 0; iService < maxtoprint; iService++)
2848 lpcfg_dump_one(f, show_defaults, lp_ctx->services[iService], lp_ctx->sDefault);
2852 * Display the contents of one service in human-readable form.
2854 void lpcfg_dump_one(FILE *f, bool show_defaults, struct loadparm_service *service, struct loadparm_service *sDefault)
2856 if (service != NULL) {
2857 if (service->szService[0] == '\0')
2858 return;
2859 lpcfg_dump_a_service(service, sDefault, f, NULL, show_defaults);
2863 struct loadparm_service *lpcfg_servicebynum(struct loadparm_context *lp_ctx,
2864 int snum)
2866 if (lp_ctx->s3_fns) {
2867 return lp_ctx->s3_fns->get_servicebynum(snum);
2870 return lp_ctx->services[snum];
2873 struct loadparm_service *lpcfg_service(struct loadparm_context *lp_ctx,
2874 const char *service_name)
2876 int iService;
2877 char *serviceName;
2879 if (lp_ctx->s3_fns) {
2880 return lp_ctx->s3_fns->get_service(service_name);
2883 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--) {
2884 if (lp_ctx->services[iService] &&
2885 lp_ctx->services[iService]->szService) {
2887 * The substitution here is used to support %U is
2888 * service names
2890 serviceName = standard_sub_basic(
2891 lp_ctx->services[iService],
2892 lp_ctx->services[iService]->szService);
2893 if (strequal(serviceName, service_name)) {
2894 talloc_free(serviceName);
2895 return lp_ctx->services[iService];
2897 talloc_free(serviceName);
2901 DEBUG(7,("lpcfg_servicenumber: couldn't find %s\n", service_name));
2902 return NULL;
2905 const char *lpcfg_servicename(const struct loadparm_service *service)
2907 return lpcfg_string((const char *)service->szService);
2911 * A useful volume label function.
2913 const char *lpcfg_volume_label(struct loadparm_service *service, struct loadparm_service *sDefault)
2915 const char *ret;
2916 ret = lpcfg_string((const char *)((service != NULL && service->volume != NULL) ?
2917 service->volume : sDefault->volume));
2918 if (!*ret)
2919 return lpcfg_servicename(service);
2920 return ret;
2924 * Return the correct printer name.
2926 const char *lpcfg_printername(struct loadparm_service *service, struct loadparm_service *sDefault)
2928 const char *ret;
2929 ret = lpcfg_string((const char *)((service != NULL && service->_printername != NULL) ?
2930 service->_printername : sDefault->_printername));
2931 if (ret == NULL || (ret != NULL && *ret == '\0'))
2932 ret = lpcfg_servicename(service);
2934 return ret;
2939 * Return the max print jobs per queue.
2941 int lpcfg_maxprintjobs(struct loadparm_service *service, struct loadparm_service *sDefault)
2943 int maxjobs = (service != NULL) ? service->iMaxPrintJobs : sDefault->iMaxPrintJobs;
2944 if (maxjobs <= 0 || maxjobs >= PRINT_MAX_JOBID)
2945 maxjobs = PRINT_MAX_JOBID - 1;
2947 return maxjobs;
2950 struct smb_iconv_handle *lpcfg_iconv_handle(struct loadparm_context *lp_ctx)
2952 if (lp_ctx == NULL) {
2953 return get_iconv_handle();
2955 return lp_ctx->iconv_handle;
2958 _PUBLIC_ void reload_charcnv(struct loadparm_context *lp_ctx)
2960 struct smb_iconv_handle *old_ic = lp_ctx->iconv_handle;
2961 if (!lp_ctx->global) {
2962 return;
2965 if (old_ic == NULL) {
2966 old_ic = global_iconv_handle;
2968 lp_ctx->iconv_handle = smb_iconv_handle_reinit_lp(lp_ctx, lp_ctx, old_ic);
2969 global_iconv_handle = lp_ctx->iconv_handle;
2972 _PUBLIC_ char *lpcfg_tls_keyfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2974 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_keyfile(lp_ctx));
2977 _PUBLIC_ char *lpcfg_tls_certfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2979 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_certfile(lp_ctx));
2982 _PUBLIC_ char *lpcfg_tls_cafile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2984 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_cafile(lp_ctx));
2987 _PUBLIC_ char *lpcfg_tls_crlfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2989 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_crlfile(lp_ctx));
2992 _PUBLIC_ char *lpcfg_tls_dhpfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2994 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_dhpfile(lp_ctx));
2997 struct gensec_settings *lpcfg_gensec_settings(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2999 struct gensec_settings *settings = talloc_zero(mem_ctx, struct gensec_settings);
3000 if (settings == NULL)
3001 return NULL;
3002 SMB_ASSERT(lp_ctx != NULL);
3003 settings->lp_ctx = talloc_reference(settings, lp_ctx);
3004 settings->target_hostname = lpcfg_parm_string(lp_ctx, NULL, "gensec", "target_hostname");
3005 return settings;
3008 int lpcfg_server_role(struct loadparm_context *lp_ctx)
3010 int domain_master = lpcfg__domain_master(lp_ctx);
3012 return lp_find_server_role(lpcfg__server_role(lp_ctx),
3013 lpcfg__security(lp_ctx),
3014 lpcfg__domain_logons(lp_ctx),
3015 (domain_master == true) ||
3016 (domain_master == Auto));
3019 int lpcfg_security(struct loadparm_context *lp_ctx)
3021 return lp_find_security(lpcfg__server_role(lp_ctx),
3022 lpcfg__security(lp_ctx));
3025 bool lpcfg_server_signing_allowed(struct loadparm_context *lp_ctx, bool *mandatory)
3027 bool allowed = true;
3028 enum smb_signing_setting signing_setting = lpcfg_server_signing(lp_ctx);
3030 *mandatory = false;
3032 if (signing_setting == SMB_SIGNING_DEFAULT) {
3034 * If we are a domain controller, SMB signing is
3035 * really important, as it can prevent a number of
3036 * attacks on communications between us and the
3037 * clients
3039 * However, it really sucks (no sendfile, CPU
3040 * overhead) performance-wise when used on a
3041 * file server, so disable it by default
3042 * on non-DCs
3045 if (lpcfg_server_role(lp_ctx) >= ROLE_ACTIVE_DIRECTORY_DC) {
3046 signing_setting = SMB_SIGNING_REQUIRED;
3047 } else {
3048 signing_setting = SMB_SIGNING_OFF;
3052 switch (signing_setting) {
3053 case SMB_SIGNING_REQUIRED:
3054 *mandatory = true;
3055 break;
3056 case SMB_SIGNING_IF_REQUIRED:
3057 break;
3058 case SMB_SIGNING_DEFAULT:
3059 case SMB_SIGNING_OFF:
3060 allowed = false;
3061 break;
3064 return allowed;
3067 int lpcfg_tdb_hash_size(struct loadparm_context *lp_ctx, const char *name)
3069 const char *base;
3071 if (name == NULL) {
3072 return 0;
3075 base = strrchr_m(name, '/');
3076 if (base != NULL) {
3077 base += 1;
3078 } else {
3079 base = name;
3081 return lpcfg_parm_int(lp_ctx, NULL, "tdb_hashsize", base, 0);
3085 int lpcfg_tdb_flags(struct loadparm_context *lp_ctx, int tdb_flags)
3087 if (!lpcfg_use_mmap(lp_ctx)) {
3088 tdb_flags |= TDB_NOMMAP;
3090 return tdb_flags;