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