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