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