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