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