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