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