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