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