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