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