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