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