param: consolidate handle_copy method between the two loadparms
[Samba.git] / lib / param / loadparm.c
blobaaf905d8f464b7b3e6316a669dd7ac49ba664733
1 /*
2 Unix SMB/CIFS implementation.
3 Parameter loading functions
4 Copyright (C) Karl Auer 1993-1998
6 Largely re-written by Andrew Tridgell, September 1994
8 Copyright (C) Simo Sorce 2001
9 Copyright (C) Alexander Bokovoy 2002
10 Copyright (C) Stefan (metze) Metzmacher 2002
11 Copyright (C) Jim McDonough (jmcd@us.ibm.com) 2003.
12 Copyright (C) James Myers 2003 <myersjj@samba.org>
13 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
14 Copyright (C) Andrew Bartlett 2011-2012
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 * Load parameters.
33 * This module provides suitable callback functions for the params
34 * module. It builds the internal table of service details which is
35 * then used by the rest of the server.
37 * To add a parameter:
39 * 1) add it to the global or service structure definition
40 * 2) add it to the parm_table
41 * 3) add it to the list of available functions (eg: using FN_GLOBAL_STRING())
42 * 4) If it's a global then initialise it in init_globals. If a local
43 * (ie. service) parameter then initialise it in the sDefault structure
46 * Notes:
47 * The configuration file is processed sequentially for speed. It is NOT
48 * accessed randomly as happens in 'real' Windows. For this reason, there
49 * is a fair bit of sequence-dependent code here - ie., code which assumes
50 * that certain things happen before others. In particular, the code which
51 * happens at the boundary between sections is delicately poised, so be
52 * careful!
56 #include "includes.h"
57 #include "version.h"
58 #include "dynconfig/dynconfig.h"
59 #include "system/time.h"
60 #include "system/locale.h"
61 #include "system/network.h" /* needed for TCP_NODELAY */
62 #include "../lib/util/dlinklist.h"
63 #include "lib/param/param.h"
64 #include "lib/param/loadparm.h"
65 #include "auth/gensec/gensec.h"
66 #include "lib/param/s3_param.h"
67 #include "lib/util/bitmap.h"
68 #include "libcli/smb/smb_constants.h"
69 #include "tdb.h"
71 #define standard_sub_basic talloc_strdup
73 static bool do_parameter(const char *, const char *, void *);
74 static bool defaults_saved = false;
76 #include "lib/param/param_global.h"
78 #define NUMPARAMETERS (sizeof(parm_table) / sizeof(struct parm_struct))
80 /* we don't need a special handler for "dos charset" and "unix charset" */
81 #define handle_dos_charset NULL
82 #define handle_charset NULL
84 /* these are parameter handlers which are not needed in the
85 * non-source3 code
87 #define handle_netbios_aliases NULL
88 #define handle_printing NULL
89 #define handle_ldap_debug_level NULL
90 #define handle_idmap_backend NULL
91 #define handle_idmap_uid NULL
92 #define handle_idmap_gid NULL
94 #ifndef N_
95 #define N_(x) x
96 #endif
98 /* prototypes for the special type handlers */
99 static bool handle_include(struct loadparm_context *lp_ctx, int unused,
100 const char *pszParmValue, char **ptr);
102 #include "lib/param/param_table.c"
104 /* local variables */
105 struct loadparm_context {
106 const char *szConfigFile;
107 struct loadparm_global *globals;
108 struct loadparm_service **services;
109 struct loadparm_service *sDefault;
110 struct smb_iconv_handle *iconv_handle;
111 int iNumServices;
112 struct loadparm_service *currentService;
113 bool bInGlobalSection;
114 struct file_lists {
115 struct file_lists *next;
116 char *name;
117 char *subfname;
118 time_t modtime;
119 } *file_lists;
120 unsigned int flags[NUMPARAMETERS];
121 bool loaded;
122 bool refuse_free;
123 bool global; /* Is this the global context, which may set
124 * global variables such as debug level etc? */
125 const struct loadparm_s3_helpers *s3_fns;
129 struct loadparm_service *lpcfg_default_service(struct loadparm_context *lp_ctx)
131 if (lp_ctx->s3_fns) {
132 return lp_ctx->s3_fns->get_default_loadparm_service();
134 return lp_ctx->sDefault;
138 * Convenience routine to grab string parameters into temporary memory
139 * and run standard_sub_basic on them.
141 * The buffers can be written to by
142 * callers without affecting the source string.
145 static const char *lpcfg_string(const char *s)
147 #if 0 /* until REWRITE done to make thread-safe */
148 size_t len = s ? strlen(s) : 0;
149 char *ret;
150 #endif
152 /* The follow debug is useful for tracking down memory problems
153 especially if you have an inner loop that is calling a lp_*()
154 function that returns a string. Perhaps this debug should be
155 present all the time? */
157 #if 0
158 DEBUG(10, ("lpcfg_string(%s)\n", s));
159 #endif
161 #if 0 /* until REWRITE done to make thread-safe */
162 if (!lp_talloc)
163 lp_talloc = talloc_init("lp_talloc");
165 ret = talloc_array(lp_talloc, char, len + 100); /* leave room for substitution */
167 if (!ret)
168 return NULL;
170 if (!s)
171 *ret = 0;
172 else
173 strlcpy(ret, s, len);
175 if (trim_string(ret, "\"", "\"")) {
176 if (strchr(ret,'"') != NULL)
177 strlcpy(ret, s, len);
180 standard_sub_basic(ret,len+100);
181 return (ret);
182 #endif
183 return s;
187 In this section all the functions that are used to access the
188 parameters from the rest of the program are defined
192 * the creation of separate lpcfg_*() and lp_*() functions is to allow
193 * for code compatibility between existing Samba4 and Samba3 code.
196 /* this global context supports the lp_*() function varients */
197 static struct loadparm_context *global_loadparm_context;
199 #define lpcfg_default_service global_loadparm_context->sDefault
200 #define lpcfg_global_service(i) global_loadparm_context->services[i]
202 #define FN_GLOBAL_STRING(fn_name,var_name) \
203 _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx, TALLOC_CTX *ctx) {\
204 if (lp_ctx == NULL) return NULL; \
205 if (lp_ctx->s3_fns) { \
206 return lp_ctx->globals->var_name ? lp_ctx->s3_fns->lp_string(ctx, lp_ctx->globals->var_name) : talloc_strdup(ctx, ""); \
208 return lp_ctx->globals->var_name ? talloc_strdup(ctx, lpcfg_string(lp_ctx->globals->var_name)) : talloc_strdup(ctx, ""); \
211 #define FN_GLOBAL_CONST_STRING(fn_name,var_name) \
212 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
213 if (lp_ctx == NULL) return NULL; \
214 return lp_ctx->globals->var_name ? lpcfg_string(lp_ctx->globals->var_name) : ""; \
217 #define FN_GLOBAL_LIST(fn_name,var_name) \
218 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
219 if (lp_ctx == NULL) return NULL; \
220 return lp_ctx->globals->var_name; \
223 #define FN_GLOBAL_BOOL(fn_name,var_name) \
224 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) {\
225 if (lp_ctx == NULL) return false; \
226 return lp_ctx->globals->var_name; \
229 #define FN_GLOBAL_INTEGER(fn_name,var_name) \
230 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
231 return lp_ctx->globals->var_name; \
234 /* Local parameters don't need the ->s3_fns because the struct
235 * loadparm_service is shared and lpcfg_service() checks the ->s3_fns
236 * hook */
237 #define FN_LOCAL_STRING(fn_name,val) \
238 _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_service *service, \
239 struct loadparm_service *sDefault, TALLOC_CTX *ctx) { \
240 return(talloc_strdup(ctx, lpcfg_string((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)))); \
243 #define FN_LOCAL_CONST_STRING(fn_name,val) \
244 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_service *service, \
245 struct loadparm_service *sDefault) { \
246 return((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)); \
249 #define FN_LOCAL_LIST(fn_name,val) \
250 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_service *service, \
251 struct loadparm_service *sDefault) {\
252 return(const char **)(service != NULL && service->val != NULL? service->val : sDefault->val); \
255 #define FN_LOCAL_PARM_BOOL(fn_name, val) FN_LOCAL_BOOL(fn_name, val)
257 #define FN_LOCAL_BOOL(fn_name,val) \
258 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_service *service, \
259 struct loadparm_service *sDefault) { \
260 return((service != NULL)? service->val : sDefault->val); \
263 #define FN_LOCAL_INTEGER(fn_name,val) \
264 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_service *service, \
265 struct loadparm_service *sDefault) { \
266 return((service != NULL)? service->val : sDefault->val); \
269 #define FN_LOCAL_PARM_INTEGER(fn_name, val) FN_LOCAL_INTEGER(fn_name, val)
271 #define FN_LOCAL_PARM_CHAR(fn_name,val) \
272 _PUBLIC_ char lpcfg_ ## fn_name(struct loadparm_service *service, \
273 struct loadparm_service *sDefault) { \
274 return((service != NULL)? service->val : sDefault->val); \
277 #include "lib/param/param_functions.c"
279 /* These functions cannot be auto-generated */
280 FN_LOCAL_BOOL(autoloaded, autoloaded)
281 FN_GLOBAL_CONST_STRING(dnsdomain, dnsdomain)
283 /* local prototypes */
284 static struct loadparm_service *lpcfg_getservicebyname(struct loadparm_context *lp_ctx,
285 const char *pszServiceName);
286 static bool lpcfg_service_ok(struct loadparm_service *service);
287 static bool do_section(const char *pszSectionName, void *);
288 static void init_copymap(struct loadparm_service *pservice);
290 /* This is a helper function for parametrical options support. */
291 /* It returns a pointer to parametrical option value if it exists or NULL otherwise */
292 /* Actual parametrical functions are quite simple */
293 const char *lpcfg_get_parametric(struct loadparm_context *lp_ctx,
294 struct loadparm_service *service,
295 const char *type, const char *option)
297 char *vfskey_tmp = NULL;
298 char *vfskey = NULL;
299 struct parmlist_entry *data;
301 if (lp_ctx == NULL)
302 return NULL;
304 if (lp_ctx->s3_fns) {
305 return lp_ctx->s3_fns->get_parametric(service, type, option);
308 data = (service == NULL ? lp_ctx->globals->param_opt : service->param_opt);
310 vfskey_tmp = talloc_asprintf(NULL, "%s:%s", type, option);
311 if (vfskey_tmp == NULL) return NULL;
312 vfskey = strlower_talloc(NULL, vfskey_tmp);
313 talloc_free(vfskey_tmp);
315 while (data) {
316 if (strcmp(data->key, vfskey) == 0) {
317 talloc_free(vfskey);
318 return data->value;
320 data = data->next;
323 if (service != NULL) {
324 /* Try to fetch the same option but from globals */
325 /* but only if we are not already working with globals */
326 for (data = lp_ctx->globals->param_opt; data;
327 data = data->next) {
328 if (strcmp(data->key, vfskey) == 0) {
329 talloc_free(vfskey);
330 return data->value;
335 talloc_free(vfskey);
337 return NULL;
342 * convenience routine to return int parameters.
344 static int lp_int(const char *s)
347 if (!s) {
348 DEBUG(0,("lp_int(%s): is called with NULL!\n",s));
349 return -1;
352 return strtol(s, NULL, 0);
356 * convenience routine to return unsigned long parameters.
358 static unsigned long lp_ulong(const char *s)
361 if (!s) {
362 DEBUG(0,("lp_ulong(%s): is called with NULL!\n",s));
363 return -1;
366 return strtoul(s, NULL, 0);
370 * convenience routine to return unsigned long parameters.
372 static long lp_long(const char *s)
375 if (!s) {
376 DEBUG(0,("lp_long(%s): is called with NULL!\n",s));
377 return -1;
380 return strtol(s, NULL, 0);
384 * convenience routine to return unsigned long parameters.
386 static double lp_double(const char *s)
389 if (!s) {
390 DEBUG(0,("lp_double(%s): is called with NULL!\n",s));
391 return -1;
394 return strtod(s, NULL);
398 * convenience routine to return boolean parameters.
400 static bool lp_bool(const char *s)
402 bool ret = false;
404 if (!s) {
405 DEBUG(0,("lp_bool(%s): is called with NULL!\n",s));
406 return false;
409 if (!set_boolean(s, &ret)) {
410 DEBUG(0,("lp_bool(%s): value is not boolean!\n",s));
411 return false;
414 return ret;
419 * Return parametric option from a given service. Type is a part of option before ':'
420 * Parametric option has following syntax: 'Type: option = value'
421 * Returned value is allocated in 'lp_talloc' context
424 const char *lpcfg_parm_string(struct loadparm_context *lp_ctx,
425 struct loadparm_service *service, const char *type,
426 const char *option)
428 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
430 if (value)
431 return lpcfg_string(value);
433 return NULL;
437 * Return parametric option from a given service. Type is a part of option before ':'
438 * Parametric option has following syntax: 'Type: option = value'
439 * Returned value is allocated in 'lp_talloc' context
442 const char **lpcfg_parm_string_list(TALLOC_CTX *mem_ctx,
443 struct loadparm_context *lp_ctx,
444 struct loadparm_service *service,
445 const char *type,
446 const char *option, const char *separator)
448 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
450 if (value != NULL)
451 return (const char **)str_list_make(mem_ctx, value, separator);
453 return NULL;
457 * Return parametric option from a given service. Type is a part of option before ':'
458 * Parametric option has following syntax: 'Type: option = value'
461 int lpcfg_parm_int(struct loadparm_context *lp_ctx,
462 struct loadparm_service *service, const char *type,
463 const char *option, int default_v)
465 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
467 if (value)
468 return lp_int(value);
470 return default_v;
474 * Return parametric option from a given service. Type is a part of
475 * option before ':'.
476 * Parametric option has following syntax: 'Type: option = value'.
479 int lpcfg_parm_bytes(struct loadparm_context *lp_ctx,
480 struct loadparm_service *service, const char *type,
481 const char *option, int default_v)
483 uint64_t bval;
485 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
487 if (value && conv_str_size_error(value, &bval)) {
488 if (bval <= INT_MAX) {
489 return (int)bval;
493 return default_v;
497 * Return parametric option from a given service.
498 * Type is a part of option before ':'
499 * Parametric option has following syntax: 'Type: option = value'
501 unsigned long lpcfg_parm_ulong(struct loadparm_context *lp_ctx,
502 struct loadparm_service *service, const char *type,
503 const char *option, unsigned long default_v)
505 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
507 if (value)
508 return lp_ulong(value);
510 return default_v;
513 long lpcfg_parm_long(struct loadparm_context *lp_ctx,
514 struct loadparm_service *service, const char *type,
515 const char *option, long default_v)
517 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
519 if (value)
520 return lp_long(value);
522 return default_v;
525 double lpcfg_parm_double(struct loadparm_context *lp_ctx,
526 struct loadparm_service *service, const char *type,
527 const char *option, double default_v)
529 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
531 if (value != NULL)
532 return lp_double(value);
534 return default_v;
538 * Return parametric option from a given service. Type is a part of option before ':'
539 * Parametric option has following syntax: 'Type: option = value'
542 bool lpcfg_parm_bool(struct loadparm_context *lp_ctx,
543 struct loadparm_service *service, const char *type,
544 const char *option, bool default_v)
546 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
548 if (value != NULL)
549 return lp_bool(value);
551 return default_v;
556 * Set a string value, deallocating any existing space, and allocing the space
557 * for the string
559 static bool lpcfg_string_set(TALLOC_CTX *mem_ctx, char **dest, const char *src)
561 talloc_free(*dest);
563 if (src == NULL)
564 src = "";
566 *dest = talloc_strdup(mem_ctx, src);
567 if ((*dest) == NULL) {
568 DEBUG(0,("Out of memory in string_set\n"));
569 return false;
572 return true;
576 * Set a string value, deallocating any existing space, and allocing the space
577 * for the string
579 static bool lpcfg_string_set_upper(TALLOC_CTX *mem_ctx, char **dest, const char *src)
581 talloc_free(*dest);
583 if (src == NULL)
584 src = "";
586 *dest = strupper_talloc(mem_ctx, src);
587 if ((*dest) == NULL) {
588 DEBUG(0,("Out of memory in string_set_upper\n"));
589 return false;
592 return true;
598 * Add a new service to the services array initialising it with the given
599 * service.
602 struct loadparm_service *lpcfg_add_service(struct loadparm_context *lp_ctx,
603 const struct loadparm_service *pservice,
604 const char *name)
606 int i;
607 int num_to_alloc = lp_ctx->iNumServices + 1;
608 struct parmlist_entry *data, *pdata;
610 if (pservice == NULL) {
611 pservice = lp_ctx->sDefault;
614 /* it might already exist */
615 if (name) {
616 struct loadparm_service *service = lpcfg_getservicebyname(lp_ctx,
617 name);
618 if (service != NULL) {
619 /* Clean all parametric options for service */
620 /* They will be added during parsing again */
621 data = service->param_opt;
622 while (data) {
623 pdata = data->next;
624 talloc_free(data);
625 data = pdata;
627 service->param_opt = NULL;
628 return service;
632 /* find an invalid one */
633 for (i = 0; i < lp_ctx->iNumServices; i++)
634 if (lp_ctx->services[i] == NULL)
635 break;
637 /* if not, then create one */
638 if (i == lp_ctx->iNumServices) {
639 struct loadparm_service **tsp;
641 tsp = talloc_realloc(lp_ctx, lp_ctx->services, struct loadparm_service *, num_to_alloc);
643 if (!tsp) {
644 DEBUG(0,("lpcfg_add_service: failed to enlarge services!\n"));
645 return NULL;
646 } else {
647 lp_ctx->services = tsp;
648 lp_ctx->services[lp_ctx->iNumServices] = NULL;
651 lp_ctx->iNumServices++;
654 lp_ctx->services[i] = talloc_zero(lp_ctx->services, struct loadparm_service);
655 if (lp_ctx->services[i] == NULL) {
656 DEBUG(0,("lpcfg_add_service: out of memory!\n"));
657 return NULL;
659 copy_service(lp_ctx->services[i], pservice, NULL);
660 if (name != NULL)
661 lpcfg_string_set(lp_ctx->services[i], &lp_ctx->services[i]->szService, name);
662 return lp_ctx->services[i];
666 * Add a new home service, with the specified home directory, defaults coming
667 * from service ifrom.
670 bool lpcfg_add_home(struct loadparm_context *lp_ctx,
671 const char *pszHomename,
672 struct loadparm_service *default_service,
673 const char *user, const char *pszHomedir)
675 struct loadparm_service *service;
677 service = lpcfg_add_service(lp_ctx, default_service, pszHomename);
679 if (service == NULL)
680 return false;
682 if (!(*(default_service->path))
683 || strequal(default_service->path, lp_ctx->sDefault->path)) {
684 service->path = talloc_strdup(service, pszHomedir);
685 } else {
686 service->path = string_sub_talloc(service, lpcfg_path(default_service, lp_ctx->sDefault, service), "%H", pszHomedir);
689 if (!(*(service->comment))) {
690 service->comment = talloc_asprintf(service, "Home directory of %s", user);
692 service->bAvailable = default_service->bAvailable;
693 service->browseable = default_service->browseable;
695 DEBUG(3, ("adding home's share [%s] for user '%s' at '%s'\n",
696 pszHomename, user, service->path));
698 return true;
702 * Add a new printer service, with defaults coming from service iFrom.
705 bool lpcfg_add_printer(struct loadparm_context *lp_ctx,
706 const char *pszPrintername,
707 struct loadparm_service *default_service)
709 const char *comment = "From Printcap";
710 struct loadparm_service *service;
711 service = lpcfg_add_service(lp_ctx, default_service, pszPrintername);
713 if (service == NULL)
714 return false;
716 /* note that we do NOT default the availability flag to True - */
717 /* we take it from the default service passed. This allows all */
718 /* dynamic printers to be disabled by disabling the [printers] */
719 /* entry (if/when the 'available' keyword is implemented!). */
721 /* the printer name is set to the service name. */
722 lpcfg_string_set(service, &service->_printername, pszPrintername);
723 lpcfg_string_set(service, &service->comment, comment);
724 service->browseable = default_service->browseable;
725 /* Printers cannot be read_only. */
726 service->read_only = false;
727 /* Printer services must be printable. */
728 service->printable = true;
730 DEBUG(3, ("adding printer service %s\n", pszPrintername));
732 return true;
736 * Map a parameter's string representation to something we can use.
737 * Returns False if the parameter string is not recognised, else TRUE.
740 int lpcfg_map_parameter(const char *pszParmName)
742 int iIndex;
744 for (iIndex = 0; parm_table[iIndex].label; iIndex++)
745 if (strwicmp(parm_table[iIndex].label, pszParmName) == 0)
746 return iIndex;
748 /* Warn only if it isn't parametric option */
749 if (strchr(pszParmName, ':') == NULL)
750 DEBUG(0, ("Unknown parameter encountered: \"%s\"\n", pszParmName));
751 /* We do return 'fail' for parametric options as well because they are
752 stored in different storage
754 return -1;
759 return the parameter structure for a parameter
761 struct parm_struct *lpcfg_parm_struct(struct loadparm_context *lp_ctx, const char *name)
763 int parmnum;
765 if (lp_ctx->s3_fns) {
766 return lp_ctx->s3_fns->get_parm_struct(name);
769 parmnum = lpcfg_map_parameter(name);
770 if (parmnum == -1) return NULL;
771 return &parm_table[parmnum];
775 return the parameter pointer for a parameter
777 void *lpcfg_parm_ptr(struct loadparm_context *lp_ctx,
778 struct loadparm_service *service, struct parm_struct *parm)
780 if (lp_ctx->s3_fns) {
781 return lp_ctx->s3_fns->get_parm_ptr(service, parm);
784 if (service == NULL) {
785 if (parm->p_class == P_LOCAL)
786 return ((char *)lp_ctx->sDefault)+parm->offset;
787 else if (parm->p_class == P_GLOBAL)
788 return ((char *)lp_ctx->globals)+parm->offset;
789 else return NULL;
790 } else {
791 return ((char *)service) + parm->offset;
796 return the parameter pointer for a parameter
798 bool lpcfg_parm_is_cmdline(struct loadparm_context *lp_ctx, const char *name)
800 int parmnum;
802 if (lp_ctx->s3_fns) {
803 struct parm_struct *parm = lp_ctx->s3_fns->get_parm_struct(name);
804 if (parm) {
805 return parm->flags & FLAG_CMDLINE;
807 return false;
810 parmnum = lpcfg_map_parameter(name);
811 if (parmnum == -1) return false;
813 return lp_ctx->flags[parmnum] & FLAG_CMDLINE;
817 * Find a service by name. Otherwise works like get_service.
820 static struct loadparm_service *lpcfg_getservicebyname(struct loadparm_context *lp_ctx,
821 const char *pszServiceName)
823 int iService;
825 if (lp_ctx->s3_fns) {
826 return lp_ctx->s3_fns->get_service(pszServiceName);
829 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--)
830 if (lp_ctx->services[iService] != NULL &&
831 strwicmp(lp_ctx->services[iService]->szService, pszServiceName) == 0) {
832 return lp_ctx->services[iService];
835 return NULL;
839 * Add a parametric option to a parmlist_entry,
840 * replacing old value, if already present.
842 void set_param_opt(TALLOC_CTX *mem_ctx,
843 struct parmlist_entry **opt_list,
844 const char *opt_name,
845 const char *opt_value,
846 unsigned priority)
848 struct parmlist_entry *new_opt, *opt;
849 bool not_added;
851 opt = *opt_list;
852 not_added = true;
854 /* Traverse destination */
855 while (opt) {
856 /* If we already have same option, override it */
857 if (strwicmp(opt->key, opt_name) == 0) {
858 if ((opt->priority & FLAG_CMDLINE) &&
859 !(priority & FLAG_CMDLINE)) {
860 /* it's been marked as not to be
861 overridden */
862 return;
864 TALLOC_FREE(opt->value);
865 TALLOC_FREE(opt->list);
866 opt->value = talloc_strdup(opt, opt_value);
867 opt->priority = priority;
868 not_added = false;
869 break;
871 opt = opt->next;
873 if (not_added) {
874 new_opt = talloc(mem_ctx, struct parmlist_entry);
875 if (new_opt == NULL) {
876 smb_panic("OOM");
879 new_opt->key = talloc_strdup(new_opt, opt_name);
880 if (new_opt->key == NULL) {
881 smb_panic("talloc_strdup failed");
884 new_opt->value = talloc_strdup(new_opt, opt_value);
885 if (new_opt->value == NULL) {
886 smb_panic("talloc_strdup failed");
889 new_opt->list = NULL;
890 new_opt->priority = priority;
891 DLIST_ADD(*opt_list, new_opt);
896 * Copy a service structure to another.
897 * If pcopymapDest is NULL then copy all fields
900 void copy_service(struct loadparm_service *pserviceDest,
901 const struct loadparm_service *pserviceSource,
902 struct bitmap *pcopymapDest)
904 int i;
905 bool bcopyall = (pcopymapDest == NULL);
906 struct parmlist_entry *data;
908 for (i = 0; parm_table[i].label; i++)
909 if (parm_table[i].p_class == P_LOCAL &&
910 (bcopyall || bitmap_query(pcopymapDest, i))) {
911 const void *src_ptr =
912 ((const char *)pserviceSource) + parm_table[i].offset;
913 void *dest_ptr =
914 ((char *)pserviceDest) + parm_table[i].offset;
916 switch (parm_table[i].type) {
917 case P_BOOL:
918 case P_BOOLREV:
919 *(bool *)dest_ptr = *(const bool *)src_ptr;
920 break;
922 case P_INTEGER:
923 case P_BYTES:
924 case P_OCTAL:
925 case P_ENUM:
926 *(int *)dest_ptr = *(const int *)src_ptr;
927 break;
929 case P_CHAR:
930 *(char *)dest_ptr = *(const char *)src_ptr;
931 break;
933 case P_STRING:
934 lpcfg_string_set(pserviceDest,
935 (char **)dest_ptr,
936 *(const char * const *)src_ptr);
937 break;
939 case P_USTRING:
940 lpcfg_string_set_upper(pserviceDest,
941 (char **)dest_ptr,
942 *(const char * const *)src_ptr);
943 break;
944 case P_LIST:
945 TALLOC_FREE(*((char ***)dest_ptr));
946 *(const char * const **)dest_ptr = (const char * const *)str_list_copy(pserviceDest,
947 *(const char * * const *)src_ptr);
948 break;
949 default:
950 break;
954 if (bcopyall) {
955 init_copymap(pserviceDest);
956 if (pserviceSource->copymap)
957 bitmap_copy(pserviceDest->copymap,
958 pserviceSource->copymap);
961 for (data = pserviceSource->param_opt; data != NULL; data = data->next) {
962 set_param_opt(pserviceDest, &pserviceDest->param_opt,
963 data->key, data->value, data->priority);
968 * Check a service for consistency. Return False if the service is in any way
969 * incomplete or faulty, else True.
971 static bool lpcfg_service_ok(struct loadparm_service *service)
973 bool bRetval;
975 bRetval = true;
976 if (service->szService[0] == '\0') {
977 DEBUG(0, ("The following message indicates an internal error:\n"));
978 DEBUG(0, ("No service name in service entry.\n"));
979 bRetval = false;
982 /* The [printers] entry MUST be printable. I'm all for flexibility, but */
983 /* I can't see why you'd want a non-printable printer service... */
984 if (strwicmp(service->szService, PRINTERS_NAME) == 0) {
985 if (!service->printable) {
986 DEBUG(0, ("WARNING: [%s] service MUST be printable!\n",
987 service->szService));
988 service->printable = true;
990 /* [printers] service must also be non-browsable. */
991 if (service->browseable)
992 service->browseable = false;
995 /* If a service is flagged unavailable, log the fact at level 0. */
996 if (!service->bAvailable)
997 DEBUG(1, ("NOTE: Service %s is flagged unavailable.\n",
998 service->szService));
1000 return bRetval;
1004 /*******************************************************************
1005 Keep a linked list of all config files so we know when one has changed
1006 it's date and needs to be reloaded.
1007 ********************************************************************/
1009 static void add_to_file_list(struct loadparm_context *lp_ctx,
1010 const char *fname, const char *subfname)
1012 struct file_lists *f = lp_ctx->file_lists;
1014 while (f) {
1015 if (f->name && !strcmp(f->name, fname))
1016 break;
1017 f = f->next;
1020 if (!f) {
1021 f = talloc(lp_ctx, struct file_lists);
1022 if (!f)
1023 return;
1024 f->next = lp_ctx->file_lists;
1025 f->name = talloc_strdup(f, fname);
1026 if (!f->name) {
1027 talloc_free(f);
1028 return;
1030 f->subfname = talloc_strdup(f, subfname);
1031 if (!f->subfname) {
1032 talloc_free(f);
1033 return;
1035 lp_ctx->file_lists = f;
1036 f->modtime = file_modtime(subfname);
1037 } else {
1038 time_t t = file_modtime(subfname);
1039 if (t)
1040 f->modtime = t;
1044 /*******************************************************************
1045 Check if a config file has changed date.
1046 ********************************************************************/
1047 bool lpcfg_file_list_changed(struct loadparm_context *lp_ctx)
1049 struct file_lists *f;
1050 DEBUG(6, ("lpcfg_file_list_changed()\n"));
1052 for (f = lp_ctx->file_lists; f != NULL; f = f->next) {
1053 char *n2;
1054 time_t mod_time;
1056 n2 = standard_sub_basic(lp_ctx, f->name);
1058 DEBUGADD(6, ("file %s -> %s last mod_time: %s\n",
1059 f->name, n2, ctime(&f->modtime)));
1061 mod_time = file_modtime(n2);
1063 if (mod_time && ((f->modtime != mod_time) || (f->subfname == NULL) || (strcmp(n2, f->subfname) != 0))) {
1064 DEBUGADD(6, ("file %s modified: %s\n", n2,
1065 ctime(&mod_time)));
1066 f->modtime = mod_time;
1067 talloc_free(f->subfname);
1068 f->subfname = talloc_strdup(f, n2);
1069 return true;
1072 return false;
1075 /***************************************************************************
1076 Handle the "realm" parameter
1077 ***************************************************************************/
1079 bool handle_realm(struct loadparm_context *lp_ctx, int unused,
1080 const char *pszParmValue, char **ptr)
1082 char *upper;
1083 char *lower;
1085 upper = strupper_talloc(lp_ctx, pszParmValue);
1086 if (upper == NULL) {
1087 return false;
1090 lower = strlower_talloc(lp_ctx, pszParmValue);
1091 if (lower == NULL) {
1092 TALLOC_FREE(upper);
1093 return false;
1096 if (lp_ctx->s3_fns != NULL) {
1097 lp_ctx->s3_fns->lp_string_set(ptr, pszParmValue);
1098 lp_ctx->s3_fns->lp_string_set(&lp_ctx->globals->realm, upper);
1099 lp_ctx->s3_fns->lp_string_set(&lp_ctx->globals->dnsdomain, lower);
1100 } else {
1101 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1102 lpcfg_string_set(lp_ctx, &lp_ctx->globals->realm, upper);
1103 lpcfg_string_set(lp_ctx, &lp_ctx->globals->dnsdomain, lower);
1106 return true;
1109 /***************************************************************************
1110 Handle the include operation.
1111 ***************************************************************************/
1113 static bool handle_include(struct loadparm_context *lp_ctx, int unused,
1114 const char *pszParmValue, char **ptr)
1116 char *fname = standard_sub_basic(lp_ctx, pszParmValue);
1118 add_to_file_list(lp_ctx, pszParmValue, fname);
1120 lpcfg_string_set(lp_ctx, ptr, fname);
1122 if (file_exist(fname))
1123 return pm_process(fname, do_section, do_parameter, lp_ctx);
1125 DEBUG(2, ("Can't find include file %s\n", fname));
1127 return false;
1130 /***************************************************************************
1131 Handle the interpretation of the copy parameter.
1132 ***************************************************************************/
1134 bool handle_copy(struct loadparm_context *lp_ctx, int snum,
1135 const char *pszParmValue, char **ptr)
1137 bool bRetval;
1138 struct loadparm_service *serviceTemp = NULL;
1139 struct loadparm_service *current = NULL;
1141 bRetval = false;
1143 DEBUG(3, ("Copying service from service %s\n", pszParmValue));
1145 serviceTemp = lpcfg_getservicebyname(lp_ctx, pszParmValue);
1146 if (lp_ctx->s3_fns != NULL) {
1147 current = lp_ctx->s3_fns->get_servicebynum(snum);
1148 } else {
1149 current = lp_ctx->currentService;
1152 if (current == NULL) {
1153 DEBUG(0, ("Unable to copy service - invalid service destination"));
1154 return false;
1157 if (serviceTemp != NULL) {
1158 if (serviceTemp == current) {
1159 DEBUG(0, ("Can't copy service %s - unable to copy self!\n", pszParmValue));
1160 } else {
1161 copy_service(current,
1162 serviceTemp,
1163 current->copymap);
1164 lpcfg_string_set(current, ptr, pszParmValue);
1166 bRetval = true;
1168 } else {
1169 DEBUG(0, ("Unable to copy service - source not found: %s\n",
1170 pszParmValue));
1171 bRetval = false;
1174 return bRetval;
1177 bool handle_debug_list(struct loadparm_context *lp_ctx, int unused,
1178 const char *pszParmValue, char **ptr)
1180 if (lp_ctx->s3_fns != NULL) {
1181 lp_ctx->s3_fns->lp_string_set(ptr, pszParmValue);
1182 } else {
1183 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1186 return debug_parse_levels(pszParmValue);
1189 bool handle_logfile(struct loadparm_context *lp_ctx, int unused,
1190 const char *pszParmValue, char **ptr)
1192 if (lp_ctx->s3_fns != NULL) {
1193 lp_ctx->s3_fns->lp_string_set(ptr, pszParmValue);
1194 } else {
1195 debug_set_logfile(pszParmValue);
1196 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1199 return true;
1202 /***************************************************************************
1203 Initialise a copymap.
1204 ***************************************************************************/
1206 static void init_copymap(struct loadparm_service *pservice)
1208 int i;
1210 TALLOC_FREE(pservice->copymap);
1212 pservice->copymap = bitmap_talloc(NULL, NUMPARAMETERS);
1213 if (!pservice->copymap)
1214 DEBUG(0,
1215 ("Couldn't allocate copymap!! (size %d)\n",
1216 (int)NUMPARAMETERS));
1217 else
1218 for (i = 0; i < NUMPARAMETERS; i++)
1219 bitmap_set(pservice->copymap, i);
1223 * Process a parametric option
1225 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
1226 struct loadparm_service *service,
1227 const char *pszParmName,
1228 const char *pszParmValue, int flags)
1230 struct parmlist_entry *paramo, *data;
1231 char *name;
1232 TALLOC_CTX *mem_ctx;
1234 while (isspace((unsigned char)*pszParmName)) {
1235 pszParmName++;
1238 name = strlower_talloc(lp_ctx, pszParmName);
1239 if (!name) return false;
1241 if (service == NULL) {
1242 data = lp_ctx->globals->param_opt;
1243 mem_ctx = lp_ctx->globals;
1244 } else {
1245 data = service->param_opt;
1246 mem_ctx = service;
1249 /* Traverse destination */
1250 for (paramo=data; paramo; paramo=paramo->next) {
1251 /* If we already have the option set, override it unless
1252 it was a command line option and the new one isn't */
1253 if (strcmp(paramo->key, name) == 0) {
1254 if ((paramo->priority & FLAG_CMDLINE) &&
1255 !(flags & FLAG_CMDLINE)) {
1256 talloc_free(name);
1257 return true;
1260 talloc_free(paramo->value);
1261 paramo->value = talloc_strdup(paramo, pszParmValue);
1262 paramo->priority = flags;
1263 talloc_free(name);
1264 return true;
1268 paramo = talloc_zero(mem_ctx, struct parmlist_entry);
1269 if (!paramo)
1270 smb_panic("OOM");
1271 paramo->key = talloc_strdup(paramo, name);
1272 paramo->value = talloc_strdup(paramo, pszParmValue);
1273 paramo->priority = flags;
1274 if (service == NULL) {
1275 DLIST_ADD(lp_ctx->globals->param_opt, paramo);
1276 } else {
1277 DLIST_ADD(service->param_opt, paramo);
1280 talloc_free(name);
1282 return true;
1285 static bool set_variable(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
1286 const char *pszParmName, const char *pszParmValue,
1287 struct loadparm_context *lp_ctx, bool on_globals)
1289 int i;
1290 /* if it is a special case then go ahead */
1291 if (parm_table[parmnum].special) {
1292 bool ret;
1293 ret = parm_table[parmnum].special(lp_ctx, -1, pszParmValue,
1294 (char **)parm_ptr);
1295 if (!ret) {
1296 return false;
1298 goto mark_non_default;
1301 /* now switch on the type of variable it is */
1302 switch (parm_table[parmnum].type)
1304 case P_BOOL: {
1305 bool b;
1306 if (!set_boolean(pszParmValue, &b)) {
1307 DEBUG(0, ("set_variable(%s): value is not "
1308 "boolean!\n", pszParmValue));
1309 return false;
1311 *(bool *)parm_ptr = b;
1313 break;
1315 case P_BOOLREV: {
1316 bool b;
1317 if (!set_boolean(pszParmValue, &b)) {
1318 DEBUG(0, ("set_variable(%s): value is not "
1319 "boolean!\n", pszParmValue));
1320 return false;
1322 *(bool *)parm_ptr = !b;
1324 break;
1326 case P_INTEGER:
1327 *(int *)parm_ptr = atoi(pszParmValue);
1328 break;
1330 case P_CHAR:
1331 *(char *)parm_ptr = *pszParmValue;
1332 break;
1334 case P_OCTAL:
1335 *(int *)parm_ptr = strtol(pszParmValue, NULL, 8);
1336 break;
1338 case P_BYTES:
1340 uint64_t val;
1341 if (conv_str_size_error(pszParmValue, &val)) {
1342 if (val <= INT_MAX) {
1343 *(int *)parm_ptr = (int)val;
1344 break;
1348 DEBUG(0, ("set_variable(%s): value is not "
1349 "a valid size specifier!\n", pszParmValue));
1350 return false;
1353 case P_CMDLIST:
1354 *(const char * const **)parm_ptr
1355 = (const char * const *)str_list_make(mem_ctx,
1356 pszParmValue, NULL);
1357 break;
1358 case P_LIST:
1360 char **new_list = str_list_make(mem_ctx,
1361 pszParmValue, NULL);
1362 for (i=0; new_list[i]; i++) {
1363 if (*(const char ***)parm_ptr != NULL &&
1364 new_list[i][0] == '+' &&
1365 new_list[i][1])
1367 if (!str_list_check(*(const char ***)parm_ptr,
1368 &new_list[i][1])) {
1369 *(const char ***)parm_ptr = str_list_add(*(const char ***)parm_ptr,
1370 &new_list[i][1]);
1372 } else if (*(const char ***)parm_ptr != NULL &&
1373 new_list[i][0] == '-' &&
1374 new_list[i][1])
1376 str_list_remove(*(const char ***)parm_ptr,
1377 &new_list[i][1]);
1378 } else {
1379 if (i != 0) {
1380 DEBUG(0, ("Unsupported list syntax for: %s = %s\n",
1381 pszParmName, pszParmValue));
1382 return false;
1384 *(const char * const **)parm_ptr = (const char * const *) new_list;
1385 break;
1388 break;
1390 case P_STRING:
1391 lpcfg_string_set(mem_ctx, (char **)parm_ptr, pszParmValue);
1392 break;
1394 case P_USTRING:
1395 lpcfg_string_set_upper(mem_ctx, (char **)parm_ptr, pszParmValue);
1396 break;
1398 case P_ENUM:
1399 for (i = 0; parm_table[parmnum].enum_list[i].name; i++) {
1400 if (strequal
1401 (pszParmValue,
1402 parm_table[parmnum].enum_list[i].name)) {
1403 *(int *)parm_ptr =
1404 parm_table[parmnum].
1405 enum_list[i].value;
1406 break;
1409 if (!parm_table[parmnum].enum_list[i].name) {
1410 DEBUG(0,("Unknown enumerated value '%s' for '%s'\n",
1411 pszParmValue, pszParmName));
1412 return false;
1414 break;
1416 case P_SEP:
1417 break;
1420 mark_non_default:
1421 if (on_globals && (lp_ctx->flags[parmnum] & FLAG_DEFAULT)) {
1422 lp_ctx->flags[parmnum] &= ~FLAG_DEFAULT;
1423 /* we have to also unset FLAG_DEFAULT on aliases */
1424 for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
1425 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1427 for (i=parmnum+1;i<NUMPARAMETERS && parm_table[i].offset == parm_table[parmnum].offset;i++) {
1428 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1431 return true;
1435 bool lpcfg_do_global_parameter(struct loadparm_context *lp_ctx,
1436 const char *pszParmName, const char *pszParmValue)
1438 int parmnum = lpcfg_map_parameter(pszParmName);
1439 void *parm_ptr;
1441 if (parmnum < 0) {
1442 if (strchr(pszParmName, ':')) {
1443 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName, pszParmValue, 0);
1445 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1446 return true;
1449 /* if the flag has been set on the command line, then don't allow override,
1450 but don't report an error */
1451 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1452 return true;
1455 parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[parmnum]);
1457 return set_variable(lp_ctx->globals, parmnum, parm_ptr,
1458 pszParmName, pszParmValue, lp_ctx, true);
1461 bool lpcfg_do_service_parameter(struct loadparm_context *lp_ctx,
1462 struct loadparm_service *service,
1463 const char *pszParmName, const char *pszParmValue)
1465 void *parm_ptr;
1466 int i;
1467 int parmnum = lpcfg_map_parameter(pszParmName);
1469 if (parmnum < 0) {
1470 if (strchr(pszParmName, ':')) {
1471 return lp_do_parameter_parametric(lp_ctx, service, pszParmName, pszParmValue, 0);
1473 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1474 return true;
1477 /* if the flag has been set on the command line, then don't allow override,
1478 but don't report an error */
1479 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1480 return true;
1483 if (parm_table[parmnum].p_class == P_GLOBAL) {
1484 DEBUG(0,
1485 ("Global parameter %s found in service section!\n",
1486 pszParmName));
1487 return true;
1489 parm_ptr = ((char *)service) + parm_table[parmnum].offset;
1491 if (!service->copymap)
1492 init_copymap(service);
1494 /* this handles the aliases - set the copymap for other
1495 * entries with the same data pointer */
1496 for (i = 0; parm_table[i].label; i++)
1497 if (parm_table[i].offset == parm_table[parmnum].offset &&
1498 parm_table[i].p_class == parm_table[parmnum].p_class)
1499 bitmap_clear(service->copymap, i);
1501 return set_variable(service, parmnum, parm_ptr, pszParmName,
1502 pszParmValue, lp_ctx, false);
1506 * Process a parameter.
1509 static bool do_parameter(const char *pszParmName, const char *pszParmValue,
1510 void *userdata)
1512 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1514 if (lp_ctx->bInGlobalSection)
1515 return lpcfg_do_global_parameter(lp_ctx, pszParmName,
1516 pszParmValue);
1517 else
1518 return lpcfg_do_service_parameter(lp_ctx, lp_ctx->currentService,
1519 pszParmName, pszParmValue);
1523 variable argument do parameter
1525 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx, const char *pszParmName, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
1526 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx,
1527 const char *pszParmName, const char *fmt, ...)
1529 char *s;
1530 bool ret;
1531 va_list ap;
1533 va_start(ap, fmt);
1534 s = talloc_vasprintf(NULL, fmt, ap);
1535 va_end(ap);
1536 ret = lpcfg_do_global_parameter(lp_ctx, pszParmName, s);
1537 talloc_free(s);
1538 return ret;
1543 set a parameter from the commandline - this is called from command line parameter
1544 parsing code. It sets the parameter then marks the parameter as unable to be modified
1545 by smb.conf processing
1547 bool lpcfg_set_cmdline(struct loadparm_context *lp_ctx, const char *pszParmName,
1548 const char *pszParmValue)
1550 int parmnum;
1551 int i;
1553 if (lp_ctx->s3_fns) {
1554 return lp_ctx->s3_fns->set_cmdline(pszParmName, pszParmValue);
1557 parmnum = lpcfg_map_parameter(pszParmName);
1559 while (isspace((unsigned char)*pszParmValue)) pszParmValue++;
1562 if (parmnum < 0 && strchr(pszParmName, ':')) {
1563 /* set a parametric option */
1564 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName,
1565 pszParmValue, FLAG_CMDLINE);
1568 if (parmnum < 0) {
1569 DEBUG(0,("Unknown option '%s'\n", pszParmName));
1570 return false;
1573 /* reset the CMDLINE flag in case this has been called before */
1574 lp_ctx->flags[parmnum] &= ~FLAG_CMDLINE;
1576 if (!lpcfg_do_global_parameter(lp_ctx, pszParmName, pszParmValue)) {
1577 return false;
1580 lp_ctx->flags[parmnum] |= FLAG_CMDLINE;
1582 /* we have to also set FLAG_CMDLINE on aliases */
1583 for (i=parmnum-1;
1584 i>=0 && parm_table[i].p_class == parm_table[parmnum].p_class &&
1585 parm_table[i].offset == parm_table[parmnum].offset;
1586 i--) {
1587 lp_ctx->flags[i] |= FLAG_CMDLINE;
1589 for (i=parmnum+1;
1590 i<NUMPARAMETERS &&
1591 parm_table[i].p_class == parm_table[parmnum].p_class &&
1592 parm_table[i].offset == parm_table[parmnum].offset;
1593 i++) {
1594 lp_ctx->flags[i] |= FLAG_CMDLINE;
1597 return true;
1601 set a option from the commandline in 'a=b' format. Use to support --option
1603 bool lpcfg_set_option(struct loadparm_context *lp_ctx, const char *option)
1605 char *p, *s;
1606 bool ret;
1608 s = talloc_strdup(NULL, option);
1609 if (!s) {
1610 return false;
1613 p = strchr(s, '=');
1614 if (!p) {
1615 talloc_free(s);
1616 return false;
1619 *p = 0;
1621 ret = lpcfg_set_cmdline(lp_ctx, s, p+1);
1622 talloc_free(s);
1623 return ret;
1627 #define BOOLSTR(b) ((b) ? "Yes" : "No")
1630 * Print a parameter of the specified type.
1633 void lpcfg_print_parameter(struct parm_struct *p, void *ptr, FILE * f)
1635 /* For the seperation of lists values that we print below */
1636 const char *list_sep = ", ";
1637 int i;
1638 switch (p->type)
1640 case P_ENUM:
1641 for (i = 0; p->enum_list[i].name; i++) {
1642 if (*(int *)ptr == p->enum_list[i].value) {
1643 fprintf(f, "%s",
1644 p->enum_list[i].name);
1645 break;
1648 break;
1650 case P_BOOL:
1651 fprintf(f, "%s", BOOLSTR(*(bool *)ptr));
1652 break;
1654 case P_BOOLREV:
1655 fprintf(f, "%s", BOOLSTR(!*(bool *)ptr));
1656 break;
1658 case P_INTEGER:
1659 case P_BYTES:
1660 fprintf(f, "%d", *(int *)ptr);
1661 break;
1663 case P_CHAR:
1664 fprintf(f, "%c", *(char *)ptr);
1665 break;
1667 case P_OCTAL: {
1668 int val = *(int *)ptr;
1669 if (val == -1) {
1670 fprintf(f, "-1");
1671 } else {
1672 fprintf(f, "0%03o", val);
1674 break;
1677 case P_CMDLIST:
1678 list_sep = " ";
1679 /* fall through */
1680 case P_LIST:
1681 if ((char ***)ptr && *(char ***)ptr) {
1682 char **list = *(char ***)ptr;
1683 for (; *list; list++) {
1684 /* surround strings with whitespace in double quotes */
1685 if (*(list+1) == NULL) {
1686 /* last item, no extra separator */
1687 list_sep = "";
1689 if ( strchr_m( *list, ' ' ) ) {
1690 fprintf(f, "\"%s\"%s", *list, list_sep);
1691 } else {
1692 fprintf(f, "%s%s", *list, list_sep);
1696 break;
1698 case P_STRING:
1699 case P_USTRING:
1700 if (*(char **)ptr) {
1701 fprintf(f, "%s", *(char **)ptr);
1703 break;
1704 case P_SEP:
1705 break;
1710 * Check if two parameters are equal.
1713 bool lpcfg_equal_parameter(parm_type type, void *ptr1, void *ptr2)
1715 switch (type) {
1716 case P_BOOL:
1717 case P_BOOLREV:
1718 return (*((bool *)ptr1) == *((bool *)ptr2));
1720 case P_INTEGER:
1721 case P_ENUM:
1722 case P_OCTAL:
1723 case P_BYTES:
1724 return (*((int *)ptr1) == *((int *)ptr2));
1726 case P_CHAR:
1727 return (*((char *)ptr1) == *((char *)ptr2));
1729 case P_LIST:
1730 case P_CMDLIST:
1731 return str_list_equal(*(const char ***)ptr1, *(const char ***)ptr2);
1733 case P_STRING:
1734 case P_USTRING:
1736 char *p1 = *(char **)ptr1, *p2 = *(char **)ptr2;
1737 if (p1 && !*p1)
1738 p1 = NULL;
1739 if (p2 && !*p2)
1740 p2 = NULL;
1741 return (p1 == p2 || strequal(p1, p2));
1743 case P_SEP:
1744 break;
1746 return false;
1750 * Process a new section (service).
1752 * At this stage all sections are services.
1753 * Later we'll have special sections that permit server parameters to be set.
1754 * Returns True on success, False on failure.
1757 static bool do_section(const char *pszSectionName, void *userdata)
1759 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1760 bool bRetval;
1761 bool isglobal = ((strwicmp(pszSectionName, GLOBAL_NAME) == 0) ||
1762 (strwicmp(pszSectionName, GLOBAL_NAME2) == 0));
1763 bRetval = false;
1765 /* if we've just struck a global section, note the fact. */
1766 lp_ctx->bInGlobalSection = isglobal;
1768 /* check for multiple global sections */
1769 if (lp_ctx->bInGlobalSection) {
1770 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1771 return true;
1774 /* if we have a current service, tidy it up before moving on */
1775 bRetval = true;
1777 if (lp_ctx->currentService != NULL)
1778 bRetval = lpcfg_service_ok(lp_ctx->currentService);
1780 /* if all is still well, move to the next record in the services array */
1781 if (bRetval) {
1782 /* We put this here to avoid an odd message order if messages are */
1783 /* issued by the post-processing of a previous section. */
1784 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1786 if ((lp_ctx->currentService = lpcfg_add_service(lp_ctx, lp_ctx->sDefault,
1787 pszSectionName))
1788 == NULL) {
1789 DEBUG(0, ("Failed to add a new service\n"));
1790 return false;
1794 return bRetval;
1799 * Determine if a particular base parameter is currently set to the default value.
1802 static bool is_default(struct loadparm_service *sDefault, int i)
1804 void *def_ptr = ((char *)sDefault) + parm_table[i].offset;
1805 switch (parm_table[i].type) {
1806 case P_CMDLIST:
1807 case P_LIST:
1808 return str_list_equal((const char * const *)parm_table[i].def.lvalue,
1809 (const char **)def_ptr);
1810 case P_STRING:
1811 case P_USTRING:
1812 return strequal(parm_table[i].def.svalue,
1813 *(char **)def_ptr);
1814 case P_BOOL:
1815 case P_BOOLREV:
1816 return parm_table[i].def.bvalue ==
1817 *(bool *)def_ptr;
1818 case P_INTEGER:
1819 case P_CHAR:
1820 case P_OCTAL:
1821 case P_BYTES:
1822 case P_ENUM:
1823 return parm_table[i].def.ivalue ==
1824 *(int *)def_ptr;
1825 case P_SEP:
1826 break;
1828 return false;
1832 *Display the contents of the global structure.
1835 static void dump_globals(struct loadparm_context *lp_ctx, FILE *f,
1836 bool show_defaults)
1838 int i;
1839 struct parmlist_entry *data;
1841 fprintf(f, "# Global parameters\n[global]\n");
1843 for (i = 0; parm_table[i].label; i++)
1844 if (parm_table[i].p_class == P_GLOBAL &&
1845 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset))) {
1846 if (!show_defaults && (lp_ctx->flags[i] & FLAG_DEFAULT))
1847 continue;
1848 fprintf(f, "\t%s = ", parm_table[i].label);
1849 lpcfg_print_parameter(&parm_table[i], lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[i]), f);
1850 fprintf(f, "\n");
1852 if (lp_ctx->globals->param_opt != NULL) {
1853 for (data = lp_ctx->globals->param_opt; data;
1854 data = data->next) {
1855 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
1856 continue;
1858 fprintf(f, "\t%s = %s\n", data->key, data->value);
1865 * Display the contents of a single services record.
1868 static void dump_a_service(struct loadparm_service * pService, struct loadparm_service *sDefault, FILE * f,
1869 unsigned int *flags)
1871 int i;
1872 struct parmlist_entry *data;
1874 if (pService != sDefault)
1875 fprintf(f, "\n[%s]\n", pService->szService);
1877 for (i = 0; parm_table[i].label; i++) {
1878 if (parm_table[i].p_class == P_LOCAL &&
1879 (*parm_table[i].label != '-') &&
1880 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset)))
1882 if (pService == sDefault) {
1883 if (flags && (flags[i] & FLAG_DEFAULT)) {
1884 continue;
1886 if (defaults_saved) {
1887 if (is_default(sDefault, i)) {
1888 continue;
1891 } else {
1892 if (lpcfg_equal_parameter(parm_table[i].type,
1893 ((char *)pService) +
1894 parm_table[i].offset,
1895 ((char *)sDefault) +
1896 parm_table[i].offset))
1897 continue;
1900 fprintf(f, "\t%s = ", parm_table[i].label);
1901 lpcfg_print_parameter(&parm_table[i],
1902 ((char *)pService) + parm_table[i].offset, f);
1903 fprintf(f, "\n");
1906 if (pService->param_opt != NULL) {
1907 for (data = pService->param_opt; data; data = data->next) {
1908 fprintf(f, "\t%s = %s\n", data->key, data->value);
1913 bool lpcfg_dump_a_parameter(struct loadparm_context *lp_ctx,
1914 struct loadparm_service *service,
1915 const char *parm_name, FILE * f)
1917 struct parm_struct *parm;
1918 void *ptr;
1920 parm = lpcfg_parm_struct(lp_ctx, parm_name);
1921 if (!parm) {
1922 return false;
1925 if (service != NULL && parm->p_class == P_GLOBAL) {
1926 return false;
1929 ptr = lpcfg_parm_ptr(lp_ctx, service,parm);
1931 lpcfg_print_parameter(parm, ptr, f);
1932 fprintf(f, "\n");
1933 return true;
1937 * Auto-load some home services.
1939 static void lpcfg_add_auto_services(struct loadparm_context *lp_ctx,
1940 const char *str)
1942 return;
1947 * Unload unused services.
1950 void lpcfg_killunused(struct loadparm_context *lp_ctx,
1951 struct smbsrv_connection *smb,
1952 bool (*snumused) (struct smbsrv_connection *, int))
1954 int i;
1955 for (i = 0; i < lp_ctx->iNumServices; i++) {
1956 if (lp_ctx->services[i] == NULL)
1957 continue;
1959 if (!snumused || !snumused(smb, i)) {
1960 talloc_free(lp_ctx->services[i]);
1961 lp_ctx->services[i] = NULL;
1967 static int lpcfg_destructor(struct loadparm_context *lp_ctx)
1969 struct parmlist_entry *data;
1971 if (lp_ctx->refuse_free) {
1972 /* someone is trying to free the
1973 global_loadparm_context.
1974 We can't allow that. */
1975 return -1;
1978 if (lp_ctx->globals->param_opt != NULL) {
1979 struct parmlist_entry *next;
1980 for (data = lp_ctx->globals->param_opt; data; data=next) {
1981 next = data->next;
1982 if (data->priority & FLAG_CMDLINE) continue;
1983 DLIST_REMOVE(lp_ctx->globals->param_opt, data);
1984 talloc_free(data);
1988 return 0;
1992 * Initialise the global parameter structure.
1994 * Note that most callers should use loadparm_init_global() instead
1996 struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
1998 int i;
1999 char *myname;
2000 struct loadparm_context *lp_ctx;
2001 struct parmlist_entry *parm;
2002 char *logfile;
2004 lp_ctx = talloc_zero(mem_ctx, struct loadparm_context);
2005 if (lp_ctx == NULL)
2006 return NULL;
2008 talloc_set_destructor(lp_ctx, lpcfg_destructor);
2009 lp_ctx->bInGlobalSection = true;
2010 lp_ctx->globals = talloc_zero(lp_ctx, struct loadparm_global);
2011 lp_ctx->sDefault = talloc_zero(lp_ctx, struct loadparm_service);
2013 lp_ctx->sDefault->iMaxPrintJobs = 1000;
2014 lp_ctx->sDefault->bAvailable = true;
2015 lp_ctx->sDefault->browseable = true;
2016 lp_ctx->sDefault->read_only = true;
2017 lp_ctx->sDefault->map_archive = true;
2018 lp_ctx->sDefault->strict_locking = true;
2019 lp_ctx->sDefault->oplocks = true;
2020 lp_ctx->sDefault->create_mask = 0744;
2021 lp_ctx->sDefault->force_create_mode = 0000;
2022 lp_ctx->sDefault->directory_mask = 0755;
2023 lp_ctx->sDefault->force_directory_mode = 0000;
2025 DEBUG(3, ("Initialising global parameters\n"));
2027 for (i = 0; parm_table[i].label; i++) {
2028 if ((parm_table[i].type == P_STRING ||
2029 parm_table[i].type == P_USTRING) &&
2030 !(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2031 char **r;
2032 if (parm_table[i].p_class == P_LOCAL) {
2033 r = (char **)(((char *)lp_ctx->sDefault) + parm_table[i].offset);
2034 } else {
2035 r = (char **)(((char *)lp_ctx->globals) + parm_table[i].offset);
2037 *r = talloc_strdup(lp_ctx, "");
2041 logfile = talloc_asprintf(lp_ctx, "%s/log.samba", dyn_LOGFILEBASE);
2042 lpcfg_do_global_parameter(lp_ctx, "log file", logfile);
2043 talloc_free(logfile);
2045 lpcfg_do_global_parameter(lp_ctx, "log level", "0");
2047 lpcfg_do_global_parameter(lp_ctx, "syslog", "1");
2048 lpcfg_do_global_parameter(lp_ctx, "syslog only", "No");
2049 lpcfg_do_global_parameter(lp_ctx, "debug timestamp", "Yes");
2050 lpcfg_do_global_parameter(lp_ctx, "debug prefix timestamp", "No");
2051 lpcfg_do_global_parameter(lp_ctx, "debug hires timestamp", "Yes");
2052 lpcfg_do_global_parameter(lp_ctx, "debug pid", "No");
2053 lpcfg_do_global_parameter(lp_ctx, "debug uid", "No");
2054 lpcfg_do_global_parameter(lp_ctx, "debug class", "No");
2056 lpcfg_do_global_parameter(lp_ctx, "share backend", "classic");
2058 lpcfg_do_global_parameter(lp_ctx, "server role", "auto");
2059 lpcfg_do_global_parameter(lp_ctx, "domain logons", "No");
2060 lpcfg_do_global_parameter(lp_ctx, "domain master", "Auto");
2062 /* options that can be set on the command line must be initialised via
2063 the slower lpcfg_do_global_parameter() to ensure that FLAG_CMDLINE is obeyed */
2064 #ifdef TCP_NODELAY
2065 lpcfg_do_global_parameter(lp_ctx, "socket options", "TCP_NODELAY");
2066 #endif
2067 lpcfg_do_global_parameter(lp_ctx, "workgroup", DEFAULT_WORKGROUP);
2068 myname = get_myname(lp_ctx);
2069 lpcfg_do_global_parameter(lp_ctx, "netbios name", myname);
2070 talloc_free(myname);
2071 lpcfg_do_global_parameter(lp_ctx, "name resolve order", "lmhosts wins host bcast");
2073 lpcfg_do_global_parameter(lp_ctx, "fstype", "NTFS");
2075 lpcfg_do_global_parameter(lp_ctx, "ntvfs handler", "unixuid default");
2076 lpcfg_do_global_parameter(lp_ctx, "max connections", "0");
2078 lpcfg_do_global_parameter(lp_ctx, "dcerpc endpoint servers", "epmapper wkssvc rpcecho samr netlogon lsarpc spoolss drsuapi dssetup unixinfo browser eventlog6 backupkey dnsserver");
2079 lpcfg_do_global_parameter(lp_ctx, "server services", "s3fs rpc nbt wrepl ldap cldap kdc drepl winbind ntp_signd kcc dnsupdate dns");
2080 lpcfg_do_global_parameter(lp_ctx, "kccsrv:samba_kcc", "true");
2081 /* the winbind method for domain controllers is for both RODC
2082 auth forwarding and for trusted domains */
2083 lpcfg_do_global_parameter(lp_ctx, "private dir", dyn_PRIVATE_DIR);
2084 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_LOCAL_MACHINE", "hklm.ldb");
2086 /* This hive should be dynamically generated by Samba using
2087 data from the sam, but for the moment leave it in a tdb to
2088 keep regedt32 from popping up an annoying dialog. */
2089 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_USERS", "hku.ldb");
2091 /* using UTF8 by default allows us to support all chars */
2092 lpcfg_do_global_parameter(lp_ctx, "unix charset", "UTF-8");
2094 /* Use codepage 850 as a default for the dos character set */
2095 lpcfg_do_global_parameter(lp_ctx, "dos charset", "CP850");
2098 * Allow the default PASSWD_CHAT to be overridden in local.h.
2100 lpcfg_do_global_parameter(lp_ctx, "passwd chat", DEFAULT_PASSWD_CHAT);
2102 lpcfg_do_global_parameter(lp_ctx, "pid directory", dyn_PIDDIR);
2103 lpcfg_do_global_parameter(lp_ctx, "lock dir", dyn_LOCKDIR);
2104 lpcfg_do_global_parameter(lp_ctx, "state directory", dyn_STATEDIR);
2105 lpcfg_do_global_parameter(lp_ctx, "cache directory", dyn_CACHEDIR);
2106 lpcfg_do_global_parameter(lp_ctx, "ncalrpc dir", dyn_NCALRPCDIR);
2108 lpcfg_do_global_parameter(lp_ctx, "nbt client socket address", "0.0.0.0");
2109 lpcfg_do_global_parameter_var(lp_ctx, "server string",
2110 "Samba %s", SAMBA_VERSION_STRING);
2112 lpcfg_do_global_parameter(lp_ctx, "password server", "*");
2114 lpcfg_do_global_parameter(lp_ctx, "max mux", "50");
2115 lpcfg_do_global_parameter(lp_ctx, "max xmit", "16644");
2116 lpcfg_do_global_parameter(lp_ctx, "host msdfs", "true");
2118 lpcfg_do_global_parameter(lp_ctx, "LargeReadwrite", "True");
2119 lpcfg_do_global_parameter(lp_ctx, "server min protocol", "LANMAN1");
2120 lpcfg_do_global_parameter(lp_ctx, "server max protocol", "SMB3");
2121 lpcfg_do_global_parameter(lp_ctx, "client min protocol", "CORE");
2122 lpcfg_do_global_parameter(lp_ctx, "client max protocol", "NT1");
2123 lpcfg_do_global_parameter(lp_ctx, "security", "AUTO");
2124 lpcfg_do_global_parameter(lp_ctx, "EncryptPasswords", "True");
2125 lpcfg_do_global_parameter(lp_ctx, "ReadRaw", "True");
2126 lpcfg_do_global_parameter(lp_ctx, "WriteRaw", "True");
2127 lpcfg_do_global_parameter(lp_ctx, "NullPasswords", "False");
2128 lpcfg_do_global_parameter(lp_ctx, "old password allowed period", "60");
2129 lpcfg_do_global_parameter(lp_ctx, "ObeyPamRestrictions", "False");
2131 lpcfg_do_global_parameter(lp_ctx, "TimeServer", "False");
2132 lpcfg_do_global_parameter(lp_ctx, "BindInterfacesOnly", "False");
2133 lpcfg_do_global_parameter(lp_ctx, "Unicode", "True");
2134 lpcfg_do_global_parameter(lp_ctx, "ClientLanManAuth", "False");
2135 lpcfg_do_global_parameter(lp_ctx, "ClientNTLMv2Auth", "True");
2136 lpcfg_do_global_parameter(lp_ctx, "LanmanAuth", "False");
2137 lpcfg_do_global_parameter(lp_ctx, "NTLMAuth", "True");
2138 lpcfg_do_global_parameter(lp_ctx, "client use spnego principal", "False");
2140 lpcfg_do_global_parameter(lp_ctx, "UnixExtensions", "True");
2142 lpcfg_do_global_parameter(lp_ctx, "PreferredMaster", "Auto");
2143 lpcfg_do_global_parameter(lp_ctx, "LocalMaster", "True");
2145 lpcfg_do_global_parameter(lp_ctx, "wins support", "False");
2146 lpcfg_do_global_parameter(lp_ctx, "dns proxy", "True");
2148 lpcfg_do_global_parameter(lp_ctx, "winbind separator", "\\");
2149 lpcfg_do_global_parameter(lp_ctx, "winbind sealed pipes", "True");
2150 lpcfg_do_global_parameter(lp_ctx, "require strong key", "True");
2151 lpcfg_do_global_parameter(lp_ctx, "winbindd socket directory", dyn_WINBINDD_SOCKET_DIR);
2152 lpcfg_do_global_parameter(lp_ctx, "winbindd privileged socket directory", dyn_WINBINDD_PRIVILEGED_SOCKET_DIR);
2153 lpcfg_do_global_parameter(lp_ctx, "ntp signd socket directory", dyn_NTP_SIGND_SOCKET_DIR);
2154 lpcfg_do_global_parameter_var(lp_ctx, "dns update command", "%s/samba_dnsupdate", dyn_SCRIPTSBINDIR);
2155 lpcfg_do_global_parameter_var(lp_ctx, "spn update command", "%s/samba_spnupdate", dyn_SCRIPTSBINDIR);
2156 lpcfg_do_global_parameter_var(lp_ctx, "samba kcc command",
2157 "%s/samba_kcc", dyn_SCRIPTSBINDIR);
2158 lpcfg_do_global_parameter(lp_ctx, "template shell", "/bin/false");
2159 lpcfg_do_global_parameter(lp_ctx, "template homedir", "/home/%WORKGROUP%/%ACCOUNTNAME%");
2161 lpcfg_do_global_parameter(lp_ctx, "client signing", "default");
2162 lpcfg_do_global_parameter(lp_ctx, "server signing", "default");
2164 lpcfg_do_global_parameter(lp_ctx, "use spnego", "True");
2166 lpcfg_do_global_parameter(lp_ctx, "use mmap", "True");
2168 lpcfg_do_global_parameter(lp_ctx, "smb ports", "445 139");
2169 lpcfg_do_global_parameter(lp_ctx, "nbt port", "137");
2170 lpcfg_do_global_parameter(lp_ctx, "dgram port", "138");
2171 lpcfg_do_global_parameter(lp_ctx, "cldap port", "389");
2172 lpcfg_do_global_parameter(lp_ctx, "krb5 port", "88");
2173 lpcfg_do_global_parameter(lp_ctx, "kpasswd port", "464");
2174 lpcfg_do_global_parameter(lp_ctx, "web port", "901");
2176 lpcfg_do_global_parameter(lp_ctx, "nt status support", "True");
2178 lpcfg_do_global_parameter(lp_ctx, "max wins ttl", "518400"); /* 6 days */
2179 lpcfg_do_global_parameter(lp_ctx, "min wins ttl", "21600");
2181 lpcfg_do_global_parameter(lp_ctx, "tls enabled", "True");
2182 lpcfg_do_global_parameter(lp_ctx, "tls keyfile", "tls/key.pem");
2183 lpcfg_do_global_parameter(lp_ctx, "tls certfile", "tls/cert.pem");
2184 lpcfg_do_global_parameter(lp_ctx, "tls cafile", "tls/ca.pem");
2185 lpcfg_do_global_parameter(lp_ctx, "prefork children:smb", "4");
2187 lpcfg_do_global_parameter(lp_ctx, "rndc command", "/usr/sbin/rndc");
2188 lpcfg_do_global_parameter(lp_ctx, "nsupdate command", "/usr/bin/nsupdate -g");
2190 lpcfg_do_global_parameter(lp_ctx, "allow dns updates", "secure only");
2191 lpcfg_do_global_parameter(lp_ctx, "dns forwarder", "");
2193 lpcfg_do_global_parameter(lp_ctx, "algorithmic rid base", "1000");
2195 lpcfg_do_global_parameter(lp_ctx, "enhanced browsing", "True");
2197 lpcfg_do_global_parameter(lp_ctx, "winbind nss info", "template");
2199 lpcfg_do_global_parameter(lp_ctx, "server schannel", "Auto");
2201 lpcfg_do_global_parameter(lp_ctx, "short preserve case", "True");
2203 lpcfg_do_global_parameter(lp_ctx, "max open files", "16384");
2205 lpcfg_do_global_parameter(lp_ctx, "cups connection timeout", "30");
2207 lpcfg_do_global_parameter(lp_ctx, "locking", "True");
2209 lpcfg_do_global_parameter(lp_ctx, "block size", "1024");
2211 lpcfg_do_global_parameter(lp_ctx, "client use spnego", "True");
2213 lpcfg_do_global_parameter(lp_ctx, "change notify", "True");
2215 lpcfg_do_global_parameter(lp_ctx, "name cache timeout", "660");
2217 lpcfg_do_global_parameter(lp_ctx, "defer sharing violations", "True");
2219 lpcfg_do_global_parameter(lp_ctx, "ldap replication sleep", "1000");
2221 lpcfg_do_global_parameter(lp_ctx, "idmap backend", "tdb");
2223 lpcfg_do_global_parameter(lp_ctx, "enable privileges", "True");
2225 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max write", "%u", DEFAULT_SMB2_MAX_WRITE);
2227 lpcfg_do_global_parameter(lp_ctx, "passdb backend", "tdbsam");
2229 lpcfg_do_global_parameter(lp_ctx, "getwd cache", "True");
2231 lpcfg_do_global_parameter(lp_ctx, "winbind nested groups", "True");
2233 lpcfg_do_global_parameter(lp_ctx, "mangled names", "True");
2235 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max credits", "%u", DEFAULT_SMB2_MAX_CREDITS);
2237 lpcfg_do_global_parameter(lp_ctx, "ldap ssl", "start tls");
2239 lpcfg_do_global_parameter(lp_ctx, "ldap deref", "auto");
2241 lpcfg_do_global_parameter(lp_ctx, "lm interval", "60");
2243 lpcfg_do_global_parameter(lp_ctx, "mangling method", "hash2");
2245 lpcfg_do_global_parameter(lp_ctx, "hide dot files", "True");
2247 lpcfg_do_global_parameter(lp_ctx, "browse list", "True");
2249 lpcfg_do_global_parameter(lp_ctx, "passwd chat timeout", "2");
2251 lpcfg_do_global_parameter(lp_ctx, "guest account", GUEST_ACCOUNT);
2253 lpcfg_do_global_parameter(lp_ctx, "client schannel", "auto");
2255 lpcfg_do_global_parameter(lp_ctx, "smb encrypt", "default");
2257 lpcfg_do_global_parameter(lp_ctx, "max log size", "5000");
2259 lpcfg_do_global_parameter(lp_ctx, "idmap negative cache time", "120");
2261 lpcfg_do_global_parameter(lp_ctx, "ldap follow referral", "auto");
2263 lpcfg_do_global_parameter(lp_ctx, "multicast dns register", "yes");
2265 lpcfg_do_global_parameter(lp_ctx, "winbind reconnect delay", "30");
2267 lpcfg_do_global_parameter(lp_ctx, "nt acl support", "yes");
2269 lpcfg_do_global_parameter(lp_ctx, "acl check permissions", "yes");
2271 lpcfg_do_global_parameter(lp_ctx, "keepalive", "300");
2273 lpcfg_do_global_parameter(lp_ctx, "winbind cache time", "300");
2275 lpcfg_do_global_parameter(lp_ctx, "level2 oplocks", "yes");
2277 lpcfg_do_global_parameter(lp_ctx, "show add printer wizard", "yes");
2279 lpcfg_do_global_parameter(lp_ctx, "allocation roundup size", "1048576");
2281 lpcfg_do_global_parameter(lp_ctx, "ldap page size", "1024");
2283 lpcfg_do_global_parameter(lp_ctx, "kernel share modes", "yes");
2285 lpcfg_do_global_parameter(lp_ctx, "strict locking", "Auto");
2287 lpcfg_do_global_parameter(lp_ctx, "map readonly", "yes");
2289 lpcfg_do_global_parameter(lp_ctx, "allow trusted domains", "yes");
2291 lpcfg_do_global_parameter(lp_ctx, "default devmode", "yes");
2293 lpcfg_do_global_parameter(lp_ctx, "os level", "20");
2295 lpcfg_do_global_parameter(lp_ctx, "dos filetimes", "yes");
2297 lpcfg_do_global_parameter(lp_ctx, "mangling char", "~");
2299 lpcfg_do_global_parameter(lp_ctx, "printcap cache time", "750");
2301 lpcfg_do_global_parameter(lp_ctx, "create krb5 conf", "yes");
2303 lpcfg_do_global_parameter(lp_ctx, "winbind max clients", "200");
2305 lpcfg_do_global_parameter(lp_ctx, "acl map full control", "yes");
2307 lpcfg_do_global_parameter(lp_ctx, "nt pipe support", "yes");
2309 lpcfg_do_global_parameter(lp_ctx, "ldap debug threshold", "10");
2311 lpcfg_do_global_parameter(lp_ctx, "follow symlinks", "yes");
2313 lpcfg_do_global_parameter(lp_ctx, "machine password timeout", "604800");
2315 lpcfg_do_global_parameter(lp_ctx, "ldap connection timeout", "2");
2317 lpcfg_do_global_parameter(lp_ctx, "winbind expand groups", "1");
2319 lpcfg_do_global_parameter(lp_ctx, "stat cache", "yes");
2321 lpcfg_do_global_parameter(lp_ctx, "lpq cache time", "30");
2323 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max trans", "%u", DEFAULT_SMB2_MAX_TRANSACT);
2325 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max read", "%u", DEFAULT_SMB2_MAX_READ);
2327 lpcfg_do_global_parameter(lp_ctx, "durable handles", "yes");
2329 lpcfg_do_global_parameter(lp_ctx, "max stat cache size", "256");
2331 lpcfg_do_global_parameter(lp_ctx, "ldap passwd sync", "no");
2333 lpcfg_do_global_parameter(lp_ctx, "kernel change notify", "yes");
2335 lpcfg_do_global_parameter(lp_ctx, "max ttl", "259200");
2337 lpcfg_do_global_parameter(lp_ctx, "blocking locks", "yes");
2339 lpcfg_do_global_parameter(lp_ctx, "oplock contention limit", "2");
2341 lpcfg_do_global_parameter(lp_ctx, "load printers", "yes");
2343 lpcfg_do_global_parameter(lp_ctx, "idmap cache time", "604800");
2345 lpcfg_do_global_parameter(lp_ctx, "preserve case", "yes");
2347 lpcfg_do_global_parameter(lp_ctx, "lm announce", "auto");
2349 lpcfg_do_global_parameter(lp_ctx, "afs token lifetime", "604800");
2351 lpcfg_do_global_parameter(lp_ctx, "enable core files", "yes");
2353 lpcfg_do_global_parameter(lp_ctx, "winbind max domain connections", "1");
2355 lpcfg_do_global_parameter(lp_ctx, "case sensitive", "auto");
2357 lpcfg_do_global_parameter(lp_ctx, "ldap timeout", "15");
2359 lpcfg_do_global_parameter(lp_ctx, "mangle prefix", "1");
2361 lpcfg_do_global_parameter(lp_ctx, "posix locking", "yes");
2363 lpcfg_do_global_parameter(lp_ctx, "lock spin time", "200");
2365 lpcfg_do_global_parameter(lp_ctx, "directory name cache size", "100");
2367 lpcfg_do_global_parameter(lp_ctx, "nmbd bind explicit broadcast", "yes");
2369 lpcfg_do_global_parameter(lp_ctx, "init logon delay", "100");
2371 lpcfg_do_global_parameter(lp_ctx, "usershare owner only", "yes");
2373 lpcfg_do_global_parameter(lp_ctx, "-valid", "yes");
2375 lpcfg_do_global_parameter_var(lp_ctx, "usershare path", "%s/usershares", get_dyn_STATEDIR());
2377 #ifdef DEVELOPER
2378 lpcfg_do_global_parameter_var(lp_ctx, "panic action", "/bin/sleep 999999999");
2379 #endif
2381 lpcfg_do_global_parameter(lp_ctx, "smb passwd file", get_dyn_SMB_PASSWD_FILE());
2383 lpcfg_do_global_parameter(lp_ctx, "logon home", "\\\\%N\\%U");
2385 lpcfg_do_global_parameter(lp_ctx, "logon path", "\\\\%N\\%U\\profile");
2387 lpcfg_do_global_parameter(lp_ctx, "printjob username", "%U");
2389 for (i = 0; parm_table[i].label; i++) {
2390 if (!(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2391 lp_ctx->flags[i] |= FLAG_DEFAULT;
2395 for (parm=lp_ctx->globals->param_opt; parm; parm=parm->next) {
2396 if (!(parm->priority & FLAG_CMDLINE)) {
2397 parm->priority |= FLAG_DEFAULT;
2401 return lp_ctx;
2405 * Initialise the global parameter structure.
2407 struct loadparm_context *loadparm_init_global(bool load_default)
2409 if (global_loadparm_context == NULL) {
2410 global_loadparm_context = loadparm_init(NULL);
2412 if (global_loadparm_context == NULL) {
2413 return NULL;
2415 global_loadparm_context->global = true;
2416 if (load_default && !global_loadparm_context->loaded) {
2417 lpcfg_load_default(global_loadparm_context);
2419 global_loadparm_context->refuse_free = true;
2420 return global_loadparm_context;
2424 * Initialise the global parameter structure.
2426 struct loadparm_context *loadparm_init_s3(TALLOC_CTX *mem_ctx,
2427 const struct loadparm_s3_helpers *s3_fns)
2429 struct loadparm_context *loadparm_context = talloc_zero(mem_ctx, struct loadparm_context);
2430 if (!loadparm_context) {
2431 return NULL;
2433 loadparm_context->s3_fns = s3_fns;
2434 loadparm_context->globals = s3_fns->globals;
2435 return loadparm_context;
2438 const char *lpcfg_configfile(struct loadparm_context *lp_ctx)
2440 return lp_ctx->szConfigFile;
2443 const char *lp_default_path(void)
2445 if (getenv("SMB_CONF_PATH"))
2446 return getenv("SMB_CONF_PATH");
2447 else
2448 return dyn_CONFIGFILE;
2452 * Update the internal state of a loadparm context after settings
2453 * have changed.
2455 static bool lpcfg_update(struct loadparm_context *lp_ctx)
2457 struct debug_settings settings;
2458 TALLOC_CTX *tmp_ctx;
2460 tmp_ctx = talloc_new(lp_ctx);
2461 if (tmp_ctx == NULL) {
2462 return false;
2465 lpcfg_add_auto_services(lp_ctx, lpcfg_auto_services(lp_ctx, tmp_ctx));
2467 if (!lp_ctx->globals->wins_server_list && lp_ctx->globals->we_are_a_wins_server) {
2468 lpcfg_do_global_parameter(lp_ctx, "wins server", "127.0.0.1");
2471 if (!lp_ctx->global) {
2472 TALLOC_FREE(tmp_ctx);
2473 return true;
2476 panic_action = lp_ctx->globals->panic_action;
2478 reload_charcnv(lp_ctx);
2480 ZERO_STRUCT(settings);
2481 /* Add any more debug-related smb.conf parameters created in
2482 * future here */
2483 settings.syslog = lp_ctx->globals->syslog;
2484 settings.syslog_only = lp_ctx->globals->syslog_only;
2485 settings.timestamp_logs = lp_ctx->globals->timestamp_logs;
2486 settings.debug_prefix_timestamp = lp_ctx->globals->debug_prefix_timestamp;
2487 settings.debug_hires_timestamp = lp_ctx->globals->debug_hires_timestamp;
2488 settings.debug_pid = lp_ctx->globals->debug_pid;
2489 settings.debug_uid = lp_ctx->globals->debug_uid;
2490 settings.debug_class = lp_ctx->globals->debug_class;
2491 debug_set_settings(&settings);
2493 /* FIXME: This is a bit of a hack, but we can't use a global, since
2494 * not everything that uses lp also uses the socket library */
2495 if (lpcfg_parm_bool(lp_ctx, NULL, "socket", "testnonblock", false)) {
2496 setenv("SOCKET_TESTNONBLOCK", "1", 1);
2497 } else {
2498 unsetenv("SOCKET_TESTNONBLOCK");
2501 TALLOC_FREE(tmp_ctx);
2502 return true;
2505 bool lpcfg_load_default(struct loadparm_context *lp_ctx)
2507 const char *path;
2509 path = lp_default_path();
2511 if (!file_exist(path)) {
2512 /* We allow the default smb.conf file to not exist,
2513 * basically the equivalent of an empty file. */
2514 return lpcfg_update(lp_ctx);
2517 return lpcfg_load(lp_ctx, path);
2521 * Load the services array from the services file.
2523 * Return True on success, False on failure.
2525 bool lpcfg_load(struct loadparm_context *lp_ctx, const char *filename)
2527 char *n2;
2528 bool bRetval;
2530 filename = talloc_strdup(lp_ctx, filename);
2532 lp_ctx->szConfigFile = filename;
2534 if (lp_ctx->s3_fns) {
2535 return lp_ctx->s3_fns->load(filename);
2538 lp_ctx->bInGlobalSection = true;
2539 n2 = standard_sub_basic(lp_ctx, lp_ctx->szConfigFile);
2540 DEBUG(2, ("lpcfg_load: refreshing parameters from %s\n", n2));
2542 add_to_file_list(lp_ctx, lp_ctx->szConfigFile, n2);
2544 /* We get sections first, so have to start 'behind' to make up */
2545 lp_ctx->currentService = NULL;
2546 bRetval = pm_process(n2, do_section, do_parameter, lp_ctx);
2548 /* finish up the last section */
2549 DEBUG(4, ("pm_process() returned %s\n", BOOLSTR(bRetval)));
2550 if (bRetval)
2551 if (lp_ctx->currentService != NULL)
2552 bRetval = lpcfg_service_ok(lp_ctx->currentService);
2554 bRetval = bRetval && lpcfg_update(lp_ctx);
2556 /* we do this unconditionally, so that it happens even
2557 for a missing smb.conf */
2558 reload_charcnv(lp_ctx);
2560 if (bRetval == true) {
2561 /* set this up so that any child python tasks will
2562 find the right smb.conf */
2563 setenv("SMB_CONF_PATH", filename, 1);
2565 /* set the context used by the lp_*() function
2566 varients */
2567 global_loadparm_context = lp_ctx;
2568 lp_ctx->loaded = true;
2571 return bRetval;
2575 * Return the max number of services.
2578 int lpcfg_numservices(struct loadparm_context *lp_ctx)
2580 if (lp_ctx->s3_fns) {
2581 return lp_ctx->s3_fns->get_numservices();
2584 return lp_ctx->iNumServices;
2588 * Display the contents of the services array in human-readable form.
2591 void lpcfg_dump(struct loadparm_context *lp_ctx, FILE *f, bool show_defaults,
2592 int maxtoprint)
2594 int iService;
2596 if (lp_ctx->s3_fns) {
2597 lp_ctx->s3_fns->dump(f, show_defaults, maxtoprint);
2598 return;
2601 defaults_saved = !show_defaults;
2603 dump_globals(lp_ctx, f, show_defaults);
2605 dump_a_service(lp_ctx->sDefault, lp_ctx->sDefault, f, lp_ctx->flags);
2607 for (iService = 0; iService < maxtoprint; iService++)
2608 lpcfg_dump_one(f, show_defaults, lp_ctx->services[iService], lp_ctx->sDefault);
2612 * Display the contents of one service in human-readable form.
2614 void lpcfg_dump_one(FILE *f, bool show_defaults, struct loadparm_service *service, struct loadparm_service *sDefault)
2616 if (service != NULL) {
2617 if (service->szService[0] == '\0')
2618 return;
2619 dump_a_service(service, sDefault, f, NULL);
2623 struct loadparm_service *lpcfg_servicebynum(struct loadparm_context *lp_ctx,
2624 int snum)
2626 if (lp_ctx->s3_fns) {
2627 return lp_ctx->s3_fns->get_servicebynum(snum);
2630 return lp_ctx->services[snum];
2633 struct loadparm_service *lpcfg_service(struct loadparm_context *lp_ctx,
2634 const char *service_name)
2636 int iService;
2637 char *serviceName;
2639 if (lp_ctx->s3_fns) {
2640 return lp_ctx->s3_fns->get_service(service_name);
2643 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--) {
2644 if (lp_ctx->services[iService] &&
2645 lp_ctx->services[iService]->szService) {
2647 * The substitution here is used to support %U is
2648 * service names
2650 serviceName = standard_sub_basic(
2651 lp_ctx->services[iService],
2652 lp_ctx->services[iService]->szService);
2653 if (strequal(serviceName, service_name)) {
2654 talloc_free(serviceName);
2655 return lp_ctx->services[iService];
2657 talloc_free(serviceName);
2661 DEBUG(7,("lpcfg_servicenumber: couldn't find %s\n", service_name));
2662 return NULL;
2665 const char *lpcfg_servicename(const struct loadparm_service *service)
2667 return lpcfg_string((const char *)service->szService);
2671 * A useful volume label function.
2673 const char *lpcfg_volume_label(struct loadparm_service *service, struct loadparm_service *sDefault)
2675 const char *ret;
2676 ret = lpcfg_string((const char *)((service != NULL && service->volume != NULL) ?
2677 service->volume : sDefault->volume));
2678 if (!*ret)
2679 return lpcfg_servicename(service);
2680 return ret;
2684 * Return the correct printer name.
2686 const char *lpcfg_printername(struct loadparm_service *service, struct loadparm_service *sDefault)
2688 const char *ret;
2689 ret = lpcfg_string((const char *)((service != NULL && service->_printername != NULL) ?
2690 service->_printername : sDefault->_printername));
2691 if (ret == NULL || (ret != NULL && *ret == '\0'))
2692 ret = lpcfg_servicename(service);
2694 return ret;
2699 * Return the max print jobs per queue.
2701 int lpcfg_maxprintjobs(struct loadparm_service *service, struct loadparm_service *sDefault)
2703 int maxjobs = (service != NULL) ? service->iMaxPrintJobs : sDefault->iMaxPrintJobs;
2704 if (maxjobs <= 0 || maxjobs >= PRINT_MAX_JOBID)
2705 maxjobs = PRINT_MAX_JOBID - 1;
2707 return maxjobs;
2710 struct smb_iconv_handle *lpcfg_iconv_handle(struct loadparm_context *lp_ctx)
2712 if (lp_ctx == NULL) {
2713 return get_iconv_handle();
2715 return lp_ctx->iconv_handle;
2718 _PUBLIC_ void reload_charcnv(struct loadparm_context *lp_ctx)
2720 struct smb_iconv_handle *old_ic = lp_ctx->iconv_handle;
2721 if (!lp_ctx->global) {
2722 return;
2725 if (old_ic == NULL) {
2726 old_ic = global_iconv_handle;
2728 lp_ctx->iconv_handle = smb_iconv_handle_reinit_lp(lp_ctx, lp_ctx, old_ic);
2729 global_iconv_handle = lp_ctx->iconv_handle;
2732 _PUBLIC_ char *lpcfg_tls_keyfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2734 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_keyfile(lp_ctx));
2737 _PUBLIC_ char *lpcfg_tls_certfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2739 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_certfile(lp_ctx));
2742 _PUBLIC_ char *lpcfg_tls_cafile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2744 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_cafile(lp_ctx));
2747 _PUBLIC_ char *lpcfg_tls_crlfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2749 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_crlfile(lp_ctx));
2752 _PUBLIC_ char *lpcfg_tls_dhpfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2754 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_dhpfile(lp_ctx));
2757 struct gensec_settings *lpcfg_gensec_settings(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2759 struct gensec_settings *settings = talloc_zero(mem_ctx, struct gensec_settings);
2760 if (settings == NULL)
2761 return NULL;
2762 SMB_ASSERT(lp_ctx != NULL);
2763 settings->lp_ctx = talloc_reference(settings, lp_ctx);
2764 settings->target_hostname = lpcfg_parm_string(lp_ctx, NULL, "gensec", "target_hostname");
2765 return settings;
2768 int lpcfg_server_role(struct loadparm_context *lp_ctx)
2770 int domain_master = lpcfg__domain_master(lp_ctx);
2772 return lp_find_server_role(lpcfg__server_role(lp_ctx),
2773 lpcfg__security(lp_ctx),
2774 lpcfg__domain_logons(lp_ctx),
2775 (domain_master == true) ||
2776 (domain_master == Auto));
2779 int lpcfg_security(struct loadparm_context *lp_ctx)
2781 return lp_find_security(lpcfg__server_role(lp_ctx),
2782 lpcfg__security(lp_ctx));
2785 bool lpcfg_server_signing_allowed(struct loadparm_context *lp_ctx, bool *mandatory)
2787 bool allowed = true;
2788 enum smb_signing_setting signing_setting = lpcfg_server_signing(lp_ctx);
2790 *mandatory = false;
2792 if (signing_setting == SMB_SIGNING_DEFAULT) {
2794 * If we are a domain controller, SMB signing is
2795 * really important, as it can prevent a number of
2796 * attacks on communications between us and the
2797 * clients
2799 * However, it really sucks (no sendfile, CPU
2800 * overhead) performance-wise when used on a
2801 * file server, so disable it by default
2802 * on non-DCs
2805 if (lpcfg_server_role(lp_ctx) >= ROLE_ACTIVE_DIRECTORY_DC) {
2806 signing_setting = SMB_SIGNING_REQUIRED;
2807 } else {
2808 signing_setting = SMB_SIGNING_OFF;
2812 switch (signing_setting) {
2813 case SMB_SIGNING_REQUIRED:
2814 *mandatory = true;
2815 break;
2816 case SMB_SIGNING_IF_REQUIRED:
2817 break;
2818 case SMB_SIGNING_DEFAULT:
2819 case SMB_SIGNING_OFF:
2820 allowed = false;
2821 break;
2824 return allowed;
2827 int lpcfg_tdb_hash_size(struct loadparm_context *lp_ctx, const char *name)
2829 const char *base;
2831 if (name == NULL) {
2832 return 0;
2835 base = strrchr_m(name, '/');
2836 if (base != NULL) {
2837 base += 1;
2838 } else {
2839 base = name;
2841 return lpcfg_parm_int(lp_ctx, NULL, "tdb_hashsize", base, 0);
2845 int lpcfg_tdb_flags(struct loadparm_context *lp_ctx, int tdb_flags)
2847 if (!lpcfg_use_mmap(lp_ctx)) {
2848 tdb_flags |= TDB_NOMMAP;
2850 return tdb_flags;