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