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