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