2 Unix SMB/CIFS implementation.
3 client connect/disconnect routines
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Andrew Bartlett 2001-2003
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "../libcli/auth/libcli_auth.h"
23 #include "../libcli/auth/spnego.h"
29 {PROTOCOL_CORE
, "PC NETWORK PROGRAM 1.0"},
30 {PROTOCOL_COREPLUS
, "MICROSOFT NETWORKS 1.03"},
31 {PROTOCOL_LANMAN1
, "MICROSOFT NETWORKS 3.0"},
32 {PROTOCOL_LANMAN1
, "LANMAN1.0"},
33 {PROTOCOL_LANMAN2
, "LM1.2X002"},
34 {PROTOCOL_LANMAN2
, "DOS LANMAN2.1"},
35 {PROTOCOL_LANMAN2
, "LANMAN2.1"},
36 {PROTOCOL_LANMAN2
, "Samba"},
37 {PROTOCOL_NT1
, "NT LANMAN 1.0"},
38 {PROTOCOL_NT1
, "NT LM 0.12"},
41 #define STAR_SMBSERVER "*SMBSERVER"
44 * Set the user session key for a connection
45 * @param cli The cli structure to add it too
46 * @param session_key The session key used. (A copy of this is taken for the cli struct)
50 static void cli_set_session_key (struct cli_state
*cli
, const DATA_BLOB session_key
)
52 cli
->user_session_key
= data_blob(session_key
.data
, session_key
.length
);
55 /****************************************************************************
56 Do an old lanman2 style session setup.
57 ****************************************************************************/
59 static NTSTATUS
cli_session_setup_lanman2(struct cli_state
*cli
,
61 const char *pass
, size_t passlen
,
62 const char *workgroup
)
64 DATA_BLOB session_key
= data_blob_null
;
65 DATA_BLOB lm_response
= data_blob_null
;
70 if (passlen
> sizeof(pword
)-1) {
71 return NT_STATUS_INVALID_PARAMETER
;
74 /* LANMAN servers predate NT status codes and Unicode and ignore those
75 smb flags so we must disable the corresponding default capabilities
76 that would otherwise cause the Unicode and NT Status flags to be
77 set (and even returned by the server) */
79 cli
->capabilities
&= ~(CAP_UNICODE
| CAP_STATUS32
);
81 /* if in share level security then don't send a password now */
82 if (!(cli
->sec_mode
& NEGOTIATE_SECURITY_USER_LEVEL
))
85 if (passlen
> 0 && (cli
->sec_mode
& NEGOTIATE_SECURITY_CHALLENGE_RESPONSE
) && passlen
!= 24) {
86 /* Encrypted mode needed, and non encrypted password supplied. */
87 lm_response
= data_blob(NULL
, 24);
88 if (!SMBencrypt(pass
, cli
->secblob
.data
,(uchar
*)lm_response
.data
)) {
89 DEBUG(1, ("Password is > 14 chars in length, and is therefore incompatible with Lanman authentication\n"));
90 return NT_STATUS_ACCESS_DENIED
;
92 } else if ((cli
->sec_mode
& NEGOTIATE_SECURITY_CHALLENGE_RESPONSE
) && passlen
== 24) {
93 /* Encrypted mode needed, and encrypted password supplied. */
94 lm_response
= data_blob(pass
, passlen
);
95 } else if (passlen
> 0) {
96 /* Plaintext mode needed, assume plaintext supplied. */
97 passlen
= clistr_push(cli
, pword
, pass
, sizeof(pword
), STR_TERMINATE
);
98 lm_response
= data_blob(pass
, passlen
);
101 /* send a session setup command */
102 memset(cli
->outbuf
,'\0',smb_size
);
103 cli_set_message(cli
->outbuf
,10, 0, True
);
104 SCVAL(cli
->outbuf
,smb_com
,SMBsesssetupX
);
105 cli_setup_packet(cli
);
107 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
108 SSVAL(cli
->outbuf
,smb_vwv2
,cli
->max_xmit
);
109 SSVAL(cli
->outbuf
,smb_vwv3
,2);
110 SSVAL(cli
->outbuf
,smb_vwv4
,1);
111 SIVAL(cli
->outbuf
,smb_vwv5
,cli
->sesskey
);
112 SSVAL(cli
->outbuf
,smb_vwv7
,lm_response
.length
);
114 p
= smb_buf(cli
->outbuf
);
115 memcpy(p
,lm_response
.data
,lm_response
.length
);
116 p
+= lm_response
.length
;
117 p
+= clistr_push(cli
, p
, user
, -1, STR_TERMINATE
|STR_UPPER
);
118 p
+= clistr_push(cli
, p
, workgroup
, -1, STR_TERMINATE
|STR_UPPER
);
119 p
+= clistr_push(cli
, p
, "Unix", -1, STR_TERMINATE
);
120 p
+= clistr_push(cli
, p
, "Samba", -1, STR_TERMINATE
);
121 cli_setup_bcc(cli
, p
);
123 if (!cli_send_smb(cli
) || !cli_receive_smb(cli
)) {
124 return cli_nt_error(cli
);
127 show_msg(cli
->inbuf
);
129 if (cli_is_error(cli
)) {
130 return cli_nt_error(cli
);
133 /* use the returned vuid from now on */
134 cli
->vuid
= SVAL(cli
->inbuf
,smb_uid
);
135 status
= cli_set_username(cli
, user
);
136 if (!NT_STATUS_IS_OK(status
)) {
140 if (session_key
.data
) {
141 /* Have plaintext orginal */
142 cli_set_session_key(cli
, session_key
);
148 /****************************************************************************
149 Work out suitable capabilities to offer the server.
150 ****************************************************************************/
152 static uint32
cli_session_setup_capabilities(struct cli_state
*cli
)
154 uint32 capabilities
= CAP_NT_SMBS
;
156 if (!cli
->force_dos_errors
)
157 capabilities
|= CAP_STATUS32
;
159 if (cli
->use_level_II_oplocks
)
160 capabilities
|= CAP_LEVEL_II_OPLOCKS
;
162 capabilities
|= (cli
->capabilities
& (CAP_UNICODE
|CAP_LARGE_FILES
|CAP_LARGE_READX
|CAP_LARGE_WRITEX
|CAP_DFS
));
166 /****************************************************************************
167 Do a NT1 guest session setup.
168 ****************************************************************************/
170 struct cli_session_setup_guest_state
{
171 struct cli_state
*cli
;
176 static void cli_session_setup_guest_done(struct tevent_req
*subreq
);
178 struct tevent_req
*cli_session_setup_guest_create(TALLOC_CTX
*mem_ctx
,
179 struct event_context
*ev
,
180 struct cli_state
*cli
,
181 struct tevent_req
**psmbreq
)
183 struct tevent_req
*req
, *subreq
;
184 struct cli_session_setup_guest_state
*state
;
188 req
= tevent_req_create(mem_ctx
, &state
,
189 struct cli_session_setup_guest_state
);
196 SCVAL(vwv
+0, 0, 0xFF);
199 SSVAL(vwv
+2, 0, CLI_BUFFER_SIZE
);
201 SSVAL(vwv
+4, 0, cli
->pid
);
202 SIVAL(vwv
+5, 0, cli
->sesskey
);
207 SIVAL(vwv
+11, 0, cli_session_setup_capabilities(cli
));
209 bytes
= talloc_array(state
, uint8_t, 0);
211 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), "", 1, /* username */
213 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), "", 1, /* workgroup */
215 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), "Unix", 5, NULL
);
216 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), "Samba", 6, NULL
);
223 state
->bytes
.iov_base
= (void *)bytes
;
224 state
->bytes
.iov_len
= talloc_get_size(bytes
);
226 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBsesssetupX
, 0, 13, vwv
,
228 if (subreq
== NULL
) {
232 tevent_req_set_callback(subreq
, cli_session_setup_guest_done
, req
);
237 struct tevent_req
*cli_session_setup_guest_send(TALLOC_CTX
*mem_ctx
,
238 struct event_context
*ev
,
239 struct cli_state
*cli
)
241 struct tevent_req
*req
, *subreq
;
244 req
= cli_session_setup_guest_create(mem_ctx
, ev
, cli
, &subreq
);
249 status
= cli_smb_req_send(subreq
);
250 if (NT_STATUS_IS_OK(status
)) {
251 tevent_req_nterror(req
, status
);
252 return tevent_req_post(req
, ev
);
257 static void cli_session_setup_guest_done(struct tevent_req
*subreq
)
259 struct tevent_req
*req
= tevent_req_callback_data(
260 subreq
, struct tevent_req
);
261 struct cli_session_setup_guest_state
*state
= tevent_req_data(
262 req
, struct cli_session_setup_guest_state
);
263 struct cli_state
*cli
= state
->cli
;
270 status
= cli_smb_recv(subreq
, 0, NULL
, NULL
, &num_bytes
, &bytes
);
271 if (!NT_STATUS_IS_OK(status
)) {
273 tevent_req_nterror(req
, status
);
277 inbuf
= (char *)cli_smb_inbuf(subreq
);
280 cli
->vuid
= SVAL(inbuf
, smb_uid
);
282 p
+= clistr_pull(inbuf
, cli
->server_os
, (char *)p
, sizeof(fstring
),
283 bytes
+num_bytes
-p
, STR_TERMINATE
);
284 p
+= clistr_pull(inbuf
, cli
->server_type
, (char *)p
, sizeof(fstring
),
285 bytes
+num_bytes
-p
, STR_TERMINATE
);
286 p
+= clistr_pull(inbuf
, cli
->server_domain
, (char *)p
, sizeof(fstring
),
287 bytes
+num_bytes
-p
, STR_TERMINATE
);
289 if (strstr(cli
->server_type
, "Samba")) {
290 cli
->is_samba
= True
;
295 status
= cli_set_username(cli
, "");
296 if (!NT_STATUS_IS_OK(status
)) {
297 tevent_req_nterror(req
, status
);
300 tevent_req_done(req
);
303 NTSTATUS
cli_session_setup_guest_recv(struct tevent_req
*req
)
305 return tevent_req_simple_recv_ntstatus(req
);
308 static NTSTATUS
cli_session_setup_guest(struct cli_state
*cli
)
310 TALLOC_CTX
*frame
= talloc_stackframe();
311 struct event_context
*ev
;
312 struct tevent_req
*req
;
313 NTSTATUS status
= NT_STATUS_OK
;
315 if (cli_has_async_calls(cli
)) {
317 * Can't use sync call while an async call is in flight
319 status
= NT_STATUS_INVALID_PARAMETER
;
323 ev
= event_context_init(frame
);
325 status
= NT_STATUS_NO_MEMORY
;
329 req
= cli_session_setup_guest_send(frame
, ev
, cli
);
331 status
= NT_STATUS_NO_MEMORY
;
335 if (!tevent_req_poll(req
, ev
)) {
336 status
= map_nt_error_from_unix(errno
);
340 status
= cli_session_setup_guest_recv(req
);
343 if (!NT_STATUS_IS_OK(status
)) {
344 cli_set_error(cli
, status
);
349 /****************************************************************************
350 Do a NT1 plaintext session setup.
351 ****************************************************************************/
353 static NTSTATUS
cli_session_setup_plaintext(struct cli_state
*cli
,
354 const char *user
, const char *pass
,
355 const char *workgroup
)
357 uint32 capabilities
= cli_session_setup_capabilities(cli
);
362 fstr_sprintf( lanman
, "Samba %s", samba_version_string());
364 memset(cli
->outbuf
, '\0', smb_size
);
365 cli_set_message(cli
->outbuf
,13,0,True
);
366 SCVAL(cli
->outbuf
,smb_com
,SMBsesssetupX
);
367 cli_setup_packet(cli
);
369 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
370 SSVAL(cli
->outbuf
,smb_vwv2
,CLI_BUFFER_SIZE
);
371 SSVAL(cli
->outbuf
,smb_vwv3
,2);
372 SSVAL(cli
->outbuf
,smb_vwv4
,cli
->pid
);
373 SIVAL(cli
->outbuf
,smb_vwv5
,cli
->sesskey
);
374 SSVAL(cli
->outbuf
,smb_vwv8
,0);
375 SIVAL(cli
->outbuf
,smb_vwv11
,capabilities
);
376 p
= smb_buf(cli
->outbuf
);
378 /* check wether to send the ASCII or UNICODE version of the password */
380 if ( (capabilities
& CAP_UNICODE
) == 0 ) {
381 p
+= clistr_push(cli
, p
, pass
, -1, STR_TERMINATE
); /* password */
382 SSVAL(cli
->outbuf
,smb_vwv7
,PTR_DIFF(p
, smb_buf(cli
->outbuf
)));
385 /* For ucs2 passwords clistr_push calls ucs2_align, which causes
386 * the space taken by the unicode password to be one byte too
387 * long (as we're on an odd byte boundary here). Reduce the
388 * count by 1 to cope with this. Fixes smbclient against NetApp
389 * servers which can't cope. Fix from
390 * bryan.kolodziej@allenlund.com in bug #3840.
392 p
+= clistr_push(cli
, p
, pass
, -1, STR_UNICODE
|STR_TERMINATE
); /* unicode password */
393 SSVAL(cli
->outbuf
,smb_vwv8
,PTR_DIFF(p
, smb_buf(cli
->outbuf
))-1);
396 p
+= clistr_push(cli
, p
, user
, -1, STR_TERMINATE
); /* username */
397 p
+= clistr_push(cli
, p
, workgroup
, -1, STR_TERMINATE
); /* workgroup */
398 p
+= clistr_push(cli
, p
, "Unix", -1, STR_TERMINATE
);
399 p
+= clistr_push(cli
, p
, lanman
, -1, STR_TERMINATE
);
400 cli_setup_bcc(cli
, p
);
402 if (!cli_send_smb(cli
) || !cli_receive_smb(cli
)) {
403 return cli_nt_error(cli
);
406 show_msg(cli
->inbuf
);
408 if (cli_is_error(cli
)) {
409 return cli_nt_error(cli
);
412 cli
->vuid
= SVAL(cli
->inbuf
,smb_uid
);
413 p
= smb_buf(cli
->inbuf
);
414 p
+= clistr_pull(cli
->inbuf
, cli
->server_os
, p
, sizeof(fstring
),
416 p
+= clistr_pull(cli
->inbuf
, cli
->server_type
, p
, sizeof(fstring
),
418 p
+= clistr_pull(cli
->inbuf
, cli
->server_domain
, p
, sizeof(fstring
),
420 status
= cli_set_username(cli
, user
);
421 if (!NT_STATUS_IS_OK(status
)) {
424 if (strstr(cli
->server_type
, "Samba")) {
425 cli
->is_samba
= True
;
431 /****************************************************************************
432 do a NT1 NTLM/LM encrypted session setup - for when extended security
434 @param cli client state to create do session setup on
436 @param pass *either* cleartext password (passlen !=24) or LM response.
437 @param ntpass NT response, implies ntpasslen >=24, implies pass is not clear
438 @param workgroup The user's domain.
439 ****************************************************************************/
441 static NTSTATUS
cli_session_setup_nt1(struct cli_state
*cli
, const char *user
,
442 const char *pass
, size_t passlen
,
443 const char *ntpass
, size_t ntpasslen
,
444 const char *workgroup
)
446 uint32 capabilities
= cli_session_setup_capabilities(cli
);
447 DATA_BLOB lm_response
= data_blob_null
;
448 DATA_BLOB nt_response
= data_blob_null
;
449 DATA_BLOB session_key
= data_blob_null
;
455 /* do nothing - guest login */
456 } else if (passlen
!= 24) {
457 if (lp_client_ntlmv2_auth()) {
458 DATA_BLOB server_chal
;
459 DATA_BLOB names_blob
;
460 server_chal
= data_blob(cli
->secblob
.data
, MIN(cli
->secblob
.length
, 8));
462 /* note that the 'workgroup' here is a best guess - we don't know
463 the server's domain at this point. The 'server name' is also
466 names_blob
= NTLMv2_generate_names_blob(NULL
, cli
->called
.name
, workgroup
);
468 if (!SMBNTLMv2encrypt(NULL
, user
, workgroup
, pass
, &server_chal
,
470 &lm_response
, &nt_response
, NULL
, &session_key
)) {
471 data_blob_free(&names_blob
);
472 data_blob_free(&server_chal
);
473 return NT_STATUS_ACCESS_DENIED
;
475 data_blob_free(&names_blob
);
476 data_blob_free(&server_chal
);
480 E_md4hash(pass
, nt_hash
);
483 nt_response
= data_blob_null
;
485 nt_response
= data_blob(NULL
, 24);
486 SMBNTencrypt(pass
,cli
->secblob
.data
,nt_response
.data
);
488 /* non encrypted password supplied. Ignore ntpass. */
489 if (lp_client_lanman_auth()) {
490 lm_response
= data_blob(NULL
, 24);
491 if (!SMBencrypt(pass
,cli
->secblob
.data
, lm_response
.data
)) {
492 /* Oops, the LM response is invalid, just put
493 the NT response there instead */
494 data_blob_free(&lm_response
);
495 lm_response
= data_blob(nt_response
.data
, nt_response
.length
);
498 /* LM disabled, place NT# in LM field instead */
499 lm_response
= data_blob(nt_response
.data
, nt_response
.length
);
502 session_key
= data_blob(NULL
, 16);
504 E_deshash(pass
, session_key
.data
);
505 memset(&session_key
.data
[8], '\0', 8);
507 SMBsesskeygen_ntv1(nt_hash
, session_key
.data
);
510 cli_temp_set_signing(cli
);
512 /* pre-encrypted password supplied. Only used for
513 security=server, can't do
514 signing because we don't have original key */
516 lm_response
= data_blob(pass
, passlen
);
517 nt_response
= data_blob(ntpass
, ntpasslen
);
520 /* send a session setup command */
521 memset(cli
->outbuf
,'\0',smb_size
);
523 cli_set_message(cli
->outbuf
,13,0,True
);
524 SCVAL(cli
->outbuf
,smb_com
,SMBsesssetupX
);
525 cli_setup_packet(cli
);
527 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
528 SSVAL(cli
->outbuf
,smb_vwv2
,CLI_BUFFER_SIZE
);
529 SSVAL(cli
->outbuf
,smb_vwv3
,2);
530 SSVAL(cli
->outbuf
,smb_vwv4
,cli
->pid
);
531 SIVAL(cli
->outbuf
,smb_vwv5
,cli
->sesskey
);
532 SSVAL(cli
->outbuf
,smb_vwv7
,lm_response
.length
);
533 SSVAL(cli
->outbuf
,smb_vwv8
,nt_response
.length
);
534 SIVAL(cli
->outbuf
,smb_vwv11
,capabilities
);
535 p
= smb_buf(cli
->outbuf
);
536 if (lm_response
.length
) {
537 memcpy(p
,lm_response
.data
, lm_response
.length
); p
+= lm_response
.length
;
539 if (nt_response
.length
) {
540 memcpy(p
,nt_response
.data
, nt_response
.length
); p
+= nt_response
.length
;
542 p
+= clistr_push(cli
, p
, user
, -1, STR_TERMINATE
);
544 /* Upper case here might help some NTLMv2 implementations */
545 p
+= clistr_push(cli
, p
, workgroup
, -1, STR_TERMINATE
|STR_UPPER
);
546 p
+= clistr_push(cli
, p
, "Unix", -1, STR_TERMINATE
);
547 p
+= clistr_push(cli
, p
, "Samba", -1, STR_TERMINATE
);
548 cli_setup_bcc(cli
, p
);
550 if (!cli_send_smb(cli
) || !cli_receive_smb(cli
)) {
551 result
= cli_nt_error(cli
);
555 /* show_msg(cli->inbuf); */
557 if (cli_is_error(cli
)) {
558 result
= cli_nt_error(cli
);
563 ok
= cli_simple_set_signing(cli
, session_key
, lm_response
);
565 ok
= cli_simple_set_signing(cli
, session_key
, nt_response
);
568 if (!cli_check_sign_mac(cli
, cli
->inbuf
, 1)) {
569 result
= NT_STATUS_ACCESS_DENIED
;
574 /* use the returned vuid from now on */
575 cli
->vuid
= SVAL(cli
->inbuf
,smb_uid
);
577 p
= smb_buf(cli
->inbuf
);
578 p
+= clistr_pull(cli
->inbuf
, cli
->server_os
, p
, sizeof(fstring
),
580 p
+= clistr_pull(cli
->inbuf
, cli
->server_type
, p
, sizeof(fstring
),
582 p
+= clistr_pull(cli
->inbuf
, cli
->server_domain
, p
, sizeof(fstring
),
585 if (strstr(cli
->server_type
, "Samba")) {
586 cli
->is_samba
= True
;
589 result
= cli_set_username(cli
, user
);
590 if (!NT_STATUS_IS_OK(result
)) {
594 if (session_key
.data
) {
595 /* Have plaintext orginal */
596 cli_set_session_key(cli
, session_key
);
599 result
= NT_STATUS_OK
;
601 data_blob_free(&lm_response
);
602 data_blob_free(&nt_response
);
603 data_blob_free(&session_key
);
607 /****************************************************************************
608 Send a extended security session setup blob
609 ****************************************************************************/
611 static bool cli_session_setup_blob_send(struct cli_state
*cli
, DATA_BLOB blob
)
613 uint32 capabilities
= cli_session_setup_capabilities(cli
);
616 capabilities
|= CAP_EXTENDED_SECURITY
;
618 /* send a session setup command */
619 memset(cli
->outbuf
,'\0',smb_size
);
621 cli_set_message(cli
->outbuf
,12,0,True
);
622 SCVAL(cli
->outbuf
,smb_com
,SMBsesssetupX
);
624 cli_setup_packet(cli
);
626 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
627 SSVAL(cli
->outbuf
,smb_vwv2
,CLI_BUFFER_SIZE
);
628 SSVAL(cli
->outbuf
,smb_vwv3
,2);
629 SSVAL(cli
->outbuf
,smb_vwv4
,1);
630 SIVAL(cli
->outbuf
,smb_vwv5
,0);
631 SSVAL(cli
->outbuf
,smb_vwv7
,blob
.length
);
632 SIVAL(cli
->outbuf
,smb_vwv10
,capabilities
);
633 p
= smb_buf(cli
->outbuf
);
634 memcpy(p
, blob
.data
, blob
.length
);
636 p
+= clistr_push(cli
, p
, "Unix", -1, STR_TERMINATE
);
637 p
+= clistr_push(cli
, p
, "Samba", -1, STR_TERMINATE
);
638 cli_setup_bcc(cli
, p
);
639 return cli_send_smb(cli
);
642 /****************************************************************************
643 Send a extended security session setup blob, returning a reply blob.
644 ****************************************************************************/
646 static DATA_BLOB
cli_session_setup_blob_receive(struct cli_state
*cli
)
648 DATA_BLOB blob2
= data_blob_null
;
652 if (!cli_receive_smb(cli
))
655 show_msg(cli
->inbuf
);
657 if (cli_is_error(cli
) && !NT_STATUS_EQUAL(cli_nt_error(cli
),
658 NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
662 /* use the returned vuid from now on */
663 cli
->vuid
= SVAL(cli
->inbuf
,smb_uid
);
665 p
= smb_buf(cli
->inbuf
);
667 blob2
= data_blob(p
, SVAL(cli
->inbuf
, smb_vwv3
));
670 p
+= clistr_pull(cli
->inbuf
, cli
->server_os
, p
, sizeof(fstring
),
673 /* w2k with kerberos doesn't properly null terminate this field */
674 len
= smb_bufrem(cli
->inbuf
, p
);
675 if (p
+ len
< cli
->inbuf
+ cli
->bufsize
+SAFETY_MARGIN
- 2) {
676 char *end_of_buf
= p
+ len
;
679 /* Now it's null terminated. */
680 p
+= clistr_pull(cli
->inbuf
, cli
->server_type
, p
, sizeof(fstring
),
683 * See if there's another string. If so it's the
684 * server domain (part of the 'standard' Samba
687 if (p
< end_of_buf
) {
688 p
+= clistr_pull(cli
->inbuf
, cli
->server_domain
, p
, sizeof(fstring
),
693 * No room to null terminate so we can't see if there
694 * is another string (server_domain) afterwards.
696 p
+= clistr_pull(cli
->inbuf
, cli
->server_type
, p
, sizeof(fstring
),
703 /****************************************************************************
704 Send a extended security session setup blob, returning a reply blob.
705 ****************************************************************************/
707 /* The following is calculated from :
709 * (smb_wcnt * 2) = 24 (smb_wcnt == 12 in cli_session_setup_blob_send() )
710 * (strlen("Unix") + 1 + strlen("Samba") + 1) * 2 = 22 (unicode strings at
714 #define BASE_SESSSETUP_BLOB_PACKET_SIZE (35 + 24 + 22)
716 static bool cli_session_setup_blob(struct cli_state
*cli
, DATA_BLOB blob
)
718 int32 remaining
= blob
.length
;
720 DATA_BLOB send_blob
= data_blob_null
;
721 int32 max_blob_size
= 0;
722 DATA_BLOB receive_blob
= data_blob_null
;
724 if (cli
->max_xmit
< BASE_SESSSETUP_BLOB_PACKET_SIZE
+ 1) {
725 DEBUG(0,("cli_session_setup_blob: cli->max_xmit too small "
726 "(was %u, need minimum %u)\n",
727 (unsigned int)cli
->max_xmit
,
728 BASE_SESSSETUP_BLOB_PACKET_SIZE
));
729 cli_set_nt_error(cli
, NT_STATUS_INVALID_PARAMETER
);
733 max_blob_size
= cli
->max_xmit
- BASE_SESSSETUP_BLOB_PACKET_SIZE
;
735 while ( remaining
> 0) {
736 if (remaining
>= max_blob_size
) {
737 send_blob
.length
= max_blob_size
;
738 remaining
-= max_blob_size
;
740 send_blob
.length
= remaining
;
744 send_blob
.data
= &blob
.data
[cur
];
745 cur
+= send_blob
.length
;
747 DEBUG(10, ("cli_session_setup_blob: Remaining (%u) sending (%u) current (%u)\n",
748 (unsigned int)remaining
,
749 (unsigned int)send_blob
.length
,
750 (unsigned int)cur
));
752 if (!cli_session_setup_blob_send(cli
, send_blob
)) {
753 DEBUG(0, ("cli_session_setup_blob: send failed\n"));
757 receive_blob
= cli_session_setup_blob_receive(cli
);
758 data_blob_free(&receive_blob
);
760 if (cli_is_error(cli
) &&
761 !NT_STATUS_EQUAL( cli_get_nt_error(cli
),
762 NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
763 DEBUG(0, ("cli_session_setup_blob: receive failed "
764 "(%s)\n", nt_errstr(cli_get_nt_error(cli
))));
773 /****************************************************************************
774 Use in-memory credentials cache
775 ****************************************************************************/
777 static void use_in_memory_ccache(void) {
778 setenv(KRB5_ENV_CCNAME
, "MEMORY:cliconnect", 1);
781 /****************************************************************************
782 Do a spnego/kerberos encrypted session setup.
783 ****************************************************************************/
785 static ADS_STATUS
cli_session_setup_kerberos(struct cli_state
*cli
, const char *principal
, const char *workgroup
)
787 DATA_BLOB negTokenTarg
;
788 DATA_BLOB session_key_krb5
;
792 cli_temp_set_signing(cli
);
794 DEBUG(2,("Doing kerberos session setup\n"));
796 /* generate the encapsulated kerberos5 ticket */
797 rc
= spnego_gen_negTokenTarg(principal
, 0, &negTokenTarg
, &session_key_krb5
, 0, NULL
);
800 DEBUG(1, ("cli_session_setup_kerberos: spnego_gen_negTokenTarg failed: %s\n",
802 return ADS_ERROR_KRB5(rc
);
806 file_save("negTokenTarg.dat", negTokenTarg
.data
, negTokenTarg
.length
);
809 if (!cli_session_setup_blob(cli
, negTokenTarg
)) {
810 nt_status
= cli_nt_error(cli
);
814 if (cli_is_error(cli
)) {
815 nt_status
= cli_nt_error(cli
);
816 if (NT_STATUS_IS_OK(nt_status
)) {
817 nt_status
= NT_STATUS_UNSUCCESSFUL
;
822 cli_set_session_key(cli
, session_key_krb5
);
824 if (cli_simple_set_signing(
825 cli
, session_key_krb5
, data_blob_null
)) {
827 if (!cli_check_sign_mac(cli
, cli
->inbuf
, 1)) {
828 nt_status
= NT_STATUS_ACCESS_DENIED
;
833 data_blob_free(&negTokenTarg
);
834 data_blob_free(&session_key_krb5
);
836 return ADS_ERROR_NT(NT_STATUS_OK
);
839 data_blob_free(&negTokenTarg
);
840 data_blob_free(&session_key_krb5
);
842 return ADS_ERROR_NT(nt_status
);
844 #endif /* HAVE_KRB5 */
847 /****************************************************************************
848 Do a spnego/NTLMSSP encrypted session setup.
849 ****************************************************************************/
851 static NTSTATUS
cli_session_setup_ntlmssp(struct cli_state
*cli
, const char *user
,
852 const char *pass
, const char *domain
)
854 struct ntlmssp_state
*ntlmssp_state
;
858 DATA_BLOB blob
= data_blob_null
;
859 DATA_BLOB blob_in
= data_blob_null
;
860 DATA_BLOB blob_out
= data_blob_null
;
862 cli_temp_set_signing(cli
);
864 if (!NT_STATUS_IS_OK(nt_status
= ntlmssp_client_start(&ntlmssp_state
))) {
867 ntlmssp_want_feature(ntlmssp_state
, NTLMSSP_FEATURE_SESSION_KEY
);
869 if (!NT_STATUS_IS_OK(nt_status
= ntlmssp_set_username(ntlmssp_state
, user
))) {
872 if (!NT_STATUS_IS_OK(nt_status
= ntlmssp_set_domain(ntlmssp_state
, domain
))) {
875 if (!NT_STATUS_IS_OK(nt_status
= ntlmssp_set_password(ntlmssp_state
, pass
))) {
880 nt_status
= ntlmssp_update(ntlmssp_state
,
882 data_blob_free(&blob_in
);
883 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
) || NT_STATUS_IS_OK(nt_status
)) {
885 /* and wrap it in a SPNEGO wrapper */
886 msg1
= gen_negTokenInit(OID_NTLMSSP
, blob_out
);
888 /* wrap it in SPNEGO */
889 msg1
= spnego_gen_auth(blob_out
);
892 /* now send that blob on its way */
893 if (!cli_session_setup_blob_send(cli
, msg1
)) {
894 DEBUG(3, ("Failed to send NTLMSSP/SPNEGO blob to server!\n"));
895 nt_status
= NT_STATUS_UNSUCCESSFUL
;
897 blob
= cli_session_setup_blob_receive(cli
);
899 nt_status
= cli_nt_error(cli
);
900 if (cli_is_error(cli
) && NT_STATUS_IS_OK(nt_status
)) {
901 if (cli
->smb_rw_error
== SMB_READ_BAD_SIG
) {
902 nt_status
= NT_STATUS_ACCESS_DENIED
;
904 nt_status
= NT_STATUS_UNSUCCESSFUL
;
908 data_blob_free(&msg1
);
912 if (NT_STATUS_IS_OK(nt_status
)) {
913 nt_status
= NT_STATUS_UNSUCCESSFUL
;
915 } else if ((turn
== 1) &&
916 NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
917 DATA_BLOB tmp_blob
= data_blob_null
;
918 /* the server might give us back two challenges */
919 if (!spnego_parse_challenge(blob
, &blob_in
,
921 DEBUG(3,("Failed to parse challenges\n"));
922 nt_status
= NT_STATUS_INVALID_PARAMETER
;
924 data_blob_free(&tmp_blob
);
926 if (!spnego_parse_auth_response(blob
, nt_status
, OID_NTLMSSP
,
928 DEBUG(3,("Failed to parse auth response\n"));
929 if (NT_STATUS_IS_OK(nt_status
)
930 || NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
))
931 nt_status
= NT_STATUS_INVALID_PARAMETER
;
934 data_blob_free(&blob
);
935 data_blob_free(&blob_out
);
937 } while (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
));
939 data_blob_free(&blob_in
);
941 if (NT_STATUS_IS_OK(nt_status
)) {
943 if (cli
->server_domain
[0] == '\0') {
944 fstrcpy(cli
->server_domain
, ntlmssp_state
->server_domain
);
946 cli_set_session_key(cli
, ntlmssp_state
->session_key
);
948 if (cli_simple_set_signing(
949 cli
, ntlmssp_state
->session_key
, data_blob_null
)) {
951 if (!cli_check_sign_mac(cli
, cli
->inbuf
, 1)) {
952 nt_status
= NT_STATUS_ACCESS_DENIED
;
957 /* we have a reference conter on ntlmssp_state, if we are signing
958 then the state will be kept by the signing engine */
960 ntlmssp_end(&ntlmssp_state
);
962 if (!NT_STATUS_IS_OK(nt_status
)) {
968 /****************************************************************************
969 Do a spnego encrypted session setup.
971 user_domain: The shortname of the domain the user/machine is a member of.
972 dest_realm: The realm we're connecting to, if NULL we use our default realm.
973 ****************************************************************************/
975 ADS_STATUS
cli_session_setup_spnego(struct cli_state
*cli
, const char *user
,
976 const char *pass
, const char *user_domain
,
977 const char * dest_realm
)
979 char *principal
= NULL
;
980 char *OIDs
[ASN1_MAX_OIDS
];
983 const char *p
= NULL
;
984 char *account
= NULL
;
987 DEBUG(3,("Doing spnego session setup (blob length=%lu)\n", (unsigned long)cli
->secblob
.length
));
989 /* the server might not even do spnego */
990 if (cli
->secblob
.length
<= 16) {
991 DEBUG(3,("server didn't supply a full spnego negprot\n"));
996 file_save("negprot.dat", cli
->secblob
.data
, cli
->secblob
.length
);
999 /* there is 16 bytes of GUID before the real spnego packet starts */
1000 blob
= data_blob(cli
->secblob
.data
+16, cli
->secblob
.length
-16);
1002 /* The server sent us the first part of the SPNEGO exchange in the
1003 * negprot reply. It is WRONG to depend on the principal sent in the
1004 * negprot reply, but right now we do it. If we don't receive one,
1005 * we try to best guess, then fall back to NTLM. */
1006 if (!spnego_parse_negTokenInit(blob
, OIDs
, &principal
)) {
1007 data_blob_free(&blob
);
1008 return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER
);
1010 data_blob_free(&blob
);
1012 /* make sure the server understands kerberos */
1013 for (i
=0;OIDs
[i
];i
++) {
1015 DEBUG(3,("got OID=%s\n", OIDs
[i
]));
1017 DEBUGADD(3,("got OID=%s\n", OIDs
[i
]));
1018 if (strcmp(OIDs
[i
], OID_KERBEROS5_OLD
) == 0 ||
1019 strcmp(OIDs
[i
], OID_KERBEROS5
) == 0) {
1020 cli
->got_kerberos_mechanism
= True
;
1022 talloc_free(OIDs
[i
]);
1025 DEBUG(3,("got principal=%s\n", principal
? principal
: "<null>"));
1027 status
= cli_set_username(cli
, user
);
1028 if (!NT_STATUS_IS_OK(status
)) {
1029 return ADS_ERROR_NT(status
);
1033 /* If password is set we reauthenticate to kerberos server
1034 * and do not store results */
1036 if (cli
->got_kerberos_mechanism
&& cli
->use_kerberos
) {
1039 if (pass
&& *pass
) {
1042 use_in_memory_ccache();
1043 ret
= kerberos_kinit_password(user
, pass
, 0 /* no time correction for now */, NULL
);
1046 TALLOC_FREE(principal
);
1047 DEBUG(0, ("Kinit failed: %s\n", error_message(ret
)));
1048 if (cli
->fallback_after_kerberos
)
1050 return ADS_ERROR_KRB5(ret
);
1054 /* If we get a bad principal, try to guess it if
1055 we have a valid host NetBIOS name.
1057 if (strequal(principal
, ADS_IGNORE_PRINCIPAL
)) {
1058 TALLOC_FREE(principal
);
1061 if (principal
== NULL
&&
1062 !is_ipaddress(cli
->desthost
) &&
1063 !strequal(STAR_SMBSERVER
,
1066 char *machine
= NULL
;
1068 DEBUG(3,("cli_session_setup_spnego: got a "
1069 "bad server principal, trying to guess ...\n"));
1071 host
= strchr_m(cli
->desthost
, '.');
1073 machine
= SMB_STRNDUP(cli
->desthost
,
1074 host
- cli
->desthost
);
1076 machine
= SMB_STRDUP(cli
->desthost
);
1078 if (machine
== NULL
) {
1079 return ADS_ERROR_NT(NT_STATUS_NO_MEMORY
);
1083 realm
= SMB_STRDUP(dest_realm
);
1086 realm
= kerberos_get_default_realm_from_ccache();
1088 if (realm
&& *realm
) {
1089 principal
= talloc_asprintf(NULL
, "%s$@%s",
1094 return ADS_ERROR_NT(NT_STATUS_NO_MEMORY
);
1096 DEBUG(3,("cli_session_setup_spnego: guessed "
1097 "server principal=%s\n",
1098 principal
? principal
: "<null>"));
1105 rc
= cli_session_setup_kerberos(cli
, principal
,
1107 if (ADS_ERR_OK(rc
) || !cli
->fallback_after_kerberos
) {
1108 TALLOC_FREE(principal
);
1115 TALLOC_FREE(principal
);
1119 account
= talloc_strdup(talloc_tos(), user
);
1121 return ADS_ERROR_NT(NT_STATUS_NO_MEMORY
);
1124 /* when falling back to ntlmssp while authenticating with a machine
1125 * account strip off the realm - gd */
1127 if ((p
= strchr_m(user
, '@')) != NULL
) {
1128 account
[PTR_DIFF(p
,user
)] = '\0';
1131 return ADS_ERROR_NT(cli_session_setup_ntlmssp(cli
, account
, pass
, user_domain
));
1134 /****************************************************************************
1135 Send a session setup. The username and workgroup is in UNIX character
1136 format and must be converted to DOS codepage format before sending. If the
1137 password is in plaintext, the same should be done.
1138 ****************************************************************************/
1140 NTSTATUS
cli_session_setup(struct cli_state
*cli
,
1142 const char *pass
, int passlen
,
1143 const char *ntpass
, int ntpasslen
,
1144 const char *workgroup
)
1150 fstrcpy(user2
, user
);
1159 /* allow for workgroups as part of the username */
1160 if ((p
=strchr_m(user2
,'\\')) || (p
=strchr_m(user2
,'/')) ||
1161 (p
=strchr_m(user2
,*lp_winbind_separator()))) {
1167 if (cli
->protocol
< PROTOCOL_LANMAN1
) {
1168 return NT_STATUS_OK
;
1171 /* now work out what sort of session setup we are going to
1172 do. I have split this into separate functions to make the
1173 flow a bit easier to understand (tridge) */
1175 /* if its an older server then we have to use the older request format */
1177 if (cli
->protocol
< PROTOCOL_NT1
) {
1178 if (!lp_client_lanman_auth() && passlen
!= 24 && (*pass
)) {
1179 DEBUG(1, ("Server requested LM password but 'client lanman auth'"
1181 return NT_STATUS_ACCESS_DENIED
;
1184 if ((cli
->sec_mode
& NEGOTIATE_SECURITY_CHALLENGE_RESPONSE
) == 0 &&
1185 !lp_client_plaintext_auth() && (*pass
)) {
1186 DEBUG(1, ("Server requested plaintext password but "
1187 "'client plaintext auth' is disabled\n"));
1188 return NT_STATUS_ACCESS_DENIED
;
1191 return cli_session_setup_lanman2(cli
, user
, pass
, passlen
,
1195 /* if no user is supplied then we have to do an anonymous connection.
1196 passwords are ignored */
1198 if (!user
|| !*user
)
1199 return cli_session_setup_guest(cli
);
1201 /* if the server is share level then send a plaintext null
1202 password at this point. The password is sent in the tree
1205 if ((cli
->sec_mode
& NEGOTIATE_SECURITY_USER_LEVEL
) == 0)
1206 return cli_session_setup_plaintext(cli
, user
, "", workgroup
);
1208 /* if the server doesn't support encryption then we have to use
1209 plaintext. The second password is ignored */
1211 if ((cli
->sec_mode
& NEGOTIATE_SECURITY_CHALLENGE_RESPONSE
) == 0) {
1212 if (!lp_client_plaintext_auth() && (*pass
)) {
1213 DEBUG(1, ("Server requested plaintext password but "
1214 "'client plaintext auth' is disabled\n"));
1215 return NT_STATUS_ACCESS_DENIED
;
1217 return cli_session_setup_plaintext(cli
, user
, pass
, workgroup
);
1220 /* if the server supports extended security then use SPNEGO */
1222 if (cli
->capabilities
& CAP_EXTENDED_SECURITY
) {
1223 ADS_STATUS status
= cli_session_setup_spnego(cli
, user
, pass
,
1225 if (!ADS_ERR_OK(status
)) {
1226 DEBUG(3, ("SPNEGO login failed: %s\n", ads_errstr(status
)));
1227 return ads_ntstatus(status
);
1232 /* otherwise do a NT1 style session setup */
1233 status
= cli_session_setup_nt1(cli
, user
, pass
, passlen
,
1234 ntpass
, ntpasslen
, workgroup
);
1235 if (!NT_STATUS_IS_OK(status
)) {
1236 DEBUG(3,("cli_session_setup: NT1 session setup "
1237 "failed: %s\n", nt_errstr(status
)));
1242 if (strstr(cli
->server_type
, "Samba")) {
1243 cli
->is_samba
= True
;
1246 return NT_STATUS_OK
;
1249 /****************************************************************************
1251 *****************************************************************************/
1253 bool cli_ulogoff(struct cli_state
*cli
)
1255 memset(cli
->outbuf
,'\0',smb_size
);
1256 cli_set_message(cli
->outbuf
,2,0,True
);
1257 SCVAL(cli
->outbuf
,smb_com
,SMBulogoffX
);
1258 cli_setup_packet(cli
);
1259 SSVAL(cli
->outbuf
,smb_vwv0
,0xFF);
1260 SSVAL(cli
->outbuf
,smb_vwv2
,0); /* no additional info */
1263 if (!cli_receive_smb(cli
))
1266 if (cli_is_error(cli
)) {
1274 /****************************************************************************
1276 ****************************************************************************/
1278 struct cli_tcon_andx_state
{
1279 struct cli_state
*cli
;
1284 static void cli_tcon_andx_done(struct tevent_req
*subreq
);
1286 struct tevent_req
*cli_tcon_andx_create(TALLOC_CTX
*mem_ctx
,
1287 struct event_context
*ev
,
1288 struct cli_state
*cli
,
1289 const char *share
, const char *dev
,
1290 const char *pass
, int passlen
,
1291 struct tevent_req
**psmbreq
)
1293 struct tevent_req
*req
, *subreq
;
1294 struct cli_tcon_andx_state
*state
;
1300 req
= tevent_req_create(mem_ctx
, &state
, struct cli_tcon_andx_state
);
1307 fstrcpy(cli
->share
, share
);
1309 /* in user level security don't send a password now */
1310 if (cli
->sec_mode
& NEGOTIATE_SECURITY_USER_LEVEL
) {
1313 } else if (pass
== NULL
) {
1314 DEBUG(1, ("Server not using user level security and no "
1315 "password supplied.\n"));
1319 if ((cli
->sec_mode
& NEGOTIATE_SECURITY_CHALLENGE_RESPONSE
) &&
1320 *pass
&& passlen
!= 24) {
1321 if (!lp_client_lanman_auth()) {
1322 DEBUG(1, ("Server requested LANMAN password "
1323 "(share-level security) but "
1324 "'client lanman auth' is disabled\n"));
1329 * Non-encrypted passwords - convert to DOS codepage before
1333 SMBencrypt(pass
, cli
->secblob
.data
, (uchar
*)pword
);
1335 if((cli
->sec_mode
& (NEGOTIATE_SECURITY_USER_LEVEL
1336 |NEGOTIATE_SECURITY_CHALLENGE_RESPONSE
))
1338 if (!lp_client_plaintext_auth() && (*pass
)) {
1339 DEBUG(1, ("Server requested plaintext "
1340 "password but 'client plaintext "
1341 "auth' is disabled\n"));
1346 * Non-encrypted passwords - convert to DOS codepage
1349 passlen
= clistr_push(cli
, pword
, pass
, sizeof(pword
),
1351 if (passlen
== -1) {
1352 DEBUG(1, ("clistr_push(pword) failed\n"));
1357 memcpy(pword
, pass
, passlen
);
1362 SCVAL(vwv
+0, 0, 0xFF);
1365 SSVAL(vwv
+2, 0, TCONX_FLAG_EXTENDED_RESPONSE
);
1366 SSVAL(vwv
+3, 0, passlen
);
1369 bytes
= (uint8_t *)talloc_memdup(state
, pword
, passlen
);
1371 bytes
= talloc_array(state
, uint8_t, 0);
1377 tmp
= talloc_asprintf_strupper_m(talloc_tos(), "\\\\%s\\%s",
1378 cli
->desthost
, share
);
1383 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), tmp
, strlen(tmp
)+1,
1388 * Add the devicetype
1390 tmp
= talloc_strdup_upper(talloc_tos(), dev
);
1395 bytes
= smb_bytes_push_str(bytes
, false, tmp
, strlen(tmp
)+1, NULL
);
1398 if (bytes
== NULL
) {
1403 state
->bytes
.iov_base
= (void *)bytes
;
1404 state
->bytes
.iov_len
= talloc_get_size(bytes
);
1406 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBtconX
, 0, 4, vwv
,
1408 if (subreq
== NULL
) {
1412 tevent_req_set_callback(subreq
, cli_tcon_andx_done
, req
);
1417 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
1418 return tevent_req_post(req
, ev
);
1421 struct tevent_req
*cli_tcon_andx_send(TALLOC_CTX
*mem_ctx
,
1422 struct event_context
*ev
,
1423 struct cli_state
*cli
,
1424 const char *share
, const char *dev
,
1425 const char *pass
, int passlen
)
1427 struct tevent_req
*req
, *subreq
;
1430 req
= cli_tcon_andx_create(mem_ctx
, ev
, cli
, share
, dev
, pass
, passlen
,
1435 status
= cli_smb_req_send(subreq
);
1436 if (!NT_STATUS_IS_OK(status
)) {
1437 tevent_req_nterror(req
, status
);
1438 return tevent_req_post(req
, ev
);
1443 static void cli_tcon_andx_done(struct tevent_req
*subreq
)
1445 struct tevent_req
*req
= tevent_req_callback_data(
1446 subreq
, struct tevent_req
);
1447 struct cli_tcon_andx_state
*state
= tevent_req_data(
1448 req
, struct cli_tcon_andx_state
);
1449 struct cli_state
*cli
= state
->cli
;
1450 char *inbuf
= (char *)cli_smb_inbuf(subreq
);
1457 status
= cli_smb_recv(subreq
, 0, &wct
, &vwv
, &num_bytes
, &bytes
);
1458 if (!NT_STATUS_IS_OK(status
)) {
1459 TALLOC_FREE(subreq
);
1460 tevent_req_nterror(req
, status
);
1464 clistr_pull(inbuf
, cli
->dev
, bytes
, sizeof(fstring
), num_bytes
,
1465 STR_TERMINATE
|STR_ASCII
);
1467 if ((cli
->protocol
>= PROTOCOL_NT1
) && (num_bytes
== 3)) {
1468 /* almost certainly win95 - enable bug fixes */
1473 * Make sure that we have the optional support 16-bit field. WCT > 2.
1474 * Avoids issues when connecting to Win9x boxes sharing files
1477 cli
->dfsroot
= false;
1479 if ((wct
> 2) && (cli
->protocol
>= PROTOCOL_LANMAN2
)) {
1480 cli
->dfsroot
= ((SVAL(vwv
+2, 0) & SMB_SHARE_IN_DFS
) != 0);
1483 cli
->cnum
= SVAL(inbuf
,smb_tid
);
1484 tevent_req_done(req
);
1487 NTSTATUS
cli_tcon_andx_recv(struct tevent_req
*req
)
1489 return tevent_req_simple_recv_ntstatus(req
);
1492 NTSTATUS
cli_tcon_andx(struct cli_state
*cli
, const char *share
,
1493 const char *dev
, const char *pass
, int passlen
)
1495 TALLOC_CTX
*frame
= talloc_stackframe();
1496 struct event_context
*ev
;
1497 struct tevent_req
*req
;
1498 NTSTATUS status
= NT_STATUS_OK
;
1500 if (cli_has_async_calls(cli
)) {
1502 * Can't use sync call while an async call is in flight
1504 status
= NT_STATUS_INVALID_PARAMETER
;
1508 ev
= event_context_init(frame
);
1510 status
= NT_STATUS_NO_MEMORY
;
1514 req
= cli_tcon_andx_send(frame
, ev
, cli
, share
, dev
, pass
, passlen
);
1516 status
= NT_STATUS_NO_MEMORY
;
1520 if (!tevent_req_poll(req
, ev
)) {
1521 status
= map_nt_error_from_unix(errno
);
1525 status
= cli_tcon_andx_recv(req
);
1528 if (!NT_STATUS_IS_OK(status
)) {
1529 cli_set_error(cli
, status
);
1534 /****************************************************************************
1535 Send a tree disconnect.
1536 ****************************************************************************/
1538 bool cli_tdis(struct cli_state
*cli
)
1540 memset(cli
->outbuf
,'\0',smb_size
);
1541 cli_set_message(cli
->outbuf
,0,0,True
);
1542 SCVAL(cli
->outbuf
,smb_com
,SMBtdis
);
1543 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1544 cli_setup_packet(cli
);
1547 if (!cli_receive_smb(cli
))
1550 if (cli_is_error(cli
)) {
1558 /****************************************************************************
1559 Send a negprot command.
1560 ****************************************************************************/
1562 void cli_negprot_sendsync(struct cli_state
*cli
)
1567 if (cli
->protocol
< PROTOCOL_NT1
)
1568 cli
->use_spnego
= False
;
1570 memset(cli
->outbuf
,'\0',smb_size
);
1572 /* setup the protocol strings */
1573 cli_set_message(cli
->outbuf
,0,0,True
);
1575 p
= smb_buf(cli
->outbuf
);
1576 for (numprots
=0; numprots
< ARRAY_SIZE(prots
); numprots
++) {
1577 if (prots
[numprots
].prot
> cli
->protocol
) {
1581 p
+= clistr_push(cli
, p
, prots
[numprots
].name
, -1, STR_TERMINATE
);
1584 SCVAL(cli
->outbuf
,smb_com
,SMBnegprot
);
1585 cli_setup_bcc(cli
, p
);
1586 cli_setup_packet(cli
);
1588 SCVAL(smb_buf(cli
->outbuf
),0,2);
1593 /****************************************************************************
1594 Send a negprot command.
1595 ****************************************************************************/
1597 struct cli_negprot_state
{
1598 struct cli_state
*cli
;
1601 static void cli_negprot_done(struct tevent_req
*subreq
);
1603 struct tevent_req
*cli_negprot_send(TALLOC_CTX
*mem_ctx
,
1604 struct event_context
*ev
,
1605 struct cli_state
*cli
)
1607 struct tevent_req
*req
, *subreq
;
1608 struct cli_negprot_state
*state
;
1609 uint8_t *bytes
= NULL
;
1613 req
= tevent_req_create(mem_ctx
, &state
, struct cli_negprot_state
);
1619 if (cli
->protocol
< PROTOCOL_NT1
)
1620 cli
->use_spnego
= False
;
1622 /* setup the protocol strings */
1623 for (numprots
=0; numprots
< ARRAY_SIZE(prots
); numprots
++) {
1625 if (prots
[numprots
].prot
> cli
->protocol
) {
1628 bytes
= (uint8_t *)talloc_append_blob(
1629 state
, bytes
, data_blob_const(&c
, sizeof(c
)));
1630 if (tevent_req_nomem(bytes
, req
)) {
1631 return tevent_req_post(req
, ev
);
1633 bytes
= smb_bytes_push_str(bytes
, false,
1634 prots
[numprots
].name
,
1635 strlen(prots
[numprots
].name
)+1,
1637 if (tevent_req_nomem(bytes
, req
)) {
1638 return tevent_req_post(req
, ev
);
1645 subreq
= cli_smb_send(state
, ev
, cli
, SMBnegprot
, 0, 0, NULL
,
1646 talloc_get_size(bytes
), bytes
);
1649 if (tevent_req_nomem(subreq
, req
)) {
1650 return tevent_req_post(req
, ev
);
1652 tevent_req_set_callback(subreq
, cli_negprot_done
, req
);
1656 static void cli_negprot_done(struct tevent_req
*subreq
)
1658 struct tevent_req
*req
= tevent_req_callback_data(
1659 subreq
, struct tevent_req
);
1660 struct cli_negprot_state
*state
= tevent_req_data(
1661 req
, struct cli_negprot_state
);
1662 struct cli_state
*cli
= state
->cli
;
1670 status
= cli_smb_recv(subreq
, 1, &wct
, &vwv
, &num_bytes
, &bytes
);
1671 if (!NT_STATUS_IS_OK(status
)) {
1672 TALLOC_FREE(subreq
);
1676 protnum
= SVAL(vwv
, 0);
1678 if ((protnum
>= ARRAY_SIZE(prots
))
1679 || (prots
[protnum
].prot
> cli
->protocol
)) {
1680 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
1684 cli
->protocol
= prots
[protnum
].prot
;
1686 if ((cli
->protocol
< PROTOCOL_NT1
) &&
1687 client_is_signing_mandatory(cli
)) {
1688 DEBUG(0,("cli_negprot: SMB signing is mandatory and the selected protocol level doesn't support it.\n"));
1689 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
1693 if (cli
->protocol
>= PROTOCOL_NT1
) {
1695 bool negotiated_smb_signing
= false;
1698 cli
->sec_mode
= CVAL(vwv
+ 1, 0);
1699 cli
->max_mux
= SVAL(vwv
+ 1, 1);
1700 cli
->max_xmit
= IVAL(vwv
+ 3, 1);
1701 cli
->sesskey
= IVAL(vwv
+ 7, 1);
1702 cli
->serverzone
= SVALS(vwv
+ 15, 1);
1703 cli
->serverzone
*= 60;
1704 /* this time arrives in real GMT */
1705 ts
= interpret_long_date(((char *)(vwv
+11))+1);
1706 cli
->servertime
= ts
.tv_sec
;
1707 cli
->secblob
= data_blob(bytes
, num_bytes
);
1708 cli
->capabilities
= IVAL(vwv
+ 9, 1);
1709 if (cli
->capabilities
& CAP_RAW_MODE
) {
1710 cli
->readbraw_supported
= True
;
1711 cli
->writebraw_supported
= True
;
1713 /* work out if they sent us a workgroup */
1714 if (!(cli
->capabilities
& CAP_EXTENDED_SECURITY
) &&
1715 smb_buflen(cli
->inbuf
) > 8) {
1716 clistr_pull(cli
->inbuf
, cli
->server_domain
,
1717 bytes
+8, sizeof(cli
->server_domain
),
1719 STR_UNICODE
|STR_NOALIGN
);
1723 * As signing is slow we only turn it on if either the client or
1724 * the server require it. JRA.
1727 if (cli
->sec_mode
& NEGOTIATE_SECURITY_SIGNATURES_REQUIRED
) {
1728 /* Fail if server says signing is mandatory and we don't want to support it. */
1729 if (!client_is_signing_allowed(cli
)) {
1730 DEBUG(0,("cli_negprot: SMB signing is mandatory and we have disabled it.\n"));
1731 tevent_req_nterror(req
,
1732 NT_STATUS_ACCESS_DENIED
);
1735 negotiated_smb_signing
= true;
1736 } else if (client_is_signing_mandatory(cli
) && client_is_signing_allowed(cli
)) {
1737 /* Fail if client says signing is mandatory and the server doesn't support it. */
1738 if (!(cli
->sec_mode
& NEGOTIATE_SECURITY_SIGNATURES_ENABLED
)) {
1739 DEBUG(1,("cli_negprot: SMB signing is mandatory and the server doesn't support it.\n"));
1740 tevent_req_nterror(req
,
1741 NT_STATUS_ACCESS_DENIED
);
1744 negotiated_smb_signing
= true;
1745 } else if (cli
->sec_mode
& NEGOTIATE_SECURITY_SIGNATURES_ENABLED
) {
1746 negotiated_smb_signing
= true;
1749 if (negotiated_smb_signing
) {
1750 cli_set_signing_negotiated(cli
);
1753 if (cli
->capabilities
& (CAP_LARGE_READX
|CAP_LARGE_WRITEX
)) {
1754 SAFE_FREE(cli
->outbuf
);
1755 SAFE_FREE(cli
->inbuf
);
1756 cli
->outbuf
= (char *)SMB_MALLOC(CLI_SAMBA_MAX_LARGE_READX_SIZE
+LARGE_WRITEX_HDR_SIZE
+SAFETY_MARGIN
);
1757 cli
->inbuf
= (char *)SMB_MALLOC(CLI_SAMBA_MAX_LARGE_READX_SIZE
+LARGE_WRITEX_HDR_SIZE
+SAFETY_MARGIN
);
1758 cli
->bufsize
= CLI_SAMBA_MAX_LARGE_READX_SIZE
+ LARGE_WRITEX_HDR_SIZE
;
1761 } else if (cli
->protocol
>= PROTOCOL_LANMAN1
) {
1762 cli
->use_spnego
= False
;
1763 cli
->sec_mode
= SVAL(vwv
+ 1, 0);
1764 cli
->max_xmit
= SVAL(vwv
+ 2, 0);
1765 cli
->max_mux
= SVAL(vwv
+ 3, 0);
1766 cli
->sesskey
= IVAL(vwv
+ 6, 0);
1767 cli
->serverzone
= SVALS(vwv
+ 10, 0);
1768 cli
->serverzone
*= 60;
1769 /* this time is converted to GMT by make_unix_date */
1770 cli
->servertime
= cli_make_unix_date(
1771 cli
, (char *)(vwv
+ 8));
1772 cli
->readbraw_supported
= ((SVAL(vwv
+ 5, 0) & 0x1) != 0);
1773 cli
->writebraw_supported
= ((SVAL(vwv
+ 5, 0) & 0x2) != 0);
1774 cli
->secblob
= data_blob(bytes
, num_bytes
);
1776 /* the old core protocol */
1777 cli
->use_spnego
= False
;
1779 cli
->serverzone
= get_time_zone(time(NULL
));
1782 cli
->max_xmit
= MIN(cli
->max_xmit
, CLI_BUFFER_SIZE
);
1784 /* a way to force ascii SMB */
1785 if (getenv("CLI_FORCE_ASCII"))
1786 cli
->capabilities
&= ~CAP_UNICODE
;
1788 tevent_req_done(req
);
1791 NTSTATUS
cli_negprot_recv(struct tevent_req
*req
)
1793 return tevent_req_simple_recv_ntstatus(req
);
1796 NTSTATUS
cli_negprot(struct cli_state
*cli
)
1798 TALLOC_CTX
*frame
= talloc_stackframe();
1799 struct event_context
*ev
;
1800 struct tevent_req
*req
;
1801 NTSTATUS status
= NT_STATUS_OK
;
1803 if (cli_has_async_calls(cli
)) {
1805 * Can't use sync call while an async call is in flight
1807 status
= NT_STATUS_INVALID_PARAMETER
;
1811 ev
= event_context_init(frame
);
1813 status
= NT_STATUS_NO_MEMORY
;
1817 req
= cli_negprot_send(frame
, ev
, cli
);
1819 status
= NT_STATUS_NO_MEMORY
;
1823 if (!tevent_req_poll(req
, ev
)) {
1824 status
= map_nt_error_from_unix(errno
);
1828 status
= cli_negprot_recv(req
);
1831 if (!NT_STATUS_IS_OK(status
)) {
1832 cli_set_error(cli
, status
);
1837 /****************************************************************************
1838 Send a session request. See rfc1002.txt 4.3 and 4.3.2.
1839 ****************************************************************************/
1841 bool cli_session_request(struct cli_state
*cli
,
1842 struct nmb_name
*calling
, struct nmb_name
*called
)
1848 /* 445 doesn't have session request */
1849 if (cli
->port
== 445)
1852 memcpy(&(cli
->calling
), calling
, sizeof(*calling
));
1853 memcpy(&(cli
->called
), called
, sizeof(*called
));
1855 /* put in the destination name */
1857 tmp
= name_mangle(talloc_tos(), cli
->called
.name
,
1858 cli
->called
.name_type
);
1863 p
= cli
->outbuf
+len
;
1864 memcpy(p
, tmp
, name_len(tmp
));
1865 len
+= name_len(tmp
);
1870 tmp
= name_mangle(talloc_tos(), cli
->calling
.name
,
1871 cli
->calling
.name_type
);
1876 p
= cli
->outbuf
+len
;
1877 memcpy(p
, tmp
, name_len(tmp
));
1878 len
+= name_len(tmp
);
1881 /* send a session request (RFC 1002) */
1882 /* setup the packet length
1883 * Remove four bytes from the length count, since the length
1884 * field in the NBT Session Service header counts the number
1885 * of bytes which follow. The cli_send_smb() function knows
1886 * about this and accounts for those four bytes.
1890 _smb_setlen(cli
->outbuf
,len
);
1891 SCVAL(cli
->outbuf
,0,0x81);
1894 DEBUG(5,("Sent session request\n"));
1896 if (!cli_receive_smb(cli
))
1899 if (CVAL(cli
->inbuf
,0) == 0x84) {
1900 /* C. Hoch 9/14/95 Start */
1901 /* For information, here is the response structure.
1902 * We do the byte-twiddling to for portability.
1903 struct RetargetResponse{
1905 unsigned char flags;
1911 uint16_t port
= (CVAL(cli
->inbuf
,8)<<8)+CVAL(cli
->inbuf
,9);
1912 struct in_addr dest_ip
;
1915 /* SESSION RETARGET */
1916 putip((char *)&dest_ip
,cli
->inbuf
+4);
1917 in_addr_to_sockaddr_storage(&cli
->dest_ss
, dest_ip
);
1919 status
= open_socket_out(&cli
->dest_ss
, port
,
1920 LONG_CONNECT_TIMEOUT
, &cli
->fd
);
1921 if (!NT_STATUS_IS_OK(status
)) {
1925 DEBUG(3,("Retargeted\n"));
1927 set_socket_options(cli
->fd
, lp_socket_options());
1934 DEBUG(0,("Retarget recursion - failing\n"));
1938 ret
= cli_session_request(cli
, calling
, called
);
1942 } /* C. Hoch 9/14/95 End */
1944 if (CVAL(cli
->inbuf
,0) != 0x82) {
1945 /* This is the wrong place to put the error... JRA. */
1946 cli
->rap_error
= CVAL(cli
->inbuf
,4);
1956 static void smb_sock_connected(struct tevent_req
*req
)
1958 struct fd_struct
*pfd
= tevent_req_callback_data(
1959 req
, struct fd_struct
);
1963 status
= open_socket_out_defer_recv(req
, &fd
);
1964 if (NT_STATUS_IS_OK(status
)) {
1969 static NTSTATUS
open_smb_socket(const struct sockaddr_storage
*pss
,
1970 uint16_t *port
, int timeout
, int *pfd
)
1972 struct event_context
*ev
;
1973 struct tevent_req
*r139
, *r445
;
1974 struct fd_struct
*fd139
, *fd445
;
1975 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
1978 return open_socket_out(pss
, *port
, timeout
, pfd
);
1981 ev
= event_context_init(talloc_tos());
1983 return NT_STATUS_NO_MEMORY
;
1986 fd139
= talloc(ev
, struct fd_struct
);
1987 if (fd139
== NULL
) {
1992 fd445
= talloc(ev
, struct fd_struct
);
1993 if (fd445
== NULL
) {
1998 r445
= open_socket_out_defer_send(ev
, ev
, timeval_set(0, 0),
2000 r139
= open_socket_out_defer_send(ev
, ev
, timeval_set(0, 3000),
2002 if ((r445
== NULL
) || (r139
== NULL
)) {
2005 tevent_req_set_callback(r445
, smb_sock_connected
, fd445
);
2006 tevent_req_set_callback(r139
, smb_sock_connected
, fd139
);
2008 while ((fd445
->fd
== -1) && (fd139
->fd
== -1)
2009 && (tevent_req_is_in_progress(r139
)
2010 || tevent_req_is_in_progress(r445
))) {
2011 event_loop_once(ev
);
2014 if ((fd139
->fd
!= -1) && (fd445
->fd
!= -1)) {
2019 if (fd445
->fd
!= -1) {
2022 status
= NT_STATUS_OK
;
2025 if (fd139
->fd
!= -1) {
2028 status
= NT_STATUS_OK
;
2032 status
= open_socket_out_defer_recv(r445
, &fd445
->fd
);
2038 /****************************************************************************
2039 Open the client sockets.
2040 ****************************************************************************/
2042 NTSTATUS
cli_connect(struct cli_state
*cli
,
2044 struct sockaddr_storage
*dest_ss
)
2047 int name_type
= 0x20;
2048 TALLOC_CTX
*frame
= talloc_stackframe();
2049 unsigned int num_addrs
= 0;
2051 struct sockaddr_storage
*ss_arr
= NULL
;
2054 /* reasonable default hostname */
2056 host
= STAR_SMBSERVER
;
2059 fstrcpy(cli
->desthost
, host
);
2061 /* allow hostnames of the form NAME#xx and do a netbios lookup */
2062 if ((p
= strchr(cli
->desthost
, '#'))) {
2063 name_type
= strtol(p
+1, NULL
, 16);
2067 if (!dest_ss
|| is_zero_addr((struct sockaddr
*)dest_ss
)) {
2068 NTSTATUS status
=resolve_name_list(frame
,
2073 if (!NT_STATUS_IS_OK(status
)) {
2075 return NT_STATUS_BAD_NETWORK_NAME
;
2079 ss_arr
= TALLOC_P(frame
, struct sockaddr_storage
);
2082 return NT_STATUS_NO_MEMORY
;
2087 for (i
= 0; i
< num_addrs
; i
++) {
2088 cli
->dest_ss
= ss_arr
[i
];
2089 if (getenv("LIBSMB_PROG")) {
2090 cli
->fd
= sock_exec(getenv("LIBSMB_PROG"));
2092 uint16_t port
= cli
->port
;
2094 status
= open_smb_socket(&cli
->dest_ss
, &port
,
2095 cli
->timeout
, &cli
->fd
);
2096 if (NT_STATUS_IS_OK(status
)) {
2100 if (cli
->fd
== -1) {
2101 char addr
[INET6_ADDRSTRLEN
];
2102 print_sockaddr(addr
, sizeof(addr
), &ss_arr
[i
]);
2103 DEBUG(2,("Error connecting to %s (%s)\n",
2104 dest_ss
?addr
:host
,strerror(errno
)));
2106 /* Exit from loop on first connection. */
2111 if (cli
->fd
== -1) {
2113 return map_nt_error_from_unix(errno
);
2117 *dest_ss
= cli
->dest_ss
;
2120 set_socket_options(cli
->fd
, lp_socket_options());
2123 return NT_STATUS_OK
;
2127 establishes a connection to after the negprot.
2128 @param output_cli A fully initialised cli structure, non-null only on success
2129 @param dest_host The netbios name of the remote host
2130 @param dest_ss (optional) The the destination IP, NULL for name based lookup
2131 @param port (optional) The destination port (0 for default)
2132 @param retry bool. Did this connection fail with a retryable error ?
2135 NTSTATUS
cli_start_connection(struct cli_state
**output_cli
,
2136 const char *my_name
,
2137 const char *dest_host
,
2138 struct sockaddr_storage
*dest_ss
, int port
,
2139 int signing_state
, int flags
,
2143 struct nmb_name calling
;
2144 struct nmb_name called
;
2145 struct cli_state
*cli
;
2146 struct sockaddr_storage ss
;
2152 my_name
= global_myname();
2154 if (!(cli
= cli_initialise_ex(signing_state
))) {
2155 return NT_STATUS_NO_MEMORY
;
2158 make_nmb_name(&calling
, my_name
, 0x0);
2159 make_nmb_name(&called
, dest_host
, 0x20);
2161 cli_set_port(cli
, port
);
2162 cli_set_timeout(cli
, 10000); /* 10 seconds. */
2172 DEBUG(3,("Connecting to host=%s\n", dest_host
));
2174 nt_status
= cli_connect(cli
, dest_host
, &ss
);
2175 if (!NT_STATUS_IS_OK(nt_status
)) {
2176 char addr
[INET6_ADDRSTRLEN
];
2177 print_sockaddr(addr
, sizeof(addr
), &ss
);
2178 DEBUG(1,("cli_start_connection: failed to connect to %s (%s). Error %s\n",
2179 nmb_namestr(&called
), addr
, nt_errstr(nt_status
) ));
2187 if (!cli_session_request(cli
, &calling
, &called
)) {
2189 DEBUG(1,("session request to %s failed (%s)\n",
2190 called
.name
, cli_errstr(cli
)));
2191 if ((p
=strchr(called
.name
, '.')) && !is_ipaddress(called
.name
)) {
2195 if (strcmp(called
.name
, STAR_SMBSERVER
)) {
2196 make_nmb_name(&called
, STAR_SMBSERVER
, 0x20);
2199 return NT_STATUS_BAD_NETWORK_NAME
;
2202 if (flags
& CLI_FULL_CONNECTION_DONT_SPNEGO
)
2203 cli
->use_spnego
= False
;
2204 else if (flags
& CLI_FULL_CONNECTION_USE_KERBEROS
)
2205 cli
->use_kerberos
= True
;
2207 if ((flags
& CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
) &&
2208 cli
->use_kerberos
) {
2209 cli
->fallback_after_kerberos
= true;
2212 nt_status
= cli_negprot(cli
);
2213 if (!NT_STATUS_IS_OK(nt_status
)) {
2214 DEBUG(1, ("failed negprot: %s\n", nt_errstr(nt_status
)));
2220 return NT_STATUS_OK
;
2225 establishes a connection right up to doing tconX, password specified.
2226 @param output_cli A fully initialised cli structure, non-null only on success
2227 @param dest_host The netbios name of the remote host
2228 @param dest_ip (optional) The the destination IP, NULL for name based lookup
2229 @param port (optional) The destination port (0 for default)
2230 @param service (optional) The share to make the connection to. Should be 'unqualified' in any way.
2231 @param service_type The 'type' of serivice.
2232 @param user Username, unix string
2233 @param domain User's domain
2234 @param password User's password, unencrypted unix string.
2235 @param retry bool. Did this connection fail with a retryable error ?
2238 NTSTATUS
cli_full_connection(struct cli_state
**output_cli
,
2239 const char *my_name
,
2240 const char *dest_host
,
2241 struct sockaddr_storage
*dest_ss
, int port
,
2242 const char *service
, const char *service_type
,
2243 const char *user
, const char *domain
,
2244 const char *password
, int flags
,
2249 struct cli_state
*cli
= NULL
;
2250 int pw_len
= password
? strlen(password
)+1 : 0;
2254 if (password
== NULL
) {
2258 nt_status
= cli_start_connection(&cli
, my_name
, dest_host
,
2259 dest_ss
, port
, signing_state
,
2262 if (!NT_STATUS_IS_OK(nt_status
)) {
2266 cli
->use_oplocks
= ((flags
& CLI_FULL_CONNECTION_OPLOCKS
) != 0);
2267 cli
->use_level_II_oplocks
=
2268 ((flags
& CLI_FULL_CONNECTION_LEVEL_II_OPLOCKS
) != 0);
2270 nt_status
= cli_session_setup(cli
, user
, password
, pw_len
, password
,
2272 if (!NT_STATUS_IS_OK(nt_status
)) {
2274 if (!(flags
& CLI_FULL_CONNECTION_ANONYMOUS_FALLBACK
)) {
2275 DEBUG(1,("failed session setup with %s\n",
2276 nt_errstr(nt_status
)));
2281 nt_status
= cli_session_setup(cli
, "", "", 0, "", 0, domain
);
2282 if (!NT_STATUS_IS_OK(nt_status
)) {
2283 DEBUG(1,("anonymous failed session setup with %s\n",
2284 nt_errstr(nt_status
)));
2291 nt_status
= cli_tcon_andx(cli
, service
, service_type
, password
,
2293 if (!NT_STATUS_IS_OK(nt_status
)) {
2294 DEBUG(1,("failed tcon_X with %s\n", nt_errstr(nt_status
)));
2296 if (NT_STATUS_IS_OK(nt_status
)) {
2297 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2303 nt_status
= cli_init_creds(cli
, user
, domain
, password
);
2304 if (!NT_STATUS_IS_OK(nt_status
)) {
2310 return NT_STATUS_OK
;
2313 /****************************************************************************
2314 Attempt a NetBIOS session request, falling back to *SMBSERVER if needed.
2315 ****************************************************************************/
2317 bool attempt_netbios_session_request(struct cli_state
**ppcli
, const char *srchost
, const char *desthost
,
2318 struct sockaddr_storage
*pdest_ss
)
2320 struct nmb_name calling
, called
;
2322 make_nmb_name(&calling
, srchost
, 0x0);
2325 * If the called name is an IP address
2326 * then use *SMBSERVER immediately.
2329 if(is_ipaddress(desthost
)) {
2330 make_nmb_name(&called
, STAR_SMBSERVER
, 0x20);
2332 make_nmb_name(&called
, desthost
, 0x20);
2335 if (!cli_session_request(*ppcli
, &calling
, &called
)) {
2337 struct nmb_name smbservername
;
2339 make_nmb_name(&smbservername
, STAR_SMBSERVER
, 0x20);
2342 * If the name wasn't *SMBSERVER then
2343 * try with *SMBSERVER if the first name fails.
2346 if (nmb_name_equal(&called
, &smbservername
)) {
2349 * The name used was *SMBSERVER, don't bother with another name.
2352 DEBUG(0,("attempt_netbios_session_request: %s rejected the session for name *SMBSERVER \
2353 with error %s.\n", desthost
, cli_errstr(*ppcli
) ));
2358 cli_shutdown(*ppcli
);
2360 *ppcli
= cli_initialise();
2362 /* Out of memory... */
2366 status
= cli_connect(*ppcli
, desthost
, pdest_ss
);
2367 if (!NT_STATUS_IS_OK(status
) ||
2368 !cli_session_request(*ppcli
, &calling
, &smbservername
)) {
2369 DEBUG(0,("attempt_netbios_session_request: %s rejected the session for \
2370 name *SMBSERVER with error %s\n", desthost
, cli_errstr(*ppcli
) ));
2378 /****************************************************************************
2379 Send an old style tcon.
2380 ****************************************************************************/
2381 NTSTATUS
cli_raw_tcon(struct cli_state
*cli
,
2382 const char *service
, const char *pass
, const char *dev
,
2383 uint16
*max_xmit
, uint16
*tid
)
2387 if (!lp_client_plaintext_auth() && (*pass
)) {
2388 DEBUG(1, ("Server requested plaintext password but 'client "
2389 "plaintext auth' is disabled\n"));
2390 return NT_STATUS_ACCESS_DENIED
;
2393 memset(cli
->outbuf
,'\0',smb_size
);
2394 memset(cli
->inbuf
,'\0',smb_size
);
2396 cli_set_message(cli
->outbuf
, 0, 0, True
);
2397 SCVAL(cli
->outbuf
,smb_com
,SMBtcon
);
2398 cli_setup_packet(cli
);
2400 p
= smb_buf(cli
->outbuf
);
2401 *p
++ = 4; p
+= clistr_push(cli
, p
, service
, -1, STR_TERMINATE
| STR_NOALIGN
);
2402 *p
++ = 4; p
+= clistr_push(cli
, p
, pass
, -1, STR_TERMINATE
| STR_NOALIGN
);
2403 *p
++ = 4; p
+= clistr_push(cli
, p
, dev
, -1, STR_TERMINATE
| STR_NOALIGN
);
2405 cli_setup_bcc(cli
, p
);
2408 if (!cli_receive_smb(cli
)) {
2409 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
2412 if (cli_is_error(cli
)) {
2413 return cli_nt_error(cli
);
2416 *max_xmit
= SVAL(cli
->inbuf
, smb_vwv0
);
2417 *tid
= SVAL(cli
->inbuf
, smb_vwv1
);
2419 return NT_STATUS_OK
;
2422 /* Return a cli_state pointing at the IPC$ share for the given server */
2424 struct cli_state
*get_ipc_connect(char *server
,
2425 struct sockaddr_storage
*server_ss
,
2426 const struct user_auth_info
*user_info
)
2428 struct cli_state
*cli
;
2430 uint32_t flags
= CLI_FULL_CONNECTION_ANONYMOUS_FALLBACK
;
2432 if (user_info
->use_kerberos
) {
2433 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
2436 nt_status
= cli_full_connection(&cli
, NULL
, server
, server_ss
, 0, "IPC$", "IPC",
2437 user_info
->username
? user_info
->username
: "",
2439 user_info
->password
? user_info
->password
: "",
2443 if (NT_STATUS_IS_OK(nt_status
)) {
2445 } else if (is_ipaddress(server
)) {
2446 /* windows 9* needs a correct NMB name for connections */
2447 fstring remote_name
;
2449 if (name_status_find("*", 0, 0, server_ss
, remote_name
)) {
2450 cli
= get_ipc_connect(remote_name
, server_ss
, user_info
);
2459 * Given the IP address of a master browser on the network, return its
2460 * workgroup and connect to it.
2462 * This function is provided to allow additional processing beyond what
2463 * get_ipc_connect_master_ip_bcast() does, e.g. to retrieve the list of master
2464 * browsers and obtain each master browsers' list of domains (in case the
2465 * first master browser is recently on the network and has not yet
2466 * synchronized with other master browsers and therefore does not yet have the
2467 * entire network browse list)
2470 struct cli_state
*get_ipc_connect_master_ip(TALLOC_CTX
*ctx
,
2471 struct ip_service
*mb_ip
,
2472 const struct user_auth_info
*user_info
,
2473 char **pp_workgroup_out
)
2475 char addr
[INET6_ADDRSTRLEN
];
2477 struct cli_state
*cli
;
2478 struct sockaddr_storage server_ss
;
2480 *pp_workgroup_out
= NULL
;
2482 print_sockaddr(addr
, sizeof(addr
), &mb_ip
->ss
);
2483 DEBUG(99, ("Looking up name of master browser %s\n",
2487 * Do a name status query to find out the name of the master browser.
2488 * We use <01><02>__MSBROWSE__<02>#01 if *#00 fails because a domain
2489 * master browser will not respond to a wildcard query (or, at least,
2490 * an NT4 server acting as the domain master browser will not).
2492 * We might be able to use ONLY the query on MSBROWSE, but that's not
2493 * yet been tested with all Windows versions, so until it is, leave
2494 * the original wildcard query as the first choice and fall back to
2495 * MSBROWSE if the wildcard query fails.
2497 if (!name_status_find("*", 0, 0x1d, &mb_ip
->ss
, name
) &&
2498 !name_status_find(MSBROWSE
, 1, 0x1d, &mb_ip
->ss
, name
)) {
2500 DEBUG(99, ("Could not retrieve name status for %s\n",
2505 if (!find_master_ip(name
, &server_ss
)) {
2506 DEBUG(99, ("Could not find master ip for %s\n", name
));
2510 *pp_workgroup_out
= talloc_strdup(ctx
, name
);
2512 DEBUG(4, ("found master browser %s, %s\n", name
, addr
));
2514 print_sockaddr(addr
, sizeof(addr
), &server_ss
);
2515 cli
= get_ipc_connect(addr
, &server_ss
, user_info
);
2521 * Return the IP address and workgroup of a master browser on the network, and
2525 struct cli_state
*get_ipc_connect_master_ip_bcast(TALLOC_CTX
*ctx
,
2526 const struct user_auth_info
*user_info
,
2527 char **pp_workgroup_out
)
2529 struct ip_service
*ip_list
;
2530 struct cli_state
*cli
;
2533 *pp_workgroup_out
= NULL
;
2535 DEBUG(99, ("Do broadcast lookup for workgroups on local network\n"));
2537 /* Go looking for workgroups by broadcasting on the local network */
2539 if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE
, 1, &ip_list
,
2541 DEBUG(99, ("No master browsers responded\n"));
2545 for (i
= 0; i
< count
; i
++) {
2546 char addr
[INET6_ADDRSTRLEN
];
2547 print_sockaddr(addr
, sizeof(addr
), &ip_list
[i
].ss
);
2548 DEBUG(99, ("Found master browser %s\n", addr
));
2550 cli
= get_ipc_connect_master_ip(ctx
, &ip_list
[i
],
2551 user_info
, pp_workgroup_out
);