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