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