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