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 "source3/include/client.h"
28 #include "source3/libsmb/proto.h"
29 #include "libsmbclient.h"
30 #include "libsmb_internal.h"
31 #include "../librpc/gen_ndr/ndr_lsa.h"
32 #include "rpc_client/cli_pipe.h"
33 #include "rpc_client/cli_lsarpc.h"
34 #include "libcli/security/security.h"
35 #include "libsmb/nmblib.h"
36 #include "../libcli/smb/smbXcli_base.h"
37 #include "libsmb/smbsock_connect.h"
40 * Check a server for being alive and well.
41 * returns 0 if the server is in shape. Returns 1 on error
43 * Also usable outside libsmbclient to enable external cache
44 * to do some checks too.
47 SMBC_check_server(SMBCCTX * context,
50 struct cli_state *cli = server->cli;
51 time_t now, next_echo;
52 unsigned char data[16] = {0};
56 if (!cli_state_is_connected(cli)) {
60 now = time_mono(NULL);
61 next_echo = server->last_echo_time + cli->timeout/1000;
63 if ((server->last_echo_time != 0) && (now <= next_echo)) {
67 status = cli_echo(cli, 1, data_blob_const(data, sizeof(data)));
68 if (NT_STATUS_IS_OK(status)) {
73 * Some SMB2 servers (not Samba or Windows)
74 * check the session status on SMB2_ECHO and return
75 * NT_STATUS_USER_SESSION_DELETED
76 * if the session was not set. That's OK, they still
78 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13218
80 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
81 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
86 * Some NetApp servers return
87 * NT_STATUS_INVALID_PARAMETER.That's OK, they still
89 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13007
91 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
98 server->last_echo_time = now;
103 * Remove a server from the cached server list it's unused.
104 * On success, 0 is returned. 1 is returned if the server could not be removed.
106 * Also usable outside libsmbclient
109 SMBC_remove_unused_server(SMBCCTX * context,
114 /* are we being fooled ? */
115 if (!context || !context->internal->initialized || !srv) {
119 /* Check all open files/directories for a relation with this server */
120 for (file = context->internal->files; file; file = file->next) {
121 if (file->srv == srv) {
123 DBG_NOTICE("%p still used by %p.\n",
129 DLIST_REMOVE(context->internal->servers, srv);
131 cli_shutdown(srv->cli);
134 DBG_NOTICE("%p removed.\n", srv);
136 smbc_getFunctionRemoveCachedServer(context)(context, srv);
142 /****************************************************************
143 * Call the auth_fn with fixed size (fstring) buffers.
144 ***************************************************************/
146 SMBC_call_auth_fn(TALLOC_CTX *ctx,
154 fstring workgroup = { 0 };
155 fstring username = { 0 };
156 fstring password = { 0 };
157 smbc_get_auth_data_with_context_fn auth_with_context_fn;
159 if (*pp_workgroup != NULL) {
160 strlcpy(workgroup, *pp_workgroup, sizeof(workgroup));
162 if (*pp_username != NULL) {
163 strlcpy(username, *pp_username, sizeof(username));
165 if (*pp_password != NULL) {
166 strlcpy(password, *pp_password, sizeof(password));
169 /* See if there's an authentication with context function provided */
170 auth_with_context_fn = smbc_getFunctionAuthDataWithContext(context);
171 if (auth_with_context_fn)
173 (* auth_with_context_fn)(context,
175 workgroup, sizeof(workgroup),
176 username, sizeof(username),
177 password, sizeof(password));
181 smbc_getFunctionAuthData(context)(server, share,
182 workgroup, sizeof(workgroup),
183 username, sizeof(username),
184 password, sizeof(password));
187 TALLOC_FREE(*pp_workgroup);
188 TALLOC_FREE(*pp_username);
189 TALLOC_FREE(*pp_password);
191 *pp_workgroup = talloc_strdup(ctx, workgroup);
192 *pp_username = talloc_strdup(ctx, username);
193 *pp_password = talloc_strdup(ctx, password);
198 SMBC_get_auth_data(const char *server, const char *share,
199 char *workgroup_buf, int workgroup_buf_len,
200 char *username_buf, int username_buf_len,
201 char *password_buf, int password_buf_len)
203 /* Default function just uses provided data. Nothing to do. */
209 SMBC_find_server(TALLOC_CTX *ctx,
220 if (!pp_workgroup || !pp_username || !pp_password) {
226 srv = smbc_getFunctionGetCachedServer(context)(context,
231 if (!auth_called && !srv && (!*pp_username || !(*pp_username)[0] ||
232 !*pp_password || !(*pp_password)[0])) {
233 SMBC_call_auth_fn(ctx, context, server, share,
234 pp_workgroup, pp_username, pp_password);
237 * However, smbc_auth_fn may have picked up info relating to
238 * an existing connection, so try for an existing connection
242 goto check_server_cache;
250 if (smbc_getFunctionCheckServer(context)(context, srv)) {
252 * This server is no good anymore
253 * Try to remove it and check for more possible
254 * servers in the cache
256 if (smbc_getFunctionRemoveUnusedServer(context)(context, srv)) {
258 * We could not remove the server completely,
259 * remove it from the cache so we will not get
260 * it again. It will be removed when the last
261 * file/dir is closed.
263 smbc_getFunctionRemoveCachedServer(context)(context,
268 * Maybe there are more cached connections to this
271 goto check_server_cache;
277 static struct cli_credentials *SMBC_auth_credentials(TALLOC_CTX *mem_ctx,
280 const char *username,
281 const char *password)
283 struct cli_credentials *creds = NULL;
284 bool use_kerberos = false;
285 bool fallback_after_kerberos = false;
286 bool use_ccache = false;
287 bool pw_nt_hash = false;
289 use_kerberos = smbc_getOptionUseKerberos(context);
290 fallback_after_kerberos = smbc_getOptionFallbackAfterKerberos(context);
291 use_ccache = smbc_getOptionUseCCache(context);
292 pw_nt_hash = smbc_getOptionUseNTHash(context);
294 creds = cli_session_creds_init(mem_ctx,
300 fallback_after_kerberos,
307 switch (context->internal->smb_encryption_level) {
308 case SMBC_ENCRYPTLEVEL_DEFAULT:
309 /* Use the config option */
311 case SMBC_ENCRYPTLEVEL_NONE:
312 (void)cli_credentials_set_smb_encryption(
317 case SMBC_ENCRYPTLEVEL_REQUEST:
318 (void)cli_credentials_set_smb_encryption(
320 SMB_ENCRYPTION_DESIRED,
323 case SMBC_ENCRYPTLEVEL_REQUIRE:
325 (void)cli_credentials_set_smb_encryption(
327 SMB_ENCRYPTION_REQUIRED,
337 * Connect to a server, possibly on an existing connection
339 * Here, what we want to do is: If the server and username
340 * match an existing connection, reuse that, otherwise, establish a
343 * If we have to create a new connection, call the auth_fn to get the
344 * info we need, unless the username and password were passed in.
348 SMBC_server_internal(TALLOC_CTX *ctx,
350 bool connect_if_not_found,
352 const struct smb_transports *transports,
360 char *workgroup = NULL;
361 struct cli_state *c = NULL;
362 const char *server_n = server;
363 int is_ipc = (share != NULL && strcmp(share, "IPC$") == 0);
364 uint32_t fs_attrs = 0;
365 const char *username_used = NULL;
366 const char *password_used = NULL;
368 char *newserver, *newshare;
370 struct smbXcli_tcon *tcon = NULL;
371 int signing_state = SMB_SIGNING_DEFAULT;
372 struct cli_credentials *creds = NULL;
373 struct smb_transports ats = *transports;
375 const struct smb_transports *ts = &ats;
376 struct smb_transports ots = { .num_transports = 0, };
377 struct smb_transports nts = { .num_transports = 0, };
381 if (server[0] == 0) {
386 for (ati = 0; ati < ats.num_transports; ati++) {
387 const struct smb_transport *at =
388 &ats.transports[ati];
390 if (at->type == SMB_TRANSPORT_TYPE_NBT) {
391 struct smb_transport *nt =
392 &nts.transports[nts.num_transports];
394 nts.num_transports += 1;
396 struct smb_transport *ot =
397 &ots.transports[ots.num_transports];
399 ots.num_transports += 1;
403 /* Look for a cached connection */
404 srv = SMBC_find_server(ctx, context, server, share,
405 pp_workgroup, pp_username, pp_password);
408 * If we found a connection and we're only allowed one share per
412 share != NULL && *share != '\0' &&
413 smbc_getOptionOneSharePerServer(context)) {
416 * ... then if there's no current connection to the share,
417 * connect to it. SMBC_find_server(), or rather the function
418 * pointed to by context->get_cached_srv_fn which
419 * was called by SMBC_find_server(), will have issued a tree
420 * disconnect if the requested share is not the same as the
421 * one that was already connected.
425 * Use srv->cli->desthost and srv->cli->share instead of
426 * server and share below to connect to the actual share,
427 * i.e., a normal share or a referred share from
428 * 'msdfs proxy' share.
430 if (!cli_state_has_tcon(srv->cli)) {
431 /* Ensure we have accurate auth info */
432 SMBC_call_auth_fn(ctx, context,
433 smbXcli_conn_remote_name(srv->cli->conn),
439 if (!*pp_workgroup || !*pp_username || !*pp_password) {
441 cli_shutdown(srv->cli);
443 smbc_getFunctionRemoveCachedServer(context)(context,
449 * We don't need to renegotiate encryption
450 * here as the encryption context is not per
454 status = cli_tree_connect(srv->cli,
458 if (!NT_STATUS_IS_OK(status)) {
459 cli_shutdown(srv->cli);
460 errno = map_errno_from_nt_status(status);
462 smbc_getFunctionRemoveCachedServer(context)(context,
468 /* Determine if this share supports case sensitivity */
471 ("IPC$ so ignore case sensitivity\n"));
472 status = NT_STATUS_OK;
474 status = cli_get_fs_attr_info(srv->cli, &fs_attrs);
477 if (!NT_STATUS_IS_OK(status)) {
478 DEBUG(4, ("Could not retrieve "
479 "case sensitivity flag: %s.\n",
483 * We can't determine the case sensitivity of
484 * the share. We have no choice but to use the
485 * user-specified case sensitivity setting.
487 if (smbc_getOptionCaseSensitive(context)) {
488 cli_set_case_sensitive(srv->cli, true);
490 cli_set_case_sensitive(srv->cli, false);
492 } else if (!is_ipc) {
494 ("Case sensitive: %s\n",
495 (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
498 cli_set_case_sensitive(
500 (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
506 * Regenerate the dev value since it's based on both
510 const char *remote_name =
511 smbXcli_conn_remote_name(srv->cli->conn);
513 srv->dev = (dev_t)(str_checksum(remote_name) ^
514 str_checksum(srv->cli->share));
521 /* If we have a connection... */
524 /* ... then we're done here. Give 'em what they came for. */
529 /* If we're not asked to connect when a connection doesn't exist... */
530 if (! connect_if_not_found) {
531 /* ... then we're done here. */
535 if (!*pp_workgroup || !*pp_username || !*pp_password) {
540 DEBUG(4,("SMBC_server: server_n=[%s] server=[%s]\n", server_n, server));
542 DEBUG(4,(" -> server_n=[%s] server=[%s]\n", server_n, server));
544 status = NT_STATUS_UNSUCCESSFUL;
546 if (context->internal->smb_encryption_level > SMBC_ENCRYPTLEVEL_NONE) {
547 signing_state = SMB_SIGNING_REQUIRED;
550 if (context->internal->posix_extensions) {
551 flags |= CLI_FULL_CONNECTION_REQUEST_POSIX;
554 if (nts.num_transports != 0 && ots.num_transports != 0) {
555 if (share == NULL || *share == '\0' || is_ipc) {
557 * Try 139 first for IPC$
561 status = cli_start_connection(
562 context->internal->mem_ctx,
564 smbc_getNetbiosName(context),
567 &nts, /* transports */
573 if (!NT_STATUS_IS_OK(status)) {
575 * No IPC$ or 139 did not work
577 status = cli_start_connection(context->internal->mem_ctx,
579 smbc_getNetbiosName(context),
587 if (!NT_STATUS_IS_OK(status)) {
588 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
589 DBG_ERR("NetBIOS support disabled, unable to connect\n");
592 errno = map_errno_from_nt_status(status);
596 cli_set_timeout(c, smbc_getTimeout(context));
598 username_used = *pp_username;
599 password_used = *pp_password;
601 creds = SMBC_auth_credentials(c,
612 status = cli_session_setup_creds(c, creds);
613 if (!NT_STATUS_IS_OK(status)) {
615 /* Failed. Try an anonymous login, if allowed by flags. */
619 if (smbc_getOptionNoAutoAnonymousLogin(context) ||
620 !NT_STATUS_IS_OK(cli_session_setup_anon(c))) {
623 errno = map_errno_from_nt_status(status);
628 DEBUG(4,(" session setup ok\n"));
630 /* here's the fun part....to support 'msdfs proxy' shares
631 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
632 here before trying to connect to the original share.
633 cli_check_msdfs_proxy() will fail if it is a normal share. */
635 if (smbXcli_conn_dfs_supported(c->conn) &&
636 cli_check_msdfs_proxy(ctx, c, share,
637 &newserver, &newshare,
640 srv = SMBC_server_internal(ctx, context, connect_if_not_found,
641 newserver, &ats, newshare, pp_workgroup,
642 pp_username, pp_password, in_cache);
643 TALLOC_FREE(newserver);
644 TALLOC_FREE(newshare);
648 /* must be a normal share */
650 status = cli_tree_connect_creds(c, share, "?????", creds);
651 if (!NT_STATUS_IS_OK(status)) {
653 errno = map_errno_from_nt_status(status);
657 DEBUG(4,(" tconx ok\n"));
659 if (smbXcli_conn_protocol(c->conn) >= PROTOCOL_SMB2_02) {
665 /* Determine if this share supports case sensitivity */
667 DEBUG(4, ("IPC$ so ignore case sensitivity\n"));
668 status = NT_STATUS_OK;
670 status = cli_get_fs_attr_info(c, &fs_attrs);
673 if (!NT_STATUS_IS_OK(status)) {
674 DEBUG(4, ("Could not retrieve case sensitivity flag: %s.\n",
678 * We can't determine the case sensitivity of the share. We
679 * have no choice but to use the user-specified case
680 * sensitivity setting.
682 if (smbc_getOptionCaseSensitive(context)) {
683 cli_set_case_sensitive(c, True);
685 cli_set_case_sensitive(c, False);
687 } else if (!is_ipc) {
688 DEBUG(4, ("Case sensitive: %s\n",
689 (fs_attrs & FILE_CASE_SENSITIVE_SEARCH
692 smbXcli_tcon_set_fs_attributes(tcon, fs_attrs);
696 * Ok, we have got a nice connection
697 * Let's allocate a server structure.
700 srv = SMB_CALLOC_ARRAY(SMBCSRV, 1);
707 DLIST_ADD(srv->cli, c);
708 srv->dev = (dev_t)(str_checksum(server) ^ str_checksum(share));
709 srv->no_pathinfo = False;
710 srv->no_pathinfo2 = False;
711 srv->no_pathinfo3 = False;
712 srv->no_nt_session = False;
715 if (!pp_workgroup || !*pp_workgroup || !**pp_workgroup) {
716 workgroup = talloc_strdup(ctx, smbc_getWorkgroup(context));
718 workgroup = *pp_workgroup;
728 /* set the credentials to make DFS work */
729 smbc_set_credentials_with_fallback(context,
738 SMBC_server(TALLOC_CTX *ctx,
740 bool connect_if_not_found,
749 bool in_cache = false;
750 struct smb_transports ts = smbsock_transports_from_port(port);
752 srv = SMBC_server_internal(ctx, context, connect_if_not_found,
753 server, &ts, share, pp_workgroup,
754 pp_username, pp_password, &in_cache);
763 /* Now add it to the cache (internal or external) */
764 /* Let the cache function set errno if it wants to */
766 if (smbc_getFunctionAddCachedServer(context)(context, srv,
770 int saved_errno = errno;
771 DEBUG(3, (" Failed to add server to cache\n"));
780 DEBUG(2, ("Server connect ok: //%s/%s: %p\n",
781 server, share, srv));
783 DLIST_ADD(context->internal->servers, srv);
788 * Connect to a server for getting/setting attributes, possibly on an existing
789 * connection. This works similarly to SMBC_server().
792 SMBC_attr_server(TALLOC_CTX *ctx,
802 struct cli_state *ipc_cli = NULL;
803 struct rpc_pipe_client *pipe_hnd = NULL;
806 SMBCSRV *ipc_srv=NULL;
807 struct smb_transports ts = smbsock_transports_from_port(port);
808 struct cli_credentials *creds = NULL;
811 * Use srv->cli->desthost and srv->cli->share instead of
812 * server and share below to connect to the actual share,
813 * i.e., a normal share or a referred share from
814 * 'msdfs proxy' share.
816 srv = SMBC_server(ctx, context, true, server, port, share,
817 pp_workgroup, pp_username, pp_password);
821 server = smbXcli_conn_remote_name(srv->cli->conn);
822 share = srv->cli->share;
825 * See if we've already created this special connection. Reference
826 * our "special" share name '*IPC$', which is an impossible real share
827 * name due to the leading asterisk.
829 ipc_srv = SMBC_find_server(ctx, context, server, "*IPC$",
830 pp_workgroup, pp_username, pp_password);
831 if (ipc_srv != NULL) {
835 /* We didn't find a cached connection. Get the password */
836 if (!*pp_password || (*pp_password)[0] == '\0') {
837 /* ... then retrieve it now. */
838 SMBC_call_auth_fn(ctx, context, server, share,
842 if (!*pp_workgroup || !*pp_username || !*pp_password) {
850 creds = SMBC_auth_credentials(NULL,
860 nt_status = cli_full_connection_creds(NULL,
870 if (! NT_STATUS_IS_OK(nt_status)) {
872 DEBUG(1,("cli_full_connection failed! (%s)\n",
873 nt_errstr(nt_status)));
877 talloc_steal(ipc_cli, creds);
879 ipc_srv = SMB_CALLOC_ARRAY(SMBCSRV, 1);
882 cli_shutdown(ipc_cli);
885 DLIST_ADD(ipc_srv->cli, ipc_cli);
887 nt_status = cli_rpc_pipe_open_noauth(
888 ipc_srv->cli, &ndr_table_lsarpc, &pipe_hnd);
889 if (!NT_STATUS_IS_OK(nt_status)) {
890 DEBUG(1, ("cli_nt_session_open fail!\n"));
892 cli_shutdown(ipc_srv->cli);
898 * Some systems don't support
899 * SEC_FLAG_MAXIMUM_ALLOWED, but NT sends 0x2000000
900 * so we might as well do it too.
903 nt_status = rpccli_lsa_open_policy(
907 GENERIC_EXECUTE_ACCESS,
910 if (!NT_STATUS_IS_OK(nt_status)) {
911 cli_shutdown(ipc_srv->cli);
913 errno = cli_status_to_errno(nt_status);
917 /* now add it to the cache (internal or external) */
919 errno = 0; /* let cache function set errno if it likes */
920 if (smbc_getFunctionAddCachedServer(context)(context, ipc_srv,
925 DEBUG(3, (" Failed to add server to cache\n"));
929 cli_shutdown(ipc_srv->cli);
934 DLIST_ADD(context->internal->servers, ipc_srv);