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