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/>.
28 {PROTOCOL_CORE
,"PC NETWORK PROGRAM 1.0"},
29 {PROTOCOL_COREPLUS
,"MICROSOFT NETWORKS 1.03"},
30 {PROTOCOL_LANMAN1
,"MICROSOFT NETWORKS 3.0"},
31 {PROTOCOL_LANMAN1
,"LANMAN1.0"},
32 {PROTOCOL_LANMAN2
,"LM1.2X002"},
33 {PROTOCOL_LANMAN2
,"DOS LANMAN2.1"},
34 {PROTOCOL_LANMAN2
,"LANMAN2.1"},
35 {PROTOCOL_LANMAN2
,"Samba"},
36 {PROTOCOL_NT1
,"NT LANMAN 1.0"},
37 {PROTOCOL_NT1
,"NT LM 0.12"},
41 static const char *star_smbserver_name
= "*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
;
69 if (passlen
> sizeof(pword
)-1) {
70 return NT_STATUS_INVALID_PARAMETER
;
73 /* LANMAN servers predate NT status codes and Unicode and ignore those
74 smb flags so we must disable the corresponding default capabilities
75 that would otherwise cause the Unicode and NT Status flags to be
76 set (and even returned by the server) */
78 cli
->capabilities
&= ~(CAP_UNICODE
| CAP_STATUS32
);
80 /* if in share level security then don't send a password now */
81 if (!(cli
->sec_mode
& NEGOTIATE_SECURITY_USER_LEVEL
))
84 if (passlen
> 0 && (cli
->sec_mode
& NEGOTIATE_SECURITY_CHALLENGE_RESPONSE
) && passlen
!= 24) {
85 /* Encrypted mode needed, and non encrypted password supplied. */
86 lm_response
= data_blob(NULL
, 24);
87 if (!SMBencrypt(pass
, cli
->secblob
.data
,(uchar
*)lm_response
.data
)) {
88 DEBUG(1, ("Password is > 14 chars in length, and is therefore incompatible with Lanman authentication\n"));
89 return NT_STATUS_ACCESS_DENIED
;
91 } else if ((cli
->sec_mode
& NEGOTIATE_SECURITY_CHALLENGE_RESPONSE
) && passlen
== 24) {
92 /* Encrypted mode needed, and encrypted password supplied. */
93 lm_response
= data_blob(pass
, passlen
);
94 } else if (passlen
> 0) {
95 /* Plaintext mode needed, assume plaintext supplied. */
96 passlen
= clistr_push(cli
, pword
, pass
, sizeof(pword
), STR_TERMINATE
);
97 lm_response
= data_blob(pass
, passlen
);
100 /* send a session setup command */
101 memset(cli
->outbuf
,'\0',smb_size
);
102 cli_set_message(cli
->outbuf
,10, 0, True
);
103 SCVAL(cli
->outbuf
,smb_com
,SMBsesssetupX
);
104 cli_setup_packet(cli
);
106 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
107 SSVAL(cli
->outbuf
,smb_vwv2
,cli
->max_xmit
);
108 SSVAL(cli
->outbuf
,smb_vwv3
,2);
109 SSVAL(cli
->outbuf
,smb_vwv4
,1);
110 SIVAL(cli
->outbuf
,smb_vwv5
,cli
->sesskey
);
111 SSVAL(cli
->outbuf
,smb_vwv7
,lm_response
.length
);
113 p
= smb_buf(cli
->outbuf
);
114 memcpy(p
,lm_response
.data
,lm_response
.length
);
115 p
+= lm_response
.length
;
116 p
+= clistr_push(cli
, p
, user
, -1, STR_TERMINATE
|STR_UPPER
);
117 p
+= clistr_push(cli
, p
, workgroup
, -1, STR_TERMINATE
|STR_UPPER
);
118 p
+= clistr_push(cli
, p
, "Unix", -1, STR_TERMINATE
);
119 p
+= clistr_push(cli
, p
, "Samba", -1, STR_TERMINATE
);
120 cli_setup_bcc(cli
, p
);
122 if (!cli_send_smb(cli
) || !cli_receive_smb(cli
)) {
123 return cli_nt_error(cli
);
126 show_msg(cli
->inbuf
);
128 if (cli_is_error(cli
)) {
129 return cli_nt_error(cli
);
132 /* use the returned vuid from now on */
133 cli
->vuid
= SVAL(cli
->inbuf
,smb_uid
);
134 fstrcpy(cli
->user_name
, user
);
136 if (session_key
.data
) {
137 /* Have plaintext orginal */
138 cli_set_session_key(cli
, session_key
);
144 /****************************************************************************
145 Work out suitable capabilities to offer the server.
146 ****************************************************************************/
148 static uint32
cli_session_setup_capabilities(struct cli_state
*cli
)
150 uint32 capabilities
= CAP_NT_SMBS
;
152 if (!cli
->force_dos_errors
)
153 capabilities
|= CAP_STATUS32
;
155 if (cli
->use_level_II_oplocks
)
156 capabilities
|= CAP_LEVEL_II_OPLOCKS
;
158 capabilities
|= (cli
->capabilities
& (CAP_UNICODE
|CAP_LARGE_FILES
|CAP_LARGE_READX
|CAP_LARGE_WRITEX
|CAP_DFS
));
162 /****************************************************************************
163 Do a NT1 guest session setup.
164 ****************************************************************************/
166 static NTSTATUS
cli_session_setup_guest(struct cli_state
*cli
)
169 uint32 capabilities
= cli_session_setup_capabilities(cli
);
171 memset(cli
->outbuf
, '\0', smb_size
);
172 cli_set_message(cli
->outbuf
,13,0,True
);
173 SCVAL(cli
->outbuf
,smb_com
,SMBsesssetupX
);
174 cli_setup_packet(cli
);
176 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
177 SSVAL(cli
->outbuf
,smb_vwv2
,CLI_BUFFER_SIZE
);
178 SSVAL(cli
->outbuf
,smb_vwv3
,2);
179 SSVAL(cli
->outbuf
,smb_vwv4
,cli
->pid
);
180 SIVAL(cli
->outbuf
,smb_vwv5
,cli
->sesskey
);
181 SSVAL(cli
->outbuf
,smb_vwv7
,0);
182 SSVAL(cli
->outbuf
,smb_vwv8
,0);
183 SIVAL(cli
->outbuf
,smb_vwv11
,capabilities
);
184 p
= smb_buf(cli
->outbuf
);
185 p
+= clistr_push(cli
, p
, "", -1, STR_TERMINATE
); /* username */
186 p
+= clistr_push(cli
, p
, "", -1, STR_TERMINATE
); /* workgroup */
187 p
+= clistr_push(cli
, p
, "Unix", -1, STR_TERMINATE
);
188 p
+= clistr_push(cli
, p
, "Samba", -1, STR_TERMINATE
);
189 cli_setup_bcc(cli
, p
);
191 if (!cli_send_smb(cli
) || !cli_receive_smb(cli
)) {
192 return cli_nt_error(cli
);
195 show_msg(cli
->inbuf
);
197 if (cli_is_error(cli
)) {
198 return cli_nt_error(cli
);
201 cli
->vuid
= SVAL(cli
->inbuf
,smb_uid
);
203 p
= smb_buf(cli
->inbuf
);
204 p
+= clistr_pull(cli
, cli
->server_os
, p
, sizeof(fstring
), -1, STR_TERMINATE
);
205 p
+= clistr_pull(cli
, cli
->server_type
, p
, sizeof(fstring
), -1, STR_TERMINATE
);
206 p
+= clistr_pull(cli
, cli
->server_domain
, p
, sizeof(fstring
), -1, STR_TERMINATE
);
208 if (strstr(cli
->server_type
, "Samba")) {
209 cli
->is_samba
= True
;
212 fstrcpy(cli
->user_name
, "");
217 /****************************************************************************
218 Do a NT1 plaintext session setup.
219 ****************************************************************************/
221 static NTSTATUS
cli_session_setup_plaintext(struct cli_state
*cli
,
222 const char *user
, const char *pass
,
223 const char *workgroup
)
225 uint32 capabilities
= cli_session_setup_capabilities(cli
);
229 fstr_sprintf( lanman
, "Samba %s", SAMBA_VERSION_STRING
);
231 memset(cli
->outbuf
, '\0', smb_size
);
232 cli_set_message(cli
->outbuf
,13,0,True
);
233 SCVAL(cli
->outbuf
,smb_com
,SMBsesssetupX
);
234 cli_setup_packet(cli
);
236 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
237 SSVAL(cli
->outbuf
,smb_vwv2
,CLI_BUFFER_SIZE
);
238 SSVAL(cli
->outbuf
,smb_vwv3
,2);
239 SSVAL(cli
->outbuf
,smb_vwv4
,cli
->pid
);
240 SIVAL(cli
->outbuf
,smb_vwv5
,cli
->sesskey
);
241 SSVAL(cli
->outbuf
,smb_vwv8
,0);
242 SIVAL(cli
->outbuf
,smb_vwv11
,capabilities
);
243 p
= smb_buf(cli
->outbuf
);
245 /* check wether to send the ASCII or UNICODE version of the password */
247 if ( (capabilities
& CAP_UNICODE
) == 0 ) {
248 p
+= clistr_push(cli
, p
, pass
, -1, STR_TERMINATE
); /* password */
249 SSVAL(cli
->outbuf
,smb_vwv7
,PTR_DIFF(p
, smb_buf(cli
->outbuf
)));
252 /* For ucs2 passwords clistr_push calls ucs2_align, which causes
253 * the space taken by the unicode password to be one byte too
254 * long (as we're on an odd byte boundary here). Reduce the
255 * count by 1 to cope with this. Fixes smbclient against NetApp
256 * servers which can't cope. Fix from
257 * bryan.kolodziej@allenlund.com in bug #3840.
259 p
+= clistr_push(cli
, p
, pass
, -1, STR_UNICODE
|STR_TERMINATE
); /* unicode password */
260 SSVAL(cli
->outbuf
,smb_vwv8
,PTR_DIFF(p
, smb_buf(cli
->outbuf
))-1);
263 p
+= clistr_push(cli
, p
, user
, -1, STR_TERMINATE
); /* username */
264 p
+= clistr_push(cli
, p
, workgroup
, -1, STR_TERMINATE
); /* workgroup */
265 p
+= clistr_push(cli
, p
, "Unix", -1, STR_TERMINATE
);
266 p
+= clistr_push(cli
, p
, lanman
, -1, STR_TERMINATE
);
267 cli_setup_bcc(cli
, p
);
269 if (!cli_send_smb(cli
) || !cli_receive_smb(cli
)) {
270 return cli_nt_error(cli
);
273 show_msg(cli
->inbuf
);
275 if (cli_is_error(cli
)) {
276 return cli_nt_error(cli
);
279 cli
->vuid
= SVAL(cli
->inbuf
,smb_uid
);
280 p
= smb_buf(cli
->inbuf
);
281 p
+= clistr_pull(cli
, cli
->server_os
, p
, sizeof(fstring
), -1, STR_TERMINATE
);
282 p
+= clistr_pull(cli
, cli
->server_type
, p
, sizeof(fstring
), -1, STR_TERMINATE
);
283 p
+= clistr_pull(cli
, cli
->server_domain
, p
, sizeof(fstring
), -1, STR_TERMINATE
);
284 fstrcpy(cli
->user_name
, user
);
286 if (strstr(cli
->server_type
, "Samba")) {
287 cli
->is_samba
= True
;
293 /****************************************************************************
294 do a NT1 NTLM/LM encrypted session setup - for when extended security
296 @param cli client state to create do session setup on
298 @param pass *either* cleartext password (passlen !=24) or LM response.
299 @param ntpass NT response, implies ntpasslen >=24, implies pass is not clear
300 @param workgroup The user's domain.
301 ****************************************************************************/
303 static NTSTATUS
cli_session_setup_nt1(struct cli_state
*cli
, const char *user
,
304 const char *pass
, size_t passlen
,
305 const char *ntpass
, size_t ntpasslen
,
306 const char *workgroup
)
308 uint32 capabilities
= cli_session_setup_capabilities(cli
);
309 DATA_BLOB lm_response
= data_blob_null
;
310 DATA_BLOB nt_response
= data_blob_null
;
311 DATA_BLOB session_key
= data_blob_null
;
316 /* do nothing - guest login */
317 } else if (passlen
!= 24) {
318 if (lp_client_ntlmv2_auth()) {
319 DATA_BLOB server_chal
;
320 DATA_BLOB names_blob
;
321 server_chal
= data_blob(cli
->secblob
.data
, MIN(cli
->secblob
.length
, 8));
323 /* note that the 'workgroup' here is a best guess - we don't know
324 the server's domain at this point. The 'server name' is also
327 names_blob
= NTLMv2_generate_names_blob(cli
->called
.name
, workgroup
);
329 if (!SMBNTLMv2encrypt(user
, workgroup
, pass
, &server_chal
,
331 &lm_response
, &nt_response
, &session_key
)) {
332 data_blob_free(&names_blob
);
333 data_blob_free(&server_chal
);
334 return NT_STATUS_ACCESS_DENIED
;
336 data_blob_free(&names_blob
);
337 data_blob_free(&server_chal
);
341 E_md4hash(pass
, nt_hash
);
344 nt_response
= data_blob_null
;
346 nt_response
= data_blob(NULL
, 24);
347 SMBNTencrypt(pass
,cli
->secblob
.data
,nt_response
.data
);
349 /* non encrypted password supplied. Ignore ntpass. */
350 if (lp_client_lanman_auth()) {
351 lm_response
= data_blob(NULL
, 24);
352 if (!SMBencrypt(pass
,cli
->secblob
.data
, lm_response
.data
)) {
353 /* Oops, the LM response is invalid, just put
354 the NT response there instead */
355 data_blob_free(&lm_response
);
356 lm_response
= data_blob(nt_response
.data
, nt_response
.length
);
359 /* LM disabled, place NT# in LM field instead */
360 lm_response
= data_blob(nt_response
.data
, nt_response
.length
);
363 session_key
= data_blob(NULL
, 16);
365 E_deshash(pass
, session_key
.data
);
366 memset(&session_key
.data
[8], '\0', 8);
368 SMBsesskeygen_ntv1(nt_hash
, NULL
, session_key
.data
);
372 cli_simple_set_signing(cli
, session_key
, lm_response
);
374 cli_simple_set_signing(cli
, session_key
, nt_response
);
377 /* pre-encrypted password supplied. Only used for
378 security=server, can't do
379 signing because we don't have original key */
381 lm_response
= data_blob(pass
, passlen
);
382 nt_response
= data_blob(ntpass
, ntpasslen
);
385 /* send a session setup command */
386 memset(cli
->outbuf
,'\0',smb_size
);
388 cli_set_message(cli
->outbuf
,13,0,True
);
389 SCVAL(cli
->outbuf
,smb_com
,SMBsesssetupX
);
390 cli_setup_packet(cli
);
392 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
393 SSVAL(cli
->outbuf
,smb_vwv2
,CLI_BUFFER_SIZE
);
394 SSVAL(cli
->outbuf
,smb_vwv3
,2);
395 SSVAL(cli
->outbuf
,smb_vwv4
,cli
->pid
);
396 SIVAL(cli
->outbuf
,smb_vwv5
,cli
->sesskey
);
397 SSVAL(cli
->outbuf
,smb_vwv7
,lm_response
.length
);
398 SSVAL(cli
->outbuf
,smb_vwv8
,nt_response
.length
);
399 SIVAL(cli
->outbuf
,smb_vwv11
,capabilities
);
400 p
= smb_buf(cli
->outbuf
);
401 if (lm_response
.length
) {
402 memcpy(p
,lm_response
.data
, lm_response
.length
); p
+= lm_response
.length
;
404 if (nt_response
.length
) {
405 memcpy(p
,nt_response
.data
, nt_response
.length
); p
+= nt_response
.length
;
407 p
+= clistr_push(cli
, p
, user
, -1, STR_TERMINATE
);
409 /* Upper case here might help some NTLMv2 implementations */
410 p
+= clistr_push(cli
, p
, workgroup
, -1, STR_TERMINATE
|STR_UPPER
);
411 p
+= clistr_push(cli
, p
, "Unix", -1, STR_TERMINATE
);
412 p
+= clistr_push(cli
, p
, "Samba", -1, STR_TERMINATE
);
413 cli_setup_bcc(cli
, p
);
415 if (!cli_send_smb(cli
) || !cli_receive_smb(cli
)) {
416 result
= cli_nt_error(cli
);
420 /* show_msg(cli->inbuf); */
422 if (cli_is_error(cli
)) {
423 result
= cli_nt_error(cli
);
427 /* use the returned vuid from now on */
428 cli
->vuid
= SVAL(cli
->inbuf
,smb_uid
);
430 p
= smb_buf(cli
->inbuf
);
431 p
+= clistr_pull(cli
, cli
->server_os
, p
, sizeof(fstring
), -1, STR_TERMINATE
);
432 p
+= clistr_pull(cli
, cli
->server_type
, p
, sizeof(fstring
), -1, STR_TERMINATE
);
433 p
+= clistr_pull(cli
, cli
->server_domain
, p
, sizeof(fstring
), -1, STR_TERMINATE
);
435 if (strstr(cli
->server_type
, "Samba")) {
436 cli
->is_samba
= True
;
439 fstrcpy(cli
->user_name
, user
);
441 if (session_key
.data
) {
442 /* Have plaintext orginal */
443 cli_set_session_key(cli
, session_key
);
446 result
= NT_STATUS_OK
;
448 data_blob_free(&lm_response
);
449 data_blob_free(&nt_response
);
450 data_blob_free(&session_key
);
454 /****************************************************************************
455 Send a extended security session setup blob
456 ****************************************************************************/
458 static bool cli_session_setup_blob_send(struct cli_state
*cli
, DATA_BLOB blob
)
460 uint32 capabilities
= cli_session_setup_capabilities(cli
);
463 capabilities
|= CAP_EXTENDED_SECURITY
;
465 /* send a session setup command */
466 memset(cli
->outbuf
,'\0',smb_size
);
468 cli_set_message(cli
->outbuf
,12,0,True
);
469 SCVAL(cli
->outbuf
,smb_com
,SMBsesssetupX
);
471 cli_setup_packet(cli
);
473 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
474 SSVAL(cli
->outbuf
,smb_vwv2
,CLI_BUFFER_SIZE
);
475 SSVAL(cli
->outbuf
,smb_vwv3
,2);
476 SSVAL(cli
->outbuf
,smb_vwv4
,1);
477 SIVAL(cli
->outbuf
,smb_vwv5
,0);
478 SSVAL(cli
->outbuf
,smb_vwv7
,blob
.length
);
479 SIVAL(cli
->outbuf
,smb_vwv10
,capabilities
);
480 p
= smb_buf(cli
->outbuf
);
481 memcpy(p
, blob
.data
, blob
.length
);
483 p
+= clistr_push(cli
, p
, "Unix", -1, STR_TERMINATE
);
484 p
+= clistr_push(cli
, p
, "Samba", -1, STR_TERMINATE
);
485 cli_setup_bcc(cli
, p
);
486 return cli_send_smb(cli
);
489 /****************************************************************************
490 Send a extended security session setup blob, returning a reply blob.
491 ****************************************************************************/
493 static DATA_BLOB
cli_session_setup_blob_receive(struct cli_state
*cli
)
495 DATA_BLOB blob2
= data_blob_null
;
499 if (!cli_receive_smb(cli
))
502 show_msg(cli
->inbuf
);
504 if (cli_is_error(cli
) && !NT_STATUS_EQUAL(cli_nt_error(cli
),
505 NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
509 /* use the returned vuid from now on */
510 cli
->vuid
= SVAL(cli
->inbuf
,smb_uid
);
512 p
= smb_buf(cli
->inbuf
);
514 blob2
= data_blob(p
, SVAL(cli
->inbuf
, smb_vwv3
));
517 p
+= clistr_pull(cli
, cli
->server_os
, p
, sizeof(fstring
), -1, STR_TERMINATE
);
519 /* w2k with kerberos doesn't properly null terminate this field */
520 len
= smb_buflen(cli
->inbuf
) - PTR_DIFF(p
, smb_buf(cli
->inbuf
));
521 p
+= clistr_pull(cli
, cli
->server_type
, p
, sizeof(fstring
), len
, 0);
527 /****************************************************************************
528 Send a extended security session setup blob, returning a reply blob.
529 ****************************************************************************/
531 /* The following is calculated from :
533 * (smb_wcnt * 2) = 24 (smb_wcnt == 12 in cli_session_setup_blob_send() )
534 * (strlen("Unix") + 1 + strlen("Samba") + 1) * 2 = 22 (unicode strings at
538 #define BASE_SESSSETUP_BLOB_PACKET_SIZE (35 + 24 + 22)
540 static bool cli_session_setup_blob(struct cli_state
*cli
, DATA_BLOB blob
)
542 int32 remaining
= blob
.length
;
544 DATA_BLOB send_blob
= data_blob_null
;
545 int32 max_blob_size
= 0;
546 DATA_BLOB receive_blob
= data_blob_null
;
548 if (cli
->max_xmit
< BASE_SESSSETUP_BLOB_PACKET_SIZE
+ 1) {
549 DEBUG(0,("cli_session_setup_blob: cli->max_xmit too small "
550 "(was %u, need minimum %u)\n",
551 (unsigned int)cli
->max_xmit
,
552 BASE_SESSSETUP_BLOB_PACKET_SIZE
));
553 cli_set_nt_error(cli
, NT_STATUS_INVALID_PARAMETER
);
557 max_blob_size
= cli
->max_xmit
- BASE_SESSSETUP_BLOB_PACKET_SIZE
;
559 while ( remaining
> 0) {
560 if (remaining
>= max_blob_size
) {
561 send_blob
.length
= max_blob_size
;
562 remaining
-= max_blob_size
;
564 send_blob
.length
= remaining
;
568 send_blob
.data
= &blob
.data
[cur
];
569 cur
+= send_blob
.length
;
571 DEBUG(10, ("cli_session_setup_blob: Remaining (%u) sending (%u) current (%u)\n",
572 (unsigned int)remaining
,
573 (unsigned int)send_blob
.length
,
574 (unsigned int)cur
));
576 if (!cli_session_setup_blob_send(cli
, send_blob
)) {
577 DEBUG(0, ("cli_session_setup_blob: send failed\n"));
581 receive_blob
= cli_session_setup_blob_receive(cli
);
582 data_blob_free(&receive_blob
);
584 if (cli_is_error(cli
) &&
585 !NT_STATUS_EQUAL( cli_get_nt_error(cli
),
586 NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
587 DEBUG(0, ("cli_session_setup_blob: receive failed "
588 "(%s)\n", nt_errstr(cli_get_nt_error(cli
))));
597 /****************************************************************************
598 Use in-memory credentials cache
599 ****************************************************************************/
601 static void use_in_memory_ccache(void) {
602 setenv(KRB5_ENV_CCNAME
, "MEMORY:cliconnect", 1);
605 /****************************************************************************
606 Do a spnego/kerberos encrypted session setup.
607 ****************************************************************************/
609 static ADS_STATUS
cli_session_setup_kerberos(struct cli_state
*cli
, const char *principal
, const char *workgroup
)
611 DATA_BLOB negTokenTarg
;
612 DATA_BLOB session_key_krb5
;
616 cli_temp_set_signing(cli
);
618 DEBUG(2,("Doing kerberos session setup\n"));
620 /* generate the encapsulated kerberos5 ticket */
621 rc
= spnego_gen_negTokenTarg(principal
, 0, &negTokenTarg
, &session_key_krb5
, 0, NULL
);
624 DEBUG(1, ("cli_session_setup_kerberos: spnego_gen_negTokenTarg failed: %s\n",
626 return ADS_ERROR_KRB5(rc
);
630 file_save("negTokenTarg.dat", negTokenTarg
.data
, negTokenTarg
.length
);
633 if (!cli_session_setup_blob(cli
, negTokenTarg
)) {
634 nt_status
= cli_nt_error(cli
);
638 if (cli_is_error(cli
)) {
639 nt_status
= cli_nt_error(cli
);
640 if (NT_STATUS_IS_OK(nt_status
)) {
641 nt_status
= NT_STATUS_UNSUCCESSFUL
;
646 cli_set_session_key(cli
, session_key_krb5
);
648 if (cli_simple_set_signing(
649 cli
, session_key_krb5
, data_blob_null
)) {
651 /* 'resign' the last message, so we get the right sequence numbers
652 for checking the first reply from the server */
653 cli_calculate_sign_mac(cli
, cli
->outbuf
);
655 if (!cli_check_sign_mac(cli
, cli
->inbuf
)) {
656 nt_status
= NT_STATUS_ACCESS_DENIED
;
661 data_blob_free(&negTokenTarg
);
662 data_blob_free(&session_key_krb5
);
664 return ADS_ERROR_NT(NT_STATUS_OK
);
667 data_blob_free(&negTokenTarg
);
668 data_blob_free(&session_key_krb5
);
670 return ADS_ERROR_NT(nt_status
);
672 #endif /* HAVE_KRB5 */
675 /****************************************************************************
676 Do a spnego/NTLMSSP encrypted session setup.
677 ****************************************************************************/
679 static NTSTATUS
cli_session_setup_ntlmssp(struct cli_state
*cli
, const char *user
,
680 const char *pass
, const char *domain
)
682 struct ntlmssp_state
*ntlmssp_state
;
686 DATA_BLOB blob
= data_blob_null
;
687 DATA_BLOB blob_in
= data_blob_null
;
688 DATA_BLOB blob_out
= data_blob_null
;
690 cli_temp_set_signing(cli
);
692 if (!NT_STATUS_IS_OK(nt_status
= ntlmssp_client_start(&ntlmssp_state
))) {
695 ntlmssp_want_feature(ntlmssp_state
, NTLMSSP_FEATURE_SESSION_KEY
);
697 if (!NT_STATUS_IS_OK(nt_status
= ntlmssp_set_username(ntlmssp_state
, user
))) {
700 if (!NT_STATUS_IS_OK(nt_status
= ntlmssp_set_domain(ntlmssp_state
, domain
))) {
703 if (!NT_STATUS_IS_OK(nt_status
= ntlmssp_set_password(ntlmssp_state
, pass
))) {
708 nt_status
= ntlmssp_update(ntlmssp_state
,
710 data_blob_free(&blob_in
);
711 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
) || NT_STATUS_IS_OK(nt_status
)) {
713 /* and wrap it in a SPNEGO wrapper */
714 msg1
= gen_negTokenInit(OID_NTLMSSP
, blob_out
);
716 /* wrap it in SPNEGO */
717 msg1
= spnego_gen_auth(blob_out
);
720 /* now send that blob on its way */
721 if (!cli_session_setup_blob_send(cli
, msg1
)) {
722 DEBUG(3, ("Failed to send NTLMSSP/SPNEGO blob to server!\n"));
723 nt_status
= NT_STATUS_UNSUCCESSFUL
;
725 blob
= cli_session_setup_blob_receive(cli
);
727 nt_status
= cli_nt_error(cli
);
728 if (cli_is_error(cli
) && NT_STATUS_IS_OK(nt_status
)) {
729 if (cli
->smb_rw_error
== SMB_READ_BAD_SIG
) {
730 nt_status
= NT_STATUS_ACCESS_DENIED
;
732 nt_status
= NT_STATUS_UNSUCCESSFUL
;
736 data_blob_free(&msg1
);
740 if (NT_STATUS_IS_OK(nt_status
)) {
741 nt_status
= NT_STATUS_UNSUCCESSFUL
;
743 } else if ((turn
== 1) &&
744 NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
745 DATA_BLOB tmp_blob
= data_blob_null
;
746 /* the server might give us back two challenges */
747 if (!spnego_parse_challenge(blob
, &blob_in
,
749 DEBUG(3,("Failed to parse challenges\n"));
750 nt_status
= NT_STATUS_INVALID_PARAMETER
;
752 data_blob_free(&tmp_blob
);
754 if (!spnego_parse_auth_response(blob
, nt_status
, OID_NTLMSSP
,
756 DEBUG(3,("Failed to parse auth response\n"));
757 if (NT_STATUS_IS_OK(nt_status
)
758 || NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
))
759 nt_status
= NT_STATUS_INVALID_PARAMETER
;
762 data_blob_free(&blob
);
763 data_blob_free(&blob_out
);
765 } while (NT_STATUS_EQUAL(nt_status
, NT_STATUS_MORE_PROCESSING_REQUIRED
));
767 data_blob_free(&blob_in
);
769 if (NT_STATUS_IS_OK(nt_status
)) {
771 DATA_BLOB key
= data_blob(ntlmssp_state
->session_key
.data
,
772 ntlmssp_state
->session_key
.length
);
773 DATA_BLOB null_blob
= data_blob_null
;
776 fstrcpy(cli
->server_domain
, ntlmssp_state
->server_domain
);
777 cli_set_session_key(cli
, ntlmssp_state
->session_key
);
779 res
= cli_simple_set_signing(cli
, key
, null_blob
);
781 data_blob_free(&key
);
785 /* 'resign' the last message, so we get the right sequence numbers
786 for checking the first reply from the server */
787 cli_calculate_sign_mac(cli
, cli
->outbuf
);
789 if (!cli_check_sign_mac(cli
, cli
->inbuf
)) {
790 nt_status
= NT_STATUS_ACCESS_DENIED
;
795 /* we have a reference conter on ntlmssp_state, if we are signing
796 then the state will be kept by the signing engine */
798 ntlmssp_end(&ntlmssp_state
);
800 if (!NT_STATUS_IS_OK(nt_status
)) {
806 /****************************************************************************
807 Do a spnego encrypted session setup.
809 user_domain: The shortname of the domain the user/machine is a member of.
810 dest_realm: The realm we're connecting to, if NULL we use our default realm.
811 ****************************************************************************/
813 ADS_STATUS
cli_session_setup_spnego(struct cli_state
*cli
, const char *user
,
814 const char *pass
, const char *user_domain
,
815 const char * dest_realm
)
817 char *principal
= NULL
;
818 char *OIDs
[ASN1_MAX_OIDS
];
821 const char *p
= NULL
;
822 char *account
= NULL
;
824 DEBUG(3,("Doing spnego session setup (blob length=%lu)\n", (unsigned long)cli
->secblob
.length
));
826 /* the server might not even do spnego */
827 if (cli
->secblob
.length
<= 16) {
828 DEBUG(3,("server didn't supply a full spnego negprot\n"));
833 file_save("negprot.dat", cli
->secblob
.data
, cli
->secblob
.length
);
836 /* there is 16 bytes of GUID before the real spnego packet starts */
837 blob
= data_blob(cli
->secblob
.data
+16, cli
->secblob
.length
-16);
839 /* The server sent us the first part of the SPNEGO exchange in the
840 * negprot reply. It is WRONG to depend on the principal sent in the
841 * negprot reply, but right now we do it. If we don't receive one,
842 * we try to best guess, then fall back to NTLM. */
843 if (!spnego_parse_negTokenInit(blob
, OIDs
, &principal
)) {
844 data_blob_free(&blob
);
845 return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER
);
847 data_blob_free(&blob
);
849 /* make sure the server understands kerberos */
850 for (i
=0;OIDs
[i
];i
++) {
851 DEBUG(3,("got OID=%s\n", OIDs
[i
]));
852 if (strcmp(OIDs
[i
], OID_KERBEROS5_OLD
) == 0 ||
853 strcmp(OIDs
[i
], OID_KERBEROS5
) == 0) {
854 cli
->got_kerberos_mechanism
= True
;
859 DEBUG(3,("got principal=%s\n", principal
? principal
: "<null>"));
861 fstrcpy(cli
->user_name
, user
);
864 /* If password is set we reauthenticate to kerberos server
865 * and do not store results */
867 if (cli
->got_kerberos_mechanism
&& cli
->use_kerberos
) {
873 use_in_memory_ccache();
874 ret
= kerberos_kinit_password(user
, pass
, 0 /* no time correction for now */, NULL
);
877 SAFE_FREE(principal
);
878 DEBUG(0, ("Kinit failed: %s\n", error_message(ret
)));
879 if (cli
->fallback_after_kerberos
)
881 return ADS_ERROR_KRB5(ret
);
885 /* If we get a bad principal, try to guess it if
886 we have a valid host NetBIOS name.
888 if (strequal(principal
, ADS_IGNORE_PRINCIPAL
)) {
889 SAFE_FREE(principal
);
892 if (principal
== NULL
&&
893 !is_ipaddress(cli
->desthost
) &&
894 !strequal(star_smbserver_name
,
897 char *machine
= NULL
;
899 DEBUG(3,("cli_session_setup_spnego: got a "
900 "bad server principal, trying to guess ...\n"));
902 host
= strchr_m(cli
->desthost
, '.');
904 machine
= SMB_STRNDUP(cli
->desthost
,
905 host
- cli
->desthost
);
907 machine
= SMB_STRDUP(cli
->desthost
);
909 if (machine
== NULL
) {
910 return ADS_ERROR_NT(NT_STATUS_NO_MEMORY
);
914 realm
= SMB_STRDUP(dest_realm
);
917 realm
= kerberos_get_default_realm_from_ccache();
919 if (realm
&& *realm
) {
920 if (asprintf(&principal
, "%s$@%s",
921 machine
, realm
) < 0) {
924 return ADS_ERROR_NT(NT_STATUS_NO_MEMORY
);
926 DEBUG(3,("cli_session_setup_spnego: guessed "
927 "server principal=%s\n",
928 principal
? principal
: "<null>"));
935 rc
= cli_session_setup_kerberos(cli
, principal
,
937 if (ADS_ERR_OK(rc
) || !cli
->fallback_after_kerberos
) {
938 SAFE_FREE(principal
);
945 SAFE_FREE(principal
);
949 account
= talloc_strdup(talloc_tos(), user
);
951 return ADS_ERROR_NT(NT_STATUS_NO_MEMORY
);
954 /* when falling back to ntlmssp while authenticating with a machine
955 * account strip off the realm - gd */
957 if ((p
= strchr_m(user
, '@')) != NULL
) {
958 account
[PTR_DIFF(p
,user
)] = '\0';
961 return ADS_ERROR_NT(cli_session_setup_ntlmssp(cli
, account
, pass
, user_domain
));
964 /****************************************************************************
965 Send a session setup. The username and workgroup is in UNIX character
966 format and must be converted to DOS codepage format before sending. If the
967 password is in plaintext, the same should be done.
968 ****************************************************************************/
970 NTSTATUS
cli_session_setup(struct cli_state
*cli
,
972 const char *pass
, int passlen
,
973 const char *ntpass
, int ntpasslen
,
974 const char *workgroup
)
980 fstrcpy(user2
, user
);
989 /* allow for workgroups as part of the username */
990 if ((p
=strchr_m(user2
,'\\')) || (p
=strchr_m(user2
,'/')) ||
991 (p
=strchr_m(user2
,*lp_winbind_separator()))) {
997 if (cli
->protocol
< PROTOCOL_LANMAN1
) {
1001 /* now work out what sort of session setup we are going to
1002 do. I have split this into separate functions to make the
1003 flow a bit easier to understand (tridge) */
1005 /* if its an older server then we have to use the older request format */
1007 if (cli
->protocol
< PROTOCOL_NT1
) {
1008 if (!lp_client_lanman_auth() && passlen
!= 24 && (*pass
)) {
1009 DEBUG(1, ("Server requested LM password but 'client lanman auth'"
1011 return NT_STATUS_ACCESS_DENIED
;
1014 if ((cli
->sec_mode
& NEGOTIATE_SECURITY_CHALLENGE_RESPONSE
) == 0 &&
1015 !lp_client_plaintext_auth() && (*pass
)) {
1016 DEBUG(1, ("Server requested plaintext password but "
1017 "'client plaintext auth' is disabled\n"));
1018 return NT_STATUS_ACCESS_DENIED
;
1021 return cli_session_setup_lanman2(cli
, user
, pass
, passlen
,
1025 /* if no user is supplied then we have to do an anonymous connection.
1026 passwords are ignored */
1028 if (!user
|| !*user
)
1029 return cli_session_setup_guest(cli
);
1031 /* if the server is share level then send a plaintext null
1032 password at this point. The password is sent in the tree
1035 if ((cli
->sec_mode
& NEGOTIATE_SECURITY_USER_LEVEL
) == 0)
1036 return cli_session_setup_plaintext(cli
, user
, "", workgroup
);
1038 /* if the server doesn't support encryption then we have to use
1039 plaintext. The second password is ignored */
1041 if ((cli
->sec_mode
& NEGOTIATE_SECURITY_CHALLENGE_RESPONSE
) == 0) {
1042 if (!lp_client_plaintext_auth() && (*pass
)) {
1043 DEBUG(1, ("Server requested plaintext password but "
1044 "'client plaintext auth' is disabled\n"));
1045 return NT_STATUS_ACCESS_DENIED
;
1047 return cli_session_setup_plaintext(cli
, user
, pass
, workgroup
);
1050 /* if the server supports extended security then use SPNEGO */
1052 if (cli
->capabilities
& CAP_EXTENDED_SECURITY
) {
1053 ADS_STATUS status
= cli_session_setup_spnego(cli
, user
, pass
,
1055 if (!ADS_ERR_OK(status
)) {
1056 DEBUG(3, ("SPNEGO login failed: %s\n", ads_errstr(status
)));
1057 return ads_ntstatus(status
);
1062 /* otherwise do a NT1 style session setup */
1063 status
= cli_session_setup_nt1(cli
, user
, pass
, passlen
,
1064 ntpass
, ntpasslen
, workgroup
);
1065 if (!NT_STATUS_IS_OK(status
)) {
1066 DEBUG(3,("cli_session_setup: NT1 session setup "
1067 "failed: %s\n", nt_errstr(status
)));
1072 if (strstr(cli
->server_type
, "Samba")) {
1073 cli
->is_samba
= True
;
1076 return NT_STATUS_OK
;
1079 /****************************************************************************
1081 *****************************************************************************/
1083 bool cli_ulogoff(struct cli_state
*cli
)
1085 memset(cli
->outbuf
,'\0',smb_size
);
1086 cli_set_message(cli
->outbuf
,2,0,True
);
1087 SCVAL(cli
->outbuf
,smb_com
,SMBulogoffX
);
1088 cli_setup_packet(cli
);
1089 SSVAL(cli
->outbuf
,smb_vwv0
,0xFF);
1090 SSVAL(cli
->outbuf
,smb_vwv2
,0); /* no additional info */
1093 if (!cli_receive_smb(cli
))
1096 if (cli_is_error(cli
)) {
1104 /****************************************************************************
1106 ****************************************************************************/
1108 bool cli_send_tconX(struct cli_state
*cli
,
1109 const char *share
, const char *dev
, const char *pass
, int passlen
)
1111 fstring fullshare
, pword
;
1113 memset(cli
->outbuf
,'\0',smb_size
);
1114 memset(cli
->inbuf
,'\0',smb_size
);
1116 fstrcpy(cli
->share
, share
);
1118 /* in user level security don't send a password now */
1119 if (cli
->sec_mode
& NEGOTIATE_SECURITY_USER_LEVEL
) {
1123 DEBUG(1, ("Server not using user level security and no password supplied.\n"));
1127 if ((cli
->sec_mode
& NEGOTIATE_SECURITY_CHALLENGE_RESPONSE
) &&
1128 *pass
&& passlen
!= 24) {
1129 if (!lp_client_lanman_auth()) {
1130 DEBUG(1, ("Server requested LANMAN password "
1131 "(share-level security) but "
1132 "'client lanman auth' is disabled\n"));
1137 * Non-encrypted passwords - convert to DOS codepage before encryption.
1140 SMBencrypt(pass
,cli
->secblob
.data
,(uchar
*)pword
);
1142 if((cli
->sec_mode
& (NEGOTIATE_SECURITY_USER_LEVEL
|NEGOTIATE_SECURITY_CHALLENGE_RESPONSE
)) == 0) {
1143 if (!lp_client_plaintext_auth() && (*pass
)) {
1144 DEBUG(1, ("Server requested plaintext "
1145 "password but 'client plaintext "
1146 "auth' is disabled\n"));
1151 * Non-encrypted passwords - convert to DOS codepage before using.
1153 passlen
= clistr_push(cli
, pword
, pass
, sizeof(pword
), STR_TERMINATE
);
1157 memcpy(pword
, pass
, passlen
);
1162 slprintf(fullshare
, sizeof(fullshare
)-1,
1163 "\\\\%s\\%s", cli
->desthost
, share
);
1165 cli_set_message(cli
->outbuf
,4, 0, True
);
1166 SCVAL(cli
->outbuf
,smb_com
,SMBtconX
);
1167 cli_setup_packet(cli
);
1169 SSVAL(cli
->outbuf
,smb_vwv0
,0xFF);
1170 SSVAL(cli
->outbuf
,smb_vwv2
,TCONX_FLAG_EXTENDED_RESPONSE
);
1171 SSVAL(cli
->outbuf
,smb_vwv3
,passlen
);
1173 p
= smb_buf(cli
->outbuf
);
1175 memcpy(p
,pword
,passlen
);
1178 p
+= clistr_push(cli
, p
, fullshare
, -1, STR_TERMINATE
|STR_UPPER
);
1179 p
+= clistr_push(cli
, p
, dev
, -1, STR_TERMINATE
|STR_UPPER
| STR_ASCII
);
1181 cli_setup_bcc(cli
, p
);
1184 if (!cli_receive_smb(cli
))
1187 if (cli_is_error(cli
))
1190 clistr_pull(cli
, cli
->dev
, smb_buf(cli
->inbuf
), sizeof(fstring
), -1, STR_TERMINATE
|STR_ASCII
);
1192 if (cli
->protocol
>= PROTOCOL_NT1
&&
1193 smb_buflen(cli
->inbuf
) == 3) {
1194 /* almost certainly win95 - enable bug fixes */
1198 /* Make sure that we have the optional support 16-bit field. WCT > 2 */
1199 /* Avoids issues when connecting to Win9x boxes sharing files */
1201 cli
->dfsroot
= False
;
1202 if ( (CVAL(cli
->inbuf
, smb_wct
))>2 && cli
->protocol
>= PROTOCOL_LANMAN2
)
1203 cli
->dfsroot
= (SVAL( cli
->inbuf
, smb_vwv2
) & SMB_SHARE_IN_DFS
) ? True
: False
;
1205 cli
->cnum
= SVAL(cli
->inbuf
,smb_tid
);
1209 /****************************************************************************
1210 Send a tree disconnect.
1211 ****************************************************************************/
1213 bool cli_tdis(struct cli_state
*cli
)
1215 memset(cli
->outbuf
,'\0',smb_size
);
1216 cli_set_message(cli
->outbuf
,0,0,True
);
1217 SCVAL(cli
->outbuf
,smb_com
,SMBtdis
);
1218 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1219 cli_setup_packet(cli
);
1222 if (!cli_receive_smb(cli
))
1225 if (cli_is_error(cli
)) {
1233 /****************************************************************************
1234 Send a negprot command.
1235 ****************************************************************************/
1237 void cli_negprot_send(struct cli_state
*cli
)
1242 if (cli
->protocol
< PROTOCOL_NT1
)
1243 cli
->use_spnego
= False
;
1245 memset(cli
->outbuf
,'\0',smb_size
);
1247 /* setup the protocol strings */
1248 cli_set_message(cli
->outbuf
,0,0,True
);
1250 p
= smb_buf(cli
->outbuf
);
1252 prots
[numprots
].name
&& prots
[numprots
].prot
<=cli
->protocol
;
1255 p
+= clistr_push(cli
, p
, prots
[numprots
].name
, -1, STR_TERMINATE
);
1258 SCVAL(cli
->outbuf
,smb_com
,SMBnegprot
);
1259 cli_setup_bcc(cli
, p
);
1260 cli_setup_packet(cli
);
1262 SCVAL(smb_buf(cli
->outbuf
),0,2);
1267 /****************************************************************************
1268 Send a negprot command.
1269 ****************************************************************************/
1271 bool cli_negprot(struct cli_state
*cli
)
1277 if (cli
->protocol
< PROTOCOL_NT1
)
1278 cli
->use_spnego
= False
;
1280 memset(cli
->outbuf
,'\0',smb_size
);
1282 /* setup the protocol strings */
1283 for (plength
=0,numprots
=0;
1284 prots
[numprots
].name
&& prots
[numprots
].prot
<=cli
->protocol
;
1286 plength
+= strlen(prots
[numprots
].name
)+2;
1288 cli_set_message(cli
->outbuf
,0,plength
,True
);
1290 p
= smb_buf(cli
->outbuf
);
1292 prots
[numprots
].name
&& prots
[numprots
].prot
<=cli
->protocol
;
1295 p
+= clistr_push(cli
, p
, prots
[numprots
].name
, -1, STR_TERMINATE
);
1298 SCVAL(cli
->outbuf
,smb_com
,SMBnegprot
);
1299 cli_setup_packet(cli
);
1301 SCVAL(smb_buf(cli
->outbuf
),0,2);
1304 if (!cli_receive_smb(cli
))
1307 show_msg(cli
->inbuf
);
1309 if (cli_is_error(cli
) ||
1310 ((int)SVAL(cli
->inbuf
,smb_vwv0
) >= numprots
)) {
1314 cli
->protocol
= prots
[SVAL(cli
->inbuf
,smb_vwv0
)].prot
;
1316 if ((cli
->protocol
< PROTOCOL_NT1
) && cli
->sign_info
.mandatory_signing
) {
1317 DEBUG(0,("cli_negprot: SMB signing is mandatory and the selected protocol level doesn't support it.\n"));
1321 if (cli
->protocol
>= PROTOCOL_NT1
) {
1324 cli
->sec_mode
= CVAL(cli
->inbuf
,smb_vwv1
);
1325 cli
->max_mux
= SVAL(cli
->inbuf
, smb_vwv1
+1);
1326 cli
->max_xmit
= IVAL(cli
->inbuf
,smb_vwv3
+1);
1327 cli
->sesskey
= IVAL(cli
->inbuf
,smb_vwv7
+1);
1328 cli
->serverzone
= SVALS(cli
->inbuf
,smb_vwv15
+1);
1329 cli
->serverzone
*= 60;
1330 /* this time arrives in real GMT */
1331 ts
= interpret_long_date(cli
->inbuf
+smb_vwv11
+1);
1332 cli
->servertime
= ts
.tv_sec
;
1333 cli
->secblob
= data_blob(smb_buf(cli
->inbuf
),smb_buflen(cli
->inbuf
));
1334 cli
->capabilities
= IVAL(cli
->inbuf
,smb_vwv9
+1);
1335 if (cli
->capabilities
& CAP_RAW_MODE
) {
1336 cli
->readbraw_supported
= True
;
1337 cli
->writebraw_supported
= True
;
1339 /* work out if they sent us a workgroup */
1340 if (!(cli
->capabilities
& CAP_EXTENDED_SECURITY
) &&
1341 smb_buflen(cli
->inbuf
) > 8) {
1342 clistr_pull(cli
, cli
->server_domain
,
1343 smb_buf(cli
->inbuf
)+8, sizeof(cli
->server_domain
),
1344 smb_buflen(cli
->inbuf
)-8, STR_UNICODE
|STR_NOALIGN
);
1348 * As signing is slow we only turn it on if either the client or
1349 * the server require it. JRA.
1352 if (cli
->sec_mode
& NEGOTIATE_SECURITY_SIGNATURES_REQUIRED
) {
1353 /* Fail if server says signing is mandatory and we don't want to support it. */
1354 if (!cli
->sign_info
.allow_smb_signing
) {
1355 DEBUG(0,("cli_negprot: SMB signing is mandatory and we have disabled it.\n"));
1358 cli
->sign_info
.negotiated_smb_signing
= True
;
1359 cli
->sign_info
.mandatory_signing
= True
;
1360 } else if (cli
->sign_info
.mandatory_signing
&& cli
->sign_info
.allow_smb_signing
) {
1361 /* Fail if client says signing is mandatory and the server doesn't support it. */
1362 if (!(cli
->sec_mode
& NEGOTIATE_SECURITY_SIGNATURES_ENABLED
)) {
1363 DEBUG(1,("cli_negprot: SMB signing is mandatory and the server doesn't support it.\n"));
1366 cli
->sign_info
.negotiated_smb_signing
= True
;
1367 cli
->sign_info
.mandatory_signing
= True
;
1368 } else if (cli
->sec_mode
& NEGOTIATE_SECURITY_SIGNATURES_ENABLED
) {
1369 cli
->sign_info
.negotiated_smb_signing
= True
;
1372 if (cli
->capabilities
& (CAP_LARGE_READX
|CAP_LARGE_WRITEX
)) {
1373 SAFE_FREE(cli
->outbuf
);
1374 SAFE_FREE(cli
->inbuf
);
1375 cli
->outbuf
= (char *)SMB_MALLOC(CLI_SAMBA_MAX_LARGE_READX_SIZE
+LARGE_WRITEX_HDR_SIZE
+SAFETY_MARGIN
);
1376 cli
->inbuf
= (char *)SMB_MALLOC(CLI_SAMBA_MAX_LARGE_READX_SIZE
+LARGE_WRITEX_HDR_SIZE
+SAFETY_MARGIN
);
1377 cli
->bufsize
= CLI_SAMBA_MAX_LARGE_READX_SIZE
+ LARGE_WRITEX_HDR_SIZE
;
1380 } else if (cli
->protocol
>= PROTOCOL_LANMAN1
) {
1381 cli
->use_spnego
= False
;
1382 cli
->sec_mode
= SVAL(cli
->inbuf
,smb_vwv1
);
1383 cli
->max_xmit
= SVAL(cli
->inbuf
,smb_vwv2
);
1384 cli
->max_mux
= SVAL(cli
->inbuf
, smb_vwv3
);
1385 cli
->sesskey
= IVAL(cli
->inbuf
,smb_vwv6
);
1386 cli
->serverzone
= SVALS(cli
->inbuf
,smb_vwv10
);
1387 cli
->serverzone
*= 60;
1388 /* this time is converted to GMT by make_unix_date */
1389 cli
->servertime
= cli_make_unix_date(cli
,cli
->inbuf
+smb_vwv8
);
1390 cli
->readbraw_supported
= ((SVAL(cli
->inbuf
,smb_vwv5
) & 0x1) != 0);
1391 cli
->writebraw_supported
= ((SVAL(cli
->inbuf
,smb_vwv5
) & 0x2) != 0);
1392 cli
->secblob
= data_blob(smb_buf(cli
->inbuf
),smb_buflen(cli
->inbuf
));
1394 /* the old core protocol */
1395 cli
->use_spnego
= False
;
1397 cli
->serverzone
= get_time_zone(time(NULL
));
1400 cli
->max_xmit
= MIN(cli
->max_xmit
, CLI_BUFFER_SIZE
);
1402 /* a way to force ascii SMB */
1403 if (getenv("CLI_FORCE_ASCII"))
1404 cli
->capabilities
&= ~CAP_UNICODE
;
1409 /****************************************************************************
1410 Send a session request. See rfc1002.txt 4.3 and 4.3.2.
1411 ****************************************************************************/
1413 bool cli_session_request(struct cli_state
*cli
,
1414 struct nmb_name
*calling
, struct nmb_name
*called
)
1419 memcpy(&(cli
->calling
), calling
, sizeof(*calling
));
1420 memcpy(&(cli
->called
), called
, sizeof(*called
));
1422 /* put in the destination name */
1423 p
= cli
->outbuf
+len
;
1424 name_mangle(cli
->called
.name
, p
, cli
->called
.name_type
);
1428 p
= cli
->outbuf
+len
;
1429 name_mangle(cli
->calling
.name
, p
, cli
->calling
.name_type
);
1432 /* 445 doesn't have session request */
1433 if (cli
->port
== 445)
1436 /* send a session request (RFC 1002) */
1437 /* setup the packet length
1438 * Remove four bytes from the length count, since the length
1439 * field in the NBT Session Service header counts the number
1440 * of bytes which follow. The cli_send_smb() function knows
1441 * about this and accounts for those four bytes.
1445 _smb_setlen(cli
->outbuf
,len
);
1446 SCVAL(cli
->outbuf
,0,0x81);
1449 DEBUG(5,("Sent session request\n"));
1451 if (!cli_receive_smb(cli
))
1454 if (CVAL(cli
->inbuf
,0) == 0x84) {
1455 /* C. Hoch 9/14/95 Start */
1456 /* For information, here is the response structure.
1457 * We do the byte-twiddling to for portability.
1458 struct RetargetResponse{
1460 unsigned char flags;
1466 uint16_t port
= (CVAL(cli
->inbuf
,8)<<8)+CVAL(cli
->inbuf
,9);
1467 struct in_addr dest_ip
;
1469 /* SESSION RETARGET */
1470 putip((char *)&dest_ip
,cli
->inbuf
+4);
1471 in_addr_to_sockaddr_storage(&cli
->dest_ss
, dest_ip
);
1473 cli
->fd
= open_socket_out(SOCK_STREAM
,
1476 LONG_CONNECT_TIMEOUT
);
1480 DEBUG(3,("Retargeted\n"));
1482 set_socket_options(cli
->fd
, lp_socket_options());
1489 DEBUG(0,("Retarget recursion - failing\n"));
1493 ret
= cli_session_request(cli
, calling
, called
);
1497 } /* C. Hoch 9/14/95 End */
1499 if (CVAL(cli
->inbuf
,0) != 0x82) {
1500 /* This is the wrong place to put the error... JRA. */
1501 cli
->rap_error
= CVAL(cli
->inbuf
,4);
1507 /****************************************************************************
1508 Open the client sockets.
1509 ****************************************************************************/
1511 NTSTATUS
cli_connect(struct cli_state
*cli
,
1513 struct sockaddr_storage
*dest_ss
)
1516 int name_type
= 0x20;
1517 TALLOC_CTX
*frame
= talloc_stackframe();
1518 unsigned int num_addrs
= 0;
1520 struct sockaddr_storage
*ss_arr
= NULL
;
1523 /* reasonable default hostname */
1525 host
= star_smbserver_name
;
1528 fstrcpy(cli
->desthost
, host
);
1530 /* allow hostnames of the form NAME#xx and do a netbios lookup */
1531 if ((p
= strchr(cli
->desthost
, '#'))) {
1532 name_type
= strtol(p
+1, NULL
, 16);
1536 if (!dest_ss
|| is_zero_addr(dest_ss
)) {
1537 NTSTATUS status
=resolve_name_list(frame
,
1542 if (!NT_STATUS_IS_OK(status
)) {
1544 return NT_STATUS_BAD_NETWORK_NAME
;
1548 ss_arr
= TALLOC_P(frame
, struct sockaddr_storage
);
1551 return NT_STATUS_NO_MEMORY
;
1556 for (i
= 0; i
< num_addrs
; i
++) {
1557 cli
->dest_ss
= ss_arr
[i
];
1558 if (getenv("LIBSMB_PROG")) {
1559 cli
->fd
= sock_exec(getenv("LIBSMB_PROG"));
1561 /* try 445 first, then 139 */
1562 uint16_t port
= cli
->port
?cli
->port
:445;
1563 cli
->fd
= open_socket_out(SOCK_STREAM
, &cli
->dest_ss
,
1564 port
, cli
->timeout
);
1565 if (cli
->fd
== -1 && cli
->port
== 0) {
1567 cli
->fd
= open_socket_out(SOCK_STREAM
, &cli
->dest_ss
,
1568 port
, cli
->timeout
);
1570 if (cli
->fd
!= -1) {
1574 if (cli
->fd
== -1) {
1575 char addr
[INET6_ADDRSTRLEN
];
1576 print_sockaddr(addr
, sizeof(addr
), &ss_arr
[i
]);
1577 DEBUG(2,("Error connecting to %s (%s)\n",
1578 dest_ss
?addr
:host
,strerror(errno
)));
1580 /* Exit from loop on first connection. */
1585 if (cli
->fd
== -1) {
1587 return map_nt_error_from_unix(errno
);
1591 *dest_ss
= cli
->dest_ss
;
1594 set_socket_options(cli
->fd
, lp_socket_options());
1597 return NT_STATUS_OK
;
1601 establishes a connection to after the negprot.
1602 @param output_cli A fully initialised cli structure, non-null only on success
1603 @param dest_host The netbios name of the remote host
1604 @param dest_ss (optional) The the destination IP, NULL for name based lookup
1605 @param port (optional) The destination port (0 for default)
1606 @param retry bool. Did this connection fail with a retryable error ?
1609 NTSTATUS
cli_start_connection(struct cli_state
**output_cli
,
1610 const char *my_name
,
1611 const char *dest_host
,
1612 struct sockaddr_storage
*dest_ss
, int port
,
1613 int signing_state
, int flags
,
1617 struct nmb_name calling
;
1618 struct nmb_name called
;
1619 struct cli_state
*cli
;
1620 struct sockaddr_storage ss
;
1626 my_name
= global_myname();
1628 if (!(cli
= cli_initialise())) {
1629 return NT_STATUS_NO_MEMORY
;
1632 make_nmb_name(&calling
, my_name
, 0x0);
1633 make_nmb_name(&called
, dest_host
, 0x20);
1635 if (cli_set_port(cli
, port
) != port
) {
1637 return NT_STATUS_UNSUCCESSFUL
;
1640 cli_set_timeout(cli
, 10000); /* 10 seconds. */
1650 DEBUG(3,("Connecting to host=%s\n", dest_host
));
1652 nt_status
= cli_connect(cli
, dest_host
, &ss
);
1653 if (!NT_STATUS_IS_OK(nt_status
)) {
1654 char addr
[INET6_ADDRSTRLEN
];
1655 print_sockaddr(addr
, sizeof(addr
), &ss
);
1656 DEBUG(1,("cli_start_connection: failed to connect to %s (%s). Error %s\n",
1657 nmb_namestr(&called
), addr
, nt_errstr(nt_status
) ));
1665 if (!cli_session_request(cli
, &calling
, &called
)) {
1667 DEBUG(1,("session request to %s failed (%s)\n",
1668 called
.name
, cli_errstr(cli
)));
1669 if ((p
=strchr(called
.name
, '.')) && !is_ipaddress(called
.name
)) {
1673 if (strcmp(called
.name
, star_smbserver_name
)) {
1674 make_nmb_name(&called
, star_smbserver_name
, 0x20);
1677 return NT_STATUS_BAD_NETWORK_NAME
;
1680 cli_setup_signing_state(cli
, signing_state
);
1682 if (flags
& CLI_FULL_CONNECTION_DONT_SPNEGO
)
1683 cli
->use_spnego
= False
;
1684 else if (flags
& CLI_FULL_CONNECTION_USE_KERBEROS
)
1685 cli
->use_kerberos
= True
;
1687 if ((flags
& CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
) &&
1688 cli
->use_kerberos
) {
1689 cli
->fallback_after_kerberos
= true;
1692 if (!cli_negprot(cli
)) {
1693 DEBUG(1,("failed negprot\n"));
1694 nt_status
= cli_nt_error(cli
);
1695 if (NT_STATUS_IS_OK(nt_status
)) {
1696 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1703 return NT_STATUS_OK
;
1708 establishes a connection right up to doing tconX, password specified.
1709 @param output_cli A fully initialised cli structure, non-null only on success
1710 @param dest_host The netbios name of the remote host
1711 @param dest_ip (optional) The the destination IP, NULL for name based lookup
1712 @param port (optional) The destination port (0 for default)
1713 @param service (optional) The share to make the connection to. Should be 'unqualified' in any way.
1714 @param service_type The 'type' of serivice.
1715 @param user Username, unix string
1716 @param domain User's domain
1717 @param password User's password, unencrypted unix string.
1718 @param retry bool. Did this connection fail with a retryable error ?
1721 NTSTATUS
cli_full_connection(struct cli_state
**output_cli
,
1722 const char *my_name
,
1723 const char *dest_host
,
1724 struct sockaddr_storage
*dest_ss
, int port
,
1725 const char *service
, const char *service_type
,
1726 const char *user
, const char *domain
,
1727 const char *password
, int flags
,
1732 struct cli_state
*cli
= NULL
;
1733 int pw_len
= password
? strlen(password
)+1 : 0;
1737 if (password
== NULL
) {
1741 nt_status
= cli_start_connection(&cli
, my_name
, dest_host
,
1742 dest_ss
, port
, signing_state
,
1745 if (!NT_STATUS_IS_OK(nt_status
)) {
1749 nt_status
= cli_session_setup(cli
, user
, password
, pw_len
, password
,
1751 if (!NT_STATUS_IS_OK(nt_status
)) {
1753 if (!(flags
& CLI_FULL_CONNECTION_ANONYMOUS_FALLBACK
)) {
1754 DEBUG(1,("failed session setup with %s\n",
1755 nt_errstr(nt_status
)));
1760 nt_status
= cli_session_setup(cli
, "", "", 0, "", 0, domain
);
1761 if (!NT_STATUS_IS_OK(nt_status
)) {
1762 DEBUG(1,("anonymous failed session setup with %s\n",
1763 nt_errstr(nt_status
)));
1770 if (!cli_send_tconX(cli
, service
, service_type
, password
, pw_len
)) {
1771 nt_status
= cli_nt_error(cli
);
1772 DEBUG(1,("failed tcon_X with %s\n", nt_errstr(nt_status
)));
1774 if (NT_STATUS_IS_OK(nt_status
)) {
1775 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1781 cli_init_creds(cli
, user
, domain
, password
);
1784 return NT_STATUS_OK
;
1787 /****************************************************************************
1788 Attempt a NetBIOS session request, falling back to *SMBSERVER if needed.
1789 ****************************************************************************/
1791 bool attempt_netbios_session_request(struct cli_state
**ppcli
, const char *srchost
, const char *desthost
,
1792 struct sockaddr_storage
*pdest_ss
)
1794 struct nmb_name calling
, called
;
1796 make_nmb_name(&calling
, srchost
, 0x0);
1799 * If the called name is an IP address
1800 * then use *SMBSERVER immediately.
1803 if(is_ipaddress(desthost
)) {
1804 make_nmb_name(&called
, star_smbserver_name
, 0x20);
1806 make_nmb_name(&called
, desthost
, 0x20);
1809 if (!cli_session_request(*ppcli
, &calling
, &called
)) {
1811 struct nmb_name smbservername
;
1813 make_nmb_name(&smbservername
, star_smbserver_name
, 0x20);
1816 * If the name wasn't *SMBSERVER then
1817 * try with *SMBSERVER if the first name fails.
1820 if (nmb_name_equal(&called
, &smbservername
)) {
1823 * The name used was *SMBSERVER, don't bother with another name.
1826 DEBUG(0,("attempt_netbios_session_request: %s rejected the session for name *SMBSERVER \
1827 with error %s.\n", desthost
, cli_errstr(*ppcli
) ));
1832 cli_shutdown(*ppcli
);
1834 *ppcli
= cli_initialise();
1836 /* Out of memory... */
1840 status
= cli_connect(*ppcli
, desthost
, pdest_ss
);
1841 if (!NT_STATUS_IS_OK(status
) ||
1842 !cli_session_request(*ppcli
, &calling
, &smbservername
)) {
1843 DEBUG(0,("attempt_netbios_session_request: %s rejected the session for \
1844 name *SMBSERVER with error %s\n", desthost
, cli_errstr(*ppcli
) ));
1852 /****************************************************************************
1853 Send an old style tcon.
1854 ****************************************************************************/
1855 NTSTATUS
cli_raw_tcon(struct cli_state
*cli
,
1856 const char *service
, const char *pass
, const char *dev
,
1857 uint16
*max_xmit
, uint16
*tid
)
1861 if (!lp_client_plaintext_auth() && (*pass
)) {
1862 DEBUG(1, ("Server requested plaintext password but 'client "
1863 "plaintext auth' is disabled\n"));
1864 return NT_STATUS_ACCESS_DENIED
;
1867 memset(cli
->outbuf
,'\0',smb_size
);
1868 memset(cli
->inbuf
,'\0',smb_size
);
1870 cli_set_message(cli
->outbuf
, 0, 0, True
);
1871 SCVAL(cli
->outbuf
,smb_com
,SMBtcon
);
1872 cli_setup_packet(cli
);
1874 p
= smb_buf(cli
->outbuf
);
1875 *p
++ = 4; p
+= clistr_push(cli
, p
, service
, -1, STR_TERMINATE
| STR_NOALIGN
);
1876 *p
++ = 4; p
+= clistr_push(cli
, p
, pass
, -1, STR_TERMINATE
| STR_NOALIGN
);
1877 *p
++ = 4; p
+= clistr_push(cli
, p
, dev
, -1, STR_TERMINATE
| STR_NOALIGN
);
1879 cli_setup_bcc(cli
, p
);
1882 if (!cli_receive_smb(cli
)) {
1883 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
1886 if (cli_is_error(cli
)) {
1887 return cli_nt_error(cli
);
1890 *max_xmit
= SVAL(cli
->inbuf
, smb_vwv0
);
1891 *tid
= SVAL(cli
->inbuf
, smb_vwv1
);
1893 return NT_STATUS_OK
;
1896 /* Return a cli_state pointing at the IPC$ share for the given server */
1898 struct cli_state
*get_ipc_connect(char *server
,
1899 struct sockaddr_storage
*server_ss
,
1900 const struct user_auth_info
*user_info
)
1902 struct cli_state
*cli
;
1904 uint32_t flags
= CLI_FULL_CONNECTION_ANONYMOUS_FALLBACK
;
1906 if (user_info
->use_kerberos
) {
1907 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
1910 nt_status
= cli_full_connection(&cli
, NULL
, server
, server_ss
, 0, "IPC$", "IPC",
1911 user_info
->username
? user_info
->username
: "",
1913 user_info
->password
? user_info
->password
: "",
1917 if (NT_STATUS_IS_OK(nt_status
)) {
1919 } else if (is_ipaddress(server
)) {
1920 /* windows 9* needs a correct NMB name for connections */
1921 fstring remote_name
;
1923 if (name_status_find("*", 0, 0, server_ss
, remote_name
)) {
1924 cli
= get_ipc_connect(remote_name
, server_ss
, user_info
);
1933 * Given the IP address of a master browser on the network, return its
1934 * workgroup and connect to it.
1936 * This function is provided to allow additional processing beyond what
1937 * get_ipc_connect_master_ip_bcast() does, e.g. to retrieve the list of master
1938 * browsers and obtain each master browsers' list of domains (in case the
1939 * first master browser is recently on the network and has not yet
1940 * synchronized with other master browsers and therefore does not yet have the
1941 * entire network browse list)
1944 struct cli_state
*get_ipc_connect_master_ip(TALLOC_CTX
*ctx
,
1945 struct ip_service
*mb_ip
,
1946 const struct user_auth_info
*user_info
,
1947 char **pp_workgroup_out
)
1949 char addr
[INET6_ADDRSTRLEN
];
1951 struct cli_state
*cli
;
1952 struct sockaddr_storage server_ss
;
1954 *pp_workgroup_out
= NULL
;
1956 print_sockaddr(addr
, sizeof(addr
), &mb_ip
->ss
);
1957 DEBUG(99, ("Looking up name of master browser %s\n",
1961 * Do a name status query to find out the name of the master browser.
1962 * We use <01><02>__MSBROWSE__<02>#01 if *#00 fails because a domain
1963 * master browser will not respond to a wildcard query (or, at least,
1964 * an NT4 server acting as the domain master browser will not).
1966 * We might be able to use ONLY the query on MSBROWSE, but that's not
1967 * yet been tested with all Windows versions, so until it is, leave
1968 * the original wildcard query as the first choice and fall back to
1969 * MSBROWSE if the wildcard query fails.
1971 if (!name_status_find("*", 0, 0x1d, &mb_ip
->ss
, name
) &&
1972 !name_status_find(MSBROWSE
, 1, 0x1d, &mb_ip
->ss
, name
)) {
1974 DEBUG(99, ("Could not retrieve name status for %s\n",
1979 if (!find_master_ip(name
, &server_ss
)) {
1980 DEBUG(99, ("Could not find master ip for %s\n", name
));
1984 *pp_workgroup_out
= talloc_strdup(ctx
, name
);
1986 DEBUG(4, ("found master browser %s, %s\n", name
, addr
));
1988 print_sockaddr(addr
, sizeof(addr
), &server_ss
);
1989 cli
= get_ipc_connect(addr
, &server_ss
, user_info
);
1995 * Return the IP address and workgroup of a master browser on the network, and
1999 struct cli_state
*get_ipc_connect_master_ip_bcast(TALLOC_CTX
*ctx
,
2000 const struct user_auth_info
*user_info
,
2001 char **pp_workgroup_out
)
2003 struct ip_service
*ip_list
;
2004 struct cli_state
*cli
;
2007 *pp_workgroup_out
= NULL
;
2009 DEBUG(99, ("Do broadcast lookup for workgroups on local network\n"));
2011 /* Go looking for workgroups by broadcasting on the local network */
2013 if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE
, 1, &ip_list
,
2015 DEBUG(99, ("No master browsers responded\n"));
2019 for (i
= 0; i
< count
; i
++) {
2020 char addr
[INET6_ADDRSTRLEN
];
2021 print_sockaddr(addr
, sizeof(addr
), &ip_list
[i
].ss
);
2022 DEBUG(99, ("Found master browser %s\n", addr
));
2024 cli
= get_ipc_connect_master_ip(ctx
, &ip_list
[i
],
2025 user_info
, pp_workgroup_out
);