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