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