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