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