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