param: use a single handle_include function between the two loadparms
[Samba.git] / source3 / param / loadparm.c
blob554106e25b7267fc2ea71426334129e9da828c26
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 (sizeof(parm_table) / sizeof(struct parm_struct))
263 /* prototypes for the special type handlers */
264 static bool handle_idmap_backend(struct loadparm_context *unused, int snum, const char *pszParmValue, char **ptr);
265 static bool handle_idmap_uid(struct loadparm_context *unused, int snum, const char *pszParmValue, char **ptr);
266 static bool handle_idmap_gid(struct loadparm_context *unused, int snum, const char *pszParmValue, char **ptr);
267 static bool handle_netbios_aliases(struct loadparm_context *unused, int snum, const char *pszParmValue, char **ptr );
268 static bool handle_printing(struct loadparm_context *unused, int snum, const char *pszParmValue, char **ptr);
269 static bool handle_ldap_debug_level(struct loadparm_context *unused, int snum, const char *pszParmValue, char **ptr);
271 static void set_allowed_client_auth(void);
273 static bool lp_set_cmdline_helper(const char *pszParmName, const char *pszParmValue, bool store_values);
274 static void free_param_opts(struct parmlist_entry **popts);
276 #include "lib/param/param_table.c"
278 /* this is used to prevent lots of mallocs of size 1 */
279 static const char null_string[] = "";
282 Free a string value.
285 static void string_free(char **s)
287 if (!s || !(*s))
288 return;
289 if (*s == null_string)
290 *s = NULL;
291 TALLOC_FREE(*s);
295 Set a string value, deallocating any existing space, and allocing the space
296 for the string
299 static bool string_set(TALLOC_CTX *mem_ctx, char **dest,const char *src)
301 string_free(dest);
303 if (!src) {
304 src = "";
307 (*dest) = talloc_strdup(mem_ctx, src);
308 if ((*dest) == NULL) {
309 DEBUG(0,("Out of memory in string_init\n"));
310 return false;
313 return true;
316 bool lp_string_set(char **dest, const char *src) {
317 return string_set(Globals.ctx, dest, src);
320 /***************************************************************************
321 Initialise the sDefault parameter structure for the printer values.
322 ***************************************************************************/
324 static void init_printer_values(TALLOC_CTX *ctx, struct loadparm_service *pService)
326 /* choose defaults depending on the type of printing */
327 switch (pService->printing) {
328 case PRINT_BSD:
329 case PRINT_AIX:
330 case PRINT_LPRNT:
331 case PRINT_LPROS2:
332 string_set(ctx, &pService->lpq_command, "lpq -P'%p'");
333 string_set(ctx, &pService->lprm_command, "lprm -P'%p' %j");
334 string_set(ctx, &pService->print_command, "lpr -r -P'%p' %s");
335 break;
337 case PRINT_LPRNG:
338 case PRINT_PLP:
339 string_set(ctx, &pService->lpq_command, "lpq -P'%p'");
340 string_set(ctx, &pService->lprm_command, "lprm -P'%p' %j");
341 string_set(ctx, &pService->print_command, "lpr -r -P'%p' %s");
342 string_set(ctx, &pService->queuepause_command, "lpc stop '%p'");
343 string_set(ctx, &pService->queueresume_command, "lpc start '%p'");
344 string_set(ctx, &pService->lppause_command, "lpc hold '%p' %j");
345 string_set(ctx, &pService->lpresume_command, "lpc release '%p' %j");
346 break;
348 case PRINT_CUPS:
349 case PRINT_IPRINT:
350 /* set the lpq command to contain the destination printer
351 name only. This is used by cups_queue_get() */
352 string_set(ctx, &pService->lpq_command, "%p");
353 string_set(ctx, &pService->lprm_command, "");
354 string_set(ctx, &pService->print_command, "");
355 string_set(ctx, &pService->lppause_command, "");
356 string_set(ctx, &pService->lpresume_command, "");
357 string_set(ctx, &pService->queuepause_command, "");
358 string_set(ctx, &pService->queueresume_command, "");
359 break;
361 case PRINT_SYSV:
362 case PRINT_HPUX:
363 string_set(ctx, &pService->lpq_command, "lpstat -o%p");
364 string_set(ctx, &pService->lprm_command, "cancel %p-%j");
365 string_set(ctx, &pService->print_command, "lp -c -d%p %s; rm %s");
366 string_set(ctx, &pService->queuepause_command, "disable %p");
367 string_set(ctx, &pService->queueresume_command, "enable %p");
368 #ifndef HPUX
369 string_set(ctx, &pService->lppause_command, "lp -i %p-%j -H hold");
370 string_set(ctx, &pService->lpresume_command, "lp -i %p-%j -H resume");
371 #endif /* HPUX */
372 break;
374 case PRINT_QNX:
375 string_set(ctx, &pService->lpq_command, "lpq -P%p");
376 string_set(ctx, &pService->lprm_command, "lprm -P%p %j");
377 string_set(ctx, &pService->print_command, "lp -r -P%p %s");
378 break;
380 #if defined(DEVELOPER) || defined(ENABLE_SELFTEST)
382 case PRINT_TEST:
383 case PRINT_VLP: {
384 const char *tdbfile;
385 TALLOC_CTX *tmp_ctx = talloc_stackframe();
386 char *tmp;
388 tdbfile = talloc_asprintf(
389 tmp_ctx, "tdbfile=%s",
390 lp_parm_const_string(-1, "vlp", "tdbfile",
391 "/tmp/vlp.tdb"));
392 if (tdbfile == NULL) {
393 tdbfile="tdbfile=/tmp/vlp.tdb";
396 tmp = talloc_asprintf(tmp_ctx, "vlp %s print %%p %%s",
397 tdbfile);
398 string_set(ctx, &pService->print_command,
399 tmp ? tmp : "vlp print %p %s");
401 tmp = talloc_asprintf(tmp_ctx, "vlp %s lpq %%p",
402 tdbfile);
403 string_set(ctx, &pService->lpq_command,
404 tmp ? tmp : "vlp lpq %p");
406 tmp = talloc_asprintf(tmp_ctx, "vlp %s lprm %%p %%j",
407 tdbfile);
408 string_set(ctx, &pService->lprm_command,
409 tmp ? tmp : "vlp lprm %p %j");
411 tmp = talloc_asprintf(tmp_ctx, "vlp %s lppause %%p %%j",
412 tdbfile);
413 string_set(ctx, &pService->lppause_command,
414 tmp ? tmp : "vlp lppause %p %j");
416 tmp = talloc_asprintf(tmp_ctx, "vlp %s lpresume %%p %%j",
417 tdbfile);
418 string_set(ctx, &pService->lpresume_command,
419 tmp ? tmp : "vlp lpresume %p %j");
421 tmp = talloc_asprintf(tmp_ctx, "vlp %s queuepause %%p",
422 tdbfile);
423 string_set(ctx, &pService->queuepause_command,
424 tmp ? tmp : "vlp queuepause %p");
426 tmp = talloc_asprintf(tmp_ctx, "vlp %s queueresume %%p",
427 tdbfile);
428 string_set(ctx, &pService->queueresume_command,
429 tmp ? tmp : "vlp queueresume %p");
430 TALLOC_FREE(tmp_ctx);
432 break;
434 #endif /* DEVELOPER */
439 * Function to return the default value for the maximum number of open
440 * file descriptors permitted. This function tries to consult the
441 * kernel-level (sysctl) and ulimit (getrlimit()) values and goes
442 * the smaller of those.
444 static int max_open_files(void)
446 int sysctl_max = MAX_OPEN_FILES;
447 int rlimit_max = MAX_OPEN_FILES;
449 #ifdef HAVE_SYSCTLBYNAME
451 size_t size = sizeof(sysctl_max);
452 sysctlbyname("kern.maxfilesperproc", &sysctl_max, &size, NULL,
455 #endif
457 #if (defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE))
459 struct rlimit rl;
461 ZERO_STRUCT(rl);
463 if (getrlimit(RLIMIT_NOFILE, &rl) == 0)
464 rlimit_max = rl.rlim_cur;
466 #if defined(RLIM_INFINITY)
467 if(rl.rlim_cur == RLIM_INFINITY)
468 rlimit_max = MAX_OPEN_FILES;
469 #endif
471 #endif
473 if (sysctl_max < MIN_OPEN_FILES_WINDOWS) {
474 DEBUG(2,("max_open_files: increasing sysctl_max (%d) to "
475 "minimum Windows limit (%d)\n",
476 sysctl_max,
477 MIN_OPEN_FILES_WINDOWS));
478 sysctl_max = MIN_OPEN_FILES_WINDOWS;
481 if (rlimit_max < MIN_OPEN_FILES_WINDOWS) {
482 DEBUG(2,("rlimit_max: increasing rlimit_max (%d) to "
483 "minimum Windows limit (%d)\n",
484 rlimit_max,
485 MIN_OPEN_FILES_WINDOWS));
486 rlimit_max = MIN_OPEN_FILES_WINDOWS;
489 return MIN(sysctl_max, rlimit_max);
493 * Common part of freeing allocated data for one parameter.
495 static void free_one_parameter_common(void *parm_ptr,
496 struct parm_struct parm)
498 if ((parm.type == P_STRING) ||
499 (parm.type == P_USTRING))
501 string_free((char**)parm_ptr);
502 } else if (parm.type == P_LIST) {
503 TALLOC_FREE(*((char***)parm_ptr));
508 * Free the allocated data for one parameter for a share
509 * given as a service struct.
511 static void free_one_parameter(struct loadparm_service *service,
512 struct parm_struct parm)
514 void *parm_ptr;
516 if (parm.p_class != P_LOCAL) {
517 return;
520 parm_ptr = lp_parm_ptr(service, &parm);
522 free_one_parameter_common(parm_ptr, parm);
526 * Free the allocated parameter data of a share given
527 * as a service struct.
529 static void free_parameters(struct loadparm_service *service)
531 uint32_t i;
533 for (i=0; parm_table[i].label; i++) {
534 free_one_parameter(service, parm_table[i]);
539 * Free the allocated data for one parameter for a given share
540 * specified by an snum.
542 static void free_one_parameter_by_snum(int snum, struct parm_struct parm)
544 void *parm_ptr;
546 if (snum < 0) {
547 parm_ptr = lp_parm_ptr(NULL, &parm);
548 } else if (parm.p_class != P_LOCAL) {
549 return;
550 } else {
551 parm_ptr = lp_local_ptr_by_snum(snum, &parm);
554 free_one_parameter_common(parm_ptr, parm);
558 * Free the allocated parameter data for a share specified
559 * by an snum.
561 static void free_parameters_by_snum(int snum)
563 uint32_t i;
565 for (i=0; parm_table[i].label; i++) {
566 free_one_parameter_by_snum(snum, parm_table[i]);
571 * Free the allocated global parameters.
573 static void free_global_parameters(void)
575 free_param_opts(&Globals.param_opt);
576 free_parameters_by_snum(GLOBAL_SECTION_SNUM);
577 TALLOC_FREE(Globals.ctx);
580 struct lp_stored_option {
581 struct lp_stored_option *prev, *next;
582 const char *label;
583 const char *value;
586 static struct lp_stored_option *stored_options;
589 save options set by lp_set_cmdline() into a list. This list is
590 re-applied when we do a globals reset, so that cmdline set options
591 are sticky across reloads of smb.conf
593 static bool store_lp_set_cmdline(const char *pszParmName, const char *pszParmValue)
595 struct lp_stored_option *entry, *entry_next;
596 for (entry = stored_options; entry != NULL; entry = entry_next) {
597 entry_next = entry->next;
598 if (strcmp(pszParmName, entry->label) == 0) {
599 DLIST_REMOVE(stored_options, entry);
600 talloc_free(entry);
601 break;
605 entry = talloc(NULL, struct lp_stored_option);
606 if (!entry) {
607 return false;
610 entry->label = talloc_strdup(entry, pszParmName);
611 if (!entry->label) {
612 talloc_free(entry);
613 return false;
616 entry->value = talloc_strdup(entry, pszParmValue);
617 if (!entry->value) {
618 talloc_free(entry);
619 return false;
622 DLIST_ADD_END(stored_options, entry, struct lp_stored_option);
624 return true;
627 static bool apply_lp_set_cmdline(void)
629 struct lp_stored_option *entry = NULL;
630 for (entry = stored_options; entry != NULL; entry = entry->next) {
631 if (!lp_set_cmdline_helper(entry->label, entry->value, false)) {
632 DEBUG(0, ("Failed to re-apply cmdline parameter %s = %s\n",
633 entry->label, entry->value));
634 return false;
637 return true;
640 /***************************************************************************
641 Initialise the global parameter structure.
642 ***************************************************************************/
644 static void init_globals(bool reinit_globals)
646 static bool done_init = false;
647 char *s = NULL;
648 int i;
650 /* If requested to initialize only once and we've already done it... */
651 if (!reinit_globals && done_init) {
652 /* ... then we have nothing more to do */
653 return;
656 if (!done_init) {
657 /* The logfile can be set before this is invoked. Free it if so. */
658 if (Globals.logfile != NULL) {
659 string_free(&Globals.logfile);
660 Globals.logfile = NULL;
662 done_init = true;
663 } else {
664 free_global_parameters();
667 /* This memset and the free_global_parameters() above will
668 * wipe out smb.conf options set with lp_set_cmdline(). The
669 * apply_lp_set_cmdline() call puts these values back in the
670 * table once the defaults are set */
671 ZERO_STRUCT(Globals);
673 Globals.ctx = talloc_pooled_object(NULL, char, 272, 2048);
675 for (i = 0; parm_table[i].label; i++) {
676 if ((parm_table[i].type == P_STRING ||
677 parm_table[i].type == P_USTRING))
679 string_set(Globals.ctx, (char **)lp_parm_ptr(NULL, &parm_table[i]), "");
684 string_set(Globals.ctx, &sDefault.fstype, FSTYPE_STRING);
685 string_set(Globals.ctx, &sDefault.printjob_username, "%U");
687 init_printer_values(Globals.ctx, &sDefault);
689 sDefault.ntvfs_handler = (const char **)str_list_make_v3(NULL, "unixuid default", NULL);
691 DEBUG(3, ("Initialising global parameters\n"));
693 /* Must manually force to upper case here, as this does not go via the handler */
694 string_set(Globals.ctx, &Globals.netbios_name, myhostname_upper());
696 string_set(Globals.ctx, &Globals.smb_passwd_file, get_dyn_SMB_PASSWD_FILE());
697 string_set(Globals.ctx, &Globals.private_dir, get_dyn_PRIVATE_DIR());
699 /* use the new 'hash2' method by default, with a prefix of 1 */
700 string_set(Globals.ctx, &Globals.mangling_method, "hash2");
701 Globals.mangle_prefix = 1;
703 string_set(Globals.ctx, &Globals.guest_account, GUEST_ACCOUNT);
705 /* using UTF8 by default allows us to support all chars */
706 string_set(Globals.ctx, &Globals.unix_charset, DEFAULT_UNIX_CHARSET);
708 /* Use codepage 850 as a default for the dos character set */
709 string_set(Globals.ctx, &Globals.dos_charset, DEFAULT_DOS_CHARSET);
712 * Allow the default PASSWD_CHAT to be overridden in local.h.
714 string_set(Globals.ctx, &Globals.passwd_chat, DEFAULT_PASSWD_CHAT);
716 string_set(Globals.ctx, &Globals.workgroup, DEFAULT_WORKGROUP);
718 string_set(Globals.ctx, &Globals.passwd_program, "");
719 string_set(Globals.ctx, &Globals.lock_directory, get_dyn_LOCKDIR());
720 string_set(Globals.ctx, &Globals.state_directory, get_dyn_STATEDIR());
721 string_set(Globals.ctx, &Globals.cache_directory, get_dyn_CACHEDIR());
722 string_set(Globals.ctx, &Globals.pid_directory, get_dyn_PIDDIR());
723 string_set(Globals.ctx, &Globals.nbt_client_socket_address, "0.0.0.0");
725 * By default support explicit binding to broadcast
726 * addresses.
728 Globals.nmbd_bind_explicit_broadcast = true;
730 s = talloc_asprintf(talloc_tos(), "Samba %s", samba_version_string());
731 if (s == NULL) {
732 smb_panic("init_globals: ENOMEM");
734 string_set(Globals.ctx, &Globals.server_string, s);
735 TALLOC_FREE(s);
736 #ifdef DEVELOPER
737 string_set(Globals.ctx, &Globals.panic_action, "/bin/sleep 999999999");
738 #endif
740 string_set(Globals.ctx, &Globals.socket_options, DEFAULT_SOCKET_OPTIONS);
742 string_set(Globals.ctx, &Globals.logon_drive, "");
743 /* %N is the NIS auto.home server if -DAUTOHOME is used, else same as %L */
744 string_set(Globals.ctx, &Globals.logon_home, "\\\\%N\\%U");
745 string_set(Globals.ctx, &Globals.logon_path, "\\\\%N\\%U\\profile");
747 Globals.name_resolve_order = (const char **)str_list_make_v3(NULL, "lmhosts wins host bcast", NULL);
748 string_set(Globals.ctx, &Globals.password_server, "*");
750 Globals.algorithmic_rid_base = BASE_RID;
752 Globals.load_printers = true;
753 Globals.printcap_cache_time = 750; /* 12.5 minutes */
755 Globals.config_backend = config_backend;
756 Globals._server_role = ROLE_AUTO;
758 /* Was 65535 (0xFFFF). 0x4101 matches W2K and causes major speed improvements... */
759 /* Discovered by 2 days of pain by Don McCall @ HP :-). */
760 Globals.max_xmit = 0x4104;
761 Globals.max_mux = 50; /* This is *needed* for profile support. */
762 Globals.lpq_cache_time = 30; /* changed to handle large print servers better -- jerry */
763 Globals._disable_spoolss = false;
764 Globals.max_smbd_processes = 0;/* no limit specified */
765 Globals.username_level = 0;
766 Globals.deadtime = 0;
767 Globals.getwd_cache = true;
768 Globals.large_readwrite = true;
769 Globals.max_log_size = 5000;
770 Globals.max_open_files = max_open_files();
771 Globals.server_max_protocol = PROTOCOL_SMB3_00;
772 Globals.server_min_protocol = PROTOCOL_LANMAN1;
773 Globals.client_max_protocol = PROTOCOL_NT1;
774 Globals.client_min_protocol = PROTOCOL_CORE;
775 Globals._security = SEC_AUTO;
776 Globals.encrypt_passwords = true;
777 Globals.client_schannel = Auto;
778 Globals.winbind_sealed_pipes = true;
779 Globals.require_strong_key = true;
780 Globals.server_schannel = Auto;
781 Globals.read_raw = true;
782 Globals.write_raw = true;
783 Globals.null_passwords = false;
784 Globals.old_password_allowed_period = 60;
785 Globals.obey_pam_restrictions = false;
786 Globals.syslog = 1;
787 Globals.syslog_only = false;
788 Globals.timestamp_logs = true;
789 string_set(Globals.ctx, &Globals.log_level, "0");
790 Globals.debug_prefix_timestamp = false;
791 Globals.debug_hires_timestamp = true;
792 Globals.debug_pid = false;
793 Globals.debug_uid = false;
794 Globals.debug_class = false;
795 Globals.enable_core_files = true;
796 Globals.max_ttl = 60 * 60 * 24 * 3; /* 3 days default. */
797 Globals.max_wins_ttl = 60 * 60 * 24 * 6; /* 6 days default. */
798 Globals.min_wins_ttl = 60 * 60 * 6; /* 6 hours default. */
799 Globals.machine_password_timeout = 60 * 60 * 24 * 7; /* 7 days default. */
800 Globals.lm_announce = Auto; /* = Auto: send only if LM clients found */
801 Globals.lm_interval = 60;
802 #if (defined(HAVE_NETGROUP) && defined(WITH_AUTOMOUNT))
803 Globals.nis_homedir = false;
804 #ifdef WITH_NISPLUS_HOME
805 string_set(Globals.ctx, &Globals.homedir_map, "auto_home.org_dir");
806 #else
807 string_set(Globals.ctx, &Globals.homedir_map, "auto.home");
808 #endif
809 #endif
810 Globals.time_server = false;
811 Globals.bind_interfaces_only = false;
812 Globals.unix_password_sync = false;
813 Globals.pam_password_change = false;
814 Globals.passwd_chat_debug = false;
815 Globals.passwd_chat_timeout = 2; /* 2 second default. */
816 Globals.nt_pipe_support = true; /* Do NT pipes by default. */
817 Globals.nt_status_support = true; /* Use NT status by default. */
818 Globals.stat_cache = true; /* use stat cache by default */
819 Globals.max_stat_cache_size = 256; /* 256k by default */
820 Globals.restrict_anonymous = 0;
821 Globals.client_lanman_auth = false; /* Do NOT use the LanMan hash if it is available */
822 Globals.client_plaintext_auth = false; /* Do NOT use a plaintext password even if is requested by the server */
823 Globals.lanman_auth = false; /* Do NOT use the LanMan hash, even if it is supplied */
824 Globals.ntlm_auth = true; /* Do use NTLMv1 if it is supplied by the client (otherwise NTLMv2) */
825 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 */
826 /* Note, that we will also use NTLM2 session security (which is different), if it is available */
828 Globals.map_to_guest = 0; /* By Default, "Never" */
829 Globals.oplock_break_wait_time = 0; /* By Default, 0 msecs. */
830 Globals.enhanced_browsing = true;
831 Globals.lock_spin_time = WINDOWS_MINIMUM_LOCK_TIMEOUT_MS; /* msec. */
832 #ifdef MMAP_BLACKLIST
833 Globals.use_mmap = false;
834 #else
835 Globals.use_mmap = true;
836 #endif
837 Globals.unicode = true;
838 Globals.unix_extensions = true;
839 Globals.reset_on_zero_vc = false;
840 Globals.log_writeable_files_on_exit = false;
841 Globals.create_krb5_conf = true;
842 Globals.winbindMaxDomainConnections = 1;
844 /* hostname lookups can be very expensive and are broken on
845 a large number of sites (tridge) */
846 Globals.hostname_lookups = false;
848 string_set(Globals.ctx, &Globals.passdb_backend, "tdbsam");
849 string_set(Globals.ctx, &Globals.ldap_suffix, "");
850 string_set(Globals.ctx, &Globals.szLdapMachineSuffix, "");
851 string_set(Globals.ctx, &Globals.szLdapUserSuffix, "");
852 string_set(Globals.ctx, &Globals.szLdapGroupSuffix, "");
853 string_set(Globals.ctx, &Globals.szLdapIdmapSuffix, "");
855 string_set(Globals.ctx, &Globals.ldap_admin_dn, "");
856 Globals.ldap_ssl = LDAP_SSL_START_TLS;
857 Globals.ldap_ssl_ads = false;
858 Globals.ldap_deref = -1;
859 Globals.ldap_passwd_sync = LDAP_PASSWD_SYNC_OFF;
860 Globals.ldap_delete_dn = false;
861 Globals.ldap_replication_sleep = 1000; /* wait 1 sec for replication */
862 Globals.ldap_follow_referral = Auto;
863 Globals.ldap_timeout = LDAP_DEFAULT_TIMEOUT;
864 Globals.ldap_connection_timeout = LDAP_CONNECTION_DEFAULT_TIMEOUT;
865 Globals.ldap_page_size = LDAP_PAGE_SIZE;
867 Globals.ldap_debug_level = 0;
868 Globals.ldap_debug_threshold = 10;
870 /* This is what we tell the afs client. in reality we set the token
871 * to never expire, though, when this runs out the afs client will
872 * forget the token. Set to 0 to get NEVERDATE.*/
873 Globals.afs_token_lifetime = 604800;
874 Globals.cups_connection_timeout = CUPS_DEFAULT_CONNECTION_TIMEOUT;
876 /* these parameters are set to defaults that are more appropriate
877 for the increasing samba install base:
879 as a member of the workgroup, that will possibly become a
880 _local_ master browser (lm = true). this is opposed to a forced
881 local master browser startup (pm = true).
883 doesn't provide WINS server service by default (wsupp = false),
884 and doesn't provide domain master browser services by default, either.
888 Globals.show_add_printer_wizard = true;
889 Globals.os_level = 20;
890 Globals.local_master = true;
891 Globals._domain_master = Auto; /* depending on _domain_logons */
892 Globals._domain_logons = false;
893 Globals.browse_list = true;
894 Globals.we_are_a_wins_server = false;
895 Globals.wins_proxy = false;
897 TALLOC_FREE(Globals.init_logon_delayed_hosts);
898 Globals.init_logon_delay = 100; /* 100 ms default delay */
900 Globals.wins_dns_proxy = true;
902 Globals.allow_trusted_domains = true;
903 string_set(Globals.ctx, &Globals.szIdmapBackend, "tdb");
905 string_set(Globals.ctx, &Globals.template_shell, "/bin/false");
906 string_set(Globals.ctx, &Globals.template_homedir, "/home/%D/%U");
907 string_set(Globals.ctx, &Globals.winbind_separator, "\\");
908 string_set(Globals.ctx, &Globals.winbindd_socket_directory, dyn_WINBINDD_SOCKET_DIR);
910 string_set(Globals.ctx, &Globals.cups_server, "");
911 string_set(Globals.ctx, &Globals.iprint_server, "");
913 string_set(Globals.ctx, &Globals._ctdbd_socket, "");
915 Globals.cluster_addresses = NULL;
916 Globals.clustering = false;
917 Globals.ctdb_timeout = 0;
918 Globals.ctdb_locktime_warn_threshold = 0;
920 Globals.winbind_cache_time = 300; /* 5 minutes */
921 Globals.winbind_reconnect_delay = 30; /* 30 seconds */
922 Globals.winbind_max_clients = 200;
923 Globals.winbind_enum_users = false;
924 Globals.winbind_enum_groups = false;
925 Globals.winbind_use_default_domain = false;
926 Globals.winbind_trusted_domains_only = false;
927 Globals.winbind_nested_groups = true;
928 Globals.winbind_expand_groups = 1;
929 Globals.winbind_nss_info = (const char **)str_list_make_v3(NULL, "template", NULL);
930 Globals.winbind_refresh_tickets = false;
931 Globals.winbind_offline_logon = false;
933 Globals.idmap_cache_time = 86400 * 7; /* a week by default */
934 Globals.idmap_negative_cache_time = 120; /* 2 minutes by default */
936 Globals.passdb_expand_explicit = false;
938 Globals.name_cache_timeout = 660; /* In seconds */
940 Globals.use_spnego = true;
941 Globals.client_use_spnego = true;
943 Globals.client_signing = SMB_SIGNING_DEFAULT;
944 Globals.server_signing = SMB_SIGNING_DEFAULT;
946 Globals.defer_sharing_violations = true;
947 Globals.smb_ports = (const char **)str_list_make_v3(NULL, SMB_PORTS, NULL);
949 Globals.enable_privileges = true;
950 Globals.host_msdfs = true;
951 Globals.enable_asu_support = false;
953 /* User defined shares. */
954 s = talloc_asprintf(talloc_tos(), "%s/usershares", get_dyn_STATEDIR());
955 if (s == NULL) {
956 smb_panic("init_globals: ENOMEM");
958 string_set(Globals.ctx, &Globals.usershare_path, s);
959 TALLOC_FREE(s);
960 string_set(Globals.ctx, &Globals.usershare_template_share, "");
961 Globals.usershare_max_shares = 0;
962 /* By default disallow sharing of directories not owned by the sharer. */
963 Globals.usershare_owner_only = true;
964 /* By default disallow guest access to usershares. */
965 Globals.usershare_allow_guests = false;
967 Globals.keepalive = DEFAULT_KEEPALIVE;
969 /* By default no shares out of the registry */
970 Globals.registry_shares = false;
972 Globals.iminreceivefile = 0;
974 Globals.map_untrusted_to_domain = false;
975 Globals.multicast_dns_register = true;
977 Globals.smb2_max_read = DEFAULT_SMB2_MAX_READ;
978 Globals.smb2_max_write = DEFAULT_SMB2_MAX_WRITE;
979 Globals.smb2_max_trans = DEFAULT_SMB2_MAX_TRANSACT;
980 Globals.ismb2_max_credits = DEFAULT_SMB2_MAX_CREDITS;
982 string_set(Globals.ctx, &Globals.ncalrpc_dir, get_dyn_NCALRPCDIR());
984 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);
986 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);
988 Globals.tls_enabled = true;
990 string_set(Globals.ctx, &Globals._tls_keyfile, "tls/key.pem");
991 string_set(Globals.ctx, &Globals._tls_certfile, "tls/cert.pem");
992 string_set(Globals.ctx, &Globals._tls_cafile, "tls/ca.pem");
994 string_set(Globals.ctx, &Globals.share_backend, "classic");
996 Globals.iPreferredMaster = Auto;
998 Globals.allow_dns_updates = DNS_UPDATE_SIGNED;
1000 string_set(Globals.ctx, &Globals.ntp_signd_socket_directory, get_dyn_NTP_SIGND_SOCKET_DIR());
1002 string_set(Globals.ctx, &Globals.winbindd_privileged_socket_directory, get_dyn_WINBINDD_PRIVILEGED_SOCKET_DIR());
1004 s = talloc_asprintf(talloc_tos(), "%s/samba_kcc", get_dyn_SCRIPTSBINDIR());
1005 if (s == NULL) {
1006 smb_panic("init_globals: ENOMEM");
1008 Globals.samba_kcc_command = (const char **)str_list_make_v3(NULL, s, NULL);
1009 TALLOC_FREE(s);
1011 s = talloc_asprintf(talloc_tos(), "%s/samba_dnsupdate", get_dyn_SCRIPTSBINDIR());
1012 if (s == NULL) {
1013 smb_panic("init_globals: ENOMEM");
1015 Globals.dns_update_command = (const char **)str_list_make_v3(NULL, s, NULL);
1016 TALLOC_FREE(s);
1018 s = talloc_asprintf(talloc_tos(), "%s/samba_spnupdate", get_dyn_SCRIPTSBINDIR());
1019 if (s == NULL) {
1020 smb_panic("init_globals: ENOMEM");
1022 Globals.spn_update_command = (const char **)str_list_make_v3(NULL, s, NULL);
1023 TALLOC_FREE(s);
1025 Globals.nsupdate_command = (const char **)str_list_make_v3(NULL, "/usr/bin/nsupdate -g", NULL);
1027 Globals.rndc_command = (const char **)str_list_make_v3(NULL, "/usr/sbin/rndc", NULL);
1029 Globals.cldap_port = 389;
1031 Globals.dgram_port = 138;
1033 Globals.nbt_port = 137;
1035 Globals.krb5_port = 88;
1037 Globals.kpasswd_port = 464;
1039 Globals.web_port = 901;
1041 /* Now put back the settings that were set with lp_set_cmdline() */
1042 apply_lp_set_cmdline();
1045 /*******************************************************************
1046 Convenience routine to grab string parameters into talloced memory
1047 and run standard_sub_basic on them. The buffers can be written to by
1048 callers without affecting the source string.
1049 ********************************************************************/
1051 char *lp_string(TALLOC_CTX *ctx, const char *s)
1053 char *ret;
1055 /* The follow debug is useful for tracking down memory problems
1056 especially if you have an inner loop that is calling a lp_*()
1057 function that returns a string. Perhaps this debug should be
1058 present all the time? */
1060 #if 0
1061 DEBUG(10, ("lp_string(%s)\n", s));
1062 #endif
1063 if (!s) {
1064 return NULL;
1067 ret = talloc_sub_basic(ctx,
1068 get_current_username(),
1069 current_user_info.domain,
1071 if (trim_char(ret, '\"', '\"')) {
1072 if (strchr(ret,'\"') != NULL) {
1073 TALLOC_FREE(ret);
1074 ret = talloc_sub_basic(ctx,
1075 get_current_username(),
1076 current_user_info.domain,
1080 return ret;
1084 In this section all the functions that are used to access the
1085 parameters from the rest of the program are defined
1088 #define FN_GLOBAL_STRING(fn_name,ptr) \
1089 char *lp_ ## fn_name(TALLOC_CTX *ctx) {return(lp_string((ctx), *(char **)(&Globals.ptr) ? *(char **)(&Globals.ptr) : ""));}
1090 #define FN_GLOBAL_CONST_STRING(fn_name,ptr) \
1091 const char *lp_ ## fn_name(void) {return(*(const char * const *)(&Globals.ptr) ? *(const char * const *)(&Globals.ptr) : "");}
1092 #define FN_GLOBAL_LIST(fn_name,ptr) \
1093 const char **lp_ ## fn_name(void) {return(*(const char ***)(&Globals.ptr));}
1094 #define FN_GLOBAL_BOOL(fn_name,ptr) \
1095 bool lp_ ## fn_name(void) {return(*(bool *)(&Globals.ptr));}
1096 #define FN_GLOBAL_CHAR(fn_name,ptr) \
1097 char lp_ ## fn_name(void) {return(*(char *)(&Globals.ptr));}
1098 #define FN_GLOBAL_INTEGER(fn_name,ptr) \
1099 int lp_ ## fn_name(void) {return(*(int *)(&Globals.ptr));}
1101 #define FN_LOCAL_STRING(fn_name,val) \
1102 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));}
1103 #define FN_LOCAL_CONST_STRING(fn_name,val) \
1104 const char *lp_ ## fn_name(int i) {return (const char *)((LP_SNUM_OK(i) && ServicePtrs[(i)]->val) ? ServicePtrs[(i)]->val : sDefault.val);}
1105 #define FN_LOCAL_LIST(fn_name,val) \
1106 const char **lp_ ## fn_name(int i) {return(const char **)(LP_SNUM_OK(i)? ServicePtrs[(i)]->val : sDefault.val);}
1107 #define FN_LOCAL_BOOL(fn_name,val) \
1108 bool lp_ ## fn_name(int i) {return(bool)(LP_SNUM_OK(i)? ServicePtrs[(i)]->val : sDefault.val);}
1109 #define FN_LOCAL_INTEGER(fn_name,val) \
1110 int lp_ ## fn_name(int i) {return(LP_SNUM_OK(i)? ServicePtrs[(i)]->val : sDefault.val);}
1112 #define FN_LOCAL_PARM_BOOL(fn_name,val) \
1113 bool lp_ ## fn_name(const struct share_params *p) {return(bool)(LP_SNUM_OK(p->service)? ServicePtrs[(p->service)]->val : sDefault.val);}
1114 #define FN_LOCAL_PARM_INTEGER(fn_name,val) \
1115 int lp_ ## fn_name(const struct share_params *p) {return(LP_SNUM_OK(p->service)? ServicePtrs[(p->service)]->val : sDefault.val);}
1116 #define FN_LOCAL_PARM_CHAR(fn_name,val) \
1117 char lp_ ## fn_name(const struct share_params *p) {return(LP_SNUM_OK(p->service)? ServicePtrs[(p->service)]->val : sDefault.val);}
1119 static FN_GLOBAL_INTEGER(winbind_max_domain_connections_int,
1120 winbindMaxDomainConnections)
1122 int lp_winbind_max_domain_connections(void)
1124 if (lp_winbind_offline_logon() &&
1125 lp_winbind_max_domain_connections_int() > 1) {
1126 DEBUG(1, ("offline logons active, restricting max domain "
1127 "connections to 1\n"));
1128 return 1;
1130 return MAX(1, lp_winbind_max_domain_connections_int());
1133 int lp_smb2_max_credits(void)
1135 if (Globals.ismb2_max_credits == 0) {
1136 Globals.ismb2_max_credits = DEFAULT_SMB2_MAX_CREDITS;
1138 return Globals.ismb2_max_credits;
1140 int lp_cups_encrypt(void)
1142 int result = 0;
1143 #ifdef HAVE_HTTPCONNECTENCRYPT
1144 switch (Globals.CupsEncrypt) {
1145 case Auto:
1146 result = HTTP_ENCRYPT_REQUIRED;
1147 break;
1148 case true:
1149 result = HTTP_ENCRYPT_ALWAYS;
1150 break;
1151 case false:
1152 result = HTTP_ENCRYPT_NEVER;
1153 break;
1155 #endif
1156 return result;
1159 /* These functions remain in source3/param for now */
1161 #include "lib/param/param_functions.c"
1163 FN_LOCAL_STRING(servicename, szService)
1164 FN_LOCAL_CONST_STRING(const_servicename, szService)
1166 /* These functions cannot be auto-generated */
1167 FN_LOCAL_BOOL(autoloaded, autoloaded)
1169 /* local prototypes */
1171 static int map_parameter_canonical(const char *pszParmName, bool *inverse);
1172 static const char *get_boolean(bool bool_value);
1173 static bool do_parameter(const char *pszParmName, const char *pszParmValue,
1174 void *userdata);
1175 static bool do_section(const char *pszSectionName, void *userdata);
1176 static bool hash_a_service(const char *name, int number);
1177 static void free_service_byindex(int iService);
1178 static void show_parameter(int parmIndex);
1179 static bool is_synonym_of(int parm1, int parm2, bool *inverse);
1182 * This is a helper function for parametrical options support. It returns a
1183 * pointer to parametrical option value if it exists or NULL otherwise. Actual
1184 * parametrical functions are quite simple
1186 static struct parmlist_entry *get_parametrics_by_service(struct loadparm_service *service, const char *type,
1187 const char *option)
1189 bool global_section = false;
1190 char* param_key;
1191 struct parmlist_entry *data;
1192 TALLOC_CTX *mem_ctx = talloc_stackframe();
1194 if (service == NULL) {
1195 data = Globals.param_opt;
1196 global_section = true;
1197 } else {
1198 data = service->param_opt;
1201 param_key = talloc_asprintf(mem_ctx, "%s:%s", type, option);
1202 if (param_key == NULL) {
1203 DEBUG(0,("asprintf failed!\n"));
1204 TALLOC_FREE(mem_ctx);
1205 return NULL;
1208 while (data) {
1209 if (strwicmp(data->key, param_key) == 0) {
1210 TALLOC_FREE(mem_ctx);
1211 return data;
1213 data = data->next;
1216 if (!global_section) {
1217 /* Try to fetch the same option but from globals */
1218 /* but only if we are not already working with Globals */
1219 data = Globals.param_opt;
1220 while (data) {
1221 if (strwicmp(data->key, param_key) == 0) {
1222 TALLOC_FREE(mem_ctx);
1223 return data;
1225 data = data->next;
1229 TALLOC_FREE(mem_ctx);
1231 return NULL;
1235 * This is a helper function for parametrical options support. It returns a
1236 * pointer to parametrical option value if it exists or NULL otherwise. Actual
1237 * parametrical functions are quite simple
1239 static struct parmlist_entry *get_parametrics(int snum, const char *type,
1240 const char *option)
1242 if (snum >= iNumServices) return NULL;
1244 if (snum < 0) {
1245 return get_parametrics_by_service(NULL, type, option);
1246 } else {
1247 return get_parametrics_by_service(ServicePtrs[snum], type, option);
1252 #define MISSING_PARAMETER(name) \
1253 DEBUG(0, ("%s(): value is NULL or empty!\n", #name))
1255 /*******************************************************************
1256 convenience routine to return int parameters.
1257 ********************************************************************/
1258 static int lp_int(const char *s)
1261 if (!s || !*s) {
1262 MISSING_PARAMETER(lp_int);
1263 return (-1);
1266 return (int)strtol(s, NULL, 0);
1269 /*******************************************************************
1270 convenience routine to return unsigned long parameters.
1271 ********************************************************************/
1272 static unsigned long lp_ulong(const char *s)
1275 if (!s || !*s) {
1276 MISSING_PARAMETER(lp_ulong);
1277 return (0);
1280 return strtoul(s, NULL, 0);
1283 /*******************************************************************
1284 convenience routine to return boolean parameters.
1285 ********************************************************************/
1286 static bool lp_bool(const char *s)
1288 bool ret = false;
1290 if (!s || !*s) {
1291 MISSING_PARAMETER(lp_bool);
1292 return false;
1295 if (!set_boolean(s, &ret)) {
1296 DEBUG(0,("lp_bool(%s): value is not boolean!\n",s));
1297 return false;
1300 return ret;
1303 /*******************************************************************
1304 convenience routine to return enum parameters.
1305 ********************************************************************/
1306 static int lp_enum(const char *s,const struct enum_list *_enum)
1308 int i;
1310 if (!s || !*s || !_enum) {
1311 MISSING_PARAMETER(lp_enum);
1312 return (-1);
1315 for (i=0; _enum[i].name; i++) {
1316 if (strequal(_enum[i].name,s))
1317 return _enum[i].value;
1320 DEBUG(0,("lp_enum(%s,enum): value is not in enum_list!\n",s));
1321 return (-1);
1324 #undef MISSING_PARAMETER
1326 /* Return parametric option from a given service. Type is a part of option before ':' */
1327 /* Parametric option has following syntax: 'Type: option = value' */
1328 char *lp_parm_talloc_string(TALLOC_CTX *ctx, int snum, const char *type, const char *option, const char *def)
1330 struct parmlist_entry *data = get_parametrics(snum, type, option);
1332 if (data == NULL||data->value==NULL) {
1333 if (def) {
1334 return lp_string(ctx, def);
1335 } else {
1336 return NULL;
1340 return lp_string(ctx, data->value);
1343 /* Return parametric option from a given service. Type is a part of option before ':' */
1344 /* Parametric option has following syntax: 'Type: option = value' */
1345 const char *lp_parm_const_string(int snum, const char *type, const char *option, const char *def)
1347 struct parmlist_entry *data = get_parametrics(snum, type, option);
1349 if (data == NULL||data->value==NULL)
1350 return def;
1352 return data->value;
1355 const char *lp_parm_const_string_service(struct loadparm_service *service, const char *type, const char *option)
1357 struct parmlist_entry *data = get_parametrics_by_service(service, type, option);
1359 if (data == NULL||data->value==NULL)
1360 return NULL;
1362 return data->value;
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 const char **lp_parm_string_list(int snum, const char *type, const char *option, const char **def)
1371 struct parmlist_entry *data = get_parametrics(snum, type, option);
1373 if (data == NULL||data->value==NULL)
1374 return (const char **)def;
1376 if (data->list==NULL) {
1377 data->list = str_list_make_v3(NULL, data->value, NULL);
1380 return (const char **)data->list;
1383 /* Return parametric option from a given service. Type is a part of option before ':' */
1384 /* Parametric option has following syntax: 'Type: option = value' */
1386 int lp_parm_int(int snum, const char *type, const char *option, int def)
1388 struct parmlist_entry *data = get_parametrics(snum, type, option);
1390 if (data && data->value && *data->value)
1391 return lp_int(data->value);
1393 return def;
1396 /* Return parametric option from a given service. Type is a part of option before ':' */
1397 /* Parametric option has following syntax: 'Type: option = value' */
1399 unsigned long lp_parm_ulong(int snum, const char *type, const char *option, unsigned long def)
1401 struct parmlist_entry *data = get_parametrics(snum, type, option);
1403 if (data && data->value && *data->value)
1404 return lp_ulong(data->value);
1406 return def;
1409 /* Return parametric option from a given service. Type is a part of option before ':' */
1410 /* Parametric option has following syntax: 'Type: option = value' */
1412 bool lp_parm_bool(int snum, const char *type, const char *option, bool def)
1414 struct parmlist_entry *data = get_parametrics(snum, type, option);
1416 if (data && data->value && *data->value)
1417 return lp_bool(data->value);
1419 return def;
1422 /* Return parametric option from a given service. Type is a part of option before ':' */
1423 /* Parametric option has following syntax: 'Type: option = value' */
1425 int lp_parm_enum(int snum, const char *type, const char *option,
1426 const struct enum_list *_enum, int def)
1428 struct parmlist_entry *data = get_parametrics(snum, type, option);
1430 if (data && data->value && *data->value && _enum)
1431 return lp_enum(data->value, _enum);
1433 return def;
1437 * free a param_opts structure.
1438 * param_opts handling should be moved to talloc;
1439 * then this whole functions reduces to a TALLOC_FREE().
1442 static void free_param_opts(struct parmlist_entry **popts)
1444 struct parmlist_entry *opt, *next_opt;
1446 if (*popts != NULL) {
1447 DEBUG(5, ("Freeing parametrics:\n"));
1449 opt = *popts;
1450 while (opt != NULL) {
1451 string_free(&opt->key);
1452 string_free(&opt->value);
1453 TALLOC_FREE(opt->list);
1454 next_opt = opt->next;
1455 TALLOC_FREE(opt);
1456 opt = next_opt;
1458 *popts = NULL;
1461 /***************************************************************************
1462 Free the dynamically allocated parts of a service struct.
1463 ***************************************************************************/
1465 static void free_service(struct loadparm_service *pservice)
1467 if (!pservice)
1468 return;
1470 if (pservice->szService)
1471 DEBUG(5, ("free_service: Freeing service %s\n",
1472 pservice->szService));
1474 free_parameters(pservice);
1476 string_free(&pservice->szService);
1477 TALLOC_FREE(pservice->copymap);
1479 free_param_opts(&pservice->param_opt);
1481 ZERO_STRUCTP(pservice);
1485 /***************************************************************************
1486 remove a service indexed in the ServicePtrs array from the ServiceHash
1487 and free the dynamically allocated parts
1488 ***************************************************************************/
1490 static void free_service_byindex(int idx)
1492 if ( !LP_SNUM_OK(idx) )
1493 return;
1495 ServicePtrs[idx]->valid = false;
1497 /* we have to cleanup the hash record */
1499 if (ServicePtrs[idx]->szService) {
1500 char *canon_name = canonicalize_servicename(
1501 talloc_tos(),
1502 ServicePtrs[idx]->szService );
1504 dbwrap_delete_bystring(ServiceHash, canon_name );
1505 TALLOC_FREE(canon_name);
1508 free_service(ServicePtrs[idx]);
1509 talloc_free_children(ServicePtrs[idx]);
1512 /***************************************************************************
1513 Add a new service to the services array initialising it with the given
1514 service.
1515 ***************************************************************************/
1517 static int add_a_service(const struct loadparm_service *pservice, const char *name)
1519 int i;
1520 int num_to_alloc = iNumServices + 1;
1521 struct loadparm_service **tsp = NULL;
1523 /* it might already exist */
1524 if (name) {
1525 i = getservicebyname(name, NULL);
1526 if (i >= 0) {
1527 return (i);
1531 /* if not, then create one */
1532 i = iNumServices;
1533 tsp = talloc_realloc(NULL, ServicePtrs, struct loadparm_service *, num_to_alloc);
1534 if (tsp == NULL) {
1535 DEBUG(0,("add_a_service: failed to enlarge ServicePtrs!\n"));
1536 return (-1);
1538 ServicePtrs = tsp;
1539 ServicePtrs[iNumServices] = talloc_zero(NULL, struct loadparm_service);
1540 if (!ServicePtrs[iNumServices]) {
1541 DEBUG(0,("add_a_service: out of memory!\n"));
1542 return (-1);
1544 iNumServices++;
1546 ServicePtrs[i]->valid = true;
1548 copy_service(ServicePtrs[i], pservice, NULL);
1549 if (name)
1550 string_set(ServicePtrs[i], &ServicePtrs[i]->szService, name);
1552 DEBUG(8,("add_a_service: Creating snum = %d for %s\n",
1553 i, ServicePtrs[i]->szService));
1555 if (!hash_a_service(ServicePtrs[i]->szService, i)) {
1556 return (-1);
1559 return (i);
1562 /***************************************************************************
1563 Convert a string to uppercase and remove whitespaces.
1564 ***************************************************************************/
1566 char *canonicalize_servicename(TALLOC_CTX *ctx, const char *src)
1568 char *result;
1570 if ( !src ) {
1571 DEBUG(0,("canonicalize_servicename: NULL source name!\n"));
1572 return NULL;
1575 result = talloc_strdup(ctx, src);
1576 SMB_ASSERT(result != NULL);
1578 if (!strlower_m(result)) {
1579 TALLOC_FREE(result);
1580 return NULL;
1582 return result;
1585 /***************************************************************************
1586 Add a name/index pair for the services array to the hash table.
1587 ***************************************************************************/
1589 static bool hash_a_service(const char *name, int idx)
1591 char *canon_name;
1593 if ( !ServiceHash ) {
1594 DEBUG(10,("hash_a_service: creating servicehash\n"));
1595 ServiceHash = db_open_rbt(NULL);
1596 if ( !ServiceHash ) {
1597 DEBUG(0,("hash_a_service: open tdb servicehash failed!\n"));
1598 return false;
1602 DEBUG(10,("hash_a_service: hashing index %d for service name %s\n",
1603 idx, name));
1605 canon_name = canonicalize_servicename(talloc_tos(), name );
1607 dbwrap_store_bystring(ServiceHash, canon_name,
1608 make_tdb_data((uint8 *)&idx, sizeof(idx)),
1609 TDB_REPLACE);
1611 TALLOC_FREE(canon_name);
1613 return true;
1616 /***************************************************************************
1617 Add a new home service, with the specified home directory, defaults coming
1618 from service ifrom.
1619 ***************************************************************************/
1621 bool lp_add_home(const char *pszHomename, int iDefaultService,
1622 const char *user, const char *pszHomedir)
1624 int i;
1626 if (pszHomename == NULL || user == NULL || pszHomedir == NULL ||
1627 pszHomedir[0] == '\0') {
1628 return false;
1631 i = add_a_service(ServicePtrs[iDefaultService], pszHomename);
1633 if (i < 0)
1634 return false;
1636 if (!(*(ServicePtrs[iDefaultService]->path))
1637 || strequal(ServicePtrs[iDefaultService]->path,
1638 lp_path(talloc_tos(), GLOBAL_SECTION_SNUM))) {
1639 string_set(ServicePtrs[i], &ServicePtrs[i]->path, pszHomedir);
1642 if (!(*(ServicePtrs[i]->comment))) {
1643 char *comment = talloc_asprintf(talloc_tos(), "Home directory of %s", user);
1644 if (comment == NULL) {
1645 return false;
1647 string_set(ServicePtrs[i], &ServicePtrs[i]->comment, comment);
1648 TALLOC_FREE(comment);
1651 /* set the browseable flag from the global default */
1653 ServicePtrs[i]->browseable = sDefault.browseable;
1654 ServicePtrs[i]->access_based_share_enum = sDefault.access_based_share_enum;
1656 ServicePtrs[i]->autoloaded = true;
1658 DEBUG(3, ("adding home's share [%s] for user '%s' at '%s'\n", pszHomename,
1659 user, ServicePtrs[i]->path ));
1661 return true;
1664 /***************************************************************************
1665 Add a new service, based on an old one.
1666 ***************************************************************************/
1668 int lp_add_service(const char *pszService, int iDefaultService)
1670 if (iDefaultService < 0) {
1671 return add_a_service(&sDefault, pszService);
1674 return (add_a_service(ServicePtrs[iDefaultService], pszService));
1677 /***************************************************************************
1678 Add the IPC service.
1679 ***************************************************************************/
1681 static bool lp_add_ipc(const char *ipc_name, bool guest_ok)
1683 char *comment = NULL;
1684 int i = add_a_service(&sDefault, ipc_name);
1686 if (i < 0)
1687 return false;
1689 comment = talloc_asprintf(talloc_tos(), "IPC Service (%s)",
1690 Globals.server_string);
1691 if (comment == NULL) {
1692 return false;
1695 string_set(ServicePtrs[i], &ServicePtrs[i]->path, tmpdir());
1696 string_set(ServicePtrs[i], &ServicePtrs[i]->username, "");
1697 string_set(ServicePtrs[i], &ServicePtrs[i]->comment, comment);
1698 string_set(ServicePtrs[i], &ServicePtrs[i]->fstype, "IPC");
1699 ServicePtrs[i]->max_connections = 0;
1700 ServicePtrs[i]->bAvailable = true;
1701 ServicePtrs[i]->read_only = true;
1702 ServicePtrs[i]->guest_only = false;
1703 ServicePtrs[i]->administrative_share = true;
1704 ServicePtrs[i]->guest_ok = guest_ok;
1705 ServicePtrs[i]->printable = false;
1706 ServicePtrs[i]->browseable = sDefault.browseable;
1708 DEBUG(3, ("adding IPC service\n"));
1710 TALLOC_FREE(comment);
1711 return true;
1714 /***************************************************************************
1715 Add a new printer service, with defaults coming from service iFrom.
1716 ***************************************************************************/
1718 bool lp_add_printer(const char *pszPrintername, int iDefaultService)
1720 const char *comment = "From Printcap";
1721 int i = add_a_service(ServicePtrs[iDefaultService], pszPrintername);
1723 if (i < 0)
1724 return false;
1726 /* note that we do NOT default the availability flag to true - */
1727 /* we take it from the default service passed. This allows all */
1728 /* dynamic printers to be disabled by disabling the [printers] */
1729 /* entry (if/when the 'available' keyword is implemented!). */
1731 /* the printer name is set to the service name. */
1732 string_set(ServicePtrs[i], &ServicePtrs[i]->_printername, pszPrintername);
1733 string_set(ServicePtrs[i], &ServicePtrs[i]->comment, comment);
1735 /* set the browseable flag from the gloabl default */
1736 ServicePtrs[i]->browseable = sDefault.browseable;
1738 /* Printers cannot be read_only. */
1739 ServicePtrs[i]->read_only = false;
1740 /* No oplocks on printer services. */
1741 ServicePtrs[i]->oplocks = false;
1742 /* Printer services must be printable. */
1743 ServicePtrs[i]->printable = true;
1745 DEBUG(3, ("adding printer service %s\n", pszPrintername));
1747 return true;
1751 /***************************************************************************
1752 Check whether the given parameter name is valid.
1753 Parametric options (names containing a colon) are considered valid.
1754 ***************************************************************************/
1756 bool lp_parameter_is_valid(const char *pszParmName)
1758 return ((lpcfg_map_parameter(pszParmName) != -1) ||
1759 (strchr(pszParmName, ':') != NULL));
1762 /***************************************************************************
1763 Check whether the given name is the name of a global parameter.
1764 Returns true for strings belonging to parameters of class
1765 P_GLOBAL, false for all other strings, also for parametric options
1766 and strings not belonging to any option.
1767 ***************************************************************************/
1769 bool lp_parameter_is_global(const char *pszParmName)
1771 int num = lpcfg_map_parameter(pszParmName);
1773 if (num >= 0) {
1774 return (parm_table[num].p_class == P_GLOBAL);
1777 return false;
1780 /**************************************************************************
1781 Check whether the given name is the canonical name of a parameter.
1782 Returns false if it is not a valid parameter Name.
1783 For parametric options, true is returned.
1784 **************************************************************************/
1786 bool lp_parameter_is_canonical(const char *parm_name)
1788 if (!lp_parameter_is_valid(parm_name)) {
1789 return false;
1792 return (lpcfg_map_parameter(parm_name) ==
1793 map_parameter_canonical(parm_name, NULL));
1796 /**************************************************************************
1797 Determine the canonical name for a parameter.
1798 Indicate when it is an inverse (boolean) synonym instead of a
1799 "usual" synonym.
1800 **************************************************************************/
1802 bool lp_canonicalize_parameter(const char *parm_name, const char **canon_parm,
1803 bool *inverse)
1805 int num;
1807 if (!lp_parameter_is_valid(parm_name)) {
1808 *canon_parm = NULL;
1809 return false;
1812 num = map_parameter_canonical(parm_name, inverse);
1813 if (num < 0) {
1814 /* parametric option */
1815 *canon_parm = parm_name;
1816 } else {
1817 *canon_parm = parm_table[num].label;
1820 return true;
1824 /**************************************************************************
1825 Determine the canonical name for a parameter.
1826 Turn the value given into the inverse boolean expression when
1827 the synonym is an invers boolean synonym.
1829 Return true if parm_name is a valid parameter name and
1830 in case it is an invers boolean synonym, if the val string could
1831 successfully be converted to the reverse bool.
1832 Return false in all other cases.
1833 **************************************************************************/
1835 bool lp_canonicalize_parameter_with_value(const char *parm_name,
1836 const char *val,
1837 const char **canon_parm,
1838 const char **canon_val)
1840 int num;
1841 bool inverse;
1843 if (!lp_parameter_is_valid(parm_name)) {
1844 *canon_parm = NULL;
1845 *canon_val = NULL;
1846 return false;
1849 num = map_parameter_canonical(parm_name, &inverse);
1850 if (num < 0) {
1851 /* parametric option */
1852 *canon_parm = parm_name;
1853 *canon_val = val;
1854 } else {
1855 *canon_parm = parm_table[num].label;
1856 if (inverse) {
1857 if (!lp_invert_boolean(val, canon_val)) {
1858 *canon_val = NULL;
1859 return false;
1861 } else {
1862 *canon_val = val;
1866 return true;
1869 /***************************************************************************
1870 Map a parameter's string representation to the index of the canonical
1871 form of the parameter (it might be a synonym).
1872 Returns -1 if the parameter string is not recognised.
1873 ***************************************************************************/
1875 static int map_parameter_canonical(const char *pszParmName, bool *inverse)
1877 int parm_num, canon_num;
1878 bool loc_inverse = false;
1880 parm_num = lpcfg_map_parameter(pszParmName);
1881 if ((parm_num < 0) || !(parm_table[parm_num].flags & FLAG_HIDE)) {
1882 /* invalid, parametric or no canidate for synonyms ... */
1883 goto done;
1886 for (canon_num = 0; parm_table[canon_num].label; canon_num++) {
1887 if (is_synonym_of(parm_num, canon_num, &loc_inverse)) {
1888 parm_num = canon_num;
1889 goto done;
1893 done:
1894 if (inverse != NULL) {
1895 *inverse = loc_inverse;
1897 return parm_num;
1900 /***************************************************************************
1901 return true if parameter number parm1 is a synonym of parameter
1902 number parm2 (parm2 being the principal name).
1903 set inverse to true if parm1 is P_BOOLREV and parm2 is P_BOOL,
1904 false otherwise.
1905 ***************************************************************************/
1907 static bool is_synonym_of(int parm1, int parm2, bool *inverse)
1909 if ((parm_table[parm1].offset == parm_table[parm2].offset) &&
1910 (parm_table[parm1].p_class == parm_table[parm2].p_class) &&
1911 (parm_table[parm1].flags & FLAG_HIDE) &&
1912 !(parm_table[parm2].flags & FLAG_HIDE))
1914 if (inverse != NULL) {
1915 if ((parm_table[parm1].type == P_BOOLREV) &&
1916 (parm_table[parm2].type == P_BOOL))
1918 *inverse = true;
1919 } else {
1920 *inverse = false;
1923 return true;
1925 return false;
1928 /***************************************************************************
1929 Show one parameter's name, type, [values,] and flags.
1930 (helper functions for show_parameter_list)
1931 ***************************************************************************/
1933 static void show_parameter(int parmIndex)
1935 int enumIndex, flagIndex;
1936 int parmIndex2;
1937 bool hadFlag;
1938 bool hadSyn;
1939 bool inverse;
1940 const char *type[] = { "P_BOOL", "P_BOOLREV", "P_CHAR", "P_INTEGER",
1941 "P_OCTAL", "P_LIST", "P_STRING", "P_USTRING",
1942 "P_ENUM", "P_SEP"};
1943 unsigned flags[] = { FLAG_BASIC, FLAG_SHARE, FLAG_PRINT, FLAG_GLOBAL,
1944 FLAG_WIZARD, FLAG_ADVANCED, FLAG_DEVELOPER, FLAG_DEPRECATED,
1945 FLAG_HIDE};
1946 const char *flag_names[] = { "FLAG_BASIC", "FLAG_SHARE", "FLAG_PRINT",
1947 "FLAG_GLOBAL", "FLAG_WIZARD", "FLAG_ADVANCED", "FLAG_DEVELOPER",
1948 "FLAG_DEPRECATED", "FLAG_HIDE", NULL};
1950 printf("%s=%s", parm_table[parmIndex].label,
1951 type[parm_table[parmIndex].type]);
1952 if (parm_table[parmIndex].type == P_ENUM) {
1953 printf(",");
1954 for (enumIndex=0;
1955 parm_table[parmIndex].enum_list[enumIndex].name;
1956 enumIndex++)
1958 printf("%s%s",
1959 enumIndex ? "|" : "",
1960 parm_table[parmIndex].enum_list[enumIndex].name);
1963 printf(",");
1964 hadFlag = false;
1965 for (flagIndex=0; flag_names[flagIndex]; flagIndex++) {
1966 if (parm_table[parmIndex].flags & flags[flagIndex]) {
1967 printf("%s%s",
1968 hadFlag ? "|" : "",
1969 flag_names[flagIndex]);
1970 hadFlag = true;
1974 /* output synonyms */
1975 hadSyn = false;
1976 for (parmIndex2=0; parm_table[parmIndex2].label; parmIndex2++) {
1977 if (is_synonym_of(parmIndex, parmIndex2, &inverse)) {
1978 printf(" (%ssynonym of %s)", inverse ? "inverse " : "",
1979 parm_table[parmIndex2].label);
1980 } else if (is_synonym_of(parmIndex2, parmIndex, &inverse)) {
1981 if (!hadSyn) {
1982 printf(" (synonyms: ");
1983 hadSyn = true;
1984 } else {
1985 printf(", ");
1987 printf("%s%s", parm_table[parmIndex2].label,
1988 inverse ? "[i]" : "");
1991 if (hadSyn) {
1992 printf(")");
1995 printf("\n");
1998 /***************************************************************************
1999 Show all parameter's name, type, [values,] and flags.
2000 ***************************************************************************/
2002 void show_parameter_list(void)
2004 int classIndex, parmIndex;
2005 const char *section_names[] = { "local", "global", NULL};
2007 for (classIndex=0; section_names[classIndex]; classIndex++) {
2008 printf("[%s]\n", section_names[classIndex]);
2009 for (parmIndex = 0; parm_table[parmIndex].label; parmIndex++) {
2010 if (parm_table[parmIndex].p_class == classIndex) {
2011 show_parameter(parmIndex);
2017 /***************************************************************************
2018 Get the standard string representation of a boolean value ("yes" or "no")
2019 ***************************************************************************/
2021 static const char *get_boolean(bool bool_value)
2023 static const char *yes_str = "yes";
2024 static const char *no_str = "no";
2026 return (bool_value ? yes_str : no_str);
2029 /***************************************************************************
2030 Provide the string of the negated boolean value associated to the boolean
2031 given as a string. Returns false if the passed string does not correctly
2032 represent a boolean.
2033 ***************************************************************************/
2035 bool lp_invert_boolean(const char *str, const char **inverse_str)
2037 bool val;
2039 if (!set_boolean(str, &val)) {
2040 return false;
2043 *inverse_str = get_boolean(!val);
2044 return true;
2047 /***************************************************************************
2048 Provide the canonical string representation of a boolean value given
2049 as a string. Return true on success, false if the string given does
2050 not correctly represent a boolean.
2051 ***************************************************************************/
2053 bool lp_canonicalize_boolean(const char *str, const char**canon_str)
2055 bool val;
2057 if (!set_boolean(str, &val)) {
2058 return false;
2061 *canon_str = get_boolean(val);
2062 return true;
2065 /***************************************************************************
2066 Find a service by name. Otherwise works like get_service.
2067 ***************************************************************************/
2069 int getservicebyname(const char *pszServiceName, struct loadparm_service *pserviceDest)
2071 int iService = -1;
2072 char *canon_name;
2073 TDB_DATA data;
2074 NTSTATUS status;
2076 if (ServiceHash == NULL) {
2077 return -1;
2080 canon_name = canonicalize_servicename(talloc_tos(), pszServiceName);
2082 status = dbwrap_fetch_bystring(ServiceHash, canon_name, canon_name,
2083 &data);
2085 if (NT_STATUS_IS_OK(status) &&
2086 (data.dptr != NULL) &&
2087 (data.dsize == sizeof(iService)))
2089 iService = *(int *)data.dptr;
2092 TALLOC_FREE(canon_name);
2094 if ((iService != -1) && (LP_SNUM_OK(iService))
2095 && (pserviceDest != NULL)) {
2096 copy_service(pserviceDest, ServicePtrs[iService], NULL);
2099 return (iService);
2102 /* Return a pointer to a service by name. Unlike getservicebyname, it does not copy the service */
2103 struct loadparm_service *lp_service(const char *pszServiceName)
2105 int iService = getservicebyname(pszServiceName, NULL);
2106 if (iService == -1 || !LP_SNUM_OK(iService)) {
2107 return NULL;
2109 return ServicePtrs[iService];
2112 struct loadparm_service *lp_servicebynum(int snum)
2114 if ((snum == -1) || !LP_SNUM_OK(snum)) {
2115 return NULL;
2117 return ServicePtrs[snum];
2120 struct loadparm_service *lp_default_loadparm_service()
2122 return &sDefault;
2125 /***************************************************************************
2126 Check a service for consistency. Return false if the service is in any way
2127 incomplete or faulty, else true.
2128 ***************************************************************************/
2130 bool service_ok(int iService)
2132 bool bRetval;
2134 bRetval = true;
2135 if (ServicePtrs[iService]->szService[0] == '\0') {
2136 DEBUG(0, ("The following message indicates an internal error:\n"));
2137 DEBUG(0, ("No service name in service entry.\n"));
2138 bRetval = false;
2141 /* The [printers] entry MUST be printable. I'm all for flexibility, but */
2142 /* I can't see why you'd want a non-printable printer service... */
2143 if (strwicmp(ServicePtrs[iService]->szService, PRINTERS_NAME) == 0) {
2144 if (!ServicePtrs[iService]->printable) {
2145 DEBUG(0, ("WARNING: [%s] service MUST be printable!\n",
2146 ServicePtrs[iService]->szService));
2147 ServicePtrs[iService]->printable = true;
2149 /* [printers] service must also be non-browsable. */
2150 if (ServicePtrs[iService]->browseable)
2151 ServicePtrs[iService]->browseable = false;
2154 if (ServicePtrs[iService]->path[0] == '\0' &&
2155 strwicmp(ServicePtrs[iService]->szService, HOMES_NAME) != 0 &&
2156 ServicePtrs[iService]->msdfs_proxy[0] == '\0'
2158 DEBUG(0, ("WARNING: No path in service %s - making it unavailable!\n",
2159 ServicePtrs[iService]->szService));
2160 ServicePtrs[iService]->bAvailable = false;
2163 /* If a service is flagged unavailable, log the fact at level 1. */
2164 if (!ServicePtrs[iService]->bAvailable)
2165 DEBUG(1, ("NOTE: Service %s is flagged unavailable.\n",
2166 ServicePtrs[iService]->szService));
2168 return (bRetval);
2171 static struct smbconf_ctx *lp_smbconf_ctx(void)
2173 sbcErr err;
2174 static struct smbconf_ctx *conf_ctx = NULL;
2176 if (conf_ctx == NULL) {
2177 err = smbconf_init(NULL, &conf_ctx, "registry:");
2178 if (!SBC_ERROR_IS_OK(err)) {
2179 DEBUG(1, ("error initializing registry configuration: "
2180 "%s\n", sbcErrorString(err)));
2181 conf_ctx = NULL;
2185 return conf_ctx;
2188 static bool process_smbconf_service(struct smbconf_service *service)
2190 uint32_t count;
2191 bool ret;
2193 if (service == NULL) {
2194 return false;
2197 ret = do_section(service->name, NULL);
2198 if (ret != true) {
2199 return false;
2201 for (count = 0; count < service->num_params; count++) {
2202 ret = do_parameter(service->param_names[count],
2203 service->param_values[count],
2204 NULL);
2205 if (ret != true) {
2206 return false;
2209 if (iServiceIndex >= 0) {
2210 return service_ok(iServiceIndex);
2212 return true;
2216 * load a service from registry and activate it
2218 bool process_registry_service(const char *service_name)
2220 sbcErr err;
2221 struct smbconf_service *service = NULL;
2222 TALLOC_CTX *mem_ctx = talloc_stackframe();
2223 struct smbconf_ctx *conf_ctx = lp_smbconf_ctx();
2224 bool ret = false;
2226 if (conf_ctx == NULL) {
2227 goto done;
2230 DEBUG(5, ("process_registry_service: service name %s\n", service_name));
2232 if (!smbconf_share_exists(conf_ctx, service_name)) {
2234 * Registry does not contain data for this service (yet),
2235 * but make sure lp_load doesn't return false.
2237 ret = true;
2238 goto done;
2241 err = smbconf_get_share(conf_ctx, mem_ctx, service_name, &service);
2242 if (!SBC_ERROR_IS_OK(err)) {
2243 goto done;
2246 ret = process_smbconf_service(service);
2247 if (!ret) {
2248 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 * process_registry_globals
2262 static bool process_registry_globals(void)
2264 bool ret;
2266 add_to_file_list(NULL, &file_lists, INCLUDE_REGISTRY_NAME, INCLUDE_REGISTRY_NAME);
2268 ret = do_parameter("registry shares", "yes", NULL);
2269 if (!ret) {
2270 return ret;
2273 return process_registry_service(GLOBAL_NAME);
2276 bool process_registry_shares(void)
2278 sbcErr err;
2279 uint32_t count;
2280 struct smbconf_service **service = NULL;
2281 uint32_t num_shares = 0;
2282 TALLOC_CTX *mem_ctx = talloc_stackframe();
2283 struct smbconf_ctx *conf_ctx = lp_smbconf_ctx();
2284 bool ret = false;
2286 if (conf_ctx == NULL) {
2287 goto done;
2290 err = smbconf_get_config(conf_ctx, mem_ctx, &num_shares, &service);
2291 if (!SBC_ERROR_IS_OK(err)) {
2292 goto done;
2295 ret = true;
2297 for (count = 0; count < num_shares; count++) {
2298 if (strequal(service[count]->name, GLOBAL_NAME)) {
2299 continue;
2301 ret = process_smbconf_service(service[count]);
2302 if (!ret) {
2303 goto done;
2307 /* store the csn */
2308 smbconf_changed(conf_ctx, &conf_last_csn, NULL, NULL);
2310 done:
2311 TALLOC_FREE(mem_ctx);
2312 return ret;
2316 * reload those shares from registry that are already
2317 * activated in the services array.
2319 static bool reload_registry_shares(void)
2321 int i;
2322 bool ret = true;
2324 for (i = 0; i < iNumServices; i++) {
2325 if (!VALID(i)) {
2326 continue;
2329 if (ServicePtrs[i]->usershare == USERSHARE_VALID) {
2330 continue;
2333 ret = process_registry_service(ServicePtrs[i]->szService);
2334 if (!ret) {
2335 goto done;
2339 done:
2340 return ret;
2344 #define MAX_INCLUDE_DEPTH 100
2346 static uint8_t include_depth;
2349 * Free the file lists
2351 static void free_file_list(void)
2353 struct file_lists *f;
2354 struct file_lists *next;
2356 f = file_lists;
2357 while( f ) {
2358 next = f->next;
2359 TALLOC_FREE( f );
2360 f = next;
2362 file_lists = NULL;
2367 * Utility function for outsiders to check if we're running on registry.
2369 bool lp_config_backend_is_registry(void)
2371 return (lp_config_backend() == CONFIG_BACKEND_REGISTRY);
2375 * Utility function to check if the config backend is FILE.
2377 bool lp_config_backend_is_file(void)
2379 return (lp_config_backend() == CONFIG_BACKEND_FILE);
2382 /*******************************************************************
2383 Check if a config file has changed date.
2384 ********************************************************************/
2386 bool lp_file_list_changed(void)
2388 struct file_lists *f = file_lists;
2390 DEBUG(6, ("lp_file_list_changed()\n"));
2392 while (f) {
2393 time_t mod_time;
2395 if (strequal(f->name, INCLUDE_REGISTRY_NAME)) {
2396 struct smbconf_ctx *conf_ctx = lp_smbconf_ctx();
2398 if (conf_ctx == NULL) {
2399 return false;
2401 if (smbconf_changed(conf_ctx, &conf_last_csn, NULL,
2402 NULL))
2404 DEBUGADD(6, ("registry config changed\n"));
2405 return true;
2407 } else {
2408 char *n2 = NULL;
2409 n2 = talloc_sub_basic(talloc_tos(),
2410 get_current_username(),
2411 current_user_info.domain,
2412 f->name);
2413 if (!n2) {
2414 return false;
2416 DEBUGADD(6, ("file %s -> %s last mod_time: %s\n",
2417 f->name, n2, ctime(&f->modtime)));
2419 mod_time = file_modtime(n2);
2421 if (mod_time &&
2422 ((f->modtime != mod_time) ||
2423 (f->subfname == NULL) ||
2424 (strcmp(n2, f->subfname) != 0)))
2426 DEBUGADD(6,
2427 ("file %s modified: %s\n", n2,
2428 ctime(&mod_time)));
2429 f->modtime = mod_time;
2430 TALLOC_FREE(f->subfname);
2431 f->subfname = talloc_strdup(f, n2);
2432 if (f->subfname == NULL) {
2433 smb_panic("talloc_strdup failed");
2435 TALLOC_FREE(n2);
2436 return true;
2438 TALLOC_FREE(n2);
2440 f = f->next;
2442 return false;
2447 * Initialize iconv conversion descriptors.
2449 * This is called the first time it is needed, and also called again
2450 * every time the configuration is reloaded, because the charset or
2451 * codepage might have changed.
2453 static void init_iconv(void)
2455 global_iconv_handle = smb_iconv_handle_reinit(NULL, lp_dos_charset(),
2456 lp_unix_charset(),
2457 true, global_iconv_handle);
2460 static bool handle_netbios_aliases(struct loadparm_context *unused, int snum, const char *pszParmValue, char **ptr)
2462 TALLOC_FREE(Globals.netbios_aliases);
2463 Globals.netbios_aliases = (const char **)str_list_make_v3(NULL, pszParmValue, NULL);
2464 return set_netbios_aliases(Globals.netbios_aliases);
2467 /***************************************************************************
2468 Handle the include operation.
2469 ***************************************************************************/
2470 static bool bAllowIncludeRegistry = true;
2472 bool lp_include(struct loadparm_context *unused, int snum, const char *pszParmValue, char **ptr)
2474 char *fname;
2476 if (include_depth >= MAX_INCLUDE_DEPTH) {
2477 DEBUG(0, ("Error: Maximum include depth (%u) exceeded!\n",
2478 include_depth));
2479 return false;
2482 if (strequal(pszParmValue, INCLUDE_REGISTRY_NAME)) {
2483 if (!bAllowIncludeRegistry) {
2484 return true;
2486 if (bInGlobalSection) {
2487 bool ret;
2488 include_depth++;
2489 ret = process_registry_globals();
2490 include_depth--;
2491 return ret;
2492 } else {
2493 DEBUG(1, ("\"include = registry\" only effective "
2494 "in %s section\n", GLOBAL_NAME));
2495 return false;
2499 fname = talloc_sub_basic(talloc_tos(), get_current_username(),
2500 current_user_info.domain,
2501 pszParmValue);
2503 add_to_file_list(NULL, &file_lists, pszParmValue, fname);
2505 if (snum < 0) {
2506 string_set(Globals.ctx, ptr, fname);
2507 } else {
2508 string_set(ServicePtrs[snum], ptr, fname);
2511 if (file_exist(fname)) {
2512 bool ret;
2513 include_depth++;
2514 ret = pm_process(fname, do_section, do_parameter, NULL);
2515 include_depth--;
2516 TALLOC_FREE(fname);
2517 return ret;
2520 DEBUG(2, ("Can't find include file %s\n", fname));
2521 TALLOC_FREE(fname);
2522 return true;
2525 static bool handle_ldap_debug_level(struct loadparm_context *unused, int snum, const char *pszParmValue, char **ptr)
2527 Globals.ldap_debug_level = lp_int(pszParmValue);
2528 init_ldap_debugging();
2529 return true;
2533 * idmap related parameters
2536 static bool handle_idmap_backend(struct loadparm_context *unused, int snum, const char *pszParmValue, char **ptr)
2538 lp_do_parameter(snum, "idmap config * : backend", pszParmValue);
2540 return true;
2543 static bool handle_idmap_uid(struct loadparm_context *unused, int snum, const char *pszParmValue, char **ptr)
2545 lp_do_parameter(snum, "idmap config * : range", pszParmValue);
2547 return true;
2550 static bool handle_idmap_gid(struct loadparm_context *unused, int snum, const char *pszParmValue, char **ptr)
2552 lp_do_parameter(snum, "idmap config * : range", pszParmValue);
2554 return true;
2557 bool lp_idmap_range(const char *domain_name, uint32_t *low, uint32_t *high)
2559 char *config_option = NULL;
2560 const char *range = NULL;
2561 bool ret = false;
2563 SMB_ASSERT(low != NULL);
2564 SMB_ASSERT(high != NULL);
2566 if ((domain_name == NULL) || (domain_name[0] == '\0')) {
2567 domain_name = "*";
2570 config_option = talloc_asprintf(talloc_tos(), "idmap config %s",
2571 domain_name);
2572 if (config_option == NULL) {
2573 DEBUG(0, ("out of memory\n"));
2574 return false;
2577 range = lp_parm_const_string(-1, config_option, "range", NULL);
2578 if (range == NULL) {
2579 DEBUG(1, ("idmap range not specified for domain '%s'\n", domain_name));
2580 goto done;
2583 if (sscanf(range, "%u - %u", low, high) != 2) {
2584 DEBUG(1, ("error parsing idmap range '%s' for domain '%s'\n",
2585 range, domain_name));
2586 goto done;
2589 ret = true;
2591 done:
2592 talloc_free(config_option);
2593 return ret;
2597 bool lp_idmap_default_range(uint32_t *low, uint32_t *high)
2599 return lp_idmap_range("*", low, high);
2602 const char *lp_idmap_backend(const char *domain_name)
2604 char *config_option = NULL;
2605 const char *backend = NULL;
2607 if ((domain_name == NULL) || (domain_name[0] == '\0')) {
2608 domain_name = "*";
2611 config_option = talloc_asprintf(talloc_tos(), "idmap config %s",
2612 domain_name);
2613 if (config_option == NULL) {
2614 DEBUG(0, ("out of memory\n"));
2615 return false;
2618 backend = lp_parm_const_string(-1, config_option, "backend", NULL);
2619 if (backend == NULL) {
2620 DEBUG(1, ("idmap backend not specified for domain '%s'\n", domain_name));
2621 goto done;
2624 done:
2625 talloc_free(config_option);
2626 return backend;
2629 const char *lp_idmap_default_backend(void)
2631 return lp_idmap_backend("*");
2634 /***************************************************************************
2635 Handle ldap suffixes - default to ldapsuffix if sub-suffixes are not defined.
2636 ***************************************************************************/
2638 static const char *append_ldap_suffix(TALLOC_CTX *ctx, const char *str )
2640 const char *suffix_string;
2642 suffix_string = talloc_asprintf(ctx, "%s,%s", str,
2643 Globals.ldap_suffix );
2644 if ( !suffix_string ) {
2645 DEBUG(0,("append_ldap_suffix: talloc_asprintf() failed!\n"));
2646 return "";
2649 return suffix_string;
2652 const char *lp_ldap_machine_suffix(TALLOC_CTX *ctx)
2654 if (Globals.szLdapMachineSuffix[0])
2655 return append_ldap_suffix(ctx, Globals.szLdapMachineSuffix);
2657 return lp_string(ctx, Globals.ldap_suffix);
2660 const char *lp_ldap_user_suffix(TALLOC_CTX *ctx)
2662 if (Globals.szLdapUserSuffix[0])
2663 return append_ldap_suffix(ctx, Globals.szLdapUserSuffix);
2665 return lp_string(ctx, Globals.ldap_suffix);
2668 const char *lp_ldap_group_suffix(TALLOC_CTX *ctx)
2670 if (Globals.szLdapGroupSuffix[0])
2671 return append_ldap_suffix(ctx, Globals.szLdapGroupSuffix);
2673 return lp_string(ctx, Globals.ldap_suffix);
2676 const char *lp_ldap_idmap_suffix(TALLOC_CTX *ctx)
2678 if (Globals.szLdapIdmapSuffix[0])
2679 return append_ldap_suffix(ctx, Globals.szLdapIdmapSuffix);
2681 return lp_string(ctx, Globals.ldap_suffix);
2684 /****************************************************************************
2685 set the value for a P_ENUM
2686 ***************************************************************************/
2688 static void lp_set_enum_parm( struct parm_struct *parm, const char *pszParmValue,
2689 int *ptr )
2691 int i;
2693 for (i = 0; parm->enum_list[i].name; i++) {
2694 if ( strequal(pszParmValue, parm->enum_list[i].name)) {
2695 *ptr = parm->enum_list[i].value;
2696 return;
2699 DEBUG(0, ("WARNING: Ignoring invalid value '%s' for parameter '%s'\n",
2700 pszParmValue, parm->label));
2703 /***************************************************************************
2704 ***************************************************************************/
2706 static bool handle_printing(struct loadparm_context *unused, int snum, const char *pszParmValue, char **ptr)
2708 static int parm_num = -1;
2709 struct loadparm_service *s;
2711 if ( parm_num == -1 )
2712 parm_num = lpcfg_map_parameter( "printing" );
2714 lp_set_enum_parm( &parm_table[parm_num], pszParmValue, (int*)ptr );
2716 if ( snum < 0 ) {
2717 s = &sDefault;
2718 init_printer_values(Globals.ctx, s);
2719 } else {
2720 s = ServicePtrs[snum];
2721 init_printer_values(s, s);
2724 return true;
2728 return the parameter pointer for a parameter
2730 void *lp_parm_ptr(struct loadparm_service *service, struct parm_struct *parm)
2732 if (service == NULL) {
2733 if (parm->p_class == P_LOCAL)
2734 return (void *)(((char *)&sDefault)+parm->offset);
2735 else if (parm->p_class == P_GLOBAL)
2736 return (void *)(((char *)&Globals)+parm->offset);
2737 else return NULL;
2738 } else {
2739 return (void *)(((char *)service) + parm->offset);
2743 /***************************************************************************
2744 Return the local pointer to a parameter given the service number and parameter
2745 ***************************************************************************/
2747 void *lp_local_ptr_by_snum(int snum, struct parm_struct *parm)
2749 return lp_parm_ptr(ServicePtrs[snum], parm);
2752 /***************************************************************************
2753 Process a parameter for a particular service number. If snum < 0
2754 then assume we are in the globals.
2755 ***************************************************************************/
2757 bool lp_do_parameter(int snum, const char *pszParmName, const char *pszParmValue)
2759 int parmnum, i;
2760 void *parm_ptr = NULL; /* where we are going to store the result */
2761 struct parmlist_entry **opt_list;
2762 TALLOC_CTX *mem_ctx;
2764 parmnum = lpcfg_map_parameter(pszParmName);
2766 if (parmnum < 0) {
2767 if (strchr(pszParmName, ':') == NULL) {
2768 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n",
2769 pszParmName));
2770 return true;
2774 * We've got a parametric option
2777 if (snum < 0) {
2778 opt_list = &Globals.param_opt;
2779 set_param_opt(NULL, opt_list, pszParmName, pszParmValue, 0);
2780 } else {
2781 opt_list = &ServicePtrs[snum]->param_opt;
2782 set_param_opt(ServicePtrs[snum], opt_list, pszParmName, pszParmValue, 0);
2785 return true;
2788 /* if it's already been set by the command line, then we don't
2789 override here */
2790 if (parm_table[parmnum].flags & FLAG_CMDLINE) {
2791 return true;
2794 if (parm_table[parmnum].flags & FLAG_DEPRECATED) {
2795 DEBUG(1, ("WARNING: The \"%s\" option is deprecated\n",
2796 pszParmName));
2799 /* we might point at a service, the default service or a global */
2800 if (snum < 0) {
2801 parm_ptr = lp_parm_ptr(NULL, &parm_table[parmnum]);
2802 } else {
2803 if (parm_table[parmnum].p_class == P_GLOBAL) {
2804 DEBUG(0,
2805 ("Global parameter %s found in service section!\n",
2806 pszParmName));
2807 return true;
2809 parm_ptr = lp_local_ptr_by_snum(snum, &parm_table[parmnum]);
2812 if (snum >= 0) {
2813 if (!ServicePtrs[snum]->copymap)
2814 init_copymap(ServicePtrs[snum]);
2816 /* this handles the aliases - set the copymap for other entries with
2817 the same data pointer */
2818 for (i = 0; parm_table[i].label; i++) {
2819 if ((parm_table[i].offset == parm_table[parmnum].offset)
2820 && (parm_table[i].p_class == parm_table[parmnum].p_class)) {
2821 bitmap_clear(ServicePtrs[snum]->copymap, i);
2824 mem_ctx = ServicePtrs[snum];
2825 } else {
2826 mem_ctx = Globals.ctx;
2829 /* if it is a special case then go ahead */
2830 if (parm_table[parmnum].special) {
2831 bool ok;
2832 struct loadparm_context *lp_ctx = loadparm_init_s3(talloc_tos(),
2833 loadparm_s3_helpers());
2834 ok = parm_table[parmnum].special(lp_ctx, snum, pszParmValue,
2835 (char **)parm_ptr);
2836 TALLOC_FREE(lp_ctx);
2838 return ok;
2841 /* now switch on the type of variable it is */
2842 switch (parm_table[parmnum].type)
2844 case P_BOOL:
2845 *(bool *)parm_ptr = lp_bool(pszParmValue);
2846 break;
2848 case P_BOOLREV:
2849 *(bool *)parm_ptr = !lp_bool(pszParmValue);
2850 break;
2852 case P_INTEGER:
2853 *(int *)parm_ptr = lp_int(pszParmValue);
2854 break;
2856 case P_CHAR:
2857 *(char *)parm_ptr = *pszParmValue;
2858 break;
2860 case P_OCTAL:
2861 i = sscanf(pszParmValue, "%o", (int *)parm_ptr);
2862 if ( i != 1 ) {
2863 DEBUG ( 0, ("Invalid octal number %s\n", pszParmName ));
2865 break;
2867 case P_BYTES:
2869 uint64_t val;
2870 if (conv_str_size_error(pszParmValue, &val)) {
2871 if (val <= INT_MAX) {
2872 *(int *)parm_ptr = (int)val;
2873 break;
2877 DEBUG(0,("lp_do_parameter(%s): value is not "
2878 "a valid size specifier!\n", pszParmValue));
2879 return false;
2882 case P_LIST:
2883 case P_CMDLIST:
2884 TALLOC_FREE(*((char ***)parm_ptr));
2885 *(char ***)parm_ptr = str_list_make_v3(
2886 NULL, pszParmValue, NULL);
2887 break;
2889 case P_STRING:
2890 string_set(mem_ctx, (char **)parm_ptr, pszParmValue);
2891 break;
2893 case P_USTRING:
2895 char *upper_string = strupper_talloc(talloc_tos(),
2896 pszParmValue);
2897 string_set(mem_ctx, (char **)parm_ptr, upper_string);
2898 TALLOC_FREE(upper_string);
2899 break;
2901 case P_ENUM:
2902 lp_set_enum_parm( &parm_table[parmnum], pszParmValue, (int*)parm_ptr );
2903 break;
2904 case P_SEP:
2905 break;
2908 return true;
2911 /***************************************************************************
2912 set a parameter, marking it with FLAG_CMDLINE. Parameters marked as
2913 FLAG_CMDLINE won't be overridden by loads from smb.conf.
2914 ***************************************************************************/
2916 static bool lp_set_cmdline_helper(const char *pszParmName, const char *pszParmValue, bool store_values)
2918 int parmnum, i;
2919 parmnum = lpcfg_map_parameter(pszParmName);
2920 if (parmnum >= 0) {
2921 parm_table[parmnum].flags &= ~FLAG_CMDLINE;
2922 if (!lp_do_parameter(-1, pszParmName, pszParmValue)) {
2923 return false;
2925 parm_table[parmnum].flags |= FLAG_CMDLINE;
2927 /* we have to also set FLAG_CMDLINE on aliases. Aliases must
2928 * be grouped in the table, so we don't have to search the
2929 * whole table */
2930 for (i=parmnum-1;
2931 i>=0 && parm_table[i].offset == parm_table[parmnum].offset
2932 && parm_table[i].p_class == parm_table[parmnum].p_class;
2933 i--) {
2934 parm_table[i].flags |= FLAG_CMDLINE;
2936 for (i=parmnum+1;i<NUMPARAMETERS && parm_table[i].offset == parm_table[parmnum].offset
2937 && parm_table[i].p_class == parm_table[parmnum].p_class;i++) {
2938 parm_table[i].flags |= FLAG_CMDLINE;
2941 if (store_values) {
2942 store_lp_set_cmdline(pszParmName, pszParmValue);
2944 return true;
2947 /* it might be parametric */
2948 if (strchr(pszParmName, ':') != NULL) {
2949 set_param_opt(NULL, &Globals.param_opt, pszParmName, pszParmValue, FLAG_CMDLINE);
2950 if (store_values) {
2951 store_lp_set_cmdline(pszParmName, pszParmValue);
2953 return true;
2956 DEBUG(0, ("Ignoring unknown parameter \"%s\"\n", pszParmName));
2957 return true;
2960 bool lp_set_cmdline(const char *pszParmName, const char *pszParmValue)
2962 return lp_set_cmdline_helper(pszParmName, pszParmValue, true);
2965 /***************************************************************************
2966 Process a parameter.
2967 ***************************************************************************/
2969 static bool do_parameter(const char *pszParmName, const char *pszParmValue,
2970 void *userdata)
2972 if (!bInGlobalSection && bGlobalOnly)
2973 return true;
2975 DEBUGADD(4, ("doing parameter %s = %s\n", pszParmName, pszParmValue));
2977 return (lp_do_parameter(bInGlobalSection ? -2 : iServiceIndex,
2978 pszParmName, pszParmValue));
2981 /***************************************************************************
2982 Initialize any local variables in the sDefault table, after parsing a
2983 [globals] section.
2984 ***************************************************************************/
2986 static void init_locals(void)
2989 * We run this check once the [globals] is parsed, to force
2990 * the VFS objects and other per-share settings we need for
2991 * the standard way a AD DC is operated. We may change these
2992 * as our code evolves, which is why we force these settings.
2994 * We can't do this at the end of lp_load_ex(), as by that
2995 * point the services have been loaded and they will already
2996 * have "" as their vfs objects.
2998 if (lp_server_role() == ROLE_ACTIVE_DIRECTORY_DC) {
2999 const char **vfs_objects = lp_vfs_objects(-1);
3000 if (!vfs_objects || !vfs_objects[0]) {
3001 if (lp_parm_const_string(-1, "xattr_tdb", "file", NULL)) {
3002 lp_do_parameter(-1, "vfs objects", "dfs_samba4 acl_xattr xattr_tdb");
3003 } else if (lp_parm_const_string(-1, "posix", "eadb", NULL)) {
3004 lp_do_parameter(-1, "vfs objects", "dfs_samba4 acl_xattr posix_eadb");
3005 } else {
3006 lp_do_parameter(-1, "vfs objects", "dfs_samba4 acl_xattr");
3010 lp_do_parameter(-1, "map hidden", "no");
3011 lp_do_parameter(-1, "map system", "no");
3012 lp_do_parameter(-1, "map readonly", "no");
3013 lp_do_parameter(-1, "map archive", "no");
3014 lp_do_parameter(-1, "store dos attributes", "yes");
3018 /***************************************************************************
3019 Process a new section (service). At this stage all sections are services.
3020 Later we'll have special sections that permit server parameters to be set.
3021 Returns true on success, false on failure.
3022 ***************************************************************************/
3024 static bool do_section(const char *pszSectionName, void *userdata)
3026 bool bRetval;
3027 bool isglobal = ((strwicmp(pszSectionName, GLOBAL_NAME) == 0) ||
3028 (strwicmp(pszSectionName, GLOBAL_NAME2) == 0));
3029 bRetval = false;
3031 /* if we were in a global section then do the local inits */
3032 if (bInGlobalSection && !isglobal)
3033 init_locals();
3035 /* if we've just struck a global section, note the fact. */
3036 bInGlobalSection = isglobal;
3038 /* check for multiple global sections */
3039 if (bInGlobalSection) {
3040 DEBUG(3, ("Processing section \"[%s]\"\n", pszSectionName));
3041 return true;
3044 if (!bInGlobalSection && bGlobalOnly)
3045 return true;
3047 /* if we have a current service, tidy it up before moving on */
3048 bRetval = true;
3050 if (iServiceIndex >= 0)
3051 bRetval = service_ok(iServiceIndex);
3053 /* if all is still well, move to the next record in the services array */
3054 if (bRetval) {
3055 /* We put this here to avoid an odd message order if messages are */
3056 /* issued by the post-processing of a previous section. */
3057 DEBUG(2, ("Processing section \"[%s]\"\n", pszSectionName));
3059 iServiceIndex = add_a_service(&sDefault, pszSectionName);
3060 if (iServiceIndex < 0) {
3061 DEBUG(0, ("Failed to add a new service\n"));
3062 return false;
3064 /* Clean all parametric options for service */
3065 /* They will be added during parsing again */
3066 free_param_opts(&ServicePtrs[iServiceIndex]->param_opt);
3069 return bRetval;
3073 /***************************************************************************
3074 Determine if a partcular base parameter is currentl set to the default value.
3075 ***************************************************************************/
3077 static bool is_default(int i)
3079 switch (parm_table[i].type) {
3080 case P_LIST:
3081 case P_CMDLIST:
3082 return str_list_equal((const char * const *)parm_table[i].def.lvalue,
3083 *(const char ***)lp_parm_ptr(NULL,
3084 &parm_table[i]));
3085 case P_STRING:
3086 case P_USTRING:
3087 return strequal(parm_table[i].def.svalue,
3088 *(char **)lp_parm_ptr(NULL,
3089 &parm_table[i]));
3090 case P_BOOL:
3091 case P_BOOLREV:
3092 return parm_table[i].def.bvalue ==
3093 *(bool *)lp_parm_ptr(NULL,
3094 &parm_table[i]);
3095 case P_CHAR:
3096 return parm_table[i].def.cvalue ==
3097 *(char *)lp_parm_ptr(NULL,
3098 &parm_table[i]);
3099 case P_INTEGER:
3100 case P_OCTAL:
3101 case P_ENUM:
3102 case P_BYTES:
3103 return parm_table[i].def.ivalue ==
3104 *(int *)lp_parm_ptr(NULL,
3105 &parm_table[i]);
3106 case P_SEP:
3107 break;
3109 return false;
3112 /***************************************************************************
3113 Display the contents of the global structure.
3114 ***************************************************************************/
3116 static void dump_globals(FILE *f)
3118 int i;
3119 struct parmlist_entry *data;
3121 fprintf(f, "[global]\n");
3123 for (i = 0; parm_table[i].label; i++)
3124 if (parm_table[i].p_class == P_GLOBAL &&
3125 !(parm_table[i].flags & FLAG_META) &&
3126 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset))) {
3127 if (defaults_saved && is_default(i))
3128 continue;
3129 fprintf(f, "\t%s = ", parm_table[i].label);
3130 lpcfg_print_parameter(&parm_table[i], lp_parm_ptr(NULL,
3131 &parm_table[i]),
3133 fprintf(f, "\n");
3135 if (Globals.param_opt != NULL) {
3136 data = Globals.param_opt;
3137 while(data) {
3138 fprintf(f, "\t%s = %s\n", data->key, data->value);
3139 data = data->next;
3145 /***************************************************************************
3146 Display the contents of a single services record.
3147 ***************************************************************************/
3149 static void dump_a_service(struct loadparm_service *pService, FILE * f)
3151 int i;
3152 struct parmlist_entry *data;
3154 if (pService != &sDefault)
3155 fprintf(f, "[%s]\n", pService->szService);
3157 for (i = 0; parm_table[i].label; i++) {
3159 if (parm_table[i].p_class == P_LOCAL &&
3160 !(parm_table[i].flags & FLAG_META) &&
3161 (*parm_table[i].label != '-') &&
3162 (i == 0 || (parm_table[i].offset != parm_table[i - 1].offset)))
3164 if (pService == &sDefault) {
3165 if (defaults_saved && is_default(i))
3166 continue;
3167 } else {
3168 if (lpcfg_equal_parameter(parm_table[i].type,
3169 lp_parm_ptr(pService, &parm_table[i]),
3170 lp_parm_ptr(NULL, &parm_table[i])))
3171 continue;
3174 fprintf(f, "\t%s = ", parm_table[i].label);
3175 lpcfg_print_parameter(&parm_table[i],
3176 lp_parm_ptr(pService, &parm_table[i]),
3178 fprintf(f, "\n");
3182 if (pService->param_opt != NULL) {
3183 data = pService->param_opt;
3184 while(data) {
3185 fprintf(f, "\t%s = %s\n", data->key, data->value);
3186 data = data->next;
3191 /***************************************************************************
3192 Display the contents of a parameter of a single services record.
3193 ***************************************************************************/
3195 bool dump_a_parameter(int snum, char *parm_name, FILE * f, bool isGlobal)
3197 bool result = false;
3198 fstring local_parm_name;
3199 char *parm_opt;
3200 const char *parm_opt_value;
3202 struct loadparm_context *lp_ctx;
3204 /* check for parametrical option */
3205 fstrcpy( local_parm_name, parm_name);
3206 parm_opt = strchr( local_parm_name, ':');
3208 if (parm_opt) {
3209 *parm_opt = '\0';
3210 parm_opt++;
3211 if (strlen(parm_opt)) {
3212 parm_opt_value = lp_parm_const_string( snum,
3213 local_parm_name, parm_opt, NULL);
3214 if (parm_opt_value) {
3215 printf( "%s\n", parm_opt_value);
3216 result = true;
3219 return result;
3222 lp_ctx = loadparm_init_s3(talloc_tos(), loadparm_s3_helpers());
3223 if (lp_ctx == NULL) {
3224 return false;
3227 if (isGlobal) {
3228 result = lpcfg_dump_a_parameter(lp_ctx, NULL, parm_name, f);
3229 } else {
3230 result = lpcfg_dump_a_parameter(lp_ctx, ServicePtrs[snum], parm_name, f);
3232 TALLOC_FREE(lp_ctx);
3233 return result;
3236 /***************************************************************************
3237 Return info about the requested parameter (given as a string).
3238 Return NULL when the string is not a valid parameter name.
3239 ***************************************************************************/
3241 struct parm_struct *lp_get_parameter(const char *param_name)
3243 int num = lpcfg_map_parameter(param_name);
3245 if (num < 0) {
3246 return NULL;
3249 return &parm_table[num];
3252 #if 0
3253 /***************************************************************************
3254 Display the contents of a single copy structure.
3255 ***************************************************************************/
3256 static void dump_copy_map(bool *pcopymap)
3258 int i;
3259 if (!pcopymap)
3260 return;
3262 printf("\n\tNon-Copied parameters:\n");
3264 for (i = 0; parm_table[i].label; i++)
3265 if (parm_table[i].p_class == P_LOCAL &&
3266 parm_table[i].ptr && !pcopymap[i] &&
3267 (i == 0 || (parm_table[i].ptr != parm_table[i - 1].ptr)))
3269 printf("\t\t%s\n", parm_table[i].label);
3272 #endif
3274 /***************************************************************************
3275 Return TRUE if the passed service number is within range.
3276 ***************************************************************************/
3278 bool lp_snum_ok(int iService)
3280 return (LP_SNUM_OK(iService) && ServicePtrs[iService]->bAvailable);
3283 /***************************************************************************
3284 Auto-load some home services.
3285 ***************************************************************************/
3287 static void lp_add_auto_services(char *str)
3289 char *s;
3290 char *p;
3291 int homes;
3292 char *saveptr;
3294 if (!str)
3295 return;
3297 s = talloc_strdup(talloc_tos(), str);
3298 if (!s) {
3299 smb_panic("talloc_strdup failed");
3300 return;
3303 homes = lp_servicenumber(HOMES_NAME);
3305 for (p = strtok_r(s, LIST_SEP, &saveptr); p;
3306 p = strtok_r(NULL, LIST_SEP, &saveptr)) {
3307 char *home;
3309 if (lp_servicenumber(p) >= 0)
3310 continue;
3312 home = get_user_home_dir(talloc_tos(), p);
3314 if (home && home[0] && homes >= 0)
3315 lp_add_home(p, homes, p, home);
3317 TALLOC_FREE(home);
3319 TALLOC_FREE(s);
3322 /***************************************************************************
3323 Auto-load one printer.
3324 ***************************************************************************/
3326 void lp_add_one_printer(const char *name, const char *comment,
3327 const char *location, void *pdata)
3329 int printers = lp_servicenumber(PRINTERS_NAME);
3330 int i;
3332 if (lp_servicenumber(name) < 0) {
3333 lp_add_printer(name, printers);
3334 if ((i = lp_servicenumber(name)) >= 0) {
3335 string_set(ServicePtrs[i], &ServicePtrs[i]->comment, comment);
3336 ServicePtrs[i]->autoloaded = true;
3341 /***************************************************************************
3342 Have we loaded a services file yet?
3343 ***************************************************************************/
3345 bool lp_loaded(void)
3347 return (bLoaded);
3350 /***************************************************************************
3351 Unload unused services.
3352 ***************************************************************************/
3354 void lp_killunused(struct smbd_server_connection *sconn,
3355 bool (*snumused) (struct smbd_server_connection *, int))
3357 int i;
3358 for (i = 0; i < iNumServices; i++) {
3359 if (!VALID(i))
3360 continue;
3362 /* don't kill autoloaded or usershare services */
3363 if ( ServicePtrs[i]->autoloaded ||
3364 ServicePtrs[i]->usershare == USERSHARE_VALID) {
3365 continue;
3368 if (!snumused || !snumused(sconn, i)) {
3369 free_service_byindex(i);
3375 * Kill all except autoloaded and usershare services - convenience wrapper
3377 void lp_kill_all_services(void)
3379 lp_killunused(NULL, NULL);
3382 /***************************************************************************
3383 Unload a service.
3384 ***************************************************************************/
3386 void lp_killservice(int iServiceIn)
3388 if (VALID(iServiceIn)) {
3389 free_service_byindex(iServiceIn);
3393 /***************************************************************************
3394 Save the curent values of all global and sDefault parameters into the
3395 defaults union. This allows testparm to show only the
3396 changed (ie. non-default) parameters.
3397 ***************************************************************************/
3399 static void lp_save_defaults(void)
3401 int i;
3402 for (i = 0; parm_table[i].label; i++) {
3403 if (i > 0 && parm_table[i].offset == parm_table[i - 1].offset
3404 && parm_table[i].p_class == parm_table[i - 1].p_class)
3405 continue;
3406 switch (parm_table[i].type) {
3407 case P_LIST:
3408 case P_CMDLIST:
3409 parm_table[i].def.lvalue = str_list_copy(
3410 NULL, *(const char ***)lp_parm_ptr(NULL, &parm_table[i]));
3411 break;
3412 case P_STRING:
3413 case P_USTRING:
3414 parm_table[i].def.svalue = talloc_strdup(Globals.ctx, *(char **)lp_parm_ptr(NULL, &parm_table[i]));
3415 if (parm_table[i].def.svalue == NULL) {
3416 smb_panic("talloc_strdup failed");
3418 break;
3419 case P_BOOL:
3420 case P_BOOLREV:
3421 parm_table[i].def.bvalue =
3422 *(bool *)lp_parm_ptr(NULL, &parm_table[i]);
3423 break;
3424 case P_CHAR:
3425 parm_table[i].def.cvalue =
3426 *(char *)lp_parm_ptr(NULL, &parm_table[i]);
3427 break;
3428 case P_INTEGER:
3429 case P_OCTAL:
3430 case P_ENUM:
3431 case P_BYTES:
3432 parm_table[i].def.ivalue =
3433 *(int *)lp_parm_ptr(NULL, &parm_table[i]);
3434 break;
3435 case P_SEP:
3436 break;
3439 defaults_saved = true;
3442 /***********************************************************
3443 If we should send plaintext/LANMAN passwords in the clinet
3444 ************************************************************/
3446 static void set_allowed_client_auth(void)
3448 if (Globals.client_ntlmv2_auth) {
3449 Globals.client_lanman_auth = false;
3451 if (!Globals.client_lanman_auth) {
3452 Globals.client_plaintext_auth = false;
3456 /***************************************************************************
3457 JRA.
3458 The following code allows smbd to read a user defined share file.
3459 Yes, this is my intent. Yes, I'm comfortable with that...
3461 THE FOLLOWING IS SECURITY CRITICAL CODE.
3463 It washes your clothes, it cleans your house, it guards you while you sleep...
3464 Do not f%^k with it....
3465 ***************************************************************************/
3467 #define MAX_USERSHARE_FILE_SIZE (10*1024)
3469 /***************************************************************************
3470 Check allowed stat state of a usershare file.
3471 Ensure we print out who is dicking with us so the admin can
3472 get their sorry ass fired.
3473 ***************************************************************************/
3475 static bool check_usershare_stat(const char *fname,
3476 const SMB_STRUCT_STAT *psbuf)
3478 if (!S_ISREG(psbuf->st_ex_mode)) {
3479 DEBUG(0,("check_usershare_stat: file %s owned by uid %u is "
3480 "not a regular file\n",
3481 fname, (unsigned int)psbuf->st_ex_uid ));
3482 return false;
3485 /* Ensure this doesn't have the other write bit set. */
3486 if (psbuf->st_ex_mode & S_IWOTH) {
3487 DEBUG(0,("check_usershare_stat: file %s owned by uid %u allows "
3488 "public write. Refusing to allow as a usershare file.\n",
3489 fname, (unsigned int)psbuf->st_ex_uid ));
3490 return false;
3493 /* Should be 10k or less. */
3494 if (psbuf->st_ex_size > MAX_USERSHARE_FILE_SIZE) {
3495 DEBUG(0,("check_usershare_stat: file %s owned by uid %u is "
3496 "too large (%u) to be a user share file.\n",
3497 fname, (unsigned int)psbuf->st_ex_uid,
3498 (unsigned int)psbuf->st_ex_size ));
3499 return false;
3502 return true;
3505 /***************************************************************************
3506 Parse the contents of a usershare file.
3507 ***************************************************************************/
3509 enum usershare_err parse_usershare_file(TALLOC_CTX *ctx,
3510 SMB_STRUCT_STAT *psbuf,
3511 const char *servicename,
3512 int snum,
3513 char **lines,
3514 int numlines,
3515 char **pp_sharepath,
3516 char **pp_comment,
3517 char **pp_cp_servicename,
3518 struct security_descriptor **ppsd,
3519 bool *pallow_guest)
3521 const char **prefixallowlist = lp_usershare_prefix_allow_list();
3522 const char **prefixdenylist = lp_usershare_prefix_deny_list();
3523 int us_vers;
3524 DIR *dp;
3525 SMB_STRUCT_STAT sbuf;
3526 char *sharepath = NULL;
3527 char *comment = NULL;
3529 *pp_sharepath = NULL;
3530 *pp_comment = NULL;
3532 *pallow_guest = false;
3534 if (numlines < 4) {
3535 return USERSHARE_MALFORMED_FILE;
3538 if (strcmp(lines[0], "#VERSION 1") == 0) {
3539 us_vers = 1;
3540 } else if (strcmp(lines[0], "#VERSION 2") == 0) {
3541 us_vers = 2;
3542 if (numlines < 5) {
3543 return USERSHARE_MALFORMED_FILE;
3545 } else {
3546 return USERSHARE_BAD_VERSION;
3549 if (strncmp(lines[1], "path=", 5) != 0) {
3550 return USERSHARE_MALFORMED_PATH;
3553 sharepath = talloc_strdup(ctx, &lines[1][5]);
3554 if (!sharepath) {
3555 return USERSHARE_POSIX_ERR;
3557 trim_string(sharepath, " ", " ");
3559 if (strncmp(lines[2], "comment=", 8) != 0) {
3560 return USERSHARE_MALFORMED_COMMENT_DEF;
3563 comment = talloc_strdup(ctx, &lines[2][8]);
3564 if (!comment) {
3565 return USERSHARE_POSIX_ERR;
3567 trim_string(comment, " ", " ");
3568 trim_char(comment, '"', '"');
3570 if (strncmp(lines[3], "usershare_acl=", 14) != 0) {
3571 return USERSHARE_MALFORMED_ACL_DEF;
3574 if (!parse_usershare_acl(ctx, &lines[3][14], ppsd)) {
3575 return USERSHARE_ACL_ERR;
3578 if (us_vers == 2) {
3579 if (strncmp(lines[4], "guest_ok=", 9) != 0) {
3580 return USERSHARE_MALFORMED_ACL_DEF;
3582 if (lines[4][9] == 'y') {
3583 *pallow_guest = true;
3586 /* Backwards compatible extension to file version #2. */
3587 if (numlines > 5) {
3588 if (strncmp(lines[5], "sharename=", 10) != 0) {
3589 return USERSHARE_MALFORMED_SHARENAME_DEF;
3591 if (!strequal(&lines[5][10], servicename)) {
3592 return USERSHARE_BAD_SHARENAME;
3594 *pp_cp_servicename = talloc_strdup(ctx, &lines[5][10]);
3595 if (!*pp_cp_servicename) {
3596 return USERSHARE_POSIX_ERR;
3601 if (*pp_cp_servicename == NULL) {
3602 *pp_cp_servicename = talloc_strdup(ctx, servicename);
3603 if (!*pp_cp_servicename) {
3604 return USERSHARE_POSIX_ERR;
3608 if (snum != -1 && (strcmp(sharepath, ServicePtrs[snum]->path) == 0)) {
3609 /* Path didn't change, no checks needed. */
3610 *pp_sharepath = sharepath;
3611 *pp_comment = comment;
3612 return USERSHARE_OK;
3615 /* The path *must* be absolute. */
3616 if (sharepath[0] != '/') {
3617 DEBUG(2,("parse_usershare_file: share %s: path %s is not an absolute path.\n",
3618 servicename, sharepath));
3619 return USERSHARE_PATH_NOT_ABSOLUTE;
3622 /* If there is a usershare prefix deny list ensure one of these paths
3623 doesn't match the start of the user given path. */
3624 if (prefixdenylist) {
3625 int i;
3626 for ( i=0; prefixdenylist[i]; i++ ) {
3627 DEBUG(10,("parse_usershare_file: share %s : checking prefixdenylist[%d]='%s' against %s\n",
3628 servicename, i, prefixdenylist[i], sharepath ));
3629 if (memcmp( sharepath, prefixdenylist[i], strlen(prefixdenylist[i])) == 0) {
3630 DEBUG(2,("parse_usershare_file: share %s path %s starts with one of the "
3631 "usershare prefix deny list entries.\n",
3632 servicename, sharepath));
3633 return USERSHARE_PATH_IS_DENIED;
3638 /* If there is a usershare prefix allow list ensure one of these paths
3639 does match the start of the user given path. */
3641 if (prefixallowlist) {
3642 int i;
3643 for ( i=0; prefixallowlist[i]; i++ ) {
3644 DEBUG(10,("parse_usershare_file: share %s checking prefixallowlist[%d]='%s' against %s\n",
3645 servicename, i, prefixallowlist[i], sharepath ));
3646 if (memcmp( sharepath, prefixallowlist[i], strlen(prefixallowlist[i])) == 0) {
3647 break;
3650 if (prefixallowlist[i] == NULL) {
3651 DEBUG(2,("parse_usershare_file: share %s path %s doesn't start with one of the "
3652 "usershare prefix allow list entries.\n",
3653 servicename, sharepath));
3654 return USERSHARE_PATH_NOT_ALLOWED;
3658 /* Ensure this is pointing to a directory. */
3659 dp = opendir(sharepath);
3661 if (!dp) {
3662 DEBUG(2,("parse_usershare_file: share %s path %s is not a directory.\n",
3663 servicename, sharepath));
3664 return USERSHARE_PATH_NOT_DIRECTORY;
3667 /* Ensure the owner of the usershare file has permission to share
3668 this directory. */
3670 if (sys_stat(sharepath, &sbuf, false) == -1) {
3671 DEBUG(2,("parse_usershare_file: share %s : stat failed on path %s. %s\n",
3672 servicename, sharepath, strerror(errno) ));
3673 closedir(dp);
3674 return USERSHARE_POSIX_ERR;
3677 closedir(dp);
3679 if (!S_ISDIR(sbuf.st_ex_mode)) {
3680 DEBUG(2,("parse_usershare_file: share %s path %s is not a directory.\n",
3681 servicename, sharepath ));
3682 return USERSHARE_PATH_NOT_DIRECTORY;
3685 /* Check if sharing is restricted to owner-only. */
3686 /* psbuf is the stat of the usershare definition file,
3687 sbuf is the stat of the target directory to be shared. */
3689 if (lp_usershare_owner_only()) {
3690 /* root can share anything. */
3691 if ((psbuf->st_ex_uid != 0) && (sbuf.st_ex_uid != psbuf->st_ex_uid)) {
3692 return USERSHARE_PATH_NOT_ALLOWED;
3696 *pp_sharepath = sharepath;
3697 *pp_comment = comment;
3698 return USERSHARE_OK;
3701 /***************************************************************************
3702 Deal with a usershare file.
3703 Returns:
3704 >= 0 - snum
3705 -1 - Bad name, invalid contents.
3706 - service name already existed and not a usershare, problem
3707 with permissions to share directory etc.
3708 ***************************************************************************/
3710 static int process_usershare_file(const char *dir_name, const char *file_name, int snum_template)
3712 SMB_STRUCT_STAT sbuf;
3713 SMB_STRUCT_STAT lsbuf;
3714 char *fname = NULL;
3715 char *sharepath = NULL;
3716 char *comment = NULL;
3717 char *cp_service_name = NULL;
3718 char **lines = NULL;
3719 int numlines = 0;
3720 int fd = -1;
3721 int iService = -1;
3722 TALLOC_CTX *ctx = talloc_stackframe();
3723 struct security_descriptor *psd = NULL;
3724 bool guest_ok = false;
3725 char *canon_name = NULL;
3726 bool added_service = false;
3727 int ret = -1;
3729 /* Ensure share name doesn't contain invalid characters. */
3730 if (!validate_net_name(file_name, INVALID_SHARENAME_CHARS, strlen(file_name))) {
3731 DEBUG(0,("process_usershare_file: share name %s contains "
3732 "invalid characters (any of %s)\n",
3733 file_name, INVALID_SHARENAME_CHARS ));
3734 goto out;
3737 canon_name = canonicalize_servicename(ctx, file_name);
3738 if (!canon_name) {
3739 goto out;
3742 fname = talloc_asprintf(ctx, "%s/%s", dir_name, file_name);
3743 if (!fname) {
3744 goto out;
3747 /* Minimize the race condition by doing an lstat before we
3748 open and fstat. Ensure this isn't a symlink link. */
3750 if (sys_lstat(fname, &lsbuf, false) != 0) {
3751 DEBUG(0,("process_usershare_file: stat of %s failed. %s\n",
3752 fname, strerror(errno) ));
3753 goto out;
3756 /* This must be a regular file, not a symlink, directory or
3757 other strange filetype. */
3758 if (!check_usershare_stat(fname, &lsbuf)) {
3759 goto out;
3763 TDB_DATA data;
3764 NTSTATUS status;
3766 status = dbwrap_fetch_bystring(ServiceHash, canon_name,
3767 canon_name, &data);
3769 iService = -1;
3771 if (NT_STATUS_IS_OK(status) &&
3772 (data.dptr != NULL) &&
3773 (data.dsize == sizeof(iService))) {
3774 memcpy(&iService, data.dptr, sizeof(iService));
3778 if (iService != -1 &&
3779 timespec_compare(&ServicePtrs[iService]->usershare_last_mod,
3780 &lsbuf.st_ex_mtime) == 0) {
3781 /* Nothing changed - Mark valid and return. */
3782 DEBUG(10,("process_usershare_file: service %s not changed.\n",
3783 canon_name ));
3784 ServicePtrs[iService]->usershare = USERSHARE_VALID;
3785 ret = iService;
3786 goto out;
3789 /* Try and open the file read only - no symlinks allowed. */
3790 #ifdef O_NOFOLLOW
3791 fd = open(fname, O_RDONLY|O_NOFOLLOW, 0);
3792 #else
3793 fd = open(fname, O_RDONLY, 0);
3794 #endif
3796 if (fd == -1) {
3797 DEBUG(0,("process_usershare_file: unable to open %s. %s\n",
3798 fname, strerror(errno) ));
3799 goto out;
3802 /* Now fstat to be *SURE* it's a regular file. */
3803 if (sys_fstat(fd, &sbuf, false) != 0) {
3804 close(fd);
3805 DEBUG(0,("process_usershare_file: fstat of %s failed. %s\n",
3806 fname, strerror(errno) ));
3807 goto out;
3810 /* Is it the same dev/inode as was lstated ? */
3811 if (!check_same_stat(&lsbuf, &sbuf)) {
3812 close(fd);
3813 DEBUG(0,("process_usershare_file: fstat of %s is a different file from lstat. "
3814 "Symlink spoofing going on ?\n", fname ));
3815 goto out;
3818 /* This must be a regular file, not a symlink, directory or
3819 other strange filetype. */
3820 if (!check_usershare_stat(fname, &sbuf)) {
3821 close(fd);
3822 goto out;
3825 lines = fd_lines_load(fd, &numlines, MAX_USERSHARE_FILE_SIZE, NULL);
3827 close(fd);
3828 if (lines == NULL) {
3829 DEBUG(0,("process_usershare_file: loading file %s owned by %u failed.\n",
3830 fname, (unsigned int)sbuf.st_ex_uid ));
3831 goto out;
3834 if (parse_usershare_file(ctx, &sbuf, file_name,
3835 iService, lines, numlines, &sharepath,
3836 &comment, &cp_service_name,
3837 &psd, &guest_ok) != USERSHARE_OK) {
3838 goto out;
3841 /* Everything ok - add the service possibly using a template. */
3842 if (iService < 0) {
3843 const struct loadparm_service *sp = &sDefault;
3844 if (snum_template != -1) {
3845 sp = ServicePtrs[snum_template];
3848 if ((iService = add_a_service(sp, cp_service_name)) < 0) {
3849 DEBUG(0, ("process_usershare_file: Failed to add "
3850 "new service %s\n", cp_service_name));
3851 goto out;
3854 added_service = true;
3856 /* Read only is controlled by usershare ACL below. */
3857 ServicePtrs[iService]->read_only = false;
3860 /* Write the ACL of the new/modified share. */
3861 if (!set_share_security(canon_name, psd)) {
3862 DEBUG(0, ("process_usershare_file: Failed to set share "
3863 "security for user share %s\n",
3864 canon_name ));
3865 goto out;
3868 /* If from a template it may be marked invalid. */
3869 ServicePtrs[iService]->valid = true;
3871 /* Set the service as a valid usershare. */
3872 ServicePtrs[iService]->usershare = USERSHARE_VALID;
3874 /* Set guest access. */
3875 if (lp_usershare_allow_guests()) {
3876 ServicePtrs[iService]->guest_ok = guest_ok;
3879 /* And note when it was loaded. */
3880 ServicePtrs[iService]->usershare_last_mod = sbuf.st_ex_mtime;
3881 string_set(ServicePtrs[iService], &ServicePtrs[iService]->path, sharepath);
3882 string_set(ServicePtrs[iService], &ServicePtrs[iService]->comment, comment);
3884 ret = iService;
3886 out:
3888 if (ret == -1 && iService != -1 && added_service) {
3889 lp_remove_service(iService);
3892 TALLOC_FREE(lines);
3893 TALLOC_FREE(ctx);
3894 return ret;
3897 /***************************************************************************
3898 Checks if a usershare entry has been modified since last load.
3899 ***************************************************************************/
3901 static bool usershare_exists(int iService, struct timespec *last_mod)
3903 SMB_STRUCT_STAT lsbuf;
3904 const char *usersharepath = Globals.usershare_path;
3905 char *fname;
3907 fname = talloc_asprintf(talloc_tos(),
3908 "%s/%s",
3909 usersharepath,
3910 ServicePtrs[iService]->szService);
3911 if (fname == NULL) {
3912 return false;
3915 if (sys_lstat(fname, &lsbuf, false) != 0) {
3916 TALLOC_FREE(fname);
3917 return false;
3920 if (!S_ISREG(lsbuf.st_ex_mode)) {
3921 TALLOC_FREE(fname);
3922 return false;
3925 TALLOC_FREE(fname);
3926 *last_mod = lsbuf.st_ex_mtime;
3927 return true;
3930 /***************************************************************************
3931 Load a usershare service by name. Returns a valid servicenumber or -1.
3932 ***************************************************************************/
3934 int load_usershare_service(const char *servicename)
3936 SMB_STRUCT_STAT sbuf;
3937 const char *usersharepath = Globals.usershare_path;
3938 int max_user_shares = Globals.usershare_max_shares;
3939 int snum_template = -1;
3941 if (*usersharepath == 0 || max_user_shares == 0) {
3942 return -1;
3945 if (sys_stat(usersharepath, &sbuf, false) != 0) {
3946 DEBUG(0,("load_usershare_service: stat of %s failed. %s\n",
3947 usersharepath, strerror(errno) ));
3948 return -1;
3951 if (!S_ISDIR(sbuf.st_ex_mode)) {
3952 DEBUG(0,("load_usershare_service: %s is not a directory.\n",
3953 usersharepath ));
3954 return -1;
3958 * This directory must be owned by root, and have the 't' bit set.
3959 * It also must not be writable by "other".
3962 #ifdef S_ISVTX
3963 if (sbuf.st_ex_uid != 0 || !(sbuf.st_ex_mode & S_ISVTX) || (sbuf.st_ex_mode & S_IWOTH)) {
3964 #else
3965 if (sbuf.st_ex_uid != 0 || (sbuf.st_ex_mode & S_IWOTH)) {
3966 #endif
3967 DEBUG(0,("load_usershare_service: directory %s is not owned by root "
3968 "or does not have the sticky bit 't' set or is writable by anyone.\n",
3969 usersharepath ));
3970 return -1;
3973 /* Ensure the template share exists if it's set. */
3974 if (Globals.usershare_template_share[0]) {
3975 /* We can't use lp_servicenumber here as we are recommending that
3976 template shares have -valid=false set. */
3977 for (snum_template = iNumServices - 1; snum_template >= 0; snum_template--) {
3978 if (ServicePtrs[snum_template]->szService &&
3979 strequal(ServicePtrs[snum_template]->szService,
3980 Globals.usershare_template_share)) {
3981 break;
3985 if (snum_template == -1) {
3986 DEBUG(0,("load_usershare_service: usershare template share %s "
3987 "does not exist.\n",
3988 Globals.usershare_template_share ));
3989 return -1;
3993 return process_usershare_file(usersharepath, servicename, snum_template);
3996 /***************************************************************************
3997 Load all user defined shares from the user share directory.
3998 We only do this if we're enumerating the share list.
3999 This is the function that can delete usershares that have
4000 been removed.
4001 ***************************************************************************/
4003 int load_usershare_shares(struct smbd_server_connection *sconn,
4004 bool (*snumused) (struct smbd_server_connection *, int))
4006 DIR *dp;
4007 SMB_STRUCT_STAT sbuf;
4008 struct dirent *de;
4009 int num_usershares = 0;
4010 int max_user_shares = Globals.usershare_max_shares;
4011 unsigned int num_dir_entries, num_bad_dir_entries, num_tmp_dir_entries;
4012 unsigned int allowed_bad_entries = ((2*max_user_shares)/10);
4013 unsigned int allowed_tmp_entries = ((2*max_user_shares)/10);
4014 int iService;
4015 int snum_template = -1;
4016 const char *usersharepath = Globals.usershare_path;
4017 int ret = lp_numservices();
4018 TALLOC_CTX *tmp_ctx;
4020 if (max_user_shares == 0 || *usersharepath == '\0') {
4021 return lp_numservices();
4024 if (sys_stat(usersharepath, &sbuf, false) != 0) {
4025 DEBUG(0,("load_usershare_shares: stat of %s failed. %s\n",
4026 usersharepath, strerror(errno) ));
4027 return ret;
4031 * This directory must be owned by root, and have the 't' bit set.
4032 * It also must not be writable by "other".
4035 #ifdef S_ISVTX
4036 if (sbuf.st_ex_uid != 0 || !(sbuf.st_ex_mode & S_ISVTX) || (sbuf.st_ex_mode & S_IWOTH)) {
4037 #else
4038 if (sbuf.st_ex_uid != 0 || (sbuf.st_ex_mode & S_IWOTH)) {
4039 #endif
4040 DEBUG(0,("load_usershare_shares: directory %s is not owned by root "
4041 "or does not have the sticky bit 't' set or is writable by anyone.\n",
4042 usersharepath ));
4043 return ret;
4046 /* Ensure the template share exists if it's set. */
4047 if (Globals.usershare_template_share[0]) {
4048 /* We can't use lp_servicenumber here as we are recommending that
4049 template shares have -valid=false set. */
4050 for (snum_template = iNumServices - 1; snum_template >= 0; snum_template--) {
4051 if (ServicePtrs[snum_template]->szService &&
4052 strequal(ServicePtrs[snum_template]->szService,
4053 Globals.usershare_template_share)) {
4054 break;
4058 if (snum_template == -1) {
4059 DEBUG(0,("load_usershare_shares: usershare template share %s "
4060 "does not exist.\n",
4061 Globals.usershare_template_share ));
4062 return ret;
4066 /* Mark all existing usershares as pending delete. */
4067 for (iService = iNumServices - 1; iService >= 0; iService--) {
4068 if (VALID(iService) && ServicePtrs[iService]->usershare) {
4069 ServicePtrs[iService]->usershare = USERSHARE_PENDING_DELETE;
4073 dp = opendir(usersharepath);
4074 if (!dp) {
4075 DEBUG(0,("load_usershare_shares:: failed to open directory %s. %s\n",
4076 usersharepath, strerror(errno) ));
4077 return ret;
4080 for (num_dir_entries = 0, num_bad_dir_entries = 0, num_tmp_dir_entries = 0;
4081 (de = readdir(dp));
4082 num_dir_entries++ ) {
4083 int r;
4084 const char *n = de->d_name;
4086 /* Ignore . and .. */
4087 if (*n == '.') {
4088 if ((n[1] == '\0') || (n[1] == '.' && n[2] == '\0')) {
4089 continue;
4093 if (n[0] == ':') {
4094 /* Temporary file used when creating a share. */
4095 num_tmp_dir_entries++;
4098 /* Allow 20% tmp entries. */
4099 if (num_tmp_dir_entries > allowed_tmp_entries) {
4100 DEBUG(0,("load_usershare_shares: too many temp entries (%u) "
4101 "in directory %s\n",
4102 num_tmp_dir_entries, usersharepath));
4103 break;
4106 r = process_usershare_file(usersharepath, n, snum_template);
4107 if (r == 0) {
4108 /* Update the services count. */
4109 num_usershares++;
4110 if (num_usershares >= max_user_shares) {
4111 DEBUG(0,("load_usershare_shares: max user shares reached "
4112 "on file %s in directory %s\n",
4113 n, usersharepath ));
4114 break;
4116 } else if (r == -1) {
4117 num_bad_dir_entries++;
4120 /* Allow 20% bad entries. */
4121 if (num_bad_dir_entries > allowed_bad_entries) {
4122 DEBUG(0,("load_usershare_shares: too many bad entries (%u) "
4123 "in directory %s\n",
4124 num_bad_dir_entries, usersharepath));
4125 break;
4128 /* Allow 20% bad entries. */
4129 if (num_dir_entries > max_user_shares + allowed_bad_entries) {
4130 DEBUG(0,("load_usershare_shares: too many total entries (%u) "
4131 "in directory %s\n",
4132 num_dir_entries, usersharepath));
4133 break;
4137 closedir(dp);
4139 /* Sweep through and delete any non-refreshed usershares that are
4140 not currently in use. */
4141 tmp_ctx = talloc_stackframe();
4142 for (iService = iNumServices - 1; iService >= 0; iService--) {
4143 if (VALID(iService) && (ServicePtrs[iService]->usershare == USERSHARE_PENDING_DELETE)) {
4144 char *servname;
4146 if (snumused && snumused(sconn, iService)) {
4147 continue;
4150 servname = lp_servicename(tmp_ctx, iService);
4152 /* Remove from the share ACL db. */
4153 DEBUG(10,("load_usershare_shares: Removing deleted usershare %s\n",
4154 servname ));
4155 delete_share_security(servname);
4156 free_service_byindex(iService);
4159 talloc_free(tmp_ctx);
4161 return lp_numservices();
4164 /********************************************************
4165 Destroy global resources allocated in this file
4166 ********************************************************/
4168 void gfree_loadparm(void)
4170 int i;
4172 free_file_list();
4174 /* Free resources allocated to services */
4176 for ( i = 0; i < iNumServices; i++ ) {
4177 if ( VALID(i) ) {
4178 free_service_byindex(i);
4182 TALLOC_FREE( ServicePtrs );
4183 iNumServices = 0;
4185 /* Now release all resources allocated to global
4186 parameters and the default service */
4188 free_global_parameters();
4192 /***************************************************************************
4193 Allow client apps to specify that they are a client
4194 ***************************************************************************/
4195 static void lp_set_in_client(bool b)
4197 in_client = b;
4201 /***************************************************************************
4202 Determine if we're running in a client app
4203 ***************************************************************************/
4204 static bool lp_is_in_client(void)
4206 return in_client;
4209 /***************************************************************************
4210 Load the services array from the services file. Return true on success,
4211 false on failure.
4212 ***************************************************************************/
4214 static bool lp_load_ex(const char *pszFname,
4215 bool global_only,
4216 bool save_defaults,
4217 bool add_ipc,
4218 bool initialize_globals,
4219 bool allow_include_registry,
4220 bool load_all_shares)
4222 char *n2 = NULL;
4223 bool bRetval;
4225 bRetval = false;
4227 DEBUG(3, ("lp_load_ex: refreshing parameters\n"));
4229 bInGlobalSection = true;
4230 bGlobalOnly = global_only;
4231 bAllowIncludeRegistry = allow_include_registry;
4233 init_globals(initialize_globals);
4235 free_file_list();
4237 if (save_defaults) {
4238 init_locals();
4239 lp_save_defaults();
4242 if (!initialize_globals) {
4243 free_param_opts(&Globals.param_opt);
4244 apply_lp_set_cmdline();
4247 lp_do_parameter(-1, "idmap config * : backend", Globals.szIdmapBackend);
4249 /* We get sections first, so have to start 'behind' to make up */
4250 iServiceIndex = -1;
4252 if (lp_config_backend_is_file()) {
4253 n2 = talloc_sub_basic(talloc_tos(), get_current_username(),
4254 current_user_info.domain,
4255 pszFname);
4256 if (!n2) {
4257 smb_panic("lp_load_ex: out of memory");
4260 add_to_file_list(NULL, &file_lists, pszFname, n2);
4262 bRetval = pm_process(n2, do_section, do_parameter, NULL);
4263 TALLOC_FREE(n2);
4265 /* finish up the last section */
4266 DEBUG(4, ("pm_process() returned %s\n", BOOLSTR(bRetval)));
4267 if (bRetval) {
4268 if (iServiceIndex >= 0) {
4269 bRetval = service_ok(iServiceIndex);
4273 if (lp_config_backend_is_registry()) {
4274 /* config backend changed to registry in config file */
4276 * We need to use this extra global variable here to
4277 * survive restart: init_globals uses this as a default
4278 * for config_backend. Otherwise, init_globals would
4279 * send us into an endless loop here.
4281 config_backend = CONFIG_BACKEND_REGISTRY;
4282 /* start over */
4283 DEBUG(1, ("lp_load_ex: changing to config backend "
4284 "registry\n"));
4285 init_globals(true);
4286 lp_kill_all_services();
4287 return lp_load_ex(pszFname, global_only, save_defaults,
4288 add_ipc, initialize_globals,
4289 allow_include_registry,
4290 load_all_shares);
4292 } else if (lp_config_backend_is_registry()) {
4293 bRetval = process_registry_globals();
4294 } else {
4295 DEBUG(0, ("Illegal config backend given: %d\n",
4296 lp_config_backend()));
4297 bRetval = false;
4300 if (bRetval && lp_registry_shares()) {
4301 if (load_all_shares) {
4302 bRetval = process_registry_shares();
4303 } else {
4304 bRetval = reload_registry_shares();
4309 char *serv = lp_auto_services(talloc_tos());
4310 lp_add_auto_services(serv);
4311 TALLOC_FREE(serv);
4314 if (add_ipc) {
4315 /* When 'restrict anonymous = 2' guest connections to ipc$
4316 are denied */
4317 lp_add_ipc("IPC$", (lp_restrict_anonymous() < 2));
4318 if ( lp_enable_asu_support() ) {
4319 lp_add_ipc("ADMIN$", false);
4323 set_allowed_client_auth();
4325 if (lp_security() == SEC_ADS && strchr(lp_password_server(), ':')) {
4326 DEBUG(1, ("WARNING: The optional ':port' in password server = %s is deprecated\n",
4327 lp_password_server()));
4330 bLoaded = true;
4332 /* Now we check we_are_a_wins_server and set szWINSserver to 127.0.0.1 */
4333 /* if we_are_a_wins_server is true and we are in the client */
4334 if (lp_is_in_client() && Globals.we_are_a_wins_server) {
4335 lp_do_parameter(GLOBAL_SECTION_SNUM, "wins server", "127.0.0.1");
4338 init_iconv();
4340 fault_configure(smb_panic_s3);
4343 * We run this check once the whole smb.conf is parsed, to
4344 * force some settings for the standard way a AD DC is
4345 * operated. We may changed these as our code evolves, which
4346 * is why we force these settings.
4348 if (lp_server_role() == ROLE_ACTIVE_DIRECTORY_DC) {
4349 lp_do_parameter(-1, "passdb backend", "samba_dsdb");
4351 lp_do_parameter(-1, "rpc_server:default", "external");
4352 lp_do_parameter(-1, "rpc_server:svcctl", "embedded");
4353 lp_do_parameter(-1, "rpc_server:srvsvc", "embedded");
4354 lp_do_parameter(-1, "rpc_server:eventlog", "embedded");
4355 lp_do_parameter(-1, "rpc_server:ntsvcs", "embedded");
4356 lp_do_parameter(-1, "rpc_server:winreg", "embedded");
4357 lp_do_parameter(-1, "rpc_server:spoolss", "embedded");
4358 lp_do_parameter(-1, "rpc_daemon:spoolssd", "embedded");
4359 lp_do_parameter(-1, "rpc_server:tcpip", "no");
4362 bAllowIncludeRegistry = true;
4364 return (bRetval);
4367 bool lp_load(const char *pszFname,
4368 bool global_only,
4369 bool save_defaults,
4370 bool add_ipc,
4371 bool initialize_globals)
4373 return lp_load_ex(pszFname,
4374 global_only,
4375 save_defaults,
4376 add_ipc,
4377 initialize_globals,
4378 true, /* allow_include_registry */
4379 false); /* load_all_shares*/
4382 bool lp_load_initial_only(const char *pszFname)
4384 return lp_load_ex(pszFname,
4385 true, /* global only */
4386 false, /* save_defaults */
4387 false, /* add_ipc */
4388 true, /* initialize_globals */
4389 false, /* allow_include_registry */
4390 false); /* load_all_shares*/
4394 * most common lp_load wrapper, loading only the globals
4396 bool lp_load_global(const char *file_name)
4398 return lp_load_ex(file_name,
4399 true, /* global_only */
4400 false, /* save_defaults */
4401 false, /* add_ipc */
4402 true, /* initialize_globals */
4403 true, /* allow_include_registry */
4404 false); /* load_all_shares*/
4408 * lp_load wrapper, especially for clients
4410 bool lp_load_client(const char *file_name)
4412 lp_set_in_client(true);
4414 return lp_load_global(file_name);
4418 * lp_load wrapper, loading only globals, but intended
4419 * for subsequent calls, not reinitializing the globals
4420 * to default values
4422 bool lp_load_global_no_reinit(const char *file_name)
4424 return lp_load_ex(file_name,
4425 true, /* global_only */
4426 false, /* save_defaults */
4427 false, /* add_ipc */
4428 false, /* initialize_globals */
4429 true, /* allow_include_registry */
4430 false); /* load_all_shares*/
4434 * lp_load wrapper, especially for clients, no reinitialization
4436 bool lp_load_client_no_reinit(const char *file_name)
4438 lp_set_in_client(true);
4440 return lp_load_global_no_reinit(file_name);
4443 bool lp_load_with_registry_shares(const char *pszFname,
4444 bool global_only,
4445 bool save_defaults,
4446 bool add_ipc,
4447 bool initialize_globals)
4449 return lp_load_ex(pszFname,
4450 global_only,
4451 save_defaults,
4452 add_ipc,
4453 initialize_globals,
4454 true, /* allow_include_registry */
4455 true); /* load_all_shares*/
4458 /***************************************************************************
4459 Return the max number of services.
4460 ***************************************************************************/
4462 int lp_numservices(void)
4464 return (iNumServices);
4467 /***************************************************************************
4468 Display the contents of the services array in human-readable form.
4469 ***************************************************************************/
4471 void lp_dump(FILE *f, bool show_defaults, int maxtoprint)
4473 int iService;
4475 if (show_defaults)
4476 defaults_saved = false;
4478 dump_globals(f);
4480 dump_a_service(&sDefault, f);
4482 for (iService = 0; iService < maxtoprint; iService++) {
4483 fprintf(f,"\n");
4484 lp_dump_one(f, show_defaults, iService);
4488 /***************************************************************************
4489 Display the contents of one service in human-readable form.
4490 ***************************************************************************/
4492 void lp_dump_one(FILE * f, bool show_defaults, int snum)
4494 if (VALID(snum)) {
4495 if (ServicePtrs[snum]->szService[0] == '\0')
4496 return;
4497 dump_a_service(ServicePtrs[snum], f);
4501 /***************************************************************************
4502 Return the number of the service with the given name, or -1 if it doesn't
4503 exist. Note that this is a DIFFERENT ANIMAL from the internal function
4504 getservicebyname()! This works ONLY if all services have been loaded, and
4505 does not copy the found service.
4506 ***************************************************************************/
4508 int lp_servicenumber(const char *pszServiceName)
4510 int iService;
4511 fstring serviceName;
4513 if (!pszServiceName) {
4514 return GLOBAL_SECTION_SNUM;
4517 for (iService = iNumServices - 1; iService >= 0; iService--) {
4518 if (VALID(iService) && ServicePtrs[iService]->szService) {
4520 * The substitution here is used to support %U is
4521 * service names
4523 fstrcpy(serviceName, ServicePtrs[iService]->szService);
4524 standard_sub_basic(get_current_username(),
4525 current_user_info.domain,
4526 serviceName,sizeof(serviceName));
4527 if (strequal(serviceName, pszServiceName)) {
4528 break;
4533 if (iService >= 0 && ServicePtrs[iService]->usershare == USERSHARE_VALID) {
4534 struct timespec last_mod;
4536 if (!usershare_exists(iService, &last_mod)) {
4537 /* Remove the share security tdb entry for it. */
4538 delete_share_security(lp_servicename(talloc_tos(), iService));
4539 /* Remove it from the array. */
4540 free_service_byindex(iService);
4541 /* Doesn't exist anymore. */
4542 return GLOBAL_SECTION_SNUM;
4545 /* Has it been modified ? If so delete and reload. */
4546 if (timespec_compare(&ServicePtrs[iService]->usershare_last_mod,
4547 &last_mod) < 0) {
4548 /* Remove it from the array. */
4549 free_service_byindex(iService);
4550 /* and now reload it. */
4551 iService = load_usershare_service(pszServiceName);
4555 if (iService < 0) {
4556 DEBUG(7,("lp_servicenumber: couldn't find %s\n", pszServiceName));
4557 return GLOBAL_SECTION_SNUM;
4560 return (iService);
4563 /*******************************************************************
4564 A useful volume label function.
4565 ********************************************************************/
4567 const char *volume_label(TALLOC_CTX *ctx, int snum)
4569 char *ret;
4570 const char *label = lp_volume(ctx, snum);
4571 if (!*label) {
4572 label = lp_servicename(ctx, snum);
4575 /* This returns a 33 byte guarenteed null terminated string. */
4576 ret = talloc_strndup(ctx, label, 32);
4577 if (!ret) {
4578 return "";
4580 return ret;
4583 /*******************************************************************
4584 Get the default server type we will announce as via nmbd.
4585 ********************************************************************/
4587 int lp_default_server_announce(void)
4589 int default_server_announce = 0;
4590 default_server_announce |= SV_TYPE_WORKSTATION;
4591 default_server_announce |= SV_TYPE_SERVER;
4592 default_server_announce |= SV_TYPE_SERVER_UNIX;
4594 /* note that the flag should be set only if we have a
4595 printer service but nmbd doesn't actually load the
4596 services so we can't tell --jerry */
4598 default_server_announce |= SV_TYPE_PRINTQ_SERVER;
4600 default_server_announce |= SV_TYPE_SERVER_NT;
4601 default_server_announce |= SV_TYPE_NT;
4603 switch (lp_server_role()) {
4604 case ROLE_DOMAIN_MEMBER:
4605 default_server_announce |= SV_TYPE_DOMAIN_MEMBER;
4606 break;
4607 case ROLE_DOMAIN_PDC:
4608 default_server_announce |= SV_TYPE_DOMAIN_CTRL;
4609 break;
4610 case ROLE_DOMAIN_BDC:
4611 default_server_announce |= SV_TYPE_DOMAIN_BAKCTRL;
4612 break;
4613 case ROLE_STANDALONE:
4614 default:
4615 break;
4617 if (lp_time_server())
4618 default_server_announce |= SV_TYPE_TIME_SOURCE;
4620 if (lp_host_msdfs())
4621 default_server_announce |= SV_TYPE_DFS_SERVER;
4623 return default_server_announce;
4626 /***********************************************************
4627 If we are PDC then prefer us as DMB
4628 ************************************************************/
4630 bool lp_domain_master(void)
4632 if (Globals._domain_master == Auto)
4633 return (lp_server_role() == ROLE_DOMAIN_PDC);
4635 return (bool)Globals._domain_master;
4638 /***********************************************************
4639 If we are PDC then prefer us as DMB
4640 ************************************************************/
4642 static bool lp_domain_master_true_or_auto(void)
4644 if (Globals._domain_master) /* auto or yes */
4645 return true;
4647 return false;
4650 /***********************************************************
4651 If we are DMB then prefer us as LMB
4652 ************************************************************/
4654 bool lp_preferred_master(void)
4656 if (Globals.iPreferredMaster == Auto)
4657 return (lp_local_master() && lp_domain_master());
4659 return (bool)Globals.iPreferredMaster;
4662 /*******************************************************************
4663 Remove a service.
4664 ********************************************************************/
4666 void lp_remove_service(int snum)
4668 ServicePtrs[snum]->valid = false;
4671 const char *lp_printername(TALLOC_CTX *ctx, int snum)
4673 const char *ret = lp__printername(ctx, snum);
4674 if (ret == NULL || *ret == '\0') {
4675 ret = lp_const_servicename(snum);
4678 return ret;
4682 /***********************************************************
4683 Allow daemons such as winbindd to fix their logfile name.
4684 ************************************************************/
4686 void lp_set_logfile(const char *name)
4688 string_set(Globals.ctx, &Globals.logfile, name);
4689 debug_set_logfile(name);
4692 /*******************************************************************
4693 Return the max print jobs per queue.
4694 ********************************************************************/
4696 int lp_maxprintjobs(int snum)
4698 int maxjobs = LP_SNUM_OK(snum) ? ServicePtrs[snum]->iMaxPrintJobs : sDefault.iMaxPrintJobs;
4699 if (maxjobs <= 0 || maxjobs >= PRINT_MAX_JOBID)
4700 maxjobs = PRINT_MAX_JOBID - 1;
4702 return maxjobs;
4705 const char *lp_printcapname(void)
4707 if ((Globals.szPrintcapname != NULL) &&
4708 (Globals.szPrintcapname[0] != '\0'))
4709 return Globals.szPrintcapname;
4711 if (sDefault.printing == PRINT_CUPS) {
4712 return "cups";
4715 if (sDefault.printing == PRINT_BSD)
4716 return "/etc/printcap";
4718 return PRINTCAP_NAME;
4721 static uint32 spoolss_state;
4723 bool lp_disable_spoolss( void )
4725 if ( spoolss_state == SVCCTL_STATE_UNKNOWN )
4726 spoolss_state = lp__disable_spoolss() ? SVCCTL_STOPPED : SVCCTL_RUNNING;
4728 return spoolss_state == SVCCTL_STOPPED ? true : false;
4731 void lp_set_spoolss_state( uint32 state )
4733 SMB_ASSERT( (state == SVCCTL_STOPPED) || (state == SVCCTL_RUNNING) );
4735 spoolss_state = state;
4738 uint32 lp_get_spoolss_state( void )
4740 return lp_disable_spoolss() ? SVCCTL_STOPPED : SVCCTL_RUNNING;
4743 /*******************************************************************
4744 Ensure we don't use sendfile if server smb signing is active.
4745 ********************************************************************/
4747 bool lp_use_sendfile(int snum, struct smb_signing_state *signing_state)
4749 bool sign_active = false;
4751 /* Using sendfile blows the brains out of any DOS or Win9x TCP stack... JRA. */
4752 if (get_Protocol() < PROTOCOL_NT1) {
4753 return false;
4755 if (signing_state) {
4756 sign_active = smb_signing_is_active(signing_state);
4758 return (lp__use_sendfile(snum) &&
4759 (get_remote_arch() != RA_WIN95) &&
4760 !sign_active);
4763 /*******************************************************************
4764 Turn off sendfile if we find the underlying OS doesn't support it.
4765 ********************************************************************/
4767 void set_use_sendfile(int snum, bool val)
4769 if (LP_SNUM_OK(snum))
4770 ServicePtrs[snum]->_use_sendfile = val;
4771 else
4772 sDefault._use_sendfile = val;
4775 /*******************************************************************
4776 Turn off storing DOS attributes if this share doesn't support it.
4777 ********************************************************************/
4779 void set_store_dos_attributes(int snum, bool val)
4781 if (!LP_SNUM_OK(snum))
4782 return;
4783 ServicePtrs[(snum)]->store_dos_attributes = val;
4786 void lp_set_mangling_method(const char *new_method)
4788 string_set(Globals.ctx, &Globals.mangling_method, new_method);
4791 /*******************************************************************
4792 Global state for POSIX pathname processing.
4793 ********************************************************************/
4795 static bool posix_pathnames;
4797 bool lp_posix_pathnames(void)
4799 return posix_pathnames;
4802 /*******************************************************************
4803 Change everything needed to ensure POSIX pathname processing (currently
4804 not much).
4805 ********************************************************************/
4807 void lp_set_posix_pathnames(void)
4809 posix_pathnames = true;
4812 /*******************************************************************
4813 Global state for POSIX lock processing - CIFS unix extensions.
4814 ********************************************************************/
4816 bool posix_default_lock_was_set;
4817 static enum brl_flavour posix_cifsx_locktype; /* By default 0 == WINDOWS_LOCK */
4819 enum brl_flavour lp_posix_cifsu_locktype(files_struct *fsp)
4821 if (posix_default_lock_was_set) {
4822 return posix_cifsx_locktype;
4823 } else {
4824 return fsp->posix_open ? POSIX_LOCK : WINDOWS_LOCK;
4828 /*******************************************************************
4829 ********************************************************************/
4831 void lp_set_posix_default_cifsx_readwrite_locktype(enum brl_flavour val)
4833 posix_default_lock_was_set = true;
4834 posix_cifsx_locktype = val;
4837 int lp_min_receive_file_size(void)
4839 if (Globals.iminreceivefile < 0) {
4840 return 0;
4842 return Globals.iminreceivefile;
4845 /*******************************************************************
4846 Safe wide links checks.
4847 This helper function always verify the validity of wide links,
4848 even after a configuration file reload.
4849 ********************************************************************/
4851 static bool lp_widelinks_internal(int snum)
4853 return (bool)(LP_SNUM_OK(snum)? ServicePtrs[(snum)]->bWidelinks :
4854 sDefault.bWidelinks);
4857 void widelinks_warning(int snum)
4859 if (lp_allow_insecure_wide_links()) {
4860 return;
4863 if (lp_unix_extensions() && lp_widelinks_internal(snum)) {
4864 DEBUG(0,("Share '%s' has wide links and unix extensions enabled. "
4865 "These parameters are incompatible. "
4866 "Wide links will be disabled for this share.\n",
4867 lp_servicename(talloc_tos(), snum) ));
4871 bool lp_widelinks(int snum)
4873 /* wide links is always incompatible with unix extensions */
4874 if (lp_unix_extensions()) {
4876 * Unless we have "allow insecure widelinks"
4877 * turned on.
4879 if (!lp_allow_insecure_wide_links()) {
4880 return false;
4884 return lp_widelinks_internal(snum);
4887 int lp_server_role(void)
4889 return lp_find_server_role(lp__server_role(),
4890 lp__security(),
4891 lp__domain_logons(),
4892 lp_domain_master_true_or_auto());
4895 int lp_security(void)
4897 return lp_find_security(lp__server_role(),
4898 lp__security());
4901 struct loadparm_global * get_globals(void)
4903 return &Globals;