957bf4d707ccd913870eab45451c74c13e9c5066
[kai/samba.git] / source3 / libsmb / clidfs.c
1 /*
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
7
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.
12
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.
17
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/>.
20 */
21
22 #include "includes.h"
23 #include "libsmb/clirap.h"
24 #include "msdfs.h"
25
26 /********************************************************************
27  Important point.
28
29  DFS paths are *always* of the form \server\share\<pathname> (the \ characters
30  are not C escaped here).
31
32  - but if we're using POSIX paths then <pathname> may contain
33    '/' separators, not '\\' separators. So cope with '\\' or '/'
34    as a separator when looking at the pathname part.... JRA.
35 ********************************************************************/
36
37 /********************************************************************
38  Ensure a connection is encrypted.
39 ********************************************************************/
40
41 NTSTATUS cli_cm_force_encryption(struct cli_state *c,
42                         const char *username,
43                         const char *password,
44                         const char *domain,
45                         const char *sharename)
46 {
47         NTSTATUS status = cli_force_encryption(c,
48                                         username,
49                                         password,
50                                         domain);
51
52         if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED)) {
53                 d_printf("Encryption required and "
54                         "server that doesn't support "
55                         "UNIX extensions - failing connect\n");
56         } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNKNOWN_REVISION)) {
57                 d_printf("Encryption required and "
58                         "can't get UNIX CIFS extensions "
59                         "version from server.\n");
60         } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNSUPPORTED_COMPRESSION)) {
61                 d_printf("Encryption required and "
62                         "share %s doesn't support "
63                         "encryption.\n", sharename);
64         } else if (!NT_STATUS_IS_OK(status)) {
65                 d_printf("Encryption required and "
66                         "setup failed with error %s.\n",
67                         nt_errstr(status));
68         }
69
70         return status;
71 }
72
73 /********************************************************************
74  Return a connection to a server.
75 ********************************************************************/
76
77 static struct cli_state *do_connect(TALLOC_CTX *ctx,
78                                         const char *server,
79                                         const char *share,
80                                         const struct user_auth_info *auth_info,
81                                         bool show_sessetup,
82                                         bool force_encrypt,
83                                         int max_protocol,
84                                         int port,
85                                         int name_type)
86 {
87         struct cli_state *c = NULL;
88         struct nmb_name called, calling;
89         const char *called_str;
90         const char *server_n;
91         struct sockaddr_storage ss;
92         char *servicename;
93         char *sharename;
94         char *newserver, *newshare;
95         const char *username;
96         const char *password;
97         NTSTATUS status;
98
99         /* make a copy so we don't modify the global string 'service' */
100         servicename = talloc_strdup(ctx,share);
101         if (!servicename) {
102                 return NULL;
103         }
104         sharename = servicename;
105         if (*sharename == '\\') {
106                 sharename += 2;
107                 called_str = sharename;
108                 if (server == NULL) {
109                         server = sharename;
110                 }
111                 sharename = strchr_m(sharename,'\\');
112                 if (!sharename) {
113                         return NULL;
114                 }
115                 *sharename = 0;
116                 sharename++;
117         } else {
118                 called_str = server;
119         }
120
121         server_n = server;
122
123         zero_sockaddr(&ss);
124
125         make_nmb_name(&calling, global_myname(), 0x0);
126         make_nmb_name(&called , called_str, name_type);
127
128  again:
129         zero_sockaddr(&ss);
130
131         /* have to open a new connection */
132         c = cli_initialise_ex(get_cmdline_auth_info_signing_state(auth_info));
133         if (c == NULL) {
134                 d_printf("Connection to %s failed\n", server_n);
135                 return NULL;
136         }
137         if (port) {
138                 cli_set_port(c, port);
139         }
140
141         status = cli_connect(c, server_n, &ss);
142         if (!NT_STATUS_IS_OK(status)) {
143                 d_printf("Connection to %s failed (Error %s)\n",
144                                 server_n,
145                                 nt_errstr(status));
146                 cli_shutdown(c);
147                 return NULL;
148         }
149
150         if (max_protocol == 0) {
151                 max_protocol = PROTOCOL_NT1;
152         }
153         c->protocol = max_protocol;
154         c->use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
155         c->fallback_after_kerberos =
156                 get_cmdline_auth_info_fallback_after_kerberos(auth_info);
157         c->use_ccache = get_cmdline_auth_info_use_ccache(auth_info);
158
159         if (!cli_session_request(c, &calling, &called)) {
160                 char *p;
161                 d_printf("session request to %s failed (%s)\n",
162                          called.name, cli_errstr(c));
163                 cli_shutdown(c);
164                 c = NULL;
165                 if ((p=strchr_m(called.name, '.'))) {
166                         *p = 0;
167                         goto again;
168                 }
169                 if (strcmp(called.name, "*SMBSERVER")) {
170                         make_nmb_name(&called , "*SMBSERVER", 0x20);
171                         goto again;
172                 }
173                 return NULL;
174         }
175
176         DEBUG(4,(" session request ok\n"));
177
178         status = cli_negprot(c);
179
180         if (!NT_STATUS_IS_OK(status)) {
181                 d_printf("protocol negotiation failed: %s\n",
182                          nt_errstr(status));
183                 cli_shutdown(c);
184                 return NULL;
185         }
186
187         username = get_cmdline_auth_info_username(auth_info);
188         password = get_cmdline_auth_info_password(auth_info);
189
190         if (!NT_STATUS_IS_OK(cli_session_setup(c, username,
191                                                password, strlen(password),
192                                                password, strlen(password),
193                                                lp_workgroup()))) {
194                 /* If a password was not supplied then
195                  * try again with a null username. */
196                 if (password[0] || !username[0] ||
197                         get_cmdline_auth_info_use_kerberos(auth_info) ||
198                         !NT_STATUS_IS_OK(cli_session_setup(c, "",
199                                                 "", 0,
200                                                 "", 0,
201                                                lp_workgroup()))) {
202                         d_printf("session setup failed: %s\n", cli_errstr(c));
203                         if (NT_STATUS_V(cli_nt_error(c)) ==
204                             NT_STATUS_V(NT_STATUS_MORE_PROCESSING_REQUIRED))
205                                 d_printf("did you forget to run kinit?\n");
206                         cli_shutdown(c);
207                         return NULL;
208                 }
209                 d_printf("Anonymous login successful\n");
210                 status = cli_init_creds(c, "", lp_workgroup(), "");
211         } else {
212                 status = cli_init_creds(c, username, lp_workgroup(), password);
213         }
214
215         if (!NT_STATUS_IS_OK(status)) {
216                 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status)));
217                 cli_shutdown(c);
218                 return NULL;
219         }
220
221         if ( show_sessetup ) {
222                 if (*c->server_domain) {
223                         DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
224                                 c->server_domain,c->server_os,c->server_type));
225                 } else if (*c->server_os || *c->server_type) {
226                         DEBUG(0,("OS=[%s] Server=[%s]\n",
227                                  c->server_os,c->server_type));
228                 }
229         }
230         DEBUG(4,(" session setup ok\n"));
231
232         /* here's the fun part....to support 'msdfs proxy' shares
233            (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
234            here before trying to connect to the original share.
235            cli_check_msdfs_proxy() will fail if it is a normal share. */
236
237         if ((c->capabilities & CAP_DFS) &&
238                         cli_check_msdfs_proxy(ctx, c, sharename,
239                                 &newserver, &newshare,
240                                 force_encrypt,
241                                 username,
242                                 password,
243                                 lp_workgroup())) {
244                 cli_shutdown(c);
245                 return do_connect(ctx, newserver,
246                                 newshare, auth_info, false,
247                                 force_encrypt, max_protocol,
248                                 port, name_type);
249         }
250
251         /* must be a normal share */
252
253         status = cli_tcon_andx(c, sharename, "?????",
254                                password, strlen(password)+1);
255         if (!NT_STATUS_IS_OK(status)) {
256                 d_printf("tree connect failed: %s\n", nt_errstr(status));
257                 cli_shutdown(c);
258                 return NULL;
259         }
260
261         if (force_encrypt) {
262                 status = cli_cm_force_encryption(c,
263                                         username,
264                                         password,
265                                         lp_workgroup(),
266                                         sharename);
267                 if (!NT_STATUS_IS_OK(status)) {
268                         cli_shutdown(c);
269                         return NULL;
270                 }
271         }
272
273         DEBUG(4,(" tconx ok\n"));
274         return c;
275 }
276
277 /****************************************************************************
278 ****************************************************************************/
279
280 static void cli_set_mntpoint(struct cli_state *cli, const char *mnt)
281 {
282         char *name = clean_name(NULL, mnt);
283         if (!name) {
284                 return;
285         }
286         TALLOC_FREE(cli->dfs_mountpoint);
287         cli->dfs_mountpoint = talloc_strdup(cli, name);
288         TALLOC_FREE(name);
289 }
290
291 /********************************************************************
292  Add a new connection to the list.
293  referring_cli == NULL means a new initial connection.
294 ********************************************************************/
295
296 static struct cli_state *cli_cm_connect(TALLOC_CTX *ctx,
297                                         struct cli_state *referring_cli,
298                                         const char *server,
299                                         const char *share,
300                                         const struct user_auth_info *auth_info,
301                                         bool show_hdr,
302                                         bool force_encrypt,
303                                         int max_protocol,
304                                         int port,
305                                         int name_type)
306 {
307         struct cli_state *cli;
308
309         cli = do_connect(ctx, server, share,
310                                 auth_info,
311                                 show_hdr, force_encrypt, max_protocol,
312                                 port, name_type);
313
314         if (!cli ) {
315                 return NULL;
316         }
317
318         /* Enter into the list. */
319         if (referring_cli) {
320                 DLIST_ADD_END(referring_cli, cli, struct cli_state *);
321         }
322
323         if (referring_cli && referring_cli->requested_posix_capabilities) {
324                 uint16 major, minor;
325                 uint32 caplow, caphigh;
326                 NTSTATUS status;
327                 status = cli_unix_extensions_version(cli, &major, &minor,
328                                                      &caplow, &caphigh);
329                 if (NT_STATUS_IS_OK(status)) {
330                         cli_set_unix_extensions_capabilities(cli,
331                                         major, minor,
332                                         caplow, caphigh);
333                 }
334         }
335
336         return cli;
337 }
338
339 /********************************************************************
340  Return a connection to a server on a particular share.
341 ********************************************************************/
342
343 static struct cli_state *cli_cm_find(struct cli_state *cli,
344                                 const char *server,
345                                 const char *share)
346 {
347         struct cli_state *p;
348
349         if (cli == NULL) {
350                 return NULL;
351         }
352
353         /* Search to the start of the list. */
354         for (p = cli; p; p = DLIST_PREV(p)) {
355                 if (strequal(server, p->desthost) &&
356                                 strequal(share,p->share)) {
357                         return p;
358                 }
359         }
360
361         /* Search to the end of the list. */
362         for (p = cli->next; p; p = p->next) {
363                 if (strequal(server, p->desthost) &&
364                                 strequal(share,p->share)) {
365                         return p;
366                 }
367         }
368
369         return NULL;
370 }
371
372 /****************************************************************************
373  Open a client connection to a \\server\share.
374 ****************************************************************************/
375
376 struct cli_state *cli_cm_open(TALLOC_CTX *ctx,
377                                 struct cli_state *referring_cli,
378                                 const char *server,
379                                 const char *share,
380                                 const struct user_auth_info *auth_info,
381                                 bool show_hdr,
382                                 bool force_encrypt,
383                                 int max_protocol,
384                                 int port,
385                                 int name_type)
386 {
387         /* Try to reuse an existing connection in this list. */
388         struct cli_state *c = cli_cm_find(referring_cli, server, share);
389
390         if (c) {
391                 return c;
392         }
393
394         if (auth_info == NULL) {
395                 /* Can't do a new connection
396                  * without auth info. */
397                 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
398                         "without auth info\n",
399                         server, share );
400                 return NULL;
401         }
402
403         return cli_cm_connect(ctx,
404                                 referring_cli,
405                                 server,
406                                 share,
407                                 auth_info,
408                                 show_hdr,
409                                 force_encrypt,
410                                 max_protocol,
411                                 port,
412                                 name_type);
413 }
414
415 /****************************************************************************
416 ****************************************************************************/
417
418 void cli_cm_display(const struct cli_state *cli)
419 {
420         int i;
421
422         for (i=0; cli; cli = cli->next,i++ ) {
423                 d_printf("%d:\tserver=%s, share=%s\n",
424                         i, cli->desthost, cli->share );
425         }
426 }
427
428 /****************************************************************************
429 ****************************************************************************/
430
431 /****************************************************************************
432 ****************************************************************************/
433
434 #if 0
435 void cli_cm_set_credentials(struct user_auth_info *auth_info)
436 {
437         SAFE_FREE(cm_creds.username);
438         cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
439                                                auth_info));
440
441         if (get_cmdline_auth_info_got_pass(auth_info)) {
442                 cm_set_password(get_cmdline_auth_info_password(auth_info));
443         }
444
445         cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
446         cm_creds.fallback_after_kerberos = false;
447         cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
448 }
449 #endif
450
451 /**********************************************************************
452  split a dfs path into the server, share name, and extrapath components
453 **********************************************************************/
454
455 static bool split_dfs_path(TALLOC_CTX *ctx,
456                                 const char *nodepath,
457                                 char **pp_server,
458                                 char **pp_share,
459                                 char **pp_extrapath)
460 {
461         char *p, *q;
462         char *path;
463
464         *pp_server = NULL;
465         *pp_share = NULL;
466         *pp_extrapath = NULL;
467
468         path = talloc_strdup(ctx, nodepath);
469         if (!path) {
470                 goto fail;
471         }
472
473         if ( path[0] != '\\' ) {
474                 goto fail;
475         }
476
477         p = strchr_m( path + 1, '\\' );
478         if ( !p ) {
479                 goto fail;
480         }
481
482         *p = '\0';
483         p++;
484
485         /* Look for any extra/deep path */
486         q = strchr_m(p, '\\');
487         if (q != NULL) {
488                 *q = '\0';
489                 q++;
490                 *pp_extrapath = talloc_strdup(ctx, q);
491         } else {
492                 *pp_extrapath = talloc_strdup(ctx, "");
493         }
494         if (*pp_extrapath == NULL) {
495                 goto fail;
496         }
497
498         *pp_share = talloc_strdup(ctx, p);
499         if (*pp_share == NULL) {
500                 goto fail;
501         }
502
503         *pp_server = talloc_strdup(ctx, &path[1]);
504         if (*pp_server == NULL) {
505                 goto fail;
506         }
507
508         TALLOC_FREE(path);
509         return true;
510
511 fail:
512         TALLOC_FREE(*pp_share);
513         TALLOC_FREE(*pp_extrapath);
514         TALLOC_FREE(path);
515         return false;
516 }
517
518 /****************************************************************************
519  Return the original path truncated at the directory component before
520  the first wildcard character. Trust the caller to provide a NULL
521  terminated string
522 ****************************************************************************/
523
524 static char *clean_path(TALLOC_CTX *ctx, const char *path)
525 {
526         size_t len;
527         char *p1, *p2, *p;
528         char *path_out;
529
530         /* No absolute paths. */
531         while (IS_DIRECTORY_SEP(*path)) {
532                 path++;
533         }
534
535         path_out = talloc_strdup(ctx, path);
536         if (!path_out) {
537                 return NULL;
538         }
539
540         p1 = strchr_m(path_out, '*');
541         p2 = strchr_m(path_out, '?');
542
543         if (p1 || p2) {
544                 if (p1 && p2) {
545                         p = MIN(p1,p2);
546                 } else if (!p1) {
547                         p = p2;
548                 } else {
549                         p = p1;
550                 }
551                 *p = '\0';
552
553                 /* Now go back to the start of this component. */
554                 p1 = strrchr_m(path_out, '/');
555                 p2 = strrchr_m(path_out, '\\');
556                 p = MAX(p1,p2);
557                 if (p) {
558                         *p = '\0';
559                 }
560         }
561
562         /* Strip any trailing separator */
563
564         len = strlen(path_out);
565         if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
566                 path_out[len-1] = '\0';
567         }
568
569         return path_out;
570 }
571
572 /****************************************************************************
573 ****************************************************************************/
574
575 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
576                                         struct cli_state *cli,
577                                         const char *dir)
578 {
579         char path_sep = '\\';
580
581         /* Ensure the extrapath doesn't start with a separator. */
582         while (IS_DIRECTORY_SEP(*dir)) {
583                 dir++;
584         }
585
586         if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
587                 path_sep = '/';
588         }
589         return talloc_asprintf(ctx, "%c%s%c%s%c%s",
590                         path_sep,
591                         cli->desthost,
592                         path_sep,
593                         cli->share,
594                         path_sep,
595                         dir);
596 }
597
598 /********************************************************************
599  check for dfs referral
600 ********************************************************************/
601
602 static bool cli_dfs_check_error(struct cli_state *cli, NTSTATUS expected,
603                                 NTSTATUS status)
604 {
605         /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
606
607         if (!(cli->capabilities & CAP_UNICODE)) {
608                 return false;
609         }
610         if (!(cli->capabilities & CAP_STATUS32)) {
611                 return false;
612         }
613         if (NT_STATUS_EQUAL(status, expected)) {
614                 return true;
615         }
616         return false;
617 }
618
619 /********************************************************************
620  Get the dfs referral link.
621 ********************************************************************/
622
623 NTSTATUS cli_dfs_get_referral(TALLOC_CTX *ctx,
624                         struct cli_state *cli,
625                         const char *path,
626                         struct client_dfs_referral **refs,
627                         size_t *num_refs,
628                         size_t *consumed)
629 {
630         unsigned int data_len = 0;
631         unsigned int param_len = 0;
632         uint16 setup[1];
633         uint8_t *param = NULL;
634         uint8_t *rdata = NULL;
635         char *p;
636         char *endp;
637         size_t pathlen = 2*(strlen(path)+1);
638         smb_ucs2_t *path_ucs;
639         char *consumed_path = NULL;
640         uint16_t consumed_ucs;
641         uint16 num_referrals;
642         struct client_dfs_referral *referrals = NULL;
643         NTSTATUS status;
644
645         *num_refs = 0;
646         *refs = NULL;
647
648         SSVAL(setup, 0, TRANSACT2_GET_DFS_REFERRAL);
649
650         param = SMB_MALLOC_ARRAY(uint8_t, 2+pathlen+2);
651         if (!param) {
652                 status = NT_STATUS_NO_MEMORY;
653                 goto out;
654         }
655         SSVAL(param, 0, 0x03);  /* max referral level */
656         p = (char *)(&param[2]);
657
658         path_ucs = (smb_ucs2_t *)p;
659         p += clistr_push(cli, p, path, pathlen, STR_TERMINATE);
660         param_len = PTR_DIFF(p, param);
661
662         status = cli_trans(talloc_tos(), cli, SMBtrans2,
663                            NULL, 0xffff, 0, 0,
664                            setup, 1, 0,
665                            param, param_len, 2,
666                            NULL, 0, cli->max_xmit,
667                            NULL,
668                            NULL, 0, NULL, /* rsetup */
669                            NULL, 0, NULL,
670                            &rdata, 4, &data_len);
671         if (!NT_STATUS_IS_OK(status)) {
672                 goto out;
673         }
674         if (data_len < 4) {
675                 goto out;
676         }
677
678         endp = (char *)rdata + data_len;
679
680         consumed_ucs  = SVAL(rdata, 0);
681         num_referrals = SVAL(rdata, 2);
682
683         /* consumed_ucs is the number of bytes
684          * of the UCS2 path consumed not counting any
685          * terminating null. We need to convert
686          * back to unix charset and count again
687          * to get the number of bytes consumed from
688          * the incoming path. */
689
690         if (pull_string_talloc(talloc_tos(),
691                         NULL,
692                         0,
693                         &consumed_path,
694                         path_ucs,
695                         consumed_ucs,
696                         STR_UNICODE) == 0) {
697                 goto out;
698         }
699         if (consumed_path == NULL) {
700                 goto out;
701         }
702         *consumed = strlen(consumed_path);
703
704         if (num_referrals != 0) {
705                 uint16 ref_version;
706                 uint16 ref_size;
707                 int i;
708                 uint16 node_offset;
709
710                 referrals = talloc_array(ctx, struct client_dfs_referral,
711                                          num_referrals);
712
713                 if (!referrals) {
714                         goto out;
715                 }
716                 /* start at the referrals array */
717
718                 p = (char *)rdata+8;
719                 for (i=0; i<num_referrals && p < endp; i++) {
720                         if (p + 18 > endp) {
721                                 goto out;
722                         }
723                         ref_version = SVAL(p, 0);
724                         ref_size    = SVAL(p, 2);
725                         node_offset = SVAL(p, 16);
726
727                         if (ref_version != 3) {
728                                 p += ref_size;
729                                 continue;
730                         }
731
732                         referrals[i].proximity = SVAL(p, 8);
733                         referrals[i].ttl       = SVAL(p, 10);
734
735                         if (p + node_offset > endp) {
736                                 goto out;
737                         }
738                         clistr_pull_talloc(ctx, cli->inbuf,
739                                            SVAL(cli->inbuf, smb_flg2),
740                                            &referrals[i].dfspath,
741                                            p+node_offset,
742                                            cli->bufsize - ((p+node_offset)-cli->inbuf),
743                                            STR_TERMINATE|STR_UNICODE);
744
745                         if (!referrals[i].dfspath) {
746                                 goto out;
747                         }
748                         p += ref_size;
749                 }
750                 if (i < num_referrals) {
751                         goto out;
752                 }
753         }
754
755         *num_refs = num_referrals;
756         *refs = referrals;
757
758   out:
759
760         TALLOC_FREE(consumed_path);
761         SAFE_FREE(param);
762         TALLOC_FREE(rdata);
763         return status;
764 }
765
766 /********************************************************************
767 ********************************************************************/
768
769 bool cli_resolve_path(TALLOC_CTX *ctx,
770                         const char *mountpt,
771                         const struct user_auth_info *dfs_auth_info,
772                         struct cli_state *rootcli,
773                         const char *path,
774                         struct cli_state **targetcli,
775                         char **pp_targetpath)
776 {
777         struct client_dfs_referral *refs = NULL;
778         size_t num_refs = 0;
779         size_t consumed = 0;
780         struct cli_state *cli_ipc = NULL;
781         char *dfs_path = NULL;
782         char *cleanpath = NULL;
783         char *extrapath = NULL;
784         int pathlen;
785         char *server = NULL;
786         char *share = NULL;
787         struct cli_state *newcli = NULL;
788         char *newpath = NULL;
789         char *newmount = NULL;
790         char *ppath = NULL;
791         SMB_STRUCT_STAT sbuf;
792         uint32 attributes;
793         NTSTATUS status;
794
795         if ( !rootcli || !path || !targetcli ) {
796                 return false;
797         }
798
799         /* Don't do anything if this is not a DFS root. */
800
801         if ( !rootcli->dfsroot) {
802                 *targetcli = rootcli;
803                 *pp_targetpath = talloc_strdup(ctx, path);
804                 if (!*pp_targetpath) {
805                         return false;
806                 }
807                 return true;
808         }
809
810         *targetcli = NULL;
811
812         /* Send a trans2_query_path_info to check for a referral. */
813
814         cleanpath = clean_path(ctx, path);
815         if (!cleanpath) {
816                 return false;
817         }
818
819         dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
820         if (!dfs_path) {
821                 return false;
822         }
823
824         status = cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes);
825         if (NT_STATUS_IS_OK(status)) {
826                 /* This is an ordinary path, just return it. */
827                 *targetcli = rootcli;
828                 *pp_targetpath = talloc_strdup(ctx, path);
829                 if (!*pp_targetpath) {
830                         return false;
831                 }
832                 goto done;
833         }
834
835         /* Special case where client asked for a path that does not exist */
836
837         if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND,
838                                 status)) {
839                 *targetcli = rootcli;
840                 *pp_targetpath = talloc_strdup(ctx, path);
841                 if (!*pp_targetpath) {
842                         return false;
843                 }
844                 goto done;
845         }
846
847         /* We got an error, check for DFS referral. */
848
849         if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED,
850                                  status)) {
851                 return false;
852         }
853
854         /* Check for the referral. */
855
856         if (!(cli_ipc = cli_cm_open(ctx,
857                                 rootcli,
858                                 rootcli->desthost,
859                                 "IPC$",
860                                 dfs_auth_info,
861                                 false,
862                                 (rootcli->trans_enc_state != NULL),
863                                 rootcli->protocol,
864                                 0,
865                                 0x20))) {
866                 return false;
867         }
868
869         status = cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
870                                       &num_refs, &consumed);
871         if (!NT_STATUS_IS_OK(status) || !num_refs) {
872                 return false;
873         }
874
875         /* Just store the first referral for now. */
876
877         if (!refs[0].dfspath) {
878                 return false;
879         }
880         if (!split_dfs_path(ctx, refs[0].dfspath, &server, &share,
881                             &extrapath)) {
882                 return false;
883         }
884
885         /* Make sure to recreate the original string including any wildcards. */
886
887         dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
888         if (!dfs_path) {
889                 return false;
890         }
891         pathlen = strlen(dfs_path);
892         consumed = MIN(pathlen, consumed);
893         *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed]);
894         if (!*pp_targetpath) {
895                 return false;
896         }
897         dfs_path[consumed] = '\0';
898
899         /*
900          * *pp_targetpath is now the unconsumed part of the path.
901          * dfs_path is now the consumed part of the path
902          * (in \server\share\path format).
903          */
904
905         /* Open the connection to the target server & share */
906         if ((*targetcli = cli_cm_open(ctx, rootcli,
907                                         server,
908                                         share,
909                                         dfs_auth_info,
910                                         false,
911                                         (rootcli->trans_enc_state != NULL),
912                                         rootcli->protocol,
913                                         0,
914                                         0x20)) == NULL) {
915                 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
916                         server, share );
917                 return false;
918         }
919
920         if (extrapath && strlen(extrapath) > 0) {
921                 *pp_targetpath = talloc_asprintf(ctx,
922                                                 "%s%s",
923                                                 extrapath,
924                                                 *pp_targetpath);
925                 if (!*pp_targetpath) {
926                         return false;
927                 }
928         }
929
930         /* parse out the consumed mount path */
931         /* trim off the \server\share\ */
932
933         ppath = dfs_path;
934
935         if (*ppath != '\\') {
936                 d_printf("cli_resolve_path: "
937                         "dfs_path (%s) not in correct format.\n",
938                         dfs_path );
939                 return false;
940         }
941
942         ppath++; /* Now pointing at start of server name. */
943
944         if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
945                 return false;
946         }
947
948         ppath++; /* Now pointing at start of share name. */
949
950         if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
951                 return false;
952         }
953
954         ppath++; /* Now pointing at path component. */
955
956         newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
957         if (!newmount) {
958                 return false;
959         }
960
961         cli_set_mntpoint(*targetcli, newmount);
962
963         /* Check for another dfs referral, note that we are not
964            checking for loops here. */
965
966         if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
967                 if (cli_resolve_path(ctx,
968                                         newmount,
969                                         dfs_auth_info,
970                                         *targetcli,
971                                         *pp_targetpath,
972                                         &newcli,
973                                         &newpath)) {
974                         /*
975                          * When cli_resolve_path returns true here it's always
976                          * returning the complete path in newpath, so we're done
977                          * here.
978                          */
979                         *targetcli = newcli;
980                         *pp_targetpath = newpath;
981                         return true;
982                 }
983         }
984
985   done:
986
987         /* If returning true ensure we return a dfs root full path. */
988         if ((*targetcli)->dfsroot) {
989                 dfs_path = talloc_strdup(ctx, *pp_targetpath);
990                 if (!dfs_path) {
991                         return false;
992                 }
993                 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
994         }
995
996         return true;
997 }
998
999 /********************************************************************
1000 ********************************************************************/
1001
1002 bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
1003                                 struct cli_state *cli,
1004                                 const char *sharename,
1005                                 char **pp_newserver,
1006                                 char **pp_newshare,
1007                                 bool force_encrypt,
1008                                 const char *username,
1009                                 const char *password,
1010                                 const char *domain)
1011 {
1012         struct client_dfs_referral *refs = NULL;
1013         size_t num_refs = 0;
1014         size_t consumed = 0;
1015         char *fullpath = NULL;
1016         bool res;
1017         uint16 cnum;
1018         char *newextrapath = NULL;
1019         NTSTATUS status;
1020
1021         if (!cli || !sharename) {
1022                 return false;
1023         }
1024
1025         cnum = cli->cnum;
1026
1027         /* special case.  never check for a referral on the IPC$ share */
1028
1029         if (strequal(sharename, "IPC$")) {
1030                 return false;
1031         }
1032
1033         /* send a trans2_query_path_info to check for a referral */
1034
1035         fullpath = talloc_asprintf(ctx, "\\%s\\%s", cli->desthost, sharename );
1036         if (!fullpath) {
1037                 return false;
1038         }
1039
1040         /* check for the referral */
1041
1042         if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, "IPC$", "IPC", NULL, 0))) {
1043                 return false;
1044         }
1045
1046         if (force_encrypt) {
1047                 status = cli_cm_force_encryption(cli,
1048                                         username,
1049                                         password,
1050                                         lp_workgroup(),
1051                                         "IPC$");
1052                 if (!NT_STATUS_IS_OK(status)) {
1053                         return false;
1054                 }
1055         }
1056
1057         status = cli_dfs_get_referral(ctx, cli, fullpath, &refs,
1058                                       &num_refs, &consumed);
1059         res = NT_STATUS_IS_OK(status);
1060
1061         status = cli_tdis(cli);
1062         if (!NT_STATUS_IS_OK(status)) {
1063                 return false;
1064         }
1065
1066         cli->cnum = cnum;
1067
1068         if (!res || !num_refs) {
1069                 return false;
1070         }
1071
1072         if (!refs[0].dfspath) {
1073                 return false;
1074         }
1075
1076         if (!split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1077                             pp_newshare, &newextrapath)) {
1078                 return false;
1079         }
1080
1081         /* check that this is not a self-referral */
1082
1083         if (strequal(cli->desthost, *pp_newserver) &&
1084                         strequal(sharename, *pp_newshare)) {
1085                 return false;
1086         }
1087
1088         return true;
1089 }