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/>.
26 #include "libsmb/libsmb.h"
27 #include "libsmbclient.h"
28 #include "libsmb_internal.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
48 SMBC_module_init(void * punused
)
50 bool conf_loaded
= False
;
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");
61 if (asprintf(&conf
, "%s/.smb/smb.conf", home
) > 0) {
62 if (lp_load_client(conf
)) {
65 DEBUG(5, ("Could not load config file: %s\n",
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
80 if (!lp_load_client(get_dyn_CONFIGFILE())) {
81 DEBUG(5, ("Could not load config file: %s\n",
82 get_dyn_CONFIGFILE()));
86 * We loaded the global config file. Now lets
87 * load user-specific modifications to the
91 "%s/.smb/smb.conf.append",
93 if (!lp_load_client_no_reinit(conf
)) {
95 ("Could not append config file: "
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");
126 SMBC_module_terminate(void)
128 TALLOC_CTX
*frame
= talloc_stackframe();
131 SMBC_initialized
= false;
137 * Get a new empty handle to fill in with your own info
140 smbc_new_context(void)
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
);
159 ZERO_STRUCTP(context
);
161 context
->internal
= SMB_MALLOC_P(struct SMBC_internal_data
);
162 if (!context
->internal
) {
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
);
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
);
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
,
267 frame
= talloc_stackframe();
271 DEBUG(1,("Performing aggressive shutdown.\n"));
273 f
= context
->internal
->files
;
275 SMBCFILE
*next
= f
->next
;
276 smbc_getFunctionClose(context
)(context
, f
);
279 context
->internal
->files
= NULL
;
281 /* First try to remove the servers the nice way. */
282 if (smbc_getFunctionPurgeCachedServers(context
)(context
)) {
285 DEBUG(1, ("Could not purge all servers, "
286 "Nice way shutdown failed.\n"));
287 s
= context
->internal
->servers
;
289 DEBUG(1, ("Forced shutdown: %p (cli=%p)\n",
291 cli_shutdown(s
->cli
);
292 smbc_getFunctionRemoveCachedServer(context
)(context
,
295 DLIST_REMOVE(context
->internal
->servers
, s
);
299 context
->internal
->servers
= NULL
;
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"));
311 if (context
->internal
->servers
) {
312 DEBUG(1, ("Active servers in context, "
313 "free_context failed.\n"));
318 if (context
->internal
->files
) {
319 DEBUG(1, ("Active files in context, "
320 "free_context failed.\n"));
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
);
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'");
365 * Deprecated interface. Do not use. Instead, use the various
366 * smbc_setOption*() functions or smbc_setFunctionAuthDataWithContext().
369 smbc_option_set(SMBCCTX
*context
,
371 ... /* option_value */)
377 smbc_get_auth_data_with_context_fn auth_fn
;
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
);
455 * Deprecated interface. Do not use. Instead, use the various
456 * smbc_getOption*() functions.
459 smbc_option_get(SMBCCTX
*context
,
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
);
466 return (void *) smbc_getOptionDebugToStderr(context
);
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
);
473 return (void *) smbc_getOptionFullTimeNames(context
);
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
);
480 return (void *) smbc_getOptionOpenShareMode(context
);
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");
495 return discard_const_p(void, "none");
497 return discard_const_p(void, "request");
499 return discard_const_p(void, "require");
502 } else if (strcmp(option_name
, "smb_encrypt_on") == 0) {
504 unsigned int num_servers
= 0;
506 for (s
= context
->internal
->servers
; s
; s
= s
->next
) {
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);
515 return (void *) (bool) (num_servers
> 0);
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
);
522 return (void *) smbc_getOptionBrowseMaxLmbCount(context
);
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
);
529 return (void *) (bool) smbc_getOptionUrlEncodeReaddirEntries(context
);
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
);
536 return (void *) (bool) smbc_getOptionOneSharePerServer(context
);
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
);
543 return (void *) (bool) smbc_getOptionUseKerberos(context
);
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
);
550 return (void *) (bool) smbc_getOptionFallbackAfterKerberos(context
);
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
);
557 return (void *) (bool) smbc_getOptionUseCCache(context
);
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
);
564 return (void *) (bool) smbc_getOptionNoAutoAnonymousLogin(context
);
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.
580 smbc_init_context(SMBCCTX
*context
)
590 /* Do not initialise the same client twice */
591 if (context
->internal
->initialized
) {
595 frame
= talloc_stackframe();
597 if ((!smbc_getFunctionAuthData(context
) &&
598 !smbc_getFunctionAuthDataWithContext(context
)) ||
599 smbc_getDebug(context
) < 0 ||
600 smbc_getDebug(context
) > 100) {
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 */
615 user
= SMB_STRDUP("guest");
617 user
= SMB_STRDUP(user
);
626 smbc_setUser(context
, user
);
629 if (!smbc_getUser(context
)) {
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
643 if (lp_netbios_name()) {
644 netbios_name
= SMB_STRDUP(lp_netbios_name());
647 * Hmmm, I want to get hostname as well, but I am too
648 * lazy for the moment
651 netbios_name
= (char *)SMB_MALLOC(17);
657 slprintf(netbios_name
, 16,
658 "smbc%s%d", smbc_getUser(context
), pid
);
667 smbc_setNetbiosName(context
, netbios_name
);
668 SAFE_FREE(netbios_name
);
670 if (!smbc_getNetbiosName(context
)) {
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();
685 /* TODO: Think about a decent default workgroup */
689 smbc_setWorkgroup(context
, workgroup
);
691 if (!smbc_getWorkgroup(context
)) {
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'");
723 /* Return the version of samba, and thus libsmbclient */
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...
737 smbc_set_credentials(const char *workgroup
,
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
,
749 const char *password
)
751 struct cli_credentials
*creds
= NULL
;
752 enum credentials_use_kerberos kerberos_state
=
753 CRED_USE_KERBEROS_DISABLED
;
760 if (! workgroup
|| ! *workgroup
) {
761 workgroup
= smbc_getWorkgroup(context
);
765 user
= smbc_getUser(context
);
772 creds
= cli_credentials_init(NULL
);
774 DEBUG(0, ("smbc_set_credentials_with_fallback: allocation fail\n"));
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
,
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
,
804 TALLOC_FREE(context
->internal
->creds
);
805 context
->internal
->creds
= creds
;