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