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 bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
36 struct cli_state *cli,
37 const char *sharename,
45 /********************************************************************
46 Ensure a connection is encrypted.
47 ********************************************************************/
49 NTSTATUS cli_cm_force_encryption(struct cli_state *c,
53 const char *sharename)
55 NTSTATUS status = cli_force_encryption(c,
60 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED)) {
61 d_printf("Encryption required and "
62 "server that doesn't support "
63 "UNIX extensions - failing connect\n");
64 } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNKNOWN_REVISION)) {
65 d_printf("Encryption required and "
66 "can't get UNIX CIFS extensions "
67 "version from server.\n");
68 } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNSUPPORTED_COMPRESSION)) {
69 d_printf("Encryption required and "
70 "share %s doesn't support "
71 "encryption.\n", sharename);
72 } else if (!NT_STATUS_IS_OK(status)) {
73 d_printf("Encryption required and "
74 "setup failed with error %s.\n",
81 /********************************************************************
82 Return a connection to a server.
83 ********************************************************************/
85 static struct cli_state *do_connect(TALLOC_CTX *ctx,
88 const struct user_auth_info *auth_info,
95 struct cli_state *c = NULL;
96 struct nmb_name called, calling;
98 struct sockaddr_storage ss;
101 char *newserver, *newshare;
102 const char *username;
103 const char *password;
106 /* make a copy so we don't modify the global string 'service' */
107 servicename = talloc_strdup(ctx,share);
111 sharename = servicename;
112 if (*sharename == '\\') {
114 if (server == NULL) {
117 sharename = strchr_m(sharename,'\\');
129 make_nmb_name(&calling, global_myname(), 0x0);
130 make_nmb_name(&called , server, name_type);
135 /* have to open a new connection */
136 if (!(c=cli_initialise_ex(get_cmdline_auth_info_signing_state(auth_info)))) {
137 d_printf("Connection to %s failed\n", server_n);
144 cli_set_port(c, port);
147 status = cli_connect(c, server_n, &ss);
148 if (!NT_STATUS_IS_OK(status)) {
149 d_printf("Connection to %s failed (Error %s)\n",
156 if (max_protocol == 0) {
157 max_protocol = PROTOCOL_NT1;
159 c->protocol = max_protocol;
160 c->use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
161 c->fallback_after_kerberos =
162 get_cmdline_auth_info_fallback_after_kerberos(auth_info);
164 if (!cli_session_request(c, &calling, &called)) {
166 d_printf("session request to %s failed (%s)\n",
167 called.name, cli_errstr(c));
170 if ((p=strchr_m(called.name, '.'))) {
174 if (strcmp(called.name, "*SMBSERVER")) {
175 make_nmb_name(&called , "*SMBSERVER", 0x20);
181 DEBUG(4,(" session request ok\n"));
183 status = cli_negprot(c);
185 if (!NT_STATUS_IS_OK(status)) {
186 d_printf("protocol negotiation failed: %s\n",
192 username = get_cmdline_auth_info_username(auth_info);
193 password = get_cmdline_auth_info_password(auth_info);
195 if (!NT_STATUS_IS_OK(cli_session_setup(c, username,
196 password, strlen(password),
197 password, strlen(password),
199 /* If a password was not supplied then
200 * try again with a null username. */
201 if (password[0] || !username[0] ||
202 get_cmdline_auth_info_use_kerberos(auth_info) ||
203 !NT_STATUS_IS_OK(cli_session_setup(c, "",
207 d_printf("session setup failed: %s\n", cli_errstr(c));
208 if (NT_STATUS_V(cli_nt_error(c)) ==
209 NT_STATUS_V(NT_STATUS_MORE_PROCESSING_REQUIRED))
210 d_printf("did you forget to run kinit?\n");
214 d_printf("Anonymous login successful\n");
217 if ( show_sessetup ) {
218 if (*c->server_domain) {
219 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
220 c->server_domain,c->server_os,c->server_type));
221 } else if (*c->server_os || *c->server_type) {
222 DEBUG(0,("OS=[%s] Server=[%s]\n",
223 c->server_os,c->server_type));
226 DEBUG(4,(" session setup ok\n"));
228 /* here's the fun part....to support 'msdfs proxy' shares
229 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
230 here before trying to connect to the original share.
231 check_dfs_proxy() will fail if it is a normal share. */
233 if ((c->capabilities & CAP_DFS) &&
234 cli_check_msdfs_proxy(ctx, c, sharename,
235 &newserver, &newshare,
241 return do_connect(ctx, newserver,
242 newshare, auth_info, false,
243 force_encrypt, max_protocol,
247 /* must be a normal share */
249 status = cli_tcon_andx(c, sharename, "?????",
250 password, strlen(password)+1);
251 if (!NT_STATUS_IS_OK(status)) {
252 d_printf("tree connect failed: %s\n", nt_errstr(status));
258 status = cli_cm_force_encryption(c,
263 if (!NT_STATUS_IS_OK(status)) {
269 DEBUG(4,(" tconx ok\n"));
273 /****************************************************************************
274 ****************************************************************************/
276 static void cli_set_mntpoint(struct cli_state *cli, const char *mnt)
278 char *name = clean_name(NULL, mnt);
282 TALLOC_FREE(cli->dfs_mountpoint);
283 cli->dfs_mountpoint = talloc_strdup(cli, name);
287 /********************************************************************
288 Add a new connection to the list.
289 referring_cli == NULL means a new initial connection.
290 ********************************************************************/
292 static struct cli_state *cli_cm_connect(TALLOC_CTX *ctx,
293 struct cli_state *referring_cli,
296 const struct user_auth_info *auth_info,
303 struct cli_state *cli;
305 cli = do_connect(ctx, server, share,
307 show_hdr, force_encrypt, max_protocol,
314 /* Enter into the list. */
316 DLIST_ADD_END(referring_cli, cli, struct cli_state *);
319 if (referring_cli && referring_cli->posix_capabilities) {
321 uint32 caplow, caphigh;
322 if (cli_unix_extensions_version(cli, &major,
323 &minor, &caplow, &caphigh)) {
324 cli_set_unix_extensions_capabilities(cli,
333 /********************************************************************
334 Return a connection to a server on a particular share.
335 ********************************************************************/
337 static struct cli_state *cli_cm_find(struct cli_state *cli,
347 /* Search to the start of the list. */
348 for (p = cli; p; p = p->prev) {
349 if (strequal(server, p->desthost) &&
350 strequal(share,p->share)) {
355 /* Search to the end of the list. */
356 for (p = cli->next; p; p = p->next) {
357 if (strequal(server, p->desthost) &&
358 strequal(share,p->share)) {
366 /****************************************************************************
367 Open a client connection to a \\server\share.
368 ****************************************************************************/
370 struct cli_state *cli_cm_open(TALLOC_CTX *ctx,
371 struct cli_state *referring_cli,
374 const struct user_auth_info *auth_info,
381 /* Try to reuse an existing connection in this list. */
382 struct cli_state *c = cli_cm_find(referring_cli, server, share);
388 if (auth_info == NULL) {
389 /* Can't do a new connection
390 * without auth info. */
391 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
392 "without auth info\n",
397 return cli_cm_connect(ctx,
409 /****************************************************************************
410 ****************************************************************************/
412 void cli_cm_display(const struct cli_state *cli)
416 for (i=0; cli; cli = cli->next,i++ ) {
417 d_printf("%d:\tserver=%s, share=%s\n",
418 i, cli->desthost, cli->share );
422 /****************************************************************************
423 ****************************************************************************/
425 /****************************************************************************
426 ****************************************************************************/
429 void cli_cm_set_credentials(struct user_auth_info *auth_info)
431 SAFE_FREE(cm_creds.username);
432 cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
435 if (get_cmdline_auth_info_got_pass(auth_info)) {
436 cm_set_password(get_cmdline_auth_info_password(auth_info));
439 cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
440 cm_creds.fallback_after_kerberos = false;
441 cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
445 /**********************************************************************
446 split a dfs path into the server, share name, and extrapath components
447 **********************************************************************/
449 static void split_dfs_path(TALLOC_CTX *ctx,
450 const char *nodepath,
460 *pp_extrapath = NULL;
462 path = talloc_strdup(ctx, nodepath);
467 if ( path[0] != '\\' ) {
471 p = strchr_m( path + 1, '\\' );
479 /* Look for any extra/deep path */
480 q = strchr_m(p, '\\');
484 *pp_extrapath = talloc_strdup(ctx, q);
486 *pp_extrapath = talloc_strdup(ctx, "");
489 *pp_share = talloc_strdup(ctx, p);
490 *pp_server = talloc_strdup(ctx, &path[1]);
493 /****************************************************************************
494 Return the original path truncated at the directory component before
495 the first wildcard character. Trust the caller to provide a NULL
497 ****************************************************************************/
499 static char *clean_path(TALLOC_CTX *ctx, const char *path)
505 /* No absolute paths. */
506 while (IS_DIRECTORY_SEP(*path)) {
510 path_out = talloc_strdup(ctx, path);
515 p1 = strchr_m(path_out, '*');
516 p2 = strchr_m(path_out, '?');
528 /* Now go back to the start of this component. */
529 p1 = strrchr_m(path_out, '/');
530 p2 = strrchr_m(path_out, '\\');
537 /* Strip any trailing separator */
539 len = strlen(path_out);
540 if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
541 path_out[len-1] = '\0';
547 /****************************************************************************
548 ****************************************************************************/
550 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
551 struct cli_state *cli,
554 char path_sep = '\\';
556 /* Ensure the extrapath doesn't start with a separator. */
557 while (IS_DIRECTORY_SEP(*dir)) {
561 if (cli->posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
564 return talloc_asprintf(ctx, "%c%s%c%s%c%s",
573 /********************************************************************
574 check for dfs referral
575 ********************************************************************/
577 static bool cli_dfs_check_error( struct cli_state *cli, NTSTATUS status )
579 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
581 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
583 if (!((flgs2&FLAGS2_32_BIT_ERROR_CODES) &&
584 (flgs2&FLAGS2_UNICODE_STRINGS)))
587 if (NT_STATUS_EQUAL(status, NT_STATUS(IVAL(cli->inbuf,smb_rcls))))
593 /********************************************************************
594 Get the dfs referral link.
595 ********************************************************************/
597 bool cli_dfs_get_referral(TALLOC_CTX *ctx,
598 struct cli_state *cli,
600 CLIENT_DFS_REFERRAL**refs,
604 unsigned int data_len = 0;
605 unsigned int param_len = 0;
606 uint16 setup = TRANSACT2_GET_DFS_REFERRAL;
608 char *rparam=NULL, *rdata=NULL;
611 size_t pathlen = 2*(strlen(path)+1);
612 uint16 num_referrals;
613 CLIENT_DFS_REFERRAL *referrals = NULL;
619 param = SMB_MALLOC_ARRAY(char, 2+pathlen+2);
623 SSVAL(param, 0, 0x03); /* max referral level */
626 p += clistr_push(cli, p, path, pathlen, STR_TERMINATE);
627 param_len = PTR_DIFF(p, param);
629 if (!cli_send_trans(cli, SMBtrans2,
631 -1, 0, /* fid, flags */
632 &setup, 1, 0, /* setup, length, max */
633 param, param_len, 2, /* param, length, max */
634 NULL, 0, cli->max_xmit /* data, length, max */
642 if (!cli_receive_trans(cli, SMBtrans2,
644 &rdata, &data_len)) {
652 endp = rdata + data_len;
654 *consumed = SVAL(rdata, 0);
655 num_referrals = SVAL(rdata, 2);
657 if (num_referrals != 0) {
663 referrals = TALLOC_ARRAY(ctx, CLIENT_DFS_REFERRAL,
669 /* start at the referrals array */
672 for (i=0; i<num_referrals && p < endp; i++) {
676 ref_version = SVAL(p, 0);
677 ref_size = SVAL(p, 2);
678 node_offset = SVAL(p, 16);
680 if (ref_version != 3) {
685 referrals[i].proximity = SVAL(p, 8);
686 referrals[i].ttl = SVAL(p, 10);
688 if (p + node_offset > endp) {
691 clistr_pull_talloc(ctx, cli->inbuf,
692 &referrals[i].dfspath,
694 STR_TERMINATE|STR_UNICODE);
696 if (!referrals[i].dfspath) {
701 if (i < num_referrals) {
708 *num_refs = num_referrals;
718 /********************************************************************
719 ********************************************************************/
721 bool cli_resolve_path(TALLOC_CTX *ctx,
723 const struct user_auth_info *dfs_auth_info,
724 struct cli_state *rootcli,
726 struct cli_state **targetcli,
727 char **pp_targetpath)
729 CLIENT_DFS_REFERRAL *refs = NULL;
732 struct cli_state *cli_ipc = NULL;
733 char *dfs_path = NULL;
734 char *cleanpath = NULL;
735 char *extrapath = NULL;
739 struct cli_state *newcli = NULL;
740 char *newpath = NULL;
741 char *newmount = NULL;
743 SMB_STRUCT_STAT sbuf;
746 if ( !rootcli || !path || !targetcli ) {
750 /* Don't do anything if this is not a DFS root. */
752 if ( !rootcli->dfsroot) {
753 *targetcli = rootcli;
754 *pp_targetpath = talloc_strdup(ctx, path);
755 if (!*pp_targetpath) {
763 /* Send a trans2_query_path_info to check for a referral. */
765 cleanpath = clean_path(ctx, path);
770 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
775 if (cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes)) {
776 /* This is an ordinary path, just return it. */
777 *targetcli = rootcli;
778 *pp_targetpath = talloc_strdup(ctx, path);
779 if (!*pp_targetpath) {
785 /* Special case where client asked for a path that does not exist */
787 if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
788 *targetcli = rootcli;
789 *pp_targetpath = talloc_strdup(ctx, path);
790 if (!*pp_targetpath) {
796 /* We got an error, check for DFS referral. */
798 if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED)) {
802 /* Check for the referral. */
804 if (!(cli_ipc = cli_cm_open(ctx,
810 (rootcli->trans_enc_state != NULL),
817 if (!cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
818 &num_refs, &consumed) || !num_refs) {
822 /* Just store the first referral for now. */
824 if (!refs[0].dfspath) {
827 split_dfs_path(ctx, refs[0].dfspath, &server, &share, &extrapath );
829 if (!server || !share) {
833 /* Make sure to recreate the original string including any wildcards. */
835 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
839 pathlen = strlen(dfs_path)*2;
840 consumed = MIN(pathlen, consumed);
841 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed/2]);
842 if (!*pp_targetpath) {
845 dfs_path[consumed/2] = '\0';
848 * *pp_targetpath is now the unconsumed part of the path.
849 * dfs_path is now the consumed part of the path
850 * (in \server\share\path format).
853 /* Open the connection to the target server & share */
854 if ((*targetcli = cli_cm_open(ctx, rootcli,
859 (rootcli->trans_enc_state != NULL),
863 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
868 if (extrapath && strlen(extrapath) > 0) {
869 *pp_targetpath = talloc_asprintf(ctx,
873 if (!*pp_targetpath) {
878 /* parse out the consumed mount path */
879 /* trim off the \server\share\ */
883 if (*ppath != '\\') {
884 d_printf("cli_resolve_path: "
885 "dfs_path (%s) not in correct format.\n",
890 ppath++; /* Now pointing at start of server name. */
892 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
896 ppath++; /* Now pointing at start of share name. */
898 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
902 ppath++; /* Now pointing at path component. */
904 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
909 cli_set_mntpoint(*targetcli, newmount);
911 /* Check for another dfs referral, note that we are not
912 checking for loops here. */
914 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
915 if (cli_resolve_path(ctx,
923 * When cli_resolve_path returns true here it's always
924 * returning the complete path in newpath, so we're done
928 *pp_targetpath = newpath;
935 /* If returning true ensure we return a dfs root full path. */
936 if ((*targetcli)->dfsroot) {
937 dfs_path = talloc_strdup(ctx, *pp_targetpath);
941 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
947 /********************************************************************
948 ********************************************************************/
950 static bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
951 struct cli_state *cli,
952 const char *sharename,
956 const char *username,
957 const char *password,
960 CLIENT_DFS_REFERRAL *refs = NULL;
963 char *fullpath = NULL;
966 char *newextrapath = NULL;
968 if (!cli || !sharename) {
974 /* special case. never check for a referral on the IPC$ share */
976 if (strequal(sharename, "IPC$")) {
980 /* send a trans2_query_path_info to check for a referral */
982 fullpath = talloc_asprintf(ctx, "\\%s\\%s", cli->desthost, sharename );
987 /* check for the referral */
989 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, "IPC$", "IPC", NULL, 0))) {
994 NTSTATUS status = cli_cm_force_encryption(cli,
999 if (!NT_STATUS_IS_OK(status)) {
1004 res = cli_dfs_get_referral(ctx, cli, fullpath, &refs, &num_refs, &consumed);
1006 if (!cli_tdis(cli)) {
1012 if (!res || !num_refs) {
1016 if (!refs[0].dfspath) {
1020 split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1021 pp_newshare, &newextrapath );
1023 if ((*pp_newserver == NULL) || (*pp_newshare == NULL)) {
1027 /* check that this is not a self-referral */
1029 if (strequal(cli->desthost, *pp_newserver) &&
1030 strequal(sharename, *pp_newshare)) {