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