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 "libsmbclient.h"
28 #include "libsmb_internal.h"
32 * Check a server for being alive and well.
33 * returns 0 if the server is in shape. Returns 1 on error
35 * Also useable outside libsmbclient to enable external cache
36 * to do some checks too.
39 SMBC_check_server(SMBCCTX
* context
,
46 return (getpeername(server
->cli
->fd
, &addr
, &size
) == -1);
50 * Remove a server from the cached server list it's unused.
51 * On success, 0 is returned. 1 is returned if the server could not be removed.
53 * Also useable outside libsmbclient
56 SMBC_remove_unused_server(SMBCCTX
* context
,
61 /* are we being fooled ? */
62 if (!context
|| !context
->internal
->initialized
|| !srv
) {
66 /* Check all open files/directories for a relation with this server */
67 for (file
= context
->internal
->files
; file
; file
= file
->next
) {
68 if (file
->srv
== srv
) {
70 DEBUG(3, ("smbc_remove_usused_server: "
71 "%p still used by %p.\n",
77 DLIST_REMOVE(context
->internal
->servers
, srv
);
79 cli_shutdown(srv
->cli
);
82 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv
));
84 smbc_getFunctionRemoveCachedServer(context
)(context
, srv
);
90 /****************************************************************
91 * Call the auth_fn with fixed size (fstring) buffers.
92 ***************************************************************/
94 SMBC_call_auth_fn(TALLOC_CTX
*ctx
,
105 smbc_get_auth_data_with_context_fn auth_with_context_fn
;
107 strlcpy(workgroup
, *pp_workgroup
, sizeof(workgroup
));
108 strlcpy(username
, *pp_username
, sizeof(username
));
109 strlcpy(password
, *pp_password
, sizeof(password
));
111 /* See if there's an authentication with context function provided */
112 auth_with_context_fn
= smbc_getFunctionAuthDataWithContext(context
);
113 if (auth_with_context_fn
)
115 (* auth_with_context_fn
)(context
,
117 workgroup
, sizeof(workgroup
),
118 username
, sizeof(username
),
119 password
, sizeof(password
));
123 smbc_getFunctionAuthData(context
)(server
, share
,
124 workgroup
, sizeof(workgroup
),
125 username
, sizeof(username
),
126 password
, sizeof(password
));
129 TALLOC_FREE(*pp_workgroup
);
130 TALLOC_FREE(*pp_username
);
131 TALLOC_FREE(*pp_password
);
133 *pp_workgroup
= talloc_strdup(ctx
, workgroup
);
134 *pp_username
= talloc_strdup(ctx
, username
);
135 *pp_password
= talloc_strdup(ctx
, password
);
140 SMBC_get_auth_data(const char *server
, const char *share
,
141 char *workgroup_buf
, int workgroup_buf_len
,
142 char *username_buf
, int username_buf_len
,
143 char *password_buf
, int password_buf_len
)
145 /* Default function just uses provided data. Nothing to do. */
151 SMBC_find_server(TALLOC_CTX
*ctx
,
162 if (!pp_workgroup
|| !pp_username
|| !pp_password
) {
168 srv
= smbc_getFunctionGetCachedServer(context
)(context
,
173 if (!auth_called
&& !srv
&& (!*pp_username
|| !(*pp_username
)[0] ||
174 !*pp_password
|| !(*pp_password
)[0])) {
175 SMBC_call_auth_fn(ctx
, context
, server
, share
,
176 pp_workgroup
, pp_username
, pp_password
);
179 * However, smbc_auth_fn may have picked up info relating to
180 * an existing connection, so try for an existing connection
184 goto check_server_cache
;
189 if (smbc_getFunctionCheckServer(context
)(context
, srv
)) {
191 * This server is no good anymore
192 * Try to remove it and check for more possible
193 * servers in the cache
195 if (smbc_getFunctionRemoveUnusedServer(context
)(context
,
198 * We could not remove the server completely,
199 * remove it from the cache so we will not get
200 * it again. It will be removed when the last
201 * file/dir is closed.
203 smbc_getFunctionRemoveCachedServer(context
)(context
,
208 * Maybe there are more cached connections to this
211 goto check_server_cache
;
221 * Connect to a server, possibly on an existing connection
223 * Here, what we want to do is: If the server and username
224 * match an existing connection, reuse that, otherwise, establish a
227 * If we have to create a new connection, call the auth_fn to get the
228 * info we need, unless the username and password were passed in.
232 SMBC_server_internal(TALLOC_CTX
*ctx
,
234 bool connect_if_not_found
,
243 char *workgroup
= NULL
;
245 struct nmb_name called
, calling
;
246 const char *server_n
= server
;
247 struct sockaddr_storage ss
;
248 int tried_reverse
= 0;
251 int is_ipc
= (share
!= NULL
&& strcmp(share
, "IPC$") == 0);
253 const char *username_used
;
255 char *newserver
, *newshare
;
261 if (server
[0] == 0) {
266 /* Look for a cached connection */
267 srv
= SMBC_find_server(ctx
, context
, server
, share
,
268 pp_workgroup
, pp_username
, pp_password
);
271 * If we found a connection and we're only allowed one share per
276 smbc_getOptionOneSharePerServer(context
)) {
279 * ... then if there's no current connection to the share,
280 * connect to it. SMBC_find_server(), or rather the function
281 * pointed to by context->get_cached_srv_fn which
282 * was called by SMBC_find_server(), will have issued a tree
283 * disconnect if the requested share is not the same as the
284 * one that was already connected.
288 * Use srv->cli->desthost and srv->cli->share instead of
289 * server and share below to connect to the actual share,
290 * i.e., a normal share or a referred share from
291 * 'msdfs proxy' share.
293 if (srv
->cli
->cnum
== (uint16
) -1) {
294 /* Ensure we have accurate auth info */
295 SMBC_call_auth_fn(ctx
, context
,
302 if (!*pp_workgroup
|| !*pp_username
|| !*pp_password
) {
304 cli_shutdown(srv
->cli
);
306 smbc_getFunctionRemoveCachedServer(context
)(context
,
312 * We don't need to renegotiate encryption
313 * here as the encryption context is not per
317 status
= cli_tcon_andx(srv
->cli
, srv
->cli
->share
, "?????",
319 strlen(*pp_password
)+1);
320 if (!NT_STATUS_IS_OK(status
)) {
321 errno
= map_errno_from_nt_status(status
);
322 cli_shutdown(srv
->cli
);
324 smbc_getFunctionRemoveCachedServer(context
)(context
,
329 /* Determine if this share supports case sensitivity */
332 ("IPC$ so ignore case sensitivity\n"));
333 } else if (!NT_STATUS_IS_OK(cli_get_fs_attr_info(c
, &fs_attrs
))) {
334 DEBUG(4, ("Could not retrieve "
335 "case sensitivity flag: %s.\n",
339 * We can't determine the case sensitivity of
340 * the share. We have no choice but to use the
341 * user-specified case sensitivity setting.
343 if (smbc_getOptionCaseSensitive(context
)) {
344 cli_set_case_sensitive(c
, True
);
346 cli_set_case_sensitive(c
, False
);
350 ("Case sensitive: %s\n",
351 (fs_attrs
& FILE_CASE_SENSITIVE_SEARCH
354 cli_set_case_sensitive(
356 (fs_attrs
& FILE_CASE_SENSITIVE_SEARCH
362 * Regenerate the dev value since it's based on both
366 srv
->dev
= (dev_t
)(str_checksum(srv
->cli
->desthost
) ^
367 str_checksum(srv
->cli
->share
));
372 /* If we have a connection... */
375 /* ... then we're done here. Give 'em what they came for. */
380 /* If we're not asked to connect when a connection doesn't exist... */
381 if (! connect_if_not_found
) {
382 /* ... then we're done here. */
386 if (!*pp_workgroup
|| !*pp_username
|| !*pp_password
) {
391 make_nmb_name(&calling
, smbc_getNetbiosName(context
), 0x0);
392 make_nmb_name(&called
, server
, 0x20);
394 DEBUG(4,("SMBC_server: server_n=[%s] server=[%s]\n", server_n
, server
));
396 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n
, server
));
402 /* have to open a new connection */
403 if ((c
= cli_initialise()) == NULL
) {
408 if (smbc_getOptionUseKerberos(context
)) {
409 c
->use_kerberos
= True
;
412 if (smbc_getOptionFallbackAfterKerberos(context
)) {
413 c
->fallback_after_kerberos
= True
;
416 if (smbc_getOptionUseCCache(context
)) {
417 c
->use_ccache
= True
;
420 c
->timeout
= smbc_getTimeout(context
);
423 * Force use of port 139 for first try if share is $IPC, empty, or
424 * null, so browse lists can work
426 if (share
== NULL
|| *share
== '\0' || is_ipc
) {
427 port_try_first
= 139;
430 port_try_first
= 445;
434 c
->port
= port_try_first
;
436 status
= cli_connect(c
, server_n
, &ss
);
437 if (!NT_STATUS_IS_OK(status
)) {
439 /* First connection attempt failed. Try alternate port. */
440 c
->port
= port_try_next
;
442 status
= cli_connect(c
, server_n
, &ss
);
443 if (!NT_STATUS_IS_OK(status
)) {
450 if (!cli_session_request(c
, &calling
, &called
)) {
452 if (strcmp(called
.name
, "*SMBSERVER")) {
453 make_nmb_name(&called
, "*SMBSERVER", 0x20);
455 } else { /* Try one more time, but ensure we don't loop */
457 /* Only try this if server is an IP address ... */
459 if (is_ipaddress(server
) && !tried_reverse
) {
461 struct sockaddr_storage rem_ss
;
463 if (!interpret_string_addr(&rem_ss
, server
,
465 DEBUG(4, ("Could not convert IP address "
466 "%s to struct sockaddr_storage\n",
472 tried_reverse
++; /* Yuck */
474 if (name_status_find("*", 0, 0,
475 &rem_ss
, remote_name
)) {
476 make_nmb_name(&called
,
487 DEBUG(4,(" session request ok\n"));
489 status
= cli_negprot(c
);
491 if (!NT_STATUS_IS_OK(status
)) {
497 username_used
= *pp_username
;
499 if (!NT_STATUS_IS_OK(cli_session_setup(c
, username_used
,
501 strlen(*pp_password
),
503 strlen(*pp_password
),
506 /* Failed. Try an anonymous login, if allowed by flags. */
509 if (smbc_getOptionNoAutoAnonymousLogin(context
) ||
510 !NT_STATUS_IS_OK(cli_session_setup(c
, username_used
,
521 status
= cli_init_creds(c
, username_used
,
522 *pp_workgroup
, *pp_password
);
523 if (!NT_STATUS_IS_OK(status
)) {
524 errno
= map_errno_from_nt_status(status
);
529 DEBUG(4,(" session setup ok\n"));
531 /* here's the fun part....to support 'msdfs proxy' shares
532 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
533 here before trying to connect to the original share.
534 cli_check_msdfs_proxy() will fail if it is a normal share. */
536 if ((c
->capabilities
& CAP_DFS
) &&
537 cli_check_msdfs_proxy(ctx
, c
, share
,
538 &newserver
, &newshare
,
539 /* FIXME: cli_check_msdfs_proxy() does
540 not support smbc_smb_encrypt_level type */
541 context
->internal
->smb_encryption_level
?
547 srv
= SMBC_server_internal(ctx
, context
, connect_if_not_found
,
548 newserver
, newshare
, pp_workgroup
,
549 pp_username
, pp_password
, in_cache
);
550 TALLOC_FREE(newserver
);
551 TALLOC_FREE(newshare
);
555 /* must be a normal share */
557 status
= cli_tcon_andx(c
, share
, "?????", *pp_password
,
558 strlen(*pp_password
)+1);
559 if (!NT_STATUS_IS_OK(status
)) {
560 errno
= map_errno_from_nt_status(status
);
565 DEBUG(4,(" tconx ok\n"));
567 /* Determine if this share supports case sensitivity */
569 DEBUG(4, ("IPC$ so ignore case sensitivity\n"));
570 } else if (!NT_STATUS_IS_OK(cli_get_fs_attr_info(c
, &fs_attrs
))) {
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
);
585 DEBUG(4, ("Case sensitive: %s\n",
586 (fs_attrs
& FILE_CASE_SENSITIVE_SEARCH
589 cli_set_case_sensitive(c
,
590 (fs_attrs
& FILE_CASE_SENSITIVE_SEARCH
595 if (context
->internal
->smb_encryption_level
) {
596 /* Attempt UNIX smb encryption. */
597 if (!NT_STATUS_IS_OK(cli_force_encryption(c
,
603 * context->smb_encryption_level == 1
604 * means don't fail if encryption can't be negotiated,
605 * == 2 means fail if encryption can't be negotiated.
608 DEBUG(4,(" SMB encrypt failed\n"));
610 if (context
->internal
->smb_encryption_level
== 2) {
616 DEBUG(4,(" SMB encrypt ok\n"));
620 * Ok, we have got a nice connection
621 * Let's allocate a server structure.
624 srv
= SMB_MALLOC_P(SMBCSRV
);
633 srv
->dev
= (dev_t
)(str_checksum(server
) ^ str_checksum(share
));
634 srv
->no_pathinfo
= False
;
635 srv
->no_pathinfo2
= False
;
636 srv
->no_nt_session
= False
;
639 if (!pp_workgroup
|| !*pp_workgroup
|| !**pp_workgroup
) {
640 workgroup
= talloc_strdup(ctx
, smbc_getWorkgroup(context
));
642 workgroup
= *pp_workgroup
;
648 /* set the credentials to make DFS work */
649 smbc_set_credentials_with_fallback(context
,
658 SMBC_server(TALLOC_CTX
*ctx
,
660 bool connect_if_not_found
,
668 bool in_cache
= false;
670 srv
= SMBC_server_internal(ctx
, context
, connect_if_not_found
,
671 server
, share
, pp_workgroup
,
672 pp_username
, pp_password
, &in_cache
);
681 /* Now add it to the cache (internal or external) */
682 /* Let the cache function set errno if it wants to */
684 if (smbc_getFunctionAddCachedServer(context
)(context
, srv
,
688 int saved_errno
= errno
;
689 DEBUG(3, (" Failed to add server to cache\n"));
698 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
699 server
, share
, srv
));
701 DLIST_ADD(context
->internal
->servers
, srv
);
706 * Connect to a server for getting/setting attributes, possibly on an existing
707 * connection. This works similarly to SMBC_server().
710 SMBC_attr_server(TALLOC_CTX
*ctx
,
719 struct sockaddr_storage ss
;
720 struct cli_state
*ipc_cli
= NULL
;
721 struct rpc_pipe_client
*pipe_hnd
= NULL
;
724 SMBCSRV
*ipc_srv
=NULL
;
727 * Use srv->cli->desthost and srv->cli->share instead of
728 * server and share below to connect to the actual share,
729 * i.e., a normal share or a referred share from
730 * 'msdfs proxy' share.
732 srv
= SMBC_server(ctx
, context
, true, server
, share
,
733 pp_workgroup
, pp_username
, pp_password
);
737 server
= srv
->cli
->desthost
;
738 share
= srv
->cli
->share
;
741 * See if we've already created this special connection. Reference
742 * our "special" share name '*IPC$', which is an impossible real share
743 * name due to the leading asterisk.
745 ipc_srv
= SMBC_find_server(ctx
, context
, server
, "*IPC$",
746 pp_workgroup
, pp_username
, pp_password
);
749 /* We didn't find a cached connection. Get the password */
750 if (!*pp_password
|| (*pp_password
)[0] == '\0') {
751 /* ... then retrieve it now. */
752 SMBC_call_auth_fn(ctx
, context
, server
, share
,
756 if (!*pp_workgroup
|| !*pp_username
|| !*pp_password
) {
763 if (smbc_getOptionUseKerberos(context
)) {
764 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
766 if (smbc_getOptionUseCCache(context
)) {
767 flags
|= CLI_FULL_CONNECTION_USE_CCACHE
;
771 nt_status
= cli_full_connection(&ipc_cli
,
772 global_myname(), server
,
773 &ss
, 0, "IPC$", "?????",
779 if (! NT_STATUS_IS_OK(nt_status
)) {
780 DEBUG(1,("cli_full_connection failed! (%s)\n",
781 nt_errstr(nt_status
)));
786 if (context
->internal
->smb_encryption_level
) {
787 /* Attempt UNIX smb encryption. */
788 if (!NT_STATUS_IS_OK(cli_force_encryption(ipc_cli
,
794 * context->smb_encryption_level ==
795 * 1 means don't fail if encryption can't be
796 * negotiated, == 2 means fail if encryption
797 * can't be negotiated.
800 DEBUG(4,(" SMB encrypt failed on IPC$\n"));
802 if (context
->internal
->smb_encryption_level
== 2) {
803 cli_shutdown(ipc_cli
);
808 DEBUG(4,(" SMB encrypt ok on IPC$\n"));
811 ipc_srv
= SMB_MALLOC_P(SMBCSRV
);
814 cli_shutdown(ipc_cli
);
818 ZERO_STRUCTP(ipc_srv
);
819 ipc_srv
->cli
= ipc_cli
;
821 nt_status
= cli_rpc_pipe_open_noauth(
822 ipc_srv
->cli
, &ndr_table_lsarpc
.syntax_id
, &pipe_hnd
);
823 if (!NT_STATUS_IS_OK(nt_status
)) {
824 DEBUG(1, ("cli_nt_session_open fail!\n"));
826 cli_shutdown(ipc_srv
->cli
);
832 * Some systems don't support
833 * SEC_FLAG_MAXIMUM_ALLOWED, but NT sends 0x2000000
834 * so we might as well do it too.
837 nt_status
= rpccli_lsa_open_policy(
841 GENERIC_EXECUTE_ACCESS
,
844 if (!NT_STATUS_IS_OK(nt_status
)) {
845 errno
= SMBC_errno(context
, ipc_srv
->cli
);
846 cli_shutdown(ipc_srv
->cli
);
850 /* now add it to the cache (internal or external) */
852 errno
= 0; /* let cache function set errno if it likes */
853 if (smbc_getFunctionAddCachedServer(context
)(context
, ipc_srv
,
858 DEBUG(3, (" Failed to add server to cache\n"));
862 cli_shutdown(ipc_srv
->cli
);
867 DLIST_ADD(context
->internal
->servers
, ipc_srv
);