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