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