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