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