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