param: handle P_BYTES in more places
[mat/samba.git] / lib / param / loadparm.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Parameter loading functions
4    Copyright (C) Karl Auer 1993-1998
5
6    Largely re-written by Andrew Tridgell, September 1994
7
8    Copyright (C) Simo Sorce 2001
9    Copyright (C) Alexander Bokovoy 2002
10    Copyright (C) Stefan (metze) Metzmacher 2002
11    Copyright (C) Jim McDonough (jmcd@us.ibm.com)  2003.
12    Copyright (C) James Myers 2003 <myersjj@samba.org>
13    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
14
15    This program is free software; you can redistribute it and/or modify
16    it under the terms of the GNU General Public License as published by
17    the Free Software Foundation; either version 3 of the License, or
18    (at your option) any later version.
19
20    This program is distributed in the hope that it will be useful,
21    but WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23    GNU General Public License for more details.
24
25    You should have received a copy of the GNU General Public License
26    along with this program.  If not, see <http://www.gnu.org/licenses/>.
27 */
28
29 /*
30  *  Load parameters.
31  *
32  *  This module provides suitable callback functions for the params
33  *  module. It builds the internal table of service details which is
34  *  then used by the rest of the server.
35  *
36  * To add a parameter:
37  *
38  * 1) add it to the global or service structure definition
39  * 2) add it to the parm_table
40  * 3) add it to the list of available functions (eg: using FN_GLOBAL_STRING())
41  * 4) If it's a global then initialise it in init_globals. If a local
42  *    (ie. service) parameter then initialise it in the sDefault structure
43  *
44  *
45  * Notes:
46  *   The configuration file is processed sequentially for speed. It is NOT
47  *   accessed randomly as happens in 'real' Windows. For this reason, there
48  *   is a fair bit of sequence-dependent code here - ie., code which assumes
49  *   that certain things happen before others. In particular, the code which
50  *   happens at the boundary between sections is delicately poised, so be
51  *   careful!
52  *
53  */
54
55 #include "includes.h"
56 #include "version.h"
57 #include "dynconfig/dynconfig.h"
58 #include "system/time.h"
59 #include "system/locale.h"
60 #include "system/network.h" /* needed for TCP_NODELAY */
61 #include "../lib/util/dlinklist.h"
62 #include "lib/param/param.h"
63 #include "lib/param/loadparm.h"
64 #include "auth/gensec/gensec.h"
65 #include "s3_param.h"
66 #include "lib/util/bitmap.h"
67 #include "libcli/smb/smb_constants.h"
68 #include "source4/dns_server/dns_update.h"
69
70 #define standard_sub_basic talloc_strdup
71
72 static bool do_parameter(const char *, const char *, void *);
73 static bool defaults_saved = false;
74
75 #define LOADPARM_EXTRA_GLOBALS \
76         struct parmlist_entry *param_opt;                               \
77         char *szRealm;                                                  \
78         char *tls_keyfile;                                              \
79         char *tls_certfile;                                             \
80         char *tls_cafile;                                               \
81         char *tls_crlfile;                                              \
82         char *tls_dhpfile;                                              \
83         char *loglevel;                                                 \
84         char *panic_action;                                             \
85         int server_role;                                                \
86         int security;                                                   \
87         int domain_master;                                              \
88         int domain_logons;                                              \
89         int bPreferredMaster;
90
91 #include "param_global.h"
92
93 #define NUMPARAMETERS (sizeof(parm_table) / sizeof(struct parm_struct))
94
95
96 /* prototypes for the special type handlers */
97 static bool handle_include(struct loadparm_context *lp_ctx, int unused,
98                            const char *pszParmValue, char **ptr);
99 static bool handle_realm(struct loadparm_context *lp_ctx, int unused,
100                          const char *pszParmValue, char **ptr);
101 static bool handle_copy(struct loadparm_context *lp_ctx, int unused,
102                         const char *pszParmValue, char **ptr);
103 static bool handle_debuglevel(struct loadparm_context *lp_ctx, int unused,
104                               const char *pszParmValue, char **ptr);
105 static bool handle_logfile(struct loadparm_context *lp_ctx, int unused,
106                            const char *pszParmValue, char **ptr);
107
108 #include "param_enums.c"
109
110 #define GLOBAL_VAR(name) offsetof(struct loadparm_global, name)
111 #define LOCAL_VAR(name) offsetof(struct loadparm_service, name)
112
113 static struct parm_struct parm_table[] = {
114         {
115                 .label          = "server role",
116                 .type           = P_ENUM,
117                 .p_class        = P_GLOBAL,
118                 .offset         = GLOBAL_VAR(server_role),
119                 .special        = NULL,
120                 .enum_list      = enum_server_role
121         },
122         {
123                 .label          = "domain logons",
124                 .type           = P_ENUM,
125                 .p_class        = P_GLOBAL,
126                 .offset         = GLOBAL_VAR(domain_logons),
127                 .special        = NULL,
128                 .enum_list      = enum_bool_auto
129         },
130         {
131                 .label          = "domain master",
132                 .type           = P_ENUM,
133                 .p_class        = P_GLOBAL,
134                 .offset         = GLOBAL_VAR(domain_master),
135                 .special        = NULL,
136                 .enum_list      = enum_bool_auto
137         },
138         {
139                 .label          = "dos charset",
140                 .type           = P_STRING,
141                 .p_class        = P_GLOBAL,
142                 .offset         = GLOBAL_VAR(dos_charset),
143                 .special        = NULL,
144                 .enum_list      = NULL
145         },
146         {
147                 .label          = "unix charset",
148                 .type           = P_STRING,
149                 .p_class        = P_GLOBAL,
150                 .offset         = GLOBAL_VAR(unix_charset),
151                 .special        = NULL,
152                 .enum_list      = NULL
153         },
154         {
155                 .label          = "ncalrpc dir",
156                 .type           = P_STRING,
157                 .p_class        = P_GLOBAL,
158                 .offset         = GLOBAL_VAR(ncalrpc_dir),
159                 .special        = NULL,
160                 .enum_list      = NULL
161         },
162         {
163                 .label          = "comment",
164                 .type           = P_STRING,
165                 .p_class        = P_LOCAL,
166                 .offset         = LOCAL_VAR(comment),
167                 .special        = NULL,
168                 .enum_list      = NULL
169         },
170         {
171                 .label          = "path",
172                 .type           = P_STRING,
173                 .p_class        = P_LOCAL,
174                 .offset         = LOCAL_VAR(szPath),
175                 .special        = NULL,
176                 .enum_list      = NULL
177         },
178         {
179                 .label          = "directory",
180                 .type           = P_STRING,
181                 .p_class        = P_LOCAL,
182                 .offset         = LOCAL_VAR(szPath),
183                 .special        = NULL,
184                 .enum_list      = NULL
185         },
186         {
187                 .label          = "workgroup",
188                 .type           = P_USTRING,
189                 .p_class        = P_GLOBAL,
190                 .offset         = GLOBAL_VAR(szWorkgroup),
191                 .special        = NULL,
192                 .enum_list      = NULL
193         },
194         {
195                 .label          = "realm",
196                 .type           = P_STRING,
197                 .p_class        = P_GLOBAL,
198                 .offset         = GLOBAL_VAR(szRealm),
199                 .special        = handle_realm,
200                 .enum_list      = NULL
201         },
202         {
203                 .label          = "netbios name",
204                 .type           = P_USTRING,
205                 .p_class        = P_GLOBAL,
206                 .offset         = GLOBAL_VAR(szNetbiosName),
207                 .special        = NULL,
208                 .enum_list      = NULL
209         },
210         {
211                 .label          = "netbios aliases",
212                 .type           = P_LIST,
213                 .p_class        = P_GLOBAL,
214                 .offset         = GLOBAL_VAR(szNetbiosAliases),
215                 .special        = NULL,
216                 .enum_list      = NULL
217         },
218         {
219                 .label          = "netbios scope",
220                 .type           = P_USTRING,
221                 .p_class        = P_GLOBAL,
222                 .offset         = GLOBAL_VAR(szNetbiosScope),
223                 .special        = NULL,
224                 .enum_list      = NULL
225         },
226         {
227                 .label          = "server string",
228                 .type           = P_STRING,
229                 .p_class        = P_GLOBAL,
230                 .offset         = GLOBAL_VAR(szServerString),
231                 .special        = NULL,
232                 .enum_list      = NULL
233         },
234         {
235                 .label          = "interfaces",
236                 .type           = P_LIST,
237                 .p_class        = P_GLOBAL,
238                 .offset         = GLOBAL_VAR(szInterfaces),
239                 .special        = NULL,
240                 .enum_list      = NULL
241         },
242         {
243                 .label          = "bind interfaces only",
244                 .type           = P_BOOL,
245                 .p_class        = P_GLOBAL,
246                 .offset         = GLOBAL_VAR(bBindInterfacesOnly),
247                 .special        = NULL,
248                 .enum_list      = NULL
249         },
250         {
251                 .label          = "ntvfs handler",
252                 .type           = P_LIST,
253                 .p_class        = P_LOCAL,
254                 .offset         = LOCAL_VAR(ntvfs_handler),
255                 .special        = NULL,
256                 .enum_list      = NULL
257         },
258         {
259                 .label          = "ntptr providor",
260                 .type           = P_STRING,
261                 .p_class        = P_GLOBAL,
262                 .offset         = GLOBAL_VAR(ntptr_providor),
263                 .special        = NULL,
264                 .enum_list      = NULL
265         },
266         {
267                 .label          = "passdb backend",
268                 .type           = P_STRING,
269                 .p_class        = P_GLOBAL,
270                 .offset         = GLOBAL_VAR(passdb_backend),
271                 .special        = NULL,
272                 .enum_list      = NULL
273         },
274         {
275                 .label          = "dcerpc endpoint servers",
276                 .type           = P_LIST,
277                 .p_class        = P_GLOBAL,
278                 .offset         = GLOBAL_VAR(dcerpc_ep_servers),
279                 .special        = NULL,
280                 .enum_list      = NULL
281         },
282         {
283                 .label          = "server services",
284                 .type           = P_LIST,
285                 .p_class        = P_GLOBAL,
286                 .offset         = GLOBAL_VAR(server_services),
287                 .special        = NULL,
288                 .enum_list      = NULL
289         },
290
291         {
292                 .label          = "security",
293                 .type           = P_ENUM,
294                 .p_class        = P_GLOBAL,
295                 .offset         = GLOBAL_VAR(security),
296                 .special        = NULL,
297                 .enum_list      = enum_security
298         },
299         {
300                 .label          = "encrypt passwords",
301                 .type           = P_BOOL,
302                 .p_class        = P_GLOBAL,
303                 .offset         = GLOBAL_VAR(bEncryptPasswords),
304                 .special        = NULL,
305                 .enum_list      = NULL
306         },
307         {
308                 .label          = "null passwords",
309                 .type           = P_BOOL,
310                 .p_class        = P_GLOBAL,
311                 .offset         = GLOBAL_VAR(bNullPasswords),
312                 .special        = NULL,
313                 .enum_list      = NULL
314         },
315         {
316                 .label          = "obey pam restrictions",
317                 .type           = P_BOOL,
318                 .p_class        = P_GLOBAL,
319                 .offset         = GLOBAL_VAR(bObeyPamRestrictions),
320                 .special        = NULL,
321                 .enum_list      = NULL
322         },
323         {
324                 .label          = "password server",
325                 .type           = P_LIST,
326                 .p_class        = P_GLOBAL,
327                 .offset         = GLOBAL_VAR(szPasswordServers),
328                 .special        = NULL,
329                 .enum_list      = NULL
330         },
331         {
332                 .label          = "private dir",
333                 .type           = P_STRING,
334                 .p_class        = P_GLOBAL,
335                 .offset         = GLOBAL_VAR(szPrivateDir),
336                 .special        = NULL,
337                 .enum_list      = NULL
338         },
339         {
340                 .label          = "passwd chat",
341                 .type           = P_STRING,
342                 .p_class        = P_GLOBAL,
343                 .offset         = GLOBAL_VAR(szPasswdChat),
344                 .special        = NULL,
345                 .enum_list      = NULL
346         },
347         {
348                 .label          = "password level",
349                 .type           = P_INTEGER,
350                 .p_class        = P_GLOBAL,
351                 .offset         = GLOBAL_VAR(pwordlevel),
352                 .special        = NULL,
353                 .enum_list      = NULL
354         },
355         {
356                 .label          = "lanman auth",
357                 .type           = P_BOOL,
358                 .p_class        = P_GLOBAL,
359                 .offset         = GLOBAL_VAR(bLanmanAuth),
360                 .special        = NULL,
361                 .enum_list      = NULL
362         },
363         {
364                 .label          = "ntlm auth",
365                 .type           = P_BOOL,
366                 .p_class        = P_GLOBAL,
367                 .offset         = GLOBAL_VAR(bNTLMAuth),
368                 .special        = NULL,
369                 .enum_list      = NULL
370         },
371         {
372                 .label          = "client NTLMv2 auth",
373                 .type           = P_BOOL,
374                 .p_class        = P_GLOBAL,
375                 .offset         = GLOBAL_VAR(bClientNTLMv2Auth),
376                 .special        = NULL,
377                 .enum_list      = NULL
378         },
379         {
380                 .label          = "client lanman auth",
381                 .type           = P_BOOL,
382                 .p_class        = P_GLOBAL,
383                 .offset         = GLOBAL_VAR(bClientLanManAuth),
384                 .special        = NULL,
385                 .enum_list      = NULL
386         },
387         {
388                 .label          = "client plaintext auth",
389                 .type           = P_BOOL,
390                 .p_class        = P_GLOBAL,
391                 .offset         = GLOBAL_VAR(bClientPlaintextAuth),
392                 .special        = NULL,
393                 .enum_list      = NULL
394         },
395         {
396                 .label          = "client use spnego principal",
397                 .type           = P_BOOL,
398                 .p_class        = P_GLOBAL,
399                 .offset         = GLOBAL_VAR(client_use_spnego_principal),
400                 .special        = NULL,
401                 .enum_list      = NULL
402         },
403
404         {
405                 .label          = "read only",
406                 .type           = P_BOOL,
407                 .p_class        = P_LOCAL,
408                 .offset         = LOCAL_VAR(bRead_only),
409                 .special        = NULL,
410                 .enum_list      = NULL
411         },
412
413         {
414                 .label          = "create mask",
415                 .type           = P_OCTAL,
416                 .p_class        = P_LOCAL,
417                 .offset         = LOCAL_VAR(iCreate_mask),
418                 .special        = NULL,
419                 .enum_list      = NULL
420         },
421         {
422                 .label          = "force create mode",
423                 .type           = P_OCTAL,
424                 .p_class        = P_LOCAL,
425                 .offset         = LOCAL_VAR(iCreate_force_mode),
426                 .special        = NULL,
427                 .enum_list      = NULL
428         },
429         {
430                 .label          = "directory mask",
431                 .type           = P_OCTAL,
432                 .p_class        = P_LOCAL,
433                 .offset         = LOCAL_VAR(iDir_mask),
434                 .special        = NULL,
435                 .enum_list      = NULL
436         },
437         {
438                 .label          = "force directory mode",
439                 .type           = P_OCTAL,
440                 .p_class        = P_LOCAL,
441                 .offset         = LOCAL_VAR(iDir_force_mode),
442                 .special        = NULL,
443                 .enum_list      = NULL
444         },
445
446         {
447                 .label          = "hosts allow",
448                 .type           = P_LIST,
449                 .p_class        = P_LOCAL,
450                 .offset         = LOCAL_VAR(szHostsallow),
451                 .special        = NULL,
452                 .enum_list      = NULL
453         },
454         {
455                 .label          = "hosts deny",
456                 .type           = P_LIST,
457                 .p_class        = P_LOCAL,
458                 .offset         = LOCAL_VAR(szHostsdeny),
459                 .special        = NULL,
460                 .enum_list      = NULL
461         },
462
463         {
464                 .label          = "log level",
465                 .type           = P_STRING,
466                 .p_class        = P_GLOBAL,
467                 .offset         = GLOBAL_VAR(loglevel),
468                 .special        = handle_debuglevel,
469                 .enum_list      = NULL
470         },
471         {
472                 .label          = "debuglevel",
473                 .type           = P_STRING,
474                 .p_class        = P_GLOBAL,
475                 .offset         = GLOBAL_VAR(loglevel),
476                 .special        = handle_debuglevel,
477                 .enum_list      = NULL
478         },
479         {
480                 .label          = "log file",
481                 .type           = P_STRING,
482                 .p_class        = P_GLOBAL,
483                 .offset         = GLOBAL_VAR(logfile),
484                 .special        = handle_logfile,
485                 .enum_list      = NULL
486         },
487
488         {
489                 .label          = "smb ports",
490                 .type           = P_LIST,
491                 .p_class        = P_GLOBAL,
492                 .offset         = GLOBAL_VAR(smb_ports),
493                 .special        = NULL,
494                 .enum_list      = NULL
495         },
496         {
497                 .label          = "nbt port",
498                 .type           = P_INTEGER,
499                 .p_class        = P_GLOBAL,
500                 .offset         = GLOBAL_VAR(nbt_port),
501                 .special        = NULL,
502                 .enum_list      = NULL
503         },
504         {
505                 .label          = "dgram port",
506                 .type           = P_INTEGER,
507                 .p_class        = P_GLOBAL,
508                 .offset         = GLOBAL_VAR(dgram_port),
509                 .special        = NULL,
510                 .enum_list      = NULL
511         },
512         {
513                 .label          = "cldap port",
514                 .type           = P_INTEGER,
515                 .p_class        = P_GLOBAL,
516                 .offset         = GLOBAL_VAR(cldap_port),
517                 .special        = NULL,
518                 .enum_list      = NULL
519         },
520         {
521                 .label          = "krb5 port",
522                 .type           = P_INTEGER,
523                 .p_class        = P_GLOBAL,
524                 .offset         = GLOBAL_VAR(krb5_port),
525                 .special        = NULL,
526                 .enum_list      = NULL
527         },
528         {
529                 .label          = "kpasswd port",
530                 .type           = P_INTEGER,
531                 .p_class        = P_GLOBAL,
532                 .offset         = GLOBAL_VAR(kpasswd_port),
533                 .special        = NULL,
534                 .enum_list      = NULL
535         },
536         {
537                 .label          = "web port",
538                 .type           = P_INTEGER,
539                 .p_class        = P_GLOBAL,
540                 .offset         = GLOBAL_VAR(web_port),
541                 .special        = NULL,
542                 .enum_list      = NULL
543         },
544         {
545                 .label          = "tls enabled",
546                 .type           = P_BOOL,
547                 .p_class        = P_GLOBAL,
548                 .offset         = GLOBAL_VAR(tls_enabled),
549                 .special        = NULL,
550                 .enum_list      = NULL
551         },
552         {
553                 .label          = "tls keyfile",
554                 .type           = P_STRING,
555                 .p_class        = P_GLOBAL,
556                 .offset         = GLOBAL_VAR(tls_keyfile),
557                 .special        = NULL,
558                 .enum_list      = NULL
559         },
560         {
561                 .label          = "tls certfile",
562                 .type           = P_STRING,
563                 .p_class        = P_GLOBAL,
564                 .offset         = GLOBAL_VAR(tls_certfile),
565                 .special        = NULL,
566                 .enum_list      = NULL
567         },
568         {
569                 .label          = "tls cafile",
570                 .type           = P_STRING,
571                 .p_class        = P_GLOBAL,
572                 .offset         = GLOBAL_VAR(tls_cafile),
573                 .special        = NULL,
574                 .enum_list      = NULL
575         },
576         {
577                 .label          = "tls crlfile",
578                 .type           = P_STRING,
579                 .p_class        = P_GLOBAL,
580                 .offset         = GLOBAL_VAR(tls_crlfile),
581                 .special        = NULL,
582                 .enum_list      = NULL
583         },
584         {
585                 .label          = "tls dh params file",
586                 .type           = P_STRING,
587                 .p_class        = P_GLOBAL,
588                 .offset         = GLOBAL_VAR(tls_dhpfile),
589                 .special        = NULL,
590                 .enum_list      = NULL
591         },
592         {
593                 .label          = "large readwrite",
594                 .type           = P_BOOL,
595                 .p_class        = P_GLOBAL,
596                 .offset         = GLOBAL_VAR(bLargeReadwrite),
597                 .special        = NULL,
598                 .enum_list      = NULL
599         },
600         {
601                 .label          = "server max protocol",
602                 .type           = P_ENUM,
603                 .p_class        = P_GLOBAL,
604                 .offset         = GLOBAL_VAR(srv_maxprotocol),
605                 .special        = NULL,
606                 .enum_list      = enum_protocol
607         },
608         {
609                 .label          = "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_BYTES:
2103                                 case P_OCTAL:
2104                                 case P_ENUM:
2105                                         *(int *)dest_ptr = *(int *)src_ptr;
2106                                         break;
2107
2108                                 case P_STRING:
2109                                         lpcfg_string_set(pserviceDest,
2110                                                    (char **)dest_ptr,
2111                                                    *(char **)src_ptr);
2112                                         break;
2113
2114                                 case P_USTRING:
2115                                         lpcfg_string_set_upper(pserviceDest,
2116                                                          (char **)dest_ptr,
2117                                                          *(char **)src_ptr);
2118                                         break;
2119                                 case P_LIST:
2120                                         *(const char ***)dest_ptr = (const char **)str_list_copy(pserviceDest, 
2121                                                                                   *(const char ***)src_ptr);
2122                                         break;
2123                                 default:
2124                                         break;
2125                         }
2126                 }
2127
2128         if (bcopyall) {
2129                 init_copymap(pserviceDest);
2130                 if (pserviceSource->copymap)
2131                         bitmap_copy(pserviceDest->copymap,
2132                                     pserviceSource->copymap);
2133         }
2134
2135         data = pserviceSource->param_opt;
2136         while (data) {
2137                 not_added = true;
2138                 pdata = pserviceDest->param_opt;
2139                 /* Traverse destination */
2140                 while (pdata) {
2141                         /* If we already have same option, override it */
2142                         if (strcmp(pdata->key, data->key) == 0) {
2143                                 talloc_free(pdata->value);
2144                                 pdata->value = talloc_strdup(pdata,
2145                                                              data->value);
2146                                 not_added = false;
2147                                 break;
2148                         }
2149                         pdata = pdata->next;
2150                 }
2151                 if (not_added) {
2152                         paramo = talloc_zero(pserviceDest, struct parmlist_entry);
2153                         if (paramo == NULL)
2154                                 smb_panic("OOM");
2155                         paramo->key = talloc_strdup(paramo, data->key);
2156                         paramo->value = talloc_strdup(paramo, data->value);
2157                         DLIST_ADD(pserviceDest->param_opt, paramo);
2158                 }
2159                 data = data->next;
2160         }
2161 }
2162
2163 /**
2164  * Check a service for consistency. Return False if the service is in any way
2165  * incomplete or faulty, else True.
2166  */
2167 static bool lpcfg_service_ok(struct loadparm_service *service)
2168 {
2169         bool bRetval;
2170
2171         bRetval = true;
2172         if (service->szService[0] == '\0') {
2173                 DEBUG(0, ("The following message indicates an internal error:\n"));
2174                 DEBUG(0, ("No service name in service entry.\n"));
2175                 bRetval = false;
2176         }
2177
2178         /* The [printers] entry MUST be printable. I'm all for flexibility, but */
2179         /* I can't see why you'd want a non-printable printer service...        */
2180         if (strwicmp(service->szService, PRINTERS_NAME) == 0) {
2181                 if (!service->bPrint_ok) {
2182                         DEBUG(0, ("WARNING: [%s] service MUST be printable!\n",
2183                                service->szService));
2184                         service->bPrint_ok = true;
2185                 }
2186                 /* [printers] service must also be non-browsable. */
2187                 if (service->bBrowseable)
2188                         service->bBrowseable = false;
2189         }
2190
2191         /* If a service is flagged unavailable, log the fact at level 0. */
2192         if (!service->bAvailable)
2193                 DEBUG(1, ("NOTE: Service %s is flagged unavailable.\n",
2194                           service->szService));
2195
2196         return bRetval;
2197 }
2198
2199
2200 /*******************************************************************
2201  Keep a linked list of all config files so we know when one has changed
2202  it's date and needs to be reloaded.
2203 ********************************************************************/
2204
2205 static void add_to_file_list(struct loadparm_context *lp_ctx,
2206                              const char *fname, const char *subfname)
2207 {
2208         struct file_lists *f = lp_ctx->file_lists;
2209
2210         while (f) {
2211                 if (f->name && !strcmp(f->name, fname))
2212                         break;
2213                 f = f->next;
2214         }
2215
2216         if (!f) {
2217                 f = talloc(lp_ctx, struct file_lists);
2218                 if (!f)
2219                         return;
2220                 f->next = lp_ctx->file_lists;
2221                 f->name = talloc_strdup(f, fname);
2222                 if (!f->name) {
2223                         talloc_free(f);
2224                         return;
2225                 }
2226                 f->subfname = talloc_strdup(f, subfname);
2227                 if (!f->subfname) {
2228                         talloc_free(f);
2229                         return;
2230                 }
2231                 lp_ctx->file_lists = f;
2232                 f->modtime = file_modtime(subfname);
2233         } else {
2234                 time_t t = file_modtime(subfname);
2235                 if (t)
2236                         f->modtime = t;
2237         }
2238 }
2239
2240 /*******************************************************************
2241  Check if a config file has changed date.
2242 ********************************************************************/
2243 bool lpcfg_file_list_changed(struct loadparm_context *lp_ctx)
2244 {
2245         struct file_lists *f;
2246         DEBUG(6, ("lp_file_list_changed()\n"));
2247
2248         for (f = lp_ctx->file_lists; f != NULL; f = f->next) {
2249                 char *n2;
2250                 time_t mod_time;
2251
2252                 n2 = standard_sub_basic(lp_ctx, f->name);
2253
2254                 DEBUGADD(6, ("file %s -> %s  last mod_time: %s\n",
2255                              f->name, n2, ctime(&f->modtime)));
2256
2257                 mod_time = file_modtime(n2);
2258
2259                 if (mod_time && ((f->modtime != mod_time) || (f->subfname == NULL) || (strcmp(n2, f->subfname) != 0))) {
2260                         DEBUGADD(6, ("file %s modified: %s\n", n2,
2261                                   ctime(&mod_time)));
2262                         f->modtime = mod_time;
2263                         talloc_free(f->subfname);
2264                         f->subfname = talloc_strdup(f, n2);
2265                         return true;
2266                 }
2267         }
2268         return false;
2269 }
2270
2271 /***************************************************************************
2272  Handle the "realm" parameter
2273 ***************************************************************************/
2274
2275 static bool handle_realm(struct loadparm_context *lp_ctx, int unused,
2276                          const char *pszParmValue, char **ptr)
2277 {
2278         lpcfg_string_set(lp_ctx, ptr, pszParmValue);
2279
2280         talloc_free(lp_ctx->globals->szRealm_upper);
2281         talloc_free(lp_ctx->globals->szRealm_lower);
2282
2283         lp_ctx->globals->szRealm_upper = strupper_talloc(lp_ctx, pszParmValue);
2284         lp_ctx->globals->szRealm_lower = strlower_talloc(lp_ctx, pszParmValue);
2285
2286         return true;
2287 }
2288
2289 /***************************************************************************
2290  Handle the include operation.
2291 ***************************************************************************/
2292
2293 static bool handle_include(struct loadparm_context *lp_ctx, int unused,
2294                            const char *pszParmValue, char **ptr)
2295 {
2296         char *fname = standard_sub_basic(lp_ctx, pszParmValue);
2297
2298         add_to_file_list(lp_ctx, pszParmValue, fname);
2299
2300         lpcfg_string_set(lp_ctx, ptr, fname);
2301
2302         if (file_exist(fname))
2303                 return pm_process(fname, do_section, do_parameter, lp_ctx);
2304
2305         DEBUG(2, ("Can't find include file %s\n", fname));
2306
2307         return false;
2308 }
2309
2310 /***************************************************************************
2311  Handle the interpretation of the copy parameter.
2312 ***************************************************************************/
2313
2314 static bool handle_copy(struct loadparm_context *lp_ctx, int unused,
2315                         const char *pszParmValue, char **ptr)
2316 {
2317         bool bRetval;
2318         struct loadparm_service *serviceTemp;
2319
2320         lpcfg_string_set(lp_ctx, ptr, pszParmValue);
2321
2322         bRetval = false;
2323
2324         DEBUG(3, ("Copying service from service %s\n", pszParmValue));
2325
2326         if ((serviceTemp = getservicebyname(lp_ctx, pszParmValue)) != NULL) {
2327                 if (serviceTemp == lp_ctx->currentService) {
2328                         DEBUG(0, ("Can't copy service %s - unable to copy self!\n", pszParmValue));
2329                 } else {
2330                         copy_service(lp_ctx->currentService,
2331                                      serviceTemp,
2332                                      lp_ctx->currentService->copymap);
2333                         bRetval = true;
2334                 }
2335         } else {
2336                 DEBUG(0, ("Unable to copy service - source not found: %s\n",
2337                           pszParmValue));
2338                 bRetval = false;
2339         }
2340
2341         return bRetval;
2342 }
2343
2344 static bool handle_debuglevel(struct loadparm_context *lp_ctx, int unused,
2345                         const char *pszParmValue, char **ptr)
2346 {
2347
2348         lpcfg_string_set(lp_ctx, ptr, pszParmValue);
2349         if (lp_ctx->global) {
2350                 return debug_parse_levels(pszParmValue);
2351         }
2352         return true;
2353 }
2354
2355 static bool handle_logfile(struct loadparm_context *lp_ctx, int unused,
2356                         const char *pszParmValue, char **ptr)
2357 {
2358         debug_set_logfile(pszParmValue);
2359         if (lp_ctx->global) {
2360                 lpcfg_string_set(lp_ctx, ptr, pszParmValue);
2361         }
2362         return true;
2363 }
2364
2365 /***************************************************************************
2366  Initialise a copymap.
2367 ***************************************************************************/
2368
2369 static void init_copymap(struct loadparm_service *pservice)
2370 {
2371         int i;
2372
2373         TALLOC_FREE(pservice->copymap);
2374
2375         pservice->copymap = bitmap_talloc(NULL, NUMPARAMETERS);
2376         if (!pservice->copymap)
2377                 DEBUG(0,
2378                       ("Couldn't allocate copymap!! (size %d)\n",
2379                        (int)NUMPARAMETERS));
2380         else
2381                 for (i = 0; i < NUMPARAMETERS; i++)
2382                         bitmap_set(pservice->copymap, i);
2383 }
2384
2385 /**
2386  * Process a parametric option
2387  */
2388 static bool lp_do_parameter_parametric(struct loadparm_context *lp_ctx,
2389                                        struct loadparm_service *service,
2390                                        const char *pszParmName,
2391                                        const char *pszParmValue, int flags)
2392 {
2393         struct parmlist_entry *paramo, *data;
2394         char *name;
2395         TALLOC_CTX *mem_ctx;
2396
2397         while (isspace((unsigned char)*pszParmName)) {
2398                 pszParmName++;
2399         }
2400
2401         name = strlower_talloc(lp_ctx, pszParmName);
2402         if (!name) return false;
2403
2404         if (service == NULL) {
2405                 data = lp_ctx->globals->param_opt;
2406                 mem_ctx = lp_ctx->globals;
2407         } else {
2408                 data = service->param_opt;
2409                 mem_ctx = service;
2410         }
2411
2412         /* Traverse destination */
2413         for (paramo=data; paramo; paramo=paramo->next) {
2414                 /* If we already have the option set, override it unless
2415                    it was a command line option and the new one isn't */
2416                 if (strcmp(paramo->key, name) == 0) {
2417                         if ((paramo->priority & FLAG_CMDLINE) &&
2418                             !(flags & FLAG_CMDLINE)) {
2419                                 talloc_free(name);
2420                                 return true;
2421                         }
2422
2423                         talloc_free(paramo->value);
2424                         paramo->value = talloc_strdup(paramo, pszParmValue);
2425                         paramo->priority = flags;
2426                         talloc_free(name);
2427                         return true;
2428                 }
2429         }
2430
2431         paramo = talloc_zero(mem_ctx, struct parmlist_entry);
2432         if (!paramo)
2433                 smb_panic("OOM");
2434         paramo->key = talloc_strdup(paramo, name);
2435         paramo->value = talloc_strdup(paramo, pszParmValue);
2436         paramo->priority = flags;
2437         if (service == NULL) {
2438                 DLIST_ADD(lp_ctx->globals->param_opt, paramo);
2439         } else {
2440                 DLIST_ADD(service->param_opt, paramo);
2441         }
2442
2443         talloc_free(name);
2444
2445         return true;
2446 }
2447
2448 static bool set_variable(TALLOC_CTX *mem_ctx, int parmnum, void *parm_ptr,
2449                          const char *pszParmName, const char *pszParmValue,
2450                          struct loadparm_context *lp_ctx, bool on_globals)
2451 {
2452         int i;
2453         /* if it is a special case then go ahead */
2454         if (parm_table[parmnum].special) {
2455                 bool ret;
2456                 ret = parm_table[parmnum].special(lp_ctx, -1, pszParmValue,
2457                                                   (char **)parm_ptr);
2458                 if (!ret) {
2459                         return false;
2460                 }
2461                 goto mark_non_default;
2462         }
2463
2464         /* now switch on the type of variable it is */
2465         switch (parm_table[parmnum].type)
2466         {
2467                 case P_BOOL: {
2468                         bool b;
2469                         if (!set_boolean(pszParmValue, &b)) {
2470                                 DEBUG(0,("lp_do_parameter(%s): value is not boolean!\n", pszParmValue));
2471                                 return false;
2472                         }
2473                         *(bool *)parm_ptr = b;
2474                         }
2475                         break;
2476
2477                 case P_BOOLREV: {
2478                         bool b;
2479                         if (!set_boolean(pszParmValue, &b)) {
2480                                 DEBUG(0,("lp_do_parameter(%s): value is not boolean!\n", pszParmValue));
2481                                 return false;
2482                         }
2483                         *(bool *)parm_ptr = !b;
2484                         }
2485                         break;
2486
2487                 case P_INTEGER:
2488                         *(int *)parm_ptr = atoi(pszParmValue);
2489                         break;
2490
2491                 case P_CHAR:
2492                         *(char *)parm_ptr = *pszParmValue;
2493                         break;
2494
2495                 case P_OCTAL:
2496                         *(int *)parm_ptr = strtol(pszParmValue, NULL, 8);
2497                         break;
2498
2499                 case P_BYTES:
2500                 {
2501                         uint64_t val;
2502                         if (conv_str_size_error(pszParmValue, &val)) {
2503                                 if (val <= INT_MAX) {
2504                                         *(int *)parm_ptr = (int)val;
2505                                         break;
2506                                 }
2507                         }
2508
2509                         DEBUG(0,("lp_do_parameter(%s): value is not "
2510                             "a valid size specifier!\n", pszParmValue));
2511                         return false;
2512                 }
2513
2514                 case P_CMDLIST:
2515                         *(const char ***)parm_ptr = (const char **)str_list_make(mem_ctx,
2516                                                                   pszParmValue, NULL);
2517                         break;
2518                 case P_LIST:
2519                 {
2520                         char **new_list = str_list_make(mem_ctx,
2521                                                         pszParmValue, NULL);
2522                         for (i=0; new_list[i]; i++) {
2523                                 if (new_list[i][0] == '+' && new_list[i][1]) {
2524                                         if (!str_list_check(*(const char ***)parm_ptr,
2525                                                             &new_list[i][1])) {
2526                                                 *(const char ***)parm_ptr = str_list_add(*(const char ***)parm_ptr,
2527                                                                                          &new_list[i][1]);
2528                                         }
2529                                 } else if (new_list[i][0] == '-' && new_list[i][1]) {
2530                                         str_list_remove(*(const char ***)parm_ptr,
2531                                                         &new_list[i][1]);
2532                                 } else {
2533                                         if (i != 0) {
2534                                                 DEBUG(0, ("Unsupported list syntax for: %s = %s\n",
2535                                                           pszParmName, pszParmValue));
2536                                                 return false;
2537                                         }
2538                                         *(const char ***)parm_ptr = (const char **) new_list;
2539                                         break;
2540                                 }
2541                         }
2542                         break;
2543                 }
2544                 case P_STRING:
2545                         lpcfg_string_set(mem_ctx, (char **)parm_ptr, pszParmValue);
2546                         break;
2547
2548                 case P_USTRING:
2549                         lpcfg_string_set_upper(mem_ctx, (char **)parm_ptr, pszParmValue);
2550                         break;
2551
2552                 case P_ENUM:
2553                         for (i = 0; parm_table[parmnum].enum_list[i].name; i++) {
2554                                 if (strequal
2555                                     (pszParmValue,
2556                                      parm_table[parmnum].enum_list[i].name)) {
2557                                         *(int *)parm_ptr =
2558                                                 parm_table[parmnum].
2559                                                 enum_list[i].value;
2560                                         break;
2561                                 }
2562                         }
2563                         if (!parm_table[parmnum].enum_list[i].name) {
2564                                 DEBUG(0,("Unknown enumerated value '%s' for '%s'\n", 
2565                                          pszParmValue, pszParmName));
2566                                 return false;
2567                         }
2568                         break;
2569         }
2570
2571 mark_non_default:
2572         if (on_globals && (lp_ctx->flags[parmnum] & FLAG_DEFAULT)) {
2573                 lp_ctx->flags[parmnum] &= ~FLAG_DEFAULT;
2574                 /* we have to also unset FLAG_DEFAULT on aliases */
2575                 for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
2576                         lp_ctx->flags[i] &= ~FLAG_DEFAULT;
2577                 }
2578                 for (i=parmnum+1;i<NUMPARAMETERS && parm_table[i].offset == parm_table[parmnum].offset;i++) {
2579                         lp_ctx->flags[i] &= ~FLAG_DEFAULT;
2580                 }
2581         }
2582         return true;
2583 }
2584
2585
2586 bool lpcfg_do_global_parameter(struct loadparm_context *lp_ctx,
2587                                const char *pszParmName, const char *pszParmValue)
2588 {
2589         int parmnum = map_parameter(pszParmName);
2590         void *parm_ptr;
2591
2592         if (parmnum < 0) {
2593                 if (strchr(pszParmName, ':')) {
2594                         return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName, pszParmValue, 0);
2595                 }
2596                 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
2597                 return true;
2598         }
2599
2600         /* if the flag has been set on the command line, then don't allow override,
2601            but don't report an error */
2602         if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
2603                 return true;
2604         }
2605
2606         parm_ptr = lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[parmnum]);
2607
2608         return set_variable(lp_ctx->globals, parmnum, parm_ptr,
2609                             pszParmName, pszParmValue, lp_ctx, true);
2610 }
2611
2612 bool lpcfg_do_service_parameter(struct loadparm_context *lp_ctx,
2613                                 struct loadparm_service *service,
2614                                 const char *pszParmName, const char *pszParmValue)
2615 {
2616         void *parm_ptr;
2617         int i;
2618         int parmnum = map_parameter(pszParmName);
2619
2620         if (parmnum < 0) {
2621                 if (strchr(pszParmName, ':')) {
2622                         return lp_do_parameter_parametric(lp_ctx, service, pszParmName, pszParmValue, 0);
2623                 }
2624                 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
2625                 return true;
2626         }
2627
2628         /* if the flag has been set on the command line, then don't allow override,
2629            but don't report an error */
2630         if (lp_ctx->flags[parmnum] & FLAG_CMDLINE) {
2631                 return true;
2632         }
2633
2634         if (parm_table[parmnum].p_class == P_GLOBAL) {
2635                 DEBUG(0,
2636                       ("Global parameter %s found in service section!\n",
2637                        pszParmName));
2638                 return true;
2639         }
2640         parm_ptr = ((char *)service) + parm_table[parmnum].offset;
2641
2642         if (!service->copymap)
2643                 init_copymap(service);
2644
2645         /* this handles the aliases - set the copymap for other
2646          * entries with the same data pointer */
2647         for (i = 0; parm_table[i].label; i++)
2648                 if (parm_table[i].offset == parm_table[parmnum].offset &&
2649                     parm_table[i].p_class == parm_table[parmnum].p_class)
2650                         bitmap_clear(service->copymap, i);
2651
2652         return set_variable(service, parmnum, parm_ptr, pszParmName,
2653                             pszParmValue, lp_ctx, false);
2654 }
2655
2656 /**
2657  * Process a parameter.
2658  */
2659
2660 static bool do_parameter(const char *pszParmName, const char *pszParmValue,
2661                          void *userdata)
2662 {
2663         struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
2664
2665         if (lp_ctx->bInGlobalSection)
2666                 return lpcfg_do_global_parameter(lp_ctx, pszParmName,
2667                                               pszParmValue);
2668         else
2669                 return lpcfg_do_service_parameter(lp_ctx, lp_ctx->currentService,
2670                                                   pszParmName, pszParmValue);
2671 }
2672
2673 /*
2674   variable argument do parameter
2675 */
2676 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx, const char *pszParmName, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
2677 bool lpcfg_do_global_parameter_var(struct loadparm_context *lp_ctx,
2678                                 const char *pszParmName, const char *fmt, ...)
2679 {
2680         char *s;
2681         bool ret;
2682         va_list ap;
2683
2684         va_start(ap, fmt);
2685         s = talloc_vasprintf(NULL, fmt, ap);
2686         va_end(ap);
2687         ret = lpcfg_do_global_parameter(lp_ctx, pszParmName, s);
2688         talloc_free(s);
2689         return ret;
2690 }
2691
2692
2693 /*
2694   set a parameter from the commandline - this is called from command line parameter
2695   parsing code. It sets the parameter then marks the parameter as unable to be modified
2696   by smb.conf processing
2697 */
2698 bool lpcfg_set_cmdline(struct loadparm_context *lp_ctx, const char *pszParmName,
2699                        const char *pszParmValue)
2700 {
2701         int parmnum;
2702         int i;
2703
2704         if (lp_ctx->s3_fns) {
2705                 return lp_ctx->s3_fns->set_cmdline(pszParmName, pszParmValue);
2706         }
2707
2708         parmnum = map_parameter(pszParmName);
2709
2710         while (isspace((unsigned char)*pszParmValue)) pszParmValue++;
2711
2712
2713         if (parmnum < 0 && strchr(pszParmName, ':')) {
2714                 /* set a parametric option */
2715                 return lp_do_parameter_parametric(lp_ctx, NULL, pszParmName,
2716                                                   pszParmValue, FLAG_CMDLINE);
2717         }
2718
2719         if (parmnum < 0) {
2720                 DEBUG(0,("Unknown option '%s'\n", pszParmName));
2721                 return false;
2722         }
2723
2724         /* reset the CMDLINE flag in case this has been called before */
2725         lp_ctx->flags[parmnum] &= ~FLAG_CMDLINE;
2726
2727         if (!lpcfg_do_global_parameter(lp_ctx, pszParmName, pszParmValue)) {
2728                 return false;
2729         }
2730
2731         lp_ctx->flags[parmnum] |= FLAG_CMDLINE;
2732
2733         /* we have to also set FLAG_CMDLINE on aliases */
2734         for (i=parmnum-1;i>=0 && parm_table[i].offset == parm_table[parmnum].offset;i--) {
2735                 lp_ctx->flags[i] |= FLAG_CMDLINE;
2736         }
2737         for (i=parmnum+1;i<NUMPARAMETERS && parm_table[i].offset == parm_table[parmnum].offset;i++) {
2738                 lp_ctx->flags[i] |= FLAG_CMDLINE;
2739         }
2740
2741         return true;
2742 }
2743
2744 /*
2745   set a option from the commandline in 'a=b' format. Use to support --option
2746 */
2747 bool lpcfg_set_option(struct loadparm_context *lp_ctx, const char *option)
2748 {
2749         char *p, *s;
2750         bool ret;
2751
2752         s = talloc_strdup(NULL, option);
2753         if (!s) {
2754                 return false;
2755         }
2756
2757         p = strchr(s, '=');
2758         if (!p) {
2759                 talloc_free(s);
2760                 return false;
2761         }
2762
2763         *p = 0;
2764
2765         ret = lpcfg_set_cmdline(lp_ctx, s, p+1);
2766         talloc_free(s);
2767         return ret;
2768 }
2769
2770
2771 #define BOOLSTR(b) ((b) ? "Yes" : "No")
2772
2773 /**
2774  * Print a parameter of the specified type.
2775  */
2776
2777 static void print_parameter(struct parm_struct *p, void *ptr, FILE * f)
2778 {
2779         /* For the seperation of lists values that we print below */
2780         const char *list_sep = ", ";
2781         int i;
2782         switch (p->type)
2783         {
2784                 case P_ENUM:
2785                         for (i = 0; p->enum_list[i].name; i++) {
2786                                 if (*(int *)ptr == p->enum_list[i].value) {
2787                                         fprintf(f, "%s",
2788                                                 p->enum_list[i].name);
2789                                         break;
2790                                 }
2791                         }
2792                         break;
2793
2794                 case P_BOOL:
2795                         fprintf(f, "%s", BOOLSTR(*(bool *)ptr));
2796                         break;
2797
2798                 case P_BOOLREV:
2799                         fprintf(f, "%s", BOOLSTR(!*(bool *)ptr));
2800                         break;
2801
2802                 case P_INTEGER:
2803                 case P_BYTES:
2804                         fprintf(f, "%d", *(int *)ptr);
2805                         break;
2806
2807                 case P_CHAR:
2808                         fprintf(f, "%c", *(char *)ptr);
2809                         break;
2810
2811                 case P_OCTAL: {
2812                         int val = *(int *)ptr; 
2813                         if (val == -1) {
2814                                 fprintf(f, "-1");
2815                         } else {
2816                                 fprintf(f, "0%o", val);
2817                         }
2818                         break;
2819                 }
2820
2821                 case P_CMDLIST:
2822                         list_sep = " ";
2823                         /* fall through */
2824                 case P_LIST:
2825                         if ((char ***)ptr && *(char ***)ptr) {
2826                                 char **list = *(char ***)ptr;
2827                                 for (; *list; list++) {
2828                                         /* surround strings with whitespace in double quotes */
2829                                         if (*(list+1) == NULL) {
2830                                                 /* last item, no extra separator */
2831                                                 list_sep = "";
2832                                         }
2833                                         if ( strchr_m( *list, ' ' ) ) {
2834                                                 fprintf(f, "\"%s\"%s", *list, list_sep);
2835                                         } else {
2836                                                 fprintf(f, "%s%s", *list, list_sep);
2837                                         }
2838                                 }
2839                         }
2840                         break;
2841
2842                 case P_STRING:
2843                 case P_USTRING:
2844                         if (*(char **)ptr) {
2845                                 fprintf(f, "%s", *(char **)ptr);
2846                         }
2847                         break;
2848                 case P_SEP:
2849                         break;
2850         }
2851 }
2852
2853 /**
2854  * Check if two parameters are equal.
2855  */
2856
2857 static bool equal_parameter(parm_type type, void *ptr1, void *ptr2)
2858 {
2859         switch (type) {
2860                 case P_BOOL:
2861                 case P_BOOLREV:
2862                         return (*((bool *)ptr1) == *((bool *)ptr2));
2863
2864                 case P_INTEGER:
2865                 case P_ENUM:
2866                 case P_OCTAL:
2867                 case P_BYTES:
2868                         return (*((int *)ptr1) == *((int *)ptr2));
2869
2870                 case P_CHAR:
2871                         return (*((char *)ptr1) == *((char *)ptr2));
2872
2873                 case P_LIST:
2874                 case P_CMDLIST:
2875                         return str_list_equal(*(const char ***)ptr1, *(const char ***)ptr2);
2876
2877                 case P_STRING:
2878                 case P_USTRING:
2879                 {
2880                         char *p1 = *(char **)ptr1, *p2 = *(char **)ptr2;
2881                         if (p1 && !*p1)
2882                                 p1 = NULL;
2883                         if (p2 && !*p2)
2884                                 p2 = NULL;
2885                         return (p1 == p2 || strequal(p1, p2));
2886                 }
2887                 case P_SEP:
2888                         break;
2889         }
2890         return false;
2891 }
2892
2893 /**
2894  * Process a new section (service).
2895  *
2896  * At this stage all sections are services.
2897  * Later we'll have special sections that permit server parameters to be set.
2898  * Returns True on success, False on failure.
2899  */
2900
2901 static bool do_section(const char *pszSectionName, void *userdata)
2902 {
2903         struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
2904         bool bRetval;
2905         bool isglobal = ((strwicmp(pszSectionName, GLOBAL_NAME) == 0) ||
2906                          (strwicmp(pszSectionName, GLOBAL_NAME2) == 0));
2907         bRetval = false;
2908
2909         /* if we've just struck a global section, note the fact. */
2910         lp_ctx->bInGlobalSection = isglobal;
2911
2912         /* check for multiple global sections */
2913         if (lp_ctx->bInGlobalSection) {
2914                 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
2915                 return true;
2916         }
2917
2918         /* if we have a current service, tidy it up before moving on */
2919         bRetval = true;
2920
2921         if (lp_ctx->currentService != NULL)
2922                 bRetval = lpcfg_service_ok(lp_ctx->currentService);
2923
2924         /* if all is still well, move to the next record in the services array */
2925         if (bRetval) {
2926                 /* We put this here to avoid an odd message order if messages are */
2927                 /* issued by the post-processing of a previous section. */
2928                 DEBUG(4, ("Processing section \"[%s]\"\n", pszSectionName));
2929
2930                 if ((lp_ctx->currentService = lpcfg_add_service(lp_ctx, lp_ctx->sDefault,
2931                                                                    pszSectionName))
2932                     == NULL) {
2933                         DEBUG(0, ("Failed to add a new service\n"));
2934                         return false;
2935                 }
2936         }
2937
2938         return bRetval;
2939 }
2940
2941
2942 /**
2943  * Determine if a particular base parameter is currently set to the default value.
2944  */
2945
2946 static bool is_default(struct loadparm_service *sDefault, int i)
2947 {
2948         void *def_ptr = ((char *)sDefault) + parm_table[i].offset;
2949         if (!defaults_saved)
2950                 return false;
2951         switch (parm_table[i].type) {
2952                 case P_CMDLIST:
2953                 case P_LIST:
2954                         return str_list_equal((const char **)parm_table[i].def.lvalue, 
2955                                               (const char **)def_ptr);
2956                 case P_STRING:
2957                 case P_USTRING:
2958                         return strequal(parm_table[i].def.svalue,
2959                                         *(char **)def_ptr);
2960                 case P_BOOL:
2961                 case P_BOOLREV:
2962                         return parm_table[i].def.bvalue ==
2963                                 *(bool *)def_ptr;
2964                 case P_INTEGER:
2965                 case P_CHAR:
2966                 case P_OCTAL:
2967                 case P_BYTES:
2968                 case P_ENUM:
2969                         return parm_table[i].def.ivalue ==
2970                                 *(int *)def_ptr;
2971         }
2972         return false;
2973 }
2974
2975 /**
2976  *Display the contents of the global structure.
2977  */
2978
2979 static void dump_globals(struct loadparm_context *lp_ctx, FILE *f,
2980                          bool show_defaults)
2981 {
2982         int i;
2983         struct parmlist_entry *data;
2984
2985         fprintf(f, "# Global parameters\n[global]\n");
2986
2987         for (i = 0; parm_table[i].label; i++)
2988                 if (parm_table[i].p_class == P_GLOBAL &&
2989                     (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset))) {
2990                         if (!show_defaults && (lp_ctx->flags[i] & FLAG_DEFAULT))
2991                                 continue;
2992                         fprintf(f, "\t%s = ", parm_table[i].label);
2993                         print_parameter(&parm_table[i], lpcfg_parm_ptr(lp_ctx, NULL, &parm_table[i]), f);
2994                         fprintf(f, "\n");
2995         }
2996         if (lp_ctx->globals->param_opt != NULL) {
2997                 for (data = lp_ctx->globals->param_opt; data;
2998                      data = data->next) {
2999                         if (!show_defaults && (data->priority & FLAG_DEFAULT)) {
3000                                 continue;
3001                         }
3002                         fprintf(f, "\t%s = %s\n", data->key, data->value);
3003                 }
3004         }
3005
3006 }
3007
3008 /**
3009  * Display the contents of a single services record.
3010  */
3011
3012 static void dump_a_service(struct loadparm_service * pService, struct loadparm_service *sDefault, FILE * f,
3013                            unsigned int *flags)
3014 {
3015         int i;
3016         struct parmlist_entry *data;
3017
3018         if (pService != sDefault)
3019                 fprintf(f, "\n[%s]\n", pService->szService);
3020
3021         for (i = 0; parm_table[i].label; i++) {
3022                 if (parm_table[i].p_class == P_LOCAL &&
3023                     (*parm_table[i].label != '-') &&
3024                     (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset)))
3025                 {
3026                         if (pService == sDefault) {
3027                                 if (flags && (flags[i] & FLAG_DEFAULT)) {
3028                                         continue;
3029                                 }
3030                                 if (defaults_saved) {
3031                                         if (is_default(sDefault, i)) {
3032                                                 continue;
3033                                         }
3034                                 }
3035                         } else {
3036                                 if (equal_parameter(parm_table[i].type,
3037                                                     ((char *)pService) +
3038                                                     parm_table[i].offset,
3039                                                     ((char *)sDefault) +
3040                                                     parm_table[i].offset))
3041                                         continue;
3042                         }
3043
3044                         fprintf(f, "\t%s = ", parm_table[i].label);
3045                         print_parameter(&parm_table[i],
3046                                         ((char *)pService) + parm_table[i].offset, f);
3047                         fprintf(f, "\n");
3048                 }
3049         }
3050         if (pService->param_opt != NULL) {
3051                 for (data = pService->param_opt; data; data = data->next) {
3052                         fprintf(f, "\t%s = %s\n", data->key, data->value);
3053                 }
3054         }
3055 }
3056
3057 bool lpcfg_dump_a_parameter(struct loadparm_context *lp_ctx,
3058                             struct loadparm_service *service,
3059                             const char *parm_name, FILE * f)
3060 {
3061         struct parm_struct *parm;
3062         void *ptr;
3063
3064         parm = lpcfg_parm_struct(lp_ctx, parm_name);
3065         if (!parm) {
3066                 return false;
3067         }
3068
3069         ptr = lpcfg_parm_ptr(lp_ctx, service,parm);
3070
3071         print_parameter(parm, ptr, f);
3072         fprintf(f, "\n");
3073         return true;
3074 }
3075
3076 /**
3077  * Return info about the next parameter in a service.
3078  * snum==-1 gives the globals.
3079  * Return NULL when out of parameters.
3080  */
3081
3082
3083 struct parm_struct *lpcfg_next_parameter(struct loadparm_context *lp_ctx, int snum, int *i,
3084                                          int allparameters)
3085 {
3086         if (snum == -1) {
3087                 /* do the globals */
3088                 for (; parm_table[*i].label; (*i)++) {
3089                         if ((*parm_table[*i].label == '-'))
3090                                 continue;
3091
3092                         if ((*i) > 0
3093                             && (parm_table[*i].offset ==
3094                                 parm_table[(*i) - 1].offset)
3095                             && (parm_table[*i].p_class ==
3096                                 parm_table[(*i) - 1].p_class))
3097                                 continue;
3098
3099                         return &parm_table[(*i)++];
3100                 }
3101         } else {
3102                 struct loadparm_service *pService = lp_ctx->services[snum];
3103
3104                 for (; parm_table[*i].label; (*i)++) {
3105                         if (parm_table[*i].p_class == P_LOCAL &&
3106                             (*parm_table[*i].label != '-') &&
3107                             ((*i) == 0 ||
3108                              (parm_table[*i].offset !=
3109                               parm_table[(*i) - 1].offset)))
3110                         {
3111                                 if (allparameters ||
3112                                     !equal_parameter(parm_table[*i].type,
3113                                                      ((char *)pService) +
3114                                                      parm_table[*i].offset,
3115                                                      ((char *)lp_ctx->sDefault) +
3116                                                      parm_table[*i].offset))
3117                                 {
3118                                         return &parm_table[(*i)++];
3119                                 }
3120                         }
3121                 }
3122         }
3123
3124         return NULL;
3125 }
3126
3127
3128 /**
3129  * Auto-load some home services.
3130  */
3131 static void lpcfg_add_auto_services(struct loadparm_context *lp_ctx,
3132                                     const char *str)
3133 {
3134         return;
3135 }
3136
3137
3138 /**
3139  * Unload unused services.
3140  */
3141
3142 void lpcfg_killunused(struct loadparm_context *lp_ctx,
3143                    struct smbsrv_connection *smb,
3144                    bool (*snumused) (struct smbsrv_connection *, int))
3145 {
3146         int i;
3147         for (i = 0; i < lp_ctx->iNumServices; i++) {
3148                 if (lp_ctx->services[i] == NULL)
3149                         continue;
3150
3151                 if (!snumused || !snumused(smb, i)) {
3152                         talloc_free(lp_ctx->services[i]);
3153                         lp_ctx->services[i] = NULL;
3154                 }
3155         }
3156 }
3157
3158
3159 static int lpcfg_destructor(struct loadparm_context *lp_ctx)
3160 {
3161         struct parmlist_entry *data;
3162
3163         if (lp_ctx->refuse_free) {
3164                 /* someone is trying to free the
3165                    global_loadparm_context.
3166                    We can't allow that. */
3167                 return -1;
3168         }
3169
3170         if (lp_ctx->globals->param_opt != NULL) {
3171                 struct parmlist_entry *next;
3172                 for (data = lp_ctx->globals->param_opt; data; data=next) {
3173                         next = data->next;
3174                         if (data->priority & FLAG_CMDLINE) continue;
3175                         DLIST_REMOVE(lp_ctx->globals->param_opt, data);
3176                         talloc_free(data);
3177                 }
3178         }
3179
3180         return 0;
3181 }
3182
3183 /**
3184  * Initialise the global parameter structure.
3185  *
3186  * Note that most callers should use loadparm_init_global() instead
3187  */
3188 struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
3189 {
3190         int i;
3191         char *myname;
3192         struct loadparm_context *lp_ctx;
3193         struct parmlist_entry *parm;
3194         char *logfile;
3195
3196         lp_ctx = talloc_zero(mem_ctx, struct loadparm_context);
3197         if (lp_ctx == NULL)
3198                 return NULL;
3199
3200         talloc_set_destructor(lp_ctx, lpcfg_destructor);
3201         lp_ctx->bInGlobalSection = true;
3202         lp_ctx->globals = talloc_zero(lp_ctx, struct loadparm_global);
3203         lp_ctx->sDefault = talloc_zero(lp_ctx, struct loadparm_service);
3204
3205         lp_ctx->sDefault->iMaxPrintJobs = 1000;
3206         lp_ctx->sDefault->bAvailable = true;
3207         lp_ctx->sDefault->bBrowseable = true;
3208         lp_ctx->sDefault->bRead_only = true;
3209         lp_ctx->sDefault->bMap_archive = true;
3210         lp_ctx->sDefault->iStrictLocking = true;
3211         lp_ctx->sDefault->bOpLocks = true;
3212         lp_ctx->sDefault->iCreate_mask = 0744;
3213         lp_ctx->sDefault->iCreate_force_mode = 0000;
3214         lp_ctx->sDefault->iDir_mask = 0755;
3215         lp_ctx->sDefault->iDir_force_mode = 0000;
3216
3217         DEBUG(3, ("Initialising global parameters\n"));
3218
3219         for (i = 0; parm_table[i].label; i++) {
3220                 if ((parm_table[i].type == P_STRING ||
3221                      parm_table[i].type == P_USTRING) &&
3222                     !(lp_ctx->flags[i] & FLAG_CMDLINE)) {
3223                         char **r;
3224                         if (parm_table[i].p_class == P_LOCAL) {
3225                                 r = (char **)(((char *)lp_ctx->sDefault) + parm_table[i].offset);
3226                         } else {
3227                                 r = (char **)(((char *)lp_ctx->globals) + parm_table[i].offset);
3228                         }
3229                         *r = talloc_strdup(lp_ctx, "");
3230                 }
3231         }
3232
3233         logfile = talloc_asprintf(lp_ctx, "%s/log.samba", dyn_LOGFILEBASE);
3234         lpcfg_do_global_parameter(lp_ctx, "log file", logfile);
3235         talloc_free(logfile);
3236
3237         lpcfg_do_global_parameter(lp_ctx, "log level", "0");
3238
3239         lpcfg_do_global_parameter(lp_ctx, "share backend", "classic");
3240
3241         lpcfg_do_global_parameter(lp_ctx, "server role", "auto");
3242         lpcfg_do_global_parameter(lp_ctx, "domain logons", "No");
3243         lpcfg_do_global_parameter(lp_ctx, "domain master", "Auto");
3244
3245         /* options that can be set on the command line must be initialised via
3246            the slower lpcfg_do_global_parameter() to ensure that FLAG_CMDLINE is obeyed */
3247 #ifdef TCP_NODELAY
3248         lpcfg_do_global_parameter(lp_ctx, "socket options", "TCP_NODELAY");
3249 #endif
3250         lpcfg_do_global_parameter(lp_ctx, "workgroup", DEFAULT_WORKGROUP);
3251         myname = get_myname(lp_ctx);
3252         lpcfg_do_global_parameter(lp_ctx, "netbios name", myname);
3253         talloc_free(myname);
3254         lpcfg_do_global_parameter(lp_ctx, "name resolve order", "wins host bcast");
3255
3256         lpcfg_do_global_parameter(lp_ctx, "fstype", "NTFS");
3257
3258         lpcfg_do_global_parameter(lp_ctx, "ntvfs handler", "unixuid default");
3259         lpcfg_do_global_parameter(lp_ctx, "max connections", "-1");
3260
3261         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");
3262         lpcfg_do_global_parameter(lp_ctx, "server services", "smb rpc nbt wrepl ldap cldap kdc drepl winbind ntp_signd kcc dnsupdate");
3263         lpcfg_do_global_parameter(lp_ctx, "ntptr providor", "simple_ldb");
3264         /* the winbind method for domain controllers is for both RODC
3265            auth forwarding and for trusted domains */
3266         lpcfg_do_global_parameter(lp_ctx, "private dir", dyn_PRIVATE_DIR);
3267         lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_LOCAL_MACHINE", "hklm.ldb");
3268
3269         /* This hive should be dynamically generated by Samba using
3270            data from the sam, but for the moment leave it in a tdb to
3271            keep regedt32 from popping up an annoying dialog. */
3272         lpcfg_do_global_parameter(lp_ctx, "registry:HKEY_USERS", "hku.ldb");
3273
3274         /* using UTF8 by default allows us to support all chars */
3275         lpcfg_do_global_parameter(lp_ctx, "unix charset", "UTF8");
3276
3277         /* Use codepage 850 as a default for the dos character set */
3278         lpcfg_do_global_parameter(lp_ctx, "dos charset", "CP850");
3279
3280         /*
3281          * Allow the default PASSWD_CHAT to be overridden in local.h.
3282          */
3283         lpcfg_do_global_parameter(lp_ctx, "passwd chat", DEFAULT_PASSWD_CHAT);
3284
3285         lpcfg_do_global_parameter(lp_ctx, "pid directory", dyn_PIDDIR);
3286         lpcfg_do_global_parameter(lp_ctx, "lock dir", dyn_LOCKDIR);
3287         lpcfg_do_global_parameter(lp_ctx, "state directory", dyn_STATEDIR);
3288         lpcfg_do_global_parameter(lp_ctx, "cache directory", dyn_CACHEDIR);
3289         lpcfg_do_global_parameter(lp_ctx, "ncalrpc dir", dyn_NCALRPCDIR);
3290
3291         lpcfg_do_global_parameter(lp_ctx, "socket address", "");
3292         lpcfg_do_global_parameter_var(lp_ctx, "server string",
3293                                    "Samba %s", SAMBA_VERSION_STRING);
3294
3295         lpcfg_do_global_parameter(lp_ctx, "password server", "*");
3296
3297         lpcfg_do_global_parameter(lp_ctx, "max mux", "50");
3298         lpcfg_do_global_parameter(lp_ctx, "max xmit", "12288");
3299         lpcfg_do_global_parameter(lp_ctx, "host msdfs", "true");
3300
3301         lpcfg_do_global_parameter(lp_ctx, "password level", "0");
3302         lpcfg_do_global_parameter(lp_ctx, "LargeReadwrite", "True");
3303         lpcfg_do_global_parameter(lp_ctx, "server min protocol", "CORE");
3304         lpcfg_do_global_parameter(lp_ctx, "server max protocol", "NT1");
3305         lpcfg_do_global_parameter(lp_ctx, "client min protocol", "CORE");
3306         lpcfg_do_global_parameter(lp_ctx, "client max protocol", "NT1");
3307         lpcfg_do_global_parameter(lp_ctx, "security", "AUTO");
3308         lpcfg_do_global_parameter(lp_ctx, "paranoid server security", "True");
3309         lpcfg_do_global_parameter(lp_ctx, "EncryptPasswords", "True");
3310         lpcfg_do_global_parameter(lp_ctx, "ReadRaw", "True");
3311         lpcfg_do_global_parameter(lp_ctx, "WriteRaw", "True");
3312         lpcfg_do_global_parameter(lp_ctx, "NullPasswords", "False");
3313         lpcfg_do_global_parameter(lp_ctx, "ObeyPamRestrictions", "False");
3314
3315         lpcfg_do_global_parameter(lp_ctx, "TimeServer", "False");
3316         lpcfg_do_global_parameter(lp_ctx, "BindInterfacesOnly", "False");
3317         lpcfg_do_global_parameter(lp_ctx, "Unicode", "True");
3318         lpcfg_do_global_parameter(lp_ctx, "ClientLanManAuth", "False");
3319         lpcfg_do_global_parameter(lp_ctx, "ClientNTLMv2Auth", "True");
3320         lpcfg_do_global_parameter(lp_ctx, "LanmanAuth", "False");
3321         lpcfg_do_global_parameter(lp_ctx, "NTLMAuth", "True");
3322         lpcfg_do_global_parameter(lp_ctx, "client use spnego principal", "False");
3323
3324         lpcfg_do_global_parameter(lp_ctx, "UnixExtensions", "False");
3325
3326         lpcfg_do_global_parameter(lp_ctx, "PreferredMaster", "Auto");
3327         lpcfg_do_global_parameter(lp_ctx, "LocalMaster", "True");
3328
3329         lpcfg_do_global_parameter(lp_ctx, "wins support", "False");
3330         lpcfg_do_global_parameter(lp_ctx, "dns proxy", "True");
3331
3332         lpcfg_do_global_parameter(lp_ctx, "winbind separator", "\\");
3333         lpcfg_do_global_parameter(lp_ctx, "winbind sealed pipes", "True");
3334         lpcfg_do_global_parameter(lp_ctx, "winbindd socket directory", dyn_WINBINDD_SOCKET_DIR);
3335 #if _SAMBA_BUILD_ >= 4
3336         lpcfg_do_global_parameter(lp_ctx, "winbindd privileged socket directory", dyn_WINBINDD_PRIVILEGED_SOCKET_DIR);
3337         lpcfg_do_global_parameter(lp_ctx, "ntp signd socket directory", dyn_NTP_SIGND_SOCKET_DIR);
3338         lpcfg_do_global_parameter_var(lp_ctx, "dns update command", "%s/samba_dnsupdate", dyn_SCRIPTSBINDIR);
3339         lpcfg_do_global_parameter_var(lp_ctx, "spn update command", "%s/samba_spnupdate", dyn_SCRIPTSBINDIR);
3340         lpcfg_do_global_parameter_var(lp_ctx, "samba kcc command",
3341                                         "%s/samba_kcc", dyn_SCRIPTSBINDIR);
3342 #endif
3343         lpcfg_do_global_parameter(lp_ctx, "template shell", "/bin/false");
3344         lpcfg_do_global_parameter(lp_ctx, "template homedir", "/home/%WORKGROUP%/%ACCOUNTNAME%");
3345         lpcfg_do_global_parameter(lp_ctx, "idmap trusted only", "False");
3346
3347         lpcfg_do_global_parameter(lp_ctx, "client signing", "default");
3348         lpcfg_do_global_parameter(lp_ctx, "server signing", "default");
3349
3350         lpcfg_do_global_parameter(lp_ctx, "use spnego", "True");
3351
3352         lpcfg_do_global_parameter(lp_ctx, "use mmap", "True");
3353
3354         lpcfg_do_global_parameter(lp_ctx, "smb ports", "445 139");
3355         lpcfg_do_global_parameter(lp_ctx, "nbt port", "137");
3356         lpcfg_do_global_parameter(lp_ctx, "dgram port", "138");
3357         lpcfg_do_global_parameter(lp_ctx, "cldap port", "389");
3358         lpcfg_do_global_parameter(lp_ctx, "krb5 port", "88");
3359         lpcfg_do_global_parameter(lp_ctx, "kpasswd port", "464");
3360         lpcfg_do_global_parameter(lp_ctx, "web port", "901");
3361
3362         lpcfg_do_global_parameter(lp_ctx, "nt status support", "True");
3363
3364         lpcfg_do_global_parameter(lp_ctx, "max wins ttl", "518400"); /* 6 days */
3365         lpcfg_do_global_parameter(lp_ctx, "min wins ttl", "10");
3366
3367         lpcfg_do_global_parameter(lp_ctx, "tls enabled", "True");
3368         lpcfg_do_global_parameter(lp_ctx, "tls keyfile", "tls/key.pem");
3369         lpcfg_do_global_parameter(lp_ctx, "tls certfile", "tls/cert.pem");
3370         lpcfg_do_global_parameter(lp_ctx, "tls cafile", "tls/ca.pem");
3371         lpcfg_do_global_parameter(lp_ctx, "prefork children:smb", "4");
3372
3373         lpcfg_do_global_parameter(lp_ctx, "rndc command", "/usr/sbin/rndc");
3374         lpcfg_do_global_parameter(lp_ctx, "nsupdate command", "/usr/bin/nsupdate -g");
3375
3376         lpcfg_do_global_parameter(lp_ctx, "allow dns updates", "False");
3377
3378         for (i = 0; parm_table[i].label; i++) {
3379                 if (!(lp_ctx->flags[i] & FLAG_CMDLINE)) {
3380                         lp_ctx->flags[i] |= FLAG_DEFAULT;
3381                 }
3382         }
3383
3384         for (parm=lp_ctx->globals->param_opt; parm; parm=parm->next) {
3385                 if (!(parm->priority & FLAG_CMDLINE)) {
3386                         parm->priority |= FLAG_DEFAULT;
3387                 }
3388         }
3389
3390         return lp_ctx;
3391 }
3392
3393 /**
3394  * Initialise the global parameter structure.
3395  */
3396 struct loadparm_context *loadparm_init_global(bool load_default)
3397 {
3398         if (global_loadparm_context == NULL) {
3399                 global_loadparm_context = loadparm_init(NULL);
3400         }
3401         if (global_loadparm_context == NULL) {
3402                 return NULL;
3403         }
3404         global_loadparm_context->global = true;
3405         if (load_default && !global_loadparm_context->loaded) {
3406                 lpcfg_load_default(global_loadparm_context);
3407         }
3408         global_loadparm_context->refuse_free = true;
3409         return global_loadparm_context;
3410 }
3411
3412 /**
3413  * Initialise the global parameter structure.
3414  */
3415 struct loadparm_context *loadparm_init_s3(TALLOC_CTX *mem_ctx, 
3416                                           const struct loadparm_s3_context *s3_fns)
3417 {
3418         struct loadparm_context *loadparm_context = talloc_zero(mem_ctx, struct loadparm_context);
3419         if (!loadparm_context) {
3420                 return NULL;
3421         }
3422         loadparm_context->s3_fns = s3_fns;
3423         return loadparm_context;
3424 }
3425
3426 const char *lpcfg_configfile(struct loadparm_context *lp_ctx)
3427 {
3428         return lp_ctx->szConfigFile;
3429 }
3430
3431 const char *lp_default_path(void)
3432 {
3433     if (getenv("SMB_CONF_PATH"))
3434         return getenv("SMB_CONF_PATH");
3435     else
3436         return dyn_CONFIGFILE;
3437 }
3438
3439 /**
3440  * Update the internal state of a loadparm context after settings 
3441  * have changed.
3442  */
3443 static bool lpcfg_update(struct loadparm_context *lp_ctx)
3444 {
3445         struct debug_settings settings;
3446         lpcfg_add_auto_services(lp_ctx, lpcfg_auto_services(lp_ctx));
3447
3448         if (!lp_ctx->globals->szWINSservers && lp_ctx->globals->bWINSsupport) {
3449                 lpcfg_do_global_parameter(lp_ctx, "wins server", "127.0.0.1");
3450         }
3451
3452         if (!lp_ctx->global) {
3453                 return true;
3454         }
3455
3456         panic_action = lp_ctx->globals->panic_action;
3457
3458         reload_charcnv(lp_ctx);
3459
3460         ZERO_STRUCT(settings);
3461         /* Add any more debug-related smb.conf parameters created in
3462          * future here */
3463         settings.timestamp_logs = true;
3464         debug_set_settings(&settings);
3465
3466         /* FIXME: This is a bit of a hack, but we can't use a global, since 
3467          * not everything that uses lp also uses the socket library */
3468         if (lpcfg_parm_bool(lp_ctx, NULL, "socket", "testnonblock", false)) {
3469                 setenv("SOCKET_TESTNONBLOCK", "1", 1);
3470         } else {
3471                 unsetenv("SOCKET_TESTNONBLOCK");
3472         }
3473
3474         return true;
3475 }
3476
3477 bool lpcfg_load_default(struct loadparm_context *lp_ctx)
3478 {
3479     const char *path;
3480
3481     path = lp_default_path();
3482
3483     if (!file_exist(path)) {
3484             /* We allow the default smb.conf file to not exist, 
3485              * basically the equivalent of an empty file. */
3486             return lpcfg_update(lp_ctx);
3487     }
3488
3489     return lpcfg_load(lp_ctx, path);
3490 }
3491
3492 /**
3493  * Load the services array from the services file.
3494  *
3495  * Return True on success, False on failure.
3496  */
3497 bool lpcfg_load(struct loadparm_context *lp_ctx, const char *filename)
3498 {
3499         char *n2;
3500         bool bRetval;
3501
3502         filename = talloc_strdup(lp_ctx, filename);
3503
3504         lp_ctx->szConfigFile = filename;
3505
3506         if (lp_ctx->s3_fns) {
3507                 return lp_ctx->s3_fns->load(filename);
3508         }
3509
3510         lp_ctx->bInGlobalSection = true;
3511         n2 = standard_sub_basic(lp_ctx, lp_ctx->szConfigFile);
3512         DEBUG(2, ("lpcfg_load: refreshing parameters from %s\n", n2));
3513
3514         add_to_file_list(lp_ctx, lp_ctx->szConfigFile, n2);
3515
3516         /* We get sections first, so have to start 'behind' to make up */
3517         lp_ctx->currentService = NULL;
3518         bRetval = pm_process(n2, do_section, do_parameter, lp_ctx);
3519
3520         /* finish up the last section */
3521         DEBUG(4, ("pm_process() returned %s\n", BOOLSTR(bRetval)));
3522         if (bRetval)
3523                 if (lp_ctx->currentService != NULL)
3524                         bRetval = lpcfg_service_ok(lp_ctx->currentService);
3525
3526         bRetval = bRetval && lpcfg_update(lp_ctx);
3527
3528         /* we do this unconditionally, so that it happens even
3529            for a missing smb.conf */
3530         reload_charcnv(lp_ctx);
3531
3532         if (bRetval == true) {
3533                 /* set this up so that any child python tasks will
3534                    find the right smb.conf */
3535                 setenv("SMB_CONF_PATH", filename, 1);
3536
3537                 /* set the context used by the lp_*() function
3538                    varients */
3539                 global_loadparm_context = lp_ctx;
3540                 lp_ctx->loaded = true;
3541         }
3542
3543         return bRetval;
3544 }
3545
3546 /**
3547  * Return the max number of services.
3548  */
3549
3550 int lpcfg_numservices(struct loadparm_context *lp_ctx)
3551 {
3552         if (lp_ctx->s3_fns) {
3553                 return lp_ctx->s3_fns->get_numservices();
3554         }
3555
3556         return lp_ctx->iNumServices;
3557 }
3558
3559 /**
3560  * Display the contents of the services array in human-readable form.
3561  */
3562
3563 void lpcfg_dump(struct loadparm_context *lp_ctx, FILE *f, bool show_defaults,
3564              int maxtoprint)
3565 {
3566         int iService;
3567
3568         if (lp_ctx->s3_fns) {
3569                 lp_ctx->s3_fns->dump(f, show_defaults, maxtoprint);
3570                 return;
3571         }
3572
3573         defaults_saved = !show_defaults;
3574
3575         dump_globals(lp_ctx, f, show_defaults);
3576
3577         dump_a_service(lp_ctx->sDefault, lp_ctx->sDefault, f, lp_ctx->flags);
3578
3579         for (iService = 0; iService < maxtoprint; iService++)
3580                 lpcfg_dump_one(f, show_defaults, lp_ctx->services[iService], lp_ctx->sDefault);
3581 }
3582
3583 /**
3584  * Display the contents of one service in human-readable form.
3585  */
3586 void lpcfg_dump_one(FILE *f, bool show_defaults, struct loadparm_service *service, struct loadparm_service *sDefault)
3587 {
3588         if (service != NULL) {
3589                 if (service->szService[0] == '\0')
3590                         return;
3591                 dump_a_service(service, sDefault, f, NULL);
3592         }
3593 }
3594
3595 struct loadparm_service *lpcfg_servicebynum(struct loadparm_context *lp_ctx,
3596                                             int snum)
3597 {
3598         if (lp_ctx->s3_fns) {
3599                 return lp_ctx->s3_fns->get_servicebynum(snum);
3600         }
3601
3602         return lp_ctx->services[snum];
3603 }
3604
3605 struct loadparm_service *lpcfg_service(struct loadparm_context *lp_ctx,
3606                                     const char *service_name)
3607 {
3608         int iService;
3609         char *serviceName;
3610
3611         if (lp_ctx->s3_fns) {
3612                 return lp_ctx->s3_fns->get_service(service_name);
3613         }
3614
3615         for (iService = lp_ctx->iNumServices - 1; iService >= 0; iService--) {
3616                 if (lp_ctx->services[iService] &&
3617                     lp_ctx->services[iService]->szService) {
3618                         /*
3619                          * The substitution here is used to support %U is
3620                          * service names
3621                          */
3622                         serviceName = standard_sub_basic(
3623                                         lp_ctx->services[iService],
3624                                         lp_ctx->services[iService]->szService);
3625                         if (strequal(serviceName, service_name)) {
3626                                 talloc_free(serviceName);
3627                                 return lp_ctx->services[iService];
3628                         }
3629                         talloc_free(serviceName);
3630                 }
3631         }
3632
3633         DEBUG(7,("lpcfg_servicenumber: couldn't find %s\n", service_name));
3634         return NULL;
3635 }
3636
3637 const char *lpcfg_servicename(const struct loadparm_service *service)
3638 {
3639         return lp_string((const char *)service->szService);
3640 }
3641
3642 /**
3643  * A useful volume label function.
3644  */
3645 const char *lpcfg_volume_label(struct loadparm_service *service, struct loadparm_service *sDefault)
3646 {
3647         const char *ret;
3648         ret = lp_string((const char *)((service != NULL && service->volume != NULL) ?
3649                                        service->volume : sDefault->volume));
3650         if (!*ret)
3651                 return lpcfg_servicename(service);
3652         return ret;
3653 }
3654
3655 /**
3656  * If we are PDC then prefer us as DMB
3657  */
3658 const char *lpcfg_printername(struct loadparm_service *service, struct loadparm_service *sDefault)
3659 {
3660         const char *ret;
3661         ret = lp_string((const char *)((service != NULL && service->szPrintername != NULL) ?
3662                                        service->szPrintername : sDefault->szPrintername));
3663         if (ret == NULL || (ret != NULL && *ret == '\0'))
3664                 ret = lpcfg_servicename(service);
3665
3666         return ret;
3667 }
3668
3669
3670 /**
3671  * Return the max print jobs per queue.
3672  */
3673 int lpcfg_maxprintjobs(struct loadparm_service *service, struct loadparm_service *sDefault)
3674 {
3675         int maxjobs = (service != NULL) ? service->iMaxPrintJobs : sDefault->iMaxPrintJobs;
3676         if (maxjobs <= 0 || maxjobs >= PRINT_MAX_JOBID)
3677                 maxjobs = PRINT_MAX_JOBID - 1;
3678
3679         return maxjobs;
3680 }
3681
3682 struct smb_iconv_handle *lpcfg_iconv_handle(struct loadparm_context *lp_ctx)
3683 {
3684         if (lp_ctx == NULL) {
3685                 return get_iconv_handle();
3686         }
3687         return lp_ctx->iconv_handle;
3688 }
3689
3690 _PUBLIC_ void reload_charcnv(struct loadparm_context *lp_ctx)
3691 {
3692         struct smb_iconv_handle *old_ic = lp_ctx->iconv_handle;
3693         if (!lp_ctx->global) {
3694                 return;
3695         }
3696
3697         if (old_ic == NULL) {
3698                 old_ic = global_iconv_handle;
3699         }
3700         lp_ctx->iconv_handle = smb_iconv_handle_reinit_lp(lp_ctx, lp_ctx, old_ic);
3701         global_iconv_handle = lp_ctx->iconv_handle;
3702 }
3703
3704 _PUBLIC_ char *lpcfg_tls_keyfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3705 {
3706         return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_keyfile);
3707 }
3708
3709 _PUBLIC_ char *lpcfg_tls_certfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3710 {
3711         return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_certfile);
3712 }
3713
3714 _PUBLIC_ char *lpcfg_tls_cafile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3715 {
3716         return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_cafile);
3717 }
3718
3719 _PUBLIC_ char *lpcfg_tls_crlfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3720 {
3721         return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_crlfile);
3722 }
3723
3724 _PUBLIC_ char *lpcfg_tls_dhpfile(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3725 {
3726         return lpcfg_private_path(mem_ctx, lp_ctx, lp_ctx->globals->tls_dhpfile);
3727 }
3728
3729 struct gensec_settings *lpcfg_gensec_settings(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
3730 {
3731         struct gensec_settings *settings = talloc_zero(mem_ctx, struct gensec_settings);
3732         if (settings == NULL)
3733                 return NULL;
3734         SMB_ASSERT(lp_ctx != NULL);
3735         settings->lp_ctx = talloc_reference(settings, lp_ctx);
3736         settings->target_hostname = lpcfg_parm_string(lp_ctx, NULL, "gensec", "target_hostname");
3737         return settings;
3738 }
3739
3740 int lpcfg_server_role(struct loadparm_context *lp_ctx)
3741 {
3742         if (lp_ctx->s3_fns) {
3743                 return lp_ctx->s3_fns->server_role();
3744         }
3745
3746         return lp_find_server_role(lp_ctx->globals->server_role,
3747                                    lp_ctx->globals->security,
3748                                    lp_ctx->globals->domain_logons,
3749                                    (lp_ctx->globals->domain_master == true) ||
3750                                    (lp_ctx->globals->domain_master == Auto));
3751 }
3752
3753 int lpcfg_security(struct loadparm_context *lp_ctx)
3754 {
3755         if (lp_ctx->s3_fns) {
3756                 return lp_ctx->s3_fns->security();
3757         }
3758
3759         return lp_find_security(lp_ctx->globals->server_role,
3760                                 lp_ctx->globals->security);
3761 }