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