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