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