param: turn 'cups encrypt' into a generated function
[Samba.git] / lib / param / loadparm.c
blob1369c70f5882f4d394047ecdde1efe6966b98cc9
1 /*
2 Unix SMB/CIFS implementation.
3 Parameter loading functions
4 Copyright (C) Karl Auer 1993-1998
6 Largely re-written by Andrew Tridgell, September 1994
8 Copyright (C) Simo Sorce 2001
9 Copyright (C) Alexander Bokovoy 2002
10 Copyright (C) Stefan (metze) Metzmacher 2002
11 Copyright (C) Jim McDonough (jmcd@us.ibm.com) 2003.
12 Copyright (C) James Myers 2003 <myersjj@samba.org>
13 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
14 Copyright (C) Andrew Bartlett 2011-2012
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 * Load parameters.
33 * This module provides suitable callback functions for the params
34 * module. It builds the internal table of service details which is
35 * then used by the rest of the server.
37 * To add a parameter:
39 * 1) add it to the global or service structure definition
40 * 2) add it to the parm_table
41 * 3) add it to the list of available functions (eg: using FN_GLOBAL_STRING())
42 * 4) If it's a global then initialise it in init_globals. If a local
43 * (ie. service) parameter then initialise it in the sDefault structure
46 * Notes:
47 * The configuration file is processed sequentially for speed. It is NOT
48 * accessed randomly as happens in 'real' Windows. For this reason, there
49 * is a fair bit of sequence-dependent code here - ie., code which assumes
50 * that certain things happen before others. In particular, the code which
51 * happens at the boundary between sections is delicately poised, so be
52 * careful!
56 #include "includes.h"
57 #include "version.h"
58 #include "dynconfig/dynconfig.h"
59 #include "system/time.h"
60 #include "system/locale.h"
61 #include "system/network.h" /* needed for TCP_NODELAY */
62 #include "../lib/util/dlinklist.h"
63 #include "lib/param/param.h"
64 #include "lib/param/loadparm.h"
65 #include "auth/gensec/gensec.h"
66 #include "lib/param/s3_param.h"
67 #include "lib/util/bitmap.h"
68 #include "libcli/smb/smb_constants.h"
69 #include "tdb.h"
70 #include "librpc/gen_ndr/nbt.h"
72 #ifdef HAVE_HTTPCONNECTENCRYPT
73 #include <cups/http.h>
74 #endif
76 #define standard_sub_basic talloc_strdup
78 #include "lib/param/param_global.h"
80 struct loadparm_service *lpcfg_default_service(struct loadparm_context *lp_ctx)
82 return lp_ctx->sDefault;
85 /**
86 * Convenience routine to grab string parameters into temporary memory
87 * and run standard_sub_basic on them.
89 * The buffers can be written to by
90 * callers without affecting the source string.
93 static const char *lpcfg_string(const char *s)
95 #if 0 /* until REWRITE done to make thread-safe */
96 size_t len = s ? strlen(s) : 0;
97 char *ret;
98 #endif
100 /* The follow debug is useful for tracking down memory problems
101 especially if you have an inner loop that is calling a lp_*()
102 function that returns a string. Perhaps this debug should be
103 present all the time? */
105 #if 0
106 DEBUG(10, ("lpcfg_string(%s)\n", s));
107 #endif
109 #if 0 /* until REWRITE done to make thread-safe */
110 if (!lp_talloc)
111 lp_talloc = talloc_init("lp_talloc");
113 ret = talloc_array(lp_talloc, char, len + 100); /* leave room for substitution */
115 if (!ret)
116 return NULL;
118 if (!s)
119 *ret = 0;
120 else
121 strlcpy(ret, s, len);
123 if (trim_string(ret, "\"", "\"")) {
124 if (strchr(ret,'"') != NULL)
125 strlcpy(ret, s, len);
128 standard_sub_basic(ret,len+100);
129 return (ret);
130 #endif
131 return s;
135 In this section all the functions that are used to access the
136 parameters from the rest of the program are defined
140 * the creation of separate lpcfg_*() and lp_*() functions is to allow
141 * for code compatibility between existing Samba4 and Samba3 code.
144 /* this global context supports the lp_*() function varients */
145 static struct loadparm_context *global_loadparm_context;
147 #define FN_GLOBAL_STRING(fn_name,var_name) \
148 _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx, TALLOC_CTX *ctx) {\
149 if (lp_ctx == NULL) return NULL; \
150 if (lp_ctx->s3_fns) { \
151 return lp_ctx->globals->var_name ? lp_ctx->s3_fns->lp_string(ctx, lp_ctx->globals->var_name) : talloc_strdup(ctx, ""); \
153 return lp_ctx->globals->var_name ? talloc_strdup(ctx, lpcfg_string(lp_ctx->globals->var_name)) : talloc_strdup(ctx, ""); \
156 #define FN_GLOBAL_CONST_STRING(fn_name,var_name) \
157 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
158 if (lp_ctx == NULL) return NULL; \
159 return lp_ctx->globals->var_name ? lpcfg_string(lp_ctx->globals->var_name) : ""; \
162 #define FN_GLOBAL_LIST(fn_name,var_name) \
163 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
164 if (lp_ctx == NULL) return NULL; \
165 return lp_ctx->globals->var_name; \
168 #define FN_GLOBAL_BOOL(fn_name,var_name) \
169 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) {\
170 if (lp_ctx == NULL) return false; \
171 return lp_ctx->globals->var_name; \
174 #define FN_GLOBAL_INTEGER(fn_name,var_name) \
175 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
176 return lp_ctx->globals->var_name; \
179 /* Local parameters don't need the ->s3_fns because the struct
180 * loadparm_service is shared and lpcfg_service() checks the ->s3_fns
181 * hook */
182 #define FN_LOCAL_STRING(fn_name,val) \
183 _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_service *service, \
184 struct loadparm_service *sDefault, TALLOC_CTX *ctx) { \
185 return(talloc_strdup(ctx, lpcfg_string((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)))); \
188 #define FN_LOCAL_CONST_STRING(fn_name,val) \
189 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_service *service, \
190 struct loadparm_service *sDefault) { \
191 return((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)); \
194 #define FN_LOCAL_LIST(fn_name,val) \
195 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_service *service, \
196 struct loadparm_service *sDefault) {\
197 return(const char **)(service != NULL && service->val != NULL? service->val : sDefault->val); \
200 #define FN_LOCAL_PARM_BOOL(fn_name, val) FN_LOCAL_BOOL(fn_name, val)
202 #define FN_LOCAL_BOOL(fn_name,val) \
203 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_service *service, \
204 struct loadparm_service *sDefault) { \
205 return((service != NULL)? service->val : sDefault->val); \
208 #define FN_LOCAL_INTEGER(fn_name,val) \
209 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_service *service, \
210 struct loadparm_service *sDefault) { \
211 return((service != NULL)? service->val : sDefault->val); \
214 #define FN_LOCAL_PARM_INTEGER(fn_name, val) FN_LOCAL_INTEGER(fn_name, val)
216 #define FN_LOCAL_CHAR(fn_name,val) \
217 _PUBLIC_ char lpcfg_ ## fn_name(struct loadparm_service *service, \
218 struct loadparm_service *sDefault) { \
219 return((service != NULL)? service->val : sDefault->val); \
222 #define FN_LOCAL_PARM_CHAR(fn_name,val) FN_LOCAL_CHAR(fn_name, val)
224 #include "lib/param/param_functions.c"
226 /* These functions cannot be auto-generated */
227 FN_LOCAL_BOOL(autoloaded, autoloaded)
228 FN_GLOBAL_CONST_STRING(dnsdomain, dnsdomain)
230 /* local prototypes */
231 static struct loadparm_service *lpcfg_getservicebyname(struct loadparm_context *lp_ctx,
232 const char *pszServiceName);
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 size_t type_len = strlen(type);
249 size_t option_len = strlen(option);
250 char param_key[type_len + option_len + 2];
251 struct parmlist_entry *data = NULL;
253 snprintf(param_key, sizeof(param_key), "%s:%s", type, option);
256 * Try to fetch the option from the data.
258 if (service != NULL) {
259 data = service->param_opt;
260 while (data != NULL) {
261 if (strwicmp(data->key, param_key) == 0) {
262 return data;
264 data = data->next;
269 * Fall back to fetching from the globals.
271 data = global_opts;
272 while (data != NULL) {
273 if (strwicmp(data->key, param_key) == 0) {
274 return data;
276 data = data->next;
279 return NULL;
282 const char *lpcfg_get_parametric(struct loadparm_context *lp_ctx,
283 struct loadparm_service *service,
284 const char *type, const char *option)
286 struct parmlist_entry *data;
288 if (lp_ctx == NULL)
289 return NULL;
291 data = get_parametric_helper(service,
292 type, option, lp_ctx->globals->param_opt);
294 if (data == NULL) {
295 return NULL;
296 } else {
297 return data->value;
303 * convenience routine to return int parameters.
305 int lp_int(const char *s)
308 if (!s || !*s) {
309 DEBUG(0,("lp_int(%s): is called with NULL!\n",s));
310 return -1;
313 return strtol(s, NULL, 0);
317 * convenience routine to return unsigned long parameters.
319 unsigned long lp_ulong(const char *s)
322 if (!s || !*s) {
323 DEBUG(0,("lp_ulong(%s): is called with NULL!\n",s));
324 return -1;
327 return strtoul(s, NULL, 0);
331 * convenience routine to return unsigned long parameters.
333 static long lp_long(const char *s)
336 if (!s) {
337 DEBUG(0,("lp_long(%s): is called with NULL!\n",s));
338 return -1;
341 return strtol(s, NULL, 0);
345 * convenience routine to return unsigned long parameters.
347 static double lp_double(const char *s)
350 if (!s) {
351 DEBUG(0,("lp_double(%s): is called with NULL!\n",s));
352 return -1;
355 return strtod(s, NULL);
359 * convenience routine to return boolean parameters.
361 bool lp_bool(const char *s)
363 bool ret = false;
365 if (!s || !*s) {
366 DEBUG(0,("lp_bool(%s): is called with NULL!\n",s));
367 return false;
370 if (!set_boolean(s, &ret)) {
371 DEBUG(0,("lp_bool(%s): value is not boolean!\n",s));
372 return false;
375 return ret;
379 * Return parametric option from a given service. Type is a part of option before ':'
380 * Parametric option has following syntax: 'Type: option = value'
381 * Returned value is allocated in 'lp_talloc' context
384 const char *lpcfg_parm_string(struct loadparm_context *lp_ctx,
385 struct loadparm_service *service, const char *type,
386 const char *option)
388 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
390 if (value)
391 return lpcfg_string(value);
393 return NULL;
397 * Return parametric option from a given service. Type is a part of option before ':'
398 * Parametric option has following syntax: 'Type: option = value'
399 * Returned value is allocated in 'lp_talloc' context
402 const char **lpcfg_parm_string_list(TALLOC_CTX *mem_ctx,
403 struct loadparm_context *lp_ctx,
404 struct loadparm_service *service,
405 const char *type,
406 const char *option, const char *separator)
408 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
410 if (value != NULL) {
411 char **l = str_list_make(mem_ctx, value, separator);
412 return discard_const_p(const char *, l);
415 return NULL;
419 * Return parametric option from a given service. Type is a part of option before ':'
420 * Parametric option has following syntax: 'Type: option = value'
423 int lpcfg_parm_int(struct loadparm_context *lp_ctx,
424 struct loadparm_service *service, const char *type,
425 const char *option, int default_v)
427 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
429 if (value)
430 return lp_int(value);
432 return default_v;
436 * Return parametric option from a given service. Type is a part of
437 * option before ':'.
438 * Parametric option has following syntax: 'Type: option = value'.
441 int lpcfg_parm_bytes(struct loadparm_context *lp_ctx,
442 struct loadparm_service *service, const char *type,
443 const char *option, int default_v)
445 uint64_t bval;
447 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
449 if (value && conv_str_size_error(value, &bval)) {
450 if (bval <= INT_MAX) {
451 return (int)bval;
455 return default_v;
459 * Return parametric option from a given service.
460 * Type is a part of option before ':'
461 * Parametric option has following syntax: 'Type: option = value'
463 unsigned long lpcfg_parm_ulong(struct loadparm_context *lp_ctx,
464 struct loadparm_service *service, const char *type,
465 const char *option, unsigned long default_v)
467 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
469 if (value)
470 return lp_ulong(value);
472 return default_v;
475 long lpcfg_parm_long(struct loadparm_context *lp_ctx,
476 struct loadparm_service *service, const char *type,
477 const char *option, long default_v)
479 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
481 if (value)
482 return lp_long(value);
484 return default_v;
487 double lpcfg_parm_double(struct loadparm_context *lp_ctx,
488 struct loadparm_service *service, const char *type,
489 const char *option, double default_v)
491 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
493 if (value != NULL)
494 return lp_double(value);
496 return default_v;
500 * Return parametric option from a given service. Type is a part of option before ':'
501 * Parametric option has following syntax: 'Type: option = value'
504 bool lpcfg_parm_bool(struct loadparm_context *lp_ctx,
505 struct loadparm_service *service, const char *type,
506 const char *option, bool default_v)
508 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
510 if (value != NULL)
511 return lp_bool(value);
513 return default_v;
518 * Set a string value, deallocating any existing space, and allocing the space
519 * for the string
521 bool lpcfg_string_set(TALLOC_CTX *mem_ctx, char **dest, const char *src)
523 talloc_free(*dest);
525 if (src == NULL)
526 src = "";
528 *dest = talloc_strdup(mem_ctx, src);
529 if ((*dest) == NULL) {
530 DEBUG(0,("Out of memory in string_set\n"));
531 return false;
534 return true;
538 * Set a string value, deallocating any existing space, and allocing the space
539 * for the string
541 bool lpcfg_string_set_upper(TALLOC_CTX *mem_ctx, char **dest, const char *src)
543 talloc_free(*dest);
545 if (src == NULL)
546 src = "";
548 *dest = strupper_talloc(mem_ctx, src);
549 if ((*dest) == NULL) {
550 DEBUG(0,("Out of memory in string_set_upper\n"));
551 return false;
554 return true;
560 * Add a new service to the services array initialising it with the given
561 * service.
564 struct loadparm_service *lpcfg_add_service(struct loadparm_context *lp_ctx,
565 const struct loadparm_service *pservice,
566 const char *name)
568 int i;
569 int num_to_alloc = lp_ctx->iNumServices + 1;
570 struct parmlist_entry *data, *pdata;
572 if (lp_ctx->s3_fns != NULL) {
573 smb_panic("Add a service should not be called on an s3 loadparm ctx");
576 if (pservice == NULL) {
577 pservice = lp_ctx->sDefault;
580 /* it might already exist */
581 if (name) {
582 struct loadparm_service *service = lpcfg_getservicebyname(lp_ctx,
583 name);
584 if (service != NULL) {
585 /* Clean all parametric options for service */
586 /* They will be added during parsing again */
587 data = service->param_opt;
588 while (data) {
589 pdata = data->next;
590 talloc_free(data);
591 data = pdata;
593 service->param_opt = NULL;
594 return service;
598 /* find an invalid one */
599 for (i = 0; i < lp_ctx->iNumServices; i++)
600 if (lp_ctx->services[i] == NULL)
601 break;
603 /* if not, then create one */
604 if (i == lp_ctx->iNumServices) {
605 struct loadparm_service **tsp;
607 tsp = talloc_realloc(lp_ctx, lp_ctx->services, struct loadparm_service *, num_to_alloc);
609 if (!tsp) {
610 DEBUG(0,("lpcfg_add_service: failed to enlarge services!\n"));
611 return NULL;
612 } else {
613 lp_ctx->services = tsp;
614 lp_ctx->services[lp_ctx->iNumServices] = NULL;
617 lp_ctx->iNumServices++;
620 lp_ctx->services[i] = talloc_zero(lp_ctx->services, struct loadparm_service);
621 if (lp_ctx->services[i] == NULL) {
622 DEBUG(0,("lpcfg_add_service: out of memory!\n"));
623 return NULL;
625 copy_service(lp_ctx->services[i], pservice, NULL);
626 if (name != NULL)
627 lpcfg_string_set(lp_ctx->services[i], &lp_ctx->services[i]->szService, name);
628 return lp_ctx->services[i];
632 * Add a new home service, with the specified home directory, defaults coming
633 * from service ifrom.
636 bool lpcfg_add_home(struct loadparm_context *lp_ctx,
637 const char *pszHomename,
638 struct loadparm_service *default_service,
639 const char *user, const char *pszHomedir)
641 struct loadparm_service *service;
643 service = lpcfg_add_service(lp_ctx, default_service, pszHomename);
645 if (service == NULL)
646 return false;
648 if (!(*(default_service->path))
649 || strequal(default_service->path, lp_ctx->sDefault->path)) {
650 service->path = talloc_strdup(service, pszHomedir);
651 } else {
652 service->path = string_sub_talloc(service, lpcfg_path(default_service, lp_ctx->sDefault, service), "%H", pszHomedir);
655 if (!(*(service->comment))) {
656 service->comment = talloc_asprintf(service, "Home directory of %s", user);
658 service->bAvailable = default_service->bAvailable;
659 service->browseable = default_service->browseable;
661 DEBUG(3, ("adding home's share [%s] for user '%s' at '%s'\n",
662 pszHomename, user, service->path));
664 return true;
668 * Add a new printer service, with defaults coming from service iFrom.
671 bool lpcfg_add_printer(struct loadparm_context *lp_ctx,
672 const char *pszPrintername,
673 struct loadparm_service *default_service)
675 const char *comment = "From Printcap";
676 struct loadparm_service *service;
677 service = lpcfg_add_service(lp_ctx, default_service, pszPrintername);
679 if (service == NULL)
680 return false;
682 /* note that we do NOT default the availability flag to True - */
683 /* we take it from the default service passed. This allows all */
684 /* dynamic printers to be disabled by disabling the [printers] */
685 /* entry (if/when the 'available' keyword is implemented!). */
687 /* the printer name is set to the service name. */
688 lpcfg_string_set(service, &service->_printername, pszPrintername);
689 lpcfg_string_set(service, &service->comment, comment);
690 service->browseable = default_service->browseable;
691 /* Printers cannot be read_only. */
692 service->read_only = false;
693 /* Printer services must be printable. */
694 service->printable = true;
696 DEBUG(3, ("adding printer service %s\n", pszPrintername));
698 return true;
702 * Map a parameter's string representation to something we can use.
703 * Returns False if the parameter string is not recognised, else TRUE.
706 int lpcfg_map_parameter(const char *pszParmName)
708 int iIndex;
710 for (iIndex = 0; parm_table[iIndex].label; iIndex++)
711 if (strwicmp(parm_table[iIndex].label, pszParmName) == 0)
712 return iIndex;
714 /* Warn only if it isn't parametric option */
715 if (strchr(pszParmName, ':') == NULL)
716 DEBUG(0, ("Unknown parameter encountered: \"%s\"\n", pszParmName));
717 /* We do return 'fail' for parametric options as well because they are
718 stored in different storage
720 return -1;
725 return the parameter structure for a parameter
727 struct parm_struct *lpcfg_parm_struct(struct loadparm_context *lp_ctx, const char *name)
729 int num = lpcfg_map_parameter(name);
731 if (num < 0) {
732 return NULL;
735 return &parm_table[num];
739 return the parameter pointer for a parameter
741 void *lpcfg_parm_ptr(struct loadparm_context *lp_ctx,
742 struct loadparm_service *service, struct parm_struct *parm)
744 if (lp_ctx->s3_fns) {
745 return lp_ctx->s3_fns->get_parm_ptr(service, parm);
748 if (service == NULL) {
749 if (parm->p_class == P_LOCAL)
750 return ((char *)lp_ctx->sDefault)+parm->offset;
751 else if (parm->p_class == P_GLOBAL)
752 return ((char *)lp_ctx->globals)+parm->offset;
753 else return NULL;
754 } else {
755 return ((char *)service) + parm->offset;
760 return the parameter pointer for a parameter
762 bool lpcfg_parm_is_cmdline(struct loadparm_context *lp_ctx, const char *name)
764 int parmnum;
766 parmnum = lpcfg_map_parameter(name);
767 if (parmnum == -1) return false;
769 return lp_ctx->flags[parmnum] & FLAG_CMDLINE;
773 * Find a service by name. Otherwise works like get_service.
776 static struct loadparm_service *lpcfg_getservicebyname(struct loadparm_context *lp_ctx,
777 const char *pszServiceName)
779 int iService;
781 if (lp_ctx->s3_fns) {
782 return lp_ctx->s3_fns->get_service(pszServiceName);
785 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--)
786 if (lp_ctx->services[iService] != NULL &&
787 strwicmp(lp_ctx->services[iService]->szService, pszServiceName) == 0) {
788 return lp_ctx->services[iService];
791 return NULL;
795 * Add a parametric option to a parmlist_entry,
796 * replacing old value, if already present.
798 void set_param_opt(TALLOC_CTX *mem_ctx,
799 struct parmlist_entry **opt_list,
800 const char *opt_name,
801 const char *opt_value,
802 unsigned priority)
804 struct parmlist_entry *new_opt, *opt;
805 bool not_added;
807 opt = *opt_list;
808 not_added = true;
810 /* Traverse destination */
811 while (opt) {
812 /* If we already have same option, override it */
813 if (strwicmp(opt->key, opt_name) == 0) {
814 if ((opt->priority & FLAG_CMDLINE) &&
815 !(priority & FLAG_CMDLINE)) {
816 /* it's been marked as not to be
817 overridden */
818 return;
820 TALLOC_FREE(opt->value);
821 TALLOC_FREE(opt->list);
822 opt->value = talloc_strdup(opt, opt_value);
823 opt->priority = priority;
824 not_added = false;
825 break;
827 opt = opt->next;
829 if (not_added) {
830 new_opt = talloc(mem_ctx, struct parmlist_entry);
831 if (new_opt == NULL) {
832 smb_panic("OOM");
835 new_opt->key = talloc_strdup(new_opt, opt_name);
836 if (new_opt->key == NULL) {
837 smb_panic("talloc_strdup failed");
840 new_opt->value = talloc_strdup(new_opt, opt_value);
841 if (new_opt->value == NULL) {
842 smb_panic("talloc_strdup failed");
845 new_opt->list = NULL;
846 new_opt->priority = priority;
847 DLIST_ADD(*opt_list, new_opt);
852 * Copy a service structure to another.
853 * If pcopymapDest is NULL then copy all fields
856 void copy_service(struct loadparm_service *pserviceDest,
857 const struct loadparm_service *pserviceSource,
858 struct bitmap *pcopymapDest)
860 int i;
861 bool bcopyall = (pcopymapDest == NULL);
862 struct parmlist_entry *data;
864 for (i = 0; parm_table[i].label; i++)
865 if (parm_table[i].p_class == P_LOCAL &&
866 (bcopyall || bitmap_query(pcopymapDest, i))) {
867 const void *src_ptr =
868 ((const char *)pserviceSource) + parm_table[i].offset;
869 void *dest_ptr =
870 ((char *)pserviceDest) + parm_table[i].offset;
872 switch (parm_table[i].type) {
873 case P_BOOL:
874 case P_BOOLREV:
875 *(bool *)dest_ptr = *(const bool *)src_ptr;
876 break;
878 case P_INTEGER:
879 case P_BYTES:
880 case P_OCTAL:
881 case P_ENUM:
882 *(int *)dest_ptr = *(const int *)src_ptr;
883 break;
885 case P_CHAR:
886 *(char *)dest_ptr = *(const char *)src_ptr;
887 break;
889 case P_STRING:
890 lpcfg_string_set(pserviceDest,
891 (char **)dest_ptr,
892 *(const char * const *)src_ptr);
893 break;
895 case P_USTRING:
896 lpcfg_string_set_upper(pserviceDest,
897 (char **)dest_ptr,
898 *(const char * const *)src_ptr);
899 break;
900 case P_CMDLIST:
901 case P_LIST:
902 TALLOC_FREE(*((char ***)dest_ptr));
903 *(char ***)dest_ptr = str_list_copy(pserviceDest,
904 *discard_const_p(const char **, src_ptr));
905 break;
906 default:
907 break;
911 if (bcopyall) {
912 init_copymap(pserviceDest);
913 if (pserviceSource->copymap)
914 bitmap_copy(pserviceDest->copymap,
915 pserviceSource->copymap);
918 for (data = pserviceSource->param_opt; data != NULL; data = data->next) {
919 set_param_opt(pserviceDest, &pserviceDest->param_opt,
920 data->key, data->value, data->priority);
925 * Check a service for consistency. Return False if the service is in any way
926 * incomplete or faulty, else True.
928 bool lpcfg_service_ok(struct loadparm_service *service)
930 bool bRetval;
932 bRetval = true;
933 if (service->szService[0] == '\0') {
934 DEBUG(0, ("The following message indicates an internal error:\n"));
935 DEBUG(0, ("No service name in service entry.\n"));
936 bRetval = false;
939 /* The [printers] entry MUST be printable. I'm all for flexibility, but */
940 /* I can't see why you'd want a non-printable printer service... */
941 if (strwicmp(service->szService, PRINTERS_NAME) == 0) {
942 if (!service->printable) {
943 DEBUG(0, ("WARNING: [%s] service MUST be printable!\n",
944 service->szService));
945 service->printable = true;
947 /* [printers] service must also be non-browsable. */
948 if (service->browseable)
949 service->browseable = false;
952 if (service->path[0] == '\0' &&
953 strwicmp(service->szService, HOMES_NAME) != 0 &&
954 service->msdfs_proxy[0] == '\0')
956 DEBUG(0, ("WARNING: No path in service %s - making it unavailable!\n",
957 service->szService));
958 service->bAvailable = false;
961 if (!service->bAvailable)
962 DEBUG(1, ("NOTE: Service %s is flagged unavailable.\n",
963 service->szService));
965 return bRetval;
969 /*******************************************************************
970 Keep a linked list of all config files so we know when one has changed
971 it's date and needs to be reloaded.
972 ********************************************************************/
974 void add_to_file_list(TALLOC_CTX *mem_ctx, struct file_lists **list,
975 const char *fname, const char *subfname)
977 struct file_lists *f = *list;
979 while (f) {
980 if (f->name && !strcmp(f->name, fname))
981 break;
982 f = f->next;
985 if (!f) {
986 f = talloc(mem_ctx, struct file_lists);
987 if (!f)
988 goto fail;
989 f->next = *list;
990 f->name = talloc_strdup(f, fname);
991 if (!f->name) {
992 TALLOC_FREE(f);
993 goto fail;
995 f->subfname = talloc_strdup(f, subfname);
996 if (!f->subfname) {
997 TALLOC_FREE(f);
998 goto fail;
1000 *list = f;
1001 f->modtime = file_modtime(subfname);
1002 } else {
1003 time_t t = file_modtime(subfname);
1004 if (t)
1005 f->modtime = t;
1007 return;
1009 fail:
1010 DEBUG(0, ("Unable to add file to file list: %s\n", fname));
1014 /*******************************************************************
1015 Check if a config file has changed date.
1016 ********************************************************************/
1017 bool lpcfg_file_list_changed(struct loadparm_context *lp_ctx)
1019 struct file_lists *f;
1020 DEBUG(6, ("lpcfg_file_list_changed()\n"));
1022 for (f = lp_ctx->file_lists; f != NULL; f = f->next) {
1023 char *n2;
1024 time_t mod_time;
1026 n2 = standard_sub_basic(lp_ctx, f->name);
1028 DEBUGADD(6, ("file %s -> %s last mod_time: %s\n",
1029 f->name, n2, ctime(&f->modtime)));
1031 mod_time = file_modtime(n2);
1033 if (mod_time && ((f->modtime != mod_time) || (f->subfname == NULL) || (strcmp(n2, f->subfname) != 0))) {
1034 DEBUGADD(6, ("file %s modified: %s\n", n2,
1035 ctime(&mod_time)));
1036 f->modtime = mod_time;
1037 talloc_free(f->subfname);
1038 f->subfname = talloc_strdup(f, n2);
1039 TALLOC_FREE(n2);
1040 return true;
1042 TALLOC_FREE(n2);
1044 return false;
1048 * set the value for a P_ENUM
1050 bool lp_set_enum_parm( struct parm_struct *parm, const char *pszParmValue,
1051 int *ptr )
1053 int i;
1055 for (i = 0; parm->enum_list[i].name; i++) {
1056 if (strwicmp(pszParmValue, parm->enum_list[i].name) == 0) {
1057 *ptr = parm->enum_list[i].value;
1058 return true;
1061 DEBUG(0, ("WARNING: Ignoring invalid value '%s' for parameter '%s'\n",
1062 pszParmValue, parm->label));
1063 return false;
1067 /***************************************************************************
1068 Handle the "realm" parameter
1069 ***************************************************************************/
1071 bool handle_realm(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1072 const char *pszParmValue, char **ptr)
1074 char *upper;
1075 char *lower;
1077 upper = strupper_talloc(lp_ctx, pszParmValue);
1078 if (upper == NULL) {
1079 return false;
1082 lower = strlower_talloc(lp_ctx, pszParmValue);
1083 if (lower == NULL) {
1084 TALLOC_FREE(upper);
1085 return false;
1088 lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1089 lpcfg_string_set(lp_ctx->globals->ctx, &lp_ctx->globals->realm, upper);
1090 lpcfg_string_set(lp_ctx->globals->ctx, &lp_ctx->globals->dnsdomain, lower);
1092 return true;
1095 /***************************************************************************
1096 Handle the include operation.
1097 ***************************************************************************/
1099 bool handle_include(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1100 const char *pszParmValue, char **ptr)
1102 char *fname;
1104 if (lp_ctx->s3_fns) {
1105 return lp_ctx->s3_fns->lp_include(lp_ctx, service, pszParmValue, ptr);
1108 fname = standard_sub_basic(lp_ctx, pszParmValue);
1110 add_to_file_list(lp_ctx, &lp_ctx->file_lists, pszParmValue, fname);
1112 lpcfg_string_set(lp_ctx, ptr, fname);
1114 if (file_exist(fname))
1115 return pm_process(fname, do_section, lpcfg_do_parameter, lp_ctx);
1117 DEBUG(2, ("Can't find include file %s\n", fname));
1119 return false;
1122 /***************************************************************************
1123 Handle the interpretation of the copy parameter.
1124 ***************************************************************************/
1126 bool handle_copy(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1127 const char *pszParmValue, char **ptr)
1129 bool bRetval;
1130 struct loadparm_service *serviceTemp = NULL;
1132 bRetval = false;
1134 DEBUG(3, ("Copying service from service %s\n", pszParmValue));
1136 serviceTemp = lpcfg_getservicebyname(lp_ctx, pszParmValue);
1138 if (service == NULL) {
1139 DEBUG(0, ("Unable to copy service - invalid service destination.\n"));
1140 return false;
1143 if (serviceTemp != NULL) {
1144 if (serviceTemp == service) {
1145 DEBUG(0, ("Can't copy service %s - unable to copy self!\n", pszParmValue));
1146 } else {
1147 copy_service(service,
1148 serviceTemp,
1149 service->copymap);
1150 lpcfg_string_set(service, ptr, pszParmValue);
1152 bRetval = true;
1154 } else {
1155 DEBUG(0, ("Unable to copy service - source not found: %s\n",
1156 pszParmValue));
1157 bRetval = false;
1160 return bRetval;
1163 bool handle_debug_list(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1164 const char *pszParmValue, char **ptr)
1166 lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1168 return debug_parse_levels(pszParmValue);
1171 bool handle_logfile(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1172 const char *pszParmValue, char **ptr)
1174 if (lp_ctx->s3_fns == NULL) {
1175 debug_set_logfile(pszParmValue);
1178 lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1180 return true;
1184 * These special charset handling methods only run in the source3 code.
1187 bool handle_charset(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1188 const char *pszParmValue, char **ptr)
1190 if (lp_ctx->s3_fns) {
1191 if (*ptr == NULL || strcmp(*ptr, pszParmValue) != 0) {
1192 global_iconv_handle = smb_iconv_handle_reinit(NULL,
1193 lpcfg_dos_charset(lp_ctx),
1194 lpcfg_unix_charset(lp_ctx),
1195 true, global_iconv_handle);
1199 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1203 bool handle_dos_charset(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1204 const char *pszParmValue, char **ptr)
1206 bool is_utf8 = false;
1207 size_t len = strlen(pszParmValue);
1209 if (lp_ctx->s3_fns) {
1210 if (len == 4 || len == 5) {
1211 /* Don't use StrCaseCmp here as we don't want to
1212 initialize iconv. */
1213 if ((toupper_m(pszParmValue[0]) == 'U') &&
1214 (toupper_m(pszParmValue[1]) == 'T') &&
1215 (toupper_m(pszParmValue[2]) == 'F')) {
1216 if (len == 4) {
1217 if (pszParmValue[3] == '8') {
1218 is_utf8 = true;
1220 } else {
1221 if (pszParmValue[3] == '-' &&
1222 pszParmValue[4] == '8') {
1223 is_utf8 = true;
1229 if (*ptr == NULL || strcmp(*ptr, pszParmValue) != 0) {
1230 if (is_utf8) {
1231 DEBUG(0,("ERROR: invalid DOS charset: 'dos charset' must not "
1232 "be UTF8, using (default value) %s instead.\n",
1233 DEFAULT_DOS_CHARSET));
1234 pszParmValue = DEFAULT_DOS_CHARSET;
1236 global_iconv_handle = smb_iconv_handle_reinit(NULL,
1237 lpcfg_dos_charset(lp_ctx),
1238 lpcfg_unix_charset(lp_ctx),
1239 true, global_iconv_handle);
1243 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1246 bool handle_printing(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1247 const char *pszParmValue, char **ptr)
1249 static int parm_num = -1;
1251 if (parm_num == -1) {
1252 parm_num = lpcfg_map_parameter("printing");
1255 if (!lp_set_enum_parm(&parm_table[parm_num], pszParmValue, (int*)ptr)) {
1256 return false;
1259 if (lp_ctx->s3_fns) {
1260 if (service == NULL) {
1261 init_printer_values(lp_ctx, lp_ctx->globals->ctx, lp_ctx->sDefault);
1262 } else {
1263 init_printer_values(lp_ctx, service, service);
1267 return true;
1270 bool handle_ldap_debug_level(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1271 const char *pszParmValue, char **ptr)
1273 lp_ctx->globals->ldap_debug_level = lp_int(pszParmValue);
1275 if (lp_ctx->s3_fns) {
1276 lp_ctx->s3_fns->init_ldap_debugging();
1278 return true;
1281 bool handle_netbios_aliases(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1282 const char *pszParmValue, char **ptr)
1284 TALLOC_FREE(lp_ctx->globals->netbios_aliases);
1285 lp_ctx->globals->netbios_aliases = str_list_make_v3_const(lp_ctx->globals->ctx,
1286 pszParmValue, NULL);
1288 if (lp_ctx->s3_fns) {
1289 return lp_ctx->s3_fns->set_netbios_aliases(lp_ctx->globals->netbios_aliases);
1291 return true;
1295 * idmap related parameters
1298 bool handle_idmap_backend(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1299 const char *pszParmValue, char **ptr)
1301 if (lp_ctx->s3_fns) {
1302 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : backend",
1303 pszParmValue, 0);
1306 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1309 bool handle_idmap_uid(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1310 const char *pszParmValue, char **ptr)
1312 if (lp_ctx->s3_fns) {
1313 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : range",
1314 pszParmValue, 0);
1317 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1320 bool handle_idmap_gid(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1321 const char *pszParmValue, char **ptr)
1323 if (lp_ctx->s3_fns) {
1324 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : range",
1325 pszParmValue, 0);
1328 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1331 bool handle_smb_ports(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1332 const char *pszParmValue, char **ptr)
1334 static int parm_num = -1;
1335 int i;
1336 const char **list;
1338 if (!pszParmValue || !*pszParmValue) {
1339 return false;
1342 if (parm_num == -1) {
1343 parm_num = lpcfg_map_parameter("smb ports");
1344 if (parm_num == -1) {
1345 return false;
1349 if(!set_variable_helper(lp_ctx->globals->ctx, parm_num, ptr, "smb ports",
1350 pszParmValue)) {
1351 return false;
1354 list = lp_ctx->globals->smb_ports;
1355 if (list == NULL) {
1356 return false;
1359 /* Check that each port is a valid integer and within range */
1360 for (i = 0; list[i] != NULL; i++) {
1361 char *end = NULL;
1362 int port = 0;
1363 port = strtol(list[i], &end, 10);
1364 if (*end != '\0' || port <= 0 || port > 65535) {
1365 TALLOC_FREE(list);
1366 return false;
1370 return true;
1373 bool handle_smb2_max_credits(struct loadparm_context *lp_ctx,
1374 struct loadparm_service *service,
1375 const char *pszParmValue, char **ptr)
1377 int value = lp_int(pszParmValue);
1379 if (value <= 0) {
1380 value = DEFAULT_SMB2_MAX_CREDITS;
1383 *(int *)ptr = value;
1385 return true;
1388 bool handle_cups_encrypt(struct loadparm_context *lp_ctx,
1389 struct loadparm_service *service,
1390 const char *pszParmValue, char **ptr)
1392 int result = 0;
1393 #ifdef HAVE_HTTPCONNECTENCRYPT
1394 int value = lp_int(pszParmValue);
1396 switch (value) {
1397 case Auto:
1398 result = HTTP_ENCRYPT_REQUIRED;
1399 break;
1400 case true:
1401 result = HTTP_ENCRYPT_ALWAYS;
1402 break;
1403 case false:
1404 result = HTTP_ENCRYPT_NEVER;
1405 break;
1406 default:
1407 result = 0;
1408 break;
1410 #endif
1411 *(int *)ptr = result;
1413 return true;
1416 /***************************************************************************
1417 Initialise a copymap.
1418 ***************************************************************************/
1421 * Initializes service copymap
1422 * Note: pservice *must* be valid TALLOC_CTX
1424 void init_copymap(struct loadparm_service *pservice)
1426 int i;
1428 TALLOC_FREE(pservice->copymap);
1430 pservice->copymap = bitmap_talloc(pservice, num_parameters());
1431 if (!pservice->copymap) {
1432 DEBUG(0,
1433 ("Couldn't allocate copymap!! (size %d)\n",
1434 (int)num_parameters()));
1435 } else {
1436 for (i = 0; i < num_parameters(); i++) {
1437 bitmap_set(pservice->copymap, i);
1443 * Process a parametric option
1445 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
1446 struct loadparm_service *service,
1447 const char *pszParmName,
1448 const char *pszParmValue, int flags)
1450 struct parmlist_entry **data;
1451 char *name;
1452 TALLOC_CTX *mem_ctx;
1454 while (isspace((unsigned char)*pszParmName)) {
1455 pszParmName++;
1458 name = strlower_talloc(lp_ctx, pszParmName);
1459 if (!name) return false;
1461 if (service == NULL) {
1462 data = &lp_ctx->globals->param_opt;
1464 * s3 code cannot deal with parametric options stored on the globals ctx.
1466 if (lp_ctx->s3_fns != NULL) {
1467 mem_ctx = NULL;
1468 } else {
1469 mem_ctx = lp_ctx->globals->ctx;
1471 } else {
1472 data = &service->param_opt;
1473 mem_ctx = service;
1476 set_param_opt(mem_ctx, data, name, pszParmValue, flags);
1478 talloc_free(name);
1480 return true;
1483 static bool set_variable_helper(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
1484 const char *pszParmName, const char *pszParmValue)
1486 int i;
1488 /* switch on the type of variable it is */
1489 switch (parm_table[parmnum].type)
1491 case P_BOOL: {
1492 bool b;
1493 if (!set_boolean(pszParmValue, &b)) {
1494 DEBUG(0, ("set_variable_helper(%s): value is not "
1495 "boolean!\n", pszParmValue));
1496 return false;
1498 *(bool *)parm_ptr = b;
1500 break;
1502 case P_BOOLREV: {
1503 bool b;
1504 if (!set_boolean(pszParmValue, &b)) {
1505 DEBUG(0, ("set_variable_helper(%s): value is not "
1506 "boolean!\n", pszParmValue));
1507 return false;
1509 *(bool *)parm_ptr = !b;
1511 break;
1513 case P_INTEGER:
1514 *(int *)parm_ptr = lp_int(pszParmValue);
1515 break;
1517 case P_CHAR:
1518 *(char *)parm_ptr = *pszParmValue;
1519 break;
1521 case P_OCTAL:
1522 i = sscanf(pszParmValue, "%o", (int *)parm_ptr);
1523 if ( i != 1 ) {
1524 DEBUG ( 0, ("Invalid octal number %s\n", pszParmName ));
1525 return false;
1527 break;
1529 case P_BYTES:
1531 uint64_t val;
1532 if (conv_str_size_error(pszParmValue, &val)) {
1533 if (val <= INT_MAX) {
1534 *(int *)parm_ptr = (int)val;
1535 break;
1539 DEBUG(0, ("set_variable_helper(%s): value is not "
1540 "a valid size specifier!\n", pszParmValue));
1541 return false;
1544 case P_CMDLIST:
1545 TALLOC_FREE(*(char ***)parm_ptr);
1546 *(char ***)parm_ptr = str_list_make_v3(mem_ctx,
1547 pszParmValue, NULL);
1548 break;
1550 case P_LIST:
1552 char **new_list = str_list_make_v3(mem_ctx,
1553 pszParmValue, NULL);
1554 if (new_list == NULL) {
1555 break;
1558 for (i=0; new_list[i]; i++) {
1559 if (*(const char ***)parm_ptr != NULL &&
1560 new_list[i][0] == '+' &&
1561 new_list[i][1])
1563 if (!str_list_check(*(const char ***)parm_ptr,
1564 &new_list[i][1])) {
1565 *(const char ***)parm_ptr = str_list_add(*(const char ***)parm_ptr,
1566 &new_list[i][1]);
1568 } else if (*(const char ***)parm_ptr != NULL &&
1569 new_list[i][0] == '-' &&
1570 new_list[i][1])
1572 str_list_remove(*(const char ***)parm_ptr,
1573 &new_list[i][1]);
1574 } else {
1575 if (i != 0) {
1576 DEBUG(0, ("Unsupported list syntax for: %s = %s\n",
1577 pszParmName, pszParmValue));
1578 return false;
1580 *(char ***)parm_ptr = new_list;
1581 break;
1584 break;
1587 case P_STRING:
1588 lpcfg_string_set(mem_ctx, (char **)parm_ptr, pszParmValue);
1589 break;
1591 case P_USTRING:
1592 lpcfg_string_set_upper(mem_ctx, (char **)parm_ptr, pszParmValue);
1593 break;
1595 case P_ENUM:
1596 if (!lp_set_enum_parm(&parm_table[parmnum], pszParmValue, (int*)parm_ptr)) {
1597 return false;
1599 break;
1603 return true;
1607 static bool set_variable(TALLOC_CTX *mem_ctx, struct loadparm_service *service,
1608 int parmnum, void *parm_ptr,
1609 const char *pszParmName, const char *pszParmValue,
1610 struct loadparm_context *lp_ctx, bool on_globals)
1612 int i;
1613 bool ok;
1615 /* if it is a special case then go ahead */
1616 if (parm_table[parmnum].special) {
1617 ok = parm_table[parmnum].special(lp_ctx, service, pszParmValue,
1618 (char **)parm_ptr);
1619 } else {
1620 ok = set_variable_helper(mem_ctx, parmnum, parm_ptr,
1621 pszParmName, pszParmValue);
1624 if (!ok) {
1625 return false;
1628 if (on_globals && (lp_ctx->flags[parmnum] & FLAG_DEFAULT)) {
1629 lp_ctx->flags[parmnum] &= ~FLAG_DEFAULT;
1630 /* we have to also unset FLAG_DEFAULT on aliases */
1631 for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
1632 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1634 for (i=parmnum+1;i<num_parameters() && parm_table[i].offset == parm_table[parmnum].offset;i++) {
1635 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1638 return true;
1642 bool lpcfg_do_global_parameter(struct loadparm_context *lp_ctx,
1643 const char *pszParmName, const char *pszParmValue)
1645 int parmnum = lpcfg_map_parameter(pszParmName);
1646 void *parm_ptr;
1648 if (parmnum < 0) {
1649 if (strchr(pszParmName, ':')) {
1650 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName, pszParmValue, 0);
1652 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1653 return true;
1656 /* if the flag has been set on the command line, then don't allow override,
1657 but don't report an error */
1658 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1659 return true;
1662 if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
1663 DEBUG(1, ("WARNING: The \"%s\" option is deprecated\n",
1664 pszParmName));
1667 parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[parmnum]);
1669 return set_variable(lp_ctx->globals->ctx, NULL, parmnum, parm_ptr,
1670 pszParmName, pszParmValue, lp_ctx, true);
1673 bool lpcfg_do_service_parameter(struct loadparm_context *lp_ctx,
1674 struct loadparm_service *service,
1675 const char *pszParmName, const char *pszParmValue)
1677 void *parm_ptr;
1678 int i;
1679 int parmnum = lpcfg_map_parameter(pszParmName);
1681 if (parmnum < 0) {
1682 if (strchr(pszParmName, ':')) {
1683 return lp_do_parameter_parametric(lp_ctx, service, pszParmName, pszParmValue, 0);
1685 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1686 return true;
1689 /* if the flag has been set on the command line, then don't allow override,
1690 but don't report an error */
1691 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1692 return true;
1695 if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
1696 DEBUG(1, ("WARNING: The \"%s\" option is deprecated\n",
1697 pszParmName));
1700 if (parm_table[parmnum].p_class == P_GLOBAL) {
1701 DEBUG(0,
1702 ("Global parameter %s found in service section!\n",
1703 pszParmName));
1704 return true;
1706 parm_ptr = ((char *)service) + parm_table[parmnum].offset;
1708 if (!service->copymap)
1709 init_copymap(service);
1711 /* this handles the aliases - set the copymap for other
1712 * entries with the same data pointer */
1713 for (i = 0; parm_table[i].label; i++)
1714 if (parm_table[i].offset == parm_table[parmnum].offset &&
1715 parm_table[i].p_class == parm_table[parmnum].p_class)
1716 bitmap_clear(service->copymap, i);
1718 return set_variable(service, service, parmnum, parm_ptr, pszParmName,
1719 pszParmValue, lp_ctx, false);
1723 * Process a parameter.
1726 bool lpcfg_do_parameter(const char *pszParmName, const char *pszParmValue,
1727 void *userdata)
1729 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1731 if (lp_ctx->bInGlobalSection)
1732 return lpcfg_do_global_parameter(lp_ctx, pszParmName,
1733 pszParmValue);
1734 else
1735 return lpcfg_do_service_parameter(lp_ctx, lp_ctx->currentService,
1736 pszParmName, pszParmValue);
1740 variable argument do parameter
1742 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx, const char *pszParmName, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
1743 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx,
1744 const char *pszParmName, const char *fmt, ...)
1746 char *s;
1747 bool ret;
1748 va_list ap;
1750 va_start(ap, fmt);
1751 s = talloc_vasprintf(NULL, fmt, ap);
1752 va_end(ap);
1753 ret = lpcfg_do_global_parameter(lp_ctx, pszParmName, s);
1754 talloc_free(s);
1755 return ret;
1760 set a parameter from the commandline - this is called from command line parameter
1761 parsing code. It sets the parameter then marks the parameter as unable to be modified
1762 by smb.conf processing
1764 bool lpcfg_set_cmdline(struct loadparm_context *lp_ctx, const char *pszParmName,
1765 const char *pszParmValue)
1767 int parmnum;
1768 int i;
1770 while (isspace((unsigned char)*pszParmValue)) pszParmValue++;
1772 parmnum = lpcfg_map_parameter(pszParmName);
1774 if (parmnum < 0 && strchr(pszParmName, ':')) {
1775 /* set a parametric option */
1776 bool ok;
1777 ok = lp_do_parameter_parametric(lp_ctx, NULL, pszParmName,
1778 pszParmValue, FLAG_CMDLINE);
1779 if (lp_ctx->s3_fns != NULL) {
1780 if (ok) {
1781 lp_ctx->s3_fns->store_cmdline(pszParmName, pszParmValue);
1784 return ok;
1787 if (parmnum < 0) {
1788 DEBUG(0,("Unknown option '%s'\n", pszParmName));
1789 return false;
1792 /* reset the CMDLINE flag in case this has been called before */
1793 lp_ctx->flags[parmnum] &= ~FLAG_CMDLINE;
1795 if (!lpcfg_do_global_parameter(lp_ctx, pszParmName, pszParmValue)) {
1796 return false;
1799 lp_ctx->flags[parmnum] |= FLAG_CMDLINE;
1801 /* we have to also set FLAG_CMDLINE on aliases */
1802 for (i=parmnum-1;
1803 i>=0 && parm_table[i].p_class == parm_table[parmnum].p_class &&
1804 parm_table[i].offset == parm_table[parmnum].offset;
1805 i--) {
1806 lp_ctx->flags[i] |= FLAG_CMDLINE;
1808 for (i=parmnum+1;
1809 i<num_parameters() &&
1810 parm_table[i].p_class == parm_table[parmnum].p_class &&
1811 parm_table[i].offset == parm_table[parmnum].offset;
1812 i++) {
1813 lp_ctx->flags[i] |= FLAG_CMDLINE;
1816 if (lp_ctx->s3_fns != NULL) {
1817 lp_ctx->s3_fns->store_cmdline(pszParmName, pszParmValue);
1820 return true;
1824 set a option from the commandline in 'a=b' format. Use to support --option
1826 bool lpcfg_set_option(struct loadparm_context *lp_ctx, const char *option)
1828 char *p, *s;
1829 bool ret;
1831 s = talloc_strdup(NULL, option);
1832 if (!s) {
1833 return false;
1836 p = strchr(s, '=');
1837 if (!p) {
1838 talloc_free(s);
1839 return false;
1842 *p = 0;
1844 ret = lpcfg_set_cmdline(lp_ctx, s, p+1);
1845 talloc_free(s);
1846 return ret;
1850 #define BOOLSTR(b) ((b) ? "Yes" : "No")
1853 * Print a parameter of the specified type.
1856 void lpcfg_print_parameter(struct parm_struct *p, void *ptr, FILE * f)
1858 /* For the seperation of lists values that we print below */
1859 const char *list_sep = ", ";
1860 int i;
1861 switch (p->type)
1863 case P_ENUM:
1864 for (i = 0; p->enum_list[i].name; i++) {
1865 if (*(int *)ptr == p->enum_list[i].value) {
1866 fprintf(f, "%s",
1867 p->enum_list[i].name);
1868 break;
1871 break;
1873 case P_BOOL:
1874 fprintf(f, "%s", BOOLSTR(*(bool *)ptr));
1875 break;
1877 case P_BOOLREV:
1878 fprintf(f, "%s", BOOLSTR(!*(bool *)ptr));
1879 break;
1881 case P_INTEGER:
1882 case P_BYTES:
1883 fprintf(f, "%d", *(int *)ptr);
1884 break;
1886 case P_CHAR:
1887 fprintf(f, "%c", *(char *)ptr);
1888 break;
1890 case P_OCTAL: {
1891 int val = *(int *)ptr;
1892 if (val == -1) {
1893 fprintf(f, "-1");
1894 } else {
1895 fprintf(f, "0%03o", val);
1897 break;
1900 case P_CMDLIST:
1901 list_sep = " ";
1902 /* fall through */
1903 case P_LIST:
1904 if ((char ***)ptr && *(char ***)ptr) {
1905 char **list = *(char ***)ptr;
1906 for (; *list; list++) {
1907 /* surround strings with whitespace in double quotes */
1908 if (*(list+1) == NULL) {
1909 /* last item, no extra separator */
1910 list_sep = "";
1912 if ( strchr_m( *list, ' ' ) ) {
1913 fprintf(f, "\"%s\"%s", *list, list_sep);
1914 } else {
1915 fprintf(f, "%s%s", *list, list_sep);
1919 break;
1921 case P_STRING:
1922 case P_USTRING:
1923 if (*(char **)ptr) {
1924 fprintf(f, "%s", *(char **)ptr);
1926 break;
1931 * Check if two parameters are equal.
1934 static bool lpcfg_equal_parameter(parm_type type, void *ptr1, void *ptr2)
1936 switch (type) {
1937 case P_BOOL:
1938 case P_BOOLREV:
1939 return (*((bool *)ptr1) == *((bool *)ptr2));
1941 case P_INTEGER:
1942 case P_ENUM:
1943 case P_OCTAL:
1944 case P_BYTES:
1945 return (*((int *)ptr1) == *((int *)ptr2));
1947 case P_CHAR:
1948 return (*((char *)ptr1) == *((char *)ptr2));
1950 case P_LIST:
1951 case P_CMDLIST:
1952 return str_list_equal(*(const char ***)ptr1, *(const char ***)ptr2);
1954 case P_STRING:
1955 case P_USTRING:
1957 char *p1 = *(char **)ptr1, *p2 = *(char **)ptr2;
1958 if (p1 && !*p1)
1959 p1 = NULL;
1960 if (p2 && !*p2)
1961 p2 = NULL;
1962 return (p1 == p2 || strequal(p1, p2));
1965 return false;
1969 * Process a new section (service).
1971 * At this stage all sections are services.
1972 * Later we'll have special sections that permit server parameters to be set.
1973 * Returns True on success, False on failure.
1976 static bool do_section(const char *pszSectionName, void *userdata)
1978 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1979 bool bRetval;
1980 bool isglobal;
1982 if (lp_ctx->s3_fns != NULL) {
1983 return lp_ctx->s3_fns->do_section(pszSectionName, lp_ctx);
1986 isglobal = ((strwicmp(pszSectionName, GLOBAL_NAME) == 0) ||
1987 (strwicmp(pszSectionName, GLOBAL_NAME2) == 0));
1989 bRetval = false;
1991 /* if we've just struck a global section, note the fact. */
1992 lp_ctx->bInGlobalSection = isglobal;
1994 /* check for multiple global sections */
1995 if (lp_ctx->bInGlobalSection) {
1996 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1997 return true;
2000 /* if we have a current service, tidy it up before moving on */
2001 bRetval = true;
2003 if (lp_ctx->currentService != NULL)
2004 bRetval = lpcfg_service_ok(lp_ctx->currentService);
2006 /* if all is still well, move to the next record in the services array */
2007 if (bRetval) {
2008 /* We put this here to avoid an odd message order if messages are */
2009 /* issued by the post-processing of a previous section. */
2010 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
2012 if ((lp_ctx->currentService = lpcfg_add_service(lp_ctx, lp_ctx->sDefault,
2013 pszSectionName))
2014 == NULL) {
2015 DEBUG(0, ("Failed to add a new service\n"));
2016 return false;
2020 return bRetval;
2025 * Determine if a particular base parameter is currently set to the default value.
2028 static bool is_default(void *base_structure, int i)
2030 void *def_ptr = ((char *)base_structure) + parm_table[i].offset;
2031 switch (parm_table[i].type) {
2032 case P_CMDLIST:
2033 case P_LIST:
2034 return str_list_equal((const char * const *)parm_table[i].def.lvalue,
2035 *(const char * const **)def_ptr);
2036 case P_STRING:
2037 case P_USTRING:
2038 return strequal(parm_table[i].def.svalue,
2039 *(char **)def_ptr);
2040 case P_BOOL:
2041 case P_BOOLREV:
2042 return parm_table[i].def.bvalue ==
2043 *(bool *)def_ptr;
2044 case P_INTEGER:
2045 case P_CHAR:
2046 case P_OCTAL:
2047 case P_BYTES:
2048 case P_ENUM:
2049 return parm_table[i].def.ivalue ==
2050 *(int *)def_ptr;
2052 return false;
2056 *Display the contents of the global structure.
2059 void lpcfg_dump_globals(struct loadparm_context *lp_ctx, FILE *f,
2060 bool show_defaults)
2062 int i;
2063 struct parmlist_entry *data;
2065 fprintf(f, "# Global parameters\n[global]\n");
2067 for (i = 0; parm_table[i].label; i++)
2068 if (parm_table[i].p_class == P_GLOBAL &&
2069 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset))) {
2070 if (!show_defaults) {
2071 if (lp_ctx->flags && (lp_ctx->flags[i] & FLAG_DEFAULT)) {
2072 continue;
2075 if (is_default(lp_ctx->globals, i)) {
2076 continue;
2080 fprintf(f, "\t%s = ", parm_table[i].label);
2081 lpcfg_print_parameter(&parm_table[i], lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[i]), f);
2082 fprintf(f, "\n");
2084 if (lp_ctx->globals->param_opt != NULL) {
2085 for (data = lp_ctx->globals->param_opt; data;
2086 data = data->next) {
2087 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
2088 continue;
2090 fprintf(f, "\t%s = %s\n", data->key, data->value);
2097 * Display the contents of a single services record.
2100 void lpcfg_dump_a_service(struct loadparm_service * pService, struct loadparm_service *sDefault, FILE * f,
2101 unsigned int *flags, bool show_defaults)
2103 int i;
2104 struct parmlist_entry *data;
2106 if (pService != sDefault)
2107 fprintf(f, "\n[%s]\n", pService->szService);
2109 for (i = 0; parm_table[i].label; i++) {
2110 if (parm_table[i].p_class == P_LOCAL &&
2111 (*parm_table[i].label != '-') &&
2112 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset)))
2114 if (pService == sDefault) {
2115 if (!show_defaults) {
2116 if (flags && (flags[i] & FLAG_DEFAULT)) {
2117 continue;
2120 if (is_default(sDefault, i)) {
2121 continue;
2124 } else {
2125 if (lpcfg_equal_parameter(parm_table[i].type,
2126 ((char *)pService) +
2127 parm_table[i].offset,
2128 ((char *)sDefault) +
2129 parm_table[i].offset))
2130 continue;
2133 fprintf(f, "\t%s = ", parm_table[i].label);
2134 lpcfg_print_parameter(&parm_table[i],
2135 ((char *)pService) + parm_table[i].offset, f);
2136 fprintf(f, "\n");
2139 if (pService->param_opt != NULL) {
2140 for (data = pService->param_opt; data; data = data->next) {
2141 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
2142 continue;
2144 fprintf(f, "\t%s = %s\n", data->key, data->value);
2149 bool lpcfg_dump_a_parameter(struct loadparm_context *lp_ctx,
2150 struct loadparm_service *service,
2151 const char *parm_name, FILE * f)
2153 struct parm_struct *parm;
2154 void *ptr;
2155 char *local_parm_name;
2156 char *parm_opt;
2157 const char *parm_opt_value;
2159 /* check for parametrical option */
2160 local_parm_name = talloc_strdup(lp_ctx, parm_name);
2161 if (local_parm_name == NULL) {
2162 return false;
2165 parm_opt = strchr( local_parm_name, ':');
2167 if (parm_opt) {
2168 *parm_opt = '\0';
2169 parm_opt++;
2170 if (strlen(parm_opt)) {
2171 parm_opt_value = lpcfg_parm_string(lp_ctx, service,
2172 local_parm_name, parm_opt);
2173 if (parm_opt_value) {
2174 fprintf(f, "%s\n", parm_opt_value);
2175 return true;
2178 return false;
2181 /* parameter is not parametric, search the table */
2182 parm = lpcfg_parm_struct(lp_ctx, parm_name);
2183 if (!parm) {
2184 return false;
2187 if (service != NULL && parm->p_class == P_GLOBAL) {
2188 return false;
2191 ptr = lpcfg_parm_ptr(lp_ctx, service,parm);
2193 lpcfg_print_parameter(parm, ptr, f);
2194 fprintf(f, "\n");
2195 return true;
2199 * Auto-load some home services.
2201 static void lpcfg_add_auto_services(struct loadparm_context *lp_ctx,
2202 const char *str)
2204 return;
2207 /***************************************************************************
2208 Initialise the sDefault parameter structure for the printer values.
2209 ***************************************************************************/
2211 void init_printer_values(struct loadparm_context *lp_ctx, TALLOC_CTX *ctx,
2212 struct loadparm_service *pService)
2214 /* choose defaults depending on the type of printing */
2215 switch (pService->printing) {
2216 case PRINT_BSD:
2217 case PRINT_AIX:
2218 case PRINT_LPRNT:
2219 case PRINT_LPROS2:
2220 lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P'%p'");
2221 lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P'%p' %j");
2222 lpcfg_string_set(ctx, &pService->print_command, "lpr -r -P'%p' %s");
2223 break;
2225 case PRINT_LPRNG:
2226 case PRINT_PLP:
2227 lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P'%p'");
2228 lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P'%p' %j");
2229 lpcfg_string_set(ctx, &pService->print_command, "lpr -r -P'%p' %s");
2230 lpcfg_string_set(ctx, &pService->queuepause_command, "lpc stop '%p'");
2231 lpcfg_string_set(ctx, &pService->queueresume_command, "lpc start '%p'");
2232 lpcfg_string_set(ctx, &pService->lppause_command, "lpc hold '%p' %j");
2233 lpcfg_string_set(ctx, &pService->lpresume_command, "lpc release '%p' %j");
2234 break;
2236 case PRINT_CUPS:
2237 case PRINT_IPRINT:
2238 /* set the lpq command to contain the destination printer
2239 name only. This is used by cups_queue_get() */
2240 lpcfg_string_set(ctx, &pService->lpq_command, "%p");
2241 lpcfg_string_set(ctx, &pService->lprm_command, "");
2242 lpcfg_string_set(ctx, &pService->print_command, "");
2243 lpcfg_string_set(ctx, &pService->lppause_command, "");
2244 lpcfg_string_set(ctx, &pService->lpresume_command, "");
2245 lpcfg_string_set(ctx, &pService->queuepause_command, "");
2246 lpcfg_string_set(ctx, &pService->queueresume_command, "");
2247 break;
2249 case PRINT_SYSV:
2250 case PRINT_HPUX:
2251 lpcfg_string_set(ctx, &pService->lpq_command, "lpstat -o%p");
2252 lpcfg_string_set(ctx, &pService->lprm_command, "cancel %p-%j");
2253 lpcfg_string_set(ctx, &pService->print_command, "lp -c -d%p %s; rm %s");
2254 lpcfg_string_set(ctx, &pService->queuepause_command, "disable %p");
2255 lpcfg_string_set(ctx, &pService->queueresume_command, "enable %p");
2256 #ifndef HPUX
2257 lpcfg_string_set(ctx, &pService->lppause_command, "lp -i %p-%j -H hold");
2258 lpcfg_string_set(ctx, &pService->lpresume_command, "lp -i %p-%j -H resume");
2259 #endif /* HPUX */
2260 break;
2262 case PRINT_QNX:
2263 lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P%p");
2264 lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P%p %j");
2265 lpcfg_string_set(ctx, &pService->print_command, "lp -r -P%p %s");
2266 break;
2268 #if defined(DEVELOPER) || defined(ENABLE_SELFTEST)
2270 case PRINT_TEST:
2271 case PRINT_VLP: {
2272 const char *tdbfile;
2273 TALLOC_CTX *tmp_ctx = talloc_new(ctx);
2274 const char *tmp;
2276 tmp = lpcfg_parm_string(lp_ctx, NULL, "vlp", "tdbfile");
2277 if (tmp == NULL) {
2278 tmp = "/tmp/vlp.tdb";
2281 tdbfile = talloc_asprintf(tmp_ctx, "tdbfile=%s", tmp);
2282 if (tdbfile == NULL) {
2283 tdbfile="tdbfile=/tmp/vlp.tdb";
2286 tmp = talloc_asprintf(tmp_ctx, "vlp %s print %%p %%s",
2287 tdbfile);
2288 lpcfg_string_set(ctx, &pService->print_command,
2289 tmp ? tmp : "vlp print %p %s");
2291 tmp = talloc_asprintf(tmp_ctx, "vlp %s lpq %%p",
2292 tdbfile);
2293 lpcfg_string_set(ctx, &pService->lpq_command,
2294 tmp ? tmp : "vlp lpq %p");
2296 tmp = talloc_asprintf(tmp_ctx, "vlp %s lprm %%p %%j",
2297 tdbfile);
2298 lpcfg_string_set(ctx, &pService->lprm_command,
2299 tmp ? tmp : "vlp lprm %p %j");
2301 tmp = talloc_asprintf(tmp_ctx, "vlp %s lppause %%p %%j",
2302 tdbfile);
2303 lpcfg_string_set(ctx, &pService->lppause_command,
2304 tmp ? tmp : "vlp lppause %p %j");
2306 tmp = talloc_asprintf(tmp_ctx, "vlp %s lpresume %%p %%j",
2307 tdbfile);
2308 lpcfg_string_set(ctx, &pService->lpresume_command,
2309 tmp ? tmp : "vlp lpresume %p %j");
2311 tmp = talloc_asprintf(tmp_ctx, "vlp %s queuepause %%p",
2312 tdbfile);
2313 lpcfg_string_set(ctx, &pService->queuepause_command,
2314 tmp ? tmp : "vlp queuepause %p");
2316 tmp = talloc_asprintf(tmp_ctx, "vlp %s queueresume %%p",
2317 tdbfile);
2318 lpcfg_string_set(ctx, &pService->queueresume_command,
2319 tmp ? tmp : "vlp queueresume %p");
2320 TALLOC_FREE(tmp_ctx);
2322 break;
2324 #endif /* DEVELOPER */
2330 * Unload unused services.
2333 void lpcfg_killunused(struct loadparm_context *lp_ctx,
2334 struct smbsrv_connection *smb,
2335 bool (*snumused) (struct smbsrv_connection *, int))
2337 int i;
2339 if (lp_ctx->s3_fns != NULL) {
2340 smb_panic("Cannot be used from an s3 loadparm ctx");
2343 for (i = 0; i < lp_ctx->iNumServices; i++) {
2344 if (lp_ctx->services[i] == NULL)
2345 continue;
2347 if (!snumused || !snumused(smb, i)) {
2348 talloc_free(lp_ctx->services[i]);
2349 lp_ctx->services[i] = NULL;
2355 static int lpcfg_destructor(struct loadparm_context *lp_ctx)
2357 struct parmlist_entry *data;
2359 if (lp_ctx->refuse_free) {
2360 /* someone is trying to free the
2361 global_loadparm_context.
2362 We can't allow that. */
2363 return -1;
2366 if (lp_ctx->globals->param_opt != NULL) {
2367 struct parmlist_entry *next;
2368 for (data = lp_ctx->globals->param_opt; data; data=next) {
2369 next = data->next;
2370 if (data->priority & FLAG_CMDLINE) continue;
2371 DLIST_REMOVE(lp_ctx->globals->param_opt, data);
2372 talloc_free(data);
2376 return 0;
2379 struct defaults_hook_data {
2380 const char *name;
2381 lpcfg_defaults_hook hook;
2382 struct defaults_hook_data *prev, *next;
2383 } *defaults_hooks = NULL;
2386 bool lpcfg_register_defaults_hook(const char *name, lpcfg_defaults_hook hook)
2388 struct defaults_hook_data *hook_data = talloc(talloc_autofree_context(),
2389 struct defaults_hook_data);
2390 hook_data->name = talloc_strdup(hook_data, name);
2391 hook_data->hook = hook;
2392 DLIST_ADD(defaults_hooks, hook_data);
2393 return false;
2397 * Initialise the global parameter structure.
2399 * Note that most callers should use loadparm_init_global() instead
2401 struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
2403 int i;
2404 char *myname;
2405 struct loadparm_context *lp_ctx;
2406 struct parmlist_entry *parm;
2407 char *logfile;
2408 struct defaults_hook_data *defaults_hook;
2410 lp_ctx = talloc_zero(mem_ctx, struct loadparm_context);
2411 if (lp_ctx == NULL)
2412 return NULL;
2414 talloc_set_destructor(lp_ctx, lpcfg_destructor);
2415 lp_ctx->bInGlobalSection = true;
2416 lp_ctx->globals = talloc_zero(lp_ctx, struct loadparm_global);
2417 /* This appears odd, but globals in s3 isn't a pointer */
2418 lp_ctx->globals->ctx = lp_ctx->globals;
2419 lp_ctx->sDefault = talloc_zero(lp_ctx, struct loadparm_service);
2420 lp_ctx->flags = talloc_zero_array(lp_ctx, unsigned int, num_parameters());
2422 lp_ctx->sDefault->max_print_jobs = 1000;
2423 lp_ctx->sDefault->bAvailable = true;
2424 lp_ctx->sDefault->browseable = true;
2425 lp_ctx->sDefault->read_only = true;
2426 lp_ctx->sDefault->map_archive = true;
2427 lp_ctx->sDefault->strict_locking = true;
2428 lp_ctx->sDefault->oplocks = true;
2429 lp_ctx->sDefault->create_mask = 0744;
2430 lp_ctx->sDefault->force_create_mode = 0000;
2431 lp_ctx->sDefault->directory_mask = 0755;
2432 lp_ctx->sDefault->force_directory_mode = 0000;
2434 DEBUG(3, ("Initialising global parameters\n"));
2436 for (i = 0; parm_table[i].label; i++) {
2437 if ((parm_table[i].type == P_STRING ||
2438 parm_table[i].type == P_USTRING) &&
2439 !(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2440 char **r;
2441 if (parm_table[i].p_class == P_LOCAL) {
2442 r = (char **)(((char *)lp_ctx->sDefault) + parm_table[i].offset);
2443 } else {
2444 r = (char **)(((char *)lp_ctx->globals) + parm_table[i].offset);
2446 *r = talloc_strdup(lp_ctx, "");
2450 logfile = talloc_asprintf(lp_ctx, "%s/log.samba", dyn_LOGFILEBASE);
2451 lpcfg_do_global_parameter(lp_ctx, "log file", logfile);
2452 talloc_free(logfile);
2454 lpcfg_do_global_parameter(lp_ctx, "log level", "0");
2456 lpcfg_do_global_parameter(lp_ctx, "syslog", "1");
2457 lpcfg_do_global_parameter(lp_ctx, "syslog only", "No");
2458 lpcfg_do_global_parameter(lp_ctx, "debug timestamp", "Yes");
2459 lpcfg_do_global_parameter(lp_ctx, "debug prefix timestamp", "No");
2460 lpcfg_do_global_parameter(lp_ctx, "debug hires timestamp", "Yes");
2461 lpcfg_do_global_parameter(lp_ctx, "debug pid", "No");
2462 lpcfg_do_global_parameter(lp_ctx, "debug uid", "No");
2463 lpcfg_do_global_parameter(lp_ctx, "debug class", "No");
2465 lpcfg_do_global_parameter(lp_ctx, "share backend", "classic");
2467 lpcfg_do_global_parameter(lp_ctx, "server role", "auto");
2468 lpcfg_do_global_parameter(lp_ctx, "domain logons", "No");
2469 lpcfg_do_global_parameter(lp_ctx, "domain master", "Auto");
2471 /* options that can be set on the command line must be initialised via
2472 the slower lpcfg_do_global_parameter() to ensure that FLAG_CMDLINE is obeyed */
2473 #ifdef TCP_NODELAY
2474 lpcfg_do_global_parameter(lp_ctx, "socket options", "TCP_NODELAY");
2475 #endif
2476 lpcfg_do_global_parameter(lp_ctx, "workgroup", DEFAULT_WORKGROUP);
2477 myname = get_myname(lp_ctx);
2478 lpcfg_do_global_parameter(lp_ctx, "netbios name", myname);
2479 talloc_free(myname);
2480 lpcfg_do_global_parameter(lp_ctx, "name resolve order", "lmhosts wins host bcast");
2482 lpcfg_do_global_parameter(lp_ctx, "fstype", "NTFS");
2484 lpcfg_do_global_parameter(lp_ctx, "ntvfs handler", "unixuid default");
2485 lpcfg_do_global_parameter(lp_ctx, "max connections", "0");
2487 lpcfg_do_global_parameter(lp_ctx, "dcerpc endpoint servers", "epmapper wkssvc rpcecho samr netlogon lsarpc spoolss drsuapi dssetup unixinfo browser eventlog6 backupkey dnsserver");
2488 lpcfg_do_global_parameter(lp_ctx, "server services", "s3fs rpc nbt wrepl ldap cldap kdc drepl winbindd ntp_signd kcc dnsupdate dns");
2489 lpcfg_do_global_parameter(lp_ctx, "kccsrv:samba_kcc", "false");
2490 /* the winbind method for domain controllers is for both RODC
2491 auth forwarding and for trusted domains */
2492 lpcfg_do_global_parameter(lp_ctx, "private dir", dyn_PRIVATE_DIR);
2493 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_LOCAL_MACHINE", "hklm.ldb");
2495 /* This hive should be dynamically generated by Samba using
2496 data from the sam, but for the moment leave it in a tdb to
2497 keep regedt32 from popping up an annoying dialog. */
2498 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_USERS", "hku.ldb");
2500 /* using UTF8 by default allows us to support all chars */
2501 lpcfg_do_global_parameter(lp_ctx, "unix charset", "UTF-8");
2503 /* Use codepage 850 as a default for the dos character set */
2504 lpcfg_do_global_parameter(lp_ctx, "dos charset", "CP850");
2507 * Allow the default PASSWD_CHAT to be overridden in local.h.
2509 lpcfg_do_global_parameter(lp_ctx, "passwd chat", DEFAULT_PASSWD_CHAT);
2511 lpcfg_do_global_parameter(lp_ctx, "pid directory", dyn_PIDDIR);
2512 lpcfg_do_global_parameter(lp_ctx, "lock dir", dyn_LOCKDIR);
2513 lpcfg_do_global_parameter(lp_ctx, "state directory", dyn_STATEDIR);
2514 lpcfg_do_global_parameter(lp_ctx, "cache directory", dyn_CACHEDIR);
2515 lpcfg_do_global_parameter(lp_ctx, "ncalrpc dir", dyn_NCALRPCDIR);
2517 lpcfg_do_global_parameter(lp_ctx, "nbt client socket address", "0.0.0.0");
2518 lpcfg_do_global_parameter_var(lp_ctx, "server string",
2519 "Samba %s", SAMBA_VERSION_STRING);
2521 lpcfg_do_global_parameter(lp_ctx, "password server", "*");
2523 lpcfg_do_global_parameter(lp_ctx, "max mux", "50");
2524 lpcfg_do_global_parameter(lp_ctx, "max xmit", "16644");
2525 lpcfg_do_global_parameter(lp_ctx, "host msdfs", "true");
2527 lpcfg_do_global_parameter(lp_ctx, "LargeReadwrite", "True");
2528 lpcfg_do_global_parameter(lp_ctx, "server min protocol", "LANMAN1");
2529 lpcfg_do_global_parameter(lp_ctx, "server max protocol", "SMB3");
2530 lpcfg_do_global_parameter(lp_ctx, "client min protocol", "CORE");
2531 lpcfg_do_global_parameter(lp_ctx, "client max protocol", "default");
2532 lpcfg_do_global_parameter(lp_ctx, "security", "AUTO");
2533 lpcfg_do_global_parameter(lp_ctx, "EncryptPasswords", "True");
2534 lpcfg_do_global_parameter(lp_ctx, "ReadRaw", "True");
2535 lpcfg_do_global_parameter(lp_ctx, "WriteRaw", "True");
2536 lpcfg_do_global_parameter(lp_ctx, "NullPasswords", "False");
2537 lpcfg_do_global_parameter(lp_ctx, "old password allowed period", "60");
2538 lpcfg_do_global_parameter(lp_ctx, "ObeyPamRestrictions", "False");
2540 lpcfg_do_global_parameter(lp_ctx, "TimeServer", "False");
2541 lpcfg_do_global_parameter(lp_ctx, "BindInterfacesOnly", "False");
2542 lpcfg_do_global_parameter(lp_ctx, "Unicode", "True");
2543 lpcfg_do_global_parameter(lp_ctx, "ClientLanManAuth", "False");
2544 lpcfg_do_global_parameter(lp_ctx, "ClientNTLMv2Auth", "True");
2545 lpcfg_do_global_parameter(lp_ctx, "LanmanAuth", "False");
2546 lpcfg_do_global_parameter(lp_ctx, "NTLMAuth", "True");
2547 lpcfg_do_global_parameter(lp_ctx, "client use spnego principal", "False");
2549 lpcfg_do_global_parameter(lp_ctx, "UnixExtensions", "True");
2551 lpcfg_do_global_parameter(lp_ctx, "PreferredMaster", "Auto");
2552 lpcfg_do_global_parameter(lp_ctx, "LocalMaster", "True");
2554 lpcfg_do_global_parameter(lp_ctx, "wins support", "False");
2555 lpcfg_do_global_parameter(lp_ctx, "dns proxy", "True");
2557 lpcfg_do_global_parameter(lp_ctx, "winbind separator", "\\");
2558 lpcfg_do_global_parameter(lp_ctx, "winbind sealed pipes", "True");
2559 lpcfg_do_global_parameter(lp_ctx, "require strong key", "True");
2560 lpcfg_do_global_parameter(lp_ctx, "winbindd socket directory", dyn_WINBINDD_SOCKET_DIR);
2561 lpcfg_do_global_parameter(lp_ctx, "winbindd privileged socket directory", dyn_WINBINDD_PRIVILEGED_SOCKET_DIR);
2562 lpcfg_do_global_parameter(lp_ctx, "ntp signd socket directory", dyn_NTP_SIGND_SOCKET_DIR);
2563 lpcfg_do_global_parameter_var(lp_ctx, "dns update command", "%s/samba_dnsupdate", dyn_SCRIPTSBINDIR);
2564 lpcfg_do_global_parameter_var(lp_ctx, "spn update command", "%s/samba_spnupdate", dyn_SCRIPTSBINDIR);
2565 lpcfg_do_global_parameter_var(lp_ctx, "samba kcc command",
2566 "%s/samba_kcc", dyn_SCRIPTSBINDIR);
2567 lpcfg_do_global_parameter(lp_ctx, "template shell", "/bin/false");
2568 lpcfg_do_global_parameter(lp_ctx, "template homedir", "/home/%D/%U");
2570 lpcfg_do_global_parameter(lp_ctx, "client signing", "default");
2571 lpcfg_do_global_parameter(lp_ctx, "server signing", "default");
2573 lpcfg_do_global_parameter(lp_ctx, "use spnego", "True");
2575 lpcfg_do_global_parameter(lp_ctx, "use mmap", "True");
2577 lpcfg_do_global_parameter(lp_ctx, "smb ports", "445 139");
2578 lpcfg_do_global_parameter_var(lp_ctx, "nbt port", "%d", NBT_NAME_SERVICE_PORT);
2579 lpcfg_do_global_parameter_var(lp_ctx, "dgram port", "%d", NBT_DGRAM_SERVICE_PORT);
2580 lpcfg_do_global_parameter(lp_ctx, "cldap port", "389");
2581 lpcfg_do_global_parameter(lp_ctx, "krb5 port", "88");
2582 lpcfg_do_global_parameter(lp_ctx, "kpasswd port", "464");
2583 lpcfg_do_global_parameter(lp_ctx, "web port", "901");
2585 lpcfg_do_global_parameter(lp_ctx, "nt status support", "True");
2587 lpcfg_do_global_parameter(lp_ctx, "max wins ttl", "518400"); /* 6 days */
2588 lpcfg_do_global_parameter(lp_ctx, "min wins ttl", "21600");
2590 lpcfg_do_global_parameter(lp_ctx, "tls enabled", "True");
2591 lpcfg_do_global_parameter(lp_ctx, "tls keyfile", "tls/key.pem");
2592 lpcfg_do_global_parameter(lp_ctx, "tls certfile", "tls/cert.pem");
2593 lpcfg_do_global_parameter(lp_ctx, "tls cafile", "tls/ca.pem");
2594 lpcfg_do_global_parameter(lp_ctx, "tls priority", "NORMAL:-VERS-SSL3.0");
2595 lpcfg_do_global_parameter(lp_ctx, "prefork children:smb", "4");
2597 lpcfg_do_global_parameter(lp_ctx, "rndc command", "/usr/sbin/rndc");
2598 lpcfg_do_global_parameter(lp_ctx, "nsupdate command", "/usr/bin/nsupdate -g");
2600 lpcfg_do_global_parameter(lp_ctx, "allow dns updates", "secure only");
2601 lpcfg_do_global_parameter(lp_ctx, "dns forwarder", "");
2603 lpcfg_do_global_parameter(lp_ctx, "algorithmic rid base", "1000");
2605 lpcfg_do_global_parameter(lp_ctx, "enhanced browsing", "True");
2607 lpcfg_do_global_parameter(lp_ctx, "winbind nss info", "template");
2609 lpcfg_do_global_parameter(lp_ctx, "server schannel", "Auto");
2611 lpcfg_do_global_parameter(lp_ctx, "short preserve case", "True");
2613 lpcfg_do_global_parameter(lp_ctx, "max open files", "16384");
2615 lpcfg_do_global_parameter(lp_ctx, "cups connection timeout", "30");
2617 lpcfg_do_global_parameter(lp_ctx, "locking", "True");
2619 lpcfg_do_global_parameter(lp_ctx, "block size", "1024");
2621 lpcfg_do_global_parameter(lp_ctx, "client use spnego", "True");
2623 lpcfg_do_global_parameter(lp_ctx, "change notify", "True");
2625 lpcfg_do_global_parameter(lp_ctx, "name cache timeout", "660");
2627 lpcfg_do_global_parameter(lp_ctx, "defer sharing violations", "True");
2629 lpcfg_do_global_parameter(lp_ctx, "ldap replication sleep", "1000");
2631 lpcfg_do_global_parameter(lp_ctx, "idmap backend", "tdb");
2633 lpcfg_do_global_parameter(lp_ctx, "enable privileges", "True");
2635 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max write", "%u", DEFAULT_SMB2_MAX_WRITE);
2637 lpcfg_do_global_parameter(lp_ctx, "passdb backend", "tdbsam");
2639 lpcfg_do_global_parameter(lp_ctx, "getwd cache", "True");
2641 lpcfg_do_global_parameter(lp_ctx, "winbind nested groups", "True");
2643 lpcfg_do_global_parameter(lp_ctx, "mangled names", "True");
2645 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max credits", "%u", DEFAULT_SMB2_MAX_CREDITS);
2647 lpcfg_do_global_parameter(lp_ctx, "ldap ssl", "start tls");
2649 lpcfg_do_global_parameter(lp_ctx, "ldap deref", "auto");
2651 lpcfg_do_global_parameter(lp_ctx, "lm interval", "60");
2653 lpcfg_do_global_parameter(lp_ctx, "mangling method", "hash2");
2655 lpcfg_do_global_parameter(lp_ctx, "hide dot files", "True");
2657 lpcfg_do_global_parameter(lp_ctx, "browse list", "True");
2659 lpcfg_do_global_parameter(lp_ctx, "passwd chat timeout", "2");
2661 lpcfg_do_global_parameter(lp_ctx, "guest account", GUEST_ACCOUNT);
2663 lpcfg_do_global_parameter(lp_ctx, "client schannel", "auto");
2665 lpcfg_do_global_parameter(lp_ctx, "smb encrypt", "default");
2667 lpcfg_do_global_parameter(lp_ctx, "max log size", "5000");
2669 lpcfg_do_global_parameter(lp_ctx, "idmap negative cache time", "120");
2671 lpcfg_do_global_parameter(lp_ctx, "ldap follow referral", "auto");
2673 lpcfg_do_global_parameter(lp_ctx, "multicast dns register", "yes");
2675 lpcfg_do_global_parameter(lp_ctx, "winbind reconnect delay", "30");
2677 lpcfg_do_global_parameter(lp_ctx, "winbind request timeout", "60");
2679 lpcfg_do_global_parameter(lp_ctx, "nt acl support", "yes");
2681 lpcfg_do_global_parameter(lp_ctx, "acl check permissions", "yes");
2683 lpcfg_do_global_parameter(lp_ctx, "keepalive", "300");
2685 lpcfg_do_global_parameter(lp_ctx, "smbd profiling level", "off");
2687 lpcfg_do_global_parameter(lp_ctx, "winbind cache time", "300");
2689 lpcfg_do_global_parameter(lp_ctx, "level2 oplocks", "yes");
2691 lpcfg_do_global_parameter(lp_ctx, "show add printer wizard", "yes");
2693 lpcfg_do_global_parameter(lp_ctx, "allocation roundup size", "1048576");
2695 lpcfg_do_global_parameter(lp_ctx, "ldap page size", "1024");
2697 lpcfg_do_global_parameter(lp_ctx, "kernel share modes", "yes");
2699 lpcfg_do_global_parameter(lp_ctx, "strict locking", "Auto");
2701 lpcfg_do_global_parameter(lp_ctx, "map readonly", "yes");
2703 lpcfg_do_global_parameter(lp_ctx, "allow trusted domains", "yes");
2705 lpcfg_do_global_parameter(lp_ctx, "default devmode", "yes");
2707 lpcfg_do_global_parameter(lp_ctx, "os level", "20");
2709 lpcfg_do_global_parameter(lp_ctx, "dos filetimes", "yes");
2711 lpcfg_do_global_parameter(lp_ctx, "mangling char", "~");
2713 lpcfg_do_global_parameter(lp_ctx, "printcap cache time", "750");
2715 lpcfg_do_global_parameter(lp_ctx, "create krb5 conf", "yes");
2717 lpcfg_do_global_parameter(lp_ctx, "winbind max clients", "200");
2719 lpcfg_do_global_parameter(lp_ctx, "acl map full control", "yes");
2721 lpcfg_do_global_parameter(lp_ctx, "nt pipe support", "yes");
2723 lpcfg_do_global_parameter(lp_ctx, "ldap debug threshold", "10");
2725 lpcfg_do_global_parameter(lp_ctx, "client ldap sasl wrapping", "sign");
2727 lpcfg_do_global_parameter(lp_ctx, "follow symlinks", "yes");
2729 lpcfg_do_global_parameter(lp_ctx, "machine password timeout", "604800");
2731 lpcfg_do_global_parameter(lp_ctx, "ldap connection timeout", "2");
2733 lpcfg_do_global_parameter(lp_ctx, "winbind expand groups", "0");
2735 lpcfg_do_global_parameter(lp_ctx, "stat cache", "yes");
2737 lpcfg_do_global_parameter(lp_ctx, "lpq cache time", "30");
2739 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max trans", "%u", DEFAULT_SMB2_MAX_TRANSACT);
2741 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max read", "%u", DEFAULT_SMB2_MAX_READ);
2743 lpcfg_do_global_parameter(lp_ctx, "durable handles", "yes");
2745 lpcfg_do_global_parameter(lp_ctx, "max stat cache size", "256");
2747 lpcfg_do_global_parameter(lp_ctx, "ldap passwd sync", "no");
2749 lpcfg_do_global_parameter(lp_ctx, "kernel change notify", "yes");
2751 lpcfg_do_global_parameter(lp_ctx, "max ttl", "259200");
2753 lpcfg_do_global_parameter(lp_ctx, "blocking locks", "yes");
2755 lpcfg_do_global_parameter(lp_ctx, "oplock contention limit", "2");
2757 lpcfg_do_global_parameter(lp_ctx, "load printers", "yes");
2759 lpcfg_do_global_parameter(lp_ctx, "idmap cache time", "604800");
2761 lpcfg_do_global_parameter(lp_ctx, "preserve case", "yes");
2763 lpcfg_do_global_parameter(lp_ctx, "lm announce", "auto");
2765 lpcfg_do_global_parameter(lp_ctx, "afs token lifetime", "604800");
2767 lpcfg_do_global_parameter(lp_ctx, "enable core files", "yes");
2769 lpcfg_do_global_parameter(lp_ctx, "winbind max domain connections", "1");
2771 lpcfg_do_global_parameter(lp_ctx, "case sensitive", "auto");
2773 lpcfg_do_global_parameter(lp_ctx, "ldap timeout", "15");
2775 lpcfg_do_global_parameter(lp_ctx, "mangle prefix", "1");
2777 lpcfg_do_global_parameter(lp_ctx, "posix locking", "yes");
2779 lpcfg_do_global_parameter(lp_ctx, "lock spin time", "200");
2781 lpcfg_do_global_parameter(lp_ctx, "directory name cache size", "100");
2783 lpcfg_do_global_parameter(lp_ctx, "nmbd bind explicit broadcast", "yes");
2785 lpcfg_do_global_parameter(lp_ctx, "init logon delay", "100");
2787 lpcfg_do_global_parameter(lp_ctx, "usershare owner only", "yes");
2789 lpcfg_do_global_parameter(lp_ctx, "-valid", "yes");
2791 lpcfg_do_global_parameter_var(lp_ctx, "usershare path", "%s/usershares", get_dyn_STATEDIR());
2793 #ifdef DEVELOPER
2794 lpcfg_do_global_parameter_var(lp_ctx, "panic action", "/bin/sleep 999999999");
2795 #endif
2797 lpcfg_do_global_parameter(lp_ctx, "smb passwd file", get_dyn_SMB_PASSWD_FILE());
2799 lpcfg_do_global_parameter(lp_ctx, "logon home", "\\\\%N\\%U");
2801 lpcfg_do_global_parameter(lp_ctx, "logon path", "\\\\%N\\%U\\profile");
2803 lpcfg_do_global_parameter(lp_ctx, "printjob username", "%U");
2805 /* Allow modules to adjust defaults */
2806 for (defaults_hook = defaults_hooks; defaults_hook;
2807 defaults_hook = defaults_hook->next) {
2808 bool ret;
2810 ret = defaults_hook->hook(lp_ctx);
2811 if (!ret) {
2812 DEBUG(1, ("Defaults hook %s failed to run.",
2813 defaults_hook->name));
2814 talloc_free(lp_ctx);
2815 return NULL;
2819 for (i = 0; parm_table[i].label; i++) {
2820 if (!(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2821 lp_ctx->flags[i] |= FLAG_DEFAULT;
2825 for (parm=lp_ctx->globals->param_opt; parm; parm=parm->next) {
2826 if (!(parm->priority & FLAG_CMDLINE)) {
2827 parm->priority |= FLAG_DEFAULT;
2831 for (parm=lp_ctx->sDefault->param_opt; parm; parm=parm->next) {
2832 if (!(parm->priority & FLAG_CMDLINE)) {
2833 parm->priority |= FLAG_DEFAULT;
2837 return lp_ctx;
2841 * Initialise the global parameter structure.
2843 struct loadparm_context *loadparm_init_global(bool load_default)
2845 if (global_loadparm_context == NULL) {
2846 global_loadparm_context = loadparm_init(NULL);
2848 if (global_loadparm_context == NULL) {
2849 return NULL;
2851 global_loadparm_context->global = true;
2852 if (load_default && !global_loadparm_context->loaded) {
2853 lpcfg_load_default(global_loadparm_context);
2855 global_loadparm_context->refuse_free = true;
2856 return global_loadparm_context;
2860 * Initialise the global parameter structure.
2862 struct loadparm_context *loadparm_init_s3(TALLOC_CTX *mem_ctx,
2863 const struct loadparm_s3_helpers *s3_fns)
2865 struct loadparm_context *loadparm_context = talloc_zero(mem_ctx, struct loadparm_context);
2866 if (!loadparm_context) {
2867 return NULL;
2869 loadparm_context->s3_fns = s3_fns;
2870 loadparm_context->globals = s3_fns->globals;
2871 loadparm_context->flags = s3_fns->flags;
2873 return loadparm_context;
2876 const char *lpcfg_configfile(struct loadparm_context *lp_ctx)
2878 return lp_ctx->szConfigFile;
2881 const char *lp_default_path(void)
2883 if (getenv("SMB_CONF_PATH"))
2884 return getenv("SMB_CONF_PATH");
2885 else
2886 return dyn_CONFIGFILE;
2890 * Update the internal state of a loadparm context after settings
2891 * have changed.
2893 static bool lpcfg_update(struct loadparm_context *lp_ctx)
2895 struct debug_settings settings;
2896 TALLOC_CTX *tmp_ctx;
2898 tmp_ctx = talloc_new(lp_ctx);
2899 if (tmp_ctx == NULL) {
2900 return false;
2903 lpcfg_add_auto_services(lp_ctx, lpcfg_auto_services(lp_ctx, tmp_ctx));
2905 if (!lp_ctx->globals->wins_server_list && lp_ctx->globals->we_are_a_wins_server) {
2906 lpcfg_do_global_parameter(lp_ctx, "wins server", "127.0.0.1");
2909 if (!lp_ctx->global) {
2910 TALLOC_FREE(tmp_ctx);
2911 return true;
2914 panic_action = lp_ctx->globals->panic_action;
2916 reload_charcnv(lp_ctx);
2918 ZERO_STRUCT(settings);
2919 /* Add any more debug-related smb.conf parameters created in
2920 * future here */
2921 settings.timestamp_logs = lp_ctx->globals->timestamp_logs;
2922 settings.debug_prefix_timestamp = lp_ctx->globals->debug_prefix_timestamp;
2923 settings.debug_hires_timestamp = lp_ctx->globals->debug_hires_timestamp;
2924 settings.debug_pid = lp_ctx->globals->debug_pid;
2925 settings.debug_uid = lp_ctx->globals->debug_uid;
2926 settings.debug_class = lp_ctx->globals->debug_class;
2927 debug_set_settings(&settings, lp_ctx->globals->logging,
2928 lp_ctx->globals->syslog,
2929 lp_ctx->globals->syslog_only);
2931 /* FIXME: This is a bit of a hack, but we can't use a global, since
2932 * not everything that uses lp also uses the socket library */
2933 if (lpcfg_parm_bool(lp_ctx, NULL, "socket", "testnonblock", false)) {
2934 setenv("SOCKET_TESTNONBLOCK", "1", 1);
2935 } else {
2936 unsetenv("SOCKET_TESTNONBLOCK");
2939 TALLOC_FREE(tmp_ctx);
2940 return true;
2943 bool lpcfg_load_default(struct loadparm_context *lp_ctx)
2945 const char *path;
2947 path = lp_default_path();
2949 if (!file_exist(path)) {
2950 /* We allow the default smb.conf file to not exist,
2951 * basically the equivalent of an empty file. */
2952 return lpcfg_update(lp_ctx);
2955 return lpcfg_load(lp_ctx, path);
2959 * Load the services array from the services file.
2961 * Return True on success, False on failure.
2963 bool lpcfg_load(struct loadparm_context *lp_ctx, const char *filename)
2965 char *n2;
2966 bool bRetval;
2968 filename = talloc_strdup(lp_ctx, filename);
2970 lp_ctx->szConfigFile = filename;
2972 if (lp_ctx->s3_fns) {
2973 return lp_ctx->s3_fns->load(filename);
2976 lp_ctx->bInGlobalSection = true;
2977 n2 = standard_sub_basic(lp_ctx, lp_ctx->szConfigFile);
2978 DEBUG(2, ("lpcfg_load: refreshing parameters from %s\n", n2));
2980 add_to_file_list(lp_ctx, &lp_ctx->file_lists, lp_ctx->szConfigFile, n2);
2982 /* We get sections first, so have to start 'behind' to make up */
2983 lp_ctx->currentService = NULL;
2984 bRetval = pm_process(n2, do_section, lpcfg_do_parameter, lp_ctx);
2986 /* finish up the last section */
2987 DEBUG(4, ("pm_process() returned %s\n", BOOLSTR(bRetval)));
2988 if (bRetval)
2989 if (lp_ctx->currentService != NULL)
2990 bRetval = lpcfg_service_ok(lp_ctx->currentService);
2992 bRetval = bRetval && lpcfg_update(lp_ctx);
2994 /* we do this unconditionally, so that it happens even
2995 for a missing smb.conf */
2996 reload_charcnv(lp_ctx);
2998 if (bRetval == true) {
2999 /* set this up so that any child python tasks will
3000 find the right smb.conf */
3001 setenv("SMB_CONF_PATH", filename, 1);
3003 /* set the context used by the lp_*() function
3004 varients */
3005 global_loadparm_context = lp_ctx;
3006 lp_ctx->loaded = true;
3009 return bRetval;
3013 * Return the max number of services.
3016 int lpcfg_numservices(struct loadparm_context *lp_ctx)
3018 if (lp_ctx->s3_fns) {
3019 return lp_ctx->s3_fns->get_numservices();
3022 return lp_ctx->iNumServices;
3026 * Display the contents of the services array in human-readable form.
3029 void lpcfg_dump(struct loadparm_context *lp_ctx, FILE *f, bool show_defaults,
3030 int maxtoprint)
3032 int iService;
3034 if (lp_ctx->s3_fns) {
3035 lp_ctx->s3_fns->dump(f, show_defaults, maxtoprint);
3036 return;
3039 lpcfg_dump_globals(lp_ctx, f, show_defaults);
3041 lpcfg_dump_a_service(lp_ctx->sDefault, lp_ctx->sDefault, f, lp_ctx->flags, show_defaults);
3043 for (iService = 0; iService < maxtoprint; iService++)
3044 lpcfg_dump_one(f, show_defaults, lp_ctx->services[iService], lp_ctx->sDefault);
3048 * Display the contents of one service in human-readable form.
3050 void lpcfg_dump_one(FILE *f, bool show_defaults, struct loadparm_service *service, struct loadparm_service *sDefault)
3052 if (service != NULL) {
3053 if (service->szService[0] == '\0')
3054 return;
3055 lpcfg_dump_a_service(service, sDefault, f, NULL, show_defaults);
3059 struct loadparm_service *lpcfg_servicebynum(struct loadparm_context *lp_ctx,
3060 int snum)
3062 if (lp_ctx->s3_fns) {
3063 return lp_ctx->s3_fns->get_servicebynum(snum);
3066 return lp_ctx->services[snum];
3069 struct loadparm_service *lpcfg_service(struct loadparm_context *lp_ctx,
3070 const char *service_name)
3072 int iService;
3073 char *serviceName;
3075 if (lp_ctx->s3_fns) {
3076 return lp_ctx->s3_fns->get_service(service_name);
3079 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--) {
3080 if (lp_ctx->services[iService] &&
3081 lp_ctx->services[iService]->szService) {
3083 * The substitution here is used to support %U is
3084 * service names
3086 serviceName = standard_sub_basic(
3087 lp_ctx->services[iService],
3088 lp_ctx->services[iService]->szService);
3089 if (strequal(serviceName, service_name)) {
3090 talloc_free(serviceName);
3091 return lp_ctx->services[iService];
3093 talloc_free(serviceName);
3097 DEBUG(7,("lpcfg_servicenumber: couldn't find %s\n", service_name));
3098 return NULL;
3101 const char *lpcfg_servicename(const struct loadparm_service *service)
3103 return lpcfg_string((const char *)service->szService);
3107 * A useful volume label function.
3109 const char *lpcfg_volume_label(struct loadparm_service *service, struct loadparm_service *sDefault)
3111 const char *ret;
3112 ret = lpcfg_string((const char *)((service != NULL && service->volume != NULL) ?
3113 service->volume : sDefault->volume));
3114 if (!*ret)
3115 return lpcfg_servicename(service);
3116 return ret;
3120 * Return the correct printer name.
3122 const char *lpcfg_printername(struct loadparm_service *service, struct loadparm_service *sDefault)
3124 const char *ret;
3125 ret = lpcfg_string((const char *)((service != NULL && service->_printername != NULL) ?
3126 service->_printername : sDefault->_printername));
3127 if (ret == NULL || (ret != NULL && *ret == '\0'))
3128 ret = lpcfg_servicename(service);
3130 return ret;
3135 * Return the max print jobs per queue.
3137 int lpcfg_maxprintjobs(struct loadparm_service *service, struct loadparm_service *sDefault)
3139 int maxjobs = lpcfg_max_print_jobs(service, sDefault);
3141 if (maxjobs <= 0 || maxjobs >= PRINT_MAX_JOBID)
3142 maxjobs = PRINT_MAX_JOBID - 1;
3144 return maxjobs;
3147 struct smb_iconv_handle *lpcfg_iconv_handle(struct loadparm_context *lp_ctx)
3149 if (lp_ctx == NULL) {
3150 return get_iconv_handle();
3152 return lp_ctx->iconv_handle;
3155 _PUBLIC_ void reload_charcnv(struct loadparm_context *lp_ctx)
3157 struct smb_iconv_handle *old_ic = lp_ctx->iconv_handle;
3158 if (!lp_ctx->global) {
3159 return;
3162 if (old_ic == NULL) {
3163 old_ic = global_iconv_handle;
3165 lp_ctx->iconv_handle = smb_iconv_handle_reinit_lp(lp_ctx, lp_ctx, old_ic);
3166 global_iconv_handle = lp_ctx->iconv_handle;
3169 _PUBLIC_ char *lpcfg_tls_keyfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3171 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_keyfile(lp_ctx));
3174 _PUBLIC_ char *lpcfg_tls_certfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3176 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_certfile(lp_ctx));
3179 _PUBLIC_ char *lpcfg_tls_cafile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3181 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_cafile(lp_ctx));
3184 _PUBLIC_ char *lpcfg_tls_crlfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3186 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_crlfile(lp_ctx));
3189 _PUBLIC_ char *lpcfg_tls_dhpfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3191 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_dhpfile(lp_ctx));
3194 struct gensec_settings *lpcfg_gensec_settings(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3196 struct gensec_settings *settings = talloc_zero(mem_ctx, struct gensec_settings);
3197 if (settings == NULL)
3198 return NULL;
3199 SMB_ASSERT(lp_ctx != NULL);
3200 settings->lp_ctx = talloc_reference(settings, lp_ctx);
3201 settings->target_hostname = lpcfg_parm_string(lp_ctx, NULL, "gensec", "target_hostname");
3202 return settings;
3205 int lpcfg_server_role(struct loadparm_context *lp_ctx)
3207 int domain_master = lpcfg__domain_master(lp_ctx);
3209 return lp_find_server_role(lpcfg__server_role(lp_ctx),
3210 lpcfg__security(lp_ctx),
3211 lpcfg__domain_logons(lp_ctx),
3212 (domain_master == true) ||
3213 (domain_master == Auto));
3216 int lpcfg_security(struct loadparm_context *lp_ctx)
3218 return lp_find_security(lpcfg__server_role(lp_ctx),
3219 lpcfg__security(lp_ctx));
3222 int lpcfg_client_max_protocol(struct loadparm_context *lp_ctx)
3224 int client_max_protocol = lpcfg__client_max_protocol(lp_ctx);
3225 if (client_max_protocol == PROTOCOL_DEFAULT) {
3226 return PROTOCOL_NT1;
3228 return client_max_protocol;
3231 bool lpcfg_server_signing_allowed(struct loadparm_context *lp_ctx, bool *mandatory)
3233 bool allowed = true;
3234 enum smb_signing_setting signing_setting = lpcfg_server_signing(lp_ctx);
3236 *mandatory = false;
3238 if (signing_setting == SMB_SIGNING_DEFAULT) {
3240 * If we are a domain controller, SMB signing is
3241 * really important, as it can prevent a number of
3242 * attacks on communications between us and the
3243 * clients
3245 * However, it really sucks (no sendfile, CPU
3246 * overhead) performance-wise when used on a
3247 * file server, so disable it by default
3248 * on non-DCs
3251 if (lpcfg_server_role(lp_ctx) >= ROLE_ACTIVE_DIRECTORY_DC) {
3252 signing_setting = SMB_SIGNING_REQUIRED;
3253 } else {
3254 signing_setting = SMB_SIGNING_OFF;
3258 switch (signing_setting) {
3259 case SMB_SIGNING_REQUIRED:
3260 *mandatory = true;
3261 break;
3262 case SMB_SIGNING_DESIRED:
3263 case SMB_SIGNING_IF_REQUIRED:
3264 break;
3265 case SMB_SIGNING_DEFAULT:
3266 case SMB_SIGNING_OFF:
3267 allowed = false;
3268 break;
3271 return allowed;
3274 int lpcfg_tdb_hash_size(struct loadparm_context *lp_ctx, const char *name)
3276 const char *base;
3278 if (name == NULL) {
3279 return 0;
3282 base = strrchr_m(name, '/');
3283 if (base != NULL) {
3284 base += 1;
3285 } else {
3286 base = name;
3288 return lpcfg_parm_int(lp_ctx, NULL, "tdb_hashsize", base, 0);
3292 int lpcfg_tdb_flags(struct loadparm_context *lp_ctx, int tdb_flags)
3294 if (!lpcfg_use_mmap(lp_ctx)) {
3295 tdb_flags |= TDB_NOMMAP;
3297 return tdb_flags;