param: Remove winbindd privileged socket directory option
[Samba.git] / lib / param / loadparm.c
bloba05610130e8d2c81803ef4eb8c9cce2825d6a6b4
1 /*
2 Unix SMB/CIFS implementation.
3 Parameter loading functions
4 Copyright (C) Karl Auer 1993-1998
6 Largely re-written by Andrew Tridgell, September 1994
8 Copyright (C) Simo Sorce 2001
9 Copyright (C) Alexander Bokovoy 2002
10 Copyright (C) Stefan (metze) Metzmacher 2002
11 Copyright (C) Jim McDonough (jmcd@us.ibm.com) 2003.
12 Copyright (C) James Myers 2003 <myersjj@samba.org>
13 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
14 Copyright (C) Andrew Bartlett 2011-2012
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 * Load parameters.
33 * This module provides suitable callback functions for the params
34 * module. It builds the internal table of service details which is
35 * then used by the rest of the server.
37 * To add a parameter:
39 * 1) add it to the global or service structure definition
40 * 2) add it to the parm_table
41 * 3) add it to the list of available functions (eg: using FN_GLOBAL_STRING())
42 * 4) If it's a global then initialise it in init_globals. If a local
43 * (ie. service) parameter then initialise it in the sDefault structure
46 * Notes:
47 * The configuration file is processed sequentially for speed. It is NOT
48 * accessed randomly as happens in 'real' Windows. For this reason, there
49 * is a fair bit of sequence-dependent code here - ie., code which assumes
50 * that certain things happen before others. In particular, the code which
51 * happens at the boundary between sections is delicately poised, so be
52 * careful!
56 #include "includes.h"
57 #include "version.h"
58 #include "dynconfig/dynconfig.h"
59 #include "system/time.h"
60 #include "system/locale.h"
61 #include "system/network.h" /* needed for TCP_NODELAY */
62 #include "../lib/util/dlinklist.h"
63 #include "lib/param/param.h"
64 #include "lib/param/loadparm.h"
65 #include "auth/gensec/gensec.h"
66 #include "lib/param/s3_param.h"
67 #include "lib/util/bitmap.h"
68 #include "libcli/smb/smb_constants.h"
69 #include "tdb.h"
70 #include "librpc/gen_ndr/nbt.h"
71 #include "libds/common/roles.h"
73 #ifdef HAVE_HTTPCONNECTENCRYPT
74 #include <cups/http.h>
75 #endif
77 #define standard_sub_basic talloc_strdup
79 #include "lib/param/param_global.h"
81 struct loadparm_service *lpcfg_default_service(struct loadparm_context *lp_ctx)
83 return lp_ctx->sDefault;
86 /**
87 * Convenience routine to grab string parameters into temporary memory
88 * and run standard_sub_basic on them.
90 * The buffers can be written to by
91 * callers without affecting the source string.
94 static const char *lpcfg_string(const char *s)
96 #if 0 /* until REWRITE done to make thread-safe */
97 size_t len = s ? strlen(s) : 0;
98 char *ret;
99 #endif
101 /* The follow debug is useful for tracking down memory problems
102 especially if you have an inner loop that is calling a lp_*()
103 function that returns a string. Perhaps this debug should be
104 present all the time? */
106 #if 0
107 DEBUG(10, ("lpcfg_string(%s)\n", s));
108 #endif
110 #if 0 /* until REWRITE done to make thread-safe */
111 if (!lp_talloc)
112 lp_talloc = talloc_init("lp_talloc");
114 ret = talloc_array(lp_talloc, char, len + 100); /* leave room for substitution */
116 if (!ret)
117 return NULL;
119 if (!s)
120 *ret = 0;
121 else
122 strlcpy(ret, s, len);
124 if (trim_string(ret, "\"", "\"")) {
125 if (strchr(ret,'"') != NULL)
126 strlcpy(ret, s, len);
129 standard_sub_basic(ret,len+100);
130 return (ret);
131 #endif
132 return s;
136 In this section all the functions that are used to access the
137 parameters from the rest of the program are defined
141 * the creation of separate lpcfg_*() and lp_*() functions is to allow
142 * for code compatibility between existing Samba4 and Samba3 code.
145 /* this global context supports the lp_*() function varients */
146 static struct loadparm_context *global_loadparm_context;
148 #define FN_GLOBAL_STRING(fn_name,var_name) \
149 _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx, TALLOC_CTX *ctx) {\
150 if (lp_ctx == NULL) return NULL; \
151 if (lp_ctx->s3_fns) { \
152 return lp_ctx->globals->var_name ? lp_ctx->s3_fns->lp_string(ctx, lp_ctx->globals->var_name) : talloc_strdup(ctx, ""); \
154 return lp_ctx->globals->var_name ? talloc_strdup(ctx, lpcfg_string(lp_ctx->globals->var_name)) : talloc_strdup(ctx, ""); \
157 #define FN_GLOBAL_CONST_STRING(fn_name,var_name) \
158 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
159 if (lp_ctx == NULL) return NULL; \
160 return lp_ctx->globals->var_name ? lpcfg_string(lp_ctx->globals->var_name) : ""; \
163 #define FN_GLOBAL_LIST(fn_name,var_name) \
164 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
165 if (lp_ctx == NULL) return NULL; \
166 return lp_ctx->globals->var_name; \
169 #define FN_GLOBAL_BOOL(fn_name,var_name) \
170 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) {\
171 if (lp_ctx == NULL) return false; \
172 return lp_ctx->globals->var_name; \
175 #define FN_GLOBAL_INTEGER(fn_name,var_name) \
176 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
177 return lp_ctx->globals->var_name; \
180 /* Local parameters don't need the ->s3_fns because the struct
181 * loadparm_service is shared and lpcfg_service() checks the ->s3_fns
182 * hook */
183 #define FN_LOCAL_STRING(fn_name,val) \
184 _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_service *service, \
185 struct loadparm_service *sDefault, TALLOC_CTX *ctx) { \
186 return(talloc_strdup(ctx, lpcfg_string((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)))); \
189 #define FN_LOCAL_CONST_STRING(fn_name,val) \
190 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_service *service, \
191 struct loadparm_service *sDefault) { \
192 return((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)); \
195 #define FN_LOCAL_LIST(fn_name,val) \
196 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_service *service, \
197 struct loadparm_service *sDefault) {\
198 return(const char **)(service != NULL && service->val != NULL? service->val : sDefault->val); \
201 #define FN_LOCAL_PARM_BOOL(fn_name, val) FN_LOCAL_BOOL(fn_name, val)
203 #define FN_LOCAL_BOOL(fn_name,val) \
204 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_service *service, \
205 struct loadparm_service *sDefault) { \
206 return((service != NULL)? service->val : sDefault->val); \
209 #define FN_LOCAL_INTEGER(fn_name,val) \
210 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_service *service, \
211 struct loadparm_service *sDefault) { \
212 return((service != NULL)? service->val : sDefault->val); \
215 #define FN_LOCAL_PARM_INTEGER(fn_name, val) FN_LOCAL_INTEGER(fn_name, val)
217 #define FN_LOCAL_CHAR(fn_name,val) \
218 _PUBLIC_ char lpcfg_ ## fn_name(struct loadparm_service *service, \
219 struct loadparm_service *sDefault) { \
220 return((service != NULL)? service->val : sDefault->val); \
223 #define FN_LOCAL_PARM_CHAR(fn_name,val) FN_LOCAL_CHAR(fn_name, val)
225 #include "lib/param/param_functions.c"
227 /* These functions cannot be auto-generated */
228 FN_LOCAL_BOOL(autoloaded, autoloaded)
229 FN_GLOBAL_CONST_STRING(dnsdomain, dnsdomain)
231 /* local prototypes */
232 static struct loadparm_service *lpcfg_getservicebyname(struct loadparm_context *lp_ctx,
233 const char *pszServiceName);
234 static bool do_section(const char *pszSectionName, void *);
235 static bool set_variable_helper(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
236 const char *pszParmName, const char *pszParmValue);
237 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
238 struct loadparm_service *service,
239 const char *pszParmName,
240 const char *pszParmValue, int flags);
242 /* The following are helper functions for parametrical options support. */
243 /* It returns a pointer to parametrical option value if it exists or NULL otherwise */
244 /* Actual parametrical functions are quite simple */
245 struct parmlist_entry *get_parametric_helper(struct loadparm_service *service,
246 const char *type, const char *option,
247 struct parmlist_entry *global_opts)
249 size_t type_len = strlen(type);
250 size_t option_len = strlen(option);
251 char param_key[type_len + option_len + 2];
252 struct parmlist_entry *data = NULL;
254 snprintf(param_key, sizeof(param_key), "%s:%s", type, option);
257 * Try to fetch the option from the data.
259 if (service != NULL) {
260 data = service->param_opt;
261 while (data != NULL) {
262 if (strwicmp(data->key, param_key) == 0) {
263 return data;
265 data = data->next;
270 * Fall back to fetching from the globals.
272 data = global_opts;
273 while (data != NULL) {
274 if (strwicmp(data->key, param_key) == 0) {
275 return data;
277 data = data->next;
280 return NULL;
283 const char *lpcfg_get_parametric(struct loadparm_context *lp_ctx,
284 struct loadparm_service *service,
285 const char *type, const char *option)
287 struct parmlist_entry *data;
289 if (lp_ctx == NULL)
290 return NULL;
292 data = get_parametric_helper(service,
293 type, option, lp_ctx->globals->param_opt);
295 if (data == NULL) {
296 return NULL;
297 } else {
298 return data->value;
304 * convenience routine to return int parameters.
306 int lp_int(const char *s)
309 if (!s || !*s) {
310 DEBUG(0,("lp_int(%s): is called with NULL!\n",s));
311 return -1;
314 return strtol(s, NULL, 0);
318 * convenience routine to return unsigned long parameters.
320 unsigned long lp_ulong(const char *s)
323 if (!s || !*s) {
324 DEBUG(0,("lp_ulong(%s): is called with NULL!\n",s));
325 return -1;
328 return strtoul(s, NULL, 0);
332 * convenience routine to return unsigned long long parameters.
334 unsigned long long lp_ulonglong(const char *s)
337 if (!s || !*s) {
338 DEBUG(0, ("lp_ulonglong(%s): is called with NULL!\n", s));
339 return -1;
342 return strtoull(s, NULL, 0);
346 * convenience routine to return unsigned long parameters.
348 static long lp_long(const char *s)
351 if (!s) {
352 DEBUG(0,("lp_long(%s): is called with NULL!\n",s));
353 return -1;
356 return strtol(s, NULL, 0);
360 * convenience routine to return unsigned long parameters.
362 static double lp_double(const char *s)
365 if (!s) {
366 DEBUG(0,("lp_double(%s): is called with NULL!\n",s));
367 return -1;
370 return strtod(s, NULL);
374 * convenience routine to return boolean parameters.
376 bool lp_bool(const char *s)
378 bool ret = false;
380 if (!s || !*s) {
381 DEBUG(0,("lp_bool(%s): is called with NULL!\n",s));
382 return false;
385 if (!set_boolean(s, &ret)) {
386 DEBUG(0,("lp_bool(%s): value is not boolean!\n",s));
387 return false;
390 return ret;
394 * Return parametric option from a given service. Type is a part of option before ':'
395 * Parametric option has following syntax: 'Type: option = value'
396 * Returned value is allocated in 'lp_talloc' context
399 const char *lpcfg_parm_string(struct loadparm_context *lp_ctx,
400 struct loadparm_service *service, const char *type,
401 const char *option)
403 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
405 if (value)
406 return lpcfg_string(value);
408 return NULL;
412 * Return parametric option from a given service. Type is a part of option before ':'
413 * Parametric option has following syntax: 'Type: option = value'
414 * Returned value is allocated in 'lp_talloc' context
417 const char **lpcfg_parm_string_list(TALLOC_CTX *mem_ctx,
418 struct loadparm_context *lp_ctx,
419 struct loadparm_service *service,
420 const char *type,
421 const char *option, const char *separator)
423 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
425 if (value != NULL) {
426 char **l = str_list_make(mem_ctx, value, separator);
427 return discard_const_p(const char *, l);
430 return NULL;
434 * Return parametric option from a given service. Type is a part of option before ':'
435 * Parametric option has following syntax: 'Type: option = value'
438 int lpcfg_parm_int(struct loadparm_context *lp_ctx,
439 struct loadparm_service *service, const char *type,
440 const char *option, int default_v)
442 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
444 if (value)
445 return lp_int(value);
447 return default_v;
451 * Return parametric option from a given service. Type is a part of
452 * option before ':'.
453 * Parametric option has following syntax: 'Type: option = value'.
456 int lpcfg_parm_bytes(struct loadparm_context *lp_ctx,
457 struct loadparm_service *service, const char *type,
458 const char *option, int default_v)
460 uint64_t bval;
462 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
464 if (value && conv_str_size_error(value, &bval)) {
465 if (bval <= INT_MAX) {
466 return (int)bval;
470 return default_v;
474 * Return parametric option from a given service.
475 * Type is a part of option before ':'
476 * Parametric option has following syntax: 'Type: option = value'
478 unsigned long lpcfg_parm_ulong(struct loadparm_context *lp_ctx,
479 struct loadparm_service *service, const char *type,
480 const char *option, unsigned long default_v)
482 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
484 if (value)
485 return lp_ulong(value);
487 return default_v;
491 * Return parametric option from a given service.
492 * Type is a part of option before ':'
493 * Parametric option has following syntax: 'Type: option = value'
495 unsigned long long lpcfg_parm_ulonglong(struct loadparm_context *lp_ctx,
496 struct loadparm_service *service,
497 const char *type, const char *option,
498 unsigned long long default_v)
500 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
502 if (value) {
503 return lp_ulonglong(value);
506 return default_v;
509 long lpcfg_parm_long(struct loadparm_context *lp_ctx,
510 struct loadparm_service *service, const char *type,
511 const char *option, long default_v)
513 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
515 if (value)
516 return lp_long(value);
518 return default_v;
521 double lpcfg_parm_double(struct loadparm_context *lp_ctx,
522 struct loadparm_service *service, const char *type,
523 const char *option, double default_v)
525 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
527 if (value != NULL)
528 return lp_double(value);
530 return default_v;
534 * Return parametric option from a given service. Type is a part of option before ':'
535 * Parametric option has following syntax: 'Type: option = value'
538 bool lpcfg_parm_bool(struct loadparm_context *lp_ctx,
539 struct loadparm_service *service, const char *type,
540 const char *option, bool default_v)
542 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
544 if (value != NULL)
545 return lp_bool(value);
547 return default_v;
551 /* this is used to prevent lots of mallocs of size 1 */
552 static const char lpcfg_string_empty[] = "";
555 Free a string value.
557 void lpcfg_string_free(char **s)
559 if (s == NULL) {
560 return;
562 if (*s == lpcfg_string_empty) {
563 *s = NULL;
564 return;
566 TALLOC_FREE(*s);
570 * Set a string value, deallocating any existing space, and allocing the space
571 * for the string
573 bool lpcfg_string_set(TALLOC_CTX *mem_ctx, char **dest, const char *src)
575 lpcfg_string_free(dest);
577 if ((src == NULL) || (*src == '\0')) {
578 *dest = discard_const_p(char, lpcfg_string_empty);
579 return true;
582 *dest = talloc_strdup(mem_ctx, src);
583 if ((*dest) == NULL) {
584 DEBUG(0,("Out of memory in string_set\n"));
585 return false;
588 return true;
592 * Set a string value, deallocating any existing space, and allocing the space
593 * for the string
595 bool lpcfg_string_set_upper(TALLOC_CTX *mem_ctx, char **dest, const char *src)
597 lpcfg_string_free(dest);
599 if ((src == NULL) || (*src == '\0')) {
600 *dest = discard_const_p(char, lpcfg_string_empty);
601 return true;
604 *dest = strupper_talloc(mem_ctx, src);
605 if ((*dest) == NULL) {
606 DEBUG(0,("Out of memory in string_set_upper\n"));
607 return false;
610 return true;
616 * Add a new service to the services array initialising it with the given
617 * service.
620 struct loadparm_service *lpcfg_add_service(struct loadparm_context *lp_ctx,
621 const struct loadparm_service *pservice,
622 const char *name)
624 int i;
625 int num_to_alloc = lp_ctx->iNumServices + 1;
626 struct parmlist_entry *data, *pdata;
628 if (lp_ctx->s3_fns != NULL) {
629 smb_panic("Add a service should not be called on an s3 loadparm ctx");
632 if (pservice == NULL) {
633 pservice = lp_ctx->sDefault;
636 /* it might already exist */
637 if (name) {
638 struct loadparm_service *service = lpcfg_getservicebyname(lp_ctx,
639 name);
640 if (service != NULL) {
641 /* Clean all parametric options for service */
642 /* They will be added during parsing again */
643 data = service->param_opt;
644 while (data) {
645 pdata = data->next;
646 talloc_free(data);
647 data = pdata;
649 service->param_opt = NULL;
650 return service;
654 /* find an invalid one */
655 for (i = 0; i < lp_ctx->iNumServices; i++)
656 if (lp_ctx->services[i] == NULL)
657 break;
659 /* if not, then create one */
660 if (i == lp_ctx->iNumServices) {
661 struct loadparm_service **tsp;
663 tsp = talloc_realloc(lp_ctx, lp_ctx->services, struct loadparm_service *, num_to_alloc);
665 if (!tsp) {
666 DEBUG(0,("lpcfg_add_service: failed to enlarge services!\n"));
667 return NULL;
668 } else {
669 lp_ctx->services = tsp;
670 lp_ctx->services[lp_ctx->iNumServices] = NULL;
673 lp_ctx->iNumServices++;
676 lp_ctx->services[i] = talloc_zero(lp_ctx->services, struct loadparm_service);
677 if (lp_ctx->services[i] == NULL) {
678 DEBUG(0,("lpcfg_add_service: out of memory!\n"));
679 return NULL;
681 copy_service(lp_ctx->services[i], pservice, NULL);
682 if (name != NULL)
683 lpcfg_string_set(lp_ctx->services[i], &lp_ctx->services[i]->szService, name);
684 return lp_ctx->services[i];
688 * Add a new home service, with the specified home directory, defaults coming
689 * from service ifrom.
692 bool lpcfg_add_home(struct loadparm_context *lp_ctx,
693 const char *pszHomename,
694 struct loadparm_service *default_service,
695 const char *user, const char *pszHomedir)
697 struct loadparm_service *service;
699 service = lpcfg_add_service(lp_ctx, default_service, pszHomename);
701 if (service == NULL)
702 return false;
704 if (!(*(default_service->path))
705 || strequal(default_service->path, lp_ctx->sDefault->path)) {
706 service->path = talloc_strdup(service, pszHomedir);
707 } else {
708 service->path = string_sub_talloc(service, lpcfg_path(default_service, lp_ctx->sDefault, service), "%H", pszHomedir);
711 if (!(*(service->comment))) {
712 service->comment = talloc_asprintf(service, "Home directory of %s", user);
714 service->available = default_service->available;
715 service->browseable = default_service->browseable;
717 DEBUG(3, ("adding home's share [%s] for user '%s' at '%s'\n",
718 pszHomename, user, service->path));
720 return true;
724 * Add a new printer service, with defaults coming from service iFrom.
727 bool lpcfg_add_printer(struct loadparm_context *lp_ctx,
728 const char *pszPrintername,
729 struct loadparm_service *default_service)
731 const char *comment = "From Printcap";
732 struct loadparm_service *service;
733 service = lpcfg_add_service(lp_ctx, default_service, pszPrintername);
735 if (service == NULL)
736 return false;
738 /* note that we do NOT default the availability flag to True - */
739 /* we take it from the default service passed. This allows all */
740 /* dynamic printers to be disabled by disabling the [printers] */
741 /* entry (if/when the 'available' keyword is implemented!). */
743 /* the printer name is set to the service name. */
744 lpcfg_string_set(service, &service->_printername, pszPrintername);
745 lpcfg_string_set(service, &service->comment, comment);
746 service->browseable = default_service->browseable;
747 /* Printers cannot be read_only. */
748 service->read_only = false;
749 /* Printer services must be printable. */
750 service->printable = true;
752 DEBUG(3, ("adding printer service %s\n", pszPrintername));
754 return true;
758 * Map a parameter's string representation to something we can use.
759 * Returns False if the parameter string is not recognised, else TRUE.
762 int lpcfg_map_parameter(const char *pszParmName)
764 int iIndex;
766 for (iIndex = 0; parm_table[iIndex].label; iIndex++)
767 if (strwicmp(parm_table[iIndex].label, pszParmName) == 0)
768 return iIndex;
770 /* Warn only if it isn't parametric option */
771 if (strchr(pszParmName, ':') == NULL)
772 DEBUG(0, ("Unknown parameter encountered: \"%s\"\n", pszParmName));
773 /* We do return 'fail' for parametric options as well because they are
774 stored in different storage
776 return -1;
781 return the parameter structure for a parameter
783 struct parm_struct *lpcfg_parm_struct(struct loadparm_context *lp_ctx, const char *name)
785 int num = lpcfg_map_parameter(name);
787 if (num < 0) {
788 return NULL;
791 return &parm_table[num];
795 return the parameter pointer for a parameter
797 void *lpcfg_parm_ptr(struct loadparm_context *lp_ctx,
798 struct loadparm_service *service, struct parm_struct *parm)
800 if (lp_ctx->s3_fns) {
801 return lp_ctx->s3_fns->get_parm_ptr(service, parm);
804 if (service == NULL) {
805 if (parm->p_class == P_LOCAL)
806 return ((char *)lp_ctx->sDefault)+parm->offset;
807 else if (parm->p_class == P_GLOBAL)
808 return ((char *)lp_ctx->globals)+parm->offset;
809 else return NULL;
810 } else {
811 return ((char *)service) + parm->offset;
816 return the parameter pointer for a parameter
818 bool lpcfg_parm_is_cmdline(struct loadparm_context *lp_ctx, const char *name)
820 int parmnum;
822 parmnum = lpcfg_map_parameter(name);
823 if (parmnum == -1) return false;
825 return lp_ctx->flags[parmnum] & FLAG_CMDLINE;
829 * Find a service by name. Otherwise works like get_service.
832 static struct loadparm_service *lpcfg_getservicebyname(struct loadparm_context *lp_ctx,
833 const char *pszServiceName)
835 int iService;
837 if (lp_ctx->s3_fns) {
838 return lp_ctx->s3_fns->get_service(pszServiceName);
841 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--)
842 if (lp_ctx->services[iService] != NULL &&
843 strwicmp(lp_ctx->services[iService]->szService, pszServiceName) == 0) {
844 return lp_ctx->services[iService];
847 return NULL;
851 * Add a parametric option to a parmlist_entry,
852 * replacing old value, if already present.
854 void set_param_opt(TALLOC_CTX *mem_ctx,
855 struct parmlist_entry **opt_list,
856 const char *opt_name,
857 const char *opt_value,
858 unsigned priority)
860 struct parmlist_entry *new_opt, *opt;
862 opt = *opt_list;
864 /* Traverse destination */
865 while (opt) {
866 /* If we already have same option, override it */
867 if (strwicmp(opt->key, opt_name) == 0) {
868 if ((opt->priority & FLAG_CMDLINE) &&
869 !(priority & FLAG_CMDLINE)) {
870 /* it's been marked as not to be
871 overridden */
872 return;
874 TALLOC_FREE(opt->list);
875 lpcfg_string_set(opt, &opt->value, opt_value);
876 opt->priority = priority;
877 return;
879 opt = opt->next;
882 new_opt = talloc_pooled_object(
883 mem_ctx, struct parmlist_entry,
884 2, strlen(opt_name) + 1 + strlen(opt_value) + 1);
885 if (new_opt == NULL) {
886 smb_panic("OOM");
888 new_opt->key = NULL;
889 lpcfg_string_set(new_opt, &new_opt->key, opt_name);
890 new_opt->value = NULL;
891 lpcfg_string_set(new_opt, &new_opt->value, opt_value);
893 new_opt->list = NULL;
894 new_opt->priority = priority;
895 DLIST_ADD(*opt_list, new_opt);
899 * Copy a service structure to another.
900 * If pcopymapDest is NULL then copy all fields
903 void copy_service(struct loadparm_service *pserviceDest,
904 const struct loadparm_service *pserviceSource,
905 struct bitmap *pcopymapDest)
907 int i;
908 bool bcopyall = (pcopymapDest == NULL);
909 struct parmlist_entry *data;
911 for (i = 0; parm_table[i].label; i++)
912 if (parm_table[i].p_class == P_LOCAL &&
913 (bcopyall || bitmap_query(pcopymapDest, i))) {
914 const void *src_ptr =
915 ((const char *)pserviceSource) + parm_table[i].offset;
916 void *dest_ptr =
917 ((char *)pserviceDest) + parm_table[i].offset;
919 switch (parm_table[i].type) {
920 case P_BOOL:
921 case P_BOOLREV:
922 *(bool *)dest_ptr = *(const bool *)src_ptr;
923 break;
925 case P_INTEGER:
926 case P_BYTES:
927 case P_OCTAL:
928 case P_ENUM:
929 *(int *)dest_ptr = *(const int *)src_ptr;
930 break;
932 case P_CHAR:
933 *(char *)dest_ptr = *(const char *)src_ptr;
934 break;
936 case P_STRING:
937 lpcfg_string_set(pserviceDest,
938 (char **)dest_ptr,
939 *(const char * const *)src_ptr);
940 break;
942 case P_USTRING:
943 lpcfg_string_set_upper(pserviceDest,
944 (char **)dest_ptr,
945 *(const char * const *)src_ptr);
946 break;
947 case P_CMDLIST:
948 case P_LIST:
949 TALLOC_FREE(*((char ***)dest_ptr));
950 *(char ***)dest_ptr = str_list_copy(pserviceDest,
951 *discard_const_p(const char **, src_ptr));
952 break;
953 default:
954 break;
958 if (bcopyall) {
959 init_copymap(pserviceDest);
960 if (pserviceSource->copymap)
961 bitmap_copy(pserviceDest->copymap,
962 pserviceSource->copymap);
965 for (data = pserviceSource->param_opt; data != NULL; data = data->next) {
966 set_param_opt(pserviceDest, &pserviceDest->param_opt,
967 data->key, data->value, data->priority);
972 * Check a service for consistency. Return False if the service is in any way
973 * incomplete or faulty, else True.
975 bool lpcfg_service_ok(struct loadparm_service *service)
977 bool bRetval;
979 bRetval = true;
980 if (service->szService[0] == '\0') {
981 DEBUG(0, ("The following message indicates an internal error:\n"));
982 DEBUG(0, ("No service name in service entry.\n"));
983 bRetval = false;
986 /* The [printers] entry MUST be printable. I'm all for flexibility, but */
987 /* I can't see why you'd want a non-printable printer service... */
988 if (strwicmp(service->szService, PRINTERS_NAME) == 0) {
989 if (!service->printable) {
990 DEBUG(0, ("WARNING: [%s] service MUST be printable!\n",
991 service->szService));
992 service->printable = true;
994 /* [printers] service must also be non-browsable. */
995 if (service->browseable)
996 service->browseable = false;
999 if (service->path[0] == '\0' &&
1000 strwicmp(service->szService, HOMES_NAME) != 0 &&
1001 service->msdfs_proxy[0] == '\0')
1003 DEBUG(0, ("WARNING: No path in service %s - making it unavailable!\n",
1004 service->szService));
1005 service->available = false;
1008 if (!service->available)
1009 DEBUG(1, ("NOTE: Service %s is flagged unavailable.\n",
1010 service->szService));
1012 return bRetval;
1016 /*******************************************************************
1017 Keep a linked list of all config files so we know when one has changed
1018 it's date and needs to be reloaded.
1019 ********************************************************************/
1021 void add_to_file_list(TALLOC_CTX *mem_ctx, struct file_lists **list,
1022 const char *fname, const char *subfname)
1024 struct file_lists *f = *list;
1026 while (f) {
1027 if (f->name && !strcmp(f->name, fname))
1028 break;
1029 f = f->next;
1032 if (!f) {
1033 f = talloc(mem_ctx, struct file_lists);
1034 if (!f)
1035 goto fail;
1036 f->next = *list;
1037 f->name = talloc_strdup(f, fname);
1038 if (!f->name) {
1039 TALLOC_FREE(f);
1040 goto fail;
1042 f->subfname = talloc_strdup(f, subfname);
1043 if (!f->subfname) {
1044 TALLOC_FREE(f);
1045 goto fail;
1047 *list = f;
1048 f->modtime = file_modtime(subfname);
1049 } else {
1050 time_t t = file_modtime(subfname);
1051 if (t)
1052 f->modtime = t;
1054 return;
1056 fail:
1057 DEBUG(0, ("Unable to add file to file list: %s\n", fname));
1061 /*******************************************************************
1062 Check if a config file has changed date.
1063 ********************************************************************/
1064 bool lpcfg_file_list_changed(struct loadparm_context *lp_ctx)
1066 struct file_lists *f;
1067 DEBUG(6, ("lpcfg_file_list_changed()\n"));
1069 for (f = lp_ctx->file_lists; f != NULL; f = f->next) {
1070 char *n2;
1071 time_t mod_time;
1073 n2 = standard_sub_basic(lp_ctx, f->name);
1075 DEBUGADD(6, ("file %s -> %s last mod_time: %s\n",
1076 f->name, n2, ctime(&f->modtime)));
1078 mod_time = file_modtime(n2);
1080 if (mod_time && ((f->modtime != mod_time) || (f->subfname == NULL) || (strcmp(n2, f->subfname) != 0))) {
1081 DEBUGADD(6, ("file %s modified: %s\n", n2,
1082 ctime(&mod_time)));
1083 f->modtime = mod_time;
1084 talloc_free(f->subfname);
1085 f->subfname = talloc_strdup(f, n2);
1086 TALLOC_FREE(n2);
1087 return true;
1089 TALLOC_FREE(n2);
1091 return false;
1095 * set the value for a P_ENUM
1097 bool lp_set_enum_parm( struct parm_struct *parm, const char *pszParmValue,
1098 int *ptr )
1100 int i;
1102 for (i = 0; parm->enum_list[i].name; i++) {
1103 if (strwicmp(pszParmValue, parm->enum_list[i].name) == 0) {
1104 *ptr = parm->enum_list[i].value;
1105 return true;
1108 DEBUG(0, ("WARNING: Ignoring invalid value '%s' for parameter '%s'\n",
1109 pszParmValue, parm->label));
1110 return false;
1114 /***************************************************************************
1115 Handle the "realm" parameter
1116 ***************************************************************************/
1118 bool handle_realm(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1119 const char *pszParmValue, char **ptr)
1121 char *upper;
1122 char *lower;
1124 upper = strupper_talloc(lp_ctx, pszParmValue);
1125 if (upper == NULL) {
1126 return false;
1129 lower = strlower_talloc(lp_ctx, pszParmValue);
1130 if (lower == NULL) {
1131 TALLOC_FREE(upper);
1132 return false;
1135 lpcfg_string_set(lp_ctx->globals->ctx, &lp_ctx->globals->realm_original, pszParmValue);
1136 lpcfg_string_set(lp_ctx->globals->ctx, &lp_ctx->globals->realm, upper);
1137 lpcfg_string_set(lp_ctx->globals->ctx, &lp_ctx->globals->dnsdomain, lower);
1139 return true;
1142 /***************************************************************************
1143 Handle the include operation.
1144 ***************************************************************************/
1146 bool handle_include(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1147 const char *pszParmValue, char **ptr)
1149 char *fname;
1150 const char *substitution_variable_substring;
1151 char next_char;
1153 if (lp_ctx->s3_fns) {
1154 return lp_ctx->s3_fns->lp_include(lp_ctx, service, pszParmValue, ptr);
1157 fname = standard_sub_basic(lp_ctx, pszParmValue);
1159 add_to_file_list(lp_ctx, &lp_ctx->file_lists, pszParmValue, fname);
1161 lpcfg_string_set(lp_ctx, ptr, fname);
1163 if (file_exist(fname))
1164 return pm_process(fname, do_section, lpcfg_do_parameter, lp_ctx);
1167 * If the file doesn't exist, we check that it isn't due to variable
1168 * substitution
1170 substitution_variable_substring = strchr(fname, '%');
1172 if (substitution_variable_substring != NULL) {
1173 next_char = substitution_variable_substring[1];
1174 if ((next_char >= 'a' && next_char <= 'z')
1175 || (next_char >= 'A' && next_char <= 'Z')) {
1176 DEBUG(2, ("Tried to load %s but variable substitution in "
1177 "filename, ignoring file.\n", fname));
1178 return true;
1182 DEBUG(2, ("Can't find include file %s\n", fname));
1184 return false;
1187 /***************************************************************************
1188 Handle the interpretation of the copy parameter.
1189 ***************************************************************************/
1191 bool handle_copy(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1192 const char *pszParmValue, char **ptr)
1194 bool bRetval;
1195 struct loadparm_service *serviceTemp = NULL;
1197 bRetval = false;
1199 DEBUG(3, ("Copying service from service %s\n", pszParmValue));
1201 serviceTemp = lpcfg_getservicebyname(lp_ctx, pszParmValue);
1203 if (service == NULL) {
1204 DEBUG(0, ("Unable to copy service - invalid service destination.\n"));
1205 return false;
1208 if (serviceTemp != NULL) {
1209 if (serviceTemp == service) {
1210 DEBUG(0, ("Can't copy service %s - unable to copy self!\n", pszParmValue));
1211 } else {
1212 copy_service(service,
1213 serviceTemp,
1214 service->copymap);
1215 lpcfg_string_set(service, ptr, pszParmValue);
1217 bRetval = true;
1219 } else {
1220 DEBUG(0, ("Unable to copy service - source not found: %s\n",
1221 pszParmValue));
1222 bRetval = false;
1225 return bRetval;
1228 bool handle_debug_list(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1229 const char *pszParmValue, char **ptr)
1231 lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1233 return debug_parse_levels(pszParmValue);
1236 bool handle_logfile(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1237 const char *pszParmValue, char **ptr)
1239 if (lp_ctx->s3_fns == NULL) {
1240 debug_set_logfile(pszParmValue);
1243 lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1245 return true;
1249 * These special charset handling methods only run in the source3 code.
1252 bool handle_charset(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1253 const char *pszParmValue, char **ptr)
1255 if (lp_ctx->s3_fns) {
1256 if (*ptr == NULL || strcmp(*ptr, pszParmValue) != 0) {
1257 global_iconv_handle = smb_iconv_handle_reinit(NULL,
1258 lpcfg_dos_charset(lp_ctx),
1259 lpcfg_unix_charset(lp_ctx),
1260 true, global_iconv_handle);
1264 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1268 bool handle_dos_charset(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1269 const char *pszParmValue, char **ptr)
1271 bool is_utf8 = false;
1272 size_t len = strlen(pszParmValue);
1274 if (lp_ctx->s3_fns) {
1275 if (len == 4 || len == 5) {
1276 /* Don't use StrCaseCmp here as we don't want to
1277 initialize iconv. */
1278 if ((toupper_m(pszParmValue[0]) == 'U') &&
1279 (toupper_m(pszParmValue[1]) == 'T') &&
1280 (toupper_m(pszParmValue[2]) == 'F')) {
1281 if (len == 4) {
1282 if (pszParmValue[3] == '8') {
1283 is_utf8 = true;
1285 } else {
1286 if (pszParmValue[3] == '-' &&
1287 pszParmValue[4] == '8') {
1288 is_utf8 = true;
1294 if (*ptr == NULL || strcmp(*ptr, pszParmValue) != 0) {
1295 if (is_utf8) {
1296 DEBUG(0,("ERROR: invalid DOS charset: 'dos charset' must not "
1297 "be UTF8, using (default value) %s instead.\n",
1298 DEFAULT_DOS_CHARSET));
1299 pszParmValue = DEFAULT_DOS_CHARSET;
1301 global_iconv_handle = smb_iconv_handle_reinit(NULL,
1302 lpcfg_dos_charset(lp_ctx),
1303 lpcfg_unix_charset(lp_ctx),
1304 true, global_iconv_handle);
1308 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1311 bool handle_printing(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1312 const char *pszParmValue, char **ptr)
1314 static int parm_num = -1;
1316 if (parm_num == -1) {
1317 parm_num = lpcfg_map_parameter("printing");
1320 if (!lp_set_enum_parm(&parm_table[parm_num], pszParmValue, (int*)ptr)) {
1321 return false;
1324 if (lp_ctx->s3_fns) {
1325 if (service == NULL) {
1326 init_printer_values(lp_ctx, lp_ctx->globals->ctx, lp_ctx->sDefault);
1327 } else {
1328 init_printer_values(lp_ctx, service, service);
1332 return true;
1335 bool handle_ldap_debug_level(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1336 const char *pszParmValue, char **ptr)
1338 lp_ctx->globals->ldap_debug_level = lp_int(pszParmValue);
1340 if (lp_ctx->s3_fns) {
1341 lp_ctx->s3_fns->init_ldap_debugging();
1343 return true;
1346 bool handle_netbios_aliases(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1347 const char *pszParmValue, char **ptr)
1349 TALLOC_FREE(lp_ctx->globals->netbios_aliases);
1350 lp_ctx->globals->netbios_aliases = str_list_make_v3_const(lp_ctx->globals->ctx,
1351 pszParmValue, NULL);
1353 if (lp_ctx->s3_fns) {
1354 return lp_ctx->s3_fns->set_netbios_aliases(lp_ctx->globals->netbios_aliases);
1356 return true;
1360 * idmap related parameters
1363 bool handle_idmap_backend(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1364 const char *pszParmValue, char **ptr)
1366 if (lp_ctx->s3_fns) {
1367 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : backend",
1368 pszParmValue, 0);
1371 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1374 bool handle_idmap_uid(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1375 const char *pszParmValue, char **ptr)
1377 if (lp_ctx->s3_fns) {
1378 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : range",
1379 pszParmValue, 0);
1382 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1385 bool handle_idmap_gid(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1386 const char *pszParmValue, char **ptr)
1388 if (lp_ctx->s3_fns) {
1389 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : range",
1390 pszParmValue, 0);
1393 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1396 bool handle_smb_ports(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1397 const char *pszParmValue, char **ptr)
1399 static int parm_num = -1;
1400 int i;
1401 const char **list;
1403 if (!pszParmValue || !*pszParmValue) {
1404 return false;
1407 if (parm_num == -1) {
1408 parm_num = lpcfg_map_parameter("smb ports");
1409 if (parm_num == -1) {
1410 return false;
1414 if(!set_variable_helper(lp_ctx->globals->ctx, parm_num, ptr, "smb ports",
1415 pszParmValue)) {
1416 return false;
1419 list = lp_ctx->globals->smb_ports;
1420 if (list == NULL) {
1421 return false;
1424 /* Check that each port is a valid integer and within range */
1425 for (i = 0; list[i] != NULL; i++) {
1426 char *end = NULL;
1427 int port = 0;
1428 port = strtol(list[i], &end, 10);
1429 if (*end != '\0' || port <= 0 || port > 65535) {
1430 TALLOC_FREE(list);
1431 return false;
1435 return true;
1438 bool handle_smb2_max_credits(struct loadparm_context *lp_ctx,
1439 struct loadparm_service *service,
1440 const char *pszParmValue, char **ptr)
1442 int value = lp_int(pszParmValue);
1444 if (value <= 0) {
1445 value = DEFAULT_SMB2_MAX_CREDITS;
1448 *(int *)ptr = value;
1450 return true;
1453 bool handle_cups_encrypt(struct loadparm_context *lp_ctx,
1454 struct loadparm_service *service,
1455 const char *pszParmValue, char **ptr)
1457 int result = 0;
1458 #ifdef HAVE_HTTPCONNECTENCRYPT
1459 int value = lp_int(pszParmValue);
1461 switch (value) {
1462 case Auto:
1463 result = HTTP_ENCRYPT_REQUIRED;
1464 break;
1465 case true:
1466 result = HTTP_ENCRYPT_ALWAYS;
1467 break;
1468 case false:
1469 result = HTTP_ENCRYPT_NEVER;
1470 break;
1471 default:
1472 result = 0;
1473 break;
1475 #endif
1476 *(int *)ptr = result;
1478 return true;
1481 /***************************************************************************
1482 Initialise a copymap.
1483 ***************************************************************************/
1486 * Initializes service copymap
1487 * Note: pservice *must* be valid TALLOC_CTX
1489 void init_copymap(struct loadparm_service *pservice)
1491 int i;
1493 TALLOC_FREE(pservice->copymap);
1495 pservice->copymap = bitmap_talloc(pservice, num_parameters());
1496 if (!pservice->copymap) {
1497 DEBUG(0,
1498 ("Couldn't allocate copymap!! (size %d)\n",
1499 (int)num_parameters()));
1500 } else {
1501 for (i = 0; i < num_parameters(); i++) {
1502 bitmap_set(pservice->copymap, i);
1508 * Process a parametric option
1510 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
1511 struct loadparm_service *service,
1512 const char *pszParmName,
1513 const char *pszParmValue, int flags)
1515 struct parmlist_entry **data;
1516 char *name;
1517 TALLOC_CTX *mem_ctx;
1519 while (isspace((unsigned char)*pszParmName)) {
1520 pszParmName++;
1523 name = strlower_talloc(lp_ctx, pszParmName);
1524 if (!name) return false;
1526 if (service == NULL) {
1527 data = &lp_ctx->globals->param_opt;
1529 * s3 code cannot deal with parametric options stored on the globals ctx.
1531 if (lp_ctx->s3_fns != NULL) {
1532 mem_ctx = NULL;
1533 } else {
1534 mem_ctx = lp_ctx->globals->ctx;
1536 } else {
1537 data = &service->param_opt;
1538 mem_ctx = service;
1541 set_param_opt(mem_ctx, data, name, pszParmValue, flags);
1543 talloc_free(name);
1545 return true;
1548 static bool set_variable_helper(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
1549 const char *pszParmName, const char *pszParmValue)
1551 int i;
1553 /* switch on the type of variable it is */
1554 switch (parm_table[parmnum].type)
1556 case P_BOOL: {
1557 bool b;
1558 if (!set_boolean(pszParmValue, &b)) {
1559 DEBUG(0, ("set_variable_helper(%s): value is not "
1560 "boolean!\n", pszParmValue));
1561 return false;
1563 *(bool *)parm_ptr = b;
1565 break;
1567 case P_BOOLREV: {
1568 bool b;
1569 if (!set_boolean(pszParmValue, &b)) {
1570 DEBUG(0, ("set_variable_helper(%s): value is not "
1571 "boolean!\n", pszParmValue));
1572 return false;
1574 *(bool *)parm_ptr = !b;
1576 break;
1578 case P_INTEGER:
1579 *(int *)parm_ptr = lp_int(pszParmValue);
1580 break;
1582 case P_CHAR:
1583 *(char *)parm_ptr = *pszParmValue;
1584 break;
1586 case P_OCTAL:
1587 i = sscanf(pszParmValue, "%o", (int *)parm_ptr);
1588 if ( i != 1 ) {
1589 DEBUG ( 0, ("Invalid octal number %s\n", pszParmName ));
1590 return false;
1592 break;
1594 case P_BYTES:
1596 uint64_t val;
1597 if (conv_str_size_error(pszParmValue, &val)) {
1598 if (val <= INT_MAX) {
1599 *(int *)parm_ptr = (int)val;
1600 break;
1604 DEBUG(0, ("set_variable_helper(%s): value is not "
1605 "a valid size specifier!\n", pszParmValue));
1606 return false;
1609 case P_CMDLIST:
1610 TALLOC_FREE(*(char ***)parm_ptr);
1611 *(char ***)parm_ptr = str_list_make_v3(mem_ctx,
1612 pszParmValue, NULL);
1613 break;
1615 case P_LIST:
1617 char **new_list = str_list_make_v3(mem_ctx,
1618 pszParmValue, NULL);
1619 if (new_list == NULL) {
1620 break;
1623 for (i=0; new_list[i]; i++) {
1624 if (*(const char ***)parm_ptr != NULL &&
1625 new_list[i][0] == '+' &&
1626 new_list[i][1])
1628 if (!str_list_check(*(const char ***)parm_ptr,
1629 &new_list[i][1])) {
1630 *(const char ***)parm_ptr = str_list_add(*(const char ***)parm_ptr,
1631 &new_list[i][1]);
1633 } else if (*(const char ***)parm_ptr != NULL &&
1634 new_list[i][0] == '-' &&
1635 new_list[i][1])
1637 str_list_remove(*(const char ***)parm_ptr,
1638 &new_list[i][1]);
1639 } else {
1640 if (i != 0) {
1641 DEBUG(0, ("Unsupported list syntax for: %s = %s\n",
1642 pszParmName, pszParmValue));
1643 return false;
1645 *(char ***)parm_ptr = new_list;
1646 break;
1649 break;
1652 case P_STRING:
1653 lpcfg_string_set(mem_ctx, (char **)parm_ptr, pszParmValue);
1654 break;
1656 case P_USTRING:
1657 lpcfg_string_set_upper(mem_ctx, (char **)parm_ptr, pszParmValue);
1658 break;
1660 case P_ENUM:
1661 if (!lp_set_enum_parm(&parm_table[parmnum], pszParmValue, (int*)parm_ptr)) {
1662 return false;
1664 break;
1668 return true;
1672 static bool set_variable(TALLOC_CTX *mem_ctx, struct loadparm_service *service,
1673 int parmnum, void *parm_ptr,
1674 const char *pszParmName, const char *pszParmValue,
1675 struct loadparm_context *lp_ctx, bool on_globals)
1677 int i;
1678 bool ok;
1680 /* if it is a special case then go ahead */
1681 if (parm_table[parmnum].special) {
1682 ok = parm_table[parmnum].special(lp_ctx, service, pszParmValue,
1683 (char **)parm_ptr);
1684 } else {
1685 ok = set_variable_helper(mem_ctx, parmnum, parm_ptr,
1686 pszParmName, pszParmValue);
1689 if (!ok) {
1690 return false;
1693 if (on_globals && (lp_ctx->flags[parmnum] & FLAG_DEFAULT)) {
1694 lp_ctx->flags[parmnum] &= ~FLAG_DEFAULT;
1695 /* we have to also unset FLAG_DEFAULT on aliases */
1696 for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
1697 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1699 for (i=parmnum+1;i<num_parameters() && parm_table[i].offset == parm_table[parmnum].offset;i++) {
1700 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1703 return true;
1707 bool lpcfg_do_global_parameter(struct loadparm_context *lp_ctx,
1708 const char *pszParmName, const char *pszParmValue)
1710 int parmnum = lpcfg_map_parameter(pszParmName);
1711 void *parm_ptr;
1713 if (parmnum < 0) {
1714 if (strchr(pszParmName, ':')) {
1715 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName, pszParmValue, 0);
1717 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1718 return true;
1721 /* if the flag has been set on the command line, then don't allow override,
1722 but don't report an error */
1723 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1724 return true;
1727 if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
1728 DEBUG(1, ("WARNING: The \"%s\" option is deprecated\n",
1729 pszParmName));
1732 parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[parmnum]);
1734 return set_variable(lp_ctx->globals->ctx, NULL, parmnum, parm_ptr,
1735 pszParmName, pszParmValue, lp_ctx, true);
1738 bool lpcfg_do_service_parameter(struct loadparm_context *lp_ctx,
1739 struct loadparm_service *service,
1740 const char *pszParmName, const char *pszParmValue)
1742 void *parm_ptr;
1743 int i;
1744 int parmnum = lpcfg_map_parameter(pszParmName);
1746 if (parmnum < 0) {
1747 if (strchr(pszParmName, ':')) {
1748 return lp_do_parameter_parametric(lp_ctx, service, pszParmName, pszParmValue, 0);
1750 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1751 return true;
1754 /* if the flag has been set on the command line, then don't allow override,
1755 but don't report an error */
1756 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1757 return true;
1760 if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
1761 DEBUG(1, ("WARNING: The \"%s\" option is deprecated\n",
1762 pszParmName));
1765 if (parm_table[parmnum].p_class == P_GLOBAL) {
1766 DEBUG(0,
1767 ("Global parameter %s found in service section!\n",
1768 pszParmName));
1769 return true;
1771 parm_ptr = ((char *)service) + parm_table[parmnum].offset;
1773 if (!service->copymap)
1774 init_copymap(service);
1776 /* this handles the aliases - set the copymap for other
1777 * entries with the same data pointer */
1778 for (i = 0; parm_table[i].label; i++)
1779 if (parm_table[i].offset == parm_table[parmnum].offset &&
1780 parm_table[i].p_class == parm_table[parmnum].p_class)
1781 bitmap_clear(service->copymap, i);
1783 return set_variable(service, service, parmnum, parm_ptr, pszParmName,
1784 pszParmValue, lp_ctx, false);
1788 * Process a parameter.
1791 bool lpcfg_do_parameter(const char *pszParmName, const char *pszParmValue,
1792 void *userdata)
1794 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1796 if (lp_ctx->bInGlobalSection)
1797 return lpcfg_do_global_parameter(lp_ctx, pszParmName,
1798 pszParmValue);
1799 else
1800 return lpcfg_do_service_parameter(lp_ctx, lp_ctx->currentService,
1801 pszParmName, pszParmValue);
1805 variable argument do parameter
1807 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx, const char *pszParmName, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
1808 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx,
1809 const char *pszParmName, const char *fmt, ...)
1811 char *s;
1812 bool ret;
1813 va_list ap;
1815 va_start(ap, fmt);
1816 s = talloc_vasprintf(NULL, fmt, ap);
1817 va_end(ap);
1818 ret = lpcfg_do_global_parameter(lp_ctx, pszParmName, s);
1819 talloc_free(s);
1820 return ret;
1825 set a parameter from the commandline - this is called from command line parameter
1826 parsing code. It sets the parameter then marks the parameter as unable to be modified
1827 by smb.conf processing
1829 bool lpcfg_set_cmdline(struct loadparm_context *lp_ctx, const char *pszParmName,
1830 const char *pszParmValue)
1832 int parmnum;
1833 int i;
1835 while (isspace((unsigned char)*pszParmValue)) pszParmValue++;
1837 parmnum = lpcfg_map_parameter(pszParmName);
1839 if (parmnum < 0 && strchr(pszParmName, ':')) {
1840 /* set a parametric option */
1841 bool ok;
1842 ok = lp_do_parameter_parametric(lp_ctx, NULL, pszParmName,
1843 pszParmValue, FLAG_CMDLINE);
1844 if (lp_ctx->s3_fns != NULL) {
1845 if (ok) {
1846 lp_ctx->s3_fns->store_cmdline(pszParmName, pszParmValue);
1849 return ok;
1852 if (parmnum < 0) {
1853 DEBUG(0,("Unknown option '%s'\n", pszParmName));
1854 return false;
1857 /* reset the CMDLINE flag in case this has been called before */
1858 lp_ctx->flags[parmnum] &= ~FLAG_CMDLINE;
1860 if (!lpcfg_do_global_parameter(lp_ctx, pszParmName, pszParmValue)) {
1861 return false;
1864 lp_ctx->flags[parmnum] |= FLAG_CMDLINE;
1866 /* we have to also set FLAG_CMDLINE on aliases */
1867 for (i=parmnum-1;
1868 i>=0 && parm_table[i].p_class == parm_table[parmnum].p_class &&
1869 parm_table[i].offset == parm_table[parmnum].offset;
1870 i--) {
1871 lp_ctx->flags[i] |= FLAG_CMDLINE;
1873 for (i=parmnum+1;
1874 i<num_parameters() &&
1875 parm_table[i].p_class == parm_table[parmnum].p_class &&
1876 parm_table[i].offset == parm_table[parmnum].offset;
1877 i++) {
1878 lp_ctx->flags[i] |= FLAG_CMDLINE;
1881 if (lp_ctx->s3_fns != NULL) {
1882 lp_ctx->s3_fns->store_cmdline(pszParmName, pszParmValue);
1885 return true;
1889 set a option from the commandline in 'a=b' format. Use to support --option
1891 bool lpcfg_set_option(struct loadparm_context *lp_ctx, const char *option)
1893 char *p, *s;
1894 bool ret;
1896 s = talloc_strdup(NULL, option);
1897 if (!s) {
1898 return false;
1901 p = strchr(s, '=');
1902 if (!p) {
1903 talloc_free(s);
1904 return false;
1907 *p = 0;
1909 ret = lpcfg_set_cmdline(lp_ctx, s, p+1);
1910 talloc_free(s);
1911 return ret;
1915 #define BOOLSTR(b) ((b) ? "Yes" : "No")
1918 * Print a parameter of the specified type.
1921 void lpcfg_print_parameter(struct parm_struct *p, void *ptr, FILE * f)
1923 /* For the seperation of lists values that we print below */
1924 const char *list_sep = ", ";
1925 int i;
1926 switch (p->type)
1928 case P_ENUM:
1929 for (i = 0; p->enum_list[i].name; i++) {
1930 if (*(int *)ptr == p->enum_list[i].value) {
1931 fprintf(f, "%s",
1932 p->enum_list[i].name);
1933 break;
1936 break;
1938 case P_BOOL:
1939 fprintf(f, "%s", BOOLSTR(*(bool *)ptr));
1940 break;
1942 case P_BOOLREV:
1943 fprintf(f, "%s", BOOLSTR(!*(bool *)ptr));
1944 break;
1946 case P_INTEGER:
1947 case P_BYTES:
1948 fprintf(f, "%d", *(int *)ptr);
1949 break;
1951 case P_CHAR:
1952 fprintf(f, "%c", *(char *)ptr);
1953 break;
1955 case P_OCTAL: {
1956 int val = *(int *)ptr;
1957 if (val == -1) {
1958 fprintf(f, "-1");
1959 } else {
1960 fprintf(f, "0%03o", val);
1962 break;
1965 case P_CMDLIST:
1966 list_sep = " ";
1967 /* fall through */
1968 case P_LIST:
1969 if ((char ***)ptr && *(char ***)ptr) {
1970 char **list = *(char ***)ptr;
1971 for (; *list; list++) {
1972 /* surround strings with whitespace in double quotes */
1973 if (*(list+1) == NULL) {
1974 /* last item, no extra separator */
1975 list_sep = "";
1977 if ( strchr_m( *list, ' ' ) ) {
1978 fprintf(f, "\"%s\"%s", *list, list_sep);
1979 } else {
1980 fprintf(f, "%s%s", *list, list_sep);
1984 break;
1986 case P_STRING:
1987 case P_USTRING:
1988 if (*(char **)ptr) {
1989 fprintf(f, "%s", *(char **)ptr);
1991 break;
1996 * Check if two parameters are equal.
1999 static bool lpcfg_equal_parameter(parm_type type, void *ptr1, void *ptr2)
2001 switch (type) {
2002 case P_BOOL:
2003 case P_BOOLREV:
2004 return (*((bool *)ptr1) == *((bool *)ptr2));
2006 case P_INTEGER:
2007 case P_ENUM:
2008 case P_OCTAL:
2009 case P_BYTES:
2010 return (*((int *)ptr1) == *((int *)ptr2));
2012 case P_CHAR:
2013 return (*((char *)ptr1) == *((char *)ptr2));
2015 case P_LIST:
2016 case P_CMDLIST:
2017 return str_list_equal(*(const char ***)ptr1, *(const char ***)ptr2);
2019 case P_STRING:
2020 case P_USTRING:
2022 char *p1 = *(char **)ptr1, *p2 = *(char **)ptr2;
2023 if (p1 && !*p1)
2024 p1 = NULL;
2025 if (p2 && !*p2)
2026 p2 = NULL;
2027 return (p1 == p2 || strequal(p1, p2));
2030 return false;
2034 * Process a new section (service).
2036 * At this stage all sections are services.
2037 * Later we'll have special sections that permit server parameters to be set.
2038 * Returns True on success, False on failure.
2041 static bool do_section(const char *pszSectionName, void *userdata)
2043 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
2044 bool bRetval;
2045 bool isglobal;
2047 if (lp_ctx->s3_fns != NULL) {
2048 return lp_ctx->s3_fns->do_section(pszSectionName, lp_ctx);
2051 isglobal = ((strwicmp(pszSectionName, GLOBAL_NAME) == 0) ||
2052 (strwicmp(pszSectionName, GLOBAL_NAME2) == 0));
2054 bRetval = false;
2056 /* if we've just struck a global section, note the fact. */
2057 lp_ctx->bInGlobalSection = isglobal;
2059 /* check for multiple global sections */
2060 if (lp_ctx->bInGlobalSection) {
2061 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
2062 return true;
2065 /* if we have a current service, tidy it up before moving on */
2066 bRetval = true;
2068 if (lp_ctx->currentService != NULL)
2069 bRetval = lpcfg_service_ok(lp_ctx->currentService);
2071 /* if all is still well, move to the next record in the services array */
2072 if (bRetval) {
2073 /* We put this here to avoid an odd message order if messages are */
2074 /* issued by the post-processing of a previous section. */
2075 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
2077 if ((lp_ctx->currentService = lpcfg_add_service(lp_ctx, lp_ctx->sDefault,
2078 pszSectionName))
2079 == NULL) {
2080 DEBUG(0, ("Failed to add a new service\n"));
2081 return false;
2085 return bRetval;
2090 * Determine if a particular base parameter is currently set to the default value.
2093 static bool is_default(void *base_structure, int i)
2095 void *def_ptr = ((char *)base_structure) + parm_table[i].offset;
2096 switch (parm_table[i].type) {
2097 case P_CMDLIST:
2098 case P_LIST:
2099 return str_list_equal((const char * const *)parm_table[i].def.lvalue,
2100 *(const char * const **)def_ptr);
2101 case P_STRING:
2102 case P_USTRING:
2103 return strequal(parm_table[i].def.svalue,
2104 *(char **)def_ptr);
2105 case P_BOOL:
2106 case P_BOOLREV:
2107 return parm_table[i].def.bvalue ==
2108 *(bool *)def_ptr;
2109 case P_INTEGER:
2110 case P_CHAR:
2111 case P_OCTAL:
2112 case P_BYTES:
2113 case P_ENUM:
2114 return parm_table[i].def.ivalue ==
2115 *(int *)def_ptr;
2117 return false;
2121 *Display the contents of the global structure.
2124 void lpcfg_dump_globals(struct loadparm_context *lp_ctx, FILE *f,
2125 bool show_defaults)
2127 int i;
2128 struct parmlist_entry *data;
2130 fprintf(f, "# Global parameters\n[global]\n");
2132 for (i = 0; parm_table[i].label; i++) {
2133 if (parm_table[i].p_class != P_GLOBAL) {
2134 continue;
2137 if (parm_table[i].flags & FLAG_SYNONYM) {
2138 continue;
2141 if (!show_defaults) {
2142 if (lp_ctx->flags && (lp_ctx->flags[i] & FLAG_DEFAULT)) {
2143 continue;
2146 if (is_default(lp_ctx->globals, i)) {
2147 continue;
2151 fprintf(f, "\t%s = ", parm_table[i].label);
2152 lpcfg_print_parameter(&parm_table[i], lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[i]), f);
2153 fprintf(f, "\n");
2155 if (lp_ctx->globals->param_opt != NULL) {
2156 for (data = lp_ctx->globals->param_opt; data;
2157 data = data->next) {
2158 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
2159 continue;
2161 fprintf(f, "\t%s = %s\n", data->key, data->value);
2168 * Display the contents of a single services record.
2171 void lpcfg_dump_a_service(struct loadparm_service * pService, struct loadparm_service *sDefault, FILE * f,
2172 unsigned int *flags, bool show_defaults)
2174 int i;
2175 struct parmlist_entry *data;
2177 if (pService != sDefault)
2178 fprintf(f, "\n[%s]\n", pService->szService);
2180 for (i = 0; parm_table[i].label; i++) {
2181 if (parm_table[i].p_class != P_LOCAL) {
2182 continue;
2185 if (parm_table[i].flags & FLAG_SYNONYM) {
2186 continue;
2189 if (*parm_table[i].label == '-') {
2190 continue;
2193 if (pService == sDefault) {
2194 if (!show_defaults) {
2195 if (flags && (flags[i] & FLAG_DEFAULT)) {
2196 continue;
2199 if (is_default(sDefault, i)) {
2200 continue;
2203 } else {
2204 bool equal;
2206 equal = lpcfg_equal_parameter(parm_table[i].type,
2207 ((char *)pService) +
2208 parm_table[i].offset,
2209 ((char *)sDefault) +
2210 parm_table[i].offset);
2211 if (equal) {
2212 continue;
2216 fprintf(f, "\t%s = ", parm_table[i].label);
2217 lpcfg_print_parameter(&parm_table[i],
2218 ((char *)pService) + parm_table[i].offset, f);
2219 fprintf(f, "\n");
2221 if (pService->param_opt != NULL) {
2222 for (data = pService->param_opt; data; data = data->next) {
2223 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
2224 continue;
2226 fprintf(f, "\t%s = %s\n", data->key, data->value);
2231 bool lpcfg_dump_a_parameter(struct loadparm_context *lp_ctx,
2232 struct loadparm_service *service,
2233 const char *parm_name, FILE * f)
2235 struct parm_struct *parm;
2236 void *ptr;
2237 char *local_parm_name;
2238 char *parm_opt;
2239 const char *parm_opt_value;
2241 /* check for parametrical option */
2242 local_parm_name = talloc_strdup(lp_ctx, parm_name);
2243 if (local_parm_name == NULL) {
2244 return false;
2247 parm_opt = strchr( local_parm_name, ':');
2249 if (parm_opt) {
2250 *parm_opt = '\0';
2251 parm_opt++;
2252 if (strlen(parm_opt)) {
2253 parm_opt_value = lpcfg_parm_string(lp_ctx, service,
2254 local_parm_name, parm_opt);
2255 if (parm_opt_value) {
2256 fprintf(f, "%s\n", parm_opt_value);
2257 return true;
2260 return false;
2263 /* parameter is not parametric, search the table */
2264 parm = lpcfg_parm_struct(lp_ctx, parm_name);
2265 if (!parm) {
2266 return false;
2269 if (service != NULL && parm->p_class == P_GLOBAL) {
2270 return false;
2273 ptr = lpcfg_parm_ptr(lp_ctx, service,parm);
2275 lpcfg_print_parameter(parm, ptr, f);
2276 fprintf(f, "\n");
2277 return true;
2281 * Auto-load some home services.
2283 static void lpcfg_add_auto_services(struct loadparm_context *lp_ctx,
2284 const char *str)
2286 return;
2289 /***************************************************************************
2290 Initialise the sDefault parameter structure for the printer values.
2291 ***************************************************************************/
2293 void init_printer_values(struct loadparm_context *lp_ctx, TALLOC_CTX *ctx,
2294 struct loadparm_service *pService)
2296 /* choose defaults depending on the type of printing */
2297 switch (pService->printing) {
2298 case PRINT_BSD:
2299 case PRINT_AIX:
2300 case PRINT_LPRNT:
2301 case PRINT_LPROS2:
2302 lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P'%p'");
2303 lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P'%p' %j");
2304 lpcfg_string_set(ctx, &pService->print_command, "lpr -r -P'%p' %s");
2305 break;
2307 case PRINT_LPRNG:
2308 case PRINT_PLP:
2309 lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P'%p'");
2310 lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P'%p' %j");
2311 lpcfg_string_set(ctx, &pService->print_command, "lpr -r -P'%p' %s");
2312 lpcfg_string_set(ctx, &pService->queuepause_command, "lpc stop '%p'");
2313 lpcfg_string_set(ctx, &pService->queueresume_command, "lpc start '%p'");
2314 lpcfg_string_set(ctx, &pService->lppause_command, "lpc hold '%p' %j");
2315 lpcfg_string_set(ctx, &pService->lpresume_command, "lpc release '%p' %j");
2316 break;
2318 case PRINT_CUPS:
2319 case PRINT_IPRINT:
2320 /* set the lpq command to contain the destination printer
2321 name only. This is used by cups_queue_get() */
2322 lpcfg_string_set(ctx, &pService->lpq_command, "%p");
2323 lpcfg_string_set(ctx, &pService->lprm_command, "");
2324 lpcfg_string_set(ctx, &pService->print_command, "");
2325 lpcfg_string_set(ctx, &pService->lppause_command, "");
2326 lpcfg_string_set(ctx, &pService->lpresume_command, "");
2327 lpcfg_string_set(ctx, &pService->queuepause_command, "");
2328 lpcfg_string_set(ctx, &pService->queueresume_command, "");
2329 break;
2331 case PRINT_SYSV:
2332 case PRINT_HPUX:
2333 lpcfg_string_set(ctx, &pService->lpq_command, "lpstat -o%p");
2334 lpcfg_string_set(ctx, &pService->lprm_command, "cancel %p-%j");
2335 lpcfg_string_set(ctx, &pService->print_command, "lp -c -d%p %s; rm %s");
2336 lpcfg_string_set(ctx, &pService->queuepause_command, "disable %p");
2337 lpcfg_string_set(ctx, &pService->queueresume_command, "enable %p");
2338 #ifndef HPUX
2339 lpcfg_string_set(ctx, &pService->lppause_command, "lp -i %p-%j -H hold");
2340 lpcfg_string_set(ctx, &pService->lpresume_command, "lp -i %p-%j -H resume");
2341 #endif /* HPUX */
2342 break;
2344 case PRINT_QNX:
2345 lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P%p");
2346 lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P%p %j");
2347 lpcfg_string_set(ctx, &pService->print_command, "lp -r -P%p %s");
2348 break;
2350 #if defined(DEVELOPER) || defined(ENABLE_SELFTEST)
2352 case PRINT_TEST:
2353 case PRINT_VLP: {
2354 const char *tdbfile;
2355 TALLOC_CTX *tmp_ctx = talloc_new(ctx);
2356 const char *tmp;
2358 tmp = lpcfg_parm_string(lp_ctx, NULL, "vlp", "tdbfile");
2359 if (tmp == NULL) {
2360 tmp = "/tmp/vlp.tdb";
2363 tdbfile = talloc_asprintf(tmp_ctx, "tdbfile=%s", tmp);
2364 if (tdbfile == NULL) {
2365 tdbfile="tdbfile=/tmp/vlp.tdb";
2368 tmp = talloc_asprintf(tmp_ctx, "vlp %s print %%p %%s",
2369 tdbfile);
2370 lpcfg_string_set(ctx, &pService->print_command,
2371 tmp ? tmp : "vlp print %p %s");
2373 tmp = talloc_asprintf(tmp_ctx, "vlp %s lpq %%p",
2374 tdbfile);
2375 lpcfg_string_set(ctx, &pService->lpq_command,
2376 tmp ? tmp : "vlp lpq %p");
2378 tmp = talloc_asprintf(tmp_ctx, "vlp %s lprm %%p %%j",
2379 tdbfile);
2380 lpcfg_string_set(ctx, &pService->lprm_command,
2381 tmp ? tmp : "vlp lprm %p %j");
2383 tmp = talloc_asprintf(tmp_ctx, "vlp %s lppause %%p %%j",
2384 tdbfile);
2385 lpcfg_string_set(ctx, &pService->lppause_command,
2386 tmp ? tmp : "vlp lppause %p %j");
2388 tmp = talloc_asprintf(tmp_ctx, "vlp %s lpresume %%p %%j",
2389 tdbfile);
2390 lpcfg_string_set(ctx, &pService->lpresume_command,
2391 tmp ? tmp : "vlp lpresume %p %j");
2393 tmp = talloc_asprintf(tmp_ctx, "vlp %s queuepause %%p",
2394 tdbfile);
2395 lpcfg_string_set(ctx, &pService->queuepause_command,
2396 tmp ? tmp : "vlp queuepause %p");
2398 tmp = talloc_asprintf(tmp_ctx, "vlp %s queueresume %%p",
2399 tdbfile);
2400 lpcfg_string_set(ctx, &pService->queueresume_command,
2401 tmp ? tmp : "vlp queueresume %p");
2402 TALLOC_FREE(tmp_ctx);
2404 break;
2406 #endif /* DEVELOPER */
2412 * Unload unused services.
2415 void lpcfg_killunused(struct loadparm_context *lp_ctx,
2416 struct smbsrv_connection *smb,
2417 bool (*snumused) (struct smbsrv_connection *, int))
2419 int i;
2421 if (lp_ctx->s3_fns != NULL) {
2422 smb_panic("Cannot be used from an s3 loadparm ctx");
2425 for (i = 0; i < lp_ctx->iNumServices; i++) {
2426 if (lp_ctx->services[i] == NULL)
2427 continue;
2429 if (!snumused || !snumused(smb, i)) {
2430 talloc_free(lp_ctx->services[i]);
2431 lp_ctx->services[i] = NULL;
2437 static int lpcfg_destructor(struct loadparm_context *lp_ctx)
2439 struct parmlist_entry *data;
2441 if (lp_ctx->refuse_free) {
2442 /* someone is trying to free the
2443 global_loadparm_context.
2444 We can't allow that. */
2445 return -1;
2448 if (lp_ctx->globals->param_opt != NULL) {
2449 struct parmlist_entry *next;
2450 for (data = lp_ctx->globals->param_opt; data; data=next) {
2451 next = data->next;
2452 if (data->priority & FLAG_CMDLINE) continue;
2453 DLIST_REMOVE(lp_ctx->globals->param_opt, data);
2454 talloc_free(data);
2458 return 0;
2461 struct defaults_hook_data {
2462 const char *name;
2463 lpcfg_defaults_hook hook;
2464 struct defaults_hook_data *prev, *next;
2465 } *defaults_hooks = NULL;
2468 bool lpcfg_register_defaults_hook(const char *name, lpcfg_defaults_hook hook)
2470 struct defaults_hook_data *hook_data = talloc(talloc_autofree_context(),
2471 struct defaults_hook_data);
2472 hook_data->name = talloc_strdup(hook_data, name);
2473 hook_data->hook = hook;
2474 DLIST_ADD(defaults_hooks, hook_data);
2475 return false;
2479 * Initialise the global parameter structure.
2481 * Note that most callers should use loadparm_init_global() instead
2483 struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
2485 int i;
2486 char *myname;
2487 struct loadparm_context *lp_ctx;
2488 struct parmlist_entry *parm;
2489 char *logfile;
2490 struct defaults_hook_data *defaults_hook;
2492 lp_ctx = talloc_zero(mem_ctx, struct loadparm_context);
2493 if (lp_ctx == NULL)
2494 return NULL;
2496 talloc_set_destructor(lp_ctx, lpcfg_destructor);
2497 lp_ctx->bInGlobalSection = true;
2498 lp_ctx->globals = talloc_zero(lp_ctx, struct loadparm_global);
2499 /* This appears odd, but globals in s3 isn't a pointer */
2500 lp_ctx->globals->ctx = lp_ctx->globals;
2501 lp_ctx->sDefault = talloc_zero(lp_ctx, struct loadparm_service);
2502 lp_ctx->flags = talloc_zero_array(lp_ctx, unsigned int, num_parameters());
2504 lp_ctx->sDefault->max_print_jobs = 1000;
2505 lp_ctx->sDefault->available = true;
2506 lp_ctx->sDefault->browseable = true;
2507 lp_ctx->sDefault->read_only = true;
2508 lp_ctx->sDefault->map_archive = true;
2509 lp_ctx->sDefault->strict_locking = true;
2510 lp_ctx->sDefault->oplocks = true;
2511 lp_ctx->sDefault->create_mask = 0744;
2512 lp_ctx->sDefault->force_create_mode = 0000;
2513 lp_ctx->sDefault->directory_mask = 0755;
2514 lp_ctx->sDefault->force_directory_mode = 0000;
2516 DEBUG(3, ("Initialising global parameters\n"));
2518 for (i = 0; parm_table[i].label; i++) {
2519 if ((parm_table[i].type == P_STRING ||
2520 parm_table[i].type == P_USTRING) &&
2521 !(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2522 TALLOC_CTX *parent_mem;
2523 char **r;
2524 if (parm_table[i].p_class == P_LOCAL) {
2525 parent_mem = lp_ctx->sDefault;
2526 r = (char **)(((char *)lp_ctx->sDefault) + parm_table[i].offset);
2527 } else {
2528 parent_mem = lp_ctx->globals;
2529 r = (char **)(((char *)lp_ctx->globals) + parm_table[i].offset);
2531 lpcfg_string_set(parent_mem, r, "");
2535 logfile = talloc_asprintf(lp_ctx, "%s/log.samba", dyn_LOGFILEBASE);
2536 lpcfg_do_global_parameter(lp_ctx, "log file", logfile);
2537 talloc_free(logfile);
2539 lpcfg_do_global_parameter(lp_ctx, "log level", "0");
2541 lpcfg_do_global_parameter(lp_ctx, "syslog", "1");
2542 lpcfg_do_global_parameter(lp_ctx, "syslog only", "No");
2543 lpcfg_do_global_parameter(lp_ctx, "debug timestamp", "Yes");
2544 lpcfg_do_global_parameter(lp_ctx, "debug prefix timestamp", "No");
2545 lpcfg_do_global_parameter(lp_ctx, "debug hires timestamp", "Yes");
2546 lpcfg_do_global_parameter(lp_ctx, "debug pid", "No");
2547 lpcfg_do_global_parameter(lp_ctx, "debug uid", "No");
2548 lpcfg_do_global_parameter(lp_ctx, "debug class", "No");
2550 lpcfg_do_global_parameter(lp_ctx, "share backend", "classic");
2552 lpcfg_do_global_parameter(lp_ctx, "server role", "auto");
2553 lpcfg_do_global_parameter(lp_ctx, "domain logons", "No");
2554 lpcfg_do_global_parameter(lp_ctx, "domain master", "Auto");
2556 /* options that can be set on the command line must be initialised via
2557 the slower lpcfg_do_global_parameter() to ensure that FLAG_CMDLINE is obeyed */
2558 #ifdef TCP_NODELAY
2559 lpcfg_do_global_parameter(lp_ctx, "socket options", "TCP_NODELAY");
2560 #endif
2561 lpcfg_do_global_parameter(lp_ctx, "workgroup", DEFAULT_WORKGROUP);
2562 myname = get_myname(lp_ctx);
2563 lpcfg_do_global_parameter(lp_ctx, "netbios name", myname);
2564 talloc_free(myname);
2565 lpcfg_do_global_parameter(lp_ctx, "name resolve order", "lmhosts wins host bcast");
2567 lpcfg_do_global_parameter(lp_ctx, "fstype", "NTFS");
2569 lpcfg_do_global_parameter(lp_ctx, "ntvfs handler", "unixuid default");
2570 lpcfg_do_global_parameter(lp_ctx, "max connections", "0");
2572 lpcfg_do_global_parameter(lp_ctx, "dcerpc endpoint servers", "epmapper wkssvc rpcecho samr netlogon lsarpc drsuapi dssetup unixinfo browser eventlog6 backupkey dnsserver");
2573 lpcfg_do_global_parameter(lp_ctx, "server services", "s3fs rpc nbt wrepl ldap cldap kdc drepl winbindd ntp_signd kcc dnsupdate dns");
2574 lpcfg_do_global_parameter(lp_ctx, "kccsrv:samba_kcc", "true");
2575 /* the winbind method for domain controllers is for both RODC
2576 auth forwarding and for trusted domains */
2577 lpcfg_do_global_parameter(lp_ctx, "private dir", dyn_PRIVATE_DIR);
2578 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_LOCAL_MACHINE", "hklm.ldb");
2580 /* This hive should be dynamically generated by Samba using
2581 data from the sam, but for the moment leave it in a tdb to
2582 keep regedt32 from popping up an annoying dialog. */
2583 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_USERS", "hku.ldb");
2585 /* using UTF8 by default allows us to support all chars */
2586 lpcfg_do_global_parameter(lp_ctx, "unix charset", "UTF-8");
2588 /* Use codepage 850 as a default for the dos character set */
2589 lpcfg_do_global_parameter(lp_ctx, "dos charset", "CP850");
2592 * Allow the default PASSWD_CHAT to be overridden in local.h.
2594 lpcfg_do_global_parameter(lp_ctx, "passwd chat", DEFAULT_PASSWD_CHAT);
2596 lpcfg_do_global_parameter(lp_ctx, "pid directory", dyn_PIDDIR);
2597 lpcfg_do_global_parameter(lp_ctx, "lock dir", dyn_LOCKDIR);
2598 lpcfg_do_global_parameter(lp_ctx, "state directory", dyn_STATEDIR);
2599 lpcfg_do_global_parameter(lp_ctx, "cache directory", dyn_CACHEDIR);
2600 lpcfg_do_global_parameter(lp_ctx, "ncalrpc dir", dyn_NCALRPCDIR);
2602 lpcfg_do_global_parameter(lp_ctx, "nbt client socket address", "0.0.0.0");
2603 lpcfg_do_global_parameter_var(lp_ctx, "server string",
2604 "Samba %s", SAMBA_VERSION_STRING);
2606 lpcfg_do_global_parameter(lp_ctx, "password server", "*");
2608 lpcfg_do_global_parameter(lp_ctx, "max mux", "50");
2609 lpcfg_do_global_parameter(lp_ctx, "max xmit", "16644");
2610 lpcfg_do_global_parameter(lp_ctx, "host msdfs", "true");
2612 lpcfg_do_global_parameter(lp_ctx, "LargeReadwrite", "True");
2613 lpcfg_do_global_parameter(lp_ctx, "server min protocol", "LANMAN1");
2614 lpcfg_do_global_parameter(lp_ctx, "server max protocol", "SMB3");
2615 lpcfg_do_global_parameter(lp_ctx, "client min protocol", "CORE");
2616 lpcfg_do_global_parameter(lp_ctx, "client max protocol", "default");
2617 lpcfg_do_global_parameter(lp_ctx, "client ipc min protocol", "default");
2618 lpcfg_do_global_parameter(lp_ctx, "client ipc max protocol", "default");
2619 lpcfg_do_global_parameter(lp_ctx, "security", "AUTO");
2620 lpcfg_do_global_parameter(lp_ctx, "EncryptPasswords", "True");
2621 lpcfg_do_global_parameter(lp_ctx, "ReadRaw", "True");
2622 lpcfg_do_global_parameter(lp_ctx, "WriteRaw", "True");
2623 lpcfg_do_global_parameter(lp_ctx, "NullPasswords", "False");
2624 lpcfg_do_global_parameter(lp_ctx, "old password allowed period", "60");
2625 lpcfg_do_global_parameter(lp_ctx, "ObeyPamRestrictions", "False");
2627 lpcfg_do_global_parameter(lp_ctx, "TimeServer", "False");
2628 lpcfg_do_global_parameter(lp_ctx, "BindInterfacesOnly", "False");
2629 lpcfg_do_global_parameter(lp_ctx, "Unicode", "True");
2630 lpcfg_do_global_parameter(lp_ctx, "ClientLanManAuth", "False");
2631 lpcfg_do_global_parameter(lp_ctx, "ClientNTLMv2Auth", "True");
2632 lpcfg_do_global_parameter(lp_ctx, "LanmanAuth", "False");
2633 lpcfg_do_global_parameter(lp_ctx, "NTLMAuth", "False");
2634 lpcfg_do_global_parameter(lp_ctx, "RawNTLMv2Auth", "False");
2635 lpcfg_do_global_parameter(lp_ctx, "client use spnego principal", "False");
2637 lpcfg_do_global_parameter(lp_ctx, "allow dcerpc auth level connect", "False");
2639 lpcfg_do_global_parameter(lp_ctx, "UnixExtensions", "True");
2641 lpcfg_do_global_parameter(lp_ctx, "PreferredMaster", "Auto");
2642 lpcfg_do_global_parameter(lp_ctx, "LocalMaster", "True");
2644 lpcfg_do_global_parameter(lp_ctx, "wins support", "False");
2645 lpcfg_do_global_parameter(lp_ctx, "dns proxy", "True");
2647 lpcfg_do_global_parameter(lp_ctx, "winbind separator", "\\");
2648 lpcfg_do_global_parameter(lp_ctx, "winbind sealed pipes", "True");
2649 lpcfg_do_global_parameter(lp_ctx, "require strong key", "True");
2650 lpcfg_do_global_parameter(lp_ctx, "winbindd socket directory", dyn_WINBINDD_SOCKET_DIR);
2651 lpcfg_do_global_parameter(lp_ctx, "ntp signd socket directory", dyn_NTP_SIGND_SOCKET_DIR);
2652 lpcfg_do_global_parameter_var(lp_ctx, "dns update command", "%s/samba_dnsupdate", dyn_SCRIPTSBINDIR);
2653 lpcfg_do_global_parameter_var(lp_ctx, "spn update command", "%s/samba_spnupdate", dyn_SCRIPTSBINDIR);
2654 lpcfg_do_global_parameter_var(lp_ctx, "samba kcc command",
2655 "%s/samba_kcc", dyn_SCRIPTSBINDIR);
2656 lpcfg_do_global_parameter(lp_ctx, "template shell", "/bin/false");
2657 lpcfg_do_global_parameter(lp_ctx, "template homedir", "/home/%D/%U");
2659 lpcfg_do_global_parameter(lp_ctx, "client signing", "default");
2660 lpcfg_do_global_parameter(lp_ctx, "client ipc signing", "default");
2661 lpcfg_do_global_parameter(lp_ctx, "server signing", "default");
2663 lpcfg_do_global_parameter(lp_ctx, "use spnego", "True");
2665 lpcfg_do_global_parameter(lp_ctx, "use mmap", "True");
2667 lpcfg_do_global_parameter(lp_ctx, "smb ports", "445 139");
2668 lpcfg_do_global_parameter_var(lp_ctx, "nbt port", "%d", NBT_NAME_SERVICE_PORT);
2669 lpcfg_do_global_parameter_var(lp_ctx, "dgram port", "%d", NBT_DGRAM_SERVICE_PORT);
2670 lpcfg_do_global_parameter(lp_ctx, "cldap port", "389");
2671 lpcfg_do_global_parameter(lp_ctx, "krb5 port", "88");
2672 lpcfg_do_global_parameter(lp_ctx, "kpasswd port", "464");
2673 lpcfg_do_global_parameter(lp_ctx, "web port", "901");
2675 lpcfg_do_global_parameter(lp_ctx, "nt status support", "True");
2677 lpcfg_do_global_parameter(lp_ctx, "max wins ttl", "518400"); /* 6 days */
2678 lpcfg_do_global_parameter(lp_ctx, "min wins ttl", "21600");
2680 lpcfg_do_global_parameter(lp_ctx, "tls enabled", "True");
2681 lpcfg_do_global_parameter(lp_ctx, "tls verify peer", "as_strict_as_possible");
2682 lpcfg_do_global_parameter(lp_ctx, "tls keyfile", "tls/key.pem");
2683 lpcfg_do_global_parameter(lp_ctx, "tls certfile", "tls/cert.pem");
2684 lpcfg_do_global_parameter(lp_ctx, "tls cafile", "tls/ca.pem");
2685 lpcfg_do_global_parameter(lp_ctx, "tls priority", "NORMAL:-VERS-SSL3.0");
2686 lpcfg_do_global_parameter(lp_ctx, "prefork children:smb", "4");
2688 lpcfg_do_global_parameter(lp_ctx, "rndc command", "/usr/sbin/rndc");
2689 lpcfg_do_global_parameter(lp_ctx, "nsupdate command", "/usr/bin/nsupdate -g");
2691 lpcfg_do_global_parameter(lp_ctx, "allow dns updates", "secure only");
2692 lpcfg_do_global_parameter(lp_ctx, "dns forwarder", "");
2694 lpcfg_do_global_parameter(lp_ctx, "algorithmic rid base", "1000");
2696 lpcfg_do_global_parameter(lp_ctx, "enhanced browsing", "True");
2698 lpcfg_do_global_parameter(lp_ctx, "winbind nss info", "template");
2700 lpcfg_do_global_parameter(lp_ctx, "server schannel", "Auto");
2702 lpcfg_do_global_parameter(lp_ctx, "short preserve case", "True");
2704 lpcfg_do_global_parameter(lp_ctx, "max open files", "16384");
2706 lpcfg_do_global_parameter(lp_ctx, "cups connection timeout", "30");
2708 lpcfg_do_global_parameter(lp_ctx, "locking", "True");
2710 lpcfg_do_global_parameter(lp_ctx, "block size", "1024");
2712 lpcfg_do_global_parameter(lp_ctx, "client use spnego", "True");
2714 lpcfg_do_global_parameter(lp_ctx, "change notify", "True");
2716 lpcfg_do_global_parameter(lp_ctx, "name cache timeout", "660");
2718 lpcfg_do_global_parameter(lp_ctx, "defer sharing violations", "True");
2720 lpcfg_do_global_parameter(lp_ctx, "ldap replication sleep", "1000");
2722 lpcfg_do_global_parameter(lp_ctx, "idmap backend", "tdb");
2724 lpcfg_do_global_parameter(lp_ctx, "enable privileges", "True");
2726 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max write", "%u", DEFAULT_SMB2_MAX_WRITE);
2728 lpcfg_do_global_parameter(lp_ctx, "passdb backend", "tdbsam");
2730 lpcfg_do_global_parameter(lp_ctx, "getwd cache", "True");
2732 lpcfg_do_global_parameter(lp_ctx, "winbind nested groups", "True");
2734 lpcfg_do_global_parameter(lp_ctx, "mangled names", "True");
2736 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max credits", "%u", DEFAULT_SMB2_MAX_CREDITS);
2738 lpcfg_do_global_parameter(lp_ctx, "ldap ssl", "start tls");
2740 lpcfg_do_global_parameter(lp_ctx, "ldap deref", "auto");
2742 lpcfg_do_global_parameter(lp_ctx, "lm interval", "60");
2744 lpcfg_do_global_parameter(lp_ctx, "mangling method", "hash2");
2746 lpcfg_do_global_parameter(lp_ctx, "hide dot files", "True");
2748 lpcfg_do_global_parameter(lp_ctx, "browse list", "True");
2750 lpcfg_do_global_parameter(lp_ctx, "passwd chat timeout", "2");
2752 lpcfg_do_global_parameter(lp_ctx, "guest account", GUEST_ACCOUNT);
2754 lpcfg_do_global_parameter(lp_ctx, "client schannel", "auto");
2756 lpcfg_do_global_parameter(lp_ctx, "smb encrypt", "default");
2758 lpcfg_do_global_parameter(lp_ctx, "max log size", "5000");
2760 lpcfg_do_global_parameter(lp_ctx, "idmap negative cache time", "120");
2762 lpcfg_do_global_parameter(lp_ctx, "ldap follow referral", "auto");
2764 lpcfg_do_global_parameter(lp_ctx, "multicast dns register", "yes");
2766 lpcfg_do_global_parameter(lp_ctx, "winbind reconnect delay", "30");
2768 lpcfg_do_global_parameter(lp_ctx, "winbind request timeout", "60");
2770 lpcfg_do_global_parameter(lp_ctx, "nt acl support", "yes");
2772 lpcfg_do_global_parameter(lp_ctx, "acl check permissions", "yes");
2774 lpcfg_do_global_parameter(lp_ctx, "keepalive", "300");
2776 lpcfg_do_global_parameter(lp_ctx, "smbd profiling level", "off");
2778 lpcfg_do_global_parameter(lp_ctx, "winbind cache time", "300");
2780 lpcfg_do_global_parameter(lp_ctx, "level2 oplocks", "yes");
2782 lpcfg_do_global_parameter(lp_ctx, "show add printer wizard", "yes");
2784 lpcfg_do_global_parameter(lp_ctx, "allocation roundup size", "1048576");
2786 lpcfg_do_global_parameter(lp_ctx, "ldap page size", "1000");
2788 lpcfg_do_global_parameter(lp_ctx, "kernel share modes", "yes");
2790 lpcfg_do_global_parameter(lp_ctx, "strict locking", "Auto");
2792 lpcfg_do_global_parameter(lp_ctx, "map readonly", "yes");
2794 lpcfg_do_global_parameter(lp_ctx, "allow trusted domains", "yes");
2796 lpcfg_do_global_parameter(lp_ctx, "default devmode", "yes");
2798 lpcfg_do_global_parameter(lp_ctx, "os level", "20");
2800 lpcfg_do_global_parameter(lp_ctx, "dos filetimes", "yes");
2802 lpcfg_do_global_parameter(lp_ctx, "mangling char", "~");
2804 lpcfg_do_global_parameter(lp_ctx, "printcap cache time", "750");
2806 lpcfg_do_global_parameter(lp_ctx, "create krb5 conf", "yes");
2808 lpcfg_do_global_parameter(lp_ctx, "winbind max clients", "200");
2810 lpcfg_do_global_parameter(lp_ctx, "acl map full control", "yes");
2812 lpcfg_do_global_parameter(lp_ctx, "nt pipe support", "yes");
2814 lpcfg_do_global_parameter(lp_ctx, "ldap debug threshold", "10");
2816 lpcfg_do_global_parameter(lp_ctx, "client ldap sasl wrapping", "sign");
2818 lpcfg_do_global_parameter(lp_ctx, "ldap server require strong auth", "yes");
2820 lpcfg_do_global_parameter(lp_ctx, "follow symlinks", "yes");
2822 lpcfg_do_global_parameter(lp_ctx, "machine password timeout", "604800");
2824 lpcfg_do_global_parameter(lp_ctx, "ldap connection timeout", "2");
2826 lpcfg_do_global_parameter(lp_ctx, "winbind expand groups", "0");
2828 lpcfg_do_global_parameter(lp_ctx, "stat cache", "yes");
2830 lpcfg_do_global_parameter(lp_ctx, "lpq cache time", "30");
2832 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max trans", "%u", DEFAULT_SMB2_MAX_TRANSACT);
2834 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max read", "%u", DEFAULT_SMB2_MAX_READ);
2836 lpcfg_do_global_parameter(lp_ctx, "durable handles", "yes");
2838 lpcfg_do_global_parameter(lp_ctx, "max stat cache size", "256");
2840 lpcfg_do_global_parameter(lp_ctx, "ldap passwd sync", "no");
2842 lpcfg_do_global_parameter(lp_ctx, "kernel change notify", "yes");
2844 lpcfg_do_global_parameter(lp_ctx, "max ttl", "259200");
2846 lpcfg_do_global_parameter(lp_ctx, "blocking locks", "yes");
2848 lpcfg_do_global_parameter(lp_ctx, "oplock contention limit", "2");
2850 lpcfg_do_global_parameter(lp_ctx, "load printers", "yes");
2852 lpcfg_do_global_parameter(lp_ctx, "idmap cache time", "604800");
2854 lpcfg_do_global_parameter(lp_ctx, "preserve case", "yes");
2856 lpcfg_do_global_parameter(lp_ctx, "lm announce", "auto");
2858 lpcfg_do_global_parameter(lp_ctx, "afs token lifetime", "604800");
2860 lpcfg_do_global_parameter(lp_ctx, "enable core files", "yes");
2862 lpcfg_do_global_parameter(lp_ctx, "winbind max domain connections", "1");
2864 lpcfg_do_global_parameter(lp_ctx, "case sensitive", "auto");
2866 lpcfg_do_global_parameter(lp_ctx, "ldap timeout", "15");
2868 lpcfg_do_global_parameter(lp_ctx, "mangle prefix", "1");
2870 lpcfg_do_global_parameter(lp_ctx, "posix locking", "yes");
2872 lpcfg_do_global_parameter(lp_ctx, "lock spin time", "200");
2874 lpcfg_do_global_parameter(lp_ctx, "directory name cache size", "100");
2876 lpcfg_do_global_parameter(lp_ctx, "nmbd bind explicit broadcast", "yes");
2878 lpcfg_do_global_parameter(lp_ctx, "init logon delay", "100");
2880 lpcfg_do_global_parameter(lp_ctx, "usershare owner only", "yes");
2882 lpcfg_do_global_parameter(lp_ctx, "-valid", "yes");
2884 lpcfg_do_global_parameter_var(lp_ctx, "usershare path", "%s/usershares", get_dyn_STATEDIR());
2886 #ifdef DEVELOPER
2887 lpcfg_do_global_parameter_var(lp_ctx, "panic action", "/bin/sleep 999999999");
2888 #endif
2890 lpcfg_do_global_parameter(lp_ctx, "smb passwd file", get_dyn_SMB_PASSWD_FILE());
2892 lpcfg_do_global_parameter(lp_ctx, "logon home", "\\\\%N\\%U");
2894 lpcfg_do_global_parameter(lp_ctx, "logon path", "\\\\%N\\%U\\profile");
2896 lpcfg_do_global_parameter(lp_ctx, "printjob username", "%U");
2898 lpcfg_do_global_parameter(lp_ctx, "aio max threads", "100");
2900 lpcfg_do_global_parameter(lp_ctx, "smb2 leases", "yes");
2902 lpcfg_do_global_parameter(lp_ctx, "kerberos encryption types", "all");
2904 /* Allow modules to adjust defaults */
2905 for (defaults_hook = defaults_hooks; defaults_hook;
2906 defaults_hook = defaults_hook->next) {
2907 bool ret;
2909 ret = defaults_hook->hook(lp_ctx);
2910 if (!ret) {
2911 DEBUG(1, ("Defaults hook %s failed to run.",
2912 defaults_hook->name));
2913 talloc_free(lp_ctx);
2914 return NULL;
2918 for (i = 0; parm_table[i].label; i++) {
2919 if (!(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2920 lp_ctx->flags[i] |= FLAG_DEFAULT;
2924 for (parm=lp_ctx->globals->param_opt; parm; parm=parm->next) {
2925 if (!(parm->priority & FLAG_CMDLINE)) {
2926 parm->priority |= FLAG_DEFAULT;
2930 for (parm=lp_ctx->sDefault->param_opt; parm; parm=parm->next) {
2931 if (!(parm->priority & FLAG_CMDLINE)) {
2932 parm->priority |= FLAG_DEFAULT;
2936 return lp_ctx;
2940 * Initialise the global parameter structure.
2942 struct loadparm_context *loadparm_init_global(bool load_default)
2944 if (global_loadparm_context == NULL) {
2945 global_loadparm_context = loadparm_init(NULL);
2947 if (global_loadparm_context == NULL) {
2948 return NULL;
2950 global_loadparm_context->global = true;
2951 if (load_default && !global_loadparm_context->loaded) {
2952 lpcfg_load_default(global_loadparm_context);
2954 global_loadparm_context->refuse_free = true;
2955 return global_loadparm_context;
2959 * Initialise the global parameter structure.
2961 struct loadparm_context *loadparm_init_s3(TALLOC_CTX *mem_ctx,
2962 const struct loadparm_s3_helpers *s3_fns)
2964 struct loadparm_context *loadparm_context = talloc_zero(mem_ctx, struct loadparm_context);
2965 if (!loadparm_context) {
2966 return NULL;
2968 loadparm_context->s3_fns = s3_fns;
2969 loadparm_context->globals = s3_fns->globals;
2970 loadparm_context->flags = s3_fns->flags;
2972 return loadparm_context;
2975 const char *lpcfg_configfile(struct loadparm_context *lp_ctx)
2977 return lp_ctx->szConfigFile;
2980 const char *lp_default_path(void)
2982 if (getenv("SMB_CONF_PATH"))
2983 return getenv("SMB_CONF_PATH");
2984 else
2985 return dyn_CONFIGFILE;
2989 * Update the internal state of a loadparm context after settings
2990 * have changed.
2992 static bool lpcfg_update(struct loadparm_context *lp_ctx)
2994 struct debug_settings settings;
2995 TALLOC_CTX *tmp_ctx;
2997 tmp_ctx = talloc_new(lp_ctx);
2998 if (tmp_ctx == NULL) {
2999 return false;
3002 lpcfg_add_auto_services(lp_ctx, lpcfg_auto_services(lp_ctx, tmp_ctx));
3004 if (!lp_ctx->globals->wins_server_list && lp_ctx->globals->we_are_a_wins_server) {
3005 lpcfg_do_global_parameter(lp_ctx, "wins server", "127.0.0.1");
3008 if (!lp_ctx->global) {
3009 TALLOC_FREE(tmp_ctx);
3010 return true;
3013 panic_action = lp_ctx->globals->panic_action;
3015 reload_charcnv(lp_ctx);
3017 ZERO_STRUCT(settings);
3018 /* Add any more debug-related smb.conf parameters created in
3019 * future here */
3020 settings.timestamp_logs = lp_ctx->globals->timestamp_logs;
3021 settings.debug_prefix_timestamp = lp_ctx->globals->debug_prefix_timestamp;
3022 settings.debug_hires_timestamp = lp_ctx->globals->debug_hires_timestamp;
3023 settings.debug_pid = lp_ctx->globals->debug_pid;
3024 settings.debug_uid = lp_ctx->globals->debug_uid;
3025 settings.debug_class = lp_ctx->globals->debug_class;
3026 debug_set_settings(&settings, lp_ctx->globals->logging,
3027 lp_ctx->globals->syslog,
3028 lp_ctx->globals->syslog_only);
3030 /* FIXME: This is a bit of a hack, but we can't use a global, since
3031 * not everything that uses lp also uses the socket library */
3032 if (lpcfg_parm_bool(lp_ctx, NULL, "socket", "testnonblock", false)) {
3033 setenv("SOCKET_TESTNONBLOCK", "1", 1);
3034 } else {
3035 unsetenv("SOCKET_TESTNONBLOCK");
3038 TALLOC_FREE(tmp_ctx);
3039 return true;
3042 bool lpcfg_load_default(struct loadparm_context *lp_ctx)
3044 const char *path;
3046 path = lp_default_path();
3048 if (!file_exist(path)) {
3049 /* We allow the default smb.conf file to not exist,
3050 * basically the equivalent of an empty file. */
3051 return lpcfg_update(lp_ctx);
3054 return lpcfg_load(lp_ctx, path);
3058 * Load the services array from the services file.
3060 * Return True on success, False on failure.
3062 bool lpcfg_load(struct loadparm_context *lp_ctx, const char *filename)
3064 char *n2;
3065 bool bRetval;
3067 filename = talloc_strdup(lp_ctx, filename);
3069 lp_ctx->szConfigFile = filename;
3071 if (lp_ctx->s3_fns) {
3072 return lp_ctx->s3_fns->load(filename);
3075 lp_ctx->bInGlobalSection = true;
3076 n2 = standard_sub_basic(lp_ctx, lp_ctx->szConfigFile);
3077 DEBUG(2, ("lpcfg_load: refreshing parameters from %s\n", n2));
3079 add_to_file_list(lp_ctx, &lp_ctx->file_lists, lp_ctx->szConfigFile, n2);
3081 /* We get sections first, so have to start 'behind' to make up */
3082 lp_ctx->currentService = NULL;
3083 bRetval = pm_process(n2, do_section, lpcfg_do_parameter, lp_ctx);
3085 /* finish up the last section */
3086 DEBUG(4, ("pm_process() returned %s\n", BOOLSTR(bRetval)));
3087 if (bRetval)
3088 if (lp_ctx->currentService != NULL)
3089 bRetval = lpcfg_service_ok(lp_ctx->currentService);
3091 bRetval = bRetval && lpcfg_update(lp_ctx);
3093 /* we do this unconditionally, so that it happens even
3094 for a missing smb.conf */
3095 reload_charcnv(lp_ctx);
3097 if (bRetval == true) {
3098 /* set this up so that any child python tasks will
3099 find the right smb.conf */
3100 setenv("SMB_CONF_PATH", filename, 1);
3102 /* set the context used by the lp_*() function
3103 varients */
3104 global_loadparm_context = lp_ctx;
3105 lp_ctx->loaded = true;
3108 return bRetval;
3112 * Return the max number of services.
3115 int lpcfg_numservices(struct loadparm_context *lp_ctx)
3117 if (lp_ctx->s3_fns) {
3118 return lp_ctx->s3_fns->get_numservices();
3121 return lp_ctx->iNumServices;
3125 * Display the contents of the services array in human-readable form.
3128 void lpcfg_dump(struct loadparm_context *lp_ctx, FILE *f, bool show_defaults,
3129 int maxtoprint)
3131 int iService;
3133 if (lp_ctx->s3_fns) {
3134 lp_ctx->s3_fns->dump(f, show_defaults, maxtoprint);
3135 return;
3138 lpcfg_dump_globals(lp_ctx, f, show_defaults);
3140 lpcfg_dump_a_service(lp_ctx->sDefault, lp_ctx->sDefault, f, lp_ctx->flags, show_defaults);
3142 for (iService = 0; iService < maxtoprint; iService++)
3143 lpcfg_dump_one(f, show_defaults, lp_ctx->services[iService], lp_ctx->sDefault);
3147 * Display the contents of one service in human-readable form.
3149 void lpcfg_dump_one(FILE *f, bool show_defaults, struct loadparm_service *service, struct loadparm_service *sDefault)
3151 if (service != NULL) {
3152 if (service->szService[0] == '\0')
3153 return;
3154 lpcfg_dump_a_service(service, sDefault, f, NULL, show_defaults);
3158 struct loadparm_service *lpcfg_servicebynum(struct loadparm_context *lp_ctx,
3159 int snum)
3161 if (lp_ctx->s3_fns) {
3162 return lp_ctx->s3_fns->get_servicebynum(snum);
3165 return lp_ctx->services[snum];
3168 struct loadparm_service *lpcfg_service(struct loadparm_context *lp_ctx,
3169 const char *service_name)
3171 int iService;
3172 char *serviceName;
3174 if (lp_ctx->s3_fns) {
3175 return lp_ctx->s3_fns->get_service(service_name);
3178 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--) {
3179 if (lp_ctx->services[iService] &&
3180 lp_ctx->services[iService]->szService) {
3182 * The substitution here is used to support %U is
3183 * service names
3185 serviceName = standard_sub_basic(
3186 lp_ctx->services[iService],
3187 lp_ctx->services[iService]->szService);
3188 if (strequal(serviceName, service_name)) {
3189 talloc_free(serviceName);
3190 return lp_ctx->services[iService];
3192 talloc_free(serviceName);
3196 DEBUG(7,("lpcfg_servicenumber: couldn't find %s\n", service_name));
3197 return NULL;
3200 const char *lpcfg_servicename(const struct loadparm_service *service)
3202 return lpcfg_string((const char *)service->szService);
3206 * A useful volume label function.
3208 const char *lpcfg_volume_label(struct loadparm_service *service, struct loadparm_service *sDefault)
3210 const char *ret;
3211 ret = lpcfg_string((const char *)((service != NULL && service->volume != NULL) ?
3212 service->volume : sDefault->volume));
3213 if (!*ret)
3214 return lpcfg_servicename(service);
3215 return ret;
3219 * Return the correct printer name.
3221 const char *lpcfg_printername(struct loadparm_service *service, struct loadparm_service *sDefault)
3223 const char *ret;
3224 ret = lpcfg_string((const char *)((service != NULL && service->_printername != NULL) ?
3225 service->_printername : sDefault->_printername));
3226 if (ret == NULL || (ret != NULL && *ret == '\0'))
3227 ret = lpcfg_servicename(service);
3229 return ret;
3234 * Return the max print jobs per queue.
3236 int lpcfg_maxprintjobs(struct loadparm_service *service, struct loadparm_service *sDefault)
3238 int maxjobs = lpcfg_max_print_jobs(service, sDefault);
3240 if (maxjobs <= 0 || maxjobs >= PRINT_MAX_JOBID)
3241 maxjobs = PRINT_MAX_JOBID - 1;
3243 return maxjobs;
3246 struct smb_iconv_handle *lpcfg_iconv_handle(struct loadparm_context *lp_ctx)
3248 if (lp_ctx == NULL) {
3249 return get_iconv_handle();
3251 return lp_ctx->iconv_handle;
3254 _PUBLIC_ void reload_charcnv(struct loadparm_context *lp_ctx)
3256 struct smb_iconv_handle *old_ic = lp_ctx->iconv_handle;
3257 if (!lp_ctx->global) {
3258 return;
3261 if (old_ic == NULL) {
3262 old_ic = global_iconv_handle;
3264 lp_ctx->iconv_handle = smb_iconv_handle_reinit_lp(lp_ctx, lp_ctx, old_ic);
3265 global_iconv_handle = lp_ctx->iconv_handle;
3268 _PUBLIC_ char *lpcfg_tls_keyfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3270 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_keyfile(lp_ctx));
3273 _PUBLIC_ char *lpcfg_tls_certfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3275 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_certfile(lp_ctx));
3278 _PUBLIC_ char *lpcfg_tls_cafile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3280 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_cafile(lp_ctx));
3283 _PUBLIC_ char *lpcfg_tls_crlfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3285 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_crlfile(lp_ctx));
3288 _PUBLIC_ char *lpcfg_tls_dhpfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3290 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_dhpfile(lp_ctx));
3293 struct gensec_settings *lpcfg_gensec_settings(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3295 struct gensec_settings *settings = talloc_zero(mem_ctx, struct gensec_settings);
3296 if (settings == NULL)
3297 return NULL;
3298 SMB_ASSERT(lp_ctx != NULL);
3299 settings->lp_ctx = talloc_reference(settings, lp_ctx);
3300 settings->target_hostname = lpcfg_parm_string(lp_ctx, NULL, "gensec", "target_hostname");
3301 return settings;
3304 int lpcfg_server_role(struct loadparm_context *lp_ctx)
3306 int domain_master = lpcfg__domain_master(lp_ctx);
3308 return lp_find_server_role(lpcfg__server_role(lp_ctx),
3309 lpcfg__security(lp_ctx),
3310 lpcfg__domain_logons(lp_ctx),
3311 (domain_master == true) ||
3312 (domain_master == Auto));
3315 int lpcfg_security(struct loadparm_context *lp_ctx)
3317 return lp_find_security(lpcfg__server_role(lp_ctx),
3318 lpcfg__security(lp_ctx));
3321 int lpcfg_client_max_protocol(struct loadparm_context *lp_ctx)
3323 int client_max_protocol = lpcfg__client_max_protocol(lp_ctx);
3324 if (client_max_protocol == PROTOCOL_DEFAULT) {
3325 return PROTOCOL_NT1;
3327 return client_max_protocol;
3330 int lpcfg_client_ipc_min_protocol(struct loadparm_context *lp_ctx)
3332 int client_ipc_min_protocol = lpcfg__client_ipc_min_protocol(lp_ctx);
3333 if (client_ipc_min_protocol == PROTOCOL_DEFAULT) {
3334 client_ipc_min_protocol = lpcfg_client_min_protocol(lp_ctx);
3336 if (client_ipc_min_protocol < PROTOCOL_NT1) {
3337 return PROTOCOL_NT1;
3339 return client_ipc_min_protocol;
3342 int lpcfg_client_ipc_max_protocol(struct loadparm_context *lp_ctx)
3344 int client_ipc_max_protocol = lpcfg__client_ipc_max_protocol(lp_ctx);
3345 if (client_ipc_max_protocol == PROTOCOL_DEFAULT) {
3346 return PROTOCOL_LATEST;
3348 if (client_ipc_max_protocol < PROTOCOL_NT1) {
3349 return PROTOCOL_NT1;
3351 return client_ipc_max_protocol;
3354 int lpcfg_client_ipc_signing(struct loadparm_context *lp_ctx)
3356 int client_ipc_signing = lpcfg__client_ipc_signing(lp_ctx);
3357 if (client_ipc_signing == SMB_SIGNING_DEFAULT) {
3358 return SMB_SIGNING_REQUIRED;
3360 return client_ipc_signing;
3363 bool lpcfg_server_signing_allowed(struct loadparm_context *lp_ctx, bool *mandatory)
3365 bool allowed = true;
3366 enum smb_signing_setting signing_setting = lpcfg_server_signing(lp_ctx);
3368 *mandatory = false;
3370 if (signing_setting == SMB_SIGNING_DEFAULT) {
3372 * If we are a domain controller, SMB signing is
3373 * really important, as it can prevent a number of
3374 * attacks on communications between us and the
3375 * clients
3377 * However, it really sucks (no sendfile, CPU
3378 * overhead) performance-wise when used on a
3379 * file server, so disable it by default
3380 * on non-DCs
3383 if (lpcfg_server_role(lp_ctx) >= ROLE_ACTIVE_DIRECTORY_DC) {
3384 signing_setting = SMB_SIGNING_REQUIRED;
3385 } else {
3386 signing_setting = SMB_SIGNING_OFF;
3390 switch (signing_setting) {
3391 case SMB_SIGNING_REQUIRED:
3392 *mandatory = true;
3393 break;
3394 case SMB_SIGNING_DESIRED:
3395 case SMB_SIGNING_IF_REQUIRED:
3396 break;
3397 case SMB_SIGNING_OFF:
3398 allowed = false;
3399 break;
3400 case SMB_SIGNING_DEFAULT:
3401 case SMB_SIGNING_IPC_DEFAULT:
3402 smb_panic(__location__);
3403 break;
3406 return allowed;
3409 int lpcfg_tdb_hash_size(struct loadparm_context *lp_ctx, const char *name)
3411 const char *base;
3413 if (name == NULL) {
3414 return 0;
3417 base = strrchr_m(name, '/');
3418 if (base != NULL) {
3419 base += 1;
3420 } else {
3421 base = name;
3423 return lpcfg_parm_int(lp_ctx, NULL, "tdb_hashsize", base, 0);
3427 int lpcfg_tdb_flags(struct loadparm_context *lp_ctx, int tdb_flags)
3429 if (!lpcfg_use_mmap(lp_ctx)) {
3430 tdb_flags |= TDB_NOMMAP;
3432 return tdb_flags;