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
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 struct client_connection {
36 struct client_connection *prev, *next;
37 struct cli_state *cli;
41 static struct cm_cred_struct {
46 bool fallback_after_kerberos;
50 static void cm_set_password(const char *newpass);
52 static struct client_connection *connections;
54 static bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
55 struct cli_state *cli,
56 const char *sharename,
64 /********************************************************************
65 Ensure a connection is encrypted.
66 ********************************************************************/
68 NTSTATUS cli_cm_force_encryption(struct cli_state *c,
72 const char *sharename)
74 NTSTATUS status = cli_force_encryption(c,
79 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED)) {
80 d_printf("Encryption required and "
81 "server that doesn't support "
82 "UNIX extensions - failing connect\n");
83 } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNKNOWN_REVISION)) {
84 d_printf("Encryption required and "
85 "can't get UNIX CIFS extensions "
86 "version from server.\n");
87 } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNSUPPORTED_COMPRESSION)) {
88 d_printf("Encryption required and "
89 "share %s doesn't support "
90 "encryption.\n", sharename);
91 } else if (!NT_STATUS_IS_OK(status)) {
92 d_printf("Encryption required and "
93 "setup failed with error %s.\n",
100 /********************************************************************
101 Return a connection to a server.
102 ********************************************************************/
104 static struct cli_state *do_connect(TALLOC_CTX *ctx,
113 struct cli_state *c = NULL;
114 struct nmb_name called, calling;
115 const char *server_n;
116 struct sockaddr_storage ss;
119 char *newserver, *newshare;
120 const char *username;
121 const char *password;
124 /* make a copy so we don't modify the global string 'service' */
125 servicename = talloc_strdup(ctx,share);
129 sharename = servicename;
130 if (*sharename == '\\') {
132 if (server == NULL) {
135 sharename = strchr_m(sharename,'\\');
147 make_nmb_name(&calling, global_myname(), 0x0);
148 make_nmb_name(&called , server, name_type);
153 /* have to open a new connection */
154 if (!(c=cli_initialise())) {
155 d_printf("Connection to %s failed\n", server_n);
162 cli_set_port(c, port);
165 status = cli_connect(c, server_n, &ss);
166 if (!NT_STATUS_IS_OK(status)) {
167 d_printf("Connection to %s failed (Error %s)\n",
174 if (max_protocol == 0) {
175 max_protocol = PROTOCOL_NT1;
177 c->protocol = max_protocol;
178 c->use_kerberos = cm_creds.use_kerberos;
179 c->fallback_after_kerberos = cm_creds.fallback_after_kerberos;
180 cli_setup_signing_state(c, cm_creds.signing_state);
182 if (!cli_session_request(c, &calling, &called)) {
184 d_printf("session request to %s failed (%s)\n",
185 called.name, cli_errstr(c));
188 if ((p=strchr_m(called.name, '.'))) {
192 if (strcmp(called.name, "*SMBSERVER")) {
193 make_nmb_name(&called , "*SMBSERVER", 0x20);
199 DEBUG(4,(" session request ok\n"));
201 status = cli_negprot(c);
203 if (!NT_STATUS_IS_OK(status)) {
204 d_printf("protocol negotiation failed: %s\n",
210 if (!cm_creds.got_pass && !cm_creds.use_kerberos) {
213 label = talloc_asprintf(ctx, "Enter %s's password: ",
215 pass = getpass(label);
217 cm_set_password(pass);
222 username = cm_creds.username ? cm_creds.username : "";
223 password = cm_creds.password ? cm_creds.password : "";
225 if (!NT_STATUS_IS_OK(cli_session_setup(c, username,
226 password, strlen(password),
227 password, strlen(password),
229 /* If a password was not supplied then
230 * try again with a null username. */
231 if (password[0] || !username[0] || cm_creds.use_kerberos ||
232 !NT_STATUS_IS_OK(cli_session_setup(c, "",
236 d_printf("session setup failed: %s\n", cli_errstr(c));
237 if (NT_STATUS_V(cli_nt_error(c)) ==
238 NT_STATUS_V(NT_STATUS_MORE_PROCESSING_REQUIRED))
239 d_printf("did you forget to run kinit?\n");
243 d_printf("Anonymous login successful\n");
246 if ( show_sessetup ) {
247 if (*c->server_domain) {
248 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
249 c->server_domain,c->server_os,c->server_type));
250 } else if (*c->server_os || *c->server_type) {
251 DEBUG(0,("OS=[%s] Server=[%s]\n",
252 c->server_os,c->server_type));
255 DEBUG(4,(" session setup ok\n"));
257 /* here's the fun part....to support 'msdfs proxy' shares
258 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
259 here before trying to connect to the original share.
260 check_dfs_proxy() will fail if it is a normal share. */
262 if ((c->capabilities & CAP_DFS) &&
263 cli_check_msdfs_proxy(ctx, c, sharename,
264 &newserver, &newshare,
270 return do_connect(ctx, newserver,
272 force_encrypt, max_protocol,
276 /* must be a normal share */
278 if (!cli_send_tconX(c, sharename, "?????",
279 password, strlen(password)+1)) {
280 d_printf("tree connect failed: %s\n", cli_errstr(c));
286 status = cli_cm_force_encryption(c,
291 if (!NT_STATUS_IS_OK(status)) {
297 DEBUG(4,(" tconx ok\n"));
301 /****************************************************************************
302 ****************************************************************************/
304 static void cli_cm_set_mntpoint(struct cli_state *c, const char *mnt)
306 struct client_connection *p;
309 for (p=connections,i=0; p; p=p->next,i++) {
310 if (strequal(p->cli->desthost, c->desthost) &&
311 strequal(p->cli->share, c->share)) {
317 char *name = clean_name(NULL, mnt);
321 TALLOC_FREE(p->mount);
322 p->mount = talloc_strdup(p, name);
327 /****************************************************************************
328 ****************************************************************************/
330 const char *cli_cm_get_mntpoint(struct cli_state *c)
332 struct client_connection *p;
335 for (p=connections,i=0; p; p=p->next,i++) {
336 if (strequal(p->cli->desthost, c->desthost) &&
337 strequal(p->cli->share, c->share)) {
348 /********************************************************************
349 Add a new connection to the list
350 ********************************************************************/
352 static struct cli_state *cli_cm_connect(TALLOC_CTX *ctx,
353 struct cli_state *referring_cli,
362 struct client_connection *node;
364 /* NB This must be the null context here... JRA. */
365 node = TALLOC_ZERO_ARRAY(NULL, struct client_connection, 1);
370 node->cli = do_connect(ctx, server, share,
371 show_hdr, force_encrypt, max_protocol,
379 DLIST_ADD( connections, node );
381 cli_cm_set_mntpoint(node->cli, "");
383 if (referring_cli && referring_cli->posix_capabilities) {
385 uint32 caplow, caphigh;
386 if (cli_unix_extensions_version(node->cli, &major,
387 &minor, &caplow, &caphigh)) {
388 cli_set_unix_extensions_capabilities(node->cli,
397 /********************************************************************
398 Return a connection to a server.
399 ********************************************************************/
401 static struct cli_state *cli_cm_find(const char *server, const char *share)
403 struct client_connection *p;
405 for (p=connections; p; p=p->next) {
406 if ( strequal(server, p->cli->desthost) &&
407 strequal(share,p->cli->share)) {
415 /****************************************************************************
416 Open a client connection to a \\server\share. Set's the current *cli
417 global variable as a side-effect (but only if the connection is successful).
418 ****************************************************************************/
420 struct cli_state *cli_cm_open(TALLOC_CTX *ctx,
421 struct cli_state *referring_cli,
432 /* try to reuse an existing connection */
434 c = cli_cm_find(server, share);
436 c = cli_cm_connect(ctx, referring_cli,
437 server, share, show_hdr, force_encrypt,
438 max_protocol, port, name_type);
444 /****************************************************************************
445 ****************************************************************************/
447 void cli_cm_shutdown(void)
449 struct client_connection *p, *x;
451 for (p=connections; p;) {
452 cli_shutdown(p->cli);
463 /****************************************************************************
464 ****************************************************************************/
466 void cli_cm_display(void)
468 struct client_connection *p;
471 for ( p=connections,i=0; p; p=p->next,i++ ) {
472 d_printf("%d:\tserver=%s, share=%s\n",
473 i, p->cli->desthost, p->cli->share );
477 /****************************************************************************
478 ****************************************************************************/
480 static void cm_set_password(const char *newpass)
482 SAFE_FREE(cm_creds.password);
483 cm_creds.password = SMB_STRDUP(newpass);
484 if (cm_creds.password) {
485 cm_creds.got_pass = true;
489 /****************************************************************************
490 ****************************************************************************/
492 void cli_cm_set_credentials(struct user_auth_info *auth_info)
494 SAFE_FREE(cm_creds.username);
495 cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
498 if (get_cmdline_auth_info_got_pass(auth_info)) {
499 cm_set_password(get_cmdline_auth_info_password(auth_info));
502 cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
503 cm_creds.fallback_after_kerberos = false;
504 cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
507 /****************************************************************************
508 ****************************************************************************/
510 void cli_cm_set_signing_state(int state)
512 cm_creds.signing_state = state;
515 /****************************************************************************
516 ****************************************************************************/
518 void cli_cm_set_username(const char *username)
520 SAFE_FREE(cm_creds.username);
521 cm_creds.username = SMB_STRDUP(username);
524 /****************************************************************************
525 ****************************************************************************/
527 void cli_cm_set_password(const char *newpass)
529 SAFE_FREE(cm_creds.password);
530 cm_creds.password = SMB_STRDUP(newpass);
531 if (cm_creds.password) {
532 cm_creds.got_pass = true;
536 /****************************************************************************
537 ****************************************************************************/
539 void cli_cm_set_use_kerberos(void)
541 cm_creds.use_kerberos = true;
544 /****************************************************************************
545 ****************************************************************************/
547 void cli_cm_set_fallback_after_kerberos(void)
549 cm_creds.fallback_after_kerberos = true;
552 /**********************************************************************
553 split a dfs path into the server, share name, and extrapath components
554 **********************************************************************/
556 static void split_dfs_path(TALLOC_CTX *ctx,
557 const char *nodepath,
567 *pp_extrapath = NULL;
569 path = talloc_strdup(ctx, nodepath);
574 if ( path[0] != '\\' ) {
578 p = strchr_m( path + 1, '\\' );
586 /* Look for any extra/deep path */
587 q = strchr_m(p, '\\');
591 *pp_extrapath = talloc_strdup(ctx, q);
593 *pp_extrapath = talloc_strdup(ctx, "");
596 *pp_share = talloc_strdup(ctx, p);
597 *pp_server = talloc_strdup(ctx, &path[1]);
600 /****************************************************************************
601 Return the original path truncated at the directory component before
602 the first wildcard character. Trust the caller to provide a NULL
604 ****************************************************************************/
606 static char *clean_path(TALLOC_CTX *ctx, const char *path)
612 /* No absolute paths. */
613 while (IS_DIRECTORY_SEP(*path)) {
617 path_out = talloc_strdup(ctx, path);
622 p1 = strchr_m(path_out, '*');
623 p2 = strchr_m(path_out, '?');
635 /* Now go back to the start of this component. */
636 p1 = strrchr_m(path_out, '/');
637 p2 = strrchr_m(path_out, '\\');
644 /* Strip any trailing separator */
646 len = strlen(path_out);
647 if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
648 path_out[len-1] = '\0';
654 /****************************************************************************
655 ****************************************************************************/
657 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
658 struct cli_state *cli,
661 /* Ensure the extrapath doesn't start with a separator. */
662 while (IS_DIRECTORY_SEP(*dir)) {
666 return talloc_asprintf(ctx, "\\%s\\%s\\%s",
667 cli->desthost, cli->share, dir);
670 /********************************************************************
671 check for dfs referral
672 ********************************************************************/
674 static bool cli_dfs_check_error( struct cli_state *cli, NTSTATUS status )
676 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
678 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
680 if (!((flgs2&FLAGS2_32_BIT_ERROR_CODES) &&
681 (flgs2&FLAGS2_UNICODE_STRINGS)))
684 if (NT_STATUS_EQUAL(status, NT_STATUS(IVAL(cli->inbuf,smb_rcls))))
690 /********************************************************************
691 Get the dfs referral link.
692 ********************************************************************/
694 bool cli_dfs_get_referral(TALLOC_CTX *ctx,
695 struct cli_state *cli,
697 CLIENT_DFS_REFERRAL**refs,
701 unsigned int data_len = 0;
702 unsigned int param_len = 0;
703 uint16 setup = TRANSACT2_GET_DFS_REFERRAL;
705 char *rparam=NULL, *rdata=NULL;
708 size_t pathlen = 2*(strlen(path)+1);
709 uint16 num_referrals;
710 CLIENT_DFS_REFERRAL *referrals = NULL;
716 param = SMB_MALLOC_ARRAY(char, 2+pathlen+2);
720 SSVAL(param, 0, 0x03); /* max referral level */
723 p += clistr_push(cli, p, path, pathlen, STR_TERMINATE);
724 param_len = PTR_DIFF(p, param);
726 if (!cli_send_trans(cli, SMBtrans2,
728 -1, 0, /* fid, flags */
729 &setup, 1, 0, /* setup, length, max */
730 param, param_len, 2, /* param, length, max */
731 NULL, 0, cli->max_xmit /* data, length, max */
739 if (!cli_receive_trans(cli, SMBtrans2,
741 &rdata, &data_len)) {
749 endp = rdata + data_len;
751 *consumed = SVAL(rdata, 0);
752 num_referrals = SVAL(rdata, 2);
754 if (num_referrals != 0) {
760 referrals = TALLOC_ARRAY(ctx, CLIENT_DFS_REFERRAL,
766 /* start at the referrals array */
769 for (i=0; i<num_referrals && p < endp; i++) {
773 ref_version = SVAL(p, 0);
774 ref_size = SVAL(p, 2);
775 node_offset = SVAL(p, 16);
777 if (ref_version != 3) {
782 referrals[i].proximity = SVAL(p, 8);
783 referrals[i].ttl = SVAL(p, 10);
785 if (p + node_offset > endp) {
788 clistr_pull_talloc(ctx, cli->inbuf,
789 &referrals[i].dfspath,
791 STR_TERMINATE|STR_UNICODE);
793 if (!referrals[i].dfspath) {
798 if (i < num_referrals) {
805 *num_refs = num_referrals;
815 /********************************************************************
816 ********************************************************************/
818 bool cli_resolve_path(TALLOC_CTX *ctx,
820 struct cli_state *rootcli,
822 struct cli_state **targetcli,
823 char **pp_targetpath)
825 CLIENT_DFS_REFERRAL *refs = NULL;
828 struct cli_state *cli_ipc = NULL;
829 char *dfs_path = NULL;
830 char *cleanpath = NULL;
831 char *extrapath = NULL;
835 struct cli_state *newcli = NULL;
836 char *newpath = NULL;
837 char *newmount = NULL;
839 SMB_STRUCT_STAT sbuf;
842 if ( !rootcli || !path || !targetcli ) {
846 /* Don't do anything if this is not a DFS root. */
848 if ( !rootcli->dfsroot) {
849 *targetcli = rootcli;
850 *pp_targetpath = talloc_strdup(ctx, path);
851 if (!*pp_targetpath) {
859 /* Send a trans2_query_path_info to check for a referral. */
861 cleanpath = clean_path(ctx, path);
866 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
871 if (cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes)) {
872 /* This is an ordinary path, just return it. */
873 *targetcli = rootcli;
874 *pp_targetpath = talloc_strdup(ctx, path);
875 if (!*pp_targetpath) {
881 /* Special case where client asked for a path that does not exist */
883 if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
884 *targetcli = rootcli;
885 *pp_targetpath = talloc_strdup(ctx, path);
886 if (!*pp_targetpath) {
892 /* We got an error, check for DFS referral. */
894 if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED)) {
898 /* Check for the referral. */
900 if (!(cli_ipc = cli_cm_open(ctx, rootcli,
903 (rootcli->trans_enc_state != NULL),
910 if (!cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
911 &num_refs, &consumed) || !num_refs) {
915 /* Just store the first referral for now. */
917 if (!refs[0].dfspath) {
920 split_dfs_path(ctx, refs[0].dfspath, &server, &share, &extrapath );
922 if (!server || !share) {
926 /* Make sure to recreate the original string including any wildcards. */
928 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
932 pathlen = strlen(dfs_path)*2;
933 consumed = MIN(pathlen, consumed);
934 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed/2]);
935 if (!*pp_targetpath) {
938 dfs_path[consumed/2] = '\0';
941 * *pp_targetpath is now the unconsumed part of the path.
942 * dfs_path is now the consumed part of the path
943 * (in \server\share\path format).
946 /* Open the connection to the target server & share */
947 if ((*targetcli = cli_cm_open(ctx, rootcli,
951 (rootcli->trans_enc_state != NULL),
955 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
960 if (extrapath && strlen(extrapath) > 0) {
961 *pp_targetpath = talloc_asprintf(ctx,
965 if (!*pp_targetpath) {
970 /* parse out the consumed mount path */
971 /* trim off the \server\share\ */
975 if (*ppath != '\\') {
976 d_printf("cli_resolve_path: "
977 "dfs_path (%s) not in correct format.\n",
982 ppath++; /* Now pointing at start of server name. */
984 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
988 ppath++; /* Now pointing at start of share name. */
990 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
994 ppath++; /* Now pointing at path component. */
996 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
1001 cli_cm_set_mntpoint(*targetcli, newmount);
1003 /* Check for another dfs referral, note that we are not
1004 checking for loops here. */
1006 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
1007 if (cli_resolve_path(ctx,
1014 * When cli_resolve_path returns true here it's always
1015 * returning the complete path in newpath, so we're done
1018 *targetcli = newcli;
1019 *pp_targetpath = newpath;
1026 /* If returning true ensure we return a dfs root full path. */
1027 if ((*targetcli)->dfsroot) {
1028 dfs_path = talloc_strdup(ctx, *pp_targetpath);
1032 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
1038 /********************************************************************
1039 ********************************************************************/
1041 static bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
1042 struct cli_state *cli,
1043 const char *sharename,
1044 char **pp_newserver,
1047 const char *username,
1048 const char *password,
1051 CLIENT_DFS_REFERRAL *refs = NULL;
1052 size_t num_refs = 0;
1054 char *fullpath = NULL;
1057 char *newextrapath = NULL;
1059 if (!cli || !sharename) {
1065 /* special case. never check for a referral on the IPC$ share */
1067 if (strequal(sharename, "IPC$")) {
1071 /* send a trans2_query_path_info to check for a referral */
1073 fullpath = talloc_asprintf(ctx, "\\%s\\%s", cli->desthost, sharename );
1078 /* check for the referral */
1080 if (!cli_send_tconX(cli, "IPC$", "IPC", NULL, 0)) {
1084 if (force_encrypt) {
1085 NTSTATUS status = cli_cm_force_encryption(cli,
1090 if (!NT_STATUS_IS_OK(status)) {
1095 res = cli_dfs_get_referral(ctx, cli, fullpath, &refs, &num_refs, &consumed);
1097 if (!cli_tdis(cli)) {
1103 if (!res || !num_refs) {
1107 if (!refs[0].dfspath) {
1111 split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1112 pp_newshare, &newextrapath );
1114 if ((*pp_newserver == NULL) || (*pp_newshare == NULL)) {
1118 /* check that this is not a self-referral */
1120 if (strequal(cli->desthost, *pp_newserver) &&
1121 strequal(sharename, *pp_newshare)) {