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