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