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