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