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