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