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