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