lib/param: clang: Fix 'dereference of a null pointer' warning
[Samba.git] / lib / param / loadparm.c
blob169d884ec04cdce9191b71387ffe4170d12544c6
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"
72 #include "lib/util/samba_util.h"
73 #include "libcli/auth/ntlm_check.h"
75 #ifdef HAVE_HTTPCONNECTENCRYPT
76 #include <cups/http.h>
77 #endif
79 #define standard_sub_basic talloc_strdup
81 #include "lib/param/param_global.h"
83 struct loadparm_service *lpcfg_default_service(struct loadparm_context *lp_ctx)
85 return lp_ctx->sDefault;
88 int lpcfg_rpc_low_port(struct loadparm_context *lp_ctx)
90 return lp_ctx->globals->rpc_low_port;
93 int lpcfg_rpc_high_port(struct loadparm_context *lp_ctx)
95 return lp_ctx->globals->rpc_high_port;
98 /**
99 * Convenience routine to grab string parameters into temporary memory
100 * and run standard_sub_basic on them.
102 * The buffers can be written to by
103 * callers without affecting the source string.
106 static const char *lpcfg_string(const char *s)
108 #if 0 /* until REWRITE done to make thread-safe */
109 size_t len = s ? strlen(s) : 0;
110 char *ret;
111 #endif
113 /* The follow debug is useful for tracking down memory problems
114 especially if you have an inner loop that is calling a lp_*()
115 function that returns a string. Perhaps this debug should be
116 present all the time? */
118 #if 0
119 DEBUG(10, ("lpcfg_string(%s)\n", s));
120 #endif
122 #if 0 /* until REWRITE done to make thread-safe */
123 if (!lp_talloc)
124 lp_talloc = talloc_init("lp_talloc");
126 ret = talloc_array(lp_talloc, char, len + 100); /* leave room for substitution */
128 if (!ret)
129 return NULL;
131 if (!s)
132 *ret = 0;
133 else
134 strlcpy(ret, s, len);
136 if (trim_string(ret, "\"", "\"")) {
137 if (strchr(ret,'"') != NULL)
138 strlcpy(ret, s, len);
141 standard_sub_basic(ret,len+100);
142 return (ret);
143 #endif
144 return s;
148 In this section all the functions that are used to access the
149 parameters from the rest of the program are defined
153 * the creation of separate lpcfg_*() and lp_*() functions is to allow
154 * for code compatibility between existing Samba4 and Samba3 code.
157 /* this global context supports the lp_*() function varients */
158 static struct loadparm_context *global_loadparm_context;
160 #define FN_GLOBAL_STRING(fn_name,var_name) \
161 _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx, TALLOC_CTX *ctx) {\
162 if (lp_ctx == NULL) return NULL; \
163 if (lp_ctx->s3_fns) { \
164 return lp_ctx->globals->var_name ? lp_ctx->s3_fns->lp_string(ctx, lp_ctx->globals->var_name) : talloc_strdup(ctx, ""); \
166 return lp_ctx->globals->var_name ? talloc_strdup(ctx, lpcfg_string(lp_ctx->globals->var_name)) : talloc_strdup(ctx, ""); \
169 #define FN_GLOBAL_CONST_STRING(fn_name,var_name) \
170 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
171 if (lp_ctx == NULL) return NULL; \
172 return lp_ctx->globals->var_name ? lpcfg_string(lp_ctx->globals->var_name) : ""; \
175 #define FN_GLOBAL_LIST(fn_name,var_name) \
176 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
177 if (lp_ctx == NULL) return NULL; \
178 return lp_ctx->globals->var_name; \
181 #define FN_GLOBAL_BOOL(fn_name,var_name) \
182 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) {\
183 if (lp_ctx == NULL) return false; \
184 return lp_ctx->globals->var_name; \
187 #define FN_GLOBAL_INTEGER(fn_name,var_name) \
188 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
189 return lp_ctx->globals->var_name; \
192 /* Local parameters don't need the ->s3_fns because the struct
193 * loadparm_service is shared and lpcfg_service() checks the ->s3_fns
194 * hook */
195 #define FN_LOCAL_STRING(fn_name,val) \
196 _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_service *service, \
197 struct loadparm_service *sDefault, TALLOC_CTX *ctx) { \
198 return(talloc_strdup(ctx, lpcfg_string((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)))); \
201 #define FN_LOCAL_CONST_STRING(fn_name,val) \
202 _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_service *service, \
203 struct loadparm_service *sDefault) { \
204 return((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)); \
207 #define FN_LOCAL_LIST(fn_name,val) \
208 _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_service *service, \
209 struct loadparm_service *sDefault) {\
210 return(const char **)(service != NULL && service->val != NULL? service->val : sDefault->val); \
213 #define FN_LOCAL_PARM_BOOL(fn_name, val) FN_LOCAL_BOOL(fn_name, val)
215 #define FN_LOCAL_BOOL(fn_name,val) \
216 _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_service *service, \
217 struct loadparm_service *sDefault) { \
218 return((service != NULL)? service->val : sDefault->val); \
221 #define FN_LOCAL_INTEGER(fn_name,val) \
222 _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_service *service, \
223 struct loadparm_service *sDefault) { \
224 return((service != NULL)? service->val : sDefault->val); \
227 #define FN_LOCAL_PARM_INTEGER(fn_name, val) FN_LOCAL_INTEGER(fn_name, val)
229 #define FN_LOCAL_CHAR(fn_name,val) \
230 _PUBLIC_ char lpcfg_ ## fn_name(struct loadparm_service *service, \
231 struct loadparm_service *sDefault) { \
232 return((service != NULL)? service->val : sDefault->val); \
235 #define FN_LOCAL_PARM_CHAR(fn_name,val) FN_LOCAL_CHAR(fn_name, val)
237 #include "lib/param/param_functions.c"
239 /* These functions cannot be auto-generated */
240 FN_LOCAL_BOOL(autoloaded, autoloaded)
241 FN_GLOBAL_CONST_STRING(dnsdomain, dnsdomain)
243 /* local prototypes */
244 static struct loadparm_service *lpcfg_getservicebyname(struct loadparm_context *lp_ctx,
245 const char *pszServiceName);
246 static bool do_section(const char *pszSectionName, void *);
247 static bool set_variable_helper(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
248 const char *pszParmName, const char *pszParmValue);
249 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
250 struct loadparm_service *service,
251 const char *pszParmName,
252 const char *pszParmValue, int flags);
254 /* The following are helper functions for parametrical options support. */
255 /* It returns a pointer to parametrical option value if it exists or NULL otherwise */
256 /* Actual parametrical functions are quite simple */
257 struct parmlist_entry *get_parametric_helper(struct loadparm_service *service,
258 const char *type, const char *option,
259 struct parmlist_entry *global_opts)
261 size_t type_len = strlen(type);
262 size_t option_len = strlen(option);
263 char param_key[type_len + option_len + 2];
264 struct parmlist_entry *data = NULL;
266 snprintf(param_key, sizeof(param_key), "%s:%s", type, option);
269 * Try to fetch the option from the data.
271 if (service != NULL) {
272 data = service->param_opt;
273 while (data != NULL) {
274 if (strwicmp(data->key, param_key) == 0) {
275 return data;
277 data = data->next;
282 * Fall back to fetching from the globals.
284 data = global_opts;
285 while (data != NULL) {
286 if (strwicmp(data->key, param_key) == 0) {
287 return data;
289 data = data->next;
292 return NULL;
295 const char *lpcfg_get_parametric(struct loadparm_context *lp_ctx,
296 struct loadparm_service *service,
297 const char *type, const char *option)
299 struct parmlist_entry *data;
301 if (lp_ctx == NULL)
302 return NULL;
304 data = get_parametric_helper(service,
305 type, option, lp_ctx->globals->param_opt);
307 if (data == NULL) {
308 return NULL;
309 } else {
310 return data->value;
316 * convenience routine to return int parameters.
318 int lp_int(const char *s)
321 if (!s || !*s) {
322 DEBUG(0,("lp_int(%s): is called with NULL!\n",s));
323 return -1;
326 return strtol(s, NULL, 0);
330 * convenience routine to return unsigned long parameters.
332 unsigned long lp_ulong(const char *s)
334 int error = 0;
335 unsigned long int ret;
337 if (!s || !*s) {
338 DBG_DEBUG("lp_ulong(%s): is called with NULL!\n",s);
339 return -1;
342 ret = strtoul_err(s, NULL, 0, &error);
343 if (error != 0) {
344 DBG_DEBUG("lp_ulong(%s): conversion failed\n",s);
345 return -1;
348 return ret;
352 * convenience routine to return unsigned long long parameters.
354 unsigned long long lp_ulonglong(const char *s)
356 int error = 0;
357 unsigned long long int ret;
359 if (!s || !*s) {
360 DBG_DEBUG("lp_ulonglong(%s): is called with NULL!\n", s);
361 return -1;
364 ret = strtoull_err(s, NULL, 0, &error);
365 if (error != 0) {
366 DBG_DEBUG("lp_ulonglong(%s): conversion failed\n",s);
367 return -1;
370 return ret;
374 * convenience routine to return unsigned long parameters.
376 static long lp_long(const char *s)
379 if (!s) {
380 DEBUG(0,("lp_long(%s): is called with NULL!\n",s));
381 return -1;
384 return strtol(s, NULL, 0);
388 * convenience routine to return unsigned long parameters.
390 static double lp_double(const char *s)
393 if (!s) {
394 DEBUG(0,("lp_double(%s): is called with NULL!\n",s));
395 return -1;
398 return strtod(s, NULL);
402 * convenience routine to return boolean parameters.
404 bool lp_bool(const char *s)
406 bool ret = false;
408 if (!s || !*s) {
409 DEBUG(0,("lp_bool(%s): is called with NULL!\n",s));
410 return false;
413 if (!set_boolean(s, &ret)) {
414 DEBUG(0,("lp_bool(%s): value is not boolean!\n",s));
415 return false;
418 return ret;
422 * Return parametric option from a given service. Type is a part of option before ':'
423 * Parametric option has following syntax: 'Type: option = value'
424 * Returned value is allocated in 'lp_talloc' context
427 const char *lpcfg_parm_string(struct loadparm_context *lp_ctx,
428 struct loadparm_service *service, const char *type,
429 const char *option)
431 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
433 if (value)
434 return lpcfg_string(value);
436 return NULL;
440 * Return parametric option from a given service. Type is a part of option before ':'
441 * Parametric option has following syntax: 'Type: option = value'
442 * Returned value is allocated in 'lp_talloc' context
445 const char **lpcfg_parm_string_list(TALLOC_CTX *mem_ctx,
446 struct loadparm_context *lp_ctx,
447 struct loadparm_service *service,
448 const char *type,
449 const char *option, const char *separator)
451 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
453 if (value != NULL) {
454 char **l = str_list_make(mem_ctx, value, separator);
455 return discard_const_p(const char *, l);
458 return NULL;
462 * Return parametric option from a given service. Type is a part of option before ':'
463 * Parametric option has following syntax: 'Type: option = value'
466 int lpcfg_parm_int(struct loadparm_context *lp_ctx,
467 struct loadparm_service *service, const char *type,
468 const char *option, int default_v)
470 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
472 if (value)
473 return lp_int(value);
475 return default_v;
479 * Return parametric option from a given service. Type is a part of
480 * option before ':'.
481 * Parametric option has following syntax: 'Type: option = value'.
484 int lpcfg_parm_bytes(struct loadparm_context *lp_ctx,
485 struct loadparm_service *service, const char *type,
486 const char *option, int default_v)
488 uint64_t bval;
490 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
492 if (value && conv_str_size_error(value, &bval)) {
493 if (bval <= INT_MAX) {
494 return (int)bval;
498 return default_v;
502 * Return parametric option from a given service.
503 * Type is a part of option before ':'
504 * Parametric option has following syntax: 'Type: option = value'
506 unsigned long lpcfg_parm_ulong(struct loadparm_context *lp_ctx,
507 struct loadparm_service *service, const char *type,
508 const char *option, unsigned long default_v)
510 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
512 if (value)
513 return lp_ulong(value);
515 return default_v;
519 * Return parametric option from a given service.
520 * Type is a part of option before ':'
521 * Parametric option has following syntax: 'Type: option = value'
523 unsigned long long lpcfg_parm_ulonglong(struct loadparm_context *lp_ctx,
524 struct loadparm_service *service,
525 const char *type, const char *option,
526 unsigned long long default_v)
528 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
530 if (value) {
531 return lp_ulonglong(value);
534 return default_v;
537 long lpcfg_parm_long(struct loadparm_context *lp_ctx,
538 struct loadparm_service *service, const char *type,
539 const char *option, long default_v)
541 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
543 if (value)
544 return lp_long(value);
546 return default_v;
549 double lpcfg_parm_double(struct loadparm_context *lp_ctx,
550 struct loadparm_service *service, const char *type,
551 const char *option, double default_v)
553 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
555 if (value != NULL)
556 return lp_double(value);
558 return default_v;
562 * Return parametric option from a given service. Type is a part of option before ':'
563 * Parametric option has following syntax: 'Type: option = value'
566 bool lpcfg_parm_bool(struct loadparm_context *lp_ctx,
567 struct loadparm_service *service, const char *type,
568 const char *option, bool default_v)
570 const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
572 if (value != NULL)
573 return lp_bool(value);
575 return default_v;
579 /* this is used to prevent lots of mallocs of size 1 */
580 static const char lpcfg_string_empty[] = "";
583 Free a string value.
585 void lpcfg_string_free(char **s)
587 if (s == NULL) {
588 return;
590 if (*s == lpcfg_string_empty) {
591 *s = NULL;
592 return;
594 TALLOC_FREE(*s);
598 * Set a string value, deallocating any existing space, and allocing the space
599 * for the string
601 bool lpcfg_string_set(TALLOC_CTX *mem_ctx, char **dest, const char *src)
603 lpcfg_string_free(dest);
605 if ((src == NULL) || (*src == '\0')) {
606 *dest = discard_const_p(char, lpcfg_string_empty);
607 return true;
610 *dest = talloc_strdup(mem_ctx, src);
611 if ((*dest) == NULL) {
612 DEBUG(0,("Out of memory in string_set\n"));
613 return false;
616 return true;
620 * Set a string value, deallocating any existing space, and allocing the space
621 * for the string
623 bool lpcfg_string_set_upper(TALLOC_CTX *mem_ctx, char **dest, const char *src)
625 lpcfg_string_free(dest);
627 if ((src == NULL) || (*src == '\0')) {
628 *dest = discard_const_p(char, lpcfg_string_empty);
629 return true;
632 *dest = strupper_talloc(mem_ctx, src);
633 if ((*dest) == NULL) {
634 DEBUG(0,("Out of memory in string_set_upper\n"));
635 return false;
638 return true;
644 * Add a new service to the services array initialising it with the given
645 * service.
648 struct loadparm_service *lpcfg_add_service(struct loadparm_context *lp_ctx,
649 const struct loadparm_service *pservice,
650 const char *name)
652 int i;
653 int num_to_alloc = lp_ctx->iNumServices + 1;
654 struct parmlist_entry *data, *pdata;
656 if (lp_ctx->s3_fns != NULL) {
657 smb_panic("Add a service should not be called on an s3 loadparm ctx");
660 if (pservice == NULL) {
661 pservice = lp_ctx->sDefault;
664 /* it might already exist */
665 if (name) {
666 struct loadparm_service *service = lpcfg_getservicebyname(lp_ctx,
667 name);
668 if (service != NULL) {
669 /* Clean all parametric options for service */
670 /* They will be added during parsing again */
671 data = service->param_opt;
672 while (data) {
673 pdata = data->next;
674 talloc_free(data);
675 data = pdata;
677 service->param_opt = NULL;
678 return service;
682 /* find an invalid one */
683 for (i = 0; i < lp_ctx->iNumServices; i++)
684 if (lp_ctx->services[i] == NULL)
685 break;
687 /* if not, then create one */
688 if (i == lp_ctx->iNumServices) {
689 struct loadparm_service **tsp;
691 tsp = talloc_realloc(lp_ctx, lp_ctx->services, struct loadparm_service *, num_to_alloc);
693 if (!tsp) {
694 DEBUG(0,("lpcfg_add_service: failed to enlarge services!\n"));
695 return NULL;
696 } else {
697 lp_ctx->services = tsp;
698 lp_ctx->services[lp_ctx->iNumServices] = NULL;
701 lp_ctx->iNumServices++;
704 lp_ctx->services[i] = talloc_zero(lp_ctx->services, struct loadparm_service);
705 if (lp_ctx->services[i] == NULL) {
706 DEBUG(0,("lpcfg_add_service: out of memory!\n"));
707 return NULL;
709 copy_service(lp_ctx->services[i], pservice, NULL);
710 if (name != NULL)
711 lpcfg_string_set(lp_ctx->services[i], &lp_ctx->services[i]->szService, name);
712 return lp_ctx->services[i];
716 * Add a new home service, with the specified home directory, defaults coming
717 * from service ifrom.
720 bool lpcfg_add_home(struct loadparm_context *lp_ctx,
721 const char *pszHomename,
722 struct loadparm_service *default_service,
723 const char *user, const char *pszHomedir)
725 struct loadparm_service *service;
727 service = lpcfg_add_service(lp_ctx, default_service, pszHomename);
729 if (service == NULL)
730 return false;
732 if (!(*(default_service->path))
733 || strequal(default_service->path, lp_ctx->sDefault->path)) {
734 service->path = talloc_strdup(service, pszHomedir);
735 } else {
736 service->path = string_sub_talloc(service, lpcfg_path(default_service, lp_ctx->sDefault, service), "%H", pszHomedir);
739 if (!(*(service->comment))) {
740 service->comment = talloc_asprintf(service, "Home directory of %s", user);
742 service->available = default_service->available;
743 service->browseable = default_service->browseable;
745 DEBUG(3, ("adding home's share [%s] for user '%s' at '%s'\n",
746 pszHomename, user, service->path));
748 return true;
752 * Add a new printer service, with defaults coming from service iFrom.
755 bool lpcfg_add_printer(struct loadparm_context *lp_ctx,
756 const char *pszPrintername,
757 struct loadparm_service *default_service)
759 const char *comment = "From Printcap";
760 struct loadparm_service *service;
761 service = lpcfg_add_service(lp_ctx, default_service, pszPrintername);
763 if (service == NULL)
764 return false;
766 /* note that we do NOT default the availability flag to True - */
767 /* we take it from the default service passed. This allows all */
768 /* dynamic printers to be disabled by disabling the [printers] */
769 /* entry (if/when the 'available' keyword is implemented!). */
771 /* the printer name is set to the service name. */
772 lpcfg_string_set(service, &service->_printername, pszPrintername);
773 lpcfg_string_set(service, &service->comment, comment);
774 service->browseable = default_service->browseable;
775 /* Printers cannot be read_only. */
776 service->read_only = false;
777 /* Printer services must be printable. */
778 service->printable = true;
780 DEBUG(3, ("adding printer service %s\n", pszPrintername));
782 return true;
786 * Map a parameter's string representation to something we can use.
787 * Returns False if the parameter string is not recognised, else TRUE.
790 int lpcfg_map_parameter(const char *pszParmName)
792 int iIndex;
794 for (iIndex = 0; parm_table[iIndex].label; iIndex++)
795 if (strwicmp(parm_table[iIndex].label, pszParmName) == 0)
796 return iIndex;
798 /* Warn only if it isn't parametric option */
799 if (strchr(pszParmName, ':') == NULL)
800 DEBUG(0, ("Unknown parameter encountered: \"%s\"\n", pszParmName));
801 /* We do return 'fail' for parametric options as well because they are
802 stored in different storage
804 return -1;
809 return the parameter structure for a parameter
811 struct parm_struct *lpcfg_parm_struct(struct loadparm_context *lp_ctx, const char *name)
813 int num = lpcfg_map_parameter(name);
815 if (num < 0) {
816 return NULL;
819 return &parm_table[num];
823 return the parameter pointer for a parameter
825 void *lpcfg_parm_ptr(struct loadparm_context *lp_ctx,
826 struct loadparm_service *service, struct parm_struct *parm)
828 if (lp_ctx->s3_fns) {
829 return lp_ctx->s3_fns->get_parm_ptr(service, parm);
832 if (service == NULL) {
833 if (parm->p_class == P_LOCAL)
834 return ((char *)lp_ctx->sDefault)+parm->offset;
835 else if (parm->p_class == P_GLOBAL)
836 return ((char *)lp_ctx->globals)+parm->offset;
837 else return NULL;
838 } else {
839 return ((char *)service) + parm->offset;
844 return the parameter pointer for a parameter
846 bool lpcfg_parm_is_cmdline(struct loadparm_context *lp_ctx, const char *name)
848 int parmnum;
850 parmnum = lpcfg_map_parameter(name);
851 if (parmnum == -1) return false;
853 return lp_ctx->flags[parmnum] & FLAG_CMDLINE;
857 * Find a service by name. Otherwise works like get_service.
860 static struct loadparm_service *lpcfg_getservicebyname(struct loadparm_context *lp_ctx,
861 const char *pszServiceName)
863 int iService;
865 if (lp_ctx->s3_fns) {
866 return lp_ctx->s3_fns->get_service(pszServiceName);
869 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--)
870 if (lp_ctx->services[iService] != NULL &&
871 strwicmp(lp_ctx->services[iService]->szService, pszServiceName) == 0) {
872 return lp_ctx->services[iService];
875 return NULL;
879 * Add a parametric option to a parmlist_entry,
880 * replacing old value, if already present.
882 void set_param_opt(TALLOC_CTX *mem_ctx,
883 struct parmlist_entry **opt_list,
884 const char *opt_name,
885 const char *opt_value,
886 unsigned priority)
888 struct parmlist_entry *new_opt, *opt;
890 opt = *opt_list;
892 /* Traverse destination */
893 while (opt) {
894 /* If we already have same option, override it */
895 if (strwicmp(opt->key, opt_name) == 0) {
896 if ((opt->priority & FLAG_CMDLINE) &&
897 !(priority & FLAG_CMDLINE)) {
898 /* it's been marked as not to be
899 overridden */
900 return;
902 TALLOC_FREE(opt->list);
903 lpcfg_string_set(opt, &opt->value, opt_value);
904 opt->priority = priority;
905 return;
907 opt = opt->next;
910 new_opt = talloc_pooled_object(
911 mem_ctx, struct parmlist_entry,
912 2, strlen(opt_name) + 1 + strlen(opt_value) + 1);
913 if (new_opt == NULL) {
914 smb_panic("OOM");
916 new_opt->key = NULL;
917 lpcfg_string_set(new_opt, &new_opt->key, opt_name);
918 new_opt->value = NULL;
919 lpcfg_string_set(new_opt, &new_opt->value, opt_value);
921 new_opt->list = NULL;
922 new_opt->priority = priority;
923 DLIST_ADD(*opt_list, new_opt);
927 * Copy a service structure to another.
928 * If pcopymapDest is NULL then copy all fields
931 void copy_service(struct loadparm_service *pserviceDest,
932 const struct loadparm_service *pserviceSource,
933 struct bitmap *pcopymapDest)
935 int i;
936 bool bcopyall = (pcopymapDest == NULL);
937 struct parmlist_entry *data;
939 for (i = 0; parm_table[i].label; i++)
940 if (parm_table[i].p_class == P_LOCAL &&
941 (bcopyall || bitmap_query(pcopymapDest, i))) {
942 const void *src_ptr =
943 ((const char *)pserviceSource) + parm_table[i].offset;
944 void *dest_ptr =
945 ((char *)pserviceDest) + parm_table[i].offset;
947 switch (parm_table[i].type) {
948 case P_BOOL:
949 case P_BOOLREV:
950 *(bool *)dest_ptr = *(const bool *)src_ptr;
951 break;
953 case P_INTEGER:
954 case P_BYTES:
955 case P_OCTAL:
956 case P_ENUM:
957 *(int *)dest_ptr = *(const int *)src_ptr;
958 break;
960 case P_CHAR:
961 *(char *)dest_ptr = *(const char *)src_ptr;
962 break;
964 case P_STRING:
965 lpcfg_string_set(pserviceDest,
966 (char **)dest_ptr,
967 *(const char * const *)src_ptr);
968 break;
970 case P_USTRING:
971 lpcfg_string_set_upper(pserviceDest,
972 (char **)dest_ptr,
973 *(const char * const *)src_ptr);
974 break;
975 case P_CMDLIST:
976 case P_LIST:
977 TALLOC_FREE(*((char ***)dest_ptr));
978 *(char ***)dest_ptr = str_list_copy(pserviceDest,
979 *discard_const_p(const char **, src_ptr));
980 break;
981 default:
982 break;
986 if (bcopyall) {
987 init_copymap(pserviceDest);
988 if (pserviceSource->copymap)
989 bitmap_copy(pserviceDest->copymap,
990 pserviceSource->copymap);
993 for (data = pserviceSource->param_opt; data != NULL; data = data->next) {
994 set_param_opt(pserviceDest, &pserviceDest->param_opt,
995 data->key, data->value, data->priority);
1000 * Check a service for consistency. Return False if the service is in any way
1001 * incomplete or faulty, else True.
1003 bool lpcfg_service_ok(struct loadparm_service *service)
1005 bool bRetval;
1007 bRetval = true;
1008 if (service->szService[0] == '\0') {
1009 DEBUG(0, ("The following message indicates an internal error:\n"));
1010 DEBUG(0, ("No service name in service entry.\n"));
1011 bRetval = false;
1014 /* The [printers] entry MUST be printable. I'm all for flexibility, but */
1015 /* I can't see why you'd want a non-printable printer service... */
1016 if (strwicmp(service->szService, PRINTERS_NAME) == 0) {
1017 if (!service->printable) {
1018 DEBUG(0, ("WARNING: [%s] service MUST be printable!\n",
1019 service->szService));
1020 service->printable = true;
1022 /* [printers] service must also be non-browsable. */
1023 if (service->browseable)
1024 service->browseable = false;
1027 if (service->path[0] == '\0' &&
1028 strwicmp(service->szService, HOMES_NAME) != 0 &&
1029 service->msdfs_proxy[0] == '\0')
1031 DEBUG(0, ("WARNING: No path in service %s - making it unavailable!\n",
1032 service->szService));
1033 service->available = false;
1036 if (!service->available)
1037 DEBUG(1, ("NOTE: Service %s is flagged unavailable.\n",
1038 service->szService));
1040 return bRetval;
1044 /*******************************************************************
1045 Keep a linked list of all config files so we know when one has changed
1046 it's date and needs to be reloaded.
1047 ********************************************************************/
1049 void add_to_file_list(TALLOC_CTX *mem_ctx, struct file_lists **list,
1050 const char *fname, const char *subfname)
1052 struct file_lists *f = *list;
1054 while (f) {
1055 if (f->name && !strcmp(f->name, fname))
1056 break;
1057 f = f->next;
1060 if (!f) {
1061 f = talloc(mem_ctx, struct file_lists);
1062 if (!f)
1063 goto fail;
1064 f->next = *list;
1065 f->name = talloc_strdup(f, fname);
1066 if (!f->name) {
1067 TALLOC_FREE(f);
1068 goto fail;
1070 f->subfname = talloc_strdup(f, subfname);
1071 if (!f->subfname) {
1072 TALLOC_FREE(f);
1073 goto fail;
1075 *list = f;
1076 f->modtime = file_modtime(subfname);
1077 } else {
1078 time_t t = file_modtime(subfname);
1079 if (t)
1080 f->modtime = t;
1082 return;
1084 fail:
1085 DEBUG(0, ("Unable to add file to file list: %s\n", fname));
1089 /*******************************************************************
1090 Check if a config file has changed date.
1091 ********************************************************************/
1092 bool lpcfg_file_list_changed(struct loadparm_context *lp_ctx)
1094 struct file_lists *f;
1095 DEBUG(6, ("lpcfg_file_list_changed()\n"));
1097 for (f = lp_ctx->file_lists; f != NULL; f = f->next) {
1098 char *n2;
1099 time_t mod_time;
1101 n2 = standard_sub_basic(lp_ctx, f->name);
1103 DEBUGADD(6, ("file %s -> %s last mod_time: %s\n",
1104 f->name, n2, ctime(&f->modtime)));
1106 mod_time = file_modtime(n2);
1108 if (mod_time && ((f->modtime != mod_time) || (f->subfname == NULL) || (strcmp(n2, f->subfname) != 0))) {
1109 DEBUGADD(6, ("file %s modified: %s\n", n2,
1110 ctime(&mod_time)));
1111 f->modtime = mod_time;
1112 talloc_free(f->subfname);
1113 f->subfname = talloc_strdup(f, n2);
1114 TALLOC_FREE(n2);
1115 return true;
1117 TALLOC_FREE(n2);
1119 return false;
1123 * set the value for a P_ENUM
1125 bool lp_set_enum_parm( struct parm_struct *parm, const char *pszParmValue,
1126 int *ptr )
1128 int i;
1130 for (i = 0; parm->enum_list[i].name; i++) {
1131 if (strwicmp(pszParmValue, parm->enum_list[i].name) == 0) {
1132 *ptr = parm->enum_list[i].value;
1133 return true;
1136 DEBUG(0, ("WARNING: Ignoring invalid value '%s' for parameter '%s'\n",
1137 pszParmValue, parm->label));
1138 return false;
1142 /***************************************************************************
1143 Handle the "realm" parameter
1144 ***************************************************************************/
1146 bool handle_realm(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1147 const char *pszParmValue, char **ptr)
1149 char *upper;
1150 char *lower;
1152 upper = strupper_talloc(lp_ctx, pszParmValue);
1153 if (upper == NULL) {
1154 return false;
1157 lower = strlower_talloc(lp_ctx, pszParmValue);
1158 if (lower == NULL) {
1159 TALLOC_FREE(upper);
1160 return false;
1163 lpcfg_string_set(lp_ctx->globals->ctx, &lp_ctx->globals->realm, upper);
1164 lpcfg_string_set(lp_ctx->globals->ctx, &lp_ctx->globals->dnsdomain, lower);
1166 return true;
1169 /***************************************************************************
1170 Handle the include operation.
1171 ***************************************************************************/
1173 bool handle_include(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1174 const char *pszParmValue, char **ptr)
1176 char *fname;
1177 const char *substitution_variable_substring;
1178 char next_char;
1180 if (lp_ctx->s3_fns) {
1181 return lp_ctx->s3_fns->lp_include(lp_ctx, service, pszParmValue, ptr);
1184 fname = standard_sub_basic(lp_ctx, pszParmValue);
1186 add_to_file_list(lp_ctx, &lp_ctx->file_lists, pszParmValue, fname);
1188 lpcfg_string_set(lp_ctx, ptr, fname);
1190 if (file_exist(fname))
1191 return pm_process(fname, do_section, lpcfg_do_parameter, lp_ctx);
1194 * If the file doesn't exist, we check that it isn't due to variable
1195 * substitution
1197 substitution_variable_substring = strchr(fname, '%');
1199 if (substitution_variable_substring != NULL) {
1200 next_char = substitution_variable_substring[1];
1201 if ((next_char >= 'a' && next_char <= 'z')
1202 || (next_char >= 'A' && next_char <= 'Z')) {
1203 DEBUG(2, ("Tried to load %s but variable substitution in "
1204 "filename, ignoring file.\n", fname));
1205 return true;
1209 DEBUG(2, ("Can't find include file %s\n", fname));
1211 return false;
1214 /***************************************************************************
1215 Handle the interpretation of the copy parameter.
1216 ***************************************************************************/
1218 bool handle_copy(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1219 const char *pszParmValue, char **ptr)
1221 bool bRetval;
1222 struct loadparm_service *serviceTemp = NULL;
1224 bRetval = false;
1226 DEBUG(3, ("Copying service from service %s\n", pszParmValue));
1228 serviceTemp = lpcfg_getservicebyname(lp_ctx, pszParmValue);
1230 if (service == NULL) {
1231 DEBUG(0, ("Unable to copy service - invalid service destination.\n"));
1232 return false;
1235 if (serviceTemp != NULL) {
1236 if (serviceTemp == service) {
1237 DEBUG(0, ("Can't copy service %s - unable to copy self!\n", pszParmValue));
1238 } else {
1239 copy_service(service,
1240 serviceTemp,
1241 service->copymap);
1242 lpcfg_string_set(service, ptr, pszParmValue);
1244 bRetval = true;
1246 } else {
1247 DEBUG(0, ("Unable to copy service - source not found: %s\n",
1248 pszParmValue));
1249 bRetval = false;
1252 return bRetval;
1255 bool handle_debug_list(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1256 const char *pszParmValue, char **ptr)
1258 lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1260 return debug_parse_levels(pszParmValue);
1263 bool handle_logfile(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1264 const char *pszParmValue, char **ptr)
1266 if (lp_ctx->s3_fns == NULL) {
1267 debug_set_logfile(pszParmValue);
1270 lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1272 return true;
1276 * These special charset handling methods only run in the source3 code.
1279 bool handle_charset(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1280 const char *pszParmValue, char **ptr)
1282 if (lp_ctx->s3_fns) {
1283 if (*ptr == NULL || strcmp(*ptr, pszParmValue) != 0) {
1284 struct smb_iconv_handle *ret = NULL;
1286 ret = reinit_iconv_handle(NULL,
1287 lpcfg_dos_charset(lp_ctx),
1288 lpcfg_unix_charset(lp_ctx));
1289 if (ret == NULL) {
1290 smb_panic("reinit_iconv_handle failed");
1295 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1299 bool handle_dos_charset(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1300 const char *pszParmValue, char **ptr)
1302 bool is_utf8 = false;
1303 size_t len = strlen(pszParmValue);
1305 if (lp_ctx->s3_fns) {
1306 if (len == 4 || len == 5) {
1307 /* Don't use StrCaseCmp here as we don't want to
1308 initialize iconv. */
1309 if ((toupper_m(pszParmValue[0]) == 'U') &&
1310 (toupper_m(pszParmValue[1]) == 'T') &&
1311 (toupper_m(pszParmValue[2]) == 'F')) {
1312 if (len == 4) {
1313 if (pszParmValue[3] == '8') {
1314 is_utf8 = true;
1316 } else {
1317 if (pszParmValue[3] == '-' &&
1318 pszParmValue[4] == '8') {
1319 is_utf8 = true;
1325 if (*ptr == NULL || strcmp(*ptr, pszParmValue) != 0) {
1326 struct smb_iconv_handle *ret = NULL;
1327 if (is_utf8) {
1328 DEBUG(0,("ERROR: invalid DOS charset: 'dos charset' must not "
1329 "be UTF8, using (default value) %s instead.\n",
1330 DEFAULT_DOS_CHARSET));
1331 pszParmValue = DEFAULT_DOS_CHARSET;
1333 ret = reinit_iconv_handle(NULL,
1334 lpcfg_dos_charset(lp_ctx),
1335 lpcfg_unix_charset(lp_ctx));
1336 if (ret == NULL) {
1337 smb_panic("reinit_iconv_handle failed");
1342 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1345 bool handle_printing(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1346 const char *pszParmValue, char **ptr)
1348 static int parm_num = -1;
1350 if (parm_num == -1) {
1351 parm_num = lpcfg_map_parameter("printing");
1354 if (!lp_set_enum_parm(&parm_table[parm_num], pszParmValue, (int*)ptr)) {
1355 return false;
1358 if (lp_ctx->s3_fns) {
1359 if (service == NULL) {
1360 init_printer_values(lp_ctx, lp_ctx->globals->ctx, lp_ctx->sDefault);
1361 } else {
1362 init_printer_values(lp_ctx, service, service);
1366 return true;
1369 bool handle_ldap_debug_level(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1370 const char *pszParmValue, char **ptr)
1372 lp_ctx->globals->ldap_debug_level = lp_int(pszParmValue);
1374 if (lp_ctx->s3_fns) {
1375 lp_ctx->s3_fns->init_ldap_debugging();
1377 return true;
1380 bool handle_netbios_aliases(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1381 const char *pszParmValue, char **ptr)
1383 TALLOC_FREE(lp_ctx->globals->netbios_aliases);
1384 lp_ctx->globals->netbios_aliases = str_list_make_v3_const(lp_ctx->globals->ctx,
1385 pszParmValue, NULL);
1387 if (lp_ctx->s3_fns) {
1388 return lp_ctx->s3_fns->set_netbios_aliases(lp_ctx->globals->netbios_aliases);
1390 return true;
1394 * idmap related parameters
1397 bool handle_idmap_backend(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1398 const char *pszParmValue, char **ptr)
1400 if (lp_ctx->s3_fns) {
1401 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : backend",
1402 pszParmValue, 0);
1405 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1408 bool handle_idmap_uid(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1409 const char *pszParmValue, char **ptr)
1411 if (lp_ctx->s3_fns) {
1412 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : range",
1413 pszParmValue, 0);
1416 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1419 bool handle_idmap_gid(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1420 const char *pszParmValue, char **ptr)
1422 if (lp_ctx->s3_fns) {
1423 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : range",
1424 pszParmValue, 0);
1427 return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1430 bool handle_smb_ports(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1431 const char *pszParmValue, char **ptr)
1433 static int parm_num = -1;
1434 int i;
1435 const char **list;
1437 if (!pszParmValue || !*pszParmValue) {
1438 return false;
1441 if (parm_num == -1) {
1442 parm_num = lpcfg_map_parameter("smb ports");
1443 if (parm_num == -1) {
1444 return false;
1448 if(!set_variable_helper(lp_ctx->globals->ctx, parm_num, ptr, "smb ports",
1449 pszParmValue)) {
1450 return false;
1453 list = lp_ctx->globals->smb_ports;
1454 if (list == NULL) {
1455 return false;
1458 /* Check that each port is a valid integer and within range */
1459 for (i = 0; list[i] != NULL; i++) {
1460 char *end = NULL;
1461 int port = 0;
1462 port = strtol(list[i], &end, 10);
1463 if (*end != '\0' || port <= 0 || port > 65535) {
1464 TALLOC_FREE(list);
1465 return false;
1469 return true;
1472 bool handle_rpc_server_dynamic_port_range(struct loadparm_context *lp_ctx,
1473 struct loadparm_service *service,
1474 const char *pszParmValue,
1475 char **ptr)
1477 int low_port = -1, high_port = -1;
1478 int rc;
1480 if (pszParmValue == NULL || pszParmValue[0] == '\0') {
1481 return false;
1484 rc = sscanf(pszParmValue, "%d - %d", &low_port, &high_port);
1485 if (rc != 2) {
1486 return false;
1489 if (low_port > high_port) {
1490 return false;
1493 if (low_port < SERVER_TCP_PORT_MIN|| high_port > SERVER_TCP_PORT_MAX) {
1494 return false;
1497 lp_ctx->globals->rpc_low_port = low_port;
1498 lp_ctx->globals->rpc_high_port = high_port;
1500 return true;
1503 bool handle_smb2_max_credits(struct loadparm_context *lp_ctx,
1504 struct loadparm_service *service,
1505 const char *pszParmValue, char **ptr)
1507 int value = lp_int(pszParmValue);
1509 if (value <= 0) {
1510 value = DEFAULT_SMB2_MAX_CREDITS;
1513 *(int *)ptr = value;
1515 return true;
1518 bool handle_cups_encrypt(struct loadparm_context *lp_ctx,
1519 struct loadparm_service *service,
1520 const char *pszParmValue, char **ptr)
1522 int result = 0;
1523 #ifdef HAVE_HTTPCONNECTENCRYPT
1524 int value = lp_int(pszParmValue);
1526 switch (value) {
1527 case Auto:
1528 result = HTTP_ENCRYPT_REQUIRED;
1529 break;
1530 case true:
1531 result = HTTP_ENCRYPT_ALWAYS;
1532 break;
1533 case false:
1534 result = HTTP_ENCRYPT_NEVER;
1535 break;
1536 default:
1537 result = 0;
1538 break;
1540 #endif
1541 *(int *)ptr = result;
1543 return true;
1546 /***************************************************************************
1547 Initialise a copymap.
1548 ***************************************************************************/
1551 * Initializes service copymap
1552 * Note: pservice *must* be valid TALLOC_CTX
1554 void init_copymap(struct loadparm_service *pservice)
1556 int i;
1558 TALLOC_FREE(pservice->copymap);
1560 pservice->copymap = bitmap_talloc(pservice, num_parameters());
1561 if (!pservice->copymap) {
1562 DEBUG(0,
1563 ("Couldn't allocate copymap!! (size %d)\n",
1564 (int)num_parameters()));
1565 } else {
1566 for (i = 0; i < num_parameters(); i++) {
1567 bitmap_set(pservice->copymap, i);
1573 * Process a parametric option
1575 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
1576 struct loadparm_service *service,
1577 const char *pszParmName,
1578 const char *pszParmValue, int flags)
1580 struct parmlist_entry **data;
1581 char *name;
1582 TALLOC_CTX *mem_ctx;
1584 while (isspace((unsigned char)*pszParmName)) {
1585 pszParmName++;
1588 name = strlower_talloc(lp_ctx, pszParmName);
1589 if (!name) return false;
1591 if (service == NULL) {
1592 data = &lp_ctx->globals->param_opt;
1594 * s3 code cannot deal with parametric options stored on the globals ctx.
1596 if (lp_ctx->s3_fns != NULL) {
1597 mem_ctx = NULL;
1598 } else {
1599 mem_ctx = lp_ctx->globals->ctx;
1601 } else {
1602 data = &service->param_opt;
1603 mem_ctx = service;
1606 set_param_opt(mem_ctx, data, name, pszParmValue, flags);
1608 talloc_free(name);
1610 return true;
1613 static bool set_variable_helper(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
1614 const char *pszParmName, const char *pszParmValue)
1616 size_t i;
1618 /* switch on the type of variable it is */
1619 switch (parm_table[parmnum].type)
1621 case P_BOOL: {
1622 bool b;
1623 if (!set_boolean(pszParmValue, &b)) {
1624 DEBUG(0, ("set_variable_helper(%s): value is not "
1625 "boolean!\n", pszParmValue));
1626 return false;
1628 *(bool *)parm_ptr = b;
1630 break;
1632 case P_BOOLREV: {
1633 bool b;
1634 if (!set_boolean(pszParmValue, &b)) {
1635 DEBUG(0, ("set_variable_helper(%s): value is not "
1636 "boolean!\n", pszParmValue));
1637 return false;
1639 *(bool *)parm_ptr = !b;
1641 break;
1643 case P_INTEGER:
1644 *(int *)parm_ptr = lp_int(pszParmValue);
1645 break;
1647 case P_CHAR:
1648 *(char *)parm_ptr = *pszParmValue;
1649 break;
1651 case P_OCTAL:
1652 i = sscanf(pszParmValue, "%o", (int *)parm_ptr);
1653 if ( i != 1 ) {
1654 DEBUG ( 0, ("Invalid octal number %s\n", pszParmName ));
1655 return false;
1657 break;
1659 case P_BYTES:
1661 uint64_t val;
1662 if (conv_str_size_error(pszParmValue, &val)) {
1663 if (val <= INT_MAX) {
1664 *(int *)parm_ptr = (int)val;
1665 break;
1669 DEBUG(0, ("set_variable_helper(%s): value is not "
1670 "a valid size specifier!\n", pszParmValue));
1671 return false;
1674 case P_CMDLIST:
1675 TALLOC_FREE(*(char ***)parm_ptr);
1676 *(char ***)parm_ptr = str_list_make_v3(mem_ctx,
1677 pszParmValue, NULL);
1678 break;
1680 case P_LIST:
1682 char **new_list = str_list_make_v3(mem_ctx,
1683 pszParmValue, NULL);
1684 if (new_list == NULL) {
1685 break;
1688 for (i=0; new_list[i]; i++) {
1689 if (*(const char ***)parm_ptr != NULL &&
1690 new_list[i][0] == '+' &&
1691 new_list[i][1])
1693 if (!str_list_check(*(const char ***)parm_ptr,
1694 &new_list[i][1])) {
1695 *(const char ***)parm_ptr = str_list_add(*(const char ***)parm_ptr,
1696 &new_list[i][1]);
1698 } else if (*(const char ***)parm_ptr != NULL &&
1699 new_list[i][0] == '-' &&
1700 new_list[i][1])
1702 str_list_remove(*(const char ***)parm_ptr,
1703 &new_list[i][1]);
1704 } else {
1705 if (i != 0) {
1706 DEBUG(0, ("Unsupported list syntax for: %s = %s\n",
1707 pszParmName, pszParmValue));
1708 return false;
1710 *(char ***)parm_ptr = new_list;
1711 break;
1714 break;
1717 case P_STRING:
1718 lpcfg_string_set(mem_ctx, (char **)parm_ptr, pszParmValue);
1719 break;
1721 case P_USTRING:
1722 lpcfg_string_set_upper(mem_ctx, (char **)parm_ptr, pszParmValue);
1723 break;
1725 case P_ENUM:
1726 if (!lp_set_enum_parm(&parm_table[parmnum], pszParmValue, (int*)parm_ptr)) {
1727 return false;
1729 break;
1733 return true;
1737 bool handle_name_resolve_order(struct loadparm_context *lp_ctx,
1738 struct loadparm_service *service,
1739 const char *pszParmValue, char **ptr)
1741 const char **valid_values = NULL;
1742 const char **values_to_set = NULL;
1743 int i;
1744 bool value_is_valid = false;
1745 valid_values = str_list_make_v3_const(NULL,
1746 DEFAULT_NAME_RESOLVE_ORDER,
1747 NULL);
1748 if (valid_values == NULL) {
1749 DBG_ERR("OOM: failed to make string list from %s\n",
1750 DEFAULT_NAME_RESOLVE_ORDER);
1751 goto out;
1753 values_to_set = str_list_make_v3_const(lp_ctx->globals->ctx,
1754 pszParmValue,
1755 NULL);
1756 if (values_to_set == NULL) {
1757 DBG_ERR("OOM: failed to make string list from %s\n",
1758 pszParmValue);
1759 goto out;
1761 TALLOC_FREE(lp_ctx->globals->name_resolve_order);
1762 for (i = 0; values_to_set[i] != NULL; i++) {
1763 value_is_valid = str_list_check(valid_values, values_to_set[i]);
1764 if (!value_is_valid) {
1765 DBG_ERR("WARNING: Ignoring invalid list value '%s' "
1766 "for parameter 'name resolve order'\n",
1767 values_to_set[i]);
1768 break;
1771 out:
1772 if (value_is_valid) {
1773 lp_ctx->globals->name_resolve_order = values_to_set;
1774 } else {
1775 TALLOC_FREE(values_to_set);
1777 TALLOC_FREE(valid_values);
1778 return value_is_valid;
1781 static bool set_variable(TALLOC_CTX *mem_ctx, struct loadparm_service *service,
1782 int parmnum, void *parm_ptr,
1783 const char *pszParmName, const char *pszParmValue,
1784 struct loadparm_context *lp_ctx, bool on_globals)
1786 int i;
1787 bool ok;
1789 /* if it is a special case then go ahead */
1790 if (parm_table[parmnum].special) {
1791 ok = parm_table[parmnum].special(lp_ctx, service, pszParmValue,
1792 (char **)parm_ptr);
1793 } else {
1794 ok = set_variable_helper(mem_ctx, parmnum, parm_ptr,
1795 pszParmName, pszParmValue);
1798 if (!ok) {
1799 return false;
1802 if (on_globals && (lp_ctx->flags[parmnum] & FLAG_DEFAULT)) {
1803 lp_ctx->flags[parmnum] &= ~FLAG_DEFAULT;
1804 /* we have to also unset FLAG_DEFAULT on aliases */
1805 for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
1806 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1808 for (i=parmnum+1;i<num_parameters() && parm_table[i].offset == parm_table[parmnum].offset;i++) {
1809 lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1812 return true;
1816 bool lpcfg_do_global_parameter(struct loadparm_context *lp_ctx,
1817 const char *pszParmName, const char *pszParmValue)
1819 int parmnum = lpcfg_map_parameter(pszParmName);
1820 void *parm_ptr;
1822 if (parmnum < 0) {
1823 if (strchr(pszParmName, ':')) {
1824 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName, pszParmValue, 0);
1826 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1827 return true;
1830 /* if the flag has been set on the command line, then don't allow override,
1831 but don't report an error */
1832 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1833 return true;
1836 if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
1837 DEBUG(1, ("WARNING: The \"%s\" option is deprecated\n",
1838 pszParmName));
1841 parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[parmnum]);
1843 return set_variable(lp_ctx->globals->ctx, NULL, parmnum, parm_ptr,
1844 pszParmName, pszParmValue, lp_ctx, true);
1847 bool lpcfg_do_service_parameter(struct loadparm_context *lp_ctx,
1848 struct loadparm_service *service,
1849 const char *pszParmName, const char *pszParmValue)
1851 void *parm_ptr;
1852 int i;
1853 int parmnum = lpcfg_map_parameter(pszParmName);
1855 if (parmnum < 0) {
1856 if (strchr(pszParmName, ':')) {
1857 return lp_do_parameter_parametric(lp_ctx, service, pszParmName, pszParmValue, 0);
1859 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1860 return true;
1863 /* if the flag has been set on the command line, then don't allow override,
1864 but don't report an error */
1865 if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1866 return true;
1869 if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
1870 DEBUG(1, ("WARNING: The \"%s\" option is deprecated\n",
1871 pszParmName));
1874 if (parm_table[parmnum].p_class == P_GLOBAL) {
1875 DEBUG(0,
1876 ("Global parameter %s found in service section!\n",
1877 pszParmName));
1878 return true;
1880 parm_ptr = ((char *)service) + parm_table[parmnum].offset;
1882 if (!service->copymap)
1883 init_copymap(service);
1885 /* this handles the aliases - set the copymap for other
1886 * entries with the same data pointer */
1887 for (i = 0; parm_table[i].label; i++)
1888 if (parm_table[i].offset == parm_table[parmnum].offset &&
1889 parm_table[i].p_class == parm_table[parmnum].p_class)
1890 bitmap_clear(service->copymap, i);
1892 return set_variable(service, service, parmnum, parm_ptr, pszParmName,
1893 pszParmValue, lp_ctx, false);
1897 * Process a parameter.
1900 bool lpcfg_do_parameter(const char *pszParmName, const char *pszParmValue,
1901 void *userdata)
1903 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1905 if (lp_ctx->bInGlobalSection)
1906 return lpcfg_do_global_parameter(lp_ctx, pszParmName,
1907 pszParmValue);
1908 else
1909 return lpcfg_do_service_parameter(lp_ctx, lp_ctx->currentService,
1910 pszParmName, pszParmValue);
1914 variable argument do parameter
1916 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx, const char *pszParmName, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
1917 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx,
1918 const char *pszParmName, const char *fmt, ...)
1920 char *s;
1921 bool ret;
1922 va_list ap;
1924 va_start(ap, fmt);
1925 s = talloc_vasprintf(NULL, fmt, ap);
1926 va_end(ap);
1927 ret = lpcfg_do_global_parameter(lp_ctx, pszParmName, s);
1928 talloc_free(s);
1929 return ret;
1934 set a parameter from the commandline - this is called from command line parameter
1935 parsing code. It sets the parameter then marks the parameter as unable to be modified
1936 by smb.conf processing
1938 bool lpcfg_set_cmdline(struct loadparm_context *lp_ctx, const char *pszParmName,
1939 const char *pszParmValue)
1941 int parmnum;
1942 int i;
1944 while (isspace((unsigned char)*pszParmValue)) pszParmValue++;
1946 parmnum = lpcfg_map_parameter(pszParmName);
1948 if (parmnum < 0 && strchr(pszParmName, ':')) {
1949 /* set a parametric option */
1950 bool ok;
1951 ok = lp_do_parameter_parametric(lp_ctx, NULL, pszParmName,
1952 pszParmValue, FLAG_CMDLINE);
1953 if (lp_ctx->s3_fns != NULL) {
1954 if (ok) {
1955 lp_ctx->s3_fns->store_cmdline(pszParmName, pszParmValue);
1958 return ok;
1961 if (parmnum < 0) {
1962 DEBUG(0,("Unknown option '%s'\n", pszParmName));
1963 return false;
1966 /* reset the CMDLINE flag in case this has been called before */
1967 lp_ctx->flags[parmnum] &= ~FLAG_CMDLINE;
1969 if (!lpcfg_do_global_parameter(lp_ctx, pszParmName, pszParmValue)) {
1970 return false;
1973 lp_ctx->flags[parmnum] |= FLAG_CMDLINE;
1975 /* we have to also set FLAG_CMDLINE on aliases */
1976 for (i=parmnum-1;
1977 i>=0 && parm_table[i].p_class == parm_table[parmnum].p_class &&
1978 parm_table[i].offset == parm_table[parmnum].offset;
1979 i--) {
1980 lp_ctx->flags[i] |= FLAG_CMDLINE;
1982 for (i=parmnum+1;
1983 i<num_parameters() &&
1984 parm_table[i].p_class == parm_table[parmnum].p_class &&
1985 parm_table[i].offset == parm_table[parmnum].offset;
1986 i++) {
1987 lp_ctx->flags[i] |= FLAG_CMDLINE;
1990 if (lp_ctx->s3_fns != NULL) {
1991 lp_ctx->s3_fns->store_cmdline(pszParmName, pszParmValue);
1994 return true;
1998 set a option from the commandline in 'a=b' format. Use to support --option
2000 bool lpcfg_set_option(struct loadparm_context *lp_ctx, const char *option)
2002 char *p, *s;
2003 bool ret;
2005 s = talloc_strdup(NULL, option);
2006 if (!s) {
2007 return false;
2010 p = strchr(s, '=');
2011 if (!p) {
2012 talloc_free(s);
2013 return false;
2016 *p = 0;
2018 ret = lpcfg_set_cmdline(lp_ctx, s, p+1);
2019 talloc_free(s);
2020 return ret;
2024 #define BOOLSTR(b) ((b) ? "Yes" : "No")
2027 * Print a parameter of the specified type.
2030 void lpcfg_print_parameter(struct parm_struct *p, void *ptr, FILE * f)
2032 /* For the seperation of lists values that we print below */
2033 const char *list_sep = ", ";
2034 int i;
2035 switch (p->type)
2037 case P_ENUM:
2038 for (i = 0; p->enum_list[i].name; i++) {
2039 if (*(int *)ptr == p->enum_list[i].value) {
2040 fprintf(f, "%s",
2041 p->enum_list[i].name);
2042 break;
2045 break;
2047 case P_BOOL:
2048 fprintf(f, "%s", BOOLSTR(*(bool *)ptr));
2049 break;
2051 case P_BOOLREV:
2052 fprintf(f, "%s", BOOLSTR(!*(bool *)ptr));
2053 break;
2055 case P_INTEGER:
2056 case P_BYTES:
2057 fprintf(f, "%d", *(int *)ptr);
2058 break;
2060 case P_CHAR:
2061 fprintf(f, "%c", *(char *)ptr);
2062 break;
2064 case P_OCTAL: {
2065 int val = *(int *)ptr;
2066 if (val == -1) {
2067 fprintf(f, "-1");
2068 } else {
2069 fprintf(f, "0%03o", val);
2071 break;
2074 case P_CMDLIST:
2075 list_sep = " ";
2077 FALL_THROUGH;
2078 case P_LIST:
2079 if ((char ***)ptr && *(char ***)ptr) {
2080 char **list = *(char ***)ptr;
2081 for (; *list; list++) {
2082 /* surround strings with whitespace in double quotes */
2083 if (*(list+1) == NULL) {
2084 /* last item, no extra separator */
2085 list_sep = "";
2087 if ( strchr_m( *list, ' ' ) ) {
2088 fprintf(f, "\"%s\"%s", *list, list_sep);
2089 } else {
2090 fprintf(f, "%s%s", *list, list_sep);
2094 break;
2096 case P_STRING:
2097 case P_USTRING:
2098 if (*(char **)ptr) {
2099 fprintf(f, "%s", *(char **)ptr);
2101 break;
2106 * Check if two parameters are equal.
2109 static bool lpcfg_equal_parameter(parm_type type, void *ptr1, void *ptr2)
2111 switch (type) {
2112 case P_BOOL:
2113 case P_BOOLREV:
2114 return (*((bool *)ptr1) == *((bool *)ptr2));
2116 case P_INTEGER:
2117 case P_ENUM:
2118 case P_OCTAL:
2119 case P_BYTES:
2120 return (*((int *)ptr1) == *((int *)ptr2));
2122 case P_CHAR:
2123 return (*((char *)ptr1) == *((char *)ptr2));
2125 case P_LIST:
2126 case P_CMDLIST:
2127 return str_list_equal(*(const char ***)ptr1, *(const char ***)ptr2);
2129 case P_STRING:
2130 case P_USTRING:
2132 char *p1 = *(char **)ptr1, *p2 = *(char **)ptr2;
2133 if (p1 && !*p1)
2134 p1 = NULL;
2135 if (p2 && !*p2)
2136 p2 = NULL;
2137 return (p1 == p2 || strequal(p1, p2));
2140 return false;
2144 * Process a new section (service).
2146 * At this stage all sections are services.
2147 * Later we'll have special sections that permit server parameters to be set.
2148 * Returns True on success, False on failure.
2151 static bool do_section(const char *pszSectionName, void *userdata)
2153 struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
2154 bool bRetval;
2155 bool isglobal;
2157 if (lp_ctx->s3_fns != NULL) {
2158 return lp_ctx->s3_fns->do_section(pszSectionName, lp_ctx);
2161 isglobal = ((strwicmp(pszSectionName, GLOBAL_NAME) == 0) ||
2162 (strwicmp(pszSectionName, GLOBAL_NAME2) == 0));
2164 bRetval = false;
2166 /* if we've just struck a global section, note the fact. */
2167 lp_ctx->bInGlobalSection = isglobal;
2169 /* check for multiple global sections */
2170 if (lp_ctx->bInGlobalSection) {
2171 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
2172 return true;
2175 /* if we have a current service, tidy it up before moving on */
2176 bRetval = true;
2178 if (lp_ctx->currentService != NULL)
2179 bRetval = lpcfg_service_ok(lp_ctx->currentService);
2181 /* if all is still well, move to the next record in the services array */
2182 if (bRetval) {
2183 /* We put this here to avoid an odd message order if messages are */
2184 /* issued by the post-processing of a previous section. */
2185 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
2187 if ((lp_ctx->currentService = lpcfg_add_service(lp_ctx, lp_ctx->sDefault,
2188 pszSectionName))
2189 == NULL) {
2190 DEBUG(0, ("Failed to add a new service\n"));
2191 return false;
2195 return bRetval;
2200 * Determine if a particular base parameter is currently set to the default value.
2203 static bool is_default(void *base_structure, int i)
2205 void *def_ptr = ((char *)base_structure) + parm_table[i].offset;
2206 switch (parm_table[i].type) {
2207 case P_CMDLIST:
2208 case P_LIST:
2209 return str_list_equal((const char * const *)parm_table[i].def.lvalue,
2210 *(const char * const **)def_ptr);
2211 case P_STRING:
2212 case P_USTRING:
2213 return strequal(parm_table[i].def.svalue,
2214 *(char **)def_ptr);
2215 case P_BOOL:
2216 case P_BOOLREV:
2217 return parm_table[i].def.bvalue ==
2218 *(bool *)def_ptr;
2219 case P_INTEGER:
2220 case P_CHAR:
2221 case P_OCTAL:
2222 case P_BYTES:
2223 case P_ENUM:
2224 return parm_table[i].def.ivalue ==
2225 *(int *)def_ptr;
2227 return false;
2231 *Display the contents of the global structure.
2234 void lpcfg_dump_globals(struct loadparm_context *lp_ctx, FILE *f,
2235 bool show_defaults)
2237 int i;
2238 struct parmlist_entry *data;
2240 fprintf(f, "# Global parameters\n[global]\n");
2242 for (i = 0; parm_table[i].label; i++) {
2243 if (parm_table[i].p_class != P_GLOBAL) {
2244 continue;
2247 if (parm_table[i].flags & FLAG_SYNONYM) {
2248 continue;
2251 if (!show_defaults) {
2252 if (lp_ctx->flags && (lp_ctx->flags[i] & FLAG_DEFAULT)) {
2253 continue;
2256 if (is_default(lp_ctx->globals, i)) {
2257 continue;
2261 fprintf(f, "\t%s = ", parm_table[i].label);
2262 lpcfg_print_parameter(&parm_table[i], lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[i]), f);
2263 fprintf(f, "\n");
2265 if (lp_ctx->globals->param_opt != NULL) {
2266 for (data = lp_ctx->globals->param_opt; data;
2267 data = data->next) {
2268 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
2269 continue;
2271 fprintf(f, "\t%s = %s\n", data->key, data->value);
2278 * Display the contents of a single services record.
2281 void lpcfg_dump_a_service(struct loadparm_service * pService, struct loadparm_service *sDefault, FILE * f,
2282 unsigned int *flags, bool show_defaults)
2284 int i;
2285 struct parmlist_entry *data;
2287 if (pService != sDefault)
2288 fprintf(f, "\n[%s]\n", pService->szService);
2290 for (i = 0; parm_table[i].label; i++) {
2291 if (parm_table[i].p_class != P_LOCAL) {
2292 continue;
2295 if (parm_table[i].flags & FLAG_SYNONYM) {
2296 continue;
2299 if (*parm_table[i].label == '-') {
2300 continue;
2303 if (pService == sDefault) {
2304 if (!show_defaults) {
2305 if (flags && (flags[i] & FLAG_DEFAULT)) {
2306 continue;
2309 if (is_default(sDefault, i)) {
2310 continue;
2313 } else {
2314 bool equal;
2316 equal = lpcfg_equal_parameter(parm_table[i].type,
2317 ((char *)pService) +
2318 parm_table[i].offset,
2319 ((char *)sDefault) +
2320 parm_table[i].offset);
2321 if (equal) {
2322 continue;
2326 fprintf(f, "\t%s = ", parm_table[i].label);
2327 lpcfg_print_parameter(&parm_table[i],
2328 ((char *)pService) + parm_table[i].offset, f);
2329 fprintf(f, "\n");
2331 if (pService->param_opt != NULL) {
2332 for (data = pService->param_opt; data; data = data->next) {
2333 if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
2334 continue;
2336 fprintf(f, "\t%s = %s\n", data->key, data->value);
2341 bool lpcfg_dump_a_parameter(struct loadparm_context *lp_ctx,
2342 struct loadparm_service *service,
2343 const char *parm_name, FILE * f)
2345 struct parm_struct *parm;
2346 void *ptr;
2347 char *local_parm_name;
2348 char *parm_opt;
2349 const char *parm_opt_value;
2351 /* check for parametrical option */
2352 local_parm_name = talloc_strdup(lp_ctx, parm_name);
2353 if (local_parm_name == NULL) {
2354 return false;
2357 parm_opt = strchr( local_parm_name, ':');
2359 if (parm_opt) {
2360 *parm_opt = '\0';
2361 parm_opt++;
2362 if (strlen(parm_opt)) {
2363 parm_opt_value = lpcfg_parm_string(lp_ctx, service,
2364 local_parm_name, parm_opt);
2365 if (parm_opt_value) {
2366 fprintf(f, "%s\n", parm_opt_value);
2367 return true;
2370 return false;
2373 /* parameter is not parametric, search the table */
2374 parm = lpcfg_parm_struct(lp_ctx, parm_name);
2375 if (!parm) {
2376 return false;
2379 if (service != NULL && parm->p_class == P_GLOBAL) {
2380 return false;
2383 ptr = lpcfg_parm_ptr(lp_ctx, service,parm);
2385 lpcfg_print_parameter(parm, ptr, f);
2386 fprintf(f, "\n");
2387 return true;
2391 * Auto-load some home services.
2393 static void lpcfg_add_auto_services(struct loadparm_context *lp_ctx,
2394 const char *str)
2396 return;
2399 /***************************************************************************
2400 Initialise the sDefault parameter structure for the printer values.
2401 ***************************************************************************/
2403 void init_printer_values(struct loadparm_context *lp_ctx, TALLOC_CTX *ctx,
2404 struct loadparm_service *pService)
2406 /* choose defaults depending on the type of printing */
2407 switch (pService->printing) {
2408 case PRINT_BSD:
2409 case PRINT_AIX:
2410 case PRINT_LPRNT:
2411 case PRINT_LPROS2:
2412 lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P'%p'");
2413 lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P'%p' %j");
2414 lpcfg_string_set(ctx, &pService->print_command, "lpr -r -P'%p' %s");
2415 break;
2417 case PRINT_LPRNG:
2418 case PRINT_PLP:
2419 lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P'%p'");
2420 lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P'%p' %j");
2421 lpcfg_string_set(ctx, &pService->print_command, "lpr -r -P'%p' %s");
2422 lpcfg_string_set(ctx, &pService->queuepause_command, "lpc stop '%p'");
2423 lpcfg_string_set(ctx, &pService->queueresume_command, "lpc start '%p'");
2424 lpcfg_string_set(ctx, &pService->lppause_command, "lpc hold '%p' %j");
2425 lpcfg_string_set(ctx, &pService->lpresume_command, "lpc release '%p' %j");
2426 break;
2428 case PRINT_CUPS:
2429 case PRINT_IPRINT:
2430 /* set the lpq command to contain the destination printer
2431 name only. This is used by cups_queue_get() */
2432 lpcfg_string_set(ctx, &pService->lpq_command, "%p");
2433 lpcfg_string_set(ctx, &pService->lprm_command, "");
2434 lpcfg_string_set(ctx, &pService->print_command, "");
2435 lpcfg_string_set(ctx, &pService->lppause_command, "");
2436 lpcfg_string_set(ctx, &pService->lpresume_command, "");
2437 lpcfg_string_set(ctx, &pService->queuepause_command, "");
2438 lpcfg_string_set(ctx, &pService->queueresume_command, "");
2439 break;
2441 case PRINT_SYSV:
2442 case PRINT_HPUX:
2443 lpcfg_string_set(ctx, &pService->lpq_command, "lpstat -o%p");
2444 lpcfg_string_set(ctx, &pService->lprm_command, "cancel %p-%j");
2445 lpcfg_string_set(ctx, &pService->print_command, "lp -c -d%p %s; rm %s");
2446 lpcfg_string_set(ctx, &pService->queuepause_command, "disable %p");
2447 lpcfg_string_set(ctx, &pService->queueresume_command, "enable %p");
2448 #ifndef HPUX
2449 lpcfg_string_set(ctx, &pService->lppause_command, "lp -i %p-%j -H hold");
2450 lpcfg_string_set(ctx, &pService->lpresume_command, "lp -i %p-%j -H resume");
2451 #endif /* HPUX */
2452 break;
2454 case PRINT_QNX:
2455 lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P%p");
2456 lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P%p %j");
2457 lpcfg_string_set(ctx, &pService->print_command, "lp -r -P%p %s");
2458 break;
2460 #if defined(DEVELOPER) || defined(ENABLE_SELFTEST)
2462 case PRINT_TEST:
2463 case PRINT_VLP: {
2464 const char *tdbfile;
2465 TALLOC_CTX *tmp_ctx = talloc_new(ctx);
2466 const char *tmp;
2468 tmp = lpcfg_parm_string(lp_ctx, NULL, "vlp", "tdbfile");
2469 if (tmp == NULL) {
2470 tmp = "/tmp/vlp.tdb";
2473 tdbfile = talloc_asprintf(tmp_ctx, "tdbfile=%s", tmp);
2474 if (tdbfile == NULL) {
2475 tdbfile="tdbfile=/tmp/vlp.tdb";
2478 tmp = talloc_asprintf(tmp_ctx, "vlp %s print %%p %%s",
2479 tdbfile);
2480 lpcfg_string_set(ctx, &pService->print_command,
2481 tmp ? tmp : "vlp print %p %s");
2483 tmp = talloc_asprintf(tmp_ctx, "vlp %s lpq %%p",
2484 tdbfile);
2485 lpcfg_string_set(ctx, &pService->lpq_command,
2486 tmp ? tmp : "vlp lpq %p");
2488 tmp = talloc_asprintf(tmp_ctx, "vlp %s lprm %%p %%j",
2489 tdbfile);
2490 lpcfg_string_set(ctx, &pService->lprm_command,
2491 tmp ? tmp : "vlp lprm %p %j");
2493 tmp = talloc_asprintf(tmp_ctx, "vlp %s lppause %%p %%j",
2494 tdbfile);
2495 lpcfg_string_set(ctx, &pService->lppause_command,
2496 tmp ? tmp : "vlp lppause %p %j");
2498 tmp = talloc_asprintf(tmp_ctx, "vlp %s lpresume %%p %%j",
2499 tdbfile);
2500 lpcfg_string_set(ctx, &pService->lpresume_command,
2501 tmp ? tmp : "vlp lpresume %p %j");
2503 tmp = talloc_asprintf(tmp_ctx, "vlp %s queuepause %%p",
2504 tdbfile);
2505 lpcfg_string_set(ctx, &pService->queuepause_command,
2506 tmp ? tmp : "vlp queuepause %p");
2508 tmp = talloc_asprintf(tmp_ctx, "vlp %s queueresume %%p",
2509 tdbfile);
2510 lpcfg_string_set(ctx, &pService->queueresume_command,
2511 tmp ? tmp : "vlp queueresume %p");
2512 TALLOC_FREE(tmp_ctx);
2514 break;
2516 #endif /* DEVELOPER */
2522 * Unload unused services.
2525 void lpcfg_killunused(struct loadparm_context *lp_ctx,
2526 struct smbsrv_connection *smb,
2527 bool (*snumused) (struct smbsrv_connection *, int))
2529 int i;
2531 if (lp_ctx->s3_fns != NULL) {
2532 smb_panic("Cannot be used from an s3 loadparm ctx");
2535 for (i = 0; i < lp_ctx->iNumServices; i++) {
2536 if (lp_ctx->services[i] == NULL)
2537 continue;
2539 if (!snumused || !snumused(smb, i)) {
2540 talloc_free(lp_ctx->services[i]);
2541 lp_ctx->services[i] = NULL;
2547 static int lpcfg_destructor(struct loadparm_context *lp_ctx)
2549 struct parmlist_entry *data;
2551 if (lp_ctx->refuse_free) {
2552 /* someone is trying to free the
2553 global_loadparm_context.
2554 We can't allow that. */
2555 return -1;
2558 if (lp_ctx->globals->param_opt != NULL) {
2559 struct parmlist_entry *next;
2560 for (data = lp_ctx->globals->param_opt; data; data=next) {
2561 next = data->next;
2562 if (data->priority & FLAG_CMDLINE) continue;
2563 DLIST_REMOVE(lp_ctx->globals->param_opt, data);
2564 talloc_free(data);
2568 return 0;
2572 * Initialise the global parameter structure.
2574 * Note that most callers should use loadparm_init_global() instead
2576 struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
2578 int i;
2579 char *myname;
2580 struct loadparm_context *lp_ctx;
2581 struct parmlist_entry *parm;
2582 char *logfile;
2584 lp_ctx = talloc_zero(mem_ctx, struct loadparm_context);
2585 if (lp_ctx == NULL)
2586 return NULL;
2588 talloc_set_destructor(lp_ctx, lpcfg_destructor);
2589 lp_ctx->bInGlobalSection = true;
2590 lp_ctx->globals = talloc_zero(lp_ctx, struct loadparm_global);
2591 /* This appears odd, but globals in s3 isn't a pointer */
2592 lp_ctx->globals->ctx = lp_ctx->globals;
2593 lp_ctx->globals->rpc_low_port = SERVER_TCP_LOW_PORT;
2594 lp_ctx->globals->rpc_high_port = SERVER_TCP_HIGH_PORT;
2595 lp_ctx->sDefault = talloc_zero(lp_ctx, struct loadparm_service);
2596 lp_ctx->flags = talloc_zero_array(lp_ctx, unsigned int, num_parameters());
2598 lp_ctx->sDefault->max_print_jobs = 1000;
2599 lp_ctx->sDefault->available = true;
2600 lp_ctx->sDefault->browseable = true;
2601 lp_ctx->sDefault->read_only = true;
2602 lp_ctx->sDefault->map_archive = true;
2603 lp_ctx->sDefault->strict_locking = true;
2604 lp_ctx->sDefault->oplocks = true;
2605 lp_ctx->sDefault->create_mask = 0744;
2606 lp_ctx->sDefault->force_create_mode = 0000;
2607 lp_ctx->sDefault->directory_mask = 0755;
2608 lp_ctx->sDefault->force_directory_mode = 0000;
2609 lp_ctx->sDefault->aio_read_size = 1;
2610 lp_ctx->sDefault->aio_write_size = 1;
2611 lp_ctx->sDefault->smbd_search_ask_sharemode = true;
2612 lp_ctx->sDefault->smbd_getinfo_ask_sharemode = true;
2614 DEBUG(3, ("Initialising global parameters\n"));
2616 for (i = 0; parm_table[i].label; i++) {
2617 if ((parm_table[i].type == P_STRING ||
2618 parm_table[i].type == P_USTRING) &&
2619 !(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2620 TALLOC_CTX *parent_mem;
2621 char **r;
2622 if (parm_table[i].p_class == P_LOCAL) {
2623 parent_mem = lp_ctx->sDefault;
2624 r = (char **)(((char *)lp_ctx->sDefault) + parm_table[i].offset);
2625 } else {
2626 parent_mem = lp_ctx->globals;
2627 r = (char **)(((char *)lp_ctx->globals) + parm_table[i].offset);
2629 lpcfg_string_set(parent_mem, r, "");
2633 logfile = talloc_asprintf(lp_ctx, "%s/log.samba", dyn_LOGFILEBASE);
2634 lpcfg_do_global_parameter(lp_ctx, "log file", logfile);
2635 talloc_free(logfile);
2637 lpcfg_do_global_parameter(lp_ctx, "log level", "0");
2639 lpcfg_do_global_parameter(lp_ctx, "syslog", "1");
2640 lpcfg_do_global_parameter(lp_ctx, "syslog only", "No");
2641 lpcfg_do_global_parameter(lp_ctx, "debug timestamp", "Yes");
2642 lpcfg_do_global_parameter(lp_ctx, "debug prefix timestamp", "No");
2643 lpcfg_do_global_parameter(lp_ctx, "debug hires timestamp", "Yes");
2644 lpcfg_do_global_parameter(lp_ctx, "debug pid", "No");
2645 lpcfg_do_global_parameter(lp_ctx, "debug uid", "No");
2646 lpcfg_do_global_parameter(lp_ctx, "debug class", "No");
2648 lpcfg_do_global_parameter(lp_ctx, "share backend", "classic");
2650 lpcfg_do_global_parameter(lp_ctx, "server role", "auto");
2651 lpcfg_do_global_parameter(lp_ctx, "domain logons", "No");
2652 lpcfg_do_global_parameter(lp_ctx, "domain master", "Auto");
2654 /* options that can be set on the command line must be initialised via
2655 the slower lpcfg_do_global_parameter() to ensure that FLAG_CMDLINE is obeyed */
2656 #ifdef TCP_NODELAY
2657 lpcfg_do_global_parameter(lp_ctx, "socket options", "TCP_NODELAY");
2658 #endif
2659 lpcfg_do_global_parameter(lp_ctx, "workgroup", DEFAULT_WORKGROUP);
2660 myname = get_myname(lp_ctx);
2661 lpcfg_do_global_parameter(lp_ctx, "netbios name", myname);
2662 talloc_free(myname);
2663 lpcfg_do_global_parameter(lp_ctx,
2664 "name resolve order",
2665 DEFAULT_NAME_RESOLVE_ORDER);
2667 lpcfg_do_global_parameter(lp_ctx, "fstype", "NTFS");
2669 lpcfg_do_global_parameter(lp_ctx, "ntvfs handler", "unixuid default");
2670 lpcfg_do_global_parameter(lp_ctx, "max connections", "0");
2672 lpcfg_do_global_parameter(lp_ctx, "dcerpc endpoint servers", "epmapper wkssvc rpcecho samr netlogon lsarpc drsuapi dssetup unixinfo browser eventlog6 backupkey dnsserver");
2673 lpcfg_do_global_parameter(lp_ctx, "server services", "s3fs rpc nbt wrepl ldap cldap kdc drepl winbindd ntp_signd kcc dnsupdate dns");
2674 lpcfg_do_global_parameter(lp_ctx, "kccsrv:samba_kcc", "true");
2675 /* the winbind method for domain controllers is for both RODC
2676 auth forwarding and for trusted domains */
2677 lpcfg_do_global_parameter(lp_ctx, "private dir", dyn_PRIVATE_DIR);
2678 lpcfg_do_global_parameter(lp_ctx, "binddns dir", dyn_BINDDNS_DIR);
2679 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_LOCAL_MACHINE", "hklm.ldb");
2681 /* This hive should be dynamically generated by Samba using
2682 data from the sam, but for the moment leave it in a tdb to
2683 keep regedt32 from popping up an annoying dialog. */
2684 lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_USERS", "hku.ldb");
2686 /* using UTF8 by default allows us to support all chars */
2687 lpcfg_do_global_parameter(lp_ctx, "unix charset", "UTF-8");
2689 /* Use codepage 850 as a default for the dos character set */
2690 lpcfg_do_global_parameter(lp_ctx, "dos charset", "CP850");
2693 * Allow the default PASSWD_CHAT to be overridden in local.h.
2695 lpcfg_do_global_parameter(lp_ctx, "passwd chat", DEFAULT_PASSWD_CHAT);
2697 lpcfg_do_global_parameter(lp_ctx, "pid directory", dyn_PIDDIR);
2698 lpcfg_do_global_parameter(lp_ctx, "lock dir", dyn_LOCKDIR);
2699 lpcfg_do_global_parameter(lp_ctx, "state directory", dyn_STATEDIR);
2700 lpcfg_do_global_parameter(lp_ctx, "cache directory", dyn_CACHEDIR);
2701 lpcfg_do_global_parameter(lp_ctx, "ncalrpc dir", dyn_NCALRPCDIR);
2703 lpcfg_do_global_parameter(lp_ctx, "nbt client socket address", "0.0.0.0");
2704 lpcfg_do_global_parameter_var(lp_ctx, "server string",
2705 "Samba %s", SAMBA_VERSION_STRING);
2707 lpcfg_do_global_parameter(lp_ctx, "password server", "*");
2709 lpcfg_do_global_parameter(lp_ctx, "max mux", "50");
2710 lpcfg_do_global_parameter(lp_ctx, "max xmit", "16644");
2711 lpcfg_do_global_parameter(lp_ctx, "host msdfs", "true");
2713 lpcfg_do_global_parameter(lp_ctx, "LargeReadwrite", "True");
2714 lpcfg_do_global_parameter(lp_ctx, "server min protocol", "LANMAN1");
2715 lpcfg_do_global_parameter(lp_ctx, "server max protocol", "SMB3");
2716 lpcfg_do_global_parameter(lp_ctx, "client min protocol", "CORE");
2717 lpcfg_do_global_parameter(lp_ctx, "client max protocol", "default");
2718 lpcfg_do_global_parameter(lp_ctx, "client ipc min protocol", "default");
2719 lpcfg_do_global_parameter(lp_ctx, "client ipc max protocol", "default");
2720 lpcfg_do_global_parameter(lp_ctx, "security", "AUTO");
2721 lpcfg_do_global_parameter(lp_ctx, "EncryptPasswords", "True");
2722 lpcfg_do_global_parameter(lp_ctx, "ReadRaw", "True");
2723 lpcfg_do_global_parameter(lp_ctx, "WriteRaw", "True");
2724 lpcfg_do_global_parameter(lp_ctx, "NullPasswords", "False");
2725 lpcfg_do_global_parameter(lp_ctx, "old password allowed period", "60");
2726 lpcfg_do_global_parameter(lp_ctx, "ObeyPamRestrictions", "False");
2728 lpcfg_do_global_parameter(lp_ctx, "TimeServer", "False");
2729 lpcfg_do_global_parameter(lp_ctx, "BindInterfacesOnly", "False");
2730 lpcfg_do_global_parameter(lp_ctx, "Unicode", "True");
2731 lpcfg_do_global_parameter(lp_ctx, "ClientLanManAuth", "False");
2732 lpcfg_do_global_parameter(lp_ctx, "ClientNTLMv2Auth", "True");
2733 lpcfg_do_global_parameter(lp_ctx, "LanmanAuth", "False");
2734 lpcfg_do_global_parameter(lp_ctx, "NTLMAuth", "ntlmv2-only");
2735 lpcfg_do_global_parameter(lp_ctx, "RawNTLMv2Auth", "False");
2736 lpcfg_do_global_parameter(lp_ctx, "client use spnego principal", "False");
2738 lpcfg_do_global_parameter(lp_ctx, "allow dcerpc auth level connect", "False");
2740 lpcfg_do_global_parameter(lp_ctx, "UnixExtensions", "True");
2742 lpcfg_do_global_parameter(lp_ctx, "PreferredMaster", "Auto");
2743 lpcfg_do_global_parameter(lp_ctx, "LocalMaster", "True");
2745 lpcfg_do_global_parameter(lp_ctx, "wins support", "False");
2746 lpcfg_do_global_parameter(lp_ctx, "dns proxy", "True");
2748 lpcfg_do_global_parameter(lp_ctx, "winbind separator", "\\");
2749 lpcfg_do_global_parameter(lp_ctx, "winbind sealed pipes", "True");
2750 lpcfg_do_global_parameter(lp_ctx, "winbind scan trusted domains", "True");
2751 lpcfg_do_global_parameter(lp_ctx, "require strong key", "True");
2752 lpcfg_do_global_parameter(lp_ctx, "winbindd socket directory", dyn_WINBINDD_SOCKET_DIR);
2753 lpcfg_do_global_parameter(lp_ctx, "ntp signd socket directory", dyn_NTP_SIGND_SOCKET_DIR);
2754 lpcfg_do_global_parameter_var(lp_ctx, "gpo update command", "%s/samba-gpupdate", dyn_SCRIPTSBINDIR);
2755 lpcfg_do_global_parameter_var(lp_ctx, "apply group policies", "False");
2756 lpcfg_do_global_parameter_var(lp_ctx, "dns update command", "%s/samba_dnsupdate", dyn_SCRIPTSBINDIR);
2757 lpcfg_do_global_parameter_var(lp_ctx, "spn update command", "%s/samba_spnupdate", dyn_SCRIPTSBINDIR);
2758 lpcfg_do_global_parameter_var(lp_ctx, "samba kcc command",
2759 "%s/samba_kcc", dyn_SCRIPTSBINDIR);
2760 #ifdef MIT_KDC_PATH
2761 lpcfg_do_global_parameter_var(lp_ctx,
2762 "mit kdc command",
2763 MIT_KDC_PATH);
2764 #endif
2765 lpcfg_do_global_parameter(lp_ctx, "template shell", "/bin/false");
2766 lpcfg_do_global_parameter(lp_ctx, "template homedir", "/home/%D/%U");
2768 lpcfg_do_global_parameter(lp_ctx, "client signing", "default");
2769 lpcfg_do_global_parameter(lp_ctx, "client ipc signing", "default");
2770 lpcfg_do_global_parameter(lp_ctx, "server signing", "default");
2772 lpcfg_do_global_parameter(lp_ctx, "use mmap", "True");
2774 lpcfg_do_global_parameter(lp_ctx, "smb ports", "445 139");
2775 lpcfg_do_global_parameter_var(lp_ctx, "nbt port", "%d", NBT_NAME_SERVICE_PORT);
2776 lpcfg_do_global_parameter_var(lp_ctx, "dgram port", "%d", NBT_DGRAM_SERVICE_PORT);
2777 lpcfg_do_global_parameter(lp_ctx, "cldap port", "389");
2778 lpcfg_do_global_parameter(lp_ctx, "krb5 port", "88");
2779 lpcfg_do_global_parameter(lp_ctx, "kpasswd port", "464");
2781 lpcfg_do_global_parameter(lp_ctx, "nt status support", "True");
2783 lpcfg_do_global_parameter(lp_ctx, "max wins ttl", "518400"); /* 6 days */
2784 lpcfg_do_global_parameter(lp_ctx, "min wins ttl", "21600");
2786 lpcfg_do_global_parameter(lp_ctx, "tls enabled", "True");
2787 lpcfg_do_global_parameter(lp_ctx, "tls verify peer", "as_strict_as_possible");
2788 lpcfg_do_global_parameter(lp_ctx, "tls keyfile", "tls/key.pem");
2789 lpcfg_do_global_parameter(lp_ctx, "tls certfile", "tls/cert.pem");
2790 lpcfg_do_global_parameter(lp_ctx, "tls cafile", "tls/ca.pem");
2791 lpcfg_do_global_parameter(lp_ctx, "tls priority", "NORMAL:-VERS-SSL3.0");
2793 lpcfg_do_global_parameter(lp_ctx, "rndc command", "/usr/sbin/rndc");
2794 lpcfg_do_global_parameter(lp_ctx, "nsupdate command", "/usr/bin/nsupdate -g");
2796 lpcfg_do_global_parameter(lp_ctx, "allow dns updates", "secure only");
2797 lpcfg_do_global_parameter(lp_ctx, "dns zone scavenging", "False");
2798 lpcfg_do_global_parameter(lp_ctx, "dns forwarder", "");
2800 lpcfg_do_global_parameter(lp_ctx, "algorithmic rid base", "1000");
2802 lpcfg_do_global_parameter(lp_ctx, "enhanced browsing", "True");
2804 lpcfg_do_global_parameter(lp_ctx, "winbind nss info", "template");
2806 lpcfg_do_global_parameter(lp_ctx, "server schannel", "True");
2808 lpcfg_do_global_parameter(lp_ctx, "short preserve case", "True");
2810 lpcfg_do_global_parameter(lp_ctx, "max open files", "16384");
2812 lpcfg_do_global_parameter(lp_ctx, "cups connection timeout", "30");
2814 lpcfg_do_global_parameter(lp_ctx, "locking", "True");
2816 lpcfg_do_global_parameter(lp_ctx, "block size", "1024");
2818 lpcfg_do_global_parameter(lp_ctx, "client use spnego", "True");
2820 lpcfg_do_global_parameter(lp_ctx, "change notify", "True");
2822 lpcfg_do_global_parameter(lp_ctx, "name cache timeout", "660");
2824 lpcfg_do_global_parameter(lp_ctx, "defer sharing violations", "True");
2826 lpcfg_do_global_parameter(lp_ctx, "ldap replication sleep", "1000");
2828 lpcfg_do_global_parameter(lp_ctx, "idmap backend", "tdb");
2830 lpcfg_do_global_parameter(lp_ctx, "enable privileges", "True");
2832 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max write", "%u", DEFAULT_SMB2_MAX_WRITE);
2834 lpcfg_do_global_parameter(lp_ctx, "passdb backend", "tdbsam");
2836 lpcfg_do_global_parameter(lp_ctx, "deadtime", "10080");
2838 lpcfg_do_global_parameter(lp_ctx, "getwd cache", "True");
2840 lpcfg_do_global_parameter(lp_ctx, "winbind nested groups", "True");
2842 lpcfg_do_global_parameter(lp_ctx, "mangled names", "True");
2844 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max credits", "%u", DEFAULT_SMB2_MAX_CREDITS);
2846 lpcfg_do_global_parameter(lp_ctx, "ldap ssl", "start tls");
2848 lpcfg_do_global_parameter(lp_ctx, "ldap deref", "auto");
2850 lpcfg_do_global_parameter(lp_ctx, "lm interval", "60");
2852 lpcfg_do_global_parameter(lp_ctx, "mangling method", "hash2");
2854 lpcfg_do_global_parameter(lp_ctx, "hide dot files", "True");
2856 lpcfg_do_global_parameter(lp_ctx, "browse list", "True");
2858 lpcfg_do_global_parameter(lp_ctx, "passwd chat timeout", "2");
2860 lpcfg_do_global_parameter(lp_ctx, "guest account", GUEST_ACCOUNT);
2862 lpcfg_do_global_parameter(lp_ctx, "client schannel", "True");
2864 lpcfg_do_global_parameter(lp_ctx, "smb encrypt", "default");
2866 lpcfg_do_global_parameter(lp_ctx, "max log size", "5000");
2868 lpcfg_do_global_parameter(lp_ctx, "idmap negative cache time", "120");
2870 lpcfg_do_global_parameter(lp_ctx, "ldap follow referral", "auto");
2872 lpcfg_do_global_parameter(lp_ctx, "multicast dns register", "yes");
2874 lpcfg_do_global_parameter(lp_ctx, "winbind reconnect delay", "30");
2876 lpcfg_do_global_parameter(lp_ctx, "winbind request timeout", "60");
2878 lpcfg_do_global_parameter(lp_ctx, "nt acl support", "yes");
2880 lpcfg_do_global_parameter(lp_ctx, "acl check permissions", "yes");
2882 lpcfg_do_global_parameter(lp_ctx, "keepalive", "300");
2884 lpcfg_do_global_parameter(lp_ctx, "smbd profiling level", "off");
2886 lpcfg_do_global_parameter(lp_ctx, "winbind cache time", "300");
2888 lpcfg_do_global_parameter(lp_ctx, "level2 oplocks", "yes");
2890 lpcfg_do_global_parameter(lp_ctx, "show add printer wizard", "yes");
2892 lpcfg_do_global_parameter(lp_ctx, "allocation roundup size", "1048576");
2894 lpcfg_do_global_parameter(lp_ctx, "ldap page size", "1000");
2896 lpcfg_do_global_parameter(lp_ctx, "kernel share modes", "yes");
2898 lpcfg_do_global_parameter(lp_ctx, "strict locking", "Auto");
2900 lpcfg_do_global_parameter(lp_ctx, "strict sync", "yes");
2902 lpcfg_do_global_parameter(lp_ctx, "map readonly", "no");
2904 lpcfg_do_global_parameter(lp_ctx, "allow trusted domains", "yes");
2906 lpcfg_do_global_parameter(lp_ctx, "default devmode", "yes");
2908 lpcfg_do_global_parameter(lp_ctx, "os level", "20");
2910 lpcfg_do_global_parameter(lp_ctx, "dos filetimes", "yes");
2912 lpcfg_do_global_parameter(lp_ctx, "mangling char", "~");
2914 lpcfg_do_global_parameter(lp_ctx, "printcap cache time", "750");
2916 lpcfg_do_global_parameter(lp_ctx, "create krb5 conf", "yes");
2918 lpcfg_do_global_parameter(lp_ctx, "winbind max clients", "200");
2920 lpcfg_do_global_parameter(lp_ctx, "acl map full control", "yes");
2922 lpcfg_do_global_parameter(lp_ctx, "nt pipe support", "yes");
2924 lpcfg_do_global_parameter(lp_ctx, "ldap debug threshold", "10");
2926 lpcfg_do_global_parameter(lp_ctx, "client ldap sasl wrapping", "sign");
2928 lpcfg_do_global_parameter(lp_ctx, "mdns name", "netbios");
2930 lpcfg_do_global_parameter(lp_ctx, "ldap server require strong auth", "yes");
2932 lpcfg_do_global_parameter(lp_ctx, "follow symlinks", "yes");
2934 lpcfg_do_global_parameter(lp_ctx, "machine password timeout", "604800");
2936 lpcfg_do_global_parameter(lp_ctx, "ldap connection timeout", "2");
2938 lpcfg_do_global_parameter(lp_ctx, "winbind expand groups", "0");
2940 lpcfg_do_global_parameter(lp_ctx, "stat cache", "yes");
2942 lpcfg_do_global_parameter(lp_ctx, "lpq cache time", "30");
2944 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max trans", "%u", DEFAULT_SMB2_MAX_TRANSACT);
2946 lpcfg_do_global_parameter_var(lp_ctx, "smb2 max read", "%u", DEFAULT_SMB2_MAX_READ);
2948 lpcfg_do_global_parameter(lp_ctx, "durable handles", "yes");
2950 lpcfg_do_global_parameter(lp_ctx, "max stat cache size", "512");
2952 lpcfg_do_global_parameter(lp_ctx, "ldap passwd sync", "no");
2954 lpcfg_do_global_parameter(lp_ctx, "kernel change notify", "yes");
2956 lpcfg_do_global_parameter(lp_ctx, "max ttl", "259200");
2958 lpcfg_do_global_parameter(lp_ctx, "blocking locks", "yes");
2960 lpcfg_do_global_parameter(lp_ctx, "load printers", "yes");
2962 lpcfg_do_global_parameter(lp_ctx, "idmap cache time", "604800");
2964 lpcfg_do_global_parameter(lp_ctx, "preserve case", "yes");
2966 lpcfg_do_global_parameter(lp_ctx, "lm announce", "auto");
2968 lpcfg_do_global_parameter(lp_ctx, "afs token lifetime", "604800");
2970 lpcfg_do_global_parameter(lp_ctx, "enable core files", "yes");
2972 lpcfg_do_global_parameter(lp_ctx, "winbind max domain connections", "1");
2974 lpcfg_do_global_parameter(lp_ctx, "case sensitive", "auto");
2976 lpcfg_do_global_parameter(lp_ctx, "ldap timeout", "15");
2978 lpcfg_do_global_parameter(lp_ctx, "mangle prefix", "1");
2980 lpcfg_do_global_parameter(lp_ctx, "posix locking", "yes");
2982 lpcfg_do_global_parameter(lp_ctx, "lock spin time", "200");
2984 lpcfg_do_global_parameter(lp_ctx, "directory name cache size", "100");
2986 lpcfg_do_global_parameter(lp_ctx, "nmbd bind explicit broadcast", "yes");
2988 lpcfg_do_global_parameter(lp_ctx, "init logon delay", "100");
2990 lpcfg_do_global_parameter(lp_ctx, "usershare owner only", "yes");
2992 lpcfg_do_global_parameter(lp_ctx, "-valid", "yes");
2994 lpcfg_do_global_parameter_var(lp_ctx, "usershare path", "%s/usershares", get_dyn_STATEDIR());
2996 #ifdef DEVELOPER
2997 lpcfg_do_global_parameter_var(lp_ctx, "panic action", "/bin/sleep 999999999");
2998 #endif
3000 lpcfg_do_global_parameter(lp_ctx, "smb passwd file", get_dyn_SMB_PASSWD_FILE());
3002 lpcfg_do_global_parameter(lp_ctx, "logon home", "\\\\%N\\%U");
3004 lpcfg_do_global_parameter(lp_ctx, "logon path", "\\\\%N\\%U\\profile");
3006 lpcfg_do_global_parameter(lp_ctx, "printjob username", "%U");
3008 lpcfg_do_global_parameter(lp_ctx, "aio max threads", "100");
3010 lpcfg_do_global_parameter(lp_ctx, "smb2 leases", "yes");
3012 lpcfg_do_global_parameter(lp_ctx, "kerberos encryption types", "all");
3014 lpcfg_do_global_parameter(lp_ctx,
3015 "rpc server dynamic port range",
3016 "49152-65535");
3018 lpcfg_do_global_parameter(lp_ctx, "prefork children", "4");
3019 lpcfg_do_global_parameter(lp_ctx, "prefork backoff increment", "10");
3020 lpcfg_do_global_parameter(lp_ctx, "prefork maximum backoff", "120");
3022 lpcfg_do_global_parameter(lp_ctx, "check parent directory delete on close", "no");
3024 lpcfg_do_global_parameter(lp_ctx, "ea support", "yes");
3026 lpcfg_do_global_parameter(lp_ctx, "store dos attributes", "yes");
3028 lpcfg_do_global_parameter(lp_ctx, "debug encryption", "no");
3030 for (i = 0; parm_table[i].label; i++) {
3031 if (!(lp_ctx->flags[i] & FLAG_CMDLINE)) {
3032 lp_ctx->flags[i] |= FLAG_DEFAULT;
3036 for (parm=lp_ctx->globals->param_opt; parm; parm=parm->next) {
3037 if (!(parm->priority & FLAG_CMDLINE)) {
3038 parm->priority |= FLAG_DEFAULT;
3042 for (parm=lp_ctx->sDefault->param_opt; parm; parm=parm->next) {
3043 if (!(parm->priority & FLAG_CMDLINE)) {
3044 parm->priority |= FLAG_DEFAULT;
3048 return lp_ctx;
3052 * Initialise the global parameter structure.
3054 struct loadparm_context *loadparm_init_global(bool load_default)
3056 if (global_loadparm_context == NULL) {
3057 global_loadparm_context = loadparm_init(NULL);
3059 if (global_loadparm_context == NULL) {
3060 return NULL;
3062 global_loadparm_context->global = true;
3063 if (load_default && !global_loadparm_context->loaded) {
3064 lpcfg_load_default(global_loadparm_context);
3066 global_loadparm_context->refuse_free = true;
3067 return global_loadparm_context;
3071 * Initialise the global parameter structure.
3073 struct loadparm_context *loadparm_init_s3(TALLOC_CTX *mem_ctx,
3074 const struct loadparm_s3_helpers *s3_fns)
3076 struct loadparm_context *loadparm_context = talloc_zero(mem_ctx, struct loadparm_context);
3077 if (!loadparm_context) {
3078 return NULL;
3080 loadparm_context->s3_fns = s3_fns;
3081 loadparm_context->globals = s3_fns->globals;
3082 loadparm_context->flags = s3_fns->flags;
3084 return loadparm_context;
3087 const char *lpcfg_configfile(struct loadparm_context *lp_ctx)
3089 return lp_ctx->szConfigFile;
3092 const char *lp_default_path(void)
3094 if (getenv("SMB_CONF_PATH"))
3095 return getenv("SMB_CONF_PATH");
3096 else
3097 return dyn_CONFIGFILE;
3101 * Update the internal state of a loadparm context after settings
3102 * have changed.
3104 static bool lpcfg_update(struct loadparm_context *lp_ctx)
3106 struct debug_settings settings;
3107 TALLOC_CTX *tmp_ctx;
3109 tmp_ctx = talloc_new(lp_ctx);
3110 if (tmp_ctx == NULL) {
3111 return false;
3114 lpcfg_add_auto_services(lp_ctx, lpcfg_auto_services(lp_ctx, tmp_ctx));
3116 if (!lp_ctx->globals->wins_server_list && lp_ctx->globals->we_are_a_wins_server) {
3117 lpcfg_do_global_parameter(lp_ctx, "wins server", "127.0.0.1");
3120 if (!lp_ctx->global) {
3121 TALLOC_FREE(tmp_ctx);
3122 return true;
3125 panic_action = lp_ctx->globals->panic_action;
3127 reload_charcnv(lp_ctx);
3129 ZERO_STRUCT(settings);
3130 /* Add any more debug-related smb.conf parameters created in
3131 * future here */
3132 settings.timestamp_logs = lp_ctx->globals->timestamp_logs;
3133 settings.debug_prefix_timestamp = lp_ctx->globals->debug_prefix_timestamp;
3134 settings.debug_hires_timestamp = lp_ctx->globals->debug_hires_timestamp;
3135 settings.debug_pid = lp_ctx->globals->debug_pid;
3136 settings.debug_uid = lp_ctx->globals->debug_uid;
3137 settings.debug_class = lp_ctx->globals->debug_class;
3138 debug_set_settings(&settings, lp_ctx->globals->logging,
3139 lp_ctx->globals->syslog,
3140 lp_ctx->globals->syslog_only);
3142 /* FIXME: This is a bit of a hack, but we can't use a global, since
3143 * not everything that uses lp also uses the socket library */
3144 if (lpcfg_parm_bool(lp_ctx, NULL, "socket", "testnonblock", false)) {
3145 setenv("SOCKET_TESTNONBLOCK", "1", 1);
3146 } else {
3147 unsetenv("SOCKET_TESTNONBLOCK");
3150 TALLOC_FREE(tmp_ctx);
3151 return true;
3154 bool lpcfg_load_default(struct loadparm_context *lp_ctx)
3156 const char *path;
3158 path = lp_default_path();
3160 if (!file_exist(path)) {
3161 /* We allow the default smb.conf file to not exist,
3162 * basically the equivalent of an empty file. */
3163 return lpcfg_update(lp_ctx);
3166 return lpcfg_load(lp_ctx, path);
3170 * Load the services array from the services file.
3172 * Return True on success, False on failure.
3174 static bool lpcfg_load_internal(struct loadparm_context *lp_ctx,
3175 const char *filename, bool set_global)
3177 char *n2;
3178 bool bRetval;
3180 filename = talloc_strdup(lp_ctx, filename);
3182 lp_ctx->szConfigFile = filename;
3184 if (lp_ctx->s3_fns) {
3185 return lp_ctx->s3_fns->load(filename);
3188 lp_ctx->bInGlobalSection = true;
3189 n2 = standard_sub_basic(lp_ctx, lp_ctx->szConfigFile);
3190 DEBUG(2, ("lpcfg_load: refreshing parameters from %s\n", n2));
3192 add_to_file_list(lp_ctx, &lp_ctx->file_lists, lp_ctx->szConfigFile, n2);
3194 /* We get sections first, so have to start 'behind' to make up */
3195 lp_ctx->currentService = NULL;
3196 bRetval = pm_process(n2, do_section, lpcfg_do_parameter, lp_ctx);
3198 /* finish up the last section */
3199 DEBUG(4, ("pm_process() returned %s\n", BOOLSTR(bRetval)));
3200 if (bRetval)
3201 if (lp_ctx->currentService != NULL)
3202 bRetval = lpcfg_service_ok(lp_ctx->currentService);
3204 bRetval = bRetval && lpcfg_update(lp_ctx);
3206 /* we do this unconditionally, so that it happens even
3207 for a missing smb.conf */
3208 reload_charcnv(lp_ctx);
3210 if (bRetval == true && set_global) {
3211 /* set this up so that any child python tasks will
3212 find the right smb.conf */
3213 setenv("SMB_CONF_PATH", filename, 1);
3215 /* set the context used by the lp_*() function
3216 varients */
3217 global_loadparm_context = lp_ctx;
3218 lp_ctx->loaded = true;
3221 return bRetval;
3224 bool lpcfg_load_no_global(struct loadparm_context *lp_ctx, const char *filename)
3226 return lpcfg_load_internal(lp_ctx, filename, false);
3229 bool lpcfg_load(struct loadparm_context *lp_ctx, const char *filename)
3231 return lpcfg_load_internal(lp_ctx, filename, true);
3235 * Return the max number of services.
3238 int lpcfg_numservices(struct loadparm_context *lp_ctx)
3240 if (lp_ctx->s3_fns) {
3241 return lp_ctx->s3_fns->get_numservices();
3244 return lp_ctx->iNumServices;
3248 * Display the contents of the services array in human-readable form.
3251 void lpcfg_dump(struct loadparm_context *lp_ctx, FILE *f, bool show_defaults,
3252 int maxtoprint)
3254 int iService;
3256 if (lp_ctx->s3_fns) {
3257 lp_ctx->s3_fns->dump(f, show_defaults, maxtoprint);
3258 return;
3261 lpcfg_dump_globals(lp_ctx, f, show_defaults);
3263 lpcfg_dump_a_service(lp_ctx->sDefault, lp_ctx->sDefault, f, lp_ctx->flags, show_defaults);
3265 for (iService = 0; iService < maxtoprint; iService++)
3266 lpcfg_dump_one(f, show_defaults, lp_ctx->services[iService], lp_ctx->sDefault);
3270 * Display the contents of one service in human-readable form.
3272 void lpcfg_dump_one(FILE *f, bool show_defaults, struct loadparm_service *service, struct loadparm_service *sDefault)
3274 if (service != NULL) {
3275 if (service->szService[0] == '\0')
3276 return;
3277 lpcfg_dump_a_service(service, sDefault, f, NULL, show_defaults);
3281 struct loadparm_service *lpcfg_servicebynum(struct loadparm_context *lp_ctx,
3282 int snum)
3284 if (lp_ctx->s3_fns) {
3285 return lp_ctx->s3_fns->get_servicebynum(snum);
3288 return lp_ctx->services[snum];
3291 struct loadparm_service *lpcfg_service(struct loadparm_context *lp_ctx,
3292 const char *service_name)
3294 int iService;
3295 char *serviceName;
3297 if (lp_ctx->s3_fns) {
3298 return lp_ctx->s3_fns->get_service(service_name);
3301 for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--) {
3302 if (lp_ctx->services[iService] &&
3303 lp_ctx->services[iService]->szService) {
3305 * The substitution here is used to support %U is
3306 * service names
3308 serviceName = standard_sub_basic(
3309 lp_ctx->services[iService],
3310 lp_ctx->services[iService]->szService);
3311 if (strequal(serviceName, service_name)) {
3312 talloc_free(serviceName);
3313 return lp_ctx->services[iService];
3315 talloc_free(serviceName);
3319 DEBUG(7,("lpcfg_servicenumber: couldn't find %s\n", service_name));
3320 return NULL;
3323 const char *lpcfg_servicename(const struct loadparm_service *service)
3325 return service ? lpcfg_string((const char *)service->szService) : NULL;
3329 * A useful volume label function.
3331 const char *lpcfg_volume_label(struct loadparm_service *service, struct loadparm_service *sDefault)
3333 const char *ret;
3334 ret = lpcfg_string((const char *)((service != NULL && service->volume != NULL) ?
3335 service->volume : sDefault->volume));
3336 if (!*ret)
3337 return lpcfg_servicename(service);
3338 return ret;
3342 * Return the correct printer name.
3344 const char *lpcfg_printername(struct loadparm_service *service, struct loadparm_service *sDefault)
3346 const char *ret;
3347 ret = lpcfg_string((const char *)((service != NULL && service->_printername != NULL) ?
3348 service->_printername : sDefault->_printername));
3349 if (ret == NULL || (ret != NULL && *ret == '\0'))
3350 ret = lpcfg_servicename(service);
3352 return ret;
3357 * Return the max print jobs per queue.
3359 int lpcfg_maxprintjobs(struct loadparm_service *service, struct loadparm_service *sDefault)
3361 int maxjobs = lpcfg_max_print_jobs(service, sDefault);
3363 if (maxjobs <= 0 || maxjobs >= PRINT_MAX_JOBID)
3364 maxjobs = PRINT_MAX_JOBID - 1;
3366 return maxjobs;
3369 struct smb_iconv_handle *lpcfg_iconv_handle(struct loadparm_context *lp_ctx)
3371 if (lp_ctx == NULL) {
3372 return get_iconv_handle();
3374 return lp_ctx->iconv_handle;
3377 _PUBLIC_ void reload_charcnv(struct loadparm_context *lp_ctx)
3379 if (!lp_ctx->global) {
3380 return;
3383 lp_ctx->iconv_handle =
3384 reinit_iconv_handle(lp_ctx,
3385 lpcfg_dos_charset(lp_ctx),
3386 lpcfg_unix_charset(lp_ctx));
3387 if (lp_ctx->iconv_handle == NULL) {
3388 smb_panic("reinit_iconv_handle failed");
3392 _PUBLIC_ char *lpcfg_tls_keyfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3394 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_keyfile(lp_ctx));
3397 _PUBLIC_ char *lpcfg_tls_certfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3399 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_certfile(lp_ctx));
3402 _PUBLIC_ char *lpcfg_tls_cafile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3404 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_cafile(lp_ctx));
3407 _PUBLIC_ char *lpcfg_tls_crlfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3409 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_crlfile(lp_ctx));
3412 _PUBLIC_ char *lpcfg_tls_dhpfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3414 return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_dhpfile(lp_ctx));
3417 struct gensec_settings *lpcfg_gensec_settings(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3419 struct gensec_settings *settings = talloc_zero(mem_ctx, struct gensec_settings);
3420 if (settings == NULL)
3421 return NULL;
3422 SMB_ASSERT(lp_ctx != NULL);
3423 settings->lp_ctx = talloc_reference(settings, lp_ctx);
3424 settings->target_hostname = lpcfg_parm_string(lp_ctx, NULL, "gensec", "target_hostname");
3425 return settings;
3428 int lpcfg_server_role(struct loadparm_context *lp_ctx)
3430 int domain_master = lpcfg__domain_master(lp_ctx);
3432 return lp_find_server_role(lpcfg__server_role(lp_ctx),
3433 lpcfg__security(lp_ctx),
3434 lpcfg__domain_logons(lp_ctx),
3435 (domain_master == true) ||
3436 (domain_master == Auto));
3439 int lpcfg_security(struct loadparm_context *lp_ctx)
3441 return lp_find_security(lpcfg__server_role(lp_ctx),
3442 lpcfg__security(lp_ctx));
3445 int lpcfg_client_max_protocol(struct loadparm_context *lp_ctx)
3447 int client_max_protocol = lpcfg__client_max_protocol(lp_ctx);
3448 if (client_max_protocol == PROTOCOL_DEFAULT) {
3449 return PROTOCOL_LATEST;
3451 return client_max_protocol;
3454 int lpcfg_client_ipc_min_protocol(struct loadparm_context *lp_ctx)
3456 int client_ipc_min_protocol = lpcfg__client_ipc_min_protocol(lp_ctx);
3457 if (client_ipc_min_protocol == PROTOCOL_DEFAULT) {
3458 client_ipc_min_protocol = lpcfg_client_min_protocol(lp_ctx);
3460 if (client_ipc_min_protocol < PROTOCOL_NT1) {
3461 return PROTOCOL_NT1;
3463 return client_ipc_min_protocol;
3466 int lpcfg_client_ipc_max_protocol(struct loadparm_context *lp_ctx)
3468 int client_ipc_max_protocol = lpcfg__client_ipc_max_protocol(lp_ctx);
3469 if (client_ipc_max_protocol == PROTOCOL_DEFAULT) {
3470 return PROTOCOL_LATEST;
3472 if (client_ipc_max_protocol < PROTOCOL_NT1) {
3473 return PROTOCOL_NT1;
3475 return client_ipc_max_protocol;
3478 int lpcfg_client_ipc_signing(struct loadparm_context *lp_ctx)
3480 int client_ipc_signing = lpcfg__client_ipc_signing(lp_ctx);
3481 if (client_ipc_signing == SMB_SIGNING_DEFAULT) {
3482 return SMB_SIGNING_REQUIRED;
3484 return client_ipc_signing;
3487 bool lpcfg_server_signing_allowed(struct loadparm_context *lp_ctx, bool *mandatory)
3489 bool allowed = true;
3490 enum smb_signing_setting signing_setting = lpcfg_server_signing(lp_ctx);
3492 *mandatory = false;
3494 if (signing_setting == SMB_SIGNING_DEFAULT) {
3496 * If we are a domain controller, SMB signing is
3497 * really important, as it can prevent a number of
3498 * attacks on communications between us and the
3499 * clients
3501 * However, it really sucks (no sendfile, CPU
3502 * overhead) performance-wise when used on a
3503 * file server, so disable it by default
3504 * on non-DCs
3507 if (lpcfg_server_role(lp_ctx) >= ROLE_ACTIVE_DIRECTORY_DC) {
3508 signing_setting = SMB_SIGNING_REQUIRED;
3509 } else {
3510 signing_setting = SMB_SIGNING_OFF;
3514 switch (signing_setting) {
3515 case SMB_SIGNING_REQUIRED:
3516 *mandatory = true;
3517 break;
3518 case SMB_SIGNING_DESIRED:
3519 case SMB_SIGNING_IF_REQUIRED:
3520 break;
3521 case SMB_SIGNING_OFF:
3522 allowed = false;
3523 break;
3524 case SMB_SIGNING_DEFAULT:
3525 case SMB_SIGNING_IPC_DEFAULT:
3526 smb_panic(__location__);
3527 break;
3530 return allowed;
3533 int lpcfg_tdb_hash_size(struct loadparm_context *lp_ctx, const char *name)
3535 const char *base;
3537 if (name == NULL) {
3538 return 0;
3541 base = strrchr_m(name, '/');
3542 if (base != NULL) {
3543 base += 1;
3544 } else {
3545 base = name;
3547 return lpcfg_parm_int(lp_ctx, NULL, "tdb_hashsize", base, 0);
3551 int lpcfg_tdb_flags(struct loadparm_context *lp_ctx, int tdb_flags)
3553 if (!lpcfg_use_mmap(lp_ctx)) {
3554 tdb_flags |= TDB_NOMMAP;
3556 return tdb_flags;
3560 * Do not allow LanMan auth if unless NTLMv1 is also allowed
3562 * This also ensures it is disabled if NTLM is totally disabled
3564 bool lpcfg_lanman_auth(struct loadparm_context *lp_ctx)
3566 enum ntlm_auth_level ntlm_auth_level = lpcfg_ntlm_auth(lp_ctx);
3568 if (ntlm_auth_level == NTLM_AUTH_ON) {
3569 return lpcfg__lanman_auth(lp_ctx);
3570 } else {
3571 return false;