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