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"
29 /********************************************************************
32 DFS paths are *always* of the form \server\share\<pathname> (the \ characters
33 are not C escaped here).
35 - but if we're using POSIX paths then <pathname> may contain
36 '/' separators, not '\\' separators. So cope with '\\' or '/'
37 as a separator when looking at the pathname part.... JRA.
38 ********************************************************************/
40 /********************************************************************
41 Ensure a connection is encrypted.
42 ********************************************************************/
44 NTSTATUS cli_cm_force_encryption(struct cli_state *c,
48 const char *sharename)
50 NTSTATUS status = cli_force_encryption(c,
55 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED)) {
56 d_printf("Encryption required and "
57 "server that doesn't support "
58 "UNIX extensions - failing connect\n");
59 } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNKNOWN_REVISION)) {
60 d_printf("Encryption required and "
61 "can't get UNIX CIFS extensions "
62 "version from server.\n");
63 } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNSUPPORTED_COMPRESSION)) {
64 d_printf("Encryption required and "
65 "share %s doesn't support "
66 "encryption.\n", sharename);
67 } else if (!NT_STATUS_IS_OK(status)) {
68 d_printf("Encryption required and "
69 "setup failed with error %s.\n",
76 /********************************************************************
77 Return a connection to a server.
78 ********************************************************************/
80 static NTSTATUS do_connect(TALLOC_CTX *ctx,
83 const struct user_auth_info *auth_info,
89 struct cli_state **pcli)
91 struct cli_state *c = NULL;
94 char *newserver, *newshare;
99 /* make a copy so we don't modify the global string 'service' */
100 servicename = talloc_strdup(ctx,share);
102 return NT_STATUS_NO_MEMORY;
104 sharename = servicename;
105 if (*sharename == '\\') {
107 if (server == NULL) {
110 sharename = strchr_m(sharename,'\\');
112 return NT_STATUS_NO_MEMORY;
117 if (server == NULL) {
118 return NT_STATUS_INVALID_PARAMETER;
121 status = cli_connect_nb(
122 server, NULL, port, name_type, NULL,
123 get_cmdline_auth_info_signing_state(auth_info), &c);
125 if (!NT_STATUS_IS_OK(status)) {
126 d_printf("Connection to %s failed (Error %s)\n",
132 if (max_protocol == 0) {
133 max_protocol = PROTOCOL_NT1;
135 c->protocol = max_protocol;
136 c->use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
137 c->fallback_after_kerberos =
138 get_cmdline_auth_info_fallback_after_kerberos(auth_info);
139 c->use_ccache = get_cmdline_auth_info_use_ccache(auth_info);
141 DEBUG(4,(" session request ok\n"));
143 status = cli_negprot(c);
145 if (!NT_STATUS_IS_OK(status)) {
146 d_printf("protocol negotiation failed: %s\n",
152 username = get_cmdline_auth_info_username(auth_info);
153 password = get_cmdline_auth_info_password(auth_info);
155 status = cli_session_setup(c, username,
156 password, strlen(password),
157 password, strlen(password),
159 if (!NT_STATUS_IS_OK(status)) {
160 /* If a password was not supplied then
161 * try again with a null username. */
162 if (password[0] || !username[0] ||
163 get_cmdline_auth_info_use_kerberos(auth_info) ||
164 !NT_STATUS_IS_OK(status = cli_session_setup(c, "",
168 d_printf("session setup failed: %s\n",
170 if (NT_STATUS_V(cli_nt_error(c)) ==
171 NT_STATUS_V(NT_STATUS_MORE_PROCESSING_REQUIRED))
172 d_printf("did you forget to run kinit?\n");
176 d_printf("Anonymous login successful\n");
177 status = cli_init_creds(c, "", lp_workgroup(), "");
179 status = cli_init_creds(c, username, lp_workgroup(), password);
182 if (!NT_STATUS_IS_OK(status)) {
183 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status)));
188 if ( show_sessetup ) {
189 if (*c->server_domain) {
190 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
191 c->server_domain,c->server_os,c->server_type));
192 } else if (*c->server_os || *c->server_type) {
193 DEBUG(0,("OS=[%s] Server=[%s]\n",
194 c->server_os,c->server_type));
197 DEBUG(4,(" session setup ok\n"));
199 /* here's the fun part....to support 'msdfs proxy' shares
200 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
201 here before trying to connect to the original share.
202 cli_check_msdfs_proxy() will fail if it is a normal share. */
204 if ((c->capabilities & CAP_DFS) &&
205 cli_check_msdfs_proxy(ctx, c, sharename,
206 &newserver, &newshare,
212 return do_connect(ctx, newserver,
213 newshare, auth_info, false,
214 force_encrypt, max_protocol,
215 port, name_type, pcli);
218 /* must be a normal share */
220 status = cli_tcon_andx(c, sharename, "?????",
221 password, strlen(password)+1);
222 if (!NT_STATUS_IS_OK(status)) {
223 d_printf("tree connect failed: %s\n", nt_errstr(status));
229 status = cli_cm_force_encryption(c,
234 if (!NT_STATUS_IS_OK(status)) {
240 DEBUG(4,(" tconx ok\n"));
245 /****************************************************************************
246 ****************************************************************************/
248 static void cli_set_mntpoint(struct cli_state *cli, const char *mnt)
250 char *name = clean_name(NULL, mnt);
254 TALLOC_FREE(cli->dfs_mountpoint);
255 cli->dfs_mountpoint = talloc_strdup(cli, name);
259 /********************************************************************
260 Add a new connection to the list.
261 referring_cli == NULL means a new initial connection.
262 ********************************************************************/
264 static NTSTATUS cli_cm_connect(TALLOC_CTX *ctx,
265 struct cli_state *referring_cli,
268 const struct user_auth_info *auth_info,
274 struct cli_state **pcli)
276 struct cli_state *cli;
279 status = do_connect(ctx, server, share,
281 show_hdr, force_encrypt, max_protocol,
282 port, name_type, &cli);
284 if (!NT_STATUS_IS_OK(status)) {
288 /* Enter into the list. */
290 DLIST_ADD_END(referring_cli, cli, struct cli_state *);
293 if (referring_cli && referring_cli->requested_posix_capabilities) {
295 uint32 caplow, caphigh;
296 status = cli_unix_extensions_version(cli, &major, &minor,
298 if (NT_STATUS_IS_OK(status)) {
299 cli_set_unix_extensions_capabilities(cli,
309 /********************************************************************
310 Return a connection to a server on a particular share.
311 ********************************************************************/
313 static struct cli_state *cli_cm_find(struct cli_state *cli,
323 /* Search to the start of the list. */
324 for (p = cli; p; p = DLIST_PREV(p)) {
325 if (strequal(server, p->desthost) &&
326 strequal(share,p->share)) {
331 /* Search to the end of the list. */
332 for (p = cli->next; p; p = p->next) {
333 if (strequal(server, p->desthost) &&
334 strequal(share,p->share)) {
342 /****************************************************************************
343 Open a client connection to a \\server\share.
344 ****************************************************************************/
346 NTSTATUS cli_cm_open(TALLOC_CTX *ctx,
347 struct cli_state *referring_cli,
350 const struct user_auth_info *auth_info,
356 struct cli_state **pcli)
358 /* Try to reuse an existing connection in this list. */
359 struct cli_state *c = cli_cm_find(referring_cli, server, share);
367 if (auth_info == NULL) {
368 /* Can't do a new connection
369 * without auth info. */
370 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
371 "without auth info\n",
373 return NT_STATUS_INVALID_PARAMETER;
376 status = cli_cm_connect(ctx,
387 if (!NT_STATUS_IS_OK(status)) {
394 /****************************************************************************
395 ****************************************************************************/
397 void cli_cm_display(const struct cli_state *cli)
401 for (i=0; cli; cli = cli->next,i++ ) {
402 d_printf("%d:\tserver=%s, share=%s\n",
403 i, cli->desthost, cli->share );
407 /****************************************************************************
408 ****************************************************************************/
410 /****************************************************************************
411 ****************************************************************************/
414 void cli_cm_set_credentials(struct user_auth_info *auth_info)
416 SAFE_FREE(cm_creds.username);
417 cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
420 if (get_cmdline_auth_info_got_pass(auth_info)) {
421 cm_set_password(get_cmdline_auth_info_password(auth_info));
424 cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
425 cm_creds.fallback_after_kerberos = false;
426 cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
430 /**********************************************************************
431 split a dfs path into the server, share name, and extrapath components
432 **********************************************************************/
434 static bool split_dfs_path(TALLOC_CTX *ctx,
435 const char *nodepath,
445 *pp_extrapath = NULL;
447 path = talloc_strdup(ctx, nodepath);
452 if ( path[0] != '\\' ) {
456 p = strchr_m( path + 1, '\\' );
464 /* Look for any extra/deep path */
465 q = strchr_m(p, '\\');
469 *pp_extrapath = talloc_strdup(ctx, q);
471 *pp_extrapath = talloc_strdup(ctx, "");
473 if (*pp_extrapath == NULL) {
477 *pp_share = talloc_strdup(ctx, p);
478 if (*pp_share == NULL) {
482 *pp_server = talloc_strdup(ctx, &path[1]);
483 if (*pp_server == NULL) {
491 TALLOC_FREE(*pp_share);
492 TALLOC_FREE(*pp_extrapath);
497 /****************************************************************************
498 Return the original path truncated at the directory component before
499 the first wildcard character. Trust the caller to provide a NULL
501 ****************************************************************************/
503 static char *clean_path(TALLOC_CTX *ctx, const char *path)
509 /* No absolute paths. */
510 while (IS_DIRECTORY_SEP(*path)) {
514 path_out = talloc_strdup(ctx, path);
519 p1 = strchr_m(path_out, '*');
520 p2 = strchr_m(path_out, '?');
532 /* Now go back to the start of this component. */
533 p1 = strrchr_m(path_out, '/');
534 p2 = strrchr_m(path_out, '\\');
541 /* Strip any trailing separator */
543 len = strlen(path_out);
544 if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
545 path_out[len-1] = '\0';
551 /****************************************************************************
552 ****************************************************************************/
554 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
555 struct cli_state *cli,
558 char path_sep = '\\';
560 /* Ensure the extrapath doesn't start with a separator. */
561 while (IS_DIRECTORY_SEP(*dir)) {
565 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
568 return talloc_asprintf(ctx, "%c%s%c%s%c%s",
577 /********************************************************************
578 check for dfs referral
579 ********************************************************************/
581 static bool cli_dfs_check_error(struct cli_state *cli, NTSTATUS expected,
584 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
586 if (!(cli->capabilities & CAP_UNICODE)) {
589 if (!(cli->capabilities & CAP_STATUS32)) {
592 if (NT_STATUS_EQUAL(status, expected)) {
598 /********************************************************************
599 Get the dfs referral link.
600 ********************************************************************/
602 NTSTATUS cli_dfs_get_referral(TALLOC_CTX *ctx,
603 struct cli_state *cli,
605 struct client_dfs_referral **refs,
609 unsigned int data_len = 0;
610 unsigned int param_len = 0;
612 uint8_t *param = NULL;
613 uint8_t *rdata = NULL;
616 size_t pathlen = 2*(strlen(path)+1);
617 smb_ucs2_t *path_ucs;
618 char *consumed_path = NULL;
619 uint16_t consumed_ucs;
620 uint16 num_referrals;
621 struct client_dfs_referral *referrals = NULL;
627 SSVAL(setup, 0, TRANSACT2_GET_DFS_REFERRAL);
629 param = SMB_MALLOC_ARRAY(uint8_t, 2+pathlen+2);
631 status = NT_STATUS_NO_MEMORY;
634 SSVAL(param, 0, 0x03); /* max referral level */
635 p = (char *)(¶m[2]);
637 path_ucs = (smb_ucs2_t *)p;
638 p += clistr_push(cli, p, path, pathlen, STR_TERMINATE);
639 param_len = PTR_DIFF(p, param);
641 status = cli_trans(talloc_tos(), cli, SMBtrans2,
645 NULL, 0, cli->max_xmit,
647 NULL, 0, NULL, /* rsetup */
649 &rdata, 4, &data_len);
650 if (!NT_STATUS_IS_OK(status)) {
657 endp = (char *)rdata + data_len;
659 consumed_ucs = SVAL(rdata, 0);
660 num_referrals = SVAL(rdata, 2);
662 /* consumed_ucs is the number of bytes
663 * of the UCS2 path consumed not counting any
664 * terminating null. We need to convert
665 * back to unix charset and count again
666 * to get the number of bytes consumed from
667 * the incoming path. */
669 if (pull_string_talloc(talloc_tos(),
678 if (consumed_path == NULL) {
681 *consumed = strlen(consumed_path);
683 if (num_referrals != 0) {
689 referrals = talloc_array(ctx, struct client_dfs_referral,
695 /* start at the referrals array */
698 for (i=0; i<num_referrals && p < endp; i++) {
702 ref_version = SVAL(p, 0);
703 ref_size = SVAL(p, 2);
704 node_offset = SVAL(p, 16);
706 if (ref_version != 3) {
711 referrals[i].proximity = SVAL(p, 8);
712 referrals[i].ttl = SVAL(p, 10);
714 if (p + node_offset > endp) {
717 clistr_pull_talloc(ctx, cli->inbuf,
718 SVAL(cli->inbuf, smb_flg2),
719 &referrals[i].dfspath,
721 cli->bufsize - ((p+node_offset)-cli->inbuf),
722 STR_TERMINATE|STR_UNICODE);
724 if (!referrals[i].dfspath) {
729 if (i < num_referrals) {
734 *num_refs = num_referrals;
739 TALLOC_FREE(consumed_path);
745 /********************************************************************
746 ********************************************************************/
748 NTSTATUS cli_resolve_path(TALLOC_CTX *ctx,
750 const struct user_auth_info *dfs_auth_info,
751 struct cli_state *rootcli,
753 struct cli_state **targetcli,
754 char **pp_targetpath)
756 struct client_dfs_referral *refs = NULL;
759 struct cli_state *cli_ipc = NULL;
760 char *dfs_path = NULL;
761 char *cleanpath = NULL;
762 char *extrapath = NULL;
766 struct cli_state *newcli = NULL;
767 char *newpath = NULL;
768 char *newmount = NULL;
770 SMB_STRUCT_STAT sbuf;
774 if ( !rootcli || !path || !targetcli ) {
775 return NT_STATUS_INVALID_PARAMETER;
778 /* Don't do anything if this is not a DFS root. */
780 if ( !rootcli->dfsroot) {
781 *targetcli = rootcli;
782 *pp_targetpath = talloc_strdup(ctx, path);
783 if (!*pp_targetpath) {
784 return NT_STATUS_NO_MEMORY;
791 /* Send a trans2_query_path_info to check for a referral. */
793 cleanpath = clean_path(ctx, path);
795 return NT_STATUS_NO_MEMORY;
798 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
800 return NT_STATUS_NO_MEMORY;
803 status = cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes);
804 if (NT_STATUS_IS_OK(status)) {
805 /* This is an ordinary path, just return it. */
806 *targetcli = rootcli;
807 *pp_targetpath = talloc_strdup(ctx, path);
808 if (!*pp_targetpath) {
809 return NT_STATUS_NO_MEMORY;
814 /* Special case where client asked for a path that does not exist */
816 if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND,
818 *targetcli = rootcli;
819 *pp_targetpath = talloc_strdup(ctx, path);
820 if (!*pp_targetpath) {
821 return NT_STATUS_NO_MEMORY;
826 /* We got an error, check for DFS referral. */
828 if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED,
833 /* Check for the referral. */
835 status = cli_cm_open(ctx,
841 (rootcli->trans_enc_state != NULL),
846 if (!NT_STATUS_IS_OK(status)) {
850 status = cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
851 &num_refs, &consumed);
852 if (!NT_STATUS_IS_OK(status) || !num_refs) {
856 /* Just store the first referral for now. */
858 if (!refs[0].dfspath) {
859 return NT_STATUS_NOT_FOUND;
861 if (!split_dfs_path(ctx, refs[0].dfspath, &server, &share,
863 return NT_STATUS_NOT_FOUND;
866 /* Make sure to recreate the original string including any wildcards. */
868 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
870 return NT_STATUS_NO_MEMORY;
872 pathlen = strlen(dfs_path);
873 consumed = MIN(pathlen, consumed);
874 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed]);
875 if (!*pp_targetpath) {
876 return NT_STATUS_NO_MEMORY;
878 dfs_path[consumed] = '\0';
881 * *pp_targetpath is now the unconsumed part of the path.
882 * dfs_path is now the consumed part of the path
883 * (in \server\share\path format).
886 /* Open the connection to the target server & share */
887 status = cli_cm_open(ctx, rootcli,
892 (rootcli->trans_enc_state != NULL),
897 if (!NT_STATUS_IS_OK(status)) {
898 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
903 if (extrapath && strlen(extrapath) > 0) {
904 /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
905 /* put the trailing \ on the path, so to be save we put one in if needed */
906 if (extrapath[strlen(extrapath)-1] != '\\' && **pp_targetpath != '\\') {
907 *pp_targetpath = talloc_asprintf(ctx,
912 *pp_targetpath = talloc_asprintf(ctx,
917 if (!*pp_targetpath) {
918 return NT_STATUS_NO_MEMORY;
922 /* parse out the consumed mount path */
923 /* trim off the \server\share\ */
927 if (*ppath != '\\') {
928 d_printf("cli_resolve_path: "
929 "dfs_path (%s) not in correct format.\n",
931 return NT_STATUS_NOT_FOUND;
934 ppath++; /* Now pointing at start of server name. */
936 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
937 return NT_STATUS_NOT_FOUND;
940 ppath++; /* Now pointing at start of share name. */
942 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
943 return NT_STATUS_NOT_FOUND;
946 ppath++; /* Now pointing at path component. */
948 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
950 return NT_STATUS_NOT_FOUND;
953 cli_set_mntpoint(*targetcli, newmount);
955 /* Check for another dfs referral, note that we are not
956 checking for loops here. */
958 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
959 status = cli_resolve_path(ctx,
966 if (NT_STATUS_IS_OK(status)) {
968 * When cli_resolve_path returns true here it's always
969 * returning the complete path in newpath, so we're done
973 *pp_targetpath = newpath;
980 /* If returning true ensure we return a dfs root full path. */
981 if ((*targetcli)->dfsroot) {
982 dfs_path = talloc_strdup(ctx, *pp_targetpath);
984 return NT_STATUS_NO_MEMORY;
986 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
987 if (*pp_targetpath == NULL) {
988 return NT_STATUS_NO_MEMORY;
995 /********************************************************************
996 ********************************************************************/
998 bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
999 struct cli_state *cli,
1000 const char *sharename,
1001 char **pp_newserver,
1004 const char *username,
1005 const char *password,
1008 struct client_dfs_referral *refs = NULL;
1009 size_t num_refs = 0;
1010 size_t consumed = 0;
1011 char *fullpath = NULL;
1014 char *newextrapath = NULL;
1017 if (!cli || !sharename) {
1023 /* special case. never check for a referral on the IPC$ share */
1025 if (strequal(sharename, "IPC$")) {
1029 /* send a trans2_query_path_info to check for a referral */
1031 fullpath = talloc_asprintf(ctx, "\\%s\\%s", cli->desthost, sharename );
1036 /* check for the referral */
1038 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, "IPC$", "IPC", NULL, 0))) {
1042 if (force_encrypt) {
1043 status = cli_cm_force_encryption(cli,
1048 if (!NT_STATUS_IS_OK(status)) {
1053 status = cli_dfs_get_referral(ctx, cli, fullpath, &refs,
1054 &num_refs, &consumed);
1055 res = NT_STATUS_IS_OK(status);
1057 status = cli_tdis(cli);
1058 if (!NT_STATUS_IS_OK(status)) {
1064 if (!res || !num_refs) {
1068 if (!refs[0].dfspath) {
1072 if (!split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1073 pp_newshare, &newextrapath)) {
1077 /* check that this is not a self-referral */
1079 if (strequal(cli->desthost, *pp_newserver) &&
1080 strequal(sharename, *pp_newshare)) {