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