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