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