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