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