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