docs-xml: add 'kdc enable fast' option
[samba.git] / source3 / 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) Michael Adam 2008
13    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
14    Copyright (C) Andrew Bartlett 2011
15
16    This program is free software; you can redistribute it and/or modify
17    it under the terms of the GNU General Public License as published by
18    the Free Software Foundation; either version 3 of the License, or
19    (at your option) any later version.
20
21    This program is distributed in the hope that it will be useful,
22    but WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24    GNU General Public License for more details.
25
26    You should have received a copy of the GNU General Public License
27    along with this program.  If not, see <http://www.gnu.org/licenses/>.
28 */
29
30 /*
31  *  Load parameters.
32  *
33  *  This module provides suitable callback functions for the params
34  *  module. It builds the internal table of service details which is
35  *  then used by the rest of the server.
36  *
37  * To add a parameter:
38  *
39  * 1) add it to the global or service structure definition
40  * 2) add it to the parm_table
41  * 3) add it to the list of available functions (eg: using FN_GLOBAL_STRING())
42  * 4) If it's a global then initialise it in init_globals. If a local
43  *    (ie. service) parameter then initialise it in the sDefault structure
44  *
45  *
46  * Notes:
47  *   The configuration file is processed sequentially for speed. It is NOT
48  *   accessed randomly as happens in 'real' Windows. For this reason, there
49  *   is a fair bit of sequence-dependent code here - ie., code which assumes
50  *   that certain things happen before others. In particular, the code which
51  *   happens at the boundary between sections is delicately poised, so be
52  *   careful!
53  *
54  */
55
56 #define LOADPARM_SUBSTITUTION_INTERNALS 1
57 #include "includes.h"
58 #include "system/filesys.h"
59 #include "util_tdb.h"
60 #include "lib/param/loadparm.h"
61 #include "lib/param/param.h"
62 #include "printing.h"
63 #include "lib/smbconf/smbconf.h"
64 #include "lib/smbconf/smbconf_init.h"
65
66 #include "include/smb_ldap.h"
67 #include "../librpc/gen_ndr/svcctl.h"
68 #include "intl.h"
69 #include "../libcli/smb/smb_signing.h"
70 #include "dbwrap/dbwrap.h"
71 #include "dbwrap/dbwrap_rbt.h"
72 #include "../lib/util/bitmap.h"
73 #include "librpc/gen_ndr/nbt.h"
74 #include "source4/lib/tls/tls.h"
75 #include "libcli/auth/ntlm_check.h"
76 #include "lib/crypto/gnutls_helpers.h"
77 #include "lib/util/string_wrappers.h"
78 #include "auth/credentials/credentials.h"
79 #include "source3/lib/substitute.h"
80
81 #ifdef HAVE_SYS_SYSCTL_H
82 #include <sys/sysctl.h>
83 #endif
84
85 bool bLoaded = false;
86
87 extern userdom_struct current_user_info;
88
89 /* the special value for the include parameter
90  * to be interpreted not as a file name but to
91  * trigger loading of the global smb.conf options
92  * from registry. */
93 #ifndef INCLUDE_REGISTRY_NAME
94 #define INCLUDE_REGISTRY_NAME "registry"
95 #endif
96
97 static bool in_client = false;          /* Not in the client by default */
98 static struct smbconf_csn conf_last_csn;
99
100 static int config_backend = CONFIG_BACKEND_FILE;
101
102 /* some helpful bits */
103 #define LP_SNUM_OK(i) (((i) >= 0) && ((i) < iNumServices) && \
104                        (ServicePtrs != NULL) && \
105                        (ServicePtrs[(i)] != NULL) && ServicePtrs[(i)]->valid)
106 #define VALID(i) ((ServicePtrs != NULL) && (ServicePtrs[i]!= NULL) && \
107                   ServicePtrs[i]->valid)
108
109 #define USERSHARE_VALID 1
110 #define USERSHARE_PENDING_DELETE 2
111
112 static bool defaults_saved = false;
113
114 #include "lib/param/param_global.h"
115
116 static struct loadparm_global Globals;
117
118 /* This is a default service used to prime a services structure */
119 static const struct loadparm_service _sDefault =
120 {
121         .valid = true,
122         .autoloaded = false,
123         .usershare = 0,
124         .usershare_last_mod = {0, 0},
125         .szService = NULL,
126         .path = NULL,
127         .invalid_users = NULL,
128         .valid_users = NULL,
129         .admin_users = NULL,
130         .copy = NULL,
131         .include = NULL,
132         .preexec = NULL,
133         .postexec = NULL,
134         .root_preexec = NULL,
135         .root_postexec = NULL,
136         .cups_options = NULL,
137         .print_command = NULL,
138         .lpq_command = NULL,
139         .lprm_command = NULL,
140         .lppause_command = NULL,
141         .lpresume_command = NULL,
142         .queuepause_command = NULL,
143         .queueresume_command = NULL,
144         ._printername = NULL,
145         .printjob_username = NULL,
146         .dont_descend = NULL,
147         .hosts_allow = NULL,
148         .hosts_deny = NULL,
149         .magic_script = NULL,
150         .magic_output = NULL,
151         .veto_files = NULL,
152         .hide_files = NULL,
153         .veto_oplock_files = NULL,
154         .comment = NULL,
155         .force_user = NULL,
156         .force_group = NULL,
157         .read_list = NULL,
158         .write_list = NULL,
159         .volume = NULL,
160         .fstype = NULL,
161         .vfs_objects = NULL,
162         .msdfs_proxy = NULL,
163         .aio_write_behind = NULL,
164         .dfree_command = NULL,
165         .min_print_space = 0,
166         .max_print_jobs = 1000,
167         .max_reported_print_jobs = 0,
168         .create_mask = 0744,
169         .force_create_mode = 0,
170         .directory_mask = 0755,
171         .force_directory_mode = 0,
172         .max_connections = 0,
173         .default_case = CASE_LOWER,
174         .printing = DEFAULT_PRINTING,
175         .csc_policy = 0,
176         .block_size = 1024,
177         .dfree_cache_time = 0,
178         .preexec_close = false,
179         .root_preexec_close = false,
180         .case_sensitive = Auto,
181         .preserve_case = true,
182         .short_preserve_case = true,
183         .hide_dot_files = true,
184         .hide_special_files = false,
185         .hide_unreadable = false,
186         .hide_unwriteable_files = false,
187         .browseable = true,
188         .access_based_share_enum = false,
189         .available = true,
190         .read_only = true,
191         .spotlight = false,
192         .guest_only = false,
193         .administrative_share = false,
194         .guest_ok = false,
195         .printable = false,
196         .print_notify_backchannel = false,
197         .map_system = false,
198         .map_hidden = false,
199         .map_archive = true,
200         .store_dos_attributes = true,
201         .smbd_max_xattr_size = 65536,
202         .dmapi_support = false,
203         .locking = true,
204         .strict_locking = Auto,
205         .posix_locking = true,
206         .oplocks = true,
207         .kernel_oplocks = false,
208         .level2_oplocks = true,
209         .mangled_names = MANGLED_NAMES_ILLEGAL,
210         .wide_links = false,
211         .follow_symlinks = true,
212         .sync_always = false,
213         .strict_allocate = false,
214         .strict_rename = false,
215         .strict_sync = true,
216         .mangling_char = '~',
217         .copymap = NULL,
218         .delete_readonly = false,
219         .fake_oplocks = false,
220         .delete_veto_files = false,
221         .dos_filemode = false,
222         .dos_filetimes = true,
223         .dos_filetime_resolution = false,
224         .fake_directory_create_times = false,
225         .blocking_locks = true,
226         .inherit_permissions = false,
227         .inherit_acls = false,
228         .inherit_owner = false,
229         .msdfs_root = false,
230         .msdfs_shuffle_referrals = false,
231         .use_client_driver = false,
232         .default_devmode = true,
233         .force_printername = false,
234         .nt_acl_support = true,
235         .force_unknown_acl_user = false,
236         ._use_sendfile = false,
237         .map_acl_inherit = false,
238         .afs_share = false,
239         .ea_support = true,
240         .acl_check_permissions = true,
241         .acl_map_full_control = true,
242         .acl_group_control = false,
243         .acl_allow_execute_always = false,
244         .acl_flag_inherited_canonicalization = true,
245         .aio_read_size = 1,
246         .aio_write_size = 1,
247         .map_readonly = MAP_READONLY_NO,
248         .directory_name_cache_size = 100,
249         .server_smb_encrypt = SMB_ENCRYPTION_DEFAULT,
250         .kernel_share_modes = false,
251         .durable_handles = true,
252         .check_parent_directory_delete_on_close = false,
253         .param_opt = NULL,
254         .smbd_search_ask_sharemode = true,
255         .smbd_getinfo_ask_sharemode = true,
256         .spotlight_backend = SPOTLIGHT_BACKEND_NOINDEX,
257         .honor_change_notify_privilege = false,
258         .dummy = ""
259 };
260
261 /*
262  * This is a copy of the default service structure. Service options in the
263  * global section would otherwise overwrite the initial default values.
264  */
265 static struct loadparm_service sDefault;
266
267 /* local variables */
268 static struct loadparm_service **ServicePtrs = NULL;
269 static int iNumServices = 0;
270 static int iServiceIndex = 0;
271 static struct db_context *ServiceHash;
272 static bool bInGlobalSection = true;
273 static bool bGlobalOnly = false;
274 static struct file_lists *file_lists = NULL;
275 static unsigned int *flags_list = NULL;
276
277 static void set_allowed_client_auth(void);
278
279 static bool lp_set_cmdline_helper(const char *pszParmName, const char *pszParmValue);
280 static void free_param_opts(struct parmlist_entry **popts);
281
282 /**
283  *  Function to return the default value for the maximum number of open
284  *  file descriptors permitted.  This function tries to consult the
285  *  kernel-level (sysctl) and ulimit (getrlimit()) values and goes
286  *  the smaller of those.
287  */
288 static int max_open_files(void)
289 {
290         int sysctl_max = MAX_OPEN_FILES;
291         int rlimit_max = MAX_OPEN_FILES;
292
293 #ifdef HAVE_SYSCTLBYNAME
294         {
295                 size_t size = sizeof(sysctl_max);
296                 sysctlbyname("kern.maxfilesperproc", &sysctl_max, &size, NULL,
297                              0);
298         }
299 #endif
300
301 #if (defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE))
302         {
303                 struct rlimit rl;
304
305                 ZERO_STRUCT(rl);
306
307                 if (getrlimit(RLIMIT_NOFILE, &rl) == 0)
308                         rlimit_max = rl.rlim_cur;
309
310 #if defined(RLIM_INFINITY)
311                 if(rl.rlim_cur == RLIM_INFINITY)
312                         rlimit_max = MAX_OPEN_FILES;
313 #endif
314         }
315 #endif
316
317         if (sysctl_max < MIN_OPEN_FILES_WINDOWS) {
318                 DEBUG(2,("max_open_files: increasing sysctl_max (%d) to "
319                         "minimum Windows limit (%d)\n",
320                         sysctl_max,
321                         MIN_OPEN_FILES_WINDOWS));
322                 sysctl_max = MIN_OPEN_FILES_WINDOWS;
323         }
324
325         if (rlimit_max < MIN_OPEN_FILES_WINDOWS) {
326                 DEBUG(2,("rlimit_max: increasing rlimit_max (%d) to "
327                         "minimum Windows limit (%d)\n",
328                         rlimit_max,
329                         MIN_OPEN_FILES_WINDOWS));
330                 rlimit_max = MIN_OPEN_FILES_WINDOWS;
331         }
332
333         return MIN(sysctl_max, rlimit_max);
334 }
335
336 /**
337  * Common part of freeing allocated data for one parameter.
338  */
339 static void free_one_parameter_common(void *parm_ptr,
340                                       struct parm_struct parm)
341 {
342         if ((parm.type == P_STRING) ||
343             (parm.type == P_USTRING))
344         {
345                 lpcfg_string_free((char**)parm_ptr);
346         } else if (parm.type == P_LIST || parm.type == P_CMDLIST) {
347                 TALLOC_FREE(*((char***)parm_ptr));
348         }
349 }
350
351 /**
352  * Free the allocated data for one parameter for a share
353  * given as a service struct.
354  */
355 static void free_one_parameter(struct loadparm_service *service,
356                                struct parm_struct parm)
357 {
358         void *parm_ptr;
359
360         if (parm.p_class != P_LOCAL) {
361                 return;
362         }
363
364         parm_ptr = lp_parm_ptr(service, &parm);
365
366         free_one_parameter_common(parm_ptr, parm);
367 }
368
369 /**
370  * Free the allocated parameter data of a share given
371  * as a service struct.
372  */
373 static void free_parameters(struct loadparm_service *service)
374 {
375         uint32_t i;
376
377         for (i=0; parm_table[i].label; i++) {
378                 free_one_parameter(service, parm_table[i]);
379         }
380 }
381
382 /**
383  * Free the allocated data for one parameter for a given share
384  * specified by an snum.
385  */
386 static void free_one_parameter_by_snum(int snum, struct parm_struct parm)
387 {
388         void *parm_ptr;
389
390         if (snum < 0) {
391                 parm_ptr = lp_parm_ptr(NULL, &parm);
392         } else if (parm.p_class != P_LOCAL) {
393                 return;
394         } else {
395                 parm_ptr = lp_parm_ptr(ServicePtrs[snum], &parm);
396         }
397
398         free_one_parameter_common(parm_ptr, parm);
399 }
400
401 /**
402  * Free the allocated parameter data for a share specified
403  * by an snum.
404  */
405 static void free_parameters_by_snum(int snum)
406 {
407         uint32_t i;
408
409         for (i=0; parm_table[i].label; i++) {
410                 free_one_parameter_by_snum(snum, parm_table[i]);
411         }
412 }
413
414 /**
415  * Free the allocated global parameters.
416  */
417 static void free_global_parameters(void)
418 {
419         uint32_t i;
420         struct parm_struct *parm;
421
422         free_param_opts(&Globals.param_opt);
423         free_parameters_by_snum(GLOBAL_SECTION_SNUM);
424
425         /* Reset references in the defaults because the context is going to be freed */
426         for (i=0; parm_table[i].label; i++) {
427                 parm = &parm_table[i];
428                 if ((parm->type == P_STRING) ||
429                     (parm->type == P_USTRING)) {
430                         if ((parm->def.svalue != NULL) &&
431                             (*(parm->def.svalue) != '\0')) {
432                                 if (talloc_parent(parm->def.svalue) == Globals.ctx) {
433                                         parm->def.svalue = NULL;
434                                 }
435                         }
436                 }
437         }
438         TALLOC_FREE(Globals.ctx);
439 }
440
441 struct lp_stored_option {
442         struct lp_stored_option *prev, *next;
443         const char *label;
444         const char *value;
445 };
446
447 static struct lp_stored_option *stored_options;
448
449 /*
450   save options set by lp_set_cmdline() into a list. This list is
451   re-applied when we do a globals reset, so that cmdline set options
452   are sticky across reloads of smb.conf
453  */
454 bool store_lp_set_cmdline(const char *pszParmName, const char *pszParmValue)
455 {
456         struct lp_stored_option *entry, *entry_next;
457         for (entry = stored_options; entry != NULL; entry = entry_next) {
458                 entry_next = entry->next;
459                 if (strcmp(pszParmName, entry->label) == 0) {
460                         DLIST_REMOVE(stored_options, entry);
461                         talloc_free(entry);
462                         break;
463                 }
464         }
465
466         entry = talloc(NULL, struct lp_stored_option);
467         if (!entry) {
468                 return false;
469         }
470
471         entry->label = talloc_strdup(entry, pszParmName);
472         if (!entry->label) {
473                 talloc_free(entry);
474                 return false;
475         }
476
477         entry->value = talloc_strdup(entry, pszParmValue);
478         if (!entry->value) {
479                 talloc_free(entry);
480                 return false;
481         }
482
483         DLIST_ADD_END(stored_options, entry);
484
485         return true;
486 }
487
488 static bool apply_lp_set_cmdline(void)
489 {
490         struct lp_stored_option *entry = NULL;
491         for (entry = stored_options; entry != NULL; entry = entry->next) {
492                 if (!lp_set_cmdline_helper(entry->label, entry->value)) {
493                         DEBUG(0, ("Failed to re-apply cmdline parameter %s = %s\n",
494                                   entry->label, entry->value));
495                         return false;
496                 }
497         }
498         return true;
499 }
500
501 /***************************************************************************
502  Initialise the global parameter structure.
503 ***************************************************************************/
504
505 static void init_globals(struct loadparm_context *lp_ctx, bool reinit_globals)
506 {
507         static bool done_init = false;
508         char *s = NULL;
509         int i;
510
511         /* If requested to initialize only once and we've already done it... */
512         if (!reinit_globals && done_init) {
513                 /* ... then we have nothing more to do */
514                 return;
515         }
516
517         if (!done_init) {
518                 /* The logfile can be set before this is invoked. Free it if so. */
519                 lpcfg_string_free(&Globals.logfile);
520                 done_init = true;
521         } else {
522                 free_global_parameters();
523         }
524
525         /* This memset and the free_global_parameters() above will
526          * wipe out smb.conf options set with lp_set_cmdline().  The
527          * apply_lp_set_cmdline() call puts these values back in the
528          * table once the defaults are set */
529         ZERO_STRUCT(Globals);
530
531         Globals.ctx = talloc_pooled_object(NULL, char, 272, 2048);
532
533         /* Initialize the flags list if necessary */
534         if (flags_list == NULL) {
535                 get_flags();
536         }
537
538         for (i = 0; parm_table[i].label; i++) {
539                 if ((parm_table[i].type == P_STRING ||
540                      parm_table[i].type == P_USTRING))
541                 {
542                         lpcfg_string_set(
543                                 Globals.ctx,
544                                 (char **)lp_parm_ptr(NULL, &parm_table[i]),
545                                 "");
546                 }
547         }
548
549
550         lpcfg_string_set(Globals.ctx, &sDefault.fstype, FSTYPE_STRING);
551         lpcfg_string_set(Globals.ctx, &sDefault.printjob_username, "%U");
552
553         init_printer_values(lp_ctx, Globals.ctx, &sDefault);
554
555         sDefault.ntvfs_handler = str_list_make_v3_const(Globals.ctx, "unixuid default", NULL);
556
557         DEBUG(3, ("Initialising global parameters\n"));
558
559         /* Must manually force to upper case here, as this does not go via the handler */
560         lpcfg_string_set(Globals.ctx, &Globals.netbios_name,
561                          myhostname_upper());
562
563         lpcfg_string_set(Globals.ctx, &Globals.smb_passwd_file,
564                          get_dyn_SMB_PASSWD_FILE());
565         lpcfg_string_set(Globals.ctx, &Globals.private_dir,
566                          get_dyn_PRIVATE_DIR());
567         lpcfg_string_set(Globals.ctx, &Globals.binddns_dir,
568                          get_dyn_BINDDNS_DIR());
569
570         /* use the new 'hash2' method by default, with a prefix of 1 */
571         lpcfg_string_set(Globals.ctx, &Globals.mangling_method, "hash2");
572         Globals.mangle_prefix = 1;
573
574         lpcfg_string_set(Globals.ctx, &Globals.guest_account, GUEST_ACCOUNT);
575
576         /* using UTF8 by default allows us to support all chars */
577         lpcfg_string_set(Globals.ctx, &Globals.unix_charset,
578                          DEFAULT_UNIX_CHARSET);
579
580         /* Use codepage 850 as a default for the dos character set */
581         lpcfg_string_set(Globals.ctx, &Globals.dos_charset,
582                          DEFAULT_DOS_CHARSET);
583
584         /*
585          * Allow the default PASSWD_CHAT to be overridden in local.h.
586          */
587         lpcfg_string_set(Globals.ctx, &Globals.passwd_chat,
588                          DEFAULT_PASSWD_CHAT);
589
590         lpcfg_string_set(Globals.ctx, &Globals.workgroup, DEFAULT_WORKGROUP);
591
592         lpcfg_string_set(Globals.ctx, &Globals.passwd_program, "");
593         lpcfg_string_set(Globals.ctx, &Globals.lock_directory,
594                          get_dyn_LOCKDIR());
595         lpcfg_string_set(Globals.ctx, &Globals.state_directory,
596                          get_dyn_STATEDIR());
597         lpcfg_string_set(Globals.ctx, &Globals.cache_directory,
598                          get_dyn_CACHEDIR());
599         lpcfg_string_set(Globals.ctx, &Globals.pid_directory,
600                          get_dyn_PIDDIR());
601         lpcfg_string_set(Globals.ctx, &Globals.nbt_client_socket_address,
602                          "0.0.0.0");
603         /*
604          * By default support explicit binding to broadcast
605          * addresses.
606          */
607         Globals.nmbd_bind_explicit_broadcast = true;
608
609         s = talloc_asprintf(talloc_tos(), "Samba %s", samba_version_string());
610         if (s == NULL) {
611                 smb_panic("init_globals: ENOMEM");
612         }
613         lpcfg_string_set(Globals.ctx, &Globals.server_string, s);
614         TALLOC_FREE(s);
615 #ifdef DEVELOPER
616         lpcfg_string_set(Globals.ctx, &Globals.panic_action,
617                          "/bin/sleep 999999999");
618 #endif
619
620         lpcfg_string_set(Globals.ctx, &Globals.socket_options,
621                          DEFAULT_SOCKET_OPTIONS);
622
623         lpcfg_string_set(Globals.ctx, &Globals.logon_drive, "");
624         /* %N is the NIS auto.home server if -DAUTOHOME is used, else same as %L */
625         lpcfg_string_set(Globals.ctx, &Globals.logon_home, "\\\\%N\\%U");
626         lpcfg_string_set(Globals.ctx, &Globals.logon_path,
627                          "\\\\%N\\%U\\profile");
628
629         Globals.name_resolve_order =
630                         str_list_make_v3_const(Globals.ctx,
631                                                DEFAULT_NAME_RESOLVE_ORDER,
632                                                NULL);
633         lpcfg_string_set(Globals.ctx, &Globals.password_server, "*");
634
635         Globals.algorithmic_rid_base = BASE_RID;
636
637         Globals.load_printers = true;
638         Globals.printcap_cache_time = 750;      /* 12.5 minutes */
639
640         Globals.config_backend = config_backend;
641         Globals._server_role = ROLE_AUTO;
642
643         /* Was 65535 (0xFFFF). 0x4101 matches W2K and causes major speed improvements... */
644         /* Discovered by 2 days of pain by Don McCall @ HP :-). */
645         Globals.max_xmit = 0x4104;
646         Globals.max_mux = 50;   /* This is *needed* for profile support. */
647         Globals.lpq_cache_time = 30;    /* changed to handle large print servers better -- jerry */
648         Globals._disable_spoolss = false;
649         Globals.max_smbd_processes = 0;/* no limit specified */
650         Globals.username_level = 0;
651         Globals.deadtime = 10080;
652         Globals.getwd_cache = true;
653         Globals.large_readwrite = true;
654         Globals.max_log_size = 5000;
655         Globals.max_open_files = max_open_files();
656         Globals.server_max_protocol = PROTOCOL_SMB3_11;
657         Globals.server_min_protocol = PROTOCOL_SMB2_02;
658         Globals._client_max_protocol = PROTOCOL_DEFAULT;
659         Globals.client_min_protocol = PROTOCOL_SMB2_02;
660         Globals._client_ipc_max_protocol = PROTOCOL_DEFAULT;
661         Globals._client_ipc_min_protocol = PROTOCOL_DEFAULT;
662         Globals._security = SEC_AUTO;
663         Globals.encrypt_passwords = true;
664         Globals.client_schannel = true;
665         Globals.winbind_sealed_pipes = true;
666         Globals.require_strong_key = true;
667         Globals.server_schannel = true;
668         Globals.read_raw = true;
669         Globals.write_raw = true;
670         Globals.null_passwords = false;
671         Globals.old_password_allowed_period = 60;
672         Globals.obey_pam_restrictions = false;
673         Globals.syslog = 1;
674         Globals.syslog_only = false;
675         Globals.timestamp_logs = true;
676         lpcfg_string_set(Globals.ctx, &Globals.log_level, "0");
677         Globals.debug_prefix_timestamp = false;
678         Globals.debug_hires_timestamp = true;
679         Globals.debug_syslog_format = false;
680         Globals.debug_pid = false;
681         Globals.debug_uid = false;
682         Globals.debug_class = false;
683         Globals.enable_core_files = true;
684         Globals.max_ttl = 60 * 60 * 24 * 3;     /* 3 days default. */
685         Globals.max_wins_ttl = 60 * 60 * 24 * 6;        /* 6 days default. */
686         Globals.min_wins_ttl = 60 * 60 * 6;     /* 6 hours default. */
687         Globals.machine_password_timeout = 60 * 60 * 24 * 7;    /* 7 days default. */
688         Globals.lm_announce = Auto;     /* = Auto: send only if LM clients found */
689         Globals.lm_interval = 60;
690         Globals.time_server = false;
691         Globals.bind_interfaces_only = false;
692         Globals.unix_password_sync = false;
693         Globals.pam_password_change = false;
694         Globals.passwd_chat_debug = false;
695         Globals.passwd_chat_timeout = 2; /* 2 second default. */
696         Globals.nt_pipe_support = true; /* Do NT pipes by default. */
697         Globals.nt_status_support = true; /* Use NT status by default. */
698         Globals.smbd_profiling_level = 0;
699         Globals.stat_cache = true;      /* use stat cache by default */
700         Globals.max_stat_cache_size = 512; /* 512k by default */
701         Globals.restrict_anonymous = 0;
702         Globals.client_lanman_auth = false;     /* Do NOT use the LanMan hash if it is available */
703         Globals.client_plaintext_auth = false;  /* Do NOT use a plaintext password even if is requested by the server */
704         Globals._lanman_auth = false;   /* Do NOT use the LanMan hash, even if it is supplied */
705         Globals.ntlm_auth = NTLM_AUTH_NTLMV2_ONLY;      /* Do NOT use NTLMv1 if it is supplied by the client (otherwise NTLMv2) */
706         Globals.raw_ntlmv2_auth = false; /* Reject NTLMv2 without NTLMSSP */
707         Globals.client_ntlmv2_auth = true; /* Client should always use use NTLMv2, as we can't tell that the server supports it, but most modern servers do */
708         /* Note, that we will also use NTLM2 session security (which is different), if it is available */
709
710         Globals.allow_dcerpc_auth_level_connect = false; /* we don't allow this by default */
711
712         Globals.map_to_guest = 0;       /* By Default, "Never" */
713         Globals.oplock_break_wait_time = 0;     /* By Default, 0 msecs. */
714         Globals.enhanced_browsing = true;
715         Globals.lock_spin_time = WINDOWS_MINIMUM_LOCK_TIMEOUT_MS; /* msec. */
716         Globals.use_mmap = true;
717         Globals.unicode = true;
718         Globals.smb1_unix_extensions = true;
719         Globals.reset_on_zero_vc = false;
720         Globals.log_writeable_files_on_exit = false;
721         Globals.create_krb5_conf = true;
722         Globals.include_system_krb5_conf = true;
723         Globals._winbind_max_domain_connections = 1;
724
725         /* hostname lookups can be very expensive and are broken on
726            a large number of sites (tridge) */
727         Globals.hostname_lookups = false;
728
729         Globals.change_notify = true,
730         Globals.kernel_change_notify = true,
731
732         lpcfg_string_set(Globals.ctx, &Globals.passdb_backend, "tdbsam");
733         lpcfg_string_set(Globals.ctx, &Globals.ldap_suffix, "");
734         lpcfg_string_set(Globals.ctx, &Globals._ldap_machine_suffix, "");
735         lpcfg_string_set(Globals.ctx, &Globals._ldap_user_suffix, "");
736         lpcfg_string_set(Globals.ctx, &Globals._ldap_group_suffix, "");
737         lpcfg_string_set(Globals.ctx, &Globals._ldap_idmap_suffix, "");
738
739         lpcfg_string_set(Globals.ctx, &Globals.ldap_admin_dn, "");
740         Globals.ldap_ssl = LDAP_SSL_START_TLS;
741         Globals.ldap_deref = -1;
742         Globals.ldap_passwd_sync = LDAP_PASSWD_SYNC_OFF;
743         Globals.ldap_delete_dn = false;
744         Globals.ldap_replication_sleep = 1000; /* wait 1 sec for replication */
745         Globals.ldap_follow_referral = Auto;
746         Globals.ldap_timeout = LDAP_DEFAULT_TIMEOUT;
747         Globals.ldap_connection_timeout = LDAP_CONNECTION_DEFAULT_TIMEOUT;
748         Globals.ldap_page_size = LDAP_PAGE_SIZE;
749
750         Globals.ldap_debug_level = 0;
751         Globals.ldap_debug_threshold = 10;
752
753         Globals.client_ldap_sasl_wrapping = ADS_AUTH_SASL_SIGN;
754
755         Globals.ldap_server_require_strong_auth =
756                 LDAP_SERVER_REQUIRE_STRONG_AUTH_YES;
757
758         /* This is what we tell the afs client. in reality we set the token
759          * to never expire, though, when this runs out the afs client will
760          * forget the token. Set to 0 to get NEVERDATE.*/
761         Globals.afs_token_lifetime = 604800;
762         Globals.cups_connection_timeout = CUPS_DEFAULT_CONNECTION_TIMEOUT;
763
764 /* these parameters are set to defaults that are more appropriate
765    for the increasing samba install base:
766
767    as a member of the workgroup, that will possibly become a
768    _local_ master browser (lm = true).  this is opposed to a forced
769    local master browser startup (pm = true).
770
771    doesn't provide WINS server service by default (wsupp = false),
772    and doesn't provide domain master browser services by default, either.
773
774 */
775
776         Globals.show_add_printer_wizard = true;
777         Globals.os_level = 20;
778         Globals.local_master = true;
779         Globals._domain_master = Auto;  /* depending on _domain_logons */
780         Globals._domain_logons = false;
781         Globals.browse_list = true;
782         Globals.we_are_a_wins_server = false;
783         Globals.wins_proxy = false;
784
785         TALLOC_FREE(Globals.init_logon_delayed_hosts);
786         Globals.init_logon_delay = 100; /* 100 ms default delay */
787
788         Globals.wins_dns_proxy = true;
789
790         Globals.allow_trusted_domains = true;
791         lpcfg_string_set(Globals.ctx, &Globals.idmap_backend, "tdb");
792
793         lpcfg_string_set(Globals.ctx, &Globals.template_shell, "/bin/false");
794         lpcfg_string_set(Globals.ctx, &Globals.template_homedir,
795                          "/home/%D/%U");
796         lpcfg_string_set(Globals.ctx, &Globals.winbind_separator, "\\");
797         lpcfg_string_set(Globals.ctx, &Globals.winbindd_socket_directory,
798                          dyn_WINBINDD_SOCKET_DIR);
799
800         lpcfg_string_set(Globals.ctx, &Globals.cups_server, "");
801         lpcfg_string_set(Globals.ctx, &Globals.iprint_server, "");
802
803         lpcfg_string_set(Globals.ctx, &Globals._ctdbd_socket, "");
804
805         Globals.cluster_addresses = NULL;
806         Globals.clustering = false;
807         Globals.ctdb_timeout = 0;
808         Globals.ctdb_locktime_warn_threshold = 0;
809
810         Globals.winbind_cache_time = 300;       /* 5 minutes */
811         Globals.winbind_reconnect_delay = 30;   /* 30 seconds */
812         Globals.winbind_request_timeout = 60;   /* 60 seconds */
813         Globals.winbind_max_clients = 200;
814         Globals.winbind_enum_users = false;
815         Globals.winbind_enum_groups = false;
816         Globals.winbind_use_default_domain = false;
817         Globals.winbind_nested_groups = true;
818         Globals.winbind_expand_groups = 0;
819         Globals.winbind_nss_info = str_list_make_v3_const(NULL, "template", NULL);
820         Globals.winbind_refresh_tickets = false;
821         Globals.winbind_offline_logon = false;
822         Globals.winbind_scan_trusted_domains = false;
823
824         Globals.idmap_cache_time = 86400 * 7; /* a week by default */
825         Globals.idmap_negative_cache_time = 120; /* 2 minutes by default */
826
827         Globals.passdb_expand_explicit = false;
828
829         Globals.name_cache_timeout = 660; /* In seconds */
830
831         Globals.client_use_spnego = true;
832
833         Globals.client_signing = SMB_SIGNING_DEFAULT;
834         Globals._client_ipc_signing = SMB_SIGNING_DEFAULT;
835         Globals.server_signing = SMB_SIGNING_DEFAULT;
836
837         Globals.defer_sharing_violations = true;
838         Globals.smb_ports = str_list_make_v3_const(NULL, SMB_PORTS, NULL);
839
840         Globals.enable_privileges = true;
841         Globals.host_msdfs        = true;
842         Globals.enable_asu_support       = false;
843
844         /* User defined shares. */
845         s = talloc_asprintf(talloc_tos(), "%s/usershares", get_dyn_STATEDIR());
846         if (s == NULL) {
847                 smb_panic("init_globals: ENOMEM");
848         }
849         lpcfg_string_set(Globals.ctx, &Globals.usershare_path, s);
850         TALLOC_FREE(s);
851         lpcfg_string_set(Globals.ctx, &Globals.usershare_template_share, "");
852         Globals.usershare_max_shares = 0;
853         /* By default disallow sharing of directories not owned by the sharer. */
854         Globals.usershare_owner_only = true;
855         /* By default disallow guest access to usershares. */
856         Globals.usershare_allow_guests = false;
857
858         Globals.keepalive = DEFAULT_KEEPALIVE;
859
860         /* By default no shares out of the registry */
861         Globals.registry_shares = false;
862
863         Globals.min_receivefile_size = 0;
864
865         Globals.multicast_dns_register = true;
866
867         Globals.smb2_max_read = DEFAULT_SMB2_MAX_READ;
868         Globals.smb2_max_write = DEFAULT_SMB2_MAX_WRITE;
869         Globals.smb2_max_trans = DEFAULT_SMB2_MAX_TRANSACT;
870         Globals.smb2_max_credits = DEFAULT_SMB2_MAX_CREDITS;
871         Globals.smb2_leases = true;
872         Globals.server_multi_channel_support = true;
873
874         lpcfg_string_set(Globals.ctx, &Globals.ncalrpc_dir,
875                          get_dyn_NCALRPCDIR());
876
877         Globals.server_services = str_list_make_v3_const(NULL, "s3fs rpc nbt wrepl ldap cldap kdc drepl winbindd ntp_signd kcc dnsupdate dns", NULL);
878
879         Globals.dcerpc_endpoint_servers = str_list_make_v3_const(NULL, "epmapper wkssvc rpcecho samr netlogon lsarpc drsuapi dssetup unixinfo browser eventlog6 backupkey dnsserver", NULL);
880
881         Globals.tls_enabled = true;
882         Globals.tls_verify_peer = TLS_VERIFY_PEER_AS_STRICT_AS_POSSIBLE;
883
884         lpcfg_string_set(Globals.ctx, &Globals._tls_keyfile, "tls/key.pem");
885         lpcfg_string_set(Globals.ctx, &Globals._tls_certfile, "tls/cert.pem");
886         lpcfg_string_set(Globals.ctx, &Globals._tls_cafile, "tls/ca.pem");
887         lpcfg_string_set(Globals.ctx,
888                          &Globals.tls_priority,
889                          "NORMAL:-VERS-SSL3.0");
890
891         Globals._preferred_master = Auto;
892
893         Globals.allow_dns_updates = DNS_UPDATE_SIGNED;
894         Globals.dns_zone_scavenging = false;
895
896         lpcfg_string_set(Globals.ctx, &Globals.ntp_signd_socket_directory,
897                          get_dyn_NTP_SIGND_SOCKET_DIR());
898
899         s = talloc_asprintf(talloc_tos(), "%s/samba_kcc", get_dyn_SCRIPTSBINDIR());
900         if (s == NULL) {
901                 smb_panic("init_globals: ENOMEM");
902         }
903         Globals.samba_kcc_command = str_list_make_v3_const(NULL, s, NULL);
904         TALLOC_FREE(s);
905
906 #ifdef MIT_KDC_PATH
907         Globals.mit_kdc_command = str_list_make_v3_const(NULL, MIT_KDC_PATH, NULL);
908 #endif
909
910         s = talloc_asprintf(talloc_tos(), "%s/samba_dnsupdate", get_dyn_SCRIPTSBINDIR());
911         if (s == NULL) {
912                 smb_panic("init_globals: ENOMEM");
913         }
914         Globals.dns_update_command = str_list_make_v3_const(NULL, s, NULL);
915         TALLOC_FREE(s);
916
917         s = talloc_asprintf(talloc_tos(), "%s/samba-gpupdate", get_dyn_SCRIPTSBINDIR());
918         if (s == NULL) {
919                 smb_panic("init_globals: ENOMEM");
920         }
921         Globals.gpo_update_command = str_list_make_v3_const(NULL, s, NULL);
922         TALLOC_FREE(s);
923
924         Globals.apply_group_policies = false;
925
926         s = talloc_asprintf(talloc_tos(), "%s/samba_spnupdate", get_dyn_SCRIPTSBINDIR());
927         if (s == NULL) {
928                 smb_panic("init_globals: ENOMEM");
929         }
930         Globals.spn_update_command = str_list_make_v3_const(NULL, s, NULL);
931         TALLOC_FREE(s);
932
933         Globals.nsupdate_command = str_list_make_v3_const(NULL, "/usr/bin/nsupdate -g", NULL);
934
935         Globals.cldap_port = 389;
936
937         Globals.dgram_port = NBT_DGRAM_SERVICE_PORT;
938
939         Globals.nbt_port = NBT_NAME_SERVICE_PORT;
940
941         Globals.krb5_port = 88;
942
943         Globals.kpasswd_port = 464;
944
945         Globals.kdc_enable_fast = true;
946
947         Globals.aio_max_threads = 100;
948
949         lpcfg_string_set(Globals.ctx,
950                          &Globals.rpc_server_dynamic_port_range,
951                          "49152-65535");
952         Globals.rpc_low_port = SERVER_TCP_LOW_PORT;
953         Globals.rpc_high_port = SERVER_TCP_HIGH_PORT;
954         Globals.prefork_children = 4;
955         Globals.prefork_backoff_increment = 10;
956         Globals.prefork_maximum_backoff = 120;
957
958         Globals.ldap_max_anonymous_request_size = 256000;
959         Globals.ldap_max_authenticated_request_size = 16777216;
960         Globals.ldap_max_search_request_size = 256000;
961
962         /* Async DNS query timeout (in seconds). */
963         Globals.async_dns_timeout = 10;
964
965         Globals.client_smb_encrypt = SMB_ENCRYPTION_DEFAULT;
966
967         Globals._client_use_kerberos = CRED_USE_KERBEROS_DESIRED;
968
969         Globals.client_protection = CRED_CLIENT_PROTECTION_DEFAULT;
970
971         Globals.winbind_use_krb5_enterprise_principals = true;
972
973         Globals.client_smb3_signing_algorithms =
974                 str_list_make_v3_const(NULL, DEFAULT_SMB3_SIGNING_ALGORITHMS, NULL);
975         Globals.server_smb3_signing_algorithms =
976                 str_list_make_v3_const(NULL, DEFAULT_SMB3_SIGNING_ALGORITHMS, NULL);
977
978         Globals.client_smb3_encryption_algorithms =
979                 str_list_make_v3_const(NULL, DEFAULT_SMB3_ENCRYPTION_ALGORITHMS, NULL);
980         Globals.server_smb3_encryption_algorithms =
981                 str_list_make_v3_const(NULL, DEFAULT_SMB3_ENCRYPTION_ALGORITHMS, NULL);
982
983         Globals.min_domain_uid = 1000;
984
985         /*
986          * By default allow smbd and winbindd to start samba-dcerpcd as
987          * a named-pipe helper.
988          */
989         Globals.rpc_start_on_demand_helpers = true;
990
991         /* Now put back the settings that were set with lp_set_cmdline() */
992         apply_lp_set_cmdline();
993 }
994
995 /* Convenience routine to setup an lp_context with additional s3 variables */
996 static struct loadparm_context *setup_lp_context(TALLOC_CTX *mem_ctx)
997 {
998         struct loadparm_context *lp_ctx;
999
1000         lp_ctx = loadparm_init_s3(mem_ctx,
1001                                   loadparm_s3_helpers());
1002         if (lp_ctx == NULL) {
1003                 DEBUG(0, ("loadparm_init_s3 failed\n"));
1004                 return NULL;
1005         }
1006
1007         lp_ctx->sDefault = talloc_zero(lp_ctx, struct loadparm_service);
1008         if (lp_ctx->sDefault == NULL) {
1009                 DBG_ERR("talloc_zero failed\n");
1010                 TALLOC_FREE(lp_ctx);
1011                 return NULL;
1012         }
1013
1014         *lp_ctx->sDefault = _sDefault;
1015         lp_ctx->services = NULL; /* We do not want to access this directly */
1016         lp_ctx->bInGlobalSection = bInGlobalSection;
1017         lp_ctx->flags = flags_list;
1018
1019         return lp_ctx;
1020 }
1021
1022 /*******************************************************************
1023  Convenience routine to grab string parameters into talloced memory
1024  and run standard_sub_basic on them. The buffers can be written to by
1025  callers without affecting the source string.
1026 ********************************************************************/
1027
1028 static char *loadparm_s3_global_substitution_fn(
1029                         TALLOC_CTX *mem_ctx,
1030                         const struct loadparm_substitution *lp_sub,
1031                         const char *s,
1032                         void *private_data)
1033 {
1034         char *ret;
1035
1036         /* The follow debug is useful for tracking down memory problems
1037            especially if you have an inner loop that is calling a lp_*()
1038            function that returns a string.  Perhaps this debug should be
1039            present all the time? */
1040
1041 #if 0
1042         DEBUG(10, ("lp_string(%s)\n", s));
1043 #endif
1044         if (!s) {
1045                 return NULL;
1046         }
1047
1048         ret = talloc_sub_basic(mem_ctx,
1049                         get_current_username(),
1050                         current_user_info.domain,
1051                         s);
1052         if (trim_char(ret, '\"', '\"')) {
1053                 if (strchr(ret,'\"') != NULL) {
1054                         TALLOC_FREE(ret);
1055                         ret = talloc_sub_basic(mem_ctx,
1056                                         get_current_username(),
1057                                         current_user_info.domain,
1058                                         s);
1059                 }
1060         }
1061         return ret;
1062 }
1063
1064 static const struct loadparm_substitution s3_global_substitution = {
1065         .substituted_string_fn = loadparm_s3_global_substitution_fn,
1066 };
1067
1068 const struct loadparm_substitution *loadparm_s3_global_substitution(void)
1069 {
1070         return &s3_global_substitution;
1071 }
1072
1073 /*
1074    In this section all the functions that are used to access the
1075    parameters from the rest of the program are defined
1076 */
1077
1078 #define FN_GLOBAL_SUBSTITUTED_STRING(fn_name,ptr) \
1079 char *lp_ ## fn_name(TALLOC_CTX *ctx, const struct loadparm_substitution *lp_sub) \
1080  {return lpcfg_substituted_string(ctx, lp_sub, *(char **)(&Globals.ptr) ? *(char **)(&Globals.ptr) : "");}
1081 #define FN_GLOBAL_CONST_STRING(fn_name,ptr) \
1082  const char *lp_ ## fn_name(void) {return(*(const char * const *)(&Globals.ptr) ? *(const char * const *)(&Globals.ptr) : "");}
1083 #define FN_GLOBAL_LIST(fn_name,ptr) \
1084  const char **lp_ ## fn_name(void) {return(*(const char ***)(&Globals.ptr));}
1085 #define FN_GLOBAL_BOOL(fn_name,ptr) \
1086  bool lp_ ## fn_name(void) {return(*(bool *)(&Globals.ptr));}
1087 #define FN_GLOBAL_CHAR(fn_name,ptr) \
1088  char lp_ ## fn_name(void) {return(*(char *)(&Globals.ptr));}
1089 #define FN_GLOBAL_INTEGER(fn_name,ptr) \
1090  int lp_ ## fn_name(void) {return(*(int *)(&Globals.ptr));}
1091
1092 #define FN_LOCAL_SUBSTITUTED_STRING(fn_name,val) \
1093 char *lp_ ## fn_name(TALLOC_CTX *ctx, const struct loadparm_substitution *lp_sub, int i) \
1094  {return lpcfg_substituted_string((ctx), lp_sub, (LP_SNUM_OK(i) && ServicePtrs[(i)]->val) ? ServicePtrs[(i)]->val : sDefault.val);}
1095 #define FN_LOCAL_CONST_STRING(fn_name,val) \
1096  const char *lp_ ## fn_name(int i) {return (const char *)((LP_SNUM_OK(i) && ServicePtrs[(i)]->val) ? ServicePtrs[(i)]->val : sDefault.val);}
1097 #define FN_LOCAL_LIST(fn_name,val) \
1098  const char **lp_ ## fn_name(int i) {return(const char **)(LP_SNUM_OK(i)? ServicePtrs[(i)]->val : sDefault.val);}
1099 #define FN_LOCAL_BOOL(fn_name,val) \
1100  bool lp_ ## fn_name(int i) {return(bool)(LP_SNUM_OK(i)? ServicePtrs[(i)]->val : sDefault.val);}
1101 #define FN_LOCAL_INTEGER(fn_name,val) \
1102  int lp_ ## fn_name(int i) {return(LP_SNUM_OK(i)? ServicePtrs[(i)]->val : sDefault.val);}
1103
1104 #define FN_LOCAL_PARM_BOOL(fn_name,val) \
1105  bool lp_ ## fn_name(const struct share_params *p) {return(bool)(LP_SNUM_OK(p->service)? ServicePtrs[(p->service)]->val : sDefault.val);}
1106 #define FN_LOCAL_PARM_INTEGER(fn_name,val) \
1107  int lp_ ## fn_name(const struct share_params *p) {return(LP_SNUM_OK(p->service)? ServicePtrs[(p->service)]->val : sDefault.val);}
1108 #define FN_LOCAL_PARM_CHAR(fn_name,val) \
1109  char lp_ ## fn_name(const struct share_params *p) {return(LP_SNUM_OK(p->service)? ServicePtrs[(p->service)]->val : sDefault.val);}
1110
1111 int lp_winbind_max_domain_connections(void)
1112 {
1113         if (lp_winbind_offline_logon() &&
1114             lp__winbind_max_domain_connections() > 1) {
1115                 DEBUG(1, ("offline logons active, restricting max domain "
1116                           "connections to 1\n"));
1117                 return 1;
1118         }
1119         return MAX(1, lp__winbind_max_domain_connections());
1120 }
1121
1122 /* These functions remain in source3/param for now */
1123
1124 #include "lib/param/param_functions.c"
1125
1126 FN_LOCAL_SUBSTITUTED_STRING(servicename, szService)
1127 FN_LOCAL_CONST_STRING(const_servicename, szService)
1128
1129 /* These functions cannot be auto-generated */
1130 FN_LOCAL_BOOL(autoloaded, autoloaded)
1131 FN_GLOBAL_CONST_STRING(dnsdomain, dnsdomain)
1132
1133 /* local prototypes */
1134
1135 static int map_parameter_canonical(const char *pszParmName, bool *inverse);
1136 static const char *get_boolean(bool bool_value);
1137 static bool do_parameter(const char *pszParmName, const char *pszParmValue,
1138                          void *userdata);
1139 static bool hash_a_service(const char *name, int number);
1140 static void free_service_byindex(int iService);
1141 static void show_parameter(int parmIndex);
1142 static bool is_synonym_of(int parm1, int parm2, bool *inverse);
1143 static bool lp_parameter_value_is_valid(const char *parm_name, const char *val);
1144
1145 /*
1146  * This is a helper function for parametrical options support.  It returns a
1147  * pointer to parametrical option value if it exists or NULL otherwise. Actual
1148  * parametrical functions are quite simple
1149  */
1150 static struct parmlist_entry *get_parametrics(int snum, const char *type,
1151                                                 const char *option)
1152 {
1153         if (snum >= iNumServices) return NULL;
1154
1155         if (snum < 0) {
1156                 return get_parametric_helper(NULL, type, option, Globals.param_opt);
1157         } else {
1158                 return get_parametric_helper(ServicePtrs[snum],
1159                                              type, option, Globals.param_opt);
1160         }
1161 }
1162
1163 static void discard_whitespace(char *str)
1164 {
1165         size_t len = strlen(str);
1166         size_t i = 0;
1167
1168         while (i < len) {
1169                 if (isspace(str[i])) {
1170                         memmove(&str[i], &str[i+1], len-i);
1171                         len -= 1;
1172                         continue;
1173                 }
1174                 i += 1;
1175         }
1176 }
1177
1178 /**
1179  * @brief Go through all global parametric parameters
1180  *
1181  * @param regex_str     A regular expression to scan param for
1182  * @param max_matches   Max number of submatches the regexp expects
1183  * @param cb            Function to call on match. Should return true
1184  *                      when it wants wi_scan_global_parametrics to stop
1185  *                      scanning
1186  * @param private_data  Anonymous pointer passed to cb
1187  *
1188  * @return              0: success, regcomp/regexec return value on error.
1189  *                      See "man regexec" for possible errors
1190  */
1191
1192 int lp_wi_scan_global_parametrics(
1193         const char *regex_str, size_t max_matches,
1194         bool (*cb)(const char *string, regmatch_t matches[],
1195                    void *private_data),
1196         void *private_data)
1197 {
1198         struct parmlist_entry *data;
1199         regex_t regex;
1200         int ret;
1201
1202         ret = regcomp(&regex, regex_str, REG_ICASE);
1203         if (ret != 0) {
1204                 return ret;
1205         }
1206
1207         for (data = Globals.param_opt; data != NULL; data = data->next) {
1208                 size_t keylen = strlen(data->key);
1209                 char key[keylen+1];
1210                 regmatch_t matches[max_matches];
1211                 bool stop;
1212
1213                 memcpy(key, data->key, sizeof(key));
1214                 discard_whitespace(key);
1215
1216                 ret = regexec(&regex, key, max_matches, matches, 0);
1217                 if (ret == REG_NOMATCH) {
1218                         continue;
1219                 }
1220                 if (ret != 0) {
1221                         goto fail;
1222                 }
1223
1224                 stop = cb(key, matches, private_data);
1225                 if (stop) {
1226                         break;
1227                 }
1228         }
1229
1230         ret = 0;
1231 fail:
1232         regfree(&regex);
1233         return ret;
1234 }
1235
1236
1237 #define MISSING_PARAMETER(name) \
1238     DEBUG(0, ("%s(): value is NULL or empty!\n", #name))
1239
1240 /*******************************************************************
1241 convenience routine to return enum parameters.
1242 ********************************************************************/
1243 static int lp_enum(const char *s,const struct enum_list *_enum)
1244 {
1245         int i;
1246
1247         if (!s || !*s || !_enum) {
1248                 MISSING_PARAMETER(lp_enum);
1249                 return (-1);
1250         }
1251
1252         for (i=0; _enum[i].name; i++) {
1253                 if (strequal(_enum[i].name,s))
1254                         return _enum[i].value;
1255         }
1256
1257         DEBUG(0,("lp_enum(%s,enum): value is not in enum_list!\n",s));
1258         return (-1);
1259 }
1260
1261 #undef MISSING_PARAMETER
1262
1263 /* Return parametric option from a given service. Type is a part of option before ':' */
1264 /* Parametric option has following syntax: 'Type: option = value' */
1265 char *lp_parm_substituted_string(TALLOC_CTX *mem_ctx,
1266                                  const struct loadparm_substitution *lp_sub,
1267                                  int snum,
1268                                  const char *type,
1269                                  const char *option,
1270                                  const char *def)
1271 {
1272         struct parmlist_entry *data = get_parametrics(snum, type, option);
1273
1274         SMB_ASSERT(lp_sub != NULL);
1275
1276         if (data == NULL||data->value==NULL) {
1277                 if (def) {
1278                         return lpcfg_substituted_string(mem_ctx, lp_sub, def);
1279                 } else {
1280                         return NULL;
1281                 }
1282         }
1283
1284         return lpcfg_substituted_string(mem_ctx, lp_sub, data->value);
1285 }
1286
1287 /* Return parametric option from a given service. Type is a part of option before ':' */
1288 /* Parametric option has following syntax: 'Type: option = value' */
1289 const char *lp_parm_const_string(int snum, const char *type, const char *option, const char *def)
1290 {
1291         struct parmlist_entry *data = get_parametrics(snum, type, option);
1292
1293         if (data == NULL||data->value==NULL)
1294                 return def;
1295
1296         return data->value;
1297 }
1298
1299
1300 /* Return parametric option from a given service. Type is a part of option before ':' */
1301 /* Parametric option has following syntax: 'Type: option = value' */
1302
1303 const char **lp_parm_string_list(int snum, const char *type, const char *option, const char **def)
1304 {
1305         struct parmlist_entry *data = get_parametrics(snum, type, option);
1306
1307         if (data == NULL||data->value==NULL)
1308                 return (const char **)def;
1309
1310         if (data->list==NULL) {
1311                 data->list = str_list_make_v3(NULL, data->value, NULL);
1312         }
1313
1314         return discard_const_p(const char *, data->list);
1315 }
1316
1317 /* Return parametric option from a given service. Type is a part of option before ':' */
1318 /* Parametric option has following syntax: 'Type: option = value' */
1319
1320 int lp_parm_int(int snum, const char *type, const char *option, int def)
1321 {
1322         struct parmlist_entry *data = get_parametrics(snum, type, option);
1323
1324         if (data && data->value && *data->value)
1325                 return lp_int(data->value);
1326
1327         return def;
1328 }
1329
1330 /* Return parametric option from a given service. Type is a part of option before ':' */
1331 /* Parametric option has following syntax: 'Type: option = value' */
1332
1333 unsigned long lp_parm_ulong(int snum, const char *type, const char *option, unsigned long def)
1334 {
1335         struct parmlist_entry *data = get_parametrics(snum, type, option);
1336
1337         if (data && data->value && *data->value)
1338                 return lp_ulong(data->value);
1339
1340         return def;
1341 }
1342
1343 /* Return parametric option from a given service. Type is a part of option before ':' */
1344 /* Parametric option has following syntax: 'Type: option = value' */
1345
1346 unsigned long long lp_parm_ulonglong(int snum, const char *type,
1347                                      const char *option, unsigned long long def)
1348 {
1349         struct parmlist_entry *data = get_parametrics(snum, type, option);
1350
1351         if (data && data->value && *data->value) {
1352                 return lp_ulonglong(data->value);
1353         }
1354
1355         return def;
1356 }
1357
1358 /* Return parametric option from a given service. Type is a part of option
1359  * before ':' */
1360 /* Parametric option has following syntax: 'Type: option = value' */
1361
1362 bool lp_parm_bool(int snum, const char *type, const char *option, bool def)
1363 {
1364         struct parmlist_entry *data = get_parametrics(snum, type, option);
1365
1366         if (data && data->value && *data->value)
1367                 return lp_bool(data->value);
1368
1369         return def;
1370 }
1371
1372 /* Return parametric option from a given service. Type is a part of option before ':' */
1373 /* Parametric option has following syntax: 'Type: option = value' */
1374
1375 int lp_parm_enum(int snum, const char *type, const char *option,
1376                  const struct enum_list *_enum, int def)
1377 {
1378         struct parmlist_entry *data = get_parametrics(snum, type, option);
1379
1380         if (data && data->value && *data->value && _enum)
1381                 return lp_enum(data->value, _enum);
1382
1383         return def;
1384 }
1385
1386 /**
1387  * free a param_opts structure.
1388  * param_opts handling should be moved to talloc;
1389  * then this whole functions reduces to a TALLOC_FREE().
1390  */
1391
1392 static void free_param_opts(struct parmlist_entry **popts)
1393 {
1394         struct parmlist_entry *opt, *next_opt;
1395
1396         if (*popts != NULL) {
1397                 DEBUG(5, ("Freeing parametrics:\n"));
1398         }
1399         opt = *popts;
1400         while (opt != NULL) {
1401                 lpcfg_string_free(&opt->key);
1402                 lpcfg_string_free(&opt->value);
1403                 TALLOC_FREE(opt->list);
1404                 next_opt = opt->next;
1405                 TALLOC_FREE(opt);
1406                 opt = next_opt;
1407         }
1408         *popts = NULL;
1409 }
1410
1411 /***************************************************************************
1412  Free the dynamically allocated parts of a service struct.
1413 ***************************************************************************/
1414
1415 static void free_service(struct loadparm_service *pservice)
1416 {
1417         if (!pservice)
1418                 return;
1419
1420         if (pservice->szService)
1421                 DEBUG(5, ("free_service: Freeing service %s\n",
1422                        pservice->szService));
1423
1424         free_parameters(pservice);
1425
1426         lpcfg_string_free(&pservice->szService);
1427         TALLOC_FREE(pservice->copymap);
1428
1429         free_param_opts(&pservice->param_opt);
1430
1431         ZERO_STRUCTP(pservice);
1432 }
1433
1434
1435 /***************************************************************************
1436  remove a service indexed in the ServicePtrs array from the ServiceHash
1437  and free the dynamically allocated parts
1438 ***************************************************************************/
1439
1440 static void free_service_byindex(int idx)
1441 {
1442         if ( !LP_SNUM_OK(idx) )
1443                 return;
1444
1445         ServicePtrs[idx]->valid = false;
1446
1447         /* we have to cleanup the hash record */
1448
1449         if (ServicePtrs[idx]->szService) {
1450                 char *canon_name = canonicalize_servicename(
1451                         talloc_tos(),
1452                         ServicePtrs[idx]->szService );
1453
1454                 dbwrap_delete_bystring(ServiceHash, canon_name );
1455                 TALLOC_FREE(canon_name);
1456         }
1457
1458         free_service(ServicePtrs[idx]);
1459         TALLOC_FREE(ServicePtrs[idx]);
1460 }
1461
1462 /***************************************************************************
1463  Add a new service to the services array initialising it with the given
1464  service.
1465 ***************************************************************************/
1466
1467 static int add_a_service(const struct loadparm_service *pservice, const char *name)
1468 {
1469         int i;
1470         struct loadparm_service **tsp = NULL;
1471
1472         /* it might already exist */
1473         if (name) {
1474                 i = getservicebyname(name, NULL);
1475                 if (i >= 0) {
1476                         return (i);
1477                 }
1478         }
1479
1480         /* Re use empty slots if any before allocating new one.*/
1481         for (i=0; i < iNumServices; i++) {
1482                 if (ServicePtrs[i] == NULL) {
1483                         break;
1484                 }
1485         }
1486         if (i == iNumServices) {
1487                 /* if not, then create one */
1488                 tsp = talloc_realloc(NULL, ServicePtrs,
1489                                      struct loadparm_service *,
1490                                      iNumServices + 1);
1491                 if (tsp == NULL) {
1492                         DEBUG(0, ("add_a_service: failed to enlarge "
1493                                   "ServicePtrs!\n"));
1494                         return (-1);
1495                 }
1496                 ServicePtrs = tsp;
1497                 iNumServices++;
1498         }
1499         ServicePtrs[i] = talloc_zero(ServicePtrs, struct loadparm_service);
1500         if (!ServicePtrs[i]) {
1501                 DEBUG(0,("add_a_service: out of memory!\n"));
1502                 return (-1);
1503         }
1504
1505         ServicePtrs[i]->valid = true;
1506
1507         copy_service(ServicePtrs[i], pservice, NULL);
1508         if (name)
1509                 lpcfg_string_set(ServicePtrs[i], &ServicePtrs[i]->szService,
1510                                  name);
1511
1512         DEBUG(8,("add_a_service: Creating snum = %d for %s\n",
1513                 i, ServicePtrs[i]->szService));
1514
1515         if (!hash_a_service(ServicePtrs[i]->szService, i)) {
1516                 return (-1);
1517         }
1518
1519         return (i);
1520 }
1521
1522 /***************************************************************************
1523   Convert a string to uppercase and remove whitespaces.
1524 ***************************************************************************/
1525
1526 char *canonicalize_servicename(TALLOC_CTX *ctx, const char *src)
1527 {
1528         char *result;
1529
1530         if ( !src ) {
1531                 DEBUG(0,("canonicalize_servicename: NULL source name!\n"));
1532                 return NULL;
1533         }
1534
1535         result = talloc_strdup(ctx, src);
1536         SMB_ASSERT(result != NULL);
1537
1538         if (!strlower_m(result)) {
1539                 TALLOC_FREE(result);
1540                 return NULL;
1541         }
1542         return result;
1543 }
1544
1545 /***************************************************************************
1546   Add a name/index pair for the services array to the hash table.
1547 ***************************************************************************/
1548
1549 static bool hash_a_service(const char *name, int idx)
1550 {
1551         char *canon_name;
1552
1553         if ( !ServiceHash ) {
1554                 DEBUG(10,("hash_a_service: creating servicehash\n"));
1555                 ServiceHash = db_open_rbt(NULL);
1556                 if ( !ServiceHash ) {
1557                         DEBUG(0,("hash_a_service: open tdb servicehash failed!\n"));
1558                         return false;
1559                 }
1560         }
1561
1562         DEBUG(10,("hash_a_service: hashing index %d for service name %s\n",
1563                 idx, name));
1564
1565         canon_name = canonicalize_servicename(talloc_tos(), name );
1566
1567         dbwrap_store_bystring(ServiceHash, canon_name,
1568                               make_tdb_data((uint8_t *)&idx, sizeof(idx)),
1569                               TDB_REPLACE);
1570
1571         TALLOC_FREE(canon_name);
1572
1573         return true;
1574 }
1575
1576 /***************************************************************************
1577  Add a new home service, with the specified home directory, defaults coming
1578  from service ifrom.
1579 ***************************************************************************/
1580
1581 bool lp_add_home(const char *pszHomename, int iDefaultService,
1582                  const char *user, const char *pszHomedir)
1583 {
1584         const struct loadparm_substitution *lp_sub =
1585                 loadparm_s3_global_substitution();
1586         int i;
1587         char *global_path;
1588
1589         if (pszHomename == NULL || user == NULL || pszHomedir == NULL ||
1590                         pszHomedir[0] == '\0') {
1591                 return false;
1592         }
1593
1594         i = add_a_service(ServicePtrs[iDefaultService], pszHomename);
1595
1596         if (i < 0)
1597                 return false;
1598
1599         global_path = lp_path(talloc_tos(), lp_sub, GLOBAL_SECTION_SNUM);
1600         if (!(*(ServicePtrs[iDefaultService]->path))
1601             || strequal(ServicePtrs[iDefaultService]->path, global_path)) {
1602                 lpcfg_string_set(ServicePtrs[i], &ServicePtrs[i]->path,
1603                                  pszHomedir);
1604         }
1605         TALLOC_FREE(global_path);
1606
1607         if (!(*(ServicePtrs[i]->comment))) {
1608                 char *comment = talloc_asprintf(talloc_tos(), "Home directory of %s", user);
1609                 if (comment == NULL) {
1610                         return false;
1611                 }
1612                 lpcfg_string_set(ServicePtrs[i], &ServicePtrs[i]->comment,
1613                                  comment);
1614                 TALLOC_FREE(comment);
1615         }
1616
1617         /* set the browseable flag from the global default */
1618
1619         ServicePtrs[i]->browseable = sDefault.browseable;
1620         ServicePtrs[i]->access_based_share_enum = sDefault.access_based_share_enum;
1621
1622         ServicePtrs[i]->autoloaded = true;
1623
1624         DEBUG(3, ("adding home's share [%s] for user '%s' at '%s'\n", pszHomename,
1625                user, ServicePtrs[i]->path ));
1626
1627         return true;
1628 }
1629
1630 /***************************************************************************
1631  Add a new service, based on an old one.
1632 ***************************************************************************/
1633
1634 int lp_add_service(const char *pszService, int iDefaultService)
1635 {
1636         if (iDefaultService < 0) {
1637                 return add_a_service(&sDefault, pszService);
1638         }
1639
1640         return (add_a_service(ServicePtrs[iDefaultService], pszService));
1641 }
1642
1643 /***************************************************************************
1644  Add the IPC service.
1645 ***************************************************************************/
1646
1647 static bool lp_add_ipc(const char *ipc_name, bool guest_ok)
1648 {
1649         char *comment = NULL;
1650         int i = add_a_service(&sDefault, ipc_name);
1651
1652         if (i < 0)
1653                 return false;
1654
1655         comment = talloc_asprintf(talloc_tos(), "IPC Service (%s)",
1656                                   Globals.server_string);
1657         if (comment == NULL) {
1658                 return false;
1659         }
1660
1661         lpcfg_string_set(ServicePtrs[i], &ServicePtrs[i]->path, tmpdir());
1662         lpcfg_string_set(ServicePtrs[i], &ServicePtrs[i]->comment, comment);
1663         lpcfg_string_set(ServicePtrs[i], &ServicePtrs[i]->fstype, "IPC");
1664         ServicePtrs[i]->max_connections = 0;
1665         ServicePtrs[i]->available = true;
1666         ServicePtrs[i]->read_only = true;
1667         ServicePtrs[i]->guest_only = false;
1668         ServicePtrs[i]->administrative_share = true;
1669         ServicePtrs[i]->guest_ok = guest_ok;
1670         ServicePtrs[i]->printable = false;
1671         ServicePtrs[i]->browseable = sDefault.browseable;
1672         ServicePtrs[i]->autoloaded = false;
1673
1674         DEBUG(3, ("adding IPC service\n"));
1675
1676         TALLOC_FREE(comment);
1677         return true;
1678 }
1679
1680 /***************************************************************************
1681  Add a new printer service, with defaults coming from service iFrom.
1682 ***************************************************************************/
1683
1684 bool lp_add_printer(const char *pszPrintername, int iDefaultService)
1685 {
1686         const char *comment = "From Printcap";
1687         int i = add_a_service(ServicePtrs[iDefaultService], pszPrintername);
1688
1689         if (i < 0)
1690                 return false;
1691
1692         /* note that we do NOT default the availability flag to true - */
1693         /* we take it from the default service passed. This allows all */
1694         /* dynamic printers to be disabled by disabling the [printers] */
1695         /* entry (if/when the 'available' keyword is implemented!).    */
1696
1697         /* the printer name is set to the service name. */
1698         lpcfg_string_set(ServicePtrs[i], &ServicePtrs[i]->_printername,
1699                          pszPrintername);
1700         lpcfg_string_set(ServicePtrs[i], &ServicePtrs[i]->comment, comment);
1701
1702         /* set the browseable flag from the gloabl default */
1703         ServicePtrs[i]->browseable = sDefault.browseable;
1704
1705         /* Printers cannot be read_only. */
1706         ServicePtrs[i]->read_only = false;
1707         /* No oplocks on printer services. */
1708         ServicePtrs[i]->oplocks = false;
1709         /* Printer services must be printable. */
1710         ServicePtrs[i]->printable = true;
1711
1712         DEBUG(3, ("adding printer service %s\n", pszPrintername));
1713
1714         return true;
1715 }
1716
1717
1718 /***************************************************************************
1719  Check whether the given parameter name is valid.
1720  Parametric options (names containing a colon) are considered valid.
1721 ***************************************************************************/
1722
1723 bool lp_parameter_is_valid(const char *pszParmName)
1724 {
1725         return ((lpcfg_map_parameter(pszParmName) != -1) ||
1726                 (strchr(pszParmName, ':') != NULL));
1727 }
1728
1729 /***************************************************************************
1730  Check whether the given name is the name of a global parameter.
1731  Returns true for strings belonging to parameters of class
1732  P_GLOBAL, false for all other strings, also for parametric options
1733  and strings not belonging to any option.
1734 ***************************************************************************/
1735
1736 bool lp_parameter_is_global(const char *pszParmName)
1737 {
1738         int num = lpcfg_map_parameter(pszParmName);
1739
1740         if (num >= 0) {
1741                 return (parm_table[num].p_class == P_GLOBAL);
1742         }
1743
1744         return false;
1745 }
1746
1747 /**************************************************************************
1748  Determine the canonical name for a parameter.
1749  Indicate when it is an inverse (boolean) synonym instead of a
1750  "usual" synonym.
1751 **************************************************************************/
1752
1753 bool lp_canonicalize_parameter(const char *parm_name, const char **canon_parm,
1754                                bool *inverse)
1755 {
1756         int num;
1757
1758         if (!lp_parameter_is_valid(parm_name)) {
1759                 *canon_parm = NULL;
1760                 return false;
1761         }
1762
1763         num = map_parameter_canonical(parm_name, inverse);
1764         if (num < 0) {
1765                 /* parametric option */
1766                 *canon_parm = parm_name;
1767         } else {
1768                 *canon_parm = parm_table[num].label;
1769         }
1770
1771         return true;
1772
1773 }
1774
1775 /**************************************************************************
1776  Determine the canonical name for a parameter.
1777  Turn the value given into the inverse boolean expression when
1778  the synonym is an invers boolean synonym.
1779
1780  Return true if
1781  - parm_name is a valid parameter name and
1782  - val is a valid value for this parameter and
1783  - in case the parameter is an inverse boolean synonym, if the val
1784    string could successfully be converted to the reverse bool.
1785  Return false in all other cases.
1786 **************************************************************************/
1787
1788 bool lp_canonicalize_parameter_with_value(const char *parm_name,
1789                                           const char *val,
1790                                           const char **canon_parm,
1791                                           const char **canon_val)
1792 {
1793         int num;
1794         bool inverse;
1795         bool ret;
1796
1797         if (!lp_parameter_is_valid(parm_name)) {
1798                 *canon_parm = NULL;
1799                 *canon_val = NULL;
1800                 return false;
1801         }
1802
1803         num = map_parameter_canonical(parm_name, &inverse);
1804         if (num < 0) {
1805                 /* parametric option */
1806                 *canon_parm = parm_name;
1807                 *canon_val = val;
1808                 return true;
1809         }
1810
1811         *canon_parm = parm_table[num].label;
1812         if (inverse) {
1813                 if (!lp_invert_boolean(val, canon_val)) {
1814                         *canon_val = NULL;
1815                         return false;
1816                 }
1817         } else {
1818                 *canon_val = val;
1819         }
1820
1821         ret = lp_parameter_value_is_valid(*canon_parm, *canon_val);
1822
1823         return ret;
1824 }
1825
1826 /***************************************************************************
1827  Map a parameter's string representation to the index of the canonical
1828  form of the parameter (it might be a synonym).
1829  Returns -1 if the parameter string is not recognised.
1830 ***************************************************************************/
1831
1832 static int map_parameter_canonical(const char *pszParmName, bool *inverse)
1833 {
1834         int parm_num, canon_num;
1835         bool loc_inverse = false;
1836
1837         parm_num = lpcfg_map_parameter(pszParmName);
1838         if ((parm_num < 0) || !(parm_table[parm_num].flags & FLAG_SYNONYM)) {
1839                 /* invalid, parametric or no canidate for synonyms ... */
1840                 goto done;
1841         }
1842
1843         for (canon_num = 0; parm_table[canon_num].label; canon_num++) {
1844                 if (is_synonym_of(parm_num, canon_num, &loc_inverse)) {
1845                         parm_num = canon_num;
1846                         goto done;
1847                 }
1848         }
1849
1850 done:
1851         if (inverse != NULL) {
1852                 *inverse = loc_inverse;
1853         }
1854         return parm_num;
1855 }
1856
1857 /***************************************************************************
1858  return true if parameter number parm1 is a synonym of parameter
1859  number parm2 (parm2 being the principal name).
1860  set inverse to true if parm1 is P_BOOLREV and parm2 is P_BOOL,
1861  false otherwise.
1862 ***************************************************************************/
1863
1864 static bool is_synonym_of(int parm1, int parm2, bool *inverse)
1865 {
1866         if ((parm_table[parm1].offset == parm_table[parm2].offset) &&
1867             (parm_table[parm1].p_class == parm_table[parm2].p_class) &&
1868             (parm_table[parm1].flags & FLAG_SYNONYM) &&
1869             !(parm_table[parm2].flags & FLAG_SYNONYM))
1870         {
1871                 if (inverse != NULL) {
1872                         if ((parm_table[parm1].type == P_BOOLREV) &&
1873                             (parm_table[parm2].type == P_BOOL))
1874                         {
1875                                 *inverse = true;
1876                         } else {
1877                                 *inverse = false;
1878                         }
1879                 }
1880                 return true;
1881         }
1882         return false;
1883 }
1884
1885 /***************************************************************************
1886  Show one parameter's name, type, [values,] and flags.
1887  (helper functions for show_parameter_list)
1888 ***************************************************************************/
1889
1890 static void show_parameter(int parmIndex)
1891 {
1892         size_t enumIndex, flagIndex;
1893         size_t parmIndex2;
1894         bool hadFlag;
1895         bool hadSyn;
1896         bool inverse;
1897         const char *type[] = { "P_BOOL", "P_BOOLREV", "P_CHAR", "P_INTEGER",
1898                 "P_OCTAL", "P_LIST", "P_STRING", "P_USTRING",
1899                 "P_ENUM", "P_BYTES", "P_CMDLIST" };
1900         unsigned flags[] = { FLAG_DEPRECATED, FLAG_SYNONYM };
1901         const char *flag_names[] = { "FLAG_DEPRECATED", "FLAG_SYNONYM", NULL};
1902
1903         printf("%s=%s", parm_table[parmIndex].label,
1904                type[parm_table[parmIndex].type]);
1905         if (parm_table[parmIndex].type == P_ENUM) {
1906                 printf(",");
1907                 for (enumIndex=0;
1908                      parm_table[parmIndex].enum_list[enumIndex].name;
1909                      enumIndex++)
1910                 {
1911                         printf("%s%s",
1912                                enumIndex ? "|" : "",
1913                                parm_table[parmIndex].enum_list[enumIndex].name);
1914                 }
1915         }
1916         printf(",");
1917         hadFlag = false;
1918         for (flagIndex=0; flag_names[flagIndex]; flagIndex++) {
1919                 if (parm_table[parmIndex].flags & flags[flagIndex]) {
1920                         printf("%s%s",
1921                                 hadFlag ? "|" : "",
1922                                 flag_names[flagIndex]);
1923                         hadFlag = true;
1924                 }
1925         }
1926
1927         /* output synonyms */
1928         hadSyn = false;
1929         for (parmIndex2=0; parm_table[parmIndex2].label; parmIndex2++) {
1930                 if (is_synonym_of(parmIndex, parmIndex2, &inverse)) {
1931                         printf(" (%ssynonym of %s)", inverse ? "inverse " : "",
1932                                parm_table[parmIndex2].label);
1933                 } else if (is_synonym_of(parmIndex2, parmIndex, &inverse)) {
1934                         if (!hadSyn) {
1935                                 printf(" (synonyms: ");
1936                                 hadSyn = true;
1937                         } else {
1938                                 printf(", ");
1939                         }
1940                         printf("%s%s", parm_table[parmIndex2].label,
1941                                inverse ? "[i]" : "");
1942                 }
1943         }
1944         if (hadSyn) {
1945                 printf(")");
1946         }
1947
1948         printf("\n");
1949 }
1950
1951 /*
1952  * Check the value for a P_ENUM
1953  */
1954 static bool check_enum_parameter(struct parm_struct *parm, const char *value)
1955 {
1956         int i;
1957
1958         for (i = 0; parm->enum_list[i].name; i++) {
1959                 if (strwicmp(value, parm->enum_list[i].name) == 0) {
1960                         return true;
1961                 }
1962         }
1963         return false;
1964 }
1965
1966 /**************************************************************************
1967  Check whether the given value is valid for the given parameter name.
1968 **************************************************************************/
1969
1970 static bool lp_parameter_value_is_valid(const char *parm_name, const char *val)
1971 {
1972         bool ret = false, tmp_bool;
1973         int num = lpcfg_map_parameter(parm_name), tmp_int;
1974         uint64_t tmp_int64 = 0;
1975         struct parm_struct *parm;
1976
1977         /* parametric options (parameter names containing a colon) cannot
1978            be checked and are therefore considered valid. */
1979         if (strchr(parm_name, ':') != NULL) {
1980                 return true;
1981         }
1982
1983         if (num >= 0) {
1984                 parm = &parm_table[num];
1985                 switch (parm->type) {
1986                         case P_BOOL:
1987                         case P_BOOLREV:
1988                                 ret = set_boolean(val, &tmp_bool);
1989                                 break;
1990
1991                         case P_INTEGER:
1992                                 ret = (sscanf(val, "%d", &tmp_int) == 1);
1993                                 break;
1994
1995                         case P_OCTAL:
1996                                 ret = (sscanf(val, "%o", &tmp_int) == 1);
1997                                 break;
1998
1999                         case P_ENUM:
2000                                 ret = check_enum_parameter(parm, val);
2001                                 break;
2002
2003                         case P_BYTES:
2004                                 if (conv_str_size_error(val, &tmp_int64) &&
2005                                     tmp_int64 <= INT_MAX) {
2006                                         ret = true;
2007                                 }
2008                                 break;
2009
2010                         case P_CHAR:
2011                         case P_LIST:
2012                         case P_STRING:
2013                         case P_USTRING:
2014                         case P_CMDLIST:
2015                                 ret = true;
2016                                 break;
2017                 }
2018         }
2019         return ret;
2020 }
2021
2022 /***************************************************************************
2023  Show all parameter's name, type, [values,] and flags.
2024 ***************************************************************************/
2025
2026 void show_parameter_list(void)
2027 {
2028         int classIndex, parmIndex;
2029         const char *section_names[] = { "local", "global", NULL};
2030
2031         for (classIndex=0; section_names[classIndex]; classIndex++) {
2032                 printf("[%s]\n", section_names[classIndex]);
2033                 for (parmIndex = 0; parm_table[parmIndex].label; parmIndex++) {
2034                         if (parm_table[parmIndex].p_class == classIndex) {
2035                                 show_parameter(parmIndex);
2036                         }
2037                 }
2038         }
2039 }
2040
2041 /***************************************************************************
2042  Get the standard string representation of a boolean value ("yes" or "no")
2043 ***************************************************************************/
2044
2045 static const char *get_boolean(bool bool_value)
2046 {
2047         static const char *yes_str = "yes";
2048         static const char *no_str = "no";
2049
2050         return (bool_value ? yes_str : no_str);
2051 }
2052
2053 /***************************************************************************
2054  Provide the string of the negated boolean value associated to the boolean
2055  given as a string. Returns false if the passed string does not correctly
2056  represent a boolean.
2057 ***************************************************************************/
2058
2059 bool lp_invert_boolean(const char *str, const char **inverse_str)
2060 {
2061         bool val;
2062
2063         if (!set_boolean(str, &val)) {
2064                 return false;
2065         }
2066
2067         *inverse_str = get_boolean(!val);
2068         return true;
2069 }
2070
2071 /***************************************************************************
2072  Provide the canonical string representation of a boolean value given
2073  as a string. Return true on success, false if the string given does
2074  not correctly represent a boolean.
2075 ***************************************************************************/
2076
2077 bool lp_canonicalize_boolean(const char *str, const char**canon_str)
2078 {
2079         bool val;
2080
2081         if (!set_boolean(str, &val)) {
2082                 return false;
2083         }
2084
2085         *canon_str = get_boolean(val);
2086         return true;
2087 }
2088
2089 /***************************************************************************
2090 Find a service by name. Otherwise works like get_service.
2091 ***************************************************************************/
2092
2093 int getservicebyname(const char *pszServiceName, struct loadparm_service *pserviceDest)
2094 {
2095         int iService = -1;
2096         char *canon_name;
2097         TDB_DATA data;
2098         NTSTATUS status;
2099
2100         if (ServiceHash == NULL) {
2101                 return -1;
2102         }
2103
2104         canon_name = canonicalize_servicename(talloc_tos(), pszServiceName);
2105
2106         status = dbwrap_fetch_bystring(ServiceHash, canon_name, canon_name,
2107                                        &data);
2108
2109         if (NT_STATUS_IS_OK(status) &&
2110             (data.dptr != NULL) &&
2111             (data.dsize == sizeof(iService)))
2112         {
2113                 memcpy(&iService, data.dptr, sizeof(iService));
2114         }
2115
2116         TALLOC_FREE(canon_name);
2117
2118         if ((iService != -1) && (LP_SNUM_OK(iService))
2119             && (pserviceDest != NULL)) {
2120                 copy_service(pserviceDest, ServicePtrs[iService], NULL);
2121         }
2122
2123         return (iService);
2124 }
2125
2126 /* Return a pointer to a service by name.  Unlike getservicebyname, it does not copy the service */
2127 struct loadparm_service *lp_service(const char *pszServiceName)
2128 {
2129         int iService = getservicebyname(pszServiceName, NULL);
2130         if (iService == -1 || !LP_SNUM_OK(iService)) {
2131                 return NULL;
2132         }
2133         return ServicePtrs[iService];
2134 }
2135
2136 struct loadparm_service *lp_servicebynum(int snum)
2137 {
2138         if ((snum == -1) || !LP_SNUM_OK(snum)) {
2139                 return NULL;
2140         }
2141         return ServicePtrs[snum];
2142 }
2143
2144 struct loadparm_service *lp_default_loadparm_service()
2145 {
2146         return &sDefault;
2147 }
2148
2149 static struct smbconf_ctx *lp_smbconf_ctx(void)
2150 {
2151         sbcErr err;
2152         static struct smbconf_ctx *conf_ctx = NULL;
2153
2154         if (conf_ctx == NULL) {
2155                 err = smbconf_init(NULL, &conf_ctx, "registry:");
2156                 if (!SBC_ERROR_IS_OK(err)) {
2157                         DEBUG(1, ("error initializing registry configuration: "
2158                                   "%s\n", sbcErrorString(err)));
2159                         conf_ctx = NULL;
2160                 }
2161         }
2162
2163         return conf_ctx;
2164 }
2165
2166 static bool process_smbconf_service(struct smbconf_service *service)
2167 {
2168         uint32_t count;
2169         bool ret;
2170
2171         if (service == NULL) {
2172                 return false;
2173         }
2174
2175         ret = lp_do_section(service->name, NULL);
2176         if (ret != true) {
2177                 return false;
2178         }
2179         for (count = 0; count < service->num_params; count++) {
2180
2181                 if (!bInGlobalSection && bGlobalOnly) {
2182                         ret = true;
2183                 } else {
2184                         const char *pszParmName = service->param_names[count];
2185                         const char *pszParmValue = service->param_values[count];
2186
2187                         DEBUGADD(4, ("doing parameter %s = %s\n", pszParmName, pszParmValue));
2188
2189                         ret = lp_do_parameter(bInGlobalSection ? -2 : iServiceIndex,
2190                                               pszParmName, pszParmValue);
2191                 }
2192
2193                 if (ret != true) {
2194                         return false;
2195                 }
2196         }
2197         if (iServiceIndex >= 0) {
2198                 return lpcfg_service_ok(ServicePtrs[iServiceIndex]);
2199         }
2200         return true;
2201 }
2202
2203 /**
2204  * load a service from registry and activate it
2205  */
2206 bool process_registry_service(const char *service_name)
2207 {
2208         sbcErr err;
2209         struct smbconf_service *service = NULL;
2210         TALLOC_CTX *mem_ctx = talloc_stackframe();
2211         struct smbconf_ctx *conf_ctx = lp_smbconf_ctx();
2212         bool ret = false;
2213
2214         if (conf_ctx == NULL) {
2215                 goto done;
2216         }
2217
2218         DEBUG(5, ("process_registry_service: service name %s\n", service_name));
2219
2220         if (!smbconf_share_exists(conf_ctx, service_name)) {
2221                 /*
2222                  * Registry does not contain data for this service (yet),
2223                  * but make sure lp_load doesn't return false.
2224                  */
2225                 ret = true;
2226                 goto done;
2227         }
2228
2229         err = smbconf_get_share(conf_ctx, mem_ctx, service_name, &service);
2230         if (!SBC_ERROR_IS_OK(err)) {
2231                 goto done;
2232         }
2233
2234         ret = process_smbconf_service(service);
2235         if (!ret) {
2236                 goto done;
2237         }
2238
2239         /* store the csn */
2240         smbconf_changed(conf_ctx, &conf_last_csn, NULL, NULL);
2241
2242 done:
2243         TALLOC_FREE(mem_ctx);
2244         return ret;
2245 }
2246
2247 /*
2248  * process_registry_globals
2249  */
2250 static bool process_registry_globals(void)
2251 {
2252         bool ret;
2253
2254         add_to_file_list(NULL, &file_lists, INCLUDE_REGISTRY_NAME, INCLUDE_REGISTRY_NAME);
2255
2256         if (!bInGlobalSection && bGlobalOnly) {
2257                 ret = true;
2258         } else {
2259                 const char *pszParmName = "registry shares";
2260                 const char *pszParmValue = "yes";
2261
2262                 DEBUGADD(4, ("doing parameter %s = %s\n", pszParmName, pszParmValue));
2263
2264                 ret = lp_do_parameter(bInGlobalSection ? -2 : iServiceIndex,
2265                                       pszParmName, pszParmValue);
2266         }
2267
2268         if (!ret) {
2269                 return ret;
2270         }
2271
2272         return process_registry_service(GLOBAL_NAME);
2273 }
2274
2275 bool process_registry_shares(void)
2276 {
2277         sbcErr err;
2278         uint32_t count;
2279         struct smbconf_service **service = NULL;
2280         uint32_t num_shares = 0;
2281         TALLOC_CTX *mem_ctx = talloc_stackframe();
2282         struct smbconf_ctx *conf_ctx = lp_smbconf_ctx();
2283         bool ret = false;
2284
2285         if (conf_ctx == NULL) {
2286                 goto done;
2287         }
2288
2289         err = smbconf_get_config(conf_ctx, mem_ctx, &num_shares, &service);
2290         if (!SBC_ERROR_IS_OK(err)) {
2291                 goto done;
2292         }
2293
2294         ret = true;
2295
2296         for (count = 0; count < num_shares; count++) {
2297                 if (strequal(service[count]->name, GLOBAL_NAME)) {
2298                         continue;
2299                 }
2300                 ret = process_smbconf_service(service[count]);
2301                 if (!ret) {
2302                         goto done;
2303                 }
2304         }
2305
2306         /* store the csn */
2307         smbconf_changed(conf_ctx, &conf_last_csn, NULL, NULL);
2308
2309 done:
2310         TALLOC_FREE(mem_ctx);
2311         return ret;
2312 }
2313
2314 /**
2315  * reload those shares from registry that are already
2316  * activated in the services array.
2317  */
2318 static bool reload_registry_shares(void)
2319 {
2320         int i;
2321         bool ret = true;
2322
2323         for (i = 0; i < iNumServices; i++) {
2324                 if (!VALID(i)) {
2325                         continue;
2326                 }
2327
2328                 if (ServicePtrs[i]->usershare == USERSHARE_VALID) {
2329                         continue;
2330                 }
2331
2332                 ret = process_registry_service(ServicePtrs[i]->szService);
2333                 if (!ret) {
2334                         goto done;
2335                 }
2336         }
2337
2338 done:
2339         return ret;
2340 }
2341
2342
2343 #define MAX_INCLUDE_DEPTH 100
2344
2345 static uint8_t include_depth;
2346
2347 /**
2348  * Free the file lists
2349  */
2350 static void free_file_list(void)
2351 {
2352         struct file_lists *f;
2353         struct file_lists *next;
2354
2355         f = file_lists;
2356         while( f ) {
2357                 next = f->next;
2358                 TALLOC_FREE( f );
2359                 f = next;
2360         }
2361         file_lists = NULL;
2362 }
2363
2364
2365 /**
2366  * Utility function for outsiders to check if we're running on registry.
2367  */
2368 bool lp_config_backend_is_registry(void)
2369 {
2370         return (lp_config_backend() == CONFIG_BACKEND_REGISTRY);
2371 }
2372
2373 /**
2374  * Utility function to check if the config backend is FILE.
2375  */
2376 bool lp_config_backend_is_file(void)
2377 {
2378         return (lp_config_backend() == CONFIG_BACKEND_FILE);
2379 }
2380
2381 /*******************************************************************
2382  Check if a config file has changed date.
2383 ********************************************************************/
2384
2385 bool lp_file_list_changed(void)
2386 {
2387         struct file_lists *f = file_lists;
2388
2389         DEBUG(6, ("lp_file_list_changed()\n"));
2390
2391         while (f) {
2392                 if (strequal(f->name, INCLUDE_REGISTRY_NAME)) {
2393                         struct smbconf_ctx *conf_ctx = lp_smbconf_ctx();
2394
2395                         if (conf_ctx == NULL) {
2396                                 return false;
2397                         }
2398                         if (smbconf_changed(conf_ctx, &conf_last_csn, NULL,
2399                                             NULL))
2400                         {
2401                                 DEBUGADD(6, ("registry config changed\n"));
2402                                 return true;
2403                         }
2404                 } else {
2405                         time_t mod_time;
2406                         char *n2 = NULL;
2407
2408                         n2 = talloc_sub_basic(talloc_tos(),
2409                                               get_current_username(),
2410                                               current_user_info.domain,
2411                                               f->name);
2412                         if (!n2) {
2413                                 return false;
2414                         }
2415                         DEBUGADD(6, ("file %s -> %s  last mod_time: %s\n",
2416                                      f->name, n2, ctime(&f->modtime)));
2417
2418                         mod_time = file_modtime(n2);
2419
2420                         if (mod_time &&
2421                             ((f->modtime != mod_time) ||
2422                              (f->subfname == NULL) ||
2423                              (strcmp(n2, f->subfname) != 0)))
2424                         {
2425                                 DEBUGADD(6,
2426                                          ("file %s modified: %s\n", n2,
2427                                           ctime(&mod_time)));
2428                                 f->modtime = mod_time;
2429                                 TALLOC_FREE(f->subfname);
2430                                 f->subfname = talloc_strdup(f, n2);
2431                                 if (f->subfname == NULL) {
2432                                         smb_panic("talloc_strdup failed");
2433                                 }
2434                                 TALLOC_FREE(n2);
2435                                 return true;
2436                         }
2437                         TALLOC_FREE(n2);
2438                 }
2439                 f = f->next;
2440         }
2441         return false;
2442 }
2443
2444
2445 /**
2446  * Initialize iconv conversion descriptors.
2447  *
2448  * This is called the first time it is needed, and also called again
2449  * every time the configuration is reloaded, because the charset or
2450  * codepage might have changed.
2451  **/
2452 static void init_iconv(void)
2453 {
2454         struct smb_iconv_handle *ret = NULL;
2455
2456         ret = reinit_iconv_handle(NULL,
2457                                   lp_dos_charset(),
2458                                   lp_unix_charset());
2459         if (ret == NULL) {
2460                 smb_panic("reinit_iconv_handle failed");
2461         }
2462 }
2463
2464 /***************************************************************************
2465  Handle the include operation.
2466 ***************************************************************************/
2467 static bool bAllowIncludeRegistry = true;
2468
2469 bool lp_include(struct loadparm_context *lp_ctx, struct loadparm_service *service,
2470                 const char *pszParmValue, char **ptr)
2471 {
2472         char *fname;
2473
2474         if (include_depth >= MAX_INCLUDE_DEPTH) {
2475                 DEBUG(0, ("Error: Maximum include depth (%u) exceeded!\n",
2476                           include_depth));
2477                 return false;
2478         }
2479
2480         if (strequal(pszParmValue, INCLUDE_REGISTRY_NAME)) {
2481                 if (!bAllowIncludeRegistry) {
2482                         return true;
2483                 }
2484                 if (lp_ctx->bInGlobalSection) {
2485                         bool ret;
2486                         include_depth++;
2487                         ret = process_registry_globals();
2488                         include_depth--;
2489                         return ret;
2490                 } else {
2491                         DEBUG(1, ("\"include = registry\" only effective "
2492                                   "in %s section\n", GLOBAL_NAME));
2493                         return false;
2494                 }
2495         }
2496
2497         fname = talloc_sub_basic(talloc_tos(), get_current_username(),
2498                                  current_user_info.domain,
2499                                  pszParmValue);
2500
2501         add_to_file_list(NULL, &file_lists, pszParmValue, fname);
2502
2503         if (service == NULL) {
2504                 lpcfg_string_set(Globals.ctx, ptr, fname);
2505         } else {
2506                 lpcfg_string_set(service, ptr, fname);
2507         }
2508
2509         if (file_exist(fname)) {
2510                 bool ret;
2511                 include_depth++;
2512                 ret = pm_process(fname, lp_do_section, do_parameter, lp_ctx);
2513                 include_depth--;
2514                 TALLOC_FREE(fname);
2515                 return ret;
2516         }
2517
2518         DEBUG(2, ("Can't find include file %s\n", fname));
2519         TALLOC_FREE(fname);
2520         return true;
2521 }
2522
2523 bool lp_idmap_range(const char *domain_name, uint32_t *low, uint32_t *high)
2524 {
2525         char *config_option = NULL;
2526         const char *range = NULL;
2527         bool ret = false;
2528
2529         SMB_ASSERT(low != NULL);
2530         SMB_ASSERT(high != NULL);
2531
2532         if ((domain_name == NULL) || (domain_name[0] == '\0')) {
2533                 domain_name = "*";
2534         }
2535
2536         config_option = talloc_asprintf(talloc_tos(), "idmap config %s",
2537                                         domain_name);
2538         if (config_option == NULL) {
2539                 DEBUG(0, ("out of memory\n"));
2540                 return false;
2541         }
2542
2543         range = lp_parm_const_string(-1, config_option, "range", NULL);
2544         if (range == NULL) {
2545                 DEBUG(1, ("idmap range not specified for domain '%s'\n", domain_name));
2546                 goto done;
2547         }
2548
2549         if (sscanf(range, "%u - %u", low, high) != 2) {
2550                 DEBUG(1, ("error parsing idmap range '%s' for domain '%s'\n",
2551                           range, domain_name));
2552                 goto done;
2553         }
2554
2555         ret = true;
2556
2557 done:
2558         talloc_free(config_option);
2559         return ret;
2560
2561 }
2562
2563 bool lp_idmap_default_range(uint32_t *low, uint32_t *high)
2564 {
2565         return lp_idmap_range("*", low, high);
2566 }
2567
2568 const char *lp_idmap_backend(const char *domain_name)
2569 {
2570         char *config_option = NULL;
2571         const char *backend = NULL;
2572
2573         if ((domain_name == NULL) || (domain_name[0] == '\0')) {
2574                 domain_name = "*";
2575         }
2576
2577         config_option = talloc_asprintf(talloc_tos(), "idmap config %s",
2578                                         domain_name);
2579         if (config_option == NULL) {
2580                 DEBUG(0, ("out of memory\n"));
2581                 return false;
2582         }
2583
2584         backend = lp_parm_const_string(-1, config_option, "backend", NULL);
2585         if (backend == NULL) {
2586                 DEBUG(1, ("idmap backend not specified for domain '%s'\n", domain_name));
2587                 goto done;
2588         }
2589
2590 done:
2591         talloc_free(config_option);
2592         return backend;
2593 }
2594
2595 const char *lp_idmap_default_backend(void)
2596 {
2597         return lp_idmap_backend("*");
2598 }
2599
2600 /***************************************************************************
2601  Handle ldap suffixes - default to ldapsuffix if sub-suffixes are not defined.
2602 ***************************************************************************/
2603
2604 static const char *append_ldap_suffix(TALLOC_CTX *ctx, const char *str )
2605 {
2606         const char *suffix_string;
2607
2608         suffix_string = talloc_asprintf(ctx, "%s,%s", str,
2609                                         Globals.ldap_suffix );
2610         if ( !suffix_string ) {
2611                 DEBUG(0,("append_ldap_suffix: talloc_asprintf() failed!\n"));
2612                 return "";
2613         }
2614
2615         return suffix_string;
2616 }
2617
2618 const char *lp_ldap_machine_suffix(TALLOC_CTX *ctx)
2619 {
2620         if (Globals._ldap_machine_suffix[0])
2621                 return append_ldap_suffix(ctx, Globals._ldap_machine_suffix);
2622
2623         return talloc_strdup(ctx, Globals.ldap_suffix);
2624 }
2625
2626 const char *lp_ldap_user_suffix(TALLOC_CTX *ctx)
2627 {
2628         if (Globals._ldap_user_suffix[0])
2629                 return append_ldap_suffix(ctx, Globals._ldap_user_suffix);
2630
2631         return talloc_strdup(ctx, Globals.ldap_suffix);
2632 }
2633
2634 const char *lp_ldap_group_suffix(TALLOC_CTX *ctx)
2635 {
2636         if (Globals._ldap_group_suffix[0])
2637                 return append_ldap_suffix(ctx, Globals._ldap_group_suffix);
2638
2639         return talloc_strdup(ctx, Globals.ldap_suffix);
2640 }
2641
2642 const char *lp_ldap_idmap_suffix(TALLOC_CTX *ctx)
2643 {
2644         if (Globals._ldap_idmap_suffix[0])
2645                 return append_ldap_suffix(ctx, Globals._ldap_idmap_suffix);
2646
2647         return talloc_strdup(ctx, Globals.ldap_suffix);
2648 }
2649
2650 /**
2651   return the parameter pointer for a parameter
2652 */
2653 void *lp_parm_ptr(struct loadparm_service *service, struct parm_struct *parm)
2654 {
2655         if (service == NULL) {
2656                 if (parm->p_class == P_LOCAL)
2657                         return (void *)(((char *)&sDefault)+parm->offset);
2658                 else if (parm->p_class == P_GLOBAL)
2659                         return (void *)(((char *)&Globals)+parm->offset);
2660                 else return NULL;
2661         } else {
2662                 return (void *)(((char *)service) + parm->offset);
2663         }
2664 }
2665
2666 /***************************************************************************
2667  Process a parameter for a particular service number. If snum < 0
2668  then assume we are in the globals.
2669 ***************************************************************************/
2670
2671 bool lp_do_parameter(int snum, const char *pszParmName, const char *pszParmValue)
2672 {
2673         TALLOC_CTX *frame = talloc_stackframe();
2674         struct loadparm_context *lp_ctx;
2675         bool ok;
2676
2677         lp_ctx = setup_lp_context(frame);
2678         if (lp_ctx == NULL) {
2679                 TALLOC_FREE(frame);
2680                 return false;
2681         }
2682
2683         if (snum < 0) {
2684                 ok = lpcfg_do_global_parameter(lp_ctx, pszParmName, pszParmValue);
2685         } else {
2686                 ok = lpcfg_do_service_parameter(lp_ctx, ServicePtrs[snum],
2687                                                 pszParmName, pszParmValue);
2688         }
2689
2690         TALLOC_FREE(frame);
2691
2692         return ok;
2693 }
2694
2695 /***************************************************************************
2696 set a parameter, marking it with FLAG_CMDLINE. Parameters marked as
2697 FLAG_CMDLINE won't be overridden by loads from smb.conf.
2698 ***************************************************************************/
2699
2700 static bool lp_set_cmdline_helper(const char *pszParmName, const char *pszParmValue)
2701 {
2702         int parmnum, i;
2703         parmnum = lpcfg_map_parameter(pszParmName);
2704         if (parmnum >= 0) {
2705                 flags_list[parmnum] &= ~FLAG_CMDLINE;
2706                 if (!lp_do_parameter(-1, pszParmName, pszParmValue)) {
2707                         return false;
2708                 }
2709                 flags_list[parmnum] |= FLAG_CMDLINE;
2710
2711                 /* we have to also set FLAG_CMDLINE on aliases.  Aliases must
2712                  * be grouped in the table, so we don't have to search the
2713                  * whole table */
2714                 for (i=parmnum-1;
2715                      i>=0 && parm_table[i].offset == parm_table[parmnum].offset
2716                              && parm_table[i].p_class == parm_table[parmnum].p_class;
2717                      i--) {
2718                         flags_list[i] |= FLAG_CMDLINE;
2719                 }
2720                 for (i=parmnum+1;i<num_parameters() && parm_table[i].offset == parm_table[parmnum].offset
2721                              && parm_table[i].p_class == parm_table[parmnum].p_class;i++) {
2722                         flags_list[i] |= FLAG_CMDLINE;
2723                 }
2724
2725                 return true;
2726         }
2727
2728         /* it might be parametric */
2729         if (strchr(pszParmName, ':') != NULL) {
2730                 set_param_opt(NULL, &Globals.param_opt, pszParmName, pszParmValue, FLAG_CMDLINE);
2731                 return true;
2732         }
2733
2734         DEBUG(0, ("Ignoring unknown parameter \"%s\"\n",  pszParmName));
2735         return false;
2736 }
2737
2738 bool lp_set_cmdline(const char *pszParmName, const char *pszParmValue)
2739 {
2740         bool ret;
2741         TALLOC_CTX *frame = talloc_stackframe();
2742         struct loadparm_context *lp_ctx;
2743
2744         lp_ctx = setup_lp_context(frame);
2745         if (lp_ctx == NULL) {
2746                 TALLOC_FREE(frame);
2747                 return false;
2748         }
2749
2750         ret = lpcfg_set_cmdline(lp_ctx, pszParmName, pszParmValue);
2751
2752         TALLOC_FREE(frame);
2753         return ret;
2754 }
2755
2756 /***************************************************************************
2757  Process a parameter.
2758 ***************************************************************************/
2759
2760 static bool do_parameter(const char *pszParmName, const char *pszParmValue,
2761                          void *userdata)
2762 {
2763         if (!bInGlobalSection && bGlobalOnly)
2764                 return true;
2765
2766         DEBUGADD(4, ("doing parameter %s = %s\n", pszParmName, pszParmValue));
2767
2768         if (bInGlobalSection) {
2769                 return lpcfg_do_global_parameter(userdata, pszParmName, pszParmValue);
2770         } else {
2771                 return lpcfg_do_service_parameter(userdata, ServicePtrs[iServiceIndex],
2772                                                   pszParmName, pszParmValue);
2773         }
2774 }
2775
2776
2777 static const char *ad_dc_req_vfs_mods[] = {"dfs_samba4", "acl_xattr", NULL};
2778
2779 /*
2780  * check that @vfs_objects includes all vfs modules required by an AD DC.
2781  */
2782 static bool check_ad_dc_required_mods(const char **vfs_objects)
2783 {
2784         int i;
2785         int j;
2786         int got_req;
2787
2788         for (i = 0; ad_dc_req_vfs_mods[i] != NULL; i++) {
2789                 got_req = false;
2790                 for (j = 0; vfs_objects[j] != NULL; j++) {
2791                         if (!strwicmp(ad_dc_req_vfs_mods[i], vfs_objects[j])) {
2792                                 got_req = true;
2793                                 break;
2794                         }
2795                 }
2796                 if (!got_req) {
2797                         DEBUG(0, ("vfs objects specified without required AD "
2798                                   "DC module: %s\n", ad_dc_req_vfs_mods[i]));
2799                         return false;
2800                 }
2801         }
2802
2803         DEBUG(6, ("vfs objects specified with all required AD DC modules\n"));
2804         return true;
2805 }
2806
2807
2808 /***************************************************************************
2809  Initialize any local variables in the sDefault table, after parsing a
2810  [globals] section.
2811 ***************************************************************************/
2812
2813 static void init_locals(void)
2814 {
2815         /*
2816          * We run this check once the [globals] is parsed, to force
2817          * the VFS objects and other per-share settings we need for
2818          * the standard way a AD DC is operated.  We may change these
2819          * as our code evolves, which is why we force these settings.
2820          *
2821          * We can't do this at the end of lp_load_ex(), as by that
2822          * point the services have been loaded and they will already
2823          * have "" as their vfs objects.
2824          */
2825         if (lp_server_role() == ROLE_ACTIVE_DIRECTORY_DC) {
2826                 const char **vfs_objects = lp_vfs_objects(-1);
2827                 if (vfs_objects != NULL) {
2828                         /* ignore return, only warn if modules are missing */
2829                         check_ad_dc_required_mods(vfs_objects);
2830                 } else {
2831                         if (lp_parm_const_string(-1, "xattr_tdb", "file", NULL)) {
2832                                 lp_do_parameter(-1, "vfs objects", "dfs_samba4 acl_xattr xattr_tdb");
2833                         } else if (lp_parm_const_string(-1, "posix", "eadb", NULL)) {
2834                                 lp_do_parameter(-1, "vfs objects", "dfs_samba4 acl_xattr posix_eadb");
2835                         } else {
2836                                 lp_do_parameter(-1, "vfs objects", "dfs_samba4 acl_xattr");
2837                         }
2838                 }
2839
2840                 lp_do_parameter(-1, "map hidden", "no");
2841                 lp_do_parameter(-1, "map system", "no");
2842                 lp_do_parameter(-1, "map readonly", "no");
2843                 lp_do_parameter(-1, "map archive", "no");
2844                 lp_do_parameter(-1, "store dos attributes", "yes");
2845         }
2846 }
2847
2848 /***************************************************************************
2849  Process a new section (service). At this stage all sections are services.
2850  Later we'll have special sections that permit server parameters to be set.
2851  Returns true on success, false on failure.
2852 ***************************************************************************/
2853
2854 bool lp_do_section(const char *pszSectionName, void *userdata)
2855 {
2856         struct loadparm_context *lp_ctx = (struct loadparm_context *)userdata;
2857         bool bRetval;
2858         bool isglobal = ((strwicmp(pszSectionName, GLOBAL_NAME) == 0) ||
2859                          (strwicmp(pszSectionName, GLOBAL_NAME2) == 0));
2860
2861         /* if we were in a global section then do the local inits */
2862         if (bInGlobalSection && !isglobal)
2863                 init_locals();
2864
2865         /* if we've just struck a global section, note the fact. */
2866         bInGlobalSection = isglobal;
2867         if (lp_ctx != NULL) {
2868                 lp_ctx->bInGlobalSection = isglobal;
2869         }
2870
2871         /* check for multiple global sections */
2872         if (bInGlobalSection) {
2873                 DEBUG(3, ("Processing section \"[%s]\"\n", pszSectionName));
2874                 return true;
2875         }
2876
2877         if (!bInGlobalSection && bGlobalOnly)
2878                 return true;
2879
2880         /* if we have a current service, tidy it up before moving on */
2881         bRetval = true;
2882
2883         if (iServiceIndex >= 0)
2884                 bRetval = lpcfg_service_ok(ServicePtrs[iServiceIndex]);
2885
2886         /* if all is still well, move to the next record in the services array */
2887         if (bRetval) {
2888                 /* We put this here to avoid an odd message order if messages are */
2889                 /* issued by the post-processing of a previous section. */
2890                 DEBUG(2, ("Processing section \"[%s]\"\n", pszSectionName));
2891
2892                 iServiceIndex = add_a_service(&sDefault, pszSectionName);
2893                 if (iServiceIndex < 0) {
2894                         DEBUG(0, ("Failed to add a new service\n"));
2895                         return false;
2896                 }
2897                 /* Clean all parametric options for service */
2898                 /* They will be added during parsing again */
2899                 free_param_opts(&ServicePtrs[iServiceIndex]->param_opt);
2900         }
2901
2902         return bRetval;
2903 }
2904
2905 /***************************************************************************
2906  Display the contents of a parameter of a single services record.
2907 ***************************************************************************/
2908
2909 bool dump_a_parameter(int snum, char *parm_name, FILE * f, bool isGlobal)
2910 {
2911         bool result = false;
2912         struct loadparm_context *lp_ctx;
2913
2914         lp_ctx = setup_lp_context(talloc_tos());
2915         if (lp_ctx == NULL) {
2916                 return false;
2917         }
2918
2919         if (isGlobal) {
2920                 result = lpcfg_dump_a_parameter(lp_ctx, NULL, parm_name, f);
2921         } else {
2922                 result = lpcfg_dump_a_parameter(lp_ctx, ServicePtrs[snum], parm_name, f);
2923         }
2924         TALLOC_FREE(lp_ctx);
2925         return result;
2926 }
2927
2928 #if 0
2929 /***************************************************************************
2930  Display the contents of a single copy structure.
2931 ***************************************************************************/
2932 static void dump_copy_map(bool *pcopymap)
2933 {
2934         int i;
2935         if (!pcopymap)
2936                 return;
2937
2938         printf("\n\tNon-Copied parameters:\n");
2939
2940         for (i = 0; parm_table[i].label; i++)
2941                 if (parm_table[i].p_class == P_LOCAL &&
2942                     parm_table[i].ptr && !pcopymap[i] &&
2943                     (i == 0 || (parm_table[i].ptr != parm_table[i - 1].ptr)))
2944                 {
2945                         printf("\t\t%s\n", parm_table[i].label);
2946                 }
2947 }
2948 #endif
2949
2950 /***************************************************************************
2951  Return TRUE if the passed service number is within range.
2952 ***************************************************************************/
2953
2954 bool lp_snum_ok(int iService)
2955 {
2956         return (LP_SNUM_OK(iService) && ServicePtrs[iService]->available);
2957 }
2958
2959 /***************************************************************************
2960  Auto-load some home services.
2961 ***************************************************************************/
2962
2963 static void lp_add_auto_services(const char *str)
2964 {
2965         char *s;
2966         char *p;
2967         int homes;
2968         char *saveptr;
2969
2970         if (!str)
2971                 return;
2972
2973         s = talloc_strdup(talloc_tos(), str);
2974         if (!s) {
2975                 smb_panic("talloc_strdup failed");
2976                 return;
2977         }
2978
2979         homes = lp_servicenumber(HOMES_NAME);
2980
2981         for (p = strtok_r(s, LIST_SEP, &saveptr); p;
2982              p = strtok_r(NULL, LIST_SEP, &saveptr)) {
2983                 char *home;
2984
2985                 if (lp_servicenumber(p) >= 0)
2986                         continue;
2987
2988                 home = get_user_home_dir(talloc_tos(), p);
2989
2990                 if (home && home[0] && homes >= 0)
2991                         lp_add_home(p, homes, p, home);
2992
2993                 TALLOC_FREE(home);
2994         }
2995         TALLOC_FREE(s);
2996 }
2997
2998 /***************************************************************************
2999  Auto-load one printer.
3000 ***************************************************************************/
3001
3002 void lp_add_one_printer(const char *name, const char *comment,
3003                         const char *location, void *pdata)
3004 {
3005         int printers = lp_servicenumber(PRINTERS_NAME);
3006         int i;
3007
3008         if (lp_servicenumber(name) < 0) {
3009                 lp_add_printer(name, printers);
3010                 if ((i = lp_servicenumber(name)) >= 0) {
3011                         lpcfg_string_set(ServicePtrs[i],
3012                                          &ServicePtrs[i]->comment, comment);
3013                         ServicePtrs[i]->autoloaded = true;
3014                 }
3015         }
3016 }
3017
3018 /***************************************************************************
3019  Have we loaded a services file yet?
3020 ***************************************************************************/
3021
3022 bool lp_loaded(void)
3023 {
3024         return (bLoaded);
3025 }
3026
3027 /***************************************************************************
3028  Unload unused services.
3029 ***************************************************************************/
3030
3031 void lp_killunused(struct smbd_server_connection *sconn,
3032                    bool (*snumused) (struct smbd_server_connection *, int))
3033 {
3034         int i;
3035         for (i = 0; i < iNumServices; i++) {
3036                 if (!VALID(i))
3037                         continue;
3038
3039                 /* don't kill autoloaded or usershare services */
3040                 if ( ServicePtrs[i]->autoloaded ||
3041                                 ServicePtrs[i]->usershare == USERSHARE_VALID) {
3042                         continue;
3043                 }
3044
3045                 if (!snumused || !snumused(sconn, i)) {
3046                         free_service_byindex(i);
3047                 }
3048         }
3049 }
3050
3051 /**
3052  * Kill all except autoloaded and usershare services - convenience wrapper
3053  */
3054 void lp_kill_all_services(void)
3055 {
3056         lp_killunused(NULL, NULL);
3057 }
3058
3059 /***************************************************************************
3060  Unload a service.
3061 ***************************************************************************/
3062
3063 void lp_killservice(int iServiceIn)
3064 {
3065         if (VALID(iServiceIn)) {
3066                 free_service_byindex(iServiceIn);
3067         }
3068 }
3069
3070 /***************************************************************************
3071  Save the curent values of all global and sDefault parameters into the
3072  defaults union. This allows testparm to show only the
3073  changed (ie. non-default) parameters.
3074 ***************************************************************************/
3075
3076 static void lp_save_defaults(void)
3077 {
3078         int i;
3079         struct parmlist_entry * parm;
3080         for (i = 0; parm_table[i].label; i++) {
3081                 if (!(flags_list[i] & FLAG_CMDLINE)) {
3082                         flags_list[i] |= FLAG_DEFAULT;
3083                 }
3084
3085                 if (i > 0 && parm_table[i].offset == parm_table[i - 1].offset
3086                     && parm_table[i].p_class == parm_table[i - 1].p_class)
3087                         continue;
3088                 switch (parm_table[i].type) {
3089                         case P_LIST:
3090                         case P_CMDLIST:
3091                                 parm_table[i].def.lvalue = str_list_copy(
3092                                         NULL, *(const char ***)lp_parm_ptr(NULL, &parm_table[i]));
3093                                 break;
3094                         case P_STRING:
3095                         case P_USTRING:
3096                                 lpcfg_string_set(
3097                                         Globals.ctx,
3098                                         &parm_table[i].def.svalue,
3099                                         *(char **)lp_parm_ptr(
3100                                                 NULL, &parm_table[i]));
3101                                 if (parm_table[i].def.svalue == NULL) {
3102                                         smb_panic("lpcfg_string_set() failed");
3103                                 }
3104                                 break;
3105                         case P_BOOL:
3106                         case P_BOOLREV:
3107                                 parm_table[i].def.bvalue =
3108                                         *(bool *)lp_parm_ptr(NULL, &parm_table[i]);
3109                                 break;
3110                         case P_CHAR:
3111                                 parm_table[i].def.cvalue =
3112                                         *(char *)lp_parm_ptr(NULL, &parm_table[i]);
3113                                 break;
3114                         case P_INTEGER:
3115                         case P_OCTAL:
3116                         case P_ENUM:
3117                         case P_BYTES:
3118                                 parm_table[i].def.ivalue =
3119                                         *(int *)lp_parm_ptr(NULL, &parm_table[i]);
3120                                 break;
3121                 }
3122         }
3123
3124         for (parm=Globals.param_opt; parm; parm=parm->next) {
3125                 if (!(parm->priority & FLAG_CMDLINE)) {
3126                         parm->priority |= FLAG_DEFAULT;
3127                 }
3128         }
3129
3130         for (parm=sDefault.param_opt; parm; parm=parm->next) {
3131                 if (!(parm->priority & FLAG_CMDLINE)) {
3132                         parm->priority |= FLAG_DEFAULT;
3133                 }
3134         }
3135
3136         defaults_saved = true;
3137 }
3138
3139 /***********************************************************
3140  If we should send plaintext/LANMAN passwords in the clinet
3141 ************************************************************/
3142
3143 static void set_allowed_client_auth(void)
3144 {
3145         if (Globals.client_ntlmv2_auth) {
3146                 Globals.client_lanman_auth = false;
3147         }
3148         if (!Globals.client_lanman_auth) {
3149                 Globals.client_plaintext_auth = false;
3150         }
3151 }
3152
3153 /***************************************************************************
3154  JRA.
3155  The following code allows smbd to read a user defined share file.
3156  Yes, this is my intent. Yes, I'm comfortable with that...
3157
3158  THE FOLLOWING IS SECURITY CRITICAL CODE.
3159
3160  It washes your clothes, it cleans your house, it guards you while you sleep...
3161  Do not f%^k with it....
3162 ***************************************************************************/
3163
3164 #define MAX_USERSHARE_FILE_SIZE (10*1024)
3165
3166 /***************************************************************************
3167  Check allowed stat state of a usershare file.
3168  Ensure we print out who is dicking with us so the admin can
3169  get their sorry ass fired.
3170 ***************************************************************************/
3171
3172 static bool check_usershare_stat(const char *fname,
3173                                  const SMB_STRUCT_STAT *psbuf)
3174 {
3175         if (!S_ISREG(psbuf->st_ex_mode)) {
3176                 DEBUG(0,("check_usershare_stat: file %s owned by uid %u is "
3177                         "not a regular file\n",
3178                         fname, (unsigned int)psbuf->st_ex_uid ));
3179                 return false;
3180         }
3181
3182         /* Ensure this doesn't have the other write bit set. */
3183         if (psbuf->st_ex_mode & S_IWOTH) {
3184                 DEBUG(0,("check_usershare_stat: file %s owned by uid %u allows "
3185                         "public write. Refusing to allow as a usershare file.\n",
3186                         fname, (unsigned int)psbuf->st_ex_uid ));
3187                 return false;
3188         }
3189
3190         /* Should be 10k or less. */
3191         if (psbuf->st_ex_size > MAX_USERSHARE_FILE_SIZE) {
3192                 DEBUG(0,("check_usershare_stat: file %s owned by uid %u is "
3193                         "too large (%u) to be a user share file.\n",
3194                         fname, (unsigned int)psbuf->st_ex_uid,
3195                         (unsigned int)psbuf->st_ex_size ));
3196                 return false;
3197         }
3198
3199         return true;
3200 }
3201
3202 /***************************************************************************
3203  Parse the contents of a usershare file.
3204 ***************************************************************************/
3205
3206 enum usershare_err parse_usershare_file(TALLOC_CTX *ctx,
3207                         SMB_STRUCT_STAT *psbuf,
3208                         const char *servicename,
3209                         int snum,
3210                         char **lines,
3211                         int numlines,
3212                         char **pp_sharepath,
3213                         char **pp_comment,
3214                         char **pp_cp_servicename,
3215                         struct security_descriptor **ppsd,
3216                         bool *pallow_guest)
3217 {
3218         const char **prefixallowlist = lp_usershare_prefix_allow_list();
3219         const char **prefixdenylist = lp_usershare_prefix_deny_list();
3220         int us_vers;
3221         DIR *dp;
3222         SMB_STRUCT_STAT sbuf;
3223         char *sharepath = NULL;
3224         char *comment = NULL;
3225
3226         *pp_sharepath = NULL;
3227         *pp_comment = NULL;
3228
3229         *pallow_guest = false;
3230
3231         if (numlines < 4) {
3232                 return USERSHARE_MALFORMED_FILE;
3233         }
3234
3235         if (strcmp(lines[0], "#VERSION 1") == 0) {
3236                 us_vers = 1;
3237         } else if (strcmp(lines[0], "#VERSION 2") == 0) {
3238                 us_vers = 2;
3239                 if (numlines < 5) {
3240                         return USERSHARE_MALFORMED_FILE;
3241                 }
3242         } else {
3243                 return USERSHARE_BAD_VERSION;
3244         }
3245
3246         if (strncmp(lines[1], "path=", 5) != 0) {
3247                 return USERSHARE_MALFORMED_PATH;
3248         }
3249
3250         sharepath = talloc_strdup(ctx, &lines[1][5]);
3251         if (!sharepath) {
3252                 return USERSHARE_POSIX_ERR;
3253         }
3254         trim_string(sharepath, " ", " ");
3255
3256         if (strncmp(lines[2], "comment=", 8) != 0) {
3257                 return USERSHARE_MALFORMED_COMMENT_DEF;
3258         }
3259
3260         comment = talloc_strdup(ctx, &lines[2][8]);
3261         if (!comment) {
3262                 return USERSHARE_POSIX_ERR;
3263         }
3264         trim_string(comment, " ", " ");
3265         trim_char(comment, '"', '"');
3266
3267         if (strncmp(lines[3], "usershare_acl=", 14) != 0) {
3268                 return USERSHARE_MALFORMED_ACL_DEF;
3269         }
3270
3271         if (!parse_usershare_acl(ctx, &lines[3][14], ppsd)) {
3272                 return USERSHARE_ACL_ERR;
3273         }
3274
3275         if (us_vers == 2) {
3276                 if (strncmp(lines[4], "guest_ok=", 9) != 0) {
3277                         return USERSHARE_MALFORMED_ACL_DEF;
3278                 }
3279                 if (lines[4][9] == 'y') {
3280                         *pallow_guest = true;
3281                 }
3282
3283                 /* Backwards compatible extension to file version #2. */
3284                 if (numlines > 5) {
3285                         if (strncmp(lines[5], "sharename=", 10) != 0) {
3286                                 return USERSHARE_MALFORMED_SHARENAME_DEF;
3287                         }
3288                         if (!strequal(&lines[5][10], servicename)) {
3289                                 return USERSHARE_BAD_SHARENAME;
3290                         }
3291                         *pp_cp_servicename = talloc_strdup(ctx, &lines[5][10]);
3292                         if (!*pp_cp_servicename) {
3293                                 return USERSHARE_POSIX_ERR;
3294                         }
3295                 }
3296         }
3297
3298         if (*pp_cp_servicename == NULL) {
3299                 *pp_cp_servicename = talloc_strdup(ctx, servicename);
3300                 if (!*pp_cp_servicename) {
3301                         return USERSHARE_POSIX_ERR;
3302                 }
3303         }
3304
3305         if (snum != -1 && (strcmp(sharepath, ServicePtrs[snum]->path) == 0)) {
3306                 /* Path didn't change, no checks needed. */
3307                 *pp_sharepath = sharepath;
3308                 *pp_comment = comment;
3309                 return USERSHARE_OK;
3310         }
3311
3312         /* The path *must* be absolute. */
3313         if (sharepath[0] != '/') {
3314                 DEBUG(2,("parse_usershare_file: share %s: path %s is not an absolute path.\n",
3315                         servicename, sharepath));
3316                 return USERSHARE_PATH_NOT_ABSOLUTE;
3317         }
3318
3319         /* If there is a usershare prefix deny list ensure one of these paths
3320            doesn't match the start of the user given path. */
3321         if (prefixdenylist) {
3322                 int i;
3323                 for ( i=0; prefixdenylist[i]; i++ ) {
3324                         DEBUG(10,("parse_usershare_file: share %s : checking prefixdenylist[%d]='%s' against %s\n",
3325                                 servicename, i, prefixdenylist[i], sharepath ));
3326                         if (memcmp( sharepath, prefixdenylist[i], strlen(prefixdenylist[i])) == 0) {
3327                                 DEBUG(2,("parse_usershare_file: share %s path %s starts with one of the "
3328                                         "usershare prefix deny list entries.\n",
3329                                         servicename, sharepath));
3330                                 return USERSHARE_PATH_IS_DENIED;
3331                         }
3332                 }
3333         }
3334
3335         /* If there is a usershare prefix allow list ensure one of these paths
3336            does match the start of the user given path. */
3337
3338         if (prefixallowlist) {
3339                 int i;
3340                 for ( i=0; prefixallowlist[i]; i++ ) {
3341                         DEBUG(10,("parse_usershare_file: share %s checking prefixallowlist[%d]='%s' against %s\n",
3342                                 servicename, i, prefixallowlist[i], sharepath ));
3343                         if (memcmp( sharepath, prefixallowlist[i], strlen(prefixallowlist[i])) == 0) {
3344                                 break;
3345                         }
3346                 }
3347                 if (prefixallowlist[i] == NULL) {
3348                         DEBUG(2,("parse_usershare_file: share %s path %s doesn't start with one of the "
3349                                 "usershare prefix allow list entries.\n",
3350                                 servicename, sharepath));
3351                         return USERSHARE_PATH_NOT_ALLOWED;
3352                 }
3353         }
3354
3355         /* Ensure this is pointing to a directory. */
3356         dp = opendir(sharepath);
3357
3358         if (!dp) {
3359                 DEBUG(2,("parse_usershare_file: share %s path %s is not a directory.\n",
3360                         servicename, sharepath));
3361                 return USERSHARE_PATH_NOT_DIRECTORY;
3362         }
3363
3364         /* Ensure the owner of the usershare file has permission to share
3365            this directory. */
3366
3367         if (sys_stat(sharepath, &sbuf, false) == -1) {
3368                 DEBUG(2,("parse_usershare_file: share %s : stat failed on path %s. %s\n",
3369                         servicename, sharepath, strerror(errno) ));
3370                 closedir(dp);
3371                 return USERSHARE_POSIX_ERR;
3372         }
3373
3374         closedir(dp);
3375
3376         if (!S_ISDIR(sbuf.st_ex_mode)) {
3377                 DEBUG(2,("parse_usershare_file: share %s path %s is not a directory.\n",
3378                         servicename, sharepath ));
3379                 return USERSHARE_PATH_NOT_DIRECTORY;
3380         }
3381
3382         /* Check if sharing is restricted to owner-only. */
3383         /* psbuf is the stat of the usershare definition file,
3384            sbuf is the stat of the target directory to be shared. */
3385
3386         if (lp_usershare_owner_only()) {
3387                 /* root can share anything. */
3388                 if ((psbuf->st_ex_uid != 0) && (sbuf.st_ex_uid != psbuf->st_ex_uid)) {
3389                         return USERSHARE_PATH_NOT_ALLOWED;
3390                 }
3391         }
3392
3393         *pp_sharepath = sharepath;
3394         *pp_comment = comment;
3395         return USERSHARE_OK;
3396 }
3397
3398 /***************************************************************************
3399  Deal with a usershare file.
3400  Returns:
3401         >= 0 - snum
3402         -1 - Bad name, invalid contents.
3403            - service name already existed and not a usershare, problem
3404             with permissions to share directory etc.
3405 ***************************************************************************/
3406
3407 static int process_usershare_file(const char *dir_name, const char *file_name, int snum_template)
3408 {
3409         SMB_STRUCT_STAT sbuf;
3410         SMB_STRUCT_STAT lsbuf;
3411         char *fname = NULL;
3412         char *sharepath = NULL;
3413         char *comment = NULL;
3414         char *cp_service_name = NULL;
3415         char **lines = NULL;
3416         int numlines = 0;
3417         int fd = -1;
3418         int iService = -1;
3419         TALLOC_CTX *ctx = talloc_stackframe();
3420         struct security_descriptor *psd = NULL;
3421         bool guest_ok = false;
3422         char *canon_name = NULL;
3423         bool added_service = false;
3424         int ret = -1;
3425         NTSTATUS status;
3426
3427         /* Ensure share name doesn't contain invalid characters. */
3428         if (!validate_net_name(file_name, INVALID_SHARENAME_CHARS, strlen(file_name))) {
3429                 DEBUG(0,("process_usershare_file: share name %s contains "
3430                         "invalid characters (any of %s)\n",
3431                         file_name, INVALID_SHARENAME_CHARS ));
3432                 goto out;
3433         }
3434
3435         canon_name = canonicalize_servicename(ctx, file_name);
3436         if (!canon_name) {
3437                 goto out;
3438         }
3439
3440         fname = talloc_asprintf(ctx, "%s/%s", dir_name, file_name);
3441         if (!fname) {
3442                 goto out;
3443         }
3444
3445         /* Minimize the race condition by doing an lstat before we
3446            open and fstat. Ensure this isn't a symlink link. */
3447
3448         if (sys_lstat(fname, &lsbuf, false) != 0) {
3449                 if (errno == ENOENT) {
3450                         /* Unknown share requested. Just ignore. */
3451                         goto out;
3452                 }
3453                 /* Only log messages for meaningful problems. */
3454                 DEBUG(0,("process_usershare_file: stat of %s failed. %s\n",
3455                         fname, strerror(errno) ));
3456                 goto out;
3457         }
3458
3459         /* This must be a regular file, not a symlink, directory or
3460            other strange filetype. */
3461         if (!check_usershare_stat(fname, &lsbuf)) {
3462                 goto out;
3463         }
3464
3465         {
3466                 TDB_DATA data;
3467
3468                 status = dbwrap_fetch_bystring(ServiceHash, canon_name,
3469                                                canon_name, &data);
3470
3471                 iService = -1;
3472
3473                 if (NT_STATUS_IS_OK(status) &&
3474                     (data.dptr != NULL) &&
3475                     (data.dsize == sizeof(iService))) {
3476                         memcpy(&iService, data.dptr, sizeof(iService));
3477                 }
3478         }
3479
3480         if (iService != -1 &&
3481             timespec_compare(&ServicePtrs[iService]->usershare_last_mod,
3482                              &lsbuf.st_ex_mtime) == 0) {
3483                 /* Nothing changed - Mark valid and return. */
3484                 DEBUG(10,("process_usershare_file: service %s not changed.\n",
3485                         canon_name ));
3486                 ServicePtrs[iService]->usershare = USERSHARE_VALID;
3487                 ret = iService;
3488                 goto out;
3489         }
3490
3491         /* Try and open the file read only - no symlinks allowed. */
3492 #ifdef O_NOFOLLOW
3493         fd = open(fname, O_RDONLY|O_NOFOLLOW, 0);
3494 #else
3495         fd = open(fname, O_RDONLY, 0);
3496 #endif
3497
3498         if (fd == -1) {
3499                 DEBUG(0,("process_usershare_file: unable to open %s. %s\n",
3500                         fname, strerror(errno) ));
3501                 goto out;
3502         }
3503
3504         /* Now fstat to be *SURE* it's a regular file. */
3505         if (sys_fstat(fd, &sbuf, false) != 0) {
3506                 close(fd);
3507                 DEBUG(0,("process_usershare_file: fstat of %s failed. %s\n",
3508                         fname, strerror(errno) ));
3509                 goto out;
3510         }
3511
3512         /* Is it the same dev/inode as was lstated ? */
3513         if (!check_same_stat(&lsbuf, &sbuf)) {
3514                 close(fd);
3515                 DEBUG(0,("process_usershare_file: fstat of %s is a different file from lstat. "
3516                         "Symlink spoofing going on ?\n", fname ));
3517                 goto out;
3518         }
3519
3520         /* This must be a regular file, not a symlink, directory or
3521            other strange filetype. */
3522         if (!check_usershare_stat(fname, &sbuf)) {
3523                 close(fd);
3524                 goto out;
3525         }
3526
3527         lines = fd_lines_load(fd, &numlines, MAX_USERSHARE_FILE_SIZE, NULL);
3528
3529         close(fd);
3530         if (lines == NULL) {
3531                 DEBUG(0,("process_usershare_file: loading file %s owned by %u failed.\n",
3532                         fname, (unsigned int)sbuf.st_ex_uid ));
3533                 goto out;
3534         }
3535
3536         if (parse_usershare_file(ctx, &sbuf, file_name,
3537                         iService, lines, numlines, &sharepath,
3538                         &comment, &cp_service_name,
3539                         &psd, &guest_ok) != USERSHARE_OK) {
3540                 goto out;
3541         }
3542
3543         /* Everything ok - add the service possibly using a template. */
3544         if (iService < 0) {
3545                 const struct loadparm_service *sp = &sDefault;
3546                 if (snum_template != -1) {
3547                         sp = ServicePtrs[snum_template];
3548                 }
3549
3550                 if ((iService = add_a_service(sp, cp_service_name)) < 0) {
3551                         DEBUG(0, ("process_usershare_file: Failed to add "
3552                                 "new service %s\n", cp_service_name));
3553                         goto out;
3554                 }
3555
3556                 added_service = true;
3557
3558                 /* Read only is controlled by usershare ACL below. */
3559                 ServicePtrs[iService]->read_only = false;
3560         }
3561
3562         /* Write the ACL of the new/modified share. */
3563         status = set_share_security(canon_name, psd);
3564         if (!NT_STATUS_IS_OK(status)) {
3565                  DEBUG(0, ("process_usershare_file: Failed to set share "
3566                         "security for user share %s\n",
3567                         canon_name ));
3568                 goto out;
3569         }
3570
3571         /* If from a template it may be marked invalid. */
3572         ServicePtrs[iService]->valid = true;
3573
3574         /* Set the service as a valid usershare. */
3575         ServicePtrs[iService]->usershare = USERSHARE_VALID;
3576
3577         /* Set guest access. */
3578         if (lp_usershare_allow_guests()) {
3579                 ServicePtrs[iService]->guest_ok = guest_ok;
3580         }
3581
3582         /* And note when it was loaded. */
3583         ServicePtrs[iService]->usershare_last_mod = sbuf.st_ex_mtime;
3584         lpcfg_string_set(ServicePtrs[iService], &ServicePtrs[iService]->path,
3585                          sharepath);
3586         lpcfg_string_set(ServicePtrs[iService],
3587                          &ServicePtrs[iService]->comment, comment);
3588
3589         ret = iService;
3590
3591   out:
3592
3593         if (ret == -1 && iService != -1 && added_service) {
3594                 lp_remove_service(iService);
3595         }
3596
3597         TALLOC_FREE(lines);
3598         TALLOC_FREE(ctx);
3599         return ret;
3600 }
3601
3602 /***************************************************************************
3603  Checks if a usershare entry has been modified since last load.
3604 ***************************************************************************/
3605
3606 static bool usershare_exists(int iService, struct timespec *last_mod)
3607 {
3608         SMB_STRUCT_STAT lsbuf;
3609         const char *usersharepath = Globals.usershare_path;
3610         char *fname;
3611
3612         fname = talloc_asprintf(talloc_tos(),
3613                                 "%s/%s",
3614                                 usersharepath,
3615                                 ServicePtrs[iService]->szService);
3616         if (fname == NULL) {
3617                 return false;
3618         }
3619
3620         if (sys_lstat(fname, &lsbuf, false) != 0) {
3621                 TALLOC_FREE(fname);
3622                 return false;
3623         }
3624
3625         if (!S_ISREG(lsbuf.st_ex_mode)) {
3626                 TALLOC_FREE(fname);
3627                 return false;
3628         }
3629
3630         TALLOC_FREE(fname);
3631         *last_mod = lsbuf.st_ex_mtime;
3632         return true;
3633 }
3634
3635 static bool usershare_directory_is_root(uid_t uid)
3636 {
3637         if (uid == 0) {
3638                 return true;
3639         }
3640
3641         if (uid_wrapper_enabled()) {
3642                 return true;
3643         }
3644
3645         return false;
3646 }
3647
3648 /***************************************************************************
3649  Load a usershare service by name. Returns a valid servicenumber or -1.
3650 ***************************************************************************/
3651
3652 int load_usershare_service(const char *servicename)
3653 {
3654         SMB_STRUCT_STAT sbuf;
3655         const char *usersharepath = Globals.usershare_path;
3656         int max_user_shares = Globals.usershare_max_shares;
3657         int snum_template = -1;
3658
3659         if (servicename[0] == '\0') {
3660                 /* Invalid service name. */
3661                 return -1;
3662         }
3663
3664         if (*usersharepath == 0 ||  max_user_shares == 0) {
3665                 return -1;
3666         }
3667
3668         if (sys_stat(usersharepath, &sbuf, false) != 0) {
3669                 DEBUG(0,("load_usershare_service: stat of %s failed. %s\n",
3670                         usersharepath, strerror(errno) ));
3671                 return -1;
3672         }
3673
3674         if (!S_ISDIR(sbuf.st_ex_mode)) {
3675                 DEBUG(0,("load_usershare_service: %s is not a directory.\n",
3676                         usersharepath ));
3677                 return -1;
3678         }
3679
3680         /*
3681          * This directory must be owned by root, and have the 't' bit set.
3682          * It also must not be writable by "other".
3683          */
3684
3685 #ifdef S_ISVTX
3686         if (!usershare_directory_is_root(sbuf.st_ex_uid) ||
3687             !(sbuf.st_ex_mode & S_ISVTX) || (sbuf.st_ex_mode & S_IWOTH)) {
3688 #else
3689         if (!usershare_directory_is_root(sbuf.st_ex_uid) ||
3690             (sbuf.st_ex_mode & S_IWOTH)) {
3691 #endif
3692                 DEBUG(0,("load_usershare_service: directory %s is not owned by root "
3693                         "or does not have the sticky bit 't' set or is writable by anyone.\n",
3694                         usersharepath ));
3695                 return -1;
3696         }
3697
3698         /* Ensure the template share exists if it's set. */
3699         if (Globals.usershare_template_share[0]) {
3700                 /* We can't use lp_servicenumber here as we are recommending that
3701                    template shares have -valid=false set. */
3702                 for (snum_template = iNumServices - 1; snum_template >= 0; snum_template--) {
3703                         if (ServicePtrs[snum_template]->szService &&
3704                                         strequal(ServicePtrs[snum_template]->szService,
3705                                                 Globals.usershare_template_share)) {
3706                                 break;
3707                         }
3708                 }
3709
3710                 if (snum_template == -1) {
3711                         DEBUG(0,("load_usershare_service: usershare template share %s "
3712                                 "does not exist.\n",
3713                                 Globals.usershare_template_share ));
3714                         return -1;
3715                 }
3716         }
3717
3718         return process_usershare_file(usersharepath, servicename, snum_template);
3719 }
3720
3721 /***************************************************************************
3722  Load all user defined shares from the user share directory.
3723  We only do this if we're enumerating the share list.
3724  This is the function that can delete usershares that have
3725  been removed.
3726 ***************************************************************************/
3727
3728 int load_usershare_shares(struct smbd_server_connection *sconn,
3729                           bool (*snumused) (struct smbd_server_connection *, int))
3730 {
3731         DIR *dp;
3732         SMB_STRUCT_STAT sbuf;
3733         struct dirent *de;
3734         int num_usershares = 0;
3735         int max_user_shares = Globals.usershare_max_shares;
3736         unsigned int num_dir_entries, num_bad_dir_entries, num_tmp_dir_entries;
3737         unsigned int allowed_bad_entries = ((2*max_user_shares)/10);
3738         unsigned int allowed_tmp_entries = ((2*max_user_shares)/10);
3739         int iService;
3740         int snum_template = -1;
3741         const char *usersharepath = Globals.usershare_path;
3742         int ret = lp_numservices();
3743         TALLOC_CTX *tmp_ctx;
3744
3745         if (max_user_shares == 0 || *usersharepath == '\0') {
3746                 return lp_numservices();
3747         }
3748
3749         if (sys_stat(usersharepath, &sbuf, false) != 0) {
3750                 DEBUG(0,("load_usershare_shares: stat of %s failed. %s\n",
3751                         usersharepath, strerror(errno) ));
3752                 return ret;
3753         }
3754
3755         /*
3756          * This directory must be owned by root, and have the 't' bit set.
3757          * It also must not be writable by "other".
3758          */
3759
3760 #ifdef S_ISVTX
3761         if (sbuf.st_ex_uid != 0 || !(sbuf.st_ex_mode & S_ISVTX) || (sbuf.st_ex_mode & S_IWOTH)) {
3762 #else
3763         if (sbuf.st_ex_uid != 0 || (sbuf.st_ex_mode & S_IWOTH)) {
3764 #endif
3765                 DEBUG(0,("load_usershare_shares: directory %s is not owned by root "
3766                         "or does not have the sticky bit 't' set or is writable by anyone.\n",
3767                         usersharepath ));
3768                 return ret;
3769         }
3770
3771         /* Ensure the template share exists if it's set. */
3772         if (Globals.usershare_template_share[0]) {
3773                 /* We can't use lp_servicenumber here as we are recommending that
3774                    template shares have -valid=false set. */
3775                 for (snum_template = iNumServices - 1; snum_template >= 0; snum_template--) {
3776                         if (ServicePtrs[snum_template]->szService &&
3777                                         strequal(ServicePtrs[snum_template]->szService,
3778                                                 Globals.usershare_template_share)) {
3779                                 break;
3780                         }
3781                 }
3782
3783                 if (snum_template == -1) {
3784                         DEBUG(0,("load_usershare_shares: usershare template share %s "
3785                                 "does not exist.\n",
3786                                 Globals.usershare_template_share ));
3787                         return ret;
3788                 }
3789         }
3790
3791         /* Mark all existing usershares as pending delete. */
3792         for (iService = iNumServices - 1; iService >= 0; iService--) {
3793                 if (VALID(iService) && ServicePtrs[iService]->usershare) {
3794                         ServicePtrs[iService]->usershare = USERSHARE_PENDING_DELETE;
3795                 }
3796         }
3797
3798         dp = opendir(usersharepath);
3799         if (!dp) {
3800                 DEBUG(0,("load_usershare_shares:: failed to open directory %s. %s\n",
3801                         usersharepath, strerror(errno) ));
3802                 return ret;
3803         }
3804
3805         for (num_dir_entries = 0, num_bad_dir_entries = 0, num_tmp_dir_entries = 0;
3806                         (de = readdir(dp));
3807                         num_dir_entries++ ) {
3808                 int r;
3809                 const char *n = de->d_name;
3810
3811                 /* Ignore . and .. */
3812                 if (*n == '.') {
3813                         if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
3814                                 continue;
3815                         }
3816                 }
3817
3818                 if (n[0] == ':') {
3819                         /* Temporary file used when creating a share. */
3820                         num_tmp_dir_entries++;
3821                 }
3822
3823                 /* Allow 20% tmp entries. */
3824                 if (num_tmp_dir_entries > allowed_tmp_entries) {
3825                         DEBUG(0,("load_usershare_shares: too many temp entries (%u) "
3826                                 "in directory %s\n",
3827                                 num_tmp_dir_entries, usersharepath));
3828                         break;
3829                 }
3830
3831                 r = process_usershare_file(usersharepath, n, snum_template);
3832                 if (r == 0) {
3833                         /* Update the services count. */
3834                         num_usershares++;
3835                         if (num_usershares >= max_user_shares) {
3836                                 DEBUG(0,("load_usershare_shares: max user shares reached "
3837                                         "on file %s in directory %s\n",
3838                                         n, usersharepath ));
3839                                 break;
3840                         }
3841                 } else if (r == -1) {
3842                         num_bad_dir_entries++;
3843                 }
3844
3845                 /* Allow 20% bad entries. */
3846                 if (num_bad_dir_entries > allowed_bad_entries) {
3847                         DEBUG(0,("load_usershare_shares: too many bad entries (%u) "
3848                                 "in directory %s\n",
3849                                 num_bad_dir_entries, usersharepath));
3850                         break;
3851                 }
3852
3853                 /* Allow 20% bad entries. */
3854                 if (num_dir_entries > max_user_shares + allowed_bad_entries) {
3855                         DEBUG(0,("load_usershare_shares: too many total entries (%u) "
3856                         "in directory %s\n",
3857                         num_dir_entries, usersharepath));
3858                         break;
3859                 }
3860         }
3861
3862         closedir(dp);
3863
3864         /* Sweep through and delete any non-refreshed usershares that are
3865            not currently in use. */
3866         tmp_ctx = talloc_stackframe();
3867         for (iService = iNumServices - 1; iService >= 0; iService--) {
3868                 if (VALID(iService) && (ServicePtrs[iService]->usershare == USERSHARE_PENDING_DELETE)) {
3869                         const struct loadparm_substitution *lp_sub =
3870                                 loadparm_s3_global_substitution();
3871                         char *servname;
3872
3873                         if (snumused && snumused(sconn, iService)) {
3874                                 continue;
3875                         }
3876
3877                         servname = lp_servicename(tmp_ctx, lp_sub, iService);
3878
3879                         /* Remove from the share ACL db. */
3880                         DEBUG(10,("load_usershare_shares: Removing deleted usershare %s\n",
3881                                   servname ));
3882                         delete_share_security(servname);
3883                         free_service_byindex(iService);
3884                 }
3885         }
3886         talloc_free(tmp_ctx);
3887
3888         return lp_numservices();
3889 }
3890
3891 /********************************************************
3892  Destroy global resources allocated in this file
3893 ********************************************************/
3894
3895 void gfree_loadparm(void)
3896 {
3897         int i;
3898
3899         free_file_list();
3900
3901         /* Free resources allocated to services */
3902
3903         for ( i = 0; i < iNumServices; i++ ) {
3904                 if ( VALID(i) ) {
3905                         free_service_byindex(i);
3906                 }
3907         }
3908
3909         TALLOC_FREE( ServicePtrs );
3910         iNumServices = 0;
3911
3912         /* Now release all resources allocated to global
3913            parameters and the default service */
3914
3915         free_global_parameters();
3916 }
3917
3918
3919 /***************************************************************************
3920  Allow client apps to specify that they are a client
3921 ***************************************************************************/
3922 static void lp_set_in_client(bool b)
3923 {
3924     in_client = b;
3925 }
3926
3927
3928 /***************************************************************************
3929  Determine if we're running in a client app
3930 ***************************************************************************/
3931 static bool lp_is_in_client(void)
3932 {
3933     return in_client;
3934 }
3935
3936 static void lp_enforce_ad_dc_settings(void)
3937 {
3938         lp_do_parameter(GLOBAL_SECTION_SNUM, "passdb backend", "samba_dsdb");
3939         lp_do_parameter(GLOBAL_SECTION_SNUM,
3940                         "winbindd:use external pipes", "true");
3941         lp_do_parameter(GLOBAL_SECTION_SNUM, "rpc_server:default", "external");
3942         lp_do_parameter(GLOBAL_SECTION_SNUM, "rpc_server:svcctl", "embedded");
3943         lp_do_parameter(GLOBAL_SECTION_SNUM, "rpc_server:srvsvc", "embedded");
3944         lp_do_parameter(GLOBAL_SECTION_SNUM, "rpc_server:eventlog", "embedded");
3945         lp_do_parameter(GLOBAL_SECTION_SNUM, "rpc_server:ntsvcs", "embedded");
3946         lp_do_parameter(GLOBAL_SECTION_SNUM, "rpc_server:winreg", "embedded");
3947         lp_do_parameter(GLOBAL_SECTION_SNUM, "rpc_server:spoolss", "embedded");
3948         lp_do_parameter(GLOBAL_SECTION_SNUM, "rpc_daemon:spoolssd", "embedded");
3949         lp_do_parameter(GLOBAL_SECTION_SNUM, "rpc_server:tcpip", "no");
3950 }
3951
3952 /***************************************************************************
3953  Load the services array from the services file. Return true on success,
3954  false on failure.
3955 ***************************************************************************/
3956
3957 static bool lp_load_ex(const char *pszFname,
3958                        bool global_only,
3959                        bool save_defaults,
3960                        bool add_ipc,
3961                        bool reinit_globals,
3962                        bool allow_include_registry,
3963                        bool load_all_shares)
3964 {
3965         char *n2 = NULL;
3966         bool bRetval;
3967         TALLOC_CTX *frame = talloc_stackframe();
3968         struct loadparm_context *lp_ctx;
3969         int max_protocol, min_protocol;
3970
3971         DEBUG(3, ("lp_load_ex: refreshing parameters\n"));
3972
3973         bInGlobalSection = true;
3974         bGlobalOnly = global_only;
3975         bAllowIncludeRegistry = allow_include_registry;
3976         sDefault = _sDefault;
3977
3978         lp_ctx = setup_lp_context(talloc_tos());
3979
3980         init_globals(lp_ctx, reinit_globals);
3981
3982         free_file_list();
3983
3984         if (save_defaults) {
3985                 init_locals();
3986                 lp_save_defaults();
3987         }
3988
3989         if (!reinit_globals) {
3990                 free_param_opts(&Globals.param_opt);
3991                 apply_lp_set_cmdline();
3992         }
3993
3994         lp_do_parameter(-1, "idmap config * : backend", Globals.idmap_backend);
3995
3996         /* We get sections first, so have to start 'behind' to make up */
3997         iServiceIndex = -1;
3998
3999         if (lp_config_backend_is_file()) {
4000                 n2 = talloc_sub_basic(talloc_tos(), get_current_username(),
4001                                         current_user_info.domain,
4002                                         pszFname);
4003                 if (!n2) {
4004                         smb_panic("lp_load_ex: out of memory");
4005                 }
4006
4007                 add_to_file_list(NULL, &file_lists, pszFname, n2);
4008
4009                 bRetval = pm_process(n2, lp_do_section, do_parameter, lp_ctx);
4010                 TALLOC_FREE(n2);
4011
4012                 /* finish up the last section */
4013                 DEBUG(4, ("pm_process() returned %s\n", BOOLSTR(bRetval)));
4014                 if (bRetval) {
4015                         if (iServiceIndex >= 0) {
4016                                 bRetval = lpcfg_service_ok(ServicePtrs[iServiceIndex]);
4017                         }
4018                 }
4019
4020                 if (lp_config_backend_is_registry()) {
4021                         bool ok;
4022                         /* config backend changed to registry in config file */
4023                         /*
4024                          * We need to use this extra global variable here to
4025                          * survive restart: init_globals uses this as a default
4026                          * for config_backend. Otherwise, init_globals would
4027                          *  send us into an endless loop here.
4028                          */
4029
4030                         config_backend = CONFIG_BACKEND_REGISTRY;
4031                         /* start over */
4032                         DEBUG(1, ("lp_load_ex: changing to config backend "
4033                                   "registry\n"));
4034                         init_globals(lp_ctx, true);
4035
4036                         TALLOC_FREE(lp_ctx);
4037
4038                         lp_kill_all_services();
4039                         ok = lp_load_ex(pszFname, global_only, save_defaults,
4040                                         add_ipc, reinit_globals,
4041                                         allow_include_registry,
4042                                         load_all_shares);
4043                         TALLOC_FREE(frame);
4044                         return ok;
4045                 }
4046         } else if (lp_config_backend_is_registry()) {
4047                 bRetval = process_registry_globals();
4048         } else {
4049                 DEBUG(0, ("Illegal config  backend given: %d\n",
4050                           lp_config_backend()));
4051                 bRetval = false;
4052         }
4053
4054         if (bRetval && lp_registry_shares()) {
4055                 if (load_all_shares) {
4056                         bRetval = process_registry_shares();
4057                 } else {
4058                         bRetval = reload_registry_shares();
4059                 }
4060         }
4061
4062         {
4063                 const struct loadparm_substitution *lp_sub =
4064                         loadparm_s3_global_substitution();
4065                 char *serv = lp_auto_services(talloc_tos(), lp_sub);
4066                 lp_add_auto_services(serv);
4067                 TALLOC_FREE(serv);
4068         }
4069
4070         if (add_ipc) {
4071                 /* When 'restrict anonymous = 2' guest connections to ipc$
4072                    are denied */
4073                 lp_add_ipc("IPC$", (lp_restrict_anonymous() < 2));
4074                 if ( lp_enable_asu_support() ) {
4075                         lp_add_ipc("ADMIN$", false);
4076                 }
4077         }
4078
4079         set_allowed_client_auth();
4080
4081         if (lp_security() == SEC_ADS && strchr(lp_password_server(), ':')) {
4082                 DEBUG(1, ("WARNING: The optional ':port' in password server = %s is deprecated\n",
4083                           lp_password_server()));
4084         }
4085
4086         bLoaded = true;
4087
4088         /* Now we check we_are_a_wins_server and set szWINSserver to 127.0.0.1 */
4089         /* if we_are_a_wins_server is true and we are in the client            */
4090         if (lp_is_in_client() && Globals.we_are_a_wins_server) {
4091                 lp_do_parameter(GLOBAL_SECTION_SNUM, "wins server", "127.0.0.1");
4092         }
4093
4094         init_iconv();
4095
4096         fault_configure(smb_panic_s3);
4097
4098         /*
4099          * We run this check once the whole smb.conf is parsed, to
4100          * force some settings for the standard way a AD DC is
4101          * operated.  We may change these as our code evolves, which
4102          * is why we force these settings.
4103          */
4104         if (lp_server_role() == ROLE_ACTIVE_DIRECTORY_DC) {
4105                 lp_enforce_ad_dc_settings();
4106         }
4107
4108         bAllowIncludeRegistry = true;
4109
4110         /* Check if command line max protocol < min protocol, if so
4111          * report a warning to the user.
4112          */
4113         max_protocol = lp_client_max_protocol();
4114         min_protocol = lp_client_min_protocol();
4115         if (max_protocol < min_protocol) {
4116                 const char *max_protocolp, *min_protocolp;
4117                 max_protocolp = lpcfg_get_smb_protocol(max_protocol);
4118                 min_protocolp = lpcfg_get_smb_protocol(min_protocol);
4119                 DBG_ERR("Max protocol %s is less than min protocol %s.\n",
4120                         max_protocolp, min_protocolp);
4121         }
4122
4123         TALLOC_FREE(frame);
4124         return (bRetval);
4125 }
4126
4127 static bool lp_load(const char *pszFname,
4128                     bool global_only,
4129                     bool save_defaults,
4130                     bool add_ipc,
4131                     bool reinit_globals)
4132 {
4133         return lp_load_ex(pszFname,
4134                           global_only,
4135                           save_defaults,
4136                           add_ipc,
4137                           reinit_globals,
4138                           true,   /* allow_include_registry */
4139                           false); /* load_all_shares*/
4140 }
4141
4142 bool lp_load_initial_only(const char *pszFname)
4143 {
4144         return lp_load_ex(pszFname,
4145                           true,   /* global only */
4146                           true,   /* save_defaults */
4147                           false,  /* add_ipc */
4148                           true,   /* reinit_globals */
4149                           false,  /* allow_include_registry */
4150                           false); /* load_all_shares*/
4151 }
4152
4153 /**
4154  * most common lp_load wrapper, loading only the globals
4155  *
4156  * If this is used in a daemon or client utility it should be called
4157  * after processing popt.
4158  */
4159 bool lp_load_global(const char *file_name)
4160 {
4161         return lp_load(file_name,
4162                        true,   /* global_only */
4163                        false,  /* save_defaults */
4164                        false,  /* add_ipc */
4165                        true);  /* reinit_globals */
4166 }
4167
4168 /**
4169  * The typical lp_load wrapper with shares, loads global and
4170  * shares, including IPC, but does not force immediate
4171  * loading of all shares from registry.
4172  */
4173 bool lp_load_with_shares(const char *file_name)
4174 {
4175         return lp_load(file_name,
4176                        false,  /* global_only */
4177                        false,  /* save_defaults */
4178                        true,   /* add_ipc */
4179                        true);  /* reinit_globals */
4180 }
4181
4182 /**
4183  * lp_load wrapper, especially for clients
4184  */
4185 bool lp_load_client(const char *file_name)
4186 {
4187         lp_set_in_client(true);
4188
4189         return lp_load_global(file_name);
4190 }
4191
4192 /**
4193  * lp_load wrapper, loading only globals, but intended
4194  * for subsequent calls, not reinitializing the globals
4195  * to default values
4196  */
4197 bool lp_load_global_no_reinit(const char *file_name)
4198 {
4199         return lp_load(file_name,
4200                        true,   /* global_only */
4201                        false,  /* save_defaults */
4202                        false,  /* add_ipc */
4203                        false); /* reinit_globals */
4204 }
4205
4206 /**
4207  * lp_load wrapper, loading globals and shares,
4208  * intended for subsequent calls, i.e. not reinitializing
4209  * the globals to default values.
4210  */
4211 bool lp_load_no_reinit(const char *file_name)
4212 {
4213         return lp_load(file_name,
4214                        false,  /* global_only */
4215                        false,  /* save_defaults */
4216                        false,  /* add_ipc */
4217                        false); /* reinit_globals */
4218 }
4219
4220
4221 /**
4222  * lp_load wrapper, especially for clients, no reinitialization
4223  */
4224 bool lp_load_client_no_reinit(const char *file_name)
4225 {
4226         lp_set_in_client(true);
4227
4228         return lp_load_global_no_reinit(file_name);
4229 }
4230
4231 bool lp_load_with_registry_shares(const char *pszFname)
4232 {
4233         return lp_load_ex(pszFname,
4234                           false, /* global_only */
4235                           true,  /* save_defaults */
4236                           false, /* add_ipc */
4237                           true, /* reinit_globals */
4238                           true,  /* allow_include_registry */
4239                           true); /* load_all_shares*/
4240 }
4241
4242 /***************************************************************************
4243  Return the max number of services.
4244 ***************************************************************************/
4245
4246 int lp_numservices(void)
4247 {
4248         return (iNumServices);
4249 }
4250
4251 /***************************************************************************
4252 Display the contents of the services array in human-readable form.
4253 ***************************************************************************/
4254
4255 void lp_dump(FILE *f, bool show_defaults, int maxtoprint)
4256 {
4257         int iService;
4258         struct loadparm_context *lp_ctx;
4259
4260         if (show_defaults)
4261                 defaults_saved = false;
4262
4263         lp_ctx = setup_lp_context(talloc_tos());
4264         if (lp_ctx == NULL) {
4265                 return;
4266         }
4267
4268         lpcfg_dump_globals(lp_ctx, f, !defaults_saved);
4269
4270         lpcfg_dump_a_service(&sDefault, &sDefault, f, flags_list, show_defaults);
4271
4272         for (iService = 0; iService < maxtoprint; iService++) {
4273                 fprintf(f,"\n");
4274                 lp_dump_one(f, show_defaults, iService);
4275         }
4276         TALLOC_FREE(lp_ctx);
4277 }
4278
4279 /***************************************************************************
4280 Display the contents of one service in human-readable form.
4281 ***************************************************************************/
4282
4283 void lp_dump_one(FILE * f, bool show_defaults, int snum)
4284 {
4285         if (VALID(snum)) {
4286                 if (ServicePtrs[snum]->szService[0] == '\0')
4287                         return;
4288                 lpcfg_dump_a_service(ServicePtrs[snum], &sDefault, f,
4289                                      flags_list, show_defaults);
4290         }
4291 }
4292
4293 /***************************************************************************
4294 Return the number of the service with the given name, or -1 if it doesn't
4295 exist. Note that this is a DIFFERENT ANIMAL from the internal function
4296 getservicebyname()! This works ONLY if all services have been loaded, and
4297 does not copy the found service.
4298 ***************************************************************************/
4299
4300 int lp_servicenumber(const char *pszServiceName)
4301 {
4302         int iService;
4303         fstring serviceName;
4304
4305         if (!pszServiceName) {
4306                 return GLOBAL_SECTION_SNUM;
4307         }
4308
4309         for (iService = iNumServices - 1; iService >= 0; iService--) {
4310                 if (VALID(iService) && ServicePtrs[iService]->szService) {
4311                         /*
4312                          * The substitution here is used to support %U in
4313                          * service names
4314                          */
4315                         fstrcpy(serviceName, ServicePtrs[iService]->szService);
4316                         standard_sub_basic(get_current_username(),
4317                                            current_user_info.domain,
4318                                            serviceName,sizeof(serviceName));
4319                         if (strequal(serviceName, pszServiceName)) {
4320                                 break;
4321                         }
4322                 }
4323         }
4324
4325         if (iService >= 0 && ServicePtrs[iService]->usershare == USERSHARE_VALID) {
4326                 struct timespec last_mod;
4327
4328                 if (!usershare_exists(iService, &last_mod)) {
4329                         /* Remove the share security tdb entry for it. */
4330                         delete_share_security(lp_const_servicename(iService));
4331                         /* Remove it from the array. */
4332                         free_service_byindex(iService);
4333                         /* Doesn't exist anymore. */
4334                         return GLOBAL_SECTION_SNUM;
4335                 }
4336
4337                 /* Has it been modified ? If so delete and reload. */
4338                 if (timespec_compare(&ServicePtrs[iService]->usershare_last_mod,
4339                                      &last_mod) < 0) {
4340                         /* Remove it from the array. */
4341                         free_service_byindex(iService);
4342                         /* and now reload it. */
4343                         iService = load_usershare_service(pszServiceName);
4344                 }
4345         }
4346
4347         if (iService < 0) {
4348                 DEBUG(7,("lp_servicenumber: couldn't find %s\n", pszServiceName));
4349                 return GLOBAL_SECTION_SNUM;
4350         }
4351
4352         return (iService);
4353 }
4354
4355 /*******************************************************************
4356  A useful volume label function.
4357 ********************************************************************/
4358
4359 const char *volume_label(TALLOC_CTX *ctx, int snum)
4360 {
4361         const struct loadparm_substitution *lp_sub =
4362                 loadparm_s3_global_substitution();
4363         char *ret;
4364         const char *label = lp_volume(ctx, lp_sub, snum);
4365         size_t end = 32;
4366
4367         if (!*label) {
4368                 label = lp_servicename(ctx, lp_sub, snum);
4369         }
4370
4371         /*
4372          * Volume label can be a max of 32 bytes. Make sure to truncate
4373          * it at a codepoint boundary if it's longer than 32 and contains
4374          * multibyte characters. Windows insists on a volume label being
4375          * a valid mb sequence, and errors out if not.
4376          */
4377         if (strlen(label) > 32) {
4378                 /*
4379                  * A MB char can be a max of 5 bytes, thus
4380                  * we should have a valid mb character at a
4381                  * minimum position of (32-5) = 27.
4382                  */
4383                 while (end >= 27) {
4384                         /*
4385                          * Check if a codepoint starting from next byte
4386                          * is valid. If yes, then the current byte is the
4387                          * end of a MB or ascii sequence and the label can
4388                          * be safely truncated here. If not, keep going
4389                          * backwards till a valid codepoint is found.
4390                          */
4391                         size_t len = 0;
4392                         const char *s = &label[end];
4393                         codepoint_t c = next_codepoint(s, &len);
4394                         if (c != INVALID_CODEPOINT) {
4395                                 break;
4396                         }
4397                         end--;
4398                 }
4399         }
4400
4401         /* This returns a max of 33 byte guarenteed null terminated string. */
4402         ret = talloc_strndup(ctx, label, end);
4403         if (!ret) {
4404                 return "";
4405         }
4406         return ret;
4407 }
4408
4409 /*******************************************************************
4410  Get the default server type we will announce as via nmbd.
4411 ********************************************************************/
4412
4413 int lp_default_server_announce(void)
4414 {
4415         int default_server_announce = 0;
4416         default_server_announce |= SV_TYPE_WORKSTATION;
4417         default_server_announce |= SV_TYPE_SERVER;
4418         default_server_announce |= SV_TYPE_SERVER_UNIX;
4419
4420         /* note that the flag should be set only if we have a
4421            printer service but nmbd doesn't actually load the
4422            services so we can't tell   --jerry */
4423
4424         default_server_announce |= SV_TYPE_PRINTQ_SERVER;
4425
4426         default_server_announce |= SV_TYPE_SERVER_NT;
4427         default_server_announce |= SV_TYPE_NT;
4428
4429         switch (lp_server_role()) {
4430                 case ROLE_DOMAIN_MEMBER:
4431                         default_server_announce |= SV_TYPE_DOMAIN_MEMBER;
4432                         break;
4433                 case ROLE_DOMAIN_PDC:
4434                 case ROLE_IPA_DC:
4435                         default_server_announce |= SV_TYPE_DOMAIN_CTRL;
4436                         break;
4437                 case ROLE_DOMAIN_BDC:
4438                         default_server_announce |= SV_TYPE_DOMAIN_BAKCTRL;
4439                         break;
4440                 case ROLE_STANDALONE:
4441                 default:
4442                         break;
4443         }
4444         if (lp_time_server())
4445                 default_server_announce |= SV_TYPE_TIME_SOURCE;
4446
4447         if (lp_host_msdfs())
4448                 default_server_announce |= SV_TYPE_DFS_SERVER;
4449
4450         return default_server_announce;
4451 }
4452
4453 /***********************************************************
4454  If we are PDC then prefer us as DMB
4455 ************************************************************/
4456
4457 bool lp_domain_master(void)
4458 {
4459         if (Globals._domain_master == Auto)
4460                 return (lp_server_role() == ROLE_DOMAIN_PDC ||
4461                         lp_server_role() == ROLE_IPA_DC);
4462
4463         return (bool)Globals._domain_master;
4464 }
4465
4466 /***********************************************************
4467  If we are PDC then prefer us as DMB
4468 ************************************************************/
4469
4470 static bool lp_domain_master_true_or_auto(void)
4471 {
4472         if (Globals._domain_master) /* auto or yes */
4473                 return true;
4474
4475         return false;
4476 }
4477
4478 /***********************************************************
4479  If we are DMB then prefer us as LMB
4480 ************************************************************/
4481
4482 bool lp_preferred_master(void)
4483 {
4484         int preferred_master = lp__preferred_master();
4485
4486         if (preferred_master == Auto)
4487                 return (lp_local_master() && lp_domain_master());
4488
4489         return (bool)preferred_master;
4490 }
4491
4492 /*******************************************************************
4493  Remove a service.
4494 ********************************************************************/
4495
4496 void lp_remove_service(int snum)
4497 {
4498         ServicePtrs[snum]->valid = false;
4499 }
4500
4501 const char *lp_printername(TALLOC_CTX *ctx,
4502                            const struct loadparm_substitution *lp_sub,
4503                            int snum)
4504 {
4505         const char *ret = lp__printername(ctx, lp_sub, snum);
4506
4507         if (ret == NULL || *ret == '\0') {
4508                 ret = lp_const_servicename(snum);
4509         }
4510
4511         return ret;
4512 }
4513
4514
4515 /***********************************************************
4516  Allow daemons such as winbindd to fix their logfile name.
4517 ************************************************************/
4518
4519 void lp_set_logfile(const char *name)
4520 {
4521         lpcfg_string_set(Globals.ctx, &Globals.logfile, name);
4522         debug_set_logfile(name);
4523 }
4524
4525 /*******************************************************************
4526  Return the max print jobs per queue.
4527 ********************************************************************/
4528
4529 int lp_maxprintjobs(int snum)
4530 {
4531         int maxjobs = lp_max_print_jobs(snum);
4532
4533         if (maxjobs <= 0 || maxjobs >= PRINT_MAX_JOBID)
4534                 maxjobs = PRINT_MAX_JOBID - 1;
4535
4536         return maxjobs;
4537 }
4538
4539 const char *lp_printcapname(void)
4540 {
4541         const char *printcap_name = lp_printcap_name();
4542
4543         if ((printcap_name != NULL) &&
4544             (printcap_name[0] != '\0'))
4545                 return printcap_name;
4546
4547         if (sDefault.printing == PRINT_CUPS) {
4548                 return "cups";
4549         }
4550
4551         if (sDefault.printing == PRINT_BSD)
4552                 return "/etc/printcap";
4553
4554         return PRINTCAP_NAME;
4555 }
4556
4557 static uint32_t spoolss_state;
4558
4559 bool lp_disable_spoolss( void )
4560 {
4561         if ( spoolss_state == SVCCTL_STATE_UNKNOWN )
4562                 spoolss_state = lp__disable_spoolss() ? SVCCTL_STOPPED : SVCCTL_RUNNING;
4563
4564         return spoolss_state == SVCCTL_STOPPED ? true : false;
4565 }
4566
4567 void lp_set_spoolss_state( uint32_t state )
4568 {
4569         SMB_ASSERT( (state == SVCCTL_STOPPED) || (state == SVCCTL_RUNNING) );
4570
4571         spoolss_state = state;
4572 }
4573
4574 uint32_t lp_get_spoolss_state( void )
4575 {
4576         return lp_disable_spoolss() ? SVCCTL_STOPPED : SVCCTL_RUNNING;
4577 }
4578
4579 /*******************************************************************
4580  Turn off sendfile if we find the underlying OS doesn't support it.
4581 ********************************************************************/
4582
4583 void set_use_sendfile(int snum, bool val)
4584 {
4585         if (LP_SNUM_OK(snum))
4586                 ServicePtrs[snum]->_use_sendfile = val;
4587         else
4588                 sDefault._use_sendfile = val;
4589 }
4590
4591 void lp_set_mangling_method(const char *new_method)
4592 {
4593         lpcfg_string_set(Globals.ctx, &Globals.mangling_method, new_method);
4594 }
4595
4596 /*******************************************************************
4597  Global state for POSIX pathname processing.
4598 ********************************************************************/
4599
4600 static bool posix_pathnames;
4601
4602 bool lp_posix_pathnames(void)
4603 {
4604         return posix_pathnames;
4605 }
4606
4607 /*******************************************************************
4608  Change everything needed to ensure POSIX pathname processing (currently
4609  not much).
4610 ********************************************************************/
4611
4612 void lp_set_posix_pathnames(void)
4613 {
4614         posix_pathnames = true;
4615 }
4616
4617 /*******************************************************************
4618  Global state for POSIX lock processing - CIFS unix extensions.
4619 ********************************************************************/
4620
4621 bool posix_default_lock_was_set;
4622 static enum brl_flavour posix_cifsx_locktype; /* By default 0 == WINDOWS_LOCK */
4623
4624 enum brl_flavour lp_posix_cifsu_locktype(files_struct *fsp)
4625 {
4626         if (posix_default_lock_was_set) {
4627                 return posix_cifsx_locktype;
4628         } else {
4629                 return (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) ?
4630                         POSIX_LOCK : WINDOWS_LOCK;
4631         }
4632 }
4633
4634 /*******************************************************************
4635 ********************************************************************/
4636
4637 void lp_set_posix_default_cifsx_readwrite_locktype(enum brl_flavour val)
4638 {
4639         posix_default_lock_was_set = true;
4640         posix_cifsx_locktype = val;
4641 }
4642
4643 int lp_min_receive_file_size(void)
4644 {
4645         int min_receivefile_size = lp_min_receivefile_size();
4646
4647         if (min_receivefile_size < 0) {
4648                 return 0;
4649         }
4650         return min_receivefile_size;
4651 }
4652
4653 /*******************************************************************
4654  Safe wide links checks.
4655  This helper function always verify the validity of wide links,
4656  even after a configuration file reload.
4657 ********************************************************************/
4658
4659 void widelinks_warning(int snum)
4660 {
4661         if (lp_allow_insecure_wide_links()) {
4662                 return;
4663         }
4664
4665         if (lp_wide_links(snum)) {
4666                 if (lp_smb1_unix_extensions()) {
4667                         DBG_ERR("Share '%s' has wide links and SMB1 unix "
4668                         "extensions enabled. "
4669                         "These parameters are incompatible. "
4670                         "Wide links will be disabled for this share.\n",
4671                          lp_const_servicename(snum));
4672                 } else if (lp_smb2_unix_extensions()) {
4673                         DBG_ERR("Share '%s' has wide links and SMB2 unix "
4674                         "extensions enabled. "
4675                         "These parameters are incompatible. "
4676                         "Wide links will be disabled for this share.\n",
4677                          lp_const_servicename(snum));
4678                 }
4679         }
4680 }
4681
4682 bool lp_widelinks(int snum)
4683 {
4684         /* wide links is always incompatible with unix extensions */
4685         if (lp_smb1_unix_extensions() || lp_smb2_unix_extensions()) {
4686                 /*
4687                  * Unless we have "allow insecure widelinks"
4688                  * turned on.
4689                  */
4690                 if (!lp_allow_insecure_wide_links()) {
4691                         return false;
4692                 }
4693         }
4694
4695         return lp_wide_links(snum);
4696 }
4697
4698 int lp_server_role(void)
4699 {
4700         return lp_find_server_role(lp__server_role(),
4701                                    lp__security(),
4702                                    lp__domain_logons(),
4703                                    lp_domain_master_true_or_auto());
4704 }
4705
4706 int lp_security(void)
4707 {
4708         return lp_find_security(lp__server_role(),
4709                                 lp__security());
4710 }
4711
4712 int lp_client_max_protocol(void)
4713 {
4714         int client_max_protocol = lp__client_max_protocol();
4715         if (client_max_protocol == PROTOCOL_DEFAULT) {
4716                 return PROTOCOL_LATEST;
4717         }
4718         return client_max_protocol;
4719 }
4720
4721 int lp_client_ipc_min_protocol(void)
4722 {
4723         int client_ipc_min_protocol = lp__client_ipc_min_protocol();
4724         if (client_ipc_min_protocol == PROTOCOL_DEFAULT) {
4725                 client_ipc_min_protocol = lp_client_min_protocol();
4726         }
4727         if (client_ipc_min_protocol < PROTOCOL_NT1) {
4728                 return PROTOCOL_NT1;
4729         }
4730         return client_ipc_min_protocol;
4731 }
4732
4733 int lp_client_ipc_max_protocol(void)
4734 {
4735         int client_ipc_max_protocol = lp__client_ipc_max_protocol();
4736         if (client_ipc_max_protocol == PROTOCOL_DEFAULT) {
4737                 return PROTOCOL_LATEST;
4738         }
4739         if (client_ipc_max_protocol < PROTOCOL_NT1) {
4740                 return PROTOCOL_NT1;
4741         }
4742         return client_ipc_max_protocol;
4743 }
4744
4745 int lp_client_ipc_signing(void)
4746 {
4747         int client_ipc_signing = lp__client_ipc_signing();
4748         if (client_ipc_signing == SMB_SIGNING_DEFAULT) {
4749                 return SMB_SIGNING_REQUIRED;
4750         }
4751         return client_ipc_signing;
4752 }
4753
4754 enum credentials_use_kerberos lp_client_use_kerberos(void)
4755 {
4756         if (lp_weak_crypto() == SAMBA_WEAK_CRYPTO_DISALLOWED) {
4757                 return CRED_USE_KERBEROS_REQUIRED;
4758         }
4759
4760         return lp__client_use_kerberos();
4761 }
4762
4763
4764 int lp_rpc_low_port(void)
4765 {
4766         return Globals.rpc_low_port;
4767 }
4768
4769 int lp_rpc_high_port(void)
4770 {
4771         return Globals.rpc_high_port;
4772 }
4773
4774 /*
4775  * Do not allow LanMan auth if unless NTLMv1 is also allowed
4776  *
4777  * This also ensures it is disabled if NTLM is totally disabled
4778  */
4779 bool lp_lanman_auth(void)
4780 {
4781         enum ntlm_auth_level ntlm_auth_level = lp_ntlm_auth();
4782
4783         if (ntlm_auth_level == NTLM_AUTH_ON) {
4784                 return lp__lanman_auth();
4785         } else {
4786                 return false;
4787         }
4788 }
4789
4790 struct loadparm_global * get_globals(void)
4791 {
4792         return &Globals;
4793 }
4794
4795 unsigned int * get_flags(void)
4796 {
4797         if (flags_list == NULL) {
4798                 flags_list = talloc_zero_array(NULL, unsigned int, num_parameters());
4799         }
4800
4801         return flags_list;
4802 }
4803
4804 enum samba_weak_crypto lp_weak_crypto()
4805 {
4806         if (Globals.weak_crypto == SAMBA_WEAK_CRYPTO_UNKNOWN) {
4807                 Globals.weak_crypto = SAMBA_WEAK_CRYPTO_DISALLOWED;
4808
4809                 if (samba_gnutls_weak_crypto_allowed()) {
4810                         Globals.weak_crypto = SAMBA_WEAK_CRYPTO_ALLOWED;
4811                 }
4812         }
4813
4814         return Globals.weak_crypto;
4815 }
4816
4817 uint32_t lp_get_async_dns_timeout(void)
4818 {
4819         /*
4820          * Clamp minimum async dns timeout to 1 second
4821          * as per the man page.
4822          */
4823         return MAX(Globals.async_dns_timeout, 1);
4824 }
4825
4826 /* SMB2 POSIX extensions. For now, *always* disabled. */
4827 bool lp_smb2_unix_extensions(void)
4828 {
4829         return false;
4830 }