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