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