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
)) {
66 server
->last_echo_time
= now
;
72 * Remove a server from the cached server list it's unused.
73 * On success, 0 is returned. 1 is returned if the server could not be removed.
75 * Also useable outside libsmbclient
78 SMBC_remove_unused_server(SMBCCTX
* context
,
83 /* are we being fooled ? */
84 if (!context
|| !context
->internal
->initialized
|| !srv
) {
88 /* Check all open files/directories for a relation with this server */
89 for (file
= context
->internal
->files
; file
; file
= file
->next
) {
90 if (file
->srv
== srv
) {
92 DEBUG(3, ("smbc_remove_usused_server: "
93 "%p still used by %p.\n",
99 DLIST_REMOVE(context
->internal
->servers
, srv
);
101 cli_shutdown(srv
->cli
);
104 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv
));
106 smbc_getFunctionRemoveCachedServer(context
)(context
, srv
);
112 /****************************************************************
113 * Call the auth_fn with fixed size (fstring) buffers.
114 ***************************************************************/
116 SMBC_call_auth_fn(TALLOC_CTX
*ctx
,
124 fstring workgroup
= { 0 };
125 fstring username
= { 0 };
126 fstring password
= { 0 };
127 smbc_get_auth_data_with_context_fn auth_with_context_fn
;
129 if (*pp_workgroup
!= NULL
) {
130 strlcpy(workgroup
, *pp_workgroup
, sizeof(workgroup
));
132 if (*pp_username
!= NULL
) {
133 strlcpy(username
, *pp_username
, sizeof(username
));
135 if (*pp_password
!= NULL
) {
136 strlcpy(password
, *pp_password
, sizeof(password
));
139 /* See if there's an authentication with context function provided */
140 auth_with_context_fn
= smbc_getFunctionAuthDataWithContext(context
);
141 if (auth_with_context_fn
)
143 (* auth_with_context_fn
)(context
,
145 workgroup
, sizeof(workgroup
),
146 username
, sizeof(username
),
147 password
, sizeof(password
));
151 smbc_getFunctionAuthData(context
)(server
, share
,
152 workgroup
, sizeof(workgroup
),
153 username
, sizeof(username
),
154 password
, sizeof(password
));
157 TALLOC_FREE(*pp_workgroup
);
158 TALLOC_FREE(*pp_username
);
159 TALLOC_FREE(*pp_password
);
161 *pp_workgroup
= talloc_strdup(ctx
, workgroup
);
162 *pp_username
= talloc_strdup(ctx
, username
);
163 *pp_password
= talloc_strdup(ctx
, password
);
168 SMBC_get_auth_data(const char *server
, const char *share
,
169 char *workgroup_buf
, int workgroup_buf_len
,
170 char *username_buf
, int username_buf_len
,
171 char *password_buf
, int password_buf_len
)
173 /* Default function just uses provided data. Nothing to do. */
179 SMBC_find_server(TALLOC_CTX
*ctx
,
190 if (!pp_workgroup
|| !pp_username
|| !pp_password
) {
196 srv
= smbc_getFunctionGetCachedServer(context
)(context
,
201 if (!auth_called
&& !srv
&& (!*pp_username
|| !(*pp_username
)[0] ||
202 !*pp_password
|| !(*pp_password
)[0])) {
203 SMBC_call_auth_fn(ctx
, context
, server
, share
,
204 pp_workgroup
, pp_username
, pp_password
);
207 * However, smbc_auth_fn may have picked up info relating to
208 * an existing connection, so try for an existing connection
212 goto check_server_cache
;
217 if (smbc_getFunctionCheckServer(context
)(context
, srv
)) {
219 * This server is no good anymore
220 * Try to remove it and check for more possible
221 * servers in the cache
223 if (smbc_getFunctionRemoveUnusedServer(context
)(context
,
226 * We could not remove the server completely,
227 * remove it from the cache so we will not get
228 * it again. It will be removed when the last
229 * file/dir is closed.
231 smbc_getFunctionRemoveCachedServer(context
)(context
,
236 * Maybe there are more cached connections to this
239 goto check_server_cache
;
249 * Connect to a server, possibly on an existing connection
251 * Here, what we want to do is: If the server and username
252 * match an existing connection, reuse that, otherwise, establish a
255 * If we have to create a new connection, call the auth_fn to get the
256 * info we need, unless the username and password were passed in.
260 SMBC_server_internal(TALLOC_CTX
*ctx
,
262 bool connect_if_not_found
,
272 char *workgroup
= NULL
;
273 struct cli_state
*c
= NULL
;
274 const char *server_n
= server
;
275 int is_ipc
= (share
!= NULL
&& strcmp(share
, "IPC$") == 0);
276 uint32_t fs_attrs
= 0;
277 const char *username_used
;
279 char *newserver
, *newshare
;
281 struct smbXcli_tcon
*tcon
= NULL
;
282 int signing_state
= SMB_SIGNING_DEFAULT
;
287 if (server
[0] == 0) {
292 /* Look for a cached connection */
293 srv
= SMBC_find_server(ctx
, context
, server
, share
,
294 pp_workgroup
, pp_username
, pp_password
);
297 * If we found a connection and we're only allowed one share per
301 share
!= NULL
&& *share
!= '\0' &&
302 smbc_getOptionOneSharePerServer(context
)) {
305 * ... then if there's no current connection to the share,
306 * connect to it. SMBC_find_server(), or rather the function
307 * pointed to by context->get_cached_srv_fn which
308 * was called by SMBC_find_server(), will have issued a tree
309 * disconnect if the requested share is not the same as the
310 * one that was already connected.
314 * Use srv->cli->desthost and srv->cli->share instead of
315 * server and share below to connect to the actual share,
316 * i.e., a normal share or a referred share from
317 * 'msdfs proxy' share.
319 if (!cli_state_has_tcon(srv
->cli
)) {
320 /* Ensure we have accurate auth info */
321 SMBC_call_auth_fn(ctx
, context
,
322 smbXcli_conn_remote_name(srv
->cli
->conn
),
328 if (!*pp_workgroup
|| !*pp_username
|| !*pp_password
) {
330 cli_shutdown(srv
->cli
);
332 smbc_getFunctionRemoveCachedServer(context
)(context
,
338 * We don't need to renegotiate encryption
339 * here as the encryption context is not per
343 status
= cli_tree_connect(srv
->cli
,
347 strlen(*pp_password
)+1);
348 if (!NT_STATUS_IS_OK(status
)) {
349 errno
= map_errno_from_nt_status(status
);
350 cli_shutdown(srv
->cli
);
352 smbc_getFunctionRemoveCachedServer(context
)(context
,
357 /* Determine if this share supports case sensitivity */
360 ("IPC$ so ignore case sensitivity\n"));
361 status
= NT_STATUS_OK
;
363 status
= cli_get_fs_attr_info(c
, &fs_attrs
);
366 if (!NT_STATUS_IS_OK(status
)) {
367 DEBUG(4, ("Could not retrieve "
368 "case sensitivity flag: %s.\n",
372 * We can't determine the case sensitivity of
373 * the share. We have no choice but to use the
374 * user-specified case sensitivity setting.
376 if (smbc_getOptionCaseSensitive(context
)) {
377 cli_set_case_sensitive(c
, True
);
379 cli_set_case_sensitive(c
, False
);
381 } else if (!is_ipc
) {
383 ("Case sensitive: %s\n",
384 (fs_attrs
& FILE_CASE_SENSITIVE_SEARCH
387 cli_set_case_sensitive(
389 (fs_attrs
& FILE_CASE_SENSITIVE_SEARCH
395 * Regenerate the dev value since it's based on both
399 const char *remote_name
=
400 smbXcli_conn_remote_name(srv
->cli
->conn
);
402 srv
->dev
= (dev_t
)(str_checksum(remote_name
) ^
403 str_checksum(srv
->cli
->share
));
408 /* If we have a connection... */
411 /* ... then we're done here. Give 'em what they came for. */
416 /* If we're not asked to connect when a connection doesn't exist... */
417 if (! connect_if_not_found
) {
418 /* ... then we're done here. */
422 if (!*pp_workgroup
|| !*pp_username
|| !*pp_password
) {
427 DEBUG(4,("SMBC_server: server_n=[%s] server=[%s]\n", server_n
, server
));
429 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n
, server
));
431 status
= NT_STATUS_UNSUCCESSFUL
;
433 if (smbc_getOptionUseKerberos(context
)) {
434 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
437 if (smbc_getOptionFallbackAfterKerberos(context
)) {
438 flags
|= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
441 if (smbc_getOptionUseCCache(context
)) {
442 flags
|= CLI_FULL_CONNECTION_USE_CCACHE
;
445 if (smbc_getOptionUseNTHash(context
)) {
446 flags
|= CLI_FULL_CONNECTION_USE_NT_HASH
;
449 if (context
->internal
->smb_encryption_level
!= SMBC_ENCRYPTLEVEL_NONE
) {
450 signing_state
= SMB_SIGNING_REQUIRED
;
454 if (share
== NULL
|| *share
== '\0' || is_ipc
) {
456 * Try 139 first for IPC$
458 status
= cli_connect_nb(server_n
, NULL
, NBT_SMB_PORT
, 0x20,
459 smbc_getNetbiosName(context
),
460 signing_state
, flags
, &c
);
464 if (!NT_STATUS_IS_OK(status
)) {
466 * No IPC$ or 139 did not work
468 status
= cli_connect_nb(server_n
, NULL
, port
, 0x20,
469 smbc_getNetbiosName(context
),
470 signing_state
, flags
, &c
);
473 if (!NT_STATUS_IS_OK(status
)) {
474 errno
= map_errno_from_nt_status(status
);
478 cli_set_timeout(c
, smbc_getTimeout(context
));
480 status
= smbXcli_negprot(c
->conn
, c
->timeout
,
481 lp_client_min_protocol(),
482 lp_client_max_protocol());
483 if (!NT_STATUS_IS_OK(status
)) {
489 if (smbXcli_conn_protocol(c
->conn
) >= PROTOCOL_SMB2_02
) {
490 /* Ensure we ask for some initial credits. */
491 smb2cli_conn_set_max_credits(c
->conn
, DEFAULT_SMB2_MAX_CREDITS
);
494 username_used
= *pp_username
;
496 if (!NT_STATUS_IS_OK(cli_session_setup(c
, username_used
,
498 strlen(*pp_password
),
500 strlen(*pp_password
),
503 /* Failed. Try an anonymous login, if allowed by flags. */
506 if (smbc_getOptionNoAutoAnonymousLogin(context
) ||
507 !NT_STATUS_IS_OK(cli_session_setup(c
, username_used
,
518 DEBUG(4,(" session setup ok\n"));
520 /* here's the fun part....to support 'msdfs proxy' shares
521 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
522 here before trying to connect to the original share.
523 cli_check_msdfs_proxy() will fail if it is a normal share. */
525 if (smbXcli_conn_dfs_supported(c
->conn
) &&
526 cli_check_msdfs_proxy(ctx
, c
, share
,
527 &newserver
, &newshare
,
528 /* FIXME: cli_check_msdfs_proxy() does
529 not support smbc_smb_encrypt_level type */
530 context
->internal
->smb_encryption_level
?
536 srv
= SMBC_server_internal(ctx
, context
, connect_if_not_found
,
537 newserver
, port
, newshare
, pp_workgroup
,
538 pp_username
, pp_password
, in_cache
);
539 TALLOC_FREE(newserver
);
540 TALLOC_FREE(newshare
);
544 /* must be a normal share */
546 status
= cli_tree_connect(c
, share
, "?????", *pp_password
,
547 strlen(*pp_password
)+1);
548 if (!NT_STATUS_IS_OK(status
)) {
549 errno
= map_errno_from_nt_status(status
);
554 DEBUG(4,(" tconx ok\n"));
556 if (smbXcli_conn_protocol(c
->conn
) >= PROTOCOL_SMB2_02
) {
562 /* Determine if this share supports case sensitivity */
564 DEBUG(4, ("IPC$ so ignore case sensitivity\n"));
565 status
= NT_STATUS_OK
;
567 status
= cli_get_fs_attr_info(c
, &fs_attrs
);
570 if (!NT_STATUS_IS_OK(status
)) {
571 DEBUG(4, ("Could not retrieve case sensitivity flag: %s.\n",
575 * We can't determine the case sensitivity of the share. We
576 * have no choice but to use the user-specified case
577 * sensitivity setting.
579 if (smbc_getOptionCaseSensitive(context
)) {
580 cli_set_case_sensitive(c
, True
);
582 cli_set_case_sensitive(c
, False
);
584 } else if (!is_ipc
) {
585 DEBUG(4, ("Case sensitive: %s\n",
586 (fs_attrs
& FILE_CASE_SENSITIVE_SEARCH
589 smbXcli_tcon_set_fs_attributes(tcon
, fs_attrs
);
592 if (context
->internal
->smb_encryption_level
) {
593 /* Attempt UNIX smb encryption. */
594 if (!NT_STATUS_IS_OK(cli_force_encryption(c
,
600 * context->smb_encryption_level == 1
601 * means don't fail if encryption can't be negotiated,
602 * == 2 means fail if encryption can't be negotiated.
605 DEBUG(4,(" SMB encrypt failed\n"));
607 if (context
->internal
->smb_encryption_level
== 2) {
613 DEBUG(4,(" SMB encrypt ok\n"));
617 * Ok, we have got a nice connection
618 * Let's allocate a server structure.
621 srv
= SMB_MALLOC_P(SMBCSRV
);
629 DLIST_ADD(srv
->cli
, c
);
630 srv
->dev
= (dev_t
)(str_checksum(server
) ^ str_checksum(share
));
631 srv
->no_pathinfo
= False
;
632 srv
->no_pathinfo2
= False
;
633 srv
->no_pathinfo3
= False
;
634 srv
->no_nt_session
= False
;
637 if (!pp_workgroup
|| !*pp_workgroup
|| !**pp_workgroup
) {
638 workgroup
= talloc_strdup(ctx
, smbc_getWorkgroup(context
));
640 workgroup
= *pp_workgroup
;
650 /* set the credentials to make DFS work */
651 smbc_set_credentials_with_fallback(context
,
660 SMBC_server(TALLOC_CTX
*ctx
,
662 bool connect_if_not_found
,
671 bool in_cache
= false;
673 srv
= SMBC_server_internal(ctx
, context
, connect_if_not_found
,
674 server
, port
, share
, pp_workgroup
,
675 pp_username
, pp_password
, &in_cache
);
684 /* Now add it to the cache (internal or external) */
685 /* Let the cache function set errno if it wants to */
687 if (smbc_getFunctionAddCachedServer(context
)(context
, srv
,
691 int saved_errno
= errno
;
692 DEBUG(3, (" Failed to add server to cache\n"));
701 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
702 server
, share
, srv
));
704 DLIST_ADD(context
->internal
->servers
, srv
);
709 * Connect to a server for getting/setting attributes, possibly on an existing
710 * connection. This works similarly to SMBC_server().
713 SMBC_attr_server(TALLOC_CTX
*ctx
,
723 struct cli_state
*ipc_cli
= NULL
;
724 struct rpc_pipe_client
*pipe_hnd
= NULL
;
727 SMBCSRV
*ipc_srv
=NULL
;
730 * Use srv->cli->desthost and srv->cli->share instead of
731 * server and share below to connect to the actual share,
732 * i.e., a normal share or a referred share from
733 * 'msdfs proxy' share.
735 srv
= SMBC_server(ctx
, context
, true, server
, port
, share
,
736 pp_workgroup
, pp_username
, pp_password
);
740 server
= smbXcli_conn_remote_name(srv
->cli
->conn
);
741 share
= srv
->cli
->share
;
744 * See if we've already created this special connection. Reference
745 * our "special" share name '*IPC$', which is an impossible real share
746 * name due to the leading asterisk.
748 ipc_srv
= SMBC_find_server(ctx
, context
, server
, "*IPC$",
749 pp_workgroup
, pp_username
, pp_password
);
751 int signing_state
= SMB_SIGNING_DEFAULT
;
753 /* We didn't find a cached connection. Get the password */
754 if (!*pp_password
|| (*pp_password
)[0] == '\0') {
755 /* ... then retrieve it now. */
756 SMBC_call_auth_fn(ctx
, context
, server
, share
,
760 if (!*pp_workgroup
|| !*pp_username
|| !*pp_password
) {
767 if (smbc_getOptionUseKerberos(context
)) {
768 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
770 if (smbc_getOptionUseCCache(context
)) {
771 flags
|= CLI_FULL_CONNECTION_USE_CCACHE
;
773 if (context
->internal
->smb_encryption_level
!= SMBC_ENCRYPTLEVEL_NONE
) {
774 signing_state
= SMB_SIGNING_REQUIRED
;
777 nt_status
= cli_full_connection(&ipc_cli
,
778 lp_netbios_name(), server
,
779 NULL
, 0, "IPC$", "?????",
785 if (! NT_STATUS_IS_OK(nt_status
)) {
786 DEBUG(1,("cli_full_connection failed! (%s)\n",
787 nt_errstr(nt_status
)));
792 if (context
->internal
->smb_encryption_level
) {
793 /* Attempt UNIX smb encryption. */
794 if (!NT_STATUS_IS_OK(cli_force_encryption(ipc_cli
,
800 * context->smb_encryption_level ==
801 * 1 means don't fail if encryption can't be
802 * negotiated, == 2 means fail if encryption
803 * can't be negotiated.
806 DEBUG(4,(" SMB encrypt failed on IPC$\n"));
808 if (context
->internal
->smb_encryption_level
== 2) {
809 cli_shutdown(ipc_cli
);
814 DEBUG(4,(" SMB encrypt ok on IPC$\n"));
817 ipc_srv
= SMB_MALLOC_P(SMBCSRV
);
820 cli_shutdown(ipc_cli
);
824 ZERO_STRUCTP(ipc_srv
);
825 DLIST_ADD(ipc_srv
->cli
, ipc_cli
);
827 nt_status
= cli_rpc_pipe_open_noauth(
828 ipc_srv
->cli
, &ndr_table_lsarpc
, &pipe_hnd
);
829 if (!NT_STATUS_IS_OK(nt_status
)) {
830 DEBUG(1, ("cli_nt_session_open fail!\n"));
832 cli_shutdown(ipc_srv
->cli
);
838 * Some systems don't support
839 * SEC_FLAG_MAXIMUM_ALLOWED, but NT sends 0x2000000
840 * so we might as well do it too.
843 nt_status
= rpccli_lsa_open_policy(
847 GENERIC_EXECUTE_ACCESS
,
850 if (!NT_STATUS_IS_OK(nt_status
)) {
851 errno
= SMBC_errno(context
, ipc_srv
->cli
);
852 cli_shutdown(ipc_srv
->cli
);
857 /* now add it to the cache (internal or external) */
859 errno
= 0; /* let cache function set errno if it likes */
860 if (smbc_getFunctionAddCachedServer(context
)(context
, ipc_srv
,
865 DEBUG(3, (" Failed to add server to cache\n"));
869 cli_shutdown(ipc_srv
->cli
);
874 DLIST_ADD(context
->internal
->servers
, ipc_srv
);