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