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 /* global state....globals reek! */
42 int max_protocol = PROTOCOL_NT1;
44 static struct cm_cred_struct {
49 bool fallback_after_kerberos;
53 static void cm_set_password(const char *newpass);
56 static int name_type = 0x20;
58 static struct client_connection *connections;
60 static bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
61 struct cli_state *cli,
62 const char *sharename,
70 /********************************************************************
71 Ensure a connection is encrypted.
72 ********************************************************************/
74 NTSTATUS cli_cm_force_encryption(struct cli_state *c,
78 const char *sharename)
80 NTSTATUS status = cli_force_encryption(c,
85 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED)) {
86 d_printf("Encryption required and "
87 "server that doesn't support "
88 "UNIX extensions - failing connect\n");
89 } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNKNOWN_REVISION)) {
90 d_printf("Encryption required and "
91 "can't get UNIX CIFS extensions "
92 "version from server.\n");
93 } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNSUPPORTED_COMPRESSION)) {
94 d_printf("Encryption required and "
95 "share %s doesn't support "
96 "encryption.\n", sharename);
97 } else if (!NT_STATUS_IS_OK(status)) {
98 d_printf("Encryption required and "
99 "setup failed with error %s.\n",
106 /********************************************************************
107 Return a connection to a server.
108 ********************************************************************/
110 static struct cli_state *do_connect(TALLOC_CTX *ctx,
116 struct cli_state *c = NULL;
117 struct nmb_name called, calling;
118 const char *server_n;
119 struct sockaddr_storage ss;
122 char *newserver, *newshare;
123 const char *username;
124 const char *password;
127 /* make a copy so we don't modify the global string 'service' */
128 servicename = talloc_strdup(ctx,share);
132 sharename = servicename;
133 if (*sharename == '\\') {
135 if (server == NULL) {
138 sharename = strchr_m(sharename,'\\');
150 make_nmb_name(&calling, global_myname(), 0x0);
151 make_nmb_name(&called , server, name_type);
156 /* have to open a new connection */
157 if (!(c=cli_initialise()) || (cli_set_port(c, port) != port)) {
158 d_printf("Connection to %s failed\n", server_n);
164 status = cli_connect(c, server_n, &ss);
165 if (!NT_STATUS_IS_OK(status)) {
166 d_printf("Connection to %s failed (Error %s)\n",
173 c->protocol = max_protocol;
174 c->use_kerberos = cm_creds.use_kerberos;
175 c->fallback_after_kerberos = cm_creds.fallback_after_kerberos;
176 cli_setup_signing_state(c, cm_creds.signing_state);
178 if (!cli_session_request(c, &calling, &called)) {
180 d_printf("session request to %s failed (%s)\n",
181 called.name, cli_errstr(c));
184 if ((p=strchr_m(called.name, '.'))) {
188 if (strcmp(called.name, "*SMBSERVER")) {
189 make_nmb_name(&called , "*SMBSERVER", 0x20);
195 DEBUG(4,(" session request ok\n"));
197 status = cli_negprot(c);
199 if (!NT_STATUS_IS_OK(status)) {
200 d_printf("protocol negotiation failed: %s\n",
206 if (!cm_creds.got_pass && !cm_creds.use_kerberos) {
209 label = talloc_asprintf(ctx, "Enter %s's password: ",
211 pass = getpass(label);
213 cm_set_password(pass);
218 username = cm_creds.username ? cm_creds.username : "";
219 password = cm_creds.password ? cm_creds.password : "";
221 if (!NT_STATUS_IS_OK(cli_session_setup(c, username,
222 password, strlen(password),
223 password, strlen(password),
225 /* If a password was not supplied then
226 * try again with a null username. */
227 if (password[0] || !username[0] || cm_creds.use_kerberos ||
228 !NT_STATUS_IS_OK(cli_session_setup(c, "",
232 d_printf("session setup failed: %s\n", cli_errstr(c));
233 if (NT_STATUS_V(cli_nt_error(c)) ==
234 NT_STATUS_V(NT_STATUS_MORE_PROCESSING_REQUIRED))
235 d_printf("did you forget to run kinit?\n");
239 d_printf("Anonymous login successful\n");
242 if ( show_sessetup ) {
243 if (*c->server_domain) {
244 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
245 c->server_domain,c->server_os,c->server_type));
246 } else if (*c->server_os || *c->server_type) {
247 DEBUG(0,("OS=[%s] Server=[%s]\n",
248 c->server_os,c->server_type));
251 DEBUG(4,(" session setup ok\n"));
253 /* here's the fun part....to support 'msdfs proxy' shares
254 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
255 here before trying to connect to the original share.
256 check_dfs_proxy() will fail if it is a normal share. */
258 if ((c->capabilities & CAP_DFS) &&
259 cli_check_msdfs_proxy(ctx, c, sharename,
260 &newserver, &newshare,
266 return do_connect(ctx, newserver,
267 newshare, false, force_encrypt);
270 /* must be a normal share */
272 if (!cli_send_tconX(c, sharename, "?????",
273 password, strlen(password)+1)) {
274 d_printf("tree connect failed: %s\n", cli_errstr(c));
280 status = cli_cm_force_encryption(c,
285 if (!NT_STATUS_IS_OK(status)) {
291 DEBUG(4,(" tconx ok\n"));
295 /****************************************************************************
296 ****************************************************************************/
298 static void cli_cm_set_mntpoint(struct cli_state *c, const char *mnt)
300 struct client_connection *p;
303 for (p=connections,i=0; p; p=p->next,i++) {
304 if (strequal(p->cli->desthost, c->desthost) &&
305 strequal(p->cli->share, c->share)) {
311 char *name = clean_name(NULL, p->mount);
315 p->mount = talloc_strdup(p, name);
320 /****************************************************************************
321 ****************************************************************************/
323 const char *cli_cm_get_mntpoint(struct cli_state *c)
325 struct client_connection *p;
328 for (p=connections,i=0; p; p=p->next,i++) {
329 if (strequal(p->cli->desthost, c->desthost) &&
330 strequal(p->cli->share, c->share)) {
341 /********************************************************************
342 Add a new connection to the list
343 ********************************************************************/
345 static struct cli_state *cli_cm_connect(TALLOC_CTX *ctx,
346 struct cli_state *referring_cli,
352 struct client_connection *node;
354 /* NB This must be the null context here... JRA. */
355 node = TALLOC_ZERO_ARRAY(NULL, struct client_connection, 1);
360 node->cli = do_connect(ctx, server, share, show_hdr, force_encrypt);
367 DLIST_ADD( connections, node );
369 cli_cm_set_mntpoint(node->cli, "");
371 if (referring_cli && referring_cli->posix_capabilities) {
373 uint32 caplow, caphigh;
374 if (cli_unix_extensions_version(node->cli, &major,
375 &minor, &caplow, &caphigh)) {
376 cli_set_unix_extensions_capabilities(node->cli,
385 /********************************************************************
386 Return a connection to a server.
387 ********************************************************************/
389 static struct cli_state *cli_cm_find(const char *server, const char *share)
391 struct client_connection *p;
393 for (p=connections; p; p=p->next) {
394 if ( strequal(server, p->cli->desthost) &&
395 strequal(share,p->cli->share)) {
403 /****************************************************************************
404 Open a client connection to a \\server\share. Set's the current *cli
405 global variable as a side-effect (but only if the connection is successful).
406 ****************************************************************************/
408 struct cli_state *cli_cm_open(TALLOC_CTX *ctx,
409 struct cli_state *referring_cli,
417 /* try to reuse an existing connection */
419 c = cli_cm_find(server, share);
421 c = cli_cm_connect(ctx, referring_cli,
422 server, share, show_hdr, force_encrypt);
428 /****************************************************************************
429 ****************************************************************************/
431 void cli_cm_shutdown(void)
433 struct client_connection *p, *x;
435 for (p=connections; p;) {
436 cli_shutdown(p->cli);
447 /****************************************************************************
448 ****************************************************************************/
450 void cli_cm_display(void)
452 struct client_connection *p;
455 for ( p=connections,i=0; p; p=p->next,i++ ) {
456 d_printf("%d:\tserver=%s, share=%s\n",
457 i, p->cli->desthost, p->cli->share );
461 /****************************************************************************
462 ****************************************************************************/
464 static void cm_set_password(const char *newpass)
466 SAFE_FREE(cm_creds.password);
467 cm_creds.password = SMB_STRDUP(newpass);
468 if (cm_creds.password) {
469 cm_creds.got_pass = true;
473 /****************************************************************************
474 ****************************************************************************/
476 void cli_cm_set_credentials(struct user_auth_info *auth_info)
478 SAFE_FREE(cm_creds.username);
479 cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
482 if (get_cmdline_auth_info_got_pass(auth_info)) {
483 cm_set_password(get_cmdline_auth_info_password(auth_info));
486 cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
487 cm_creds.fallback_after_kerberos = false;
488 cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
491 /****************************************************************************
492 ****************************************************************************/
494 void cli_cm_set_port(int port_number)
499 /****************************************************************************
500 ****************************************************************************/
502 void cli_cm_set_dest_name_type(int type)
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, &referrals[i].dfspath,
790 STR_TERMINATE|STR_UNICODE );
792 if (!referrals[i].dfspath) {
797 if (i < num_referrals) {
804 *num_refs = num_referrals;
814 /********************************************************************
815 ********************************************************************/
817 bool cli_resolve_path(TALLOC_CTX *ctx,
819 struct cli_state *rootcli,
821 struct cli_state **targetcli,
822 char **pp_targetpath)
824 CLIENT_DFS_REFERRAL *refs = NULL;
827 struct cli_state *cli_ipc = NULL;
828 char *dfs_path = NULL;
829 char *cleanpath = NULL;
830 char *extrapath = NULL;
834 struct cli_state *newcli = NULL;
835 char *newpath = NULL;
836 char *newmount = NULL;
838 SMB_STRUCT_STAT sbuf;
841 if ( !rootcli || !path || !targetcli ) {
845 /* Don't do anything if this is not a DFS root. */
847 if ( !rootcli->dfsroot) {
848 *targetcli = rootcli;
849 *pp_targetpath = talloc_strdup(ctx, path);
850 if (!*pp_targetpath) {
858 /* Send a trans2_query_path_info to check for a referral. */
860 cleanpath = clean_path(ctx, path);
865 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
870 if (cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes)) {
871 /* This is an ordinary path, just return it. */
872 *targetcli = rootcli;
873 *pp_targetpath = talloc_strdup(ctx, path);
874 if (!*pp_targetpath) {
880 /* Special case where client asked for a path that does not exist */
882 if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
883 *targetcli = rootcli;
884 *pp_targetpath = talloc_strdup(ctx, path);
885 if (!*pp_targetpath) {
891 /* We got an error, check for DFS referral. */
893 if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED)) {
897 /* Check for the referral. */
899 if (!(cli_ipc = cli_cm_open(ctx, rootcli,
902 (rootcli->trans_enc_state != NULL)))) {
906 if (!cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
907 &num_refs, &consumed) || !num_refs) {
911 /* Just store the first referral for now. */
913 if (!refs[0].dfspath) {
916 split_dfs_path(ctx, refs[0].dfspath, &server, &share, &extrapath );
918 if (!server || !share) {
922 /* Make sure to recreate the original string including any wildcards. */
924 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
928 pathlen = strlen(dfs_path)*2;
929 consumed = MIN(pathlen, consumed);
930 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed/2]);
931 if (!*pp_targetpath) {
934 dfs_path[consumed/2] = '\0';
937 * *pp_targetpath is now the unconsumed part of the path.
938 * dfs_path is now the consumed part of the path
939 * (in \server\share\path format).
942 /* Open the connection to the target server & share */
943 if ((*targetcli = cli_cm_open(ctx, rootcli,
947 (rootcli->trans_enc_state != NULL))) == NULL) {
948 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
953 if (extrapath && strlen(extrapath) > 0) {
954 *pp_targetpath = talloc_asprintf(ctx,
958 if (!*pp_targetpath) {
963 /* parse out the consumed mount path */
964 /* trim off the \server\share\ */
968 if (*ppath != '\\') {
969 d_printf("cli_resolve_path: "
970 "dfs_path (%s) not in correct format.\n",
975 ppath++; /* Now pointing at start of server name. */
977 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
981 ppath++; /* Now pointing at start of share name. */
983 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
987 ppath++; /* Now pointing at path component. */
989 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
994 cli_cm_set_mntpoint(*targetcli, newmount);
996 /* Check for another dfs referral, note that we are not
997 checking for loops here. */
999 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
1000 if (cli_resolve_path(ctx,
1007 * When cli_resolve_path returns true here it's always
1008 * returning the complete path in newpath, so we're done
1011 *targetcli = newcli;
1012 *pp_targetpath = newpath;
1019 /* If returning true ensure we return a dfs root full path. */
1020 if ((*targetcli)->dfsroot) {
1021 dfs_path = talloc_strdup(ctx, *pp_targetpath);
1025 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
1031 /********************************************************************
1032 ********************************************************************/
1034 static bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
1035 struct cli_state *cli,
1036 const char *sharename,
1037 char **pp_newserver,
1040 const char *username,
1041 const char *password,
1044 CLIENT_DFS_REFERRAL *refs = NULL;
1045 size_t num_refs = 0;
1047 char *fullpath = NULL;
1050 char *newextrapath = NULL;
1052 if (!cli || !sharename) {
1058 /* special case. never check for a referral on the IPC$ share */
1060 if (strequal(sharename, "IPC$")) {
1064 /* send a trans2_query_path_info to check for a referral */
1066 fullpath = talloc_asprintf(ctx, "\\%s\\%s", cli->desthost, sharename );
1071 /* check for the referral */
1073 if (!cli_send_tconX(cli, "IPC$", "IPC", NULL, 0)) {
1077 if (force_encrypt) {
1078 NTSTATUS status = cli_cm_force_encryption(cli,
1083 if (!NT_STATUS_IS_OK(status)) {
1088 res = cli_dfs_get_referral(ctx, cli, fullpath, &refs, &num_refs, &consumed);
1090 if (!cli_tdis(cli)) {
1096 if (!res || !num_refs) {
1100 if (!refs[0].dfspath) {
1104 split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1105 pp_newshare, &newextrapath );
1107 if ((*pp_newserver == NULL) || (*pp_newshare == NULL)) {
1111 /* check that this is not a self-referral */
1113 if (strequal(cli->desthost, *pp_newserver) &&
1114 strequal(sharename, *pp_newshare)) {