2 Unix SMB/CIFS implementation.
3 client connect/disconnect routines
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Gerald (Jerry) Carter 2004
6 Copyright (C) Jeremy Allison 2007-2009
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "libsmb/libsmb.h"
24 #include "libsmb/clirap.h"
27 #include "libsmb/nmblib.h"
28 #include "../libcli/smb/smbXcli_base.h"
30 /********************************************************************
33 DFS paths are *always* of the form \server\share\<pathname> (the \ characters
34 are not C escaped here).
36 - but if we're using POSIX paths then <pathname> may contain
37 '/' separators, not '\\' separators. So cope with '\\' or '/'
38 as a separator when looking at the pathname part.... JRA.
39 ********************************************************************/
41 /********************************************************************
42 Ensure a connection is encrypted.
43 ********************************************************************/
45 NTSTATUS cli_cm_force_encryption(struct cli_state *c,
49 const char *sharename)
51 NTSTATUS status = cli_force_encryption(c,
56 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED)) {
57 d_printf("Encryption required and "
58 "server that doesn't support "
59 "UNIX extensions - failing connect\n");
60 } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNKNOWN_REVISION)) {
61 d_printf("Encryption required and "
62 "can't get UNIX CIFS extensions "
63 "version from server.\n");
64 } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNSUPPORTED_COMPRESSION)) {
65 d_printf("Encryption required and "
66 "share %s doesn't support "
67 "encryption.\n", sharename);
68 } else if (!NT_STATUS_IS_OK(status)) {
69 d_printf("Encryption required and "
70 "setup failed with error %s.\n",
77 /********************************************************************
78 Return a connection to a server.
79 ********************************************************************/
81 static NTSTATUS do_connect(TALLOC_CTX *ctx,
84 const struct user_auth_info *auth_info,
90 struct cli_state **pcli)
92 struct cli_state *c = NULL;
95 char *newserver, *newshare;
101 /* make a copy so we don't modify the global string 'service' */
102 servicename = talloc_strdup(ctx,share);
104 return NT_STATUS_NO_MEMORY;
106 sharename = servicename;
107 if (*sharename == '\\') {
109 if (server == NULL) {
112 sharename = strchr_m(sharename,'\\');
114 return NT_STATUS_NO_MEMORY;
119 if (server == NULL) {
120 return NT_STATUS_INVALID_PARAMETER;
123 if (get_cmdline_auth_info_use_kerberos(auth_info)) {
124 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
126 if (get_cmdline_auth_info_fallback_after_kerberos(auth_info)) {
127 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
129 if (get_cmdline_auth_info_use_ccache(auth_info)) {
130 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
133 status = cli_connect_nb(
134 server, NULL, port, name_type, NULL,
135 get_cmdline_auth_info_signing_state(auth_info),
138 if (!NT_STATUS_IS_OK(status)) {
139 d_printf("Connection to %s failed (Error %s)\n",
145 if (max_protocol == 0) {
146 max_protocol = PROTOCOL_NT1;
148 DEBUG(4,(" session request ok\n"));
150 status = cli_negprot(c, max_protocol);
152 if (!NT_STATUS_IS_OK(status)) {
153 d_printf("protocol negotiation failed: %s\n",
159 username = get_cmdline_auth_info_username(auth_info);
160 password = get_cmdline_auth_info_password(auth_info);
162 status = cli_session_setup(c, username,
163 password, strlen(password),
164 password, strlen(password),
166 if (!NT_STATUS_IS_OK(status)) {
167 /* If a password was not supplied then
168 * try again with a null username. */
169 if (password[0] || !username[0] ||
170 get_cmdline_auth_info_use_kerberos(auth_info) ||
171 !NT_STATUS_IS_OK(status = cli_session_setup(c, "",
175 d_printf("session setup failed: %s\n",
177 if (NT_STATUS_EQUAL(status,
178 NT_STATUS_MORE_PROCESSING_REQUIRED))
179 d_printf("did you forget to run kinit?\n");
183 d_printf("Anonymous login successful\n");
184 status = cli_init_creds(c, "", lp_workgroup(), "");
186 status = cli_init_creds(c, username, lp_workgroup(), password);
189 if (!NT_STATUS_IS_OK(status)) {
190 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status)));
195 if ( show_sessetup ) {
196 if (*c->server_domain) {
197 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
198 c->server_domain,c->server_os,c->server_type));
199 } else if (*c->server_os || *c->server_type) {
200 DEBUG(0,("OS=[%s] Server=[%s]\n",
201 c->server_os,c->server_type));
204 DEBUG(4,(" session setup ok\n"));
206 /* here's the fun part....to support 'msdfs proxy' shares
207 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
208 here before trying to connect to the original share.
209 cli_check_msdfs_proxy() will fail if it is a normal share. */
211 if ((cli_state_capabilities(c) & CAP_DFS) &&
212 cli_check_msdfs_proxy(ctx, c, sharename,
213 &newserver, &newshare,
219 return do_connect(ctx, newserver,
220 newshare, auth_info, false,
221 force_encrypt, max_protocol,
222 port, name_type, pcli);
225 /* must be a normal share */
227 status = cli_tree_connect(c, sharename, "?????",
228 password, strlen(password)+1);
229 if (!NT_STATUS_IS_OK(status)) {
230 d_printf("tree connect failed: %s\n", nt_errstr(status));
236 status = cli_cm_force_encryption(c,
241 if (!NT_STATUS_IS_OK(status)) {
247 DEBUG(4,(" tconx ok\n"));
252 /****************************************************************************
253 ****************************************************************************/
255 static void cli_set_mntpoint(struct cli_state *cli, const char *mnt)
257 char *name = clean_name(NULL, mnt);
261 TALLOC_FREE(cli->dfs_mountpoint);
262 cli->dfs_mountpoint = talloc_strdup(cli, name);
266 /********************************************************************
267 Add a new connection to the list.
268 referring_cli == NULL means a new initial connection.
269 ********************************************************************/
271 static NTSTATUS cli_cm_connect(TALLOC_CTX *ctx,
272 struct cli_state *referring_cli,
275 const struct user_auth_info *auth_info,
281 struct cli_state **pcli)
283 struct cli_state *cli;
286 status = do_connect(ctx, server, share,
288 show_hdr, force_encrypt, max_protocol,
289 port, name_type, &cli);
291 if (!NT_STATUS_IS_OK(status)) {
295 /* Enter into the list. */
297 DLIST_ADD_END(referring_cli, cli, struct cli_state *);
300 if (referring_cli && referring_cli->requested_posix_capabilities) {
302 uint32 caplow, caphigh;
303 status = cli_unix_extensions_version(cli, &major, &minor,
305 if (NT_STATUS_IS_OK(status)) {
306 cli_set_unix_extensions_capabilities(cli,
316 /********************************************************************
317 Return a connection to a server on a particular share.
318 ********************************************************************/
320 static struct cli_state *cli_cm_find(struct cli_state *cli,
330 /* Search to the start of the list. */
331 for (p = cli; p; p = DLIST_PREV(p)) {
332 const char *remote_name =
333 smbXcli_conn_remote_name(p->conn);
335 if (strequal(server, remote_name) &&
336 strequal(share,p->share)) {
341 /* Search to the end of the list. */
342 for (p = cli->next; p; p = p->next) {
343 const char *remote_name =
344 smbXcli_conn_remote_name(p->conn);
346 if (strequal(server, remote_name) &&
347 strequal(share,p->share)) {
355 /****************************************************************************
356 Open a client connection to a \\server\share.
357 ****************************************************************************/
359 NTSTATUS cli_cm_open(TALLOC_CTX *ctx,
360 struct cli_state *referring_cli,
363 const struct user_auth_info *auth_info,
369 struct cli_state **pcli)
371 /* Try to reuse an existing connection in this list. */
372 struct cli_state *c = cli_cm_find(referring_cli, server, share);
380 if (auth_info == NULL) {
381 /* Can't do a new connection
382 * without auth info. */
383 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
384 "without auth info\n",
386 return NT_STATUS_INVALID_PARAMETER;
389 status = cli_cm_connect(ctx,
400 if (!NT_STATUS_IS_OK(status)) {
407 /****************************************************************************
408 ****************************************************************************/
410 void cli_cm_display(struct cli_state *cli)
414 for (i=0; cli; cli = cli->next,i++ ) {
415 d_printf("%d:\tserver=%s, share=%s\n",
416 i, smbXcli_conn_remote_name(cli->conn), cli->share);
420 /****************************************************************************
421 ****************************************************************************/
423 /****************************************************************************
424 ****************************************************************************/
427 void cli_cm_set_credentials(struct user_auth_info *auth_info)
429 SAFE_FREE(cm_creds.username);
430 cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
433 if (get_cmdline_auth_info_got_pass(auth_info)) {
434 cm_set_password(get_cmdline_auth_info_password(auth_info));
437 cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
438 cm_creds.fallback_after_kerberos = false;
439 cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
443 /**********************************************************************
444 split a dfs path into the server, share name, and extrapath components
445 **********************************************************************/
447 static bool split_dfs_path(TALLOC_CTX *ctx,
448 const char *nodepath,
458 *pp_extrapath = NULL;
460 path = talloc_strdup(ctx, nodepath);
465 if ( path[0] != '\\' ) {
469 p = strchr_m( path + 1, '\\' );
477 /* Look for any extra/deep path */
478 q = strchr_m(p, '\\');
482 *pp_extrapath = talloc_strdup(ctx, q);
484 *pp_extrapath = talloc_strdup(ctx, "");
486 if (*pp_extrapath == NULL) {
490 *pp_share = talloc_strdup(ctx, p);
491 if (*pp_share == NULL) {
495 *pp_server = talloc_strdup(ctx, &path[1]);
496 if (*pp_server == NULL) {
504 TALLOC_FREE(*pp_share);
505 TALLOC_FREE(*pp_extrapath);
510 /****************************************************************************
511 Return the original path truncated at the directory component before
512 the first wildcard character. Trust the caller to provide a NULL
514 ****************************************************************************/
516 static char *clean_path(TALLOC_CTX *ctx, const char *path)
522 /* No absolute paths. */
523 while (IS_DIRECTORY_SEP(*path)) {
527 path_out = talloc_strdup(ctx, path);
532 p1 = strchr_m(path_out, '*');
533 p2 = strchr_m(path_out, '?');
545 /* Now go back to the start of this component. */
546 p1 = strrchr_m(path_out, '/');
547 p2 = strrchr_m(path_out, '\\');
554 /* Strip any trailing separator */
556 len = strlen(path_out);
557 if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
558 path_out[len-1] = '\0';
564 /****************************************************************************
565 ****************************************************************************/
567 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
568 struct cli_state *cli,
571 char path_sep = '\\';
573 /* Ensure the extrapath doesn't start with a separator. */
574 while (IS_DIRECTORY_SEP(*dir)) {
578 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
581 return talloc_asprintf(ctx, "%c%s%c%s%c%s",
583 smbXcli_conn_remote_name(cli->conn),
590 /********************************************************************
591 check for dfs referral
592 ********************************************************************/
594 static bool cli_dfs_check_error(struct cli_state *cli, NTSTATUS expected,
597 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
599 if (!(cli_state_capabilities(cli) & CAP_UNICODE)) {
602 if (!(cli_state_capabilities(cli) & CAP_STATUS32)) {
605 if (NT_STATUS_EQUAL(status, expected)) {
611 /********************************************************************
612 Get the dfs referral link.
613 ********************************************************************/
615 NTSTATUS cli_dfs_get_referral(TALLOC_CTX *ctx,
616 struct cli_state *cli,
618 struct client_dfs_referral **refs,
622 unsigned int data_len = 0;
623 unsigned int param_len = 0;
625 uint16_t recv_flags2;
626 uint8_t *param = NULL;
627 uint8_t *rdata = NULL;
630 smb_ucs2_t *path_ucs;
631 char *consumed_path = NULL;
632 uint16_t consumed_ucs;
633 uint16 num_referrals;
634 struct client_dfs_referral *referrals = NULL;
636 TALLOC_CTX *frame = talloc_stackframe();
641 SSVAL(setup, 0, TRANSACT2_GET_DFS_REFERRAL);
643 param = talloc_array(talloc_tos(), uint8_t, 2);
645 status = NT_STATUS_NO_MEMORY;
648 SSVAL(param, 0, 0x03); /* max referral level */
650 param = trans2_bytes_push_str(param, cli_ucs2(cli),
651 path, strlen(path)+1,
654 status = NT_STATUS_NO_MEMORY;
657 param_len = talloc_get_size(param);
658 path_ucs = (smb_ucs2_t *)¶m[2];
660 status = cli_trans(talloc_tos(), cli, SMBtrans2,
664 NULL, 0, CLI_BUFFER_SIZE,
666 NULL, 0, NULL, /* rsetup */
668 &rdata, 4, &data_len);
669 if (!NT_STATUS_IS_OK(status)) {
673 endp = (char *)rdata + data_len;
675 consumed_ucs = SVAL(rdata, 0);
676 num_referrals = SVAL(rdata, 2);
678 /* consumed_ucs is the number of bytes
679 * of the UCS2 path consumed not counting any
680 * terminating null. We need to convert
681 * back to unix charset and count again
682 * to get the number of bytes consumed from
683 * the incoming path. */
686 if (pull_string_talloc(talloc_tos(),
694 status = map_nt_error_from_unix(errno);
696 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
700 if (consumed_path == NULL) {
701 status = map_nt_error_from_unix(errno);
704 *consumed = strlen(consumed_path);
706 if (num_referrals != 0) {
712 referrals = talloc_array(ctx, struct client_dfs_referral,
716 status = NT_STATUS_NO_MEMORY;
719 /* start at the referrals array */
722 for (i=0; i<num_referrals && p < endp; i++) {
726 ref_version = SVAL(p, 0);
727 ref_size = SVAL(p, 2);
728 node_offset = SVAL(p, 16);
730 if (ref_version != 3) {
735 referrals[i].proximity = SVAL(p, 8);
736 referrals[i].ttl = SVAL(p, 10);
738 if (p + node_offset > endp) {
739 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
742 clistr_pull_talloc(referrals,
745 &referrals[i].dfspath,
747 PTR_DIFF(endp, p+node_offset),
748 STR_TERMINATE|STR_UNICODE);
750 if (!referrals[i].dfspath) {
751 status = map_nt_error_from_unix(errno);
756 if (i < num_referrals) {
757 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
762 *num_refs = num_referrals;
771 /********************************************************************
772 ********************************************************************/
774 NTSTATUS cli_resolve_path(TALLOC_CTX *ctx,
776 const struct user_auth_info *dfs_auth_info,
777 struct cli_state *rootcli,
779 struct cli_state **targetcli,
780 char **pp_targetpath)
782 struct client_dfs_referral *refs = NULL;
785 struct cli_state *cli_ipc = NULL;
786 char *dfs_path = NULL;
787 char *cleanpath = NULL;
788 char *extrapath = NULL;
792 struct cli_state *newcli = NULL;
793 char *newpath = NULL;
794 char *newmount = NULL;
796 SMB_STRUCT_STAT sbuf;
800 if ( !rootcli || !path || !targetcli ) {
801 return NT_STATUS_INVALID_PARAMETER;
804 /* Don't do anything if this is not a DFS root. */
806 if ( !rootcli->dfsroot) {
807 *targetcli = rootcli;
808 *pp_targetpath = talloc_strdup(ctx, path);
809 if (!*pp_targetpath) {
810 return NT_STATUS_NO_MEMORY;
817 /* Send a trans2_query_path_info to check for a referral. */
819 cleanpath = clean_path(ctx, path);
821 return NT_STATUS_NO_MEMORY;
824 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
826 return NT_STATUS_NO_MEMORY;
829 status = cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes);
830 if (NT_STATUS_IS_OK(status)) {
831 /* This is an ordinary path, just return it. */
832 *targetcli = rootcli;
833 *pp_targetpath = talloc_strdup(ctx, path);
834 if (!*pp_targetpath) {
835 return NT_STATUS_NO_MEMORY;
840 /* Special case where client asked for a path that does not exist */
842 if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND,
844 *targetcli = rootcli;
845 *pp_targetpath = talloc_strdup(ctx, path);
846 if (!*pp_targetpath) {
847 return NT_STATUS_NO_MEMORY;
852 /* We got an error, check for DFS referral. */
854 if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED,
859 /* Check for the referral. */
861 status = cli_cm_open(ctx,
863 smbXcli_conn_remote_name(rootcli->conn),
867 cli_state_encryption_on(rootcli),
868 smbXcli_conn_protocol(rootcli->conn),
872 if (!NT_STATUS_IS_OK(status)) {
876 status = cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
877 &num_refs, &consumed);
878 if (!NT_STATUS_IS_OK(status) || !num_refs) {
882 /* Just store the first referral for now. */
884 if (!refs[0].dfspath) {
885 return NT_STATUS_NOT_FOUND;
887 if (!split_dfs_path(ctx, refs[0].dfspath, &server, &share,
889 return NT_STATUS_NOT_FOUND;
892 /* Make sure to recreate the original string including any wildcards. */
894 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
896 return NT_STATUS_NO_MEMORY;
898 pathlen = strlen(dfs_path);
899 consumed = MIN(pathlen, consumed);
900 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed]);
901 if (!*pp_targetpath) {
902 return NT_STATUS_NO_MEMORY;
904 dfs_path[consumed] = '\0';
907 * *pp_targetpath is now the unconsumed part of the path.
908 * dfs_path is now the consumed part of the path
909 * (in \server\share\path format).
912 /* Open the connection to the target server & share */
913 status = cli_cm_open(ctx, rootcli,
918 cli_state_encryption_on(rootcli),
919 smbXcli_conn_protocol(rootcli->conn),
923 if (!NT_STATUS_IS_OK(status)) {
924 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
929 if (extrapath && strlen(extrapath) > 0) {
930 /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
931 /* put the trailing \ on the path, so to be save we put one in if needed */
932 if (extrapath[strlen(extrapath)-1] != '\\' && **pp_targetpath != '\\') {
933 *pp_targetpath = talloc_asprintf(ctx,
938 *pp_targetpath = talloc_asprintf(ctx,
943 if (!*pp_targetpath) {
944 return NT_STATUS_NO_MEMORY;
948 /* parse out the consumed mount path */
949 /* trim off the \server\share\ */
953 if (*ppath != '\\') {
954 d_printf("cli_resolve_path: "
955 "dfs_path (%s) not in correct format.\n",
957 return NT_STATUS_NOT_FOUND;
960 ppath++; /* Now pointing at start of server name. */
962 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
963 return NT_STATUS_NOT_FOUND;
966 ppath++; /* Now pointing at start of share name. */
968 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
969 return NT_STATUS_NOT_FOUND;
972 ppath++; /* Now pointing at path component. */
974 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
976 return NT_STATUS_NOT_FOUND;
979 cli_set_mntpoint(*targetcli, newmount);
981 /* Check for another dfs referral, note that we are not
982 checking for loops here. */
984 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
985 status = cli_resolve_path(ctx,
992 if (NT_STATUS_IS_OK(status)) {
994 * When cli_resolve_path returns true here it's always
995 * returning the complete path in newpath, so we're done
999 *pp_targetpath = newpath;
1006 /* If returning true ensure we return a dfs root full path. */
1007 if ((*targetcli)->dfsroot) {
1008 dfs_path = talloc_strdup(ctx, *pp_targetpath);
1010 return NT_STATUS_NO_MEMORY;
1012 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
1013 if (*pp_targetpath == NULL) {
1014 return NT_STATUS_NO_MEMORY;
1018 return NT_STATUS_OK;
1021 /********************************************************************
1022 ********************************************************************/
1024 bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
1025 struct cli_state *cli,
1026 const char *sharename,
1027 char **pp_newserver,
1030 const char *username,
1031 const char *password,
1034 struct client_dfs_referral *refs = NULL;
1035 size_t num_refs = 0;
1036 size_t consumed = 0;
1037 char *fullpath = NULL;
1040 char *newextrapath = NULL;
1042 const char *remote_name;
1044 if (!cli || !sharename) {
1048 remote_name = smbXcli_conn_remote_name(cli->conn);
1049 cnum = cli_state_get_tid(cli);
1051 /* special case. never check for a referral on the IPC$ share */
1053 if (strequal(sharename, "IPC$")) {
1057 /* send a trans2_query_path_info to check for a referral */
1059 fullpath = talloc_asprintf(ctx, "\\%s\\%s", remote_name, sharename);
1064 /* check for the referral */
1066 if (!NT_STATUS_IS_OK(cli_tree_connect(cli, "IPC$", "IPC", NULL, 0))) {
1070 if (force_encrypt) {
1071 status = cli_cm_force_encryption(cli,
1076 if (!NT_STATUS_IS_OK(status)) {
1081 status = cli_dfs_get_referral(ctx, cli, fullpath, &refs,
1082 &num_refs, &consumed);
1083 res = NT_STATUS_IS_OK(status);
1085 status = cli_tdis(cli);
1086 if (!NT_STATUS_IS_OK(status)) {
1090 cli_state_set_tid(cli, cnum);
1092 if (!res || !num_refs) {
1096 if (!refs[0].dfspath) {
1100 if (!split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1101 pp_newshare, &newextrapath)) {
1105 /* check that this is not a self-referral */
1107 if (strequal(remote_name, *pp_newserver) &&
1108 strequal(sharename, *pp_newshare)) {