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