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