build: Update md5.h detection in waf and autoconf to use sys/md5.h and -lmd
[Samba.git] / lib / param / loadparm.c
blobfdb02c33051d12550a1cd3def7de09ffc979cdb0
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"
70 #define standard_sub_basic talloc_strdup
72 static bool do_parameter(const char *, const char *, void *);
73 static bool defaults_saved = false;
75 #define LOADPARM_EXTRA_GLOBALS \
76 struct parmlist_entry *param_opt; \
77 char *szRealm; \
78 char *szConfigFile; \
79 int iminreceivefile; \
80 char *szPrintcapname; \
81 int CupsEncrypt; \
82 int iPreferredMaster; \
83 char *szLdapMachineSuffix; \
84 char *szLdapUserSuffix; \
85 char *szLdapIdmapSuffix; \
86 char *szLdapGroupSuffix; \
87 char *szUsershareTemplateShare; \
88 char *szIdmapUID; \
89 char *szIdmapGID; \
90 char *szIdmapBackend; \
91 int winbindMaxDomainConnections; \
92 int ismb2_max_credits; \
93 char *tls_keyfile; \
94 char *tls_certfile; \
95 char *tls_cafile; \
96 char *tls_crlfile; \
97 char *tls_dhpfile; \
98 char *loglevel; \
99 char *panic_action;
101 #include "lib/param/param_global.h"
103 #define NUMPARAMETERS (sizeof(parm_table) / sizeof(struct parm_struct))
105 /* we don't need a special handler for "dos charset" and "unix charset" */
106 #define handle_dos_charset NULL
107 #define handle_charset NULL
109 /* these are parameter handlers which are not needed in the
110 * non-source3 code
112 #define handle_netbios_aliases NULL
113 #define handle_printing NULL
114 #define handle_ldap_debug_level NULL
115 #define handle_idmap_backend NULL
116 #define handle_idmap_uid NULL
117 #define handle_idmap_gid NULL
119 #ifndef N_
120 #define N_(x) x
121 #endif
123 /* prototypes for the special type handlers */
124 static bool handle_include(struct loadparm_context *lp_ctx, int unused,
125 const char *pszParmValue, char **ptr);
126 static bool handle_realm(struct loadparm_context *lp_ctx, int unused,
127 const char *pszParmValue, char **ptr);
128 static bool handle_copy(struct loadparm_context *lp_ctx, int unused,
129 const char *pszParmValue, char **ptr);
130 static bool handle_debug_list(struct loadparm_context *lp_ctx, int unused,
131 const char *pszParmValue, char **ptr);
132 static bool handle_logfile(struct loadparm_context *lp_ctx, int unused,
133 const char *pszParmValue, char **ptr);
135 #include "lib/param/param_table.c"
137 /* local variables */
138 struct loadparm_context {
139 const char *szConfigFile;
140 struct loadparm_global *globals;
141 struct loadparm_service **services;
142 struct loadparm_service *sDefault;
143 struct smb_iconv_handle *iconv_handle;
144 int iNumServices;
145 struct loadparm_service *currentService;
146 bool bInGlobalSection;
147 struct file_lists {
148 struct file_lists *next;
149 char *name;
150 char *subfname;
151 time_t modtime;
152 } *file_lists;
153 unsigned int flags[NUMPARAMETERS];
154 bool loaded;
155 bool refuse_free;
156 bool global; /* Is this the global context, which may set
157 * global variables such as debug level etc? */
158 const struct loadparm_s3_helpers *s3_fns;
162 struct loadparm_service *lpcfg_default_service(struct loadparm_context *lp_ctx)
164 if (lp_ctx->s3_fns) {
165 return lp_ctx->s3_fns->get_default_loadparm_service();
167 return lp_ctx->sDefault;
171 * Convenience routine to grab string parameters into temporary memory
172 * and run standard_sub_basic on them.
174 * The buffers can be written to by
175 * callers without affecting the source string.
178 static const char *lp_string(const char *s)
180 #if 0 /* until REWRITE done to make thread-safe */
181 size_t len = s ? strlen(s) : 0;
182 char *ret;
183 #endif
185 /* The follow debug is useful for tracking down memory problems
186 especially if you have an inner loop that is calling a lp_*()
187 function that returns a string. Perhaps this debug should be
188 present all the time? */
190 #if 0
191 DEBUG(10, ("lp_string(%s)\n", s));
192 #endif
194 #if 0 /* until REWRITE done to make thread-safe */
195 if (!lp_talloc)
196 lp_talloc = talloc_init("lp_talloc");
198 ret = talloc_array(lp_talloc, char, len + 100); /* leave room for substitution */
200 if (!ret)
201 return NULL;
203 if (!s)
204 *ret = 0;
205 else
206 strlcpy(ret, s, len);
208 if (trim_string(ret, "\"", "\"")) {
209 if (strchr(ret,'"') != NULL)
210 strlcpy(ret, s, len);
213 standard_sub_basic(ret,len+100);
214 return (ret);
215 #endif
216 return s;
220 In this section all the functions that are used to access the
221 parameters from the rest of the program are defined
225 * the creation of separate lpcfg_*() and lp_*() functions is to allow
226 * for code compatibility between existing Samba4 and Samba3 code.
229 /* this global context supports the lp_*() function varients */
230 static struct loadparm_context *global_loadparm_context;
232 #define lpcfg_default_service global_loadparm_context->sDefault
233 #define lpcfg_global_service(i) global_loadparm_context->services[i]
235 #define FN_GLOBAL_STRING(fn_name,var_name) \
236 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
237 if (lp_ctx == NULL) return NULL; \
238 if (lp_ctx->s3_fns) { \
239 SMB_ASSERT(lp_ctx->s3_fns->fn_name); \
240 return lp_ctx->s3_fns->fn_name(); \
242 return lp_ctx->globals->var_name ? lp_string(lp_ctx->globals->var_name) : ""; \
245 #define FN_GLOBAL_CONST_STRING(fn_name,var_name) \
246 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) {\
247 if (lp_ctx == NULL) return NULL; \
248 if (lp_ctx->s3_fns) { \
249 SMB_ASSERT(lp_ctx->s3_fns->fn_name); \
250 return lp_ctx->s3_fns->fn_name(); \
252 return lp_ctx->globals->var_name ? lp_string(lp_ctx->globals->var_name) : ""; \
255 #define FN_GLOBAL_LIST(fn_name,var_name) \
256 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
257 if (lp_ctx == NULL) return NULL; \
258 if (lp_ctx->s3_fns) { \
259 SMB_ASSERT(lp_ctx->s3_fns->fn_name); \
260 return lp_ctx->s3_fns->fn_name(); \
262 return lp_ctx->globals->var_name; \
265 #define FN_GLOBAL_BOOL(fn_name,var_name) \
266 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) {\
267 if (lp_ctx == NULL) return false; \
268 if (lp_ctx->s3_fns) { \
269 SMB_ASSERT(lp_ctx->s3_fns->fn_name); \
270 return lp_ctx->s3_fns->fn_name(); \
272 return lp_ctx->globals->var_name; \
275 #define FN_GLOBAL_INTEGER(fn_name,var_name) \
276 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
277 if (lp_ctx->s3_fns) { \
278 SMB_ASSERT(lp_ctx->s3_fns->fn_name); \
279 return lp_ctx->s3_fns->fn_name(); \
281 return lp_ctx->globals->var_name; \
284 /* Local parameters don't need the ->s3_fns because the struct
285 * loadparm_service is shared and lpcfg_service() checks the ->s3_fns
286 * hook */
287 #define FN_LOCAL_STRING(fn_name,val) \
288 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_service *service, \
289 struct loadparm_service *sDefault) { \
290 return(lp_string((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val))); \
293 #define FN_LOCAL_CONST_STRING(fn_name,val) FN_LOCAL_STRING(fn_name, val)
295 #define FN_LOCAL_LIST(fn_name,val) \
296 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_service *service, \
297 struct loadparm_service *sDefault) {\
298 return(const char **)(service != NULL && service->val != NULL? service->val : sDefault->val); \
301 #define FN_LOCAL_PARM_BOOL(fn_name, val) FN_LOCAL_BOOL(fn_name, val)
303 #define FN_LOCAL_BOOL(fn_name,val) \
304 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_service *service, \
305 struct loadparm_service *sDefault) { \
306 return((service != NULL)? service->val : sDefault->val); \
309 #define FN_LOCAL_INTEGER(fn_name,val) \
310 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_service *service, \
311 struct loadparm_service *sDefault) { \
312 return((service != NULL)? service->val : sDefault->val); \
315 #define FN_LOCAL_PARM_INTEGER(fn_name, val) FN_LOCAL_INTEGER(fn_name, val)
317 #define FN_LOCAL_CHAR(fn_name,val) \
318 _PUBLIC_ char lpcfg_ ## fn_name(struct loadparm_service *service, \
319 struct loadparm_service *sDefault) { \
320 return((service != NULL)? service->val : sDefault->val); \
323 #include "lib/param/param_functions.c"
325 /* These functions remain only in lib/param for now */
326 FN_GLOBAL_BOOL(readraw, bReadRaw)
327 FN_GLOBAL_BOOL(writeraw, bWriteRaw)
328 FN_GLOBAL_CONST_STRING(cachedir, szCacheDir)
329 FN_GLOBAL_CONST_STRING(statedir, szStateDir)
331 /* local prototypes */
332 static int map_parameter(const char *pszParmName);
333 static struct loadparm_service *getservicebyname(struct loadparm_context *lp_ctx,
334 const char *pszServiceName);
335 static void copy_service(struct loadparm_service *pserviceDest,
336 struct loadparm_service *pserviceSource,
337 struct bitmap *pcopymapDest);
338 static bool lpcfg_service_ok(struct loadparm_service *service);
339 static bool do_section(const char *pszSectionName, void *);
340 static void init_copymap(struct loadparm_service *pservice);
342 /* This is a helper function for parametrical options support. */
343 /* It returns a pointer to parametrical option value if it exists or NULL otherwise */
344 /* Actual parametrical functions are quite simple */
345 const char *lpcfg_get_parametric(struct loadparm_context *lp_ctx,
346 struct loadparm_service *service,
347 const char *type, const char *option)
349 char *vfskey_tmp = NULL;
350 char *vfskey = NULL;
351 struct parmlist_entry *data;
353 if (lp_ctx == NULL)
354 return NULL;
356 if (lp_ctx->s3_fns) {
357 return lp_ctx->s3_fns->get_parametric(service, type, option);
360 data = (service == NULL ? lp_ctx->globals->param_opt : service->param_opt);
362 vfskey_tmp = talloc_asprintf(NULL, "%s:%s", type, option);
363 if (vfskey_tmp == NULL) return NULL;
364 vfskey = strlower_talloc(NULL, vfskey_tmp);
365 talloc_free(vfskey_tmp);
367 while (data) {
368 if (strcmp(data->key, vfskey) == 0) {
369 talloc_free(vfskey);
370 return data->value;
372 data = data->next;
375 if (service != NULL) {
376 /* Try to fetch the same option but from globals */
377 /* but only if we are not already working with globals */
378 for (data = lp_ctx->globals->param_opt; data;
379 data = data->next) {
380 if (strcmp(data->key, vfskey) == 0) {
381 talloc_free(vfskey);
382 return data->value;
387 talloc_free(vfskey);
389 return NULL;
394 * convenience routine to return int parameters.
396 static int lp_int(const char *s)
399 if (!s) {
400 DEBUG(0,("lp_int(%s): is called with NULL!\n",s));
401 return -1;
404 return strtol(s, NULL, 0);
408 * convenience routine to return unsigned long parameters.
410 static unsigned long lp_ulong(const char *s)
413 if (!s) {
414 DEBUG(0,("lp_ulong(%s): is called with NULL!\n",s));
415 return -1;
418 return strtoul(s, NULL, 0);
422 * convenience routine to return unsigned long parameters.
424 static long lp_long(const char *s)
427 if (!s) {
428 DEBUG(0,("lp_long(%s): is called with NULL!\n",s));
429 return -1;
432 return strtol(s, NULL, 0);
436 * convenience routine to return unsigned long parameters.
438 static double lp_double(const char *s)
441 if (!s) {
442 DEBUG(0,("lp_double(%s): is called with NULL!\n",s));
443 return -1;
446 return strtod(s, NULL);
450 * convenience routine to return boolean parameters.
452 static bool lp_bool(const char *s)
454 bool ret = false;
456 if (!s) {
457 DEBUG(0,("lp_bool(%s): is called with NULL!\n",s));
458 return false;
461 if (!set_boolean(s, &ret)) {
462 DEBUG(0,("lp_bool(%s): value is not boolean!\n",s));
463 return false;
466 return ret;
471 * Return parametric option from a given service. Type is a part of option before ':'
472 * Parametric option has following syntax: 'Type: option = value'
473 * Returned value is allocated in 'lp_talloc' context
476 const char *lpcfg_parm_string(struct loadparm_context *lp_ctx,
477 struct loadparm_service *service, const char *type,
478 const char *option)
480 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
482 if (value)
483 return lp_string(value);
485 return NULL;
489 * Return parametric option from a given service. Type is a part of option before ':'
490 * Parametric option has following syntax: 'Type: option = value'
491 * Returned value is allocated in 'lp_talloc' context
494 const char **lpcfg_parm_string_list(TALLOC_CTX *mem_ctx,
495 struct loadparm_context *lp_ctx,
496 struct loadparm_service *service,
497 const char *type,
498 const char *option, const char *separator)
500 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
502 if (value != NULL)
503 return (const char **)str_list_make(mem_ctx, value, separator);
505 return NULL;
509 * Return parametric option from a given service. Type is a part of option before ':'
510 * Parametric option has following syntax: 'Type: option = value'
513 int lpcfg_parm_int(struct loadparm_context *lp_ctx,
514 struct loadparm_service *service, const char *type,
515 const char *option, int default_v)
517 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
519 if (value)
520 return lp_int(value);
522 return default_v;
526 * Return parametric option from a given service. Type is a part of
527 * option before ':'.
528 * Parametric option has following syntax: 'Type: option = value'.
531 int lpcfg_parm_bytes(struct loadparm_context *lp_ctx,
532 struct loadparm_service *service, const char *type,
533 const char *option, int default_v)
535 uint64_t bval;
537 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
539 if (value && conv_str_size_error(value, &bval)) {
540 if (bval <= INT_MAX) {
541 return (int)bval;
545 return default_v;
549 * Return parametric option from a given service.
550 * Type is a part of option before ':'
551 * Parametric option has following syntax: 'Type: option = value'
553 unsigned long lpcfg_parm_ulong(struct loadparm_context *lp_ctx,
554 struct loadparm_service *service, const char *type,
555 const char *option, unsigned long default_v)
557 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
559 if (value)
560 return lp_ulong(value);
562 return default_v;
565 long lpcfg_parm_long(struct loadparm_context *lp_ctx,
566 struct loadparm_service *service, const char *type,
567 const char *option, long default_v)
569 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
571 if (value)
572 return lp_long(value);
574 return default_v;
577 double lpcfg_parm_double(struct loadparm_context *lp_ctx,
578 struct loadparm_service *service, const char *type,
579 const char *option, double default_v)
581 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
583 if (value != NULL)
584 return lp_double(value);
586 return default_v;
590 * Return parametric option from a given service. Type is a part of option before ':'
591 * Parametric option has following syntax: 'Type: option = value'
594 bool lpcfg_parm_bool(struct loadparm_context *lp_ctx,
595 struct loadparm_service *service, const char *type,
596 const char *option, bool default_v)
598 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
600 if (value != NULL)
601 return lp_bool(value);
603 return default_v;
608 * Initialise a service to the defaults.
611 static struct loadparm_service *init_service(TALLOC_CTX *mem_ctx, struct loadparm_service *sDefault)
613 struct loadparm_service *pservice =
614 talloc_zero(mem_ctx, struct loadparm_service);
615 copy_service(pservice, sDefault, NULL);
616 return pservice;
620 * Set a string value, deallocating any existing space, and allocing the space
621 * for the string
623 static bool lpcfg_string_set(TALLOC_CTX *mem_ctx, char **dest, const char *src)
625 talloc_free(*dest);
627 if (src == NULL)
628 src = "";
630 *dest = talloc_strdup(mem_ctx, src);
631 if ((*dest) == NULL) {
632 DEBUG(0,("Out of memory in string_set\n"));
633 return false;
636 return true;
640 * Set a string value, deallocating any existing space, and allocing the space
641 * for the string
643 static bool lpcfg_string_set_upper(TALLOC_CTX *mem_ctx, char **dest, const char *src)
645 talloc_free(*dest);
647 if (src == NULL)
648 src = "";
650 *dest = strupper_talloc(mem_ctx, src);
651 if ((*dest) == NULL) {
652 DEBUG(0,("Out of memory in string_set_upper\n"));
653 return false;
656 return true;
662 * Add a new service to the services array initialising it with the given
663 * service.
666 struct loadparm_service *lpcfg_add_service(struct loadparm_context *lp_ctx,
667 const struct loadparm_service *pservice,
668 const char *name)
670 int i;
671 struct loadparm_service tservice;
672 int num_to_alloc = lp_ctx->iNumServices + 1;
673 struct parmlist_entry *data, *pdata;
675 if (pservice == NULL) {
676 pservice = lp_ctx->sDefault;
679 tservice = *pservice;
681 /* it might already exist */
682 if (name) {
683 struct loadparm_service *service = getservicebyname(lp_ctx,
684 name);
685 if (service != NULL) {
686 /* Clean all parametric options for service */
687 /* They will be added during parsing again */
688 data = service->param_opt;
689 while (data) {
690 pdata = data->next;
691 talloc_free(data);
692 data = pdata;
694 service->param_opt = NULL;
695 return service;
699 /* find an invalid one */
700 for (i = 0; i < lp_ctx->iNumServices; i++)
701 if (lp_ctx->services[i] == NULL)
702 break;
704 /* if not, then create one */
705 if (i == lp_ctx->iNumServices) {
706 struct loadparm_service **tsp;
708 tsp = talloc_realloc(lp_ctx, lp_ctx->services, struct loadparm_service *, num_to_alloc);
710 if (!tsp) {
711 DEBUG(0,("lpcfg_add_service: failed to enlarge services!\n"));
712 return NULL;
713 } else {
714 lp_ctx->services = tsp;
715 lp_ctx->services[lp_ctx->iNumServices] = NULL;
718 lp_ctx->iNumServices++;
721 lp_ctx->services[i] = init_service(lp_ctx->services, lp_ctx->sDefault);
722 if (lp_ctx->services[i] == NULL) {
723 DEBUG(0,("lpcfg_add_service: out of memory!\n"));
724 return NULL;
726 copy_service(lp_ctx->services[i], &tservice, NULL);
727 if (name != NULL)
728 lpcfg_string_set(lp_ctx->services[i], &lp_ctx->services[i]->szService, name);
729 return lp_ctx->services[i];
733 * Add a new home service, with the specified home directory, defaults coming
734 * from service ifrom.
737 bool lpcfg_add_home(struct loadparm_context *lp_ctx,
738 const char *pszHomename,
739 struct loadparm_service *default_service,
740 const char *user, const char *pszHomedir)
742 struct loadparm_service *service;
744 service = lpcfg_add_service(lp_ctx, default_service, pszHomename);
746 if (service == NULL)
747 return false;
749 if (!(*(default_service->szPath))
750 || strequal(default_service->szPath, lp_ctx->sDefault->szPath)) {
751 service->szPath = talloc_strdup(service, pszHomedir);
752 } else {
753 service->szPath = string_sub_talloc(service, lpcfg_pathname(default_service, lp_ctx->sDefault), "%H", pszHomedir);
756 if (!(*(service->comment))) {
757 service->comment = talloc_asprintf(service, "Home directory of %s", user);
759 service->bAvailable = default_service->bAvailable;
760 service->bBrowseable = default_service->bBrowseable;
762 DEBUG(3, ("adding home's share [%s] for user '%s' at '%s'\n",
763 pszHomename, user, service->szPath));
765 return true;
769 * Add a new printer service, with defaults coming from service iFrom.
772 bool lpcfg_add_printer(struct loadparm_context *lp_ctx,
773 const char *pszPrintername,
774 struct loadparm_service *default_service)
776 const char *comment = "From Printcap";
777 struct loadparm_service *service;
778 service = lpcfg_add_service(lp_ctx, default_service, pszPrintername);
780 if (service == NULL)
781 return false;
783 /* note that we do NOT default the availability flag to True - */
784 /* we take it from the default service passed. This allows all */
785 /* dynamic printers to be disabled by disabling the [printers] */
786 /* entry (if/when the 'available' keyword is implemented!). */
788 /* the printer name is set to the service name. */
789 lpcfg_string_set(service, &service->szPrintername, pszPrintername);
790 lpcfg_string_set(service, &service->comment, comment);
791 service->bBrowseable = default_service->bBrowseable;
792 /* Printers cannot be read_only. */
793 service->bRead_only = false;
794 /* Printer services must be printable. */
795 service->bPrint_ok = true;
797 DEBUG(3, ("adding printer service %s\n", pszPrintername));
799 return true;
803 * Map a parameter's string representation to something we can use.
804 * Returns False if the parameter string is not recognised, else TRUE.
807 static int map_parameter(const char *pszParmName)
809 int iIndex;
811 if (*pszParmName == '-')
812 return -1;
814 for (iIndex = 0; parm_table[iIndex].label; iIndex++)
815 if (strwicmp(parm_table[iIndex].label, pszParmName) == 0)
816 return iIndex;
818 /* Warn only if it isn't parametric option */
819 if (strchr(pszParmName, ':') == NULL)
820 DEBUG(0, ("Unknown parameter encountered: \"%s\"\n", pszParmName));
821 /* We do return 'fail' for parametric options as well because they are
822 stored in different storage
824 return -1;
829 return the parameter structure for a parameter
831 struct parm_struct *lpcfg_parm_struct(struct loadparm_context *lp_ctx, const char *name)
833 int parmnum;
835 if (lp_ctx->s3_fns) {
836 return lp_ctx->s3_fns->get_parm_struct(name);
839 parmnum = map_parameter(name);
840 if (parmnum == -1) return NULL;
841 return &parm_table[parmnum];
845 return the parameter pointer for a parameter
847 void *lpcfg_parm_ptr(struct loadparm_context *lp_ctx,
848 struct loadparm_service *service, struct parm_struct *parm)
850 if (lp_ctx->s3_fns) {
851 return lp_ctx->s3_fns->get_parm_ptr(service, parm);
854 if (service == NULL) {
855 if (parm->p_class == P_LOCAL)
856 return ((char *)lp_ctx->sDefault)+parm->offset;
857 else if (parm->p_class == P_GLOBAL)
858 return ((char *)lp_ctx->globals)+parm->offset;
859 else return NULL;
860 } else {
861 return ((char *)service) + parm->offset;
866 return the parameter pointer for a parameter
868 bool lpcfg_parm_is_cmdline(struct loadparm_context *lp_ctx, const char *name)
870 int parmnum;
872 if (lp_ctx->s3_fns) {
873 struct parm_struct *parm = lp_ctx->s3_fns->get_parm_struct(name);
874 if (parm) {
875 return parm->flags & FLAG_CMDLINE;
877 return false;
880 parmnum = map_parameter(name);
881 if (parmnum == -1) return false;
883 return lp_ctx->flags[parmnum] & FLAG_CMDLINE;
887 * Find a service by name. Otherwise works like get_service.
890 static struct loadparm_service *getservicebyname(struct loadparm_context *lp_ctx,
891 const char *pszServiceName)
893 int iService;
895 if (lp_ctx->s3_fns) {
896 return lp_ctx->s3_fns->get_service(pszServiceName);
899 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--)
900 if (lp_ctx->services[iService] != NULL &&
901 strwicmp(lp_ctx->services[iService]->szService, pszServiceName) == 0) {
902 return lp_ctx->services[iService];
905 return NULL;
909 * Copy a service structure to another.
910 * If pcopymapDest is NULL then copy all fields
913 static void copy_service(struct loadparm_service *pserviceDest,
914 struct loadparm_service *pserviceSource,
915 struct bitmap *pcopymapDest)
917 int i;
918 bool bcopyall = (pcopymapDest == NULL);
919 struct parmlist_entry *data, *pdata, *paramo;
920 bool not_added;
922 for (i = 0; parm_table[i].label; i++)
923 if (parm_table[i].p_class == P_LOCAL &&
924 (bcopyall || bitmap_query(pcopymapDest, i))) {
925 void *src_ptr =
926 ((char *)pserviceSource) + parm_table[i].offset;
927 void *dest_ptr =
928 ((char *)pserviceDest) + parm_table[i].offset;
930 switch (parm_table[i].type) {
931 case P_BOOL:
932 *(bool *)dest_ptr = *(bool *)src_ptr;
933 break;
935 case P_INTEGER:
936 case P_BYTES:
937 case P_OCTAL:
938 case P_ENUM:
939 *(int *)dest_ptr = *(int *)src_ptr;
940 break;
942 case P_STRING:
943 lpcfg_string_set(pserviceDest,
944 (char **)dest_ptr,
945 *(char **)src_ptr);
946 break;
948 case P_USTRING:
949 lpcfg_string_set_upper(pserviceDest,
950 (char **)dest_ptr,
951 *(char **)src_ptr);
952 break;
953 case P_LIST:
954 *(const char ***)dest_ptr = (const char **)str_list_copy(pserviceDest,
955 *(const char ***)src_ptr);
956 break;
957 default:
958 break;
962 if (bcopyall) {
963 init_copymap(pserviceDest);
964 if (pserviceSource->copymap)
965 bitmap_copy(pserviceDest->copymap,
966 pserviceSource->copymap);
969 data = pserviceSource->param_opt;
970 while (data) {
971 not_added = true;
972 pdata = pserviceDest->param_opt;
973 /* Traverse destination */
974 while (pdata) {
975 /* If we already have same option, override it */
976 if (strcmp(pdata->key, data->key) == 0) {
977 talloc_free(pdata->value);
978 pdata->value = talloc_strdup(pdata,
979 data->value);
980 not_added = false;
981 break;
983 pdata = pdata->next;
985 if (not_added) {
986 paramo = talloc_zero(pserviceDest, struct parmlist_entry);
987 if (paramo == NULL)
988 smb_panic("OOM");
989 paramo->key = talloc_strdup(paramo, data->key);
990 paramo->value = talloc_strdup(paramo, data->value);
991 DLIST_ADD(pserviceDest->param_opt, paramo);
993 data = data->next;
998 * Check a service for consistency. Return False if the service is in any way
999 * incomplete or faulty, else True.
1001 static bool lpcfg_service_ok(struct loadparm_service *service)
1003 bool bRetval;
1005 bRetval = true;
1006 if (service->szService[0] == '\0') {
1007 DEBUG(0, ("The following message indicates an internal error:\n"));
1008 DEBUG(0, ("No service name in service entry.\n"));
1009 bRetval = false;
1012 /* The [printers] entry MUST be printable. I'm all for flexibility, but */
1013 /* I can't see why you'd want a non-printable printer service... */
1014 if (strwicmp(service->szService, PRINTERS_NAME) == 0) {
1015 if (!service->bPrint_ok) {
1016 DEBUG(0, ("WARNING: [%s] service MUST be printable!\n",
1017 service->szService));
1018 service->bPrint_ok = true;
1020 /* [printers] service must also be non-browsable. */
1021 if (service->bBrowseable)
1022 service->bBrowseable = false;
1025 /* If a service is flagged unavailable, log the fact at level 0. */
1026 if (!service->bAvailable)
1027 DEBUG(1, ("NOTE: Service %s is flagged unavailable.\n",
1028 service->szService));
1030 return bRetval;
1034 /*******************************************************************
1035 Keep a linked list of all config files so we know when one has changed
1036 it's date and needs to be reloaded.
1037 ********************************************************************/
1039 static void add_to_file_list(struct loadparm_context *lp_ctx,
1040 const char *fname, const char *subfname)
1042 struct file_lists *f = lp_ctx->file_lists;
1044 while (f) {
1045 if (f->name && !strcmp(f->name, fname))
1046 break;
1047 f = f->next;
1050 if (!f) {
1051 f = talloc(lp_ctx, struct file_lists);
1052 if (!f)
1053 return;
1054 f->next = lp_ctx->file_lists;
1055 f->name = talloc_strdup(f, fname);
1056 if (!f->name) {
1057 talloc_free(f);
1058 return;
1060 f->subfname = talloc_strdup(f, subfname);
1061 if (!f->subfname) {
1062 talloc_free(f);
1063 return;
1065 lp_ctx->file_lists = f;
1066 f->modtime = file_modtime(subfname);
1067 } else {
1068 time_t t = file_modtime(subfname);
1069 if (t)
1070 f->modtime = t;
1074 /*******************************************************************
1075 Check if a config file has changed date.
1076 ********************************************************************/
1077 bool lpcfg_file_list_changed(struct loadparm_context *lp_ctx)
1079 struct file_lists *f;
1080 DEBUG(6, ("lpcfg_file_list_changed()\n"));
1082 for (f = lp_ctx->file_lists; f != NULL; f = f->next) {
1083 char *n2;
1084 time_t mod_time;
1086 n2 = standard_sub_basic(lp_ctx, f->name);
1088 DEBUGADD(6, ("file %s -> %s last mod_time: %s\n",
1089 f->name, n2, ctime(&f->modtime)));
1091 mod_time = file_modtime(n2);
1093 if (mod_time && ((f->modtime != mod_time) || (f->subfname == NULL) || (strcmp(n2, f->subfname) != 0))) {
1094 DEBUGADD(6, ("file %s modified: %s\n", n2,
1095 ctime(&mod_time)));
1096 f->modtime = mod_time;
1097 talloc_free(f->subfname);
1098 f->subfname = talloc_strdup(f, n2);
1099 return true;
1102 return false;
1105 /***************************************************************************
1106 Handle the "realm" parameter
1107 ***************************************************************************/
1109 static bool handle_realm(struct loadparm_context *lp_ctx, int unused,
1110 const char *pszParmValue, char **ptr)
1112 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1114 talloc_free(lp_ctx->globals->szRealm_upper);
1115 talloc_free(lp_ctx->globals->szRealm_lower);
1117 lp_ctx->globals->szRealm_upper = strupper_talloc(lp_ctx, pszParmValue);
1118 lp_ctx->globals->szRealm_lower = strlower_talloc(lp_ctx, pszParmValue);
1120 return true;
1123 /***************************************************************************
1124 Handle the include operation.
1125 ***************************************************************************/
1127 static bool handle_include(struct loadparm_context *lp_ctx, int unused,
1128 const char *pszParmValue, char **ptr)
1130 char *fname = standard_sub_basic(lp_ctx, pszParmValue);
1132 add_to_file_list(lp_ctx, pszParmValue, fname);
1134 lpcfg_string_set(lp_ctx, ptr, fname);
1136 if (file_exist(fname))
1137 return pm_process(fname, do_section, do_parameter, lp_ctx);
1139 DEBUG(2, ("Can't find include file %s\n", fname));
1141 return false;
1144 /***************************************************************************
1145 Handle the interpretation of the copy parameter.
1146 ***************************************************************************/
1148 static bool handle_copy(struct loadparm_context *lp_ctx, int unused,
1149 const char *pszParmValue, char **ptr)
1151 bool bRetval;
1152 struct loadparm_service *serviceTemp;
1154 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1156 bRetval = false;
1158 DEBUG(3, ("Copying service from service %s\n", pszParmValue));
1160 if ((serviceTemp = getservicebyname(lp_ctx, pszParmValue)) != NULL) {
1161 if (serviceTemp == lp_ctx->currentService) {
1162 DEBUG(0, ("Can't copy service %s - unable to copy self!\n", pszParmValue));
1163 } else {
1164 copy_service(lp_ctx->currentService,
1165 serviceTemp,
1166 lp_ctx->currentService->copymap);
1167 bRetval = true;
1169 } else {
1170 DEBUG(0, ("Unable to copy service - source not found: %s\n",
1171 pszParmValue));
1172 bRetval = false;
1175 return bRetval;
1178 static bool handle_debug_list(struct loadparm_context *lp_ctx, int unused,
1179 const char *pszParmValue, char **ptr)
1182 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1183 if (lp_ctx->global) {
1184 return debug_parse_levels(pszParmValue);
1186 return true;
1189 static bool handle_logfile(struct loadparm_context *lp_ctx, int unused,
1190 const char *pszParmValue, char **ptr)
1192 debug_set_logfile(pszParmValue);
1193 if (lp_ctx->global) {
1194 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
1196 return true;
1199 /***************************************************************************
1200 Initialise a copymap.
1201 ***************************************************************************/
1203 static void init_copymap(struct loadparm_service *pservice)
1205 int i;
1207 TALLOC_FREE(pservice->copymap);
1209 pservice->copymap = bitmap_talloc(NULL, NUMPARAMETERS);
1210 if (!pservice->copymap)
1211 DEBUG(0,
1212 ("Couldn't allocate copymap!! (size %d)\n",
1213 (int)NUMPARAMETERS));
1214 else
1215 for (i = 0; i < NUMPARAMETERS; i++)
1216 bitmap_set(pservice->copymap, i);
1220 * Process a parametric option
1222 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
1223 struct loadparm_service *service,
1224 const char *pszParmName,
1225 const char *pszParmValue, int flags)
1227 struct parmlist_entry *paramo, *data;
1228 char *name;
1229 TALLOC_CTX *mem_ctx;
1231 while (isspace((unsigned char)*pszParmName)) {
1232 pszParmName++;
1235 name = strlower_talloc(lp_ctx, pszParmName);
1236 if (!name) return false;
1238 if (service == NULL) {
1239 data = lp_ctx->globals->param_opt;
1240 mem_ctx = lp_ctx->globals;
1241 } else {
1242 data = service->param_opt;
1243 mem_ctx = service;
1246 /* Traverse destination */
1247 for (paramo=data; paramo; paramo=paramo->next) {
1248 /* If we already have the option set, override it unless
1249 it was a command line option and the new one isn't */
1250 if (strcmp(paramo->key, name) == 0) {
1251 if ((paramo->priority & FLAG_CMDLINE) &&
1252 !(flags & FLAG_CMDLINE)) {
1253 talloc_free(name);
1254 return true;
1257 talloc_free(paramo->value);
1258 paramo->value = talloc_strdup(paramo, pszParmValue);
1259 paramo->priority = flags;
1260 talloc_free(name);
1261 return true;
1265 paramo = talloc_zero(mem_ctx, struct parmlist_entry);
1266 if (!paramo)
1267 smb_panic("OOM");
1268 paramo->key = talloc_strdup(paramo, name);
1269 paramo->value = talloc_strdup(paramo, pszParmValue);
1270 paramo->priority = flags;
1271 if (service == NULL) {
1272 DLIST_ADD(lp_ctx->globals->param_opt, paramo);
1273 } else {
1274 DLIST_ADD(service->param_opt, paramo);
1277 talloc_free(name);
1279 return true;
1282 static bool set_variable(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
1283 const char *pszParmName, const char *pszParmValue,
1284 struct loadparm_context *lp_ctx, bool on_globals)
1286 int i;
1287 /* if it is a special case then go ahead */
1288 if (parm_table[parmnum].special) {
1289 bool ret;
1290 ret = parm_table[parmnum].special(lp_ctx, -1, pszParmValue,
1291 (char **)parm_ptr);
1292 if (!ret) {
1293 return false;
1295 goto mark_non_default;
1298 /* now switch on the type of variable it is */
1299 switch (parm_table[parmnum].type)
1301 case P_BOOL: {
1302 bool b;
1303 if (!set_boolean(pszParmValue, &b)) {
1304 DEBUG(0, ("set_variable(%s): value is not "
1305 "boolean!\n", pszParmValue));
1306 return false;
1308 *(bool *)parm_ptr = b;
1310 break;
1312 case P_BOOLREV: {
1313 bool b;
1314 if (!set_boolean(pszParmValue, &b)) {
1315 DEBUG(0, ("set_variable(%s): value is not "
1316 "boolean!\n", pszParmValue));
1317 return false;
1319 *(bool *)parm_ptr = !b;
1321 break;
1323 case P_INTEGER:
1324 *(int *)parm_ptr = atoi(pszParmValue);
1325 break;
1327 case P_CHAR:
1328 *(char *)parm_ptr = *pszParmValue;
1329 break;
1331 case P_OCTAL:
1332 *(int *)parm_ptr = strtol(pszParmValue, NULL, 8);
1333 break;
1335 case P_BYTES:
1337 uint64_t val;
1338 if (conv_str_size_error(pszParmValue, &val)) {
1339 if (val <= INT_MAX) {
1340 *(int *)parm_ptr = (int)val;
1341 break;
1345 DEBUG(0, ("set_variable(%s): value is not "
1346 "a valid size specifier!\n", pszParmValue));
1347 return false;
1350 case P_CMDLIST:
1351 *(const char ***)parm_ptr = (const char **)str_list_make(mem_ctx,
1352 pszParmValue, NULL);
1353 break;
1354 case P_LIST:
1356 char **new_list = str_list_make(mem_ctx,
1357 pszParmValue, NULL);
1358 for (i=0; new_list[i]; i++) {
1359 if (*(const char ***)parm_ptr != NULL &&
1360 new_list[i][0] == '+' &&
1361 new_list[i][1])
1363 if (!str_list_check(*(const char ***)parm_ptr,
1364 &new_list[i][1])) {
1365 *(const char ***)parm_ptr = str_list_add(*(const char ***)parm_ptr,
1366 &new_list[i][1]);
1368 } else if (*(const char ***)parm_ptr != NULL &&
1369 new_list[i][0] == '-' &&
1370 new_list[i][1])
1372 str_list_remove(*(const char ***)parm_ptr,
1373 &new_list[i][1]);
1374 } else {
1375 if (i != 0) {
1376 DEBUG(0, ("Unsupported list syntax for: %s = %s\n",
1377 pszParmName, pszParmValue));
1378 return false;
1380 *(const char ***)parm_ptr = (const char **) new_list;
1381 break;
1384 break;
1386 case P_STRING:
1387 lpcfg_string_set(mem_ctx, (char **)parm_ptr, pszParmValue);
1388 break;
1390 case P_USTRING:
1391 lpcfg_string_set_upper(mem_ctx, (char **)parm_ptr, pszParmValue);
1392 break;
1394 case P_ENUM:
1395 for (i = 0; parm_table[parmnum].enum_list[i].name; i++) {
1396 if (strequal
1397 (pszParmValue,
1398 parm_table[parmnum].enum_list[i].name)) {
1399 *(int *)parm_ptr =
1400 parm_table[parmnum].
1401 enum_list[i].value;
1402 break;
1405 if (!parm_table[parmnum].enum_list[i].name) {
1406 DEBUG(0,("Unknown enumerated value '%s' for '%s'\n",
1407 pszParmValue, pszParmName));
1408 return false;
1410 break;
1412 case P_SEP:
1413 break;
1416 mark_non_default:
1417 if (on_globals && (lp_ctx->flags[parmnum] & FLAG_DEFAULT)) {
1418 lp_ctx->flags[parmnum] &= ~FLAG_DEFAULT;
1419 /* we have to also unset FLAG_DEFAULT on aliases */
1420 for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
1421 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1423 for (i=parmnum+1;i<NUMPARAMETERS && parm_table[i].offset == parm_table[parmnum].offset;i++) {
1424 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1427 return true;
1431 bool lpcfg_do_global_parameter(struct loadparm_context *lp_ctx,
1432 const char *pszParmName, const char *pszParmValue)
1434 int parmnum = map_parameter(pszParmName);
1435 void *parm_ptr;
1437 if (parmnum < 0) {
1438 if (strchr(pszParmName, ':')) {
1439 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName, pszParmValue, 0);
1441 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1442 return true;
1445 /* if the flag has been set on the command line, then don't allow override,
1446 but don't report an error */
1447 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1448 return true;
1451 parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[parmnum]);
1453 return set_variable(lp_ctx->globals, parmnum, parm_ptr,
1454 pszParmName, pszParmValue, lp_ctx, true);
1457 bool lpcfg_do_service_parameter(struct loadparm_context *lp_ctx,
1458 struct loadparm_service *service,
1459 const char *pszParmName, const char *pszParmValue)
1461 void *parm_ptr;
1462 int i;
1463 int parmnum = map_parameter(pszParmName);
1465 if (parmnum < 0) {
1466 if (strchr(pszParmName, ':')) {
1467 return lp_do_parameter_parametric(lp_ctx, service, pszParmName, pszParmValue, 0);
1469 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1470 return true;
1473 /* if the flag has been set on the command line, then don't allow override,
1474 but don't report an error */
1475 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1476 return true;
1479 if (parm_table[parmnum].p_class == P_GLOBAL) {
1480 DEBUG(0,
1481 ("Global parameter %s found in service section!\n",
1482 pszParmName));
1483 return true;
1485 parm_ptr = ((char *)service) + parm_table[parmnum].offset;
1487 if (!service->copymap)
1488 init_copymap(service);
1490 /* this handles the aliases - set the copymap for other
1491 * entries with the same data pointer */
1492 for (i = 0; parm_table[i].label; i++)
1493 if (parm_table[i].offset == parm_table[parmnum].offset &&
1494 parm_table[i].p_class == parm_table[parmnum].p_class)
1495 bitmap_clear(service->copymap, i);
1497 return set_variable(service, parmnum, parm_ptr, pszParmName,
1498 pszParmValue, lp_ctx, false);
1502 * Process a parameter.
1505 static bool do_parameter(const char *pszParmName, const char *pszParmValue,
1506 void *userdata)
1508 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1510 if (lp_ctx->bInGlobalSection)
1511 return lpcfg_do_global_parameter(lp_ctx, pszParmName,
1512 pszParmValue);
1513 else
1514 return lpcfg_do_service_parameter(lp_ctx, lp_ctx->currentService,
1515 pszParmName, pszParmValue);
1519 variable argument do parameter
1521 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx, const char *pszParmName, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
1522 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx,
1523 const char *pszParmName, const char *fmt, ...)
1525 char *s;
1526 bool ret;
1527 va_list ap;
1529 va_start(ap, fmt);
1530 s = talloc_vasprintf(NULL, fmt, ap);
1531 va_end(ap);
1532 ret = lpcfg_do_global_parameter(lp_ctx, pszParmName, s);
1533 talloc_free(s);
1534 return ret;
1539 set a parameter from the commandline - this is called from command line parameter
1540 parsing code. It sets the parameter then marks the parameter as unable to be modified
1541 by smb.conf processing
1543 bool lpcfg_set_cmdline(struct loadparm_context *lp_ctx, const char *pszParmName,
1544 const char *pszParmValue)
1546 int parmnum;
1547 int i;
1549 if (lp_ctx->s3_fns) {
1550 return lp_ctx->s3_fns->set_cmdline(pszParmName, pszParmValue);
1553 parmnum = map_parameter(pszParmName);
1555 while (isspace((unsigned char)*pszParmValue)) pszParmValue++;
1558 if (parmnum < 0 && strchr(pszParmName, ':')) {
1559 /* set a parametric option */
1560 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName,
1561 pszParmValue, FLAG_CMDLINE);
1564 if (parmnum < 0) {
1565 DEBUG(0,("Unknown option '%s'\n", pszParmName));
1566 return false;
1569 /* reset the CMDLINE flag in case this has been called before */
1570 lp_ctx->flags[parmnum] &= ~FLAG_CMDLINE;
1572 if (!lpcfg_do_global_parameter(lp_ctx, pszParmName, pszParmValue)) {
1573 return false;
1576 lp_ctx->flags[parmnum] |= FLAG_CMDLINE;
1578 /* we have to also set FLAG_CMDLINE on aliases */
1579 for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
1580 lp_ctx->flags[i] |= FLAG_CMDLINE;
1582 for (i=parmnum+1;i<NUMPARAMETERS && parm_table[i].offset == parm_table[parmnum].offset;i++) {
1583 lp_ctx->flags[i] |= FLAG_CMDLINE;
1586 return true;
1590 set a option from the commandline in 'a=b' format. Use to support --option
1592 bool lpcfg_set_option(struct loadparm_context *lp_ctx, const char *option)
1594 char *p, *s;
1595 bool ret;
1597 s = talloc_strdup(NULL, option);
1598 if (!s) {
1599 return false;
1602 p = strchr(s, '=');
1603 if (!p) {
1604 talloc_free(s);
1605 return false;
1608 *p = 0;
1610 ret = lpcfg_set_cmdline(lp_ctx, s, p+1);
1611 talloc_free(s);
1612 return ret;
1616 #define BOOLSTR(b) ((b) ? "Yes" : "No")
1619 * Print a parameter of the specified type.
1622 static void print_parameter(struct parm_struct *p, void *ptr, FILE * f)
1624 /* For the seperation of lists values that we print below */
1625 const char *list_sep = ", ";
1626 int i;
1627 switch (p->type)
1629 case P_ENUM:
1630 for (i = 0; p->enum_list[i].name; i++) {
1631 if (*(int *)ptr == p->enum_list[i].value) {
1632 fprintf(f, "%s",
1633 p->enum_list[i].name);
1634 break;
1637 break;
1639 case P_BOOL:
1640 fprintf(f, "%s", BOOLSTR(*(bool *)ptr));
1641 break;
1643 case P_BOOLREV:
1644 fprintf(f, "%s", BOOLSTR(!*(bool *)ptr));
1645 break;
1647 case P_INTEGER:
1648 case P_BYTES:
1649 fprintf(f, "%d", *(int *)ptr);
1650 break;
1652 case P_CHAR:
1653 fprintf(f, "%c", *(char *)ptr);
1654 break;
1656 case P_OCTAL: {
1657 int val = *(int *)ptr;
1658 if (val == -1) {
1659 fprintf(f, "-1");
1660 } else {
1661 fprintf(f, "0%o", val);
1663 break;
1666 case P_CMDLIST:
1667 list_sep = " ";
1668 /* fall through */
1669 case P_LIST:
1670 if ((char ***)ptr && *(char ***)ptr) {
1671 char **list = *(char ***)ptr;
1672 for (; *list; list++) {
1673 /* surround strings with whitespace in double quotes */
1674 if (*(list+1) == NULL) {
1675 /* last item, no extra separator */
1676 list_sep = "";
1678 if ( strchr_m( *list, ' ' ) ) {
1679 fprintf(f, "\"%s\"%s", *list, list_sep);
1680 } else {
1681 fprintf(f, "%s%s", *list, list_sep);
1685 break;
1687 case P_STRING:
1688 case P_USTRING:
1689 if (*(char **)ptr) {
1690 fprintf(f, "%s", *(char **)ptr);
1692 break;
1693 case P_SEP:
1694 break;
1699 * Check if two parameters are equal.
1702 static bool equal_parameter(parm_type type, void *ptr1, void *ptr2)
1704 switch (type) {
1705 case P_BOOL:
1706 case P_BOOLREV:
1707 return (*((bool *)ptr1) == *((bool *)ptr2));
1709 case P_INTEGER:
1710 case P_ENUM:
1711 case P_OCTAL:
1712 case P_BYTES:
1713 return (*((int *)ptr1) == *((int *)ptr2));
1715 case P_CHAR:
1716 return (*((char *)ptr1) == *((char *)ptr2));
1718 case P_LIST:
1719 case P_CMDLIST:
1720 return str_list_equal(*(const char ***)ptr1, *(const char ***)ptr2);
1722 case P_STRING:
1723 case P_USTRING:
1725 char *p1 = *(char **)ptr1, *p2 = *(char **)ptr2;
1726 if (p1 && !*p1)
1727 p1 = NULL;
1728 if (p2 && !*p2)
1729 p2 = NULL;
1730 return (p1 == p2 || strequal(p1, p2));
1732 case P_SEP:
1733 break;
1735 return false;
1739 * Process a new section (service).
1741 * At this stage all sections are services.
1742 * Later we'll have special sections that permit server parameters to be set.
1743 * Returns True on success, False on failure.
1746 static bool do_section(const char *pszSectionName, void *userdata)
1748 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1749 bool bRetval;
1750 bool isglobal = ((strwicmp(pszSectionName, GLOBAL_NAME) == 0) ||
1751 (strwicmp(pszSectionName, GLOBAL_NAME2) == 0));
1752 bRetval = false;
1754 /* if we've just struck a global section, note the fact. */
1755 lp_ctx->bInGlobalSection = isglobal;
1757 /* check for multiple global sections */
1758 if (lp_ctx->bInGlobalSection) {
1759 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1760 return true;
1763 /* if we have a current service, tidy it up before moving on */
1764 bRetval = true;
1766 if (lp_ctx->currentService != NULL)
1767 bRetval = lpcfg_service_ok(lp_ctx->currentService);
1769 /* if all is still well, move to the next record in the services array */
1770 if (bRetval) {
1771 /* We put this here to avoid an odd message order if messages are */
1772 /* issued by the post-processing of a previous section. */
1773 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1775 if ((lp_ctx->currentService = lpcfg_add_service(lp_ctx, lp_ctx->sDefault,
1776 pszSectionName))
1777 == NULL) {
1778 DEBUG(0, ("Failed to add a new service\n"));
1779 return false;
1783 return bRetval;
1788 * Determine if a particular base parameter is currently set to the default value.
1791 static bool is_default(struct loadparm_service *sDefault, int i)
1793 void *def_ptr = ((char *)sDefault) + parm_table[i].offset;
1794 if (!defaults_saved)
1795 return false;
1796 switch (parm_table[i].type) {
1797 case P_CMDLIST:
1798 case P_LIST:
1799 return str_list_equal((const char **)parm_table[i].def.lvalue,
1800 (const char **)def_ptr);
1801 case P_STRING:
1802 case P_USTRING:
1803 return strequal(parm_table[i].def.svalue,
1804 *(char **)def_ptr);
1805 case P_BOOL:
1806 case P_BOOLREV:
1807 return parm_table[i].def.bvalue ==
1808 *(bool *)def_ptr;
1809 case P_INTEGER:
1810 case P_CHAR:
1811 case P_OCTAL:
1812 case P_BYTES:
1813 case P_ENUM:
1814 return parm_table[i].def.ivalue ==
1815 *(int *)def_ptr;
1816 case P_SEP:
1817 break;
1819 return false;
1823 *Display the contents of the global structure.
1826 static void dump_globals(struct loadparm_context *lp_ctx, FILE *f,
1827 bool show_defaults)
1829 int i;
1830 struct parmlist_entry *data;
1832 fprintf(f, "# Global parameters\n[global]\n");
1834 for (i = 0; parm_table[i].label; i++)
1835 if (parm_table[i].p_class == P_GLOBAL &&
1836 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset))) {
1837 if (!show_defaults && (lp_ctx->flags[i] & FLAG_DEFAULT))
1838 continue;
1839 fprintf(f, "\t%s = ", parm_table[i].label);
1840 print_parameter(&parm_table[i], lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[i]), f);
1841 fprintf(f, "\n");
1843 if (lp_ctx->globals->param_opt != NULL) {
1844 for (data = lp_ctx->globals->param_opt; data;
1845 data = data->next) {
1846 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
1847 continue;
1849 fprintf(f, "\t%s = %s\n", data->key, data->value);
1856 * Display the contents of a single services record.
1859 static void dump_a_service(struct loadparm_service * pService, struct loadparm_service *sDefault, FILE * f,
1860 unsigned int *flags)
1862 int i;
1863 struct parmlist_entry *data;
1865 if (pService != sDefault)
1866 fprintf(f, "\n[%s]\n", pService->szService);
1868 for (i = 0; parm_table[i].label; i++) {
1869 if (parm_table[i].p_class == P_LOCAL &&
1870 (*parm_table[i].label != '-') &&
1871 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset)))
1873 if (pService == sDefault) {
1874 if (flags && (flags[i] & FLAG_DEFAULT)) {
1875 continue;
1877 if (defaults_saved) {
1878 if (is_default(sDefault, i)) {
1879 continue;
1882 } else {
1883 if (equal_parameter(parm_table[i].type,
1884 ((char *)pService) +
1885 parm_table[i].offset,
1886 ((char *)sDefault) +
1887 parm_table[i].offset))
1888 continue;
1891 fprintf(f, "\t%s = ", parm_table[i].label);
1892 print_parameter(&parm_table[i],
1893 ((char *)pService) + parm_table[i].offset, f);
1894 fprintf(f, "\n");
1897 if (pService->param_opt != NULL) {
1898 for (data = pService->param_opt; data; data = data->next) {
1899 fprintf(f, "\t%s = %s\n", data->key, data->value);
1904 bool lpcfg_dump_a_parameter(struct loadparm_context *lp_ctx,
1905 struct loadparm_service *service,
1906 const char *parm_name, FILE * f)
1908 struct parm_struct *parm;
1909 void *ptr;
1911 parm = lpcfg_parm_struct(lp_ctx, parm_name);
1912 if (!parm) {
1913 return false;
1916 ptr = lpcfg_parm_ptr(lp_ctx, service,parm);
1918 print_parameter(parm, ptr, f);
1919 fprintf(f, "\n");
1920 return true;
1924 * Return info about the next parameter in a service.
1925 * snum==-1 gives the globals.
1926 * Return NULL when out of parameters.
1930 struct parm_struct *lpcfg_next_parameter(struct loadparm_context *lp_ctx, int snum, int *i,
1931 int allparameters)
1933 if (snum == -1) {
1934 /* do the globals */
1935 for (; parm_table[*i].label; (*i)++) {
1936 if ((*parm_table[*i].label == '-'))
1937 continue;
1939 if ((*i) > 0
1940 && (parm_table[*i].offset ==
1941 parm_table[(*i) - 1].offset)
1942 && (parm_table[*i].p_class ==
1943 parm_table[(*i) - 1].p_class))
1944 continue;
1946 return &parm_table[(*i)++];
1948 } else {
1949 struct loadparm_service *pService = lp_ctx->services[snum];
1951 for (; parm_table[*i].label; (*i)++) {
1952 if (parm_table[*i].p_class == P_LOCAL &&
1953 (*parm_table[*i].label != '-') &&
1954 ((*i) == 0 ||
1955 (parm_table[*i].offset !=
1956 parm_table[(*i) - 1].offset)))
1958 if (allparameters ||
1959 !equal_parameter(parm_table[*i].type,
1960 ((char *)pService) +
1961 parm_table[*i].offset,
1962 ((char *)lp_ctx->sDefault) +
1963 parm_table[*i].offset))
1965 return &parm_table[(*i)++];
1971 return NULL;
1976 * Auto-load some home services.
1978 static void lpcfg_add_auto_services(struct loadparm_context *lp_ctx,
1979 const char *str)
1981 return;
1986 * Unload unused services.
1989 void lpcfg_killunused(struct loadparm_context *lp_ctx,
1990 struct smbsrv_connection *smb,
1991 bool (*snumused) (struct smbsrv_connection *, int))
1993 int i;
1994 for (i = 0; i < lp_ctx->iNumServices; i++) {
1995 if (lp_ctx->services[i] == NULL)
1996 continue;
1998 if (!snumused || !snumused(smb, i)) {
1999 talloc_free(lp_ctx->services[i]);
2000 lp_ctx->services[i] = NULL;
2006 static int lpcfg_destructor(struct loadparm_context *lp_ctx)
2008 struct parmlist_entry *data;
2010 if (lp_ctx->refuse_free) {
2011 /* someone is trying to free the
2012 global_loadparm_context.
2013 We can't allow that. */
2014 return -1;
2017 if (lp_ctx->globals->param_opt != NULL) {
2018 struct parmlist_entry *next;
2019 for (data = lp_ctx->globals->param_opt; data; data=next) {
2020 next = data->next;
2021 if (data->priority & FLAG_CMDLINE) continue;
2022 DLIST_REMOVE(lp_ctx->globals->param_opt, data);
2023 talloc_free(data);
2027 return 0;
2031 * Initialise the global parameter structure.
2033 * Note that most callers should use loadparm_init_global() instead
2035 struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
2037 int i;
2038 char *myname;
2039 struct loadparm_context *lp_ctx;
2040 struct parmlist_entry *parm;
2041 char *logfile;
2043 lp_ctx = talloc_zero(mem_ctx, struct loadparm_context);
2044 if (lp_ctx == NULL)
2045 return NULL;
2047 talloc_set_destructor(lp_ctx, lpcfg_destructor);
2048 lp_ctx->bInGlobalSection = true;
2049 lp_ctx->globals = talloc_zero(lp_ctx, struct loadparm_global);
2050 lp_ctx->sDefault = talloc_zero(lp_ctx, struct loadparm_service);
2052 lp_ctx->sDefault->iMaxPrintJobs = 1000;
2053 lp_ctx->sDefault->bAvailable = true;
2054 lp_ctx->sDefault->bBrowseable = true;
2055 lp_ctx->sDefault->bRead_only = true;
2056 lp_ctx->sDefault->bMap_archive = true;
2057 lp_ctx->sDefault->iStrictLocking = true;
2058 lp_ctx->sDefault->bOpLocks = true;
2059 lp_ctx->sDefault->iCreate_mask = 0744;
2060 lp_ctx->sDefault->iCreate_force_mode = 0000;
2061 lp_ctx->sDefault->iDir_mask = 0755;
2062 lp_ctx->sDefault->iDir_force_mode = 0000;
2064 DEBUG(3, ("Initialising global parameters\n"));
2066 for (i = 0; parm_table[i].label; i++) {
2067 if ((parm_table[i].type == P_STRING ||
2068 parm_table[i].type == P_USTRING) &&
2069 !(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2070 char **r;
2071 if (parm_table[i].p_class == P_LOCAL) {
2072 r = (char **)(((char *)lp_ctx->sDefault) + parm_table[i].offset);
2073 } else {
2074 r = (char **)(((char *)lp_ctx->globals) + parm_table[i].offset);
2076 *r = talloc_strdup(lp_ctx, "");
2080 logfile = talloc_asprintf(lp_ctx, "%s/log.samba", dyn_LOGFILEBASE);
2081 lpcfg_do_global_parameter(lp_ctx, "log file", logfile);
2082 talloc_free(logfile);
2084 lpcfg_do_global_parameter(lp_ctx, "log level", "0");
2086 lpcfg_do_global_parameter(lp_ctx, "share backend", "classic");
2088 lpcfg_do_global_parameter(lp_ctx, "server role", "auto");
2089 lpcfg_do_global_parameter(lp_ctx, "domain logons", "No");
2090 lpcfg_do_global_parameter(lp_ctx, "domain master", "Auto");
2092 /* options that can be set on the command line must be initialised via
2093 the slower lpcfg_do_global_parameter() to ensure that FLAG_CMDLINE is obeyed */
2094 #ifdef TCP_NODELAY
2095 lpcfg_do_global_parameter(lp_ctx, "socket options", "TCP_NODELAY");
2096 #endif
2097 lpcfg_do_global_parameter(lp_ctx, "workgroup", DEFAULT_WORKGROUP);
2098 myname = get_myname(lp_ctx);
2099 lpcfg_do_global_parameter(lp_ctx, "netbios name", myname);
2100 talloc_free(myname);
2101 lpcfg_do_global_parameter(lp_ctx, "name resolve order", "wins host bcast");
2103 lpcfg_do_global_parameter(lp_ctx, "fstype", "NTFS");
2105 lpcfg_do_global_parameter(lp_ctx, "ntvfs handler", "unixuid default");
2106 lpcfg_do_global_parameter(lp_ctx, "max connections", "-1");
2108 lpcfg_do_global_parameter(lp_ctx, "dcerpc endpoint servers", "epmapper wkssvc rpcecho samr netlogon lsarpc spoolss drsuapi dssetup unixinfo browser eventlog6 backupkey dnsserver");
2109 lpcfg_do_global_parameter(lp_ctx, "server services", "s3fs rpc nbt wrepl ldap cldap kdc drepl winbind ntp_signd kcc dnsupdate dns");
2110 /* the winbind method for domain controllers is for both RODC
2111 auth forwarding and for trusted domains */
2112 lpcfg_do_global_parameter(lp_ctx, "private dir", dyn_PRIVATE_DIR);
2113 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_LOCAL_MACHINE", "hklm.ldb");
2115 /* This hive should be dynamically generated by Samba using
2116 data from the sam, but for the moment leave it in a tdb to
2117 keep regedt32 from popping up an annoying dialog. */
2118 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_USERS", "hku.ldb");
2120 /* using UTF8 by default allows us to support all chars */
2121 lpcfg_do_global_parameter(lp_ctx, "unix charset", "UTF8");
2123 /* Use codepage 850 as a default for the dos character set */
2124 lpcfg_do_global_parameter(lp_ctx, "dos charset", "CP850");
2127 * Allow the default PASSWD_CHAT to be overridden in local.h.
2129 lpcfg_do_global_parameter(lp_ctx, "passwd chat", DEFAULT_PASSWD_CHAT);
2131 lpcfg_do_global_parameter(lp_ctx, "pid directory", dyn_PIDDIR);
2132 lpcfg_do_global_parameter(lp_ctx, "lock dir", dyn_LOCKDIR);
2133 lpcfg_do_global_parameter(lp_ctx, "state directory", dyn_STATEDIR);
2134 lpcfg_do_global_parameter(lp_ctx, "cache directory", dyn_CACHEDIR);
2135 lpcfg_do_global_parameter(lp_ctx, "ncalrpc dir", dyn_NCALRPCDIR);
2137 lpcfg_do_global_parameter(lp_ctx, "nbt client socket address", "");
2138 lpcfg_do_global_parameter_var(lp_ctx, "server string",
2139 "Samba %s", SAMBA_VERSION_STRING);
2141 lpcfg_do_global_parameter(lp_ctx, "password server", "*");
2143 lpcfg_do_global_parameter(lp_ctx, "max mux", "50");
2144 lpcfg_do_global_parameter(lp_ctx, "max xmit", "12288");
2145 lpcfg_do_global_parameter(lp_ctx, "host msdfs", "true");
2147 lpcfg_do_global_parameter(lp_ctx, "password level", "0");
2148 lpcfg_do_global_parameter(lp_ctx, "LargeReadwrite", "True");
2149 lpcfg_do_global_parameter(lp_ctx, "server min protocol", "CORE");
2150 lpcfg_do_global_parameter(lp_ctx, "server max protocol", "NT1");
2151 lpcfg_do_global_parameter(lp_ctx, "client min protocol", "CORE");
2152 lpcfg_do_global_parameter(lp_ctx, "client max protocol", "NT1");
2153 lpcfg_do_global_parameter(lp_ctx, "security", "AUTO");
2154 lpcfg_do_global_parameter(lp_ctx, "EncryptPasswords", "True");
2155 lpcfg_do_global_parameter(lp_ctx, "ReadRaw", "True");
2156 lpcfg_do_global_parameter(lp_ctx, "WriteRaw", "True");
2157 lpcfg_do_global_parameter(lp_ctx, "NullPasswords", "False");
2158 lpcfg_do_global_parameter(lp_ctx, "ObeyPamRestrictions", "False");
2160 lpcfg_do_global_parameter(lp_ctx, "TimeServer", "False");
2161 lpcfg_do_global_parameter(lp_ctx, "BindInterfacesOnly", "False");
2162 lpcfg_do_global_parameter(lp_ctx, "Unicode", "True");
2163 lpcfg_do_global_parameter(lp_ctx, "ClientLanManAuth", "False");
2164 lpcfg_do_global_parameter(lp_ctx, "ClientNTLMv2Auth", "True");
2165 lpcfg_do_global_parameter(lp_ctx, "LanmanAuth", "False");
2166 lpcfg_do_global_parameter(lp_ctx, "NTLMAuth", "True");
2167 lpcfg_do_global_parameter(lp_ctx, "client use spnego principal", "False");
2169 lpcfg_do_global_parameter(lp_ctx, "UnixExtensions", "False");
2171 lpcfg_do_global_parameter(lp_ctx, "PreferredMaster", "Auto");
2172 lpcfg_do_global_parameter(lp_ctx, "LocalMaster", "True");
2174 lpcfg_do_global_parameter(lp_ctx, "wins support", "False");
2175 lpcfg_do_global_parameter(lp_ctx, "dns proxy", "True");
2177 lpcfg_do_global_parameter(lp_ctx, "winbind separator", "\\");
2178 lpcfg_do_global_parameter(lp_ctx, "winbind sealed pipes", "True");
2179 lpcfg_do_global_parameter(lp_ctx, "winbindd socket directory", dyn_WINBINDD_SOCKET_DIR);
2180 #if _SAMBA_BUILD_ >= 4
2181 lpcfg_do_global_parameter(lp_ctx, "winbindd privileged socket directory", dyn_WINBINDD_PRIVILEGED_SOCKET_DIR);
2182 lpcfg_do_global_parameter(lp_ctx, "ntp signd socket directory", dyn_NTP_SIGND_SOCKET_DIR);
2183 lpcfg_do_global_parameter_var(lp_ctx, "dns update command", "%s/samba_dnsupdate", dyn_SCRIPTSBINDIR);
2184 lpcfg_do_global_parameter_var(lp_ctx, "spn update command", "%s/samba_spnupdate", dyn_SCRIPTSBINDIR);
2185 lpcfg_do_global_parameter_var(lp_ctx, "samba kcc command",
2186 "%s/samba_kcc", dyn_SCRIPTSBINDIR);
2187 #endif
2188 lpcfg_do_global_parameter(lp_ctx, "template shell", "/bin/false");
2189 lpcfg_do_global_parameter(lp_ctx, "template homedir", "/home/%WORKGROUP%/%ACCOUNTNAME%");
2191 lpcfg_do_global_parameter(lp_ctx, "client signing", "default");
2192 lpcfg_do_global_parameter(lp_ctx, "server signing", "default");
2194 lpcfg_do_global_parameter(lp_ctx, "use spnego", "True");
2196 lpcfg_do_global_parameter(lp_ctx, "use mmap", "True");
2198 lpcfg_do_global_parameter(lp_ctx, "smb ports", "445 139");
2199 lpcfg_do_global_parameter(lp_ctx, "nbt port", "137");
2200 lpcfg_do_global_parameter(lp_ctx, "dgram port", "138");
2201 lpcfg_do_global_parameter(lp_ctx, "cldap port", "389");
2202 lpcfg_do_global_parameter(lp_ctx, "krb5 port", "88");
2203 lpcfg_do_global_parameter(lp_ctx, "kpasswd port", "464");
2204 lpcfg_do_global_parameter(lp_ctx, "web port", "901");
2206 lpcfg_do_global_parameter(lp_ctx, "nt status support", "True");
2208 lpcfg_do_global_parameter(lp_ctx, "max wins ttl", "518400"); /* 6 days */
2209 lpcfg_do_global_parameter(lp_ctx, "min wins ttl", "10");
2211 lpcfg_do_global_parameter(lp_ctx, "tls enabled", "True");
2212 lpcfg_do_global_parameter(lp_ctx, "tls keyfile", "tls/key.pem");
2213 lpcfg_do_global_parameter(lp_ctx, "tls certfile", "tls/cert.pem");
2214 lpcfg_do_global_parameter(lp_ctx, "tls cafile", "tls/ca.pem");
2215 lpcfg_do_global_parameter(lp_ctx, "prefork children:smb", "4");
2217 lpcfg_do_global_parameter(lp_ctx, "rndc command", "/usr/sbin/rndc");
2218 lpcfg_do_global_parameter(lp_ctx, "nsupdate command", "/usr/bin/nsupdate -g");
2220 lpcfg_do_global_parameter(lp_ctx, "allow dns updates", "secure only");
2221 lpcfg_do_global_parameter(lp_ctx, "dns forwarder", "");
2223 for (i = 0; parm_table[i].label; i++) {
2224 if (!(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2225 lp_ctx->flags[i] |= FLAG_DEFAULT;
2229 for (parm=lp_ctx->globals->param_opt; parm; parm=parm->next) {
2230 if (!(parm->priority & FLAG_CMDLINE)) {
2231 parm->priority |= FLAG_DEFAULT;
2235 return lp_ctx;
2239 * Initialise the global parameter structure.
2241 struct loadparm_context *loadparm_init_global(bool load_default)
2243 if (global_loadparm_context == NULL) {
2244 global_loadparm_context = loadparm_init(NULL);
2246 if (global_loadparm_context == NULL) {
2247 return NULL;
2249 global_loadparm_context->global = true;
2250 if (load_default && !global_loadparm_context->loaded) {
2251 lpcfg_load_default(global_loadparm_context);
2253 global_loadparm_context->refuse_free = true;
2254 return global_loadparm_context;
2258 * Initialise the global parameter structure.
2260 struct loadparm_context *loadparm_init_s3(TALLOC_CTX *mem_ctx,
2261 const struct loadparm_s3_helpers *s3_fns)
2263 struct loadparm_context *loadparm_context = talloc_zero(mem_ctx, struct loadparm_context);
2264 if (!loadparm_context) {
2265 return NULL;
2267 loadparm_context->s3_fns = s3_fns;
2268 return loadparm_context;
2271 const char *lpcfg_configfile(struct loadparm_context *lp_ctx)
2273 return lp_ctx->szConfigFile;
2276 const char *lp_default_path(void)
2278 if (getenv("SMB_CONF_PATH"))
2279 return getenv("SMB_CONF_PATH");
2280 else
2281 return dyn_CONFIGFILE;
2285 * Update the internal state of a loadparm context after settings
2286 * have changed.
2288 static bool lpcfg_update(struct loadparm_context *lp_ctx)
2290 struct debug_settings settings;
2291 lpcfg_add_auto_services(lp_ctx, lpcfg_auto_services(lp_ctx));
2293 if (!lp_ctx->globals->szWINSservers && lp_ctx->globals->bWINSsupport) {
2294 lpcfg_do_global_parameter(lp_ctx, "wins server", "127.0.0.1");
2297 if (!lp_ctx->global) {
2298 return true;
2301 panic_action = lp_ctx->globals->szPanicAction;
2303 reload_charcnv(lp_ctx);
2305 ZERO_STRUCT(settings);
2306 /* Add any more debug-related smb.conf parameters created in
2307 * future here */
2308 settings.timestamp_logs = true;
2309 debug_set_settings(&settings);
2311 /* FIXME: This is a bit of a hack, but we can't use a global, since
2312 * not everything that uses lp also uses the socket library */
2313 if (lpcfg_parm_bool(lp_ctx, NULL, "socket", "testnonblock", false)) {
2314 setenv("SOCKET_TESTNONBLOCK", "1", 1);
2315 } else {
2316 unsetenv("SOCKET_TESTNONBLOCK");
2319 return true;
2322 bool lpcfg_load_default(struct loadparm_context *lp_ctx)
2324 const char *path;
2326 path = lp_default_path();
2328 if (!file_exist(path)) {
2329 /* We allow the default smb.conf file to not exist,
2330 * basically the equivalent of an empty file. */
2331 return lpcfg_update(lp_ctx);
2334 return lpcfg_load(lp_ctx, path);
2338 * Load the services array from the services file.
2340 * Return True on success, False on failure.
2342 bool lpcfg_load(struct loadparm_context *lp_ctx, const char *filename)
2344 char *n2;
2345 bool bRetval;
2347 filename = talloc_strdup(lp_ctx, filename);
2349 lp_ctx->szConfigFile = filename;
2351 if (lp_ctx->s3_fns) {
2352 return lp_ctx->s3_fns->load(filename);
2355 lp_ctx->bInGlobalSection = true;
2356 n2 = standard_sub_basic(lp_ctx, lp_ctx->szConfigFile);
2357 DEBUG(2, ("lpcfg_load: refreshing parameters from %s\n", n2));
2359 add_to_file_list(lp_ctx, lp_ctx->szConfigFile, n2);
2361 /* We get sections first, so have to start 'behind' to make up */
2362 lp_ctx->currentService = NULL;
2363 bRetval = pm_process(n2, do_section, do_parameter, lp_ctx);
2365 /* finish up the last section */
2366 DEBUG(4, ("pm_process() returned %s\n", BOOLSTR(bRetval)));
2367 if (bRetval)
2368 if (lp_ctx->currentService != NULL)
2369 bRetval = lpcfg_service_ok(lp_ctx->currentService);
2371 bRetval = bRetval && lpcfg_update(lp_ctx);
2373 /* we do this unconditionally, so that it happens even
2374 for a missing smb.conf */
2375 reload_charcnv(lp_ctx);
2377 if (bRetval == true) {
2378 /* set this up so that any child python tasks will
2379 find the right smb.conf */
2380 setenv("SMB_CONF_PATH", filename, 1);
2382 /* set the context used by the lp_*() function
2383 varients */
2384 global_loadparm_context = lp_ctx;
2385 lp_ctx->loaded = true;
2388 return bRetval;
2392 * Return the max number of services.
2395 int lpcfg_numservices(struct loadparm_context *lp_ctx)
2397 if (lp_ctx->s3_fns) {
2398 return lp_ctx->s3_fns->get_numservices();
2401 return lp_ctx->iNumServices;
2405 * Display the contents of the services array in human-readable form.
2408 void lpcfg_dump(struct loadparm_context *lp_ctx, FILE *f, bool show_defaults,
2409 int maxtoprint)
2411 int iService;
2413 if (lp_ctx->s3_fns) {
2414 lp_ctx->s3_fns->dump(f, show_defaults, maxtoprint);
2415 return;
2418 defaults_saved = !show_defaults;
2420 dump_globals(lp_ctx, f, show_defaults);
2422 dump_a_service(lp_ctx->sDefault, lp_ctx->sDefault, f, lp_ctx->flags);
2424 for (iService = 0; iService < maxtoprint; iService++)
2425 lpcfg_dump_one(f, show_defaults, lp_ctx->services[iService], lp_ctx->sDefault);
2429 * Display the contents of one service in human-readable form.
2431 void lpcfg_dump_one(FILE *f, bool show_defaults, struct loadparm_service *service, struct loadparm_service *sDefault)
2433 if (service != NULL) {
2434 if (service->szService[0] == '\0')
2435 return;
2436 dump_a_service(service, sDefault, f, NULL);
2440 struct loadparm_service *lpcfg_servicebynum(struct loadparm_context *lp_ctx,
2441 int snum)
2443 if (lp_ctx->s3_fns) {
2444 return lp_ctx->s3_fns->get_servicebynum(snum);
2447 return lp_ctx->services[snum];
2450 struct loadparm_service *lpcfg_service(struct loadparm_context *lp_ctx,
2451 const char *service_name)
2453 int iService;
2454 char *serviceName;
2456 if (lp_ctx->s3_fns) {
2457 return lp_ctx->s3_fns->get_service(service_name);
2460 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--) {
2461 if (lp_ctx->services[iService] &&
2462 lp_ctx->services[iService]->szService) {
2464 * The substitution here is used to support %U is
2465 * service names
2467 serviceName = standard_sub_basic(
2468 lp_ctx->services[iService],
2469 lp_ctx->services[iService]->szService);
2470 if (strequal(serviceName, service_name)) {
2471 talloc_free(serviceName);
2472 return lp_ctx->services[iService];
2474 talloc_free(serviceName);
2478 DEBUG(7,("lpcfg_servicenumber: couldn't find %s\n", service_name));
2479 return NULL;
2482 const char *lpcfg_servicename(const struct loadparm_service *service)
2484 return lp_string((const char *)service->szService);
2488 * A useful volume label function.
2490 const char *lpcfg_volume_label(struct loadparm_service *service, struct loadparm_service *sDefault)
2492 const char *ret;
2493 ret = lp_string((const char *)((service != NULL && service->volume != NULL) ?
2494 service->volume : sDefault->volume));
2495 if (!*ret)
2496 return lpcfg_servicename(service);
2497 return ret;
2501 * If we are PDC then prefer us as DMB
2503 const char *lpcfg_printername(struct loadparm_service *service, struct loadparm_service *sDefault)
2505 const char *ret;
2506 ret = lp_string((const char *)((service != NULL && service->szPrintername != NULL) ?
2507 service->szPrintername : sDefault->szPrintername));
2508 if (ret == NULL || (ret != NULL && *ret == '\0'))
2509 ret = lpcfg_servicename(service);
2511 return ret;
2516 * Return the max print jobs per queue.
2518 int lpcfg_maxprintjobs(struct loadparm_service *service, struct loadparm_service *sDefault)
2520 int maxjobs = (service != NULL) ? service->iMaxPrintJobs : sDefault->iMaxPrintJobs;
2521 if (maxjobs <= 0 || maxjobs >= PRINT_MAX_JOBID)
2522 maxjobs = PRINT_MAX_JOBID - 1;
2524 return maxjobs;
2527 struct smb_iconv_handle *lpcfg_iconv_handle(struct loadparm_context *lp_ctx)
2529 if (lp_ctx == NULL) {
2530 return get_iconv_handle();
2532 return lp_ctx->iconv_handle;
2535 _PUBLIC_ void reload_charcnv(struct loadparm_context *lp_ctx)
2537 struct smb_iconv_handle *old_ic = lp_ctx->iconv_handle;
2538 if (!lp_ctx->global) {
2539 return;
2542 if (old_ic == NULL) {
2543 old_ic = global_iconv_handle;
2545 lp_ctx->iconv_handle = smb_iconv_handle_reinit_lp(lp_ctx, lp_ctx, old_ic);
2546 global_iconv_handle = lp_ctx->iconv_handle;
2549 _PUBLIC_ char *lpcfg_tls_keyfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2551 return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_keyfile);
2554 _PUBLIC_ char *lpcfg_tls_certfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2556 return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_certfile);
2559 _PUBLIC_ char *lpcfg_tls_cafile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2561 return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_cafile);
2564 _PUBLIC_ char *lpcfg_tls_crlfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2566 return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_crlfile);
2569 _PUBLIC_ char *lpcfg_tls_dhpfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2571 return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_dhpfile);
2574 struct gensec_settings *lpcfg_gensec_settings(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
2576 struct gensec_settings *settings = talloc_zero(mem_ctx, struct gensec_settings);
2577 if (settings == NULL)
2578 return NULL;
2579 SMB_ASSERT(lp_ctx != NULL);
2580 settings->lp_ctx = talloc_reference(settings, lp_ctx);
2581 settings->target_hostname = lpcfg_parm_string(lp_ctx, NULL, "gensec", "target_hostname");
2582 return settings;
2585 int lpcfg_server_role(struct loadparm_context *lp_ctx)
2587 int domain_master = lpcfg__domain_master(lp_ctx);
2589 return lp_find_server_role(lpcfg__server_role(lp_ctx),
2590 lpcfg__security(lp_ctx),
2591 lpcfg__domain_logons(lp_ctx),
2592 (domain_master == true) ||
2593 (domain_master == Auto));
2596 int lpcfg_security(struct loadparm_context *lp_ctx)
2598 return lp_find_security(lpcfg__server_role(lp_ctx),
2599 lpcfg__security(lp_ctx));