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