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