s3: smbd: Add some DEVELOPER-only code to panic if the destructor for an aio_lnk...
[Samba.git] / source3 / libsmb / libsmb_context.c
bloba4c4c84a83107ab0f1909462c7d5c7a2f064a0b6
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 if (getenv("LIBSMBCLIENT_NO_CCACHE") != NULL) {
195 smbc_setOptionUseCCache(context, false);
198 smbc_setFunctionAuthData(context, SMBC_get_auth_data);
199 smbc_setFunctionCheckServer(context, SMBC_check_server);
200 smbc_setFunctionRemoveUnusedServer(context, SMBC_remove_unused_server);
202 smbc_setOptionUserData(context, NULL);
203 smbc_setFunctionAddCachedServer(context, SMBC_add_cached_server);
204 smbc_setFunctionGetCachedServer(context, SMBC_get_cached_server);
205 smbc_setFunctionRemoveCachedServer(context, SMBC_remove_cached_server);
206 smbc_setFunctionPurgeCachedServers(context, SMBC_purge_cached_servers);
208 smbc_setFunctionOpen(context, SMBC_open_ctx);
209 smbc_setFunctionCreat(context, SMBC_creat_ctx);
210 smbc_setFunctionRead(context, SMBC_read_ctx);
211 smbc_setFunctionSplice(context, SMBC_splice_ctx);
212 smbc_setFunctionWrite(context, SMBC_write_ctx);
213 smbc_setFunctionClose(context, SMBC_close_ctx);
214 smbc_setFunctionUnlink(context, SMBC_unlink_ctx);
215 smbc_setFunctionRename(context, SMBC_rename_ctx);
216 smbc_setFunctionLseek(context, SMBC_lseek_ctx);
217 smbc_setFunctionFtruncate(context, SMBC_ftruncate_ctx);
218 smbc_setFunctionStat(context, SMBC_stat_ctx);
219 smbc_setFunctionStatVFS(context, SMBC_statvfs_ctx);
220 smbc_setFunctionFstatVFS(context, SMBC_fstatvfs_ctx);
221 smbc_setFunctionFstat(context, SMBC_fstat_ctx);
222 smbc_setFunctionOpendir(context, SMBC_opendir_ctx);
223 smbc_setFunctionClosedir(context, SMBC_closedir_ctx);
224 smbc_setFunctionReaddir(context, SMBC_readdir_ctx);
225 smbc_setFunctionReaddirPlus(context, SMBC_readdirplus_ctx);
226 smbc_setFunctionReaddirPlus2(context, SMBC_readdirplus2_ctx);
227 smbc_setFunctionGetdents(context, SMBC_getdents_ctx);
228 smbc_setFunctionMkdir(context, SMBC_mkdir_ctx);
229 smbc_setFunctionRmdir(context, SMBC_rmdir_ctx);
230 smbc_setFunctionTelldir(context, SMBC_telldir_ctx);
231 smbc_setFunctionLseekdir(context, SMBC_lseekdir_ctx);
232 smbc_setFunctionFstatdir(context, SMBC_fstatdir_ctx);
233 smbc_setFunctionNotify(context, SMBC_notify_ctx);
234 smbc_setFunctionChmod(context, SMBC_chmod_ctx);
235 smbc_setFunctionUtimes(context, SMBC_utimes_ctx);
236 smbc_setFunctionSetxattr(context, SMBC_setxattr_ctx);
237 smbc_setFunctionGetxattr(context, SMBC_getxattr_ctx);
238 smbc_setFunctionRemovexattr(context, SMBC_removexattr_ctx);
239 smbc_setFunctionListxattr(context, SMBC_listxattr_ctx);
241 smbc_setFunctionOpenPrintJob(context, SMBC_open_print_job_ctx);
242 smbc_setFunctionPrintFile(context, SMBC_print_file_ctx);
243 smbc_setFunctionListPrintJobs(context, SMBC_list_print_jobs_ctx);
244 smbc_setFunctionUnlinkPrintJob(context, SMBC_unlink_print_job_ctx);
246 TALLOC_FREE(frame);
247 return context;
251 * Free a context
253 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
254 * and thus you'll be leaking memory if not handled properly.
258 smbc_free_context(SMBCCTX *context,
259 int shutdown_ctx)
261 TALLOC_CTX *frame;
262 if (!context) {
263 errno = EBADF;
264 return 1;
267 frame = talloc_stackframe();
269 if (shutdown_ctx) {
270 SMBCFILE * f;
271 DEBUG(1,("Performing aggressive shutdown.\n"));
273 f = context->internal->files;
274 while (f) {
275 SMBCFILE *next = f->next;
276 smbc_getFunctionClose(context)(context, f);
277 f = next;
279 context->internal->files = NULL;
281 /* First try to remove the servers the nice way. */
282 if (smbc_getFunctionPurgeCachedServers(context)(context)) {
283 SMBCSRV * s;
284 SMBCSRV * next;
285 DEBUG(1, ("Could not purge all servers, "
286 "Nice way shutdown failed.\n"));
287 s = context->internal->servers;
288 while (s) {
289 DEBUG(1, ("Forced shutdown: %p (cli=%p)\n",
290 s, s->cli));
291 cli_shutdown(s->cli);
292 smbc_getFunctionRemoveCachedServer(context)(context,
294 next = s->next;
295 DLIST_REMOVE(context->internal->servers, s);
296 SAFE_FREE(s);
297 s = next;
299 context->internal->servers = NULL;
302 else {
303 /* This is the polite way */
304 if (smbc_getFunctionPurgeCachedServers(context)(context)) {
305 DEBUG(1, ("Could not purge all servers, "
306 "free_context failed.\n"));
307 errno = EBUSY;
308 TALLOC_FREE(frame);
309 return 1;
311 if (context->internal->servers) {
312 DEBUG(1, ("Active servers in context, "
313 "free_context failed.\n"));
314 errno = EBUSY;
315 TALLOC_FREE(frame);
316 return 1;
318 if (context->internal->files) {
319 DEBUG(1, ("Active files in context, "
320 "free_context failed.\n"));
321 errno = EBUSY;
322 TALLOC_FREE(frame);
323 return 1;
327 /* Things we have to clean up */
328 smbc_setWorkgroup(context, NULL);
329 smbc_setNetbiosName(context, NULL);
330 smbc_setUser(context, NULL);
332 DEBUG(3, ("Context %p successfully freed\n", context));
334 /* Free any DFS auth context. */
335 TALLOC_FREE(context->internal->creds);
337 TALLOC_FREE(context->internal->lp_ctx);
338 SAFE_FREE(context->internal);
339 SAFE_FREE(context);
341 /* Protect access to the count of contexts in use */
342 if (SMB_THREAD_LOCK(initialized_ctx_count_mutex) != 0) {
343 smb_panic("error locking 'initialized_ctx_count'");
346 if (initialized_ctx_count) {
347 initialized_ctx_count--;
350 if (initialized_ctx_count == 0) {
351 SMBC_module_terminate();
354 /* Unlock the mutex */
355 if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex) != 0) {
356 smb_panic("error unlocking 'initialized_ctx_count'");
359 TALLOC_FREE(frame);
360 return 0;
365 * Deprecated interface. Do not use. Instead, use the various
366 * smbc_setOption*() functions or smbc_setFunctionAuthDataWithContext().
368 void
369 smbc_option_set(SMBCCTX *context,
370 char *option_name,
371 ... /* option_value */)
373 va_list ap;
374 union {
375 int i;
376 bool b;
377 smbc_get_auth_data_with_context_fn auth_fn;
378 void *v;
379 const char *s;
380 } option_value;
382 TALLOC_CTX *frame = talloc_stackframe();
384 va_start(ap, option_name);
386 if (strcmp(option_name, "debug_to_stderr") == 0) {
387 option_value.b = (bool) va_arg(ap, int);
388 smbc_setOptionDebugToStderr(context, option_value.b);
390 } else if (strcmp(option_name, "full_time_names") == 0) {
391 option_value.b = (bool) va_arg(ap, int);
392 smbc_setOptionFullTimeNames(context, option_value.b);
394 } else if (strcmp(option_name, "open_share_mode") == 0) {
395 option_value.i = va_arg(ap, int);
396 smbc_setOptionOpenShareMode(context, option_value.i);
398 } else if (strcmp(option_name, "auth_function") == 0) {
399 option_value.auth_fn =
400 va_arg(ap, smbc_get_auth_data_with_context_fn);
401 smbc_setFunctionAuthDataWithContext(context, option_value.auth_fn);
403 } else if (strcmp(option_name, "user_data") == 0) {
404 option_value.v = va_arg(ap, void *);
405 smbc_setOptionUserData(context, option_value.v);
407 } else if (strcmp(option_name, "smb_encrypt_level") == 0) {
408 option_value.s = va_arg(ap, const char *);
409 if (strcmp(option_value.s, "none") == 0) {
410 smbc_setOptionSmbEncryptionLevel(context,
411 SMBC_ENCRYPTLEVEL_NONE);
412 } else if (strcmp(option_value.s, "request") == 0) {
413 smbc_setOptionSmbEncryptionLevel(context,
414 SMBC_ENCRYPTLEVEL_REQUEST);
415 } else if (strcmp(option_value.s, "require") == 0) {
416 smbc_setOptionSmbEncryptionLevel(context,
417 SMBC_ENCRYPTLEVEL_REQUIRE);
420 } else if (strcmp(option_name, "browse_max_lmb_count") == 0) {
421 option_value.i = va_arg(ap, int);
422 smbc_setOptionBrowseMaxLmbCount(context, option_value.i);
424 } else if (strcmp(option_name, "urlencode_readdir_entries") == 0) {
425 option_value.b = (bool) va_arg(ap, int);
426 smbc_setOptionUrlEncodeReaddirEntries(context, option_value.b);
428 } else if (strcmp(option_name, "one_share_per_server") == 0) {
429 option_value.b = (bool) va_arg(ap, int);
430 smbc_setOptionOneSharePerServer(context, option_value.b);
432 } else if (strcmp(option_name, "use_kerberos") == 0) {
433 option_value.b = (bool) va_arg(ap, int);
434 smbc_setOptionUseKerberos(context, option_value.b);
436 } else if (strcmp(option_name, "fallback_after_kerberos") == 0) {
437 option_value.b = (bool) va_arg(ap, int);
438 smbc_setOptionFallbackAfterKerberos(context, option_value.b);
440 } else if (strcmp(option_name, "use_ccache") == 0) {
441 option_value.b = (bool) va_arg(ap, int);
442 smbc_setOptionUseCCache(context, option_value.b);
444 } else if (strcmp(option_name, "no_auto_anonymous_login") == 0) {
445 option_value.b = (bool) va_arg(ap, int);
446 smbc_setOptionNoAutoAnonymousLogin(context, option_value.b);
449 va_end(ap);
450 TALLOC_FREE(frame);
455 * Deprecated interface. Do not use. Instead, use the various
456 * smbc_getOption*() functions.
458 void *
459 smbc_option_get(SMBCCTX *context,
460 char *option_name)
462 if (strcmp(option_name, "debug_stderr") == 0) {
463 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
464 return (void *) (intptr_t) smbc_getOptionDebugToStderr(context);
465 #else
466 return (void *) smbc_getOptionDebugToStderr(context);
467 #endif
469 } else if (strcmp(option_name, "full_time_names") == 0) {
470 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
471 return (void *) (intptr_t) smbc_getOptionFullTimeNames(context);
472 #else
473 return (void *) smbc_getOptionFullTimeNames(context);
474 #endif
476 } else if (strcmp(option_name, "open_share_mode") == 0) {
477 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
478 return (void *) (intptr_t) smbc_getOptionOpenShareMode(context);
479 #else
480 return (void *) smbc_getOptionOpenShareMode(context);
481 #endif
483 } else if (strcmp(option_name, "auth_function") == 0) {
484 return (void *) smbc_getFunctionAuthDataWithContext(context);
486 } else if (strcmp(option_name, "user_data") == 0) {
487 return smbc_getOptionUserData(context);
489 } else if (strcmp(option_name, "smb_encrypt_level") == 0) {
490 switch(smbc_getOptionSmbEncryptionLevel(context))
492 case SMBC_ENCRYPTLEVEL_DEFAULT:
493 return discard_const_p(void, "default");
494 case 0:
495 return discard_const_p(void, "none");
496 case 1:
497 return discard_const_p(void, "request");
498 case 2:
499 return discard_const_p(void, "require");
502 } else if (strcmp(option_name, "smb_encrypt_on") == 0) {
503 SMBCSRV *s;
504 unsigned int num_servers = 0;
506 for (s = context->internal->servers; s; s = s->next) {
507 num_servers++;
508 if (!cli_state_is_encryption_on(s->cli)) {
509 return (void *)false;
512 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
513 return (void *) (intptr_t) (bool) (num_servers > 0);
514 #else
515 return (void *) (bool) (num_servers > 0);
516 #endif
518 } else if (strcmp(option_name, "browse_max_lmb_count") == 0) {
519 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
520 return (void *) (intptr_t) smbc_getOptionBrowseMaxLmbCount(context);
521 #else
522 return (void *) smbc_getOptionBrowseMaxLmbCount(context);
523 #endif
525 } else if (strcmp(option_name, "urlencode_readdir_entries") == 0) {
526 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
527 return (void *)(intptr_t) smbc_getOptionUrlEncodeReaddirEntries(context);
528 #else
529 return (void *) (bool) smbc_getOptionUrlEncodeReaddirEntries(context);
530 #endif
532 } else if (strcmp(option_name, "one_share_per_server") == 0) {
533 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
534 return (void *) (intptr_t) smbc_getOptionOneSharePerServer(context);
535 #else
536 return (void *) (bool) smbc_getOptionOneSharePerServer(context);
537 #endif
539 } else if (strcmp(option_name, "use_kerberos") == 0) {
540 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
541 return (void *) (intptr_t) smbc_getOptionUseKerberos(context);
542 #else
543 return (void *) (bool) smbc_getOptionUseKerberos(context);
544 #endif
546 } else if (strcmp(option_name, "fallback_after_kerberos") == 0) {
547 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
548 return (void *)(intptr_t) smbc_getOptionFallbackAfterKerberos(context);
549 #else
550 return (void *) (bool) smbc_getOptionFallbackAfterKerberos(context);
551 #endif
553 } else if (strcmp(option_name, "use_ccache") == 0) {
554 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
555 return (void *) (intptr_t) smbc_getOptionUseCCache(context);
556 #else
557 return (void *) (bool) smbc_getOptionUseCCache(context);
558 #endif
560 } else if (strcmp(option_name, "no_auto_anonymous_login") == 0) {
561 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
562 return (void *) (intptr_t) smbc_getOptionNoAutoAnonymousLogin(context);
563 #else
564 return (void *) (bool) smbc_getOptionNoAutoAnonymousLogin(context);
565 #endif
568 return NULL;
573 * Initialize the library, etc.
575 * We accept a struct containing handle information.
576 * valid values for info->debug from 0 to 100,
577 * and insist that info->fn must be non-null.
579 SMBCCTX *
580 smbc_init_context(SMBCCTX *context)
582 int pid;
583 TALLOC_CTX *frame;
585 if (!context) {
586 errno = EBADF;
587 return NULL;
590 /* Do not initialise the same client twice */
591 if (context->internal->initialized) {
592 return NULL;
595 frame = talloc_stackframe();
597 if ((!smbc_getFunctionAuthData(context) &&
598 !smbc_getFunctionAuthDataWithContext(context)) ||
599 smbc_getDebug(context) < 0 ||
600 smbc_getDebug(context) > 100) {
602 TALLOC_FREE(frame);
603 errno = EINVAL;
604 return NULL;
608 if (!smbc_getUser(context)) {
610 * FIXME: Is this the best way to get the user info?
612 char *user = getenv("USER");
613 /* walk around as "guest" if no username can be found */
614 if (!user) {
615 user = SMB_STRDUP("guest");
616 } else {
617 user = SMB_STRDUP(user);
620 if (!user) {
621 TALLOC_FREE(frame);
622 errno = ENOMEM;
623 return NULL;
626 smbc_setUser(context, user);
627 SAFE_FREE(user);
629 if (!smbc_getUser(context)) {
630 TALLOC_FREE(frame);
631 errno = ENOMEM;
632 return NULL;
636 if (!smbc_getNetbiosName(context)) {
638 * We try to get our netbios name from the config. If that
639 * fails we fall back on constructing our netbios name from
640 * our hostname etc
642 char *netbios_name;
643 if (lp_netbios_name()) {
644 netbios_name = SMB_STRDUP(lp_netbios_name());
645 } else {
647 * Hmmm, I want to get hostname as well, but I am too
648 * lazy for the moment
650 pid = getpid();
651 netbios_name = (char *)SMB_MALLOC(17);
652 if (!netbios_name) {
653 TALLOC_FREE(frame);
654 errno = ENOMEM;
655 return NULL;
657 slprintf(netbios_name, 16,
658 "smbc%s%d", smbc_getUser(context), pid);
661 if (!netbios_name) {
662 TALLOC_FREE(frame);
663 errno = ENOMEM;
664 return NULL;
667 smbc_setNetbiosName(context, netbios_name);
668 SAFE_FREE(netbios_name);
670 if (!smbc_getNetbiosName(context)) {
671 TALLOC_FREE(frame);
672 errno = ENOMEM;
673 return NULL;
677 DEBUG(1, ("Using netbios name %s.\n", smbc_getNetbiosName(context)));
679 if (!smbc_getWorkgroup(context)) {
680 const char *workgroup;
682 if (lp_workgroup()) {
683 workgroup = lp_workgroup();
684 } else {
685 /* TODO: Think about a decent default workgroup */
686 workgroup = "samba";
689 smbc_setWorkgroup(context, workgroup);
691 if (!smbc_getWorkgroup(context)) {
692 TALLOC_FREE(frame);
693 errno = ENOMEM;
694 return NULL;
698 DEBUG(1, ("Using workgroup %s.\n", smbc_getWorkgroup(context)));
700 /* shortest timeout is 1 second */
701 if (smbc_getTimeout(context) > 0 && smbc_getTimeout(context) < 1000)
702 smbc_setTimeout(context, 1000);
704 context->internal->initialized = True;
706 /* Protect access to the count of contexts in use */
707 if (SMB_THREAD_LOCK(initialized_ctx_count_mutex) != 0) {
708 smb_panic("error locking 'initialized_ctx_count'");
711 initialized_ctx_count++;
713 /* Unlock the mutex */
714 if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex) != 0) {
715 smb_panic("error unlocking 'initialized_ctx_count'");
718 TALLOC_FREE(frame);
719 return context;
723 /* Return the version of samba, and thus libsmbclient */
724 const char *
725 smbc_version(void)
727 return samba_version_string();
731 * Set the credentials so DFS will work when following referrals.
732 * This function is broken and must be removed. No SMBCCTX arg...
733 * JRA.
736 void
737 smbc_set_credentials(const char *workgroup,
738 const char *user,
739 const char *password,
740 smbc_bool use_kerberos,
741 const char *signing_state)
743 d_printf("smbc_set_credentials is obsolete. Replace with smbc_set_credentials_with_fallback().\n");
746 void smbc_set_credentials_with_fallback(SMBCCTX *context,
747 const char *workgroup,
748 const char *user,
749 const char *password)
751 struct cli_credentials *creds = NULL;
752 enum credentials_use_kerberos kerberos_state =
753 CRED_USE_KERBEROS_DISABLED;
755 if (! context) {
757 return;
760 if (! workgroup || ! *workgroup) {
761 workgroup = smbc_getWorkgroup(context);
764 if (! user) {
765 user = smbc_getUser(context);
768 if (! password) {
769 password = "";
772 creds = cli_credentials_init(NULL);
773 if (creds == NULL) {
774 DEBUG(0, ("smbc_set_credentials_with_fallback: allocation fail\n"));
775 return;
778 cli_credentials_set_conf(creds, context->internal->lp_ctx);
780 if (smbc_getOptionUseKerberos(context)) {
781 kerberos_state = CRED_USE_KERBEROS_REQUIRED;
783 if (smbc_getOptionFallbackAfterKerberos(context)) {
784 kerberos_state = CRED_USE_KERBEROS_DESIRED;
788 cli_credentials_set_username(creds, user, CRED_SPECIFIED);
789 cli_credentials_set_password(creds, password, CRED_SPECIFIED);
790 cli_credentials_set_domain(creds, workgroup, CRED_SPECIFIED);
791 cli_credentials_set_kerberos_state(creds,
792 kerberos_state,
793 CRED_SPECIFIED);
794 if (smbc_getOptionUseCCache(context)) {
795 uint32_t gensec_features;
797 gensec_features = cli_credentials_get_gensec_features(creds);
798 gensec_features |= GENSEC_FEATURE_NTLM_CCACHE;
799 cli_credentials_set_gensec_features(creds,
800 gensec_features,
801 CRED_SPECIFIED);
804 TALLOC_FREE(context->internal->creds);
805 context->internal->creds = creds;