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
10 Copyright (C) SATOH Fumiyasu <fumiyas@osstech.co.jp> 2009.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "libsmb/libsmb.h"
28 #include "libsmbclient.h"
29 #include "libsmb_internal.h"
30 #include "../librpc/gen_ndr/ndr_lsa.h"
31 #include "rpc_client/cli_pipe.h"
32 #include "rpc_client/cli_lsarpc.h"
33 #include "libcli/security/security.h"
34 #include "libsmb/nmblib.h"
35 #include "../libcli/smb/smbXcli_base.h"
38 * Check a server for being alive and well.
39 * returns 0 if the server is in shape. Returns 1 on error
41 * Also useable outside libsmbclient to enable external cache
42 * to do some checks too.
45 SMBC_check_server(SMBCCTX
* context
,
50 if (!cli_state_is_connected(server
->cli
)) {
54 now
= time_mono(NULL
);
56 if (server
->last_echo_time
== (time_t)0 ||
57 now
> server
->last_echo_time
+
58 (server
->cli
->timeout
/1000)) {
59 unsigned char data
[16] = {0};
60 NTSTATUS status
= cli_echo(server
->cli
,
62 data_blob_const(data
, sizeof(data
)));
63 if (!NT_STATUS_IS_OK(status
)) {
65 * Some NetApp servers return
66 * NT_STATUS_INVALID_PARAMETER.That's OK, they still
68 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13007
70 if (!NT_STATUS_EQUAL(status
,
71 NT_STATUS_INVALID_PARAMETER
)) {
75 server
->last_echo_time
= now
;
81 * Remove a server from the cached server list it's unused.
82 * On success, 0 is returned. 1 is returned if the server could not be removed.
84 * Also useable outside libsmbclient
87 SMBC_remove_unused_server(SMBCCTX
* context
,
92 /* are we being fooled ? */
93 if (!context
|| !context
->internal
->initialized
|| !srv
) {
97 /* Check all open files/directories for a relation with this server */
98 for (file
= context
->internal
->files
; file
; file
= file
->next
) {
99 if (file
->srv
== srv
) {
101 DEBUG(3, ("smbc_remove_usused_server: "
102 "%p still used by %p.\n",
108 DLIST_REMOVE(context
->internal
->servers
, srv
);
110 cli_shutdown(srv
->cli
);
113 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv
));
115 smbc_getFunctionRemoveCachedServer(context
)(context
, srv
);
121 /****************************************************************
122 * Call the auth_fn with fixed size (fstring) buffers.
123 ***************************************************************/
125 SMBC_call_auth_fn(TALLOC_CTX
*ctx
,
133 fstring workgroup
= { 0 };
134 fstring username
= { 0 };
135 fstring password
= { 0 };
136 smbc_get_auth_data_with_context_fn auth_with_context_fn
;
138 if (*pp_workgroup
!= NULL
) {
139 strlcpy(workgroup
, *pp_workgroup
, sizeof(workgroup
));
141 if (*pp_username
!= NULL
) {
142 strlcpy(username
, *pp_username
, sizeof(username
));
144 if (*pp_password
!= NULL
) {
145 strlcpy(password
, *pp_password
, sizeof(password
));
148 /* See if there's an authentication with context function provided */
149 auth_with_context_fn
= smbc_getFunctionAuthDataWithContext(context
);
150 if (auth_with_context_fn
)
152 (* auth_with_context_fn
)(context
,
154 workgroup
, sizeof(workgroup
),
155 username
, sizeof(username
),
156 password
, sizeof(password
));
160 smbc_getFunctionAuthData(context
)(server
, share
,
161 workgroup
, sizeof(workgroup
),
162 username
, sizeof(username
),
163 password
, sizeof(password
));
166 TALLOC_FREE(*pp_workgroup
);
167 TALLOC_FREE(*pp_username
);
168 TALLOC_FREE(*pp_password
);
170 *pp_workgroup
= talloc_strdup(ctx
, workgroup
);
171 *pp_username
= talloc_strdup(ctx
, username
);
172 *pp_password
= talloc_strdup(ctx
, password
);
177 SMBC_get_auth_data(const char *server
, const char *share
,
178 char *workgroup_buf
, int workgroup_buf_len
,
179 char *username_buf
, int username_buf_len
,
180 char *password_buf
, int password_buf_len
)
182 /* Default function just uses provided data. Nothing to do. */
188 SMBC_find_server(TALLOC_CTX
*ctx
,
199 if (!pp_workgroup
|| !pp_username
|| !pp_password
) {
205 srv
= smbc_getFunctionGetCachedServer(context
)(context
,
210 if (!auth_called
&& !srv
&& (!*pp_username
|| !(*pp_username
)[0] ||
211 !*pp_password
|| !(*pp_password
)[0])) {
212 SMBC_call_auth_fn(ctx
, context
, server
, share
,
213 pp_workgroup
, pp_username
, pp_password
);
216 * However, smbc_auth_fn may have picked up info relating to
217 * an existing connection, so try for an existing connection
221 goto check_server_cache
;
226 if (smbc_getFunctionCheckServer(context
)(context
, srv
)) {
228 * This server is no good anymore
229 * Try to remove it and check for more possible
230 * servers in the cache
232 if (smbc_getFunctionRemoveUnusedServer(context
)(context
,
235 * We could not remove the server completely,
236 * remove it from the cache so we will not get
237 * it again. It will be removed when the last
238 * file/dir is closed.
240 smbc_getFunctionRemoveCachedServer(context
)(context
,
245 * Maybe there are more cached connections to this
248 goto check_server_cache
;
258 * Connect to a server, possibly on an existing connection
260 * Here, what we want to do is: If the server and username
261 * match an existing connection, reuse that, otherwise, establish a
264 * If we have to create a new connection, call the auth_fn to get the
265 * info we need, unless the username and password were passed in.
269 SMBC_server_internal(TALLOC_CTX
*ctx
,
271 bool connect_if_not_found
,
281 char *workgroup
= NULL
;
282 struct cli_state
*c
= NULL
;
283 const char *server_n
= server
;
284 int is_ipc
= (share
!= NULL
&& strcmp(share
, "IPC$") == 0);
285 uint32_t fs_attrs
= 0;
286 const char *username_used
= NULL
;
287 const char *password_used
= NULL
;
289 char *newserver
, *newshare
;
291 struct smbXcli_tcon
*tcon
= NULL
;
292 int signing_state
= SMB_SIGNING_DEFAULT
;
293 struct cli_credentials
*creds
= NULL
;
294 bool use_kerberos
= false;
295 bool fallback_after_kerberos
= false;
296 bool use_ccache
= false;
297 bool pw_nt_hash
= false;
302 if (server
[0] == 0) {
307 /* Look for a cached connection */
308 srv
= SMBC_find_server(ctx
, context
, server
, share
,
309 pp_workgroup
, pp_username
, pp_password
);
312 * If we found a connection and we're only allowed one share per
316 share
!= NULL
&& *share
!= '\0' &&
317 smbc_getOptionOneSharePerServer(context
)) {
320 * ... then if there's no current connection to the share,
321 * connect to it. SMBC_find_server(), or rather the function
322 * pointed to by context->get_cached_srv_fn which
323 * was called by SMBC_find_server(), will have issued a tree
324 * disconnect if the requested share is not the same as the
325 * one that was already connected.
329 * Use srv->cli->desthost and srv->cli->share instead of
330 * server and share below to connect to the actual share,
331 * i.e., a normal share or a referred share from
332 * 'msdfs proxy' share.
334 if (!cli_state_has_tcon(srv
->cli
)) {
335 /* Ensure we have accurate auth info */
336 SMBC_call_auth_fn(ctx
, context
,
337 smbXcli_conn_remote_name(srv
->cli
->conn
),
343 if (!*pp_workgroup
|| !*pp_username
|| !*pp_password
) {
345 cli_shutdown(srv
->cli
);
347 smbc_getFunctionRemoveCachedServer(context
)(context
,
353 * We don't need to renegotiate encryption
354 * here as the encryption context is not per
358 status
= cli_tree_connect(srv
->cli
,
362 if (!NT_STATUS_IS_OK(status
)) {
363 cli_shutdown(srv
->cli
);
364 errno
= map_errno_from_nt_status(status
);
366 smbc_getFunctionRemoveCachedServer(context
)(context
,
371 /* Determine if this share supports case sensitivity */
374 ("IPC$ so ignore case sensitivity\n"));
375 status
= NT_STATUS_OK
;
377 status
= cli_get_fs_attr_info(c
, &fs_attrs
);
380 if (!NT_STATUS_IS_OK(status
)) {
381 DEBUG(4, ("Could not retrieve "
382 "case sensitivity flag: %s.\n",
386 * We can't determine the case sensitivity of
387 * the share. We have no choice but to use the
388 * user-specified case sensitivity setting.
390 if (smbc_getOptionCaseSensitive(context
)) {
391 cli_set_case_sensitive(c
, True
);
393 cli_set_case_sensitive(c
, False
);
395 } else if (!is_ipc
) {
397 ("Case sensitive: %s\n",
398 (fs_attrs
& FILE_CASE_SENSITIVE_SEARCH
401 cli_set_case_sensitive(
403 (fs_attrs
& FILE_CASE_SENSITIVE_SEARCH
409 * Regenerate the dev value since it's based on both
413 const char *remote_name
=
414 smbXcli_conn_remote_name(srv
->cli
->conn
);
416 srv
->dev
= (dev_t
)(str_checksum(remote_name
) ^
417 str_checksum(srv
->cli
->share
));
422 /* If we have a connection... */
425 /* ... then we're done here. Give 'em what they came for. */
430 /* If we're not asked to connect when a connection doesn't exist... */
431 if (! connect_if_not_found
) {
432 /* ... then we're done here. */
436 if (!*pp_workgroup
|| !*pp_username
|| !*pp_password
) {
441 DEBUG(4,("SMBC_server: server_n=[%s] server=[%s]\n", server_n
, server
));
443 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n
, server
));
445 status
= NT_STATUS_UNSUCCESSFUL
;
447 if (smbc_getOptionUseKerberos(context
)) {
448 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
452 if (smbc_getOptionFallbackAfterKerberos(context
)) {
453 flags
|= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
454 fallback_after_kerberos
= true;
457 if (smbc_getOptionUseCCache(context
)) {
458 flags
|= CLI_FULL_CONNECTION_USE_CCACHE
;
462 if (smbc_getOptionUseNTHash(context
)) {
463 flags
|= CLI_FULL_CONNECTION_USE_NT_HASH
;
467 if (context
->internal
->smb_encryption_level
!= SMBC_ENCRYPTLEVEL_NONE
) {
468 signing_state
= SMB_SIGNING_REQUIRED
;
472 if (share
== NULL
|| *share
== '\0' || is_ipc
) {
474 * Try 139 first for IPC$
476 status
= cli_connect_nb(server_n
, NULL
, NBT_SMB_PORT
, 0x20,
477 smbc_getNetbiosName(context
),
478 signing_state
, flags
, &c
);
482 if (!NT_STATUS_IS_OK(status
)) {
484 * No IPC$ or 139 did not work
486 status
= cli_connect_nb(server_n
, NULL
, port
, 0x20,
487 smbc_getNetbiosName(context
),
488 signing_state
, flags
, &c
);
491 if (!NT_STATUS_IS_OK(status
)) {
492 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_SUPPORTED
)) {
493 DBG_ERR("NetBIOS support disabled, unable to connect");
496 errno
= map_errno_from_nt_status(status
);
500 cli_set_timeout(c
, smbc_getTimeout(context
));
502 status
= smbXcli_negprot(c
->conn
, c
->timeout
,
503 lp_client_min_protocol(),
504 lp_client_max_protocol());
505 if (!NT_STATUS_IS_OK(status
)) {
507 errno
= map_errno_from_nt_status(status
);
511 if (smbXcli_conn_protocol(c
->conn
) >= PROTOCOL_SMB2_02
) {
512 /* Ensure we ask for some initial credits. */
513 smb2cli_conn_set_max_credits(c
->conn
, DEFAULT_SMB2_MAX_CREDITS
);
516 username_used
= *pp_username
;
517 password_used
= *pp_password
;
519 creds
= cli_session_creds_init(c
,
525 fallback_after_kerberos
,
534 status
= cli_session_setup_creds(c
, creds
);
535 if (!NT_STATUS_IS_OK(status
)) {
537 /* Failed. Try an anonymous login, if allowed by flags. */
541 if (smbc_getOptionNoAutoAnonymousLogin(context
) ||
542 !NT_STATUS_IS_OK(cli_session_setup_anon(c
))) {
550 DEBUG(4,(" session setup ok\n"));
552 /* here's the fun part....to support 'msdfs proxy' shares
553 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
554 here before trying to connect to the original share.
555 cli_check_msdfs_proxy() will fail if it is a normal share. */
557 if (smbXcli_conn_dfs_supported(c
->conn
) &&
558 cli_check_msdfs_proxy(ctx
, c
, share
,
559 &newserver
, &newshare
,
560 /* FIXME: cli_check_msdfs_proxy() does
561 not support smbc_smb_encrypt_level type */
562 context
->internal
->smb_encryption_level
?
566 srv
= SMBC_server_internal(ctx
, context
, connect_if_not_found
,
567 newserver
, port
, newshare
, pp_workgroup
,
568 pp_username
, pp_password
, in_cache
);
569 TALLOC_FREE(newserver
);
570 TALLOC_FREE(newshare
);
574 /* must be a normal share */
576 status
= cli_tree_connect_creds(c
, share
, "?????", creds
);
577 if (!NT_STATUS_IS_OK(status
)) {
579 errno
= map_errno_from_nt_status(status
);
583 DEBUG(4,(" tconx ok\n"));
585 if (smbXcli_conn_protocol(c
->conn
) >= PROTOCOL_SMB2_02
) {
591 /* Determine if this share supports case sensitivity */
593 DEBUG(4, ("IPC$ so ignore case sensitivity\n"));
594 status
= NT_STATUS_OK
;
596 status
= cli_get_fs_attr_info(c
, &fs_attrs
);
599 if (!NT_STATUS_IS_OK(status
)) {
600 DEBUG(4, ("Could not retrieve case sensitivity flag: %s.\n",
604 * We can't determine the case sensitivity of the share. We
605 * have no choice but to use the user-specified case
606 * sensitivity setting.
608 if (smbc_getOptionCaseSensitive(context
)) {
609 cli_set_case_sensitive(c
, True
);
611 cli_set_case_sensitive(c
, False
);
613 } else if (!is_ipc
) {
614 DEBUG(4, ("Case sensitive: %s\n",
615 (fs_attrs
& FILE_CASE_SENSITIVE_SEARCH
618 smbXcli_tcon_set_fs_attributes(tcon
, fs_attrs
);
621 if (context
->internal
->smb_encryption_level
) {
622 /* Attempt encryption. */
623 status
= cli_cm_force_encryption_creds(c
,
626 if (!NT_STATUS_IS_OK(status
)) {
629 * context->smb_encryption_level == 1
630 * means don't fail if encryption can't be negotiated,
631 * == 2 means fail if encryption can't be negotiated.
634 DEBUG(4,(" SMB encrypt failed\n"));
636 if (context
->internal
->smb_encryption_level
== 2) {
642 DEBUG(4,(" SMB encrypt ok\n"));
646 * Ok, we have got a nice connection
647 * Let's allocate a server structure.
650 srv
= SMB_MALLOC_P(SMBCSRV
);
658 DLIST_ADD(srv
->cli
, c
);
659 srv
->dev
= (dev_t
)(str_checksum(server
) ^ str_checksum(share
));
660 srv
->no_pathinfo
= False
;
661 srv
->no_pathinfo2
= False
;
662 srv
->no_pathinfo3
= False
;
663 srv
->no_nt_session
= False
;
666 if (!pp_workgroup
|| !*pp_workgroup
|| !**pp_workgroup
) {
667 workgroup
= talloc_strdup(ctx
, smbc_getWorkgroup(context
));
669 workgroup
= *pp_workgroup
;
679 /* set the credentials to make DFS work */
680 smbc_set_credentials_with_fallback(context
,
689 SMBC_server(TALLOC_CTX
*ctx
,
691 bool connect_if_not_found
,
700 bool in_cache
= false;
702 srv
= SMBC_server_internal(ctx
, context
, connect_if_not_found
,
703 server
, port
, share
, pp_workgroup
,
704 pp_username
, pp_password
, &in_cache
);
713 /* Now add it to the cache (internal or external) */
714 /* Let the cache function set errno if it wants to */
716 if (smbc_getFunctionAddCachedServer(context
)(context
, srv
,
720 int saved_errno
= errno
;
721 DEBUG(3, (" Failed to add server to cache\n"));
730 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
731 server
, share
, srv
));
733 DLIST_ADD(context
->internal
->servers
, srv
);
738 * Connect to a server for getting/setting attributes, possibly on an existing
739 * connection. This works similarly to SMBC_server().
742 SMBC_attr_server(TALLOC_CTX
*ctx
,
752 struct cli_state
*ipc_cli
= NULL
;
753 struct rpc_pipe_client
*pipe_hnd
= NULL
;
756 SMBCSRV
*ipc_srv
=NULL
;
759 * Use srv->cli->desthost and srv->cli->share instead of
760 * server and share below to connect to the actual share,
761 * i.e., a normal share or a referred share from
762 * 'msdfs proxy' share.
764 srv
= SMBC_server(ctx
, context
, true, server
, port
, share
,
765 pp_workgroup
, pp_username
, pp_password
);
769 server
= smbXcli_conn_remote_name(srv
->cli
->conn
);
770 share
= srv
->cli
->share
;
773 * See if we've already created this special connection. Reference
774 * our "special" share name '*IPC$', which is an impossible real share
775 * name due to the leading asterisk.
777 ipc_srv
= SMBC_find_server(ctx
, context
, server
, "*IPC$",
778 pp_workgroup
, pp_username
, pp_password
);
780 int signing_state
= SMB_SIGNING_DEFAULT
;
782 /* We didn't find a cached connection. Get the password */
783 if (!*pp_password
|| (*pp_password
)[0] == '\0') {
784 /* ... then retrieve it now. */
785 SMBC_call_auth_fn(ctx
, context
, server
, share
,
789 if (!*pp_workgroup
|| !*pp_username
|| !*pp_password
) {
796 if (smbc_getOptionUseKerberos(context
)) {
797 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
799 if (smbc_getOptionUseCCache(context
)) {
800 flags
|= CLI_FULL_CONNECTION_USE_CCACHE
;
802 if (context
->internal
->smb_encryption_level
!= SMBC_ENCRYPTLEVEL_NONE
) {
803 signing_state
= SMB_SIGNING_REQUIRED
;
806 nt_status
= cli_full_connection(&ipc_cli
,
807 lp_netbios_name(), server
,
808 NULL
, 0, "IPC$", "?????",
814 if (! NT_STATUS_IS_OK(nt_status
)) {
815 DEBUG(1,("cli_full_connection failed! (%s)\n",
816 nt_errstr(nt_status
)));
821 if (context
->internal
->smb_encryption_level
) {
822 /* Attempt encryption. */
823 nt_status
= cli_cm_force_encryption(ipc_cli
,
828 if (!NT_STATUS_IS_OK(nt_status
)) {
831 * context->smb_encryption_level ==
832 * 1 means don't fail if encryption can't be
833 * negotiated, == 2 means fail if encryption
834 * can't be negotiated.
837 DEBUG(4,(" SMB encrypt failed on IPC$\n"));
839 if (context
->internal
->smb_encryption_level
== 2) {
840 cli_shutdown(ipc_cli
);
845 DEBUG(4,(" SMB encrypt ok on IPC$\n"));
848 ipc_srv
= SMB_MALLOC_P(SMBCSRV
);
851 cli_shutdown(ipc_cli
);
855 ZERO_STRUCTP(ipc_srv
);
856 DLIST_ADD(ipc_srv
->cli
, ipc_cli
);
858 nt_status
= cli_rpc_pipe_open_noauth(
859 ipc_srv
->cli
, &ndr_table_lsarpc
, &pipe_hnd
);
860 if (!NT_STATUS_IS_OK(nt_status
)) {
861 DEBUG(1, ("cli_nt_session_open fail!\n"));
863 cli_shutdown(ipc_srv
->cli
);
869 * Some systems don't support
870 * SEC_FLAG_MAXIMUM_ALLOWED, but NT sends 0x2000000
871 * so we might as well do it too.
874 nt_status
= rpccli_lsa_open_policy(
878 GENERIC_EXECUTE_ACCESS
,
881 if (!NT_STATUS_IS_OK(nt_status
)) {
882 errno
= SMBC_errno(context
, ipc_srv
->cli
);
883 cli_shutdown(ipc_srv
->cli
);
888 /* now add it to the cache (internal or external) */
890 errno
= 0; /* let cache function set errno if it likes */
891 if (smbc_getFunctionAddCachedServer(context
)(context
, ipc_srv
,
896 DEBUG(3, (" Failed to add server to cache\n"));
900 cli_shutdown(ipc_srv
->cli
);
905 DLIST_ADD(context
->internal
->servers
, ipc_srv
);