credentials: Prioritise command-line specified options above defaults from smb.conf
[idra/samba.git] / lib / 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 "../lib/util/dlinklist.h"
62 #include "lib/param/param.h"
63 #include "lib/param/loadparm.h"
64 #include "auth/gensec/gensec.h"
65 #include "s3_param.h"
66 #include "lib/util/bitmap.h"
67 #include "libcli/smb/smb_constants.h"
68
69 #define standard_sub_basic talloc_strdup
70
71 static bool do_parameter(const char *, const char *, void *);
72 static bool defaults_saved = false;
73
74 #define LOADPARM_EXTRA_GLOBALS \
75         struct parmlist_entry *param_opt;                               \
76         char *szRealm;                                                  \
77         char *tls_keyfile;                                              \
78         char *tls_certfile;                                             \
79         char *tls_cafile;                                               \
80         char *tls_crlfile;                                              \
81         char *tls_dhpfile;                                              \
82         char *loglevel;                                                 \
83         char *panic_action;                                             \
84         int bPreferredMaster;
85
86 #include "param_global.h"
87
88 #define NUMPARAMETERS (sizeof(parm_table) / sizeof(struct parm_struct))
89
90
91 /* prototypes for the special type handlers */
92 static bool handle_include(struct loadparm_context *lp_ctx, int unused,
93                            const char *pszParmValue, char **ptr);
94 static bool handle_realm(struct loadparm_context *lp_ctx, int unused,
95                          const char *pszParmValue, char **ptr);
96 static bool handle_copy(struct loadparm_context *lp_ctx, int unused,
97                         const char *pszParmValue, char **ptr);
98 static bool handle_debuglevel(struct loadparm_context *lp_ctx, int unused,
99                               const char *pszParmValue, char **ptr);
100 static bool handle_logfile(struct loadparm_context *lp_ctx, int unused,
101                            const char *pszParmValue, char **ptr);
102
103 static const struct enum_list enum_protocol[] = {
104         {PROTOCOL_SMB2_02, "SMB2"},
105         {PROTOCOL_SMB2_02, "SMB2_02"},
106         {PROTOCOL_NT1, "NT1"},
107         {PROTOCOL_LANMAN2, "LANMAN2"},
108         {PROTOCOL_LANMAN1, "LANMAN1"},
109         {PROTOCOL_CORE, "CORE"},
110         {PROTOCOL_COREPLUS, "COREPLUS"},
111         {PROTOCOL_COREPLUS, "CORE+"},
112         {-1, NULL}
113 };
114
115 static const struct enum_list enum_security[] = {
116         {SEC_SHARE, "SHARE"},
117         {SEC_USER, "USER"},
118         {SEC_ADS, "ADS"},
119         {-1, NULL}
120 };
121
122 static const struct enum_list enum_bool_auto[] = {
123         {false, "No"},
124         {false, "False"},
125         {false, "0"},
126         {true, "Yes"},
127         {true, "True"},
128         {true, "1"},
129         {Auto, "Auto"},
130         {-1, NULL}
131 };
132
133 /* Client-side offline caching policy types */
134
135 static const struct enum_list enum_csc_policy[] = {
136         {CSC_POLICY_MANUAL, "manual"},
137         {CSC_POLICY_DOCUMENTS, "documents"},
138         {CSC_POLICY_PROGRAMS, "programs"},
139         {CSC_POLICY_DISABLE, "disable"},
140         {-1, NULL}
141 };
142
143 /* SMB signing types. */
144 static const struct enum_list enum_smb_signing_vals[] = {
145         {SMB_SIGNING_OFF, "No"},
146         {SMB_SIGNING_OFF, "False"},
147         {SMB_SIGNING_OFF, "0"},
148         {SMB_SIGNING_OFF, "Off"},
149         {SMB_SIGNING_OFF, "disabled"},
150         {SMB_SIGNING_SUPPORTED, "Yes"},
151         {SMB_SIGNING_SUPPORTED, "True"},
152         {SMB_SIGNING_SUPPORTED, "1"},
153         {SMB_SIGNING_SUPPORTED, "On"},
154         {SMB_SIGNING_SUPPORTED, "enabled"},
155         {SMB_SIGNING_REQUIRED, "required"},
156         {SMB_SIGNING_REQUIRED, "mandatory"},
157         {SMB_SIGNING_REQUIRED, "force"},
158         {SMB_SIGNING_REQUIRED, "forced"},
159         {SMB_SIGNING_REQUIRED, "enforced"},
160         {SMB_SIGNING_AUTO, "auto"},
161         {-1, NULL}
162 };
163
164 static const struct enum_list enum_server_role[] = {
165         {ROLE_STANDALONE, "standalone"},
166         {ROLE_DOMAIN_MEMBER, "member server"},
167         {ROLE_DOMAIN_MEMBER, "member"},
168         {ROLE_DOMAIN_CONTROLLER, "domain controller"},
169         {ROLE_DOMAIN_CONTROLLER, "dc"},
170         {-1, NULL}
171 };
172
173 #define GLOBAL_VAR(name) offsetof(struct loadparm_global, name)
174 #define LOCAL_VAR(name) offsetof(struct loadparm_service, name)
175
176 static struct parm_struct parm_table[] = {
177         {
178                 .label          = "server role",
179                 .type           = P_ENUM,
180                 .p_class        = P_GLOBAL,
181                 .offset         = GLOBAL_VAR(server_role),
182                 .special        = NULL,
183                 .enum_list      = enum_server_role
184         },
185         {
186                 .label          = "dos charset",
187                 .type           = P_STRING,
188                 .p_class        = P_GLOBAL,
189                 .offset         = GLOBAL_VAR(dos_charset),
190                 .special        = NULL,
191                 .enum_list      = NULL
192         },
193         {
194                 .label          = "unix charset",
195                 .type           = P_STRING,
196                 .p_class        = P_GLOBAL,
197                 .offset         = GLOBAL_VAR(unix_charset),
198                 .special        = NULL,
199                 .enum_list      = NULL
200         },
201         {
202                 .label          = "ncalrpc dir",
203                 .type           = P_STRING,
204                 .p_class        = P_GLOBAL,
205                 .offset         = GLOBAL_VAR(ncalrpc_dir),
206                 .special        = NULL,
207                 .enum_list      = NULL
208         },
209         {
210                 .label          = "comment",
211                 .type           = P_STRING,
212                 .p_class        = P_LOCAL,
213                 .offset         = LOCAL_VAR(comment),
214                 .special        = NULL,
215                 .enum_list      = NULL
216         },
217         {
218                 .label          = "path",
219                 .type           = P_STRING,
220                 .p_class        = P_LOCAL,
221                 .offset         = LOCAL_VAR(szPath),
222                 .special        = NULL,
223                 .enum_list      = NULL
224         },
225         {
226                 .label          = "directory",
227                 .type           = P_STRING,
228                 .p_class        = P_LOCAL,
229                 .offset         = LOCAL_VAR(szPath),
230                 .special        = NULL,
231                 .enum_list      = NULL
232         },
233         {
234                 .label          = "workgroup",
235                 .type           = P_USTRING,
236                 .p_class        = P_GLOBAL,
237                 .offset         = GLOBAL_VAR(szWorkgroup),
238                 .special        = NULL,
239                 .enum_list      = NULL
240         },
241         {
242                 .label          = "realm",
243                 .type           = P_STRING,
244                 .p_class        = P_GLOBAL,
245                 .offset         = GLOBAL_VAR(szRealm),
246                 .special        = handle_realm,
247                 .enum_list      = NULL
248         },
249         {
250                 .label          = "netbios name",
251                 .type           = P_USTRING,
252                 .p_class        = P_GLOBAL,
253                 .offset         = GLOBAL_VAR(szNetbiosName),
254                 .special        = NULL,
255                 .enum_list      = NULL
256         },
257         {
258                 .label          = "netbios aliases",
259                 .type           = P_LIST,
260                 .p_class        = P_GLOBAL,
261                 .offset         = GLOBAL_VAR(szNetbiosAliases),
262                 .special        = NULL,
263                 .enum_list      = NULL
264         },
265         {
266                 .label          = "netbios scope",
267                 .type           = P_USTRING,
268                 .p_class        = P_GLOBAL,
269                 .offset         = GLOBAL_VAR(szNetbiosScope),
270                 .special        = NULL,
271                 .enum_list      = NULL
272         },
273         {
274                 .label          = "server string",
275                 .type           = P_STRING,
276                 .p_class        = P_GLOBAL,
277                 .offset         = GLOBAL_VAR(szServerString),
278                 .special        = NULL,
279                 .enum_list      = NULL
280         },
281         {
282                 .label          = "interfaces",
283                 .type           = P_LIST,
284                 .p_class        = P_GLOBAL,
285                 .offset         = GLOBAL_VAR(szInterfaces),
286                 .special        = NULL,
287                 .enum_list      = NULL
288         },
289         {
290                 .label          = "bind interfaces only",
291                 .type           = P_BOOL,
292                 .p_class        = P_GLOBAL,
293                 .offset         = GLOBAL_VAR(bBindInterfacesOnly),
294                 .special        = NULL,
295                 .enum_list      = NULL
296         },
297         {
298                 .label          = "ntvfs handler",
299                 .type           = P_LIST,
300                 .p_class        = P_LOCAL,
301                 .offset         = LOCAL_VAR(ntvfs_handler),
302                 .special        = NULL,
303                 .enum_list      = NULL
304         },
305         {
306                 .label          = "ntptr providor",
307                 .type           = P_STRING,
308                 .p_class        = P_GLOBAL,
309                 .offset         = GLOBAL_VAR(ntptr_providor),
310                 .special        = NULL,
311                 .enum_list      = NULL
312         },
313         {
314                 .label          = "passdb backend",
315                 .type           = P_STRING,
316                 .p_class        = P_GLOBAL,
317                 .offset         = GLOBAL_VAR(passdb_backend),
318                 .special        = NULL,
319                 .enum_list      = NULL
320         },
321         {
322                 .label          = "dcerpc endpoint servers",
323                 .type           = P_LIST,
324                 .p_class        = P_GLOBAL,
325                 .offset         = GLOBAL_VAR(dcerpc_ep_servers),
326                 .special        = NULL,
327                 .enum_list      = NULL
328         },
329         {
330                 .label          = "server services",
331                 .type           = P_LIST,
332                 .p_class        = P_GLOBAL,
333                 .offset         = GLOBAL_VAR(server_services),
334                 .special        = NULL,
335                 .enum_list      = NULL
336         },
337
338         {
339                 .label          = "security",
340                 .type           = P_ENUM,
341                 .p_class        = P_GLOBAL,
342                 .offset         = GLOBAL_VAR(security),
343                 .special        = NULL,
344                 .enum_list      = enum_security
345         },
346         {
347                 .label          = "encrypt passwords",
348                 .type           = P_BOOL,
349                 .p_class        = P_GLOBAL,
350                 .offset         = GLOBAL_VAR(bEncryptPasswords),
351                 .special        = NULL,
352                 .enum_list      = NULL
353         },
354         {
355                 .label          = "null passwords",
356                 .type           = P_BOOL,
357                 .p_class        = P_GLOBAL,
358                 .offset         = GLOBAL_VAR(bNullPasswords),
359                 .special        = NULL,
360                 .enum_list      = NULL
361         },
362         {
363                 .label          = "obey pam restrictions",
364                 .type           = P_BOOL,
365                 .p_class        = P_GLOBAL,
366                 .offset         = GLOBAL_VAR(bObeyPamRestrictions),
367                 .special        = NULL,
368                 .enum_list      = NULL
369         },
370         {
371                 .label          = "password server",
372                 .type           = P_LIST,
373                 .p_class        = P_GLOBAL,
374                 .offset         = GLOBAL_VAR(szPasswordServers),
375                 .special        = NULL,
376                 .enum_list      = NULL
377         },
378         {
379                 .label          = "spoolss database",
380                 .type           = P_STRING,
381                 .p_class        = P_GLOBAL,
382                 .offset         = GLOBAL_VAR(szSPOOLSS_URL),
383                 .special        = NULL,
384                 .enum_list      = NULL
385         },
386         {
387                 .label          = "wins config database",
388                 .type           = P_STRING,
389                 .p_class        = P_GLOBAL,
390                 .offset         = GLOBAL_VAR(szWINS_CONFIG_URL),
391                 .special        = NULL,
392                 .enum_list      = NULL
393         },
394         {
395                 .label          = "wins database",
396                 .type           = P_STRING,
397                 .p_class        = P_GLOBAL,
398                 .offset         = GLOBAL_VAR(szWINS_URL),
399                 .special        = NULL,
400                 .enum_list      = NULL
401         },
402         {
403                 .label          = "private dir",
404                 .type           = P_STRING,
405                 .p_class        = P_GLOBAL,
406                 .offset         = GLOBAL_VAR(szPrivateDir),
407                 .special        = NULL,
408                 .enum_list      = NULL
409         },
410         {
411                 .label          = "passwd chat",
412                 .type           = P_STRING,
413                 .p_class        = P_GLOBAL,
414                 .offset         = GLOBAL_VAR(szPasswdChat),
415                 .special        = NULL,
416                 .enum_list      = NULL
417         },
418         {
419                 .label          = "password level",
420                 .type           = P_INTEGER,
421                 .p_class        = P_GLOBAL,
422                 .offset         = GLOBAL_VAR(pwordlevel),
423                 .special        = NULL,
424                 .enum_list      = NULL
425         },
426         {
427                 .label          = "lanman auth",
428                 .type           = P_BOOL,
429                 .p_class        = P_GLOBAL,
430                 .offset         = GLOBAL_VAR(bLanmanAuth),
431                 .special        = NULL,
432                 .enum_list      = NULL
433         },
434         {
435                 .label          = "ntlm auth",
436                 .type           = P_BOOL,
437                 .p_class        = P_GLOBAL,
438                 .offset         = GLOBAL_VAR(bNTLMAuth),
439                 .special        = NULL,
440                 .enum_list      = NULL
441         },
442         {
443                 .label          = "client NTLMv2 auth",
444                 .type           = P_BOOL,
445                 .p_class        = P_GLOBAL,
446                 .offset         = GLOBAL_VAR(bClientNTLMv2Auth),
447                 .special        = NULL,
448                 .enum_list      = NULL
449         },
450         {
451                 .label          = "client lanman auth",
452                 .type           = P_BOOL,
453                 .p_class        = P_GLOBAL,
454                 .offset         = GLOBAL_VAR(bClientLanManAuth),
455                 .special        = NULL,
456                 .enum_list      = NULL
457         },
458         {
459                 .label          = "client plaintext auth",
460                 .type           = P_BOOL,
461                 .p_class        = P_GLOBAL,
462                 .offset         = GLOBAL_VAR(bClientPlaintextAuth),
463                 .special        = NULL,
464                 .enum_list      = NULL
465         },
466         {
467                 .label          = "client use spnego principal",
468                 .type           = P_BOOL,
469                 .p_class        = P_GLOBAL,
470                 .offset         = GLOBAL_VAR(client_use_spnego_principal),
471                 .special        = NULL,
472                 .enum_list      = NULL
473         },
474
475         {
476                 .label          = "read only",
477                 .type           = P_BOOL,
478                 .p_class        = P_LOCAL,
479                 .offset         = LOCAL_VAR(bRead_only),
480                 .special        = NULL,
481                 .enum_list      = NULL
482         },
483
484         {
485                 .label          = "create mask",
486                 .type           = P_OCTAL,
487                 .p_class        = P_LOCAL,
488                 .offset         = LOCAL_VAR(iCreate_mask),
489                 .special        = NULL,
490                 .enum_list      = NULL
491         },
492         {
493                 .label          = "force create mode",
494                 .type           = P_OCTAL,
495                 .p_class        = P_LOCAL,
496                 .offset         = LOCAL_VAR(iCreate_force_mode),
497                 .special        = NULL,
498                 .enum_list      = NULL
499         },
500         {
501                 .label          = "directory mask",
502                 .type           = P_OCTAL,
503                 .p_class        = P_LOCAL,
504                 .offset         = LOCAL_VAR(iDir_mask),
505                 .special        = NULL,
506                 .enum_list      = NULL
507         },
508         {
509                 .label          = "force directory mode",
510                 .type           = P_OCTAL,
511                 .p_class        = P_LOCAL,
512                 .offset         = LOCAL_VAR(iDir_force_mode),
513                 .special        = NULL,
514                 .enum_list      = NULL
515         },
516
517         {
518                 .label          = "hosts allow",
519                 .type           = P_LIST,
520                 .p_class        = P_LOCAL,
521                 .offset         = LOCAL_VAR(szHostsallow),
522                 .special        = NULL,
523                 .enum_list      = NULL
524         },
525         {
526                 .label          = "hosts deny",
527                 .type           = P_LIST,
528                 .p_class        = P_LOCAL,
529                 .offset         = LOCAL_VAR(szHostsdeny),
530                 .special        = NULL,
531                 .enum_list      = NULL
532         },
533
534         {
535                 .label          = "log level",
536                 .type           = P_STRING,
537                 .p_class        = P_GLOBAL,
538                 .offset         = GLOBAL_VAR(loglevel),
539                 .special        = handle_debuglevel,
540                 .enum_list      = NULL
541         },
542         {
543                 .label          = "debuglevel",
544                 .type           = P_STRING,
545                 .p_class        = P_GLOBAL,
546                 .offset         = GLOBAL_VAR(loglevel),
547                 .special        = handle_debuglevel,
548                 .enum_list      = NULL
549         },
550         {
551                 .label          = "log file",
552                 .type           = P_STRING,
553                 .p_class        = P_GLOBAL,
554                 .offset         = GLOBAL_VAR(logfile),
555                 .special        = handle_logfile,
556                 .enum_list      = NULL
557         },
558
559         {
560                 .label          = "smb ports",
561                 .type           = P_LIST,
562                 .p_class        = P_GLOBAL,
563                 .offset         = GLOBAL_VAR(smb_ports),
564                 .special        = NULL,
565                 .enum_list      = NULL
566         },
567         {
568                 .label          = "nbt port",
569                 .type           = P_INTEGER,
570                 .p_class        = P_GLOBAL,
571                 .offset         = GLOBAL_VAR(nbt_port),
572                 .special        = NULL,
573                 .enum_list      = NULL
574         },
575         {
576                 .label          = "dgram port",
577                 .type           = P_INTEGER,
578                 .p_class        = P_GLOBAL,
579                 .offset         = GLOBAL_VAR(dgram_port),
580                 .special        = NULL,
581                 .enum_list      = NULL
582         },
583         {
584                 .label          = "cldap port",
585                 .type           = P_INTEGER,
586                 .p_class        = P_GLOBAL,
587                 .offset         = GLOBAL_VAR(cldap_port),
588                 .special        = NULL,
589                 .enum_list      = NULL
590         },
591         {
592                 .label          = "krb5 port",
593                 .type           = P_INTEGER,
594                 .p_class        = P_GLOBAL,
595                 .offset         = GLOBAL_VAR(krb5_port),
596                 .special        = NULL,
597                 .enum_list      = NULL
598         },
599         {
600                 .label          = "kpasswd port",
601                 .type           = P_INTEGER,
602                 .p_class        = P_GLOBAL,
603                 .offset         = GLOBAL_VAR(kpasswd_port),
604                 .special        = NULL,
605                 .enum_list      = NULL
606         },
607         {
608                 .label          = "web port",
609                 .type           = P_INTEGER,
610                 .p_class        = P_GLOBAL,
611                 .offset         = GLOBAL_VAR(web_port),
612                 .special        = NULL,
613                 .enum_list      = NULL
614         },
615         {
616                 .label          = "tls enabled",
617                 .type           = P_BOOL,
618                 .p_class        = P_GLOBAL,
619                 .offset         = GLOBAL_VAR(tls_enabled),
620                 .special        = NULL,
621                 .enum_list      = NULL
622         },
623         {
624                 .label          = "tls keyfile",
625                 .type           = P_STRING,
626                 .p_class        = P_GLOBAL,
627                 .offset         = GLOBAL_VAR(tls_keyfile),
628                 .special        = NULL,
629                 .enum_list      = NULL
630         },
631         {
632                 .label          = "tls certfile",
633                 .type           = P_STRING,
634                 .p_class        = P_GLOBAL,
635                 .offset         = GLOBAL_VAR(tls_certfile),
636                 .special        = NULL,
637                 .enum_list      = NULL
638         },
639         {
640                 .label          = "tls cafile",
641                 .type           = P_STRING,
642                 .p_class        = P_GLOBAL,
643                 .offset         = GLOBAL_VAR(tls_cafile),
644                 .special        = NULL,
645                 .enum_list      = NULL
646         },
647         {
648                 .label          = "tls crlfile",
649                 .type           = P_STRING,
650                 .p_class        = P_GLOBAL,
651                 .offset         = GLOBAL_VAR(tls_crlfile),
652                 .special        = NULL,
653                 .enum_list      = NULL
654         },
655         {
656                 .label          = "tls dh params file",
657                 .type           = P_STRING,
658                 .p_class        = P_GLOBAL,
659                 .offset         = GLOBAL_VAR(tls_dhpfile),
660                 .special        = NULL,
661                 .enum_list      = NULL
662         },
663         {
664                 .label          = "large readwrite",
665                 .type           = P_BOOL,
666                 .p_class        = P_GLOBAL,
667                 .offset         = GLOBAL_VAR(bLargeReadwrite),
668                 .special        = NULL,
669                 .enum_list      = NULL
670         },
671         {
672                 .label          = "server max protocol",
673                 .type           = P_ENUM,
674                 .p_class        = P_GLOBAL,
675                 .offset         = GLOBAL_VAR(srv_maxprotocol),
676                 .special        = NULL,
677                 .enum_list      = enum_protocol
678         },
679         {
680                 .label          = "server min protocol",
681                 .type           = P_ENUM,
682                 .p_class        = P_GLOBAL,
683                 .offset         = GLOBAL_VAR(srv_minprotocol),
684                 .special        = NULL,
685                 .enum_list      = enum_protocol
686         },
687         {
688                 .label          = "client max protocol",
689                 .type           = P_ENUM,
690                 .p_class        = P_GLOBAL,
691                 .offset         = GLOBAL_VAR(cli_maxprotocol),
692                 .special        = NULL,
693                 .enum_list      = enum_protocol
694         },
695         {
696                 .label          = "client min protocol",
697                 .type           = P_ENUM,
698                 .p_class        = P_GLOBAL,
699                 .offset         = GLOBAL_VAR(cli_minprotocol),
700                 .special        = NULL,
701                 .enum_list      = enum_protocol
702         },
703         {
704                 .label          = "unicode",
705                 .type           = P_BOOL,
706                 .p_class        = P_GLOBAL,
707                 .offset         = GLOBAL_VAR(bUnicode),
708                 .special        = NULL,
709                 .enum_list      = NULL
710         },
711         {
712                 .label          = "read raw",
713                 .type           = P_BOOL,
714                 .p_class        = P_GLOBAL,
715                 .offset         = GLOBAL_VAR(bReadRaw),
716                 .special        = NULL,
717                 .enum_list      = NULL
718         },
719         {
720                 .label          = "write raw",
721                 .type           = P_BOOL,
722                 .p_class        = P_GLOBAL,
723                 .offset         = GLOBAL_VAR(bWriteRaw),
724                 .special        = NULL,
725                 .enum_list      = NULL
726         },
727         {
728                 .label          = "disable netbios",
729                 .type           = P_BOOL,
730                 .p_class        = P_GLOBAL,
731                 .offset         = GLOBAL_VAR(bDisableNetbios),
732                 .special        = NULL,
733                 .enum_list      = NULL
734         },
735
736         {
737                 .label          = "nt status support",
738                 .type           = P_BOOL,
739                 .p_class        = P_GLOBAL,
740                 .offset         = GLOBAL_VAR(bNTStatusSupport),
741                 .special        = NULL,
742                 .enum_list      = NULL
743         },
744
745         {
746                 .label          = "max mux",
747                 .type           = P_INTEGER,
748                 .p_class        = P_GLOBAL,
749                 .offset         = GLOBAL_VAR(max_mux),
750                 .special        = NULL,
751                 .enum_list      = NULL
752         },
753         {
754                 .label          = "max xmit",
755                 .type           = P_BYTES,
756                 .p_class        = P_GLOBAL,
757                 .offset         = GLOBAL_VAR(max_xmit),
758                 .special        = NULL,
759                 .enum_list      = NULL
760         },
761
762         {
763                 .label          = "name resolve order",
764                 .type           = P_LIST,
765                 .p_class        = P_GLOBAL,
766                 .offset         = GLOBAL_VAR(szNameResolveOrder),
767                 .special        = NULL,
768                 .enum_list      = NULL
769         },
770         {
771                 .label          = "max wins ttl",
772                 .type           = P_INTEGER,
773                 .p_class        = P_GLOBAL,
774                 .offset         = GLOBAL_VAR(max_wins_ttl),
775                 .special        = NULL,
776                 .enum_list      = NULL
777         },
778         {
779                 .label          = "min wins ttl",
780                 .type           = P_INTEGER,
781                 .p_class        = P_GLOBAL,
782                 .offset         = GLOBAL_VAR(min_wins_ttl),
783                 .special        = NULL,
784                 .enum_list      = NULL
785         },
786         {
787                 .label          = "time server",
788                 .type           = P_BOOL,
789                 .p_class        = P_GLOBAL,
790                 .offset         = GLOBAL_VAR(bTimeServer),
791                 .special        = NULL,
792                 .enum_list      = NULL
793         },
794         {
795                 .label          = "unix extensions",
796                 .type           = P_BOOL,
797                 .p_class        = P_GLOBAL,
798                 .offset         = GLOBAL_VAR(bUnixExtensions),
799                 .special        = NULL,
800                 .enum_list      = NULL
801         },
802         {
803                 .label          = "use spnego",
804                 .type           = P_BOOL,
805                 .p_class        = P_GLOBAL,
806                 .offset         = GLOBAL_VAR(bUseSpnego),
807                 .special        = NULL,
808                 .enum_list      = NULL
809         },
810         {
811                 .label          = "server signing",
812                 .type           = P_ENUM,
813                 .p_class        = P_GLOBAL,
814                 .offset         = GLOBAL_VAR(server_signing),
815                 .special        = NULL,
816                 .enum_list      = enum_smb_signing_vals
817         },
818         {
819                 .label          = "client signing",
820                 .type           = P_ENUM,
821                 .p_class        = P_GLOBAL,
822                 .offset         = GLOBAL_VAR(client_signing),
823                 .special        = NULL,
824                 .enum_list      = enum_smb_signing_vals
825         },
826         {
827                 .label          = "rpc big endian",
828                 .type           = P_BOOL,
829                 .p_class        = P_GLOBAL,
830                 .offset         = GLOBAL_VAR(bRpcBigEndian),
831                 .special        = NULL,
832                 .enum_list      = NULL
833         },
834
835         {
836                 .label          = "max connections",
837                 .type           = P_INTEGER,
838                 .p_class        = P_LOCAL,
839                 .offset         = LOCAL_VAR(iMaxConnections),
840                 .special        = NULL,
841                 .enum_list      = NULL
842         },
843         {
844                 .label          = "paranoid server security",
845                 .type           = P_BOOL,
846                 .p_class        = P_GLOBAL,
847                 .offset         = GLOBAL_VAR(paranoid_server_security),
848                 .special        = NULL,
849                 .enum_list      = NULL
850         },
851         {
852                 .label          = "socket options",
853                 .type           = P_STRING,
854                 .p_class        = P_GLOBAL,
855                 .offset         = GLOBAL_VAR(socket_options),
856                 .special        = NULL,
857                 .enum_list      = NULL
858         },
859
860         {
861                 .label          = "strict sync",
862                 .type           = P_BOOL,
863                 .p_class        = P_LOCAL,
864                 .offset         = LOCAL_VAR(bStrictSync),
865                 .special        = NULL,
866                 .enum_list      = NULL
867         },
868         {
869                 .label          = "use mmap",
870                 .type           = P_BOOL,
871                 .p_class        = P_GLOBAL,
872                 .offset         = GLOBAL_VAR(bUseMmap),
873                 .special        = NULL,
874                 .enum_list      = NULL,
875                 .flags          = FLAG_ADVANCED,
876         },
877         {
878                 .label          = "case insensitive filesystem",
879                 .type           = P_BOOL,
880                 .p_class        = P_LOCAL,
881                 .offset         = LOCAL_VAR(bCIFileSystem),
882                 .special        = NULL,
883                 .enum_list      = NULL
884         },
885
886         {
887                 .label          = "max print jobs",
888                 .type           = P_INTEGER,
889                 .p_class        = P_LOCAL,
890                 .offset         = LOCAL_VAR(iMaxPrintJobs),
891                 .special        = NULL,
892                 .enum_list      = NULL
893         },
894         {
895                 .label          = "printable",
896                 .type           = P_BOOL,
897                 .p_class        = P_LOCAL,
898                 .offset         = LOCAL_VAR(bPrint_ok),
899                 .special        = NULL,
900                 .enum_list      = NULL
901         },
902         {
903                 .label          = "print ok",
904                 .type           = P_BOOL,
905                 .p_class        = P_LOCAL,
906                 .offset         = LOCAL_VAR(bPrint_ok),
907                 .special        = NULL,
908                 .enum_list      = NULL
909         },
910
911         {
912                 .label          = "printer name",
913                 .type           = P_STRING,
914                 .p_class        = P_LOCAL,
915                 .offset         = LOCAL_VAR(szPrintername),
916                 .special        = NULL,
917                 .enum_list      = NULL
918         },
919         {
920                 .label          = "printer",
921                 .type           = P_STRING,
922                 .p_class        = P_LOCAL,
923                 .offset         = LOCAL_VAR(szPrintername),
924                 .special        = NULL,
925                 .enum_list      = NULL
926         },
927
928         {
929                 .label          = "map system",
930                 .type           = P_BOOL,
931                 .p_class        = P_LOCAL,
932                 .offset         = LOCAL_VAR(bMap_system),
933                 .special        = NULL,
934                 .enum_list      = NULL
935         },
936         {
937                 .label          = "map hidden",
938                 .type           = P_BOOL,
939                 .p_class        = P_LOCAL,
940                 .offset         = LOCAL_VAR(bMap_hidden),
941                 .special        = NULL,
942                 .enum_list      = NULL
943         },
944         {
945                 .label          = "map archive",
946                 .type           = P_BOOL,
947                 .p_class        = P_LOCAL,
948                 .offset         = LOCAL_VAR(bMap_archive),
949                 .special        = NULL,
950                 .enum_list      = NULL
951         },
952
953         {
954                 .label          = "preferred master",
955                 .type           = P_ENUM,
956                 .p_class        = P_GLOBAL,
957                 .offset         = GLOBAL_VAR(bPreferredMaster),
958                 .special        = NULL,
959                 .enum_list      = enum_bool_auto
960         },
961         {
962                 .label          = "prefered master",
963                 .type           = P_ENUM,
964                 .p_class        = P_GLOBAL,
965                 .offset         = GLOBAL_VAR(bPreferredMaster),
966                 .special        = NULL,
967                 .enum_list      = enum_bool_auto
968         },
969         {
970                 .label          = "local master",
971                 .type           = P_BOOL,
972                 .p_class        = P_GLOBAL,
973                 .offset         = GLOBAL_VAR(bLocalMaster),
974                 .special        = NULL,
975                 .enum_list      = NULL
976         },
977         {
978                 .label          = "browseable",
979                 .type           = P_BOOL,
980                 .p_class        = P_LOCAL,
981                 .offset         = LOCAL_VAR(bBrowseable),
982                 .special        = NULL,
983                 .enum_list      = NULL
984         },
985         {
986                 .label          = "browsable",
987                 .type           = P_BOOL,
988                 .p_class        = P_LOCAL,
989                 .offset         = LOCAL_VAR(bBrowseable),
990                 .special        = NULL,
991                 .enum_list      = NULL
992         },
993
994         {
995                 .label          = "wins server",
996                 .type           = P_LIST,
997                 .p_class        = P_GLOBAL,
998                 .offset         = GLOBAL_VAR(szWINSservers),
999                 .special        = NULL,
1000                 .enum_list      = NULL
1001         },
1002         {
1003                 .label          = "wins support",
1004                 .type           = P_BOOL,
1005                 .p_class        = P_GLOBAL,
1006                 .offset         = GLOBAL_VAR(bWINSsupport),
1007                 .special        = NULL,
1008                 .enum_list      = NULL
1009         },
1010         {
1011                 .label          = "dns proxy",
1012                 .type           = P_BOOL,
1013                 .p_class        = P_GLOBAL,
1014                 .offset         = GLOBAL_VAR(bWINSdnsProxy),
1015                 .special        = NULL,
1016                 .enum_list      = NULL
1017         },
1018         {
1019                 .label          = "wins hook",
1020                 .type           = P_STRING,
1021                 .p_class        = P_GLOBAL,
1022                 .offset         = GLOBAL_VAR(szWINSHook),
1023                 .special        = NULL,
1024                 .enum_list      = NULL
1025         },
1026
1027         {
1028                 .label          = "csc policy",
1029                 .type           = P_ENUM,
1030                 .p_class        = P_LOCAL,
1031                 .offset         = LOCAL_VAR(iCSCPolicy),
1032                 .special        = NULL,
1033                 .enum_list      = enum_csc_policy
1034         },
1035
1036         {
1037                 .label          = "strict locking",
1038                 .type           = P_BOOL,
1039                 .p_class        = P_LOCAL,
1040                 .offset         = LOCAL_VAR(iStrictLocking),
1041                 .special        = NULL,
1042                 .enum_list      = NULL
1043         },
1044         {
1045                 .label          = "oplocks",
1046                 .type           = P_BOOL,
1047                 .p_class        = P_LOCAL,
1048                 .offset         = LOCAL_VAR(bOpLocks),
1049                 .special        = NULL,
1050                 .enum_list      = NULL
1051         },
1052
1053         {
1054                 .label          = "share backend",
1055                 .type           = P_STRING,
1056                 .p_class        = P_GLOBAL,
1057                 .offset         = GLOBAL_VAR(szShareBackend),
1058                 .special        = NULL,
1059                 .enum_list      = NULL
1060         },
1061         {
1062                 .label          = "preload",
1063                 .type           = P_STRING,
1064                 .p_class        = P_GLOBAL,
1065                 .offset         = GLOBAL_VAR(szAutoServices),
1066                 .special        = NULL,
1067                 .enum_list      = NULL
1068         },
1069         {
1070                 .label          = "auto services",
1071                 .type           = P_STRING,
1072                 .p_class        = P_GLOBAL,
1073                 .offset         = GLOBAL_VAR(szAutoServices),
1074                 .special        = NULL,
1075                 .enum_list      = NULL
1076         },
1077         {
1078                 .label          = "lock dir",
1079                 .type           = P_STRING,
1080                 .p_class        = P_GLOBAL,
1081                 .offset         = GLOBAL_VAR(szLockDir),
1082                 .special        = NULL,
1083                 .enum_list      = NULL
1084         },
1085         {
1086                 .label          = "lock directory",
1087                 .type           = P_STRING,
1088                 .p_class        = P_GLOBAL,
1089                 .offset         = GLOBAL_VAR(szLockDir),
1090                 .special        = NULL,
1091                 .enum_list      = NULL
1092         },
1093         {
1094                 .label          = "state directory",
1095                 .type           = P_STRING,
1096                 .p_class        = P_GLOBAL,
1097                 .offset         = GLOBAL_VAR(szStateDir),
1098                 .special        = NULL,
1099                 .enum_list      = NULL
1100         },
1101         {
1102                 .label          = "cache directory",
1103                 .type           = P_STRING,
1104                 .p_class        = P_GLOBAL,
1105                 .offset         = GLOBAL_VAR(szCacheDir),
1106                 .special        = NULL,
1107                 .enum_list      = NULL
1108         },
1109         {
1110                 .label          = "pid directory",
1111                 .type           = P_STRING,
1112                 .p_class        = P_GLOBAL,
1113                 .offset         = GLOBAL_VAR(szPidDir),
1114                 .special        = NULL,
1115                 .enum_list      = NULL
1116         },
1117
1118         {
1119                 .label          = "socket address",
1120                 .type           = P_STRING,
1121                 .p_class        = P_GLOBAL,
1122                 .offset         = GLOBAL_VAR(szSocketAddress),
1123                 .special        = NULL,
1124                 .enum_list      = NULL
1125         },
1126         {
1127                 .label          = "copy",
1128                 .type           = P_STRING,
1129                 .p_class        = P_LOCAL,
1130                 .offset         = LOCAL_VAR(szCopy),
1131                 .special        = handle_copy,
1132                 .enum_list      = NULL
1133         },
1134         {
1135                 .label          = "include",
1136                 .type           = P_STRING,
1137                 .p_class        = P_LOCAL,
1138                 .offset         = LOCAL_VAR(szInclude),
1139                 .special        = handle_include,
1140                 .enum_list      = NULL
1141         },
1142
1143         {
1144                 .label          = "available",
1145                 .type           = P_BOOL,
1146                 .p_class        = P_LOCAL,
1147                 .offset         = LOCAL_VAR(bAvailable),
1148                 .special        = NULL,
1149                 .enum_list      = NULL
1150         },
1151         {
1152                 .label          = "volume",
1153                 .type           = P_STRING,
1154                 .p_class        = P_LOCAL,
1155                 .offset         = LOCAL_VAR(volume),
1156                 .special        = NULL,
1157                 .enum_list      = NULL
1158         },
1159         {
1160                 .label          = "fstype",
1161                 .type           = P_STRING,
1162                 .p_class        = P_LOCAL,
1163                 .offset         = LOCAL_VAR(fstype),
1164                 .special        = NULL,
1165                 .enum_list      = NULL
1166         },
1167
1168         {
1169                 .label          = "panic action",
1170                 .type           = P_STRING,
1171                 .p_class        = P_GLOBAL,
1172                 .offset         = GLOBAL_VAR(panic_action),
1173                 .special        = NULL,
1174                 .enum_list      = NULL
1175         },
1176
1177         {
1178                 .label          = "msdfs root",
1179                 .type           = P_BOOL,
1180                 .p_class        = P_LOCAL,
1181                 .offset         = LOCAL_VAR(bMSDfsRoot),
1182                 .special        = NULL,
1183                 .enum_list      = NULL
1184         },
1185         {
1186                 .label          = "host msdfs",
1187                 .type           = P_BOOL,
1188                 .p_class        = P_GLOBAL,
1189                 .offset         = GLOBAL_VAR(bHostMSDfs),
1190                 .special        = NULL,
1191                 .enum_list      = NULL
1192         },
1193         {
1194                 .label          = "winbind separator",
1195                 .type           = P_STRING,
1196                 .p_class        = P_GLOBAL,
1197                 .offset         = GLOBAL_VAR(szWinbindSeparator),
1198                 .special        = NULL,
1199                 .enum_list      = NULL
1200         },
1201         {
1202                 .label          = "winbindd socket directory",
1203                 .type           = P_STRING,
1204                 .p_class        = P_GLOBAL,
1205                 .offset         = GLOBAL_VAR(szWinbinddSocketDirectory),
1206                 .special        = NULL,
1207                 .enum_list      = NULL
1208         },
1209         {
1210                 .label          = "winbindd privileged socket directory",
1211                 .type           = P_STRING,
1212                 .p_class        = P_GLOBAL,
1213                 .offset         = GLOBAL_VAR(szWinbinddPrivilegedSocketDirectory),
1214                 .special        = NULL,
1215                 .enum_list      = NULL
1216         },
1217         {
1218                 .label          = "winbind sealed pipes",
1219                 .type           = P_BOOL,
1220                 .p_class        = P_GLOBAL,
1221                 .offset         = GLOBAL_VAR(bWinbindSealedPipes),
1222                 .special        = NULL,
1223                 .enum_list      = NULL
1224         },
1225         {
1226                 .label          = "template shell",
1227                 .type           = P_STRING,
1228                 .p_class        = P_GLOBAL,
1229                 .offset         = GLOBAL_VAR(szTemplateShell),
1230                 .special        = NULL,
1231                 .enum_list      = NULL
1232         },
1233         {
1234                 .label          = "template homedir",
1235                 .type           = P_STRING,
1236                 .p_class        = P_GLOBAL,
1237                 .offset         = GLOBAL_VAR(szTemplateHomedir),
1238                 .special        = NULL,
1239                 .enum_list      = NULL
1240         },
1241         {
1242                 .label          = "idmap trusted only",
1243                 .type           = P_BOOL,
1244                 .p_class        = P_GLOBAL,
1245                 .offset         = GLOBAL_VAR(bIdmapTrustedOnly),
1246                 .special        = NULL,
1247                 .enum_list      = NULL
1248         },
1249
1250         {
1251                 .label          = "ntp signd socket directory",
1252                 .type           = P_STRING,
1253                 .p_class        = P_GLOBAL,
1254                 .offset         = GLOBAL_VAR(szNTPSignDSocketDirectory),
1255                 .special        = NULL,
1256                 .enum_list      = NULL
1257         },
1258         {
1259                 .label          = "rndc command",
1260                 .type           = P_CMDLIST,
1261                 .p_class        = P_GLOBAL,
1262                 .offset         = GLOBAL_VAR(szRNDCCommand),
1263                 .special        = NULL,
1264                 .enum_list      = NULL
1265         },
1266         {
1267                 .label          = "dns update command",
1268                 .type           = P_CMDLIST,
1269                 .p_class        = P_GLOBAL,
1270                 .offset         = GLOBAL_VAR(szDNSUpdateCommand),
1271                 .special        = NULL,
1272                 .enum_list      = NULL
1273         },
1274         {
1275                 .label          = "spn update command",
1276                 .type           = P_CMDLIST,
1277                 .p_class        = P_GLOBAL,
1278                 .offset         = GLOBAL_VAR(szSPNUpdateCommand),
1279                 .special        = NULL,
1280                 .enum_list      = NULL
1281         },
1282         {
1283                 .label          = "nsupdate command",
1284                 .type           = P_CMDLIST,
1285                 .p_class        = P_GLOBAL,
1286                 .offset         = GLOBAL_VAR(szNSUpdateCommand),
1287                 .special        = NULL,
1288                 .enum_list      = NULL
1289         },
1290
1291         {NULL,  P_BOOL,  P_NONE,  0,  NULL,  NULL,  0}
1292 };
1293
1294
1295 /* local variables */
1296 struct loadparm_context {
1297         const char *szConfigFile;
1298         struct loadparm_global *globals;
1299         struct loadparm_service **services;
1300         struct loadparm_service *sDefault;
1301         struct smb_iconv_handle *iconv_handle;
1302         int iNumServices;
1303         struct loadparm_service *currentService;
1304         bool bInGlobalSection;
1305         struct file_lists {
1306                 struct file_lists *next;
1307                 char *name;
1308                 char *subfname;
1309                 time_t modtime;
1310         } *file_lists;
1311         unsigned int flags[NUMPARAMETERS];
1312         bool loaded;
1313         bool refuse_free;
1314         bool global; /* Is this the global context, which may set
1315                       * global variables such as debug level etc? */
1316         const struct loadparm_s3_context *s3_fns;
1317 };
1318
1319
1320 struct loadparm_service *lpcfg_default_service(struct loadparm_context *lp_ctx)
1321 {
1322         if (lp_ctx->s3_fns) {
1323                 return lp_ctx->s3_fns->get_default_loadparm_service();
1324         }
1325         return lp_ctx->sDefault;
1326 }
1327
1328 /**
1329  * Convenience routine to grab string parameters into temporary memory
1330  * and run standard_sub_basic on them.
1331  *
1332  * The buffers can be written to by
1333  * callers without affecting the source string.
1334  */
1335
1336 static const char *lp_string(const char *s)
1337 {
1338 #if 0  /* until REWRITE done to make thread-safe */
1339         size_t len = s ? strlen(s) : 0;
1340         char *ret;
1341 #endif
1342
1343         /* The follow debug is useful for tracking down memory problems
1344            especially if you have an inner loop that is calling a lp_*()
1345            function that returns a string.  Perhaps this debug should be
1346            present all the time? */
1347
1348 #if 0
1349         DEBUG(10, ("lp_string(%s)\n", s));
1350 #endif
1351
1352 #if 0  /* until REWRITE done to make thread-safe */
1353         if (!lp_talloc)
1354                 lp_talloc = talloc_init("lp_talloc");
1355
1356         ret = talloc_array(lp_talloc, char, len + 100); /* leave room for substitution */
1357
1358         if (!ret)
1359                 return NULL;
1360
1361         if (!s)
1362                 *ret = 0;
1363         else
1364                 strlcpy(ret, s, len);
1365
1366         if (trim_string(ret, "\"", "\"")) {
1367                 if (strchr(ret,'"') != NULL)
1368                         strlcpy(ret, s, len);
1369         }
1370
1371         standard_sub_basic(ret,len+100);
1372         return (ret);
1373 #endif
1374         return s;
1375 }
1376
1377 /*
1378    In this section all the functions that are used to access the
1379    parameters from the rest of the program are defined
1380 */
1381
1382 /*
1383  * the creation of separate lpcfg_*() and lp_*() functions is to allow
1384  * for code compatibility between existing Samba4 and Samba3 code.
1385  */
1386
1387 /* this global context supports the lp_*() function varients */
1388 static struct loadparm_context *global_loadparm_context;
1389
1390 #define lpcfg_default_service global_loadparm_context->sDefault
1391 #define lpcfg_global_service(i) global_loadparm_context->services[i]
1392
1393 #define FN_GLOBAL_STRING(fn_name,var_name)                              \
1394  _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
1395         if (lp_ctx == NULL) return NULL;                                \
1396         if (lp_ctx->s3_fns) {                                           \
1397                 SMB_ASSERT(lp_ctx->s3_fns->fn_name);                    \
1398                 return lp_ctx->s3_fns->fn_name();                       \
1399         }                                                               \
1400         return lp_ctx->globals->var_name ? lp_string(lp_ctx->globals->var_name) : ""; \
1401 }
1402
1403 #define FN_GLOBAL_CONST_STRING(fn_name,var_name) \
1404  _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) {\
1405          if (lp_ctx == NULL) return NULL;                               \
1406          if (lp_ctx->s3_fns) {                                          \
1407                  SMB_ASSERT(lp_ctx->s3_fns->fn_name);                   \
1408                  return lp_ctx->s3_fns->fn_name();                      \
1409          }                                                              \
1410          return lp_ctx->globals->var_name ? lp_string(lp_ctx->globals->var_name) : ""; \
1411  }
1412
1413 #define FN_GLOBAL_LIST(fn_name,var_name)                                \
1414  _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
1415          if (lp_ctx == NULL) return NULL;                               \
1416          if (lp_ctx->s3_fns) {                                          \
1417                  SMB_ASSERT(lp_ctx->s3_fns->fn_name);                   \
1418                  return lp_ctx->s3_fns->fn_name();                      \
1419          }                                                              \
1420          return lp_ctx->globals->var_name;                              \
1421  }
1422
1423 #define FN_GLOBAL_BOOL(fn_name,var_name) \
1424  _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) {\
1425          if (lp_ctx == NULL) return false;                              \
1426          if (lp_ctx->s3_fns) {                                          \
1427                  SMB_ASSERT(lp_ctx->s3_fns->fn_name);                   \
1428                  return lp_ctx->s3_fns->fn_name();                      \
1429          }                                                              \
1430          return lp_ctx->globals->var_name;                              \
1431 }
1432
1433 #define FN_GLOBAL_INTEGER(fn_name,var_name) \
1434  _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_context *lp_ctx) { \
1435          if (lp_ctx->s3_fns) {                                          \
1436                  SMB_ASSERT(lp_ctx->s3_fns->fn_name);                   \
1437                  return lp_ctx->s3_fns->fn_name();                      \
1438          }                                                              \
1439          return lp_ctx->globals->var_name;                              \
1440  }
1441
1442 /* Local parameters don't need the ->s3_fns because the struct
1443  * loadparm_service is shared and lpcfg_service() checks the ->s3_fns
1444  * hook */
1445 #define FN_LOCAL_STRING(fn_name,val) \
1446  _PUBLIC_ const char *lpcfg_ ## fn_name(struct loadparm_service *service, \
1447                                         struct loadparm_service *sDefault) { \
1448          return(lp_string((const char *)((service != NULL && service->val != NULL) ? service->val : sDefault->val))); \
1449  }
1450
1451 #define FN_LOCAL_CONST_STRING(fn_name,val) FN_LOCAL_STRING(fn_name, val)
1452
1453 #define FN_LOCAL_LIST(fn_name,val) \
1454  _PUBLIC_ const char **lpcfg_ ## fn_name(struct loadparm_service *service, \
1455                                          struct loadparm_service *sDefault) {\
1456          return(const char **)(service != NULL && service->val != NULL? service->val : sDefault->val); \
1457  }
1458
1459 #define FN_LOCAL_PARM_BOOL(fn_name, val) FN_LOCAL_BOOL(fn_name, val)
1460
1461 #define FN_LOCAL_BOOL(fn_name,val) \
1462  _PUBLIC_ bool lpcfg_ ## fn_name(struct loadparm_service *service, \
1463                                  struct loadparm_service *sDefault) {   \
1464          return((service != NULL)? service->val : sDefault->val); \
1465  }
1466
1467 #define FN_LOCAL_INTEGER(fn_name,val) \
1468  _PUBLIC_ int lpcfg_ ## fn_name(struct loadparm_service *service, \
1469                                 struct loadparm_service *sDefault) {    \
1470          return((service != NULL)? service->val : sDefault->val); \
1471  }
1472
1473 #define FN_LOCAL_PARM_INTEGER(fn_name, val) FN_LOCAL_INTEGER(fn_name, val)
1474
1475 #define FN_LOCAL_PARM_CHAR(fn_name, val) FN_LOCAL_CHAR(fn_name, val)
1476
1477 #define FN_LOCAL_CHAR(fn_name,val) \
1478  _PUBLIC_ char lpcfg_ ## fn_name(struct loadparm_service *service, \
1479                                 struct loadparm_service *sDefault) {    \
1480          return((service != NULL)? service->val : sDefault->val); \
1481  }
1482
1483 #include "lib/param/param_functions.c"
1484
1485 FN_GLOBAL_INTEGER(server_role, server_role)
1486 FN_GLOBAL_LIST(smb_ports, smb_ports)
1487 FN_GLOBAL_INTEGER(nbt_port, nbt_port)
1488 FN_GLOBAL_INTEGER(dgram_port, dgram_port)
1489 FN_GLOBAL_INTEGER(cldap_port, cldap_port)
1490 FN_GLOBAL_INTEGER(krb5_port, krb5_port)
1491 FN_GLOBAL_INTEGER(kpasswd_port, kpasswd_port)
1492 FN_GLOBAL_INTEGER(web_port, web_port)
1493 FN_GLOBAL_BOOL(tls_enabled, tls_enabled)
1494 FN_GLOBAL_STRING(logfile, logfile)
1495 FN_GLOBAL_STRING(share_backend, szShareBackend)
1496 FN_GLOBAL_STRING(spoolss_url, szSPOOLSS_URL)
1497 FN_GLOBAL_STRING(wins_config_url, szWINS_CONFIG_URL)
1498 FN_GLOBAL_STRING(wins_url, szWINS_URL)
1499 FN_GLOBAL_CONST_STRING(winbind_separator, szWinbindSeparator)
1500 FN_GLOBAL_CONST_STRING(winbindd_socket_directory, szWinbinddSocketDirectory)
1501 FN_GLOBAL_CONST_STRING(winbindd_privileged_socket_directory, szWinbinddPrivilegedSocketDirectory)
1502 FN_GLOBAL_CONST_STRING(template_shell, szTemplateShell)
1503 FN_GLOBAL_CONST_STRING(template_homedir, szTemplateHomedir)
1504 FN_GLOBAL_BOOL(winbind_sealed_pipes, bWinbindSealedPipes)
1505 FN_GLOBAL_BOOL(idmap_trusted_only, bIdmapTrustedOnly)
1506 FN_GLOBAL_STRING(private_dir, szPrivateDir)
1507 FN_GLOBAL_STRING(serverstring, szServerString)
1508 FN_GLOBAL_STRING(lockdir, szLockDir)
1509 FN_GLOBAL_STRING(statedir, szStateDir)
1510 FN_GLOBAL_STRING(cachedir, szCacheDir)
1511 FN_GLOBAL_STRING(ncalrpc_dir, ncalrpc_dir)
1512 FN_GLOBAL_STRING(dos_charset, dos_charset)
1513 FN_GLOBAL_STRING(unix_charset, unix_charset)
1514 FN_GLOBAL_STRING(piddir, szPidDir)
1515 FN_GLOBAL_LIST(rndc_command, szRNDCCommand)
1516 FN_GLOBAL_LIST(dns_update_command, szDNSUpdateCommand)
1517 FN_GLOBAL_LIST(spn_update_command, szSPNUpdateCommand)
1518 FN_GLOBAL_LIST(nsupdate_command, szNSUpdateCommand)
1519 FN_GLOBAL_LIST(dcerpc_endpoint_servers, dcerpc_ep_servers)
1520 FN_GLOBAL_LIST(server_services, server_services)
1521 FN_GLOBAL_STRING(ntptr_providor, ntptr_providor)
1522 FN_GLOBAL_STRING(passdb_backend, passdb_backend)
1523 FN_GLOBAL_STRING(auto_services, szAutoServices)
1524 FN_GLOBAL_STRING(passwd_chat, szPasswdChat)
1525 FN_GLOBAL_LIST(passwordserver, szPasswordServers)
1526 FN_GLOBAL_LIST(name_resolve_order, szNameResolveOrder)
1527 FN_GLOBAL_STRING(realm, szRealm_upper)
1528 FN_GLOBAL_STRING(dnsdomain, szRealm_lower)
1529 FN_GLOBAL_STRING(socket_options, socket_options)
1530 FN_GLOBAL_STRING(workgroup, szWorkgroup)
1531 FN_GLOBAL_STRING(netbios_name, szNetbiosName)
1532 FN_GLOBAL_STRING(netbios_scope, szNetbiosScope)
1533 FN_GLOBAL_LIST(wins_server_list, szWINSservers)
1534 FN_GLOBAL_LIST(interfaces, szInterfaces)
1535 FN_GLOBAL_STRING(socket_address, szSocketAddress)
1536 FN_GLOBAL_LIST(netbios_aliases, szNetbiosAliases)
1537 FN_GLOBAL_BOOL(disable_netbios, bDisableNetbios)
1538 FN_GLOBAL_BOOL(we_are_a_wins_server, bWINSsupport)
1539 FN_GLOBAL_BOOL(wins_dns_proxy, bWINSdnsProxy)
1540 FN_GLOBAL_STRING(wins_hook, szWINSHook)
1541 FN_GLOBAL_BOOL(local_master, bLocalMaster)
1542 FN_GLOBAL_BOOL(readraw, bReadRaw)
1543 FN_GLOBAL_BOOL(large_readwrite, bLargeReadwrite)
1544 FN_GLOBAL_BOOL(writeraw, bWriteRaw)
1545 FN_GLOBAL_BOOL(null_passwords, bNullPasswords)
1546 FN_GLOBAL_BOOL(obey_pam_restrictions, bObeyPamRestrictions)
1547 FN_GLOBAL_BOOL(encrypted_passwords, bEncryptPasswords)
1548 FN_GLOBAL_BOOL(time_server, bTimeServer)
1549 FN_GLOBAL_BOOL(bind_interfaces_only, bBindInterfacesOnly)
1550 FN_GLOBAL_BOOL(unicode, bUnicode)
1551 FN_GLOBAL_BOOL(nt_status_support, bNTStatusSupport)
1552 FN_GLOBAL_BOOL(lanman_auth, bLanmanAuth)
1553 FN_GLOBAL_BOOL(ntlm_auth, bNTLMAuth)
1554 FN_GLOBAL_BOOL(client_plaintext_auth, bClientPlaintextAuth)
1555 FN_GLOBAL_BOOL(client_lanman_auth, bClientLanManAuth)
1556 FN_GLOBAL_BOOL(client_ntlmv2_auth, bClientNTLMv2Auth)
1557 FN_GLOBAL_BOOL(client_use_spnego_principal, client_use_spnego_principal)
1558 FN_GLOBAL_BOOL(host_msdfs, bHostMSDfs)
1559 FN_GLOBAL_BOOL(unix_extensions, bUnixExtensions)
1560 FN_GLOBAL_BOOL(use_spnego, bUseSpnego)
1561 FN_GLOBAL_BOOL(use_mmap, bUseMmap)
1562 FN_GLOBAL_BOOL(rpc_big_endian, bRpcBigEndian)
1563 FN_GLOBAL_INTEGER(max_wins_ttl, max_wins_ttl)
1564 FN_GLOBAL_INTEGER(min_wins_ttl, min_wins_ttl)
1565 FN_GLOBAL_INTEGER(maxmux, max_mux)
1566 FN_GLOBAL_INTEGER(max_xmit, max_xmit)
1567 FN_GLOBAL_INTEGER(passwordlevel, pwordlevel)
1568 FN_GLOBAL_INTEGER(srv_maxprotocol, srv_maxprotocol)
1569 FN_GLOBAL_INTEGER(srv_minprotocol, srv_minprotocol)
1570 FN_GLOBAL_INTEGER(cli_maxprotocol, cli_maxprotocol)
1571 FN_GLOBAL_INTEGER(cli_minprotocol, cli_minprotocol)
1572 FN_GLOBAL_INTEGER(security, security)
1573 FN_GLOBAL_BOOL(paranoid_server_security, paranoid_server_security)
1574
1575 FN_GLOBAL_INTEGER(server_signing, server_signing)
1576 FN_GLOBAL_INTEGER(client_signing, client_signing)
1577
1578 FN_GLOBAL_CONST_STRING(ntp_signd_socket_directory, szNTPSignDSocketDirectory)
1579
1580 /* local prototypes */
1581 static int map_parameter(const char *pszParmName);
1582 static struct loadparm_service *getservicebyname(struct loadparm_context *lp_ctx,
1583                                         const char *pszServiceName);
1584 static void copy_service(struct loadparm_service *pserviceDest,
1585                          struct loadparm_service *pserviceSource,
1586                          struct bitmap *pcopymapDest);
1587 static bool lpcfg_service_ok(struct loadparm_service *service);
1588 static bool do_section(const char *pszSectionName, void *);
1589 static void init_copymap(struct loadparm_service *pservice);
1590
1591 /* This is a helper function for parametrical options support. */
1592 /* It returns a pointer to parametrical option value if it exists or NULL otherwise */
1593 /* Actual parametrical functions are quite simple */
1594 const char *lpcfg_get_parametric(struct loadparm_context *lp_ctx,
1595                               struct loadparm_service *service,
1596                               const char *type, const char *option)
1597 {
1598         char *vfskey_tmp = NULL;
1599         char *vfskey = NULL;
1600         struct parmlist_entry *data;
1601
1602         if (lp_ctx == NULL)
1603                 return NULL;
1604
1605         if (lp_ctx->s3_fns) {
1606                 return lp_ctx->s3_fns->get_parametric(service, type, option);
1607         }
1608
1609         data = (service == NULL ? lp_ctx->globals->param_opt : service->param_opt);
1610
1611         vfskey_tmp = talloc_asprintf(NULL, "%s:%s", type, option);
1612         if (vfskey_tmp == NULL) return NULL;
1613         vfskey = strlower_talloc(NULL, vfskey_tmp);
1614         talloc_free(vfskey_tmp);
1615
1616         while (data) {
1617                 if (strcmp(data->key, vfskey) == 0) {
1618                         talloc_free(vfskey);
1619                         return data->value;
1620                 }
1621                 data = data->next;
1622         }
1623
1624         if (service != NULL) {
1625                 /* Try to fetch the same option but from globals */
1626                 /* but only if we are not already working with globals */
1627                 for (data = lp_ctx->globals->param_opt; data;
1628                      data = data->next) {
1629                         if (strcmp(data->key, vfskey) == 0) {
1630                                 talloc_free(vfskey);
1631                                 return data->value;
1632                         }
1633                 }
1634         }
1635
1636         talloc_free(vfskey);
1637
1638         return NULL;
1639 }
1640
1641
1642 /**
1643  * convenience routine to return int parameters.
1644  */
1645 static int lp_int(const char *s)
1646 {
1647
1648         if (!s) {
1649                 DEBUG(0,("lp_int(%s): is called with NULL!\n",s));
1650                 return -1;
1651         }
1652
1653         return strtol(s, NULL, 0);
1654 }
1655
1656 /**
1657  * convenience routine to return unsigned long parameters.
1658  */
1659 static int lp_ulong(const char *s)
1660 {
1661
1662         if (!s) {
1663                 DEBUG(0,("lp_int(%s): is called with NULL!\n",s));
1664                 return -1;
1665         }
1666
1667         return strtoul(s, NULL, 0);
1668 }
1669
1670 /**
1671  * convenience routine to return unsigned long parameters.
1672  */
1673 static double lp_double(const char *s)
1674 {
1675
1676         if (!s) {
1677                 DEBUG(0,("lp_double(%s): is called with NULL!\n",s));
1678                 return -1;
1679         }
1680
1681         return strtod(s, NULL);
1682 }
1683
1684 /**
1685  * convenience routine to return boolean parameters.
1686  */
1687 static bool lp_bool(const char *s)
1688 {
1689         bool ret = false;
1690
1691         if (!s) {
1692                 DEBUG(0,("lp_bool(%s): is called with NULL!\n",s));
1693                 return false;
1694         }
1695
1696         if (!set_boolean(s, &ret)) {
1697                 DEBUG(0,("lp_bool(%s): value is not boolean!\n",s));
1698                 return false;
1699         }
1700
1701         return ret;
1702 }
1703
1704
1705 /**
1706  * Return parametric option from a given service. Type is a part of option before ':'
1707  * Parametric option has following syntax: 'Type: option = value'
1708  * Returned value is allocated in 'lp_talloc' context
1709  */
1710
1711 const char *lpcfg_parm_string(struct loadparm_context *lp_ctx,
1712                               struct loadparm_service *service, const char *type,
1713                               const char *option)
1714 {
1715         const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
1716
1717         if (value)
1718                 return lp_string(value);
1719
1720         return NULL;
1721 }
1722
1723 /**
1724  * Return parametric option from a given service. Type is a part of option before ':'
1725  * Parametric option has following syntax: 'Type: option = value'
1726  * Returned value is allocated in 'lp_talloc' context
1727  */
1728
1729 const char **lpcfg_parm_string_list(TALLOC_CTX *mem_ctx,
1730                                     struct loadparm_context *lp_ctx,
1731                                     struct loadparm_service *service,
1732                                     const char *type,
1733                                     const char *option, const char *separator)
1734 {
1735         const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
1736
1737         if (value != NULL)
1738                 return (const char **)str_list_make(mem_ctx, value, separator);
1739
1740         return NULL;
1741 }
1742
1743 /**
1744  * Return parametric option from a given service. Type is a part of option before ':'
1745  * Parametric option has following syntax: 'Type: option = value'
1746  */
1747
1748 int lpcfg_parm_int(struct loadparm_context *lp_ctx,
1749                    struct loadparm_service *service, const char *type,
1750                    const char *option, int default_v)
1751 {
1752         const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
1753
1754         if (value)
1755                 return lp_int(value);
1756
1757         return default_v;
1758 }
1759
1760 /**
1761  * Return parametric option from a given service. Type is a part of
1762  * option before ':'.
1763  * Parametric option has following syntax: 'Type: option = value'.
1764  */
1765
1766 int lpcfg_parm_bytes(struct loadparm_context *lp_ctx,
1767                   struct loadparm_service *service, const char *type,
1768                   const char *option, int default_v)
1769 {
1770         uint64_t bval;
1771
1772         const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
1773
1774         if (value && conv_str_size_error(value, &bval)) {
1775                 if (bval <= INT_MAX) {
1776                         return (int)bval;
1777                 }
1778         }
1779
1780         return default_v;
1781 }
1782
1783 /**
1784  * Return parametric option from a given service.
1785  * Type is a part of option before ':'
1786  * Parametric option has following syntax: 'Type: option = value'
1787  */
1788 unsigned long lpcfg_parm_ulong(struct loadparm_context *lp_ctx,
1789                             struct loadparm_service *service, const char *type,
1790                             const char *option, unsigned long default_v)
1791 {
1792         const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
1793
1794         if (value)
1795                 return lp_ulong(value);
1796
1797         return default_v;
1798 }
1799
1800
1801 double lpcfg_parm_double(struct loadparm_context *lp_ctx,
1802                       struct loadparm_service *service, const char *type,
1803                       const char *option, double default_v)
1804 {
1805         const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
1806
1807         if (value != NULL)
1808                 return lp_double(value);
1809
1810         return default_v;
1811 }
1812
1813 /**
1814  * Return parametric option from a given service. Type is a part of option before ':'
1815  * Parametric option has following syntax: 'Type: option = value'
1816  */
1817
1818 bool lpcfg_parm_bool(struct loadparm_context *lp_ctx,
1819                      struct loadparm_service *service, const char *type,
1820                      const char *option, bool default_v)
1821 {
1822         const char *value = lpcfg_get_parametric(lp_ctx, service, type, option);
1823
1824         if (value != NULL)
1825                 return lp_bool(value);
1826
1827         return default_v;
1828 }
1829
1830
1831 /**
1832  * Initialise a service to the defaults.
1833  */
1834
1835 static struct loadparm_service *init_service(TALLOC_CTX *mem_ctx, struct loadparm_service *sDefault)
1836 {
1837         struct loadparm_service *pservice =
1838                 talloc_zero(mem_ctx, struct loadparm_service);
1839         copy_service(pservice, sDefault, NULL);
1840         return pservice;
1841 }
1842
1843 /**
1844  * Set a string value, deallocating any existing space, and allocing the space
1845  * for the string
1846  */
1847 static bool lpcfg_string_set(TALLOC_CTX *mem_ctx, char **dest, const char *src)
1848 {
1849         talloc_free(*dest);
1850
1851         if (src == NULL)
1852                 src = "";
1853
1854         *dest = talloc_strdup(mem_ctx, src);
1855         if ((*dest) == NULL) {
1856                 DEBUG(0,("Out of memory in string_set\n"));
1857                 return false;
1858         }
1859
1860         return true;
1861 }
1862
1863 /**
1864  * Set a string value, deallocating any existing space, and allocing the space
1865  * for the string
1866  */
1867 static bool lpcfg_string_set_upper(TALLOC_CTX *mem_ctx, char **dest, const char *src)
1868 {
1869         talloc_free(*dest);
1870
1871         if (src == NULL)
1872                 src = "";
1873
1874         *dest = strupper_talloc(mem_ctx, src);
1875         if ((*dest) == NULL) {
1876                 DEBUG(0,("Out of memory in string_set_upper\n"));
1877                 return false;
1878         }
1879
1880         return true;
1881 }
1882
1883
1884
1885 /**
1886  * Add a new service to the services array initialising it with the given
1887  * service.
1888  */
1889
1890 struct loadparm_service *lpcfg_add_service(struct loadparm_context *lp_ctx,
1891                                            const struct loadparm_service *pservice,
1892                                            const char *name)
1893 {
1894         int i;
1895         struct loadparm_service tservice;
1896         int num_to_alloc = lp_ctx->iNumServices + 1;
1897         struct parmlist_entry *data, *pdata;
1898
1899         if (pservice == NULL) {
1900                 pservice = lp_ctx->sDefault;
1901         }
1902
1903         tservice = *pservice;
1904
1905         /* it might already exist */
1906         if (name) {
1907                 struct loadparm_service *service = getservicebyname(lp_ctx,
1908                                                                     name);
1909                 if (service != NULL) {
1910                         /* Clean all parametric options for service */
1911                         /* They will be added during parsing again */
1912                         data = service->param_opt;
1913                         while (data) {
1914                                 pdata = data->next;
1915                                 talloc_free(data);
1916                                 data = pdata;
1917                         }
1918                         service->param_opt = NULL;
1919                         return service;
1920                 }
1921         }
1922
1923         /* find an invalid one */
1924         for (i = 0; i < lp_ctx->iNumServices; i++)
1925                 if (lp_ctx->services[i] == NULL)
1926                         break;
1927
1928         /* if not, then create one */
1929         if (i == lp_ctx->iNumServices) {
1930                 struct loadparm_service **tsp;
1931
1932                 tsp = talloc_realloc(lp_ctx, lp_ctx->services, struct loadparm_service *, num_to_alloc);
1933
1934                 if (!tsp) {
1935                         DEBUG(0,("lpcfg_add_service: failed to enlarge services!\n"));
1936                         return NULL;
1937                 } else {
1938                         lp_ctx->services = tsp;
1939                         lp_ctx->services[lp_ctx->iNumServices] = NULL;
1940                 }
1941
1942                 lp_ctx->iNumServices++;
1943         }
1944
1945         lp_ctx->services[i] = init_service(lp_ctx->services, lp_ctx->sDefault);
1946         if (lp_ctx->services[i] == NULL) {
1947                 DEBUG(0,("lpcfg_add_service: out of memory!\n"));
1948                 return NULL;
1949         }
1950         copy_service(lp_ctx->services[i], &tservice, NULL);
1951         if (name != NULL)
1952                 lpcfg_string_set(lp_ctx->services[i], &lp_ctx->services[i]->szService, name);
1953         return lp_ctx->services[i];
1954 }
1955
1956 /**
1957  * Add a new home service, with the specified home directory, defaults coming
1958  * from service ifrom.
1959  */
1960
1961 bool lpcfg_add_home(struct loadparm_context *lp_ctx,
1962                  const char *pszHomename,
1963                  struct loadparm_service *default_service,
1964                  const char *user, const char *pszHomedir)
1965 {
1966         struct loadparm_service *service;
1967
1968         service = lpcfg_add_service(lp_ctx, default_service, pszHomename);
1969
1970         if (service == NULL)
1971                 return false;
1972
1973         if (!(*(default_service->szPath))
1974             || strequal(default_service->szPath, lp_ctx->sDefault->szPath)) {
1975                 service->szPath = talloc_strdup(service, pszHomedir);
1976         } else {
1977                 service->szPath = string_sub_talloc(service, lpcfg_pathname(default_service, lp_ctx->sDefault), "%H", pszHomedir);
1978         }
1979
1980         if (!(*(service->comment))) {
1981                 service->comment = talloc_asprintf(service, "Home directory of %s", user);
1982         }
1983         service->bAvailable = default_service->bAvailable;
1984         service->bBrowseable = default_service->bBrowseable;
1985
1986         DEBUG(3, ("adding home's share [%s] for user '%s' at '%s'\n",
1987                   pszHomename, user, service->szPath));
1988
1989         return true;
1990 }
1991
1992 /**
1993  * Add a new printer service, with defaults coming from service iFrom.
1994  */
1995
1996 bool lpcfg_add_printer(struct loadparm_context *lp_ctx,
1997                        const char *pszPrintername,
1998                        struct loadparm_service *default_service)
1999 {
2000         const char *comment = "From Printcap";
2001         struct loadparm_service *service;
2002         service = lpcfg_add_service(lp_ctx, default_service, pszPrintername);
2003
2004         if (service == NULL)
2005                 return false;
2006
2007         /* note that we do NOT default the availability flag to True - */
2008         /* we take it from the default service passed. This allows all */
2009         /* dynamic printers to be disabled by disabling the [printers] */
2010         /* entry (if/when the 'available' keyword is implemented!).    */
2011
2012         /* the printer name is set to the service name. */
2013         lpcfg_string_set(service, &service->szPrintername, pszPrintername);
2014         lpcfg_string_set(service, &service->comment, comment);
2015         service->bBrowseable = default_service->bBrowseable;
2016         /* Printers cannot be read_only. */
2017         service->bRead_only = false;
2018         /* Printer services must be printable. */
2019         service->bPrint_ok = true;
2020
2021         DEBUG(3, ("adding printer service %s\n", pszPrintername));
2022
2023         return true;
2024 }
2025
2026 /**
2027  * Map a parameter's string representation to something we can use.
2028  * Returns False if the parameter string is not recognised, else TRUE.
2029  */
2030
2031 static int map_parameter(const char *pszParmName)
2032 {
2033         int iIndex;
2034
2035         if (*pszParmName == '-')
2036                 return -1;
2037
2038         for (iIndex = 0; parm_table[iIndex].label; iIndex++)
2039                 if (strwicmp(parm_table[iIndex].label, pszParmName) == 0)
2040                         return iIndex;
2041
2042         /* Warn only if it isn't parametric option */
2043         if (strchr(pszParmName, ':') == NULL)
2044                 DEBUG(0, ("Unknown parameter encountered: \"%s\"\n", pszParmName));
2045         /* We do return 'fail' for parametric options as well because they are
2046            stored in different storage
2047          */
2048         return -1;
2049 }
2050
2051
2052 /**
2053   return the parameter structure for a parameter
2054 */
2055 struct parm_struct *lpcfg_parm_struct(struct loadparm_context *lp_ctx, const char *name)
2056 {
2057         int parmnum;
2058
2059         if (lp_ctx->s3_fns) {
2060                 return lp_ctx->s3_fns->get_parm_struct(name);
2061         }
2062
2063         parmnum = map_parameter(name);
2064         if (parmnum == -1) return NULL;
2065         return &parm_table[parmnum];
2066 }
2067
2068 /**
2069   return the parameter pointer for a parameter
2070 */
2071 void *lpcfg_parm_ptr(struct loadparm_context *lp_ctx,
2072                   struct loadparm_service *service, struct parm_struct *parm)
2073 {
2074         if (lp_ctx->s3_fns) {
2075                 return lp_ctx->s3_fns->get_parm_ptr(service, parm);
2076         }
2077
2078         if (service == NULL) {
2079                 if (parm->p_class == P_LOCAL)
2080                         return ((char *)lp_ctx->sDefault)+parm->offset;
2081                 else if (parm->p_class == P_GLOBAL)
2082                         return ((char *)lp_ctx->globals)+parm->offset;
2083                 else return NULL;
2084         } else {
2085                 return ((char *)service) + parm->offset;
2086         }
2087 }
2088
2089 /**
2090   return the parameter pointer for a parameter
2091 */
2092 bool lpcfg_parm_is_cmdline(struct loadparm_context *lp_ctx, const char *name)
2093 {
2094         int parmnum;
2095
2096         if (lp_ctx->s3_fns) {
2097                 struct parm_struct *parm = lp_ctx->s3_fns->get_parm_struct(name);
2098                 if (parm) {
2099                         return parm->flags & FLAG_CMDLINE;
2100                 }
2101                 return false;
2102         }
2103
2104         parmnum = map_parameter(name);
2105         if (parmnum == -1) return false;
2106
2107         return lp_ctx->flags[parmnum] & FLAG_CMDLINE;
2108 }
2109
2110 /**
2111  * Find a service by name. Otherwise works like get_service.
2112  */
2113
2114 static struct loadparm_service *getservicebyname(struct loadparm_context *lp_ctx,
2115                                         const char *pszServiceName)
2116 {
2117         int iService;
2118
2119         if (lp_ctx->s3_fns) {
2120                 return lp_ctx->s3_fns->get_service(pszServiceName);
2121         }
2122
2123         for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--)
2124                 if (lp_ctx->services[iService] != NULL &&
2125                     strwicmp(lp_ctx->services[iService]->szService, pszServiceName) == 0) {
2126                         return lp_ctx->services[iService];
2127                 }
2128
2129         return NULL;
2130 }
2131
2132 /**
2133  * Copy a service structure to another.
2134  * If pcopymapDest is NULL then copy all fields
2135  */
2136
2137 static void copy_service(struct loadparm_service *pserviceDest,
2138                          struct loadparm_service *pserviceSource,
2139                          struct bitmap *pcopymapDest)
2140 {
2141         int i;
2142         bool bcopyall = (pcopymapDest == NULL);
2143         struct parmlist_entry *data, *pdata, *paramo;
2144         bool not_added;
2145
2146         for (i = 0; parm_table[i].label; i++)
2147                 if (parm_table[i].p_class == P_LOCAL &&
2148                     (bcopyall || bitmap_query(pcopymapDest, i))) {
2149                         void *src_ptr =
2150                                 ((char *)pserviceSource) + parm_table[i].offset;
2151                         void *dest_ptr =
2152                                 ((char *)pserviceDest) + parm_table[i].offset;
2153
2154                         switch (parm_table[i].type) {
2155                                 case P_BOOL:
2156                                         *(bool *)dest_ptr = *(bool *)src_ptr;
2157                                         break;
2158
2159                                 case P_INTEGER:
2160                                 case P_OCTAL:
2161                                 case P_ENUM:
2162                                         *(int *)dest_ptr = *(int *)src_ptr;
2163                                         break;
2164
2165                                 case P_STRING:
2166                                         lpcfg_string_set(pserviceDest,
2167                                                    (char **)dest_ptr,
2168                                                    *(char **)src_ptr);
2169                                         break;
2170
2171                                 case P_USTRING:
2172                                         lpcfg_string_set_upper(pserviceDest,
2173                                                          (char **)dest_ptr,
2174                                                          *(char **)src_ptr);
2175                                         break;
2176                                 case P_LIST:
2177                                         *(const char ***)dest_ptr = (const char **)str_list_copy(pserviceDest, 
2178                                                                                   *(const char ***)src_ptr);
2179                                         break;
2180                                 default:
2181                                         break;
2182                         }
2183                 }
2184
2185         if (bcopyall) {
2186                 init_copymap(pserviceDest);
2187                 if (pserviceSource->copymap)
2188                         bitmap_copy(pserviceDest->copymap,
2189                                     pserviceSource->copymap);
2190         }
2191
2192         data = pserviceSource->param_opt;
2193         while (data) {
2194                 not_added = true;
2195                 pdata = pserviceDest->param_opt;
2196                 /* Traverse destination */
2197                 while (pdata) {
2198                         /* If we already have same option, override it */
2199                         if (strcmp(pdata->key, data->key) == 0) {
2200                                 talloc_free(pdata->value);
2201                                 pdata->value = talloc_reference(pdata,
2202                                                              data->value);
2203                                 not_added = false;
2204                                 break;
2205                         }
2206                         pdata = pdata->next;
2207                 }
2208                 if (not_added) {
2209                         paramo = talloc_zero(pserviceDest, struct parmlist_entry);
2210                         if (paramo == NULL)
2211                                 smb_panic("OOM");
2212                         paramo->key = talloc_reference(paramo, data->key);
2213                         paramo->value = talloc_reference(paramo, data->value);
2214                         DLIST_ADD(pserviceDest->param_opt, paramo);
2215                 }
2216                 data = data->next;
2217         }
2218 }
2219
2220 /**
2221  * Check a service for consistency. Return False if the service is in any way
2222  * incomplete or faulty, else True.
2223  */
2224 static bool lpcfg_service_ok(struct loadparm_service *service)
2225 {
2226         bool bRetval;
2227
2228         bRetval = true;
2229         if (service->szService[0] == '\0') {
2230                 DEBUG(0, ("The following message indicates an internal error:\n"));
2231                 DEBUG(0, ("No service name in service entry.\n"));
2232                 bRetval = false;
2233         }
2234
2235         /* The [printers] entry MUST be printable. I'm all for flexibility, but */
2236         /* I can't see why you'd want a non-printable printer service...        */
2237         if (strwicmp(service->szService, PRINTERS_NAME) == 0) {
2238                 if (!service->bPrint_ok) {
2239                         DEBUG(0, ("WARNING: [%s] service MUST be printable!\n",
2240                                service->szService));
2241                         service->bPrint_ok = true;
2242                 }
2243                 /* [printers] service must also be non-browsable. */
2244                 if (service->bBrowseable)
2245                         service->bBrowseable = false;
2246         }
2247
2248         /* If a service is flagged unavailable, log the fact at level 0. */
2249         if (!service->bAvailable)
2250                 DEBUG(1, ("NOTE: Service %s is flagged unavailable.\n",
2251                           service->szService));
2252
2253         return bRetval;
2254 }
2255
2256
2257 /*******************************************************************
2258  Keep a linked list of all config files so we know when one has changed
2259  it's date and needs to be reloaded.
2260 ********************************************************************/
2261
2262 static void add_to_file_list(struct loadparm_context *lp_ctx,
2263                              const char *fname, const char *subfname)
2264 {
2265         struct file_lists *f = lp_ctx->file_lists;
2266
2267         while (f) {
2268                 if (f->name && !strcmp(f->name, fname))
2269                         break;
2270                 f = f->next;
2271         }
2272
2273         if (!f) {
2274                 f = talloc(lp_ctx, struct file_lists);
2275                 if (!f)
2276                         return;
2277                 f->next = lp_ctx->file_lists;
2278                 f->name = talloc_strdup(f, fname);
2279                 if (!f->name) {
2280                         talloc_free(f);
2281                         return;
2282                 }
2283                 f->subfname = talloc_strdup(f, subfname);
2284                 if (!f->subfname) {
2285                         talloc_free(f);
2286                         return;
2287                 }
2288                 lp_ctx->file_lists = f;
2289                 f->modtime = file_modtime(subfname);
2290         } else {
2291                 time_t t = file_modtime(subfname);
2292                 if (t)
2293                         f->modtime = t;
2294         }
2295 }
2296
2297 /*******************************************************************
2298  Check if a config file has changed date.
2299 ********************************************************************/
2300 bool lpcfg_file_list_changed(struct loadparm_context *lp_ctx)
2301 {
2302         struct file_lists *f;
2303         DEBUG(6, ("lp_file_list_changed()\n"));
2304
2305         for (f = lp_ctx->file_lists; f != NULL; f = f->next) {
2306                 char *n2;
2307                 time_t mod_time;
2308
2309                 n2 = standard_sub_basic(lp_ctx, f->name);
2310
2311                 DEBUGADD(6, ("file %s -> %s  last mod_time: %s\n",
2312                              f->name, n2, ctime(&f->modtime)));
2313
2314                 mod_time = file_modtime(n2);
2315
2316                 if (mod_time && ((f->modtime != mod_time) || (f->subfname == NULL) || (strcmp(n2, f->subfname) != 0))) {
2317                         DEBUGADD(6, ("file %s modified: %s\n", n2,
2318                                   ctime(&mod_time)));
2319                         f->modtime = mod_time;
2320                         talloc_free(f->subfname);
2321                         f->subfname = talloc_strdup(f, n2);
2322                         return true;
2323                 }
2324         }
2325         return false;
2326 }
2327
2328 /***************************************************************************
2329  Handle the "realm" parameter
2330 ***************************************************************************/
2331
2332 static bool handle_realm(struct loadparm_context *lp_ctx, int unused,
2333                          const char *pszParmValue, char **ptr)
2334 {
2335         lpcfg_string_set(lp_ctx, ptr, pszParmValue);
2336
2337         talloc_free(lp_ctx->globals->szRealm_upper);
2338         talloc_free(lp_ctx->globals->szRealm_lower);
2339
2340         lp_ctx->globals->szRealm_upper = strupper_talloc(lp_ctx, pszParmValue);
2341         lp_ctx->globals->szRealm_lower = strlower_talloc(lp_ctx, pszParmValue);
2342
2343         return true;
2344 }
2345
2346 /***************************************************************************
2347  Handle the include operation.
2348 ***************************************************************************/
2349
2350 static bool handle_include(struct loadparm_context *lp_ctx, int unused,
2351                            const char *pszParmValue, char **ptr)
2352 {
2353         char *fname = standard_sub_basic(lp_ctx, pszParmValue);
2354
2355         add_to_file_list(lp_ctx, pszParmValue, fname);
2356
2357         lpcfg_string_set(lp_ctx, ptr, fname);
2358
2359         if (file_exist(fname))
2360                 return pm_process(fname, do_section, do_parameter, lp_ctx);
2361
2362         DEBUG(2, ("Can't find include file %s\n", fname));
2363
2364         return false;
2365 }
2366
2367 /***************************************************************************
2368  Handle the interpretation of the copy parameter.
2369 ***************************************************************************/
2370
2371 static bool handle_copy(struct loadparm_context *lp_ctx, int unused,
2372                         const char *pszParmValue, char **ptr)
2373 {
2374         bool bRetval;
2375         struct loadparm_service *serviceTemp;
2376
2377         lpcfg_string_set(lp_ctx, ptr, pszParmValue);
2378
2379         bRetval = false;
2380
2381         DEBUG(3, ("Copying service from service %s\n", pszParmValue));
2382
2383         if ((serviceTemp = getservicebyname(lp_ctx, pszParmValue)) != NULL) {
2384                 if (serviceTemp == lp_ctx->currentService) {
2385                         DEBUG(0, ("Can't copy service %s - unable to copy self!\n", pszParmValue));
2386                 } else {
2387                         copy_service(lp_ctx->currentService,
2388                                      serviceTemp,
2389                                      lp_ctx->currentService->copymap);
2390                         bRetval = true;
2391                 }
2392         } else {
2393                 DEBUG(0, ("Unable to copy service - source not found: %s\n",
2394                           pszParmValue));
2395                 bRetval = false;
2396         }
2397
2398         return bRetval;
2399 }
2400
2401 static bool handle_debuglevel(struct loadparm_context *lp_ctx, int unused,
2402                         const char *pszParmValue, char **ptr)
2403 {
2404
2405         lpcfg_string_set(lp_ctx, ptr, pszParmValue);
2406         if (lp_ctx->global) {
2407                 return debug_parse_levels(pszParmValue);
2408         }
2409         return true;
2410 }
2411
2412 static bool handle_logfile(struct loadparm_context *lp_ctx, int unused,
2413                         const char *pszParmValue, char **ptr)
2414 {
2415         debug_set_logfile(pszParmValue);
2416         if (lp_ctx->global) {
2417                 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
2418         }
2419         return true;
2420 }
2421
2422 /***************************************************************************
2423  Initialise a copymap.
2424 ***************************************************************************/
2425
2426 static void init_copymap(struct loadparm_service *pservice)
2427 {
2428         int i;
2429
2430         TALLOC_FREE(pservice->copymap);
2431
2432         pservice->copymap = bitmap_talloc(NULL, NUMPARAMETERS);
2433         if (!pservice->copymap)
2434                 DEBUG(0,
2435                       ("Couldn't allocate copymap!! (size %d)\n",
2436                        (int)NUMPARAMETERS));
2437         else
2438                 for (i = 0; i < NUMPARAMETERS; i++)
2439                         bitmap_set(pservice->copymap, i);
2440 }
2441
2442 /**
2443  * Process a parametric option
2444  */
2445 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
2446                                        struct loadparm_service *service,
2447                                        const char *pszParmName,
2448                                        const char *pszParmValue, int flags)
2449 {
2450         struct parmlist_entry *paramo, *data;
2451         char *name;
2452         TALLOC_CTX *mem_ctx;
2453
2454         while (isspace((unsigned char)*pszParmName)) {
2455                 pszParmName++;
2456         }
2457
2458         name = strlower_talloc(lp_ctx, pszParmName);
2459         if (!name) return false;
2460
2461         if (service == NULL) {
2462                 data = lp_ctx->globals->param_opt;
2463                 mem_ctx = lp_ctx->globals;
2464         } else {
2465                 data = service->param_opt;
2466                 mem_ctx = service;
2467         }
2468
2469         /* Traverse destination */
2470         for (paramo=data; paramo; paramo=paramo->next) {
2471                 /* If we already have the option set, override it unless
2472                    it was a command line option and the new one isn't */
2473                 if (strcmp(paramo->key, name) == 0) {
2474                         if ((paramo->priority & FLAG_CMDLINE) &&
2475                             !(flags & FLAG_CMDLINE)) {
2476                                 talloc_free(name);
2477                                 return true;
2478                         }
2479
2480                         talloc_free(paramo->value);
2481                         paramo->value = talloc_strdup(paramo, pszParmValue);
2482                         paramo->priority = flags;
2483                         talloc_free(name);
2484                         return true;
2485                 }
2486         }
2487
2488         paramo = talloc_zero(mem_ctx, struct parmlist_entry);
2489         if (!paramo)
2490                 smb_panic("OOM");
2491         paramo->key = talloc_strdup(paramo, name);
2492         paramo->value = talloc_strdup(paramo, pszParmValue);
2493         paramo->priority = flags;
2494         if (service == NULL) {
2495                 DLIST_ADD(lp_ctx->globals->param_opt, paramo);
2496         } else {
2497                 DLIST_ADD(service->param_opt, paramo);
2498         }
2499
2500         talloc_free(name);
2501
2502         return true;
2503 }
2504
2505 static bool set_variable(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
2506                          const char *pszParmName, const char *pszParmValue,
2507                          struct loadparm_context *lp_ctx, bool on_globals)
2508 {
2509         int i;
2510         /* if it is a special case then go ahead */
2511         if (parm_table[parmnum].special) {
2512                 bool ret;
2513                 ret = parm_table[parmnum].special(lp_ctx, -1, pszParmValue,
2514                                                   (char **)parm_ptr);
2515                 if (!ret) {
2516                         return false;
2517                 }
2518                 goto mark_non_default;
2519         }
2520
2521         /* now switch on the type of variable it is */
2522         switch (parm_table[parmnum].type)
2523         {
2524                 case P_BOOL: {
2525                         bool b;
2526                         if (!set_boolean(pszParmValue, &b)) {
2527                                 DEBUG(0,("lp_do_parameter(%s): value is not boolean!\n", pszParmValue));
2528                                 return false;
2529                         }
2530                         *(bool *)parm_ptr = b;
2531                         }
2532                         break;
2533
2534                 case P_BOOLREV: {
2535                         bool b;
2536                         if (!set_boolean(pszParmValue, &b)) {
2537                                 DEBUG(0,("lp_do_parameter(%s): value is not boolean!\n", pszParmValue));
2538                                 return false;
2539                         }
2540                         *(bool *)parm_ptr = !b;
2541                         }
2542                         break;
2543
2544                 case P_INTEGER:
2545                         *(int *)parm_ptr = atoi(pszParmValue);
2546                         break;
2547
2548                 case P_CHAR:
2549                         *(char *)parm_ptr = *pszParmValue;
2550                         break;
2551
2552                 case P_OCTAL:
2553                         *(int *)parm_ptr = strtol(pszParmValue, NULL, 8);
2554                         break;
2555
2556                 case P_BYTES:
2557                 {
2558                         uint64_t val;
2559                         if (conv_str_size_error(pszParmValue, &val)) {
2560                                 if (val <= INT_MAX) {
2561                                         *(int *)parm_ptr = (int)val;
2562                                         break;
2563                                 }
2564                         }
2565
2566                         DEBUG(0,("lp_do_parameter(%s): value is not "
2567                             "a valid size specifier!\n", pszParmValue));
2568                         return false;
2569                 }
2570
2571                 case P_CMDLIST:
2572                         *(const char ***)parm_ptr = (const char **)str_list_make(mem_ctx,
2573                                                                   pszParmValue, NULL);
2574                         break;
2575                 case P_LIST:
2576                 {
2577                         char **new_list = str_list_make(mem_ctx,
2578                                                         pszParmValue, NULL);
2579                         for (i=0; new_list[i]; i++) {
2580                                 if (new_list[i][0] == '+' && new_list[i][1] &&
2581                                     (!str_list_check(*(const char ***)parm_ptr,
2582                                                      &new_list[i][1]))) {
2583                                         *(const char ***)parm_ptr = str_list_add(*(const char ***)parm_ptr,
2584                                                                                  &new_list[i][1]);
2585                                 } else if (new_list[i][0] == '-' && new_list[i][1]) {
2586 #if 0 /* This is commented out because we sometimes parse the list
2587        * twice, and so we can't assert on this */
2588                                         if (!str_list_check(*(const char ***)parm_ptr,
2589                                                             &new_list[i][1])) {
2590                                                 DEBUG(0, ("Unsupported value for: %s = %s, %s is not in the original list [%s]\n",
2591                                                           pszParmName, pszParmValue, new_list[i],
2592                                                           str_list_join_shell(mem_ctx, *(const char ***)parm_ptr, ' ')));
2593                                                 return false;
2594
2595                                         }
2596 #endif
2597                                         str_list_remove(*(const char ***)parm_ptr,
2598                                                         &new_list[i][1]);
2599                                 } else {
2600                                         if (i != 0) {
2601                                                 DEBUG(0, ("Unsupported list syntax for: %s = %s\n",
2602                                                           pszParmName, pszParmValue));
2603                                                 return false;
2604                                         }
2605                                         *(const char ***)parm_ptr = (const char **) new_list;
2606                                         break;
2607                                 }
2608                         }
2609                         break;
2610                 }
2611                 case P_STRING:
2612                         lpcfg_string_set(mem_ctx, (char **)parm_ptr, pszParmValue);
2613                         break;
2614
2615                 case P_USTRING:
2616                         lpcfg_string_set_upper(mem_ctx, (char **)parm_ptr, pszParmValue);
2617                         break;
2618
2619                 case P_ENUM:
2620                         for (i = 0; parm_table[parmnum].enum_list[i].name; i++) {
2621                                 if (strequal
2622                                     (pszParmValue,
2623                                      parm_table[parmnum].enum_list[i].name)) {
2624                                         *(int *)parm_ptr =
2625                                                 parm_table[parmnum].
2626                                                 enum_list[i].value;
2627                                         break;
2628                                 }
2629                         }
2630                         if (!parm_table[parmnum].enum_list[i].name) {
2631                                 DEBUG(0,("Unknown enumerated value '%s' for '%s'\n", 
2632                                          pszParmValue, pszParmName));
2633                                 return false;
2634                         }
2635                         break;
2636         }
2637
2638 mark_non_default:
2639         if (on_globals && (lp_ctx->flags[parmnum] & FLAG_DEFAULT)) {
2640                 lp_ctx->flags[parmnum] &= ~FLAG_DEFAULT;
2641                 /* we have to also unset FLAG_DEFAULT on aliases */
2642                 for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
2643                         lp_ctx->flags[i] &= ~FLAG_DEFAULT;
2644                 }
2645                 for (i=parmnum+1;i<NUMPARAMETERS && parm_table[i].offset == parm_table[parmnum].offset;i++) {
2646                         lp_ctx->flags[i] &= ~FLAG_DEFAULT;
2647                 }
2648         }
2649         return true;
2650 }
2651
2652
2653 bool lpcfg_do_global_parameter(struct loadparm_context *lp_ctx,
2654                                const char *pszParmName, const char *pszParmValue)
2655 {
2656         int parmnum = map_parameter(pszParmName);
2657         void *parm_ptr;
2658
2659         if (parmnum < 0) {
2660                 if (strchr(pszParmName, ':')) {
2661                         return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName, pszParmValue, 0);
2662                 }
2663                 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
2664                 return true;
2665         }
2666
2667         /* if the flag has been set on the command line, then don't allow override,
2668            but don't report an error */
2669         if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
2670                 return true;
2671         }
2672
2673         parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[parmnum]);
2674
2675         return set_variable(lp_ctx->globals, parmnum, parm_ptr,
2676                             pszParmName, pszParmValue, lp_ctx, true);
2677 }
2678
2679 bool lpcfg_do_service_parameter(struct loadparm_context *lp_ctx,
2680                                 struct loadparm_service *service,
2681                                 const char *pszParmName, const char *pszParmValue)
2682 {
2683         void *parm_ptr;
2684         int i;
2685         int parmnum = map_parameter(pszParmName);
2686
2687         if (parmnum < 0) {
2688                 if (strchr(pszParmName, ':')) {
2689                         return lp_do_parameter_parametric(lp_ctx, service, pszParmName, pszParmValue, 0);
2690                 }
2691                 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
2692                 return true;
2693         }
2694
2695         /* if the flag has been set on the command line, then don't allow override,
2696            but don't report an error */
2697         if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
2698                 return true;
2699         }
2700
2701         if (parm_table[parmnum].p_class == P_GLOBAL) {
2702                 DEBUG(0,
2703                       ("Global parameter %s found in service section!\n",
2704                        pszParmName));
2705                 return true;
2706         }
2707         parm_ptr = ((char *)service) + parm_table[parmnum].offset;
2708
2709         if (!service->copymap)
2710                 init_copymap(service);
2711
2712         /* this handles the aliases - set the copymap for other
2713          * entries with the same data pointer */
2714         for (i = 0; parm_table[i].label; i++)
2715                 if (parm_table[i].offset == parm_table[parmnum].offset &&
2716                     parm_table[i].p_class == parm_table[parmnum].p_class)
2717                         bitmap_clear(service->copymap, i);
2718
2719         return set_variable(service, parmnum, parm_ptr, pszParmName,
2720                             pszParmValue, lp_ctx, false);
2721 }
2722
2723 /**
2724  * Process a parameter.
2725  */
2726
2727 static bool do_parameter(const char *pszParmName, const char *pszParmValue,
2728                          void *userdata)
2729 {
2730         struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
2731
2732         if (lp_ctx->bInGlobalSection)
2733                 return lpcfg_do_global_parameter(lp_ctx, pszParmName,
2734                                               pszParmValue);
2735         else
2736                 return lpcfg_do_service_parameter(lp_ctx, lp_ctx->currentService,
2737                                                   pszParmName, pszParmValue);
2738 }
2739
2740 /*
2741   variable argument do parameter
2742 */
2743 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx, const char *pszParmName, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
2744 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx,
2745                                 const char *pszParmName, const char *fmt, ...)
2746 {
2747         char *s;
2748         bool ret;
2749         va_list ap;
2750
2751         va_start(ap, fmt);
2752         s = talloc_vasprintf(NULL, fmt, ap);
2753         va_end(ap);
2754         ret = lpcfg_do_global_parameter(lp_ctx, pszParmName, s);
2755         talloc_free(s);
2756         return ret;
2757 }
2758
2759
2760 /*
2761   set a parameter from the commandline - this is called from command line parameter
2762   parsing code. It sets the parameter then marks the parameter as unable to be modified
2763   by smb.conf processing
2764 */
2765 bool lpcfg_set_cmdline(struct loadparm_context *lp_ctx, const char *pszParmName,
2766                        const char *pszParmValue)
2767 {
2768         int parmnum;
2769         int i;
2770
2771         if (lp_ctx->s3_fns) {
2772                 return lp_ctx->s3_fns->set_cmdline(pszParmName, pszParmValue);
2773         }
2774
2775         parmnum = map_parameter(pszParmName);
2776
2777         while (isspace((unsigned char)*pszParmValue)) pszParmValue++;
2778
2779
2780         if (parmnum < 0 && strchr(pszParmName, ':')) {
2781                 /* set a parametric option */
2782                 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName,
2783                                                   pszParmValue, FLAG_CMDLINE);
2784         }
2785
2786         if (parmnum < 0) {
2787                 DEBUG(0,("Unknown option '%s'\n", pszParmName));
2788                 return false;
2789         }
2790
2791         /* reset the CMDLINE flag in case this has been called before */
2792         lp_ctx->flags[parmnum] &= ~FLAG_CMDLINE;
2793
2794         if (!lpcfg_do_global_parameter(lp_ctx, pszParmName, pszParmValue)) {
2795                 return false;
2796         }
2797
2798         lp_ctx->flags[parmnum] |= FLAG_CMDLINE;
2799
2800         /* we have to also set FLAG_CMDLINE on aliases */
2801         for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
2802                 lp_ctx->flags[i] |= FLAG_CMDLINE;
2803         }
2804         for (i=parmnum+1;i<NUMPARAMETERS && parm_table[i].offset == parm_table[parmnum].offset;i++) {
2805                 lp_ctx->flags[i] |= FLAG_CMDLINE;
2806         }
2807
2808         return true;
2809 }
2810
2811 /*
2812   set a option from the commandline in 'a=b' format. Use to support --option
2813 */
2814 bool lpcfg_set_option(struct loadparm_context *lp_ctx, const char *option)
2815 {
2816         char *p, *s;
2817         bool ret;
2818
2819         s = talloc_strdup(NULL, option);
2820         if (!s) {
2821                 return false;
2822         }
2823
2824         p = strchr(s, '=');
2825         if (!p) {
2826                 talloc_free(s);
2827                 return false;
2828         }
2829
2830         *p = 0;
2831
2832         ret = lpcfg_set_cmdline(lp_ctx, s, p+1);
2833         talloc_free(s);
2834         return ret;
2835 }
2836
2837
2838 #define BOOLSTR(b) ((b) ? "Yes" : "No")
2839
2840 /**
2841  * Print a parameter of the specified type.
2842  */
2843
2844 static void print_parameter(struct parm_struct *p, void *ptr, FILE * f)
2845 {
2846         /* For the seperation of lists values that we print below */
2847         const char *list_sep = ", ";
2848         int i;
2849         switch (p->type)
2850         {
2851                 case P_ENUM:
2852                         for (i = 0; p->enum_list[i].name; i++) {
2853                                 if (*(int *)ptr == p->enum_list[i].value) {
2854                                         fprintf(f, "%s",
2855                                                 p->enum_list[i].name);
2856                                         break;
2857                                 }
2858                         }
2859                         break;
2860
2861                 case P_BOOL:
2862                         fprintf(f, "%s", BOOLSTR(*(bool *)ptr));
2863                         break;
2864
2865                 case P_BOOLREV:
2866                         fprintf(f, "%s", BOOLSTR(!*(bool *)ptr));
2867                         break;
2868
2869                 case P_INTEGER:
2870                 case P_BYTES:
2871                         fprintf(f, "%d", *(int *)ptr);
2872                         break;
2873
2874                 case P_CHAR:
2875                         fprintf(f, "%c", *(char *)ptr);
2876                         break;
2877
2878                 case P_OCTAL: {
2879                         int val = *(int *)ptr; 
2880                         if (val == -1) {
2881                                 fprintf(f, "-1");
2882                         } else {
2883                                 fprintf(f, "0%o", val);
2884                         }
2885                         break;
2886                 }
2887
2888                 case P_CMDLIST:
2889                         list_sep = " ";
2890                         /* fall through */
2891                 case P_LIST:
2892                         if ((char ***)ptr && *(char ***)ptr) {
2893                                 char **list = *(char ***)ptr;
2894                                 for (; *list; list++) {
2895                                         /* surround strings with whitespace in double quotes */
2896                                         if (*(list+1) == NULL) {
2897                                                 /* last item, no extra separator */
2898                                                 list_sep = "";
2899                                         }
2900                                         if ( strchr_m( *list, ' ' ) ) {
2901                                                 fprintf(f, "\"%s\"%s", *list, list_sep);
2902                                         } else {
2903                                                 fprintf(f, "%s%s", *list, list_sep);
2904                                         }
2905                                 }
2906                         }
2907                         break;
2908
2909                 case P_STRING:
2910                 case P_USTRING:
2911                         if (*(char **)ptr) {
2912                                 fprintf(f, "%s", *(char **)ptr);
2913                         }
2914                         break;
2915                 case P_SEP:
2916                         break;
2917         }
2918 }
2919
2920 /**
2921  * Check if two parameters are equal.
2922  */
2923
2924 static bool equal_parameter(parm_type type, void *ptr1, void *ptr2)
2925 {
2926         switch (type) {
2927                 case P_BOOL:
2928                 case P_BOOLREV:
2929                         return (*((bool *)ptr1) == *((bool *)ptr2));
2930
2931                 case P_INTEGER:
2932                 case P_ENUM:
2933                 case P_OCTAL:
2934                 case P_BYTES:
2935                         return (*((int *)ptr1) == *((int *)ptr2));
2936
2937                 case P_CHAR:
2938                         return (*((char *)ptr1) == *((char *)ptr2));
2939
2940                 case P_LIST:
2941                 case P_CMDLIST:
2942                         return str_list_equal(*(const char ***)ptr1, *(const char ***)ptr2);
2943
2944                 case P_STRING:
2945                 case P_USTRING:
2946                 {
2947                         char *p1 = *(char **)ptr1, *p2 = *(char **)ptr2;
2948                         if (p1 && !*p1)
2949                                 p1 = NULL;
2950                         if (p2 && !*p2)
2951                                 p2 = NULL;
2952                         return (p1 == p2 || strequal(p1, p2));
2953                 }
2954                 case P_SEP:
2955                         break;
2956         }
2957         return false;
2958 }
2959
2960 /**
2961  * Process a new section (service).
2962  *
2963  * At this stage all sections are services.
2964  * Later we'll have special sections that permit server parameters to be set.
2965  * Returns True on success, False on failure.
2966  */
2967
2968 static bool do_section(const char *pszSectionName, void *userdata)
2969 {
2970         struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
2971         bool bRetval;
2972         bool isglobal = ((strwicmp(pszSectionName, GLOBAL_NAME) == 0) ||
2973                          (strwicmp(pszSectionName, GLOBAL_NAME2) == 0));
2974         bRetval = false;
2975
2976         /* if we've just struck a global section, note the fact. */
2977         lp_ctx->bInGlobalSection = isglobal;
2978
2979         /* check for multiple global sections */
2980         if (lp_ctx->bInGlobalSection) {
2981                 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
2982                 return true;
2983         }
2984
2985         /* if we have a current service, tidy it up before moving on */
2986         bRetval = true;
2987
2988         if (lp_ctx->currentService != NULL)
2989                 bRetval = lpcfg_service_ok(lp_ctx->currentService);
2990
2991         /* if all is still well, move to the next record in the services array */
2992         if (bRetval) {
2993                 /* We put this here to avoid an odd message order if messages are */
2994                 /* issued by the post-processing of a previous section. */
2995                 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
2996
2997                 if ((lp_ctx->currentService = lpcfg_add_service(lp_ctx, lp_ctx->sDefault,
2998                                                                    pszSectionName))
2999                     == NULL) {
3000                         DEBUG(0, ("Failed to add a new service\n"));
3001                         return false;
3002                 }
3003         }
3004
3005         return bRetval;
3006 }
3007
3008
3009 /**
3010  * Determine if a particular base parameter is currently set to the default value.
3011  */
3012
3013 static bool is_default(struct loadparm_service *sDefault, int i)
3014 {
3015         void *def_ptr = ((char *)sDefault) + parm_table[i].offset;
3016         if (!defaults_saved)
3017                 return false;
3018         switch (parm_table[i].type) {
3019                 case P_CMDLIST:
3020                 case P_LIST:
3021                         return str_list_equal((const char **)parm_table[i].def.lvalue, 
3022                                               (const char **)def_ptr);
3023                 case P_STRING:
3024                 case P_USTRING:
3025                         return strequal(parm_table[i].def.svalue,
3026                                         *(char **)def_ptr);
3027                 case P_BOOL:
3028                 case P_BOOLREV:
3029                         return parm_table[i].def.bvalue ==
3030                                 *(bool *)def_ptr;
3031                 case P_INTEGER:
3032                 case P_CHAR:
3033                 case P_OCTAL:
3034                 case P_BYTES:
3035                 case P_ENUM:
3036                         return parm_table[i].def.ivalue ==
3037                                 *(int *)def_ptr;
3038         }
3039         return false;
3040 }
3041
3042 /**
3043  *Display the contents of the global structure.
3044  */
3045
3046 static void dump_globals(struct loadparm_context *lp_ctx, FILE *f,
3047                          bool show_defaults)
3048 {
3049         int i;
3050         struct parmlist_entry *data;
3051
3052         fprintf(f, "# Global parameters\n[global]\n");
3053
3054         for (i = 0; parm_table[i].label; i++)
3055                 if (parm_table[i].p_class == P_GLOBAL &&
3056                     (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset))) {
3057                         if (!show_defaults && (lp_ctx->flags[i] & FLAG_DEFAULT))
3058                                 continue;
3059                         fprintf(f, "\t%s = ", parm_table[i].label);
3060                         print_parameter(&parm_table[i], lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[i]), f);
3061                         fprintf(f, "\n");
3062         }
3063         if (lp_ctx->globals->param_opt != NULL) {
3064                 for (data = lp_ctx->globals->param_opt; data;
3065                      data = data->next) {
3066                         if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
3067                                 continue;
3068                         }
3069                         fprintf(f, "\t%s = %s\n", data->key, data->value);
3070                 }
3071         }
3072
3073 }
3074
3075 /**
3076  * Display the contents of a single services record.
3077  */
3078
3079 static void dump_a_service(struct loadparm_service * pService, struct loadparm_service *sDefault, FILE * f,
3080                            unsigned int *flags)
3081 {
3082         int i;
3083         struct parmlist_entry *data;
3084
3085         if (pService != sDefault)
3086                 fprintf(f, "\n[%s]\n", pService->szService);
3087
3088         for (i = 0; parm_table[i].label; i++) {
3089                 if (parm_table[i].p_class == P_LOCAL &&
3090                     (*parm_table[i].label != '-') &&
3091                     (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset)))
3092                 {
3093                         if (pService == sDefault) {
3094                                 if (flags && (flags[i] & FLAG_DEFAULT)) {
3095                                         continue;
3096                                 }
3097                                 if (defaults_saved) {
3098                                         if (is_default(sDefault, i)) {
3099                                                 continue;
3100                                         }
3101                                 }
3102                         } else {
3103                                 if (equal_parameter(parm_table[i].type,
3104                                                     ((char *)pService) +
3105                                                     parm_table[i].offset,
3106                                                     ((char *)sDefault) +
3107                                                     parm_table[i].offset))
3108                                         continue;
3109                         }
3110
3111                         fprintf(f, "\t%s = ", parm_table[i].label);
3112                         print_parameter(&parm_table[i],
3113                                         ((char *)pService) + parm_table[i].offset, f);
3114                         fprintf(f, "\n");
3115                 }
3116         }
3117         if (pService->param_opt != NULL) {
3118                 for (data = pService->param_opt; data; data = data->next) {
3119                         fprintf(f, "\t%s = %s\n", data->key, data->value);
3120                 }
3121         }
3122 }
3123
3124 bool lpcfg_dump_a_parameter(struct loadparm_context *lp_ctx,
3125                             struct loadparm_service *service,
3126                             const char *parm_name, FILE * f)
3127 {
3128         struct parm_struct *parm;
3129         void *ptr;
3130
3131         parm = lpcfg_parm_struct(lp_ctx, parm_name);
3132         if (!parm) {
3133                 return false;
3134         }
3135
3136         ptr = lpcfg_parm_ptr(lp_ctx, service,parm);
3137
3138         print_parameter(parm, ptr, f);
3139         fprintf(f, "\n");
3140         return true;
3141 }
3142
3143 /**
3144  * Return info about the next parameter in a service.
3145  * snum==-1 gives the globals.
3146  * Return NULL when out of parameters.
3147  */
3148
3149
3150 struct parm_struct *lpcfg_next_parameter(struct loadparm_context *lp_ctx, int snum, int *i,
3151                                          int allparameters)
3152 {
3153         if (snum == -1) {
3154                 /* do the globals */
3155                 for (; parm_table[*i].label; (*i)++) {
3156                         if ((*parm_table[*i].label == '-'))
3157                                 continue;
3158
3159                         if ((*i) > 0
3160                             && (parm_table[*i].offset ==
3161                                 parm_table[(*i) - 1].offset)
3162                             && (parm_table[*i].p_class ==
3163                                 parm_table[(*i) - 1].p_class))
3164                                 continue;
3165
3166                         return &parm_table[(*i)++];
3167                 }
3168         } else {
3169                 struct loadparm_service *pService = lp_ctx->services[snum];
3170
3171                 for (; parm_table[*i].label; (*i)++) {
3172                         if (parm_table[*i].p_class == P_LOCAL &&
3173                             (*parm_table[*i].label != '-') &&
3174                             ((*i) == 0 ||
3175                              (parm_table[*i].offset !=
3176                               parm_table[(*i) - 1].offset)))
3177                         {
3178                                 if (allparameters ||
3179                                     !equal_parameter(parm_table[*i].type,
3180                                                      ((char *)pService) +
3181                                                      parm_table[*i].offset,
3182                                                      ((char *)lp_ctx->sDefault) +
3183                                                      parm_table[*i].offset))
3184                                 {
3185                                         return &parm_table[(*i)++];
3186                                 }
3187                         }
3188                 }
3189         }
3190
3191         return NULL;
3192 }
3193
3194
3195 /**
3196  * Auto-load some home services.
3197  */
3198 static void lpcfg_add_auto_services(struct loadparm_context *lp_ctx,
3199                                     const char *str)
3200 {
3201         return;
3202 }
3203
3204
3205 /**
3206  * Unload unused services.
3207  */
3208
3209 void lpcfg_killunused(struct loadparm_context *lp_ctx,
3210                    struct smbsrv_connection *smb,
3211                    bool (*snumused) (struct smbsrv_connection *, int))
3212 {
3213         int i;
3214         for (i = 0; i < lp_ctx->iNumServices; i++) {
3215                 if (lp_ctx->services[i] == NULL)
3216                         continue;
3217
3218                 if (!snumused || !snumused(smb, i)) {
3219                         talloc_free(lp_ctx->services[i]);
3220                         lp_ctx->services[i] = NULL;
3221                 }
3222         }
3223 }
3224
3225
3226 static int lpcfg_destructor(struct loadparm_context *lp_ctx)
3227 {
3228         struct parmlist_entry *data;
3229
3230         if (lp_ctx->refuse_free) {
3231                 /* someone is trying to free the
3232                    global_loadparm_context.
3233                    We can't allow that. */
3234                 return -1;
3235         }
3236
3237         if (lp_ctx->globals->param_opt != NULL) {
3238                 struct parmlist_entry *next;
3239                 for (data = lp_ctx->globals->param_opt; data; data=next) {
3240                         next = data->next;
3241                         if (data->priority & FLAG_CMDLINE) continue;
3242                         DLIST_REMOVE(lp_ctx->globals->param_opt, data);
3243                         talloc_free(data);
3244                 }
3245         }
3246
3247         return 0;
3248 }
3249
3250 /**
3251  * Initialise the global parameter structure.
3252  *
3253  * Note that most callers should use loadparm_init_global() instead
3254  */
3255 struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
3256 {
3257         int i;
3258         char *myname;
3259         struct loadparm_context *lp_ctx;
3260         struct parmlist_entry *parm;
3261         char *logfile;
3262
3263         lp_ctx = talloc_zero(mem_ctx, struct loadparm_context);
3264         if (lp_ctx == NULL)
3265                 return NULL;
3266
3267         talloc_set_destructor(lp_ctx, lpcfg_destructor);
3268         lp_ctx->bInGlobalSection = true;
3269         lp_ctx->globals = talloc_zero(lp_ctx, struct loadparm_global);
3270         lp_ctx->sDefault = talloc_zero(lp_ctx, struct loadparm_service);
3271
3272         lp_ctx->sDefault->iMaxPrintJobs = 1000;
3273         lp_ctx->sDefault->bAvailable = true;
3274         lp_ctx->sDefault->bBrowseable = true;
3275         lp_ctx->sDefault->bRead_only = true;
3276         lp_ctx->sDefault->bMap_archive = true;
3277         lp_ctx->sDefault->iStrictLocking = true;
3278         lp_ctx->sDefault->bOpLocks = true;
3279         lp_ctx->sDefault->iCreate_mask = 0744;
3280         lp_ctx->sDefault->iCreate_force_mode = 0000;
3281         lp_ctx->sDefault->iDir_mask = 0755;
3282         lp_ctx->sDefault->iDir_force_mode = 0000;
3283
3284         DEBUG(3, ("Initialising global parameters\n"));
3285
3286         for (i = 0; parm_table[i].label; i++) {
3287                 if ((parm_table[i].type == P_STRING ||
3288                      parm_table[i].type == P_USTRING) &&
3289                     !(lp_ctx->flags[i] & FLAG_CMDLINE)) {
3290                         char **r;
3291                         if (parm_table[i].p_class == P_LOCAL) {
3292                                 r = (char **)(((char *)lp_ctx->sDefault) + parm_table[i].offset);
3293                         } else {
3294                                 r = (char **)(((char *)lp_ctx->globals) + parm_table[i].offset);
3295                         }
3296                         *r = talloc_strdup(lp_ctx, "");
3297                 }
3298         }
3299
3300         logfile = talloc_asprintf(lp_ctx, "%s/log.samba", dyn_LOGFILEBASE);
3301         lpcfg_do_global_parameter(lp_ctx, "log file", logfile);
3302         talloc_free(logfile);
3303
3304         lpcfg_do_global_parameter(lp_ctx, "log level", "0");
3305
3306         lpcfg_do_global_parameter(lp_ctx, "share backend", "classic");
3307
3308         lpcfg_do_global_parameter(lp_ctx, "share backend", "classic");
3309
3310         lpcfg_do_global_parameter(lp_ctx, "server role", "standalone");
3311
3312         /* options that can be set on the command line must be initialised via
3313            the slower lpcfg_do_global_parameter() to ensure that FLAG_CMDLINE is obeyed */
3314 #ifdef TCP_NODELAY
3315         lpcfg_do_global_parameter(lp_ctx, "socket options", "TCP_NODELAY");
3316 #endif
3317         lpcfg_do_global_parameter(lp_ctx, "workgroup", DEFAULT_WORKGROUP);
3318         myname = get_myname(lp_ctx);
3319         lpcfg_do_global_parameter(lp_ctx, "netbios name", myname);
3320         talloc_free(myname);
3321         lpcfg_do_global_parameter(lp_ctx, "name resolve order", "wins host bcast");
3322
3323         lpcfg_do_global_parameter(lp_ctx, "fstype", "NTFS");
3324
3325         lpcfg_do_global_parameter(lp_ctx, "ntvfs handler", "unixuid default");
3326         lpcfg_do_global_parameter(lp_ctx, "max connections", "-1");
3327
3328         lpcfg_do_global_parameter(lp_ctx, "dcerpc endpoint servers", "epmapper srvsvc wkssvc rpcecho samr netlogon lsarpc spoolss drsuapi winreg dssetup unixinfo browser eventlog6 backupkey");
3329         lpcfg_do_global_parameter(lp_ctx, "server services", "smb rpc nbt wrepl ldap cldap kdc drepl winbind ntp_signd kcc dnsupdate");
3330         lpcfg_do_global_parameter(lp_ctx, "ntptr providor", "simple_ldb");
3331         /* the winbind method for domain controllers is for both RODC
3332            auth forwarding and for trusted domains */
3333         lpcfg_do_global_parameter(lp_ctx, "private dir", dyn_PRIVATE_DIR);
3334         lpcfg_do_global_parameter(lp_ctx, "spoolss database", "spoolss.ldb");
3335         lpcfg_do_global_parameter(lp_ctx, "wins config database", "wins_config.ldb");
3336         lpcfg_do_global_parameter(lp_ctx, "wins database", "wins.ldb");
3337         lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_LOCAL_MACHINE", "hklm.ldb");
3338
3339         /* This hive should be dynamically generated by Samba using
3340            data from the sam, but for the moment leave it in a tdb to
3341            keep regedt32 from popping up an annoying dialog. */
3342         lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_USERS", "hku.ldb");
3343
3344         /* using UTF8 by default allows us to support all chars */
3345         lpcfg_do_global_parameter(lp_ctx, "unix charset", "UTF8");
3346
3347         /* Use codepage 850 as a default for the dos character set */
3348         lpcfg_do_global_parameter(lp_ctx, "dos charset", "CP850");
3349
3350         /*
3351          * Allow the default PASSWD_CHAT to be overridden in local.h.
3352          */
3353         lpcfg_do_global_parameter(lp_ctx, "passwd chat", DEFAULT_PASSWD_CHAT);
3354
3355         lpcfg_do_global_parameter(lp_ctx, "pid directory", dyn_PIDDIR);
3356         lpcfg_do_global_parameter(lp_ctx, "lock dir", dyn_LOCKDIR);
3357         lpcfg_do_global_parameter(lp_ctx, "state directory", dyn_STATEDIR);
3358         lpcfg_do_global_parameter(lp_ctx, "cache directory", dyn_CACHEDIR);
3359         lpcfg_do_global_parameter(lp_ctx, "ncalrpc dir", dyn_NCALRPCDIR);
3360
3361         lpcfg_do_global_parameter(lp_ctx, "socket address", "");
3362         lpcfg_do_global_parameter_var(lp_ctx, "server string",
3363                                    "Samba %s", SAMBA_VERSION_STRING);
3364
3365         lpcfg_do_global_parameter(lp_ctx, "password server", "*");
3366
3367         lpcfg_do_global_parameter(lp_ctx, "max mux", "50");
3368         lpcfg_do_global_parameter(lp_ctx, "max xmit", "12288");
3369         lpcfg_do_global_parameter(lp_ctx, "password level", "0");
3370         lpcfg_do_global_parameter(lp_ctx, "LargeReadwrite", "True");
3371         lpcfg_do_global_parameter(lp_ctx, "server min protocol", "CORE");
3372         lpcfg_do_global_parameter(lp_ctx, "server max protocol", "NT1");
3373         lpcfg_do_global_parameter(lp_ctx, "client min protocol", "CORE");
3374         lpcfg_do_global_parameter(lp_ctx, "client max protocol", "NT1");
3375         lpcfg_do_global_parameter(lp_ctx, "security", "USER");
3376         lpcfg_do_global_parameter(lp_ctx, "paranoid server security", "True");
3377         lpcfg_do_global_parameter(lp_ctx, "EncryptPasswords", "True");
3378         lpcfg_do_global_parameter(lp_ctx, "ReadRaw", "True");
3379         lpcfg_do_global_parameter(lp_ctx, "WriteRaw", "True");
3380         lpcfg_do_global_parameter(lp_ctx, "NullPasswords", "False");
3381         lpcfg_do_global_parameter(lp_ctx, "ObeyPamRestrictions", "False");
3382
3383         lpcfg_do_global_parameter(lp_ctx, "TimeServer", "False");
3384         lpcfg_do_global_parameter(lp_ctx, "BindInterfacesOnly", "False");
3385         lpcfg_do_global_parameter(lp_ctx, "Unicode", "True");
3386         lpcfg_do_global_parameter(lp_ctx, "ClientLanManAuth", "False");
3387         lpcfg_do_global_parameter(lp_ctx, "ClientNTLMv2Auth", "True");
3388         lpcfg_do_global_parameter(lp_ctx, "LanmanAuth", "False");
3389         lpcfg_do_global_parameter(lp_ctx, "NTLMAuth", "True");
3390         lpcfg_do_global_parameter(lp_ctx, "client use spnego principal", "False");
3391
3392         lpcfg_do_global_parameter(lp_ctx, "UnixExtensions", "False");
3393
3394         lpcfg_do_global_parameter(lp_ctx, "PreferredMaster", "Auto");
3395         lpcfg_do_global_parameter(lp_ctx, "LocalMaster", "True");
3396
3397         lpcfg_do_global_parameter(lp_ctx, "wins support", "False");
3398         lpcfg_do_global_parameter(lp_ctx, "dns proxy", "True");
3399
3400         lpcfg_do_global_parameter(lp_ctx, "winbind separator", "\\");
3401         lpcfg_do_global_parameter(lp_ctx, "winbind sealed pipes", "True");
3402         lpcfg_do_global_parameter(lp_ctx, "winbindd socket directory", dyn_WINBINDD_SOCKET_DIR);
3403 #if _SAMBA_BUILD_ >= 4
3404         lpcfg_do_global_parameter(lp_ctx, "winbindd privileged socket directory", dyn_WINBINDD_PRIVILEGED_SOCKET_DIR);
3405         lpcfg_do_global_parameter(lp_ctx, "ntp signd socket directory", dyn_NTP_SIGND_SOCKET_DIR);
3406         lpcfg_do_global_parameter_var(lp_ctx, "dns update command", "%s/samba_dnsupdate", dyn_SCRIPTSBINDIR);
3407         lpcfg_do_global_parameter_var(lp_ctx, "spn update command", "%s/samba_spnupdate", dyn_SCRIPTSBINDIR);
3408 #endif
3409         lpcfg_do_global_parameter(lp_ctx, "template shell", "/bin/false");
3410         lpcfg_do_global_parameter(lp_ctx, "template homedir", "/home/%WORKGROUP%/%ACCOUNTNAME%");
3411         lpcfg_do_global_parameter(lp_ctx, "idmap trusted only", "False");
3412
3413         lpcfg_do_global_parameter(lp_ctx, "client signing", "Yes");
3414         lpcfg_do_global_parameter(lp_ctx, "server signing", "auto");
3415
3416         lpcfg_do_global_parameter(lp_ctx, "use spnego", "True");
3417
3418         lpcfg_do_global_parameter(lp_ctx, "use mmap", "True");
3419
3420         lpcfg_do_global_parameter(lp_ctx, "smb ports", "445 139");
3421         lpcfg_do_global_parameter(lp_ctx, "nbt port", "137");
3422         lpcfg_do_global_parameter(lp_ctx, "dgram port", "138");
3423         lpcfg_do_global_parameter(lp_ctx, "cldap port", "389");
3424         lpcfg_do_global_parameter(lp_ctx, "krb5 port", "88");
3425         lpcfg_do_global_parameter(lp_ctx, "kpasswd port", "464");
3426         lpcfg_do_global_parameter(lp_ctx, "web port", "901");
3427
3428         lpcfg_do_global_parameter(lp_ctx, "nt status support", "True");
3429
3430         lpcfg_do_global_parameter(lp_ctx, "max wins ttl", "518400"); /* 6 days */
3431         lpcfg_do_global_parameter(lp_ctx, "min wins ttl", "10");
3432
3433         lpcfg_do_global_parameter(lp_ctx, "tls enabled", "True");
3434         lpcfg_do_global_parameter(lp_ctx, "tls keyfile", "tls/key.pem");
3435         lpcfg_do_global_parameter(lp_ctx, "tls certfile", "tls/cert.pem");
3436         lpcfg_do_global_parameter(lp_ctx, "tls cafile", "tls/ca.pem");
3437         lpcfg_do_global_parameter(lp_ctx, "prefork children:smb", "4");
3438
3439         lpcfg_do_global_parameter(lp_ctx, "rndc command", "/usr/sbin/rndc");
3440         lpcfg_do_global_parameter(lp_ctx, "nsupdate command", "/usr/bin/nsupdate -g");
3441
3442         for (i = 0; parm_table[i].label; i++) {
3443                 if (!(lp_ctx->flags[i] & FLAG_CMDLINE)) {
3444                         lp_ctx->flags[i] |= FLAG_DEFAULT;
3445                 }
3446         }
3447
3448         for (parm=lp_ctx->globals->param_opt; parm; parm=parm->next) {
3449                 if (!(parm->priority & FLAG_CMDLINE)) {
3450                         parm->priority |= FLAG_DEFAULT;
3451                 }
3452         }
3453
3454         return lp_ctx;
3455 }
3456
3457 /**
3458  * Initialise the global parameter structure.
3459  */
3460 struct loadparm_context *loadparm_init_global(bool load_default)
3461 {
3462         if (global_loadparm_context == NULL) {
3463                 global_loadparm_context = loadparm_init(NULL);
3464         }
3465         if (global_loadparm_context == NULL) {
3466                 return NULL;
3467         }
3468         global_loadparm_context->global = true;
3469         if (load_default && !global_loadparm_context->loaded) {
3470                 lpcfg_load_default(global_loadparm_context);
3471         }
3472         global_loadparm_context->refuse_free = true;
3473         return global_loadparm_context;
3474 }
3475
3476 /**
3477  * Initialise the global parameter structure.
3478  */
3479 struct loadparm_context *loadparm_init_s3(TALLOC_CTX *mem_ctx, 
3480                                           const struct loadparm_s3_context *s3_fns)
3481 {
3482         struct loadparm_context *loadparm_context = talloc_zero(mem_ctx, struct loadparm_context);
3483         if (!loadparm_context) {
3484                 return NULL;
3485         }
3486         loadparm_context->s3_fns = s3_fns;
3487         return loadparm_context;
3488 }
3489
3490 const char *lpcfg_configfile(struct loadparm_context *lp_ctx)
3491 {
3492         return lp_ctx->szConfigFile;
3493 }
3494
3495 const char *lp_default_path(void)
3496 {
3497     if (getenv("SMB_CONF_PATH"))
3498         return getenv("SMB_CONF_PATH");
3499     else
3500         return dyn_CONFIGFILE;
3501 }
3502
3503 /**
3504  * Update the internal state of a loadparm context after settings 
3505  * have changed.
3506  */
3507 static bool lpcfg_update(struct loadparm_context *lp_ctx)
3508 {
3509         struct debug_settings settings;
3510         lpcfg_add_auto_services(lp_ctx, lpcfg_auto_services(lp_ctx));
3511
3512         if (!lp_ctx->globals->szWINSservers && lp_ctx->globals->bWINSsupport) {
3513                 lpcfg_do_global_parameter(lp_ctx, "wins server", "127.0.0.1");
3514         }
3515
3516         if (!lp_ctx->global) {
3517                 return true;
3518         }
3519
3520         panic_action = lp_ctx->globals->panic_action;
3521
3522         reload_charcnv(lp_ctx);
3523
3524         ZERO_STRUCT(settings);
3525         /* Add any more debug-related smb.conf parameters created in
3526          * future here */
3527         settings.timestamp_logs = true;
3528         debug_set_settings(&settings);
3529
3530         /* FIXME: This is a bit of a hack, but we can't use a global, since 
3531          * not everything that uses lp also uses the socket library */
3532         if (lpcfg_parm_bool(lp_ctx, NULL, "socket", "testnonblock", false)) {
3533                 setenv("SOCKET_TESTNONBLOCK", "1", 1);
3534         } else {
3535                 unsetenv("SOCKET_TESTNONBLOCK");
3536         }
3537
3538         return true;
3539 }
3540
3541 bool lpcfg_load_default(struct loadparm_context *lp_ctx)
3542 {
3543     const char *path;
3544
3545     path = lp_default_path();
3546
3547     if (!file_exist(path)) {
3548             /* We allow the default smb.conf file to not exist, 
3549              * basically the equivalent of an empty file. */
3550             return lpcfg_update(lp_ctx);
3551     }
3552
3553     return lpcfg_load(lp_ctx, path);
3554 }
3555
3556 /**
3557  * Load the services array from the services file.
3558  *
3559  * Return True on success, False on failure.
3560  */
3561 bool lpcfg_load(struct loadparm_context *lp_ctx, const char *filename)
3562 {
3563         char *n2;
3564         bool bRetval;
3565
3566         filename = talloc_strdup(lp_ctx, filename);
3567
3568         lp_ctx->szConfigFile = filename;
3569
3570         if (lp_ctx->s3_fns) {
3571                 return lp_ctx->s3_fns->load(filename);
3572         }
3573
3574         lp_ctx->bInGlobalSection = true;
3575         n2 = standard_sub_basic(lp_ctx, lp_ctx->szConfigFile);
3576         DEBUG(2, ("lpcfg_load: refreshing parameters from %s\n", n2));
3577
3578         add_to_file_list(lp_ctx, lp_ctx->szConfigFile, n2);
3579
3580         /* We get sections first, so have to start 'behind' to make up */
3581         lp_ctx->currentService = NULL;
3582         bRetval = pm_process(n2, do_section, do_parameter, lp_ctx);
3583
3584         /* finish up the last section */
3585         DEBUG(4, ("pm_process() returned %s\n", BOOLSTR(bRetval)));
3586         if (bRetval)
3587                 if (lp_ctx->currentService != NULL)
3588                         bRetval = lpcfg_service_ok(lp_ctx->currentService);
3589
3590         bRetval = bRetval && lpcfg_update(lp_ctx);
3591
3592         /* we do this unconditionally, so that it happens even
3593            for a missing smb.conf */
3594         reload_charcnv(lp_ctx);
3595
3596         if (bRetval == true) {
3597                 /* set this up so that any child python tasks will
3598                    find the right smb.conf */
3599                 setenv("SMB_CONF_PATH", filename, 1);
3600
3601                 /* set the context used by the lp_*() function
3602                    varients */
3603                 global_loadparm_context = lp_ctx;
3604                 lp_ctx->loaded = true;
3605         }
3606
3607         return bRetval;
3608 }
3609
3610 /**
3611  * Return the max number of services.
3612  */
3613
3614 int lpcfg_numservices(struct loadparm_context *lp_ctx)
3615 {
3616         if (lp_ctx->s3_fns) {
3617                 return lp_ctx->s3_fns->get_numservices();
3618         }
3619
3620         return lp_ctx->iNumServices;
3621 }
3622
3623 /**
3624  * Display the contents of the services array in human-readable form.
3625  */
3626
3627 void lpcfg_dump(struct loadparm_context *lp_ctx, FILE *f, bool show_defaults,
3628              int maxtoprint)
3629 {
3630         int iService;
3631
3632         if (lp_ctx->s3_fns) {
3633                 lp_ctx->s3_fns->dump(f, show_defaults, maxtoprint);
3634                 return;
3635         }
3636
3637         defaults_saved = !show_defaults;
3638
3639         dump_globals(lp_ctx, f, show_defaults);
3640
3641         dump_a_service(lp_ctx->sDefault, lp_ctx->sDefault, f, lp_ctx->flags);
3642
3643         for (iService = 0; iService < maxtoprint; iService++)
3644                 lpcfg_dump_one(f, show_defaults, lp_ctx->services[iService], lp_ctx->sDefault);
3645 }
3646
3647 /**
3648  * Display the contents of one service in human-readable form.
3649  */
3650 void lpcfg_dump_one(FILE *f, bool show_defaults, struct loadparm_service *service, struct loadparm_service *sDefault)
3651 {
3652         if (service != NULL) {
3653                 if (service->szService[0] == '\0')
3654                         return;
3655                 dump_a_service(service, sDefault, f, NULL);
3656         }
3657 }
3658
3659 struct loadparm_service *lpcfg_servicebynum(struct loadparm_context *lp_ctx,
3660                                             int snum)
3661 {
3662         if (lp_ctx->s3_fns) {
3663                 return lp_ctx->s3_fns->get_servicebynum(snum);
3664         }
3665
3666         return lp_ctx->services[snum];
3667 }
3668
3669 struct loadparm_service *lpcfg_service(struct loadparm_context *lp_ctx,
3670                                     const char *service_name)
3671 {
3672         int iService;
3673         char *serviceName;
3674
3675         if (lp_ctx->s3_fns) {
3676                 return lp_ctx->s3_fns->get_service(service_name);
3677         }
3678
3679         for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--) {
3680                 if (lp_ctx->services[iService] &&
3681                     lp_ctx->services[iService]->szService) {
3682                         /*
3683                          * The substitution here is used to support %U is
3684                          * service names
3685                          */
3686                         serviceName = standard_sub_basic(
3687                                         lp_ctx->services[iService],
3688                                         lp_ctx->services[iService]->szService);
3689                         if (strequal(serviceName, service_name)) {
3690                                 talloc_free(serviceName);
3691                                 return lp_ctx->services[iService];
3692                         }
3693                         talloc_free(serviceName);
3694                 }
3695         }
3696
3697         DEBUG(7,("lpcfg_servicenumber: couldn't find %s\n", service_name));
3698         return NULL;
3699 }
3700
3701 const char *lpcfg_servicename(const struct loadparm_service *service)
3702 {
3703         return lp_string((const char *)service->szService);
3704 }
3705
3706 /**
3707  * A useful volume label function.
3708  */
3709 const char *lpcfg_volume_label(struct loadparm_service *service, struct loadparm_service *sDefault)
3710 {
3711         const char *ret;
3712         ret = lp_string((const char *)((service != NULL && service->volume != NULL) ?
3713                                        service->volume : sDefault->volume));
3714         if (!*ret)
3715                 return lpcfg_servicename(service);
3716         return ret;
3717 }
3718
3719 /**
3720  * If we are PDC then prefer us as DMB
3721  */
3722 const char *lpcfg_printername(struct loadparm_service *service, struct loadparm_service *sDefault)
3723 {
3724         const char *ret;
3725         ret = lp_string((const char *)((service != NULL && service->szPrintername != NULL) ?
3726                                        service->szPrintername : sDefault->szPrintername));
3727         if (ret == NULL || (ret != NULL && *ret == '\0'))
3728                 ret = lpcfg_servicename(service);
3729
3730         return ret;
3731 }
3732
3733
3734 /**
3735  * Return the max print jobs per queue.
3736  */
3737 int lpcfg_maxprintjobs(struct loadparm_service *service, struct loadparm_service *sDefault)
3738 {
3739         int maxjobs = (service != NULL) ? service->iMaxPrintJobs : sDefault->iMaxPrintJobs;
3740         if (maxjobs <= 0 || maxjobs >= PRINT_MAX_JOBID)
3741                 maxjobs = PRINT_MAX_JOBID - 1;
3742
3743         return maxjobs;
3744 }
3745
3746 struct smb_iconv_handle *lpcfg_iconv_handle(struct loadparm_context *lp_ctx)
3747 {
3748         if (lp_ctx == NULL) {
3749                 return get_iconv_handle();
3750         }
3751         return lp_ctx->iconv_handle;
3752 }
3753
3754 _PUBLIC_ void reload_charcnv(struct loadparm_context *lp_ctx)
3755 {
3756         struct smb_iconv_handle *old_ic = lp_ctx->iconv_handle;
3757         if (!lp_ctx->global) {
3758                 return;
3759         }
3760
3761         if (old_ic == NULL) {
3762                 old_ic = global_iconv_handle;
3763         }
3764         lp_ctx->iconv_handle = smb_iconv_handle_reinit_lp(lp_ctx, lp_ctx, old_ic);
3765         global_iconv_handle = lp_ctx->iconv_handle;
3766 }
3767
3768 _PUBLIC_ char *lpcfg_tls_keyfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3769 {
3770         return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_keyfile);
3771 }
3772
3773 _PUBLIC_ char *lpcfg_tls_certfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3774 {
3775         return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_certfile);
3776 }
3777
3778 _PUBLIC_ char *lpcfg_tls_cafile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3779 {
3780         return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_cafile);
3781 }
3782
3783 _PUBLIC_ char *lpcfg_tls_crlfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3784 {
3785         return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_crlfile);
3786 }
3787
3788 _PUBLIC_ char *lpcfg_tls_dhpfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3789 {
3790         return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_dhpfile);
3791 }
3792
3793 struct gensec_settings *lpcfg_gensec_settings(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3794 {
3795         struct gensec_settings *settings = talloc(mem_ctx, struct gensec_settings);
3796         if (settings == NULL)
3797                 return NULL;
3798         SMB_ASSERT(lp_ctx != NULL);
3799         settings->lp_ctx = talloc_reference(settings, lp_ctx);
3800         settings->target_hostname = lpcfg_parm_string(lp_ctx, NULL, "gensec", "target_hostname");
3801         return settings;
3802 }
3803