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