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