Convert all uses of uint32/16/8 to _t in source3/registry.
[Samba.git] / source3 / libsmb / libsmb_context.c
blobf66c0632bddb1a27a32179f681b425075ec78a46
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"
33 * Is the logging working / configfile read ?
35 static bool SMBC_initialized = false;
36 static unsigned int initialized_ctx_count = 0;
37 static void *initialized_ctx_count_mutex = NULL;
40 * Do some module- and library-wide intializations
42 static void
43 SMBC_module_init(void * punused)
45 bool conf_loaded = False;
46 char *home = NULL;
47 TALLOC_CTX *frame = talloc_stackframe();
49 setup_logging("libsmbclient", DEBUG_STDOUT);
51 /* Here we would open the smb.conf file if needed ... */
53 home = getenv("HOME");
54 if (home) {
55 char *conf = NULL;
56 if (asprintf(&conf, "%s/.smb/smb.conf", home) > 0) {
57 if (lp_load_client(conf)) {
58 conf_loaded = True;
59 } else {
60 DEBUG(5, ("Could not load config file: %s\n",
61 conf));
63 SAFE_FREE(conf);
67 if (!conf_loaded) {
69 * Well, if that failed, try the get_dyn_CONFIGFILE
70 * Which points to the standard locn, and if that
71 * fails, silently ignore it and use the internal
72 * defaults ...
75 if (!lp_load_client(get_dyn_CONFIGFILE())) {
76 DEBUG(5, ("Could not load config file: %s\n",
77 get_dyn_CONFIGFILE()));
78 } else if (home) {
79 char *conf;
81 * We loaded the global config file. Now lets
82 * load user-specific modifications to the
83 * global config.
85 if (asprintf(&conf,
86 "%s/.smb/smb.conf.append",
87 home) > 0) {
88 if (!lp_load_client_no_reinit(conf)) {
89 DEBUG(10,
90 ("Could not append config file: "
91 "%s\n",
92 conf));
94 SAFE_FREE(conf);
99 load_interfaces(); /* Load the list of interfaces ... */
101 reopen_logs(); /* Get logging working ... */
104 * Block SIGPIPE (from lib/util_sock.c: write())
105 * It is not needed and should not stop execution
107 BlockSignals(True, SIGPIPE);
109 /* Create the mutex we'll use to protect initialized_ctx_count */
110 if (SMB_THREAD_CREATE_MUTEX("initialized_ctx_count_mutex",
111 initialized_ctx_count_mutex) != 0) {
112 smb_panic("SMBC_module_init: "
113 "failed to create 'initialized_ctx_count' mutex");
117 TALLOC_FREE(frame);
121 static void
122 SMBC_module_terminate(void)
124 TALLOC_CTX *frame = talloc_stackframe();
125 secrets_shutdown();
126 gfree_all();
127 SMBC_initialized = false;
128 TALLOC_FREE(frame);
133 * Get a new empty handle to fill in with your own info
135 SMBCCTX *
136 smbc_new_context(void)
138 SMBCCTX *context;
139 TALLOC_CTX *frame = talloc_stackframe();
141 /* The first call to this function should initialize the module */
142 SMB_THREAD_ONCE(&SMBC_initialized, SMBC_module_init, NULL);
145 * All newly added context fields should be placed in
146 * SMBC_internal_data, not directly in SMBCCTX.
148 context = SMB_MALLOC_P(SMBCCTX);
149 if (!context) {
150 TALLOC_FREE(frame);
151 errno = ENOMEM;
152 return NULL;
155 ZERO_STRUCTP(context);
157 context->internal = SMB_MALLOC_P(struct SMBC_internal_data);
158 if (!context->internal) {
159 TALLOC_FREE(frame);
160 SAFE_FREE(context);
161 errno = ENOMEM;
162 return NULL;
165 /* Initialize the context and establish reasonable defaults */
166 ZERO_STRUCTP(context->internal);
168 smbc_setDebug(context, 0);
169 smbc_setTimeout(context, 20000);
170 smbc_setPort(context, 0);
172 smbc_setOptionFullTimeNames(context, False);
173 smbc_setOptionOpenShareMode(context, SMBC_SHAREMODE_DENY_NONE);
174 smbc_setOptionSmbEncryptionLevel(context, SMBC_ENCRYPTLEVEL_NONE);
175 smbc_setOptionUseCCache(context, True);
176 smbc_setOptionCaseSensitive(context, False);
177 smbc_setOptionBrowseMaxLmbCount(context, 3); /* # LMBs to query */
178 smbc_setOptionUrlEncodeReaddirEntries(context, False);
179 smbc_setOptionOneSharePerServer(context, False);
180 if (getenv("LIBSMBCLIENT_NO_CCACHE") == NULL) {
181 smbc_setOptionUseCCache(context, true);
184 smbc_setFunctionAuthData(context, SMBC_get_auth_data);
185 smbc_setFunctionCheckServer(context, SMBC_check_server);
186 smbc_setFunctionRemoveUnusedServer(context, SMBC_remove_unused_server);
188 smbc_setOptionUserData(context, NULL);
189 smbc_setFunctionAddCachedServer(context, SMBC_add_cached_server);
190 smbc_setFunctionGetCachedServer(context, SMBC_get_cached_server);
191 smbc_setFunctionRemoveCachedServer(context, SMBC_remove_cached_server);
192 smbc_setFunctionPurgeCachedServers(context, SMBC_purge_cached_servers);
194 smbc_setFunctionOpen(context, SMBC_open_ctx);
195 smbc_setFunctionCreat(context, SMBC_creat_ctx);
196 smbc_setFunctionRead(context, SMBC_read_ctx);
197 smbc_setFunctionWrite(context, SMBC_write_ctx);
198 smbc_setFunctionClose(context, SMBC_close_ctx);
199 smbc_setFunctionUnlink(context, SMBC_unlink_ctx);
200 smbc_setFunctionRename(context, SMBC_rename_ctx);
201 smbc_setFunctionLseek(context, SMBC_lseek_ctx);
202 smbc_setFunctionFtruncate(context, SMBC_ftruncate_ctx);
203 smbc_setFunctionStat(context, SMBC_stat_ctx);
204 smbc_setFunctionStatVFS(context, SMBC_statvfs_ctx);
205 smbc_setFunctionFstatVFS(context, SMBC_fstatvfs_ctx);
206 smbc_setFunctionFstat(context, SMBC_fstat_ctx);
207 smbc_setFunctionOpendir(context, SMBC_opendir_ctx);
208 smbc_setFunctionClosedir(context, SMBC_closedir_ctx);
209 smbc_setFunctionReaddir(context, SMBC_readdir_ctx);
210 smbc_setFunctionGetdents(context, SMBC_getdents_ctx);
211 smbc_setFunctionMkdir(context, SMBC_mkdir_ctx);
212 smbc_setFunctionRmdir(context, SMBC_rmdir_ctx);
213 smbc_setFunctionTelldir(context, SMBC_telldir_ctx);
214 smbc_setFunctionLseekdir(context, SMBC_lseekdir_ctx);
215 smbc_setFunctionFstatdir(context, SMBC_fstatdir_ctx);
216 smbc_setFunctionChmod(context, SMBC_chmod_ctx);
217 smbc_setFunctionUtimes(context, SMBC_utimes_ctx);
218 smbc_setFunctionSetxattr(context, SMBC_setxattr_ctx);
219 smbc_setFunctionGetxattr(context, SMBC_getxattr_ctx);
220 smbc_setFunctionRemovexattr(context, SMBC_removexattr_ctx);
221 smbc_setFunctionListxattr(context, SMBC_listxattr_ctx);
223 smbc_setFunctionOpenPrintJob(context, SMBC_open_print_job_ctx);
224 smbc_setFunctionPrintFile(context, SMBC_print_file_ctx);
225 smbc_setFunctionListPrintJobs(context, SMBC_list_print_jobs_ctx);
226 smbc_setFunctionUnlinkPrintJob(context, SMBC_unlink_print_job_ctx);
228 TALLOC_FREE(frame);
229 return context;
233 * Free a context
235 * Returns 0 on success. Otherwise returns 1, the SMBCCTX is _not_ freed
236 * and thus you'll be leaking memory if not handled properly.
240 smbc_free_context(SMBCCTX *context,
241 int shutdown_ctx)
243 TALLOC_CTX *frame;
244 if (!context) {
245 errno = EBADF;
246 return 1;
249 frame = talloc_stackframe();
251 if (shutdown_ctx) {
252 SMBCFILE * f;
253 DEBUG(1,("Performing aggressive shutdown.\n"));
255 f = context->internal->files;
256 while (f) {
257 smbc_getFunctionClose(context)(context, f);
258 f = f->next;
260 context->internal->files = NULL;
262 /* First try to remove the servers the nice way. */
263 if (smbc_getFunctionPurgeCachedServers(context)(context)) {
264 SMBCSRV * s;
265 SMBCSRV * next;
266 DEBUG(1, ("Could not purge all servers, "
267 "Nice way shutdown failed.\n"));
268 s = context->internal->servers;
269 while (s) {
270 DEBUG(1, ("Forced shutdown: %p (cli=%p)\n",
271 s, s->cli));
272 cli_shutdown(s->cli);
273 smbc_getFunctionRemoveCachedServer(context)(context,
275 next = s->next;
276 DLIST_REMOVE(context->internal->servers, s);
277 SAFE_FREE(s);
278 s = next;
280 context->internal->servers = NULL;
283 else {
284 /* This is the polite way */
285 if (smbc_getFunctionPurgeCachedServers(context)(context)) {
286 DEBUG(1, ("Could not purge all servers, "
287 "free_context failed.\n"));
288 errno = EBUSY;
289 TALLOC_FREE(frame);
290 return 1;
292 if (context->internal->servers) {
293 DEBUG(1, ("Active servers in context, "
294 "free_context failed.\n"));
295 errno = EBUSY;
296 TALLOC_FREE(frame);
297 return 1;
299 if (context->internal->files) {
300 DEBUG(1, ("Active files in context, "
301 "free_context failed.\n"));
302 errno = EBUSY;
303 TALLOC_FREE(frame);
304 return 1;
308 /* Things we have to clean up */
309 smbc_setWorkgroup(context, NULL);
310 smbc_setNetbiosName(context, NULL);
311 smbc_setUser(context, NULL);
313 DEBUG(3, ("Context %p successfully freed\n", context));
315 /* Free any DFS auth context. */
316 TALLOC_FREE(context->internal->auth_info);
318 SAFE_FREE(context->internal);
319 SAFE_FREE(context);
321 /* Protect access to the count of contexts in use */
322 if (SMB_THREAD_LOCK(initialized_ctx_count_mutex) != 0) {
323 smb_panic("error locking 'initialized_ctx_count'");
326 if (initialized_ctx_count) {
327 initialized_ctx_count--;
330 if (initialized_ctx_count == 0) {
331 SMBC_module_terminate();
334 /* Unlock the mutex */
335 if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex) != 0) {
336 smb_panic("error unlocking 'initialized_ctx_count'");
339 TALLOC_FREE(frame);
340 return 0;
345 * Deprecated interface. Do not use. Instead, use the various
346 * smbc_setOption*() functions or smbc_setFunctionAuthDataWithContext().
348 void
349 smbc_option_set(SMBCCTX *context,
350 char *option_name,
351 ... /* option_value */)
353 va_list ap;
354 union {
355 int i;
356 bool b;
357 smbc_get_auth_data_with_context_fn auth_fn;
358 void *v;
359 const char *s;
360 } option_value;
362 TALLOC_CTX *frame = talloc_stackframe();
364 va_start(ap, option_name);
366 if (strcmp(option_name, "debug_to_stderr") == 0) {
367 option_value.b = (bool) va_arg(ap, int);
368 smbc_setOptionDebugToStderr(context, option_value.b);
370 } else if (strcmp(option_name, "full_time_names") == 0) {
371 option_value.b = (bool) va_arg(ap, int);
372 smbc_setOptionFullTimeNames(context, option_value.b);
374 } else if (strcmp(option_name, "open_share_mode") == 0) {
375 option_value.i = va_arg(ap, int);
376 smbc_setOptionOpenShareMode(context, option_value.i);
378 } else if (strcmp(option_name, "auth_function") == 0) {
379 option_value.auth_fn =
380 va_arg(ap, smbc_get_auth_data_with_context_fn);
381 smbc_setFunctionAuthDataWithContext(context, option_value.auth_fn);
383 } else if (strcmp(option_name, "user_data") == 0) {
384 option_value.v = va_arg(ap, void *);
385 smbc_setOptionUserData(context, option_value.v);
387 } else if (strcmp(option_name, "smb_encrypt_level") == 0) {
388 option_value.s = va_arg(ap, const char *);
389 if (strcmp(option_value.s, "none") == 0) {
390 smbc_setOptionSmbEncryptionLevel(context,
391 SMBC_ENCRYPTLEVEL_NONE);
392 } else if (strcmp(option_value.s, "request") == 0) {
393 smbc_setOptionSmbEncryptionLevel(context,
394 SMBC_ENCRYPTLEVEL_REQUEST);
395 } else if (strcmp(option_value.s, "require") == 0) {
396 smbc_setOptionSmbEncryptionLevel(context,
397 SMBC_ENCRYPTLEVEL_REQUIRE);
400 } else if (strcmp(option_name, "browse_max_lmb_count") == 0) {
401 option_value.i = va_arg(ap, int);
402 smbc_setOptionBrowseMaxLmbCount(context, option_value.i);
404 } else if (strcmp(option_name, "urlencode_readdir_entries") == 0) {
405 option_value.b = (bool) va_arg(ap, int);
406 smbc_setOptionUrlEncodeReaddirEntries(context, option_value.b);
408 } else if (strcmp(option_name, "one_share_per_server") == 0) {
409 option_value.b = (bool) va_arg(ap, int);
410 smbc_setOptionOneSharePerServer(context, option_value.b);
412 } else if (strcmp(option_name, "use_kerberos") == 0) {
413 option_value.b = (bool) va_arg(ap, int);
414 smbc_setOptionUseKerberos(context, option_value.b);
416 } else if (strcmp(option_name, "fallback_after_kerberos") == 0) {
417 option_value.b = (bool) va_arg(ap, int);
418 smbc_setOptionFallbackAfterKerberos(context, option_value.b);
420 } else if (strcmp(option_name, "use_ccache") == 0) {
421 option_value.b = (bool) va_arg(ap, int);
422 smbc_setOptionUseCCache(context, option_value.b);
424 } else if (strcmp(option_name, "no_auto_anonymous_login") == 0) {
425 option_value.b = (bool) va_arg(ap, int);
426 smbc_setOptionNoAutoAnonymousLogin(context, option_value.b);
429 va_end(ap);
430 TALLOC_FREE(frame);
435 * Deprecated interface. Do not use. Instead, use the various
436 * smbc_getOption*() functions.
438 void *
439 smbc_option_get(SMBCCTX *context,
440 char *option_name)
442 if (strcmp(option_name, "debug_stderr") == 0) {
443 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
444 return (void *) (intptr_t) smbc_getOptionDebugToStderr(context);
445 #else
446 return (void *) smbc_getOptionDebugToStderr(context);
447 #endif
449 } else if (strcmp(option_name, "full_time_names") == 0) {
450 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
451 return (void *) (intptr_t) smbc_getOptionFullTimeNames(context);
452 #else
453 return (void *) smbc_getOptionFullTimeNames(context);
454 #endif
456 } else if (strcmp(option_name, "open_share_mode") == 0) {
457 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
458 return (void *) (intptr_t) smbc_getOptionOpenShareMode(context);
459 #else
460 return (void *) smbc_getOptionOpenShareMode(context);
461 #endif
463 } else if (strcmp(option_name, "auth_function") == 0) {
464 return (void *) smbc_getFunctionAuthDataWithContext(context);
466 } else if (strcmp(option_name, "user_data") == 0) {
467 return smbc_getOptionUserData(context);
469 } else if (strcmp(option_name, "smb_encrypt_level") == 0) {
470 switch(smbc_getOptionSmbEncryptionLevel(context))
472 case 0:
473 return discard_const_p(void, "none");
474 case 1:
475 return discard_const_p(void, "request");
476 case 2:
477 return discard_const_p(void, "require");
480 } else if (strcmp(option_name, "smb_encrypt_on") == 0) {
481 SMBCSRV *s;
482 unsigned int num_servers = 0;
484 for (s = context->internal->servers; s; s = s->next) {
485 num_servers++;
486 if (!smb1cli_conn_encryption_on(s->cli->conn)) {
487 return (void *)false;
490 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
491 return (void *) (intptr_t) (bool) (num_servers > 0);
492 #else
493 return (void *) (bool) (num_servers > 0);
494 #endif
496 } else if (strcmp(option_name, "browse_max_lmb_count") == 0) {
497 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
498 return (void *) (intptr_t) smbc_getOptionBrowseMaxLmbCount(context);
499 #else
500 return (void *) smbc_getOptionBrowseMaxLmbCount(context);
501 #endif
503 } else if (strcmp(option_name, "urlencode_readdir_entries") == 0) {
504 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
505 return (void *)(intptr_t) smbc_getOptionUrlEncodeReaddirEntries(context);
506 #else
507 return (void *) (bool) smbc_getOptionUrlEncodeReaddirEntries(context);
508 #endif
510 } else if (strcmp(option_name, "one_share_per_server") == 0) {
511 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
512 return (void *) (intptr_t) smbc_getOptionOneSharePerServer(context);
513 #else
514 return (void *) (bool) smbc_getOptionOneSharePerServer(context);
515 #endif
517 } else if (strcmp(option_name, "use_kerberos") == 0) {
518 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
519 return (void *) (intptr_t) smbc_getOptionUseKerberos(context);
520 #else
521 return (void *) (bool) smbc_getOptionUseKerberos(context);
522 #endif
524 } else if (strcmp(option_name, "fallback_after_kerberos") == 0) {
525 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
526 return (void *)(intptr_t) smbc_getOptionFallbackAfterKerberos(context);
527 #else
528 return (void *) (bool) smbc_getOptionFallbackAfterKerberos(context);
529 #endif
531 } else if (strcmp(option_name, "use_ccache") == 0) {
532 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
533 return (void *) (intptr_t) smbc_getOptionUseCCache(context);
534 #else
535 return (void *) (bool) smbc_getOptionUseCCache(context);
536 #endif
538 } else if (strcmp(option_name, "no_auto_anonymous_login") == 0) {
539 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
540 return (void *) (intptr_t) smbc_getOptionNoAutoAnonymousLogin(context);
541 #else
542 return (void *) (bool) smbc_getOptionNoAutoAnonymousLogin(context);
543 #endif
546 return NULL;
551 * Initialize the library, etc.
553 * We accept a struct containing handle information.
554 * valid values for info->debug from 0 to 100,
555 * and insist that info->fn must be non-null.
557 SMBCCTX *
558 smbc_init_context(SMBCCTX *context)
560 int pid;
561 TALLOC_CTX *frame;
563 if (!context) {
564 errno = EBADF;
565 return NULL;
568 /* Do not initialise the same client twice */
569 if (context->internal->initialized) {
570 return NULL;
573 frame = talloc_stackframe();
575 if ((!smbc_getFunctionAuthData(context) &&
576 !smbc_getFunctionAuthDataWithContext(context)) ||
577 smbc_getDebug(context) < 0 ||
578 smbc_getDebug(context) > 100) {
580 TALLOC_FREE(frame);
581 errno = EINVAL;
582 return NULL;
586 if (!smbc_getUser(context)) {
588 * FIXME: Is this the best way to get the user info?
590 char *user = getenv("USER");
591 /* walk around as "guest" if no username can be found */
592 if (!user) {
593 user = SMB_STRDUP("guest");
594 } else {
595 user = SMB_STRDUP(user);
598 if (!user) {
599 TALLOC_FREE(frame);
600 errno = ENOMEM;
601 return NULL;
604 smbc_setUser(context, user);
605 SAFE_FREE(user);
607 if (!smbc_getUser(context)) {
608 TALLOC_FREE(frame);
609 errno = ENOMEM;
610 return NULL;
614 if (!smbc_getNetbiosName(context)) {
616 * We try to get our netbios name from the config. If that
617 * fails we fall back on constructing our netbios name from
618 * our hostname etc
620 char *netbios_name;
621 if (lp_netbios_name()) {
622 netbios_name = SMB_STRDUP(lp_netbios_name());
623 } else {
625 * Hmmm, I want to get hostname as well, but I am too
626 * lazy for the moment
628 pid = getpid();
629 netbios_name = (char *)SMB_MALLOC(17);
630 if (!netbios_name) {
631 TALLOC_FREE(frame);
632 errno = ENOMEM;
633 return NULL;
635 slprintf(netbios_name, 16,
636 "smbc%s%d", smbc_getUser(context), pid);
639 if (!netbios_name) {
640 TALLOC_FREE(frame);
641 errno = ENOMEM;
642 return NULL;
645 smbc_setNetbiosName(context, netbios_name);
646 SAFE_FREE(netbios_name);
648 if (!smbc_getNetbiosName(context)) {
649 TALLOC_FREE(frame);
650 errno = ENOMEM;
651 return NULL;
655 DEBUG(1, ("Using netbios name %s.\n", smbc_getNetbiosName(context)));
657 if (!smbc_getWorkgroup(context)) {
658 char *workgroup;
660 if (lp_workgroup()) {
661 workgroup = SMB_STRDUP(lp_workgroup());
663 else {
664 /* TODO: Think about a decent default workgroup */
665 workgroup = SMB_STRDUP("samba");
668 if (!workgroup) {
669 TALLOC_FREE(frame);
670 errno = ENOMEM;
671 return NULL;
674 smbc_setWorkgroup(context, workgroup);
675 SAFE_FREE(workgroup);
677 if (!smbc_getWorkgroup(context)) {
678 TALLOC_FREE(frame);
679 errno = ENOMEM;
680 return NULL;
684 DEBUG(1, ("Using workgroup %s.\n", smbc_getWorkgroup(context)));
686 /* shortest timeout is 1 second */
687 if (smbc_getTimeout(context) > 0 && smbc_getTimeout(context) < 1000)
688 smbc_setTimeout(context, 1000);
690 context->internal->initialized = True;
692 /* Protect access to the count of contexts in use */
693 if (SMB_THREAD_LOCK(initialized_ctx_count_mutex) != 0) {
694 smb_panic("error locking 'initialized_ctx_count'");
697 initialized_ctx_count++;
699 /* Unlock the mutex */
700 if (SMB_THREAD_UNLOCK(initialized_ctx_count_mutex) != 0) {
701 smb_panic("error unlocking 'initialized_ctx_count'");
704 TALLOC_FREE(frame);
705 return context;
709 /* Return the verion of samba, and thus libsmbclient */
710 const char *
711 smbc_version(void)
713 return samba_version_string();
717 * Set the credentials so DFS will work when following referrals.
718 * This function is broken and must be removed. No SMBCCTX arg...
719 * JRA.
722 void
723 smbc_set_credentials(const char *workgroup,
724 const char *user,
725 const char *password,
726 smbc_bool use_kerberos,
727 const char *signing_state)
729 d_printf("smbc_set_credentials is obsolete. Replace with smbc_set_credentials_with_fallback().\n");
732 void smbc_set_credentials_with_fallback(SMBCCTX *context,
733 const char *workgroup,
734 const char *user,
735 const char *password)
737 smbc_bool use_kerberos = false;
738 const char *signing_state = "off";
739 struct user_auth_info *auth_info = NULL;
740 TALLOC_CTX *frame;
742 if (! context) {
744 return;
747 frame = talloc_stackframe();
749 if (! workgroup || ! *workgroup) {
750 workgroup = smbc_getWorkgroup(context);
753 if (! user) {
754 user = smbc_getUser(context);
757 if (! password) {
758 password = "";
761 auth_info = user_auth_info_init(NULL);
763 if (! auth_info) {
764 DEBUG(0, ("smbc_set_credentials_with_fallback: allocation fail\n"));
765 TALLOC_FREE(frame);
766 return;
769 if (smbc_getOptionUseKerberos(context)) {
770 use_kerberos = True;
773 if (lp_client_signing() != SMB_SIGNING_OFF) {
774 signing_state = "if_required";
777 if (lp_client_signing() == SMB_SIGNING_REQUIRED) {
778 signing_state = "required";
781 set_cmdline_auth_info_username(auth_info, user);
782 set_cmdline_auth_info_domain(auth_info, workgroup);
783 set_cmdline_auth_info_password(auth_info, password);
784 set_cmdline_auth_info_use_kerberos(auth_info, use_kerberos);
785 set_cmdline_auth_info_signing_state(auth_info, signing_state);
786 set_cmdline_auth_info_fallback_after_kerberos(auth_info,
787 smbc_getOptionFallbackAfterKerberos(context));
788 set_cmdline_auth_info_use_ccache(
789 auth_info, smbc_getOptionUseCCache(context));
791 TALLOC_FREE(context->internal->auth_info);
793 context->internal->auth_info = auth_info;
794 TALLOC_FREE(frame);