lib/param: Allow enum values to also be white-space insentive in comparison
[samba.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
71 #define standard_sub_basic talloc_strdup
72
73 #include "lib/param/param_global.h"
74
75 struct loadparm_service *lpcfg_default_service(struct loadparm_context *lp_ctx)
76 {
77         return lp_ctx->sDefault;
78 }
79
80 /**
81  * Convenience routine to grab string parameters into temporary memory
82  * and run standard_sub_basic on them.
83  *
84  * The buffers can be written to by
85  * callers without affecting the source string.
86  */
87
88 static const char *lpcfg_string(const char *s)
89 {
90 #if 0  /* until REWRITE done to make thread-safe */
91         size_t len = s ? strlen(s) : 0;
92         char *ret;
93 #endif
94
95         /* The follow debug is useful for tracking down memory problems
96            especially if you have an inner loop that is calling a lp_*()
97            function that returns a string.  Perhaps this debug should be
98            present all the time? */
99
100 #if 0
101         DEBUG(10, ("lpcfg_string(%s)\n", s));
102 #endif
103
104 #if 0  /* until REWRITE done to make thread-safe */
105         if (!lp_talloc)
106                 lp_talloc = talloc_init("lp_talloc");
107
108         ret = talloc_array(lp_talloc, char, len + 100); /* leave room for substitution */
109
110         if (!ret)
111                 return NULL;
112
113         if (!s)
114                 *ret = 0;
115         else
116                 strlcpy(ret, s, len);
117
118         if (trim_string(ret, "\"", "\"")) {
119                 if (strchr(ret,'"') != NULL)
120                         strlcpy(ret, s, len);
121         }
122
123         standard_sub_basic(ret,len+100);
124         return (ret);
125 #endif
126         return s;
127 }
128
129 /*
130    In this section all the functions that are used to access the
131    parameters from the rest of the program are defined
132 */
133
134 /*
135  * the creation of separate lpcfg_*() and lp_*() functions is to allow
136  * for code compatibility between existing Samba4 and Samba3 code.
137  */
138
139 /* this global context supports the lp_*() function varients */
140 static struct loadparm_context *global_loadparm_context;
141
142 #define lpcfg_default_service global_loadparm_context->sDefault
143 #define lpcfg_global_service(i) global_loadparm_context->services[i]
144
145 #define FN_GLOBAL_STRING(fn_name,var_name) \
146  _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx, TALLOC_CTX *ctx) {\
147          if (lp_ctx == NULL) return NULL;                               \
148          if (lp_ctx->s3_fns) {                                          \
149                  return lp_ctx->globals->var_name ? lp_ctx->s3_fns->lp_string(ctx, lp_ctx->globals->var_name) : talloc_strdup(ctx, ""); \
150          }                                                              \
151          return lp_ctx->globals->var_name ? talloc_strdup(ctx, lpcfg_string(lp_ctx->globals->var_name)) : talloc_strdup(ctx, ""); \
152 }
153
154 #define FN_GLOBAL_CONST_STRING(fn_name,var_name)                                \
155  _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
156         if (lp_ctx == NULL) return NULL;                                \
157         return lp_ctx->globals->var_name ? lpcfg_string(lp_ctx->globals->var_name) : ""; \
158 }
159
160 #define FN_GLOBAL_LIST(fn_name,var_name)                                \
161  _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
162          if (lp_ctx == NULL) return NULL;                               \
163          return lp_ctx->globals->var_name;                              \
164  }
165
166 #define FN_GLOBAL_BOOL(fn_name,var_name) \
167  _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) {\
168          if (lp_ctx == NULL) return false;                              \
169          return lp_ctx->globals->var_name;                              \
170 }
171
172 #define FN_GLOBAL_INTEGER(fn_name,var_name) \
173  _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
174          return lp_ctx->globals->var_name;                              \
175  }
176
177 /* Local parameters don't need the ->s3_fns because the struct
178  * loadparm_service is shared and lpcfg_service() checks the ->s3_fns
179  * hook */
180 #define FN_LOCAL_STRING(fn_name,val) \
181  _PUBLIC_ char *lpcfg_ ## fn_name(struct loadparm_service *service, \
182                                         struct loadparm_service *sDefault, TALLOC_CTX *ctx) { \
183          return(talloc_strdup(ctx, lpcfg_string((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)))); \
184  }
185
186 #define FN_LOCAL_CONST_STRING(fn_name,val) \
187  _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_service *service, \
188                                         struct loadparm_service *sDefault) { \
189          return((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val)); \
190  }
191
192 #define FN_LOCAL_LIST(fn_name,val) \
193  _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_service *service, \
194                                          struct loadparm_service *sDefault) {\
195          return(const char **)(service != NULL && service->val != NULL? service->val : sDefault->val); \
196  }
197
198 #define FN_LOCAL_PARM_BOOL(fn_name, val) FN_LOCAL_BOOL(fn_name, val)
199
200 #define FN_LOCAL_BOOL(fn_name,val) \
201  _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_service *service, \
202                                  struct loadparm_service *sDefault) {   \
203          return((service != NULL)? service->val : sDefault->val); \
204  }
205
206 #define FN_LOCAL_INTEGER(fn_name,val) \
207  _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_service *service, \
208                                 struct loadparm_service *sDefault) {    \
209          return((service != NULL)? service->val : sDefault->val); \
210  }
211
212 #define FN_LOCAL_PARM_INTEGER(fn_name, val) FN_LOCAL_INTEGER(fn_name, val)
213
214 #define FN_LOCAL_PARM_CHAR(fn_name,val) \
215  _PUBLIC_ char lpcfg_ ## fn_name(struct loadparm_service *service, \
216                                 struct loadparm_service *sDefault) {    \
217          return((service != NULL)? service->val : sDefault->val); \
218  }
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         }
1341
1342         if(!set_variable_helper(lp_ctx->globals->ctx, parm_num, ptr, "smb ports",
1343                                 pszParmValue)) {
1344                 return false;
1345         }
1346
1347         list = lp_ctx->globals->smb_ports;
1348         if (list == NULL) {
1349                 return false;
1350         }
1351
1352         /* Check that each port is a valid integer and within range */
1353         for (i = 0; list[i] != NULL; i++) {
1354                 char *end = NULL;
1355                 int port = 0;
1356                 port = strtol(list[i], &end, 10);
1357                 if (*end != '\0' || port <= 0 || port > 65535) {
1358                         TALLOC_FREE(list);
1359                         return false;
1360                 }
1361         }
1362
1363         return true;
1364 }
1365
1366 /***************************************************************************
1367  Initialise a copymap.
1368 ***************************************************************************/
1369
1370 /**
1371  * Initializes service copymap
1372  * Note: pservice *must* be valid TALLOC_CTX
1373  */
1374 void init_copymap(struct loadparm_service *pservice)
1375 {
1376         int i;
1377
1378         TALLOC_FREE(pservice->copymap);
1379
1380         pservice->copymap = bitmap_talloc(pservice, num_parameters());
1381         if (!pservice->copymap) {
1382                 DEBUG(0,
1383                       ("Couldn't allocate copymap!! (size %d)\n",
1384                        (int)num_parameters()));
1385         } else {
1386                 for (i = 0; i < num_parameters(); i++) {
1387                         bitmap_set(pservice->copymap, i);
1388                 }
1389         }
1390 }
1391
1392 /**
1393  * Process a parametric option
1394  */
1395 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
1396                                        struct loadparm_service *service,
1397                                        const char *pszParmName,
1398                                        const char *pszParmValue, int flags)
1399 {
1400         struct parmlist_entry **data;
1401         char *name;
1402         TALLOC_CTX *mem_ctx;
1403
1404         while (isspace((unsigned char)*pszParmName)) {
1405                 pszParmName++;
1406         }
1407
1408         name = strlower_talloc(lp_ctx, pszParmName);
1409         if (!name) return false;
1410
1411         if (service == NULL) {
1412                 data = &lp_ctx->globals->param_opt;
1413                 /**
1414                  * s3 code cannot deal with parametric options stored on the globals ctx.
1415                  */
1416                 if (lp_ctx->s3_fns != NULL) {
1417                         mem_ctx = NULL;
1418                 } else {
1419                         mem_ctx = lp_ctx->globals->ctx;
1420                 }
1421         } else {
1422                 data = &service->param_opt;
1423                 mem_ctx = service;
1424         }
1425
1426         set_param_opt(mem_ctx, data, name, pszParmValue, flags);
1427
1428         talloc_free(name);
1429
1430         return true;
1431 }
1432
1433 static bool set_variable_helper(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
1434                          const char *pszParmName, const char *pszParmValue)
1435 {
1436         int i;
1437
1438         /* switch on the type of variable it is */
1439         switch (parm_table[parmnum].type)
1440         {
1441                 case P_BOOL: {
1442                         bool b;
1443                         if (!set_boolean(pszParmValue, &b)) {
1444                                 DEBUG(0, ("set_variable_helper(%s): value is not "
1445                                           "boolean!\n", pszParmValue));
1446                                 return false;
1447                         }
1448                         *(bool *)parm_ptr = b;
1449                         }
1450                         break;
1451
1452                 case P_BOOLREV: {
1453                         bool b;
1454                         if (!set_boolean(pszParmValue, &b)) {
1455                                 DEBUG(0, ("set_variable_helper(%s): value is not "
1456                                           "boolean!\n", pszParmValue));
1457                                 return false;
1458                         }
1459                         *(bool *)parm_ptr = !b;
1460                         }
1461                         break;
1462
1463                 case P_INTEGER:
1464                         *(int *)parm_ptr = lp_int(pszParmValue);
1465                         break;
1466
1467                 case P_CHAR:
1468                         *(char *)parm_ptr = *pszParmValue;
1469                         break;
1470
1471                 case P_OCTAL:
1472                         i = sscanf(pszParmValue, "%o", (int *)parm_ptr);
1473                         if ( i != 1 ) {
1474                                 DEBUG ( 0, ("Invalid octal number %s\n", pszParmName ));
1475                                 return false;
1476                         }
1477                         break;
1478
1479                 case P_BYTES:
1480                 {
1481                         uint64_t val;
1482                         if (conv_str_size_error(pszParmValue, &val)) {
1483                                 if (val <= INT_MAX) {
1484                                         *(int *)parm_ptr = (int)val;
1485                                         break;
1486                                 }
1487                         }
1488
1489                         DEBUG(0, ("set_variable_helper(%s): value is not "
1490                                   "a valid size specifier!\n", pszParmValue));
1491                         return false;
1492                 }
1493
1494                 case P_CMDLIST:
1495                         TALLOC_FREE(*(char ***)parm_ptr);
1496                         *(char ***)parm_ptr = str_list_make_v3(mem_ctx,
1497                                                         pszParmValue, NULL);
1498                         break;
1499
1500                 case P_LIST:
1501                 {
1502                         char **new_list = str_list_make_v3(mem_ctx,
1503                                                         pszParmValue, NULL);
1504                         if (new_list == NULL) {
1505                                 break;
1506                         }
1507
1508                         for (i=0; new_list[i]; i++) {
1509                                 if (*(const char ***)parm_ptr != NULL &&
1510                                     new_list[i][0] == '+' &&
1511                                     new_list[i][1])
1512                                 {
1513                                         if (!str_list_check(*(const char ***)parm_ptr,
1514                                                             &new_list[i][1])) {
1515                                                 *(const char ***)parm_ptr = str_list_add(*(const char ***)parm_ptr,
1516                                                                                          &new_list[i][1]);
1517                                         }
1518                                 } else if (*(const char ***)parm_ptr != NULL &&
1519                                            new_list[i][0] == '-' &&
1520                                            new_list[i][1])
1521                                 {
1522                                         str_list_remove(*(const char ***)parm_ptr,
1523                                                         &new_list[i][1]);
1524                                 } else {
1525                                         if (i != 0) {
1526                                                 DEBUG(0, ("Unsupported list syntax for: %s = %s\n",
1527                                                           pszParmName, pszParmValue));
1528                                                 return false;
1529                                         }
1530                                         *(char ***)parm_ptr = new_list;
1531                                         break;
1532                                 }
1533                         }
1534                         break;
1535                 }
1536
1537                 case P_STRING:
1538                         lpcfg_string_set(mem_ctx, (char **)parm_ptr, pszParmValue);
1539                         break;
1540
1541                 case P_USTRING:
1542                         lpcfg_string_set_upper(mem_ctx, (char **)parm_ptr, pszParmValue);
1543                         break;
1544
1545                 case P_ENUM:
1546                         if (!lp_set_enum_parm(&parm_table[parmnum], pszParmValue, (int*)parm_ptr)) {
1547                                 return false;
1548                         }
1549                         break;
1550
1551                 case P_SEP:
1552                         break;
1553         }
1554
1555         return true;
1556
1557 }
1558
1559 bool set_variable(TALLOC_CTX *mem_ctx, struct loadparm_service *service, int parmnum, void *parm_ptr,
1560                          const char *pszParmName, const char *pszParmValue,
1561                          struct loadparm_context *lp_ctx, bool on_globals)
1562 {
1563         int i;
1564         bool ok;
1565
1566         /* if it is a special case then go ahead */
1567         if (parm_table[parmnum].special) {
1568                 ok = parm_table[parmnum].special(lp_ctx, service, pszParmValue,
1569                                                   (char **)parm_ptr);
1570                 if (!ok) {
1571                         return false;
1572                 }
1573                 goto mark_non_default;
1574         }
1575
1576         ok = set_variable_helper(mem_ctx, parmnum, parm_ptr, pszParmName, pszParmValue);
1577
1578         if (!ok) {
1579                 return false;
1580         }
1581
1582 mark_non_default:
1583         if (on_globals && (lp_ctx->flags[parmnum] & FLAG_DEFAULT)) {
1584                 lp_ctx->flags[parmnum] &= ~FLAG_DEFAULT;
1585                 /* we have to also unset FLAG_DEFAULT on aliases */
1586                 for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
1587                         lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1588                 }
1589                 for (i=parmnum+1;i<num_parameters() && parm_table[i].offset == parm_table[parmnum].offset;i++) {
1590                         lp_ctx->flags[i] &= ~FLAG_DEFAULT;
1591                 }
1592         }
1593         return true;
1594 }
1595
1596
1597 bool lpcfg_do_global_parameter(struct loadparm_context *lp_ctx,
1598                                const char *pszParmName, const char *pszParmValue)
1599 {
1600         int parmnum = lpcfg_map_parameter(pszParmName);
1601         void *parm_ptr;
1602
1603         if (parmnum < 0) {
1604                 if (strchr(pszParmName, ':')) {
1605                         return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName, pszParmValue, 0);
1606                 }
1607                 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1608                 return true;
1609         }
1610
1611         /* if the flag has been set on the command line, then don't allow override,
1612            but don't report an error */
1613         if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1614                 return true;
1615         }
1616
1617         if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
1618                 DEBUG(1, ("WARNING: The \"%s\" option is deprecated\n",
1619                           pszParmName));
1620         }
1621
1622         parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[parmnum]);
1623
1624         return set_variable(lp_ctx->globals->ctx, NULL, parmnum, parm_ptr,
1625                             pszParmName, pszParmValue, lp_ctx, true);
1626 }
1627
1628 bool lpcfg_do_service_parameter(struct loadparm_context *lp_ctx,
1629                                 struct loadparm_service *service,
1630                                 const char *pszParmName, const char *pszParmValue)
1631 {
1632         void *parm_ptr;
1633         int i;
1634         int parmnum = lpcfg_map_parameter(pszParmName);
1635
1636         if (parmnum < 0) {
1637                 if (strchr(pszParmName, ':')) {
1638                         return lp_do_parameter_parametric(lp_ctx, service, pszParmName, pszParmValue, 0);
1639                 }
1640                 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
1641                 return true;
1642         }
1643
1644         /* if the flag has been set on the command line, then don't allow override,
1645            but don't report an error */
1646         if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
1647                 return true;
1648         }
1649
1650         if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
1651                 DEBUG(1, ("WARNING: The \"%s\" option is deprecated\n",
1652                           pszParmName));
1653         }
1654
1655         if (parm_table[parmnum].p_class == P_GLOBAL) {
1656                 DEBUG(0,
1657                       ("Global parameter %s found in service section!\n",
1658                        pszParmName));
1659                 return true;
1660         }
1661         parm_ptr = ((char *)service) + parm_table[parmnum].offset;
1662
1663         if (!service->copymap)
1664                 init_copymap(service);
1665
1666         /* this handles the aliases - set the copymap for other
1667          * entries with the same data pointer */
1668         for (i = 0; parm_table[i].label; i++)
1669                 if (parm_table[i].offset == parm_table[parmnum].offset &&
1670                     parm_table[i].p_class == parm_table[parmnum].p_class)
1671                         bitmap_clear(service->copymap, i);
1672
1673         return set_variable(service, service, parmnum, parm_ptr, pszParmName,
1674                             pszParmValue, lp_ctx, false);
1675 }
1676
1677 /**
1678  * Process a parameter.
1679  */
1680
1681 bool lpcfg_do_parameter(const char *pszParmName, const char *pszParmValue,
1682                          void *userdata)
1683 {
1684         struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1685
1686         if (lp_ctx->bInGlobalSection)
1687                 return lpcfg_do_global_parameter(lp_ctx, pszParmName,
1688                                               pszParmValue);
1689         else
1690                 return lpcfg_do_service_parameter(lp_ctx, lp_ctx->currentService,
1691                                                   pszParmName, pszParmValue);
1692 }
1693
1694 /*
1695   variable argument do parameter
1696 */
1697 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx, const char *pszParmName, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
1698 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx,
1699                                 const char *pszParmName, const char *fmt, ...)
1700 {
1701         char *s;
1702         bool ret;
1703         va_list ap;
1704
1705         va_start(ap, fmt);
1706         s = talloc_vasprintf(NULL, fmt, ap);
1707         va_end(ap);
1708         ret = lpcfg_do_global_parameter(lp_ctx, pszParmName, s);
1709         talloc_free(s);
1710         return ret;
1711 }
1712
1713
1714 /*
1715   set a parameter from the commandline - this is called from command line parameter
1716   parsing code. It sets the parameter then marks the parameter as unable to be modified
1717   by smb.conf processing
1718 */
1719 bool lpcfg_set_cmdline(struct loadparm_context *lp_ctx, const char *pszParmName,
1720                        const char *pszParmValue)
1721 {
1722         int parmnum;
1723         int i;
1724
1725         while (isspace((unsigned char)*pszParmValue)) pszParmValue++;
1726
1727         parmnum = lpcfg_map_parameter(pszParmName);
1728
1729         if (parmnum < 0 && strchr(pszParmName, ':')) {
1730                 /* set a parametric option */
1731                 bool ok;
1732                 ok = lp_do_parameter_parametric(lp_ctx, NULL, pszParmName,
1733                                                 pszParmValue, FLAG_CMDLINE);
1734                 if (lp_ctx->s3_fns != NULL) {
1735                         if (ok) {
1736                                 lp_ctx->s3_fns->store_cmdline(pszParmName, pszParmValue);
1737                         }
1738                 }
1739                 return ok;
1740         }
1741
1742         if (parmnum < 0) {
1743                 DEBUG(0,("Unknown option '%s'\n", pszParmName));
1744                 return false;
1745         }
1746
1747         /* reset the CMDLINE flag in case this has been called before */
1748         lp_ctx->flags[parmnum] &= ~FLAG_CMDLINE;
1749
1750         if (!lpcfg_do_global_parameter(lp_ctx, pszParmName, pszParmValue)) {
1751                 return false;
1752         }
1753
1754         lp_ctx->flags[parmnum] |= FLAG_CMDLINE;
1755
1756         /* we have to also set FLAG_CMDLINE on aliases */
1757         for (i=parmnum-1;
1758              i>=0 && parm_table[i].p_class == parm_table[parmnum].p_class &&
1759              parm_table[i].offset == parm_table[parmnum].offset;
1760              i--) {
1761                 lp_ctx->flags[i] |= FLAG_CMDLINE;
1762         }
1763         for (i=parmnum+1;
1764              i<num_parameters() &&
1765              parm_table[i].p_class == parm_table[parmnum].p_class &&
1766              parm_table[i].offset == parm_table[parmnum].offset;
1767              i++) {
1768                 lp_ctx->flags[i] |= FLAG_CMDLINE;
1769         }
1770
1771         if (lp_ctx->s3_fns != NULL) {
1772                 lp_ctx->s3_fns->store_cmdline(pszParmName, pszParmValue);
1773         }
1774
1775         return true;
1776 }
1777
1778 /*
1779   set a option from the commandline in 'a=b' format. Use to support --option
1780 */
1781 bool lpcfg_set_option(struct loadparm_context *lp_ctx, const char *option)
1782 {
1783         char *p, *s;
1784         bool ret;
1785
1786         s = talloc_strdup(NULL, option);
1787         if (!s) {
1788                 return false;
1789         }
1790
1791         p = strchr(s, '=');
1792         if (!p) {
1793                 talloc_free(s);
1794                 return false;
1795         }
1796
1797         *p = 0;
1798
1799         ret = lpcfg_set_cmdline(lp_ctx, s, p+1);
1800         talloc_free(s);
1801         return ret;
1802 }
1803
1804
1805 #define BOOLSTR(b) ((b) ? "Yes" : "No")
1806
1807 /**
1808  * Print a parameter of the specified type.
1809  */
1810
1811 void lpcfg_print_parameter(struct parm_struct *p, void *ptr, FILE * f)
1812 {
1813         /* For the seperation of lists values that we print below */
1814         const char *list_sep = ", ";
1815         int i;
1816         switch (p->type)
1817         {
1818                 case P_ENUM:
1819                         for (i = 0; p->enum_list[i].name; i++) {
1820                                 if (*(int *)ptr == p->enum_list[i].value) {
1821                                         fprintf(f, "%s",
1822                                                 p->enum_list[i].name);
1823                                         break;
1824                                 }
1825                         }
1826                         break;
1827
1828                 case P_BOOL:
1829                         fprintf(f, "%s", BOOLSTR(*(bool *)ptr));
1830                         break;
1831
1832                 case P_BOOLREV:
1833                         fprintf(f, "%s", BOOLSTR(!*(bool *)ptr));
1834                         break;
1835
1836                 case P_INTEGER:
1837                 case P_BYTES:
1838                         fprintf(f, "%d", *(int *)ptr);
1839                         break;
1840
1841                 case P_CHAR:
1842                         fprintf(f, "%c", *(char *)ptr);
1843                         break;
1844
1845                 case P_OCTAL: {
1846                         int val = *(int *)ptr; 
1847                         if (val == -1) {
1848                                 fprintf(f, "-1");
1849                         } else {
1850                                 fprintf(f, "0%03o", val);
1851                         }
1852                         break;
1853                 }
1854
1855                 case P_CMDLIST:
1856                         list_sep = " ";
1857                         /* fall through */
1858                 case P_LIST:
1859                         if ((char ***)ptr && *(char ***)ptr) {
1860                                 char **list = *(char ***)ptr;
1861                                 for (; *list; list++) {
1862                                         /* surround strings with whitespace in double quotes */
1863                                         if (*(list+1) == NULL) {
1864                                                 /* last item, no extra separator */
1865                                                 list_sep = "";
1866                                         }
1867                                         if ( strchr_m( *list, ' ' ) ) {
1868                                                 fprintf(f, "\"%s\"%s", *list, list_sep);
1869                                         } else {
1870                                                 fprintf(f, "%s%s", *list, list_sep);
1871                                         }
1872                                 }
1873                         }
1874                         break;
1875
1876                 case P_STRING:
1877                 case P_USTRING:
1878                         if (*(char **)ptr) {
1879                                 fprintf(f, "%s", *(char **)ptr);
1880                         }
1881                         break;
1882                 case P_SEP:
1883                         break;
1884         }
1885 }
1886
1887 /**
1888  * Check if two parameters are equal.
1889  */
1890
1891 static bool lpcfg_equal_parameter(parm_type type, void *ptr1, void *ptr2)
1892 {
1893         switch (type) {
1894                 case P_BOOL:
1895                 case P_BOOLREV:
1896                         return (*((bool *)ptr1) == *((bool *)ptr2));
1897
1898                 case P_INTEGER:
1899                 case P_ENUM:
1900                 case P_OCTAL:
1901                 case P_BYTES:
1902                         return (*((int *)ptr1) == *((int *)ptr2));
1903
1904                 case P_CHAR:
1905                         return (*((char *)ptr1) == *((char *)ptr2));
1906
1907                 case P_LIST:
1908                 case P_CMDLIST:
1909                         return str_list_equal(*(const char ***)ptr1, *(const char ***)ptr2);
1910
1911                 case P_STRING:
1912                 case P_USTRING:
1913                 {
1914                         char *p1 = *(char **)ptr1, *p2 = *(char **)ptr2;
1915                         if (p1 && !*p1)
1916                                 p1 = NULL;
1917                         if (p2 && !*p2)
1918                                 p2 = NULL;
1919                         return (p1 == p2 || strequal(p1, p2));
1920                 }
1921                 case P_SEP:
1922                         break;
1923         }
1924         return false;
1925 }
1926
1927 /**
1928  * Process a new section (service).
1929  *
1930  * At this stage all sections are services.
1931  * Later we'll have special sections that permit server parameters to be set.
1932  * Returns True on success, False on failure.
1933  */
1934
1935 static bool do_section(const char *pszSectionName, void *userdata)
1936 {
1937         struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
1938         bool bRetval;
1939         bool isglobal;
1940
1941         if (lp_ctx->s3_fns != NULL) {
1942                 return lp_ctx->s3_fns->do_section(pszSectionName, lp_ctx);
1943         }
1944
1945         isglobal = ((strwicmp(pszSectionName, GLOBAL_NAME) == 0) ||
1946                          (strwicmp(pszSectionName, GLOBAL_NAME2) == 0));
1947
1948         bRetval = false;
1949
1950         /* if we've just struck a global section, note the fact. */
1951         lp_ctx->bInGlobalSection = isglobal;
1952
1953         /* check for multiple global sections */
1954         if (lp_ctx->bInGlobalSection) {
1955                 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1956                 return true;
1957         }
1958
1959         /* if we have a current service, tidy it up before moving on */
1960         bRetval = true;
1961
1962         if (lp_ctx->currentService != NULL)
1963                 bRetval = lpcfg_service_ok(lp_ctx->currentService);
1964
1965         /* if all is still well, move to the next record in the services array */
1966         if (bRetval) {
1967                 /* We put this here to avoid an odd message order if messages are */
1968                 /* issued by the post-processing of a previous section. */
1969                 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
1970
1971                 if ((lp_ctx->currentService = lpcfg_add_service(lp_ctx, lp_ctx->sDefault,
1972                                                                    pszSectionName))
1973                     == NULL) {
1974                         DEBUG(0, ("Failed to add a new service\n"));
1975                         return false;
1976                 }
1977         }
1978
1979         return bRetval;
1980 }
1981
1982
1983 /**
1984  * Determine if a particular base parameter is currently set to the default value.
1985  */
1986
1987 static bool is_default(void *base_structure, int i)
1988 {
1989         void *def_ptr = ((char *)base_structure) + parm_table[i].offset;
1990         switch (parm_table[i].type) {
1991                 case P_CMDLIST:
1992                 case P_LIST:
1993                         return str_list_equal((const char * const *)parm_table[i].def.lvalue,
1994                                               *(const char * const **)def_ptr);
1995                 case P_STRING:
1996                 case P_USTRING:
1997                         return strequal(parm_table[i].def.svalue,
1998                                         *(char **)def_ptr);
1999                 case P_BOOL:
2000                 case P_BOOLREV:
2001                         return parm_table[i].def.bvalue ==
2002                                 *(bool *)def_ptr;
2003                 case P_INTEGER:
2004                 case P_CHAR:
2005                 case P_OCTAL:
2006                 case P_BYTES:
2007                 case P_ENUM:
2008                         return parm_table[i].def.ivalue ==
2009                                 *(int *)def_ptr;
2010                 case P_SEP:
2011                         break;
2012         }
2013         return false;
2014 }
2015
2016 /**
2017  *Display the contents of the global structure.
2018  */
2019
2020 void lpcfg_dump_globals(struct loadparm_context *lp_ctx, FILE *f,
2021                          bool show_defaults)
2022 {
2023         int i;
2024         struct parmlist_entry *data;
2025
2026         fprintf(f, "# Global parameters\n[global]\n");
2027
2028         for (i = 0; parm_table[i].label; i++)
2029                 if (parm_table[i].p_class == P_GLOBAL &&
2030                     (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset))) {
2031                         if (!show_defaults) {
2032                                 if (lp_ctx->flags && (lp_ctx->flags[i] & FLAG_DEFAULT)) {
2033                                         continue;
2034                                 }
2035
2036                                 if (is_default(lp_ctx->globals, i)) {
2037                                         continue;
2038                                 }
2039                         }
2040
2041                         fprintf(f, "\t%s = ", parm_table[i].label);
2042                         lpcfg_print_parameter(&parm_table[i], lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[i]), f);
2043                         fprintf(f, "\n");
2044         }
2045         if (lp_ctx->globals->param_opt != NULL) {
2046                 for (data = lp_ctx->globals->param_opt; data;
2047                      data = data->next) {
2048                         if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
2049                                 continue;
2050                         }
2051                         fprintf(f, "\t%s = %s\n", data->key, data->value);
2052                 }
2053         }
2054
2055 }
2056
2057 /**
2058  * Display the contents of a single services record.
2059  */
2060
2061 void lpcfg_dump_a_service(struct loadparm_service * pService, struct loadparm_service *sDefault, FILE * f,
2062                           unsigned int *flags, bool show_defaults)
2063 {
2064         int i;
2065         struct parmlist_entry *data;
2066
2067         if (pService != sDefault)
2068                 fprintf(f, "\n[%s]\n", pService->szService);
2069
2070         for (i = 0; parm_table[i].label; i++) {
2071                 if (parm_table[i].p_class == P_LOCAL &&
2072                     (*parm_table[i].label != '-') &&
2073                     (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset)))
2074                 {
2075                         if (pService == sDefault) {
2076                                 if (flags && (flags[i] & FLAG_DEFAULT)) {
2077                                         continue;
2078                                 }
2079                                 if (!show_defaults) {
2080                                         if (is_default(sDefault, i)) {
2081                                                 continue;
2082                                         }
2083                                 }
2084                         } else {
2085                                 if (lpcfg_equal_parameter(parm_table[i].type,
2086                                                           ((char *)pService) +
2087                                                           parm_table[i].offset,
2088                                                           ((char *)sDefault) +
2089                                                           parm_table[i].offset))
2090                                         continue;
2091                         }
2092
2093                         fprintf(f, "\t%s = ", parm_table[i].label);
2094                         lpcfg_print_parameter(&parm_table[i],
2095                                         ((char *)pService) + parm_table[i].offset, f);
2096                         fprintf(f, "\n");
2097                 }
2098         }
2099         if (pService->param_opt != NULL) {
2100                 for (data = pService->param_opt; data; data = data->next) {
2101                         if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
2102                                 continue;
2103                         }
2104                         fprintf(f, "\t%s = %s\n", data->key, data->value);
2105                 }
2106         }
2107 }
2108
2109 bool lpcfg_dump_a_parameter(struct loadparm_context *lp_ctx,
2110                             struct loadparm_service *service,
2111                             const char *parm_name, FILE * f)
2112 {
2113         struct parm_struct *parm;
2114         void *ptr;
2115         char *local_parm_name;
2116         char *parm_opt;
2117         const char *parm_opt_value;
2118
2119         /* check for parametrical option */
2120         local_parm_name = talloc_strdup(lp_ctx, parm_name);
2121         if (local_parm_name == NULL) {
2122                 return false;
2123         }
2124
2125         parm_opt = strchr( local_parm_name, ':');
2126
2127         if (parm_opt) {
2128                 *parm_opt = '\0';
2129                 parm_opt++;
2130                 if (strlen(parm_opt)) {
2131                         parm_opt_value = lpcfg_parm_string(lp_ctx, service,
2132                                 local_parm_name, parm_opt);
2133                         if (parm_opt_value) {
2134                                 fprintf(f, "%s\n", parm_opt_value);
2135                                 return true;
2136                         }
2137                 }
2138                 return false;
2139         }
2140
2141         /* parameter is not parametric, search the table */
2142         parm = lpcfg_parm_struct(lp_ctx, parm_name);
2143         if (!parm) {
2144                 return false;
2145         }
2146
2147         if (service != NULL && parm->p_class == P_GLOBAL) {
2148                 return false;
2149         }
2150
2151         ptr = lpcfg_parm_ptr(lp_ctx, service,parm);
2152
2153         lpcfg_print_parameter(parm, ptr, f);
2154         fprintf(f, "\n");
2155         return true;
2156 }
2157
2158 /**
2159  * Auto-load some home services.
2160  */
2161 static void lpcfg_add_auto_services(struct loadparm_context *lp_ctx,
2162                                     const char *str)
2163 {
2164         return;
2165 }
2166
2167 /***************************************************************************
2168  Initialise the sDefault parameter structure for the printer values.
2169 ***************************************************************************/
2170
2171 void init_printer_values(struct loadparm_context *lp_ctx, TALLOC_CTX *ctx,
2172                          struct loadparm_service *pService)
2173 {
2174         /* choose defaults depending on the type of printing */
2175         switch (pService->printing) {
2176                 case PRINT_BSD:
2177                 case PRINT_AIX:
2178                 case PRINT_LPRNT:
2179                 case PRINT_LPROS2:
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                         break;
2184
2185                 case PRINT_LPRNG:
2186                 case PRINT_PLP:
2187                         lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P'%p'");
2188                         lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P'%p' %j");
2189                         lpcfg_string_set(ctx, &pService->print_command, "lpr -r -P'%p' %s");
2190                         lpcfg_string_set(ctx, &pService->queuepause_command, "lpc stop '%p'");
2191                         lpcfg_string_set(ctx, &pService->queueresume_command, "lpc start '%p'");
2192                         lpcfg_string_set(ctx, &pService->lppause_command, "lpc hold '%p' %j");
2193                         lpcfg_string_set(ctx, &pService->lpresume_command, "lpc release '%p' %j");
2194                         break;
2195
2196                 case PRINT_CUPS:
2197                 case PRINT_IPRINT:
2198                         /* set the lpq command to contain the destination printer
2199                            name only.  This is used by cups_queue_get() */
2200                         lpcfg_string_set(ctx, &pService->lpq_command, "%p");
2201                         lpcfg_string_set(ctx, &pService->lprm_command, "");
2202                         lpcfg_string_set(ctx, &pService->print_command, "");
2203                         lpcfg_string_set(ctx, &pService->lppause_command, "");
2204                         lpcfg_string_set(ctx, &pService->lpresume_command, "");
2205                         lpcfg_string_set(ctx, &pService->queuepause_command, "");
2206                         lpcfg_string_set(ctx, &pService->queueresume_command, "");
2207                         break;
2208
2209                 case PRINT_SYSV:
2210                 case PRINT_HPUX:
2211                         lpcfg_string_set(ctx, &pService->lpq_command, "lpstat -o%p");
2212                         lpcfg_string_set(ctx, &pService->lprm_command, "cancel %p-%j");
2213                         lpcfg_string_set(ctx, &pService->print_command, "lp -c -d%p %s; rm %s");
2214                         lpcfg_string_set(ctx, &pService->queuepause_command, "disable %p");
2215                         lpcfg_string_set(ctx, &pService->queueresume_command, "enable %p");
2216 #ifndef HPUX
2217                         lpcfg_string_set(ctx, &pService->lppause_command, "lp -i %p-%j -H hold");
2218                         lpcfg_string_set(ctx, &pService->lpresume_command, "lp -i %p-%j -H resume");
2219 #endif /* HPUX */
2220                         break;
2221
2222                 case PRINT_QNX:
2223                         lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P%p");
2224                         lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P%p %j");
2225                         lpcfg_string_set(ctx, &pService->print_command, "lp -r -P%p %s");
2226                         break;
2227
2228 #if defined(DEVELOPER) || defined(ENABLE_SELFTEST)
2229
2230         case PRINT_TEST:
2231         case PRINT_VLP: {
2232                 const char *tdbfile;
2233                 TALLOC_CTX *tmp_ctx = talloc_new(ctx);
2234                 const char *tmp;
2235
2236                 tmp = lpcfg_parm_string(lp_ctx, NULL, "vlp", "tdbfile");
2237                 if (tmp == NULL) {
2238                         tmp = "/tmp/vlp.tdb";
2239                 }
2240
2241                 tdbfile = talloc_asprintf(tmp_ctx, "tdbfile=%s", tmp);
2242                 if (tdbfile == NULL) {
2243                         tdbfile="tdbfile=/tmp/vlp.tdb";
2244                 }
2245
2246                 tmp = talloc_asprintf(tmp_ctx, "vlp %s print %%p %%s",
2247                                       tdbfile);
2248                 lpcfg_string_set(ctx, &pService->print_command,
2249                            tmp ? tmp : "vlp print %p %s");
2250
2251                 tmp = talloc_asprintf(tmp_ctx, "vlp %s lpq %%p",
2252                                       tdbfile);
2253                 lpcfg_string_set(ctx, &pService->lpq_command,
2254                            tmp ? tmp : "vlp lpq %p");
2255
2256                 tmp = talloc_asprintf(tmp_ctx, "vlp %s lprm %%p %%j",
2257                                       tdbfile);
2258                 lpcfg_string_set(ctx, &pService->lprm_command,
2259                            tmp ? tmp : "vlp lprm %p %j");
2260
2261                 tmp = talloc_asprintf(tmp_ctx, "vlp %s lppause %%p %%j",
2262                                       tdbfile);
2263                 lpcfg_string_set(ctx, &pService->lppause_command,
2264                            tmp ? tmp : "vlp lppause %p %j");
2265
2266                 tmp = talloc_asprintf(tmp_ctx, "vlp %s lpresume %%p %%j",
2267                                       tdbfile);
2268                 lpcfg_string_set(ctx, &pService->lpresume_command,
2269                            tmp ? tmp : "vlp lpresume %p %j");
2270
2271                 tmp = talloc_asprintf(tmp_ctx, "vlp %s queuepause %%p",
2272                                       tdbfile);
2273                 lpcfg_string_set(ctx, &pService->queuepause_command,
2274                            tmp ? tmp : "vlp queuepause %p");
2275
2276                 tmp = talloc_asprintf(tmp_ctx, "vlp %s queueresume %%p",
2277                                       tdbfile);
2278                 lpcfg_string_set(ctx, &pService->queueresume_command,
2279                            tmp ? tmp : "vlp queueresume %p");
2280                 TALLOC_FREE(tmp_ctx);
2281
2282                 break;
2283         }
2284 #endif /* DEVELOPER */
2285
2286         }
2287 }
2288
2289 /**
2290  * Unload unused services.
2291  */
2292
2293 void lpcfg_killunused(struct loadparm_context *lp_ctx,
2294                    struct smbsrv_connection *smb,
2295                    bool (*snumused) (struct smbsrv_connection *, int))
2296 {
2297         int i;
2298
2299         if (lp_ctx->s3_fns != NULL) {
2300                 smb_panic("Cannot be used from an s3 loadparm ctx");
2301         }
2302
2303         for (i = 0; i < lp_ctx->iNumServices; i++) {
2304                 if (lp_ctx->services[i] == NULL)
2305                         continue;
2306
2307                 if (!snumused || !snumused(smb, i)) {
2308                         talloc_free(lp_ctx->services[i]);
2309                         lp_ctx->services[i] = NULL;
2310                 }
2311         }
2312 }
2313
2314
2315 static int lpcfg_destructor(struct loadparm_context *lp_ctx)
2316 {
2317         struct parmlist_entry *data;
2318
2319         if (lp_ctx->refuse_free) {
2320                 /* someone is trying to free the
2321                    global_loadparm_context.
2322                    We can't allow that. */
2323                 return -1;
2324         }
2325
2326         if (lp_ctx->globals->param_opt != NULL) {
2327                 struct parmlist_entry *next;
2328                 for (data = lp_ctx->globals->param_opt; data; data=next) {
2329                         next = data->next;
2330                         if (data->priority & FLAG_CMDLINE) continue;
2331                         DLIST_REMOVE(lp_ctx->globals->param_opt, data);
2332                         talloc_free(data);
2333                 }
2334         }
2335
2336         return 0;
2337 }
2338
2339 /**
2340  * Initialise the global parameter structure.
2341  *
2342  * Note that most callers should use loadparm_init_global() instead
2343  */
2344 struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
2345 {
2346         int i;
2347         char *myname;
2348         struct loadparm_context *lp_ctx;
2349         struct parmlist_entry *parm;
2350         char *logfile;
2351
2352         lp_ctx = talloc_zero(mem_ctx, struct loadparm_context);
2353         if (lp_ctx == NULL)
2354                 return NULL;
2355
2356         talloc_set_destructor(lp_ctx, lpcfg_destructor);
2357         lp_ctx->bInGlobalSection = true;
2358         lp_ctx->globals = talloc_zero(lp_ctx, struct loadparm_global);
2359         /* This appears odd, but globals in s3 isn't a pointer */
2360         lp_ctx->globals->ctx = lp_ctx->globals;
2361         lp_ctx->sDefault = talloc_zero(lp_ctx, struct loadparm_service);
2362         lp_ctx->flags = talloc_zero_array(lp_ctx, unsigned int, num_parameters());
2363
2364         lp_ctx->sDefault->iMaxPrintJobs = 1000;
2365         lp_ctx->sDefault->bAvailable = true;
2366         lp_ctx->sDefault->browseable = true;
2367         lp_ctx->sDefault->read_only = true;
2368         lp_ctx->sDefault->map_archive = true;
2369         lp_ctx->sDefault->strict_locking = true;
2370         lp_ctx->sDefault->oplocks = true;
2371         lp_ctx->sDefault->create_mask = 0744;
2372         lp_ctx->sDefault->force_create_mode = 0000;
2373         lp_ctx->sDefault->directory_mask = 0755;
2374         lp_ctx->sDefault->force_directory_mode = 0000;
2375
2376         DEBUG(3, ("Initialising global parameters\n"));
2377
2378         for (i = 0; parm_table[i].label; i++) {
2379                 if ((parm_table[i].type == P_STRING ||
2380                      parm_table[i].type == P_USTRING) &&
2381                     !(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2382                         char **r;
2383                         if (parm_table[i].p_class == P_LOCAL) {
2384                                 r = (char **)(((char *)lp_ctx->sDefault) + parm_table[i].offset);
2385                         } else {
2386                                 r = (char **)(((char *)lp_ctx->globals) + parm_table[i].offset);
2387                         }
2388                         *r = talloc_strdup(lp_ctx, "");
2389                 }
2390         }
2391
2392         logfile = talloc_asprintf(lp_ctx, "%s/log.samba", dyn_LOGFILEBASE);
2393         lpcfg_do_global_parameter(lp_ctx, "log file", logfile);
2394         talloc_free(logfile);
2395
2396         lpcfg_do_global_parameter(lp_ctx, "log level", "0");
2397
2398         lpcfg_do_global_parameter(lp_ctx, "syslog", "1");
2399         lpcfg_do_global_parameter(lp_ctx, "syslog only", "No");
2400         lpcfg_do_global_parameter(lp_ctx, "debug timestamp", "Yes");
2401         lpcfg_do_global_parameter(lp_ctx, "debug prefix timestamp", "No");
2402         lpcfg_do_global_parameter(lp_ctx, "debug hires timestamp", "Yes");
2403         lpcfg_do_global_parameter(lp_ctx, "debug pid", "No");
2404         lpcfg_do_global_parameter(lp_ctx, "debug uid", "No");
2405         lpcfg_do_global_parameter(lp_ctx, "debug class", "No");
2406
2407         lpcfg_do_global_parameter(lp_ctx, "share backend", "classic");
2408
2409         lpcfg_do_global_parameter(lp_ctx, "server role", "auto");
2410         lpcfg_do_global_parameter(lp_ctx, "domain logons", "No");
2411         lpcfg_do_global_parameter(lp_ctx, "domain master", "Auto");
2412
2413         /* options that can be set on the command line must be initialised via
2414            the slower lpcfg_do_global_parameter() to ensure that FLAG_CMDLINE is obeyed */
2415 #ifdef TCP_NODELAY
2416         lpcfg_do_global_parameter(lp_ctx, "socket options", "TCP_NODELAY");
2417 #endif
2418         lpcfg_do_global_parameter(lp_ctx, "workgroup", DEFAULT_WORKGROUP);
2419         myname = get_myname(lp_ctx);
2420         lpcfg_do_global_parameter(lp_ctx, "netbios name", myname);
2421         talloc_free(myname);
2422         lpcfg_do_global_parameter(lp_ctx, "name resolve order", "lmhosts wins host bcast");
2423
2424         lpcfg_do_global_parameter(lp_ctx, "fstype", "NTFS");
2425
2426         lpcfg_do_global_parameter(lp_ctx, "ntvfs handler", "unixuid default");
2427         lpcfg_do_global_parameter(lp_ctx, "max connections", "0");
2428
2429         lpcfg_do_global_parameter(lp_ctx, "dcerpc endpoint servers", "epmapper wkssvc rpcecho samr netlogon lsarpc spoolss drsuapi dssetup unixinfo browser eventlog6 backupkey dnsserver");
2430         lpcfg_do_global_parameter(lp_ctx, "server services", "s3fs rpc nbt wrepl ldap cldap kdc drepl winbindd ntp_signd kcc dnsupdate dns");
2431         lpcfg_do_global_parameter(lp_ctx, "kccsrv:samba_kcc", "false");
2432         /* the winbind method for domain controllers is for both RODC
2433            auth forwarding and for trusted domains */
2434         lpcfg_do_global_parameter(lp_ctx, "private dir", dyn_PRIVATE_DIR);
2435         lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_LOCAL_MACHINE", "hklm.ldb");
2436
2437         /* This hive should be dynamically generated by Samba using
2438            data from the sam, but for the moment leave it in a tdb to
2439            keep regedt32 from popping up an annoying dialog. */
2440         lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_USERS", "hku.ldb");
2441
2442         /* using UTF8 by default allows us to support all chars */
2443         lpcfg_do_global_parameter(lp_ctx, "unix charset", "UTF-8");
2444
2445         /* Use codepage 850 as a default for the dos character set */
2446         lpcfg_do_global_parameter(lp_ctx, "dos charset", "CP850");
2447
2448         /*
2449          * Allow the default PASSWD_CHAT to be overridden in local.h.
2450          */
2451         lpcfg_do_global_parameter(lp_ctx, "passwd chat", DEFAULT_PASSWD_CHAT);
2452
2453         lpcfg_do_global_parameter(lp_ctx, "pid directory", dyn_PIDDIR);
2454         lpcfg_do_global_parameter(lp_ctx, "lock dir", dyn_LOCKDIR);
2455         lpcfg_do_global_parameter(lp_ctx, "state directory", dyn_STATEDIR);
2456         lpcfg_do_global_parameter(lp_ctx, "cache directory", dyn_CACHEDIR);
2457         lpcfg_do_global_parameter(lp_ctx, "ncalrpc dir", dyn_NCALRPCDIR);
2458
2459         lpcfg_do_global_parameter(lp_ctx, "nbt client socket address", "0.0.0.0");
2460         lpcfg_do_global_parameter_var(lp_ctx, "server string",
2461                                    "Samba %s", SAMBA_VERSION_STRING);
2462
2463         lpcfg_do_global_parameter(lp_ctx, "password server", "*");
2464
2465         lpcfg_do_global_parameter(lp_ctx, "max mux", "50");
2466         lpcfg_do_global_parameter(lp_ctx, "max xmit", "16644");
2467         lpcfg_do_global_parameter(lp_ctx, "host msdfs", "true");
2468
2469         lpcfg_do_global_parameter(lp_ctx, "LargeReadwrite", "True");
2470         lpcfg_do_global_parameter(lp_ctx, "server min protocol", "LANMAN1");
2471         lpcfg_do_global_parameter(lp_ctx, "server max protocol", "SMB3");
2472         lpcfg_do_global_parameter(lp_ctx, "client min protocol", "CORE");
2473         lpcfg_do_global_parameter(lp_ctx, "client max protocol", "default");
2474         lpcfg_do_global_parameter(lp_ctx, "security", "AUTO");
2475         lpcfg_do_global_parameter(lp_ctx, "EncryptPasswords", "True");
2476         lpcfg_do_global_parameter(lp_ctx, "ReadRaw", "True");
2477         lpcfg_do_global_parameter(lp_ctx, "WriteRaw", "True");
2478         lpcfg_do_global_parameter(lp_ctx, "NullPasswords", "False");
2479         lpcfg_do_global_parameter(lp_ctx, "old password allowed period", "60");
2480         lpcfg_do_global_parameter(lp_ctx, "ObeyPamRestrictions", "False");
2481
2482         lpcfg_do_global_parameter(lp_ctx, "TimeServer", "False");
2483         lpcfg_do_global_parameter(lp_ctx, "BindInterfacesOnly", "False");
2484         lpcfg_do_global_parameter(lp_ctx, "Unicode", "True");
2485         lpcfg_do_global_parameter(lp_ctx, "ClientLanManAuth", "False");
2486         lpcfg_do_global_parameter(lp_ctx, "ClientNTLMv2Auth", "True");
2487         lpcfg_do_global_parameter(lp_ctx, "LanmanAuth", "False");
2488         lpcfg_do_global_parameter(lp_ctx, "NTLMAuth", "True");
2489         lpcfg_do_global_parameter(lp_ctx, "client use spnego principal", "False");
2490
2491         lpcfg_do_global_parameter(lp_ctx, "UnixExtensions", "True");
2492
2493         lpcfg_do_global_parameter(lp_ctx, "PreferredMaster", "Auto");
2494         lpcfg_do_global_parameter(lp_ctx, "LocalMaster", "True");
2495
2496         lpcfg_do_global_parameter(lp_ctx, "wins support", "False");
2497         lpcfg_do_global_parameter(lp_ctx, "dns proxy", "True");
2498
2499         lpcfg_do_global_parameter(lp_ctx, "winbind separator", "\\");
2500         lpcfg_do_global_parameter(lp_ctx, "winbind sealed pipes", "True");
2501         lpcfg_do_global_parameter(lp_ctx, "require strong key", "True");
2502         lpcfg_do_global_parameter(lp_ctx, "winbindd socket directory", dyn_WINBINDD_SOCKET_DIR);
2503         lpcfg_do_global_parameter(lp_ctx, "winbindd privileged socket directory", dyn_WINBINDD_PRIVILEGED_SOCKET_DIR);
2504         lpcfg_do_global_parameter(lp_ctx, "ntp signd socket directory", dyn_NTP_SIGND_SOCKET_DIR);
2505         lpcfg_do_global_parameter_var(lp_ctx, "dns update command", "%s/samba_dnsupdate", dyn_SCRIPTSBINDIR);
2506         lpcfg_do_global_parameter_var(lp_ctx, "spn update command", "%s/samba_spnupdate", dyn_SCRIPTSBINDIR);
2507         lpcfg_do_global_parameter_var(lp_ctx, "samba kcc command",
2508                                         "%s/samba_kcc", dyn_SCRIPTSBINDIR);
2509         lpcfg_do_global_parameter(lp_ctx, "template shell", "/bin/false");
2510         lpcfg_do_global_parameter(lp_ctx, "template homedir", "/home/%D/%U");
2511
2512         lpcfg_do_global_parameter(lp_ctx, "client signing", "default");
2513         lpcfg_do_global_parameter(lp_ctx, "server signing", "default");
2514
2515         lpcfg_do_global_parameter(lp_ctx, "use spnego", "True");
2516
2517         lpcfg_do_global_parameter(lp_ctx, "use mmap", "True");
2518
2519         lpcfg_do_global_parameter(lp_ctx, "smb ports", "445 139");
2520         lpcfg_do_global_parameter(lp_ctx, "nbt port", "137");
2521         lpcfg_do_global_parameter(lp_ctx, "dgram port", "138");
2522         lpcfg_do_global_parameter(lp_ctx, "cldap port", "389");
2523         lpcfg_do_global_parameter(lp_ctx, "krb5 port", "88");
2524         lpcfg_do_global_parameter(lp_ctx, "kpasswd port", "464");
2525         lpcfg_do_global_parameter(lp_ctx, "web port", "901");
2526
2527         lpcfg_do_global_parameter(lp_ctx, "nt status support", "True");
2528
2529         lpcfg_do_global_parameter(lp_ctx, "max wins ttl", "518400"); /* 6 days */
2530         lpcfg_do_global_parameter(lp_ctx, "min wins ttl", "21600");
2531
2532         lpcfg_do_global_parameter(lp_ctx, "tls enabled", "True");
2533         lpcfg_do_global_parameter(lp_ctx, "tls keyfile", "tls/key.pem");
2534         lpcfg_do_global_parameter(lp_ctx, "tls certfile", "tls/cert.pem");
2535         lpcfg_do_global_parameter(lp_ctx, "tls cafile", "tls/ca.pem");
2536         lpcfg_do_global_parameter(lp_ctx, "prefork children:smb", "4");
2537
2538         lpcfg_do_global_parameter(lp_ctx, "rndc command", "/usr/sbin/rndc");
2539         lpcfg_do_global_parameter(lp_ctx, "nsupdate command", "/usr/bin/nsupdate -g");
2540
2541         lpcfg_do_global_parameter(lp_ctx, "allow dns updates", "secure only");
2542         lpcfg_do_global_parameter(lp_ctx, "dns forwarder", "");
2543
2544         lpcfg_do_global_parameter(lp_ctx, "algorithmic rid base", "1000");
2545
2546         lpcfg_do_global_parameter(lp_ctx, "enhanced browsing", "True");
2547
2548         lpcfg_do_global_parameter(lp_ctx, "winbind nss info", "template");
2549
2550         lpcfg_do_global_parameter(lp_ctx, "server schannel", "Auto");
2551
2552         lpcfg_do_global_parameter(lp_ctx, "short preserve case", "True");
2553
2554         lpcfg_do_global_parameter(lp_ctx, "max open files", "16384");
2555
2556         lpcfg_do_global_parameter(lp_ctx, "cups connection timeout", "30");
2557
2558         lpcfg_do_global_parameter(lp_ctx, "locking", "True");
2559
2560         lpcfg_do_global_parameter(lp_ctx, "block size", "1024");
2561
2562         lpcfg_do_global_parameter(lp_ctx, "client use spnego", "True");
2563
2564         lpcfg_do_global_parameter(lp_ctx, "change notify", "True");
2565
2566         lpcfg_do_global_parameter(lp_ctx, "name cache timeout", "660");
2567
2568         lpcfg_do_global_parameter(lp_ctx, "defer sharing violations", "True");
2569
2570         lpcfg_do_global_parameter(lp_ctx, "ldap replication sleep", "1000");
2571
2572         lpcfg_do_global_parameter(lp_ctx, "idmap backend", "tdb");
2573
2574         lpcfg_do_global_parameter(lp_ctx, "enable privileges", "True");
2575
2576         lpcfg_do_global_parameter_var(lp_ctx, "smb2 max write", "%u", DEFAULT_SMB2_MAX_WRITE);
2577
2578         lpcfg_do_global_parameter(lp_ctx, "passdb backend", "tdbsam");
2579
2580         lpcfg_do_global_parameter(lp_ctx, "getwd cache", "True");
2581
2582         lpcfg_do_global_parameter(lp_ctx, "winbind nested groups", "True");
2583
2584         lpcfg_do_global_parameter(lp_ctx, "mangled names", "True");
2585
2586         lpcfg_do_global_parameter_var(lp_ctx, "smb2 max credits", "%u", DEFAULT_SMB2_MAX_CREDITS);
2587
2588         lpcfg_do_global_parameter(lp_ctx, "ldap ssl", "start tls");
2589
2590         lpcfg_do_global_parameter(lp_ctx, "ldap deref", "auto");
2591
2592         lpcfg_do_global_parameter(lp_ctx, "lm interval", "60");
2593
2594         lpcfg_do_global_parameter(lp_ctx, "mangling method", "hash2");
2595
2596         lpcfg_do_global_parameter(lp_ctx, "hide dot files", "True");
2597
2598         lpcfg_do_global_parameter(lp_ctx, "browse list", "True");
2599
2600         lpcfg_do_global_parameter(lp_ctx, "passwd chat timeout", "2");
2601
2602         lpcfg_do_global_parameter(lp_ctx, "guest account", GUEST_ACCOUNT);
2603
2604         lpcfg_do_global_parameter(lp_ctx, "client schannel", "auto");
2605
2606         lpcfg_do_global_parameter(lp_ctx, "smb encrypt", "default");
2607
2608         lpcfg_do_global_parameter(lp_ctx, "max log size", "5000");
2609
2610         lpcfg_do_global_parameter(lp_ctx, "idmap negative cache time", "120");
2611
2612         lpcfg_do_global_parameter(lp_ctx, "ldap follow referral", "auto");
2613
2614         lpcfg_do_global_parameter(lp_ctx, "multicast dns register", "yes");
2615
2616         lpcfg_do_global_parameter(lp_ctx, "winbind reconnect delay", "30");
2617
2618         lpcfg_do_global_parameter(lp_ctx, "winbind request timeout", "60");
2619
2620         lpcfg_do_global_parameter(lp_ctx, "nt acl support", "yes");
2621
2622         lpcfg_do_global_parameter(lp_ctx, "acl check permissions", "yes");
2623
2624         lpcfg_do_global_parameter(lp_ctx, "keepalive", "300");
2625
2626         lpcfg_do_global_parameter(lp_ctx, "smbd profiling level", "off");
2627
2628         lpcfg_do_global_parameter(lp_ctx, "winbind cache time", "300");
2629
2630         lpcfg_do_global_parameter(lp_ctx, "level2 oplocks", "yes");
2631
2632         lpcfg_do_global_parameter(lp_ctx, "show add printer wizard", "yes");
2633
2634         lpcfg_do_global_parameter(lp_ctx, "allocation roundup size", "1048576");
2635
2636         lpcfg_do_global_parameter(lp_ctx, "ldap page size", "1024");
2637
2638         lpcfg_do_global_parameter(lp_ctx, "kernel share modes", "yes");
2639
2640         lpcfg_do_global_parameter(lp_ctx, "strict locking", "Auto");
2641
2642         lpcfg_do_global_parameter(lp_ctx, "map readonly", "yes");
2643
2644         lpcfg_do_global_parameter(lp_ctx, "allow trusted domains", "yes");
2645
2646         lpcfg_do_global_parameter(lp_ctx, "default devmode", "yes");
2647
2648         lpcfg_do_global_parameter(lp_ctx, "os level", "20");
2649
2650         lpcfg_do_global_parameter(lp_ctx, "dos filetimes", "yes");
2651
2652         lpcfg_do_global_parameter(lp_ctx, "mangling char", "~");
2653
2654         lpcfg_do_global_parameter(lp_ctx, "printcap cache time", "750");
2655
2656         lpcfg_do_global_parameter(lp_ctx, "create krb5 conf", "yes");
2657
2658         lpcfg_do_global_parameter(lp_ctx, "winbind max clients", "200");
2659
2660         lpcfg_do_global_parameter(lp_ctx, "acl map full control", "yes");
2661
2662         lpcfg_do_global_parameter(lp_ctx, "nt pipe support", "yes");
2663
2664         lpcfg_do_global_parameter(lp_ctx, "ldap debug threshold", "10");
2665
2666         lpcfg_do_global_parameter(lp_ctx, "client ldap sasl wrapping", "sign");
2667
2668         lpcfg_do_global_parameter(lp_ctx, "follow symlinks", "yes");
2669
2670         lpcfg_do_global_parameter(lp_ctx, "machine password timeout", "604800");
2671
2672         lpcfg_do_global_parameter(lp_ctx, "ldap connection timeout", "2");
2673
2674         lpcfg_do_global_parameter(lp_ctx, "winbind expand groups", "0");
2675
2676         lpcfg_do_global_parameter(lp_ctx, "stat cache", "yes");
2677
2678         lpcfg_do_global_parameter(lp_ctx, "lpq cache time", "30");
2679
2680         lpcfg_do_global_parameter_var(lp_ctx, "smb2 max trans", "%u", DEFAULT_SMB2_MAX_TRANSACT);
2681
2682         lpcfg_do_global_parameter_var(lp_ctx, "smb2 max read", "%u", DEFAULT_SMB2_MAX_READ);
2683
2684         lpcfg_do_global_parameter(lp_ctx, "durable handles", "yes");
2685
2686         lpcfg_do_global_parameter(lp_ctx, "max stat cache size", "256");
2687
2688         lpcfg_do_global_parameter(lp_ctx, "ldap passwd sync", "no");
2689
2690         lpcfg_do_global_parameter(lp_ctx, "kernel change notify", "yes");
2691
2692         lpcfg_do_global_parameter(lp_ctx, "max ttl", "259200");
2693
2694         lpcfg_do_global_parameter(lp_ctx, "blocking locks", "yes");
2695
2696         lpcfg_do_global_parameter(lp_ctx, "oplock contention limit", "2");
2697
2698         lpcfg_do_global_parameter(lp_ctx, "load printers", "yes");
2699
2700         lpcfg_do_global_parameter(lp_ctx, "idmap cache time", "604800");
2701
2702         lpcfg_do_global_parameter(lp_ctx, "preserve case", "yes");
2703
2704         lpcfg_do_global_parameter(lp_ctx, "lm announce", "auto");
2705
2706         lpcfg_do_global_parameter(lp_ctx, "afs token lifetime", "604800");
2707
2708         lpcfg_do_global_parameter(lp_ctx, "enable core files", "yes");
2709
2710         lpcfg_do_global_parameter(lp_ctx, "winbind max domain connections", "1");
2711
2712         lpcfg_do_global_parameter(lp_ctx, "case sensitive", "auto");
2713
2714         lpcfg_do_global_parameter(lp_ctx, "ldap timeout", "15");
2715
2716         lpcfg_do_global_parameter(lp_ctx, "mangle prefix", "1");
2717
2718         lpcfg_do_global_parameter(lp_ctx, "posix locking", "yes");
2719
2720         lpcfg_do_global_parameter(lp_ctx, "lock spin time", "200");
2721
2722         lpcfg_do_global_parameter(lp_ctx, "directory name cache size", "100");
2723
2724         lpcfg_do_global_parameter(lp_ctx, "nmbd bind explicit broadcast", "yes");
2725
2726         lpcfg_do_global_parameter(lp_ctx, "init logon delay", "100");
2727
2728         lpcfg_do_global_parameter(lp_ctx, "usershare owner only", "yes");
2729
2730         lpcfg_do_global_parameter(lp_ctx, "-valid", "yes");
2731
2732         lpcfg_do_global_parameter_var(lp_ctx, "usershare path", "%s/usershares", get_dyn_STATEDIR());
2733
2734 #ifdef DEVELOPER
2735         lpcfg_do_global_parameter_var(lp_ctx, "panic action", "/bin/sleep 999999999");
2736 #endif
2737
2738         lpcfg_do_global_parameter(lp_ctx, "smb passwd file", get_dyn_SMB_PASSWD_FILE());
2739
2740         lpcfg_do_global_parameter(lp_ctx, "logon home", "\\\\%N\\%U");
2741
2742         lpcfg_do_global_parameter(lp_ctx, "logon path", "\\\\%N\\%U\\profile");
2743
2744         lpcfg_do_global_parameter(lp_ctx, "printjob username", "%U");
2745
2746         for (i = 0; parm_table[i].label; i++) {
2747                 if (!(lp_ctx->flags[i] & FLAG_CMDLINE)) {
2748                         lp_ctx->flags[i] |= FLAG_DEFAULT;
2749                 }
2750         }
2751
2752         for (parm=lp_ctx->globals->param_opt; parm; parm=parm->next) {
2753                 if (!(parm->priority & FLAG_CMDLINE)) {
2754                         parm->priority |= FLAG_DEFAULT;
2755                 }
2756         }
2757
2758         for (parm=lp_ctx->sDefault->param_opt; parm; parm=parm->next) {
2759                 if (!(parm->priority & FLAG_CMDLINE)) {
2760                         parm->priority |= FLAG_DEFAULT;
2761                 }
2762         }
2763
2764
2765         return lp_ctx;
2766 }
2767
2768 /**
2769  * Initialise the global parameter structure.
2770  */
2771 struct loadparm_context *loadparm_init_global(bool load_default)
2772 {
2773         if (global_loadparm_context == NULL) {
2774                 global_loadparm_context = loadparm_init(NULL);
2775         }
2776         if (global_loadparm_context == NULL) {
2777                 return NULL;
2778         }
2779         global_loadparm_context->global = true;
2780         if (load_default && !global_loadparm_context->loaded) {
2781                 lpcfg_load_default(global_loadparm_context);
2782         }
2783         global_loadparm_context->refuse_free = true;
2784         return global_loadparm_context;
2785 }
2786
2787 /**
2788  * Initialise the global parameter structure.
2789  */
2790 struct loadparm_context *loadparm_init_s3(TALLOC_CTX *mem_ctx, 
2791                                           const struct loadparm_s3_helpers *s3_fns)
2792 {
2793         struct loadparm_context *loadparm_context = talloc_zero(mem_ctx, struct loadparm_context);
2794         if (!loadparm_context) {
2795                 return NULL;
2796         }
2797         loadparm_context->s3_fns = s3_fns;
2798         loadparm_context->globals = s3_fns->globals;
2799         loadparm_context->flags = s3_fns->flags;
2800
2801         return loadparm_context;
2802 }
2803
2804 const char *lpcfg_configfile(struct loadparm_context *lp_ctx)
2805 {
2806         return lp_ctx->szConfigFile;
2807 }
2808
2809 const char *lp_default_path(void)
2810 {
2811     if (getenv("SMB_CONF_PATH"))
2812         return getenv("SMB_CONF_PATH");
2813     else
2814         return dyn_CONFIGFILE;
2815 }
2816
2817 /**
2818  * Update the internal state of a loadparm context after settings 
2819  * have changed.
2820  */
2821 static bool lpcfg_update(struct loadparm_context *lp_ctx)
2822 {
2823         struct debug_settings settings;
2824         TALLOC_CTX *tmp_ctx;
2825
2826         tmp_ctx = talloc_new(lp_ctx);
2827         if (tmp_ctx == NULL) {
2828                 return false;
2829         }
2830
2831         lpcfg_add_auto_services(lp_ctx, lpcfg_auto_services(lp_ctx, tmp_ctx));
2832
2833         if (!lp_ctx->globals->wins_server_list && lp_ctx->globals->we_are_a_wins_server) {
2834                 lpcfg_do_global_parameter(lp_ctx, "wins server", "127.0.0.1");
2835         }
2836
2837         if (!lp_ctx->global) {
2838                 TALLOC_FREE(tmp_ctx);
2839                 return true;
2840         }
2841
2842         panic_action = lp_ctx->globals->panic_action;
2843
2844         reload_charcnv(lp_ctx);
2845
2846         ZERO_STRUCT(settings);
2847         /* Add any more debug-related smb.conf parameters created in
2848          * future here */
2849         settings.syslog = lp_ctx->globals->syslog;
2850         settings.syslog_only = lp_ctx->globals->syslog_only;
2851         settings.timestamp_logs = lp_ctx->globals->timestamp_logs;
2852         settings.debug_prefix_timestamp = lp_ctx->globals->debug_prefix_timestamp;
2853         settings.debug_hires_timestamp = lp_ctx->globals->debug_hires_timestamp;
2854         settings.debug_pid = lp_ctx->globals->debug_pid;
2855         settings.debug_uid = lp_ctx->globals->debug_uid;
2856         settings.debug_class = lp_ctx->globals->debug_class;
2857         debug_set_settings(&settings);
2858
2859         /* FIXME: This is a bit of a hack, but we can't use a global, since 
2860          * not everything that uses lp also uses the socket library */
2861         if (lpcfg_parm_bool(lp_ctx, NULL, "socket", "testnonblock", false)) {
2862                 setenv("SOCKET_TESTNONBLOCK", "1", 1);
2863         } else {
2864                 unsetenv("SOCKET_TESTNONBLOCK");
2865         }
2866
2867         TALLOC_FREE(tmp_ctx);
2868         return true;
2869 }
2870
2871 bool lpcfg_load_default(struct loadparm_context *lp_ctx)
2872 {
2873     const char *path;
2874
2875     path = lp_default_path();
2876
2877     if (!file_exist(path)) {
2878             /* We allow the default smb.conf file to not exist, 
2879              * basically the equivalent of an empty file. */
2880             return lpcfg_update(lp_ctx);
2881     }
2882
2883     return lpcfg_load(lp_ctx, path);
2884 }
2885
2886 /**
2887  * Load the services array from the services file.
2888  *
2889  * Return True on success, False on failure.
2890  */
2891 bool lpcfg_load(struct loadparm_context *lp_ctx, const char *filename)
2892 {
2893         char *n2;
2894         bool bRetval;
2895
2896         filename = talloc_strdup(lp_ctx, filename);
2897
2898         lp_ctx->szConfigFile = filename;
2899
2900         if (lp_ctx->s3_fns) {
2901                 return lp_ctx->s3_fns->load(filename);
2902         }
2903
2904         lp_ctx->bInGlobalSection = true;
2905         n2 = standard_sub_basic(lp_ctx, lp_ctx->szConfigFile);
2906         DEBUG(2, ("lpcfg_load: refreshing parameters from %s\n", n2));
2907
2908         add_to_file_list(lp_ctx, &lp_ctx->file_lists, lp_ctx->szConfigFile, n2);
2909
2910         /* We get sections first, so have to start 'behind' to make up */
2911         lp_ctx->currentService = NULL;
2912         bRetval = pm_process(n2, do_section, lpcfg_do_parameter, lp_ctx);
2913
2914         /* finish up the last section */
2915         DEBUG(4, ("pm_process() returned %s\n", BOOLSTR(bRetval)));
2916         if (bRetval)
2917                 if (lp_ctx->currentService != NULL)
2918                         bRetval = lpcfg_service_ok(lp_ctx->currentService);
2919
2920         bRetval = bRetval && lpcfg_update(lp_ctx);
2921
2922         /* we do this unconditionally, so that it happens even
2923            for a missing smb.conf */
2924         reload_charcnv(lp_ctx);
2925
2926         if (bRetval == true) {
2927                 /* set this up so that any child python tasks will
2928                    find the right smb.conf */
2929                 setenv("SMB_CONF_PATH", filename, 1);
2930
2931                 /* set the context used by the lp_*() function
2932                    varients */
2933                 global_loadparm_context = lp_ctx;
2934                 lp_ctx->loaded = true;
2935         }
2936
2937         return bRetval;
2938 }
2939
2940 /**
2941  * Return the max number of services.
2942  */
2943
2944 int lpcfg_numservices(struct loadparm_context *lp_ctx)
2945 {
2946         if (lp_ctx->s3_fns) {
2947                 return lp_ctx->s3_fns->get_numservices();
2948         }
2949
2950         return lp_ctx->iNumServices;
2951 }
2952
2953 /**
2954  * Display the contents of the services array in human-readable form.
2955  */
2956
2957 void lpcfg_dump(struct loadparm_context *lp_ctx, FILE *f, bool show_defaults,
2958              int maxtoprint)
2959 {
2960         int iService;
2961
2962         if (lp_ctx->s3_fns) {
2963                 lp_ctx->s3_fns->dump(f, show_defaults, maxtoprint);
2964                 return;
2965         }
2966
2967         lpcfg_dump_globals(lp_ctx, f, show_defaults);
2968
2969         lpcfg_dump_a_service(lp_ctx->sDefault, lp_ctx->sDefault, f, lp_ctx->flags, show_defaults);
2970
2971         for (iService = 0; iService < maxtoprint; iService++)
2972                 lpcfg_dump_one(f, show_defaults, lp_ctx->services[iService], lp_ctx->sDefault);
2973 }
2974
2975 /**
2976  * Display the contents of one service in human-readable form.
2977  */
2978 void lpcfg_dump_one(FILE *f, bool show_defaults, struct loadparm_service *service, struct loadparm_service *sDefault)
2979 {
2980         if (service != NULL) {
2981                 if (service->szService[0] == '\0')
2982                         return;
2983                 lpcfg_dump_a_service(service, sDefault, f, NULL, show_defaults);
2984         }
2985 }
2986
2987 struct loadparm_service *lpcfg_servicebynum(struct loadparm_context *lp_ctx,
2988                                             int snum)
2989 {
2990         if (lp_ctx->s3_fns) {
2991                 return lp_ctx->s3_fns->get_servicebynum(snum);
2992         }
2993
2994         return lp_ctx->services[snum];
2995 }
2996
2997 struct loadparm_service *lpcfg_service(struct loadparm_context *lp_ctx,
2998                                     const char *service_name)
2999 {
3000         int iService;
3001         char *serviceName;
3002
3003         if (lp_ctx->s3_fns) {
3004                 return lp_ctx->s3_fns->get_service(service_name);
3005         }
3006
3007         for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--) {
3008                 if (lp_ctx->services[iService] &&
3009                     lp_ctx->services[iService]->szService) {
3010                         /*
3011                          * The substitution here is used to support %U is
3012                          * service names
3013                          */
3014                         serviceName = standard_sub_basic(
3015                                         lp_ctx->services[iService],
3016                                         lp_ctx->services[iService]->szService);
3017                         if (strequal(serviceName, service_name)) {
3018                                 talloc_free(serviceName);
3019                                 return lp_ctx->services[iService];
3020                         }
3021                         talloc_free(serviceName);
3022                 }
3023         }
3024
3025         DEBUG(7,("lpcfg_servicenumber: couldn't find %s\n", service_name));
3026         return NULL;
3027 }
3028
3029 const char *lpcfg_servicename(const struct loadparm_service *service)
3030 {
3031         return lpcfg_string((const char *)service->szService);
3032 }
3033
3034 /**
3035  * A useful volume label function.
3036  */
3037 const char *lpcfg_volume_label(struct loadparm_service *service, struct loadparm_service *sDefault)
3038 {
3039         const char *ret;
3040         ret = lpcfg_string((const char *)((service != NULL && service->volume != NULL) ?
3041                                        service->volume : sDefault->volume));
3042         if (!*ret)
3043                 return lpcfg_servicename(service);
3044         return ret;
3045 }
3046
3047 /**
3048  * Return the correct printer name.
3049  */
3050 const char *lpcfg_printername(struct loadparm_service *service, struct loadparm_service *sDefault)
3051 {
3052         const char *ret;
3053         ret = lpcfg_string((const char *)((service != NULL && service->_printername != NULL) ?
3054                                        service->_printername : sDefault->_printername));
3055         if (ret == NULL || (ret != NULL && *ret == '\0'))
3056                 ret = lpcfg_servicename(service);
3057
3058         return ret;
3059 }
3060
3061
3062 /**
3063  * Return the max print jobs per queue.
3064  */
3065 int lpcfg_maxprintjobs(struct loadparm_service *service, struct loadparm_service *sDefault)
3066 {
3067         int maxjobs = (service != NULL) ? service->iMaxPrintJobs : sDefault->iMaxPrintJobs;
3068         if (maxjobs <= 0 || maxjobs >= PRINT_MAX_JOBID)
3069                 maxjobs = PRINT_MAX_JOBID - 1;
3070
3071         return maxjobs;
3072 }
3073
3074 struct smb_iconv_handle *lpcfg_iconv_handle(struct loadparm_context *lp_ctx)
3075 {
3076         if (lp_ctx == NULL) {
3077                 return get_iconv_handle();
3078         }
3079         return lp_ctx->iconv_handle;
3080 }
3081
3082 _PUBLIC_ void reload_charcnv(struct loadparm_context *lp_ctx)
3083 {
3084         struct smb_iconv_handle *old_ic = lp_ctx->iconv_handle;
3085         if (!lp_ctx->global) {
3086                 return;
3087         }
3088
3089         if (old_ic == NULL) {
3090                 old_ic = global_iconv_handle;
3091         }
3092         lp_ctx->iconv_handle = smb_iconv_handle_reinit_lp(lp_ctx, lp_ctx, old_ic);
3093         global_iconv_handle = lp_ctx->iconv_handle;
3094 }
3095
3096 _PUBLIC_ char *lpcfg_tls_keyfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3097 {
3098         return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_keyfile(lp_ctx));
3099 }
3100
3101 _PUBLIC_ char *lpcfg_tls_certfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3102 {
3103         return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_certfile(lp_ctx));
3104 }
3105
3106 _PUBLIC_ char *lpcfg_tls_cafile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3107 {
3108         return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_cafile(lp_ctx));
3109 }
3110
3111 _PUBLIC_ char *lpcfg_tls_crlfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3112 {
3113         return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_crlfile(lp_ctx));
3114 }
3115
3116 _PUBLIC_ char *lpcfg_tls_dhpfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3117 {
3118         return lpcfg_private_path(mem_ctx, lp_ctx, lpcfg__tls_dhpfile(lp_ctx));
3119 }
3120
3121 struct gensec_settings *lpcfg_gensec_settings(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3122 {
3123         struct gensec_settings *settings = talloc_zero(mem_ctx, struct gensec_settings);
3124         if (settings == NULL)
3125                 return NULL;
3126         SMB_ASSERT(lp_ctx != NULL);
3127         settings->lp_ctx = talloc_reference(settings, lp_ctx);
3128         settings->target_hostname = lpcfg_parm_string(lp_ctx, NULL, "gensec", "target_hostname");
3129         return settings;
3130 }
3131
3132 int lpcfg_server_role(struct loadparm_context *lp_ctx)
3133 {
3134         int domain_master = lpcfg__domain_master(lp_ctx);
3135
3136         return lp_find_server_role(lpcfg__server_role(lp_ctx),
3137                                    lpcfg__security(lp_ctx),
3138                                    lpcfg__domain_logons(lp_ctx),
3139                                    (domain_master == true) ||
3140                                    (domain_master == Auto));
3141 }
3142
3143 int lpcfg_security(struct loadparm_context *lp_ctx)
3144 {
3145         return lp_find_security(lpcfg__server_role(lp_ctx),
3146                                 lpcfg__security(lp_ctx));
3147 }
3148
3149 int lpcfg_client_max_protocol(struct loadparm_context *lp_ctx)
3150 {
3151         int client_max_protocol = lpcfg__client_max_protocol(lp_ctx);
3152         if (client_max_protocol == PROTOCOL_DEFAULT) {
3153                 return PROTOCOL_NT1;
3154         }
3155         return client_max_protocol;
3156 }
3157
3158 bool lpcfg_server_signing_allowed(struct loadparm_context *lp_ctx, bool *mandatory)
3159 {
3160         bool allowed = true;
3161         enum smb_signing_setting signing_setting = lpcfg_server_signing(lp_ctx);
3162
3163         *mandatory = false;
3164
3165         if (signing_setting == SMB_SIGNING_DEFAULT) {
3166                 /*
3167                  * If we are a domain controller, SMB signing is
3168                  * really important, as it can prevent a number of
3169                  * attacks on communications between us and the
3170                  * clients
3171                  *
3172                  * However, it really sucks (no sendfile, CPU
3173                  * overhead) performance-wise when used on a
3174                  * file server, so disable it by default
3175                  * on non-DCs
3176                  */
3177
3178                 if (lpcfg_server_role(lp_ctx) >= ROLE_ACTIVE_DIRECTORY_DC) {
3179                         signing_setting = SMB_SIGNING_REQUIRED;
3180                 } else {
3181                         signing_setting = SMB_SIGNING_OFF;
3182                 }
3183         }
3184
3185         switch (signing_setting) {
3186         case SMB_SIGNING_REQUIRED:
3187                 *mandatory = true;
3188                 break;
3189         case SMB_SIGNING_IF_REQUIRED:
3190                 break;
3191         case SMB_SIGNING_DEFAULT:
3192         case SMB_SIGNING_OFF:
3193                 allowed = false;
3194                 break;
3195         }
3196
3197         return allowed;
3198 }
3199
3200 int lpcfg_tdb_hash_size(struct loadparm_context *lp_ctx, const char *name)
3201 {
3202         const char *base;
3203
3204         if (name == NULL) {
3205                 return 0;
3206         }
3207
3208         base = strrchr_m(name, '/');
3209         if (base != NULL) {
3210                 base += 1;
3211         } else {
3212                 base = name;
3213         }
3214         return lpcfg_parm_int(lp_ctx, NULL, "tdb_hashsize", base, 0);
3215
3216 }
3217
3218 int lpcfg_tdb_flags(struct loadparm_context *lp_ctx, int tdb_flags)
3219 {
3220         if (!lpcfg_use_mmap(lp_ctx)) {
3221                 tdb_flags |= TDB_NOMMAP;
3222         }
3223         return tdb_flags;
3224 }