s4:dsdb:tests: Also pass tests if asserted identity is present
[Samba.git] / source3 / libsmb / libsmb_context.c
blob23155fe263fe1f375366e6a55b6a695c98501fae
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"
36 * Is the logging working / configfile read ?
38 static bool SMBC_initialized = false;
39 static unsigned int initialized_ctx_count = 0;
40 static void *initialized_ctx_count_mutex = NULL;
43 * Do some module- and library-wide intializations
45 static void
46 SMBC_module_init(void * punused)
48 bool conf_loaded = False;
49 char *home = NULL;
50 TALLOC_CTX *frame = talloc_stackframe();
52 setup_logging("libsmbclient", DEBUG_STDOUT);
54 /* Here we would open the smb.conf file if needed ... */
56 home = getenv("HOME");
57 if (home) {
58 char *conf = NULL;
59 if (asprintf(&conf, "%s/.smb/smb.conf", home) > 0) {
60 if (lp_load_client(conf)) {
61 conf_loaded = True;
62 } else {
63 DEBUG(5, ("Could not load config file: %s\n",
64 conf));
66 SAFE_FREE(conf);
70 if (!conf_loaded) {
72 * Well, if that failed, try the get_dyn_CONFIGFILE
73 * Which points to the standard locn, and if that
74 * fails, silently ignore it and use the internal
75 * defaults ...
78 if (!lp_load_client(get_dyn_CONFIGFILE())) {
79 DEBUG(5, ("Could not load config file: %s\n",
80 get_dyn_CONFIGFILE()));
81 } else if (home) {
82 char *conf;
84 * We loaded the global config file. Now lets
85 * load user-specific modifications to the
86 * global config.
88 if (asprintf(&conf,
89 "%s/.smb/smb.conf.append",
90 home) > 0) {
91 if (!lp_load_client_no_reinit(conf)) {
92 DEBUG(10,
93 ("Could not append config file: "
94 "%s\n",
95 conf));
97 SAFE_FREE(conf);
102 load_interfaces(); /* Load the list of interfaces ... */
104 reopen_logs(); /* Get logging working ... */
107 * Block SIGPIPE (from lib/util_sock.c: write())
108 * It is not needed and should not stop execution
110 BlockSignals(True, SIGPIPE);
112 /* Create the mutex we'll use to protect initialized_ctx_count */
113 if (SMB_THREAD_CREATE_MUTEX("initialized_ctx_count_mutex",
114 initialized_ctx_count_mutex) != 0) {
115 smb_panic("SMBC_module_init: "
116 "failed to create 'initialized_ctx_count' mutex");
120 TALLOC_FREE(frame);
124 static void
125 SMBC_module_terminate(void)
127 TALLOC_CTX *frame = talloc_stackframe();
128 secrets_shutdown();
129 gfree_all();
130 SMBC_initialized = false;
131 TALLOC_FREE(frame);
136 * Get a new empty handle to fill in with your own info
138 SMBCCTX *
139 smbc_new_context(void)
141 SMBCCTX *context;
142 TALLOC_CTX *frame = talloc_stackframe();
144 /* The first call to this function should initialize the module */
145 SMB_THREAD_ONCE(&SMBC_initialized, SMBC_module_init, NULL);
148 * All newly added context fields should be placed in
149 * SMBC_internal_data, not directly in SMBCCTX.
151 context = SMB_MALLOC_P(SMBCCTX);
152 if (!context) {
153 TALLOC_FREE(frame);
154 errno = ENOMEM;
155 return NULL;
158 ZERO_STRUCTP(context);
160 context->internal = SMB_MALLOC_P(struct SMBC_internal_data);
161 if (!context->internal) {
162 TALLOC_FREE(frame);
163 SAFE_FREE(context);
164 errno = ENOMEM;
165 return NULL;
168 /* Initialize the context and establish reasonable defaults */
169 ZERO_STRUCTP(context->internal);
171 smbc_setDebug(context, 0);
172 smbc_setTimeout(context, 20000);
173 smbc_setPort(context, 0);
175 smbc_setOptionFullTimeNames(context, False);
176 smbc_setOptionOpenShareMode(context, SMBC_SHAREMODE_DENY_NONE);
177 smbc_setOptionSmbEncryptionLevel(context, SMBC_ENCRYPTLEVEL_DEFAULT);
178 smbc_setOptionUseCCache(context, True);
179 smbc_setOptionCaseSensitive(context, False);
180 smbc_setOptionBrowseMaxLmbCount(context, 3); /* # LMBs to query */
181 smbc_setOptionUrlEncodeReaddirEntries(context, False);
182 smbc_setOptionOneSharePerServer(context, False);
183 if (getenv("LIBSMBCLIENT_NO_CCACHE") != NULL) {
184 smbc_setOptionUseCCache(context, false);
187 smbc_setFunctionAuthData(context, SMBC_get_auth_data);
188 smbc_setFunctionCheckServer(context, SMBC_check_server);
189 smbc_setFunctionRemoveUnusedServer(context, SMBC_remove_unused_server);
191 smbc_setOptionUserData(context, NULL);
192 smbc_setFunctionAddCachedServer(context, SMBC_add_cached_server);
193 smbc_setFunctionGetCachedServer(context, SMBC_get_cached_server);
194 smbc_setFunctionRemoveCachedServer(context, SMBC_remove_cached_server);
195 smbc_setFunctionPurgeCachedServers(context, SMBC_purge_cached_servers);
197 smbc_setFunctionOpen(context, SMBC_open_ctx);
198 smbc_setFunctionCreat(context, SMBC_creat_ctx);
199 smbc_setFunctionRead(context, SMBC_read_ctx);
200 smbc_setFunctionSplice(context, SMBC_splice_ctx);
201 smbc_setFunctionWrite(context, SMBC_write_ctx);
202 smbc_setFunctionClose(context, SMBC_close_ctx);
203 smbc_setFunctionUnlink(context, SMBC_unlink_ctx);
204 smbc_setFunctionRename(context, SMBC_rename_ctx);
205 smbc_setFunctionLseek(context, SMBC_lseek_ctx);
206 smbc_setFunctionFtruncate(context, SMBC_ftruncate_ctx);
207 smbc_setFunctionStat(context, SMBC_stat_ctx);
208 smbc_setFunctionStatVFS(context, SMBC_statvfs_ctx);
209 smbc_setFunctionFstatVFS(context, SMBC_fstatvfs_ctx);
210 smbc_setFunctionFstat(context, SMBC_fstat_ctx);
211 smbc_setFunctionOpendir(context, SMBC_opendir_ctx);
212 smbc_setFunctionClosedir(context, SMBC_closedir_ctx);
213 smbc_setFunctionReaddir(context, SMBC_readdir_ctx);
214 smbc_setFunctionReaddirPlus(context, SMBC_readdirplus_ctx);
215 smbc_setFunctionReaddirPlus2(context, SMBC_readdirplus2_ctx);
216 smbc_setFunctionGetdents(context, SMBC_getdents_ctx);
217 smbc_setFunctionMkdir(context, SMBC_mkdir_ctx);
218 smbc_setFunctionRmdir(context, SMBC_rmdir_ctx);
219 smbc_setFunctionTelldir(context, SMBC_telldir_ctx);
220 smbc_setFunctionLseekdir(context, SMBC_lseekdir_ctx);
221 smbc_setFunctionFstatdir(context, SMBC_fstatdir_ctx);
222 smbc_setFunctionNotify(context, SMBC_notify_ctx);
223 smbc_setFunctionChmod(context, SMBC_chmod_ctx);
224 smbc_setFunctionUtimes(context, SMBC_utimes_ctx);
225 smbc_setFunctionSetxattr(context, SMBC_setxattr_ctx);
226 smbc_setFunctionGetxattr(context, SMBC_getxattr_ctx);
227 smbc_setFunctionRemovexattr(context, SMBC_removexattr_ctx);
228 smbc_setFunctionListxattr(context, SMBC_listxattr_ctx);
230 smbc_setFunctionOpenPrintJob(context, SMBC_open_print_job_ctx);
231 smbc_setFunctionPrintFile(context, SMBC_print_file_ctx);
232 smbc_setFunctionListPrintJobs(context, SMBC_list_print_jobs_ctx);
233 smbc_setFunctionUnlinkPrintJob(context, SMBC_unlink_print_job_ctx);
235 TALLOC_FREE(frame);
236 return context;
240 * Free a context
242 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
243 * and thus you'll be leaking memory if not handled properly.
247 smbc_free_context(SMBCCTX *context,
248 int shutdown_ctx)
250 TALLOC_CTX *frame;
251 if (!context) {
252 errno = EBADF;
253 return 1;
256 frame = talloc_stackframe();
258 if (shutdown_ctx) {
259 SMBCFILE * f;
260 DEBUG(1,("Performing aggressive shutdown.\n"));
262 f = context->internal->files;
263 while (f) {
264 SMBCFILE *next = f->next;
265 smbc_getFunctionClose(context)(context, f);
266 f = next;
268 context->internal->files = NULL;
270 /* First try to remove the servers the nice way. */
271 if (smbc_getFunctionPurgeCachedServers(context)(context)) {
272 SMBCSRV * s;
273 SMBCSRV * next;
274 DEBUG(1, ("Could not purge all servers, "
275 "Nice way shutdown failed.\n"));
276 s = context->internal->servers;
277 while (s) {
278 DEBUG(1, ("Forced shutdown: %p (cli=%p)\n",
279 s, s->cli));
280 cli_shutdown(s->cli);
281 smbc_getFunctionRemoveCachedServer(context)(context,
283 next = s->next;
284 DLIST_REMOVE(context->internal->servers, s);
285 SAFE_FREE(s);
286 s = next;
288 context->internal->servers = NULL;
291 else {
292 /* This is the polite way */
293 if (smbc_getFunctionPurgeCachedServers(context)(context)) {
294 DEBUG(1, ("Could not purge all servers, "
295 "free_context failed.\n"));
296 errno = EBUSY;
297 TALLOC_FREE(frame);
298 return 1;
300 if (context->internal->servers) {
301 DEBUG(1, ("Active servers in context, "
302 "free_context failed.\n"));
303 errno = EBUSY;
304 TALLOC_FREE(frame);
305 return 1;
307 if (context->internal->files) {
308 DEBUG(1, ("Active files in context, "
309 "free_context failed.\n"));
310 errno = EBUSY;
311 TALLOC_FREE(frame);
312 return 1;
316 /* Things we have to clean up */
317 smbc_setWorkgroup(context, NULL);
318 smbc_setNetbiosName(context, NULL);
319 smbc_setUser(context, NULL);
321 DEBUG(3, ("Context %p successfully freed\n", context));
323 /* Free any DFS auth context. */
324 TALLOC_FREE(context->internal->creds);
326 SAFE_FREE(context->internal);
327 SAFE_FREE(context);
329 /* Protect access to the count of contexts in use */
330 if (SMB_THREAD_LOCK(initialized_ctx_count_mutex) != 0) {
331 smb_panic("error locking 'initialized_ctx_count'");
334 if (initialized_ctx_count) {
335 initialized_ctx_count--;
338 if (initialized_ctx_count == 0) {
339 SMBC_module_terminate();
342 /* Unlock the mutex */
343 if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex) != 0) {
344 smb_panic("error unlocking 'initialized_ctx_count'");
347 TALLOC_FREE(frame);
348 return 0;
353 * Deprecated interface. Do not use. Instead, use the various
354 * smbc_setOption*() functions or smbc_setFunctionAuthDataWithContext().
356 void
357 smbc_option_set(SMBCCTX *context,
358 char *option_name,
359 ... /* option_value */)
361 va_list ap;
362 union {
363 int i;
364 bool b;
365 smbc_get_auth_data_with_context_fn auth_fn;
366 void *v;
367 const char *s;
368 } option_value;
370 TALLOC_CTX *frame = talloc_stackframe();
372 va_start(ap, option_name);
374 if (strcmp(option_name, "debug_to_stderr") == 0) {
375 option_value.b = (bool) va_arg(ap, int);
376 smbc_setOptionDebugToStderr(context, option_value.b);
378 } else if (strcmp(option_name, "full_time_names") == 0) {
379 option_value.b = (bool) va_arg(ap, int);
380 smbc_setOptionFullTimeNames(context, option_value.b);
382 } else if (strcmp(option_name, "open_share_mode") == 0) {
383 option_value.i = va_arg(ap, int);
384 smbc_setOptionOpenShareMode(context, option_value.i);
386 } else if (strcmp(option_name, "auth_function") == 0) {
387 option_value.auth_fn =
388 va_arg(ap, smbc_get_auth_data_with_context_fn);
389 smbc_setFunctionAuthDataWithContext(context, option_value.auth_fn);
391 } else if (strcmp(option_name, "user_data") == 0) {
392 option_value.v = va_arg(ap, void *);
393 smbc_setOptionUserData(context, option_value.v);
395 } else if (strcmp(option_name, "smb_encrypt_level") == 0) {
396 option_value.s = va_arg(ap, const char *);
397 if (strcmp(option_value.s, "none") == 0) {
398 smbc_setOptionSmbEncryptionLevel(context,
399 SMBC_ENCRYPTLEVEL_NONE);
400 } else if (strcmp(option_value.s, "request") == 0) {
401 smbc_setOptionSmbEncryptionLevel(context,
402 SMBC_ENCRYPTLEVEL_REQUEST);
403 } else if (strcmp(option_value.s, "require") == 0) {
404 smbc_setOptionSmbEncryptionLevel(context,
405 SMBC_ENCRYPTLEVEL_REQUIRE);
408 } else if (strcmp(option_name, "browse_max_lmb_count") == 0) {
409 option_value.i = va_arg(ap, int);
410 smbc_setOptionBrowseMaxLmbCount(context, option_value.i);
412 } else if (strcmp(option_name, "urlencode_readdir_entries") == 0) {
413 option_value.b = (bool) va_arg(ap, int);
414 smbc_setOptionUrlEncodeReaddirEntries(context, option_value.b);
416 } else if (strcmp(option_name, "one_share_per_server") == 0) {
417 option_value.b = (bool) va_arg(ap, int);
418 smbc_setOptionOneSharePerServer(context, option_value.b);
420 } else if (strcmp(option_name, "use_kerberos") == 0) {
421 option_value.b = (bool) va_arg(ap, int);
422 smbc_setOptionUseKerberos(context, option_value.b);
424 } else if (strcmp(option_name, "fallback_after_kerberos") == 0) {
425 option_value.b = (bool) va_arg(ap, int);
426 smbc_setOptionFallbackAfterKerberos(context, option_value.b);
428 } else if (strcmp(option_name, "use_ccache") == 0) {
429 option_value.b = (bool) va_arg(ap, int);
430 smbc_setOptionUseCCache(context, option_value.b);
432 } else if (strcmp(option_name, "no_auto_anonymous_login") == 0) {
433 option_value.b = (bool) va_arg(ap, int);
434 smbc_setOptionNoAutoAnonymousLogin(context, option_value.b);
437 va_end(ap);
438 TALLOC_FREE(frame);
443 * Deprecated interface. Do not use. Instead, use the various
444 * smbc_getOption*() functions.
446 void *
447 smbc_option_get(SMBCCTX *context,
448 char *option_name)
450 if (strcmp(option_name, "debug_stderr") == 0) {
451 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
452 return (void *) (intptr_t) smbc_getOptionDebugToStderr(context);
453 #else
454 return (void *) smbc_getOptionDebugToStderr(context);
455 #endif
457 } else if (strcmp(option_name, "full_time_names") == 0) {
458 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
459 return (void *) (intptr_t) smbc_getOptionFullTimeNames(context);
460 #else
461 return (void *) smbc_getOptionFullTimeNames(context);
462 #endif
464 } else if (strcmp(option_name, "open_share_mode") == 0) {
465 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
466 return (void *) (intptr_t) smbc_getOptionOpenShareMode(context);
467 #else
468 return (void *) smbc_getOptionOpenShareMode(context);
469 #endif
471 } else if (strcmp(option_name, "auth_function") == 0) {
472 return (void *) smbc_getFunctionAuthDataWithContext(context);
474 } else if (strcmp(option_name, "user_data") == 0) {
475 return smbc_getOptionUserData(context);
477 } else if (strcmp(option_name, "smb_encrypt_level") == 0) {
478 switch(smbc_getOptionSmbEncryptionLevel(context))
480 case SMBC_ENCRYPTLEVEL_DEFAULT:
481 return discard_const_p(void, "default");
482 case 0:
483 return discard_const_p(void, "none");
484 case 1:
485 return discard_const_p(void, "request");
486 case 2:
487 return discard_const_p(void, "require");
490 } else if (strcmp(option_name, "smb_encrypt_on") == 0) {
491 SMBCSRV *s;
492 unsigned int num_servers = 0;
494 for (s = context->internal->servers; s; s = s->next) {
495 num_servers++;
496 if (!cli_state_is_encryption_on(s->cli)) {
497 return (void *)false;
500 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
501 return (void *) (intptr_t) (bool) (num_servers > 0);
502 #else
503 return (void *) (bool) (num_servers > 0);
504 #endif
506 } else if (strcmp(option_name, "browse_max_lmb_count") == 0) {
507 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
508 return (void *) (intptr_t) smbc_getOptionBrowseMaxLmbCount(context);
509 #else
510 return (void *) smbc_getOptionBrowseMaxLmbCount(context);
511 #endif
513 } else if (strcmp(option_name, "urlencode_readdir_entries") == 0) {
514 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
515 return (void *)(intptr_t) smbc_getOptionUrlEncodeReaddirEntries(context);
516 #else
517 return (void *) (bool) smbc_getOptionUrlEncodeReaddirEntries(context);
518 #endif
520 } else if (strcmp(option_name, "one_share_per_server") == 0) {
521 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
522 return (void *) (intptr_t) smbc_getOptionOneSharePerServer(context);
523 #else
524 return (void *) (bool) smbc_getOptionOneSharePerServer(context);
525 #endif
527 } else if (strcmp(option_name, "use_kerberos") == 0) {
528 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
529 return (void *) (intptr_t) smbc_getOptionUseKerberos(context);
530 #else
531 return (void *) (bool) smbc_getOptionUseKerberos(context);
532 #endif
534 } else if (strcmp(option_name, "fallback_after_kerberos") == 0) {
535 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
536 return (void *)(intptr_t) smbc_getOptionFallbackAfterKerberos(context);
537 #else
538 return (void *) (bool) smbc_getOptionFallbackAfterKerberos(context);
539 #endif
541 } else if (strcmp(option_name, "use_ccache") == 0) {
542 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
543 return (void *) (intptr_t) smbc_getOptionUseCCache(context);
544 #else
545 return (void *) (bool) smbc_getOptionUseCCache(context);
546 #endif
548 } else if (strcmp(option_name, "no_auto_anonymous_login") == 0) {
549 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
550 return (void *) (intptr_t) smbc_getOptionNoAutoAnonymousLogin(context);
551 #else
552 return (void *) (bool) smbc_getOptionNoAutoAnonymousLogin(context);
553 #endif
556 return NULL;
561 * Initialize the library, etc.
563 * We accept a struct containing handle information.
564 * valid values for info->debug from 0 to 100,
565 * and insist that info->fn must be non-null.
567 SMBCCTX *
568 smbc_init_context(SMBCCTX *context)
570 int pid;
571 TALLOC_CTX *frame;
573 if (!context) {
574 errno = EBADF;
575 return NULL;
578 /* Do not initialise the same client twice */
579 if (context->internal->initialized) {
580 return NULL;
583 frame = talloc_stackframe();
585 if ((!smbc_getFunctionAuthData(context) &&
586 !smbc_getFunctionAuthDataWithContext(context)) ||
587 smbc_getDebug(context) < 0 ||
588 smbc_getDebug(context) > 100) {
590 TALLOC_FREE(frame);
591 errno = EINVAL;
592 return NULL;
596 if (!smbc_getUser(context)) {
598 * FIXME: Is this the best way to get the user info?
600 char *user = getenv("USER");
601 /* walk around as "guest" if no username can be found */
602 if (!user) {
603 user = SMB_STRDUP("guest");
604 } else {
605 user = SMB_STRDUP(user);
608 if (!user) {
609 TALLOC_FREE(frame);
610 errno = ENOMEM;
611 return NULL;
614 smbc_setUser(context, user);
615 SAFE_FREE(user);
617 if (!smbc_getUser(context)) {
618 TALLOC_FREE(frame);
619 errno = ENOMEM;
620 return NULL;
624 if (!smbc_getNetbiosName(context)) {
626 * We try to get our netbios name from the config. If that
627 * fails we fall back on constructing our netbios name from
628 * our hostname etc
630 char *netbios_name;
631 if (lp_netbios_name()) {
632 netbios_name = SMB_STRDUP(lp_netbios_name());
633 } else {
635 * Hmmm, I want to get hostname as well, but I am too
636 * lazy for the moment
638 pid = getpid();
639 netbios_name = (char *)SMB_MALLOC(17);
640 if (!netbios_name) {
641 TALLOC_FREE(frame);
642 errno = ENOMEM;
643 return NULL;
645 slprintf(netbios_name, 16,
646 "smbc%s%d", smbc_getUser(context), pid);
649 if (!netbios_name) {
650 TALLOC_FREE(frame);
651 errno = ENOMEM;
652 return NULL;
655 smbc_setNetbiosName(context, netbios_name);
656 SAFE_FREE(netbios_name);
658 if (!smbc_getNetbiosName(context)) {
659 TALLOC_FREE(frame);
660 errno = ENOMEM;
661 return NULL;
665 DEBUG(1, ("Using netbios name %s.\n", smbc_getNetbiosName(context)));
667 if (!smbc_getWorkgroup(context)) {
668 const char *workgroup;
670 if (lp_workgroup()) {
671 workgroup = lp_workgroup();
672 } else {
673 /* TODO: Think about a decent default workgroup */
674 workgroup = "samba";
677 smbc_setWorkgroup(context, workgroup);
679 if (!smbc_getWorkgroup(context)) {
680 TALLOC_FREE(frame);
681 errno = ENOMEM;
682 return NULL;
686 DEBUG(1, ("Using workgroup %s.\n", smbc_getWorkgroup(context)));
688 /* shortest timeout is 1 second */
689 if (smbc_getTimeout(context) > 0 && smbc_getTimeout(context) < 1000)
690 smbc_setTimeout(context, 1000);
692 context->internal->initialized = True;
694 /* Protect access to the count of contexts in use */
695 if (SMB_THREAD_LOCK(initialized_ctx_count_mutex) != 0) {
696 smb_panic("error locking 'initialized_ctx_count'");
699 initialized_ctx_count++;
701 /* Unlock the mutex */
702 if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex) != 0) {
703 smb_panic("error unlocking 'initialized_ctx_count'");
706 TALLOC_FREE(frame);
707 return context;
711 /* Return the version of samba, and thus libsmbclient */
712 const char *
713 smbc_version(void)
715 return samba_version_string();
719 * Set the credentials so DFS will work when following referrals.
720 * This function is broken and must be removed. No SMBCCTX arg...
721 * JRA.
724 void
725 smbc_set_credentials(const char *workgroup,
726 const char *user,
727 const char *password,
728 smbc_bool use_kerberos,
729 const char *signing_state)
731 d_printf("smbc_set_credentials is obsolete. Replace with smbc_set_credentials_with_fallback().\n");
734 void smbc_set_credentials_with_fallback(SMBCCTX *context,
735 const char *workgroup,
736 const char *user,
737 const char *password)
739 struct loadparm_context *lp_ctx = NULL;
740 struct cli_credentials *creds = NULL;
741 enum credentials_use_kerberos kerberos_state =
742 CRED_USE_KERBEROS_DISABLED;
744 if (! context) {
746 return;
749 if (! workgroup || ! *workgroup) {
750 workgroup = smbc_getWorkgroup(context);
753 if (! user) {
754 user = smbc_getUser(context);
757 if (! password) {
758 password = "";
761 creds = cli_credentials_init(NULL);
762 if (creds == NULL) {
763 DEBUG(0, ("smbc_set_credentials_with_fallback: allocation fail\n"));
764 return;
767 lp_ctx = loadparm_init_s3(creds, loadparm_s3_helpers());
768 if (lp_ctx == NULL) {
769 TALLOC_FREE(creds);
770 return;
773 cli_credentials_set_conf(creds, lp_ctx);
775 if (smbc_getOptionUseKerberos(context)) {
776 kerberos_state = CRED_USE_KERBEROS_REQUIRED;
778 if (smbc_getOptionFallbackAfterKerberos(context)) {
779 kerberos_state = CRED_USE_KERBEROS_DESIRED;
783 cli_credentials_set_username(creds, user, CRED_SPECIFIED);
784 cli_credentials_set_password(creds, password, CRED_SPECIFIED);
785 cli_credentials_set_domain(creds, workgroup, CRED_SPECIFIED);
786 cli_credentials_set_kerberos_state(creds,
787 kerberos_state,
788 CRED_SPECIFIED);
789 if (smbc_getOptionUseCCache(context)) {
790 uint32_t gensec_features;
792 gensec_features = cli_credentials_get_gensec_features(creds);
793 gensec_features |= GENSEC_FEATURE_NTLM_CCACHE;
794 cli_credentials_set_gensec_features(creds,
795 gensec_features,
796 CRED_SPECIFIED);
799 TALLOC_FREE(context->internal->creds);
800 context->internal->creds = creds;