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 = smbXcli_negprot(c->conn, c->timeout, PROTOCOL_CORE,
153 if (!NT_STATUS_IS_OK(status)) {
154 d_printf("protocol negotiation failed: %s\n",
160 username = get_cmdline_auth_info_username(auth_info);
161 password = get_cmdline_auth_info_password(auth_info);
163 status = cli_session_setup(c, username,
164 password, strlen(password),
165 password, strlen(password),
167 if (!NT_STATUS_IS_OK(status)) {
168 /* If a password was not supplied then
169 * try again with a null username. */
170 if (password[0] || !username[0] ||
171 get_cmdline_auth_info_use_kerberos(auth_info) ||
172 !NT_STATUS_IS_OK(status = cli_session_setup(c, "",
176 d_printf("session setup failed: %s\n",
178 if (NT_STATUS_EQUAL(status,
179 NT_STATUS_MORE_PROCESSING_REQUIRED))
180 d_printf("did you forget to run kinit?\n");
184 d_printf("Anonymous login successful\n");
185 status = cli_init_creds(c, "", lp_workgroup(), "");
187 status = cli_init_creds(c, username, lp_workgroup(), password);
190 if (!NT_STATUS_IS_OK(status)) {
191 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status)));
196 if ( show_sessetup ) {
197 if (*c->server_domain) {
198 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
199 c->server_domain,c->server_os,c->server_type));
200 } else if (*c->server_os || *c->server_type) {
201 DEBUG(0,("OS=[%s] Server=[%s]\n",
202 c->server_os,c->server_type));
205 DEBUG(4,(" session setup ok\n"));
207 /* here's the fun part....to support 'msdfs proxy' shares
208 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
209 here before trying to connect to the original share.
210 cli_check_msdfs_proxy() will fail if it is a normal share. */
212 if ((smb1cli_conn_capabilities(c->conn) & CAP_DFS) &&
213 cli_check_msdfs_proxy(ctx, c, sharename,
214 &newserver, &newshare,
220 return do_connect(ctx, newserver,
221 newshare, auth_info, false,
222 force_encrypt, max_protocol,
223 port, name_type, pcli);
226 /* must be a normal share */
228 status = cli_tree_connect(c, sharename, "?????",
229 password, strlen(password)+1);
230 if (!NT_STATUS_IS_OK(status)) {
231 d_printf("tree connect failed: %s\n", nt_errstr(status));
237 status = cli_cm_force_encryption(c,
242 if (!NT_STATUS_IS_OK(status)) {
248 DEBUG(4,(" tconx ok\n"));
253 /****************************************************************************
254 ****************************************************************************/
256 static void cli_set_mntpoint(struct cli_state *cli, const char *mnt)
258 char *name = clean_name(NULL, mnt);
262 TALLOC_FREE(cli->dfs_mountpoint);
263 cli->dfs_mountpoint = talloc_strdup(cli, name);
267 /********************************************************************
268 Add a new connection to the list.
269 referring_cli == NULL means a new initial connection.
270 ********************************************************************/
272 static NTSTATUS cli_cm_connect(TALLOC_CTX *ctx,
273 struct cli_state *referring_cli,
276 const struct user_auth_info *auth_info,
282 struct cli_state **pcli)
284 struct cli_state *cli;
287 status = do_connect(ctx, server, share,
289 show_hdr, force_encrypt, max_protocol,
290 port, name_type, &cli);
292 if (!NT_STATUS_IS_OK(status)) {
296 /* Enter into the list. */
298 DLIST_ADD_END(referring_cli, cli, struct cli_state *);
301 if (referring_cli && referring_cli->requested_posix_capabilities) {
303 uint32 caplow, caphigh;
304 status = cli_unix_extensions_version(cli, &major, &minor,
306 if (NT_STATUS_IS_OK(status)) {
307 cli_set_unix_extensions_capabilities(cli,
317 /********************************************************************
318 Return a connection to a server on a particular share.
319 ********************************************************************/
321 static struct cli_state *cli_cm_find(struct cli_state *cli,
331 /* Search to the start of the list. */
332 for (p = cli; p; p = DLIST_PREV(p)) {
333 const char *remote_name =
334 smbXcli_conn_remote_name(p->conn);
336 if (strequal(server, remote_name) &&
337 strequal(share,p->share)) {
342 /* Search to the end of the list. */
343 for (p = cli->next; p; p = p->next) {
344 const char *remote_name =
345 smbXcli_conn_remote_name(p->conn);
347 if (strequal(server, remote_name) &&
348 strequal(share,p->share)) {
356 /****************************************************************************
357 Open a client connection to a \\server\share.
358 ****************************************************************************/
360 NTSTATUS cli_cm_open(TALLOC_CTX *ctx,
361 struct cli_state *referring_cli,
364 const struct user_auth_info *auth_info,
370 struct cli_state **pcli)
372 /* Try to reuse an existing connection in this list. */
373 struct cli_state *c = cli_cm_find(referring_cli, server, share);
381 if (auth_info == NULL) {
382 /* Can't do a new connection
383 * without auth info. */
384 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
385 "without auth info\n",
387 return NT_STATUS_INVALID_PARAMETER;
390 status = cli_cm_connect(ctx,
401 if (!NT_STATUS_IS_OK(status)) {
408 /****************************************************************************
409 ****************************************************************************/
411 void cli_cm_display(struct cli_state *cli)
415 for (i=0; cli; cli = cli->next,i++ ) {
416 d_printf("%d:\tserver=%s, share=%s\n",
417 i, smbXcli_conn_remote_name(cli->conn), cli->share);
421 /****************************************************************************
422 ****************************************************************************/
424 /****************************************************************************
425 ****************************************************************************/
428 void cli_cm_set_credentials(struct user_auth_info *auth_info)
430 SAFE_FREE(cm_creds.username);
431 cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
434 if (get_cmdline_auth_info_got_pass(auth_info)) {
435 cm_set_password(get_cmdline_auth_info_password(auth_info));
438 cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
439 cm_creds.fallback_after_kerberos = false;
440 cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
444 /**********************************************************************
445 split a dfs path into the server, share name, and extrapath components
446 **********************************************************************/
448 static bool split_dfs_path(TALLOC_CTX *ctx,
449 const char *nodepath,
459 *pp_extrapath = NULL;
461 path = talloc_strdup(ctx, nodepath);
466 if ( path[0] != '\\' ) {
470 p = strchr_m( path + 1, '\\' );
478 /* Look for any extra/deep path */
479 q = strchr_m(p, '\\');
483 *pp_extrapath = talloc_strdup(ctx, q);
485 *pp_extrapath = talloc_strdup(ctx, "");
487 if (*pp_extrapath == NULL) {
491 *pp_share = talloc_strdup(ctx, p);
492 if (*pp_share == NULL) {
496 *pp_server = talloc_strdup(ctx, &path[1]);
497 if (*pp_server == NULL) {
505 TALLOC_FREE(*pp_share);
506 TALLOC_FREE(*pp_extrapath);
511 /****************************************************************************
512 Return the original path truncated at the directory component before
513 the first wildcard character. Trust the caller to provide a NULL
515 ****************************************************************************/
517 static char *clean_path(TALLOC_CTX *ctx, const char *path)
523 /* No absolute paths. */
524 while (IS_DIRECTORY_SEP(*path)) {
528 path_out = talloc_strdup(ctx, path);
533 p1 = strchr_m(path_out, '*');
534 p2 = strchr_m(path_out, '?');
546 /* Now go back to the start of this component. */
547 p1 = strrchr_m(path_out, '/');
548 p2 = strrchr_m(path_out, '\\');
555 /* Strip any trailing separator */
557 len = strlen(path_out);
558 if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
559 path_out[len-1] = '\0';
565 /****************************************************************************
566 ****************************************************************************/
568 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
569 struct cli_state *cli,
572 char path_sep = '\\';
574 /* Ensure the extrapath doesn't start with a separator. */
575 while (IS_DIRECTORY_SEP(*dir)) {
579 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
582 return talloc_asprintf(ctx, "%c%s%c%s%c%s",
584 smbXcli_conn_remote_name(cli->conn),
591 /********************************************************************
592 check for dfs referral
593 ********************************************************************/
595 static bool cli_dfs_check_error(struct cli_state *cli, NTSTATUS expected,
598 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
600 if (!(smb1cli_conn_capabilities(cli->conn) & CAP_UNICODE)) {
603 if (!(smb1cli_conn_capabilities(cli->conn) & CAP_STATUS32)) {
606 if (NT_STATUS_EQUAL(status, expected)) {
612 /********************************************************************
613 Get the dfs referral link.
614 ********************************************************************/
616 NTSTATUS cli_dfs_get_referral(TALLOC_CTX *ctx,
617 struct cli_state *cli,
619 struct client_dfs_referral **refs,
623 unsigned int data_len = 0;
624 unsigned int param_len = 0;
626 uint16_t recv_flags2;
627 uint8_t *param = NULL;
628 uint8_t *rdata = NULL;
631 smb_ucs2_t *path_ucs;
632 char *consumed_path = NULL;
633 uint16_t consumed_ucs;
634 uint16 num_referrals;
635 struct client_dfs_referral *referrals = NULL;
637 TALLOC_CTX *frame = talloc_stackframe();
642 SSVAL(setup, 0, TRANSACT2_GET_DFS_REFERRAL);
644 param = talloc_array(talloc_tos(), uint8_t, 2);
646 status = NT_STATUS_NO_MEMORY;
649 SSVAL(param, 0, 0x03); /* max referral level */
651 param = trans2_bytes_push_str(param, cli_ucs2(cli),
652 path, strlen(path)+1,
655 status = NT_STATUS_NO_MEMORY;
658 param_len = talloc_get_size(param);
659 path_ucs = (smb_ucs2_t *)¶m[2];
661 status = cli_trans(talloc_tos(), cli, SMBtrans2,
665 NULL, 0, CLI_BUFFER_SIZE,
667 NULL, 0, NULL, /* rsetup */
669 &rdata, 4, &data_len);
670 if (!NT_STATUS_IS_OK(status)) {
674 endp = (char *)rdata + data_len;
676 consumed_ucs = SVAL(rdata, 0);
677 num_referrals = SVAL(rdata, 2);
679 /* consumed_ucs is the number of bytes
680 * of the UCS2 path consumed not counting any
681 * terminating null. We need to convert
682 * back to unix charset and count again
683 * to get the number of bytes consumed from
684 * the incoming path. */
687 if (pull_string_talloc(talloc_tos(),
695 status = map_nt_error_from_unix(errno);
697 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
701 if (consumed_path == NULL) {
702 status = map_nt_error_from_unix(errno);
705 *consumed = strlen(consumed_path);
707 if (num_referrals != 0) {
713 referrals = talloc_array(ctx, struct client_dfs_referral,
717 status = NT_STATUS_NO_MEMORY;
720 /* start at the referrals array */
723 for (i=0; i<num_referrals && p < endp; i++) {
727 ref_version = SVAL(p, 0);
728 ref_size = SVAL(p, 2);
729 node_offset = SVAL(p, 16);
731 if (ref_version != 3) {
736 referrals[i].proximity = SVAL(p, 8);
737 referrals[i].ttl = SVAL(p, 10);
739 if (p + node_offset > endp) {
740 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
743 clistr_pull_talloc(referrals,
746 &referrals[i].dfspath,
748 PTR_DIFF(endp, p+node_offset),
749 STR_TERMINATE|STR_UNICODE);
751 if (!referrals[i].dfspath) {
752 status = map_nt_error_from_unix(errno);
757 if (i < num_referrals) {
758 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
763 *num_refs = num_referrals;
772 /********************************************************************
773 ********************************************************************/
775 NTSTATUS cli_resolve_path(TALLOC_CTX *ctx,
777 const struct user_auth_info *dfs_auth_info,
778 struct cli_state *rootcli,
780 struct cli_state **targetcli,
781 char **pp_targetpath)
783 struct client_dfs_referral *refs = NULL;
786 struct cli_state *cli_ipc = NULL;
787 char *dfs_path = NULL;
788 char *cleanpath = NULL;
789 char *extrapath = NULL;
793 struct cli_state *newcli = NULL;
794 char *newpath = NULL;
795 char *newmount = NULL;
797 SMB_STRUCT_STAT sbuf;
801 if ( !rootcli || !path || !targetcli ) {
802 return NT_STATUS_INVALID_PARAMETER;
805 /* Don't do anything if this is not a DFS root. */
807 if ( !rootcli->dfsroot) {
808 *targetcli = rootcli;
809 *pp_targetpath = talloc_strdup(ctx, path);
810 if (!*pp_targetpath) {
811 return NT_STATUS_NO_MEMORY;
818 /* Send a trans2_query_path_info to check for a referral. */
820 cleanpath = clean_path(ctx, path);
822 return NT_STATUS_NO_MEMORY;
825 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
827 return NT_STATUS_NO_MEMORY;
830 status = cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes);
831 if (NT_STATUS_IS_OK(status)) {
832 /* This is an ordinary path, just return it. */
833 *targetcli = rootcli;
834 *pp_targetpath = talloc_strdup(ctx, path);
835 if (!*pp_targetpath) {
836 return NT_STATUS_NO_MEMORY;
841 /* Special case where client asked for a path that does not exist */
843 if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND,
845 *targetcli = rootcli;
846 *pp_targetpath = talloc_strdup(ctx, path);
847 if (!*pp_targetpath) {
848 return NT_STATUS_NO_MEMORY;
853 /* We got an error, check for DFS referral. */
855 if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED,
860 /* Check for the referral. */
862 status = cli_cm_open(ctx,
864 smbXcli_conn_remote_name(rootcli->conn),
868 smb1cli_conn_encryption_on(rootcli->conn),
869 smbXcli_conn_protocol(rootcli->conn),
873 if (!NT_STATUS_IS_OK(status)) {
877 status = cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
878 &num_refs, &consumed);
879 if (!NT_STATUS_IS_OK(status) || !num_refs) {
883 /* Just store the first referral for now. */
885 if (!refs[0].dfspath) {
886 return NT_STATUS_NOT_FOUND;
888 if (!split_dfs_path(ctx, refs[0].dfspath, &server, &share,
890 return NT_STATUS_NOT_FOUND;
893 /* Make sure to recreate the original string including any wildcards. */
895 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
897 return NT_STATUS_NO_MEMORY;
899 pathlen = strlen(dfs_path);
900 consumed = MIN(pathlen, consumed);
901 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed]);
902 if (!*pp_targetpath) {
903 return NT_STATUS_NO_MEMORY;
905 dfs_path[consumed] = '\0';
908 * *pp_targetpath is now the unconsumed part of the path.
909 * dfs_path is now the consumed part of the path
910 * (in \server\share\path format).
913 /* Open the connection to the target server & share */
914 status = cli_cm_open(ctx, rootcli,
919 smb1cli_conn_encryption_on(rootcli->conn),
920 smbXcli_conn_protocol(rootcli->conn),
924 if (!NT_STATUS_IS_OK(status)) {
925 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
930 if (extrapath && strlen(extrapath) > 0) {
931 /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
932 /* put the trailing \ on the path, so to be save we put one in if needed */
933 if (extrapath[strlen(extrapath)-1] != '\\' && **pp_targetpath != '\\') {
934 *pp_targetpath = talloc_asprintf(ctx,
939 *pp_targetpath = talloc_asprintf(ctx,
944 if (!*pp_targetpath) {
945 return NT_STATUS_NO_MEMORY;
949 /* parse out the consumed mount path */
950 /* trim off the \server\share\ */
954 if (*ppath != '\\') {
955 d_printf("cli_resolve_path: "
956 "dfs_path (%s) not in correct format.\n",
958 return NT_STATUS_NOT_FOUND;
961 ppath++; /* Now pointing at start of server name. */
963 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
964 return NT_STATUS_NOT_FOUND;
967 ppath++; /* Now pointing at start of share name. */
969 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
970 return NT_STATUS_NOT_FOUND;
973 ppath++; /* Now pointing at path component. */
975 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
977 return NT_STATUS_NOT_FOUND;
980 cli_set_mntpoint(*targetcli, newmount);
982 /* Check for another dfs referral, note that we are not
983 checking for loops here. */
985 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
986 status = cli_resolve_path(ctx,
993 if (NT_STATUS_IS_OK(status)) {
995 * When cli_resolve_path returns true here it's always
996 * returning the complete path in newpath, so we're done
1000 *pp_targetpath = newpath;
1007 /* If returning true ensure we return a dfs root full path. */
1008 if ((*targetcli)->dfsroot) {
1009 dfs_path = talloc_strdup(ctx, *pp_targetpath);
1011 return NT_STATUS_NO_MEMORY;
1013 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
1014 if (*pp_targetpath == NULL) {
1015 return NT_STATUS_NO_MEMORY;
1019 return NT_STATUS_OK;
1022 /********************************************************************
1023 ********************************************************************/
1025 bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
1026 struct cli_state *cli,
1027 const char *sharename,
1028 char **pp_newserver,
1031 const char *username,
1032 const char *password,
1035 struct client_dfs_referral *refs = NULL;
1036 size_t num_refs = 0;
1037 size_t consumed = 0;
1038 char *fullpath = NULL;
1041 char *newextrapath = NULL;
1043 const char *remote_name;
1045 if (!cli || !sharename) {
1049 remote_name = smbXcli_conn_remote_name(cli->conn);
1050 cnum = cli_state_get_tid(cli);
1052 /* special case. never check for a referral on the IPC$ share */
1054 if (strequal(sharename, "IPC$")) {
1058 /* send a trans2_query_path_info to check for a referral */
1060 fullpath = talloc_asprintf(ctx, "\\%s\\%s", remote_name, sharename);
1065 /* check for the referral */
1067 if (!NT_STATUS_IS_OK(cli_tree_connect(cli, "IPC$", "IPC", NULL, 0))) {
1071 if (force_encrypt) {
1072 status = cli_cm_force_encryption(cli,
1077 if (!NT_STATUS_IS_OK(status)) {
1082 status = cli_dfs_get_referral(ctx, cli, fullpath, &refs,
1083 &num_refs, &consumed);
1084 res = NT_STATUS_IS_OK(status);
1086 status = cli_tdis(cli);
1087 if (!NT_STATUS_IS_OK(status)) {
1091 cli_state_set_tid(cli, cnum);
1093 if (!res || !num_refs) {
1097 if (!refs[0].dfspath) {
1101 if (!split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1102 pp_newshare, &newextrapath)) {
1106 /* check that this is not a self-referral */
1108 if (strequal(remote_name, *pp_newserver) &&
1109 strequal(sharename, *pp_newshare)) {