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