CVE-2015-5370: s3:librpc/rpc: make use of auth->auth_context_id in dcerpc_add_auth_fo...
[Samba.git] / lib / param / loadparm.c
blobdac0f00ca52dc0fcecbcf49cf48c2de3c9bf8e1d
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 #define standard_sub_basic talloc_strdup
74 #include "lib/param/param_global.h"
76 struct loadparm_service *lpcfg_default_service(struct loadparm_context *lp_ctx)
78 return lp_ctx->sDefault;
81 /**
82 * Convenience routine to grab string parameters into temporary memory
83 * and run standard_sub_basic on them.
85 * The buffers can be written to by
86 * callers without affecting the source string.
89 static const char *lpcfg_string(const char *s)
91 #if 0 /* until REWRITE done to make thread-safe */
92 size_t len = s ? strlen(s) : 0;
93 char *ret;
94 #endif
96 /* The follow debug is useful for tracking down memory problems
97 especially if you have an inner loop that is calling a lp_*()
98 function that returns a string. Perhaps this debug should be
99 present all the time? */
101 #if 0
102 DEBUG(10, ("lpcfg_string(%s)\n", s));
103 #endif
105 #if 0 /* until REWRITE done to make thread-safe */
106 if (!lp_talloc)
107 lp_talloc = talloc_init("lp_talloc");
109 ret = talloc_array(lp_talloc, char, len + 100); /* leave room for substitution */
111 if (!ret)
112 return NULL;
114 if (!s)
115 *ret = 0;
116 else
117 strlcpy(ret, s, len);
119 if (trim_string(ret, "\"", "\"")) {
120 if (strchr(ret,'"') != NULL)
121 strlcpy(ret, s, len);
124 standard_sub_basic(ret,len+100);
125 return (ret);
126 #endif
127 return s;
131 In this section all the functions that are used to access the
132 parameters from the rest of the program are defined
136 * the creation of separate lpcfg_*() and lp_*() functions is to allow
137 * for code compatibility between existing Samba4 and Samba3 code.
140 /* this global context supports the lp_*() function varients */
141 static struct loadparm_context *global_loadparm_context;
143 #define FN_GLOBAL_STRING(fn_name,var_name) \
144 _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx, TALLOC_CTX *ctx) {\
145 if (lp_ctx == NULL) return NULL; \
146 if (lp_ctx->s3_fns) { \
147 return lp_ctx->globals->var_name ? lp_ctx->s3_fns->lp_string(ctx, lp_ctx->globals->var_name) : talloc_strdup(ctx, ""); \
149 return lp_ctx->globals->var_name ? talloc_strdup(ctx, lpcfg_string(lp_ctx->globals->var_name)) : talloc_strdup(ctx, ""); \
152 #define FN_GLOBAL_CONST_STRING(fn_name,var_name) \
153 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
154 if (lp_ctx == NULL) return NULL; \
155 return lp_ctx->globals->var_name ? lpcfg_string(lp_ctx->globals->var_name) : ""; \
158 #define FN_GLOBAL_LIST(fn_name,var_name) \
159 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
160 if (lp_ctx == NULL) return NULL; \
161 return lp_ctx->globals->var_name; \
164 #define FN_GLOBAL_BOOL(fn_name,var_name) \
165 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) {\
166 if (lp_ctx == NULL) return false; \
167 return lp_ctx->globals->var_name; \
170 #define FN_GLOBAL_INTEGER(fn_name,var_name) \
171 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
172 return lp_ctx->globals->var_name; \
175 /* Local parameters don't need the ->s3_fns because the struct
176 * loadparm_service is shared and lpcfg_service() checks the ->s3_fns
177 * hook */
178 #define FN_LOCAL_STRING(fn_name,val) \
179 _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_service *service, \
180 struct loadparm_service *sDefault, TALLOC_CTX *ctx) { \
181 return(talloc_strdup(ctx, lpcfg_string((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)))); \
184 #define FN_LOCAL_CONST_STRING(fn_name,val) \
185 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_service *service, \
186 struct loadparm_service *sDefault) { \
187 return((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)); \
190 #define FN_LOCAL_LIST(fn_name,val) \
191 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_service *service, \
192 struct loadparm_service *sDefault) {\
193 return(const char **)(service != NULL && service->val != NULL? service->val : sDefault->val); \
196 #define FN_LOCAL_PARM_BOOL(fn_name, val) FN_LOCAL_BOOL(fn_name, val)
198 #define FN_LOCAL_BOOL(fn_name,val) \
199 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_service *service, \
200 struct loadparm_service *sDefault) { \
201 return((service != NULL)? service->val : sDefault->val); \
204 #define FN_LOCAL_INTEGER(fn_name,val) \
205 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_service *service, \
206 struct loadparm_service *sDefault) { \
207 return((service != NULL)? service->val : sDefault->val); \
210 #define FN_LOCAL_PARM_INTEGER(fn_name, val) FN_LOCAL_INTEGER(fn_name, val)
212 #define FN_LOCAL_PARM_CHAR(fn_name,val) \
213 _PUBLIC_ char lpcfg_ ## fn_name(struct loadparm_service *service, \
214 struct loadparm_service *sDefault) { \
215 return((service != NULL)? service->val : sDefault->val); \
218 #include "lib/param/param_functions.c"
220 /* These functions cannot be auto-generated */
221 FN_LOCAL_BOOL(autoloaded, autoloaded)
222 FN_GLOBAL_CONST_STRING(dnsdomain, dnsdomain)
224 /* local prototypes */
225 static struct loadparm_service *lpcfg_getservicebyname(struct loadparm_context *lp_ctx,
226 const char *pszServiceName);
227 static bool do_section(const char *pszSectionName, void *);
228 static bool set_variable_helper(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
229 const char *pszParmName, const char *pszParmValue);
230 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
231 struct loadparm_service *service,
232 const char *pszParmName,
233 const char *pszParmValue, int flags);
235 /* The following are helper functions for parametrical options support. */
236 /* It returns a pointer to parametrical option value if it exists or NULL otherwise */
237 /* Actual parametrical functions are quite simple */
238 struct parmlist_entry *get_parametric_helper(struct loadparm_service *service,
239 const char *type, const char *option,
240 struct parmlist_entry *global_opts)
242 size_t type_len = strlen(type);
243 size_t option_len = strlen(option);
244 char param_key[type_len + option_len + 2];
245 struct parmlist_entry *data = NULL;
247 snprintf(param_key, sizeof(param_key), "%s:%s", type, option);
250 * Try to fetch the option from the data.
252 if (service != NULL) {
253 data = service->param_opt;
254 while (data != NULL) {
255 if (strwicmp(data->key, param_key) == 0) {
256 return data;
258 data = data->next;
263 * Fall back to fetching from the globals.
265 data = global_opts;
266 while (data != NULL) {
267 if (strwicmp(data->key, param_key) == 0) {
268 return data;
270 data = data->next;
273 return NULL;
276 const char *lpcfg_get_parametric(struct loadparm_context *lp_ctx,
277 struct loadparm_service *service,
278 const char *type, const char *option)
280 struct parmlist_entry *data;
282 if (lp_ctx == NULL)
283 return NULL;
285 data = get_parametric_helper(service,
286 type, option, lp_ctx->globals->param_opt);
288 if (data == NULL) {
289 return NULL;
290 } else {
291 return data->value;
297 * convenience routine to return int parameters.
299 int lp_int(const char *s)
302 if (!s || !*s) {
303 DEBUG(0,("lp_int(%s): is called with NULL!\n",s));
304 return -1;
307 return strtol(s, NULL, 0);
311 * convenience routine to return unsigned long parameters.
313 unsigned long lp_ulong(const char *s)
316 if (!s || !*s) {
317 DEBUG(0,("lp_ulong(%s): is called with NULL!\n",s));
318 return -1;
321 return strtoul(s, NULL, 0);
325 * convenience routine to return unsigned long parameters.
327 static long lp_long(const char *s)
330 if (!s) {
331 DEBUG(0,("lp_long(%s): is called with NULL!\n",s));
332 return -1;
335 return strtol(s, NULL, 0);
339 * convenience routine to return unsigned long parameters.
341 static double lp_double(const char *s)
344 if (!s) {
345 DEBUG(0,("lp_double(%s): is called with NULL!\n",s));
346 return -1;
349 return strtod(s, NULL);
353 * convenience routine to return boolean parameters.
355 bool lp_bool(const char *s)
357 bool ret = false;
359 if (!s || !*s) {
360 DEBUG(0,("lp_bool(%s): is called with NULL!\n",s));
361 return false;
364 if (!set_boolean(s, &ret)) {
365 DEBUG(0,("lp_bool(%s): value is not boolean!\n",s));
366 return false;
369 return ret;
373 * Return parametric option from a given service. Type is a part of option before ':'
374 * Parametric option has following syntax: 'Type: option = value'
375 * Returned value is allocated in 'lp_talloc' context
378 const char *lpcfg_parm_string(struct loadparm_context *lp_ctx,
379 struct loadparm_service *service, const char *type,
380 const char *option)
382 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
384 if (value)
385 return lpcfg_string(value);
387 return NULL;
391 * Return parametric option from a given service. Type is a part of option before ':'
392 * Parametric option has following syntax: 'Type: option = value'
393 * Returned value is allocated in 'lp_talloc' context
396 const char **lpcfg_parm_string_list(TALLOC_CTX *mem_ctx,
397 struct loadparm_context *lp_ctx,
398 struct loadparm_service *service,
399 const char *type,
400 const char *option, const char *separator)
402 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
404 if (value != NULL) {
405 char **l = str_list_make(mem_ctx, value, separator);
406 return discard_const_p(const char *, l);
409 return NULL;
413 * Return parametric option from a given service. Type is a part of option before ':'
414 * Parametric option has following syntax: 'Type: option = value'
417 int lpcfg_parm_int(struct loadparm_context *lp_ctx,
418 struct loadparm_service *service, const char *type,
419 const char *option, int default_v)
421 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
423 if (value)
424 return lp_int(value);
426 return default_v;
430 * Return parametric option from a given service. Type is a part of
431 * option before ':'.
432 * Parametric option has following syntax: 'Type: option = value'.
435 int lpcfg_parm_bytes(struct loadparm_context *lp_ctx,
436 struct loadparm_service *service, const char *type,
437 const char *option, int default_v)
439 uint64_t bval;
441 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
443 if (value && conv_str_size_error(value, &bval)) {
444 if (bval <= INT_MAX) {
445 return (int)bval;
449 return default_v;
453 * Return parametric option from a given service.
454 * Type is a part of option before ':'
455 * Parametric option has following syntax: 'Type: option = value'
457 unsigned long lpcfg_parm_ulong(struct loadparm_context *lp_ctx,
458 struct loadparm_service *service, const char *type,
459 const char *option, unsigned long default_v)
461 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
463 if (value)
464 return lp_ulong(value);
466 return default_v;
469 long lpcfg_parm_long(struct loadparm_context *lp_ctx,
470 struct loadparm_service *service, const char *type,
471 const char *option, long default_v)
473 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
475 if (value)
476 return lp_long(value);
478 return default_v;
481 double lpcfg_parm_double(struct loadparm_context *lp_ctx,
482 struct loadparm_service *service, const char *type,
483 const char *option, double default_v)
485 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
487 if (value != NULL)
488 return lp_double(value);
490 return default_v;
494 * Return parametric option from a given service. Type is a part of option before ':'
495 * Parametric option has following syntax: 'Type: option = value'
498 bool lpcfg_parm_bool(struct loadparm_context *lp_ctx,
499 struct loadparm_service *service, const char *type,
500 const char *option, bool default_v)
502 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
504 if (value != NULL)
505 return lp_bool(value);
507 return default_v;
511 /* this is used to prevent lots of mallocs of size 1 */
512 static const char lpcfg_string_emtpy[] = "";
515 Free a string value.
517 void lpcfg_string_free(char **s)
519 if (s == NULL) {
520 return;
522 if (*s == lpcfg_string_emtpy) {
523 *s = NULL;
524 return;
526 TALLOC_FREE(*s);
530 * Set a string value, deallocating any existing space, and allocing the space
531 * for the string
533 bool lpcfg_string_set(TALLOC_CTX *mem_ctx, char **dest, const char *src)
535 lpcfg_string_free(dest);
537 if ((src == NULL) || (*src == '\0')) {
538 *dest = discard_const_p(char, lpcfg_string_emtpy);
539 return true;
542 *dest = talloc_strdup(mem_ctx, src);
543 if ((*dest) == NULL) {
544 DEBUG(0,("Out of memory in string_set\n"));
545 return false;
548 return true;
552 * Set a string value, deallocating any existing space, and allocing the space
553 * for the string
555 bool lpcfg_string_set_upper(TALLOC_CTX *mem_ctx, char **dest, const char *src)
557 lpcfg_string_free(dest);
559 if ((src == NULL) || (*src == '\0')) {
560 *dest = discard_const_p(char, lpcfg_string_emtpy);
561 return true;
564 *dest = strupper_talloc(mem_ctx, src);
565 if ((*dest) == NULL) {
566 DEBUG(0,("Out of memory in string_set_upper\n"));
567 return false;
570 return true;
576 * Add a new service to the services array initialising it with the given
577 * service.
580 struct loadparm_service *lpcfg_add_service(struct loadparm_context *lp_ctx,
581 const struct loadparm_service *pservice,
582 const char *name)
584 int i;
585 int num_to_alloc = lp_ctx->iNumServices + 1;
586 struct parmlist_entry *data, *pdata;
588 if (lp_ctx->s3_fns != NULL) {
589 smb_panic("Add a service should not be called on an s3 loadparm ctx");
592 if (pservice == NULL) {
593 pservice = lp_ctx->sDefault;
596 /* it might already exist */
597 if (name) {
598 struct loadparm_service *service = lpcfg_getservicebyname(lp_ctx,
599 name);
600 if (service != NULL) {
601 /* Clean all parametric options for service */
602 /* They will be added during parsing again */
603 data = service->param_opt;
604 while (data) {
605 pdata = data->next;
606 talloc_free(data);
607 data = pdata;
609 service->param_opt = NULL;
610 return service;
614 /* find an invalid one */
615 for (i = 0; i < lp_ctx->iNumServices; i++)
616 if (lp_ctx->services[i] == NULL)
617 break;
619 /* if not, then create one */
620 if (i == lp_ctx->iNumServices) {
621 struct loadparm_service **tsp;
623 tsp = talloc_realloc(lp_ctx, lp_ctx->services, struct loadparm_service *, num_to_alloc);
625 if (!tsp) {
626 DEBUG(0,("lpcfg_add_service: failed to enlarge services!\n"));
627 return NULL;
628 } else {
629 lp_ctx->services = tsp;
630 lp_ctx->services[lp_ctx->iNumServices] = NULL;
633 lp_ctx->iNumServices++;
636 lp_ctx->services[i] = talloc_zero(lp_ctx->services, struct loadparm_service);
637 if (lp_ctx->services[i] == NULL) {
638 DEBUG(0,("lpcfg_add_service: out of memory!\n"));
639 return NULL;
641 copy_service(lp_ctx->services[i], pservice, NULL);
642 if (name != NULL)
643 lpcfg_string_set(lp_ctx->services[i], &lp_ctx->services[i]->szService, name);
644 return lp_ctx->services[i];
648 * Add a new home service, with the specified home directory, defaults coming
649 * from service ifrom.
652 bool lpcfg_add_home(struct loadparm_context *lp_ctx,
653 const char *pszHomename,
654 struct loadparm_service *default_service,
655 const char *user, const char *pszHomedir)
657 struct loadparm_service *service;
659 service = lpcfg_add_service(lp_ctx, default_service, pszHomename);
661 if (service == NULL)
662 return false;
664 if (!(*(default_service->path))
665 || strequal(default_service->path, lp_ctx->sDefault->path)) {
666 service->path = talloc_strdup(service, pszHomedir);
667 } else {
668 service->path = string_sub_talloc(service, lpcfg_path(default_service, lp_ctx->sDefault, service), "%H", pszHomedir);
671 if (!(*(service->comment))) {
672 service->comment = talloc_asprintf(service, "Home directory of %s", user);
674 service->bAvailable = default_service->bAvailable;
675 service->browseable = default_service->browseable;
677 DEBUG(3, ("adding home's share [%s] for user '%s' at '%s'\n",
678 pszHomename, user, service->path));
680 return true;
684 * Add a new printer service, with defaults coming from service iFrom.
687 bool lpcfg_add_printer(struct loadparm_context *lp_ctx,
688 const char *pszPrintername,
689 struct loadparm_service *default_service)
691 const char *comment = "From Printcap";
692 struct loadparm_service *service;
693 service = lpcfg_add_service(lp_ctx, default_service, pszPrintername);
695 if (service == NULL)
696 return false;
698 /* note that we do NOT default the availability flag to True - */
699 /* we take it from the default service passed. This allows all */
700 /* dynamic printers to be disabled by disabling the [printers] */
701 /* entry (if/when the 'available' keyword is implemented!). */
703 /* the printer name is set to the service name. */
704 lpcfg_string_set(service, &service->_printername, pszPrintername);
705 lpcfg_string_set(service, &service->comment, comment);
706 service->browseable = default_service->browseable;
707 /* Printers cannot be read_only. */
708 service->read_only = false;
709 /* Printer services must be printable. */
710 service->printable = true;
712 DEBUG(3, ("adding printer service %s\n", pszPrintername));
714 return true;
718 * Map a parameter's string representation to something we can use.
719 * Returns False if the parameter string is not recognised, else TRUE.
722 int lpcfg_map_parameter(const char *pszParmName)
724 int iIndex;
726 for (iIndex = 0; parm_table[iIndex].label; iIndex++)
727 if (strwicmp(parm_table[iIndex].label, pszParmName) == 0)
728 return iIndex;
730 /* Warn only if it isn't parametric option */
731 if (strchr(pszParmName, ':') == NULL)
732 DEBUG(0, ("Unknown parameter encountered: \"%s\"\n", pszParmName));
733 /* We do return 'fail' for parametric options as well because they are
734 stored in different storage
736 return -1;
741 return the parameter structure for a parameter
743 struct parm_struct *lpcfg_parm_struct(struct loadparm_context *lp_ctx, const char *name)
745 int num = lpcfg_map_parameter(name);
747 if (num < 0) {
748 return NULL;
751 return &parm_table[num];
755 return the parameter pointer for a parameter
757 void *lpcfg_parm_ptr(struct loadparm_context *lp_ctx,
758 struct loadparm_service *service, struct parm_struct *parm)
760 if (lp_ctx->s3_fns) {
761 return lp_ctx->s3_fns->get_parm_ptr(service, parm);
764 if (service == NULL) {
765 if (parm->p_class == P_LOCAL)
766 return ((char *)lp_ctx->sDefault)+parm->offset;
767 else if (parm->p_class == P_GLOBAL)
768 return ((char *)lp_ctx->globals)+parm->offset;
769 else return NULL;
770 } else {
771 return ((char *)service) + parm->offset;
776 return the parameter pointer for a parameter
778 bool lpcfg_parm_is_cmdline(struct loadparm_context *lp_ctx, const char *name)
780 int parmnum;
782 parmnum = lpcfg_map_parameter(name);
783 if (parmnum == -1) return false;
785 return lp_ctx->flags[parmnum] & FLAG_CMDLINE;
789 * Find a service by name. Otherwise works like get_service.
792 static struct loadparm_service *lpcfg_getservicebyname(struct loadparm_context *lp_ctx,
793 const char *pszServiceName)
795 int iService;
797 if (lp_ctx->s3_fns) {
798 return lp_ctx->s3_fns->get_service(pszServiceName);
801 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--)
802 if (lp_ctx->services[iService] != NULL &&
803 strwicmp(lp_ctx->services[iService]->szService, pszServiceName) == 0) {
804 return lp_ctx->services[iService];
807 return NULL;
811 * Add a parametric option to a parmlist_entry,
812 * replacing old value, if already present.
814 void set_param_opt(TALLOC_CTX *mem_ctx,
815 struct parmlist_entry **opt_list,
816 const char *opt_name,
817 const char *opt_value,
818 unsigned priority)
820 struct parmlist_entry *new_opt, *opt;
821 bool not_added;
823 opt = *opt_list;
824 not_added = true;
826 /* Traverse destination */
827 while (opt) {
828 /* If we already have same option, override it */
829 if (strwicmp(opt->key, opt_name) == 0) {
830 if ((opt->priority & FLAG_CMDLINE) &&
831 !(priority & FLAG_CMDLINE)) {
832 /* it's been marked as not to be
833 overridden */
834 return;
836 TALLOC_FREE(opt->list);
837 lpcfg_string_set(opt, &opt->value, opt_value);
838 opt->priority = priority;
839 not_added = false;
840 break;
842 opt = opt->next;
844 if (not_added) {
845 new_opt = talloc(mem_ctx, struct parmlist_entry);
846 if (new_opt == NULL) {
847 smb_panic("OOM");
849 new_opt->key = NULL;
850 lpcfg_string_set(new_opt, &new_opt->key, opt_name);
851 new_opt->value = NULL;
852 lpcfg_string_set(new_opt, &new_opt->value, opt_value);
854 new_opt->list = NULL;
855 new_opt->priority = priority;
856 DLIST_ADD(*opt_list, new_opt);
861 * Copy a service structure to another.
862 * If pcopymapDest is NULL then copy all fields
865 void copy_service(struct loadparm_service *pserviceDest,
866 const struct loadparm_service *pserviceSource,
867 struct bitmap *pcopymapDest)
869 int i;
870 bool bcopyall = (pcopymapDest == NULL);
871 struct parmlist_entry *data;
873 for (i = 0; parm_table[i].label; i++)
874 if (parm_table[i].p_class == P_LOCAL &&
875 (bcopyall || bitmap_query(pcopymapDest, i))) {
876 const void *src_ptr =
877 ((const char *)pserviceSource) + parm_table[i].offset;
878 void *dest_ptr =
879 ((char *)pserviceDest) + parm_table[i].offset;
881 switch (parm_table[i].type) {
882 case P_BOOL:
883 case P_BOOLREV:
884 *(bool *)dest_ptr = *(const bool *)src_ptr;
885 break;
887 case P_INTEGER:
888 case P_BYTES:
889 case P_OCTAL:
890 case P_ENUM:
891 *(int *)dest_ptr = *(const int *)src_ptr;
892 break;
894 case P_CHAR:
895 *(char *)dest_ptr = *(const char *)src_ptr;
896 break;
898 case P_STRING:
899 lpcfg_string_set(pserviceDest,
900 (char **)dest_ptr,
901 *(const char * const *)src_ptr);
902 break;
904 case P_USTRING:
905 lpcfg_string_set_upper(pserviceDest,
906 (char **)dest_ptr,
907 *(const char * const *)src_ptr);
908 break;
909 case P_CMDLIST:
910 case P_LIST:
911 TALLOC_FREE(*((char ***)dest_ptr));
912 *(char ***)dest_ptr = str_list_copy(pserviceDest,
913 *discard_const_p(const char **, src_ptr));
914 break;
915 default:
916 break;
920 if (bcopyall) {
921 init_copymap(pserviceDest);
922 if (pserviceSource->copymap)
923 bitmap_copy(pserviceDest->copymap,
924 pserviceSource->copymap);
927 for (data = pserviceSource->param_opt; data != NULL; data = data->next) {
928 set_param_opt(pserviceDest, &pserviceDest->param_opt,
929 data->key, data->value, data->priority);
934 * Check a service for consistency. Return False if the service is in any way
935 * incomplete or faulty, else True.
937 bool lpcfg_service_ok(struct loadparm_service *service)
939 bool bRetval;
941 bRetval = true;
942 if (service->szService[0] == '\0') {
943 DEBUG(0, ("The following message indicates an internal error:\n"));
944 DEBUG(0, ("No service name in service entry.\n"));
945 bRetval = false;
948 /* The [printers] entry MUST be printable. I'm all for flexibility, but */
949 /* I can't see why you'd want a non-printable printer service... */
950 if (strwicmp(service->szService, PRINTERS_NAME) == 0) {
951 if (!service->printable) {
952 DEBUG(0, ("WARNING: [%s] service MUST be printable!\n",
953 service->szService));
954 service->printable = true;
956 /* [printers] service must also be non-browsable. */
957 if (service->browseable)
958 service->browseable = false;
961 if (service->path[0] == '\0' &&
962 strwicmp(service->szService, HOMES_NAME) != 0 &&
963 service->msdfs_proxy[0] == '\0')
965 DEBUG(0, ("WARNING: No path in service %s - making it unavailable!\n",
966 service->szService));
967 service->bAvailable = false;
970 if (!service->bAvailable)
971 DEBUG(1, ("NOTE: Service %s is flagged unavailable.\n",
972 service->szService));
974 return bRetval;
978 /*******************************************************************
979 Keep a linked list of all config files so we know when one has changed
980 it's date and needs to be reloaded.
981 ********************************************************************/
983 void add_to_file_list(TALLOC_CTX *mem_ctx, struct file_lists **list,
984 const char *fname, const char *subfname)
986 struct file_lists *f = *list;
988 while (f) {
989 if (f->name && !strcmp(f->name, fname))
990 break;
991 f = f->next;
994 if (!f) {
995 f = talloc(mem_ctx, struct file_lists);
996 if (!f)
997 goto fail;
998 f->next = *list;
999 f->name = talloc_strdup(f, fname);
1000 if (!f->name) {
1001 TALLOC_FREE(f);
1002 goto fail;
1004 f->subfname = talloc_strdup(f, subfname);
1005 if (!f->subfname) {
1006 TALLOC_FREE(f);
1007 goto fail;
1009 *list = f;
1010 f->modtime = file_modtime(subfname);
1011 } else {
1012 time_t t = file_modtime(subfname);
1013 if (t)
1014 f->modtime = t;
1016 return;
1018 fail:
1019 DEBUG(0, ("Unable to add file to file list: %s\n", fname));
1023 /*******************************************************************
1024 Check if a config file has changed date.
1025 ********************************************************************/
1026 bool lpcfg_file_list_changed(struct loadparm_context *lp_ctx)
1028 struct file_lists *f;
1029 DEBUG(6, ("lpcfg_file_list_changed()\n"));
1031 for (f = lp_ctx->file_lists; f != NULL; f = f->next) {
1032 char *n2;
1033 time_t mod_time;
1035 n2 = standard_sub_basic(lp_ctx, f->name);
1037 DEBUGADD(6, ("file %s -> %s last mod_time: %s\n",
1038 f->name, n2, ctime(&f->modtime)));
1040 mod_time = file_modtime(n2);
1042 if (mod_time && ((f->modtime != mod_time) || (f->subfname == NULL) || (strcmp(n2, f->subfname) != 0))) {
1043 DEBUGADD(6, ("file %s modified: %s\n", n2,
1044 ctime(&mod_time)));
1045 f->modtime = mod_time;
1046 talloc_free(f->subfname);
1047 f->subfname = talloc_strdup(f, n2);
1048 TALLOC_FREE(n2);
1049 return true;
1051 TALLOC_FREE(n2);
1053 return false;
1057 * set the value for a P_ENUM
1059 bool lp_set_enum_parm( struct parm_struct *parm, const char *pszParmValue,
1060 int *ptr )
1062 int i;
1064 for (i = 0; parm->enum_list[i].name; i++) {
1065 if (strwicmp(pszParmValue, parm->enum_list[i].name) == 0) {
1066 *ptr = parm->enum_list[i].value;
1067 return true;
1070 DEBUG(0, ("WARNING: Ignoring invalid value '%s' for parameter '%s'\n",
1071 pszParmValue, parm->label));
1072 return false;
1076 /***************************************************************************
1077 Handle the "realm" parameter
1078 ***************************************************************************/
1080 bool handle_realm(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1081 const char *pszParmValue, char **ptr)
1083 char *upper;
1084 char *lower;
1086 upper = strupper_talloc(lp_ctx, pszParmValue);
1087 if (upper == NULL) {
1088 return false;
1091 lower = strlower_talloc(lp_ctx, pszParmValue);
1092 if (lower == NULL) {
1093 TALLOC_FREE(upper);
1094 return false;
1097 lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1098 lpcfg_string_set(lp_ctx->globals->ctx, &lp_ctx->globals->realm, upper);
1099 lpcfg_string_set(lp_ctx->globals->ctx, &lp_ctx->globals->dnsdomain, lower);
1101 return true;
1104 /***************************************************************************
1105 Handle the include operation.
1106 ***************************************************************************/
1108 bool handle_include(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1109 const char *pszParmValue, char **ptr)
1111 char *fname;
1113 if (lp_ctx->s3_fns) {
1114 return lp_ctx->s3_fns->lp_include(lp_ctx, service, pszParmValue, ptr);
1117 fname = standard_sub_basic(lp_ctx, pszParmValue);
1119 add_to_file_list(lp_ctx, &lp_ctx->file_lists, pszParmValue, fname);
1121 lpcfg_string_set(lp_ctx, ptr, fname);
1123 if (file_exist(fname))
1124 return pm_process(fname, do_section, lpcfg_do_parameter, lp_ctx);
1126 DEBUG(2, ("Can't find include file %s\n", fname));
1128 return false;
1131 /***************************************************************************
1132 Handle the interpretation of the copy parameter.
1133 ***************************************************************************/
1135 bool handle_copy(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1136 const char *pszParmValue, char **ptr)
1138 bool bRetval;
1139 struct loadparm_service *serviceTemp = NULL;
1141 bRetval = false;
1143 DEBUG(3, ("Copying service from service %s\n", pszParmValue));
1145 serviceTemp = lpcfg_getservicebyname(lp_ctx, pszParmValue);
1147 if (service == NULL) {
1148 DEBUG(0, ("Unable to copy service - invalid service destination.\n"));
1149 return false;
1152 if (serviceTemp != NULL) {
1153 if (serviceTemp == service) {
1154 DEBUG(0, ("Can't copy service %s - unable to copy self!\n", pszParmValue));
1155 } else {
1156 copy_service(service,
1157 serviceTemp,
1158 service->copymap);
1159 lpcfg_string_set(service, ptr, pszParmValue);
1161 bRetval = true;
1163 } else {
1164 DEBUG(0, ("Unable to copy service - source not found: %s\n",
1165 pszParmValue));
1166 bRetval = false;
1169 return bRetval;
1172 bool handle_debug_list(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1173 const char *pszParmValue, char **ptr)
1175 lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1177 return debug_parse_levels(pszParmValue);
1180 bool handle_logfile(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1181 const char *pszParmValue, char **ptr)
1183 if (lp_ctx->s3_fns == NULL) {
1184 debug_set_logfile(pszParmValue);
1187 lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1189 return true;
1193 * These special charset handling methods only run in the source3 code.
1196 bool handle_charset(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1197 const char *pszParmValue, char **ptr)
1199 if (lp_ctx->s3_fns) {
1200 if (*ptr == NULL || strcmp(*ptr, pszParmValue) != 0) {
1201 global_iconv_handle = smb_iconv_handle_reinit(NULL,
1202 lpcfg_dos_charset(lp_ctx),
1203 lpcfg_unix_charset(lp_ctx),
1204 true, global_iconv_handle);
1208 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1212 bool handle_dos_charset(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1213 const char *pszParmValue, char **ptr)
1215 bool is_utf8 = false;
1216 size_t len = strlen(pszParmValue);
1218 if (lp_ctx->s3_fns) {
1219 if (len == 4 || len == 5) {
1220 /* Don't use StrCaseCmp here as we don't want to
1221 initialize iconv. */
1222 if ((toupper_m(pszParmValue[0]) == 'U') &&
1223 (toupper_m(pszParmValue[1]) == 'T') &&
1224 (toupper_m(pszParmValue[2]) == 'F')) {
1225 if (len == 4) {
1226 if (pszParmValue[3] == '8') {
1227 is_utf8 = true;
1229 } else {
1230 if (pszParmValue[3] == '-' &&
1231 pszParmValue[4] == '8') {
1232 is_utf8 = true;
1238 if (*ptr == NULL || strcmp(*ptr, pszParmValue) != 0) {
1239 if (is_utf8) {
1240 DEBUG(0,("ERROR: invalid DOS charset: 'dos charset' must not "
1241 "be UTF8, using (default value) %s instead.\n",
1242 DEFAULT_DOS_CHARSET));
1243 pszParmValue = DEFAULT_DOS_CHARSET;
1245 global_iconv_handle = smb_iconv_handle_reinit(NULL,
1246 lpcfg_dos_charset(lp_ctx),
1247 lpcfg_unix_charset(lp_ctx),
1248 true, global_iconv_handle);
1252 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1255 bool handle_printing(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1256 const char *pszParmValue, char **ptr)
1258 static int parm_num = -1;
1260 if (parm_num == -1) {
1261 parm_num = lpcfg_map_parameter("printing");
1264 if (!lp_set_enum_parm(&parm_table[parm_num], pszParmValue, (int*)ptr)) {
1265 return false;
1268 if (lp_ctx->s3_fns) {
1269 if (service == NULL) {
1270 init_printer_values(lp_ctx, lp_ctx->globals->ctx, lp_ctx->sDefault);
1271 } else {
1272 init_printer_values(lp_ctx, service, service);
1276 return true;
1279 bool handle_ldap_debug_level(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1280 const char *pszParmValue, char **ptr)
1282 lp_ctx->globals->ldap_debug_level = lp_int(pszParmValue);
1284 if (lp_ctx->s3_fns) {
1285 lp_ctx->s3_fns->init_ldap_debugging();
1287 return true;
1290 bool handle_netbios_aliases(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1291 const char *pszParmValue, char **ptr)
1293 TALLOC_FREE(lp_ctx->globals->netbios_aliases);
1294 lp_ctx->globals->netbios_aliases = str_list_make_v3_const(lp_ctx->globals->ctx,
1295 pszParmValue, NULL);
1297 if (lp_ctx->s3_fns) {
1298 return lp_ctx->s3_fns->set_netbios_aliases(lp_ctx->globals->netbios_aliases);
1300 return true;
1304 * idmap related parameters
1307 bool handle_idmap_backend(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1308 const char *pszParmValue, char **ptr)
1310 if (lp_ctx->s3_fns) {
1311 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : backend",
1312 pszParmValue, 0);
1315 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1318 bool handle_idmap_uid(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1319 const char *pszParmValue, char **ptr)
1321 if (lp_ctx->s3_fns) {
1322 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : range",
1323 pszParmValue, 0);
1326 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1329 bool handle_idmap_gid(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1330 const char *pszParmValue, char **ptr)
1332 if (lp_ctx->s3_fns) {
1333 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : range",
1334 pszParmValue, 0);
1337 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1340 bool handle_smb_ports(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1341 const char *pszParmValue, char **ptr)
1343 static int parm_num = -1;
1344 int i;
1345 const char **list;
1347 if (!pszParmValue || !*pszParmValue) {
1348 return false;
1351 if (parm_num == -1) {
1352 parm_num = lpcfg_map_parameter("smb ports");
1353 if (parm_num == -1) {
1354 return false;
1358 if(!set_variable_helper(lp_ctx->globals->ctx, parm_num, ptr, "smb ports",
1359 pszParmValue)) {
1360 return false;
1363 list = lp_ctx->globals->smb_ports;
1364 if (list == NULL) {
1365 return false;
1368 /* Check that each port is a valid integer and within range */
1369 for (i = 0; list[i] != NULL; i++) {
1370 char *end = NULL;
1371 int port = 0;
1372 port = strtol(list[i], &end, 10);
1373 if (*end != '\0' || port <= 0 || port > 65535) {
1374 TALLOC_FREE(list);
1375 return false;
1379 return true;
1382 /***************************************************************************
1383 Initialise a copymap.
1384 ***************************************************************************/
1387 * Initializes service copymap
1388 * Note: pservice *must* be valid TALLOC_CTX
1390 void init_copymap(struct loadparm_service *pservice)
1392 int i;
1394 TALLOC_FREE(pservice->copymap);
1396 pservice->copymap = bitmap_talloc(pservice, num_parameters());
1397 if (!pservice->copymap) {
1398 DEBUG(0,
1399 ("Couldn't allocate copymap!! (size %d)\n",
1400 (int)num_parameters()));
1401 } else {
1402 for (i = 0; i < num_parameters(); i++) {
1403 bitmap_set(pservice->copymap, i);
1409 * Process a parametric option
1411 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
1412 struct loadparm_service *service,
1413 const char *pszParmName,
1414 const char *pszParmValue, int flags)
1416 struct parmlist_entry **data;
1417 char *name;
1418 TALLOC_CTX *mem_ctx;
1420 while (isspace((unsigned char)*pszParmName)) {
1421 pszParmName++;
1424 name = strlower_talloc(lp_ctx, pszParmName);
1425 if (!name) return false;
1427 if (service == NULL) {
1428 data = &lp_ctx->globals->param_opt;
1430 * s3 code cannot deal with parametric options stored on the globals ctx.
1432 if (lp_ctx->s3_fns != NULL) {
1433 mem_ctx = NULL;
1434 } else {
1435 mem_ctx = lp_ctx->globals->ctx;
1437 } else {
1438 data = &service->param_opt;
1439 mem_ctx = service;
1442 set_param_opt(mem_ctx, data, name, pszParmValue, flags);
1444 talloc_free(name);
1446 return true;
1449 static bool set_variable_helper(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
1450 const char *pszParmName, const char *pszParmValue)
1452 int i;
1454 /* switch on the type of variable it is */
1455 switch (parm_table[parmnum].type)
1457 case P_BOOL: {
1458 bool b;
1459 if (!set_boolean(pszParmValue, &b)) {
1460 DEBUG(0, ("set_variable_helper(%s): value is not "
1461 "boolean!\n", pszParmValue));
1462 return false;
1464 *(bool *)parm_ptr = b;
1466 break;
1468 case P_BOOLREV: {
1469 bool b;
1470 if (!set_boolean(pszParmValue, &b)) {
1471 DEBUG(0, ("set_variable_helper(%s): value is not "
1472 "boolean!\n", pszParmValue));
1473 return false;
1475 *(bool *)parm_ptr = !b;
1477 break;
1479 case P_INTEGER:
1480 *(int *)parm_ptr = lp_int(pszParmValue);
1481 break;
1483 case P_CHAR:
1484 *(char *)parm_ptr = *pszParmValue;
1485 break;
1487 case P_OCTAL:
1488 i = sscanf(pszParmValue, "%o", (int *)parm_ptr);
1489 if ( i != 1 ) {
1490 DEBUG ( 0, ("Invalid octal number %s\n", pszParmName ));
1491 return false;
1493 break;
1495 case P_BYTES:
1497 uint64_t val;
1498 if (conv_str_size_error(pszParmValue, &val)) {
1499 if (val <= INT_MAX) {
1500 *(int *)parm_ptr = (int)val;
1501 break;
1505 DEBUG(0, ("set_variable_helper(%s): value is not "
1506 "a valid size specifier!\n", pszParmValue));
1507 return false;
1510 case P_CMDLIST:
1511 TALLOC_FREE(*(char ***)parm_ptr);
1512 *(char ***)parm_ptr = str_list_make_v3(mem_ctx,
1513 pszParmValue, NULL);
1514 break;
1516 case P_LIST:
1518 char **new_list = str_list_make_v3(mem_ctx,
1519 pszParmValue, NULL);
1520 if (new_list == NULL) {
1521 break;
1524 for (i=0; new_list[i]; i++) {
1525 if (*(const char ***)parm_ptr != NULL &&
1526 new_list[i][0] == '+' &&
1527 new_list[i][1])
1529 if (!str_list_check(*(const char ***)parm_ptr,
1530 &new_list[i][1])) {
1531 *(const char ***)parm_ptr = str_list_add(*(const char ***)parm_ptr,
1532 &new_list[i][1]);
1534 } else if (*(const char ***)parm_ptr != NULL &&
1535 new_list[i][0] == '-' &&
1536 new_list[i][1])
1538 str_list_remove(*(const char ***)parm_ptr,
1539 &new_list[i][1]);
1540 } else {
1541 if (i != 0) {
1542 DEBUG(0, ("Unsupported list syntax for: %s = %s\n",
1543 pszParmName, pszParmValue));
1544 return false;
1546 *(char ***)parm_ptr = new_list;
1547 break;
1550 break;
1553 case P_STRING:
1554 lpcfg_string_set(mem_ctx, (char **)parm_ptr, pszParmValue);
1555 break;
1557 case P_USTRING:
1558 lpcfg_string_set_upper(mem_ctx, (char **)parm_ptr, pszParmValue);
1559 break;
1561 case P_ENUM:
1562 if (!lp_set_enum_parm(&parm_table[parmnum], pszParmValue, (int*)parm_ptr)) {
1563 return false;
1565 break;
1569 return true;
1573 bool set_variable(TALLOC_CTX *mem_ctx, struct loadparm_service *service, int parmnum, void *parm_ptr,
1574 const char *pszParmName, const char *pszParmValue,
1575 struct loadparm_context *lp_ctx, bool on_globals)
1577 int i;
1578 bool ok;
1580 /* if it is a special case then go ahead */
1581 if (parm_table[parmnum].special) {
1582 ok = parm_table[parmnum].special(lp_ctx, service, pszParmValue,
1583 (char **)parm_ptr);
1584 } else {
1585 ok = set_variable_helper(mem_ctx, parmnum, parm_ptr,
1586 pszParmName, pszParmValue);
1589 if (!ok) {
1590 return false;
1593 if (on_globals && (lp_ctx->flags[parmnum] & FLAG_DEFAULT)) {
1594 lp_ctx->flags[parmnum] &= ~FLAG_DEFAULT;
1595 /* we have to also unset FLAG_DEFAULT on aliases */
1596 for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
1597 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1599 for (i=parmnum+1;i<num_parameters() && parm_table[i].offset == parm_table[parmnum].offset;i++) {
1600 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1603 return true;
1607 bool lpcfg_do_global_parameter(struct loadparm_context *lp_ctx,
1608 const char *pszParmName, const char *pszParmValue)
1610 int parmnum = lpcfg_map_parameter(pszParmName);
1611 void *parm_ptr;
1613 if (parmnum < 0) {
1614 if (strchr(pszParmName, ':')) {
1615 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName, pszParmValue, 0);
1617 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1618 return true;
1621 /* if the flag has been set on the command line, then don't allow override,
1622 but don't report an error */
1623 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1624 return true;
1627 if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
1628 DEBUG(1, ("WARNING: The \"%s\" option is deprecated\n",
1629 pszParmName));
1632 parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[parmnum]);
1634 return set_variable(lp_ctx->globals->ctx, NULL, parmnum, parm_ptr,
1635 pszParmName, pszParmValue, lp_ctx, true);
1638 bool lpcfg_do_service_parameter(struct loadparm_context *lp_ctx,
1639 struct loadparm_service *service,
1640 const char *pszParmName, const char *pszParmValue)
1642 void *parm_ptr;
1643 int i;
1644 int parmnum = lpcfg_map_parameter(pszParmName);
1646 if (parmnum < 0) {
1647 if (strchr(pszParmName, ':')) {
1648 return lp_do_parameter_parametric(lp_ctx, service, pszParmName, pszParmValue, 0);
1650 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1651 return true;
1654 /* if the flag has been set on the command line, then don't allow override,
1655 but don't report an error */
1656 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1657 return true;
1660 if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
1661 DEBUG(1, ("WARNING: The \"%s\" option is deprecated\n",
1662 pszParmName));
1665 if (parm_table[parmnum].p_class == P_GLOBAL) {
1666 DEBUG(0,
1667 ("Global parameter %s found in service section!\n",
1668 pszParmName));
1669 return true;
1671 parm_ptr = ((char *)service) + parm_table[parmnum].offset;
1673 if (!service->copymap)
1674 init_copymap(service);
1676 /* this handles the aliases - set the copymap for other
1677 * entries with the same data pointer */
1678 for (i = 0; parm_table[i].label; i++)
1679 if (parm_table[i].offset == parm_table[parmnum].offset &&
1680 parm_table[i].p_class == parm_table[parmnum].p_class)
1681 bitmap_clear(service->copymap, i);
1683 return set_variable(service, service, parmnum, parm_ptr, pszParmName,
1684 pszParmValue, lp_ctx, false);
1688 * Process a parameter.
1691 bool lpcfg_do_parameter(const char *pszParmName, const char *pszParmValue,
1692 void *userdata)
1694 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1696 if (lp_ctx->bInGlobalSection)
1697 return lpcfg_do_global_parameter(lp_ctx, pszParmName,
1698 pszParmValue);
1699 else
1700 return lpcfg_do_service_parameter(lp_ctx, lp_ctx->currentService,
1701 pszParmName, pszParmValue);
1705 variable argument do parameter
1707 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx, const char *pszParmName, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
1708 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx,
1709 const char *pszParmName, const char *fmt, ...)
1711 char *s;
1712 bool ret;
1713 va_list ap;
1715 va_start(ap, fmt);
1716 s = talloc_vasprintf(NULL, fmt, ap);
1717 va_end(ap);
1718 ret = lpcfg_do_global_parameter(lp_ctx, pszParmName, s);
1719 talloc_free(s);
1720 return ret;
1725 set a parameter from the commandline - this is called from command line parameter
1726 parsing code. It sets the parameter then marks the parameter as unable to be modified
1727 by smb.conf processing
1729 bool lpcfg_set_cmdline(struct loadparm_context *lp_ctx, const char *pszParmName,
1730 const char *pszParmValue)
1732 int parmnum;
1733 int i;
1735 while (isspace((unsigned char)*pszParmValue)) pszParmValue++;
1737 parmnum = lpcfg_map_parameter(pszParmName);
1739 if (parmnum < 0 && strchr(pszParmName, ':')) {
1740 /* set a parametric option */
1741 bool ok;
1742 ok = lp_do_parameter_parametric(lp_ctx, NULL, pszParmName,
1743 pszParmValue, FLAG_CMDLINE);
1744 if (lp_ctx->s3_fns != NULL) {
1745 if (ok) {
1746 lp_ctx->s3_fns->store_cmdline(pszParmName, pszParmValue);
1749 return ok;
1752 if (parmnum < 0) {
1753 DEBUG(0,("Unknown option '%s'\n", pszParmName));
1754 return false;
1757 /* reset the CMDLINE flag in case this has been called before */
1758 lp_ctx->flags[parmnum] &= ~FLAG_CMDLINE;
1760 if (!lpcfg_do_global_parameter(lp_ctx, pszParmName, pszParmValue)) {
1761 return false;
1764 lp_ctx->flags[parmnum] |= FLAG_CMDLINE;
1766 /* we have to also set FLAG_CMDLINE on aliases */
1767 for (i=parmnum-1;
1768 i>=0 && parm_table[i].p_class == parm_table[parmnum].p_class &&
1769 parm_table[i].offset == parm_table[parmnum].offset;
1770 i--) {
1771 lp_ctx->flags[i] |= FLAG_CMDLINE;
1773 for (i=parmnum+1;
1774 i<num_parameters() &&
1775 parm_table[i].p_class == parm_table[parmnum].p_class &&
1776 parm_table[i].offset == parm_table[parmnum].offset;
1777 i++) {
1778 lp_ctx->flags[i] |= FLAG_CMDLINE;
1781 if (lp_ctx->s3_fns != NULL) {
1782 lp_ctx->s3_fns->store_cmdline(pszParmName, pszParmValue);
1785 return true;
1789 set a option from the commandline in 'a=b' format. Use to support --option
1791 bool lpcfg_set_option(struct loadparm_context *lp_ctx, const char *option)
1793 char *p, *s;
1794 bool ret;
1796 s = talloc_strdup(NULL, option);
1797 if (!s) {
1798 return false;
1801 p = strchr(s, '=');
1802 if (!p) {
1803 talloc_free(s);
1804 return false;
1807 *p = 0;
1809 ret = lpcfg_set_cmdline(lp_ctx, s, p+1);
1810 talloc_free(s);
1811 return ret;
1815 #define BOOLSTR(b) ((b) ? "Yes" : "No")
1818 * Print a parameter of the specified type.
1821 void lpcfg_print_parameter(struct parm_struct *p, void *ptr, FILE * f)
1823 /* For the seperation of lists values that we print below */
1824 const char *list_sep = ", ";
1825 int i;
1826 switch (p->type)
1828 case P_ENUM:
1829 for (i = 0; p->enum_list[i].name; i++) {
1830 if (*(int *)ptr == p->enum_list[i].value) {
1831 fprintf(f, "%s",
1832 p->enum_list[i].name);
1833 break;
1836 break;
1838 case P_BOOL:
1839 fprintf(f, "%s", BOOLSTR(*(bool *)ptr));
1840 break;
1842 case P_BOOLREV:
1843 fprintf(f, "%s", BOOLSTR(!*(bool *)ptr));
1844 break;
1846 case P_INTEGER:
1847 case P_BYTES:
1848 fprintf(f, "%d", *(int *)ptr);
1849 break;
1851 case P_CHAR:
1852 fprintf(f, "%c", *(char *)ptr);
1853 break;
1855 case P_OCTAL: {
1856 int val = *(int *)ptr;
1857 if (val == -1) {
1858 fprintf(f, "-1");
1859 } else {
1860 fprintf(f, "0%03o", val);
1862 break;
1865 case P_CMDLIST:
1866 list_sep = " ";
1867 /* fall through */
1868 case P_LIST:
1869 if ((char ***)ptr && *(char ***)ptr) {
1870 char **list = *(char ***)ptr;
1871 for (; *list; list++) {
1872 /* surround strings with whitespace in double quotes */
1873 if (*(list+1) == NULL) {
1874 /* last item, no extra separator */
1875 list_sep = "";
1877 if ( strchr_m( *list, ' ' ) ) {
1878 fprintf(f, "\"%s\"%s", *list, list_sep);
1879 } else {
1880 fprintf(f, "%s%s", *list, list_sep);
1884 break;
1886 case P_STRING:
1887 case P_USTRING:
1888 if (*(char **)ptr) {
1889 fprintf(f, "%s", *(char **)ptr);
1891 break;
1896 * Check if two parameters are equal.
1899 static bool lpcfg_equal_parameter(parm_type type, void *ptr1, void *ptr2)
1901 switch (type) {
1902 case P_BOOL:
1903 case P_BOOLREV:
1904 return (*((bool *)ptr1) == *((bool *)ptr2));
1906 case P_INTEGER:
1907 case P_ENUM:
1908 case P_OCTAL:
1909 case P_BYTES:
1910 return (*((int *)ptr1) == *((int *)ptr2));
1912 case P_CHAR:
1913 return (*((char *)ptr1) == *((char *)ptr2));
1915 case P_LIST:
1916 case P_CMDLIST:
1917 return str_list_equal(*(const char ***)ptr1, *(const char ***)ptr2);
1919 case P_STRING:
1920 case P_USTRING:
1922 char *p1 = *(char **)ptr1, *p2 = *(char **)ptr2;
1923 if (p1 && !*p1)
1924 p1 = NULL;
1925 if (p2 && !*p2)
1926 p2 = NULL;
1927 return (p1 == p2 || strequal(p1, p2));
1930 return false;
1934 * Process a new section (service).
1936 * At this stage all sections are services.
1937 * Later we'll have special sections that permit server parameters to be set.
1938 * Returns True on success, False on failure.
1941 static bool do_section(const char *pszSectionName, void *userdata)
1943 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1944 bool bRetval;
1945 bool isglobal;
1947 if (lp_ctx->s3_fns != NULL) {
1948 return lp_ctx->s3_fns->do_section(pszSectionName, lp_ctx);
1951 isglobal = ((strwicmp(pszSectionName, GLOBAL_NAME) == 0) ||
1952 (strwicmp(pszSectionName, GLOBAL_NAME2) == 0));
1954 bRetval = false;
1956 /* if we've just struck a global section, note the fact. */
1957 lp_ctx->bInGlobalSection = isglobal;
1959 /* check for multiple global sections */
1960 if (lp_ctx->bInGlobalSection) {
1961 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1962 return true;
1965 /* if we have a current service, tidy it up before moving on */
1966 bRetval = true;
1968 if (lp_ctx->currentService != NULL)
1969 bRetval = lpcfg_service_ok(lp_ctx->currentService);
1971 /* if all is still well, move to the next record in the services array */
1972 if (bRetval) {
1973 /* We put this here to avoid an odd message order if messages are */
1974 /* issued by the post-processing of a previous section. */
1975 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1977 if ((lp_ctx->currentService = lpcfg_add_service(lp_ctx, lp_ctx->sDefault,
1978 pszSectionName))
1979 == NULL) {
1980 DEBUG(0, ("Failed to add a new service\n"));
1981 return false;
1985 return bRetval;
1990 * Determine if a particular base parameter is currently set to the default value.
1993 static bool is_default(void *base_structure, int i)
1995 void *def_ptr = ((char *)base_structure) + parm_table[i].offset;
1996 switch (parm_table[i].type) {
1997 case P_CMDLIST:
1998 case P_LIST:
1999 return str_list_equal((const char * const *)parm_table[i].def.lvalue,
2000 *(const char * const **)def_ptr);
2001 case P_STRING:
2002 case P_USTRING:
2003 return strequal(parm_table[i].def.svalue,
2004 *(char **)def_ptr);
2005 case P_BOOL:
2006 case P_BOOLREV:
2007 return parm_table[i].def.bvalue ==
2008 *(bool *)def_ptr;
2009 case P_INTEGER:
2010 case P_CHAR:
2011 case P_OCTAL:
2012 case P_BYTES:
2013 case P_ENUM:
2014 return parm_table[i].def.ivalue ==
2015 *(int *)def_ptr;
2017 return false;
2021 *Display the contents of the global structure.
2024 void lpcfg_dump_globals(struct loadparm_context *lp_ctx, FILE *f,
2025 bool show_defaults)
2027 int i;
2028 struct parmlist_entry *data;
2030 fprintf(f, "# Global parameters\n[global]\n");
2032 for (i = 0; parm_table[i].label; i++) {
2033 if (parm_table[i].p_class != P_GLOBAL) {
2034 continue;
2037 if (parm_table[i].flags & FLAG_SYNONYM) {
2038 continue;
2041 if (!show_defaults) {
2042 if (lp_ctx->flags && (lp_ctx->flags[i] & FLAG_DEFAULT)) {
2043 continue;
2046 if (is_default(lp_ctx->globals, i)) {
2047 continue;
2051 fprintf(f, "\t%s = ", parm_table[i].label);
2052 lpcfg_print_parameter(&parm_table[i], lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[i]), f);
2053 fprintf(f, "\n");
2055 if (lp_ctx->globals->param_opt != NULL) {
2056 for (data = lp_ctx->globals->param_opt; data;
2057 data = data->next) {
2058 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
2059 continue;
2061 fprintf(f, "\t%s = %s\n", data->key, data->value);
2068 * Display the contents of a single services record.
2071 void lpcfg_dump_a_service(struct loadparm_service * pService, struct loadparm_service *sDefault, FILE * f,
2072 unsigned int *flags, bool show_defaults)
2074 int i;
2075 struct parmlist_entry *data;
2077 if (pService != sDefault)
2078 fprintf(f, "\n[%s]\n", pService->szService);
2080 for (i = 0; parm_table[i].label; i++) {
2081 if (parm_table[i].p_class != P_LOCAL) {
2082 continue;
2085 if (parm_table[i].flags & FLAG_SYNONYM) {
2086 continue;
2089 if (*parm_table[i].label == '-') {
2090 continue;
2093 if (pService == sDefault) {
2094 if (!show_defaults) {
2095 if (flags && (flags[i] & FLAG_DEFAULT)) {
2096 continue;
2099 if (is_default(sDefault, i)) {
2100 continue;
2103 } else {
2104 bool equal;
2106 equal = lpcfg_equal_parameter(parm_table[i].type,
2107 ((char *)pService) +
2108 parm_table[i].offset,
2109 ((char *)sDefault) +
2110 parm_table[i].offset);
2111 if (equal) {
2112 continue;
2116 fprintf(f, "\t%s = ", parm_table[i].label);
2117 lpcfg_print_parameter(&parm_table[i],
2118 ((char *)pService) + parm_table[i].offset, f);
2119 fprintf(f, "\n");
2121 if (pService->param_opt != NULL) {
2122 for (data = pService->param_opt; data; data = data->next) {
2123 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
2124 continue;
2126 fprintf(f, "\t%s = %s\n", data->key, data->value);
2131 bool lpcfg_dump_a_parameter(struct loadparm_context *lp_ctx,
2132 struct loadparm_service *service,
2133 const char *parm_name, FILE * f)
2135 struct parm_struct *parm;
2136 void *ptr;
2137 char *local_parm_name;
2138 char *parm_opt;
2139 const char *parm_opt_value;
2141 /* check for parametrical option */
2142 local_parm_name = talloc_strdup(lp_ctx, parm_name);
2143 if (local_parm_name == NULL) {
2144 return false;
2147 parm_opt = strchr( local_parm_name, ':');
2149 if (parm_opt) {
2150 *parm_opt = '\0';
2151 parm_opt++;
2152 if (strlen(parm_opt)) {
2153 parm_opt_value = lpcfg_parm_string(lp_ctx, service,
2154 local_parm_name, parm_opt);
2155 if (parm_opt_value) {
2156 fprintf(f, "%s\n", parm_opt_value);
2157 return true;
2160 return false;
2163 /* parameter is not parametric, search the table */
2164 parm = lpcfg_parm_struct(lp_ctx, parm_name);
2165 if (!parm) {
2166 return false;
2169 if (service != NULL && parm->p_class == P_GLOBAL) {
2170 return false;
2173 ptr = lpcfg_parm_ptr(lp_ctx, service,parm);
2175 lpcfg_print_parameter(parm, ptr, f);
2176 fprintf(f, "\n");
2177 return true;
2181 * Auto-load some home services.
2183 static void lpcfg_add_auto_services(struct loadparm_context *lp_ctx,
2184 const char *str)
2186 return;
2189 /***************************************************************************
2190 Initialise the sDefault parameter structure for the printer values.
2191 ***************************************************************************/
2193 void init_printer_values(struct loadparm_context *lp_ctx, TALLOC_CTX *ctx,
2194 struct loadparm_service *pService)
2196 /* choose defaults depending on the type of printing */
2197 switch (pService->printing) {
2198 case PRINT_BSD:
2199 case PRINT_AIX:
2200 case PRINT_LPRNT:
2201 case PRINT_LPROS2:
2202 lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P'%p'");
2203 lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P'%p' %j");
2204 lpcfg_string_set(ctx, &pService->print_command, "lpr -r -P'%p' %s");
2205 break;
2207 case PRINT_LPRNG:
2208 case PRINT_PLP:
2209 lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P'%p'");
2210 lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P'%p' %j");
2211 lpcfg_string_set(ctx, &pService->print_command, "lpr -r -P'%p' %s");
2212 lpcfg_string_set(ctx, &pService->queuepause_command, "lpc stop '%p'");
2213 lpcfg_string_set(ctx, &pService->queueresume_command, "lpc start '%p'");
2214 lpcfg_string_set(ctx, &pService->lppause_command, "lpc hold '%p' %j");
2215 lpcfg_string_set(ctx, &pService->lpresume_command, "lpc release '%p' %j");
2216 break;
2218 case PRINT_CUPS:
2219 case PRINT_IPRINT:
2220 /* set the lpq command to contain the destination printer
2221 name only. This is used by cups_queue_get() */
2222 lpcfg_string_set(ctx, &pService->lpq_command, "%p");
2223 lpcfg_string_set(ctx, &pService->lprm_command, "");
2224 lpcfg_string_set(ctx, &pService->print_command, "");
2225 lpcfg_string_set(ctx, &pService->lppause_command, "");
2226 lpcfg_string_set(ctx, &pService->lpresume_command, "");
2227 lpcfg_string_set(ctx, &pService->queuepause_command, "");
2228 lpcfg_string_set(ctx, &pService->queueresume_command, "");
2229 break;
2231 case PRINT_SYSV:
2232 case PRINT_HPUX:
2233 lpcfg_string_set(ctx, &pService->lpq_command, "lpstat -o%p");
2234 lpcfg_string_set(ctx, &pService->lprm_command, "cancel %p-%j");
2235 lpcfg_string_set(ctx, &pService->print_command, "lp -c -d%p %s; rm %s");
2236 lpcfg_string_set(ctx, &pService->queuepause_command, "disable %p");
2237 lpcfg_string_set(ctx, &pService->queueresume_command, "enable %p");
2238 #ifndef HPUX
2239 lpcfg_string_set(ctx, &pService->lppause_command, "lp -i %p-%j -H hold");
2240 lpcfg_string_set(ctx, &pService->lpresume_command, "lp -i %p-%j -H resume");
2241 #endif /* HPUX */
2242 break;
2244 case PRINT_QNX:
2245 lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P%p");
2246 lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P%p %j");
2247 lpcfg_string_set(ctx, &pService->print_command, "lp -r -P%p %s");
2248 break;
2250 #if defined(DEVELOPER) || defined(ENABLE_SELFTEST)
2252 case PRINT_TEST:
2253 case PRINT_VLP: {
2254 const char *tdbfile;
2255 TALLOC_CTX *tmp_ctx = talloc_new(ctx);
2256 const char *tmp;
2258 tmp = lpcfg_parm_string(lp_ctx, NULL, "vlp", "tdbfile");
2259 if (tmp == NULL) {
2260 tmp = "/tmp/vlp.tdb";
2263 tdbfile = talloc_asprintf(tmp_ctx, "tdbfile=%s", tmp);
2264 if (tdbfile == NULL) {
2265 tdbfile="tdbfile=/tmp/vlp.tdb";
2268 tmp = talloc_asprintf(tmp_ctx, "vlp %s print %%p %%s",
2269 tdbfile);
2270 lpcfg_string_set(ctx, &pService->print_command,
2271 tmp ? tmp : "vlp print %p %s");
2273 tmp = talloc_asprintf(tmp_ctx, "vlp %s lpq %%p",
2274 tdbfile);
2275 lpcfg_string_set(ctx, &pService->lpq_command,
2276 tmp ? tmp : "vlp lpq %p");
2278 tmp = talloc_asprintf(tmp_ctx, "vlp %s lprm %%p %%j",
2279 tdbfile);
2280 lpcfg_string_set(ctx, &pService->lprm_command,
2281 tmp ? tmp : "vlp lprm %p %j");
2283 tmp = talloc_asprintf(tmp_ctx, "vlp %s lppause %%p %%j",
2284 tdbfile);
2285 lpcfg_string_set(ctx, &pService->lppause_command,
2286 tmp ? tmp : "vlp lppause %p %j");
2288 tmp = talloc_asprintf(tmp_ctx, "vlp %s lpresume %%p %%j",
2289 tdbfile);
2290 lpcfg_string_set(ctx, &pService->lpresume_command,
2291 tmp ? tmp : "vlp lpresume %p %j");
2293 tmp = talloc_asprintf(tmp_ctx, "vlp %s queuepause %%p",
2294 tdbfile);
2295 lpcfg_string_set(ctx, &pService->queuepause_command,
2296 tmp ? tmp : "vlp queuepause %p");
2298 tmp = talloc_asprintf(tmp_ctx, "vlp %s queueresume %%p",
2299 tdbfile);
2300 lpcfg_string_set(ctx, &pService->queueresume_command,
2301 tmp ? tmp : "vlp queueresume %p");
2302 TALLOC_FREE(tmp_ctx);
2304 break;
2306 #endif /* DEVELOPER */
2312 * Unload unused services.
2315 void lpcfg_killunused(struct loadparm_context *lp_ctx,
2316 struct smbsrv_connection *smb,
2317 bool (*snumused) (struct smbsrv_connection *, int))
2319 int i;
2321 if (lp_ctx->s3_fns != NULL) {
2322 smb_panic("Cannot be used from an s3 loadparm ctx");
2325 for (i = 0; i < lp_ctx->iNumServices; i++) {
2326 if (lp_ctx->services[i] == NULL)
2327 continue;
2329 if (!snumused || !snumused(smb, i)) {
2330 talloc_free(lp_ctx->services[i]);
2331 lp_ctx->services[i] = NULL;
2337 static int lpcfg_destructor(struct loadparm_context *lp_ctx)
2339 struct parmlist_entry *data;
2341 if (lp_ctx->refuse_free) {
2342 /* someone is trying to free the
2343 global_loadparm_context.
2344 We can't allow that. */
2345 return -1;
2348 if (lp_ctx->globals->param_opt != NULL) {
2349 struct parmlist_entry *next;
2350 for (data = lp_ctx->globals->param_opt; data; data=next) {
2351 next = data->next;
2352 if (data->priority & FLAG_CMDLINE) continue;
2353 DLIST_REMOVE(lp_ctx->globals->param_opt, data);
2354 talloc_free(data);
2358 return 0;
2361 struct defaults_hook_data {
2362 const char *name;
2363 lpcfg_defaults_hook hook;
2364 struct defaults_hook_data *prev, *next;
2365 } *defaults_hooks = NULL;
2368 bool lpcfg_register_defaults_hook(const char *name, lpcfg_defaults_hook hook)
2370 struct defaults_hook_data *hook_data = talloc(talloc_autofree_context(),
2371 struct defaults_hook_data);
2372 hook_data->name = talloc_strdup(hook_data, name);
2373 hook_data->hook = hook;
2374 DLIST_ADD(defaults_hooks, hook_data);
2375 return false;
2379 * Initialise the global parameter structure.
2381 * Note that most callers should use loadparm_init_global() instead
2383 struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
2385 int i;
2386 char *myname;
2387 struct loadparm_context *lp_ctx;
2388 struct parmlist_entry *parm;
2389 char *logfile;
2390 struct defaults_hook_data *defaults_hook;
2392 lp_ctx = talloc_zero(mem_ctx, struct loadparm_context);
2393 if (lp_ctx == NULL)
2394 return NULL;
2396 talloc_set_destructor(lp_ctx, lpcfg_destructor);
2397 lp_ctx->bInGlobalSection = true;
2398 lp_ctx->globals = talloc_zero(lp_ctx, struct loadparm_global);
2399 /* This appears odd, but globals in s3 isn't a pointer */
2400 lp_ctx->globals->ctx = lp_ctx->globals;
2401 lp_ctx->sDefault = talloc_zero(lp_ctx, struct loadparm_service);
2402 lp_ctx->flags = talloc_zero_array(lp_ctx, unsigned int, num_parameters());
2404 lp_ctx->sDefault->iMaxPrintJobs = 1000;
2405 lp_ctx->sDefault->bAvailable = true;
2406 lp_ctx->sDefault->browseable = true;
2407 lp_ctx->sDefault->read_only = true;
2408 lp_ctx->sDefault->map_archive = true;
2409 lp_ctx->sDefault->strict_locking = true;
2410 lp_ctx->sDefault->oplocks = true;
2411 lp_ctx->sDefault->create_mask = 0744;
2412 lp_ctx->sDefault->force_create_mode = 0000;
2413 lp_ctx->sDefault->directory_mask = 0755;
2414 lp_ctx->sDefault->force_directory_mode = 0000;
2416 DEBUG(3, ("Initialising global parameters\n"));
2418 for (i = 0; parm_table[i].label; i++) {
2419 if ((parm_table[i].type == P_STRING ||
2420 parm_table[i].type == P_USTRING) &&
2421 !(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2422 TALLOC_CTX *parent_mem;
2423 char **r;
2424 if (parm_table[i].p_class == P_LOCAL) {
2425 parent_mem = lp_ctx->sDefault;
2426 r = (char **)(((char *)lp_ctx->sDefault) + parm_table[i].offset);
2427 } else {
2428 parent_mem = lp_ctx->globals;
2429 r = (char **)(((char *)lp_ctx->globals) + parm_table[i].offset);
2431 lpcfg_string_set(parent_mem, r, "");
2435 logfile = talloc_asprintf(lp_ctx, "%s/log.samba", dyn_LOGFILEBASE);
2436 lpcfg_do_global_parameter(lp_ctx, "log file", logfile);
2437 talloc_free(logfile);
2439 lpcfg_do_global_parameter(lp_ctx, "log level", "0");
2441 lpcfg_do_global_parameter(lp_ctx, "syslog", "1");
2442 lpcfg_do_global_parameter(lp_ctx, "syslog only", "No");
2443 lpcfg_do_global_parameter(lp_ctx, "debug timestamp", "Yes");
2444 lpcfg_do_global_parameter(lp_ctx, "debug prefix timestamp", "No");
2445 lpcfg_do_global_parameter(lp_ctx, "debug hires timestamp", "Yes");
2446 lpcfg_do_global_parameter(lp_ctx, "debug pid", "No");
2447 lpcfg_do_global_parameter(lp_ctx, "debug uid", "No");
2448 lpcfg_do_global_parameter(lp_ctx, "debug class", "No");
2450 lpcfg_do_global_parameter(lp_ctx, "share backend", "classic");
2452 lpcfg_do_global_parameter(lp_ctx, "server role", "auto");
2453 lpcfg_do_global_parameter(lp_ctx, "domain logons", "No");
2454 lpcfg_do_global_parameter(lp_ctx, "domain master", "Auto");
2456 /* options that can be set on the command line must be initialised via
2457 the slower lpcfg_do_global_parameter() to ensure that FLAG_CMDLINE is obeyed */
2458 #ifdef TCP_NODELAY
2459 lpcfg_do_global_parameter(lp_ctx, "socket options", "TCP_NODELAY");
2460 #endif
2461 lpcfg_do_global_parameter(lp_ctx, "workgroup", DEFAULT_WORKGROUP);
2462 myname = get_myname(lp_ctx);
2463 lpcfg_do_global_parameter(lp_ctx, "netbios name", myname);
2464 talloc_free(myname);
2465 lpcfg_do_global_parameter(lp_ctx, "name resolve order", "lmhosts wins host bcast");
2467 lpcfg_do_global_parameter(lp_ctx, "fstype", "NTFS");
2469 lpcfg_do_global_parameter(lp_ctx, "ntvfs handler", "unixuid default");
2470 lpcfg_do_global_parameter(lp_ctx, "max connections", "0");
2472 lpcfg_do_global_parameter(lp_ctx, "dcerpc endpoint servers", "epmapper wkssvc rpcecho samr netlogon lsarpc spoolss drsuapi dssetup unixinfo browser eventlog6 backupkey dnsserver");
2473 lpcfg_do_global_parameter(lp_ctx, "server services", "s3fs rpc nbt wrepl ldap cldap kdc drepl winbindd ntp_signd kcc dnsupdate dns");
2474 lpcfg_do_global_parameter(lp_ctx, "kccsrv:samba_kcc", "false");
2475 /* the winbind method for domain controllers is for both RODC
2476 auth forwarding and for trusted domains */
2477 lpcfg_do_global_parameter(lp_ctx, "private dir", dyn_PRIVATE_DIR);
2478 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_LOCAL_MACHINE", "hklm.ldb");
2480 /* This hive should be dynamically generated by Samba using
2481 data from the sam, but for the moment leave it in a tdb to
2482 keep regedt32 from popping up an annoying dialog. */
2483 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_USERS", "hku.ldb");
2485 /* using UTF8 by default allows us to support all chars */
2486 lpcfg_do_global_parameter(lp_ctx, "unix charset", "UTF-8");
2488 /* Use codepage 850 as a default for the dos character set */
2489 lpcfg_do_global_parameter(lp_ctx, "dos charset", "CP850");
2492 * Allow the default PASSWD_CHAT to be overridden in local.h.
2494 lpcfg_do_global_parameter(lp_ctx, "passwd chat", DEFAULT_PASSWD_CHAT);
2496 lpcfg_do_global_parameter(lp_ctx, "pid directory", dyn_PIDDIR);
2497 lpcfg_do_global_parameter(lp_ctx, "lock dir", dyn_LOCKDIR);
2498 lpcfg_do_global_parameter(lp_ctx, "state directory", dyn_STATEDIR);
2499 lpcfg_do_global_parameter(lp_ctx, "cache directory", dyn_CACHEDIR);
2500 lpcfg_do_global_parameter(lp_ctx, "ncalrpc dir", dyn_NCALRPCDIR);
2502 lpcfg_do_global_parameter(lp_ctx, "nbt client socket address", "0.0.0.0");
2503 lpcfg_do_global_parameter_var(lp_ctx, "server string",
2504 "Samba %s", SAMBA_VERSION_STRING);
2506 lpcfg_do_global_parameter(lp_ctx, "password server", "*");
2508 lpcfg_do_global_parameter(lp_ctx, "max mux", "50");
2509 lpcfg_do_global_parameter(lp_ctx, "max xmit", "16644");
2510 lpcfg_do_global_parameter(lp_ctx, "host msdfs", "true");
2512 lpcfg_do_global_parameter(lp_ctx, "LargeReadwrite", "True");
2513 lpcfg_do_global_parameter(lp_ctx, "server min protocol", "LANMAN1");
2514 lpcfg_do_global_parameter(lp_ctx, "server max protocol", "SMB3");
2515 lpcfg_do_global_parameter(lp_ctx, "client min protocol", "CORE");
2516 lpcfg_do_global_parameter(lp_ctx, "client max protocol", "default");
2517 lpcfg_do_global_parameter(lp_ctx, "client ipc min protocol", "default");
2518 lpcfg_do_global_parameter(lp_ctx, "client ipc max protocol", "default");
2519 lpcfg_do_global_parameter(lp_ctx, "security", "AUTO");
2520 lpcfg_do_global_parameter(lp_ctx, "EncryptPasswords", "True");
2521 lpcfg_do_global_parameter(lp_ctx, "ReadRaw", "True");
2522 lpcfg_do_global_parameter(lp_ctx, "WriteRaw", "True");
2523 lpcfg_do_global_parameter(lp_ctx, "NullPasswords", "False");
2524 lpcfg_do_global_parameter(lp_ctx, "old password allowed period", "60");
2525 lpcfg_do_global_parameter(lp_ctx, "ObeyPamRestrictions", "False");
2527 lpcfg_do_global_parameter(lp_ctx, "TimeServer", "False");
2528 lpcfg_do_global_parameter(lp_ctx, "BindInterfacesOnly", "False");
2529 lpcfg_do_global_parameter(lp_ctx, "Unicode", "True");
2530 lpcfg_do_global_parameter(lp_ctx, "ClientLanManAuth", "False");
2531 lpcfg_do_global_parameter(lp_ctx, "ClientNTLMv2Auth", "True");
2532 lpcfg_do_global_parameter(lp_ctx, "LanmanAuth", "False");
2533 lpcfg_do_global_parameter(lp_ctx, "NTLMAuth", "True");
2534 lpcfg_do_global_parameter(lp_ctx, "RawNTLMv2Auth", "False");
2535 lpcfg_do_global_parameter(lp_ctx, "client use spnego principal", "False");
2537 lpcfg_do_global_parameter(lp_ctx, "allow dcerpc auth level connect", "False");
2539 lpcfg_do_global_parameter(lp_ctx, "UnixExtensions", "True");
2541 lpcfg_do_global_parameter(lp_ctx, "PreferredMaster", "Auto");
2542 lpcfg_do_global_parameter(lp_ctx, "LocalMaster", "True");
2544 lpcfg_do_global_parameter(lp_ctx, "wins support", "False");
2545 lpcfg_do_global_parameter(lp_ctx, "dns proxy", "True");
2547 lpcfg_do_global_parameter(lp_ctx, "winbind separator", "\\");
2548 lpcfg_do_global_parameter(lp_ctx, "winbind sealed pipes", "True");
2549 lpcfg_do_global_parameter(lp_ctx, "require strong key", "True");
2550 lpcfg_do_global_parameter(lp_ctx, "winbindd socket directory", dyn_WINBINDD_SOCKET_DIR);
2551 lpcfg_do_global_parameter(lp_ctx, "winbindd privileged socket directory", dyn_WINBINDD_PRIVILEGED_SOCKET_DIR);
2552 lpcfg_do_global_parameter(lp_ctx, "ntp signd socket directory", dyn_NTP_SIGND_SOCKET_DIR);
2553 lpcfg_do_global_parameter_var(lp_ctx, "dns update command", "%s/samba_dnsupdate", dyn_SCRIPTSBINDIR);
2554 lpcfg_do_global_parameter_var(lp_ctx, "spn update command", "%s/samba_spnupdate", dyn_SCRIPTSBINDIR);
2555 lpcfg_do_global_parameter_var(lp_ctx, "samba kcc command",
2556 "%s/samba_kcc", dyn_SCRIPTSBINDIR);
2557 lpcfg_do_global_parameter(lp_ctx, "template shell", "/bin/false");
2558 lpcfg_do_global_parameter(lp_ctx, "template homedir", "/home/%D/%U");
2560 lpcfg_do_global_parameter(lp_ctx, "client signing", "default");
2561 lpcfg_do_global_parameter(lp_ctx, "client ipc signing", "default");
2562 lpcfg_do_global_parameter(lp_ctx, "server signing", "default");
2564 lpcfg_do_global_parameter(lp_ctx, "use spnego", "True");
2566 lpcfg_do_global_parameter(lp_ctx, "use mmap", "True");
2568 lpcfg_do_global_parameter(lp_ctx, "smb ports", "445 139");
2569 lpcfg_do_global_parameter_var(lp_ctx, "nbt port", "%d", NBT_NAME_SERVICE_PORT);
2570 lpcfg_do_global_parameter_var(lp_ctx, "dgram port", "%d", NBT_DGRAM_SERVICE_PORT);
2571 lpcfg_do_global_parameter(lp_ctx, "cldap port", "389");
2572 lpcfg_do_global_parameter(lp_ctx, "krb5 port", "88");
2573 lpcfg_do_global_parameter(lp_ctx, "kpasswd port", "464");
2574 lpcfg_do_global_parameter(lp_ctx, "web port", "901");
2576 lpcfg_do_global_parameter(lp_ctx, "nt status support", "True");
2578 lpcfg_do_global_parameter(lp_ctx, "max wins ttl", "518400"); /* 6 days */
2579 lpcfg_do_global_parameter(lp_ctx, "min wins ttl", "21600");
2581 lpcfg_do_global_parameter(lp_ctx, "tls enabled", "True");
2582 lpcfg_do_global_parameter(lp_ctx, "tls verify peer", "as_strict_as_possible");
2583 lpcfg_do_global_parameter(lp_ctx, "tls keyfile", "tls/key.pem");
2584 lpcfg_do_global_parameter(lp_ctx, "tls certfile", "tls/cert.pem");
2585 lpcfg_do_global_parameter(lp_ctx, "tls cafile", "tls/ca.pem");
2586 lpcfg_do_global_parameter(lp_ctx, "tls priority", "NORMAL:-VERS-SSL3.0");
2587 lpcfg_do_global_parameter(lp_ctx, "prefork children:smb", "4");
2589 lpcfg_do_global_parameter(lp_ctx, "rndc command", "/usr/sbin/rndc");
2590 lpcfg_do_global_parameter(lp_ctx, "nsupdate command", "/usr/bin/nsupdate -g");
2592 lpcfg_do_global_parameter(lp_ctx, "allow dns updates", "secure only");
2593 lpcfg_do_global_parameter(lp_ctx, "dns forwarder", "");
2595 lpcfg_do_global_parameter(lp_ctx, "algorithmic rid base", "1000");
2597 lpcfg_do_global_parameter(lp_ctx, "enhanced browsing", "True");
2599 lpcfg_do_global_parameter(lp_ctx, "winbind nss info", "template");
2601 lpcfg_do_global_parameter(lp_ctx, "server schannel", "Auto");
2603 lpcfg_do_global_parameter(lp_ctx, "short preserve case", "True");
2605 lpcfg_do_global_parameter(lp_ctx, "max open files", "16384");
2607 lpcfg_do_global_parameter(lp_ctx, "cups connection timeout", "30");
2609 lpcfg_do_global_parameter(lp_ctx, "locking", "True");
2611 lpcfg_do_global_parameter(lp_ctx, "block size", "1024");
2613 lpcfg_do_global_parameter(lp_ctx, "client use spnego", "True");
2615 lpcfg_do_global_parameter(lp_ctx, "change notify", "True");
2617 lpcfg_do_global_parameter(lp_ctx, "name cache timeout", "660");
2619 lpcfg_do_global_parameter(lp_ctx, "defer sharing violations", "True");
2621 lpcfg_do_global_parameter(lp_ctx, "ldap replication sleep", "1000");
2623 lpcfg_do_global_parameter(lp_ctx, "idmap backend", "tdb");
2625 lpcfg_do_global_parameter(lp_ctx, "enable privileges", "True");
2627 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max write", "%u", DEFAULT_SMB2_MAX_WRITE);
2629 lpcfg_do_global_parameter(lp_ctx, "passdb backend", "tdbsam");
2631 lpcfg_do_global_parameter(lp_ctx, "getwd cache", "True");
2633 lpcfg_do_global_parameter(lp_ctx, "winbind nested groups", "True");
2635 lpcfg_do_global_parameter(lp_ctx, "mangled names", "True");
2637 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max credits", "%u", DEFAULT_SMB2_MAX_CREDITS);
2639 lpcfg_do_global_parameter(lp_ctx, "ldap ssl", "start tls");
2641 lpcfg_do_global_parameter(lp_ctx, "ldap deref", "auto");
2643 lpcfg_do_global_parameter(lp_ctx, "lm interval", "60");
2645 lpcfg_do_global_parameter(lp_ctx, "mangling method", "hash2");
2647 lpcfg_do_global_parameter(lp_ctx, "hide dot files", "True");
2649 lpcfg_do_global_parameter(lp_ctx, "browse list", "True");
2651 lpcfg_do_global_parameter(lp_ctx, "passwd chat timeout", "2");
2653 lpcfg_do_global_parameter(lp_ctx, "guest account", GUEST_ACCOUNT);
2655 lpcfg_do_global_parameter(lp_ctx, "client schannel", "auto");
2657 lpcfg_do_global_parameter(lp_ctx, "smb encrypt", "default");
2659 lpcfg_do_global_parameter(lp_ctx, "max log size", "5000");
2661 lpcfg_do_global_parameter(lp_ctx, "idmap negative cache time", "120");
2663 lpcfg_do_global_parameter(lp_ctx, "ldap follow referral", "auto");
2665 lpcfg_do_global_parameter(lp_ctx, "multicast dns register", "yes");
2667 lpcfg_do_global_parameter(lp_ctx, "winbind reconnect delay", "30");
2669 lpcfg_do_global_parameter(lp_ctx, "winbind request timeout", "60");
2671 lpcfg_do_global_parameter(lp_ctx, "nt acl support", "yes");
2673 lpcfg_do_global_parameter(lp_ctx, "acl check permissions", "yes");
2675 lpcfg_do_global_parameter(lp_ctx, "keepalive", "300");
2677 lpcfg_do_global_parameter(lp_ctx, "smbd profiling level", "off");
2679 lpcfg_do_global_parameter(lp_ctx, "winbind cache time", "300");
2681 lpcfg_do_global_parameter(lp_ctx, "level2 oplocks", "yes");
2683 lpcfg_do_global_parameter(lp_ctx, "show add printer wizard", "yes");
2685 lpcfg_do_global_parameter(lp_ctx, "allocation roundup size", "1048576");
2687 lpcfg_do_global_parameter(lp_ctx, "ldap page size", "1024");
2689 lpcfg_do_global_parameter(lp_ctx, "kernel share modes", "yes");
2691 lpcfg_do_global_parameter(lp_ctx, "strict locking", "Auto");
2693 lpcfg_do_global_parameter(lp_ctx, "map readonly", "yes");
2695 lpcfg_do_global_parameter(lp_ctx, "allow trusted domains", "yes");
2697 lpcfg_do_global_parameter(lp_ctx, "default devmode", "yes");
2699 lpcfg_do_global_parameter(lp_ctx, "os level", "20");
2701 lpcfg_do_global_parameter(lp_ctx, "dos filetimes", "yes");
2703 lpcfg_do_global_parameter(lp_ctx, "mangling char", "~");
2705 lpcfg_do_global_parameter(lp_ctx, "printcap cache time", "750");
2707 lpcfg_do_global_parameter(lp_ctx, "create krb5 conf", "yes");
2709 lpcfg_do_global_parameter(lp_ctx, "winbind max clients", "200");
2711 lpcfg_do_global_parameter(lp_ctx, "acl map full control", "yes");
2713 lpcfg_do_global_parameter(lp_ctx, "nt pipe support", "yes");
2715 lpcfg_do_global_parameter(lp_ctx, "ldap debug threshold", "10");
2717 lpcfg_do_global_parameter(lp_ctx, "client ldap sasl wrapping", "sign");
2719 lpcfg_do_global_parameter(lp_ctx, "ldap server require strong auth", "yes");
2721 lpcfg_do_global_parameter(lp_ctx, "follow symlinks", "yes");
2723 lpcfg_do_global_parameter(lp_ctx, "machine password timeout", "604800");
2725 lpcfg_do_global_parameter(lp_ctx, "ldap connection timeout", "2");
2727 lpcfg_do_global_parameter(lp_ctx, "winbind expand groups", "0");
2729 lpcfg_do_global_parameter(lp_ctx, "stat cache", "yes");
2731 lpcfg_do_global_parameter(lp_ctx, "lpq cache time", "30");
2733 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max trans", "%u", DEFAULT_SMB2_MAX_TRANSACT);
2735 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max read", "%u", DEFAULT_SMB2_MAX_READ);
2737 lpcfg_do_global_parameter(lp_ctx, "durable handles", "yes");
2739 lpcfg_do_global_parameter(lp_ctx, "max stat cache size", "256");
2741 lpcfg_do_global_parameter(lp_ctx, "ldap passwd sync", "no");
2743 lpcfg_do_global_parameter(lp_ctx, "kernel change notify", "yes");
2745 lpcfg_do_global_parameter(lp_ctx, "max ttl", "259200");
2747 lpcfg_do_global_parameter(lp_ctx, "blocking locks", "yes");
2749 lpcfg_do_global_parameter(lp_ctx, "oplock contention limit", "2");
2751 lpcfg_do_global_parameter(lp_ctx, "load printers", "yes");
2753 lpcfg_do_global_parameter(lp_ctx, "idmap cache time", "604800");
2755 lpcfg_do_global_parameter(lp_ctx, "preserve case", "yes");
2757 lpcfg_do_global_parameter(lp_ctx, "lm announce", "auto");
2759 lpcfg_do_global_parameter(lp_ctx, "afs token lifetime", "604800");
2761 lpcfg_do_global_parameter(lp_ctx, "enable core files", "yes");
2763 lpcfg_do_global_parameter(lp_ctx, "winbind max domain connections", "1");
2765 lpcfg_do_global_parameter(lp_ctx, "case sensitive", "auto");
2767 lpcfg_do_global_parameter(lp_ctx, "ldap timeout", "15");
2769 lpcfg_do_global_parameter(lp_ctx, "mangle prefix", "1");
2771 lpcfg_do_global_parameter(lp_ctx, "posix locking", "yes");
2773 lpcfg_do_global_parameter(lp_ctx, "lock spin time", "200");
2775 lpcfg_do_global_parameter(lp_ctx, "directory name cache size", "100");
2777 lpcfg_do_global_parameter(lp_ctx, "nmbd bind explicit broadcast", "yes");
2779 lpcfg_do_global_parameter(lp_ctx, "init logon delay", "100");
2781 lpcfg_do_global_parameter(lp_ctx, "usershare owner only", "yes");
2783 lpcfg_do_global_parameter(lp_ctx, "-valid", "yes");
2785 lpcfg_do_global_parameter_var(lp_ctx, "usershare path", "%s/usershares", get_dyn_STATEDIR());
2787 #ifdef DEVELOPER
2788 lpcfg_do_global_parameter_var(lp_ctx, "panic action", "/bin/sleep 999999999");
2789 #endif
2791 lpcfg_do_global_parameter(lp_ctx, "smb passwd file", get_dyn_SMB_PASSWD_FILE());
2793 lpcfg_do_global_parameter(lp_ctx, "logon home", "\\\\%N\\%U");
2795 lpcfg_do_global_parameter(lp_ctx, "logon path", "\\\\%N\\%U\\profile");
2797 lpcfg_do_global_parameter(lp_ctx, "printjob username", "%U");
2799 /* Allow modules to adjust defaults */
2800 for (defaults_hook = defaults_hooks; defaults_hook;
2801 defaults_hook = defaults_hook->next) {
2802 bool ret;
2804 ret = defaults_hook->hook(lp_ctx);
2805 if (!ret) {
2806 DEBUG(1, ("Defaults hook %s failed to run.",
2807 defaults_hook->name));
2808 talloc_free(lp_ctx);
2809 return NULL;
2813 for (i = 0; parm_table[i].label; i++) {
2814 if (!(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2815 lp_ctx->flags[i] |= FLAG_DEFAULT;
2819 for (parm=lp_ctx->globals->param_opt; parm; parm=parm->next) {
2820 if (!(parm->priority & FLAG_CMDLINE)) {
2821 parm->priority |= FLAG_DEFAULT;
2825 for (parm=lp_ctx->sDefault->param_opt; parm; parm=parm->next) {
2826 if (!(parm->priority & FLAG_CMDLINE)) {
2827 parm->priority |= FLAG_DEFAULT;
2831 return lp_ctx;
2835 * Initialise the global parameter structure.
2837 struct loadparm_context *loadparm_init_global(bool load_default)
2839 if (global_loadparm_context == NULL) {
2840 global_loadparm_context = loadparm_init(NULL);
2842 if (global_loadparm_context == NULL) {
2843 return NULL;
2845 global_loadparm_context->global = true;
2846 if (load_default && !global_loadparm_context->loaded) {
2847 lpcfg_load_default(global_loadparm_context);
2849 global_loadparm_context->refuse_free = true;
2850 return global_loadparm_context;
2854 * Initialise the global parameter structure.
2856 struct loadparm_context *loadparm_init_s3(TALLOC_CTX *mem_ctx,
2857 const struct loadparm_s3_helpers *s3_fns)
2859 struct loadparm_context *loadparm_context = talloc_zero(mem_ctx, struct loadparm_context);
2860 if (!loadparm_context) {
2861 return NULL;
2863 loadparm_context->s3_fns = s3_fns;
2864 loadparm_context->globals = s3_fns->globals;
2865 loadparm_context->flags = s3_fns->flags;
2867 return loadparm_context;
2870 const char *lpcfg_configfile(struct loadparm_context *lp_ctx)
2872 return lp_ctx->szConfigFile;
2875 const char *lp_default_path(void)
2877 if (getenv("SMB_CONF_PATH"))
2878 return getenv("SMB_CONF_PATH");
2879 else
2880 return dyn_CONFIGFILE;
2884 * Update the internal state of a loadparm context after settings
2885 * have changed.
2887 static bool lpcfg_update(struct loadparm_context *lp_ctx)
2889 struct debug_settings settings;
2890 TALLOC_CTX *tmp_ctx;
2892 tmp_ctx = talloc_new(lp_ctx);
2893 if (tmp_ctx == NULL) {
2894 return false;
2897 lpcfg_add_auto_services(lp_ctx, lpcfg_auto_services(lp_ctx, tmp_ctx));
2899 if (!lp_ctx->globals->wins_server_list && lp_ctx->globals->we_are_a_wins_server) {
2900 lpcfg_do_global_parameter(lp_ctx, "wins server", "127.0.0.1");
2903 if (!lp_ctx->global) {
2904 TALLOC_FREE(tmp_ctx);
2905 return true;
2908 panic_action = lp_ctx->globals->panic_action;
2910 reload_charcnv(lp_ctx);
2912 ZERO_STRUCT(settings);
2913 /* Add any more debug-related smb.conf parameters created in
2914 * future here */
2915 settings.timestamp_logs = lp_ctx->globals->timestamp_logs;
2916 settings.debug_prefix_timestamp = lp_ctx->globals->debug_prefix_timestamp;
2917 settings.debug_hires_timestamp = lp_ctx->globals->debug_hires_timestamp;
2918 settings.debug_pid = lp_ctx->globals->debug_pid;
2919 settings.debug_uid = lp_ctx->globals->debug_uid;
2920 settings.debug_class = lp_ctx->globals->debug_class;
2921 debug_set_settings(&settings, lp_ctx->globals->logging,
2922 lp_ctx->globals->syslog,
2923 lp_ctx->globals->syslog_only);
2925 /* FIXME: This is a bit of a hack, but we can't use a global, since
2926 * not everything that uses lp also uses the socket library */
2927 if (lpcfg_parm_bool(lp_ctx, NULL, "socket", "testnonblock", false)) {
2928 setenv("SOCKET_TESTNONBLOCK", "1", 1);
2929 } else {
2930 unsetenv("SOCKET_TESTNONBLOCK");
2933 TALLOC_FREE(tmp_ctx);
2934 return true;
2937 bool lpcfg_load_default(struct loadparm_context *lp_ctx)
2939 const char *path;
2941 path = lp_default_path();
2943 if (!file_exist(path)) {
2944 /* We allow the default smb.conf file to not exist,
2945 * basically the equivalent of an empty file. */
2946 return lpcfg_update(lp_ctx);
2949 return lpcfg_load(lp_ctx, path);
2953 * Load the services array from the services file.
2955 * Return True on success, False on failure.
2957 bool lpcfg_load(struct loadparm_context *lp_ctx, const char *filename)
2959 char *n2;
2960 bool bRetval;
2962 filename = talloc_strdup(lp_ctx, filename);
2964 lp_ctx->szConfigFile = filename;
2966 if (lp_ctx->s3_fns) {
2967 return lp_ctx->s3_fns->load(filename);
2970 lp_ctx->bInGlobalSection = true;
2971 n2 = standard_sub_basic(lp_ctx, lp_ctx->szConfigFile);
2972 DEBUG(2, ("lpcfg_load: refreshing parameters from %s\n", n2));
2974 add_to_file_list(lp_ctx, &lp_ctx->file_lists, lp_ctx->szConfigFile, n2);
2976 /* We get sections first, so have to start 'behind' to make up */
2977 lp_ctx->currentService = NULL;
2978 bRetval = pm_process(n2, do_section, lpcfg_do_parameter, lp_ctx);
2980 /* finish up the last section */
2981 DEBUG(4, ("pm_process() returned %s\n", BOOLSTR(bRetval)));
2982 if (bRetval)
2983 if (lp_ctx->currentService != NULL)
2984 bRetval = lpcfg_service_ok(lp_ctx->currentService);
2986 bRetval = bRetval && lpcfg_update(lp_ctx);
2988 /* we do this unconditionally, so that it happens even
2989 for a missing smb.conf */
2990 reload_charcnv(lp_ctx);
2992 if (bRetval == true) {
2993 /* set this up so that any child python tasks will
2994 find the right smb.conf */
2995 setenv("SMB_CONF_PATH", filename, 1);
2997 /* set the context used by the lp_*() function
2998 varients */
2999 global_loadparm_context = lp_ctx;
3000 lp_ctx->loaded = true;
3003 return bRetval;
3007 * Return the max number of services.
3010 int lpcfg_numservices(struct loadparm_context *lp_ctx)
3012 if (lp_ctx->s3_fns) {
3013 return lp_ctx->s3_fns->get_numservices();
3016 return lp_ctx->iNumServices;
3020 * Display the contents of the services array in human-readable form.
3023 void lpcfg_dump(struct loadparm_context *lp_ctx, FILE *f, bool show_defaults,
3024 int maxtoprint)
3026 int iService;
3028 if (lp_ctx->s3_fns) {
3029 lp_ctx->s3_fns->dump(f, show_defaults, maxtoprint);
3030 return;
3033 lpcfg_dump_globals(lp_ctx, f, show_defaults);
3035 lpcfg_dump_a_service(lp_ctx->sDefault, lp_ctx->sDefault, f, lp_ctx->flags, show_defaults);
3037 for (iService = 0; iService < maxtoprint; iService++)
3038 lpcfg_dump_one(f, show_defaults, lp_ctx->services[iService], lp_ctx->sDefault);
3042 * Display the contents of one service in human-readable form.
3044 void lpcfg_dump_one(FILE *f, bool show_defaults, struct loadparm_service *service, struct loadparm_service *sDefault)
3046 if (service != NULL) {
3047 if (service->szService[0] == '\0')
3048 return;
3049 lpcfg_dump_a_service(service, sDefault, f, NULL, show_defaults);
3053 struct loadparm_service *lpcfg_servicebynum(struct loadparm_context *lp_ctx,
3054 int snum)
3056 if (lp_ctx->s3_fns) {
3057 return lp_ctx->s3_fns->get_servicebynum(snum);
3060 return lp_ctx->services[snum];
3063 struct loadparm_service *lpcfg_service(struct loadparm_context *lp_ctx,
3064 const char *service_name)
3066 int iService;
3067 char *serviceName;
3069 if (lp_ctx->s3_fns) {
3070 return lp_ctx->s3_fns->get_service(service_name);
3073 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--) {
3074 if (lp_ctx->services[iService] &&
3075 lp_ctx->services[iService]->szService) {
3077 * The substitution here is used to support %U is
3078 * service names
3080 serviceName = standard_sub_basic(
3081 lp_ctx->services[iService],
3082 lp_ctx->services[iService]->szService);
3083 if (strequal(serviceName, service_name)) {
3084 talloc_free(serviceName);
3085 return lp_ctx->services[iService];
3087 talloc_free(serviceName);
3091 DEBUG(7,("lpcfg_servicenumber: couldn't find %s\n", service_name));
3092 return NULL;
3095 const char *lpcfg_servicename(const struct loadparm_service *service)
3097 return lpcfg_string((const char *)service->szService);
3101 * A useful volume label function.
3103 const char *lpcfg_volume_label(struct loadparm_service *service, struct loadparm_service *sDefault)
3105 const char *ret;
3106 ret = lpcfg_string((const char *)((service != NULL && service->volume != NULL) ?
3107 service->volume : sDefault->volume));
3108 if (!*ret)
3109 return lpcfg_servicename(service);
3110 return ret;
3114 * Return the correct printer name.
3116 const char *lpcfg_printername(struct loadparm_service *service, struct loadparm_service *sDefault)
3118 const char *ret;
3119 ret = lpcfg_string((const char *)((service != NULL && service->_printername != NULL) ?
3120 service->_printername : sDefault->_printername));
3121 if (ret == NULL || (ret != NULL && *ret == '\0'))
3122 ret = lpcfg_servicename(service);
3124 return ret;
3129 * Return the max print jobs per queue.
3131 int lpcfg_maxprintjobs(struct loadparm_service *service, struct loadparm_service *sDefault)
3133 int maxjobs = (service != NULL) ? service->iMaxPrintJobs : sDefault->iMaxPrintJobs;
3134 if (maxjobs <= 0 || maxjobs >= PRINT_MAX_JOBID)
3135 maxjobs = PRINT_MAX_JOBID - 1;
3137 return maxjobs;
3140 struct smb_iconv_handle *lpcfg_iconv_handle(struct loadparm_context *lp_ctx)
3142 if (lp_ctx == NULL) {
3143 return get_iconv_handle();
3145 return lp_ctx->iconv_handle;
3148 _PUBLIC_ void reload_charcnv(struct loadparm_context *lp_ctx)
3150 struct smb_iconv_handle *old_ic = lp_ctx->iconv_handle;
3151 if (!lp_ctx->global) {
3152 return;
3155 if (old_ic == NULL) {
3156 old_ic = global_iconv_handle;
3158 lp_ctx->iconv_handle = smb_iconv_handle_reinit_lp(lp_ctx, lp_ctx, old_ic);
3159 global_iconv_handle = lp_ctx->iconv_handle;
3162 _PUBLIC_ char *lpcfg_tls_keyfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3164 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_keyfile(lp_ctx));
3167 _PUBLIC_ char *lpcfg_tls_certfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3169 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_certfile(lp_ctx));
3172 _PUBLIC_ char *lpcfg_tls_cafile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3174 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_cafile(lp_ctx));
3177 _PUBLIC_ char *lpcfg_tls_crlfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3179 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_crlfile(lp_ctx));
3182 _PUBLIC_ char *lpcfg_tls_dhpfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3184 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_dhpfile(lp_ctx));
3187 struct gensec_settings *lpcfg_gensec_settings(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3189 struct gensec_settings *settings = talloc_zero(mem_ctx, struct gensec_settings);
3190 if (settings == NULL)
3191 return NULL;
3192 SMB_ASSERT(lp_ctx != NULL);
3193 settings->lp_ctx = talloc_reference(settings, lp_ctx);
3194 settings->target_hostname = lpcfg_parm_string(lp_ctx, NULL, "gensec", "target_hostname");
3195 return settings;
3198 int lpcfg_server_role(struct loadparm_context *lp_ctx)
3200 int domain_master = lpcfg__domain_master(lp_ctx);
3202 return lp_find_server_role(lpcfg__server_role(lp_ctx),
3203 lpcfg__security(lp_ctx),
3204 lpcfg__domain_logons(lp_ctx),
3205 (domain_master == true) ||
3206 (domain_master == Auto));
3209 int lpcfg_security(struct loadparm_context *lp_ctx)
3211 return lp_find_security(lpcfg__server_role(lp_ctx),
3212 lpcfg__security(lp_ctx));
3215 int lpcfg_client_max_protocol(struct loadparm_context *lp_ctx)
3217 int client_max_protocol = lpcfg__client_max_protocol(lp_ctx);
3218 if (client_max_protocol == PROTOCOL_DEFAULT) {
3219 return PROTOCOL_NT1;
3221 return client_max_protocol;
3224 int lpcfg_client_ipc_min_protocol(struct loadparm_context *lp_ctx)
3226 int client_ipc_min_protocol = lpcfg__client_ipc_min_protocol(lp_ctx);
3227 if (client_ipc_min_protocol == PROTOCOL_DEFAULT) {
3228 client_ipc_min_protocol = lpcfg_client_min_protocol(lp_ctx);
3230 if (client_ipc_min_protocol < PROTOCOL_NT1) {
3231 return PROTOCOL_NT1;
3233 return client_ipc_min_protocol;
3236 int lpcfg_client_ipc_max_protocol(struct loadparm_context *lp_ctx)
3238 int client_ipc_max_protocol = lpcfg__client_ipc_max_protocol(lp_ctx);
3239 if (client_ipc_max_protocol == PROTOCOL_DEFAULT) {
3240 return PROTOCOL_LATEST;
3242 if (client_ipc_max_protocol < PROTOCOL_NT1) {
3243 return PROTOCOL_NT1;
3245 return client_ipc_max_protocol;
3248 int lpcfg_client_ipc_signing(struct loadparm_context *lp_ctx)
3250 int client_ipc_signing = lpcfg__client_ipc_signing(lp_ctx);
3251 if (client_ipc_signing == SMB_SIGNING_DEFAULT) {
3252 return SMB_SIGNING_REQUIRED;
3254 return client_ipc_signing;
3257 bool lpcfg_server_signing_allowed(struct loadparm_context *lp_ctx, bool *mandatory)
3259 bool allowed = true;
3260 enum smb_signing_setting signing_setting = lpcfg_server_signing(lp_ctx);
3262 *mandatory = false;
3264 if (signing_setting == SMB_SIGNING_DEFAULT) {
3266 * If we are a domain controller, SMB signing is
3267 * really important, as it can prevent a number of
3268 * attacks on communications between us and the
3269 * clients
3271 * However, it really sucks (no sendfile, CPU
3272 * overhead) performance-wise when used on a
3273 * file server, so disable it by default
3274 * on non-DCs
3277 if (lpcfg_server_role(lp_ctx) >= ROLE_ACTIVE_DIRECTORY_DC) {
3278 signing_setting = SMB_SIGNING_REQUIRED;
3279 } else {
3280 signing_setting = SMB_SIGNING_OFF;
3284 switch (signing_setting) {
3285 case SMB_SIGNING_REQUIRED:
3286 *mandatory = true;
3287 break;
3288 case SMB_SIGNING_DESIRED:
3289 case SMB_SIGNING_IF_REQUIRED:
3290 break;
3291 case SMB_SIGNING_OFF:
3292 allowed = false;
3293 break;
3294 case SMB_SIGNING_DEFAULT:
3295 case SMB_SIGNING_IPC_DEFAULT:
3296 smb_panic(__location__);
3297 break;
3300 return allowed;
3303 int lpcfg_tdb_hash_size(struct loadparm_context *lp_ctx, const char *name)
3305 const char *base;
3307 if (name == NULL) {
3308 return 0;
3311 base = strrchr_m(name, '/');
3312 if (base != NULL) {
3313 base += 1;
3314 } else {
3315 base = name;
3317 return lpcfg_parm_int(lp_ctx, NULL, "tdb_hashsize", base, 0);
3321 int lpcfg_tdb_flags(struct loadparm_context *lp_ctx, int tdb_flags)
3323 if (!lpcfg_use_mmap(lp_ctx)) {
3324 tdb_flags |= TDB_NOMMAP;
3326 return tdb_flags;