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