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