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