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