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