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/clirap.h"
27 /********************************************************************
30 DFS paths are *always* of the form \server\share\<pathname> (the \ characters
31 are not C escaped here).
33 - but if we're using POSIX paths then <pathname> may contain
34 '/' separators, not '\\' separators. So cope with '\\' or '/'
35 as a separator when looking at the pathname part.... JRA.
36 ********************************************************************/
38 /********************************************************************
39 Ensure a connection is encrypted.
40 ********************************************************************/
42 NTSTATUS cli_cm_force_encryption(struct cli_state *c,
46 const char *sharename)
48 NTSTATUS status = cli_force_encryption(c,
53 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED)) {
54 d_printf("Encryption required and "
55 "server that doesn't support "
56 "UNIX extensions - failing connect\n");
57 } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNKNOWN_REVISION)) {
58 d_printf("Encryption required and "
59 "can't get UNIX CIFS extensions "
60 "version from server.\n");
61 } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNSUPPORTED_COMPRESSION)) {
62 d_printf("Encryption required and "
63 "share %s doesn't support "
64 "encryption.\n", sharename);
65 } else if (!NT_STATUS_IS_OK(status)) {
66 d_printf("Encryption required and "
67 "setup failed with error %s.\n",
74 /********************************************************************
75 Return a connection to a server.
76 ********************************************************************/
78 static struct cli_state *do_connect(TALLOC_CTX *ctx,
81 const struct user_auth_info *auth_info,
88 struct cli_state *c = NULL;
89 struct nmb_name called, calling;
90 const char *called_str;
92 struct sockaddr_storage ss;
95 char *newserver, *newshare;
100 /* make a copy so we don't modify the global string 'service' */
101 servicename = talloc_strdup(ctx,share);
105 sharename = servicename;
106 if (*sharename == '\\') {
108 called_str = sharename;
109 if (server == NULL) {
112 sharename = strchr_m(sharename,'\\');
126 make_nmb_name(&calling, global_myname(), 0x0);
127 make_nmb_name(&called , called_str, name_type);
132 /* have to open a new connection */
133 c = cli_initialise_ex(get_cmdline_auth_info_signing_state(auth_info));
135 d_printf("Connection to %s failed\n", server_n);
139 cli_set_port(c, port);
142 status = cli_connect(c, server_n, &ss);
143 if (!NT_STATUS_IS_OK(status)) {
144 d_printf("Connection to %s failed (Error %s)\n",
151 if (max_protocol == 0) {
152 max_protocol = PROTOCOL_NT1;
154 c->protocol = max_protocol;
155 c->use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
156 c->fallback_after_kerberos =
157 get_cmdline_auth_info_fallback_after_kerberos(auth_info);
158 c->use_ccache = get_cmdline_auth_info_use_ccache(auth_info);
160 if (!cli_session_request(c, &calling, &called)) {
162 d_printf("session request to %s failed (%s)\n",
163 called.name, cli_errstr(c));
166 if ((p=strchr_m(called.name, '.'))) {
170 if (strcmp(called.name, "*SMBSERVER")) {
171 make_nmb_name(&called , "*SMBSERVER", 0x20);
177 DEBUG(4,(" session request ok\n"));
179 status = cli_negprot(c);
181 if (!NT_STATUS_IS_OK(status)) {
182 d_printf("protocol negotiation failed: %s\n",
188 username = get_cmdline_auth_info_username(auth_info);
189 password = get_cmdline_auth_info_password(auth_info);
191 if (!NT_STATUS_IS_OK(cli_session_setup(c, username,
192 password, strlen(password),
193 password, strlen(password),
195 /* If a password was not supplied then
196 * try again with a null username. */
197 if (password[0] || !username[0] ||
198 get_cmdline_auth_info_use_kerberos(auth_info) ||
199 !NT_STATUS_IS_OK(cli_session_setup(c, "",
203 d_printf("session setup failed: %s\n", cli_errstr(c));
204 if (NT_STATUS_V(cli_nt_error(c)) ==
205 NT_STATUS_V(NT_STATUS_MORE_PROCESSING_REQUIRED))
206 d_printf("did you forget to run kinit?\n");
210 d_printf("Anonymous login successful\n");
211 status = cli_init_creds(c, "", lp_workgroup(), "");
213 status = cli_init_creds(c, username, lp_workgroup(), password);
216 if (!NT_STATUS_IS_OK(status)) {
217 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status)));
222 if ( show_sessetup ) {
223 if (*c->server_domain) {
224 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
225 c->server_domain,c->server_os,c->server_type));
226 } else if (*c->server_os || *c->server_type) {
227 DEBUG(0,("OS=[%s] Server=[%s]\n",
228 c->server_os,c->server_type));
231 DEBUG(4,(" session setup ok\n"));
233 /* here's the fun part....to support 'msdfs proxy' shares
234 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
235 here before trying to connect to the original share.
236 cli_check_msdfs_proxy() will fail if it is a normal share. */
238 if ((c->capabilities & CAP_DFS) &&
239 cli_check_msdfs_proxy(ctx, c, sharename,
240 &newserver, &newshare,
246 return do_connect(ctx, newserver,
247 newshare, auth_info, false,
248 force_encrypt, max_protocol,
252 /* must be a normal share */
254 status = cli_tcon_andx(c, sharename, "?????",
255 password, strlen(password)+1);
256 if (!NT_STATUS_IS_OK(status)) {
257 d_printf("tree connect failed: %s\n", nt_errstr(status));
263 status = cli_cm_force_encryption(c,
268 if (!NT_STATUS_IS_OK(status)) {
274 DEBUG(4,(" tconx ok\n"));
278 /****************************************************************************
279 ****************************************************************************/
281 static void cli_set_mntpoint(struct cli_state *cli, const char *mnt)
283 char *name = clean_name(NULL, mnt);
287 TALLOC_FREE(cli->dfs_mountpoint);
288 cli->dfs_mountpoint = talloc_strdup(cli, name);
292 /********************************************************************
293 Add a new connection to the list.
294 referring_cli == NULL means a new initial connection.
295 ********************************************************************/
297 static struct cli_state *cli_cm_connect(TALLOC_CTX *ctx,
298 struct cli_state *referring_cli,
301 const struct user_auth_info *auth_info,
308 struct cli_state *cli;
310 cli = do_connect(ctx, server, share,
312 show_hdr, force_encrypt, max_protocol,
319 /* Enter into the list. */
321 DLIST_ADD_END(referring_cli, cli, struct cli_state *);
324 if (referring_cli && referring_cli->requested_posix_capabilities) {
326 uint32 caplow, caphigh;
328 status = cli_unix_extensions_version(cli, &major, &minor,
330 if (NT_STATUS_IS_OK(status)) {
331 cli_set_unix_extensions_capabilities(cli,
340 /********************************************************************
341 Return a connection to a server on a particular share.
342 ********************************************************************/
344 static struct cli_state *cli_cm_find(struct cli_state *cli,
354 /* Search to the start of the list. */
355 for (p = cli; p; p = DLIST_PREV(p)) {
356 if (strequal(server, p->desthost) &&
357 strequal(share,p->share)) {
362 /* Search to the end of the list. */
363 for (p = cli->next; p; p = p->next) {
364 if (strequal(server, p->desthost) &&
365 strequal(share,p->share)) {
373 /****************************************************************************
374 Open a client connection to a \\server\share.
375 ****************************************************************************/
377 struct cli_state *cli_cm_open(TALLOC_CTX *ctx,
378 struct cli_state *referring_cli,
381 const struct user_auth_info *auth_info,
388 /* Try to reuse an existing connection in this list. */
389 struct cli_state *c = cli_cm_find(referring_cli, server, share);
395 if (auth_info == NULL) {
396 /* Can't do a new connection
397 * without auth info. */
398 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
399 "without auth info\n",
404 return cli_cm_connect(ctx,
416 /****************************************************************************
417 ****************************************************************************/
419 void cli_cm_display(const struct cli_state *cli)
423 for (i=0; cli; cli = cli->next,i++ ) {
424 d_printf("%d:\tserver=%s, share=%s\n",
425 i, cli->desthost, cli->share );
429 /****************************************************************************
430 ****************************************************************************/
432 /****************************************************************************
433 ****************************************************************************/
436 void cli_cm_set_credentials(struct user_auth_info *auth_info)
438 SAFE_FREE(cm_creds.username);
439 cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
442 if (get_cmdline_auth_info_got_pass(auth_info)) {
443 cm_set_password(get_cmdline_auth_info_password(auth_info));
446 cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
447 cm_creds.fallback_after_kerberos = false;
448 cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
452 /**********************************************************************
453 split a dfs path into the server, share name, and extrapath components
454 **********************************************************************/
456 static bool split_dfs_path(TALLOC_CTX *ctx,
457 const char *nodepath,
467 *pp_extrapath = NULL;
469 path = talloc_strdup(ctx, nodepath);
474 if ( path[0] != '\\' ) {
478 p = strchr_m( path + 1, '\\' );
486 /* Look for any extra/deep path */
487 q = strchr_m(p, '\\');
491 *pp_extrapath = talloc_strdup(ctx, q);
493 *pp_extrapath = talloc_strdup(ctx, "");
495 if (*pp_extrapath == NULL) {
499 *pp_share = talloc_strdup(ctx, p);
500 if (*pp_share == NULL) {
504 *pp_server = talloc_strdup(ctx, &path[1]);
505 if (*pp_server == NULL) {
513 TALLOC_FREE(*pp_share);
514 TALLOC_FREE(*pp_extrapath);
519 /****************************************************************************
520 Return the original path truncated at the directory component before
521 the first wildcard character. Trust the caller to provide a NULL
523 ****************************************************************************/
525 static char *clean_path(TALLOC_CTX *ctx, const char *path)
531 /* No absolute paths. */
532 while (IS_DIRECTORY_SEP(*path)) {
536 path_out = talloc_strdup(ctx, path);
541 p1 = strchr_m(path_out, '*');
542 p2 = strchr_m(path_out, '?');
554 /* Now go back to the start of this component. */
555 p1 = strrchr_m(path_out, '/');
556 p2 = strrchr_m(path_out, '\\');
563 /* Strip any trailing separator */
565 len = strlen(path_out);
566 if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
567 path_out[len-1] = '\0';
573 /****************************************************************************
574 ****************************************************************************/
576 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
577 struct cli_state *cli,
580 char path_sep = '\\';
582 /* Ensure the extrapath doesn't start with a separator. */
583 while (IS_DIRECTORY_SEP(*dir)) {
587 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
590 return talloc_asprintf(ctx, "%c%s%c%s%c%s",
599 /********************************************************************
600 check for dfs referral
601 ********************************************************************/
603 static bool cli_dfs_check_error(struct cli_state *cli, NTSTATUS expected,
606 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
608 if (!(cli->capabilities & CAP_UNICODE)) {
611 if (!(cli->capabilities & CAP_STATUS32)) {
614 if (NT_STATUS_EQUAL(status, expected)) {
620 /********************************************************************
621 Get the dfs referral link.
622 ********************************************************************/
624 NTSTATUS cli_dfs_get_referral(TALLOC_CTX *ctx,
625 struct cli_state *cli,
627 struct client_dfs_referral **refs,
631 unsigned int data_len = 0;
632 unsigned int param_len = 0;
634 uint8_t *param = NULL;
635 uint8_t *rdata = NULL;
638 size_t pathlen = 2*(strlen(path)+1);
639 smb_ucs2_t *path_ucs;
640 char *consumed_path = NULL;
641 uint16_t consumed_ucs;
642 uint16 num_referrals;
643 struct client_dfs_referral *referrals = NULL;
649 SSVAL(setup, 0, TRANSACT2_GET_DFS_REFERRAL);
651 param = SMB_MALLOC_ARRAY(uint8_t, 2+pathlen+2);
653 status = NT_STATUS_NO_MEMORY;
656 SSVAL(param, 0, 0x03); /* max referral level */
657 p = (char *)(¶m[2]);
659 path_ucs = (smb_ucs2_t *)p;
660 p += clistr_push(cli, p, path, pathlen, STR_TERMINATE);
661 param_len = PTR_DIFF(p, param);
663 status = cli_trans(talloc_tos(), cli, SMBtrans2,
667 NULL, 0, cli->max_xmit,
669 NULL, 0, NULL, /* rsetup */
671 &rdata, 4, &data_len);
672 if (!NT_STATUS_IS_OK(status)) {
679 endp = (char *)rdata + data_len;
681 consumed_ucs = SVAL(rdata, 0);
682 num_referrals = SVAL(rdata, 2);
684 /* consumed_ucs is the number of bytes
685 * of the UCS2 path consumed not counting any
686 * terminating null. We need to convert
687 * back to unix charset and count again
688 * to get the number of bytes consumed from
689 * the incoming path. */
691 if (pull_string_talloc(talloc_tos(),
700 if (consumed_path == NULL) {
703 *consumed = strlen(consumed_path);
705 if (num_referrals != 0) {
711 referrals = talloc_array(ctx, struct client_dfs_referral,
717 /* start at the referrals array */
720 for (i=0; i<num_referrals && p < endp; i++) {
724 ref_version = SVAL(p, 0);
725 ref_size = SVAL(p, 2);
726 node_offset = SVAL(p, 16);
728 if (ref_version != 3) {
733 referrals[i].proximity = SVAL(p, 8);
734 referrals[i].ttl = SVAL(p, 10);
736 if (p + node_offset > endp) {
739 clistr_pull_talloc(ctx, cli->inbuf,
740 SVAL(cli->inbuf, smb_flg2),
741 &referrals[i].dfspath,
743 cli->bufsize - ((p+node_offset)-cli->inbuf),
744 STR_TERMINATE|STR_UNICODE);
746 if (!referrals[i].dfspath) {
751 if (i < num_referrals) {
756 *num_refs = num_referrals;
761 TALLOC_FREE(consumed_path);
767 /********************************************************************
768 ********************************************************************/
770 bool cli_resolve_path(TALLOC_CTX *ctx,
772 const struct user_auth_info *dfs_auth_info,
773 struct cli_state *rootcli,
775 struct cli_state **targetcli,
776 char **pp_targetpath)
778 struct client_dfs_referral *refs = NULL;
781 struct cli_state *cli_ipc = NULL;
782 char *dfs_path = NULL;
783 char *cleanpath = NULL;
784 char *extrapath = NULL;
788 struct cli_state *newcli = NULL;
789 char *newpath = NULL;
790 char *newmount = NULL;
792 SMB_STRUCT_STAT sbuf;
796 if ( !rootcli || !path || !targetcli ) {
800 /* Don't do anything if this is not a DFS root. */
802 if ( !rootcli->dfsroot) {
803 *targetcli = rootcli;
804 *pp_targetpath = talloc_strdup(ctx, path);
805 if (!*pp_targetpath) {
813 /* Send a trans2_query_path_info to check for a referral. */
815 cleanpath = clean_path(ctx, path);
820 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
825 status = cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes);
826 if (NT_STATUS_IS_OK(status)) {
827 /* This is an ordinary path, just return it. */
828 *targetcli = rootcli;
829 *pp_targetpath = talloc_strdup(ctx, path);
830 if (!*pp_targetpath) {
836 /* Special case where client asked for a path that does not exist */
838 if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND,
840 *targetcli = rootcli;
841 *pp_targetpath = talloc_strdup(ctx, path);
842 if (!*pp_targetpath) {
848 /* We got an error, check for DFS referral. */
850 if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED,
855 /* Check for the referral. */
857 if (!(cli_ipc = cli_cm_open(ctx,
863 (rootcli->trans_enc_state != NULL),
870 status = cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
871 &num_refs, &consumed);
872 if (!NT_STATUS_IS_OK(status) || !num_refs) {
876 /* Just store the first referral for now. */
878 if (!refs[0].dfspath) {
881 if (!split_dfs_path(ctx, refs[0].dfspath, &server, &share,
886 /* Make sure to recreate the original string including any wildcards. */
888 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
892 pathlen = strlen(dfs_path);
893 consumed = MIN(pathlen, consumed);
894 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed]);
895 if (!*pp_targetpath) {
898 dfs_path[consumed] = '\0';
901 * *pp_targetpath is now the unconsumed part of the path.
902 * dfs_path is now the consumed part of the path
903 * (in \server\share\path format).
906 /* Open the connection to the target server & share */
907 if ((*targetcli = cli_cm_open(ctx, rootcli,
912 (rootcli->trans_enc_state != NULL),
916 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
921 if (extrapath && strlen(extrapath) > 0) {
922 *pp_targetpath = talloc_asprintf(ctx,
926 if (!*pp_targetpath) {
931 /* parse out the consumed mount path */
932 /* trim off the \server\share\ */
936 if (*ppath != '\\') {
937 d_printf("cli_resolve_path: "
938 "dfs_path (%s) not in correct format.\n",
943 ppath++; /* Now pointing at start of server name. */
945 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
949 ppath++; /* Now pointing at start of share name. */
951 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
955 ppath++; /* Now pointing at path component. */
957 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
962 cli_set_mntpoint(*targetcli, newmount);
964 /* Check for another dfs referral, note that we are not
965 checking for loops here. */
967 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
968 if (cli_resolve_path(ctx,
976 * When cli_resolve_path returns true here it's always
977 * returning the complete path in newpath, so we're done
981 *pp_targetpath = newpath;
988 /* If returning true ensure we return a dfs root full path. */
989 if ((*targetcli)->dfsroot) {
990 dfs_path = talloc_strdup(ctx, *pp_targetpath);
994 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
1000 /********************************************************************
1001 ********************************************************************/
1003 bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
1004 struct cli_state *cli,
1005 const char *sharename,
1006 char **pp_newserver,
1009 const char *username,
1010 const char *password,
1013 struct client_dfs_referral *refs = NULL;
1014 size_t num_refs = 0;
1015 size_t consumed = 0;
1016 char *fullpath = NULL;
1019 char *newextrapath = NULL;
1022 if (!cli || !sharename) {
1028 /* special case. never check for a referral on the IPC$ share */
1030 if (strequal(sharename, "IPC$")) {
1034 /* send a trans2_query_path_info to check for a referral */
1036 fullpath = talloc_asprintf(ctx, "\\%s\\%s", cli->desthost, sharename );
1041 /* check for the referral */
1043 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, "IPC$", "IPC", NULL, 0))) {
1047 if (force_encrypt) {
1048 status = cli_cm_force_encryption(cli,
1053 if (!NT_STATUS_IS_OK(status)) {
1058 status = cli_dfs_get_referral(ctx, cli, fullpath, &refs,
1059 &num_refs, &consumed);
1060 res = NT_STATUS_IS_OK(status);
1062 status = cli_tdis(cli);
1063 if (!NT_STATUS_IS_OK(status)) {
1069 if (!res || !num_refs) {
1073 if (!refs[0].dfspath) {
1077 if (!split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1078 pp_newshare, &newextrapath)) {
1082 /* check that this is not a self-referral */
1084 if (strequal(cli->desthost, *pp_newserver) &&
1085 strequal(sharename, *pp_newshare)) {