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"
37 * Check a server for being alive and well.
38 * returns 0 if the server is in shape. Returns 1 on error
40 * Also useable outside libsmbclient to enable external cache
41 * to do some checks too.
44 SMBC_check_server(SMBCCTX
* context
,
51 return (getpeername(server
->cli
->fd
, &addr
, &size
) == -1);
55 * Remove a server from the cached server list it's unused.
56 * On success, 0 is returned. 1 is returned if the server could not be removed.
58 * Also useable outside libsmbclient
61 SMBC_remove_unused_server(SMBCCTX
* context
,
66 /* are we being fooled ? */
67 if (!context
|| !context
->internal
->initialized
|| !srv
) {
71 /* Check all open files/directories for a relation with this server */
72 for (file
= context
->internal
->files
; file
; file
= file
->next
) {
73 if (file
->srv
== srv
) {
75 DEBUG(3, ("smbc_remove_usused_server: "
76 "%p still used by %p.\n",
82 DLIST_REMOVE(context
->internal
->servers
, srv
);
84 cli_shutdown(srv
->cli
);
87 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv
));
89 smbc_getFunctionRemoveCachedServer(context
)(context
, srv
);
95 /****************************************************************
96 * Call the auth_fn with fixed size (fstring) buffers.
97 ***************************************************************/
99 SMBC_call_auth_fn(TALLOC_CTX
*ctx
,
110 smbc_get_auth_data_with_context_fn auth_with_context_fn
;
112 strlcpy(workgroup
, *pp_workgroup
, sizeof(workgroup
));
113 strlcpy(username
, *pp_username
, sizeof(username
));
114 strlcpy(password
, *pp_password
, sizeof(password
));
116 /* See if there's an authentication with context function provided */
117 auth_with_context_fn
= smbc_getFunctionAuthDataWithContext(context
);
118 if (auth_with_context_fn
)
120 (* auth_with_context_fn
)(context
,
122 workgroup
, sizeof(workgroup
),
123 username
, sizeof(username
),
124 password
, sizeof(password
));
128 smbc_getFunctionAuthData(context
)(server
, share
,
129 workgroup
, sizeof(workgroup
),
130 username
, sizeof(username
),
131 password
, sizeof(password
));
134 TALLOC_FREE(*pp_workgroup
);
135 TALLOC_FREE(*pp_username
);
136 TALLOC_FREE(*pp_password
);
138 *pp_workgroup
= talloc_strdup(ctx
, workgroup
);
139 *pp_username
= talloc_strdup(ctx
, username
);
140 *pp_password
= talloc_strdup(ctx
, password
);
145 SMBC_get_auth_data(const char *server
, const char *share
,
146 char *workgroup_buf
, int workgroup_buf_len
,
147 char *username_buf
, int username_buf_len
,
148 char *password_buf
, int password_buf_len
)
150 /* Default function just uses provided data. Nothing to do. */
156 SMBC_find_server(TALLOC_CTX
*ctx
,
167 if (!pp_workgroup
|| !pp_username
|| !pp_password
) {
173 srv
= smbc_getFunctionGetCachedServer(context
)(context
,
178 if (!auth_called
&& !srv
&& (!*pp_username
|| !(*pp_username
)[0] ||
179 !*pp_password
|| !(*pp_password
)[0])) {
180 SMBC_call_auth_fn(ctx
, context
, server
, share
,
181 pp_workgroup
, pp_username
, pp_password
);
184 * However, smbc_auth_fn may have picked up info relating to
185 * an existing connection, so try for an existing connection
189 goto check_server_cache
;
194 if (smbc_getFunctionCheckServer(context
)(context
, srv
)) {
196 * This server is no good anymore
197 * Try to remove it and check for more possible
198 * servers in the cache
200 if (smbc_getFunctionRemoveUnusedServer(context
)(context
,
203 * We could not remove the server completely,
204 * remove it from the cache so we will not get
205 * it again. It will be removed when the last
206 * file/dir is closed.
208 smbc_getFunctionRemoveCachedServer(context
)(context
,
213 * Maybe there are more cached connections to this
216 goto check_server_cache
;
226 * Connect to a server, possibly on an existing connection
228 * Here, what we want to do is: If the server and username
229 * match an existing connection, reuse that, otherwise, establish a
232 * If we have to create a new connection, call the auth_fn to get the
233 * info we need, unless the username and password were passed in.
237 SMBC_server_internal(TALLOC_CTX
*ctx
,
239 bool connect_if_not_found
,
248 char *workgroup
= NULL
;
250 struct nmb_name called
, calling
;
251 const char *server_n
= server
;
252 struct sockaddr_storage ss
;
253 int tried_reverse
= 0;
256 int is_ipc
= (share
!= NULL
&& strcmp(share
, "IPC$") == 0);
258 const char *username_used
;
260 char *newserver
, *newshare
;
266 if (server
[0] == 0) {
271 /* Look for a cached connection */
272 srv
= SMBC_find_server(ctx
, context
, server
, share
,
273 pp_workgroup
, pp_username
, pp_password
);
276 * If we found a connection and we're only allowed one share per
281 smbc_getOptionOneSharePerServer(context
)) {
284 * ... then if there's no current connection to the share,
285 * connect to it. SMBC_find_server(), or rather the function
286 * pointed to by context->get_cached_srv_fn which
287 * was called by SMBC_find_server(), will have issued a tree
288 * disconnect if the requested share is not the same as the
289 * one that was already connected.
293 * Use srv->cli->desthost and srv->cli->share instead of
294 * server and share below to connect to the actual share,
295 * i.e., a normal share or a referred share from
296 * 'msdfs proxy' share.
298 if (srv
->cli
->cnum
== (uint16
) -1) {
299 /* Ensure we have accurate auth info */
300 SMBC_call_auth_fn(ctx
, context
,
307 if (!*pp_workgroup
|| !*pp_username
|| !*pp_password
) {
309 cli_shutdown(srv
->cli
);
311 smbc_getFunctionRemoveCachedServer(context
)(context
,
317 * We don't need to renegotiate encryption
318 * here as the encryption context is not per
322 status
= cli_tcon_andx(srv
->cli
, srv
->cli
->share
, "?????",
324 strlen(*pp_password
)+1);
325 if (!NT_STATUS_IS_OK(status
)) {
326 errno
= map_errno_from_nt_status(status
);
327 cli_shutdown(srv
->cli
);
329 smbc_getFunctionRemoveCachedServer(context
)(context
,
334 /* Determine if this share supports case sensitivity */
337 ("IPC$ so ignore case sensitivity\n"));
338 } else if (!NT_STATUS_IS_OK(cli_get_fs_attr_info(c
, &fs_attrs
))) {
339 DEBUG(4, ("Could not retrieve "
340 "case sensitivity flag: %s.\n",
344 * We can't determine the case sensitivity of
345 * the share. We have no choice but to use the
346 * user-specified case sensitivity setting.
348 if (smbc_getOptionCaseSensitive(context
)) {
349 cli_set_case_sensitive(c
, True
);
351 cli_set_case_sensitive(c
, False
);
355 ("Case sensitive: %s\n",
356 (fs_attrs
& FILE_CASE_SENSITIVE_SEARCH
359 cli_set_case_sensitive(
361 (fs_attrs
& FILE_CASE_SENSITIVE_SEARCH
367 * Regenerate the dev value since it's based on both
371 srv
->dev
= (dev_t
)(str_checksum(srv
->cli
->desthost
) ^
372 str_checksum(srv
->cli
->share
));
377 /* If we have a connection... */
380 /* ... then we're done here. Give 'em what they came for. */
385 /* If we're not asked to connect when a connection doesn't exist... */
386 if (! connect_if_not_found
) {
387 /* ... then we're done here. */
391 if (!*pp_workgroup
|| !*pp_username
|| !*pp_password
) {
396 make_nmb_name(&calling
, smbc_getNetbiosName(context
), 0x0);
397 make_nmb_name(&called
, server
, 0x20);
399 DEBUG(4,("SMBC_server: server_n=[%s] server=[%s]\n", server_n
, server
));
401 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n
, server
));
407 /* have to open a new connection */
408 if ((c
= cli_initialise()) == NULL
) {
413 if (smbc_getOptionUseKerberos(context
)) {
414 c
->use_kerberos
= True
;
417 if (smbc_getOptionFallbackAfterKerberos(context
)) {
418 c
->fallback_after_kerberos
= True
;
421 if (smbc_getOptionUseCCache(context
)) {
422 c
->use_ccache
= True
;
425 c
->timeout
= smbc_getTimeout(context
);
428 * Force use of port 139 for first try if share is $IPC, empty, or
429 * null, so browse lists can work
431 if (share
== NULL
|| *share
== '\0' || is_ipc
) {
432 port_try_first
= 139;
435 port_try_first
= 445;
439 c
->port
= port_try_first
;
441 status
= cli_connect(c
, server_n
, &ss
);
442 if (!NT_STATUS_IS_OK(status
)) {
444 /* First connection attempt failed. Try alternate port. */
445 c
->port
= port_try_next
;
447 status
= cli_connect(c
, server_n
, &ss
);
448 if (!NT_STATUS_IS_OK(status
)) {
455 if (!cli_session_request(c
, &calling
, &called
)) {
457 if (strcmp(called
.name
, "*SMBSERVER")) {
458 make_nmb_name(&called
, "*SMBSERVER", 0x20);
460 } else { /* Try one more time, but ensure we don't loop */
462 /* Only try this if server is an IP address ... */
464 if (is_ipaddress(server
) && !tried_reverse
) {
466 struct sockaddr_storage rem_ss
;
468 if (!interpret_string_addr(&rem_ss
, server
,
470 DEBUG(4, ("Could not convert IP address "
471 "%s to struct sockaddr_storage\n",
477 tried_reverse
++; /* Yuck */
479 if (name_status_find("*", 0, 0,
480 &rem_ss
, remote_name
)) {
481 make_nmb_name(&called
,
492 DEBUG(4,(" session request ok\n"));
494 status
= cli_negprot(c
);
496 if (!NT_STATUS_IS_OK(status
)) {
502 username_used
= *pp_username
;
504 if (!NT_STATUS_IS_OK(cli_session_setup(c
, username_used
,
506 strlen(*pp_password
),
508 strlen(*pp_password
),
511 /* Failed. Try an anonymous login, if allowed by flags. */
514 if (smbc_getOptionNoAutoAnonymousLogin(context
) ||
515 !NT_STATUS_IS_OK(cli_session_setup(c
, username_used
,
526 status
= cli_init_creds(c
, username_used
,
527 *pp_workgroup
, *pp_password
);
528 if (!NT_STATUS_IS_OK(status
)) {
529 errno
= map_errno_from_nt_status(status
);
534 DEBUG(4,(" session setup ok\n"));
536 /* here's the fun part....to support 'msdfs proxy' shares
537 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
538 here before trying to connect to the original share.
539 cli_check_msdfs_proxy() will fail if it is a normal share. */
541 if ((c
->capabilities
& CAP_DFS
) &&
542 cli_check_msdfs_proxy(ctx
, c
, share
,
543 &newserver
, &newshare
,
544 /* FIXME: cli_check_msdfs_proxy() does
545 not support smbc_smb_encrypt_level type */
546 context
->internal
->smb_encryption_level
?
552 srv
= SMBC_server_internal(ctx
, context
, connect_if_not_found
,
553 newserver
, newshare
, pp_workgroup
,
554 pp_username
, pp_password
, in_cache
);
555 TALLOC_FREE(newserver
);
556 TALLOC_FREE(newshare
);
560 /* must be a normal share */
562 status
= cli_tcon_andx(c
, share
, "?????", *pp_password
,
563 strlen(*pp_password
)+1);
564 if (!NT_STATUS_IS_OK(status
)) {
565 errno
= map_errno_from_nt_status(status
);
570 DEBUG(4,(" tconx ok\n"));
572 /* Determine if this share supports case sensitivity */
574 DEBUG(4, ("IPC$ so ignore case sensitivity\n"));
575 } else if (!NT_STATUS_IS_OK(cli_get_fs_attr_info(c
, &fs_attrs
))) {
576 DEBUG(4, ("Could not retrieve case sensitivity flag: %s.\n",
580 * We can't determine the case sensitivity of the share. We
581 * have no choice but to use the user-specified case
582 * sensitivity setting.
584 if (smbc_getOptionCaseSensitive(context
)) {
585 cli_set_case_sensitive(c
, True
);
587 cli_set_case_sensitive(c
, False
);
590 DEBUG(4, ("Case sensitive: %s\n",
591 (fs_attrs
& FILE_CASE_SENSITIVE_SEARCH
594 cli_set_case_sensitive(c
,
595 (fs_attrs
& FILE_CASE_SENSITIVE_SEARCH
600 if (context
->internal
->smb_encryption_level
) {
601 /* Attempt UNIX smb encryption. */
602 if (!NT_STATUS_IS_OK(cli_force_encryption(c
,
608 * context->smb_encryption_level == 1
609 * means don't fail if encryption can't be negotiated,
610 * == 2 means fail if encryption can't be negotiated.
613 DEBUG(4,(" SMB encrypt failed\n"));
615 if (context
->internal
->smb_encryption_level
== 2) {
621 DEBUG(4,(" SMB encrypt ok\n"));
625 * Ok, we have got a nice connection
626 * Let's allocate a server structure.
629 srv
= SMB_MALLOC_P(SMBCSRV
);
638 srv
->dev
= (dev_t
)(str_checksum(server
) ^ str_checksum(share
));
639 srv
->no_pathinfo
= False
;
640 srv
->no_pathinfo2
= False
;
641 srv
->no_nt_session
= False
;
644 if (!pp_workgroup
|| !*pp_workgroup
|| !**pp_workgroup
) {
645 workgroup
= talloc_strdup(ctx
, smbc_getWorkgroup(context
));
647 workgroup
= *pp_workgroup
;
653 /* set the credentials to make DFS work */
654 smbc_set_credentials_with_fallback(context
,
663 SMBC_server(TALLOC_CTX
*ctx
,
665 bool connect_if_not_found
,
673 bool in_cache
= false;
675 srv
= SMBC_server_internal(ctx
, context
, connect_if_not_found
,
676 server
, share
, pp_workgroup
,
677 pp_username
, pp_password
, &in_cache
);
686 /* Now add it to the cache (internal or external) */
687 /* Let the cache function set errno if it wants to */
689 if (smbc_getFunctionAddCachedServer(context
)(context
, srv
,
693 int saved_errno
= errno
;
694 DEBUG(3, (" Failed to add server to cache\n"));
703 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
704 server
, share
, srv
));
706 DLIST_ADD(context
->internal
->servers
, srv
);
711 * Connect to a server for getting/setting attributes, possibly on an existing
712 * connection. This works similarly to SMBC_server().
715 SMBC_attr_server(TALLOC_CTX
*ctx
,
724 struct sockaddr_storage ss
;
725 struct cli_state
*ipc_cli
= NULL
;
726 struct rpc_pipe_client
*pipe_hnd
= NULL
;
729 SMBCSRV
*ipc_srv
=NULL
;
732 * Use srv->cli->desthost and srv->cli->share instead of
733 * server and share below to connect to the actual share,
734 * i.e., a normal share or a referred share from
735 * 'msdfs proxy' share.
737 srv
= SMBC_server(ctx
, context
, true, server
, share
,
738 pp_workgroup
, pp_username
, pp_password
);
742 server
= srv
->cli
->desthost
;
743 share
= srv
->cli
->share
;
746 * See if we've already created this special connection. Reference
747 * our "special" share name '*IPC$', which is an impossible real share
748 * name due to the leading asterisk.
750 ipc_srv
= SMBC_find_server(ctx
, context
, server
, "*IPC$",
751 pp_workgroup
, pp_username
, pp_password
);
754 /* We didn't find a cached connection. Get the password */
755 if (!*pp_password
|| (*pp_password
)[0] == '\0') {
756 /* ... then retrieve it now. */
757 SMBC_call_auth_fn(ctx
, context
, server
, share
,
761 if (!*pp_workgroup
|| !*pp_username
|| !*pp_password
) {
768 if (smbc_getOptionUseKerberos(context
)) {
769 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
771 if (smbc_getOptionUseCCache(context
)) {
772 flags
|= CLI_FULL_CONNECTION_USE_CCACHE
;
776 nt_status
= cli_full_connection(&ipc_cli
,
777 global_myname(), server
,
778 &ss
, 0, "IPC$", "?????",
784 if (! NT_STATUS_IS_OK(nt_status
)) {
785 DEBUG(1,("cli_full_connection failed! (%s)\n",
786 nt_errstr(nt_status
)));
791 if (context
->internal
->smb_encryption_level
) {
792 /* Attempt UNIX smb encryption. */
793 if (!NT_STATUS_IS_OK(cli_force_encryption(ipc_cli
,
799 * context->smb_encryption_level ==
800 * 1 means don't fail if encryption can't be
801 * negotiated, == 2 means fail if encryption
802 * can't be negotiated.
805 DEBUG(4,(" SMB encrypt failed on IPC$\n"));
807 if (context
->internal
->smb_encryption_level
== 2) {
808 cli_shutdown(ipc_cli
);
813 DEBUG(4,(" SMB encrypt ok on IPC$\n"));
816 ipc_srv
= SMB_MALLOC_P(SMBCSRV
);
819 cli_shutdown(ipc_cli
);
823 ZERO_STRUCTP(ipc_srv
);
824 ipc_srv
->cli
= ipc_cli
;
826 nt_status
= cli_rpc_pipe_open_noauth(
827 ipc_srv
->cli
, &ndr_table_lsarpc
.syntax_id
, &pipe_hnd
);
828 if (!NT_STATUS_IS_OK(nt_status
)) {
829 DEBUG(1, ("cli_nt_session_open fail!\n"));
831 cli_shutdown(ipc_srv
->cli
);
837 * Some systems don't support
838 * SEC_FLAG_MAXIMUM_ALLOWED, but NT sends 0x2000000
839 * so we might as well do it too.
842 nt_status
= rpccli_lsa_open_policy(
846 GENERIC_EXECUTE_ACCESS
,
849 if (!NT_STATUS_IS_OK(nt_status
)) {
850 errno
= SMBC_errno(context
, ipc_srv
->cli
);
851 cli_shutdown(ipc_srv
->cli
);
855 /* now add it to the cache (internal or external) */
857 errno
= 0; /* let cache function set errno if it likes */
858 if (smbc_getFunctionAddCachedServer(context
)(context
, ipc_srv
,
863 DEBUG(3, (" Failed to add server to cache\n"));
867 cli_shutdown(ipc_srv
->cli
);
872 DLIST_ADD(context
->internal
->servers
, ipc_srv
);