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