CVE-2022-38023 docs-xml/smbdotconf: change 'reject md5 servers' default to yes
[samba.git] / lib / param / loadparm.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Parameter loading functions
4    Copyright (C) Karl Auer 1993-1998
5
6    Largely re-written by Andrew Tridgell, September 1994
7
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
15
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.
20
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.
25
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/>.
28 */
29
30 /*
31  *  Load parameters.
32  *
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.
36  *
37  * To add a parameter:
38  *
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
44  *
45  *
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!
53  *
54  */
55
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 #define LOADPARM_SUBSTITUTION_INTERNALS 1
65 #include "lib/param/loadparm.h"
66 #include "auth/gensec/gensec.h"
67 #include "lib/param/s3_param.h"
68 #include "lib/util/bitmap.h"
69 #include "libcli/smb/smb_constants.h"
70 #include "tdb.h"
71 #include "librpc/gen_ndr/nbt.h"
72 #include "librpc/gen_ndr/dns.h"
73 #include "libds/common/roles.h"
74 #include "lib/util/samba_util.h"
75 #include "libcli/auth/ntlm_check.h"
76 #include "lib/crypto/gnutls_helpers.h"
77 #include "lib/util/smb_strtox.h"
78 #include "auth/credentials/credentials.h"
79
80 #ifdef HAVE_HTTPCONNECTENCRYPT
81 #include <cups/http.h>
82 #endif
83
84 #define standard_sub_basic talloc_strdup
85
86 #include "lib/param/param_global.h"
87
88 struct loadparm_service *lpcfg_default_service(struct loadparm_context *lp_ctx)
89 {
90         return lp_ctx->sDefault;
91 }
92
93 int lpcfg_rpc_low_port(struct loadparm_context *lp_ctx)
94 {
95         return lp_ctx->globals->rpc_low_port;
96 }
97
98 int lpcfg_rpc_high_port(struct loadparm_context *lp_ctx)
99 {
100         return lp_ctx->globals->rpc_high_port;
101 }
102
103 enum samba_weak_crypto lpcfg_weak_crypto(struct loadparm_context *lp_ctx)
104 {
105         if (lp_ctx->globals->weak_crypto == SAMBA_WEAK_CRYPTO_UNKNOWN) {
106                 lp_ctx->globals->weak_crypto = SAMBA_WEAK_CRYPTO_DISALLOWED;
107
108                 if (samba_gnutls_weak_crypto_allowed()) {
109                         lp_ctx->globals->weak_crypto = SAMBA_WEAK_CRYPTO_ALLOWED;
110                 }
111         }
112
113         return lp_ctx->globals->weak_crypto;
114 }
115
116 /**
117  * Convenience routine to grab string parameters into temporary memory
118  * and run standard_sub_basic on them.
119  *
120  * The buffers can be written to by
121  * callers without affecting the source string.
122  */
123
124 static const char *lpcfg_string(const char *s)
125 {
126 #if 0  /* until REWRITE done to make thread-safe */
127         size_t len = s ? strlen(s) : 0;
128         char *ret;
129 #endif
130
131         /* The follow debug is useful for tracking down memory problems
132            especially if you have an inner loop that is calling a lp_*()
133            function that returns a string.  Perhaps this debug should be
134            present all the time? */
135
136 #if 0
137         DEBUG(10, ("lpcfg_string(%s)\n", s));
138 #endif
139
140 #if 0  /* until REWRITE done to make thread-safe */
141         if (!lp_talloc)
142                 lp_talloc = talloc_init("lp_talloc");
143
144         ret = talloc_array(lp_talloc, char, len + 100); /* leave room for substitution */
145
146         if (!ret)
147                 return NULL;
148
149         if (!s)
150                 *ret = 0;
151         else
152                 strlcpy(ret, s, len);
153
154         if (trim_string(ret, "\"", "\"")) {
155                 if (strchr(ret,'"') != NULL)
156                         strlcpy(ret, s, len);
157         }
158
159         standard_sub_basic(ret,len+100);
160         return (ret);
161 #endif
162         return s;
163 }
164
165 /*
166    In this section all the functions that are used to access the
167    parameters from the rest of the program are defined
168 */
169
170 /*
171  * the creation of separate lpcfg_*() and lp_*() functions is to allow
172  * for code compatibility between existing Samba4 and Samba3 code.
173  */
174
175 /* this global context supports the lp_*() function varients */
176 static struct loadparm_context *global_loadparm_context;
177
178 #define FN_GLOBAL_SUBSTITUTED_STRING(fn_name,var_name) \
179  _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx, \
180                  const struct loadparm_substitution *lp_sub, TALLOC_CTX *mem_ctx) \
181 { \
182          if (lp_ctx == NULL) return NULL;                               \
183          return lpcfg_substituted_string(mem_ctx, lp_sub, \
184                          lp_ctx->globals->var_name ? lp_ctx->globals->var_name : ""); \
185 }
186
187 #define FN_GLOBAL_CONST_STRING(fn_name,var_name)                                \
188  _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
189         if (lp_ctx == NULL) return NULL;                                \
190         return lp_ctx->globals->var_name ? lpcfg_string(lp_ctx->globals->var_name) : ""; \
191 }
192
193 #define FN_GLOBAL_LIST(fn_name,var_name)                                \
194  _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
195          if (lp_ctx == NULL) return NULL;                               \
196          return lp_ctx->globals->var_name;                              \
197  }
198
199 #define FN_GLOBAL_BOOL(fn_name,var_name) \
200  _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) {\
201          if (lp_ctx == NULL) return false;                              \
202          return lp_ctx->globals->var_name;                              \
203 }
204
205 #define FN_GLOBAL_INTEGER(fn_name,var_name) \
206  _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
207          return lp_ctx->globals->var_name;                              \
208  }
209
210 /* Local parameters don't need the ->s3_fns because the struct
211  * loadparm_service is shared and lpcfg_service() checks the ->s3_fns
212  * hook */
213 #define FN_LOCAL_SUBSTITUTED_STRING(fn_name,val) \
214  _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_service *service, \
215                                         struct loadparm_service *sDefault, TALLOC_CTX *ctx) { \
216          return(talloc_strdup(ctx, lpcfg_string((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)))); \
217  }
218
219 #define FN_LOCAL_CONST_STRING(fn_name,val) \
220  _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_service *service, \
221                                         struct loadparm_service *sDefault) { \
222          return((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)); \
223  }
224
225 #define FN_LOCAL_LIST(fn_name,val) \
226  _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_service *service, \
227                                          struct loadparm_service *sDefault) {\
228          return(const char **)(service != NULL && service->val != NULL? service->val : sDefault->val); \
229  }
230
231 #define FN_LOCAL_PARM_BOOL(fn_name, val) FN_LOCAL_BOOL(fn_name, val)
232
233 #define FN_LOCAL_BOOL(fn_name,val) \
234  _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_service *service, \
235                                  struct loadparm_service *sDefault) {   \
236          return((service != NULL)? service->val : sDefault->val); \
237  }
238
239 #define FN_LOCAL_INTEGER(fn_name,val) \
240  _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_service *service, \
241                                 struct loadparm_service *sDefault) {    \
242          return((service != NULL)? service->val : sDefault->val); \
243  }
244
245 #define FN_LOCAL_PARM_INTEGER(fn_name, val) FN_LOCAL_INTEGER(fn_name, val)
246
247 #define FN_LOCAL_CHAR(fn_name,val) \
248  _PUBLIC_ char lpcfg_ ## fn_name(struct loadparm_service *service, \
249                                 struct loadparm_service *sDefault) {    \
250          return((service != NULL)? service->val : sDefault->val); \
251  }
252
253 #define FN_LOCAL_PARM_CHAR(fn_name,val) FN_LOCAL_CHAR(fn_name, val)
254
255 #include "lib/param/param_functions.c"
256
257 /* These functions cannot be auto-generated */
258 FN_LOCAL_BOOL(autoloaded, autoloaded)
259 FN_GLOBAL_CONST_STRING(dnsdomain, dnsdomain)
260
261 /* local prototypes */
262 static struct loadparm_service *lpcfg_getservicebyname(struct loadparm_context *lp_ctx,
263                                         const char *pszServiceName);
264 static bool do_section(const char *pszSectionName, void *);
265 static bool set_variable_helper(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
266                                 const char *pszParmName, const char *pszParmValue);
267 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
268                                        struct loadparm_service *service,
269                                        const char *pszParmName,
270                                        const char *pszParmValue, int flags);
271
272 /* The following are helper functions for parametrical options support. */
273 /* It returns a pointer to parametrical option value if it exists or NULL otherwise */
274 /* Actual parametrical functions are quite simple */
275 struct parmlist_entry *get_parametric_helper(struct loadparm_service *service,
276                                              const char *type, const char *option,
277                                              struct parmlist_entry *global_opts)
278 {
279         size_t type_len = strlen(type);
280         size_t option_len = strlen(option);
281         char param_key[type_len + option_len + 2];
282         struct parmlist_entry *data = NULL;
283
284         snprintf(param_key, sizeof(param_key), "%s:%s", type, option);
285
286         /*
287          * Try to fetch the option from the data.
288          */
289         if (service != NULL) {
290                 data = service->param_opt;
291                 while (data != NULL) {
292                         if (strwicmp(data->key, param_key) == 0) {
293                                 return data;
294                         }
295                         data = data->next;
296                 }
297         }
298
299         /*
300          * Fall back to fetching from the globals.
301          */
302         data = global_opts;
303         while (data != NULL) {
304                 if (strwicmp(data->key, param_key) == 0) {
305                         return data;
306                 }
307                 data = data->next;
308         }
309
310         return NULL;
311 }
312
313 const char *lpcfg_get_parametric(struct loadparm_context *lp_ctx,
314                               struct loadparm_service *service,
315                               const char *type, const char *option)
316 {
317         struct parmlist_entry *data;
318
319         if (lp_ctx == NULL)
320                 return NULL;
321
322         data = get_parametric_helper(service,
323                                      type, option, lp_ctx->globals->param_opt);
324
325         if (data == NULL) {
326                 return NULL;
327         } else {
328                 return data->value;
329         }
330 }
331
332
333 /**
334  * convenience routine to return int parameters.
335  */
336 int lp_int(const char *s)
337 {
338
339         if (!s || !*s) {
340                 DEBUG(0,("lp_int(%s): is called with NULL!\n",s));
341                 return -1;
342         }
343
344         return strtol(s, NULL, 0);
345 }
346
347 /**
348  * convenience routine to return unsigned long parameters.
349  */
350 unsigned long lp_ulong(const char *s)
351 {
352         int error = 0;
353         unsigned long int ret;
354
355         if (!s || !*s) {
356                 DBG_DEBUG("lp_ulong(%s): is called with NULL!\n",s);
357                 return -1;
358         }
359
360         ret = smb_strtoul(s, NULL, 0, &error, SMB_STR_STANDARD);
361         if (error != 0) {
362                 DBG_DEBUG("lp_ulong(%s): conversion failed\n",s);
363                 return -1;
364         }
365
366         return ret;
367 }
368
369 /**
370  * convenience routine to return unsigned long long parameters.
371  */
372 unsigned long long lp_ulonglong(const char *s)
373 {
374         int error = 0;
375         unsigned long long int ret;
376
377         if (!s || !*s) {
378                 DBG_DEBUG("lp_ulonglong(%s): is called with NULL!\n", s);
379                 return -1;
380         }
381
382         ret = smb_strtoull(s, NULL, 0, &error, SMB_STR_STANDARD);
383         if (error != 0) {
384                 DBG_DEBUG("lp_ulonglong(%s): conversion failed\n",s);
385                 return -1;
386         }
387
388         return ret;
389 }
390
391 /**
392  * convenience routine to return unsigned long parameters.
393  */
394 static long lp_long(const char *s)
395 {
396
397         if (!s) {
398                 DEBUG(0,("lp_long(%s): is called with NULL!\n",s));
399                 return -1;
400         }
401
402         return strtol(s, NULL, 0);
403 }
404
405 /**
406  * convenience routine to return unsigned long parameters.
407  */
408 static double lp_double(const char *s)
409 {
410
411         if (!s) {
412                 DEBUG(0,("lp_double(%s): is called with NULL!\n",s));
413                 return -1;
414         }
415
416         return strtod(s, NULL);
417 }
418
419 /**
420  * convenience routine to return boolean parameters.
421  */
422 bool lp_bool(const char *s)
423 {
424         bool ret = false;
425
426         if (!s || !*s) {
427                 DEBUG(0,("lp_bool(%s): is called with NULL!\n",s));
428                 return false;
429         }
430
431         if (!set_boolean(s, &ret)) {
432                 DEBUG(0,("lp_bool(%s): value is not boolean!\n",s));
433                 return false;
434         }
435
436         return ret;
437 }
438
439 /**
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
443  */
444
445 const char *lpcfg_parm_string(struct loadparm_context *lp_ctx,
446                               struct loadparm_service *service, const char *type,
447                               const char *option)
448 {
449         const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
450
451         if (value)
452                 return lpcfg_string(value);
453
454         return NULL;
455 }
456
457 /**
458  * Return parametric option from a given service. Type is a part of option before ':'
459  * Parametric option has following syntax: 'Type: option = value'
460  * Returned value is allocated in 'lp_talloc' context
461  */
462
463 const char **lpcfg_parm_string_list(TALLOC_CTX *mem_ctx,
464                                     struct loadparm_context *lp_ctx,
465                                     struct loadparm_service *service,
466                                     const char *type,
467                                     const char *option, const char *separator)
468 {
469         const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
470
471         if (value != NULL) {
472                 char **l = str_list_make(mem_ctx, value, separator);
473                 return discard_const_p(const char *, l);
474         }
475
476         return NULL;
477 }
478
479 /**
480  * Return parametric option from a given service. Type is a part of option before ':'
481  * Parametric option has following syntax: 'Type: option = value'
482  */
483
484 int lpcfg_parm_int(struct loadparm_context *lp_ctx,
485                    struct loadparm_service *service, const char *type,
486                    const char *option, int default_v)
487 {
488         const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
489
490         if (value)
491                 return lp_int(value);
492
493         return default_v;
494 }
495
496 /**
497  * Return parametric option from a given service. Type is a part of
498  * option before ':'.
499  * Parametric option has following syntax: 'Type: option = value'.
500  */
501
502 int lpcfg_parm_bytes(struct loadparm_context *lp_ctx,
503                   struct loadparm_service *service, const char *type,
504                   const char *option, int default_v)
505 {
506         uint64_t bval;
507
508         const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
509
510         if (value && conv_str_size_error(value, &bval)) {
511                 if (bval <= INT_MAX) {
512                         return (int)bval;
513                 }
514         }
515
516         return default_v;
517 }
518
519 /**
520  * Return parametric option from a given service.
521  * Type is a part of option before ':'
522  * Parametric option has following syntax: 'Type: option = value'
523  */
524 unsigned long lpcfg_parm_ulong(struct loadparm_context *lp_ctx,
525                             struct loadparm_service *service, const char *type,
526                             const char *option, unsigned long default_v)
527 {
528         const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
529
530         if (value)
531                 return lp_ulong(value);
532
533         return default_v;
534 }
535
536 /**
537  * Return parametric option from a given service.
538  * Type is a part of option before ':'
539  * Parametric option has following syntax: 'Type: option = value'
540  */
541 unsigned long long lpcfg_parm_ulonglong(struct loadparm_context *lp_ctx,
542                                         struct loadparm_service *service,
543                                         const char *type, const char *option,
544                                         unsigned long long default_v)
545 {
546         const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
547
548         if (value) {
549                 return lp_ulonglong(value);
550         }
551
552         return default_v;
553 }
554
555 long lpcfg_parm_long(struct loadparm_context *lp_ctx,
556                      struct loadparm_service *service, const char *type,
557                      const char *option, long default_v)
558 {
559         const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
560
561         if (value)
562                 return lp_long(value);
563
564         return default_v;
565 }
566
567 double lpcfg_parm_double(struct loadparm_context *lp_ctx,
568                       struct loadparm_service *service, const char *type,
569                       const char *option, double default_v)
570 {
571         const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
572
573         if (value != NULL)
574                 return lp_double(value);
575
576         return default_v;
577 }
578
579 /**
580  * Return parametric option from a given service. Type is a part of option before ':'
581  * Parametric option has following syntax: 'Type: option = value'
582  */
583
584 bool lpcfg_parm_bool(struct loadparm_context *lp_ctx,
585                      struct loadparm_service *service, const char *type,
586                      const char *option, bool default_v)
587 {
588         const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
589
590         if (value != NULL)
591                 return lp_bool(value);
592
593         return default_v;
594 }
595
596
597 /* this is used to prevent lots of mallocs of size 1 */
598 static const char lpcfg_string_empty[] = "";
599
600 /**
601  Free a string value.
602 **/
603 void lpcfg_string_free(char **s)
604 {
605         if (s == NULL) {
606                 return;
607         }
608         if (*s == lpcfg_string_empty) {
609                 *s = NULL;
610                 return;
611         }
612         TALLOC_FREE(*s);
613 }
614
615 /**
616  * Set a string value, deallocating any existing space, and allocing the space
617  * for the string
618  */
619 bool lpcfg_string_set(TALLOC_CTX *mem_ctx, char **dest, const char *src)
620 {
621         lpcfg_string_free(dest);
622
623         if ((src == NULL) || (*src == '\0')) {
624                 *dest = discard_const_p(char, lpcfg_string_empty);
625                 return true;
626         }
627
628         *dest = talloc_strdup(mem_ctx, src);
629         if ((*dest) == NULL) {
630                 DEBUG(0,("Out of memory in string_set\n"));
631                 return false;
632         }
633
634         return true;
635 }
636
637 /**
638  * Set a string value, deallocating any existing space, and allocing the space
639  * for the string
640  */
641 bool lpcfg_string_set_upper(TALLOC_CTX *mem_ctx, char **dest, const char *src)
642 {
643         lpcfg_string_free(dest);
644
645         if ((src == NULL) || (*src == '\0')) {
646                 *dest = discard_const_p(char, lpcfg_string_empty);
647                 return true;
648         }
649
650         *dest = strupper_talloc(mem_ctx, src);
651         if ((*dest) == NULL) {
652                 DEBUG(0,("Out of memory in string_set_upper\n"));
653                 return false;
654         }
655
656         return true;
657 }
658
659
660
661 /**
662  * Add a new service to the services array initialising it with the given
663  * service.
664  */
665
666 struct loadparm_service *lpcfg_add_service(struct loadparm_context *lp_ctx,
667                                            const struct loadparm_service *pservice,
668                                            const char *name)
669 {
670         int i;
671         int num_to_alloc = lp_ctx->iNumServices + 1;
672         struct parmlist_entry *data, *pdata;
673
674         if (lp_ctx->s3_fns != NULL) {
675                 smb_panic("Add a service should not be called on an s3 loadparm ctx");
676         }
677
678         if (pservice == NULL) {
679                 pservice = lp_ctx->sDefault;
680         }
681
682         /* it might already exist */
683         if (name) {
684                 struct loadparm_service *service = lpcfg_getservicebyname(lp_ctx,
685                                                                     name);
686                 if (service != NULL) {
687                         /* Clean all parametric options for service */
688                         /* They will be added during parsing again */
689                         data = service->param_opt;
690                         while (data) {
691                                 pdata = data->next;
692                                 talloc_free(data);
693                                 data = pdata;
694                         }
695                         service->param_opt = NULL;
696                         return service;
697                 }
698         }
699
700         /* find an invalid one */
701         for (i = 0; i < lp_ctx->iNumServices; i++)
702                 if (lp_ctx->services[i] == NULL)
703                         break;
704
705         /* if not, then create one */
706         if (i == lp_ctx->iNumServices) {
707                 struct loadparm_service **tsp;
708
709                 tsp = talloc_realloc(lp_ctx, lp_ctx->services, struct loadparm_service *, num_to_alloc);
710
711                 if (!tsp) {
712                         DEBUG(0,("lpcfg_add_service: failed to enlarge services!\n"));
713                         return NULL;
714                 } else {
715                         lp_ctx->services = tsp;
716                         lp_ctx->services[lp_ctx->iNumServices] = NULL;
717                 }
718
719                 lp_ctx->iNumServices++;
720         }
721
722         lp_ctx->services[i] = talloc_zero(lp_ctx->services, struct loadparm_service);
723         if (lp_ctx->services[i] == NULL) {
724                 DEBUG(0,("lpcfg_add_service: out of memory!\n"));
725                 return NULL;
726         }
727         copy_service(lp_ctx->services[i], pservice, NULL);
728         if (name != NULL)
729                 lpcfg_string_set(lp_ctx->services[i], &lp_ctx->services[i]->szService, name);
730         return lp_ctx->services[i];
731 }
732
733 /**
734  * Map a parameter's string representation to something we can use.
735  * Returns False if the parameter string is not recognised, else TRUE.
736  */
737
738 int lpcfg_map_parameter(const char *pszParmName)
739 {
740         int iIndex;
741
742         for (iIndex = 0; parm_table[iIndex].label; iIndex++)
743                 if (strwicmp(parm_table[iIndex].label, pszParmName) == 0)
744                         return iIndex;
745
746         /* Warn only if it isn't parametric option */
747         if (strchr(pszParmName, ':') == NULL)
748                 DEBUG(0, ("Unknown parameter encountered: \"%s\"\n", pszParmName));
749         /* We do return 'fail' for parametric options as well because they are
750            stored in different storage
751          */
752         return -1;
753 }
754
755
756 /**
757   return the parameter structure for a parameter
758 */
759 struct parm_struct *lpcfg_parm_struct(struct loadparm_context *lp_ctx, const char *name)
760 {
761         int num = lpcfg_map_parameter(name);
762
763         if (num < 0) {
764                 return NULL;
765         }
766
767         return &parm_table[num];
768 }
769
770 /**
771   return the parameter pointer for a parameter
772 */
773 void *lpcfg_parm_ptr(struct loadparm_context *lp_ctx,
774                   struct loadparm_service *service, struct parm_struct *parm)
775 {
776         if (lp_ctx->s3_fns) {
777                 return lp_ctx->s3_fns->get_parm_ptr(service, parm);
778         }
779
780         if (service == NULL) {
781                 if (parm->p_class == P_LOCAL)
782                         return ((char *)lp_ctx->sDefault)+parm->offset;
783                 else if (parm->p_class == P_GLOBAL)
784                         return ((char *)lp_ctx->globals)+parm->offset;
785                 else return NULL;
786         } else {
787                 return ((char *)service) + parm->offset;
788         }
789 }
790
791 /**
792   return the parameter pointer for a parameter
793 */
794 bool lpcfg_parm_is_cmdline(struct loadparm_context *lp_ctx, const char *name)
795 {
796         int parmnum;
797
798         parmnum = lpcfg_map_parameter(name);
799         if (parmnum == -1) return false;
800
801         return lp_ctx->flags[parmnum] & FLAG_CMDLINE;
802 }
803
804 bool lpcfg_parm_is_unspecified(struct loadparm_context *lp_ctx, const char *name)
805 {
806         int parmnum;
807
808         parmnum = lpcfg_map_parameter(name);
809         if (parmnum == -1) return false;
810
811         return lp_ctx->flags[parmnum] & FLAG_DEFAULT;
812 }
813
814 /**
815  * Find a service by name. Otherwise works like get_service.
816  */
817
818 static struct loadparm_service *lpcfg_getservicebyname(struct loadparm_context *lp_ctx,
819                                         const char *pszServiceName)
820 {
821         int iService;
822
823         if (lp_ctx->s3_fns) {
824                 return lp_ctx->s3_fns->get_service(pszServiceName);
825         }
826
827         for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--)
828                 if (lp_ctx->services[iService] != NULL &&
829                     strwicmp(lp_ctx->services[iService]->szService, pszServiceName) == 0) {
830                         return lp_ctx->services[iService];
831                 }
832
833         return NULL;
834 }
835
836 /**
837  * Add a parametric option to a parmlist_entry,
838  * replacing old value, if already present.
839  */
840 void set_param_opt(TALLOC_CTX *mem_ctx,
841                    struct parmlist_entry **opt_list,
842                    const char *opt_name,
843                    const char *opt_value,
844                    unsigned priority)
845 {
846         struct parmlist_entry *new_opt, *opt;
847
848         opt = *opt_list;
849
850         /* Traverse destination */
851         while (opt) {
852                 /* If we already have same option, override it */
853                 if (strwicmp(opt->key, opt_name) == 0) {
854                         if ((opt->priority & FLAG_CMDLINE) &&
855                             !(priority & FLAG_CMDLINE)) {
856                                 /* it's been marked as not to be
857                                    overridden */
858                                 return;
859                         }
860                         TALLOC_FREE(opt->list);
861                         lpcfg_string_set(opt, &opt->value, opt_value);
862                         opt->priority = priority;
863                         return;
864                 }
865                 opt = opt->next;
866         }
867
868         new_opt = talloc_pooled_object(
869                 mem_ctx, struct parmlist_entry,
870                 2, strlen(opt_name) + 1 + strlen(opt_value) + 1);
871         if (new_opt == NULL) {
872                 smb_panic("OOM");
873         }
874         new_opt->key = NULL;
875         lpcfg_string_set(new_opt, &new_opt->key, opt_name);
876         new_opt->value = NULL;
877         lpcfg_string_set(new_opt, &new_opt->value, opt_value);
878
879         new_opt->list = NULL;
880         new_opt->priority = priority;
881         DLIST_ADD(*opt_list, new_opt);
882 }
883
884 /**
885  * Copy a service structure to another.
886  * If pcopymapDest is NULL then copy all fields
887  */
888
889 void copy_service(struct loadparm_service *pserviceDest,
890                   const struct loadparm_service *pserviceSource,
891                   struct bitmap *pcopymapDest)
892 {
893         int i;
894         bool bcopyall = (pcopymapDest == NULL);
895         struct parmlist_entry *data;
896
897         for (i = 0; parm_table[i].label; i++)
898                 if (parm_table[i].p_class == P_LOCAL &&
899                     (bcopyall || bitmap_query(pcopymapDest, i))) {
900                         const void *src_ptr =
901                                 ((const char *)pserviceSource) + parm_table[i].offset;
902                         void *dest_ptr =
903                                 ((char *)pserviceDest) + parm_table[i].offset;
904
905                         switch (parm_table[i].type) {
906                                 case P_BOOL:
907                                 case P_BOOLREV:
908                                         *(bool *)dest_ptr = *(const bool *)src_ptr;
909                                         break;
910
911                                 case P_INTEGER:
912                                 case P_BYTES:
913                                 case P_OCTAL:
914                                 case P_ENUM:
915                                         *(int *)dest_ptr = *(const int *)src_ptr;
916                                         break;
917
918                                 case P_CHAR:
919                                         *(char *)dest_ptr = *(const char *)src_ptr;
920                                         break;
921
922                                 case P_STRING:
923                                         lpcfg_string_set(pserviceDest,
924                                                    (char **)dest_ptr,
925                                                    *(const char * const *)src_ptr);
926                                         break;
927
928                                 case P_USTRING:
929                                         lpcfg_string_set_upper(pserviceDest,
930                                                          (char **)dest_ptr,
931                                                          *(const char * const *)src_ptr);
932                                         break;
933                                 case P_CMDLIST:
934                                 case P_LIST:
935                                         TALLOC_FREE(*((char ***)dest_ptr));
936                                         *(char ***)dest_ptr = str_list_copy(pserviceDest,
937                                                                             *discard_const_p(const char **, src_ptr));
938                                         break;
939                                 default:
940                                         break;
941                         }
942                 }
943
944         if (bcopyall) {
945                 init_copymap(pserviceDest);
946                 if (pserviceSource->copymap)
947                         bitmap_copy(pserviceDest->copymap,
948                                     pserviceSource->copymap);
949         }
950
951         for (data = pserviceSource->param_opt; data != NULL; data = data->next) {
952                 set_param_opt(pserviceDest, &pserviceDest->param_opt,
953                               data->key, data->value, data->priority);
954         }
955 }
956
957 /**
958  * Check a service for consistency. Return False if the service is in any way
959  * incomplete or faulty, else True.
960  */
961 bool lpcfg_service_ok(struct loadparm_service *service)
962 {
963         bool bRetval;
964
965         bRetval = true;
966         if (service->szService[0] == '\0') {
967                 DEBUG(0, ("The following message indicates an internal error:\n"));
968                 DEBUG(0, ("No service name in service entry.\n"));
969                 bRetval = false;
970         }
971
972         /* The [printers] entry MUST be printable. I'm all for flexibility, but */
973         /* I can't see why you'd want a non-printable printer service...        */
974         if (strwicmp(service->szService, PRINTERS_NAME) == 0) {
975                 if (!service->printable) {
976                         DEBUG(0, ("WARNING: [%s] service MUST be printable!\n",
977                                service->szService));
978                         service->printable = true;
979                 }
980                 /* [printers] service must also be non-browsable. */
981                 if (service->browseable)
982                         service->browseable = false;
983         }
984
985         if (service->path[0] == '\0' &&
986             strwicmp(service->szService, HOMES_NAME) != 0 &&
987             service->msdfs_proxy[0] == '\0')
988         {
989                 DEBUG(0, ("WARNING: No path in service %s - making it unavailable!\n",
990                         service->szService));
991                 service->available = false;
992         }
993
994         if (!service->available)
995                 DEBUG(1, ("NOTE: Service %s is flagged unavailable.\n",
996                           service->szService));
997
998         return bRetval;
999 }
1000
1001
1002 /*******************************************************************
1003  Keep a linked list of all config files so we know when one has changed
1004  it's date and needs to be reloaded.
1005 ********************************************************************/
1006
1007 void add_to_file_list(TALLOC_CTX *mem_ctx, struct file_lists **list,
1008                              const char *fname, const char *subfname)
1009 {
1010         struct file_lists *f = *list;
1011
1012         while (f) {
1013                 if (f->name && !strcmp(f->name, fname))
1014                         break;
1015                 f = f->next;
1016         }
1017
1018         if (!f) {
1019                 f = talloc(mem_ctx, struct file_lists);
1020                 if (!f)
1021                         goto fail;
1022                 f->next = *list;
1023                 f->name = talloc_strdup(f, fname);
1024                 if (!f->name) {
1025                         TALLOC_FREE(f);
1026                         goto fail;
1027                 }
1028                 f->subfname = talloc_strdup(f, subfname);
1029                 if (!f->subfname) {
1030                         TALLOC_FREE(f);
1031                         goto fail;
1032                 }
1033                 *list = f;
1034                 f->modtime = file_modtime(subfname);
1035         } else {
1036                 time_t t = file_modtime(subfname);
1037                 if (t)
1038                         f->modtime = t;
1039         }
1040         return;
1041
1042 fail:
1043         DEBUG(0, ("Unable to add file to file list: %s\n", fname));
1044
1045 }
1046
1047 /*
1048  * set the value for a P_ENUM
1049  */
1050 bool lp_set_enum_parm( struct parm_struct *parm, const char *pszParmValue,
1051                               int *ptr )
1052 {
1053         int i;
1054
1055         for (i = 0; parm->enum_list[i].name; i++) {
1056                 if (strwicmp(pszParmValue, parm->enum_list[i].name) == 0) {
1057                         *ptr = parm->enum_list[i].value;
1058                         return true;
1059                 }
1060         }
1061         DEBUG(0, ("WARNING: Ignoring invalid value '%s' for parameter '%s'\n",
1062                   pszParmValue, parm->label));
1063         return false;
1064 }
1065
1066
1067 /***************************************************************************
1068  Handle the "realm" parameter
1069 ***************************************************************************/
1070
1071 bool handle_realm(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1072                   const char *pszParmValue, char **ptr)
1073 {
1074         char *upper;
1075         char *lower;
1076
1077         upper = strupper_talloc(lp_ctx, pszParmValue);
1078         if (upper == NULL) {
1079                 return false;
1080         }
1081
1082         lower = strlower_talloc(lp_ctx, pszParmValue);
1083         if (lower == NULL) {
1084                 TALLOC_FREE(upper);
1085                 return false;
1086         }
1087
1088         lpcfg_string_set(lp_ctx->globals->ctx, &lp_ctx->globals->realm, upper);
1089         lpcfg_string_set(lp_ctx->globals->ctx, &lp_ctx->globals->dnsdomain, lower);
1090
1091         return true;
1092 }
1093
1094 /***************************************************************************
1095  Handle the include operation.
1096 ***************************************************************************/
1097
1098 bool handle_include(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1099                            const char *pszParmValue, char **ptr)
1100 {
1101         char *fname;
1102         const char *substitution_variable_substring;
1103         char next_char;
1104
1105         if (lp_ctx->s3_fns) {
1106                 return lp_ctx->s3_fns->lp_include(lp_ctx, service, pszParmValue, ptr);
1107         }
1108
1109         fname = standard_sub_basic(lp_ctx, pszParmValue);
1110
1111         add_to_file_list(lp_ctx, &lp_ctx->file_lists, pszParmValue, fname);
1112
1113         lpcfg_string_set(lp_ctx, ptr, fname);
1114
1115         if (file_exist(fname))
1116                 return pm_process(fname, do_section, lpcfg_do_parameter, lp_ctx);
1117
1118        /*
1119         * If the file doesn't exist, we check that it isn't due to variable
1120         * substitution
1121         */
1122         substitution_variable_substring = strchr(fname, '%');
1123
1124         if (substitution_variable_substring != NULL) {
1125                 next_char = substitution_variable_substring[1];
1126                 if ((next_char >= 'a' && next_char <= 'z')
1127                     || (next_char >= 'A' && next_char <= 'Z')) {
1128                         DEBUG(2, ("Tried to load %s but variable substitution in "
1129                                  "filename, ignoring file.\n", fname));
1130                         return true;
1131                 }
1132         }
1133
1134         DEBUG(2, ("Can't find include file %s\n", fname));
1135
1136         return false;
1137 }
1138
1139 /***************************************************************************
1140  Handle the interpretation of the copy parameter.
1141 ***************************************************************************/
1142
1143 bool handle_copy(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1144                         const char *pszParmValue, char **ptr)
1145 {
1146         bool bRetval;
1147         struct loadparm_service *serviceTemp = NULL;
1148
1149         bRetval = false;
1150
1151         DEBUG(3, ("Copying service from service %s\n", pszParmValue));
1152
1153         serviceTemp = lpcfg_getservicebyname(lp_ctx, pszParmValue);
1154
1155         if (service == NULL) {
1156                 DEBUG(0, ("Unable to copy service - invalid service destination.\n"));
1157                 return false;
1158         }
1159
1160         if (serviceTemp != NULL) {
1161                 if (serviceTemp == service) {
1162                         DEBUG(0, ("Can't copy service %s - unable to copy self!\n", pszParmValue));
1163                 } else {
1164                         copy_service(service,
1165                                      serviceTemp,
1166                                      service->copymap);
1167                         lpcfg_string_set(service, ptr, pszParmValue);
1168
1169                         bRetval = true;
1170                 }
1171         } else {
1172                 DEBUG(0, ("Unable to copy service - source not found: %s\n",
1173                           pszParmValue));
1174                 bRetval = false;
1175         }
1176
1177         return bRetval;
1178 }
1179
1180 bool handle_debug_list(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1181                         const char *pszParmValue, char **ptr)
1182 {
1183         lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1184
1185         return debug_parse_levels(pszParmValue);
1186 }
1187
1188 bool handle_logfile(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1189                     const char *pszParmValue, char **ptr)
1190 {
1191         if (lp_ctx->s3_fns == NULL) {
1192                 debug_set_logfile(pszParmValue);
1193         }
1194
1195         lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1196
1197         return true;
1198 }
1199
1200 /*
1201  * These special charset handling methods only run in the source3 code.
1202  */
1203
1204 bool handle_charset(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1205                         const char *pszParmValue, char **ptr)
1206 {
1207         if (lp_ctx->s3_fns) {
1208                 if (*ptr == NULL || strcmp(*ptr, pszParmValue) != 0) {
1209                         struct smb_iconv_handle *ret = NULL;
1210
1211                         ret = reinit_iconv_handle(NULL,
1212                                                   lpcfg_dos_charset(lp_ctx),
1213                                                   lpcfg_unix_charset(lp_ctx));
1214                         if (ret == NULL) {
1215                                 smb_panic("reinit_iconv_handle failed");
1216                         }
1217                 }
1218
1219         }
1220         return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1221
1222 }
1223
1224 bool handle_dos_charset(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1225                         const char *pszParmValue, char **ptr)
1226 {
1227         bool is_utf8 = false;
1228         size_t len = strlen(pszParmValue);
1229
1230         if (lp_ctx->s3_fns) {
1231                 if (len == 4 || len == 5) {
1232                         /* Don't use StrCaseCmp here as we don't want to
1233                            initialize iconv. */
1234                         if ((toupper_m(pszParmValue[0]) == 'U') &&
1235                             (toupper_m(pszParmValue[1]) == 'T') &&
1236                             (toupper_m(pszParmValue[2]) == 'F')) {
1237                                 if (len == 4) {
1238                                         if (pszParmValue[3] == '8') {
1239                                                 is_utf8 = true;
1240                                         }
1241                                 } else {
1242                                         if (pszParmValue[3] == '-' &&
1243                                             pszParmValue[4] == '8') {
1244                                                 is_utf8 = true;
1245                                         }
1246                                 }
1247                         }
1248                 }
1249
1250                 if (*ptr == NULL || strcmp(*ptr, pszParmValue) != 0) {
1251                         struct smb_iconv_handle *ret = NULL;
1252                         if (is_utf8) {
1253                                 DEBUG(0,("ERROR: invalid DOS charset: 'dos charset' must not "
1254                                         "be UTF8, using (default value) %s instead.\n",
1255                                         DEFAULT_DOS_CHARSET));
1256                                 pszParmValue = DEFAULT_DOS_CHARSET;
1257                         }
1258                         ret = reinit_iconv_handle(NULL,
1259                                                 lpcfg_dos_charset(lp_ctx),
1260                                                 lpcfg_unix_charset(lp_ctx));
1261                         if (ret == NULL) {
1262                                 smb_panic("reinit_iconv_handle failed");
1263                         }
1264                 }
1265         }
1266
1267         return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1268 }
1269
1270 bool handle_printing(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1271                             const char *pszParmValue, char **ptr)
1272 {
1273         static int parm_num = -1;
1274
1275         if (parm_num == -1) {
1276                 parm_num = lpcfg_map_parameter("printing");
1277         }
1278
1279         if (!lp_set_enum_parm(&parm_table[parm_num], pszParmValue, (int*)ptr)) {
1280                 return false;
1281         }
1282
1283         if (lp_ctx->s3_fns) {
1284                 if (service == NULL) {
1285                         init_printer_values(lp_ctx, lp_ctx->globals->ctx, lp_ctx->sDefault);
1286                 } else {
1287                         init_printer_values(lp_ctx, service, service);
1288                 }
1289         }
1290
1291         return true;
1292 }
1293
1294 bool handle_ldap_debug_level(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1295                              const char *pszParmValue, char **ptr)
1296 {
1297         lp_ctx->globals->ldap_debug_level = lp_int(pszParmValue);
1298
1299         if (lp_ctx->s3_fns) {
1300                 lp_ctx->s3_fns->init_ldap_debugging();
1301         }
1302         return true;
1303 }
1304
1305 /*
1306  * idmap related parameters
1307  */
1308
1309 bool handle_idmap_backend(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1310                           const char *pszParmValue, char **ptr)
1311 {
1312         if (lp_ctx->s3_fns) {
1313                 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : backend",
1314                                            pszParmValue, 0);
1315         }
1316
1317         return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1318 }
1319
1320 bool handle_idmap_uid(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1321                       const char *pszParmValue, char **ptr)
1322 {
1323         if (lp_ctx->s3_fns) {
1324                 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : range",
1325                                            pszParmValue, 0);
1326         }
1327
1328         return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1329 }
1330
1331 bool handle_idmap_gid(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1332                       const char *pszParmValue, char **ptr)
1333 {
1334         if (lp_ctx->s3_fns) {
1335                 lp_do_parameter_parametric(lp_ctx, service, "idmap config * : range",
1336                                            pszParmValue, 0);
1337         }
1338
1339         return lpcfg_string_set(lp_ctx->globals->ctx, ptr, pszParmValue);
1340 }
1341
1342 bool handle_smb_ports(struct loadparm_context *lp_ctx, struct loadparm_service *service,
1343                       const char *pszParmValue, char **ptr)
1344 {
1345         static int parm_num = -1;
1346         int i;
1347         const char **list;
1348
1349         if (!pszParmValue || !*pszParmValue) {
1350                 return false;
1351         }
1352
1353         if (parm_num == -1) {
1354                 parm_num = lpcfg_map_parameter("smb ports");
1355                 if (parm_num == -1) {
1356                         return false;
1357                 }
1358         }
1359
1360         if (!set_variable_helper(lp_ctx->globals->ctx, parm_num, ptr, "smb ports",
1361                                 pszParmValue)) {
1362                 return false;
1363         }
1364
1365         list = lp_ctx->globals->smb_ports;
1366         if (list == NULL) {
1367                 return false;
1368         }
1369
1370         /* Check that each port is a valid integer and within range */
1371         for (i = 0; list[i] != NULL; i++) {
1372                 char *end = NULL;
1373                 int port = 0;
1374                 port = strtol(list[i], &end, 10);
1375                 if (*end != '\0' || port <= 0 || port > 65535) {
1376                         TALLOC_FREE(list);
1377                         return false;
1378                 }
1379         }
1380
1381         return true;
1382 }
1383
1384 bool handle_rpc_server_dynamic_port_range(struct loadparm_context *lp_ctx,
1385                                           struct loadparm_service *service,
1386                                           const char *pszParmValue,
1387                                           char **ptr)
1388 {
1389         static int parm_num = -1;
1390         int low_port = -1, high_port = -1;
1391         int rc;
1392
1393         if (parm_num == -1) {
1394                 parm_num = lpcfg_map_parameter("rpc server dynamic port range");
1395                 if (parm_num == -1) {
1396                         return false;
1397                 }
1398         }
1399
1400         if (pszParmValue == NULL || pszParmValue[0] == '\0') {
1401                 return false;
1402         }
1403
1404         rc = sscanf(pszParmValue, "%d - %d", &low_port, &high_port);
1405         if (rc != 2) {
1406                 return false;
1407         }
1408
1409         if (low_port > high_port) {
1410                 return false;
1411         }
1412
1413         if (low_port < SERVER_TCP_PORT_MIN|| high_port > SERVER_TCP_PORT_MAX) {
1414                 return false;
1415         }
1416
1417         if (!set_variable_helper(lp_ctx->globals->ctx, parm_num, ptr,
1418                                  "rpc server dynamic port range",
1419                                  pszParmValue)) {
1420                 return false;
1421         }
1422
1423         lp_ctx->globals->rpc_low_port = low_port;
1424         lp_ctx->globals->rpc_high_port = high_port;
1425
1426         return true;
1427 }
1428
1429 bool handle_smb2_max_credits(struct loadparm_context *lp_ctx,
1430                              struct loadparm_service *service,
1431                              const char *pszParmValue, char **ptr)
1432 {
1433         int value = lp_int(pszParmValue);
1434
1435         if (value <= 0) {
1436                 value = DEFAULT_SMB2_MAX_CREDITS;
1437         }
1438
1439         *(int *)ptr = value;
1440
1441         return true;
1442 }
1443
1444 bool handle_cups_encrypt(struct loadparm_context *lp_ctx,
1445                          struct loadparm_service *service,
1446                          const char *pszParmValue, char **ptr)
1447 {
1448         int result = 0;
1449 #ifdef HAVE_HTTPCONNECTENCRYPT
1450         int value = lp_int(pszParmValue);
1451
1452         switch (value) {
1453                 case Auto:
1454                         result = HTTP_ENCRYPT_REQUIRED;
1455                         break;
1456                 case true:
1457                         result = HTTP_ENCRYPT_ALWAYS;
1458                         break;
1459                 case false:
1460                         result = HTTP_ENCRYPT_NEVER;
1461                         break;
1462                 default:
1463                         result = 0;
1464                         break;
1465         }
1466 #endif
1467         *(int *)ptr = result;
1468
1469         return true;
1470 }
1471
1472 /***************************************************************************
1473  Initialise a copymap.
1474 ***************************************************************************/
1475
1476 /**
1477  * Initializes service copymap
1478  * Note: pservice *must* be valid TALLOC_CTX
1479  */
1480 void init_copymap(struct loadparm_service *pservice)
1481 {
1482         int i;
1483
1484         TALLOC_FREE(pservice->copymap);
1485
1486         pservice->copymap = bitmap_talloc(pservice, num_parameters());
1487         if (!pservice->copymap) {
1488                 DEBUG(0,
1489                       ("Couldn't allocate copymap!! (size %d)\n",
1490                        (int)num_parameters()));
1491         } else {
1492                 for (i = 0; i < num_parameters(); i++) {
1493                         bitmap_set(pservice->copymap, i);
1494                 }
1495         }
1496 }
1497
1498 /**
1499  * Process a parametric option
1500  */
1501 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
1502                                        struct loadparm_service *service,
1503                                        const char *pszParmName,
1504                                        const char *pszParmValue, int flags)
1505 {
1506         struct parmlist_entry **data;
1507         char *name;
1508         TALLOC_CTX *mem_ctx;
1509
1510         while (isspace((unsigned char)*pszParmName)) {
1511                 pszParmName++;
1512         }
1513
1514         name = strlower_talloc(lp_ctx, pszParmName);
1515         if (!name) return false;
1516
1517         if (service == NULL) {
1518                 data = &lp_ctx->globals->param_opt;
1519                 /**
1520                  * s3 code cannot deal with parametric options stored on the globals ctx.
1521                  */
1522                 if (lp_ctx->s3_fns != NULL) {
1523                         mem_ctx = NULL;
1524                 } else {
1525                         mem_ctx = lp_ctx->globals->ctx;
1526                 }
1527         } else {
1528                 data = &service->param_opt;
1529                 mem_ctx = service;
1530         }
1531
1532         set_param_opt(mem_ctx, data, name, pszParmValue, flags);
1533
1534         talloc_free(name);
1535
1536         return true;
1537 }
1538
1539 static bool set_variable_helper(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
1540                          const char *pszParmName, const char *pszParmValue)
1541 {
1542         size_t i;
1543
1544         /* switch on the type of variable it is */
1545         switch (parm_table[parmnum].type)
1546         {
1547                 case P_BOOL: {
1548                         bool b;
1549                         if (!set_boolean(pszParmValue, &b)) {
1550                                 DEBUG(0, ("set_variable_helper(%s): value is not "
1551                                           "boolean!\n", pszParmValue));
1552                                 return false;
1553                         }
1554                         *(bool *)parm_ptr = b;
1555                         }
1556                         break;
1557
1558                 case P_BOOLREV: {
1559                         bool b;
1560                         if (!set_boolean(pszParmValue, &b)) {
1561                                 DEBUG(0, ("set_variable_helper(%s): value is not "
1562                                           "boolean!\n", pszParmValue));
1563                                 return false;
1564                         }
1565                         *(bool *)parm_ptr = !b;
1566                         }
1567                         break;
1568
1569                 case P_INTEGER:
1570                         *(int *)parm_ptr = lp_int(pszParmValue);
1571                         break;
1572
1573                 case P_CHAR:
1574                         *(char *)parm_ptr = *pszParmValue;
1575                         break;
1576
1577                 case P_OCTAL:
1578                         i = sscanf(pszParmValue, "%o", (int *)parm_ptr);
1579                         if ( i != 1 ) {
1580                                 DEBUG ( 0, ("Invalid octal number %s\n", pszParmName ));
1581                                 return false;
1582                         }
1583                         break;
1584
1585                 case P_BYTES:
1586                 {
1587                         uint64_t val;
1588                         if (conv_str_size_error(pszParmValue, &val)) {
1589                                 if (val <= INT_MAX) {
1590                                         *(int *)parm_ptr = (int)val;
1591                                         break;
1592                                 }
1593                         }
1594
1595                         DEBUG(0, ("set_variable_helper(%s): value is not "
1596                                   "a valid size specifier!\n", pszParmValue));
1597                         return false;
1598                 }
1599
1600                 case P_CMDLIST:
1601                         TALLOC_FREE(*(char ***)parm_ptr);
1602                         *(char ***)parm_ptr = str_list_make_v3(mem_ctx,
1603                                                         pszParmValue, NULL);
1604                         break;
1605
1606                 case P_LIST:
1607                 {
1608                         char **new_list = str_list_make_v3(mem_ctx,
1609                                                         pszParmValue, NULL);
1610                         if (new_list == NULL) {
1611                                 break;
1612                         }
1613
1614                         for (i=0; new_list[i]; i++) {
1615                                 if (*(const char ***)parm_ptr != NULL &&
1616                                     new_list[i][0] == '+' &&
1617                                     new_list[i][1])
1618                                 {
1619                                         if (!str_list_check(*(const char ***)parm_ptr,
1620                                                             &new_list[i][1])) {
1621                                                 *(const char ***)parm_ptr = str_list_add(*(const char ***)parm_ptr,
1622                                                                                          &new_list[i][1]);
1623                                         }
1624                                 } else if (*(const char ***)parm_ptr != NULL &&
1625                                            new_list[i][0] == '-' &&
1626                                            new_list[i][1])
1627                                 {
1628                                         str_list_remove(*(const char ***)parm_ptr,
1629                                                         &new_list[i][1]);
1630                                 } else {
1631                                         if (i != 0) {
1632                                                 DEBUG(0, ("Unsupported list syntax for: %s = %s\n",
1633                                                           pszParmName, pszParmValue));
1634                                                 return false;
1635                                         }
1636                                         *(char ***)parm_ptr = new_list;
1637                                         break;
1638                                 }
1639                         }
1640                         break;
1641                 }
1642
1643                 case P_STRING:
1644                         lpcfg_string_set(mem_ctx, (char **)parm_ptr, pszParmValue);
1645                         break;
1646
1647                 case P_USTRING:
1648                         lpcfg_string_set_upper(mem_ctx, (char **)parm_ptr, pszParmValue);
1649                         break;
1650
1651                 case P_ENUM:
1652                         if (!lp_set_enum_parm(&parm_table[parmnum], pszParmValue, (int*)parm_ptr)) {
1653                                 return false;
1654                         }
1655                         break;
1656
1657         }
1658
1659         return true;
1660
1661 }
1662
1663 bool handle_name_resolve_order(struct loadparm_context *lp_ctx,
1664                                struct loadparm_service *service,
1665                                const char *pszParmValue, char **ptr)
1666 {
1667         const char **valid_values = NULL;
1668         const char **values_to_set = NULL;
1669         int i;
1670         bool value_is_valid = false;
1671         valid_values = str_list_make_v3_const(NULL,
1672                                               DEFAULT_NAME_RESOLVE_ORDER,
1673                                               NULL);
1674         if (valid_values == NULL) {
1675                 DBG_ERR("OOM: failed to make string list from %s\n",
1676                         DEFAULT_NAME_RESOLVE_ORDER);
1677                 goto out;
1678         }
1679         values_to_set = str_list_make_v3_const(lp_ctx->globals->ctx,
1680                                                pszParmValue,
1681                                                NULL);
1682         if (values_to_set == NULL) {
1683                 DBG_ERR("OOM: failed to make string list from %s\n",
1684                         pszParmValue);
1685                 goto out;
1686         }
1687         TALLOC_FREE(lp_ctx->globals->name_resolve_order);
1688         for (i = 0; values_to_set[i] != NULL; i++) {
1689                 value_is_valid = str_list_check(valid_values, values_to_set[i]);
1690                 if (!value_is_valid) {
1691                         DBG_ERR("WARNING: Ignoring invalid list value '%s' "
1692                                 "for parameter 'name resolve order'\n",
1693                                 values_to_set[i]);
1694                         break;
1695                 }
1696         }
1697 out:
1698         if (value_is_valid) {
1699                 lp_ctx->globals->name_resolve_order = values_to_set;
1700         } else {
1701                 TALLOC_FREE(values_to_set);
1702         }
1703         TALLOC_FREE(valid_values);
1704         return value_is_valid;
1705 }
1706
1707 static bool set_variable(TALLOC_CTX *mem_ctx, struct loadparm_service *service,
1708                          int parmnum, void *parm_ptr,
1709                          const char *pszParmName, const char *pszParmValue,
1710                          struct loadparm_context *lp_ctx, bool on_globals)
1711 {
1712         int i;
1713         bool ok;
1714
1715         /* if it is a special case then go ahead */
1716         if (parm_table[parmnum].special) {
1717                 ok = parm_table[parmnum].special(lp_ctx, service, pszParmValue,
1718                                                   (char **)parm_ptr);
1719         } else {
1720                 ok = set_variable_helper(mem_ctx, parmnum, parm_ptr,
1721                                          pszParmName, pszParmValue);
1722         }
1723
1724         if (!ok) {
1725                 return false;
1726         }
1727
1728         if (on_globals && (lp_ctx->flags[parmnum] & FLAG_DEFAULT)) {
1729                 lp_ctx->flags[parmnum] &= ~FLAG_DEFAULT;
1730                 /* we have to also unset FLAG_DEFAULT on aliases */
1731                 for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
1732                         lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1733                 }
1734                 for (i=parmnum+1;i<num_parameters() && parm_table[i].offset == parm_table[parmnum].offset;i++) {
1735                         lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1736                 }
1737         }
1738         return true;
1739 }
1740
1741
1742 bool lpcfg_do_global_parameter(struct loadparm_context *lp_ctx,
1743                                const char *pszParmName, const char *pszParmValue)
1744 {
1745         int parmnum = lpcfg_map_parameter(pszParmName);
1746         void *parm_ptr;
1747
1748         if (parmnum < 0) {
1749                 if (strchr(pszParmName, ':')) {
1750                         return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName, pszParmValue, 0);
1751                 }
1752                 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1753                 return true;
1754         }
1755
1756         /* if the flag has been set on the command line, then don't allow override,
1757            but don't report an error */
1758         if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1759                 return true;
1760         }
1761
1762         if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
1763                 char *suppress_env = getenv("SAMBA_DEPRECATED_SUPPRESS");
1764                 bool print_warning = (suppress_env == NULL
1765                                       || suppress_env[0] == '\0');
1766                 if (print_warning) {
1767                         DBG_WARNING("WARNING: The \"%s\" option "
1768                                     "is deprecated\n",
1769                                     pszParmName);
1770
1771                 }
1772         }
1773
1774         parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[parmnum]);
1775
1776         return set_variable(lp_ctx->globals->ctx, NULL, parmnum, parm_ptr,
1777                             pszParmName, pszParmValue, lp_ctx, true);
1778 }
1779
1780 bool lpcfg_do_service_parameter(struct loadparm_context *lp_ctx,
1781                                 struct loadparm_service *service,
1782                                 const char *pszParmName, const char *pszParmValue)
1783 {
1784         void *parm_ptr;
1785         int i;
1786         int parmnum = lpcfg_map_parameter(pszParmName);
1787
1788         if (parmnum < 0) {
1789                 if (strchr(pszParmName, ':')) {
1790                         return lp_do_parameter_parametric(lp_ctx, service, pszParmName, pszParmValue, 0);
1791                 }
1792                 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1793                 return true;
1794         }
1795
1796         /* if the flag has been set on the command line, then don't allow override,
1797            but don't report an error */
1798         if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1799                 return true;
1800         }
1801
1802         if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
1803                 char *suppress_env = getenv("SAMBA_DEPRECATED_SUPPRESS");
1804                 bool print_warning = (suppress_env == NULL
1805                                       || suppress_env[0] == '\0');
1806                 if (print_warning) {
1807                         DBG_WARNING("WARNING: The \"%s\" option "
1808                                     "is deprecated\n",
1809                                     pszParmName);
1810
1811                 }
1812         }
1813
1814         if (parm_table[parmnum].p_class == P_GLOBAL) {
1815                 DEBUG(0,
1816                       ("Global parameter %s found in service section!\n",
1817                        pszParmName));
1818                 return true;
1819         }
1820         parm_ptr = ((char *)service) + parm_table[parmnum].offset;
1821
1822         if (!service->copymap)
1823                 init_copymap(service);
1824
1825         /* this handles the aliases - set the copymap for other
1826          * entries with the same data pointer */
1827         for (i = 0; parm_table[i].label; i++)
1828                 if (parm_table[i].offset == parm_table[parmnum].offset &&
1829                     parm_table[i].p_class == parm_table[parmnum].p_class)
1830                         bitmap_clear(service->copymap, i);
1831
1832         return set_variable(service, service, parmnum, parm_ptr, pszParmName,
1833                             pszParmValue, lp_ctx, false);
1834 }
1835
1836 /**
1837  * Process a parameter.
1838  */
1839
1840 bool lpcfg_do_parameter(const char *pszParmName, const char *pszParmValue,
1841                          void *userdata)
1842 {
1843         struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1844
1845         if (lp_ctx->bInGlobalSection)
1846                 return lpcfg_do_global_parameter(lp_ctx, pszParmName,
1847                                               pszParmValue);
1848         else
1849                 return lpcfg_do_service_parameter(lp_ctx, lp_ctx->currentService,
1850                                                   pszParmName, pszParmValue);
1851 }
1852
1853 /*
1854   variable argument do parameter
1855 */
1856 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx, const char *pszParmName, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
1857 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx,
1858                                 const char *pszParmName, const char *fmt, ...)
1859 {
1860         char *s;
1861         bool ret;
1862         va_list ap;
1863
1864         va_start(ap, fmt);
1865         s = talloc_vasprintf(NULL, fmt, ap);
1866         va_end(ap);
1867         ret = lpcfg_do_global_parameter(lp_ctx, pszParmName, s);
1868         talloc_free(s);
1869         return ret;
1870 }
1871
1872
1873 /*
1874   set a parameter from the commandline - this is called from command line parameter
1875   parsing code. It sets the parameter then marks the parameter as unable to be modified
1876   by smb.conf processing
1877 */
1878 bool lpcfg_set_cmdline(struct loadparm_context *lp_ctx, const char *pszParmName,
1879                        const char *pszParmValue)
1880 {
1881         int parmnum;
1882         int i;
1883
1884         while (isspace((unsigned char)*pszParmValue)) pszParmValue++;
1885
1886         parmnum = lpcfg_map_parameter(pszParmName);
1887
1888         if (parmnum < 0 && strchr(pszParmName, ':')) {
1889                 /* set a parametric option */
1890                 bool ok;
1891                 ok = lp_do_parameter_parametric(lp_ctx, NULL, pszParmName,
1892                                                 pszParmValue, FLAG_CMDLINE);
1893                 if (lp_ctx->s3_fns != NULL) {
1894                         if (ok) {
1895                                 lp_ctx->s3_fns->store_cmdline(pszParmName, pszParmValue);
1896                         }
1897                 }
1898                 return ok;
1899         }
1900
1901         if (parmnum < 0) {
1902                 DEBUG(0,("Unknown option '%s'\n", pszParmName));
1903                 return false;
1904         }
1905
1906         /* reset the CMDLINE flag in case this has been called before */
1907         lp_ctx->flags[parmnum] &= ~FLAG_CMDLINE;
1908
1909         if (!lpcfg_do_global_parameter(lp_ctx, pszParmName, pszParmValue)) {
1910                 return false;
1911         }
1912
1913         lp_ctx->flags[parmnum] |= FLAG_CMDLINE;
1914
1915         /* we have to also set FLAG_CMDLINE on aliases */
1916         for (i=parmnum-1;
1917              i>=0 && parm_table[i].p_class == parm_table[parmnum].p_class &&
1918              parm_table[i].offset == parm_table[parmnum].offset;
1919              i--) {
1920                 lp_ctx->flags[i] |= FLAG_CMDLINE;
1921         }
1922         for (i=parmnum+1;
1923              i<num_parameters() &&
1924              parm_table[i].p_class == parm_table[parmnum].p_class &&
1925              parm_table[i].offset == parm_table[parmnum].offset;
1926              i++) {
1927                 lp_ctx->flags[i] |= FLAG_CMDLINE;
1928         }
1929
1930         if (lp_ctx->s3_fns != NULL) {
1931                 lp_ctx->s3_fns->store_cmdline(pszParmName, pszParmValue);
1932         }
1933
1934         return true;
1935 }
1936
1937 /*
1938   set a option from the commandline in 'a=b' format. Use to support --option
1939 */
1940 bool lpcfg_set_option(struct loadparm_context *lp_ctx, const char *option)
1941 {
1942         char *p, *s;
1943         bool ret;
1944
1945         s = talloc_strdup(NULL, option);
1946         if (!s) {
1947                 return false;
1948         }
1949
1950         p = strchr(s, '=');
1951         if (!p) {
1952                 talloc_free(s);
1953                 return false;
1954         }
1955
1956         *p = 0;
1957
1958         ret = lpcfg_set_cmdline(lp_ctx, s, p+1);
1959         talloc_free(s);
1960         return ret;
1961 }
1962
1963
1964 #define BOOLSTR(b) ((b) ? "Yes" : "No")
1965
1966 /**
1967  * Print a parameter of the specified type.
1968  */
1969
1970 void lpcfg_print_parameter(struct parm_struct *p, void *ptr, FILE * f)
1971 {
1972         /* For the seperation of lists values that we print below */
1973         const char *list_sep = ", ";
1974         int i;
1975         switch (p->type)
1976         {
1977                 case P_ENUM:
1978                         for (i = 0; p->enum_list[i].name; i++) {
1979                                 if (*(int *)ptr == p->enum_list[i].value) {
1980                                         fprintf(f, "%s",
1981                                                 p->enum_list[i].name);
1982                                         break;
1983                                 }
1984                         }
1985                         break;
1986
1987                 case P_BOOL:
1988                         fprintf(f, "%s", BOOLSTR(*(bool *)ptr));
1989                         break;
1990
1991                 case P_BOOLREV:
1992                         fprintf(f, "%s", BOOLSTR(!*(bool *)ptr));
1993                         break;
1994
1995                 case P_INTEGER:
1996                 case P_BYTES:
1997                         fprintf(f, "%d", *(int *)ptr);
1998                         break;
1999
2000                 case P_CHAR:
2001                         fprintf(f, "%c", *(char *)ptr);
2002                         break;
2003
2004                 case P_OCTAL: {
2005                         int val = *(int *)ptr; 
2006                         if (val == -1) {
2007                                 fprintf(f, "-1");
2008                         } else {
2009                                 fprintf(f, "0%03o", val);
2010                         }
2011                         break;
2012                 }
2013
2014                 case P_CMDLIST:
2015                         list_sep = " ";
2016
2017                         FALL_THROUGH;
2018                 case P_LIST:
2019                         if ((char ***)ptr && *(char ***)ptr) {
2020                                 char **list = *(char ***)ptr;
2021                                 for (; *list; list++) {
2022                                         /* surround strings with whitespace in double quotes */
2023                                         if (*(list+1) == NULL) {
2024                                                 /* last item, no extra separator */
2025                                                 list_sep = "";
2026                                         }
2027                                         if ( strchr_m( *list, ' ' ) ) {
2028                                                 fprintf(f, "\"%s\"%s", *list, list_sep);
2029                                         } else {
2030                                                 fprintf(f, "%s%s", *list, list_sep);
2031                                         }
2032                                 }
2033                         }
2034                         break;
2035
2036                 case P_STRING:
2037                 case P_USTRING:
2038                         if (*(char **)ptr) {
2039                                 fprintf(f, "%s", *(char **)ptr);
2040                         }
2041                         break;
2042         }
2043 }
2044
2045 /**
2046  * Check if two parameters are equal.
2047  */
2048
2049 static bool lpcfg_equal_parameter(parm_type type, void *ptr1, void *ptr2)
2050 {
2051         switch (type) {
2052                 case P_BOOL:
2053                 case P_BOOLREV:
2054                         return (*((bool *)ptr1) == *((bool *)ptr2));
2055
2056                 case P_INTEGER:
2057                 case P_ENUM:
2058                 case P_OCTAL:
2059                 case P_BYTES:
2060                         return (*((int *)ptr1) == *((int *)ptr2));
2061
2062                 case P_CHAR:
2063                         return (*((char *)ptr1) == *((char *)ptr2));
2064
2065                 case P_LIST:
2066                 case P_CMDLIST:
2067                         return str_list_equal(*(const char ***)ptr1, *(const char ***)ptr2);
2068
2069                 case P_STRING:
2070                 case P_USTRING:
2071                 {
2072                         char *p1 = *(char **)ptr1, *p2 = *(char **)ptr2;
2073                         if (p1 && !*p1)
2074                                 p1 = NULL;
2075                         if (p2 && !*p2)
2076                                 p2 = NULL;
2077                         return (p1 == p2 || strequal(p1, p2));
2078                 }
2079         }
2080         return false;
2081 }
2082
2083 /**
2084  * Process a new section (service).
2085  *
2086  * At this stage all sections are services.
2087  * Later we'll have special sections that permit server parameters to be set.
2088  * Returns True on success, False on failure.
2089  */
2090
2091 static bool do_section(const char *pszSectionName, void *userdata)
2092 {
2093         struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
2094         bool bRetval;
2095         bool isglobal;
2096
2097         if (lp_ctx->s3_fns != NULL) {
2098                 return lp_ctx->s3_fns->do_section(pszSectionName, lp_ctx);
2099         }
2100
2101         isglobal = ((strwicmp(pszSectionName, GLOBAL_NAME) == 0) ||
2102                          (strwicmp(pszSectionName, GLOBAL_NAME2) == 0));
2103
2104         /* if we've just struck a global section, note the fact. */
2105         lp_ctx->bInGlobalSection = isglobal;
2106
2107         /* check for multiple global sections */
2108         if (lp_ctx->bInGlobalSection) {
2109                 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
2110                 bRetval = true;
2111                 goto out;
2112         }
2113
2114         /* if we have a current service, tidy it up before moving on */
2115         bRetval = true;
2116
2117         if (lp_ctx->currentService != NULL)
2118                 bRetval = lpcfg_service_ok(lp_ctx->currentService);
2119
2120         /* if all is still well, move to the next record in the services array */
2121         if (bRetval) {
2122                 /* We put this here to avoid an odd message order if messages are */
2123                 /* issued by the post-processing of a previous section. */
2124                 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
2125
2126                 if ((lp_ctx->currentService = lpcfg_add_service(lp_ctx, lp_ctx->sDefault,
2127                                                                    pszSectionName))
2128                     == NULL) {
2129                         DEBUG(0, ("Failed to add a new service\n"));
2130                         bRetval = false;
2131                         goto out;
2132                 }
2133         }
2134 out:
2135         return bRetval;
2136 }
2137
2138
2139 /**
2140  * Determine if a particular base parameter is currently set to the default value.
2141  */
2142
2143 static bool is_default(void *base_structure, int i)
2144 {
2145         void *def_ptr = ((char *)base_structure) + parm_table[i].offset;
2146         switch (parm_table[i].type) {
2147                 case P_CMDLIST:
2148                 case P_LIST:
2149                         return str_list_equal((const char * const *)parm_table[i].def.lvalue,
2150                                               *(const char * const **)def_ptr);
2151                 case P_STRING:
2152                 case P_USTRING:
2153                         return strequal(parm_table[i].def.svalue,
2154                                         *(char **)def_ptr);
2155                 case P_BOOL:
2156                 case P_BOOLREV:
2157                         return parm_table[i].def.bvalue ==
2158                                 *(bool *)def_ptr;
2159                 case P_INTEGER:
2160                 case P_CHAR:
2161                 case P_OCTAL:
2162                 case P_BYTES:
2163                 case P_ENUM:
2164                         return parm_table[i].def.ivalue ==
2165                                 *(int *)def_ptr;
2166         }
2167         return false;
2168 }
2169
2170 /**
2171  *Display the contents of the global structure.
2172  */
2173
2174 void lpcfg_dump_globals(struct loadparm_context *lp_ctx, FILE *f,
2175                          bool show_defaults)
2176 {
2177         int i;
2178         struct parmlist_entry *data;
2179
2180         fprintf(f, "# Global parameters\n[global]\n");
2181
2182         for (i = 0; parm_table[i].label; i++) {
2183                 if (parm_table[i].p_class != P_GLOBAL) {
2184                         continue;
2185                 }
2186
2187                 if (parm_table[i].flags & FLAG_SYNONYM) {
2188                         continue;
2189                 }
2190
2191                 if (!show_defaults) {
2192                         if (lp_ctx->flags && (lp_ctx->flags[i] & FLAG_DEFAULT)) {
2193                                 continue;
2194                         }
2195
2196                         if (is_default(lp_ctx->globals, i)) {
2197                                 continue;
2198                         }
2199                 }
2200
2201                 fprintf(f, "\t%s = ", parm_table[i].label);
2202                 lpcfg_print_parameter(&parm_table[i], lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[i]), f);
2203                 fprintf(f, "\n");
2204         }
2205         if (lp_ctx->globals->param_opt != NULL) {
2206                 for (data = lp_ctx->globals->param_opt; data;
2207                      data = data->next) {
2208                         if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
2209                                 continue;
2210                         }
2211                         fprintf(f, "\t%s = %s\n", data->key, data->value);
2212                 }
2213         }
2214
2215 }
2216
2217 /**
2218  * Display the contents of a single services record.
2219  */
2220
2221 void lpcfg_dump_a_service(struct loadparm_service * pService, struct loadparm_service *sDefault, FILE * f,
2222                           unsigned int *flags, bool show_defaults)
2223 {
2224         int i;
2225         struct parmlist_entry *data;
2226
2227         if (pService != sDefault)
2228                 fprintf(f, "\n[%s]\n", pService->szService);
2229
2230         for (i = 0; parm_table[i].label; i++) {
2231                 if (parm_table[i].p_class != P_LOCAL) {
2232                         continue;
2233                 }
2234
2235                 if (parm_table[i].flags & FLAG_SYNONYM) {
2236                         continue;
2237                 }
2238
2239                 if (*parm_table[i].label == '-') {
2240                         continue;
2241                 }
2242
2243                 if (pService == sDefault) {
2244                         if (!show_defaults) {
2245                                 if (flags && (flags[i] & FLAG_DEFAULT)) {
2246                                         continue;
2247                                 }
2248
2249                                 if (is_default(sDefault, i)) {
2250                                         continue;
2251                                 }
2252                         }
2253                 } else {
2254                         bool equal;
2255
2256                         equal = lpcfg_equal_parameter(parm_table[i].type,
2257                                                       ((char *)pService) +
2258                                                       parm_table[i].offset,
2259                                                       ((char *)sDefault) +
2260                                                       parm_table[i].offset);
2261                         if (equal) {
2262                                 continue;
2263                         }
2264                 }
2265
2266                 fprintf(f, "\t%s = ", parm_table[i].label);
2267                 lpcfg_print_parameter(&parm_table[i],
2268                                 ((char *)pService) + parm_table[i].offset, f);
2269                 fprintf(f, "\n");
2270         }
2271         if (pService->param_opt != NULL) {
2272                 for (data = pService->param_opt; data; data = data->next) {
2273                         if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
2274                                 continue;
2275                         }
2276                         fprintf(f, "\t%s = %s\n", data->key, data->value);
2277                 }
2278         }
2279 }
2280
2281 bool lpcfg_dump_a_parameter(struct loadparm_context *lp_ctx,
2282                             struct loadparm_service *service,
2283                             const char *parm_name, FILE * f)
2284 {
2285         struct parm_struct *parm;
2286         void *ptr;
2287         char *local_parm_name;
2288         char *parm_opt;
2289         const char *parm_opt_value;
2290
2291         /* check for parametrical option */
2292         local_parm_name = talloc_strdup(lp_ctx, parm_name);
2293         if (local_parm_name == NULL) {
2294                 return false;
2295         }
2296
2297         parm_opt = strchr( local_parm_name, ':');
2298
2299         if (parm_opt) {
2300                 *parm_opt = '\0';
2301                 parm_opt++;
2302                 if (strlen(parm_opt)) {
2303                         parm_opt_value = lpcfg_parm_string(lp_ctx, service,
2304                                 local_parm_name, parm_opt);
2305                         if (parm_opt_value) {
2306                                 fprintf(f, "%s\n", parm_opt_value);
2307                                 TALLOC_FREE(local_parm_name);
2308                                 return true;
2309                         }
2310                 }
2311                 TALLOC_FREE(local_parm_name);
2312                 return false;
2313         }
2314         TALLOC_FREE(local_parm_name);
2315
2316         /* parameter is not parametric, search the table */
2317         parm = lpcfg_parm_struct(lp_ctx, parm_name);
2318         if (!parm) {
2319                 return false;
2320         }
2321
2322         if (service != NULL && parm->p_class == P_GLOBAL) {
2323                 return false;
2324         }
2325
2326         ptr = lpcfg_parm_ptr(lp_ctx, service,parm);
2327
2328         lpcfg_print_parameter(parm, ptr, f);
2329         fprintf(f, "\n");
2330         return true;
2331 }
2332
2333 /**
2334  * Auto-load some home services.
2335  */
2336 static void lpcfg_add_auto_services(struct loadparm_context *lp_ctx,
2337                                     const char *str)
2338 {
2339         return;
2340 }
2341
2342 /***************************************************************************
2343  Initialise the sDefault parameter structure for the printer values.
2344 ***************************************************************************/
2345
2346 void init_printer_values(struct loadparm_context *lp_ctx, TALLOC_CTX *ctx,
2347                          struct loadparm_service *pService)
2348 {
2349         /* choose defaults depending on the type of printing */
2350         switch (pService->printing) {
2351                 case PRINT_BSD:
2352                 case PRINT_AIX:
2353                 case PRINT_LPRNT:
2354                 case PRINT_LPROS2:
2355                         lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P'%p'");
2356                         lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P'%p' %j");
2357                         lpcfg_string_set(ctx, &pService->print_command, "lpr -r -P'%p' %s");
2358                         break;
2359
2360                 case PRINT_LPRNG:
2361                 case PRINT_PLP:
2362                         lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P'%p'");
2363                         lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P'%p' %j");
2364                         lpcfg_string_set(ctx, &pService->print_command, "lpr -r -P'%p' %s");
2365                         lpcfg_string_set(ctx, &pService->queuepause_command, "lpc stop '%p'");
2366                         lpcfg_string_set(ctx, &pService->queueresume_command, "lpc start '%p'");
2367                         lpcfg_string_set(ctx, &pService->lppause_command, "lpc hold '%p' %j");
2368                         lpcfg_string_set(ctx, &pService->lpresume_command, "lpc release '%p' %j");
2369                         break;
2370
2371                 case PRINT_CUPS:
2372                 case PRINT_IPRINT:
2373                         /* set the lpq command to contain the destination printer
2374                            name only.  This is used by cups_queue_get() */
2375                         lpcfg_string_set(ctx, &pService->lpq_command, "%p");
2376                         lpcfg_string_set(ctx, &pService->lprm_command, "");
2377                         lpcfg_string_set(ctx, &pService->print_command, "");
2378                         lpcfg_string_set(ctx, &pService->lppause_command, "");
2379                         lpcfg_string_set(ctx, &pService->lpresume_command, "");
2380                         lpcfg_string_set(ctx, &pService->queuepause_command, "");
2381                         lpcfg_string_set(ctx, &pService->queueresume_command, "");
2382                         break;
2383
2384                 case PRINT_SYSV:
2385                 case PRINT_HPUX:
2386                         lpcfg_string_set(ctx, &pService->lpq_command, "lpstat -o%p");
2387                         lpcfg_string_set(ctx, &pService->lprm_command, "cancel %p-%j");
2388                         lpcfg_string_set(ctx, &pService->print_command, "lp -c -d%p %s; rm %s");
2389                         lpcfg_string_set(ctx, &pService->queuepause_command, "disable %p");
2390                         lpcfg_string_set(ctx, &pService->queueresume_command, "enable %p");
2391 #ifndef HPUX
2392                         lpcfg_string_set(ctx, &pService->lppause_command, "lp -i %p-%j -H hold");
2393                         lpcfg_string_set(ctx, &pService->lpresume_command, "lp -i %p-%j -H resume");
2394 #endif /* HPUX */
2395                         break;
2396
2397                 case PRINT_QNX:
2398                         lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P%p");
2399                         lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P%p %j");
2400                         lpcfg_string_set(ctx, &pService->print_command, "lp -r -P%p %s");
2401                         break;
2402
2403 #if defined(DEVELOPER) || defined(ENABLE_SELFTEST)
2404
2405         case PRINT_TEST:
2406         case PRINT_VLP: {
2407                 const char *tdbfile;
2408                 TALLOC_CTX *tmp_ctx = talloc_new(ctx);
2409                 const char *tmp;
2410
2411                 tmp = lpcfg_parm_string(lp_ctx, NULL, "vlp", "tdbfile");
2412                 if (tmp == NULL) {
2413                         tmp = "/tmp/vlp.tdb";
2414                 }
2415
2416                 tdbfile = talloc_asprintf(tmp_ctx, "tdbfile=%s", tmp);
2417                 if (tdbfile == NULL) {
2418                         tdbfile="tdbfile=/tmp/vlp.tdb";
2419                 }
2420
2421                 tmp = talloc_asprintf(tmp_ctx, "vlp %s print %%p %%s",
2422                                       tdbfile);
2423                 lpcfg_string_set(ctx, &pService->print_command,
2424                            tmp ? tmp : "vlp print %p %s");
2425
2426                 tmp = talloc_asprintf(tmp_ctx, "vlp %s lpq %%p",
2427                                       tdbfile);
2428                 lpcfg_string_set(ctx, &pService->lpq_command,
2429                            tmp ? tmp : "vlp lpq %p");
2430
2431                 tmp = talloc_asprintf(tmp_ctx, "vlp %s lprm %%p %%j",
2432                                       tdbfile);
2433                 lpcfg_string_set(ctx, &pService->lprm_command,
2434                            tmp ? tmp : "vlp lprm %p %j");
2435
2436                 tmp = talloc_asprintf(tmp_ctx, "vlp %s lppause %%p %%j",
2437                                       tdbfile);
2438                 lpcfg_string_set(ctx, &pService->lppause_command,
2439                            tmp ? tmp : "vlp lppause %p %j");
2440
2441                 tmp = talloc_asprintf(tmp_ctx, "vlp %s lpresume %%p %%j",
2442                                       tdbfile);
2443                 lpcfg_string_set(ctx, &pService->lpresume_command,
2444                            tmp ? tmp : "vlp lpresume %p %j");
2445
2446                 tmp = talloc_asprintf(tmp_ctx, "vlp %s queuepause %%p",
2447                                       tdbfile);
2448                 lpcfg_string_set(ctx, &pService->queuepause_command,
2449                            tmp ? tmp : "vlp queuepause %p");
2450
2451                 tmp = talloc_asprintf(tmp_ctx, "vlp %s queueresume %%p",
2452                                       tdbfile);
2453                 lpcfg_string_set(ctx, &pService->queueresume_command,
2454                            tmp ? tmp : "vlp queueresume %p");
2455                 TALLOC_FREE(tmp_ctx);
2456
2457                 break;
2458         }
2459 #endif /* DEVELOPER */
2460
2461         }
2462 }
2463
2464
2465 static int lpcfg_destructor(struct loadparm_context *lp_ctx)
2466 {
2467         struct parmlist_entry *data;
2468
2469         if (lp_ctx->refuse_free) {
2470                 /* someone is trying to free the
2471                    global_loadparm_context.
2472                    We can't allow that. */
2473                 return -1;
2474         }
2475
2476         if (lp_ctx->globals->param_opt != NULL) {
2477                 struct parmlist_entry *next;
2478                 for (data = lp_ctx->globals->param_opt; data; data=next) {
2479                         next = data->next;
2480                         if (data->priority & FLAG_CMDLINE) continue;
2481                         DLIST_REMOVE(lp_ctx->globals->param_opt, data);
2482                         talloc_free(data);
2483                 }
2484         }
2485
2486         return 0;
2487 }
2488
2489 /**
2490  * Initialise the global parameter structure.
2491  *
2492  * Note that most callers should use loadparm_init_global() instead
2493  */
2494 struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
2495 {
2496         int i;
2497         char *myname;
2498         struct loadparm_context *lp_ctx;
2499         struct parmlist_entry *parm;
2500         char *logfile;
2501
2502         lp_ctx = talloc_zero(mem_ctx, struct loadparm_context);
2503         if (lp_ctx == NULL)
2504                 return NULL;
2505
2506         talloc_set_destructor(lp_ctx, lpcfg_destructor);
2507         lp_ctx->bInGlobalSection = true;
2508         lp_ctx->globals = talloc_zero(lp_ctx, struct loadparm_global);
2509         /* This appears odd, but globals in s3 isn't a pointer */
2510         lp_ctx->globals->ctx = lp_ctx->globals;
2511         lp_ctx->globals->rpc_low_port = SERVER_TCP_LOW_PORT;
2512         lp_ctx->globals->rpc_high_port = SERVER_TCP_HIGH_PORT;
2513         lp_ctx->globals->weak_crypto = SAMBA_WEAK_CRYPTO_UNKNOWN;
2514         lp_ctx->sDefault = talloc_zero(lp_ctx, struct loadparm_service);
2515         lp_ctx->flags = talloc_zero_array(lp_ctx, unsigned int, num_parameters());
2516
2517         lp_ctx->sDefault->max_print_jobs = 1000;
2518         lp_ctx->sDefault->available = true;
2519         lp_ctx->sDefault->browseable = true;
2520         lp_ctx->sDefault->read_only = true;
2521         lp_ctx->sDefault->map_archive = true;
2522         lp_ctx->sDefault->strict_locking = true;
2523         lp_ctx->sDefault->oplocks = true;
2524         lp_ctx->sDefault->create_mask = 0744;
2525         lp_ctx->sDefault->force_create_mode = 0000;
2526         lp_ctx->sDefault->directory_mask = 0755;
2527         lp_ctx->sDefault->force_directory_mode = 0000;
2528         lp_ctx->sDefault->aio_read_size = 1;
2529         lp_ctx->sDefault->aio_write_size = 1;
2530         lp_ctx->sDefault->smbd_search_ask_sharemode = true;
2531         lp_ctx->sDefault->smbd_getinfo_ask_sharemode = true;
2532         lp_ctx->sDefault->volume_serial_number = -1;
2533
2534         DEBUG(3, ("Initialising global parameters\n"));
2535
2536         for (i = 0; parm_table[i].label; i++) {
2537                 if ((parm_table[i].type == P_STRING ||
2538                      parm_table[i].type == P_USTRING) &&
2539                     !(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2540                         TALLOC_CTX *parent_mem;
2541                         char **r;
2542                         if (parm_table[i].p_class == P_LOCAL) {
2543                                 parent_mem = lp_ctx->sDefault;
2544                                 r = (char **)(((char *)lp_ctx->sDefault) + parm_table[i].offset);
2545                         } else {
2546                                 parent_mem = lp_ctx->globals;
2547                                 r = (char **)(((char *)lp_ctx->globals) + parm_table[i].offset);
2548                         }
2549                         lpcfg_string_set(parent_mem, r, "");
2550                 }
2551         }
2552
2553         logfile = talloc_asprintf(lp_ctx, "%s/log.samba", dyn_LOGFILEBASE);
2554         lpcfg_do_global_parameter(lp_ctx, "log file", logfile);
2555         talloc_free(logfile);
2556
2557         lpcfg_do_global_parameter(lp_ctx, "log level", "0");
2558
2559         lpcfg_do_global_parameter(lp_ctx, "syslog", "1");
2560         lpcfg_do_global_parameter(lp_ctx, "syslog only", "No");
2561         lpcfg_do_global_parameter(lp_ctx, "debug timestamp", "Yes");
2562         lpcfg_do_global_parameter(lp_ctx, "debug prefix timestamp", "No");
2563         lpcfg_do_global_parameter(lp_ctx, "debug hires timestamp", "Yes");
2564         lpcfg_do_global_parameter(lp_ctx, "debug syslog format", "No");
2565         lpcfg_do_global_parameter(lp_ctx, "debug pid", "No");
2566         lpcfg_do_global_parameter(lp_ctx, "debug uid", "No");
2567         lpcfg_do_global_parameter(lp_ctx, "debug class", "No");
2568
2569         lpcfg_do_global_parameter(lp_ctx, "server role", "auto");
2570         lpcfg_do_global_parameter(lp_ctx, "domain logons", "No");
2571         lpcfg_do_global_parameter(lp_ctx, "domain master", "Auto");
2572
2573         /* options that can be set on the command line must be initialised via
2574            the slower lpcfg_do_global_parameter() to ensure that FLAG_CMDLINE is obeyed */
2575 #ifdef TCP_NODELAY
2576         lpcfg_do_global_parameter(lp_ctx, "socket options", "TCP_NODELAY");
2577 #endif
2578         lpcfg_do_global_parameter(lp_ctx, "workgroup", DEFAULT_WORKGROUP);
2579         myname = get_myname(lp_ctx);
2580         lpcfg_do_global_parameter(lp_ctx, "netbios name", myname);
2581         talloc_free(myname);
2582         lpcfg_do_global_parameter(lp_ctx,
2583                                   "name resolve order",
2584                                   DEFAULT_NAME_RESOLVE_ORDER);
2585
2586         lpcfg_do_global_parameter(lp_ctx, "fstype", "NTFS");
2587
2588         lpcfg_do_global_parameter(lp_ctx, "ntvfs handler", "unixuid default");
2589         lpcfg_do_global_parameter(lp_ctx, "max connections", "0");
2590
2591         lpcfg_do_global_parameter(lp_ctx, "dcerpc endpoint servers", "epmapper wkssvc rpcecho samr netlogon lsarpc drsuapi dssetup unixinfo browser eventlog6 backupkey dnsserver");
2592         lpcfg_do_global_parameter(lp_ctx, "server services", "s3fs rpc nbt wrepl ldap cldap kdc drepl winbindd ntp_signd kcc dnsupdate dns");
2593         lpcfg_do_global_parameter(lp_ctx, "kccsrv:samba_kcc", "true");
2594         /* the winbind method for domain controllers is for both RODC
2595            auth forwarding and for trusted domains */
2596         lpcfg_do_global_parameter(lp_ctx, "private dir", dyn_PRIVATE_DIR);
2597         lpcfg_do_global_parameter(lp_ctx, "binddns dir", dyn_BINDDNS_DIR);
2598         lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_LOCAL_MACHINE", "hklm.ldb");
2599
2600         /* This hive should be dynamically generated by Samba using
2601            data from the sam, but for the moment leave it in a tdb to
2602            keep regedt32 from popping up an annoying dialog. */
2603         lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_USERS", "hku.ldb");
2604
2605         /* using UTF8 by default allows us to support all chars */
2606         lpcfg_do_global_parameter(lp_ctx, "unix charset", "UTF-8");
2607
2608         /* Use codepage 850 as a default for the dos character set */
2609         lpcfg_do_global_parameter(lp_ctx, "dos charset", "CP850");
2610
2611         /*
2612          * Allow the default PASSWD_CHAT to be overridden in local.h.
2613          */
2614         lpcfg_do_global_parameter(lp_ctx, "passwd chat", DEFAULT_PASSWD_CHAT);
2615
2616         lpcfg_do_global_parameter(lp_ctx, "pid directory", dyn_PIDDIR);
2617         lpcfg_do_global_parameter(lp_ctx, "lock dir", dyn_LOCKDIR);
2618         lpcfg_do_global_parameter(lp_ctx, "state directory", dyn_STATEDIR);
2619         lpcfg_do_global_parameter(lp_ctx, "cache directory", dyn_CACHEDIR);
2620         lpcfg_do_global_parameter(lp_ctx, "ncalrpc dir", dyn_NCALRPCDIR);
2621
2622         lpcfg_do_global_parameter(lp_ctx, "nbt client socket address", "0.0.0.0");
2623         lpcfg_do_global_parameter_var(lp_ctx, "server string",
2624                                    "Samba %s", SAMBA_VERSION_STRING);
2625
2626         lpcfg_do_global_parameter(lp_ctx, "password server", "*");
2627
2628         lpcfg_do_global_parameter(lp_ctx, "max mux", "50");
2629         lpcfg_do_global_parameter(lp_ctx, "max xmit", "16644");
2630         lpcfg_do_global_parameter(lp_ctx, "host msdfs", "true");
2631
2632         lpcfg_do_global_parameter(lp_ctx, "LargeReadwrite", "True");
2633         lpcfg_do_global_parameter(lp_ctx, "server min protocol", "SMB2_02");
2634         lpcfg_do_global_parameter(lp_ctx, "server max protocol", "SMB3");
2635         lpcfg_do_global_parameter(lp_ctx, "client min protocol", "SMB2_02");
2636         lpcfg_do_global_parameter(lp_ctx, "client max protocol", "default");
2637         lpcfg_do_global_parameter(lp_ctx, "client ipc min protocol", "default");
2638         lpcfg_do_global_parameter(lp_ctx, "client ipc max protocol", "default");
2639         lpcfg_do_global_parameter(lp_ctx, "security", "AUTO");
2640         lpcfg_do_global_parameter(lp_ctx, "EncryptPasswords", "True");
2641         lpcfg_do_global_parameter(lp_ctx, "ReadRaw", "True");
2642         lpcfg_do_global_parameter(lp_ctx, "WriteRaw", "True");
2643         lpcfg_do_global_parameter(lp_ctx, "NullPasswords", "False");
2644         lpcfg_do_global_parameter(lp_ctx, "old password allowed period", "60");
2645         lpcfg_do_global_parameter(lp_ctx, "ObeyPamRestrictions", "False");
2646
2647         lpcfg_do_global_parameter(lp_ctx, "TimeServer", "False");
2648         lpcfg_do_global_parameter(lp_ctx, "BindInterfacesOnly", "False");
2649         lpcfg_do_global_parameter(lp_ctx, "Unicode", "True");
2650         lpcfg_do_global_parameter(lp_ctx, "ClientLanManAuth", "False");
2651         lpcfg_do_global_parameter(lp_ctx, "ClientNTLMv2Auth", "True");
2652         lpcfg_do_global_parameter(lp_ctx, "LanmanAuth", "False");
2653         lpcfg_do_global_parameter(lp_ctx, "NTLMAuth", "ntlmv2-only");
2654         lpcfg_do_global_parameter(lp_ctx, "NT hash store", "always");
2655         lpcfg_do_global_parameter(lp_ctx, "RawNTLMv2Auth", "False");
2656         lpcfg_do_global_parameter(lp_ctx, "client use spnego principal", "False");
2657
2658         lpcfg_do_global_parameter(lp_ctx, "allow dcerpc auth level connect", "False");
2659
2660         lpcfg_do_global_parameter(lp_ctx, "UnixExtensions", "True");
2661
2662         lpcfg_do_global_parameter(lp_ctx, "PreferredMaster", "Auto");
2663         lpcfg_do_global_parameter(lp_ctx, "LocalMaster", "True");
2664
2665         lpcfg_do_global_parameter(lp_ctx, "wins support", "False");
2666         lpcfg_do_global_parameter(lp_ctx, "dns proxy", "True");
2667
2668         lpcfg_do_global_parameter(lp_ctx, "winbind separator", "\\");
2669         lpcfg_do_global_parameter(lp_ctx, "winbind sealed pipes", "True");
2670         lpcfg_do_global_parameter(lp_ctx, "winbind scan trusted domains", "False");
2671         lpcfg_do_global_parameter(lp_ctx, "require strong key", "True");
2672         lpcfg_do_global_parameter(lp_ctx, "reject md5 servers", "True");
2673         lpcfg_do_global_parameter(lp_ctx, "winbindd socket directory", dyn_WINBINDD_SOCKET_DIR);
2674         lpcfg_do_global_parameter(lp_ctx, "ntp signd socket directory", dyn_NTP_SIGND_SOCKET_DIR);
2675         lpcfg_do_global_parameter_var(lp_ctx, "gpo update command", "%s/samba-gpupdate", dyn_SCRIPTSBINDIR);
2676         lpcfg_do_global_parameter_var(lp_ctx, "apply group policies", "False");
2677         lpcfg_do_global_parameter_var(lp_ctx, "dns update command", "%s/samba_dnsupdate", dyn_SCRIPTSBINDIR);
2678         lpcfg_do_global_parameter_var(lp_ctx, "spn update command", "%s/samba_spnupdate", dyn_SCRIPTSBINDIR);
2679         lpcfg_do_global_parameter_var(lp_ctx, "samba kcc command",
2680                                         "%s/samba_kcc", dyn_SCRIPTSBINDIR);
2681 #ifdef MIT_KDC_PATH
2682         lpcfg_do_global_parameter_var(lp_ctx,
2683                                       "mit kdc command",
2684                                       MIT_KDC_PATH);
2685 #endif
2686         lpcfg_do_global_parameter(lp_ctx, "template shell", "/bin/false");
2687         lpcfg_do_global_parameter(lp_ctx, "template homedir", "/home/%D/%U");
2688
2689         lpcfg_do_global_parameter(lp_ctx, "client signing", "default");
2690         lpcfg_do_global_parameter(lp_ctx, "client ipc signing", "default");
2691         lpcfg_do_global_parameter(lp_ctx, "server signing", "default");
2692
2693         lpcfg_do_global_parameter(lp_ctx, "use mmap", "True");
2694
2695         lpcfg_do_global_parameter(lp_ctx, "smb ports", "445 139");
2696         lpcfg_do_global_parameter_var(lp_ctx, "nbt port", "%d", NBT_NAME_SERVICE_PORT);
2697         lpcfg_do_global_parameter_var(lp_ctx, "dgram port", "%d", NBT_DGRAM_SERVICE_PORT);
2698         lpcfg_do_global_parameter(lp_ctx, "cldap port", "389");
2699         lpcfg_do_global_parameter(lp_ctx, "krb5 port", "88");
2700         lpcfg_do_global_parameter(lp_ctx, "kpasswd port", "464");
2701         lpcfg_do_global_parameter_var(lp_ctx, "dns port", "%d", DNS_SERVICE_PORT);
2702
2703         lpcfg_do_global_parameter(lp_ctx, "kdc enable fast", "True");
2704
2705         lpcfg_do_global_parameter(lp_ctx, "nt status support", "True");
2706
2707         lpcfg_do_global_parameter(lp_ctx, "max wins ttl", "518400"); /* 6 days */
2708         lpcfg_do_global_parameter(lp_ctx, "min wins ttl", "21600");
2709
2710         lpcfg_do_global_parameter(lp_ctx, "tls enabled", "True");
2711         lpcfg_do_global_parameter(lp_ctx, "tls verify peer", "as_strict_as_possible");
2712         lpcfg_do_global_parameter(lp_ctx, "tls keyfile", "tls/key.pem");
2713         lpcfg_do_global_parameter(lp_ctx, "tls certfile", "tls/cert.pem");
2714         lpcfg_do_global_parameter(lp_ctx, "tls cafile", "tls/ca.pem");
2715         lpcfg_do_global_parameter(lp_ctx,
2716                                   "tls priority",
2717                                   "NORMAL:-VERS-SSL3.0");
2718
2719         lpcfg_do_global_parameter(lp_ctx, "nsupdate command", "/usr/bin/nsupdate -g");
2720
2721         lpcfg_do_global_parameter(lp_ctx, "allow dns updates", "secure only");
2722         lpcfg_do_global_parameter(lp_ctx, "dns zone scavenging", "False");
2723         lpcfg_do_global_parameter(lp_ctx, "dns forwarder", "");
2724
2725         lpcfg_do_global_parameter(lp_ctx, "algorithmic rid base", "1000");
2726
2727         lpcfg_do_global_parameter(lp_ctx, "enhanced browsing", "True");
2728
2729         lpcfg_do_global_parameter(lp_ctx, "winbind nss info", "template");
2730
2731         lpcfg_do_global_parameter(lp_ctx, "server schannel", "True");
2732
2733         lpcfg_do_global_parameter(lp_ctx, "short preserve case", "True");
2734
2735         lpcfg_do_global_parameter(lp_ctx, "max open files", "16384");
2736
2737         lpcfg_do_global_parameter(lp_ctx, "cups connection timeout", "30");
2738
2739         lpcfg_do_global_parameter(lp_ctx, "locking", "True");
2740
2741         lpcfg_do_global_parameter(lp_ctx, "block size", "1024");
2742
2743         lpcfg_do_global_parameter(lp_ctx, "client use spnego", "True");
2744
2745         lpcfg_do_global_parameter(lp_ctx, "change notify", "True");
2746
2747         lpcfg_do_global_parameter(lp_ctx, "name cache timeout", "660");
2748
2749         lpcfg_do_global_parameter(lp_ctx, "defer sharing violations", "True");
2750
2751         lpcfg_do_global_parameter(lp_ctx, "ldap replication sleep", "1000");
2752
2753         lpcfg_do_global_parameter(lp_ctx, "idmap backend", "tdb");
2754
2755         lpcfg_do_global_parameter(lp_ctx, "enable privileges", "True");
2756
2757         lpcfg_do_global_parameter_var(lp_ctx, "smb2 max write", "%u", DEFAULT_SMB2_MAX_WRITE);
2758
2759         lpcfg_do_global_parameter(lp_ctx, "passdb backend", "tdbsam");
2760
2761         lpcfg_do_global_parameter(lp_ctx, "deadtime", "10080");
2762
2763         lpcfg_do_global_parameter(lp_ctx, "getwd cache", "True");
2764
2765         lpcfg_do_global_parameter(lp_ctx, "winbind nested groups", "True");
2766
2767         lpcfg_do_global_parameter(lp_ctx, "mangled names", "illegal");
2768
2769         lpcfg_do_global_parameter_var(lp_ctx, "smb2 max credits", "%u", DEFAULT_SMB2_MAX_CREDITS);
2770
2771         lpcfg_do_global_parameter(lp_ctx, "ldap ssl", "start tls");
2772
2773         lpcfg_do_global_parameter(lp_ctx, "ldap deref", "auto");
2774
2775         lpcfg_do_global_parameter(lp_ctx, "lm interval", "60");
2776
2777         lpcfg_do_global_parameter(lp_ctx, "mangling method", "hash2");
2778
2779         lpcfg_do_global_parameter(lp_ctx, "hide dot files", "True");
2780
2781         lpcfg_do_global_parameter(lp_ctx, "browse list", "True");
2782
2783         lpcfg_do_global_parameter(lp_ctx, "passwd chat timeout", "2");
2784
2785         lpcfg_do_global_parameter(lp_ctx, "guest account", GUEST_ACCOUNT);
2786
2787         lpcfg_do_global_parameter(lp_ctx, "client schannel", "True");
2788
2789         lpcfg_do_global_parameter(lp_ctx, "smb encrypt", "default");
2790
2791         lpcfg_do_global_parameter(lp_ctx, "max log size", "5000");
2792
2793         lpcfg_do_global_parameter(lp_ctx, "idmap negative cache time", "120");
2794
2795         lpcfg_do_global_parameter(lp_ctx, "ldap follow referral", "auto");
2796
2797         lpcfg_do_global_parameter(lp_ctx, "multicast dns register", "yes");
2798
2799         lpcfg_do_global_parameter(lp_ctx, "winbind reconnect delay", "30");
2800
2801         lpcfg_do_global_parameter(lp_ctx, "winbind request timeout", "60");
2802
2803         lpcfg_do_global_parameter(lp_ctx, "nt acl support", "yes");
2804
2805         lpcfg_do_global_parameter(lp_ctx, "acl check permissions", "yes");
2806
2807         lpcfg_do_global_parameter(lp_ctx, "keepalive", "300");
2808
2809         lpcfg_do_global_parameter(lp_ctx, "smbd profiling level", "off");
2810
2811         lpcfg_do_global_parameter(lp_ctx, "winbind cache time", "300");
2812
2813         lpcfg_do_global_parameter(lp_ctx, "level2 oplocks", "yes");
2814
2815         lpcfg_do_global_parameter(lp_ctx, "show add printer wizard", "yes");
2816
2817         lpcfg_do_global_parameter(lp_ctx, "ldap page size", "1000");
2818
2819         lpcfg_do_global_parameter(lp_ctx, "kernel share modes", "no");
2820
2821         lpcfg_do_global_parameter(lp_ctx, "strict locking", "Auto");
2822
2823         lpcfg_do_global_parameter(lp_ctx, "strict sync", "yes");
2824
2825         lpcfg_do_global_parameter(lp_ctx, "map readonly", "no");
2826
2827         lpcfg_do_global_parameter(lp_ctx, "allow trusted domains", "yes");
2828
2829         lpcfg_do_global_parameter(lp_ctx, "default devmode", "yes");
2830
2831         lpcfg_do_global_parameter(lp_ctx, "os level", "20");
2832
2833         lpcfg_do_global_parameter(lp_ctx, "dos filetimes", "yes");
2834
2835         lpcfg_do_global_parameter(lp_ctx, "mangling char", "~");
2836
2837         lpcfg_do_global_parameter(lp_ctx, "printcap cache time", "750");
2838
2839         lpcfg_do_global_parameter(lp_ctx, "create krb5 conf", "yes");
2840
2841         lpcfg_do_global_parameter(lp_ctx, "winbind max clients", "200");
2842
2843         lpcfg_do_global_parameter(lp_ctx, "acl map full control", "yes");
2844
2845         lpcfg_do_global_parameter(lp_ctx, "nt pipe support", "yes");
2846
2847         lpcfg_do_global_parameter(lp_ctx, "ldap debug threshold", "10");
2848
2849         lpcfg_do_global_parameter(lp_ctx, "client ldap sasl wrapping", "sign");
2850
2851         lpcfg_do_global_parameter(lp_ctx, "mdns name", "netbios");
2852
2853         lpcfg_do_global_parameter(lp_ctx, "ldap server require strong auth", "yes");
2854
2855         lpcfg_do_global_parameter(lp_ctx, "follow symlinks", "yes");
2856
2857         lpcfg_do_global_parameter(lp_ctx, "machine password timeout", "604800");
2858
2859         lpcfg_do_global_parameter(lp_ctx, "ldap connection timeout", "2");
2860
2861         lpcfg_do_global_parameter(lp_ctx, "winbind expand groups", "0");
2862
2863         lpcfg_do_global_parameter(lp_ctx, "stat cache", "yes");
2864
2865         lpcfg_do_global_parameter(lp_ctx, "lpq cache time", "30");
2866
2867         lpcfg_do_global_parameter_var(lp_ctx, "smb2 max trans", "%u", DEFAULT_SMB2_MAX_TRANSACT);
2868
2869         lpcfg_do_global_parameter_var(lp_ctx, "smb2 max read", "%u", DEFAULT_SMB2_MAX_READ);
2870
2871         lpcfg_do_global_parameter(lp_ctx, "durable handles", "yes");
2872
2873         lpcfg_do_global_parameter(lp_ctx, "max stat cache size", "512");
2874
2875         lpcfg_do_global_parameter(lp_ctx, "ldap passwd sync", "no");
2876
2877         lpcfg_do_global_parameter(lp_ctx, "kernel change notify", "yes");
2878
2879         lpcfg_do_global_parameter(lp_ctx, "max ttl", "259200");
2880
2881         lpcfg_do_global_parameter(lp_ctx, "blocking locks", "yes");
2882
2883         lpcfg_do_global_parameter(lp_ctx, "load printers", "yes");
2884
2885         lpcfg_do_global_parameter(lp_ctx, "idmap cache time", "604800");
2886
2887         lpcfg_do_global_parameter(lp_ctx, "preserve case", "yes");
2888
2889         lpcfg_do_global_parameter(lp_ctx, "lm announce", "auto");
2890
2891         lpcfg_do_global_parameter(lp_ctx, "afs token lifetime", "604800");
2892
2893         lpcfg_do_global_parameter(lp_ctx, "enable core files", "yes");
2894
2895         lpcfg_do_global_parameter(lp_ctx, "winbind max domain connections", "1");
2896
2897         lpcfg_do_global_parameter(lp_ctx, "case sensitive", "auto");
2898
2899         lpcfg_do_global_parameter(lp_ctx, "ldap timeout", "15");
2900
2901         lpcfg_do_global_parameter(lp_ctx, "mangle prefix", "1");
2902
2903         lpcfg_do_global_parameter(lp_ctx, "posix locking", "yes");
2904
2905         lpcfg_do_global_parameter(lp_ctx, "lock spin time", "200");
2906
2907         lpcfg_do_global_parameter(lp_ctx, "directory name cache size", "100");
2908
2909         lpcfg_do_global_parameter(lp_ctx, "nmbd bind explicit broadcast", "yes");
2910
2911         lpcfg_do_global_parameter(lp_ctx, "init logon delay", "100");
2912
2913         lpcfg_do_global_parameter(lp_ctx, "usershare owner only", "yes");
2914
2915         lpcfg_do_global_parameter(lp_ctx, "-valid", "yes");
2916
2917         lpcfg_do_global_parameter_var(lp_ctx, "usershare path", "%s/usershares", get_dyn_STATEDIR());
2918
2919 #ifdef DEVELOPER
2920         lpcfg_do_global_parameter_var(lp_ctx, "panic action", "/bin/sleep 999999999");
2921 #endif
2922
2923         lpcfg_do_global_parameter(lp_ctx, "smb passwd file", get_dyn_SMB_PASSWD_FILE());
2924
2925         lpcfg_do_global_parameter(lp_ctx, "logon home", "\\\\%N\\%U");
2926
2927         lpcfg_do_global_parameter(lp_ctx, "logon path", "\\\\%N\\%U\\profile");
2928
2929         lpcfg_do_global_parameter(lp_ctx, "printjob username", "%U");
2930
2931         lpcfg_do_global_parameter(lp_ctx, "aio max threads", "100");
2932
2933         lpcfg_do_global_parameter(lp_ctx, "smb2 leases", "yes");
2934
2935         lpcfg_do_global_parameter(lp_ctx, "server multi channel support", "yes");
2936
2937         lpcfg_do_global_parameter(lp_ctx, "kerberos encryption types", "all");
2938
2939         lpcfg_do_global_parameter(lp_ctx,
2940                                   "rpc server dynamic port range",
2941                                   "49152-65535");
2942
2943         lpcfg_do_global_parameter(lp_ctx, "prefork children", "4");
2944         lpcfg_do_global_parameter(lp_ctx, "prefork backoff increment", "10");
2945         lpcfg_do_global_parameter(lp_ctx, "prefork maximum backoff", "120");
2946
2947         lpcfg_do_global_parameter(lp_ctx, "check parent directory delete on close", "no");
2948
2949         lpcfg_do_global_parameter(lp_ctx, "ea support", "yes");
2950
2951         lpcfg_do_global_parameter(lp_ctx, "store dos attributes", "yes");
2952
2953         lpcfg_do_global_parameter(lp_ctx, "debug encryption", "no");
2954
2955         lpcfg_do_global_parameter(lp_ctx, "spotlight backend", "noindex");
2956
2957         lpcfg_do_global_parameter(
2958                 lp_ctx, "ldap max anonymous request size", "256000");
2959         lpcfg_do_global_parameter(
2960                 lp_ctx, "ldap max authenticated request size", "16777216");
2961         lpcfg_do_global_parameter(
2962                 lp_ctx, "ldap max search request size", "256000");
2963
2964         /* Async DNS query timeout in seconds. */
2965         lpcfg_do_global_parameter(lp_ctx, "async dns timeout", "10");
2966
2967         lpcfg_do_global_parameter(lp_ctx,
2968                                   "client smb encrypt",
2969                                   "default");
2970
2971         lpcfg_do_global_parameter(lp_ctx,
2972                                   "client use kerberos",
2973                                   "desired");
2974
2975         lpcfg_do_global_parameter(lp_ctx,
2976                                   "client protection",
2977                                   "default");
2978
2979         lpcfg_do_global_parameter(lp_ctx,
2980                                   "smbd max xattr size",
2981                                   "65536");
2982
2983         lpcfg_do_global_parameter(lp_ctx,
2984                                   "acl flag inherited canonicalization",
2985                                   "yes");
2986
2987         lpcfg_do_global_parameter(lp_ctx,
2988                                   "winbind use krb5 enterprise principals",
2989                                   "yes");
2990
2991         lpcfg_do_global_parameter(lp_ctx,
2992                                   "client smb3 signing algorithms",
2993                                   DEFAULT_SMB3_SIGNING_ALGORITHMS);
2994         lpcfg_do_global_parameter(lp_ctx,
2995                                   "server smb3 signing algorithms",
2996                                   DEFAULT_SMB3_SIGNING_ALGORITHMS);
2997
2998         lpcfg_do_global_parameter(lp_ctx,
2999                                   "client smb3 encryption algorithms",
3000                                   DEFAULT_SMB3_ENCRYPTION_ALGORITHMS);
3001         lpcfg_do_global_parameter(lp_ctx,
3002                                   "server smb3 encryption algorithms",
3003                                   DEFAULT_SMB3_ENCRYPTION_ALGORITHMS);
3004
3005         lpcfg_do_global_parameter(lp_ctx,
3006                                   "min domain uid",
3007                                   "1000");
3008
3009         lpcfg_do_global_parameter(lp_ctx,
3010                                   "rpc start on demand helpers",
3011                                   "yes");
3012
3013         for (i = 0; parm_table[i].label; i++) {
3014                 if (!(lp_ctx->flags[i] & FLAG_CMDLINE)) {
3015                         lp_ctx->flags[i] |= FLAG_DEFAULT;
3016                 }
3017         }
3018
3019         for (parm=lp_ctx->globals->param_opt; parm; parm=parm->next) {
3020                 if (!(parm->priority & FLAG_CMDLINE)) {
3021                         parm->priority |= FLAG_DEFAULT;
3022                 }
3023         }
3024
3025         for (parm=lp_ctx->sDefault->param_opt; parm; parm=parm->next) {
3026                 if (!(parm->priority & FLAG_CMDLINE)) {
3027                         parm->priority |= FLAG_DEFAULT;
3028                 }
3029         }
3030
3031         return lp_ctx;
3032 }
3033
3034 /**
3035  * Initialise the global parameter structure.
3036  */
3037 struct loadparm_context *loadparm_init_global(bool load_default)
3038 {
3039         if (global_loadparm_context == NULL) {
3040                 global_loadparm_context = loadparm_init(NULL);
3041         }
3042         if (global_loadparm_context == NULL) {
3043                 return NULL;
3044         }
3045         global_loadparm_context->global = true;
3046         if (load_default && !global_loadparm_context->loaded) {
3047                 lpcfg_load_default(global_loadparm_context);
3048         }
3049         global_loadparm_context->refuse_free = true;
3050         return global_loadparm_context;
3051 }
3052
3053 /**
3054  * Initialise the global parameter structure.
3055  */
3056 struct loadparm_context *loadparm_init_s3(TALLOC_CTX *mem_ctx, 
3057                                           const struct loadparm_s3_helpers *s3_fns)
3058 {
3059         struct loadparm_context *loadparm_context = talloc_zero(mem_ctx, struct loadparm_context);
3060         if (!loadparm_context) {
3061                 return NULL;
3062         }
3063         loadparm_context->s3_fns = s3_fns;
3064         loadparm_context->globals = s3_fns->globals;
3065         loadparm_context->flags = s3_fns->flags;
3066
3067         return loadparm_context;
3068 }
3069
3070 const char *lpcfg_configfile(struct loadparm_context *lp_ctx)
3071 {
3072         return lp_ctx->szConfigFile;
3073 }
3074
3075 const char *lp_default_path(void)
3076 {
3077     if (getenv("SMB_CONF_PATH"))
3078         return getenv("SMB_CONF_PATH");
3079     else
3080         return dyn_CONFIGFILE;
3081 }
3082
3083 /**
3084  * Update the internal state of a loadparm context after settings 
3085  * have changed.
3086  */
3087 static bool lpcfg_update(struct loadparm_context *lp_ctx)
3088 {
3089         struct debug_settings settings;
3090         int max_protocol, min_protocol;
3091         TALLOC_CTX *tmp_ctx;
3092         const struct loadparm_substitution *lp_sub =
3093                 lpcfg_noop_substitution();
3094
3095         tmp_ctx = talloc_new(lp_ctx);
3096         if (tmp_ctx == NULL) {
3097                 return false;
3098         }
3099
3100         lpcfg_add_auto_services(lp_ctx, lpcfg_auto_services(lp_ctx, lp_sub, tmp_ctx));
3101
3102         if (!lp_ctx->globals->wins_server_list && lp_ctx->globals->we_are_a_wins_server) {
3103                 lpcfg_do_global_parameter(lp_ctx, "wins server", "127.0.0.1");
3104         }
3105
3106         if (!lp_ctx->global) {
3107                 TALLOC_FREE(tmp_ctx);
3108                 return true;
3109         }
3110
3111         panic_action = lp_ctx->globals->panic_action;
3112
3113         reload_charcnv(lp_ctx);
3114
3115         ZERO_STRUCT(settings);
3116         /* Add any more debug-related smb.conf parameters created in
3117          * future here */
3118         settings.timestamp_logs = lp_ctx->globals->timestamp_logs;
3119         settings.debug_prefix_timestamp = lp_ctx->globals->debug_prefix_timestamp;
3120         settings.debug_hires_timestamp = lp_ctx->globals->debug_hires_timestamp;
3121         settings.debug_syslog_format = lp_ctx->globals->debug_syslog_format;
3122         settings.debug_pid = lp_ctx->globals->debug_pid;
3123         settings.debug_uid = lp_ctx->globals->debug_uid;
3124         settings.debug_class = lp_ctx->globals->debug_class;
3125         settings.max_log_size = lp_ctx->globals->max_log_size;
3126         debug_set_settings(&settings, lp_ctx->globals->logging,
3127                            lp_ctx->globals->syslog,
3128                            lp_ctx->globals->syslog_only);
3129
3130         /* FIXME: This is a bit of a hack, but we can't use a global, since 
3131          * not everything that uses lp also uses the socket library */
3132         if (lpcfg_parm_bool(lp_ctx, NULL, "socket", "testnonblock", false)) {
3133                 setenv("SOCKET_TESTNONBLOCK", "1", 1);
3134         } else {
3135                 unsetenv("SOCKET_TESTNONBLOCK");
3136         }
3137
3138         /* Check if command line max protocol < min protocol, if so
3139          * report a warning to the user.
3140          */
3141         max_protocol = lpcfg_client_max_protocol(lp_ctx);
3142         min_protocol = lpcfg_client_min_protocol(lp_ctx);
3143         if (lpcfg_client_max_protocol(lp_ctx) < lpcfg_client_min_protocol(lp_ctx)) {
3144                 const char *max_protocolp, *min_protocolp;
3145                 max_protocolp = lpcfg_get_smb_protocol(max_protocol);
3146                 min_protocolp = lpcfg_get_smb_protocol(min_protocol);
3147                 DBG_ERR("Max protocol %s is less than min protocol %s.\n",
3148                         max_protocolp, min_protocolp);
3149         }
3150
3151         TALLOC_FREE(tmp_ctx);
3152         return true;
3153 }
3154
3155 bool lpcfg_load_default(struct loadparm_context *lp_ctx)
3156 {
3157     const char *path;
3158
3159     path = lp_default_path();
3160
3161     if (!file_exist(path)) {
3162             /* We allow the default smb.conf file to not exist, 
3163              * basically the equivalent of an empty file. */
3164             return lpcfg_update(lp_ctx);
3165     }
3166
3167     return lpcfg_load(lp_ctx, path);
3168 }
3169
3170 /**
3171  * Load the services array from the services file.
3172  *
3173  * Return True on success, False on failure.
3174  */
3175 static bool lpcfg_load_internal(struct loadparm_context *lp_ctx,
3176                                 const char *filename, bool set_global)
3177 {
3178         char *n2;
3179         bool bRetval;
3180
3181         if (lp_ctx->szConfigFile != NULL) {
3182                 talloc_free(discard_const_p(char, lp_ctx->szConfigFile));
3183                 lp_ctx->szConfigFile = NULL;
3184         }
3185
3186         lp_ctx->szConfigFile = talloc_strdup(lp_ctx, filename);
3187
3188         if (lp_ctx->s3_fns) {
3189                 return lp_ctx->s3_fns->load(filename);
3190         }
3191
3192         lp_ctx->bInGlobalSection = true;
3193         n2 = standard_sub_basic(lp_ctx, lp_ctx->szConfigFile);
3194         DEBUG(2, ("lpcfg_load: refreshing parameters from %s\n", n2));
3195
3196         add_to_file_list(lp_ctx, &lp_ctx->file_lists, lp_ctx->szConfigFile, n2);
3197
3198         /* We get sections first, so have to start 'behind' to make up */
3199         lp_ctx->currentService = NULL;
3200         bRetval = pm_process(n2, do_section, lpcfg_do_parameter, lp_ctx);
3201
3202         /* finish up the last section */
3203         DEBUG(4, ("pm_process() returned %s\n", BOOLSTR(bRetval)));
3204         if (bRetval)
3205                 if (lp_ctx->currentService != NULL)
3206                         bRetval = lpcfg_service_ok(lp_ctx->currentService);
3207
3208         bRetval = bRetval && lpcfg_update(lp_ctx);
3209
3210         /* we do this unconditionally, so that it happens even
3211            for a missing smb.conf */
3212         reload_charcnv(lp_ctx);
3213
3214         if (bRetval == true && set_global) {
3215                 /* set this up so that any child python tasks will
3216                    find the right smb.conf */
3217                 setenv("SMB_CONF_PATH", filename, 1);
3218
3219                 /* set the context used by the lp_*() function
3220                    varients */
3221                 global_loadparm_context = lp_ctx;
3222                 lp_ctx->loaded = true;
3223         }
3224
3225         return bRetval;
3226 }
3227
3228 bool lpcfg_load_no_global(struct loadparm_context *lp_ctx, const char *filename)
3229 {
3230     return lpcfg_load_internal(lp_ctx, filename, false);
3231 }
3232
3233 bool lpcfg_load(struct loadparm_context *lp_ctx, const char *filename)
3234 {
3235     return lpcfg_load_internal(lp_ctx, filename, true);
3236 }
3237
3238 /**
3239  * Return the max number of services.
3240  */
3241
3242 int lpcfg_numservices(struct loadparm_context *lp_ctx)
3243 {
3244         if (lp_ctx->s3_fns) {
3245                 return lp_ctx->s3_fns->get_numservices();
3246         }
3247
3248         return lp_ctx->iNumServices;
3249 }
3250
3251 /**
3252  * Display the contents of the services array in human-readable form.
3253  */
3254
3255 void lpcfg_dump(struct loadparm_context *lp_ctx, FILE *f, bool show_defaults,
3256              int maxtoprint)
3257 {
3258         int iService;
3259
3260         if (lp_ctx->s3_fns) {
3261                 lp_ctx->s3_fns->dump(f, show_defaults, maxtoprint);
3262                 return;
3263         }
3264
3265         lpcfg_dump_globals(lp_ctx, f, show_defaults);
3266
3267         lpcfg_dump_a_service(lp_ctx->sDefault, lp_ctx->sDefault, f, lp_ctx->flags, show_defaults);
3268
3269         for (iService = 0; iService < maxtoprint; iService++)
3270                 lpcfg_dump_one(f, show_defaults, lp_ctx->services[iService], lp_ctx->sDefault);
3271 }
3272
3273 /**
3274  * Display the contents of one service in human-readable form.
3275  */
3276 void lpcfg_dump_one(FILE *f, bool show_defaults, struct loadparm_service *service, struct loadparm_service *sDefault)
3277 {
3278         if (service != NULL) {
3279                 if (service->szService[0] == '\0')
3280                         return;
3281                 lpcfg_dump_a_service(service, sDefault, f, NULL, show_defaults);
3282         }
3283 }
3284
3285 struct loadparm_service *lpcfg_servicebynum(struct loadparm_context *lp_ctx,
3286                                             int snum)
3287 {
3288         if (lp_ctx->s3_fns) {
3289                 return lp_ctx->s3_fns->get_servicebynum(snum);
3290         }
3291
3292         return lp_ctx->services[snum];
3293 }
3294
3295 struct loadparm_service *lpcfg_service(struct loadparm_context *lp_ctx,
3296                                     const char *service_name)
3297 {
3298         int iService;
3299         char *serviceName;
3300
3301         if (lp_ctx->s3_fns) {
3302                 return lp_ctx->s3_fns->get_service(service_name);
3303         }
3304
3305         for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--) {
3306                 if (lp_ctx->services[iService] &&
3307                     lp_ctx->services[iService]->szService) {
3308                         /*
3309                          * The substitution here is used to support %U is
3310                          * service names
3311                          */
3312                         serviceName = standard_sub_basic(
3313                                         lp_ctx->services[iService],
3314                                         lp_ctx->services[iService]->szService);
3315                         if (strequal(serviceName, service_name)) {
3316                                 talloc_free(serviceName);
3317                                 return lp_ctx->services[iService];
3318                         }
3319                         talloc_free(serviceName);
3320                 }
3321         }
3322
3323         DEBUG(7,("lpcfg_servicenumber: couldn't find %s\n", service_name));
3324         return NULL;
3325 }
3326
3327 const char *lpcfg_servicename(const struct loadparm_service *service)
3328 {
3329         return service ? lpcfg_string((const char *)service->szService) : NULL;
3330 }
3331
3332 struct smb_iconv_handle *lpcfg_iconv_handle(struct loadparm_context *lp_ctx)
3333 {
3334         if (lp_ctx == NULL) {
3335                 return get_iconv_handle();
3336         }
3337         return lp_ctx->iconv_handle;
3338 }
3339
3340 _PUBLIC_ void reload_charcnv(struct loadparm_context *lp_ctx)
3341 {
3342         if (!lp_ctx->global) {
3343                 return;
3344         }
3345
3346         lp_ctx->iconv_handle =
3347                 reinit_iconv_handle(lp_ctx,
3348                                     lpcfg_dos_charset(lp_ctx),
3349                                     lpcfg_unix_charset(lp_ctx));
3350         if (lp_ctx->iconv_handle == NULL) {
3351                 smb_panic("reinit_iconv_handle failed");
3352         }
3353 }
3354
3355 _PUBLIC_ char *lpcfg_tls_keyfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3356 {
3357         return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_keyfile(lp_ctx));
3358 }
3359
3360 _PUBLIC_ char *lpcfg_tls_certfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3361 {
3362         return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_certfile(lp_ctx));
3363 }
3364
3365 _PUBLIC_ char *lpcfg_tls_cafile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3366 {
3367         return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_cafile(lp_ctx));
3368 }
3369
3370 _PUBLIC_ char *lpcfg_tls_crlfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3371 {
3372         return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_crlfile(lp_ctx));
3373 }
3374
3375 _PUBLIC_ char *lpcfg_tls_dhpfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3376 {
3377         return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_dhpfile(lp_ctx));
3378 }
3379
3380 struct gensec_settings *lpcfg_gensec_settings(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3381 {
3382         struct gensec_settings *settings = talloc_zero(mem_ctx, struct gensec_settings);
3383         if (settings == NULL)
3384                 return NULL;
3385         SMB_ASSERT(lp_ctx != NULL);
3386         settings->lp_ctx = talloc_reference(settings, lp_ctx);
3387         settings->target_hostname = lpcfg_parm_string(lp_ctx, NULL, "gensec", "target_hostname");
3388         return settings;
3389 }
3390
3391 int lpcfg_server_role(struct loadparm_context *lp_ctx)
3392 {
3393         int domain_master = lpcfg__domain_master(lp_ctx);
3394
3395         return lp_find_server_role(lpcfg__server_role(lp_ctx),
3396                                    lpcfg__security(lp_ctx),
3397                                    lpcfg__domain_logons(lp_ctx),
3398                                    (domain_master == true) ||
3399                                    (domain_master == Auto));
3400 }
3401
3402 int lpcfg_security(struct loadparm_context *lp_ctx)
3403 {
3404         return lp_find_security(lpcfg__server_role(lp_ctx),
3405                                 lpcfg__security(lp_ctx));
3406 }
3407
3408 int lpcfg_client_max_protocol(struct loadparm_context *lp_ctx)
3409 {
3410         int client_max_protocol = lpcfg__client_max_protocol(lp_ctx);
3411         if (client_max_protocol == PROTOCOL_DEFAULT) {
3412                 return PROTOCOL_LATEST;
3413         }
3414         return client_max_protocol;
3415 }
3416
3417 int lpcfg_client_ipc_min_protocol(struct loadparm_context *lp_ctx)
3418 {
3419         int client_ipc_min_protocol = lpcfg__client_ipc_min_protocol(lp_ctx);
3420         if (client_ipc_min_protocol == PROTOCOL_DEFAULT) {
3421                 client_ipc_min_protocol = lpcfg_client_min_protocol(lp_ctx);
3422         }
3423         if (client_ipc_min_protocol < PROTOCOL_NT1) {
3424                 return PROTOCOL_NT1;
3425         }
3426         return client_ipc_min_protocol;
3427 }
3428
3429 int lpcfg_client_ipc_max_protocol(struct loadparm_context *lp_ctx)
3430 {
3431         int client_ipc_max_protocol = lpcfg__client_ipc_max_protocol(lp_ctx);
3432         if (client_ipc_max_protocol == PROTOCOL_DEFAULT) {
3433                 return PROTOCOL_LATEST;
3434         }
3435         if (client_ipc_max_protocol < PROTOCOL_NT1) {
3436                 return PROTOCOL_NT1;
3437         }
3438         return client_ipc_max_protocol;
3439 }
3440
3441 int lpcfg_client_ipc_signing(struct loadparm_context *lp_ctx)
3442 {
3443         int client_ipc_signing = lpcfg__client_ipc_signing(lp_ctx);
3444         if (client_ipc_signing == SMB_SIGNING_DEFAULT) {
3445                 return SMB_SIGNING_REQUIRED;
3446         }
3447         return client_ipc_signing;
3448 }
3449
3450 enum credentials_use_kerberos lpcfg_client_use_kerberos(struct loadparm_context *lp_ctx)
3451 {
3452         if (lpcfg_weak_crypto(lp_ctx) == SAMBA_WEAK_CRYPTO_DISALLOWED) {
3453                 return CRED_USE_KERBEROS_REQUIRED;
3454         }
3455
3456         return lpcfg__client_use_kerberos(lp_ctx);
3457 }
3458
3459 bool lpcfg_server_signing_allowed(struct loadparm_context *lp_ctx, bool *mandatory)
3460 {
3461         bool allowed = true;
3462         enum smb_signing_setting signing_setting = lpcfg_server_signing(lp_ctx);
3463
3464         *mandatory = false;
3465
3466         if (signing_setting == SMB_SIGNING_DEFAULT) {
3467                 /*
3468                  * If we are a domain controller, SMB signing is
3469                  * really important, as it can prevent a number of
3470                  * attacks on communications between us and the
3471                  * clients
3472                  *
3473                  * However, it really sucks (no sendfile, CPU
3474                  * overhead) performance-wise when used on a
3475                  * file server, so disable it by default
3476                  * on non-DCs
3477                  */
3478
3479                 if (lpcfg_server_role(lp_ctx) >= ROLE_ACTIVE_DIRECTORY_DC) {
3480                         signing_setting = SMB_SIGNING_REQUIRED;
3481                 } else {
3482                         signing_setting = SMB_SIGNING_OFF;
3483                 }
3484         }
3485
3486         switch (signing_setting) {
3487         case SMB_SIGNING_REQUIRED:
3488                 *mandatory = true;
3489                 break;
3490         case SMB_SIGNING_DESIRED:
3491         case SMB_SIGNING_IF_REQUIRED:
3492                 break;
3493         case SMB_SIGNING_OFF:
3494                 allowed = false;
3495                 break;
3496         case SMB_SIGNING_DEFAULT:
3497         case SMB_SIGNING_IPC_DEFAULT:
3498                 smb_panic(__location__);
3499                 break;
3500         }
3501
3502         return allowed;
3503 }
3504
3505 int lpcfg_tdb_hash_size(struct loadparm_context *lp_ctx, const char *name)
3506 {
3507         const char *base;
3508
3509         if (name == NULL) {
3510                 return 0;
3511         }
3512
3513         base = strrchr_m(name, '/');
3514         if (base != NULL) {
3515                 base += 1;
3516         } else {
3517                 base = name;
3518         }
3519         return lpcfg_parm_int(lp_ctx, NULL, "tdb_hashsize", base, 0);
3520
3521 }
3522
3523 int lpcfg_tdb_flags(struct loadparm_context *lp_ctx, int tdb_flags)
3524 {
3525         if (!lpcfg_use_mmap(lp_ctx)) {
3526                 tdb_flags |= TDB_NOMMAP;
3527         }
3528         return tdb_flags;
3529 }
3530
3531 /*
3532  * Do not allow LanMan auth if unless NTLMv1 is also allowed
3533  *
3534  * This also ensures it is disabled if NTLM is totally disabled
3535  */
3536 bool lpcfg_lanman_auth(struct loadparm_context *lp_ctx)
3537 {
3538         enum ntlm_auth_level ntlm_auth_level = lpcfg_ntlm_auth(lp_ctx);
3539
3540         if (ntlm_auth_level == NTLM_AUTH_ON) {
3541                 return lpcfg__lanman_auth(lp_ctx);
3542         } else {
3543                 return false;
3544         }
3545 }
3546
3547 static char *lpcfg_noop_substitution_fn(
3548                         TALLOC_CTX *mem_ctx,
3549                         const struct loadparm_substitution *lp_sub,
3550                         const char *raw_value,
3551                         void *private_data)
3552 {
3553         return talloc_strdup(mem_ctx, raw_value);
3554 }
3555
3556 static const struct loadparm_substitution global_noop_substitution = {
3557         .substituted_string_fn = lpcfg_noop_substitution_fn,
3558 };
3559
3560 const struct loadparm_substitution *lpcfg_noop_substitution(void)
3561 {
3562         return &global_noop_substitution;
3563 }
3564
3565 char *lpcfg_substituted_string(TALLOC_CTX *mem_ctx,
3566                                const struct loadparm_substitution *lp_sub,
3567                                const char *raw_value)
3568 {
3569         return lp_sub->substituted_string_fn(mem_ctx,
3570                                              lp_sub,
3571                                              raw_value,
3572                                              lp_sub->private_data);
3573 }
3574
3575 /**
3576  * @brief Parse a string value of a given parameter to its integer enum value.
3577  *
3578  * @param[in]  param_name    The parameter name (e.g. 'client smb encrypt')
3579  *
3580  * @param[in]  param_value   The parameter value (e.g. 'required').
3581  *
3582  * @return The integer value of the enum the param_value matches or INT32_MIN
3583  * on error.
3584  */
3585 int32_t lpcfg_parse_enum_vals(const char *param_name,
3586                               const char *param_value)
3587 {
3588         struct parm_struct *parm = NULL;
3589         int32_t ret = INT32_MIN;
3590         bool ok;
3591
3592         parm = lpcfg_parm_struct(NULL, param_name);
3593         if (parm == NULL) {
3594                 return INT32_MIN;
3595         }
3596
3597         ok = lp_set_enum_parm(parm, param_value, &ret);
3598         if (!ok) {
3599                 return INT32_MIN;
3600         }
3601
3602         return ret;
3603 }