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