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