param: remove fstring from dump_a_parameter
[Samba.git] / source3 / param / loadparm.c
blob358e62089e5316daafb8189023aa23e4f320a9f2
1 /*
2 Unix SMB/CIFS implementation.
3 Parameter loading functions
4 Copyright (C) Karl Auer 1993-1998
6 Largely re-written by Andrew Tridgell, September 1994
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
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.
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.
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/>.
31 * Load parameters.
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.
37 * To add a parameter:
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
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!
56 #include "includes.h"
57 #include "system/filesys.h"
58 #include "util_tdb.h"
59 #include "lib/param/loadparm.h"
60 #include "lib/param/param.h"
61 #include "printing.h"
62 #include "lib/smbconf/smbconf.h"
63 #include "lib/smbconf/smbconf_init.h"
65 #include "ads.h"
66 #include "../librpc/gen_ndr/svcctl.h"
67 #include "intl.h"
68 #include "../libcli/smb/smb_signing.h"
69 #include "dbwrap/dbwrap.h"
70 #include "dbwrap/dbwrap_rbt.h"
71 #include "../lib/util/bitmap.h"
73 #ifdef HAVE_SYS_SYSCTL_H
74 #include <sys/sysctl.h>
75 #endif
77 #ifdef HAVE_HTTPCONNECTENCRYPT
78 #include <cups/http.h>
79 #endif
81 bool bLoaded = false;
83 extern userdom_struct current_user_info;
85 /* the special value for the include parameter
86 * to be interpreted not as a file name but to
87 * trigger loading of the global smb.conf options
88 * from registry. */
89 #ifndef INCLUDE_REGISTRY_NAME
90 #define INCLUDE_REGISTRY_NAME "registry"
91 #endif
93 static bool in_client = false; /* Not in the client by default */
94 static struct smbconf_csn conf_last_csn;
96 static int config_backend = CONFIG_BACKEND_FILE;
98 /* some helpful bits */
99 #define LP_SNUM_OK(i) (((i) >= 0) && ((i) < iNumServices) && (ServicePtrs != NULL) && ServicePtrs[(i)]->valid)
100 #define VALID(i) (ServicePtrs != NULL && ServicePtrs[i]->valid)
102 #define USERSHARE_VALID 1
103 #define USERSHARE_PENDING_DELETE 2
105 static bool defaults_saved = false;
107 #include "lib/param/param_global.h"
109 static struct loadparm_global Globals;
111 /* This is a default service used to prime a services structure */
112 static struct loadparm_service sDefault =
114 .valid = true,
115 .autoloaded = false,
116 .usershare = 0,
117 .usershare_last_mod = {0, 0},
118 .szService = NULL,
119 .path = NULL,
120 .username = NULL,
121 .invalid_users = NULL,
122 .valid_users = NULL,
123 .admin_users = NULL,
124 .szCopy = NULL,
125 .szInclude = NULL,
126 .preexec = NULL,
127 .postexec = NULL,
128 .root_preexec = NULL,
129 .root_postexec = NULL,
130 .cups_options = NULL,
131 .print_command = NULL,
132 .lpq_command = NULL,
133 .lprm_command = NULL,
134 .lppause_command = NULL,
135 .lpresume_command = NULL,
136 .queuepause_command = NULL,
137 .queueresume_command = NULL,
138 ._printername = NULL,
139 .printjob_username = NULL,
140 .dont_descend = NULL,
141 .hosts_allow = NULL,
142 .hosts_deny = NULL,
143 .magic_script = NULL,
144 .magic_output = NULL,
145 .veto_files = NULL,
146 .hide_files = NULL,
147 .veto_oplock_files = NULL,
148 .comment = NULL,
149 .force_user = NULL,
150 .force_group = NULL,
151 .read_list = NULL,
152 .write_list = NULL,
153 .volume = NULL,
154 .fstype = NULL,
155 .vfs_objects = NULL,
156 .msdfs_proxy = NULL,
157 .aio_write_behind = NULL,
158 .dfree_command = NULL,
159 .min_print_space = 0,
160 .iMaxPrintJobs = 1000,
161 .max_reported_print_jobs = 0,
162 .write_cache_size = 0,
163 .create_mask = 0744,
164 .force_create_mode = 0,
165 .directory_mask = 0755,
166 .force_directory_mode = 0,
167 .max_connections = 0,
168 .default_case = CASE_LOWER,
169 .printing = DEFAULT_PRINTING,
170 .oplock_contention_limit = 2,
171 .csc_policy = 0,
172 .block_size = 1024,
173 .dfree_cache_time = 0,
174 .preexec_close = false,
175 .root_preexec_close = false,
176 .case_sensitive = Auto,
177 .preserve_case = true,
178 .short_preserve_case = true,
179 .hide_dot_files = true,
180 .hide_special_files = false,
181 .hide_unreadable = false,
182 .hide_unwriteable_files = false,
183 .browseable = true,
184 .access_based_share_enum = false,
185 .bAvailable = true,
186 .read_only = true,
187 .guest_only = false,
188 .administrative_share = false,
189 .guest_ok = false,
190 .printable = false,
191 .print_notify_backchannel = false,
192 .map_system = false,
193 .map_hidden = false,
194 .map_archive = true,
195 .store_dos_attributes = false,
196 .dmapi_support = false,
197 .locking = true,
198 .strict_locking = Auto,
199 .posix_locking = true,
200 .oplocks = true,
201 .kernel_oplocks = false,
202 .level2_oplocks = true,
203 .only_user = false,
204 .mangled_names = true,
205 .bWidelinks = false,
206 .follow_symlinks = true,
207 .sync_always = false,
208 .strict_allocate = false,
209 .strict_sync = false,
210 .mangling_char = '~',
211 .copymap = NULL,
212 .delete_readonly = false,
213 .fake_oplocks = false,
214 .delete_veto_files = false,
215 .dos_filemode = false,
216 .dos_filetimes = true,
217 .dos_filetime_resolution = false,
218 .fake_directory_create_times = false,
219 .blocking_locks = true,
220 .inherit_permissions = false,
221 .inherit_acls = false,
222 .inherit_owner = false,
223 .msdfs_root = false,
224 .use_client_driver = false,
225 .default_devmode = true,
226 .force_printername = false,
227 .nt_acl_support = true,
228 .force_unknown_acl_user = false,
229 ._use_sendfile = false,
230 .profile_acls = false,
231 .map_acl_inherit = false,
232 .afs_share = false,
233 .ea_support = false,
234 .acl_check_permissions = true,
235 .acl_map_full_control = true,
236 .acl_group_control = false,
237 .acl_allow_execute_always = false,
238 .change_notify = true,
239 .kernel_change_notify = true,
240 .allocation_roundup_size = SMB_ROUNDUP_ALLOCATION_SIZE,
241 .aio_read_size = 0,
242 .aio_write_size = 0,
243 .map_readonly = MAP_READONLY_YES,
244 .directory_name_cache_size = 100,
245 .smb_encrypt = SMB_SIGNING_DEFAULT,
246 .kernel_share_modes = true,
247 .durable_handles = true,
248 .param_opt = NULL,
249 .dummy = ""
252 /* local variables */
253 static struct loadparm_service **ServicePtrs = NULL;
254 static int iNumServices = 0;
255 static int iServiceIndex = 0;
256 static struct db_context *ServiceHash;
257 static bool bInGlobalSection = true;
258 static bool bGlobalOnly = false;
259 static struct file_lists *file_lists = NULL;
261 #define NUMPARAMETERS (num_parameters())
263 static void set_allowed_client_auth(void);
265 static bool lp_set_cmdline_helper(const char *pszParmName, const char *pszParmValue, bool store_values);
266 static void free_param_opts(struct parmlist_entry **popts);
268 /* this is used to prevent lots of mallocs of size 1 */
269 static const char null_string[] = "";
272 Free a string value.
275 static void string_free(char **s)
277 if (!s || !(*s))
278 return;
279 if (*s == null_string)
280 *s = NULL;
281 TALLOC_FREE(*s);
285 Set a string value, deallocating any existing space, and allocing the space
286 for the string
289 static bool string_set(TALLOC_CTX *mem_ctx, char **dest,const char *src)
291 string_free(dest);
293 if (!src) {
294 src = "";
297 (*dest) = talloc_strdup(mem_ctx, src);
298 if ((*dest) == NULL) {
299 DEBUG(0,("Out of memory in string_init\n"));
300 return false;
303 return true;
306 bool lp_string_set(char **dest, const char *src) {
307 return string_set(Globals.ctx, dest, src);
310 /***************************************************************************
311 Initialise the sDefault parameter structure for the printer values.
312 ***************************************************************************/
314 void init_printer_values(TALLOC_CTX *ctx, struct loadparm_service *pService)
316 /* choose defaults depending on the type of printing */
317 switch (pService->printing) {
318 case PRINT_BSD:
319 case PRINT_AIX:
320 case PRINT_LPRNT:
321 case PRINT_LPROS2:
322 lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P'%p'");
323 lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P'%p' %j");
324 lpcfg_string_set(ctx, &pService->print_command, "lpr -r -P'%p' %s");
325 break;
327 case PRINT_LPRNG:
328 case PRINT_PLP:
329 lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P'%p'");
330 lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P'%p' %j");
331 lpcfg_string_set(ctx, &pService->print_command, "lpr -r -P'%p' %s");
332 lpcfg_string_set(ctx, &pService->queuepause_command, "lpc stop '%p'");
333 lpcfg_string_set(ctx, &pService->queueresume_command, "lpc start '%p'");
334 lpcfg_string_set(ctx, &pService->lppause_command, "lpc hold '%p' %j");
335 lpcfg_string_set(ctx, &pService->lpresume_command, "lpc release '%p' %j");
336 break;
338 case PRINT_CUPS:
339 case PRINT_IPRINT:
340 /* set the lpq command to contain the destination printer
341 name only. This is used by cups_queue_get() */
342 lpcfg_string_set(ctx, &pService->lpq_command, "%p");
343 lpcfg_string_set(ctx, &pService->lprm_command, "");
344 lpcfg_string_set(ctx, &pService->print_command, "");
345 lpcfg_string_set(ctx, &pService->lppause_command, "");
346 lpcfg_string_set(ctx, &pService->lpresume_command, "");
347 lpcfg_string_set(ctx, &pService->queuepause_command, "");
348 lpcfg_string_set(ctx, &pService->queueresume_command, "");
349 break;
351 case PRINT_SYSV:
352 case PRINT_HPUX:
353 lpcfg_string_set(ctx, &pService->lpq_command, "lpstat -o%p");
354 lpcfg_string_set(ctx, &pService->lprm_command, "cancel %p-%j");
355 lpcfg_string_set(ctx, &pService->print_command, "lp -c -d%p %s; rm %s");
356 lpcfg_string_set(ctx, &pService->queuepause_command, "disable %p");
357 lpcfg_string_set(ctx, &pService->queueresume_command, "enable %p");
358 #ifndef HPUX
359 lpcfg_string_set(ctx, &pService->lppause_command, "lp -i %p-%j -H hold");
360 lpcfg_string_set(ctx, &pService->lpresume_command, "lp -i %p-%j -H resume");
361 #endif /* HPUX */
362 break;
364 case PRINT_QNX:
365 lpcfg_string_set(ctx, &pService->lpq_command, "lpq -P%p");
366 lpcfg_string_set(ctx, &pService->lprm_command, "lprm -P%p %j");
367 lpcfg_string_set(ctx, &pService->print_command, "lp -r -P%p %s");
368 break;
370 #if defined(DEVELOPER) || defined(ENABLE_SELFTEST)
372 case PRINT_TEST:
373 case PRINT_VLP: {
374 const char *tdbfile;
375 TALLOC_CTX *tmp_ctx = talloc_new(ctx);
376 char *tmp;
378 tdbfile = talloc_asprintf(
379 tmp_ctx, "tdbfile=%s",
380 lp_parm_const_string(-1, "vlp", "tdbfile",
381 "/tmp/vlp.tdb"));
382 if (tdbfile == NULL) {
383 tdbfile="tdbfile=/tmp/vlp.tdb";
386 tmp = talloc_asprintf(tmp_ctx, "vlp %s print %%p %%s",
387 tdbfile);
388 lpcfg_string_set(ctx, &pService->print_command,
389 tmp ? tmp : "vlp print %p %s");
391 tmp = talloc_asprintf(tmp_ctx, "vlp %s lpq %%p",
392 tdbfile);
393 lpcfg_string_set(ctx, &pService->lpq_command,
394 tmp ? tmp : "vlp lpq %p");
396 tmp = talloc_asprintf(tmp_ctx, "vlp %s lprm %%p %%j",
397 tdbfile);
398 lpcfg_string_set(ctx, &pService->lprm_command,
399 tmp ? tmp : "vlp lprm %p %j");
401 tmp = talloc_asprintf(tmp_ctx, "vlp %s lppause %%p %%j",
402 tdbfile);
403 lpcfg_string_set(ctx, &pService->lppause_command,
404 tmp ? tmp : "vlp lppause %p %j");
406 tmp = talloc_asprintf(tmp_ctx, "vlp %s lpresume %%p %%j",
407 tdbfile);
408 lpcfg_string_set(ctx, &pService->lpresume_command,
409 tmp ? tmp : "vlp lpresume %p %j");
411 tmp = talloc_asprintf(tmp_ctx, "vlp %s queuepause %%p",
412 tdbfile);
413 lpcfg_string_set(ctx, &pService->queuepause_command,
414 tmp ? tmp : "vlp queuepause %p");
416 tmp = talloc_asprintf(tmp_ctx, "vlp %s queueresume %%p",
417 tdbfile);
418 lpcfg_string_set(ctx, &pService->queueresume_command,
419 tmp ? tmp : "vlp queueresume %p");
420 TALLOC_FREE(tmp_ctx);
422 break;
424 #endif /* DEVELOPER */
429 * Function to return the default value for the maximum number of open
430 * file descriptors permitted. This function tries to consult the
431 * kernel-level (sysctl) and ulimit (getrlimit()) values and goes
432 * the smaller of those.
434 static int max_open_files(void)
436 int sysctl_max = MAX_OPEN_FILES;
437 int rlimit_max = MAX_OPEN_FILES;
439 #ifdef HAVE_SYSCTLBYNAME
441 size_t size = sizeof(sysctl_max);
442 sysctlbyname("kern.maxfilesperproc", &sysctl_max, &size, NULL,
445 #endif
447 #if (defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE))
449 struct rlimit rl;
451 ZERO_STRUCT(rl);
453 if (getrlimit(RLIMIT_NOFILE, &rl) == 0)
454 rlimit_max = rl.rlim_cur;
456 #if defined(RLIM_INFINITY)
457 if(rl.rlim_cur == RLIM_INFINITY)
458 rlimit_max = MAX_OPEN_FILES;
459 #endif
461 #endif
463 if (sysctl_max < MIN_OPEN_FILES_WINDOWS) {
464 DEBUG(2,("max_open_files: increasing sysctl_max (%d) to "
465 "minimum Windows limit (%d)\n",
466 sysctl_max,
467 MIN_OPEN_FILES_WINDOWS));
468 sysctl_max = MIN_OPEN_FILES_WINDOWS;
471 if (rlimit_max < MIN_OPEN_FILES_WINDOWS) {
472 DEBUG(2,("rlimit_max: increasing rlimit_max (%d) to "
473 "minimum Windows limit (%d)\n",
474 rlimit_max,
475 MIN_OPEN_FILES_WINDOWS));
476 rlimit_max = MIN_OPEN_FILES_WINDOWS;
479 return MIN(sysctl_max, rlimit_max);
483 * Common part of freeing allocated data for one parameter.
485 static void free_one_parameter_common(void *parm_ptr,
486 struct parm_struct parm)
488 if ((parm.type == P_STRING) ||
489 (parm.type == P_USTRING))
491 string_free((char**)parm_ptr);
492 } else if (parm.type == P_LIST) {
493 TALLOC_FREE(*((char***)parm_ptr));
498 * Free the allocated data for one parameter for a share
499 * given as a service struct.
501 static void free_one_parameter(struct loadparm_service *service,
502 struct parm_struct parm)
504 void *parm_ptr;
506 if (parm.p_class != P_LOCAL) {
507 return;
510 parm_ptr = lp_parm_ptr(service, &parm);
512 free_one_parameter_common(parm_ptr, parm);
516 * Free the allocated parameter data of a share given
517 * as a service struct.
519 static void free_parameters(struct loadparm_service *service)
521 uint32_t i;
523 for (i=0; parm_table[i].label; i++) {
524 free_one_parameter(service, parm_table[i]);
529 * Free the allocated data for one parameter for a given share
530 * specified by an snum.
532 static void free_one_parameter_by_snum(int snum, struct parm_struct parm)
534 void *parm_ptr;
536 if (snum < 0) {
537 parm_ptr = lp_parm_ptr(NULL, &parm);
538 } else if (parm.p_class != P_LOCAL) {
539 return;
540 } else {
541 parm_ptr = lp_local_ptr_by_snum(snum, &parm);
544 free_one_parameter_common(parm_ptr, parm);
548 * Free the allocated parameter data for a share specified
549 * by an snum.
551 static void free_parameters_by_snum(int snum)
553 uint32_t i;
555 for (i=0; parm_table[i].label; i++) {
556 free_one_parameter_by_snum(snum, parm_table[i]);
561 * Free the allocated global parameters.
563 static void free_global_parameters(void)
565 free_param_opts(&Globals.param_opt);
566 free_parameters_by_snum(GLOBAL_SECTION_SNUM);
567 TALLOC_FREE(Globals.ctx);
570 struct lp_stored_option {
571 struct lp_stored_option *prev, *next;
572 const char *label;
573 const char *value;
576 static struct lp_stored_option *stored_options;
579 save options set by lp_set_cmdline() into a list. This list is
580 re-applied when we do a globals reset, so that cmdline set options
581 are sticky across reloads of smb.conf
583 static bool store_lp_set_cmdline(const char *pszParmName, const char *pszParmValue)
585 struct lp_stored_option *entry, *entry_next;
586 for (entry = stored_options; entry != NULL; entry = entry_next) {
587 entry_next = entry->next;
588 if (strcmp(pszParmName, entry->label) == 0) {
589 DLIST_REMOVE(stored_options, entry);
590 talloc_free(entry);
591 break;
595 entry = talloc(NULL, struct lp_stored_option);
596 if (!entry) {
597 return false;
600 entry->label = talloc_strdup(entry, pszParmName);
601 if (!entry->label) {
602 talloc_free(entry);
603 return false;
606 entry->value = talloc_strdup(entry, pszParmValue);
607 if (!entry->value) {
608 talloc_free(entry);
609 return false;
612 DLIST_ADD_END(stored_options, entry, struct lp_stored_option);
614 return true;
617 static bool apply_lp_set_cmdline(void)
619 struct lp_stored_option *entry = NULL;
620 for (entry = stored_options; entry != NULL; entry = entry->next) {
621 if (!lp_set_cmdline_helper(entry->label, entry->value, false)) {
622 DEBUG(0, ("Failed to re-apply cmdline parameter %s = %s\n",
623 entry->label, entry->value));
624 return false;
627 return true;
630 /***************************************************************************
631 Initialise the global parameter structure.
632 ***************************************************************************/
634 static void init_globals(bool reinit_globals)
636 static bool done_init = false;
637 char *s = NULL;
638 int i;
640 /* If requested to initialize only once and we've already done it... */
641 if (!reinit_globals && done_init) {
642 /* ... then we have nothing more to do */
643 return;
646 if (!done_init) {
647 /* The logfile can be set before this is invoked. Free it if so. */
648 if (Globals.logfile != NULL) {
649 string_free(&Globals.logfile);
650 Globals.logfile = NULL;
652 done_init = true;
653 } else {
654 free_global_parameters();
657 /* This memset and the free_global_parameters() above will
658 * wipe out smb.conf options set with lp_set_cmdline(). The
659 * apply_lp_set_cmdline() call puts these values back in the
660 * table once the defaults are set */
661 ZERO_STRUCT(Globals);
663 Globals.ctx = talloc_pooled_object(NULL, char, 272, 2048);
665 for (i = 0; parm_table[i].label; i++) {
666 if ((parm_table[i].type == P_STRING ||
667 parm_table[i].type == P_USTRING))
669 string_set(Globals.ctx, (char **)lp_parm_ptr(NULL, &parm_table[i]), "");
674 string_set(Globals.ctx, &sDefault.fstype, FSTYPE_STRING);
675 string_set(Globals.ctx, &sDefault.printjob_username, "%U");
677 init_printer_values(Globals.ctx, &sDefault);
679 sDefault.ntvfs_handler = (const char **)str_list_make_v3(NULL, "unixuid default", NULL);
681 DEBUG(3, ("Initialising global parameters\n"));
683 /* Must manually force to upper case here, as this does not go via the handler */
684 string_set(Globals.ctx, &Globals.netbios_name, myhostname_upper());
686 string_set(Globals.ctx, &Globals.smb_passwd_file, get_dyn_SMB_PASSWD_FILE());
687 string_set(Globals.ctx, &Globals.private_dir, get_dyn_PRIVATE_DIR());
689 /* use the new 'hash2' method by default, with a prefix of 1 */
690 string_set(Globals.ctx, &Globals.mangling_method, "hash2");
691 Globals.mangle_prefix = 1;
693 string_set(Globals.ctx, &Globals.guest_account, GUEST_ACCOUNT);
695 /* using UTF8 by default allows us to support all chars */
696 string_set(Globals.ctx, &Globals.unix_charset, DEFAULT_UNIX_CHARSET);
698 /* Use codepage 850 as a default for the dos character set */
699 string_set(Globals.ctx, &Globals.dos_charset, DEFAULT_DOS_CHARSET);
702 * Allow the default PASSWD_CHAT to be overridden in local.h.
704 string_set(Globals.ctx, &Globals.passwd_chat, DEFAULT_PASSWD_CHAT);
706 string_set(Globals.ctx, &Globals.workgroup, DEFAULT_WORKGROUP);
708 string_set(Globals.ctx, &Globals.passwd_program, "");
709 string_set(Globals.ctx, &Globals.lock_directory, get_dyn_LOCKDIR());
710 string_set(Globals.ctx, &Globals.state_directory, get_dyn_STATEDIR());
711 string_set(Globals.ctx, &Globals.cache_directory, get_dyn_CACHEDIR());
712 string_set(Globals.ctx, &Globals.pid_directory, get_dyn_PIDDIR());
713 string_set(Globals.ctx, &Globals.nbt_client_socket_address, "0.0.0.0");
715 * By default support explicit binding to broadcast
716 * addresses.
718 Globals.nmbd_bind_explicit_broadcast = true;
720 s = talloc_asprintf(talloc_tos(), "Samba %s", samba_version_string());
721 if (s == NULL) {
722 smb_panic("init_globals: ENOMEM");
724 string_set(Globals.ctx, &Globals.server_string, s);
725 TALLOC_FREE(s);
726 #ifdef DEVELOPER
727 string_set(Globals.ctx, &Globals.panic_action, "/bin/sleep 999999999");
728 #endif
730 string_set(Globals.ctx, &Globals.socket_options, DEFAULT_SOCKET_OPTIONS);
732 string_set(Globals.ctx, &Globals.logon_drive, "");
733 /* %N is the NIS auto.home server if -DAUTOHOME is used, else same as %L */
734 string_set(Globals.ctx, &Globals.logon_home, "\\\\%N\\%U");
735 string_set(Globals.ctx, &Globals.logon_path, "\\\\%N\\%U\\profile");
737 Globals.name_resolve_order = (const char **)str_list_make_v3(NULL, "lmhosts wins host bcast", NULL);
738 string_set(Globals.ctx, &Globals.password_server, "*");
740 Globals.algorithmic_rid_base = BASE_RID;
742 Globals.load_printers = true;
743 Globals.printcap_cache_time = 750; /* 12.5 minutes */
745 Globals.config_backend = config_backend;
746 Globals._server_role = ROLE_AUTO;
748 /* Was 65535 (0xFFFF). 0x4101 matches W2K and causes major speed improvements... */
749 /* Discovered by 2 days of pain by Don McCall @ HP :-). */
750 Globals.max_xmit = 0x4104;
751 Globals.max_mux = 50; /* This is *needed* for profile support. */
752 Globals.lpq_cache_time = 30; /* changed to handle large print servers better -- jerry */
753 Globals._disable_spoolss = false;
754 Globals.max_smbd_processes = 0;/* no limit specified */
755 Globals.username_level = 0;
756 Globals.deadtime = 0;
757 Globals.getwd_cache = true;
758 Globals.large_readwrite = true;
759 Globals.max_log_size = 5000;
760 Globals.max_open_files = max_open_files();
761 Globals.server_max_protocol = PROTOCOL_SMB3_00;
762 Globals.server_min_protocol = PROTOCOL_LANMAN1;
763 Globals.client_max_protocol = PROTOCOL_NT1;
764 Globals.client_min_protocol = PROTOCOL_CORE;
765 Globals._security = SEC_AUTO;
766 Globals.encrypt_passwords = true;
767 Globals.client_schannel = Auto;
768 Globals.winbind_sealed_pipes = true;
769 Globals.require_strong_key = true;
770 Globals.server_schannel = Auto;
771 Globals.read_raw = true;
772 Globals.write_raw = true;
773 Globals.null_passwords = false;
774 Globals.old_password_allowed_period = 60;
775 Globals.obey_pam_restrictions = false;
776 Globals.syslog = 1;
777 Globals.syslog_only = false;
778 Globals.timestamp_logs = true;
779 string_set(Globals.ctx, &Globals.log_level, "0");
780 Globals.debug_prefix_timestamp = false;
781 Globals.debug_hires_timestamp = true;
782 Globals.debug_pid = false;
783 Globals.debug_uid = false;
784 Globals.debug_class = false;
785 Globals.enable_core_files = true;
786 Globals.max_ttl = 60 * 60 * 24 * 3; /* 3 days default. */
787 Globals.max_wins_ttl = 60 * 60 * 24 * 6; /* 6 days default. */
788 Globals.min_wins_ttl = 60 * 60 * 6; /* 6 hours default. */
789 Globals.machine_password_timeout = 60 * 60 * 24 * 7; /* 7 days default. */
790 Globals.lm_announce = Auto; /* = Auto: send only if LM clients found */
791 Globals.lm_interval = 60;
792 #if (defined(HAVE_NETGROUP) && defined(WITH_AUTOMOUNT))
793 Globals.nis_homedir = false;
794 #ifdef WITH_NISPLUS_HOME
795 string_set(Globals.ctx, &Globals.homedir_map, "auto_home.org_dir");
796 #else
797 string_set(Globals.ctx, &Globals.homedir_map, "auto.home");
798 #endif
799 #endif
800 Globals.time_server = false;
801 Globals.bind_interfaces_only = false;
802 Globals.unix_password_sync = false;
803 Globals.pam_password_change = false;
804 Globals.passwd_chat_debug = false;
805 Globals.passwd_chat_timeout = 2; /* 2 second default. */
806 Globals.nt_pipe_support = true; /* Do NT pipes by default. */
807 Globals.nt_status_support = true; /* Use NT status by default. */
808 Globals.stat_cache = true; /* use stat cache by default */
809 Globals.max_stat_cache_size = 256; /* 256k by default */
810 Globals.restrict_anonymous = 0;
811 Globals.client_lanman_auth = false; /* Do NOT use the LanMan hash if it is available */
812 Globals.client_plaintext_auth = false; /* Do NOT use a plaintext password even if is requested by the server */
813 Globals.lanman_auth = false; /* Do NOT use the LanMan hash, even if it is supplied */
814 Globals.ntlm_auth = true; /* Do use NTLMv1 if it is supplied by the client (otherwise NTLMv2) */
815 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 */
816 /* Note, that we will also use NTLM2 session security (which is different), if it is available */
818 Globals.map_to_guest = 0; /* By Default, "Never" */
819 Globals.oplock_break_wait_time = 0; /* By Default, 0 msecs. */
820 Globals.enhanced_browsing = true;
821 Globals.lock_spin_time = WINDOWS_MINIMUM_LOCK_TIMEOUT_MS; /* msec. */
822 #ifdef MMAP_BLACKLIST
823 Globals.use_mmap = false;
824 #else
825 Globals.use_mmap = true;
826 #endif
827 Globals.unicode = true;
828 Globals.unix_extensions = true;
829 Globals.reset_on_zero_vc = false;
830 Globals.log_writeable_files_on_exit = false;
831 Globals.create_krb5_conf = true;
832 Globals.winbindMaxDomainConnections = 1;
834 /* hostname lookups can be very expensive and are broken on
835 a large number of sites (tridge) */
836 Globals.hostname_lookups = false;
838 string_set(Globals.ctx, &Globals.passdb_backend, "tdbsam");
839 string_set(Globals.ctx, &Globals.ldap_suffix, "");
840 string_set(Globals.ctx, &Globals.szLdapMachineSuffix, "");
841 string_set(Globals.ctx, &Globals.szLdapUserSuffix, "");
842 string_set(Globals.ctx, &Globals.szLdapGroupSuffix, "");
843 string_set(Globals.ctx, &Globals.szLdapIdmapSuffix, "");
845 string_set(Globals.ctx, &Globals.ldap_admin_dn, "");
846 Globals.ldap_ssl = LDAP_SSL_START_TLS;
847 Globals.ldap_ssl_ads = false;
848 Globals.ldap_deref = -1;
849 Globals.ldap_passwd_sync = LDAP_PASSWD_SYNC_OFF;
850 Globals.ldap_delete_dn = false;
851 Globals.ldap_replication_sleep = 1000; /* wait 1 sec for replication */
852 Globals.ldap_follow_referral = Auto;
853 Globals.ldap_timeout = LDAP_DEFAULT_TIMEOUT;
854 Globals.ldap_connection_timeout = LDAP_CONNECTION_DEFAULT_TIMEOUT;
855 Globals.ldap_page_size = LDAP_PAGE_SIZE;
857 Globals.ldap_debug_level = 0;
858 Globals.ldap_debug_threshold = 10;
860 /* This is what we tell the afs client. in reality we set the token
861 * to never expire, though, when this runs out the afs client will
862 * forget the token. Set to 0 to get NEVERDATE.*/
863 Globals.afs_token_lifetime = 604800;
864 Globals.cups_connection_timeout = CUPS_DEFAULT_CONNECTION_TIMEOUT;
866 /* these parameters are set to defaults that are more appropriate
867 for the increasing samba install base:
869 as a member of the workgroup, that will possibly become a
870 _local_ master browser (lm = true). this is opposed to a forced
871 local master browser startup (pm = true).
873 doesn't provide WINS server service by default (wsupp = false),
874 and doesn't provide domain master browser services by default, either.
878 Globals.show_add_printer_wizard = true;
879 Globals.os_level = 20;
880 Globals.local_master = true;
881 Globals._domain_master = Auto; /* depending on _domain_logons */
882 Globals._domain_logons = false;
883 Globals.browse_list = true;
884 Globals.we_are_a_wins_server = false;
885 Globals.wins_proxy = false;
887 TALLOC_FREE(Globals.init_logon_delayed_hosts);
888 Globals.init_logon_delay = 100; /* 100 ms default delay */
890 Globals.wins_dns_proxy = true;
892 Globals.allow_trusted_domains = true;
893 string_set(Globals.ctx, &Globals.szIdmapBackend, "tdb");
895 string_set(Globals.ctx, &Globals.template_shell, "/bin/false");
896 string_set(Globals.ctx, &Globals.template_homedir, "/home/%D/%U");
897 string_set(Globals.ctx, &Globals.winbind_separator, "\\");
898 string_set(Globals.ctx, &Globals.winbindd_socket_directory, dyn_WINBINDD_SOCKET_DIR);
900 string_set(Globals.ctx, &Globals.cups_server, "");
901 string_set(Globals.ctx, &Globals.iprint_server, "");
903 string_set(Globals.ctx, &Globals._ctdbd_socket, "");
905 Globals.cluster_addresses = NULL;
906 Globals.clustering = false;
907 Globals.ctdb_timeout = 0;
908 Globals.ctdb_locktime_warn_threshold = 0;
910 Globals.winbind_cache_time = 300; /* 5 minutes */
911 Globals.winbind_reconnect_delay = 30; /* 30 seconds */
912 Globals.winbind_max_clients = 200;
913 Globals.winbind_enum_users = false;
914 Globals.winbind_enum_groups = false;
915 Globals.winbind_use_default_domain = false;
916 Globals.winbind_trusted_domains_only = false;
917 Globals.winbind_nested_groups = true;
918 Globals.winbind_expand_groups = 1;
919 Globals.winbind_nss_info = (const char **)str_list_make_v3(NULL, "template", NULL);
920 Globals.winbind_refresh_tickets = false;
921 Globals.winbind_offline_logon = false;
923 Globals.idmap_cache_time = 86400 * 7; /* a week by default */
924 Globals.idmap_negative_cache_time = 120; /* 2 minutes by default */
926 Globals.passdb_expand_explicit = false;
928 Globals.name_cache_timeout = 660; /* In seconds */
930 Globals.use_spnego = true;
931 Globals.client_use_spnego = true;
933 Globals.client_signing = SMB_SIGNING_DEFAULT;
934 Globals.server_signing = SMB_SIGNING_DEFAULT;
936 Globals.defer_sharing_violations = true;
937 Globals.smb_ports = (const char **)str_list_make_v3(NULL, SMB_PORTS, NULL);
939 Globals.enable_privileges = true;
940 Globals.host_msdfs = true;
941 Globals.enable_asu_support = false;
943 /* User defined shares. */
944 s = talloc_asprintf(talloc_tos(), "%s/usershares", get_dyn_STATEDIR());
945 if (s == NULL) {
946 smb_panic("init_globals: ENOMEM");
948 string_set(Globals.ctx, &Globals.usershare_path, s);
949 TALLOC_FREE(s);
950 string_set(Globals.ctx, &Globals.usershare_template_share, "");
951 Globals.usershare_max_shares = 0;
952 /* By default disallow sharing of directories not owned by the sharer. */
953 Globals.usershare_owner_only = true;
954 /* By default disallow guest access to usershares. */
955 Globals.usershare_allow_guests = false;
957 Globals.keepalive = DEFAULT_KEEPALIVE;
959 /* By default no shares out of the registry */
960 Globals.registry_shares = false;
962 Globals.iminreceivefile = 0;
964 Globals.map_untrusted_to_domain = false;
965 Globals.multicast_dns_register = true;
967 Globals.smb2_max_read = DEFAULT_SMB2_MAX_READ;
968 Globals.smb2_max_write = DEFAULT_SMB2_MAX_WRITE;
969 Globals.smb2_max_trans = DEFAULT_SMB2_MAX_TRANSACT;
970 Globals.ismb2_max_credits = DEFAULT_SMB2_MAX_CREDITS;
972 string_set(Globals.ctx, &Globals.ncalrpc_dir, get_dyn_NCALRPCDIR());
974 Globals.server_services = (const char **)str_list_make_v3(NULL, "s3fs rpc nbt wrepl ldap cldap kdc drepl winbind ntp_signd kcc dnsupdate dns", NULL);
976 Globals.dcerpc_endpoint_servers = (const char **)str_list_make_v3(NULL, "epmapper wkssvc rpcecho samr netlogon lsarpc spoolss drsuapi dssetup unixinfo browser eventlog6 backupkey dnsserver", NULL);
978 Globals.tls_enabled = true;
980 string_set(Globals.ctx, &Globals._tls_keyfile, "tls/key.pem");
981 string_set(Globals.ctx, &Globals._tls_certfile, "tls/cert.pem");
982 string_set(Globals.ctx, &Globals._tls_cafile, "tls/ca.pem");
984 string_set(Globals.ctx, &Globals.share_backend, "classic");
986 Globals.iPreferredMaster = Auto;
988 Globals.allow_dns_updates = DNS_UPDATE_SIGNED;
990 string_set(Globals.ctx, &Globals.ntp_signd_socket_directory, get_dyn_NTP_SIGND_SOCKET_DIR());
992 string_set(Globals.ctx, &Globals.winbindd_privileged_socket_directory, get_dyn_WINBINDD_PRIVILEGED_SOCKET_DIR());
994 s = talloc_asprintf(talloc_tos(), "%s/samba_kcc", get_dyn_SCRIPTSBINDIR());
995 if (s == NULL) {
996 smb_panic("init_globals: ENOMEM");
998 Globals.samba_kcc_command = (const char **)str_list_make_v3(NULL, s, NULL);
999 TALLOC_FREE(s);
1001 s = talloc_asprintf(talloc_tos(), "%s/samba_dnsupdate", get_dyn_SCRIPTSBINDIR());
1002 if (s == NULL) {
1003 smb_panic("init_globals: ENOMEM");
1005 Globals.dns_update_command = (const char **)str_list_make_v3(NULL, s, NULL);
1006 TALLOC_FREE(s);
1008 s = talloc_asprintf(talloc_tos(), "%s/samba_spnupdate", get_dyn_SCRIPTSBINDIR());
1009 if (s == NULL) {
1010 smb_panic("init_globals: ENOMEM");
1012 Globals.spn_update_command = (const char **)str_list_make_v3(NULL, s, NULL);
1013 TALLOC_FREE(s);
1015 Globals.nsupdate_command = (const char **)str_list_make_v3(NULL, "/usr/bin/nsupdate -g", NULL);
1017 Globals.rndc_command = (const char **)str_list_make_v3(NULL, "/usr/sbin/rndc", NULL);
1019 Globals.cldap_port = 389;
1021 Globals.dgram_port = 138;
1023 Globals.nbt_port = 137;
1025 Globals.krb5_port = 88;
1027 Globals.kpasswd_port = 464;
1029 Globals.web_port = 901;
1031 /* Now put back the settings that were set with lp_set_cmdline() */
1032 apply_lp_set_cmdline();
1035 /*******************************************************************
1036 Convenience routine to grab string parameters into talloced memory
1037 and run standard_sub_basic on them. The buffers can be written to by
1038 callers without affecting the source string.
1039 ********************************************************************/
1041 char *lp_string(TALLOC_CTX *ctx, const char *s)
1043 char *ret;
1045 /* The follow debug is useful for tracking down memory problems
1046 especially if you have an inner loop that is calling a lp_*()
1047 function that returns a string. Perhaps this debug should be
1048 present all the time? */
1050 #if 0
1051 DEBUG(10, ("lp_string(%s)\n", s));
1052 #endif
1053 if (!s) {
1054 return NULL;
1057 ret = talloc_sub_basic(ctx,
1058 get_current_username(),
1059 current_user_info.domain,
1061 if (trim_char(ret, '\"', '\"')) {
1062 if (strchr(ret,'\"') != NULL) {
1063 TALLOC_FREE(ret);
1064 ret = talloc_sub_basic(ctx,
1065 get_current_username(),
1066 current_user_info.domain,
1070 return ret;
1074 In this section all the functions that are used to access the
1075 parameters from the rest of the program are defined
1078 #define FN_GLOBAL_STRING(fn_name,ptr) \
1079 char *lp_ ## fn_name(TALLOC_CTX *ctx) {return(lp_string((ctx), *(char **)(&Globals.ptr) ? *(char **)(&Globals.ptr) : ""));}
1080 #define FN_GLOBAL_CONST_STRING(fn_name,ptr) \
1081 const char *lp_ ## fn_name(void) {return(*(const char * const *)(&Globals.ptr) ? *(const char * const *)(&Globals.ptr) : "");}
1082 #define FN_GLOBAL_LIST(fn_name,ptr) \
1083 const char **lp_ ## fn_name(void) {return(*(const char ***)(&Globals.ptr));}
1084 #define FN_GLOBAL_BOOL(fn_name,ptr) \
1085 bool lp_ ## fn_name(void) {return(*(bool *)(&Globals.ptr));}
1086 #define FN_GLOBAL_CHAR(fn_name,ptr) \
1087 char lp_ ## fn_name(void) {return(*(char *)(&Globals.ptr));}
1088 #define FN_GLOBAL_INTEGER(fn_name,ptr) \
1089 int lp_ ## fn_name(void) {return(*(int *)(&Globals.ptr));}
1091 #define FN_LOCAL_STRING(fn_name,val) \
1092 char *lp_ ## fn_name(TALLOC_CTX *ctx,int i) {return(lp_string((ctx), (LP_SNUM_OK(i) && ServicePtrs[(i)]->val) ? ServicePtrs[(i)]->val : sDefault.val));}
1093 #define FN_LOCAL_CONST_STRING(fn_name,val) \
1094 const char *lp_ ## fn_name(int i) {return (const char *)((LP_SNUM_OK(i) && ServicePtrs[(i)]->val) ? ServicePtrs[(i)]->val : sDefault.val);}
1095 #define FN_LOCAL_LIST(fn_name,val) \
1096 const char **lp_ ## fn_name(int i) {return(const char **)(LP_SNUM_OK(i)? ServicePtrs[(i)]->val : sDefault.val);}
1097 #define FN_LOCAL_BOOL(fn_name,val) \
1098 bool lp_ ## fn_name(int i) {return(bool)(LP_SNUM_OK(i)? ServicePtrs[(i)]->val : sDefault.val);}
1099 #define FN_LOCAL_INTEGER(fn_name,val) \
1100 int lp_ ## fn_name(int i) {return(LP_SNUM_OK(i)? ServicePtrs[(i)]->val : sDefault.val);}
1102 #define FN_LOCAL_PARM_BOOL(fn_name,val) \
1103 bool lp_ ## fn_name(const struct share_params *p) {return(bool)(LP_SNUM_OK(p->service)? ServicePtrs[(p->service)]->val : sDefault.val);}
1104 #define FN_LOCAL_PARM_INTEGER(fn_name,val) \
1105 int lp_ ## fn_name(const struct share_params *p) {return(LP_SNUM_OK(p->service)? ServicePtrs[(p->service)]->val : sDefault.val);}
1106 #define FN_LOCAL_PARM_CHAR(fn_name,val) \
1107 char lp_ ## fn_name(const struct share_params *p) {return(LP_SNUM_OK(p->service)? ServicePtrs[(p->service)]->val : sDefault.val);}
1109 static FN_GLOBAL_INTEGER(winbind_max_domain_connections_int,
1110 winbindMaxDomainConnections)
1112 int lp_winbind_max_domain_connections(void)
1114 if (lp_winbind_offline_logon() &&
1115 lp_winbind_max_domain_connections_int() > 1) {
1116 DEBUG(1, ("offline logons active, restricting max domain "
1117 "connections to 1\n"));
1118 return 1;
1120 return MAX(1, lp_winbind_max_domain_connections_int());
1123 int lp_smb2_max_credits(void)
1125 if (Globals.ismb2_max_credits == 0) {
1126 Globals.ismb2_max_credits = DEFAULT_SMB2_MAX_CREDITS;
1128 return Globals.ismb2_max_credits;
1130 int lp_cups_encrypt(void)
1132 int result = 0;
1133 #ifdef HAVE_HTTPCONNECTENCRYPT
1134 switch (Globals.CupsEncrypt) {
1135 case Auto:
1136 result = HTTP_ENCRYPT_REQUIRED;
1137 break;
1138 case true:
1139 result = HTTP_ENCRYPT_ALWAYS;
1140 break;
1141 case false:
1142 result = HTTP_ENCRYPT_NEVER;
1143 break;
1145 #endif
1146 return result;
1149 /* These functions remain in source3/param for now */
1151 #include "lib/param/param_functions.c"
1153 FN_LOCAL_STRING(servicename, szService)
1154 FN_LOCAL_CONST_STRING(const_servicename, szService)
1156 /* These functions cannot be auto-generated */
1157 FN_LOCAL_BOOL(autoloaded, autoloaded)
1159 /* local prototypes */
1161 static int map_parameter_canonical(const char *pszParmName, bool *inverse);
1162 static const char *get_boolean(bool bool_value);
1163 static bool do_parameter(const char *pszParmName, const char *pszParmValue,
1164 void *userdata);
1165 static bool do_section(const char *pszSectionName, void *userdata);
1166 static bool hash_a_service(const char *name, int number);
1167 static void free_service_byindex(int iService);
1168 static void show_parameter(int parmIndex);
1169 static bool is_synonym_of(int parm1, int parm2, bool *inverse);
1172 * This is a helper function for parametrical options support. It returns a
1173 * pointer to parametrical option value if it exists or NULL otherwise. Actual
1174 * parametrical functions are quite simple
1176 static struct parmlist_entry *get_parametrics_by_service(struct loadparm_service *service, const char *type,
1177 const char *option)
1179 bool global_section = false;
1180 char* param_key;
1181 struct parmlist_entry *data;
1182 TALLOC_CTX *mem_ctx = talloc_stackframe();
1184 if (service == NULL) {
1185 data = Globals.param_opt;
1186 global_section = true;
1187 } else {
1188 data = service->param_opt;
1191 param_key = talloc_asprintf(mem_ctx, "%s:%s", type, option);
1192 if (param_key == NULL) {
1193 DEBUG(0,("asprintf failed!\n"));
1194 TALLOC_FREE(mem_ctx);
1195 return NULL;
1198 while (data) {
1199 if (strwicmp(data->key, param_key) == 0) {
1200 TALLOC_FREE(mem_ctx);
1201 return data;
1203 data = data->next;
1206 if (!global_section) {
1207 /* Try to fetch the same option but from globals */
1208 /* but only if we are not already working with Globals */
1209 data = Globals.param_opt;
1210 while (data) {
1211 if (strwicmp(data->key, param_key) == 0) {
1212 TALLOC_FREE(mem_ctx);
1213 return data;
1215 data = data->next;
1219 TALLOC_FREE(mem_ctx);
1221 return NULL;
1225 * This is a helper function for parametrical options support. It returns a
1226 * pointer to parametrical option value if it exists or NULL otherwise. Actual
1227 * parametrical functions are quite simple
1229 static struct parmlist_entry *get_parametrics(int snum, const char *type,
1230 const char *option)
1232 if (snum >= iNumServices) return NULL;
1234 if (snum < 0) {
1235 return get_parametrics_by_service(NULL, type, option);
1236 } else {
1237 return get_parametrics_by_service(ServicePtrs[snum], type, option);
1242 #define MISSING_PARAMETER(name) \
1243 DEBUG(0, ("%s(): value is NULL or empty!\n", #name))
1245 /*******************************************************************
1246 convenience routine to return enum parameters.
1247 ********************************************************************/
1248 static int lp_enum(const char *s,const struct enum_list *_enum)
1250 int i;
1252 if (!s || !*s || !_enum) {
1253 MISSING_PARAMETER(lp_enum);
1254 return (-1);
1257 for (i=0; _enum[i].name; i++) {
1258 if (strequal(_enum[i].name,s))
1259 return _enum[i].value;
1262 DEBUG(0,("lp_enum(%s,enum): value is not in enum_list!\n",s));
1263 return (-1);
1266 #undef MISSING_PARAMETER
1268 /* Return parametric option from a given service. Type is a part of option before ':' */
1269 /* Parametric option has following syntax: 'Type: option = value' */
1270 char *lp_parm_talloc_string(TALLOC_CTX *ctx, int snum, const char *type, const char *option, const char *def)
1272 struct parmlist_entry *data = get_parametrics(snum, type, option);
1274 if (data == NULL||data->value==NULL) {
1275 if (def) {
1276 return lp_string(ctx, def);
1277 } else {
1278 return NULL;
1282 return lp_string(ctx, data->value);
1285 /* Return parametric option from a given service. Type is a part of option before ':' */
1286 /* Parametric option has following syntax: 'Type: option = value' */
1287 const char *lp_parm_const_string(int snum, const char *type, const char *option, const char *def)
1289 struct parmlist_entry *data = get_parametrics(snum, type, option);
1291 if (data == NULL||data->value==NULL)
1292 return def;
1294 return data->value;
1297 const char *lp_parm_const_string_service(struct loadparm_service *service,
1298 const char *type, const char *option,
1299 const char *def)
1301 struct parmlist_entry *data = get_parametrics_by_service(service, type, option);
1303 if (data == NULL||data->value==NULL)
1304 return def;
1306 return data->value;
1310 /* Return parametric option from a given service. Type is a part of option before ':' */
1311 /* Parametric option has following syntax: 'Type: option = value' */
1313 const char **lp_parm_string_list(int snum, const char *type, const char *option, const char **def)
1315 struct parmlist_entry *data = get_parametrics(snum, type, option);
1317 if (data == NULL||data->value==NULL)
1318 return (const char **)def;
1320 if (data->list==NULL) {
1321 data->list = str_list_make_v3(NULL, data->value, NULL);
1324 return (const char **)data->list;
1327 /* Return parametric option from a given service. Type is a part of option before ':' */
1328 /* Parametric option has following syntax: 'Type: option = value' */
1330 int lp_parm_int(int snum, const char *type, const char *option, int def)
1332 struct parmlist_entry *data = get_parametrics(snum, type, option);
1334 if (data && data->value && *data->value)
1335 return lp_int(data->value);
1337 return def;
1340 /* Return parametric option from a given service. Type is a part of option before ':' */
1341 /* Parametric option has following syntax: 'Type: option = value' */
1343 unsigned long lp_parm_ulong(int snum, const char *type, const char *option, unsigned long def)
1345 struct parmlist_entry *data = get_parametrics(snum, type, option);
1347 if (data && data->value && *data->value)
1348 return lp_ulong(data->value);
1350 return def;
1353 /* Return parametric option from a given service. Type is a part of option before ':' */
1354 /* Parametric option has following syntax: 'Type: option = value' */
1356 bool lp_parm_bool(int snum, const char *type, const char *option, bool def)
1358 struct parmlist_entry *data = get_parametrics(snum, type, option);
1360 if (data && data->value && *data->value)
1361 return lp_bool(data->value);
1363 return def;
1366 /* Return parametric option from a given service. Type is a part of option before ':' */
1367 /* Parametric option has following syntax: 'Type: option = value' */
1369 int lp_parm_enum(int snum, const char *type, const char *option,
1370 const struct enum_list *_enum, int def)
1372 struct parmlist_entry *data = get_parametrics(snum, type, option);
1374 if (data && data->value && *data->value && _enum)
1375 return lp_enum(data->value, _enum);
1377 return def;
1381 * free a param_opts structure.
1382 * param_opts handling should be moved to talloc;
1383 * then this whole functions reduces to a TALLOC_FREE().
1386 static void free_param_opts(struct parmlist_entry **popts)
1388 struct parmlist_entry *opt, *next_opt;
1390 if (*popts != NULL) {
1391 DEBUG(5, ("Freeing parametrics:\n"));
1393 opt = *popts;
1394 while (opt != NULL) {
1395 string_free(&opt->key);
1396 string_free(&opt->value);
1397 TALLOC_FREE(opt->list);
1398 next_opt = opt->next;
1399 TALLOC_FREE(opt);
1400 opt = next_opt;
1402 *popts = NULL;
1405 /***************************************************************************
1406 Free the dynamically allocated parts of a service struct.
1407 ***************************************************************************/
1409 static void free_service(struct loadparm_service *pservice)
1411 if (!pservice)
1412 return;
1414 if (pservice->szService)
1415 DEBUG(5, ("free_service: Freeing service %s\n",
1416 pservice->szService));
1418 free_parameters(pservice);
1420 string_free(&pservice->szService);
1421 TALLOC_FREE(pservice->copymap);
1423 free_param_opts(&pservice->param_opt);
1425 ZERO_STRUCTP(pservice);
1429 /***************************************************************************
1430 remove a service indexed in the ServicePtrs array from the ServiceHash
1431 and free the dynamically allocated parts
1432 ***************************************************************************/
1434 static void free_service_byindex(int idx)
1436 if ( !LP_SNUM_OK(idx) )
1437 return;
1439 ServicePtrs[idx]->valid = false;
1441 /* we have to cleanup the hash record */
1443 if (ServicePtrs[idx]->szService) {
1444 char *canon_name = canonicalize_servicename(
1445 talloc_tos(),
1446 ServicePtrs[idx]->szService );
1448 dbwrap_delete_bystring(ServiceHash, canon_name );
1449 TALLOC_FREE(canon_name);
1452 free_service(ServicePtrs[idx]);
1453 talloc_free_children(ServicePtrs[idx]);
1456 /***************************************************************************
1457 Add a new service to the services array initialising it with the given
1458 service.
1459 ***************************************************************************/
1461 static int add_a_service(const struct loadparm_service *pservice, const char *name)
1463 int i;
1464 int num_to_alloc = iNumServices + 1;
1465 struct loadparm_service **tsp = NULL;
1467 /* it might already exist */
1468 if (name) {
1469 i = getservicebyname(name, NULL);
1470 if (i >= 0) {
1471 return (i);
1475 /* if not, then create one */
1476 i = iNumServices;
1477 tsp = talloc_realloc(NULL, ServicePtrs, struct loadparm_service *, num_to_alloc);
1478 if (tsp == NULL) {
1479 DEBUG(0,("add_a_service: failed to enlarge ServicePtrs!\n"));
1480 return (-1);
1482 ServicePtrs = tsp;
1483 ServicePtrs[iNumServices] = talloc_zero(NULL, struct loadparm_service);
1484 if (!ServicePtrs[iNumServices]) {
1485 DEBUG(0,("add_a_service: out of memory!\n"));
1486 return (-1);
1488 iNumServices++;
1490 ServicePtrs[i]->valid = true;
1492 copy_service(ServicePtrs[i], pservice, NULL);
1493 if (name)
1494 string_set(ServicePtrs[i], &ServicePtrs[i]->szService, name);
1496 DEBUG(8,("add_a_service: Creating snum = %d for %s\n",
1497 i, ServicePtrs[i]->szService));
1499 if (!hash_a_service(ServicePtrs[i]->szService, i)) {
1500 return (-1);
1503 return (i);
1506 /***************************************************************************
1507 Convert a string to uppercase and remove whitespaces.
1508 ***************************************************************************/
1510 char *canonicalize_servicename(TALLOC_CTX *ctx, const char *src)
1512 char *result;
1514 if ( !src ) {
1515 DEBUG(0,("canonicalize_servicename: NULL source name!\n"));
1516 return NULL;
1519 result = talloc_strdup(ctx, src);
1520 SMB_ASSERT(result != NULL);
1522 if (!strlower_m(result)) {
1523 TALLOC_FREE(result);
1524 return NULL;
1526 return result;
1529 /***************************************************************************
1530 Add a name/index pair for the services array to the hash table.
1531 ***************************************************************************/
1533 static bool hash_a_service(const char *name, int idx)
1535 char *canon_name;
1537 if ( !ServiceHash ) {
1538 DEBUG(10,("hash_a_service: creating servicehash\n"));
1539 ServiceHash = db_open_rbt(NULL);
1540 if ( !ServiceHash ) {
1541 DEBUG(0,("hash_a_service: open tdb servicehash failed!\n"));
1542 return false;
1546 DEBUG(10,("hash_a_service: hashing index %d for service name %s\n",
1547 idx, name));
1549 canon_name = canonicalize_servicename(talloc_tos(), name );
1551 dbwrap_store_bystring(ServiceHash, canon_name,
1552 make_tdb_data((uint8 *)&idx, sizeof(idx)),
1553 TDB_REPLACE);
1555 TALLOC_FREE(canon_name);
1557 return true;
1560 /***************************************************************************
1561 Add a new home service, with the specified home directory, defaults coming
1562 from service ifrom.
1563 ***************************************************************************/
1565 bool lp_add_home(const char *pszHomename, int iDefaultService,
1566 const char *user, const char *pszHomedir)
1568 int i;
1570 if (pszHomename == NULL || user == NULL || pszHomedir == NULL ||
1571 pszHomedir[0] == '\0') {
1572 return false;
1575 i = add_a_service(ServicePtrs[iDefaultService], pszHomename);
1577 if (i < 0)
1578 return false;
1580 if (!(*(ServicePtrs[iDefaultService]->path))
1581 || strequal(ServicePtrs[iDefaultService]->path,
1582 lp_path(talloc_tos(), GLOBAL_SECTION_SNUM))) {
1583 string_set(ServicePtrs[i], &ServicePtrs[i]->path, pszHomedir);
1586 if (!(*(ServicePtrs[i]->comment))) {
1587 char *comment = talloc_asprintf(talloc_tos(), "Home directory of %s", user);
1588 if (comment == NULL) {
1589 return false;
1591 string_set(ServicePtrs[i], &ServicePtrs[i]->comment, comment);
1592 TALLOC_FREE(comment);
1595 /* set the browseable flag from the global default */
1597 ServicePtrs[i]->browseable = sDefault.browseable;
1598 ServicePtrs[i]->access_based_share_enum = sDefault.access_based_share_enum;
1600 ServicePtrs[i]->autoloaded = true;
1602 DEBUG(3, ("adding home's share [%s] for user '%s' at '%s'\n", pszHomename,
1603 user, ServicePtrs[i]->path ));
1605 return true;
1608 /***************************************************************************
1609 Add a new service, based on an old one.
1610 ***************************************************************************/
1612 int lp_add_service(const char *pszService, int iDefaultService)
1614 if (iDefaultService < 0) {
1615 return add_a_service(&sDefault, pszService);
1618 return (add_a_service(ServicePtrs[iDefaultService], pszService));
1621 /***************************************************************************
1622 Add the IPC service.
1623 ***************************************************************************/
1625 static bool lp_add_ipc(const char *ipc_name, bool guest_ok)
1627 char *comment = NULL;
1628 int i = add_a_service(&sDefault, ipc_name);
1630 if (i < 0)
1631 return false;
1633 comment = talloc_asprintf(talloc_tos(), "IPC Service (%s)",
1634 Globals.server_string);
1635 if (comment == NULL) {
1636 return false;
1639 string_set(ServicePtrs[i], &ServicePtrs[i]->path, tmpdir());
1640 string_set(ServicePtrs[i], &ServicePtrs[i]->username, "");
1641 string_set(ServicePtrs[i], &ServicePtrs[i]->comment, comment);
1642 string_set(ServicePtrs[i], &ServicePtrs[i]->fstype, "IPC");
1643 ServicePtrs[i]->max_connections = 0;
1644 ServicePtrs[i]->bAvailable = true;
1645 ServicePtrs[i]->read_only = true;
1646 ServicePtrs[i]->guest_only = false;
1647 ServicePtrs[i]->administrative_share = true;
1648 ServicePtrs[i]->guest_ok = guest_ok;
1649 ServicePtrs[i]->printable = false;
1650 ServicePtrs[i]->browseable = sDefault.browseable;
1652 DEBUG(3, ("adding IPC service\n"));
1654 TALLOC_FREE(comment);
1655 return true;
1658 /***************************************************************************
1659 Add a new printer service, with defaults coming from service iFrom.
1660 ***************************************************************************/
1662 bool lp_add_printer(const char *pszPrintername, int iDefaultService)
1664 const char *comment = "From Printcap";
1665 int i = add_a_service(ServicePtrs[iDefaultService], pszPrintername);
1667 if (i < 0)
1668 return false;
1670 /* note that we do NOT default the availability flag to true - */
1671 /* we take it from the default service passed. This allows all */
1672 /* dynamic printers to be disabled by disabling the [printers] */
1673 /* entry (if/when the 'available' keyword is implemented!). */
1675 /* the printer name is set to the service name. */
1676 string_set(ServicePtrs[i], &ServicePtrs[i]->_printername, pszPrintername);
1677 string_set(ServicePtrs[i], &ServicePtrs[i]->comment, comment);
1679 /* set the browseable flag from the gloabl default */
1680 ServicePtrs[i]->browseable = sDefault.browseable;
1682 /* Printers cannot be read_only. */
1683 ServicePtrs[i]->read_only = false;
1684 /* No oplocks on printer services. */
1685 ServicePtrs[i]->oplocks = false;
1686 /* Printer services must be printable. */
1687 ServicePtrs[i]->printable = true;
1689 DEBUG(3, ("adding printer service %s\n", pszPrintername));
1691 return true;
1695 /***************************************************************************
1696 Check whether the given parameter name is valid.
1697 Parametric options (names containing a colon) are considered valid.
1698 ***************************************************************************/
1700 bool lp_parameter_is_valid(const char *pszParmName)
1702 return ((lpcfg_map_parameter(pszParmName) != -1) ||
1703 (strchr(pszParmName, ':') != NULL));
1706 /***************************************************************************
1707 Check whether the given name is the name of a global parameter.
1708 Returns true for strings belonging to parameters of class
1709 P_GLOBAL, false for all other strings, also for parametric options
1710 and strings not belonging to any option.
1711 ***************************************************************************/
1713 bool lp_parameter_is_global(const char *pszParmName)
1715 int num = lpcfg_map_parameter(pszParmName);
1717 if (num >= 0) {
1718 return (parm_table[num].p_class == P_GLOBAL);
1721 return false;
1724 /**************************************************************************
1725 Check whether the given name is the canonical name of a parameter.
1726 Returns false if it is not a valid parameter Name.
1727 For parametric options, true is returned.
1728 **************************************************************************/
1730 bool lp_parameter_is_canonical(const char *parm_name)
1732 if (!lp_parameter_is_valid(parm_name)) {
1733 return false;
1736 return (lpcfg_map_parameter(parm_name) ==
1737 map_parameter_canonical(parm_name, NULL));
1740 /**************************************************************************
1741 Determine the canonical name for a parameter.
1742 Indicate when it is an inverse (boolean) synonym instead of a
1743 "usual" synonym.
1744 **************************************************************************/
1746 bool lp_canonicalize_parameter(const char *parm_name, const char **canon_parm,
1747 bool *inverse)
1749 int num;
1751 if (!lp_parameter_is_valid(parm_name)) {
1752 *canon_parm = NULL;
1753 return false;
1756 num = map_parameter_canonical(parm_name, inverse);
1757 if (num < 0) {
1758 /* parametric option */
1759 *canon_parm = parm_name;
1760 } else {
1761 *canon_parm = parm_table[num].label;
1764 return true;
1768 /**************************************************************************
1769 Determine the canonical name for a parameter.
1770 Turn the value given into the inverse boolean expression when
1771 the synonym is an invers boolean synonym.
1773 Return true if parm_name is a valid parameter name and
1774 in case it is an invers boolean synonym, if the val string could
1775 successfully be converted to the reverse bool.
1776 Return false in all other cases.
1777 **************************************************************************/
1779 bool lp_canonicalize_parameter_with_value(const char *parm_name,
1780 const char *val,
1781 const char **canon_parm,
1782 const char **canon_val)
1784 int num;
1785 bool inverse;
1787 if (!lp_parameter_is_valid(parm_name)) {
1788 *canon_parm = NULL;
1789 *canon_val = NULL;
1790 return false;
1793 num = map_parameter_canonical(parm_name, &inverse);
1794 if (num < 0) {
1795 /* parametric option */
1796 *canon_parm = parm_name;
1797 *canon_val = val;
1798 } else {
1799 *canon_parm = parm_table[num].label;
1800 if (inverse) {
1801 if (!lp_invert_boolean(val, canon_val)) {
1802 *canon_val = NULL;
1803 return false;
1805 } else {
1806 *canon_val = val;
1810 return true;
1813 /***************************************************************************
1814 Map a parameter's string representation to the index of the canonical
1815 form of the parameter (it might be a synonym).
1816 Returns -1 if the parameter string is not recognised.
1817 ***************************************************************************/
1819 static int map_parameter_canonical(const char *pszParmName, bool *inverse)
1821 int parm_num, canon_num;
1822 bool loc_inverse = false;
1824 parm_num = lpcfg_map_parameter(pszParmName);
1825 if ((parm_num < 0) || !(parm_table[parm_num].flags & FLAG_HIDE)) {
1826 /* invalid, parametric or no canidate for synonyms ... */
1827 goto done;
1830 for (canon_num = 0; parm_table[canon_num].label; canon_num++) {
1831 if (is_synonym_of(parm_num, canon_num, &loc_inverse)) {
1832 parm_num = canon_num;
1833 goto done;
1837 done:
1838 if (inverse != NULL) {
1839 *inverse = loc_inverse;
1841 return parm_num;
1844 /***************************************************************************
1845 return true if parameter number parm1 is a synonym of parameter
1846 number parm2 (parm2 being the principal name).
1847 set inverse to true if parm1 is P_BOOLREV and parm2 is P_BOOL,
1848 false otherwise.
1849 ***************************************************************************/
1851 static bool is_synonym_of(int parm1, int parm2, bool *inverse)
1853 if ((parm_table[parm1].offset == parm_table[parm2].offset) &&
1854 (parm_table[parm1].p_class == parm_table[parm2].p_class) &&
1855 (parm_table[parm1].flags & FLAG_HIDE) &&
1856 !(parm_table[parm2].flags & FLAG_HIDE))
1858 if (inverse != NULL) {
1859 if ((parm_table[parm1].type == P_BOOLREV) &&
1860 (parm_table[parm2].type == P_BOOL))
1862 *inverse = true;
1863 } else {
1864 *inverse = false;
1867 return true;
1869 return false;
1872 /***************************************************************************
1873 Show one parameter's name, type, [values,] and flags.
1874 (helper functions for show_parameter_list)
1875 ***************************************************************************/
1877 static void show_parameter(int parmIndex)
1879 int enumIndex, flagIndex;
1880 int parmIndex2;
1881 bool hadFlag;
1882 bool hadSyn;
1883 bool inverse;
1884 const char *type[] = { "P_BOOL", "P_BOOLREV", "P_CHAR", "P_INTEGER",
1885 "P_OCTAL", "P_LIST", "P_STRING", "P_USTRING",
1886 "P_ENUM", "P_SEP"};
1887 unsigned flags[] = { FLAG_BASIC, FLAG_SHARE, FLAG_PRINT, FLAG_GLOBAL,
1888 FLAG_WIZARD, FLAG_ADVANCED, FLAG_DEVELOPER, FLAG_DEPRECATED,
1889 FLAG_HIDE};
1890 const char *flag_names[] = { "FLAG_BASIC", "FLAG_SHARE", "FLAG_PRINT",
1891 "FLAG_GLOBAL", "FLAG_WIZARD", "FLAG_ADVANCED", "FLAG_DEVELOPER",
1892 "FLAG_DEPRECATED", "FLAG_HIDE", NULL};
1894 printf("%s=%s", parm_table[parmIndex].label,
1895 type[parm_table[parmIndex].type]);
1896 if (parm_table[parmIndex].type == P_ENUM) {
1897 printf(",");
1898 for (enumIndex=0;
1899 parm_table[parmIndex].enum_list[enumIndex].name;
1900 enumIndex++)
1902 printf("%s%s",
1903 enumIndex ? "|" : "",
1904 parm_table[parmIndex].enum_list[enumIndex].name);
1907 printf(",");
1908 hadFlag = false;
1909 for (flagIndex=0; flag_names[flagIndex]; flagIndex++) {
1910 if (parm_table[parmIndex].flags & flags[flagIndex]) {
1911 printf("%s%s",
1912 hadFlag ? "|" : "",
1913 flag_names[flagIndex]);
1914 hadFlag = true;
1918 /* output synonyms */
1919 hadSyn = false;
1920 for (parmIndex2=0; parm_table[parmIndex2].label; parmIndex2++) {
1921 if (is_synonym_of(parmIndex, parmIndex2, &inverse)) {
1922 printf(" (%ssynonym of %s)", inverse ? "inverse " : "",
1923 parm_table[parmIndex2].label);
1924 } else if (is_synonym_of(parmIndex2, parmIndex, &inverse)) {
1925 if (!hadSyn) {
1926 printf(" (synonyms: ");
1927 hadSyn = true;
1928 } else {
1929 printf(", ");
1931 printf("%s%s", parm_table[parmIndex2].label,
1932 inverse ? "[i]" : "");
1935 if (hadSyn) {
1936 printf(")");
1939 printf("\n");
1942 /***************************************************************************
1943 Show all parameter's name, type, [values,] and flags.
1944 ***************************************************************************/
1946 void show_parameter_list(void)
1948 int classIndex, parmIndex;
1949 const char *section_names[] = { "local", "global", NULL};
1951 for (classIndex=0; section_names[classIndex]; classIndex++) {
1952 printf("[%s]\n", section_names[classIndex]);
1953 for (parmIndex = 0; parm_table[parmIndex].label; parmIndex++) {
1954 if (parm_table[parmIndex].p_class == classIndex) {
1955 show_parameter(parmIndex);
1961 /***************************************************************************
1962 Get the standard string representation of a boolean value ("yes" or "no")
1963 ***************************************************************************/
1965 static const char *get_boolean(bool bool_value)
1967 static const char *yes_str = "yes";
1968 static const char *no_str = "no";
1970 return (bool_value ? yes_str : no_str);
1973 /***************************************************************************
1974 Provide the string of the negated boolean value associated to the boolean
1975 given as a string. Returns false if the passed string does not correctly
1976 represent a boolean.
1977 ***************************************************************************/
1979 bool lp_invert_boolean(const char *str, const char **inverse_str)
1981 bool val;
1983 if (!set_boolean(str, &val)) {
1984 return false;
1987 *inverse_str = get_boolean(!val);
1988 return true;
1991 /***************************************************************************
1992 Provide the canonical string representation of a boolean value given
1993 as a string. Return true on success, false if the string given does
1994 not correctly represent a boolean.
1995 ***************************************************************************/
1997 bool lp_canonicalize_boolean(const char *str, const char**canon_str)
1999 bool val;
2001 if (!set_boolean(str, &val)) {
2002 return false;
2005 *canon_str = get_boolean(val);
2006 return true;
2009 /***************************************************************************
2010 Find a service by name. Otherwise works like get_service.
2011 ***************************************************************************/
2013 int getservicebyname(const char *pszServiceName, struct loadparm_service *pserviceDest)
2015 int iService = -1;
2016 char *canon_name;
2017 TDB_DATA data;
2018 NTSTATUS status;
2020 if (ServiceHash == NULL) {
2021 return -1;
2024 canon_name = canonicalize_servicename(talloc_tos(), pszServiceName);
2026 status = dbwrap_fetch_bystring(ServiceHash, canon_name, canon_name,
2027 &data);
2029 if (NT_STATUS_IS_OK(status) &&
2030 (data.dptr != NULL) &&
2031 (data.dsize == sizeof(iService)))
2033 iService = *(int *)data.dptr;
2036 TALLOC_FREE(canon_name);
2038 if ((iService != -1) && (LP_SNUM_OK(iService))
2039 && (pserviceDest != NULL)) {
2040 copy_service(pserviceDest, ServicePtrs[iService], NULL);
2043 return (iService);
2046 /* Return a pointer to a service by name. Unlike getservicebyname, it does not copy the service */
2047 struct loadparm_service *lp_service(const char *pszServiceName)
2049 int iService = getservicebyname(pszServiceName, NULL);
2050 if (iService == -1 || !LP_SNUM_OK(iService)) {
2051 return NULL;
2053 return ServicePtrs[iService];
2056 struct loadparm_service *lp_servicebynum(int snum)
2058 if ((snum == -1) || !LP_SNUM_OK(snum)) {
2059 return NULL;
2061 return ServicePtrs[snum];
2064 struct loadparm_service *lp_default_loadparm_service()
2066 return &sDefault;
2069 /***************************************************************************
2070 Check a service for consistency. Return false if the service is in any way
2071 incomplete or faulty, else true.
2072 ***************************************************************************/
2074 bool service_ok(int iService)
2076 bool bRetval;
2078 bRetval = true;
2079 if (ServicePtrs[iService]->szService[0] == '\0') {
2080 DEBUG(0, ("The following message indicates an internal error:\n"));
2081 DEBUG(0, ("No service name in service entry.\n"));
2082 bRetval = false;
2085 /* The [printers] entry MUST be printable. I'm all for flexibility, but */
2086 /* I can't see why you'd want a non-printable printer service... */
2087 if (strwicmp(ServicePtrs[iService]->szService, PRINTERS_NAME) == 0) {
2088 if (!ServicePtrs[iService]->printable) {
2089 DEBUG(0, ("WARNING: [%s] service MUST be printable!\n",
2090 ServicePtrs[iService]->szService));
2091 ServicePtrs[iService]->printable = true;
2093 /* [printers] service must also be non-browsable. */
2094 if (ServicePtrs[iService]->browseable)
2095 ServicePtrs[iService]->browseable = false;
2098 if (ServicePtrs[iService]->path[0] == '\0' &&
2099 strwicmp(ServicePtrs[iService]->szService, HOMES_NAME) != 0 &&
2100 ServicePtrs[iService]->msdfs_proxy[0] == '\0'
2102 DEBUG(0, ("WARNING: No path in service %s - making it unavailable!\n",
2103 ServicePtrs[iService]->szService));
2104 ServicePtrs[iService]->bAvailable = false;
2107 /* If a service is flagged unavailable, log the fact at level 1. */
2108 if (!ServicePtrs[iService]->bAvailable)
2109 DEBUG(1, ("NOTE: Service %s is flagged unavailable.\n",
2110 ServicePtrs[iService]->szService));
2112 return (bRetval);
2115 static struct smbconf_ctx *lp_smbconf_ctx(void)
2117 sbcErr err;
2118 static struct smbconf_ctx *conf_ctx = NULL;
2120 if (conf_ctx == NULL) {
2121 err = smbconf_init(NULL, &conf_ctx, "registry:");
2122 if (!SBC_ERROR_IS_OK(err)) {
2123 DEBUG(1, ("error initializing registry configuration: "
2124 "%s\n", sbcErrorString(err)));
2125 conf_ctx = NULL;
2129 return conf_ctx;
2132 static bool process_smbconf_service(struct smbconf_service *service)
2134 uint32_t count;
2135 bool ret;
2137 if (service == NULL) {
2138 return false;
2141 ret = do_section(service->name, NULL);
2142 if (ret != true) {
2143 return false;
2145 for (count = 0; count < service->num_params; count++) {
2146 ret = do_parameter(service->param_names[count],
2147 service->param_values[count],
2148 NULL);
2149 if (ret != true) {
2150 return false;
2153 if (iServiceIndex >= 0) {
2154 return service_ok(iServiceIndex);
2156 return true;
2160 * load a service from registry and activate it
2162 bool process_registry_service(const char *service_name)
2164 sbcErr err;
2165 struct smbconf_service *service = NULL;
2166 TALLOC_CTX *mem_ctx = talloc_stackframe();
2167 struct smbconf_ctx *conf_ctx = lp_smbconf_ctx();
2168 bool ret = false;
2170 if (conf_ctx == NULL) {
2171 goto done;
2174 DEBUG(5, ("process_registry_service: service name %s\n", service_name));
2176 if (!smbconf_share_exists(conf_ctx, service_name)) {
2178 * Registry does not contain data for this service (yet),
2179 * but make sure lp_load doesn't return false.
2181 ret = true;
2182 goto done;
2185 err = smbconf_get_share(conf_ctx, mem_ctx, service_name, &service);
2186 if (!SBC_ERROR_IS_OK(err)) {
2187 goto done;
2190 ret = process_smbconf_service(service);
2191 if (!ret) {
2192 goto done;
2195 /* store the csn */
2196 smbconf_changed(conf_ctx, &conf_last_csn, NULL, NULL);
2198 done:
2199 TALLOC_FREE(mem_ctx);
2200 return ret;
2204 * process_registry_globals
2206 static bool process_registry_globals(void)
2208 bool ret;
2210 add_to_file_list(NULL, &file_lists, INCLUDE_REGISTRY_NAME, INCLUDE_REGISTRY_NAME);
2212 ret = do_parameter("registry shares", "yes", NULL);
2213 if (!ret) {
2214 return ret;
2217 return process_registry_service(GLOBAL_NAME);
2220 bool process_registry_shares(void)
2222 sbcErr err;
2223 uint32_t count;
2224 struct smbconf_service **service = NULL;
2225 uint32_t num_shares = 0;
2226 TALLOC_CTX *mem_ctx = talloc_stackframe();
2227 struct smbconf_ctx *conf_ctx = lp_smbconf_ctx();
2228 bool ret = false;
2230 if (conf_ctx == NULL) {
2231 goto done;
2234 err = smbconf_get_config(conf_ctx, mem_ctx, &num_shares, &service);
2235 if (!SBC_ERROR_IS_OK(err)) {
2236 goto done;
2239 ret = true;
2241 for (count = 0; count < num_shares; count++) {
2242 if (strequal(service[count]->name, GLOBAL_NAME)) {
2243 continue;
2245 ret = process_smbconf_service(service[count]);
2246 if (!ret) {
2247 goto done;
2251 /* store the csn */
2252 smbconf_changed(conf_ctx, &conf_last_csn, NULL, NULL);
2254 done:
2255 TALLOC_FREE(mem_ctx);
2256 return ret;
2260 * reload those shares from registry that are already
2261 * activated in the services array.
2263 static bool reload_registry_shares(void)
2265 int i;
2266 bool ret = true;
2268 for (i = 0; i < iNumServices; i++) {
2269 if (!VALID(i)) {
2270 continue;
2273 if (ServicePtrs[i]->usershare == USERSHARE_VALID) {
2274 continue;
2277 ret = process_registry_service(ServicePtrs[i]->szService);
2278 if (!ret) {
2279 goto done;
2283 done:
2284 return ret;
2288 #define MAX_INCLUDE_DEPTH 100
2290 static uint8_t include_depth;
2293 * Free the file lists
2295 static void free_file_list(void)
2297 struct file_lists *f;
2298 struct file_lists *next;
2300 f = file_lists;
2301 while( f ) {
2302 next = f->next;
2303 TALLOC_FREE( f );
2304 f = next;
2306 file_lists = NULL;
2311 * Utility function for outsiders to check if we're running on registry.
2313 bool lp_config_backend_is_registry(void)
2315 return (lp_config_backend() == CONFIG_BACKEND_REGISTRY);
2319 * Utility function to check if the config backend is FILE.
2321 bool lp_config_backend_is_file(void)
2323 return (lp_config_backend() == CONFIG_BACKEND_FILE);
2326 /*******************************************************************
2327 Check if a config file has changed date.
2328 ********************************************************************/
2330 bool lp_file_list_changed(void)
2332 struct file_lists *f = file_lists;
2334 DEBUG(6, ("lp_file_list_changed()\n"));
2336 while (f) {
2337 time_t mod_time;
2339 if (strequal(f->name, INCLUDE_REGISTRY_NAME)) {
2340 struct smbconf_ctx *conf_ctx = lp_smbconf_ctx();
2342 if (conf_ctx == NULL) {
2343 return false;
2345 if (smbconf_changed(conf_ctx, &conf_last_csn, NULL,
2346 NULL))
2348 DEBUGADD(6, ("registry config changed\n"));
2349 return true;
2351 } else {
2352 char *n2 = NULL;
2353 n2 = talloc_sub_basic(talloc_tos(),
2354 get_current_username(),
2355 current_user_info.domain,
2356 f->name);
2357 if (!n2) {
2358 return false;
2360 DEBUGADD(6, ("file %s -> %s last mod_time: %s\n",
2361 f->name, n2, ctime(&f->modtime)));
2363 mod_time = file_modtime(n2);
2365 if (mod_time &&
2366 ((f->modtime != mod_time) ||
2367 (f->subfname == NULL) ||
2368 (strcmp(n2, f->subfname) != 0)))
2370 DEBUGADD(6,
2371 ("file %s modified: %s\n", n2,
2372 ctime(&mod_time)));
2373 f->modtime = mod_time;
2374 TALLOC_FREE(f->subfname);
2375 f->subfname = talloc_strdup(f, n2);
2376 if (f->subfname == NULL) {
2377 smb_panic("talloc_strdup failed");
2379 TALLOC_FREE(n2);
2380 return true;
2382 TALLOC_FREE(n2);
2384 f = f->next;
2386 return false;
2391 * Initialize iconv conversion descriptors.
2393 * This is called the first time it is needed, and also called again
2394 * every time the configuration is reloaded, because the charset or
2395 * codepage might have changed.
2397 static void init_iconv(void)
2399 global_iconv_handle = smb_iconv_handle_reinit(NULL, lp_dos_charset(),
2400 lp_unix_charset(),
2401 true, global_iconv_handle);
2404 /***************************************************************************
2405 Handle the include operation.
2406 ***************************************************************************/
2407 static bool bAllowIncludeRegistry = true;
2409 bool lp_include(struct loadparm_context *unused, int snum, const char *pszParmValue, char **ptr)
2411 char *fname;
2413 if (include_depth >= MAX_INCLUDE_DEPTH) {
2414 DEBUG(0, ("Error: Maximum include depth (%u) exceeded!\n",
2415 include_depth));
2416 return false;
2419 if (strequal(pszParmValue, INCLUDE_REGISTRY_NAME)) {
2420 if (!bAllowIncludeRegistry) {
2421 return true;
2423 if (bInGlobalSection) {
2424 bool ret;
2425 include_depth++;
2426 ret = process_registry_globals();
2427 include_depth--;
2428 return ret;
2429 } else {
2430 DEBUG(1, ("\"include = registry\" only effective "
2431 "in %s section\n", GLOBAL_NAME));
2432 return false;
2436 fname = talloc_sub_basic(talloc_tos(), get_current_username(),
2437 current_user_info.domain,
2438 pszParmValue);
2440 add_to_file_list(NULL, &file_lists, pszParmValue, fname);
2442 if (snum < 0) {
2443 string_set(Globals.ctx, ptr, fname);
2444 } else {
2445 string_set(ServicePtrs[snum], ptr, fname);
2448 if (file_exist(fname)) {
2449 bool ret;
2450 include_depth++;
2451 ret = pm_process(fname, do_section, do_parameter, NULL);
2452 include_depth--;
2453 TALLOC_FREE(fname);
2454 return ret;
2457 DEBUG(2, ("Can't find include file %s\n", fname));
2458 TALLOC_FREE(fname);
2459 return true;
2462 bool lp_idmap_range(const char *domain_name, uint32_t *low, uint32_t *high)
2464 char *config_option = NULL;
2465 const char *range = NULL;
2466 bool ret = false;
2468 SMB_ASSERT(low != NULL);
2469 SMB_ASSERT(high != NULL);
2471 if ((domain_name == NULL) || (domain_name[0] == '\0')) {
2472 domain_name = "*";
2475 config_option = talloc_asprintf(talloc_tos(), "idmap config %s",
2476 domain_name);
2477 if (config_option == NULL) {
2478 DEBUG(0, ("out of memory\n"));
2479 return false;
2482 range = lp_parm_const_string(-1, config_option, "range", NULL);
2483 if (range == NULL) {
2484 DEBUG(1, ("idmap range not specified for domain '%s'\n", domain_name));
2485 goto done;
2488 if (sscanf(range, "%u - %u", low, high) != 2) {
2489 DEBUG(1, ("error parsing idmap range '%s' for domain '%s'\n",
2490 range, domain_name));
2491 goto done;
2494 ret = true;
2496 done:
2497 talloc_free(config_option);
2498 return ret;
2502 bool lp_idmap_default_range(uint32_t *low, uint32_t *high)
2504 return lp_idmap_range("*", low, high);
2507 const char *lp_idmap_backend(const char *domain_name)
2509 char *config_option = NULL;
2510 const char *backend = NULL;
2512 if ((domain_name == NULL) || (domain_name[0] == '\0')) {
2513 domain_name = "*";
2516 config_option = talloc_asprintf(talloc_tos(), "idmap config %s",
2517 domain_name);
2518 if (config_option == NULL) {
2519 DEBUG(0, ("out of memory\n"));
2520 return false;
2523 backend = lp_parm_const_string(-1, config_option, "backend", NULL);
2524 if (backend == NULL) {
2525 DEBUG(1, ("idmap backend not specified for domain '%s'\n", domain_name));
2526 goto done;
2529 done:
2530 talloc_free(config_option);
2531 return backend;
2534 const char *lp_idmap_default_backend(void)
2536 return lp_idmap_backend("*");
2539 /***************************************************************************
2540 Handle ldap suffixes - default to ldapsuffix if sub-suffixes are not defined.
2541 ***************************************************************************/
2543 static const char *append_ldap_suffix(TALLOC_CTX *ctx, const char *str )
2545 const char *suffix_string;
2547 suffix_string = talloc_asprintf(ctx, "%s,%s", str,
2548 Globals.ldap_suffix );
2549 if ( !suffix_string ) {
2550 DEBUG(0,("append_ldap_suffix: talloc_asprintf() failed!\n"));
2551 return "";
2554 return suffix_string;
2557 const char *lp_ldap_machine_suffix(TALLOC_CTX *ctx)
2559 if (Globals.szLdapMachineSuffix[0])
2560 return append_ldap_suffix(ctx, Globals.szLdapMachineSuffix);
2562 return lp_string(ctx, Globals.ldap_suffix);
2565 const char *lp_ldap_user_suffix(TALLOC_CTX *ctx)
2567 if (Globals.szLdapUserSuffix[0])
2568 return append_ldap_suffix(ctx, Globals.szLdapUserSuffix);
2570 return lp_string(ctx, Globals.ldap_suffix);
2573 const char *lp_ldap_group_suffix(TALLOC_CTX *ctx)
2575 if (Globals.szLdapGroupSuffix[0])
2576 return append_ldap_suffix(ctx, Globals.szLdapGroupSuffix);
2578 return lp_string(ctx, Globals.ldap_suffix);
2581 const char *lp_ldap_idmap_suffix(TALLOC_CTX *ctx)
2583 if (Globals.szLdapIdmapSuffix[0])
2584 return append_ldap_suffix(ctx, Globals.szLdapIdmapSuffix);
2586 return lp_string(ctx, Globals.ldap_suffix);
2590 return the parameter pointer for a parameter
2592 void *lp_parm_ptr(struct loadparm_service *service, struct parm_struct *parm)
2594 if (service == NULL) {
2595 if (parm->p_class == P_LOCAL)
2596 return (void *)(((char *)&sDefault)+parm->offset);
2597 else if (parm->p_class == P_GLOBAL)
2598 return (void *)(((char *)&Globals)+parm->offset);
2599 else return NULL;
2600 } else {
2601 return (void *)(((char *)service) + parm->offset);
2605 /***************************************************************************
2606 Return the local pointer to a parameter given the service number and parameter
2607 ***************************************************************************/
2609 void *lp_local_ptr_by_snum(int snum, struct parm_struct *parm)
2611 return lp_parm_ptr(ServicePtrs[snum], parm);
2614 /***************************************************************************
2615 Process a parameter for a particular service number. If snum < 0
2616 then assume we are in the globals.
2617 ***************************************************************************/
2619 bool lp_do_parameter(int snum, const char *pszParmName, const char *pszParmValue)
2621 int parmnum, i;
2622 void *parm_ptr = NULL; /* where we are going to store the result */
2623 struct parmlist_entry **opt_list;
2624 TALLOC_CTX *mem_ctx;
2626 parmnum = lpcfg_map_parameter(pszParmName);
2628 if (parmnum < 0) {
2629 if (strchr(pszParmName, ':') == NULL) {
2630 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n",
2631 pszParmName));
2632 return true;
2636 * We've got a parametric option
2639 if (snum < 0) {
2640 opt_list = &Globals.param_opt;
2641 set_param_opt(NULL, opt_list, pszParmName, pszParmValue, 0);
2642 } else {
2643 opt_list = &ServicePtrs[snum]->param_opt;
2644 set_param_opt(ServicePtrs[snum], opt_list, pszParmName, pszParmValue, 0);
2647 return true;
2650 /* if it's already been set by the command line, then we don't
2651 override here */
2652 if (parm_table[parmnum].flags & FLAG_CMDLINE) {
2653 return true;
2656 if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
2657 DEBUG(1, ("WARNING: The \"%s\" option is deprecated\n",
2658 pszParmName));
2661 /* we might point at a service, the default service or a global */
2662 if (snum < 0) {
2663 parm_ptr = lp_parm_ptr(NULL, &parm_table[parmnum]);
2664 } else {
2665 if (parm_table[parmnum].p_class == P_GLOBAL) {
2666 DEBUG(0,
2667 ("Global parameter %s found in service section!\n",
2668 pszParmName));
2669 return true;
2671 parm_ptr = lp_local_ptr_by_snum(snum, &parm_table[parmnum]);
2674 if (snum >= 0) {
2675 if (!ServicePtrs[snum]->copymap)
2676 init_copymap(ServicePtrs[snum]);
2678 /* this handles the aliases - set the copymap for other entries with
2679 the same data pointer */
2680 for (i = 0; parm_table[i].label; i++) {
2681 if ((parm_table[i].offset == parm_table[parmnum].offset)
2682 && (parm_table[i].p_class == parm_table[parmnum].p_class)) {
2683 bitmap_clear(ServicePtrs[snum]->copymap, i);
2686 mem_ctx = ServicePtrs[snum];
2687 } else {
2688 mem_ctx = Globals.ctx;
2691 /* if it is a special case then go ahead */
2692 if (parm_table[parmnum].special) {
2693 bool ok;
2694 struct loadparm_context *lp_ctx = loadparm_init_s3(talloc_tos(),
2695 loadparm_s3_helpers());
2696 lp_ctx->sDefault = &sDefault;
2697 lp_ctx->services = ServicePtrs;
2698 ok = parm_table[parmnum].special(lp_ctx, snum, pszParmValue,
2699 (char **)parm_ptr);
2700 TALLOC_FREE(lp_ctx);
2702 return ok;
2705 /* now switch on the type of variable it is */
2706 switch (parm_table[parmnum].type)
2708 case P_BOOL:
2709 *(bool *)parm_ptr = lp_bool(pszParmValue);
2710 break;
2712 case P_BOOLREV:
2713 *(bool *)parm_ptr = !lp_bool(pszParmValue);
2714 break;
2716 case P_INTEGER:
2717 *(int *)parm_ptr = lp_int(pszParmValue);
2718 break;
2720 case P_CHAR:
2721 *(char *)parm_ptr = *pszParmValue;
2722 break;
2724 case P_OCTAL:
2725 i = sscanf(pszParmValue, "%o", (int *)parm_ptr);
2726 if ( i != 1 ) {
2727 DEBUG ( 0, ("Invalid octal number %s\n", pszParmName ));
2729 break;
2731 case P_BYTES:
2733 uint64_t val;
2734 if (conv_str_size_error(pszParmValue, &val)) {
2735 if (val <= INT_MAX) {
2736 *(int *)parm_ptr = (int)val;
2737 break;
2741 DEBUG(0,("lp_do_parameter(%s): value is not "
2742 "a valid size specifier!\n", pszParmValue));
2743 return false;
2746 case P_LIST:
2747 case P_CMDLIST:
2748 TALLOC_FREE(*((char ***)parm_ptr));
2749 *(char ***)parm_ptr = str_list_make_v3(
2750 NULL, pszParmValue, NULL);
2751 break;
2753 case P_STRING:
2754 string_set(mem_ctx, (char **)parm_ptr, pszParmValue);
2755 break;
2757 case P_USTRING:
2759 char *upper_string = strupper_talloc(talloc_tos(),
2760 pszParmValue);
2761 string_set(mem_ctx, (char **)parm_ptr, upper_string);
2762 TALLOC_FREE(upper_string);
2763 break;
2765 case P_ENUM:
2766 if (!lp_set_enum_parm(&parm_table[parmnum], pszParmValue, (int*)parm_ptr)) {
2767 return false;
2769 break;
2770 case P_SEP:
2771 break;
2774 return true;
2777 /***************************************************************************
2778 set a parameter, marking it with FLAG_CMDLINE. Parameters marked as
2779 FLAG_CMDLINE won't be overridden by loads from smb.conf.
2780 ***************************************************************************/
2782 static bool lp_set_cmdline_helper(const char *pszParmName, const char *pszParmValue, bool store_values)
2784 int parmnum, i;
2785 parmnum = lpcfg_map_parameter(pszParmName);
2786 if (parmnum >= 0) {
2787 parm_table[parmnum].flags &= ~FLAG_CMDLINE;
2788 if (!lp_do_parameter(-1, pszParmName, pszParmValue)) {
2789 return false;
2791 parm_table[parmnum].flags |= FLAG_CMDLINE;
2793 /* we have to also set FLAG_CMDLINE on aliases. Aliases must
2794 * be grouped in the table, so we don't have to search the
2795 * whole table */
2796 for (i=parmnum-1;
2797 i>=0 && parm_table[i].offset == parm_table[parmnum].offset
2798 && parm_table[i].p_class == parm_table[parmnum].p_class;
2799 i--) {
2800 parm_table[i].flags |= FLAG_CMDLINE;
2802 for (i=parmnum+1;i<NUMPARAMETERS && parm_table[i].offset == parm_table[parmnum].offset
2803 && parm_table[i].p_class == parm_table[parmnum].p_class;i++) {
2804 parm_table[i].flags |= FLAG_CMDLINE;
2807 if (store_values) {
2808 store_lp_set_cmdline(pszParmName, pszParmValue);
2810 return true;
2813 /* it might be parametric */
2814 if (strchr(pszParmName, ':') != NULL) {
2815 set_param_opt(NULL, &Globals.param_opt, pszParmName, pszParmValue, FLAG_CMDLINE);
2816 if (store_values) {
2817 store_lp_set_cmdline(pszParmName, pszParmValue);
2819 return true;
2822 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
2823 return true;
2826 bool lp_set_cmdline(const char *pszParmName, const char *pszParmValue)
2828 return lp_set_cmdline_helper(pszParmName, pszParmValue, true);
2831 /***************************************************************************
2832 Process a parameter.
2833 ***************************************************************************/
2835 static bool do_parameter(const char *pszParmName, const char *pszParmValue,
2836 void *userdata)
2838 if (!bInGlobalSection && bGlobalOnly)
2839 return true;
2841 DEBUGADD(4, ("doing parameter %s = %s\n", pszParmName, pszParmValue));
2843 return (lp_do_parameter(bInGlobalSection ? -2 : iServiceIndex,
2844 pszParmName, pszParmValue));
2847 /***************************************************************************
2848 Initialize any local variables in the sDefault table, after parsing a
2849 [globals] section.
2850 ***************************************************************************/
2852 static void init_locals(void)
2855 * We run this check once the [globals] is parsed, to force
2856 * the VFS objects and other per-share settings we need for
2857 * the standard way a AD DC is operated. We may change these
2858 * as our code evolves, which is why we force these settings.
2860 * We can't do this at the end of lp_load_ex(), as by that
2861 * point the services have been loaded and they will already
2862 * have "" as their vfs objects.
2864 if (lp_server_role() == ROLE_ACTIVE_DIRECTORY_DC) {
2865 const char **vfs_objects = lp_vfs_objects(-1);
2866 if (!vfs_objects || !vfs_objects[0]) {
2867 if (lp_parm_const_string(-1, "xattr_tdb", "file", NULL)) {
2868 lp_do_parameter(-1, "vfs objects", "dfs_samba4 acl_xattr xattr_tdb");
2869 } else if (lp_parm_const_string(-1, "posix", "eadb", NULL)) {
2870 lp_do_parameter(-1, "vfs objects", "dfs_samba4 acl_xattr posix_eadb");
2871 } else {
2872 lp_do_parameter(-1, "vfs objects", "dfs_samba4 acl_xattr");
2876 lp_do_parameter(-1, "map hidden", "no");
2877 lp_do_parameter(-1, "map system", "no");
2878 lp_do_parameter(-1, "map readonly", "no");
2879 lp_do_parameter(-1, "map archive", "no");
2880 lp_do_parameter(-1, "store dos attributes", "yes");
2884 /***************************************************************************
2885 Process a new section (service). At this stage all sections are services.
2886 Later we'll have special sections that permit server parameters to be set.
2887 Returns true on success, false on failure.
2888 ***************************************************************************/
2890 static bool do_section(const char *pszSectionName, void *userdata)
2892 bool bRetval;
2893 bool isglobal = ((strwicmp(pszSectionName, GLOBAL_NAME) == 0) ||
2894 (strwicmp(pszSectionName, GLOBAL_NAME2) == 0));
2895 bRetval = false;
2897 /* if we were in a global section then do the local inits */
2898 if (bInGlobalSection && !isglobal)
2899 init_locals();
2901 /* if we've just struck a global section, note the fact. */
2902 bInGlobalSection = isglobal;
2904 /* check for multiple global sections */
2905 if (bInGlobalSection) {
2906 DEBUG(3, ("Processing section \"[%s]\"\n", pszSectionName));
2907 return true;
2910 if (!bInGlobalSection && bGlobalOnly)
2911 return true;
2913 /* if we have a current service, tidy it up before moving on */
2914 bRetval = true;
2916 if (iServiceIndex >= 0)
2917 bRetval = service_ok(iServiceIndex);
2919 /* if all is still well, move to the next record in the services array */
2920 if (bRetval) {
2921 /* We put this here to avoid an odd message order if messages are */
2922 /* issued by the post-processing of a previous section. */
2923 DEBUG(2, ("Processing section \"[%s]\"\n", pszSectionName));
2925 iServiceIndex = add_a_service(&sDefault, pszSectionName);
2926 if (iServiceIndex < 0) {
2927 DEBUG(0, ("Failed to add a new service\n"));
2928 return false;
2930 /* Clean all parametric options for service */
2931 /* They will be added during parsing again */
2932 free_param_opts(&ServicePtrs[iServiceIndex]->param_opt);
2935 return bRetval;
2939 /***************************************************************************
2940 Determine if a partcular base parameter is currentl set to the default value.
2941 ***************************************************************************/
2943 static bool is_default(int i)
2945 switch (parm_table[i].type) {
2946 case P_LIST:
2947 case P_CMDLIST:
2948 return str_list_equal((const char * const *)parm_table[i].def.lvalue,
2949 *(const char ***)lp_parm_ptr(NULL,
2950 &parm_table[i]));
2951 case P_STRING:
2952 case P_USTRING:
2953 return strequal(parm_table[i].def.svalue,
2954 *(char **)lp_parm_ptr(NULL,
2955 &parm_table[i]));
2956 case P_BOOL:
2957 case P_BOOLREV:
2958 return parm_table[i].def.bvalue ==
2959 *(bool *)lp_parm_ptr(NULL,
2960 &parm_table[i]);
2961 case P_CHAR:
2962 return parm_table[i].def.cvalue ==
2963 *(char *)lp_parm_ptr(NULL,
2964 &parm_table[i]);
2965 case P_INTEGER:
2966 case P_OCTAL:
2967 case P_ENUM:
2968 case P_BYTES:
2969 return parm_table[i].def.ivalue ==
2970 *(int *)lp_parm_ptr(NULL,
2971 &parm_table[i]);
2972 case P_SEP:
2973 break;
2975 return false;
2978 /***************************************************************************
2979 Display the contents of the global structure.
2980 ***************************************************************************/
2982 static void dump_globals(FILE *f)
2984 int i;
2985 struct parmlist_entry *data;
2987 fprintf(f, "[global]\n");
2989 for (i = 0; parm_table[i].label; i++)
2990 if (parm_table[i].p_class == P_GLOBAL &&
2991 !(parm_table[i].flags & FLAG_META) &&
2992 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset))) {
2993 if (defaults_saved && is_default(i))
2994 continue;
2995 fprintf(f, "\t%s = ", parm_table[i].label);
2996 lpcfg_print_parameter(&parm_table[i], lp_parm_ptr(NULL,
2997 &parm_table[i]),
2999 fprintf(f, "\n");
3001 if (Globals.param_opt != NULL) {
3002 data = Globals.param_opt;
3003 while(data) {
3004 fprintf(f, "\t%s = %s\n", data->key, data->value);
3005 data = data->next;
3011 /***************************************************************************
3012 Display the contents of a single services record.
3013 ***************************************************************************/
3015 static void dump_a_service(struct loadparm_service *pService, FILE * f)
3017 int i;
3018 struct parmlist_entry *data;
3020 if (pService != &sDefault)
3021 fprintf(f, "[%s]\n", pService->szService);
3023 for (i = 0; parm_table[i].label; i++) {
3025 if (parm_table[i].p_class == P_LOCAL &&
3026 !(parm_table[i].flags & FLAG_META) &&
3027 (*parm_table[i].label != '-') &&
3028 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset)))
3030 if (pService == &sDefault) {
3031 if (defaults_saved && is_default(i))
3032 continue;
3033 } else {
3034 if (lpcfg_equal_parameter(parm_table[i].type,
3035 lp_parm_ptr(pService, &parm_table[i]),
3036 lp_parm_ptr(NULL, &parm_table[i])))
3037 continue;
3040 fprintf(f, "\t%s = ", parm_table[i].label);
3041 lpcfg_print_parameter(&parm_table[i],
3042 lp_parm_ptr(pService, &parm_table[i]),
3044 fprintf(f, "\n");
3048 if (pService->param_opt != NULL) {
3049 data = pService->param_opt;
3050 while(data) {
3051 fprintf(f, "\t%s = %s\n", data->key, data->value);
3052 data = data->next;
3057 /***************************************************************************
3058 Display the contents of a parameter of a single services record.
3059 ***************************************************************************/
3061 bool dump_a_parameter(int snum, char *parm_name, FILE * f, bool isGlobal)
3063 bool result = false;
3064 char *local_parm_name;
3065 char *parm_opt;
3066 const char *parm_opt_value;
3068 struct loadparm_context *lp_ctx;
3070 lp_ctx = loadparm_init_s3(talloc_tos(), loadparm_s3_helpers());
3071 if (lp_ctx == NULL) {
3072 return false;
3075 /* check for parametrical option */
3076 local_parm_name = talloc_strdup(lp_ctx, parm_name);
3077 if (local_parm_name == NULL) {
3078 TALLOC_FREE(lp_ctx);
3079 return false;
3082 parm_opt = strchr( local_parm_name, ':');
3084 if (parm_opt) {
3085 *parm_opt = '\0';
3086 parm_opt++;
3087 if (strlen(parm_opt)) {
3088 parm_opt_value = lp_parm_const_string( snum,
3089 local_parm_name, parm_opt, NULL);
3090 if (parm_opt_value) {
3091 fprintf(f, "%s\n", parm_opt_value);
3092 result = true;
3095 return result;
3098 if (isGlobal) {
3099 result = lpcfg_dump_a_parameter(lp_ctx, NULL, parm_name, f);
3100 } else {
3101 result = lpcfg_dump_a_parameter(lp_ctx, ServicePtrs[snum], parm_name, f);
3103 TALLOC_FREE(lp_ctx);
3104 return result;
3107 /***************************************************************************
3108 Return info about the requested parameter (given as a string).
3109 Return NULL when the string is not a valid parameter name.
3110 ***************************************************************************/
3112 struct parm_struct *lp_get_parameter(const char *param_name)
3114 int num = lpcfg_map_parameter(param_name);
3116 if (num < 0) {
3117 return NULL;
3120 return &parm_table[num];
3123 #if 0
3124 /***************************************************************************
3125 Display the contents of a single copy structure.
3126 ***************************************************************************/
3127 static void dump_copy_map(bool *pcopymap)
3129 int i;
3130 if (!pcopymap)
3131 return;
3133 printf("\n\tNon-Copied parameters:\n");
3135 for (i = 0; parm_table[i].label; i++)
3136 if (parm_table[i].p_class == P_LOCAL &&
3137 parm_table[i].ptr && !pcopymap[i] &&
3138 (i == 0 || (parm_table[i].ptr != parm_table[i - 1].ptr)))
3140 printf("\t\t%s\n", parm_table[i].label);
3143 #endif
3145 /***************************************************************************
3146 Return TRUE if the passed service number is within range.
3147 ***************************************************************************/
3149 bool lp_snum_ok(int iService)
3151 return (LP_SNUM_OK(iService) && ServicePtrs[iService]->bAvailable);
3154 /***************************************************************************
3155 Auto-load some home services.
3156 ***************************************************************************/
3158 static void lp_add_auto_services(char *str)
3160 char *s;
3161 char *p;
3162 int homes;
3163 char *saveptr;
3165 if (!str)
3166 return;
3168 s = talloc_strdup(talloc_tos(), str);
3169 if (!s) {
3170 smb_panic("talloc_strdup failed");
3171 return;
3174 homes = lp_servicenumber(HOMES_NAME);
3176 for (p = strtok_r(s, LIST_SEP, &saveptr); p;
3177 p = strtok_r(NULL, LIST_SEP, &saveptr)) {
3178 char *home;
3180 if (lp_servicenumber(p) >= 0)
3181 continue;
3183 home = get_user_home_dir(talloc_tos(), p);
3185 if (home && home[0] && homes >= 0)
3186 lp_add_home(p, homes, p, home);
3188 TALLOC_FREE(home);
3190 TALLOC_FREE(s);
3193 /***************************************************************************
3194 Auto-load one printer.
3195 ***************************************************************************/
3197 void lp_add_one_printer(const char *name, const char *comment,
3198 const char *location, void *pdata)
3200 int printers = lp_servicenumber(PRINTERS_NAME);
3201 int i;
3203 if (lp_servicenumber(name) < 0) {
3204 lp_add_printer(name, printers);
3205 if ((i = lp_servicenumber(name)) >= 0) {
3206 string_set(ServicePtrs[i], &ServicePtrs[i]->comment, comment);
3207 ServicePtrs[i]->autoloaded = true;
3212 /***************************************************************************
3213 Have we loaded a services file yet?
3214 ***************************************************************************/
3216 bool lp_loaded(void)
3218 return (bLoaded);
3221 /***************************************************************************
3222 Unload unused services.
3223 ***************************************************************************/
3225 void lp_killunused(struct smbd_server_connection *sconn,
3226 bool (*snumused) (struct smbd_server_connection *, int))
3228 int i;
3229 for (i = 0; i < iNumServices; i++) {
3230 if (!VALID(i))
3231 continue;
3233 /* don't kill autoloaded or usershare services */
3234 if ( ServicePtrs[i]->autoloaded ||
3235 ServicePtrs[i]->usershare == USERSHARE_VALID) {
3236 continue;
3239 if (!snumused || !snumused(sconn, i)) {
3240 free_service_byindex(i);
3246 * Kill all except autoloaded and usershare services - convenience wrapper
3248 void lp_kill_all_services(void)
3250 lp_killunused(NULL, NULL);
3253 /***************************************************************************
3254 Unload a service.
3255 ***************************************************************************/
3257 void lp_killservice(int iServiceIn)
3259 if (VALID(iServiceIn)) {
3260 free_service_byindex(iServiceIn);
3264 /***************************************************************************
3265 Save the curent values of all global and sDefault parameters into the
3266 defaults union. This allows testparm to show only the
3267 changed (ie. non-default) parameters.
3268 ***************************************************************************/
3270 static void lp_save_defaults(void)
3272 int i;
3273 for (i = 0; parm_table[i].label; i++) {
3274 if (i > 0 && parm_table[i].offset == parm_table[i - 1].offset
3275 && parm_table[i].p_class == parm_table[i - 1].p_class)
3276 continue;
3277 switch (parm_table[i].type) {
3278 case P_LIST:
3279 case P_CMDLIST:
3280 parm_table[i].def.lvalue = str_list_copy(
3281 NULL, *(const char ***)lp_parm_ptr(NULL, &parm_table[i]));
3282 break;
3283 case P_STRING:
3284 case P_USTRING:
3285 parm_table[i].def.svalue = talloc_strdup(Globals.ctx, *(char **)lp_parm_ptr(NULL, &parm_table[i]));
3286 if (parm_table[i].def.svalue == NULL) {
3287 smb_panic("talloc_strdup failed");
3289 break;
3290 case P_BOOL:
3291 case P_BOOLREV:
3292 parm_table[i].def.bvalue =
3293 *(bool *)lp_parm_ptr(NULL, &parm_table[i]);
3294 break;
3295 case P_CHAR:
3296 parm_table[i].def.cvalue =
3297 *(char *)lp_parm_ptr(NULL, &parm_table[i]);
3298 break;
3299 case P_INTEGER:
3300 case P_OCTAL:
3301 case P_ENUM:
3302 case P_BYTES:
3303 parm_table[i].def.ivalue =
3304 *(int *)lp_parm_ptr(NULL, &parm_table[i]);
3305 break;
3306 case P_SEP:
3307 break;
3310 defaults_saved = true;
3313 /***********************************************************
3314 If we should send plaintext/LANMAN passwords in the clinet
3315 ************************************************************/
3317 static void set_allowed_client_auth(void)
3319 if (Globals.client_ntlmv2_auth) {
3320 Globals.client_lanman_auth = false;
3322 if (!Globals.client_lanman_auth) {
3323 Globals.client_plaintext_auth = false;
3327 /***************************************************************************
3328 JRA.
3329 The following code allows smbd to read a user defined share file.
3330 Yes, this is my intent. Yes, I'm comfortable with that...
3332 THE FOLLOWING IS SECURITY CRITICAL CODE.
3334 It washes your clothes, it cleans your house, it guards you while you sleep...
3335 Do not f%^k with it....
3336 ***************************************************************************/
3338 #define MAX_USERSHARE_FILE_SIZE (10*1024)
3340 /***************************************************************************
3341 Check allowed stat state of a usershare file.
3342 Ensure we print out who is dicking with us so the admin can
3343 get their sorry ass fired.
3344 ***************************************************************************/
3346 static bool check_usershare_stat(const char *fname,
3347 const SMB_STRUCT_STAT *psbuf)
3349 if (!S_ISREG(psbuf->st_ex_mode)) {
3350 DEBUG(0,("check_usershare_stat: file %s owned by uid %u is "
3351 "not a regular file\n",
3352 fname, (unsigned int)psbuf->st_ex_uid ));
3353 return false;
3356 /* Ensure this doesn't have the other write bit set. */
3357 if (psbuf->st_ex_mode & S_IWOTH) {
3358 DEBUG(0,("check_usershare_stat: file %s owned by uid %u allows "
3359 "public write. Refusing to allow as a usershare file.\n",
3360 fname, (unsigned int)psbuf->st_ex_uid ));
3361 return false;
3364 /* Should be 10k or less. */
3365 if (psbuf->st_ex_size > MAX_USERSHARE_FILE_SIZE) {
3366 DEBUG(0,("check_usershare_stat: file %s owned by uid %u is "
3367 "too large (%u) to be a user share file.\n",
3368 fname, (unsigned int)psbuf->st_ex_uid,
3369 (unsigned int)psbuf->st_ex_size ));
3370 return false;
3373 return true;
3376 /***************************************************************************
3377 Parse the contents of a usershare file.
3378 ***************************************************************************/
3380 enum usershare_err parse_usershare_file(TALLOC_CTX *ctx,
3381 SMB_STRUCT_STAT *psbuf,
3382 const char *servicename,
3383 int snum,
3384 char **lines,
3385 int numlines,
3386 char **pp_sharepath,
3387 char **pp_comment,
3388 char **pp_cp_servicename,
3389 struct security_descriptor **ppsd,
3390 bool *pallow_guest)
3392 const char **prefixallowlist = lp_usershare_prefix_allow_list();
3393 const char **prefixdenylist = lp_usershare_prefix_deny_list();
3394 int us_vers;
3395 DIR *dp;
3396 SMB_STRUCT_STAT sbuf;
3397 char *sharepath = NULL;
3398 char *comment = NULL;
3400 *pp_sharepath = NULL;
3401 *pp_comment = NULL;
3403 *pallow_guest = false;
3405 if (numlines < 4) {
3406 return USERSHARE_MALFORMED_FILE;
3409 if (strcmp(lines[0], "#VERSION 1") == 0) {
3410 us_vers = 1;
3411 } else if (strcmp(lines[0], "#VERSION 2") == 0) {
3412 us_vers = 2;
3413 if (numlines < 5) {
3414 return USERSHARE_MALFORMED_FILE;
3416 } else {
3417 return USERSHARE_BAD_VERSION;
3420 if (strncmp(lines[1], "path=", 5) != 0) {
3421 return USERSHARE_MALFORMED_PATH;
3424 sharepath = talloc_strdup(ctx, &lines[1][5]);
3425 if (!sharepath) {
3426 return USERSHARE_POSIX_ERR;
3428 trim_string(sharepath, " ", " ");
3430 if (strncmp(lines[2], "comment=", 8) != 0) {
3431 return USERSHARE_MALFORMED_COMMENT_DEF;
3434 comment = talloc_strdup(ctx, &lines[2][8]);
3435 if (!comment) {
3436 return USERSHARE_POSIX_ERR;
3438 trim_string(comment, " ", " ");
3439 trim_char(comment, '"', '"');
3441 if (strncmp(lines[3], "usershare_acl=", 14) != 0) {
3442 return USERSHARE_MALFORMED_ACL_DEF;
3445 if (!parse_usershare_acl(ctx, &lines[3][14], ppsd)) {
3446 return USERSHARE_ACL_ERR;
3449 if (us_vers == 2) {
3450 if (strncmp(lines[4], "guest_ok=", 9) != 0) {
3451 return USERSHARE_MALFORMED_ACL_DEF;
3453 if (lines[4][9] == 'y') {
3454 *pallow_guest = true;
3457 /* Backwards compatible extension to file version #2. */
3458 if (numlines > 5) {
3459 if (strncmp(lines[5], "sharename=", 10) != 0) {
3460 return USERSHARE_MALFORMED_SHARENAME_DEF;
3462 if (!strequal(&lines[5][10], servicename)) {
3463 return USERSHARE_BAD_SHARENAME;
3465 *pp_cp_servicename = talloc_strdup(ctx, &lines[5][10]);
3466 if (!*pp_cp_servicename) {
3467 return USERSHARE_POSIX_ERR;
3472 if (*pp_cp_servicename == NULL) {
3473 *pp_cp_servicename = talloc_strdup(ctx, servicename);
3474 if (!*pp_cp_servicename) {
3475 return USERSHARE_POSIX_ERR;
3479 if (snum != -1 && (strcmp(sharepath, ServicePtrs[snum]->path) == 0)) {
3480 /* Path didn't change, no checks needed. */
3481 *pp_sharepath = sharepath;
3482 *pp_comment = comment;
3483 return USERSHARE_OK;
3486 /* The path *must* be absolute. */
3487 if (sharepath[0] != '/') {
3488 DEBUG(2,("parse_usershare_file: share %s: path %s is not an absolute path.\n",
3489 servicename, sharepath));
3490 return USERSHARE_PATH_NOT_ABSOLUTE;
3493 /* If there is a usershare prefix deny list ensure one of these paths
3494 doesn't match the start of the user given path. */
3495 if (prefixdenylist) {
3496 int i;
3497 for ( i=0; prefixdenylist[i]; i++ ) {
3498 DEBUG(10,("parse_usershare_file: share %s : checking prefixdenylist[%d]='%s' against %s\n",
3499 servicename, i, prefixdenylist[i], sharepath ));
3500 if (memcmp( sharepath, prefixdenylist[i], strlen(prefixdenylist[i])) == 0) {
3501 DEBUG(2,("parse_usershare_file: share %s path %s starts with one of the "
3502 "usershare prefix deny list entries.\n",
3503 servicename, sharepath));
3504 return USERSHARE_PATH_IS_DENIED;
3509 /* If there is a usershare prefix allow list ensure one of these paths
3510 does match the start of the user given path. */
3512 if (prefixallowlist) {
3513 int i;
3514 for ( i=0; prefixallowlist[i]; i++ ) {
3515 DEBUG(10,("parse_usershare_file: share %s checking prefixallowlist[%d]='%s' against %s\n",
3516 servicename, i, prefixallowlist[i], sharepath ));
3517 if (memcmp( sharepath, prefixallowlist[i], strlen(prefixallowlist[i])) == 0) {
3518 break;
3521 if (prefixallowlist[i] == NULL) {
3522 DEBUG(2,("parse_usershare_file: share %s path %s doesn't start with one of the "
3523 "usershare prefix allow list entries.\n",
3524 servicename, sharepath));
3525 return USERSHARE_PATH_NOT_ALLOWED;
3529 /* Ensure this is pointing to a directory. */
3530 dp = opendir(sharepath);
3532 if (!dp) {
3533 DEBUG(2,("parse_usershare_file: share %s path %s is not a directory.\n",
3534 servicename, sharepath));
3535 return USERSHARE_PATH_NOT_DIRECTORY;
3538 /* Ensure the owner of the usershare file has permission to share
3539 this directory. */
3541 if (sys_stat(sharepath, &sbuf, false) == -1) {
3542 DEBUG(2,("parse_usershare_file: share %s : stat failed on path %s. %s\n",
3543 servicename, sharepath, strerror(errno) ));
3544 closedir(dp);
3545 return USERSHARE_POSIX_ERR;
3548 closedir(dp);
3550 if (!S_ISDIR(sbuf.st_ex_mode)) {
3551 DEBUG(2,("parse_usershare_file: share %s path %s is not a directory.\n",
3552 servicename, sharepath ));
3553 return USERSHARE_PATH_NOT_DIRECTORY;
3556 /* Check if sharing is restricted to owner-only. */
3557 /* psbuf is the stat of the usershare definition file,
3558 sbuf is the stat of the target directory to be shared. */
3560 if (lp_usershare_owner_only()) {
3561 /* root can share anything. */
3562 if ((psbuf->st_ex_uid != 0) && (sbuf.st_ex_uid != psbuf->st_ex_uid)) {
3563 return USERSHARE_PATH_NOT_ALLOWED;
3567 *pp_sharepath = sharepath;
3568 *pp_comment = comment;
3569 return USERSHARE_OK;
3572 /***************************************************************************
3573 Deal with a usershare file.
3574 Returns:
3575 >= 0 - snum
3576 -1 - Bad name, invalid contents.
3577 - service name already existed and not a usershare, problem
3578 with permissions to share directory etc.
3579 ***************************************************************************/
3581 static int process_usershare_file(const char *dir_name, const char *file_name, int snum_template)
3583 SMB_STRUCT_STAT sbuf;
3584 SMB_STRUCT_STAT lsbuf;
3585 char *fname = NULL;
3586 char *sharepath = NULL;
3587 char *comment = NULL;
3588 char *cp_service_name = NULL;
3589 char **lines = NULL;
3590 int numlines = 0;
3591 int fd = -1;
3592 int iService = -1;
3593 TALLOC_CTX *ctx = talloc_stackframe();
3594 struct security_descriptor *psd = NULL;
3595 bool guest_ok = false;
3596 char *canon_name = NULL;
3597 bool added_service = false;
3598 int ret = -1;
3600 /* Ensure share name doesn't contain invalid characters. */
3601 if (!validate_net_name(file_name, INVALID_SHARENAME_CHARS, strlen(file_name))) {
3602 DEBUG(0,("process_usershare_file: share name %s contains "
3603 "invalid characters (any of %s)\n",
3604 file_name, INVALID_SHARENAME_CHARS ));
3605 goto out;
3608 canon_name = canonicalize_servicename(ctx, file_name);
3609 if (!canon_name) {
3610 goto out;
3613 fname = talloc_asprintf(ctx, "%s/%s", dir_name, file_name);
3614 if (!fname) {
3615 goto out;
3618 /* Minimize the race condition by doing an lstat before we
3619 open and fstat. Ensure this isn't a symlink link. */
3621 if (sys_lstat(fname, &lsbuf, false) != 0) {
3622 DEBUG(0,("process_usershare_file: stat of %s failed. %s\n",
3623 fname, strerror(errno) ));
3624 goto out;
3627 /* This must be a regular file, not a symlink, directory or
3628 other strange filetype. */
3629 if (!check_usershare_stat(fname, &lsbuf)) {
3630 goto out;
3634 TDB_DATA data;
3635 NTSTATUS status;
3637 status = dbwrap_fetch_bystring(ServiceHash, canon_name,
3638 canon_name, &data);
3640 iService = -1;
3642 if (NT_STATUS_IS_OK(status) &&
3643 (data.dptr != NULL) &&
3644 (data.dsize == sizeof(iService))) {
3645 memcpy(&iService, data.dptr, sizeof(iService));
3649 if (iService != -1 &&
3650 timespec_compare(&ServicePtrs[iService]->usershare_last_mod,
3651 &lsbuf.st_ex_mtime) == 0) {
3652 /* Nothing changed - Mark valid and return. */
3653 DEBUG(10,("process_usershare_file: service %s not changed.\n",
3654 canon_name ));
3655 ServicePtrs[iService]->usershare = USERSHARE_VALID;
3656 ret = iService;
3657 goto out;
3660 /* Try and open the file read only - no symlinks allowed. */
3661 #ifdef O_NOFOLLOW
3662 fd = open(fname, O_RDONLY|O_NOFOLLOW, 0);
3663 #else
3664 fd = open(fname, O_RDONLY, 0);
3665 #endif
3667 if (fd == -1) {
3668 DEBUG(0,("process_usershare_file: unable to open %s. %s\n",
3669 fname, strerror(errno) ));
3670 goto out;
3673 /* Now fstat to be *SURE* it's a regular file. */
3674 if (sys_fstat(fd, &sbuf, false) != 0) {
3675 close(fd);
3676 DEBUG(0,("process_usershare_file: fstat of %s failed. %s\n",
3677 fname, strerror(errno) ));
3678 goto out;
3681 /* Is it the same dev/inode as was lstated ? */
3682 if (!check_same_stat(&lsbuf, &sbuf)) {
3683 close(fd);
3684 DEBUG(0,("process_usershare_file: fstat of %s is a different file from lstat. "
3685 "Symlink spoofing going on ?\n", fname ));
3686 goto out;
3689 /* This must be a regular file, not a symlink, directory or
3690 other strange filetype. */
3691 if (!check_usershare_stat(fname, &sbuf)) {
3692 close(fd);
3693 goto out;
3696 lines = fd_lines_load(fd, &numlines, MAX_USERSHARE_FILE_SIZE, NULL);
3698 close(fd);
3699 if (lines == NULL) {
3700 DEBUG(0,("process_usershare_file: loading file %s owned by %u failed.\n",
3701 fname, (unsigned int)sbuf.st_ex_uid ));
3702 goto out;
3705 if (parse_usershare_file(ctx, &sbuf, file_name,
3706 iService, lines, numlines, &sharepath,
3707 &comment, &cp_service_name,
3708 &psd, &guest_ok) != USERSHARE_OK) {
3709 goto out;
3712 /* Everything ok - add the service possibly using a template. */
3713 if (iService < 0) {
3714 const struct loadparm_service *sp = &sDefault;
3715 if (snum_template != -1) {
3716 sp = ServicePtrs[snum_template];
3719 if ((iService = add_a_service(sp, cp_service_name)) < 0) {
3720 DEBUG(0, ("process_usershare_file: Failed to add "
3721 "new service %s\n", cp_service_name));
3722 goto out;
3725 added_service = true;
3727 /* Read only is controlled by usershare ACL below. */
3728 ServicePtrs[iService]->read_only = false;
3731 /* Write the ACL of the new/modified share. */
3732 if (!set_share_security(canon_name, psd)) {
3733 DEBUG(0, ("process_usershare_file: Failed to set share "
3734 "security for user share %s\n",
3735 canon_name ));
3736 goto out;
3739 /* If from a template it may be marked invalid. */
3740 ServicePtrs[iService]->valid = true;
3742 /* Set the service as a valid usershare. */
3743 ServicePtrs[iService]->usershare = USERSHARE_VALID;
3745 /* Set guest access. */
3746 if (lp_usershare_allow_guests()) {
3747 ServicePtrs[iService]->guest_ok = guest_ok;
3750 /* And note when it was loaded. */
3751 ServicePtrs[iService]->usershare_last_mod = sbuf.st_ex_mtime;
3752 string_set(ServicePtrs[iService], &ServicePtrs[iService]->path, sharepath);
3753 string_set(ServicePtrs[iService], &ServicePtrs[iService]->comment, comment);
3755 ret = iService;
3757 out:
3759 if (ret == -1 && iService != -1 && added_service) {
3760 lp_remove_service(iService);
3763 TALLOC_FREE(lines);
3764 TALLOC_FREE(ctx);
3765 return ret;
3768 /***************************************************************************
3769 Checks if a usershare entry has been modified since last load.
3770 ***************************************************************************/
3772 static bool usershare_exists(int iService, struct timespec *last_mod)
3774 SMB_STRUCT_STAT lsbuf;
3775 const char *usersharepath = Globals.usershare_path;
3776 char *fname;
3778 fname = talloc_asprintf(talloc_tos(),
3779 "%s/%s",
3780 usersharepath,
3781 ServicePtrs[iService]->szService);
3782 if (fname == NULL) {
3783 return false;
3786 if (sys_lstat(fname, &lsbuf, false) != 0) {
3787 TALLOC_FREE(fname);
3788 return false;
3791 if (!S_ISREG(lsbuf.st_ex_mode)) {
3792 TALLOC_FREE(fname);
3793 return false;
3796 TALLOC_FREE(fname);
3797 *last_mod = lsbuf.st_ex_mtime;
3798 return true;
3801 /***************************************************************************
3802 Load a usershare service by name. Returns a valid servicenumber or -1.
3803 ***************************************************************************/
3805 int load_usershare_service(const char *servicename)
3807 SMB_STRUCT_STAT sbuf;
3808 const char *usersharepath = Globals.usershare_path;
3809 int max_user_shares = Globals.usershare_max_shares;
3810 int snum_template = -1;
3812 if (*usersharepath == 0 || max_user_shares == 0) {
3813 return -1;
3816 if (sys_stat(usersharepath, &sbuf, false) != 0) {
3817 DEBUG(0,("load_usershare_service: stat of %s failed. %s\n",
3818 usersharepath, strerror(errno) ));
3819 return -1;
3822 if (!S_ISDIR(sbuf.st_ex_mode)) {
3823 DEBUG(0,("load_usershare_service: %s is not a directory.\n",
3824 usersharepath ));
3825 return -1;
3829 * This directory must be owned by root, and have the 't' bit set.
3830 * It also must not be writable by "other".
3833 #ifdef S_ISVTX
3834 if (sbuf.st_ex_uid != 0 || !(sbuf.st_ex_mode & S_ISVTX) || (sbuf.st_ex_mode & S_IWOTH)) {
3835 #else
3836 if (sbuf.st_ex_uid != 0 || (sbuf.st_ex_mode & S_IWOTH)) {
3837 #endif
3838 DEBUG(0,("load_usershare_service: directory %s is not owned by root "
3839 "or does not have the sticky bit 't' set or is writable by anyone.\n",
3840 usersharepath ));
3841 return -1;
3844 /* Ensure the template share exists if it's set. */
3845 if (Globals.usershare_template_share[0]) {
3846 /* We can't use lp_servicenumber here as we are recommending that
3847 template shares have -valid=false set. */
3848 for (snum_template = iNumServices - 1; snum_template >= 0; snum_template--) {
3849 if (ServicePtrs[snum_template]->szService &&
3850 strequal(ServicePtrs[snum_template]->szService,
3851 Globals.usershare_template_share)) {
3852 break;
3856 if (snum_template == -1) {
3857 DEBUG(0,("load_usershare_service: usershare template share %s "
3858 "does not exist.\n",
3859 Globals.usershare_template_share ));
3860 return -1;
3864 return process_usershare_file(usersharepath, servicename, snum_template);
3867 /***************************************************************************
3868 Load all user defined shares from the user share directory.
3869 We only do this if we're enumerating the share list.
3870 This is the function that can delete usershares that have
3871 been removed.
3872 ***************************************************************************/
3874 int load_usershare_shares(struct smbd_server_connection *sconn,
3875 bool (*snumused) (struct smbd_server_connection *, int))
3877 DIR *dp;
3878 SMB_STRUCT_STAT sbuf;
3879 struct dirent *de;
3880 int num_usershares = 0;
3881 int max_user_shares = Globals.usershare_max_shares;
3882 unsigned int num_dir_entries, num_bad_dir_entries, num_tmp_dir_entries;
3883 unsigned int allowed_bad_entries = ((2*max_user_shares)/10);
3884 unsigned int allowed_tmp_entries = ((2*max_user_shares)/10);
3885 int iService;
3886 int snum_template = -1;
3887 const char *usersharepath = Globals.usershare_path;
3888 int ret = lp_numservices();
3889 TALLOC_CTX *tmp_ctx;
3891 if (max_user_shares == 0 || *usersharepath == '\0') {
3892 return lp_numservices();
3895 if (sys_stat(usersharepath, &sbuf, false) != 0) {
3896 DEBUG(0,("load_usershare_shares: stat of %s failed. %s\n",
3897 usersharepath, strerror(errno) ));
3898 return ret;
3902 * This directory must be owned by root, and have the 't' bit set.
3903 * It also must not be writable by "other".
3906 #ifdef S_ISVTX
3907 if (sbuf.st_ex_uid != 0 || !(sbuf.st_ex_mode & S_ISVTX) || (sbuf.st_ex_mode & S_IWOTH)) {
3908 #else
3909 if (sbuf.st_ex_uid != 0 || (sbuf.st_ex_mode & S_IWOTH)) {
3910 #endif
3911 DEBUG(0,("load_usershare_shares: directory %s is not owned by root "
3912 "or does not have the sticky bit 't' set or is writable by anyone.\n",
3913 usersharepath ));
3914 return ret;
3917 /* Ensure the template share exists if it's set. */
3918 if (Globals.usershare_template_share[0]) {
3919 /* We can't use lp_servicenumber here as we are recommending that
3920 template shares have -valid=false set. */
3921 for (snum_template = iNumServices - 1; snum_template >= 0; snum_template--) {
3922 if (ServicePtrs[snum_template]->szService &&
3923 strequal(ServicePtrs[snum_template]->szService,
3924 Globals.usershare_template_share)) {
3925 break;
3929 if (snum_template == -1) {
3930 DEBUG(0,("load_usershare_shares: usershare template share %s "
3931 "does not exist.\n",
3932 Globals.usershare_template_share ));
3933 return ret;
3937 /* Mark all existing usershares as pending delete. */
3938 for (iService = iNumServices - 1; iService >= 0; iService--) {
3939 if (VALID(iService) && ServicePtrs[iService]->usershare) {
3940 ServicePtrs[iService]->usershare = USERSHARE_PENDING_DELETE;
3944 dp = opendir(usersharepath);
3945 if (!dp) {
3946 DEBUG(0,("load_usershare_shares:: failed to open directory %s. %s\n",
3947 usersharepath, strerror(errno) ));
3948 return ret;
3951 for (num_dir_entries = 0, num_bad_dir_entries = 0, num_tmp_dir_entries = 0;
3952 (de = readdir(dp));
3953 num_dir_entries++ ) {
3954 int r;
3955 const char *n = de->d_name;
3957 /* Ignore . and .. */
3958 if (*n == '.') {
3959 if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
3960 continue;
3964 if (n[0] == ':') {
3965 /* Temporary file used when creating a share. */
3966 num_tmp_dir_entries++;
3969 /* Allow 20% tmp entries. */
3970 if (num_tmp_dir_entries > allowed_tmp_entries) {
3971 DEBUG(0,("load_usershare_shares: too many temp entries (%u) "
3972 "in directory %s\n",
3973 num_tmp_dir_entries, usersharepath));
3974 break;
3977 r = process_usershare_file(usersharepath, n, snum_template);
3978 if (r == 0) {
3979 /* Update the services count. */
3980 num_usershares++;
3981 if (num_usershares >= max_user_shares) {
3982 DEBUG(0,("load_usershare_shares: max user shares reached "
3983 "on file %s in directory %s\n",
3984 n, usersharepath ));
3985 break;
3987 } else if (r == -1) {
3988 num_bad_dir_entries++;
3991 /* Allow 20% bad entries. */
3992 if (num_bad_dir_entries > allowed_bad_entries) {
3993 DEBUG(0,("load_usershare_shares: too many bad entries (%u) "
3994 "in directory %s\n",
3995 num_bad_dir_entries, usersharepath));
3996 break;
3999 /* Allow 20% bad entries. */
4000 if (num_dir_entries > max_user_shares + allowed_bad_entries) {
4001 DEBUG(0,("load_usershare_shares: too many total entries (%u) "
4002 "in directory %s\n",
4003 num_dir_entries, usersharepath));
4004 break;
4008 closedir(dp);
4010 /* Sweep through and delete any non-refreshed usershares that are
4011 not currently in use. */
4012 tmp_ctx = talloc_stackframe();
4013 for (iService = iNumServices - 1; iService >= 0; iService--) {
4014 if (VALID(iService) && (ServicePtrs[iService]->usershare == USERSHARE_PENDING_DELETE)) {
4015 char *servname;
4017 if (snumused && snumused(sconn, iService)) {
4018 continue;
4021 servname = lp_servicename(tmp_ctx, iService);
4023 /* Remove from the share ACL db. */
4024 DEBUG(10,("load_usershare_shares: Removing deleted usershare %s\n",
4025 servname ));
4026 delete_share_security(servname);
4027 free_service_byindex(iService);
4030 talloc_free(tmp_ctx);
4032 return lp_numservices();
4035 /********************************************************
4036 Destroy global resources allocated in this file
4037 ********************************************************/
4039 void gfree_loadparm(void)
4041 int i;
4043 free_file_list();
4045 /* Free resources allocated to services */
4047 for ( i = 0; i < iNumServices; i++ ) {
4048 if ( VALID(i) ) {
4049 free_service_byindex(i);
4053 TALLOC_FREE( ServicePtrs );
4054 iNumServices = 0;
4056 /* Now release all resources allocated to global
4057 parameters and the default service */
4059 free_global_parameters();
4063 /***************************************************************************
4064 Allow client apps to specify that they are a client
4065 ***************************************************************************/
4066 static void lp_set_in_client(bool b)
4068 in_client = b;
4072 /***************************************************************************
4073 Determine if we're running in a client app
4074 ***************************************************************************/
4075 static bool lp_is_in_client(void)
4077 return in_client;
4080 /***************************************************************************
4081 Load the services array from the services file. Return true on success,
4082 false on failure.
4083 ***************************************************************************/
4085 static bool lp_load_ex(const char *pszFname,
4086 bool global_only,
4087 bool save_defaults,
4088 bool add_ipc,
4089 bool initialize_globals,
4090 bool allow_include_registry,
4091 bool load_all_shares)
4093 char *n2 = NULL;
4094 bool bRetval;
4096 bRetval = false;
4098 DEBUG(3, ("lp_load_ex: refreshing parameters\n"));
4100 bInGlobalSection = true;
4101 bGlobalOnly = global_only;
4102 bAllowIncludeRegistry = allow_include_registry;
4104 init_globals(initialize_globals);
4106 free_file_list();
4108 if (save_defaults) {
4109 init_locals();
4110 lp_save_defaults();
4113 if (!initialize_globals) {
4114 free_param_opts(&Globals.param_opt);
4115 apply_lp_set_cmdline();
4118 lp_do_parameter(-1, "idmap config * : backend", Globals.szIdmapBackend);
4120 /* We get sections first, so have to start 'behind' to make up */
4121 iServiceIndex = -1;
4123 if (lp_config_backend_is_file()) {
4124 n2 = talloc_sub_basic(talloc_tos(), get_current_username(),
4125 current_user_info.domain,
4126 pszFname);
4127 if (!n2) {
4128 smb_panic("lp_load_ex: out of memory");
4131 add_to_file_list(NULL, &file_lists, pszFname, n2);
4133 bRetval = pm_process(n2, do_section, do_parameter, NULL);
4134 TALLOC_FREE(n2);
4136 /* finish up the last section */
4137 DEBUG(4, ("pm_process() returned %s\n", BOOLSTR(bRetval)));
4138 if (bRetval) {
4139 if (iServiceIndex >= 0) {
4140 bRetval = service_ok(iServiceIndex);
4144 if (lp_config_backend_is_registry()) {
4145 /* config backend changed to registry in config file */
4147 * We need to use this extra global variable here to
4148 * survive restart: init_globals uses this as a default
4149 * for config_backend. Otherwise, init_globals would
4150 * send us into an endless loop here.
4152 config_backend = CONFIG_BACKEND_REGISTRY;
4153 /* start over */
4154 DEBUG(1, ("lp_load_ex: changing to config backend "
4155 "registry\n"));
4156 init_globals(true);
4157 lp_kill_all_services();
4158 return lp_load_ex(pszFname, global_only, save_defaults,
4159 add_ipc, initialize_globals,
4160 allow_include_registry,
4161 load_all_shares);
4163 } else if (lp_config_backend_is_registry()) {
4164 bRetval = process_registry_globals();
4165 } else {
4166 DEBUG(0, ("Illegal config backend given: %d\n",
4167 lp_config_backend()));
4168 bRetval = false;
4171 if (bRetval && lp_registry_shares()) {
4172 if (load_all_shares) {
4173 bRetval = process_registry_shares();
4174 } else {
4175 bRetval = reload_registry_shares();
4180 char *serv = lp_auto_services(talloc_tos());
4181 lp_add_auto_services(serv);
4182 TALLOC_FREE(serv);
4185 if (add_ipc) {
4186 /* When 'restrict anonymous = 2' guest connections to ipc$
4187 are denied */
4188 lp_add_ipc("IPC$", (lp_restrict_anonymous() < 2));
4189 if ( lp_enable_asu_support() ) {
4190 lp_add_ipc("ADMIN$", false);
4194 set_allowed_client_auth();
4196 if (lp_security() == SEC_ADS && strchr(lp_password_server(), ':')) {
4197 DEBUG(1, ("WARNING: The optional ':port' in password server = %s is deprecated\n",
4198 lp_password_server()));
4201 bLoaded = true;
4203 /* Now we check we_are_a_wins_server and set szWINSserver to 127.0.0.1 */
4204 /* if we_are_a_wins_server is true and we are in the client */
4205 if (lp_is_in_client() && Globals.we_are_a_wins_server) {
4206 lp_do_parameter(GLOBAL_SECTION_SNUM, "wins server", "127.0.0.1");
4209 init_iconv();
4211 fault_configure(smb_panic_s3);
4214 * We run this check once the whole smb.conf is parsed, to
4215 * force some settings for the standard way a AD DC is
4216 * operated. We may changed these as our code evolves, which
4217 * is why we force these settings.
4219 if (lp_server_role() == ROLE_ACTIVE_DIRECTORY_DC) {
4220 lp_do_parameter(-1, "passdb backend", "samba_dsdb");
4222 lp_do_parameter(-1, "rpc_server:default", "external");
4223 lp_do_parameter(-1, "rpc_server:svcctl", "embedded");
4224 lp_do_parameter(-1, "rpc_server:srvsvc", "embedded");
4225 lp_do_parameter(-1, "rpc_server:eventlog", "embedded");
4226 lp_do_parameter(-1, "rpc_server:ntsvcs", "embedded");
4227 lp_do_parameter(-1, "rpc_server:winreg", "embedded");
4228 lp_do_parameter(-1, "rpc_server:spoolss", "embedded");
4229 lp_do_parameter(-1, "rpc_daemon:spoolssd", "embedded");
4230 lp_do_parameter(-1, "rpc_server:tcpip", "no");
4233 bAllowIncludeRegistry = true;
4235 return (bRetval);
4238 bool lp_load(const char *pszFname,
4239 bool global_only,
4240 bool save_defaults,
4241 bool add_ipc,
4242 bool initialize_globals)
4244 return lp_load_ex(pszFname,
4245 global_only,
4246 save_defaults,
4247 add_ipc,
4248 initialize_globals,
4249 true, /* allow_include_registry */
4250 false); /* load_all_shares*/
4253 bool lp_load_initial_only(const char *pszFname)
4255 return lp_load_ex(pszFname,
4256 true, /* global only */
4257 false, /* save_defaults */
4258 false, /* add_ipc */
4259 true, /* initialize_globals */
4260 false, /* allow_include_registry */
4261 false); /* load_all_shares*/
4265 * most common lp_load wrapper, loading only the globals
4267 bool lp_load_global(const char *file_name)
4269 return lp_load_ex(file_name,
4270 true, /* global_only */
4271 false, /* save_defaults */
4272 false, /* add_ipc */
4273 true, /* initialize_globals */
4274 true, /* allow_include_registry */
4275 false); /* load_all_shares*/
4279 * lp_load wrapper, especially for clients
4281 bool lp_load_client(const char *file_name)
4283 lp_set_in_client(true);
4285 return lp_load_global(file_name);
4289 * lp_load wrapper, loading only globals, but intended
4290 * for subsequent calls, not reinitializing the globals
4291 * to default values
4293 bool lp_load_global_no_reinit(const char *file_name)
4295 return lp_load_ex(file_name,
4296 true, /* global_only */
4297 false, /* save_defaults */
4298 false, /* add_ipc */
4299 false, /* initialize_globals */
4300 true, /* allow_include_registry */
4301 false); /* load_all_shares*/
4305 * lp_load wrapper, especially for clients, no reinitialization
4307 bool lp_load_client_no_reinit(const char *file_name)
4309 lp_set_in_client(true);
4311 return lp_load_global_no_reinit(file_name);
4314 bool lp_load_with_registry_shares(const char *pszFname,
4315 bool global_only,
4316 bool save_defaults,
4317 bool add_ipc,
4318 bool initialize_globals)
4320 return lp_load_ex(pszFname,
4321 global_only,
4322 save_defaults,
4323 add_ipc,
4324 initialize_globals,
4325 true, /* allow_include_registry */
4326 true); /* load_all_shares*/
4329 /***************************************************************************
4330 Return the max number of services.
4331 ***************************************************************************/
4333 int lp_numservices(void)
4335 return (iNumServices);
4338 /***************************************************************************
4339 Display the contents of the services array in human-readable form.
4340 ***************************************************************************/
4342 void lp_dump(FILE *f, bool show_defaults, int maxtoprint)
4344 int iService;
4346 if (show_defaults)
4347 defaults_saved = false;
4349 dump_globals(f);
4351 dump_a_service(&sDefault, f);
4353 for (iService = 0; iService < maxtoprint; iService++) {
4354 fprintf(f,"\n");
4355 lp_dump_one(f, show_defaults, iService);
4359 /***************************************************************************
4360 Display the contents of one service in human-readable form.
4361 ***************************************************************************/
4363 void lp_dump_one(FILE * f, bool show_defaults, int snum)
4365 if (VALID(snum)) {
4366 if (ServicePtrs[snum]->szService[0] == '\0')
4367 return;
4368 dump_a_service(ServicePtrs[snum], f);
4372 /***************************************************************************
4373 Return the number of the service with the given name, or -1 if it doesn't
4374 exist. Note that this is a DIFFERENT ANIMAL from the internal function
4375 getservicebyname()! This works ONLY if all services have been loaded, and
4376 does not copy the found service.
4377 ***************************************************************************/
4379 int lp_servicenumber(const char *pszServiceName)
4381 int iService;
4382 fstring serviceName;
4384 if (!pszServiceName) {
4385 return GLOBAL_SECTION_SNUM;
4388 for (iService = iNumServices - 1; iService >= 0; iService--) {
4389 if (VALID(iService) && ServicePtrs[iService]->szService) {
4391 * The substitution here is used to support %U is
4392 * service names
4394 fstrcpy(serviceName, ServicePtrs[iService]->szService);
4395 standard_sub_basic(get_current_username(),
4396 current_user_info.domain,
4397 serviceName,sizeof(serviceName));
4398 if (strequal(serviceName, pszServiceName)) {
4399 break;
4404 if (iService >= 0 && ServicePtrs[iService]->usershare == USERSHARE_VALID) {
4405 struct timespec last_mod;
4407 if (!usershare_exists(iService, &last_mod)) {
4408 /* Remove the share security tdb entry for it. */
4409 delete_share_security(lp_servicename(talloc_tos(), iService));
4410 /* Remove it from the array. */
4411 free_service_byindex(iService);
4412 /* Doesn't exist anymore. */
4413 return GLOBAL_SECTION_SNUM;
4416 /* Has it been modified ? If so delete and reload. */
4417 if (timespec_compare(&ServicePtrs[iService]->usershare_last_mod,
4418 &last_mod) < 0) {
4419 /* Remove it from the array. */
4420 free_service_byindex(iService);
4421 /* and now reload it. */
4422 iService = load_usershare_service(pszServiceName);
4426 if (iService < 0) {
4427 DEBUG(7,("lp_servicenumber: couldn't find %s\n", pszServiceName));
4428 return GLOBAL_SECTION_SNUM;
4431 return (iService);
4434 /*******************************************************************
4435 A useful volume label function.
4436 ********************************************************************/
4438 const char *volume_label(TALLOC_CTX *ctx, int snum)
4440 char *ret;
4441 const char *label = lp_volume(ctx, snum);
4442 if (!*label) {
4443 label = lp_servicename(ctx, snum);
4446 /* This returns a 33 byte guarenteed null terminated string. */
4447 ret = talloc_strndup(ctx, label, 32);
4448 if (!ret) {
4449 return "";
4451 return ret;
4454 /*******************************************************************
4455 Get the default server type we will announce as via nmbd.
4456 ********************************************************************/
4458 int lp_default_server_announce(void)
4460 int default_server_announce = 0;
4461 default_server_announce |= SV_TYPE_WORKSTATION;
4462 default_server_announce |= SV_TYPE_SERVER;
4463 default_server_announce |= SV_TYPE_SERVER_UNIX;
4465 /* note that the flag should be set only if we have a
4466 printer service but nmbd doesn't actually load the
4467 services so we can't tell --jerry */
4469 default_server_announce |= SV_TYPE_PRINTQ_SERVER;
4471 default_server_announce |= SV_TYPE_SERVER_NT;
4472 default_server_announce |= SV_TYPE_NT;
4474 switch (lp_server_role()) {
4475 case ROLE_DOMAIN_MEMBER:
4476 default_server_announce |= SV_TYPE_DOMAIN_MEMBER;
4477 break;
4478 case ROLE_DOMAIN_PDC:
4479 default_server_announce |= SV_TYPE_DOMAIN_CTRL;
4480 break;
4481 case ROLE_DOMAIN_BDC:
4482 default_server_announce |= SV_TYPE_DOMAIN_BAKCTRL;
4483 break;
4484 case ROLE_STANDALONE:
4485 default:
4486 break;
4488 if (lp_time_server())
4489 default_server_announce |= SV_TYPE_TIME_SOURCE;
4491 if (lp_host_msdfs())
4492 default_server_announce |= SV_TYPE_DFS_SERVER;
4494 return default_server_announce;
4497 /***********************************************************
4498 If we are PDC then prefer us as DMB
4499 ************************************************************/
4501 bool lp_domain_master(void)
4503 if (Globals._domain_master == Auto)
4504 return (lp_server_role() == ROLE_DOMAIN_PDC);
4506 return (bool)Globals._domain_master;
4509 /***********************************************************
4510 If we are PDC then prefer us as DMB
4511 ************************************************************/
4513 static bool lp_domain_master_true_or_auto(void)
4515 if (Globals._domain_master) /* auto or yes */
4516 return true;
4518 return false;
4521 /***********************************************************
4522 If we are DMB then prefer us as LMB
4523 ************************************************************/
4525 bool lp_preferred_master(void)
4527 if (Globals.iPreferredMaster == Auto)
4528 return (lp_local_master() && lp_domain_master());
4530 return (bool)Globals.iPreferredMaster;
4533 /*******************************************************************
4534 Remove a service.
4535 ********************************************************************/
4537 void lp_remove_service(int snum)
4539 ServicePtrs[snum]->valid = false;
4542 const char *lp_printername(TALLOC_CTX *ctx, int snum)
4544 const char *ret = lp__printername(ctx, snum);
4545 if (ret == NULL || *ret == '\0') {
4546 ret = lp_const_servicename(snum);
4549 return ret;
4553 /***********************************************************
4554 Allow daemons such as winbindd to fix their logfile name.
4555 ************************************************************/
4557 void lp_set_logfile(const char *name)
4559 string_set(Globals.ctx, &Globals.logfile, name);
4560 debug_set_logfile(name);
4563 /*******************************************************************
4564 Return the max print jobs per queue.
4565 ********************************************************************/
4567 int lp_maxprintjobs(int snum)
4569 int maxjobs = LP_SNUM_OK(snum) ? ServicePtrs[snum]->iMaxPrintJobs : sDefault.iMaxPrintJobs;
4570 if (maxjobs <= 0 || maxjobs >= PRINT_MAX_JOBID)
4571 maxjobs = PRINT_MAX_JOBID - 1;
4573 return maxjobs;
4576 const char *lp_printcapname(void)
4578 if ((Globals.szPrintcapname != NULL) &&
4579 (Globals.szPrintcapname[0] != '\0'))
4580 return Globals.szPrintcapname;
4582 if (sDefault.printing == PRINT_CUPS) {
4583 return "cups";
4586 if (sDefault.printing == PRINT_BSD)
4587 return "/etc/printcap";
4589 return PRINTCAP_NAME;
4592 static uint32 spoolss_state;
4594 bool lp_disable_spoolss( void )
4596 if ( spoolss_state == SVCCTL_STATE_UNKNOWN )
4597 spoolss_state = lp__disable_spoolss() ? SVCCTL_STOPPED : SVCCTL_RUNNING;
4599 return spoolss_state == SVCCTL_STOPPED ? true : false;
4602 void lp_set_spoolss_state( uint32 state )
4604 SMB_ASSERT( (state == SVCCTL_STOPPED) || (state == SVCCTL_RUNNING) );
4606 spoolss_state = state;
4609 uint32 lp_get_spoolss_state( void )
4611 return lp_disable_spoolss() ? SVCCTL_STOPPED : SVCCTL_RUNNING;
4614 /*******************************************************************
4615 Ensure we don't use sendfile if server smb signing is active.
4616 ********************************************************************/
4618 bool lp_use_sendfile(int snum, struct smb_signing_state *signing_state)
4620 bool sign_active = false;
4622 /* Using sendfile blows the brains out of any DOS or Win9x TCP stack... JRA. */
4623 if (get_Protocol() < PROTOCOL_NT1) {
4624 return false;
4626 if (signing_state) {
4627 sign_active = smb_signing_is_active(signing_state);
4629 return (lp__use_sendfile(snum) &&
4630 (get_remote_arch() != RA_WIN95) &&
4631 !sign_active);
4634 /*******************************************************************
4635 Turn off sendfile if we find the underlying OS doesn't support it.
4636 ********************************************************************/
4638 void set_use_sendfile(int snum, bool val)
4640 if (LP_SNUM_OK(snum))
4641 ServicePtrs[snum]->_use_sendfile = val;
4642 else
4643 sDefault._use_sendfile = val;
4646 /*******************************************************************
4647 Turn off storing DOS attributes if this share doesn't support it.
4648 ********************************************************************/
4650 void set_store_dos_attributes(int snum, bool val)
4652 if (!LP_SNUM_OK(snum))
4653 return;
4654 ServicePtrs[(snum)]->store_dos_attributes = val;
4657 void lp_set_mangling_method(const char *new_method)
4659 string_set(Globals.ctx, &Globals.mangling_method, new_method);
4662 /*******************************************************************
4663 Global state for POSIX pathname processing.
4664 ********************************************************************/
4666 static bool posix_pathnames;
4668 bool lp_posix_pathnames(void)
4670 return posix_pathnames;
4673 /*******************************************************************
4674 Change everything needed to ensure POSIX pathname processing (currently
4675 not much).
4676 ********************************************************************/
4678 void lp_set_posix_pathnames(void)
4680 posix_pathnames = true;
4683 /*******************************************************************
4684 Global state for POSIX lock processing - CIFS unix extensions.
4685 ********************************************************************/
4687 bool posix_default_lock_was_set;
4688 static enum brl_flavour posix_cifsx_locktype; /* By default 0 == WINDOWS_LOCK */
4690 enum brl_flavour lp_posix_cifsu_locktype(files_struct *fsp)
4692 if (posix_default_lock_was_set) {
4693 return posix_cifsx_locktype;
4694 } else {
4695 return fsp->posix_open ? POSIX_LOCK : WINDOWS_LOCK;
4699 /*******************************************************************
4700 ********************************************************************/
4702 void lp_set_posix_default_cifsx_readwrite_locktype(enum brl_flavour val)
4704 posix_default_lock_was_set = true;
4705 posix_cifsx_locktype = val;
4708 int lp_min_receive_file_size(void)
4710 if (Globals.iminreceivefile < 0) {
4711 return 0;
4713 return Globals.iminreceivefile;
4716 /*******************************************************************
4717 Safe wide links checks.
4718 This helper function always verify the validity of wide links,
4719 even after a configuration file reload.
4720 ********************************************************************/
4722 static bool lp_widelinks_internal(int snum)
4724 return (bool)(LP_SNUM_OK(snum)? ServicePtrs[(snum)]->bWidelinks :
4725 sDefault.bWidelinks);
4728 void widelinks_warning(int snum)
4730 if (lp_allow_insecure_wide_links()) {
4731 return;
4734 if (lp_unix_extensions() && lp_widelinks_internal(snum)) {
4735 DEBUG(0,("Share '%s' has wide links and unix extensions enabled. "
4736 "These parameters are incompatible. "
4737 "Wide links will be disabled for this share.\n",
4738 lp_servicename(talloc_tos(), snum) ));
4742 bool lp_widelinks(int snum)
4744 /* wide links is always incompatible with unix extensions */
4745 if (lp_unix_extensions()) {
4747 * Unless we have "allow insecure widelinks"
4748 * turned on.
4750 if (!lp_allow_insecure_wide_links()) {
4751 return false;
4755 return lp_widelinks_internal(snum);
4758 int lp_server_role(void)
4760 return lp_find_server_role(lp__server_role(),
4761 lp__security(),
4762 lp__domain_logons(),
4763 lp_domain_master_true_or_auto());
4766 int lp_security(void)
4768 return lp_find_security(lp__server_role(),
4769 lp__security());
4772 struct loadparm_global * get_globals(void)
4774 return &Globals;