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