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
,
48 if (!cli_state_is_connected(server
->cli
)) {
56 * Remove a server from the cached server list it's unused.
57 * On success, 0 is returned. 1 is returned if the server could not be removed.
59 * Also useable outside libsmbclient
62 SMBC_remove_unused_server(SMBCCTX
* context
,
67 /* are we being fooled ? */
68 if (!context
|| !context
->internal
->initialized
|| !srv
) {
72 /* Check all open files/directories for a relation with this server */
73 for (file
= context
->internal
->files
; file
; file
= file
->next
) {
74 if (file
->srv
== srv
) {
76 DEBUG(3, ("smbc_remove_usused_server: "
77 "%p still used by %p.\n",
83 DLIST_REMOVE(context
->internal
->servers
, srv
);
85 cli_shutdown(srv
->cli
);
88 DEBUG(3, ("smbc_remove_usused_server: %p removed.\n", srv
));
90 smbc_getFunctionRemoveCachedServer(context
)(context
, srv
);
96 /****************************************************************
97 * Call the auth_fn with fixed size (fstring) buffers.
98 ***************************************************************/
100 SMBC_call_auth_fn(TALLOC_CTX
*ctx
,
111 smbc_get_auth_data_with_context_fn auth_with_context_fn
;
113 strlcpy(workgroup
, *pp_workgroup
, sizeof(workgroup
));
114 strlcpy(username
, *pp_username
, sizeof(username
));
115 strlcpy(password
, *pp_password
, sizeof(password
));
117 /* See if there's an authentication with context function provided */
118 auth_with_context_fn
= smbc_getFunctionAuthDataWithContext(context
);
119 if (auth_with_context_fn
)
121 (* auth_with_context_fn
)(context
,
123 workgroup
, sizeof(workgroup
),
124 username
, sizeof(username
),
125 password
, sizeof(password
));
129 smbc_getFunctionAuthData(context
)(server
, share
,
130 workgroup
, sizeof(workgroup
),
131 username
, sizeof(username
),
132 password
, sizeof(password
));
135 TALLOC_FREE(*pp_workgroup
);
136 TALLOC_FREE(*pp_username
);
137 TALLOC_FREE(*pp_password
);
139 *pp_workgroup
= talloc_strdup(ctx
, workgroup
);
140 *pp_username
= talloc_strdup(ctx
, username
);
141 *pp_password
= talloc_strdup(ctx
, password
);
146 SMBC_get_auth_data(const char *server
, const char *share
,
147 char *workgroup_buf
, int workgroup_buf_len
,
148 char *username_buf
, int username_buf_len
,
149 char *password_buf
, int password_buf_len
)
151 /* Default function just uses provided data. Nothing to do. */
157 SMBC_find_server(TALLOC_CTX
*ctx
,
168 if (!pp_workgroup
|| !pp_username
|| !pp_password
) {
174 srv
= smbc_getFunctionGetCachedServer(context
)(context
,
179 if (!auth_called
&& !srv
&& (!*pp_username
|| !(*pp_username
)[0] ||
180 !*pp_password
|| !(*pp_password
)[0])) {
181 SMBC_call_auth_fn(ctx
, context
, server
, share
,
182 pp_workgroup
, pp_username
, pp_password
);
185 * However, smbc_auth_fn may have picked up info relating to
186 * an existing connection, so try for an existing connection
190 goto check_server_cache
;
195 if (smbc_getFunctionCheckServer(context
)(context
, srv
)) {
197 * This server is no good anymore
198 * Try to remove it and check for more possible
199 * servers in the cache
201 if (smbc_getFunctionRemoveUnusedServer(context
)(context
,
204 * We could not remove the server completely,
205 * remove it from the cache so we will not get
206 * it again. It will be removed when the last
207 * file/dir is closed.
209 smbc_getFunctionRemoveCachedServer(context
)(context
,
214 * Maybe there are more cached connections to this
217 goto check_server_cache
;
227 * Connect to a server, possibly on an existing connection
229 * Here, what we want to do is: If the server and username
230 * match an existing connection, reuse that, otherwise, establish a
233 * If we have to create a new connection, call the auth_fn to get the
234 * info we need, unless the username and password were passed in.
238 SMBC_server_internal(TALLOC_CTX
*ctx
,
240 bool connect_if_not_found
,
249 char *workgroup
= NULL
;
250 struct cli_state
*c
= NULL
;
251 const char *server_n
= server
;
252 int is_ipc
= (share
!= NULL
&& strcmp(share
, "IPC$") == 0);
254 const char *username_used
;
256 char *newserver
, *newshare
;
262 if (server
[0] == 0) {
267 /* Look for a cached connection */
268 srv
= SMBC_find_server(ctx
, context
, server
, share
,
269 pp_workgroup
, pp_username
, pp_password
);
272 * If we found a connection and we're only allowed one share per
277 smbc_getOptionOneSharePerServer(context
)) {
280 * ... then if there's no current connection to the share,
281 * connect to it. SMBC_find_server(), or rather the function
282 * pointed to by context->get_cached_srv_fn which
283 * was called by SMBC_find_server(), will have issued a tree
284 * disconnect if the requested share is not the same as the
285 * one that was already connected.
289 * Use srv->cli->desthost and srv->cli->share instead of
290 * server and share below to connect to the actual share,
291 * i.e., a normal share or a referred share from
292 * 'msdfs proxy' share.
294 if (!cli_state_has_tcon(srv
->cli
)) {
295 /* Ensure we have accurate auth info */
296 SMBC_call_auth_fn(ctx
, context
,
297 smbXcli_conn_remote_name(srv
->cli
->conn
),
303 if (!*pp_workgroup
|| !*pp_username
|| !*pp_password
) {
305 cli_shutdown(srv
->cli
);
307 smbc_getFunctionRemoveCachedServer(context
)(context
,
313 * We don't need to renegotiate encryption
314 * here as the encryption context is not per
318 status
= cli_tree_connect(srv
->cli
,
322 strlen(*pp_password
)+1);
323 if (!NT_STATUS_IS_OK(status
)) {
324 errno
= map_errno_from_nt_status(status
);
325 cli_shutdown(srv
->cli
);
327 smbc_getFunctionRemoveCachedServer(context
)(context
,
332 /* Determine if this share supports case sensitivity */
335 ("IPC$ so ignore case sensitivity\n"));
336 status
= NT_STATUS_OK
;
338 status
= cli_get_fs_attr_info(c
, &fs_attrs
);
341 if (!NT_STATUS_IS_OK(status
)) {
342 DEBUG(4, ("Could not retrieve "
343 "case sensitivity flag: %s.\n",
347 * We can't determine the case sensitivity of
348 * the share. We have no choice but to use the
349 * user-specified case sensitivity setting.
351 if (smbc_getOptionCaseSensitive(context
)) {
352 cli_set_case_sensitive(c
, True
);
354 cli_set_case_sensitive(c
, False
);
356 } else if (!is_ipc
) {
358 ("Case sensitive: %s\n",
359 (fs_attrs
& FILE_CASE_SENSITIVE_SEARCH
362 cli_set_case_sensitive(
364 (fs_attrs
& FILE_CASE_SENSITIVE_SEARCH
370 * Regenerate the dev value since it's based on both
374 const char *remote_name
=
375 smbXcli_conn_remote_name(srv
->cli
->conn
);
377 srv
->dev
= (dev_t
)(str_checksum(remote_name
) ^
378 str_checksum(srv
->cli
->share
));
383 /* If we have a connection... */
386 /* ... then we're done here. Give 'em what they came for. */
391 /* If we're not asked to connect when a connection doesn't exist... */
392 if (! connect_if_not_found
) {
393 /* ... then we're done here. */
397 if (!*pp_workgroup
|| !*pp_username
|| !*pp_password
) {
402 DEBUG(4,("SMBC_server: server_n=[%s] server=[%s]\n", server_n
, server
));
404 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n
, server
));
406 status
= NT_STATUS_UNSUCCESSFUL
;
408 if (smbc_getOptionUseKerberos(context
)) {
409 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
412 if (smbc_getOptionFallbackAfterKerberos(context
)) {
413 flags
|= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
416 if (smbc_getOptionUseCCache(context
)) {
417 flags
|= CLI_FULL_CONNECTION_USE_CCACHE
;
420 if (share
== NULL
|| *share
== '\0' || is_ipc
) {
422 * Try 139 first for IPC$
424 status
= cli_connect_nb(server_n
, NULL
, NBT_SMB_PORT
, 0x20,
425 smbc_getNetbiosName(context
),
426 SMB_SIGNING_DEFAULT
, flags
, &c
);
429 if (!NT_STATUS_IS_OK(status
)) {
431 * No IPC$ or 139 did not work
433 status
= cli_connect_nb(server_n
, NULL
, 0, 0x20,
434 smbc_getNetbiosName(context
),
435 SMB_SIGNING_DEFAULT
, flags
, &c
);
438 if (!NT_STATUS_IS_OK(status
)) {
439 errno
= map_errno_from_nt_status(status
);
443 cli_set_timeout(c
, smbc_getTimeout(context
));
445 status
= smbXcli_negprot(c
->conn
, c
->timeout
, PROTOCOL_CORE
,
448 if (!NT_STATUS_IS_OK(status
)) {
454 username_used
= *pp_username
;
456 if (!NT_STATUS_IS_OK(cli_session_setup(c
, username_used
,
458 strlen(*pp_password
),
460 strlen(*pp_password
),
463 /* Failed. Try an anonymous login, if allowed by flags. */
466 if (smbc_getOptionNoAutoAnonymousLogin(context
) ||
467 !NT_STATUS_IS_OK(cli_session_setup(c
, username_used
,
478 status
= cli_init_creds(c
, username_used
,
479 *pp_workgroup
, *pp_password
);
480 if (!NT_STATUS_IS_OK(status
)) {
481 errno
= map_errno_from_nt_status(status
);
486 DEBUG(4,(" session setup ok\n"));
488 /* here's the fun part....to support 'msdfs proxy' shares
489 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
490 here before trying to connect to the original share.
491 cli_check_msdfs_proxy() will fail if it is a normal share. */
493 if ((smb1cli_conn_capabilities(c
->conn
) & CAP_DFS
) &&
494 cli_check_msdfs_proxy(ctx
, c
, share
,
495 &newserver
, &newshare
,
496 /* FIXME: cli_check_msdfs_proxy() does
497 not support smbc_smb_encrypt_level type */
498 context
->internal
->smb_encryption_level
?
504 srv
= SMBC_server_internal(ctx
, context
, connect_if_not_found
,
505 newserver
, newshare
, pp_workgroup
,
506 pp_username
, pp_password
, in_cache
);
507 TALLOC_FREE(newserver
);
508 TALLOC_FREE(newshare
);
512 /* must be a normal share */
514 status
= cli_tree_connect(c
, share
, "?????", *pp_password
,
515 strlen(*pp_password
)+1);
516 if (!NT_STATUS_IS_OK(status
)) {
517 errno
= map_errno_from_nt_status(status
);
522 DEBUG(4,(" tconx ok\n"));
524 /* Determine if this share supports case sensitivity */
526 DEBUG(4, ("IPC$ so ignore case sensitivity\n"));
527 status
= NT_STATUS_OK
;
529 status
= cli_get_fs_attr_info(c
, &fs_attrs
);
532 if (!NT_STATUS_IS_OK(status
)) {
533 DEBUG(4, ("Could not retrieve case sensitivity flag: %s.\n",
537 * We can't determine the case sensitivity of the share. We
538 * have no choice but to use the user-specified case
539 * sensitivity setting.
541 if (smbc_getOptionCaseSensitive(context
)) {
542 cli_set_case_sensitive(c
, True
);
544 cli_set_case_sensitive(c
, False
);
546 } else if (!is_ipc
) {
547 DEBUG(4, ("Case sensitive: %s\n",
548 (fs_attrs
& FILE_CASE_SENSITIVE_SEARCH
551 cli_set_case_sensitive(c
,
552 (fs_attrs
& FILE_CASE_SENSITIVE_SEARCH
557 if (context
->internal
->smb_encryption_level
) {
558 /* Attempt UNIX smb encryption. */
559 if (!NT_STATUS_IS_OK(cli_force_encryption(c
,
565 * context->smb_encryption_level == 1
566 * means don't fail if encryption can't be negotiated,
567 * == 2 means fail if encryption can't be negotiated.
570 DEBUG(4,(" SMB encrypt failed\n"));
572 if (context
->internal
->smb_encryption_level
== 2) {
578 DEBUG(4,(" SMB encrypt ok\n"));
582 * Ok, we have got a nice connection
583 * Let's allocate a server structure.
586 srv
= SMB_MALLOC_P(SMBCSRV
);
595 srv
->dev
= (dev_t
)(str_checksum(server
) ^ str_checksum(share
));
596 srv
->no_pathinfo
= False
;
597 srv
->no_pathinfo2
= False
;
598 srv
->no_nt_session
= False
;
601 if (!pp_workgroup
|| !*pp_workgroup
|| !**pp_workgroup
) {
602 workgroup
= talloc_strdup(ctx
, smbc_getWorkgroup(context
));
604 workgroup
= *pp_workgroup
;
614 /* set the credentials to make DFS work */
615 smbc_set_credentials_with_fallback(context
,
624 SMBC_server(TALLOC_CTX
*ctx
,
626 bool connect_if_not_found
,
634 bool in_cache
= false;
636 srv
= SMBC_server_internal(ctx
, context
, connect_if_not_found
,
637 server
, share
, pp_workgroup
,
638 pp_username
, pp_password
, &in_cache
);
647 /* Now add it to the cache (internal or external) */
648 /* Let the cache function set errno if it wants to */
650 if (smbc_getFunctionAddCachedServer(context
)(context
, srv
,
654 int saved_errno
= errno
;
655 DEBUG(3, (" Failed to add server to cache\n"));
664 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
665 server
, share
, srv
));
667 DLIST_ADD(context
->internal
->servers
, srv
);
672 * Connect to a server for getting/setting attributes, possibly on an existing
673 * connection. This works similarly to SMBC_server().
676 SMBC_attr_server(TALLOC_CTX
*ctx
,
685 struct cli_state
*ipc_cli
= NULL
;
686 struct rpc_pipe_client
*pipe_hnd
= NULL
;
689 SMBCSRV
*ipc_srv
=NULL
;
692 * Use srv->cli->desthost and srv->cli->share instead of
693 * server and share below to connect to the actual share,
694 * i.e., a normal share or a referred share from
695 * 'msdfs proxy' share.
697 srv
= SMBC_server(ctx
, context
, true, server
, share
,
698 pp_workgroup
, pp_username
, pp_password
);
702 server
= smbXcli_conn_remote_name(srv
->cli
->conn
);
703 share
= srv
->cli
->share
;
706 * See if we've already created this special connection. Reference
707 * our "special" share name '*IPC$', which is an impossible real share
708 * name due to the leading asterisk.
710 ipc_srv
= SMBC_find_server(ctx
, context
, server
, "*IPC$",
711 pp_workgroup
, pp_username
, pp_password
);
714 /* We didn't find a cached connection. Get the password */
715 if (!*pp_password
|| (*pp_password
)[0] == '\0') {
716 /* ... then retrieve it now. */
717 SMBC_call_auth_fn(ctx
, context
, server
, share
,
721 if (!*pp_workgroup
|| !*pp_username
|| !*pp_password
) {
728 if (smbc_getOptionUseKerberos(context
)) {
729 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
731 if (smbc_getOptionUseCCache(context
)) {
732 flags
|= CLI_FULL_CONNECTION_USE_CCACHE
;
735 nt_status
= cli_full_connection(&ipc_cli
,
736 lp_netbios_name(), server
,
737 NULL
, 0, "IPC$", "?????",
742 SMB_SIGNING_DEFAULT
);
743 if (! NT_STATUS_IS_OK(nt_status
)) {
744 DEBUG(1,("cli_full_connection failed! (%s)\n",
745 nt_errstr(nt_status
)));
750 if (context
->internal
->smb_encryption_level
) {
751 /* Attempt UNIX smb encryption. */
752 if (!NT_STATUS_IS_OK(cli_force_encryption(ipc_cli
,
758 * context->smb_encryption_level ==
759 * 1 means don't fail if encryption can't be
760 * negotiated, == 2 means fail if encryption
761 * can't be negotiated.
764 DEBUG(4,(" SMB encrypt failed on IPC$\n"));
766 if (context
->internal
->smb_encryption_level
== 2) {
767 cli_shutdown(ipc_cli
);
772 DEBUG(4,(" SMB encrypt ok on IPC$\n"));
775 ipc_srv
= SMB_MALLOC_P(SMBCSRV
);
778 cli_shutdown(ipc_cli
);
782 ZERO_STRUCTP(ipc_srv
);
783 ipc_srv
->cli
= ipc_cli
;
785 nt_status
= cli_rpc_pipe_open_noauth(
786 ipc_srv
->cli
, &ndr_table_lsarpc
.syntax_id
, &pipe_hnd
);
787 if (!NT_STATUS_IS_OK(nt_status
)) {
788 DEBUG(1, ("cli_nt_session_open fail!\n"));
790 cli_shutdown(ipc_srv
->cli
);
796 * Some systems don't support
797 * SEC_FLAG_MAXIMUM_ALLOWED, but NT sends 0x2000000
798 * so we might as well do it too.
801 nt_status
= rpccli_lsa_open_policy(
805 GENERIC_EXECUTE_ACCESS
,
808 if (!NT_STATUS_IS_OK(nt_status
)) {
809 errno
= SMBC_errno(context
, ipc_srv
->cli
);
810 cli_shutdown(ipc_srv
->cli
);
814 /* now add it to the cache (internal or external) */
816 errno
= 0; /* let cache function set errno if it likes */
817 if (smbc_getFunctionAddCachedServer(context
)(context
, ipc_srv
,
822 DEBUG(3, (" Failed to add server to cache\n"));
826 cli_shutdown(ipc_srv
->cli
);
831 DLIST_ADD(context
->internal
->servers
, ipc_srv
);