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 struct cli_state *do_connect(TALLOC_CTX *ctx,
83 const struct user_auth_info *auth_info,
90 struct cli_state *c = NULL;
93 char *newserver, *newshare;
98 /* make a copy so we don't modify the global string 'service' */
99 servicename = talloc_strdup(ctx,share);
103 sharename = servicename;
104 if (*sharename == '\\') {
106 if (server == NULL) {
109 sharename = strchr_m(sharename,'\\');
116 if (server == NULL) {
120 status = cli_connect_nb(
121 server, NULL, port, name_type, NULL,
122 get_cmdline_auth_info_signing_state(auth_info), &c);
124 if (!NT_STATUS_IS_OK(status)) {
125 d_printf("Connection to %s failed (Error %s)\n",
131 if (max_protocol == 0) {
132 max_protocol = PROTOCOL_NT1;
134 c->protocol = max_protocol;
135 c->use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
136 c->fallback_after_kerberos =
137 get_cmdline_auth_info_fallback_after_kerberos(auth_info);
138 c->use_ccache = get_cmdline_auth_info_use_ccache(auth_info);
140 DEBUG(4,(" session request ok\n"));
142 status = cli_negprot(c);
144 if (!NT_STATUS_IS_OK(status)) {
145 d_printf("protocol negotiation failed: %s\n",
151 username = get_cmdline_auth_info_username(auth_info);
152 password = get_cmdline_auth_info_password(auth_info);
154 if (!NT_STATUS_IS_OK(cli_session_setup(c, username,
155 password, strlen(password),
156 password, strlen(password),
158 /* If a password was not supplied then
159 * try again with a null username. */
160 if (password[0] || !username[0] ||
161 get_cmdline_auth_info_use_kerberos(auth_info) ||
162 !NT_STATUS_IS_OK(cli_session_setup(c, "",
166 d_printf("session setup failed: %s\n", cli_errstr(c));
167 if (NT_STATUS_V(cli_nt_error(c)) ==
168 NT_STATUS_V(NT_STATUS_MORE_PROCESSING_REQUIRED))
169 d_printf("did you forget to run kinit?\n");
173 d_printf("Anonymous login successful\n");
174 status = cli_init_creds(c, "", lp_workgroup(), "");
176 status = cli_init_creds(c, username, lp_workgroup(), password);
179 if (!NT_STATUS_IS_OK(status)) {
180 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status)));
185 if ( show_sessetup ) {
186 if (*c->server_domain) {
187 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
188 c->server_domain,c->server_os,c->server_type));
189 } else if (*c->server_os || *c->server_type) {
190 DEBUG(0,("OS=[%s] Server=[%s]\n",
191 c->server_os,c->server_type));
194 DEBUG(4,(" session setup ok\n"));
196 /* here's the fun part....to support 'msdfs proxy' shares
197 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
198 here before trying to connect to the original share.
199 cli_check_msdfs_proxy() will fail if it is a normal share. */
201 if ((c->capabilities & CAP_DFS) &&
202 cli_check_msdfs_proxy(ctx, c, sharename,
203 &newserver, &newshare,
209 return do_connect(ctx, newserver,
210 newshare, auth_info, false,
211 force_encrypt, max_protocol,
215 /* must be a normal share */
217 status = cli_tcon_andx(c, sharename, "?????",
218 password, strlen(password)+1);
219 if (!NT_STATUS_IS_OK(status)) {
220 d_printf("tree connect failed: %s\n", nt_errstr(status));
226 status = cli_cm_force_encryption(c,
231 if (!NT_STATUS_IS_OK(status)) {
237 DEBUG(4,(" tconx ok\n"));
241 /****************************************************************************
242 ****************************************************************************/
244 static void cli_set_mntpoint(struct cli_state *cli, const char *mnt)
246 char *name = clean_name(NULL, mnt);
250 TALLOC_FREE(cli->dfs_mountpoint);
251 cli->dfs_mountpoint = talloc_strdup(cli, name);
255 /********************************************************************
256 Add a new connection to the list.
257 referring_cli == NULL means a new initial connection.
258 ********************************************************************/
260 static struct cli_state *cli_cm_connect(TALLOC_CTX *ctx,
261 struct cli_state *referring_cli,
264 const struct user_auth_info *auth_info,
271 struct cli_state *cli;
273 cli = do_connect(ctx, server, share,
275 show_hdr, force_encrypt, max_protocol,
282 /* Enter into the list. */
284 DLIST_ADD_END(referring_cli, cli, struct cli_state *);
287 if (referring_cli && referring_cli->requested_posix_capabilities) {
289 uint32 caplow, caphigh;
291 status = cli_unix_extensions_version(cli, &major, &minor,
293 if (NT_STATUS_IS_OK(status)) {
294 cli_set_unix_extensions_capabilities(cli,
303 /********************************************************************
304 Return a connection to a server on a particular share.
305 ********************************************************************/
307 static struct cli_state *cli_cm_find(struct cli_state *cli,
317 /* Search to the start of the list. */
318 for (p = cli; p; p = DLIST_PREV(p)) {
319 if (strequal(server, p->desthost) &&
320 strequal(share,p->share)) {
325 /* Search to the end of the list. */
326 for (p = cli->next; p; p = p->next) {
327 if (strequal(server, p->desthost) &&
328 strequal(share,p->share)) {
336 /****************************************************************************
337 Open a client connection to a \\server\share.
338 ****************************************************************************/
340 struct cli_state *cli_cm_open(TALLOC_CTX *ctx,
341 struct cli_state *referring_cli,
344 const struct user_auth_info *auth_info,
351 /* Try to reuse an existing connection in this list. */
352 struct cli_state *c = cli_cm_find(referring_cli, server, share);
358 if (auth_info == NULL) {
359 /* Can't do a new connection
360 * without auth info. */
361 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
362 "without auth info\n",
367 return cli_cm_connect(ctx,
379 /****************************************************************************
380 ****************************************************************************/
382 void cli_cm_display(const struct cli_state *cli)
386 for (i=0; cli; cli = cli->next,i++ ) {
387 d_printf("%d:\tserver=%s, share=%s\n",
388 i, cli->desthost, cli->share );
392 /****************************************************************************
393 ****************************************************************************/
395 /****************************************************************************
396 ****************************************************************************/
399 void cli_cm_set_credentials(struct user_auth_info *auth_info)
401 SAFE_FREE(cm_creds.username);
402 cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
405 if (get_cmdline_auth_info_got_pass(auth_info)) {
406 cm_set_password(get_cmdline_auth_info_password(auth_info));
409 cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
410 cm_creds.fallback_after_kerberos = false;
411 cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
415 /**********************************************************************
416 split a dfs path into the server, share name, and extrapath components
417 **********************************************************************/
419 static bool split_dfs_path(TALLOC_CTX *ctx,
420 const char *nodepath,
430 *pp_extrapath = NULL;
432 path = talloc_strdup(ctx, nodepath);
437 if ( path[0] != '\\' ) {
441 p = strchr_m( path + 1, '\\' );
449 /* Look for any extra/deep path */
450 q = strchr_m(p, '\\');
454 *pp_extrapath = talloc_strdup(ctx, q);
456 *pp_extrapath = talloc_strdup(ctx, "");
458 if (*pp_extrapath == NULL) {
462 *pp_share = talloc_strdup(ctx, p);
463 if (*pp_share == NULL) {
467 *pp_server = talloc_strdup(ctx, &path[1]);
468 if (*pp_server == NULL) {
476 TALLOC_FREE(*pp_share);
477 TALLOC_FREE(*pp_extrapath);
482 /****************************************************************************
483 Return the original path truncated at the directory component before
484 the first wildcard character. Trust the caller to provide a NULL
486 ****************************************************************************/
488 static char *clean_path(TALLOC_CTX *ctx, const char *path)
494 /* No absolute paths. */
495 while (IS_DIRECTORY_SEP(*path)) {
499 path_out = talloc_strdup(ctx, path);
504 p1 = strchr_m(path_out, '*');
505 p2 = strchr_m(path_out, '?');
517 /* Now go back to the start of this component. */
518 p1 = strrchr_m(path_out, '/');
519 p2 = strrchr_m(path_out, '\\');
526 /* Strip any trailing separator */
528 len = strlen(path_out);
529 if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
530 path_out[len-1] = '\0';
536 /****************************************************************************
537 ****************************************************************************/
539 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
540 struct cli_state *cli,
543 char path_sep = '\\';
545 /* Ensure the extrapath doesn't start with a separator. */
546 while (IS_DIRECTORY_SEP(*dir)) {
550 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
553 return talloc_asprintf(ctx, "%c%s%c%s%c%s",
562 /********************************************************************
563 check for dfs referral
564 ********************************************************************/
566 static bool cli_dfs_check_error(struct cli_state *cli, NTSTATUS expected,
569 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
571 if (!(cli->capabilities & CAP_UNICODE)) {
574 if (!(cli->capabilities & CAP_STATUS32)) {
577 if (NT_STATUS_EQUAL(status, expected)) {
583 /********************************************************************
584 Get the dfs referral link.
585 ********************************************************************/
587 NTSTATUS cli_dfs_get_referral(TALLOC_CTX *ctx,
588 struct cli_state *cli,
590 struct client_dfs_referral **refs,
594 unsigned int data_len = 0;
595 unsigned int param_len = 0;
597 uint8_t *param = NULL;
598 uint8_t *rdata = NULL;
601 size_t pathlen = 2*(strlen(path)+1);
602 smb_ucs2_t *path_ucs;
603 char *consumed_path = NULL;
604 uint16_t consumed_ucs;
605 uint16 num_referrals;
606 struct client_dfs_referral *referrals = NULL;
612 SSVAL(setup, 0, TRANSACT2_GET_DFS_REFERRAL);
614 param = SMB_MALLOC_ARRAY(uint8_t, 2+pathlen+2);
616 status = NT_STATUS_NO_MEMORY;
619 SSVAL(param, 0, 0x03); /* max referral level */
620 p = (char *)(¶m[2]);
622 path_ucs = (smb_ucs2_t *)p;
623 p += clistr_push(cli, p, path, pathlen, STR_TERMINATE);
624 param_len = PTR_DIFF(p, param);
626 status = cli_trans(talloc_tos(), cli, SMBtrans2,
630 NULL, 0, cli->max_xmit,
632 NULL, 0, NULL, /* rsetup */
634 &rdata, 4, &data_len);
635 if (!NT_STATUS_IS_OK(status)) {
642 endp = (char *)rdata + data_len;
644 consumed_ucs = SVAL(rdata, 0);
645 num_referrals = SVAL(rdata, 2);
647 /* consumed_ucs is the number of bytes
648 * of the UCS2 path consumed not counting any
649 * terminating null. We need to convert
650 * back to unix charset and count again
651 * to get the number of bytes consumed from
652 * the incoming path. */
654 if (pull_string_talloc(talloc_tos(),
663 if (consumed_path == NULL) {
666 *consumed = strlen(consumed_path);
668 if (num_referrals != 0) {
674 referrals = talloc_array(ctx, struct client_dfs_referral,
680 /* start at the referrals array */
683 for (i=0; i<num_referrals && p < endp; i++) {
687 ref_version = SVAL(p, 0);
688 ref_size = SVAL(p, 2);
689 node_offset = SVAL(p, 16);
691 if (ref_version != 3) {
696 referrals[i].proximity = SVAL(p, 8);
697 referrals[i].ttl = SVAL(p, 10);
699 if (p + node_offset > endp) {
702 clistr_pull_talloc(ctx, cli->inbuf,
703 SVAL(cli->inbuf, smb_flg2),
704 &referrals[i].dfspath,
706 cli->bufsize - ((p+node_offset)-cli->inbuf),
707 STR_TERMINATE|STR_UNICODE);
709 if (!referrals[i].dfspath) {
714 if (i < num_referrals) {
719 *num_refs = num_referrals;
724 TALLOC_FREE(consumed_path);
730 /********************************************************************
731 ********************************************************************/
733 bool cli_resolve_path(TALLOC_CTX *ctx,
735 const struct user_auth_info *dfs_auth_info,
736 struct cli_state *rootcli,
738 struct cli_state **targetcli,
739 char **pp_targetpath)
741 struct client_dfs_referral *refs = NULL;
744 struct cli_state *cli_ipc = NULL;
745 char *dfs_path = NULL;
746 char *cleanpath = NULL;
747 char *extrapath = NULL;
751 struct cli_state *newcli = NULL;
752 char *newpath = NULL;
753 char *newmount = NULL;
755 SMB_STRUCT_STAT sbuf;
759 if ( !rootcli || !path || !targetcli ) {
763 /* Don't do anything if this is not a DFS root. */
765 if ( !rootcli->dfsroot) {
766 *targetcli = rootcli;
767 *pp_targetpath = talloc_strdup(ctx, path);
768 if (!*pp_targetpath) {
776 /* Send a trans2_query_path_info to check for a referral. */
778 cleanpath = clean_path(ctx, path);
783 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
788 status = cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes);
789 if (NT_STATUS_IS_OK(status)) {
790 /* This is an ordinary path, just return it. */
791 *targetcli = rootcli;
792 *pp_targetpath = talloc_strdup(ctx, path);
793 if (!*pp_targetpath) {
799 /* Special case where client asked for a path that does not exist */
801 if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND,
803 *targetcli = rootcli;
804 *pp_targetpath = talloc_strdup(ctx, path);
805 if (!*pp_targetpath) {
811 /* We got an error, check for DFS referral. */
813 if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED,
818 /* Check for the referral. */
820 if (!(cli_ipc = cli_cm_open(ctx,
826 (rootcli->trans_enc_state != NULL),
833 status = cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
834 &num_refs, &consumed);
835 if (!NT_STATUS_IS_OK(status) || !num_refs) {
839 /* Just store the first referral for now. */
841 if (!refs[0].dfspath) {
844 if (!split_dfs_path(ctx, refs[0].dfspath, &server, &share,
849 /* Make sure to recreate the original string including any wildcards. */
851 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
855 pathlen = strlen(dfs_path);
856 consumed = MIN(pathlen, consumed);
857 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed]);
858 if (!*pp_targetpath) {
861 dfs_path[consumed] = '\0';
864 * *pp_targetpath is now the unconsumed part of the path.
865 * dfs_path is now the consumed part of the path
866 * (in \server\share\path format).
869 /* Open the connection to the target server & share */
870 if ((*targetcli = cli_cm_open(ctx, rootcli,
875 (rootcli->trans_enc_state != NULL),
879 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
884 if (extrapath && strlen(extrapath) > 0) {
885 /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
886 /* put the trailing \ on the path, so to be save we put one in if needed */
887 if (extrapath[strlen(extrapath)-1] != '\\' && **pp_targetpath != '\\') {
888 *pp_targetpath = talloc_asprintf(ctx,
893 *pp_targetpath = talloc_asprintf(ctx,
898 if (!*pp_targetpath) {
903 /* parse out the consumed mount path */
904 /* trim off the \server\share\ */
908 if (*ppath != '\\') {
909 d_printf("cli_resolve_path: "
910 "dfs_path (%s) not in correct format.\n",
915 ppath++; /* Now pointing at start of server name. */
917 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
921 ppath++; /* Now pointing at start of share name. */
923 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
927 ppath++; /* Now pointing at path component. */
929 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
934 cli_set_mntpoint(*targetcli, newmount);
936 /* Check for another dfs referral, note that we are not
937 checking for loops here. */
939 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
940 if (cli_resolve_path(ctx,
948 * When cli_resolve_path returns true here it's always
949 * returning the complete path in newpath, so we're done
953 *pp_targetpath = newpath;
960 /* If returning true ensure we return a dfs root full path. */
961 if ((*targetcli)->dfsroot) {
962 dfs_path = talloc_strdup(ctx, *pp_targetpath);
966 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
972 /********************************************************************
973 ********************************************************************/
975 bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
976 struct cli_state *cli,
977 const char *sharename,
981 const char *username,
982 const char *password,
985 struct client_dfs_referral *refs = NULL;
988 char *fullpath = NULL;
991 char *newextrapath = NULL;
994 if (!cli || !sharename) {
1000 /* special case. never check for a referral on the IPC$ share */
1002 if (strequal(sharename, "IPC$")) {
1006 /* send a trans2_query_path_info to check for a referral */
1008 fullpath = talloc_asprintf(ctx, "\\%s\\%s", cli->desthost, sharename );
1013 /* check for the referral */
1015 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, "IPC$", "IPC", NULL, 0))) {
1019 if (force_encrypt) {
1020 status = cli_cm_force_encryption(cli,
1025 if (!NT_STATUS_IS_OK(status)) {
1030 status = cli_dfs_get_referral(ctx, cli, fullpath, &refs,
1031 &num_refs, &consumed);
1032 res = NT_STATUS_IS_OK(status);
1034 status = cli_tdis(cli);
1035 if (!NT_STATUS_IS_OK(status)) {
1041 if (!res || !num_refs) {
1045 if (!refs[0].dfspath) {
1049 if (!split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1050 pp_newshare, &newextrapath)) {
1054 /* check that this is not a self-referral */
1056 if (strequal(cli->desthost, *pp_newserver) &&
1057 strequal(sharename, *pp_newshare)) {