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