smbd: Fix crossing automounter mount points
[Samba.git] / source3 / libsmb / libsmb_context.c
blob97a4430fd6a01069f836230e82cd9ed269a0ada5
1 /*
2 Unix SMB/Netbios implementation.
3 SMB client library implementation
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Richard Sharpe 2000, 2002
6 Copyright (C) John Terpstra 2000
7 Copyright (C) Tom Jansen (Ninja ISD) 2002
8 Copyright (C) Derrell Lipman 2003-2008
9 Copyright (C) Jeremy Allison 2007, 2008
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "includes.h"
26 #include "libsmb/libsmb.h"
27 #include "libsmbclient.h"
28 #include "libsmb_internal.h"
29 #include "secrets.h"
30 #include "../libcli/smb/smbXcli_base.h"
31 #include "auth/credentials/credentials.h"
32 #include "auth/gensec/gensec.h"
33 #include "lib/param/param.h"
34 #include "../lib/util/smb_threads.h"
35 #include "../lib/util/smb_threads_internal.h"
38 * Is the logging working / configfile read ?
40 static bool SMBC_initialized = false;
41 static unsigned int initialized_ctx_count = 0;
42 static void *initialized_ctx_count_mutex = NULL;
45 * Do some module- and library-wide initializations
47 static void
48 SMBC_module_init(void * punused)
50 bool conf_loaded = False;
51 char *home = NULL;
52 TALLOC_CTX *frame = talloc_stackframe();
54 setup_logging("libsmbclient", DEBUG_STDOUT);
56 /* Here we would open the smb.conf file if needed ... */
58 home = getenv("HOME");
59 if (home) {
60 char *conf = NULL;
61 if (asprintf(&conf, "%s/.smb/smb.conf", home) > 0) {
62 if (lp_load_client(conf)) {
63 conf_loaded = True;
64 } else {
65 DEBUG(5, ("Could not load config file: %s\n",
66 conf));
68 SAFE_FREE(conf);
72 if (!conf_loaded) {
74 * Well, if that failed, try the get_dyn_CONFIGFILE
75 * Which points to the standard locn, and if that
76 * fails, silently ignore it and use the internal
77 * defaults ...
80 if (!lp_load_client(get_dyn_CONFIGFILE())) {
81 DEBUG(5, ("Could not load config file: %s\n",
82 get_dyn_CONFIGFILE()));
83 } else if (home) {
84 char *conf;
86 * We loaded the global config file. Now lets
87 * load user-specific modifications to the
88 * global config.
90 if (asprintf(&conf,
91 "%s/.smb/smb.conf.append",
92 home) > 0) {
93 if (!lp_load_client_no_reinit(conf)) {
94 DEBUG(10,
95 ("Could not append config file: "
96 "%s\n",
97 conf));
99 SAFE_FREE(conf);
104 load_interfaces(); /* Load the list of interfaces ... */
106 reopen_logs(); /* Get logging working ... */
109 * Block SIGPIPE (from lib/util_sock.c: write())
110 * It is not needed and should not stop execution
112 BlockSignals(True, SIGPIPE);
114 /* Create the mutex we'll use to protect initialized_ctx_count */
115 if (SMB_THREAD_CREATE_MUTEX("initialized_ctx_count_mutex",
116 initialized_ctx_count_mutex) != 0) {
117 smb_panic("SMBC_module_init: "
118 "failed to create 'initialized_ctx_count' mutex");
121 TALLOC_FREE(frame);
125 static void
126 SMBC_module_terminate(void)
128 TALLOC_CTX *frame = talloc_stackframe();
129 secrets_shutdown();
130 gfree_all();
131 SMBC_initialized = false;
132 TALLOC_FREE(frame);
137 * Get a new empty handle to fill in with your own info
139 SMBCCTX *
140 smbc_new_context(void)
142 SMBCCTX *context;
143 TALLOC_CTX *frame = talloc_stackframe();
145 /* The first call to this function should initialize the module */
146 SMB_THREAD_ONCE(&SMBC_initialized, SMBC_module_init, NULL);
149 * All newly added context fields should be placed in
150 * SMBC_internal_data, not directly in SMBCCTX.
152 context = SMB_MALLOC_P(SMBCCTX);
153 if (!context) {
154 TALLOC_FREE(frame);
155 errno = ENOMEM;
156 return NULL;
159 ZERO_STRUCTP(context);
161 context->internal = SMB_MALLOC_P(struct SMBC_internal_data);
162 if (!context->internal) {
163 TALLOC_FREE(frame);
164 SAFE_FREE(context);
165 errno = ENOMEM;
166 return NULL;
169 /* Initialize the context and establish reasonable defaults */
170 ZERO_STRUCTP(context->internal);
172 context->internal->lp_ctx = loadparm_init_s3(NULL,
173 loadparm_s3_helpers());
174 if (context->internal->lp_ctx == NULL) {
175 SAFE_FREE(context->internal);
176 SAFE_FREE(context);
177 TALLOC_FREE(frame);
178 errno = ENOMEM;
179 return NULL;
182 smbc_setDebug(context, 0);
183 smbc_setTimeout(context, 20000);
184 smbc_setPort(context, 0);
186 smbc_setOptionFullTimeNames(context, False);
187 smbc_setOptionOpenShareMode(context, SMBC_SHAREMODE_DENY_NONE);
188 smbc_setOptionSmbEncryptionLevel(context, SMBC_ENCRYPTLEVEL_DEFAULT);
189 smbc_setOptionUseCCache(context, True);
190 smbc_setOptionCaseSensitive(context, False);
191 smbc_setOptionBrowseMaxLmbCount(context, 3); /* # LMBs to query */
192 smbc_setOptionUrlEncodeReaddirEntries(context, False);
193 smbc_setOptionOneSharePerServer(context, False);
194 smbc_setOptionPosixExtensions(context, false);
195 if (getenv("LIBSMBCLIENT_NO_CCACHE") != NULL) {
196 smbc_setOptionUseCCache(context, false);
199 smbc_setFunctionAuthData(context, SMBC_get_auth_data);
200 smbc_setFunctionCheckServer(context, SMBC_check_server);
201 smbc_setFunctionRemoveUnusedServer(context, SMBC_remove_unused_server);
203 smbc_setOptionUserData(context, NULL);
204 smbc_setFunctionAddCachedServer(context, SMBC_add_cached_server);
205 smbc_setFunctionGetCachedServer(context, SMBC_get_cached_server);
206 smbc_setFunctionRemoveCachedServer(context, SMBC_remove_cached_server);
207 smbc_setFunctionPurgeCachedServers(context, SMBC_purge_cached_servers);
209 smbc_setFunctionOpen(context, SMBC_open_ctx);
210 smbc_setFunctionCreat(context, SMBC_creat_ctx);
211 smbc_setFunctionRead(context, SMBC_read_ctx);
212 smbc_setFunctionSplice(context, SMBC_splice_ctx);
213 smbc_setFunctionWrite(context, SMBC_write_ctx);
214 smbc_setFunctionClose(context, SMBC_close_ctx);
215 smbc_setFunctionUnlink(context, SMBC_unlink_ctx);
216 smbc_setFunctionRename(context, SMBC_rename_ctx);
217 smbc_setFunctionLseek(context, SMBC_lseek_ctx);
218 smbc_setFunctionFtruncate(context, SMBC_ftruncate_ctx);
219 smbc_setFunctionStat(context, SMBC_stat_ctx);
220 smbc_setFunctionStatVFS(context, SMBC_statvfs_ctx);
221 smbc_setFunctionFstatVFS(context, SMBC_fstatvfs_ctx);
222 smbc_setFunctionFstat(context, SMBC_fstat_ctx);
223 smbc_setFunctionOpendir(context, SMBC_opendir_ctx);
224 smbc_setFunctionClosedir(context, SMBC_closedir_ctx);
225 smbc_setFunctionReaddir(context, SMBC_readdir_ctx);
226 smbc_setFunctionReaddirPlus(context, SMBC_readdirplus_ctx);
227 smbc_setFunctionReaddirPlus2(context, SMBC_readdirplus2_ctx);
228 smbc_setFunctionGetdents(context, SMBC_getdents_ctx);
229 smbc_setFunctionMkdir(context, SMBC_mkdir_ctx);
230 smbc_setFunctionRmdir(context, SMBC_rmdir_ctx);
231 smbc_setFunctionTelldir(context, SMBC_telldir_ctx);
232 smbc_setFunctionLseekdir(context, SMBC_lseekdir_ctx);
233 smbc_setFunctionFstatdir(context, SMBC_fstatdir_ctx);
234 smbc_setFunctionNotify(context, SMBC_notify_ctx);
235 smbc_setFunctionChmod(context, SMBC_chmod_ctx);
236 smbc_setFunctionUtimes(context, SMBC_utimes_ctx);
237 smbc_setFunctionSetxattr(context, SMBC_setxattr_ctx);
238 smbc_setFunctionGetxattr(context, SMBC_getxattr_ctx);
239 smbc_setFunctionRemovexattr(context, SMBC_removexattr_ctx);
240 smbc_setFunctionListxattr(context, SMBC_listxattr_ctx);
242 smbc_setFunctionOpenPrintJob(context, SMBC_open_print_job_ctx);
243 smbc_setFunctionPrintFile(context, SMBC_print_file_ctx);
244 smbc_setFunctionListPrintJobs(context, SMBC_list_print_jobs_ctx);
245 smbc_setFunctionUnlinkPrintJob(context, SMBC_unlink_print_job_ctx);
247 TALLOC_FREE(frame);
248 return context;
252 * Free a context
254 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
255 * and thus you'll be leaking memory if not handled properly.
259 smbc_free_context(SMBCCTX *context,
260 int shutdown_ctx)
262 TALLOC_CTX *frame;
263 if (!context) {
264 errno = EBADF;
265 return 1;
268 frame = talloc_stackframe();
270 if (shutdown_ctx) {
271 SMBCFILE * f;
272 DEBUG(1,("Performing aggressive shutdown.\n"));
274 f = context->internal->files;
275 while (f) {
276 SMBCFILE *next = f->next;
277 smbc_getFunctionClose(context)(context, f);
278 f = next;
280 context->internal->files = NULL;
282 /* First try to remove the servers the nice way. */
283 if (smbc_getFunctionPurgeCachedServers(context)(context)) {
284 SMBCSRV * s;
285 SMBCSRV * next;
286 DEBUG(1, ("Could not purge all servers, "
287 "Nice way shutdown failed.\n"));
288 s = context->internal->servers;
289 while (s) {
290 DEBUG(1, ("Forced shutdown: %p (cli=%p)\n",
291 s, s->cli));
292 cli_shutdown(s->cli);
293 smbc_getFunctionRemoveCachedServer(context)(context,
295 next = s->next;
296 DLIST_REMOVE(context->internal->servers, s);
297 SAFE_FREE(s);
298 s = next;
300 context->internal->servers = NULL;
303 else {
304 /* This is the polite way */
305 if (smbc_getFunctionPurgeCachedServers(context)(context)) {
306 DEBUG(1, ("Could not purge all servers, "
307 "free_context failed.\n"));
308 errno = EBUSY;
309 TALLOC_FREE(frame);
310 return 1;
312 if (context->internal->servers) {
313 DEBUG(1, ("Active servers in context, "
314 "free_context failed.\n"));
315 errno = EBUSY;
316 TALLOC_FREE(frame);
317 return 1;
319 if (context->internal->files) {
320 DEBUG(1, ("Active files in context, "
321 "free_context failed.\n"));
322 errno = EBUSY;
323 TALLOC_FREE(frame);
324 return 1;
328 /* Things we have to clean up */
329 smbc_setWorkgroup(context, NULL);
330 smbc_setNetbiosName(context, NULL);
331 smbc_setUser(context, NULL);
333 DEBUG(3, ("Context %p successfully freed\n", context));
335 /* Free any DFS auth context. */
336 TALLOC_FREE(context->internal->creds);
338 TALLOC_FREE(context->internal->lp_ctx);
339 SAFE_FREE(context->internal);
340 SAFE_FREE(context);
342 /* Protect access to the count of contexts in use */
343 if (SMB_THREAD_LOCK(initialized_ctx_count_mutex) != 0) {
344 smb_panic("error locking 'initialized_ctx_count'");
347 if (initialized_ctx_count) {
348 initialized_ctx_count--;
351 if (initialized_ctx_count == 0) {
352 SMBC_module_terminate();
355 /* Unlock the mutex */
356 if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex) != 0) {
357 smb_panic("error unlocking 'initialized_ctx_count'");
360 TALLOC_FREE(frame);
361 return 0;
366 * Deprecated interface. Do not use. Instead, use the various
367 * smbc_setOption*() functions or smbc_setFunctionAuthDataWithContext().
369 void
370 smbc_option_set(SMBCCTX *context,
371 char *option_name,
372 ... /* option_value */)
374 va_list ap;
375 union {
376 int i;
377 bool b;
378 smbc_get_auth_data_with_context_fn auth_fn;
379 void *v;
380 const char *s;
381 } option_value;
383 TALLOC_CTX *frame = talloc_stackframe();
385 va_start(ap, option_name);
387 if (strcmp(option_name, "debug_to_stderr") == 0) {
388 option_value.b = (bool) va_arg(ap, int);
389 smbc_setOptionDebugToStderr(context, option_value.b);
391 } else if (strcmp(option_name, "full_time_names") == 0) {
392 option_value.b = (bool) va_arg(ap, int);
393 smbc_setOptionFullTimeNames(context, option_value.b);
395 } else if (strcmp(option_name, "open_share_mode") == 0) {
396 option_value.i = va_arg(ap, int);
397 smbc_setOptionOpenShareMode(context, option_value.i);
399 } else if (strcmp(option_name, "auth_function") == 0) {
400 option_value.auth_fn =
401 va_arg(ap, smbc_get_auth_data_with_context_fn);
402 smbc_setFunctionAuthDataWithContext(context, option_value.auth_fn);
404 } else if (strcmp(option_name, "user_data") == 0) {
405 option_value.v = va_arg(ap, void *);
406 smbc_setOptionUserData(context, option_value.v);
408 } else if (strcmp(option_name, "smb_encrypt_level") == 0) {
409 option_value.s = va_arg(ap, const char *);
410 if (strcmp(option_value.s, "none") == 0) {
411 smbc_setOptionSmbEncryptionLevel(context,
412 SMBC_ENCRYPTLEVEL_NONE);
413 } else if (strcmp(option_value.s, "request") == 0) {
414 smbc_setOptionSmbEncryptionLevel(context,
415 SMBC_ENCRYPTLEVEL_REQUEST);
416 } else if (strcmp(option_value.s, "require") == 0) {
417 smbc_setOptionSmbEncryptionLevel(context,
418 SMBC_ENCRYPTLEVEL_REQUIRE);
421 } else if (strcmp(option_name, "browse_max_lmb_count") == 0) {
422 option_value.i = va_arg(ap, int);
423 smbc_setOptionBrowseMaxLmbCount(context, option_value.i);
425 } else if (strcmp(option_name, "urlencode_readdir_entries") == 0) {
426 option_value.b = (bool) va_arg(ap, int);
427 smbc_setOptionUrlEncodeReaddirEntries(context, option_value.b);
429 } else if (strcmp(option_name, "one_share_per_server") == 0) {
430 option_value.b = (bool) va_arg(ap, int);
431 smbc_setOptionOneSharePerServer(context, option_value.b);
433 } else if (strcmp(option_name, "use_kerberos") == 0) {
434 option_value.b = (bool) va_arg(ap, int);
435 smbc_setOptionUseKerberos(context, option_value.b);
437 } else if (strcmp(option_name, "fallback_after_kerberos") == 0) {
438 option_value.b = (bool) va_arg(ap, int);
439 smbc_setOptionFallbackAfterKerberos(context, option_value.b);
441 } else if (strcmp(option_name, "use_ccache") == 0) {
442 option_value.b = (bool) va_arg(ap, int);
443 smbc_setOptionUseCCache(context, option_value.b);
445 } else if (strcmp(option_name, "no_auto_anonymous_login") == 0) {
446 option_value.b = (bool) va_arg(ap, int);
447 smbc_setOptionNoAutoAnonymousLogin(context, option_value.b);
450 va_end(ap);
451 TALLOC_FREE(frame);
456 * Deprecated interface. Do not use. Instead, use the various
457 * smbc_getOption*() functions.
459 void *
460 smbc_option_get(SMBCCTX *context,
461 char *option_name)
463 if (strcmp(option_name, "debug_stderr") == 0) {
464 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
465 return (void *) (intptr_t) smbc_getOptionDebugToStderr(context);
466 #else
467 return (void *) smbc_getOptionDebugToStderr(context);
468 #endif
470 } else if (strcmp(option_name, "full_time_names") == 0) {
471 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
472 return (void *) (intptr_t) smbc_getOptionFullTimeNames(context);
473 #else
474 return (void *) smbc_getOptionFullTimeNames(context);
475 #endif
477 } else if (strcmp(option_name, "open_share_mode") == 0) {
478 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
479 return (void *) (intptr_t) smbc_getOptionOpenShareMode(context);
480 #else
481 return (void *) smbc_getOptionOpenShareMode(context);
482 #endif
484 } else if (strcmp(option_name, "auth_function") == 0) {
485 return (void *) smbc_getFunctionAuthDataWithContext(context);
487 } else if (strcmp(option_name, "user_data") == 0) {
488 return smbc_getOptionUserData(context);
490 } else if (strcmp(option_name, "smb_encrypt_level") == 0) {
491 switch(smbc_getOptionSmbEncryptionLevel(context))
493 case SMBC_ENCRYPTLEVEL_DEFAULT:
494 return discard_const_p(void, "default");
495 case 0:
496 return discard_const_p(void, "none");
497 case 1:
498 return discard_const_p(void, "request");
499 case 2:
500 return discard_const_p(void, "require");
503 } else if (strcmp(option_name, "smb_encrypt_on") == 0) {
504 SMBCSRV *s;
505 unsigned int num_servers = 0;
507 for (s = context->internal->servers; s; s = s->next) {
508 num_servers++;
509 if (!cli_state_is_encryption_on(s->cli)) {
510 return (void *)false;
513 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
514 return (void *) (intptr_t) (bool) (num_servers > 0);
515 #else
516 return (void *) (bool) (num_servers > 0);
517 #endif
519 } else if (strcmp(option_name, "browse_max_lmb_count") == 0) {
520 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
521 return (void *) (intptr_t) smbc_getOptionBrowseMaxLmbCount(context);
522 #else
523 return (void *) smbc_getOptionBrowseMaxLmbCount(context);
524 #endif
526 } else if (strcmp(option_name, "urlencode_readdir_entries") == 0) {
527 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
528 return (void *)(intptr_t) smbc_getOptionUrlEncodeReaddirEntries(context);
529 #else
530 return (void *) (bool) smbc_getOptionUrlEncodeReaddirEntries(context);
531 #endif
533 } else if (strcmp(option_name, "one_share_per_server") == 0) {
534 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
535 return (void *) (intptr_t) smbc_getOptionOneSharePerServer(context);
536 #else
537 return (void *) (bool) smbc_getOptionOneSharePerServer(context);
538 #endif
540 } else if (strcmp(option_name, "use_kerberos") == 0) {
541 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
542 return (void *) (intptr_t) smbc_getOptionUseKerberos(context);
543 #else
544 return (void *) (bool) smbc_getOptionUseKerberos(context);
545 #endif
547 } else if (strcmp(option_name, "fallback_after_kerberos") == 0) {
548 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
549 return (void *)(intptr_t) smbc_getOptionFallbackAfterKerberos(context);
550 #else
551 return (void *) (bool) smbc_getOptionFallbackAfterKerberos(context);
552 #endif
554 } else if (strcmp(option_name, "use_ccache") == 0) {
555 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
556 return (void *) (intptr_t) smbc_getOptionUseCCache(context);
557 #else
558 return (void *) (bool) smbc_getOptionUseCCache(context);
559 #endif
561 } else if (strcmp(option_name, "no_auto_anonymous_login") == 0) {
562 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
563 return (void *) (intptr_t) smbc_getOptionNoAutoAnonymousLogin(context);
564 #else
565 return (void *) (bool) smbc_getOptionNoAutoAnonymousLogin(context);
566 #endif
569 return NULL;
574 * Initialize the library, etc.
576 * We accept a struct containing handle information.
577 * valid values for info->debug from 0 to 100,
578 * and insist that info->fn must be non-null.
580 SMBCCTX *
581 smbc_init_context(SMBCCTX *context)
583 int pid;
584 TALLOC_CTX *frame;
586 if (!context) {
587 errno = EBADF;
588 return NULL;
591 /* Do not initialise the same client twice */
592 if (context->internal->initialized) {
593 return NULL;
596 frame = talloc_stackframe();
598 if ((!smbc_getFunctionAuthData(context) &&
599 !smbc_getFunctionAuthDataWithContext(context)) ||
600 smbc_getDebug(context) < 0 ||
601 smbc_getDebug(context) > 100) {
603 TALLOC_FREE(frame);
604 errno = EINVAL;
605 return NULL;
609 if (!smbc_getUser(context)) {
611 * FIXME: Is this the best way to get the user info?
613 char *user = getenv("USER");
614 /* walk around as "guest" if no username can be found */
615 if (!user) {
616 user = SMB_STRDUP("guest");
617 } else {
618 user = SMB_STRDUP(user);
621 if (!user) {
622 TALLOC_FREE(frame);
623 errno = ENOMEM;
624 return NULL;
627 smbc_setUser(context, user);
628 SAFE_FREE(user);
630 if (!smbc_getUser(context)) {
631 TALLOC_FREE(frame);
632 errno = ENOMEM;
633 return NULL;
637 if (!smbc_getNetbiosName(context)) {
639 * We try to get our netbios name from the config. If that
640 * fails we fall back on constructing our netbios name from
641 * our hostname etc
643 char *netbios_name;
644 if (lp_netbios_name()) {
645 netbios_name = SMB_STRDUP(lp_netbios_name());
646 } else {
648 * Hmmm, I want to get hostname as well, but I am too
649 * lazy for the moment
651 pid = getpid();
652 netbios_name = (char *)SMB_MALLOC(17);
653 if (!netbios_name) {
654 TALLOC_FREE(frame);
655 errno = ENOMEM;
656 return NULL;
658 slprintf(netbios_name, 16,
659 "smbc%s%d", smbc_getUser(context), pid);
662 if (!netbios_name) {
663 TALLOC_FREE(frame);
664 errno = ENOMEM;
665 return NULL;
668 smbc_setNetbiosName(context, netbios_name);
669 SAFE_FREE(netbios_name);
671 if (!smbc_getNetbiosName(context)) {
672 TALLOC_FREE(frame);
673 errno = ENOMEM;
674 return NULL;
678 DEBUG(1, ("Using netbios name %s.\n", smbc_getNetbiosName(context)));
680 if (!smbc_getWorkgroup(context)) {
681 const char *workgroup;
683 if (lp_workgroup()) {
684 workgroup = lp_workgroup();
685 } else {
686 /* TODO: Think about a decent default workgroup */
687 workgroup = "samba";
690 smbc_setWorkgroup(context, workgroup);
692 if (!smbc_getWorkgroup(context)) {
693 TALLOC_FREE(frame);
694 errno = ENOMEM;
695 return NULL;
699 DEBUG(1, ("Using workgroup %s.\n", smbc_getWorkgroup(context)));
701 /* shortest timeout is 1 second */
702 if (smbc_getTimeout(context) > 0 && smbc_getTimeout(context) < 1000)
703 smbc_setTimeout(context, 1000);
705 context->internal->initialized = True;
707 /* Protect access to the count of contexts in use */
708 if (SMB_THREAD_LOCK(initialized_ctx_count_mutex) != 0) {
709 smb_panic("error locking 'initialized_ctx_count'");
712 initialized_ctx_count++;
714 /* Unlock the mutex */
715 if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex) != 0) {
716 smb_panic("error unlocking 'initialized_ctx_count'");
719 TALLOC_FREE(frame);
720 return context;
724 /* Return the version of samba, and thus libsmbclient */
725 const char *
726 smbc_version(void)
728 return samba_version_string();
732 * Set the credentials so DFS will work when following referrals.
733 * This function is broken and must be removed. No SMBCCTX arg...
734 * JRA.
737 void
738 smbc_set_credentials(const char *workgroup,
739 const char *user,
740 const char *password,
741 smbc_bool use_kerberos,
742 const char *signing_state)
744 d_printf("smbc_set_credentials is obsolete. Replace with smbc_set_credentials_with_fallback().\n");
747 void smbc_set_credentials_with_fallback(SMBCCTX *context,
748 const char *workgroup,
749 const char *user,
750 const char *password)
752 struct cli_credentials *creds = NULL;
753 enum credentials_use_kerberos kerberos_state =
754 CRED_USE_KERBEROS_DISABLED;
756 if (! context) {
758 return;
761 if (! workgroup || ! *workgroup) {
762 workgroup = smbc_getWorkgroup(context);
765 if (! user) {
766 user = smbc_getUser(context);
769 if (! password) {
770 password = "";
773 creds = cli_credentials_init(NULL);
774 if (creds == NULL) {
775 DEBUG(0, ("smbc_set_credentials_with_fallback: allocation fail\n"));
776 return;
779 cli_credentials_set_conf(creds, context->internal->lp_ctx);
781 if (smbc_getOptionUseKerberos(context)) {
782 kerberos_state = CRED_USE_KERBEROS_REQUIRED;
784 if (smbc_getOptionFallbackAfterKerberos(context)) {
785 kerberos_state = CRED_USE_KERBEROS_DESIRED;
789 cli_credentials_set_username(creds, user, CRED_SPECIFIED);
790 cli_credentials_set_password(creds, password, CRED_SPECIFIED);
791 cli_credentials_set_domain(creds, workgroup, CRED_SPECIFIED);
792 cli_credentials_set_kerberos_state(creds,
793 kerberos_state,
794 CRED_SPECIFIED);
795 if (smbc_getOptionUseCCache(context)) {
796 cli_credentials_add_gensec_features(creds,
797 GENSEC_FEATURE_NTLM_CCACHE,
798 CRED_SPECIFIED);
801 TALLOC_FREE(context->internal->creds);
802 context->internal->creds = creds;