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