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