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