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