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/>.
24 /********************************************************************
27 DFS paths are *always* of the form \server\share\<pathname> (the \ characters
28 are not C escaped here).
30 - but if we're using POSIX paths then <pathname> may contain
31 '/' separators, not '\\' separators. So cope with '\\' or '/'
32 as a separator when looking at the pathname part.... JRA.
33 ********************************************************************/
35 static struct cm_cred_struct {
40 bool fallback_after_kerberos;
44 static void cm_set_password(const char *newpass);
46 static bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
47 struct cli_state *cli,
48 const char *sharename,
56 /********************************************************************
57 Ensure a connection is encrypted.
58 ********************************************************************/
60 NTSTATUS cli_cm_force_encryption(struct cli_state *c,
64 const char *sharename)
66 NTSTATUS status = cli_force_encryption(c,
71 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED)) {
72 d_printf("Encryption required and "
73 "server that doesn't support "
74 "UNIX extensions - failing connect\n");
75 } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNKNOWN_REVISION)) {
76 d_printf("Encryption required and "
77 "can't get UNIX CIFS extensions "
78 "version from server.\n");
79 } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNSUPPORTED_COMPRESSION)) {
80 d_printf("Encryption required and "
81 "share %s doesn't support "
82 "encryption.\n", sharename);
83 } else if (!NT_STATUS_IS_OK(status)) {
84 d_printf("Encryption required and "
85 "setup failed with error %s.\n",
92 /********************************************************************
93 Return a connection to a server.
94 ********************************************************************/
96 static struct cli_state *do_connect(TALLOC_CTX *ctx,
105 struct cli_state *c = NULL;
106 struct nmb_name called, calling;
107 const char *server_n;
108 struct sockaddr_storage ss;
111 char *newserver, *newshare;
112 const char *username;
113 const char *password;
116 /* make a copy so we don't modify the global string 'service' */
117 servicename = talloc_strdup(ctx,share);
121 sharename = servicename;
122 if (*sharename == '\\') {
124 if (server == NULL) {
127 sharename = strchr_m(sharename,'\\');
139 make_nmb_name(&calling, global_myname(), 0x0);
140 make_nmb_name(&called , server, name_type);
145 /* have to open a new connection */
146 if (!(c=cli_initialise_ex(cm_creds.signing_state))) {
147 d_printf("Connection to %s failed\n", server_n);
154 cli_set_port(c, port);
157 status = cli_connect(c, server_n, &ss);
158 if (!NT_STATUS_IS_OK(status)) {
159 d_printf("Connection to %s failed (Error %s)\n",
166 if (max_protocol == 0) {
167 max_protocol = PROTOCOL_NT1;
169 c->protocol = max_protocol;
170 c->use_kerberos = cm_creds.use_kerberos;
171 c->fallback_after_kerberos = cm_creds.fallback_after_kerberos;
173 if (!cli_session_request(c, &calling, &called)) {
175 d_printf("session request to %s failed (%s)\n",
176 called.name, cli_errstr(c));
179 if ((p=strchr_m(called.name, '.'))) {
183 if (strcmp(called.name, "*SMBSERVER")) {
184 make_nmb_name(&called , "*SMBSERVER", 0x20);
190 DEBUG(4,(" session request ok\n"));
192 status = cli_negprot(c);
194 if (!NT_STATUS_IS_OK(status)) {
195 d_printf("protocol negotiation failed: %s\n",
201 if (!cm_creds.got_pass && !cm_creds.use_kerberos) {
204 label = talloc_asprintf(ctx, "Enter %s's password: ",
206 pass = getpass(label);
208 cm_set_password(pass);
213 username = cm_creds.username ? cm_creds.username : "";
214 password = cm_creds.password ? cm_creds.password : "";
216 if (!NT_STATUS_IS_OK(cli_session_setup(c, username,
217 password, strlen(password),
218 password, strlen(password),
220 /* If a password was not supplied then
221 * try again with a null username. */
222 if (password[0] || !username[0] || cm_creds.use_kerberos ||
223 !NT_STATUS_IS_OK(cli_session_setup(c, "",
227 d_printf("session setup failed: %s\n", cli_errstr(c));
228 if (NT_STATUS_V(cli_nt_error(c)) ==
229 NT_STATUS_V(NT_STATUS_MORE_PROCESSING_REQUIRED))
230 d_printf("did you forget to run kinit?\n");
234 d_printf("Anonymous login successful\n");
237 if ( show_sessetup ) {
238 if (*c->server_domain) {
239 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
240 c->server_domain,c->server_os,c->server_type));
241 } else if (*c->server_os || *c->server_type) {
242 DEBUG(0,("OS=[%s] Server=[%s]\n",
243 c->server_os,c->server_type));
246 DEBUG(4,(" session setup ok\n"));
248 /* here's the fun part....to support 'msdfs proxy' shares
249 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
250 here before trying to connect to the original share.
251 check_dfs_proxy() will fail if it is a normal share. */
253 if ((c->capabilities & CAP_DFS) &&
254 cli_check_msdfs_proxy(ctx, c, sharename,
255 &newserver, &newshare,
261 return do_connect(ctx, newserver,
263 force_encrypt, max_protocol,
267 /* must be a normal share */
269 status = cli_tcon_andx(c, sharename, "?????",
270 password, strlen(password)+1);
271 if (!NT_STATUS_IS_OK(status)) {
272 d_printf("tree connect failed: %s\n", nt_errstr(status));
278 status = cli_cm_force_encryption(c,
283 if (!NT_STATUS_IS_OK(status)) {
289 DEBUG(4,(" tconx ok\n"));
293 /****************************************************************************
294 ****************************************************************************/
296 static void cli_set_mntpoint(struct cli_state *cli, const char *mnt)
298 char *name = clean_name(NULL, mnt);
302 TALLOC_FREE(cli->dfs_mountpoint);
303 cli->dfs_mountpoint = talloc_strdup(cli, name);
307 /********************************************************************
308 Add a new connection to the list.
309 referring_cli == NULL means a new initial connection.
310 ********************************************************************/
312 static struct cli_state *cli_cm_connect(TALLOC_CTX *ctx,
313 struct cli_state *referring_cli,
322 struct cli_state *cli;
324 cli = do_connect(ctx, server, share,
325 show_hdr, force_encrypt, max_protocol,
332 /* Enter into the list. */
334 DLIST_ADD_END(referring_cli, cli, struct cli_state *);
337 if (referring_cli && referring_cli->posix_capabilities) {
339 uint32 caplow, caphigh;
340 if (cli_unix_extensions_version(cli, &major,
341 &minor, &caplow, &caphigh)) {
342 cli_set_unix_extensions_capabilities(cli,
351 /********************************************************************
352 Return a connection to a server on a particular share.
353 ********************************************************************/
355 static struct cli_state *cli_cm_find(struct cli_state *cli,
365 /* Search to the start of the list. */
366 for (p = cli; p; p = p->prev) {
367 if (strequal(server, p->desthost) &&
368 strequal(share,p->share)) {
373 /* Search to the end of the list. */
374 for (p = cli->next; p; p = p->next) {
375 if (strequal(server, p->desthost) &&
376 strequal(share,p->share)) {
384 /****************************************************************************
385 Open a client connection to a \\server\share.
386 ****************************************************************************/
388 struct cli_state *cli_cm_open(TALLOC_CTX *ctx,
389 struct cli_state *referring_cli,
398 /* Try to reuse an existing connection in this list. */
399 struct cli_state *c = cli_cm_find(referring_cli, server, share);
405 return cli_cm_connect(ctx, referring_cli,
406 server, share, show_hdr, force_encrypt,
407 max_protocol, port, name_type);
410 /****************************************************************************
411 ****************************************************************************/
413 void cli_cm_display(const struct cli_state *cli)
417 for (i=0; cli; cli = cli->next,i++ ) {
418 d_printf("%d:\tserver=%s, share=%s\n",
419 i, cli->desthost, cli->share );
423 /****************************************************************************
424 ****************************************************************************/
426 static void cm_set_password(const char *newpass)
428 SAFE_FREE(cm_creds.password);
429 cm_creds.password = SMB_STRDUP(newpass);
430 if (cm_creds.password) {
431 cm_creds.got_pass = true;
435 /****************************************************************************
436 ****************************************************************************/
438 void cli_cm_set_credentials(struct user_auth_info *auth_info)
440 SAFE_FREE(cm_creds.username);
441 cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
444 if (get_cmdline_auth_info_got_pass(auth_info)) {
445 cm_set_password(get_cmdline_auth_info_password(auth_info));
448 cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
449 cm_creds.fallback_after_kerberos = false;
450 cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
453 /****************************************************************************
454 ****************************************************************************/
456 void cli_cm_set_signing_state(int state)
458 cm_creds.signing_state = state;
461 /****************************************************************************
462 ****************************************************************************/
464 void cli_cm_set_username(const char *username)
466 SAFE_FREE(cm_creds.username);
467 cm_creds.username = SMB_STRDUP(username);
470 /****************************************************************************
471 ****************************************************************************/
473 void cli_cm_set_password(const char *newpass)
475 SAFE_FREE(cm_creds.password);
476 cm_creds.password = SMB_STRDUP(newpass);
477 if (cm_creds.password) {
478 cm_creds.got_pass = true;
482 /****************************************************************************
483 ****************************************************************************/
485 void cli_cm_set_use_kerberos(void)
487 cm_creds.use_kerberos = true;
490 /****************************************************************************
491 ****************************************************************************/
493 void cli_cm_set_fallback_after_kerberos(void)
495 cm_creds.fallback_after_kerberos = true;
498 /**********************************************************************
499 split a dfs path into the server, share name, and extrapath components
500 **********************************************************************/
502 static void split_dfs_path(TALLOC_CTX *ctx,
503 const char *nodepath,
513 *pp_extrapath = NULL;
515 path = talloc_strdup(ctx, nodepath);
520 if ( path[0] != '\\' ) {
524 p = strchr_m( path + 1, '\\' );
532 /* Look for any extra/deep path */
533 q = strchr_m(p, '\\');
537 *pp_extrapath = talloc_strdup(ctx, q);
539 *pp_extrapath = talloc_strdup(ctx, "");
542 *pp_share = talloc_strdup(ctx, p);
543 *pp_server = talloc_strdup(ctx, &path[1]);
546 /****************************************************************************
547 Return the original path truncated at the directory component before
548 the first wildcard character. Trust the caller to provide a NULL
550 ****************************************************************************/
552 static char *clean_path(TALLOC_CTX *ctx, const char *path)
558 /* No absolute paths. */
559 while (IS_DIRECTORY_SEP(*path)) {
563 path_out = talloc_strdup(ctx, path);
568 p1 = strchr_m(path_out, '*');
569 p2 = strchr_m(path_out, '?');
581 /* Now go back to the start of this component. */
582 p1 = strrchr_m(path_out, '/');
583 p2 = strrchr_m(path_out, '\\');
590 /* Strip any trailing separator */
592 len = strlen(path_out);
593 if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
594 path_out[len-1] = '\0';
600 /****************************************************************************
601 ****************************************************************************/
603 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
604 struct cli_state *cli,
607 /* Ensure the extrapath doesn't start with a separator. */
608 while (IS_DIRECTORY_SEP(*dir)) {
612 return talloc_asprintf(ctx, "\\%s\\%s\\%s",
613 cli->desthost, cli->share, dir);
616 /********************************************************************
617 check for dfs referral
618 ********************************************************************/
620 static bool cli_dfs_check_error( struct cli_state *cli, NTSTATUS status )
622 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
624 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
626 if (!((flgs2&FLAGS2_32_BIT_ERROR_CODES) &&
627 (flgs2&FLAGS2_UNICODE_STRINGS)))
630 if (NT_STATUS_EQUAL(status, NT_STATUS(IVAL(cli->inbuf,smb_rcls))))
636 /********************************************************************
637 Get the dfs referral link.
638 ********************************************************************/
640 bool cli_dfs_get_referral(TALLOC_CTX *ctx,
641 struct cli_state *cli,
643 CLIENT_DFS_REFERRAL**refs,
647 unsigned int data_len = 0;
648 unsigned int param_len = 0;
649 uint16 setup = TRANSACT2_GET_DFS_REFERRAL;
651 char *rparam=NULL, *rdata=NULL;
654 size_t pathlen = 2*(strlen(path)+1);
655 uint16 num_referrals;
656 CLIENT_DFS_REFERRAL *referrals = NULL;
662 param = SMB_MALLOC_ARRAY(char, 2+pathlen+2);
666 SSVAL(param, 0, 0x03); /* max referral level */
669 p += clistr_push(cli, p, path, pathlen, STR_TERMINATE);
670 param_len = PTR_DIFF(p, param);
672 if (!cli_send_trans(cli, SMBtrans2,
674 -1, 0, /* fid, flags */
675 &setup, 1, 0, /* setup, length, max */
676 param, param_len, 2, /* param, length, max */
677 NULL, 0, cli->max_xmit /* data, length, max */
685 if (!cli_receive_trans(cli, SMBtrans2,
687 &rdata, &data_len)) {
695 endp = rdata + data_len;
697 *consumed = SVAL(rdata, 0);
698 num_referrals = SVAL(rdata, 2);
700 if (num_referrals != 0) {
706 referrals = TALLOC_ARRAY(ctx, CLIENT_DFS_REFERRAL,
712 /* start at the referrals array */
715 for (i=0; i<num_referrals && p < endp; i++) {
719 ref_version = SVAL(p, 0);
720 ref_size = SVAL(p, 2);
721 node_offset = SVAL(p, 16);
723 if (ref_version != 3) {
728 referrals[i].proximity = SVAL(p, 8);
729 referrals[i].ttl = SVAL(p, 10);
731 if (p + node_offset > endp) {
734 clistr_pull_talloc(ctx, cli->inbuf,
735 &referrals[i].dfspath,
737 STR_TERMINATE|STR_UNICODE);
739 if (!referrals[i].dfspath) {
744 if (i < num_referrals) {
751 *num_refs = num_referrals;
761 /********************************************************************
762 ********************************************************************/
764 bool cli_resolve_path(TALLOC_CTX *ctx,
766 struct cli_state *rootcli,
768 struct cli_state **targetcli,
769 char **pp_targetpath)
771 CLIENT_DFS_REFERRAL *refs = NULL;
774 struct cli_state *cli_ipc = NULL;
775 char *dfs_path = NULL;
776 char *cleanpath = NULL;
777 char *extrapath = NULL;
781 struct cli_state *newcli = NULL;
782 char *newpath = NULL;
783 char *newmount = NULL;
785 SMB_STRUCT_STAT sbuf;
788 if ( !rootcli || !path || !targetcli ) {
792 /* Don't do anything if this is not a DFS root. */
794 if ( !rootcli->dfsroot) {
795 *targetcli = rootcli;
796 *pp_targetpath = talloc_strdup(ctx, path);
797 if (!*pp_targetpath) {
805 /* Send a trans2_query_path_info to check for a referral. */
807 cleanpath = clean_path(ctx, path);
812 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
817 if (cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes)) {
818 /* This is an ordinary path, just return it. */
819 *targetcli = rootcli;
820 *pp_targetpath = talloc_strdup(ctx, path);
821 if (!*pp_targetpath) {
827 /* Special case where client asked for a path that does not exist */
829 if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
830 *targetcli = rootcli;
831 *pp_targetpath = talloc_strdup(ctx, path);
832 if (!*pp_targetpath) {
838 /* We got an error, check for DFS referral. */
840 if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED)) {
844 /* Check for the referral. */
846 if (!(cli_ipc = cli_cm_open(ctx, rootcli,
849 (rootcli->trans_enc_state != NULL),
856 if (!cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
857 &num_refs, &consumed) || !num_refs) {
861 /* Just store the first referral for now. */
863 if (!refs[0].dfspath) {
866 split_dfs_path(ctx, refs[0].dfspath, &server, &share, &extrapath );
868 if (!server || !share) {
872 /* Make sure to recreate the original string including any wildcards. */
874 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
878 pathlen = strlen(dfs_path)*2;
879 consumed = MIN(pathlen, consumed);
880 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed/2]);
881 if (!*pp_targetpath) {
884 dfs_path[consumed/2] = '\0';
887 * *pp_targetpath is now the unconsumed part of the path.
888 * dfs_path is now the consumed part of the path
889 * (in \server\share\path format).
892 /* Open the connection to the target server & share */
893 if ((*targetcli = cli_cm_open(ctx, rootcli,
897 (rootcli->trans_enc_state != NULL),
901 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
906 if (extrapath && strlen(extrapath) > 0) {
907 *pp_targetpath = talloc_asprintf(ctx,
911 if (!*pp_targetpath) {
916 /* parse out the consumed mount path */
917 /* trim off the \server\share\ */
921 if (*ppath != '\\') {
922 d_printf("cli_resolve_path: "
923 "dfs_path (%s) not in correct format.\n",
928 ppath++; /* Now pointing at start of server name. */
930 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
934 ppath++; /* Now pointing at start of share name. */
936 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
940 ppath++; /* Now pointing at path component. */
942 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
947 cli_set_mntpoint(*targetcli, newmount);
949 /* Check for another dfs referral, note that we are not
950 checking for loops here. */
952 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
953 if (cli_resolve_path(ctx,
960 * When cli_resolve_path returns true here it's always
961 * returning the complete path in newpath, so we're done
965 *pp_targetpath = newpath;
972 /* If returning true ensure we return a dfs root full path. */
973 if ((*targetcli)->dfsroot) {
974 dfs_path = talloc_strdup(ctx, *pp_targetpath);
978 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
984 /********************************************************************
985 ********************************************************************/
987 static bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
988 struct cli_state *cli,
989 const char *sharename,
993 const char *username,
994 const char *password,
997 CLIENT_DFS_REFERRAL *refs = NULL;
1000 char *fullpath = NULL;
1003 char *newextrapath = NULL;
1005 if (!cli || !sharename) {
1011 /* special case. never check for a referral on the IPC$ share */
1013 if (strequal(sharename, "IPC$")) {
1017 /* send a trans2_query_path_info to check for a referral */
1019 fullpath = talloc_asprintf(ctx, "\\%s\\%s", cli->desthost, sharename );
1024 /* check for the referral */
1026 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, "IPC$", "IPC", NULL, 0))) {
1030 if (force_encrypt) {
1031 NTSTATUS status = cli_cm_force_encryption(cli,
1036 if (!NT_STATUS_IS_OK(status)) {
1041 res = cli_dfs_get_referral(ctx, cli, fullpath, &refs, &num_refs, &consumed);
1043 if (!cli_tdis(cli)) {
1049 if (!res || !num_refs) {
1053 if (!refs[0].dfspath) {
1057 split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1058 pp_newshare, &newextrapath );
1060 if ((*pp_newserver == NULL) || (*pp_newshare == NULL)) {
1064 /* check that this is not a self-referral */
1066 if (strequal(cli->desthost, *pp_newserver) &&
1067 strequal(sharename, *pp_newshare)) {