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