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