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