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