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