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