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