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