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