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