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