2 Unix SMB/CIFS implementation.
3 client connect/disconnect routines
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Andrew Barteltt 2001-2002
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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 {PROTOCOL_CORE
,"PC NETWORK PROGRAM 1.0"},
32 {PROTOCOL_COREPLUS
,"MICROSOFT NETWORKS 1.03"},
33 {PROTOCOL_LANMAN1
,"MICROSOFT NETWORKS 3.0"},
34 {PROTOCOL_LANMAN1
,"LANMAN1.0"},
35 {PROTOCOL_LANMAN2
,"LM1.2X002"},
36 {PROTOCOL_LANMAN2
,"DOS LANMAN2.1"},
37 {PROTOCOL_LANMAN2
,"Samba"},
38 {PROTOCOL_NT1
,"NT LANMAN 1.0"},
39 {PROTOCOL_NT1
,"NT LM 0.12"},
43 /****************************************************************************
44 Do an old lanman2 style session setup.
45 ****************************************************************************/
47 static BOOL
cli_session_setup_lanman2(struct cli_state
*cli
, char *user
,
48 char *pass
, int passlen
, const char *workgroup
)
53 if (passlen
> sizeof(pword
)-1)
56 /* if in share level security then don't send a password now */
57 if (!(cli
->sec_mode
& NEGOTIATE_SECURITY_USER_LEVEL
))
60 if (passlen
> 0 && (cli
->sec_mode
& NEGOTIATE_SECURITY_CHALLENGE_RESPONSE
) && passlen
!= 24) {
61 /* Encrypted mode needed, and non encrypted password supplied. */
63 SMBencrypt(pass
,cli
->secblob
.data
,(uchar
*)pword
);
64 } else if ((cli
->sec_mode
& NEGOTIATE_SECURITY_CHALLENGE_RESPONSE
) && passlen
== 24) {
65 /* Encrypted mode needed, and encrypted password supplied. */
66 memcpy(pword
, pass
, passlen
);
67 } else if (passlen
> 0) {
68 /* Plaintext mode needed, assume plaintext supplied. */
69 passlen
= clistr_push(cli
, pword
, pass
, -1, STR_TERMINATE
);
72 /* send a session setup command */
73 memset(cli
->outbuf
,'\0',smb_size
);
74 set_message(cli
->outbuf
,10, 0, True
);
75 SCVAL(cli
->outbuf
,smb_com
,SMBsesssetupX
);
76 cli_setup_packet(cli
);
78 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
79 SSVAL(cli
->outbuf
,smb_vwv2
,cli
->max_xmit
);
80 SSVAL(cli
->outbuf
,smb_vwv3
,2);
81 SSVAL(cli
->outbuf
,smb_vwv4
,1);
82 SIVAL(cli
->outbuf
,smb_vwv5
,cli
->sesskey
);
83 SSVAL(cli
->outbuf
,smb_vwv7
,passlen
);
85 p
= smb_buf(cli
->outbuf
);
86 memcpy(p
,pword
,passlen
);
88 p
+= clistr_push(cli
, p
, user
, -1, STR_TERMINATE
|STR_UPPER
);
89 p
+= clistr_push(cli
, p
, workgroup
, -1, STR_TERMINATE
|STR_UPPER
);
90 p
+= clistr_push(cli
, p
, "Unix", -1, STR_TERMINATE
);
91 p
+= clistr_push(cli
, p
, "Samba", -1, STR_TERMINATE
);
92 cli_setup_bcc(cli
, p
);
95 if (!cli_receive_smb(cli
))
100 if (cli_is_error(cli
))
103 /* use the returned vuid from now on */
104 cli
->vuid
= SVAL(cli
->inbuf
,smb_uid
);
105 fstrcpy(cli
->user_name
, user
);
110 /****************************************************************************
111 Work out suitable capabilities to offer the server.
112 ****************************************************************************/
114 static uint32
cli_session_setup_capabilities(struct cli_state
*cli
)
116 uint32 capabilities
= CAP_NT_SMBS
;
118 if (!cli
->force_dos_errors
)
119 capabilities
|= CAP_STATUS32
;
121 if (cli
->use_level_II_oplocks
)
122 capabilities
|= CAP_LEVEL_II_OPLOCKS
;
124 if (cli
->capabilities
& CAP_UNICODE
)
125 capabilities
|= CAP_UNICODE
;
130 /****************************************************************************
131 Do a NT1 guest session setup.
132 ****************************************************************************/
134 static BOOL
cli_session_setup_guest(struct cli_state
*cli
)
137 uint32 capabilities
= cli_session_setup_capabilities(cli
);
139 set_message(cli
->outbuf
,13,0,True
);
140 SCVAL(cli
->outbuf
,smb_com
,SMBsesssetupX
);
141 cli_setup_packet(cli
);
143 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
144 SSVAL(cli
->outbuf
,smb_vwv2
,CLI_BUFFER_SIZE
);
145 SSVAL(cli
->outbuf
,smb_vwv3
,2);
146 SSVAL(cli
->outbuf
,smb_vwv4
,cli
->pid
);
147 SIVAL(cli
->outbuf
,smb_vwv5
,cli
->sesskey
);
148 SSVAL(cli
->outbuf
,smb_vwv7
,0);
149 SSVAL(cli
->outbuf
,smb_vwv8
,0);
150 SIVAL(cli
->outbuf
,smb_vwv11
,capabilities
);
151 p
= smb_buf(cli
->outbuf
);
152 p
+= clistr_push(cli
, p
, "", -1, STR_TERMINATE
); /* username */
153 p
+= clistr_push(cli
, p
, "", -1, STR_TERMINATE
); /* workgroup */
154 p
+= clistr_push(cli
, p
, "Unix", -1, STR_TERMINATE
);
155 p
+= clistr_push(cli
, p
, "Samba", -1, STR_TERMINATE
);
156 cli_setup_bcc(cli
, p
);
159 if (!cli_receive_smb(cli
))
162 show_msg(cli
->inbuf
);
164 if (cli_is_error(cli
))
167 cli
->vuid
= SVAL(cli
->inbuf
,smb_uid
);
169 p
= smb_buf(cli
->inbuf
);
170 p
+= clistr_pull(cli
, cli
->server_os
, p
, sizeof(fstring
), -1, STR_TERMINATE
);
171 p
+= clistr_pull(cli
, cli
->server_type
, p
, sizeof(fstring
), -1, STR_TERMINATE
);
172 p
+= clistr_pull(cli
, cli
->server_domain
, p
, sizeof(fstring
), -1, STR_TERMINATE
);
174 fstrcpy(cli
->user_name
, "");
179 /****************************************************************************
180 Do a NT1 plaintext session setup.
181 ****************************************************************************/
183 static BOOL
cli_session_setup_plaintext(struct cli_state
*cli
, char *user
,
184 char *pass
, char *workgroup
)
186 uint32 capabilities
= cli_session_setup_capabilities(cli
);
191 passlen
= clistr_push(cli
, pword
, pass
, sizeof(pword
), STR_TERMINATE
|STR_ASCII
);
193 set_message(cli
->outbuf
,13,0,True
);
194 SCVAL(cli
->outbuf
,smb_com
,SMBsesssetupX
);
195 cli_setup_packet(cli
);
197 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
198 SSVAL(cli
->outbuf
,smb_vwv2
,CLI_BUFFER_SIZE
);
199 SSVAL(cli
->outbuf
,smb_vwv3
,2);
200 SSVAL(cli
->outbuf
,smb_vwv4
,cli
->pid
);
201 SIVAL(cli
->outbuf
,smb_vwv5
,cli
->sesskey
);
202 SSVAL(cli
->outbuf
,smb_vwv8
,0);
203 SIVAL(cli
->outbuf
,smb_vwv11
,capabilities
);
204 p
= smb_buf(cli
->outbuf
);
205 p
+= clistr_push(cli
, p
, pword
, -1, STR_TERMINATE
); /* password */
206 SSVAL(cli
->outbuf
,smb_vwv7
,PTR_DIFF(p
, smb_buf(cli
->outbuf
)));
207 p
+= clistr_push(cli
, p
, user
, -1, STR_TERMINATE
); /* username */
208 p
+= clistr_push(cli
, p
, workgroup
, -1, STR_TERMINATE
); /* workgroup */
209 p
+= clistr_push(cli
, p
, "Unix", -1, STR_TERMINATE
);
210 p
+= clistr_push(cli
, p
, "Samba", -1, STR_TERMINATE
);
211 cli_setup_bcc(cli
, p
);
214 if (!cli_receive_smb(cli
))
217 show_msg(cli
->inbuf
);
219 if (cli_is_error(cli
))
222 cli
->vuid
= SVAL(cli
->inbuf
,smb_uid
);
223 p
= smb_buf(cli
->inbuf
);
224 p
+= clistr_pull(cli
, cli
->server_os
, p
, sizeof(fstring
), -1, STR_TERMINATE
);
225 p
+= clistr_pull(cli
, cli
->server_type
, p
, sizeof(fstring
), -1, STR_TERMINATE
);
226 p
+= clistr_pull(cli
, cli
->server_domain
, p
, sizeof(fstring
), -1, STR_TERMINATE
);
227 fstrcpy(cli
->user_name
, user
);
232 static void set_signing_on_cli (struct cli_state
*cli
, char* pass
, uint8 response
[24])
235 ZERO_STRUCT(zero_sig
);
237 DEBUG(5, ("Server returned security sig:\n"));
238 dump_data(5, &cli
->inbuf
[smb_ss_field
], 8);
240 if (cli
->sign_info
.use_smb_signing
) {
241 DEBUG(5, ("smb signing already active on connection\n"));
242 } else if (memcmp(&cli
->inbuf
[smb_ss_field
], zero_sig
, 8) != 0) {
244 DEBUG(3, ("smb signing enabled!\n"));
245 cli
->sign_info
.use_smb_signing
= True
;
246 cli_calculate_mac_key(cli
, pass
, response
);
248 DEBUG(5, ("smb signing NOT enabled!\n"));
252 static void set_temp_signing_on_cli(struct cli_state
*cli
)
254 if (cli
->sign_info
.negotiated_smb_signing
)
255 cli
->sign_info
.temp_smb_signing
= True
;
259 /****************************************************************************
260 do a NT1 NTLM/LM encrypted session setup
261 @param cli client state to create do session setup on
263 @param pass *either* cleartext password (passlen !=24) or LM response.
264 @param ntpass NT response, implies ntpasslen >=24, implies pass is not clear
265 @param workgroup The user's domain.
266 ****************************************************************************/
268 static BOOL
cli_session_setup_nt1(struct cli_state
*cli
, char *user
,
269 char *pass
, int passlen
,
270 char *ntpass
, int ntpasslen
,
273 uint32 capabilities
= cli_session_setup_capabilities(cli
);
277 BOOL have_plaintext
= False
;
279 if (passlen
> sizeof(pword
) || ntpasslen
> sizeof(ntpword
))
283 /* non encrypted password supplied. Ignore ntpass. */
286 SMBencrypt(pass
,cli
->secblob
.data
,pword
);
287 SMBNTencrypt(pass
,cli
->secblob
.data
,ntpword
);
289 have_plaintext
= True
;
290 set_temp_signing_on_cli(cli
);
292 /* pre-encrypted password supplied. Only used for
293 security=server, can't do
294 signing becouse we don't have oringial key */
295 memcpy(pword
, pass
, 24);
297 memcpy(ntpword
, ntpass
, 24);
299 ZERO_STRUCT(ntpword
);
302 /* send a session setup command */
303 memset(cli
->outbuf
,'\0',smb_size
);
305 set_message(cli
->outbuf
,13,0,True
);
306 SCVAL(cli
->outbuf
,smb_com
,SMBsesssetupX
);
307 cli_setup_packet(cli
);
309 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
310 SSVAL(cli
->outbuf
,smb_vwv2
,CLI_BUFFER_SIZE
);
311 SSVAL(cli
->outbuf
,smb_vwv3
,2);
312 SSVAL(cli
->outbuf
,smb_vwv4
,cli
->pid
);
313 SIVAL(cli
->outbuf
,smb_vwv5
,cli
->sesskey
);
314 SSVAL(cli
->outbuf
,smb_vwv7
,passlen
);
315 SSVAL(cli
->outbuf
,smb_vwv8
,ntpasslen
);
316 SIVAL(cli
->outbuf
,smb_vwv11
,capabilities
);
317 p
= smb_buf(cli
->outbuf
);
318 memcpy(p
,pword
,passlen
); p
+= passlen
;
319 memcpy(p
,ntpword
,ntpasslen
); p
+= ntpasslen
;
320 p
+= clistr_push(cli
, p
, user
, -1, STR_TERMINATE
);
321 p
+= clistr_push(cli
, p
, workgroup
, -1, STR_TERMINATE
);
322 p
+= clistr_push(cli
, p
, "Unix", -1, STR_TERMINATE
);
323 p
+= clistr_push(cli
, p
, "Samba", -1, STR_TERMINATE
);
324 cli_setup_bcc(cli
, p
);
326 if (!cli_send_smb(cli
))
329 if (!cli_receive_smb(cli
))
332 show_msg(cli
->inbuf
);
334 if (cli_is_error(cli
))
337 /* use the returned vuid from now on */
338 cli
->vuid
= SVAL(cli
->inbuf
,smb_uid
);
340 p
= smb_buf(cli
->inbuf
);
341 p
+= clistr_pull(cli
, cli
->server_os
, p
, sizeof(fstring
), -1, STR_TERMINATE
);
342 p
+= clistr_pull(cli
, cli
->server_type
, p
, sizeof(fstring
), -1, STR_TERMINATE
);
343 p
+= clistr_pull(cli
, cli
->server_domain
, p
, sizeof(fstring
), -1, STR_TERMINATE
);
345 fstrcpy(cli
->user_name
, user
);
347 if (have_plaintext
) {
348 /* Have plaintext orginal */
349 set_signing_on_cli(cli
, pass
, ntpword
);
355 /****************************************************************************
356 Send a extended security session setup blob, returning a reply blob.
357 ****************************************************************************/
359 static DATA_BLOB
cli_session_setup_blob(struct cli_state
*cli
, DATA_BLOB blob
)
361 uint32 capabilities
= cli_session_setup_capabilities(cli
);
366 blob2
= data_blob(NULL
, 0);
368 capabilities
|= CAP_EXTENDED_SECURITY
;
370 /* send a session setup command */
371 memset(cli
->outbuf
,'\0',smb_size
);
373 set_message(cli
->outbuf
,12,0,True
);
374 SCVAL(cli
->outbuf
,smb_com
,SMBsesssetupX
);
376 set_temp_signing_on_cli(cli
);
378 cli_setup_packet(cli
);
380 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
381 SSVAL(cli
->outbuf
,smb_vwv2
,CLI_BUFFER_SIZE
);
382 SSVAL(cli
->outbuf
,smb_vwv3
,2);
383 SSVAL(cli
->outbuf
,smb_vwv4
,1);
384 SIVAL(cli
->outbuf
,smb_vwv5
,0);
385 SSVAL(cli
->outbuf
,smb_vwv7
,blob
.length
);
386 SIVAL(cli
->outbuf
,smb_vwv10
,capabilities
);
387 p
= smb_buf(cli
->outbuf
);
388 memcpy(p
, blob
.data
, blob
.length
);
390 p
+= clistr_push(cli
, p
, "Unix", -1, STR_TERMINATE
);
391 p
+= clistr_push(cli
, p
, "Samba", -1, STR_TERMINATE
);
392 cli_setup_bcc(cli
, p
);
395 if (!cli_receive_smb(cli
))
398 show_msg(cli
->inbuf
);
400 if (cli_is_error(cli
) && !NT_STATUS_EQUAL(cli_nt_error(cli
),
401 NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
405 /* use the returned vuid from now on */
406 cli
->vuid
= SVAL(cli
->inbuf
,smb_uid
);
408 p
= smb_buf(cli
->inbuf
);
410 blob2
= data_blob(p
, SVAL(cli
->inbuf
, smb_vwv3
));
413 p
+= clistr_pull(cli
, cli
->server_os
, p
, sizeof(fstring
), -1, STR_TERMINATE
);
415 /* w2k with kerberos doesn't properly null terminate this field */
416 len
= smb_buflen(cli
->inbuf
) - PTR_DIFF(p
, smb_buf(cli
->inbuf
));
417 p
+= clistr_pull(cli
, cli
->server_type
, p
, sizeof(fstring
), len
, 0);
423 /****************************************************************************
424 Do a spnego/kerberos encrypted session setup.
425 ****************************************************************************/
427 static BOOL
cli_session_setup_kerberos(struct cli_state
*cli
, char *principal
, char *workgroup
)
429 DATA_BLOB blob2
, negTokenTarg
;
431 DEBUG(2,("Doing kerberos session setup\n"));
433 /* generate the encapsulated kerberos5 ticket */
434 negTokenTarg
= spnego_gen_negTokenTarg(principal
, 0);
436 if (!negTokenTarg
.data
) return False
;
439 file_save("negTokenTarg.dat", negTokenTarg
.data
, negTokenTarg
.length
);
442 blob2
= cli_session_setup_blob(cli
, negTokenTarg
);
444 /* we don't need this blob for kerberos */
445 data_blob_free(&blob2
);
447 data_blob_free(&negTokenTarg
);
449 return !cli_is_error(cli
);
453 /****************************************************************************
454 Do a spnego/NTLMSSP encrypted session setup.
455 ****************************************************************************/
457 static BOOL
cli_session_setup_ntlmssp(struct cli_state
*cli
, char *user
,
458 char *pass
, char *workgroup
)
460 DATA_BLOB msg1
, struct_blob
;
461 DATA_BLOB blob
, chal1
, chal2
, auth
, challenge_blob
;
463 uint8 nthash
[24], lmhash
[24], sess_key
[16];
464 uint32 neg_flags
, chal_flags
, ntlmssp_command
, unkn1
, unkn2
;
465 pstring server_domain
; /* FIX THIS, SHOULD be UCS2-LE */
467 neg_flags
= NTLMSSP_NEGOTIATE_UNICODE
|
468 NTLMSSP_NEGOTIATE_128
|
469 NTLMSSP_NEGOTIATE_NTLM
;
471 memset(sess_key
, 0, 16);
473 DEBUG(10, ("sending NTLMSSP_NEGOTIATE\n"));
475 /* generate the ntlmssp negotiate packet */
476 msrpc_gen(&blob
, "CddAA",
480 workgroup
, strlen(workgroup
),
481 cli
->calling
.name
, strlen(cli
->calling
.name
) + 1);
482 DEBUG(10, ("neg_flags: %0X, workgroup: %s, calling name %s\n",
483 neg_flags
, workgroup
, cli
->calling
.name
));
484 /* and wrap it in a SPNEGO wrapper */
485 msg1
= gen_negTokenInit(OID_NTLMSSP
, blob
);
486 data_blob_free(&blob
);
488 /* now send that blob on its way */
489 blob
= cli_session_setup_blob(cli
, msg1
);
491 data_blob_free(&msg1
);
493 if (!NT_STATUS_EQUAL(cli_nt_error(cli
), NT_STATUS_MORE_PROCESSING_REQUIRED
))
497 file_save("chal.dat", blob
.data
, blob
.length
);
500 /* the server gives us back two challenges */
501 if (!spnego_parse_challenge(blob
, &chal1
, &chal2
)) {
502 DEBUG(3,("Failed to parse challenges\n"));
506 data_blob_free(&blob
);
509 * Ok, chal1 and chal2 are actually two identical copies of
510 * the NTLMSSP Challenge BLOB, and they contain, encoded in them
511 * the challenge to use.
514 if (!msrpc_parse(&chal1
, "CdUdbddB",
522 DEBUG(0, ("Failed to parse the NTLMSSP Challenge\n"));
526 if (ntlmssp_command
!= NTLMSSP_CHALLENGE
) {
527 DEBUG(0, ("NTLMSSP Response != NTLMSSP_CHALLENGE. Got %0X\n",
532 DEBUG(10, ("Challenge:\n"));
533 dump_data(10, challenge_blob
.data
, 8);
535 /* encrypt the password with the challenge which is in the blob */
536 memcpy(challenge
, challenge_blob
.data
, 8);
537 SMBencrypt(pass
, challenge
,lmhash
);
538 SMBNTencrypt(pass
, challenge
,nthash
);
539 data_blob_free(&challenge_blob
);
542 file_save("nthash.dat", nthash
, 24);
543 file_save("lmhash.dat", lmhash
, 24);
544 file_save("chal1.dat", chal1
.data
, chal1
.length
);
547 data_blob_free(&chal1
);
548 data_blob_free(&chal2
);
550 /* this generates the actual auth packet */
551 msrpc_gen(&blob
, "CdBBUUUBd",
562 /* wrap it in SPNEGO */
563 auth
= spnego_gen_auth(blob
);
565 data_blob_free(&blob
);
567 /* now send the auth packet and we should be done */
568 blob
= cli_session_setup_blob(cli
, auth
);
570 data_blob_free(&auth
);
571 data_blob_free(&blob
);
573 if (cli_is_error(cli
))
576 set_signing_on_cli(cli
, pass
, nthash
);
581 /****************************************************************************
582 Do a spnego encrypted session setup.
583 ****************************************************************************/
585 static BOOL
cli_session_setup_spnego(struct cli_state
*cli
, char *user
,
586 char *pass
, char *workgroup
)
589 char *OIDs
[ASN1_MAX_OIDS
];
591 BOOL got_kerberos_mechanism
= False
;
594 DEBUG(2,("Doing spnego session setup (blob length=%d)\n", cli
->secblob
.length
));
596 /* the server might not even do spnego */
597 if (cli
->secblob
.length
<= 16) {
598 DEBUG(3,("server didn't supply a full spnego negprot\n"));
603 file_save("negprot.dat", cli
->secblob
.data
, cli
->secblob
.length
);
606 /* there is 16 bytes of GUID before the real spnego packet starts */
607 blob
= data_blob(cli
->secblob
.data
+16, cli
->secblob
.length
-16);
609 /* the server sent us the first part of the SPNEGO exchange in the negprot
611 if (!spnego_parse_negTokenInit(blob
, OIDs
, &principal
)) {
612 data_blob_free(&blob
);
615 data_blob_free(&blob
);
617 /* make sure the server understands kerberos */
618 for (i
=0;OIDs
[i
];i
++) {
619 DEBUG(3,("got OID=%s\n", OIDs
[i
]));
620 if (strcmp(OIDs
[i
], OID_KERBEROS5_OLD
) == 0 ||
621 strcmp(OIDs
[i
], OID_KERBEROS5
) == 0) {
622 got_kerberos_mechanism
= True
;
626 DEBUG(3,("got principal=%s\n", principal
));
628 fstrcpy(cli
->user_name
, user
);
631 if (got_kerberos_mechanism
&& cli
->use_kerberos
) {
632 return cli_session_setup_kerberos(cli
, principal
, workgroup
);
640 return cli_session_setup_ntlmssp(cli
, user
, pass
, workgroup
);
643 /****************************************************************************
644 Send a session setup. The username and workgroup is in UNIX character
645 format and must be converted to DOS codepage format before sending. If the
646 password is in plaintext, the same should be done.
647 ****************************************************************************/
649 BOOL
cli_session_setup(struct cli_state
*cli
,
651 char *pass
, int passlen
,
652 char *ntpass
, int ntpasslen
,
658 /* allow for workgroups as part of the username */
659 fstrcpy(user2
, user
);
660 if ((p
=strchr_m(user2
,'\\')) || (p
=strchr_m(user2
,'/')) ||
661 (p
=strchr_m(user2
,*lp_winbind_separator()))) {
667 if (cli
->protocol
< PROTOCOL_LANMAN1
)
670 /* now work out what sort of session setup we are going to
671 do. I have split this into separate functions to make the
672 flow a bit easier to understand (tridge) */
674 /* if its an older server then we have to use the older request format */
676 if (cli
->protocol
< PROTOCOL_NT1
)
677 return cli_session_setup_lanman2(cli
, user
, pass
, passlen
, workgroup
);
679 /* if no user is supplied then we have to do an anonymous connection.
680 passwords are ignored */
683 return cli_session_setup_guest(cli
);
685 /* if the server is share level then send a plaintext null
686 password at this point. The password is sent in the tree
689 if ((cli
->sec_mode
& NEGOTIATE_SECURITY_USER_LEVEL
) == 0)
690 return cli_session_setup_plaintext(cli
, user
, "", workgroup
);
692 /* if the server doesn't support encryption then we have to use
693 plaintext. The second password is ignored */
695 if ((cli
->sec_mode
& NEGOTIATE_SECURITY_CHALLENGE_RESPONSE
) == 0)
696 return cli_session_setup_plaintext(cli
, user
, pass
, workgroup
);
698 /* Indidicate signing */
700 /* if the server supports extended security then use SPNEGO */
702 if (cli
->capabilities
& CAP_EXTENDED_SECURITY
)
703 return cli_session_setup_spnego(cli
, user
, pass
, workgroup
);
705 /* otherwise do a NT1 style session setup */
707 return cli_session_setup_nt1(cli
, user
,
708 pass
, passlen
, ntpass
, ntpasslen
,
712 /****************************************************************************
714 *****************************************************************************/
716 BOOL
cli_ulogoff(struct cli_state
*cli
)
718 memset(cli
->outbuf
,'\0',smb_size
);
719 set_message(cli
->outbuf
,2,0,True
);
720 SCVAL(cli
->outbuf
,smb_com
,SMBulogoffX
);
721 cli_setup_packet(cli
);
722 SSVAL(cli
->outbuf
,smb_vwv0
,0xFF);
723 SSVAL(cli
->outbuf
,smb_vwv2
,0); /* no additional info */
726 if (!cli_receive_smb(cli
))
729 return !cli_is_error(cli
);
732 /****************************************************************************
734 ****************************************************************************/
736 BOOL
cli_send_tconX(struct cli_state
*cli
,
737 const char *share
, const char *dev
, const char *pass
, int passlen
)
739 fstring fullshare
, pword
;
741 memset(cli
->outbuf
,'\0',smb_size
);
742 memset(cli
->inbuf
,'\0',smb_size
);
744 fstrcpy(cli
->share
, share
);
746 /* in user level security don't send a password now */
747 if (cli
->sec_mode
& NEGOTIATE_SECURITY_USER_LEVEL
) {
752 if ((cli
->sec_mode
& NEGOTIATE_SECURITY_CHALLENGE_RESPONSE
) && *pass
&& passlen
!= 24) {
754 * Non-encrypted passwords - convert to DOS codepage before encryption.
757 SMBencrypt(pass
,cli
->secblob
.data
,(uchar
*)pword
);
759 if((cli
->sec_mode
& (NEGOTIATE_SECURITY_USER_LEVEL
|NEGOTIATE_SECURITY_CHALLENGE_RESPONSE
)) == 0) {
761 * Non-encrypted passwords - convert to DOS codepage before using.
763 passlen
= clistr_push(cli
, pword
, pass
, -1, STR_TERMINATE
);
765 memcpy(pword
, pass
, passlen
);
769 if (cli
->port
== 445) {
770 slprintf(fullshare
, sizeof(fullshare
)-1,
773 slprintf(fullshare
, sizeof(fullshare
)-1,
774 "\\\\%s\\%s", cli
->desthost
, share
);
777 set_message(cli
->outbuf
,4, 0, True
);
778 SCVAL(cli
->outbuf
,smb_com
,SMBtconX
);
779 cli_setup_packet(cli
);
781 SSVAL(cli
->outbuf
,smb_vwv0
,0xFF);
782 SSVAL(cli
->outbuf
,smb_vwv3
,passlen
);
784 p
= smb_buf(cli
->outbuf
);
785 memcpy(p
,pword
,passlen
);
787 p
+= clistr_push(cli
, p
, fullshare
, -1, STR_TERMINATE
|STR_UPPER
);
788 fstrcpy(p
, dev
); p
+= strlen(dev
)+1;
790 cli_setup_bcc(cli
, p
);
793 if (!cli_receive_smb(cli
))
796 if (cli_is_error(cli
))
799 clistr_pull(cli
, cli
->dev
, smb_buf(cli
->inbuf
), sizeof(fstring
), -1, STR_TERMINATE
|STR_ASCII
);
801 if (strcasecmp(share
,"IPC$")==0)
802 fstrcpy(cli
->dev
, "IPC");
804 if (cli
->protocol
>= PROTOCOL_NT1
&&
805 smb_buflen(cli
->inbuf
) == 3) {
806 /* almost certainly win95 - enable bug fixes */
810 cli
->cnum
= SVAL(cli
->inbuf
,smb_tid
);
814 /****************************************************************************
815 Send a tree disconnect.
816 ****************************************************************************/
818 BOOL
cli_tdis(struct cli_state
*cli
)
820 memset(cli
->outbuf
,'\0',smb_size
);
821 set_message(cli
->outbuf
,0,0,True
);
822 SCVAL(cli
->outbuf
,smb_com
,SMBtdis
);
823 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
824 cli_setup_packet(cli
);
827 if (!cli_receive_smb(cli
))
830 return !cli_is_error(cli
);
833 /****************************************************************************
834 Send a negprot command.
835 ****************************************************************************/
837 void cli_negprot_send(struct cli_state
*cli
)
842 if (cli
->protocol
< PROTOCOL_NT1
)
843 cli
->use_spnego
= False
;
845 memset(cli
->outbuf
,'\0',smb_size
);
847 /* setup the protocol strings */
848 set_message(cli
->outbuf
,0,0,True
);
850 p
= smb_buf(cli
->outbuf
);
852 prots
[numprots
].name
&& prots
[numprots
].prot
<=cli
->protocol
;
855 p
+= clistr_push(cli
, p
, prots
[numprots
].name
, -1, STR_TERMINATE
);
858 SCVAL(cli
->outbuf
,smb_com
,SMBnegprot
);
859 cli_setup_bcc(cli
, p
);
860 cli_setup_packet(cli
);
862 SCVAL(smb_buf(cli
->outbuf
),0,2);
867 /****************************************************************************
868 Send a negprot command.
869 ****************************************************************************/
871 BOOL
cli_negprot(struct cli_state
*cli
)
877 if (cli
->sign_info
.use_smb_signing
) {
878 DEBUG(0, ("Cannot send negprot again, particularly after setting up SMB Signing\n"));
882 if (cli
->protocol
< PROTOCOL_NT1
)
883 cli
->use_spnego
= False
;
885 memset(cli
->outbuf
,'\0',smb_size
);
887 /* setup the protocol strings */
888 for (plength
=0,numprots
=0;
889 prots
[numprots
].name
&& prots
[numprots
].prot
<=cli
->protocol
;
891 plength
+= strlen(prots
[numprots
].name
)+2;
893 set_message(cli
->outbuf
,0,plength
,True
);
895 p
= smb_buf(cli
->outbuf
);
897 prots
[numprots
].name
&& prots
[numprots
].prot
<=cli
->protocol
;
900 p
+= clistr_push(cli
, p
, prots
[numprots
].name
, -1, STR_TERMINATE
);
903 SCVAL(cli
->outbuf
,smb_com
,SMBnegprot
);
904 cli_setup_packet(cli
);
906 SCVAL(smb_buf(cli
->outbuf
),0,2);
909 if (!cli_receive_smb(cli
))
912 show_msg(cli
->inbuf
);
914 if (cli_is_error(cli
) ||
915 ((int)SVAL(cli
->inbuf
,smb_vwv0
) >= numprots
)) {
919 cli
->protocol
= prots
[SVAL(cli
->inbuf
,smb_vwv0
)].prot
;
921 if (cli
->protocol
>= PROTOCOL_NT1
) {
923 cli
->sec_mode
= CVAL(cli
->inbuf
,smb_vwv1
);
924 cli
->max_mux
= SVAL(cli
->inbuf
, smb_vwv1
+1);
925 cli
->max_xmit
= IVAL(cli
->inbuf
,smb_vwv3
+1);
926 cli
->sesskey
= IVAL(cli
->inbuf
,smb_vwv7
+1);
927 cli
->serverzone
= SVALS(cli
->inbuf
,smb_vwv15
+1);
928 cli
->serverzone
*= 60;
929 /* this time arrives in real GMT */
930 cli
->servertime
= interpret_long_date(cli
->inbuf
+smb_vwv11
+1);
931 cli
->secblob
= data_blob(smb_buf(cli
->inbuf
),smb_buflen(cli
->inbuf
));
932 cli
->capabilities
= IVAL(cli
->inbuf
,smb_vwv9
+1);
933 if (cli
->capabilities
& CAP_RAW_MODE
) {
934 cli
->readbraw_supported
= True
;
935 cli
->writebraw_supported
= True
;
937 /* work out if they sent us a workgroup */
938 if (!(cli
->capabilities
& CAP_EXTENDED_SECURITY
) &&
939 smb_buflen(cli
->inbuf
) > 8) {
940 clistr_pull(cli
, cli
->server_domain
,
941 smb_buf(cli
->inbuf
)+8, sizeof(cli
->server_domain
),
942 smb_buflen(cli
->inbuf
)-8, STR_UNICODE
|STR_NOALIGN
);
945 if ((cli
->sec_mode
& NEGOTIATE_SECURITY_SIGNATURES_ENABLED
))
946 cli
->sign_info
.negotiated_smb_signing
= True
;
948 } else if (cli
->protocol
>= PROTOCOL_LANMAN1
) {
949 cli
->use_spnego
= False
;
950 cli
->sec_mode
= SVAL(cli
->inbuf
,smb_vwv1
);
951 cli
->max_xmit
= SVAL(cli
->inbuf
,smb_vwv2
);
952 cli
->sesskey
= IVAL(cli
->inbuf
,smb_vwv6
);
953 cli
->serverzone
= SVALS(cli
->inbuf
,smb_vwv10
);
954 cli
->serverzone
*= 60;
955 /* this time is converted to GMT by make_unix_date */
956 cli
->servertime
= make_unix_date(cli
->inbuf
+smb_vwv8
);
957 cli
->readbraw_supported
= ((SVAL(cli
->inbuf
,smb_vwv5
) & 0x1) != 0);
958 cli
->writebraw_supported
= ((SVAL(cli
->inbuf
,smb_vwv5
) & 0x2) != 0);
959 cli
->secblob
= data_blob(smb_buf(cli
->inbuf
),smb_buflen(cli
->inbuf
));
961 /* the old core protocol */
962 cli
->use_spnego
= False
;
964 cli
->serverzone
= TimeDiff(time(NULL
));
967 cli
->max_xmit
= MIN(cli
->max_xmit
, CLI_BUFFER_SIZE
);
969 /* a way to force ascii SMB */
970 if (getenv("CLI_FORCE_ASCII"))
971 cli
->capabilities
&= ~CAP_UNICODE
;
976 /****************************************************************************
977 Send a session request. See rfc1002.txt 4.3 and 4.3.2.
978 ****************************************************************************/
980 BOOL
cli_session_request(struct cli_state
*cli
,
981 struct nmb_name
*calling
, struct nmb_name
*called
)
985 extern pstring user_socket_options
;
987 memcpy(&(cli
->calling
), calling
, sizeof(*calling
));
988 memcpy(&(cli
->called
), called
, sizeof(*called
));
990 /* put in the destination name */
992 name_mangle(cli
->called
.name
, p
, cli
->called
.name_type
);
997 name_mangle(cli
->calling
.name
, p
, cli
->calling
.name_type
);
1000 /* 445 doesn't have session request */
1001 if (cli
->port
== 445)
1004 if (cli
->sign_info
.use_smb_signing
) {
1005 DEBUG(0, ("Cannot send session resquest again, particularly after setting up SMB Signing\n"));
1009 /* send a session request (RFC 1002) */
1010 /* setup the packet length
1011 * Remove four bytes from the length count, since the length
1012 * field in the NBT Session Service header counts the number
1013 * of bytes which follow. The cli_send_smb() function knows
1014 * about this and accounts for those four bytes.
1018 _smb_setlen(cli
->outbuf
,len
);
1019 SCVAL(cli
->outbuf
,0,0x81);
1022 DEBUG(5,("Sent session request\n"));
1024 if (!cli_receive_smb(cli
))
1027 if (CVAL(cli
->inbuf
,0) == 0x84) {
1028 /* C. Hoch 9/14/95 Start */
1029 /* For information, here is the response structure.
1030 * We do the byte-twiddling to for portability.
1031 struct RetargetResponse{
1033 unsigned char flags;
1039 int port
= (CVAL(cli
->inbuf
,8)<<8)+CVAL(cli
->inbuf
,9);
1040 /* SESSION RETARGET */
1041 putip((char *)&cli
->dest_ip
,cli
->inbuf
+4);
1043 cli
->fd
= open_socket_out(SOCK_STREAM
, &cli
->dest_ip
, port
, LONG_CONNECT_TIMEOUT
);
1047 DEBUG(3,("Retargeted\n"));
1049 set_socket_options(cli
->fd
,user_socket_options
);
1056 DEBUG(0,("Retarget recursion - failing\n"));
1060 ret
= cli_session_request(cli
, calling
, called
);
1064 } /* C. Hoch 9/14/95 End */
1066 if (CVAL(cli
->inbuf
,0) != 0x82) {
1067 /* This is the wrong place to put the error... JRA. */
1068 cli
->rap_error
= CVAL(cli
->inbuf
,4);
1074 /****************************************************************************
1075 Open the client sockets.
1076 ****************************************************************************/
1078 BOOL
cli_connect(struct cli_state
*cli
, const char *host
, struct in_addr
*ip
)
1080 extern pstring user_socket_options
;
1081 int name_type
= 0x20;
1084 /* reasonable default hostname */
1085 if (!host
) host
= "*SMBSERVER";
1087 fstrcpy(cli
->desthost
, host
);
1089 /* allow hostnames of the form NAME#xx and do a netbios lookup */
1090 if ((p
= strchr(cli
->desthost
, '#'))) {
1091 name_type
= strtol(p
+1, NULL
, 16);
1095 if (!ip
|| is_zero_ip(*ip
)) {
1096 if (!resolve_name(cli
->desthost
, &cli
->dest_ip
, name_type
)) {
1099 if (ip
) *ip
= cli
->dest_ip
;
1104 if (getenv("LIBSMB_PROG")) {
1105 cli
->fd
= sock_exec(getenv("LIBSMB_PROG"));
1107 /* try 445 first, then 139 */
1108 int port
= cli
->port
?cli
->port
:445;
1109 cli
->fd
= open_socket_out(SOCK_STREAM
, &cli
->dest_ip
,
1110 port
, cli
->timeout
);
1111 if (cli
->fd
== -1 && cli
->port
== 0) {
1113 cli
->fd
= open_socket_out(SOCK_STREAM
, &cli
->dest_ip
,
1114 port
, cli
->timeout
);
1119 if (cli
->fd
== -1) {
1120 DEBUG(1,("Error connecting to %s (%s)\n",
1121 ip
?inet_ntoa(*ip
):host
,strerror(errno
)));
1125 set_socket_options(cli
->fd
,user_socket_options
);
1130 /****************************************************************************
1131 Initialise client credentials for authenticated pipe access.
1132 ****************************************************************************/
1134 static void init_creds(struct ntuser_creds
*creds
, char* username
,
1135 char* domain
, char* password
)
1137 ZERO_STRUCTP(creds
);
1139 pwd_set_cleartext(&creds
->pwd
, password
);
1141 fstrcpy(creds
->user_name
, username
);
1142 fstrcpy(creds
->domain
, domain
);
1145 creds
->pwd
.null_pwd
= True
;
1150 establishes a connection right up to doing tconX, password specified.
1151 @param output_cli A fully initialised cli structure, non-null only on success
1152 @param dest_host The netbios name of the remote host
1153 @param dest_ip (optional) The the destination IP, NULL for name based lookup
1154 @param port (optional) The destination port (0 for default)
1155 @param service (optional) The share to make the connection to. Should be 'unqualified' in any way.
1156 @param service_type The 'type' of serivice.
1157 @param user Username, unix string
1158 @param domain User's domain
1159 @param password User's password, unencrypted unix string.
1162 NTSTATUS
cli_full_connection(struct cli_state
**output_cli
,
1163 const char *my_name
,
1164 const char *dest_host
,
1165 struct in_addr
*dest_ip
, int port
,
1166 char *service
, char *service_type
,
1167 char *user
, char *domain
,
1168 char *password
, int flags
)
1170 struct ntuser_creds creds
;
1172 struct nmb_name calling
;
1173 struct nmb_name called
;
1174 struct cli_state
*cli
;
1176 extern pstring global_myname
;
1179 my_name
= global_myname
;
1181 if (!(cli
= cli_initialise(NULL
)))
1182 return NT_STATUS_NO_MEMORY
;
1184 make_nmb_name(&calling
, my_name
, 0x0);
1185 make_nmb_name(&called
, dest_host
, 0x20);
1187 if (cli_set_port(cli
, port
) != port
) {
1189 return NT_STATUS_UNSUCCESSFUL
;
1199 DEBUG(3,("Connecting to host=%s share=%s\n", dest_host
, service
));
1201 if (!cli_connect(cli
, dest_host
, &ip
)) {
1202 DEBUG(1,("cli_full_connection: failed to connect to %s (%s)\n",
1203 nmb_namestr(&called
), inet_ntoa(ip
)));
1205 return NT_STATUS_UNSUCCESSFUL
;
1208 if (!cli_session_request(cli
, &calling
, &called
)) {
1210 DEBUG(1,("session request to %s failed (%s)\n",
1211 called
.name
, cli_errstr(cli
)));
1212 if ((p
=strchr(called
.name
, '.')) && !is_ipaddress(called
.name
)) {
1216 if (strcmp(called
.name
, "*SMBSERVER")) {
1217 make_nmb_name(&called
, "*SMBSERVER", 0x20);
1220 return NT_STATUS_UNSUCCESSFUL
;
1223 if (flags
& CLI_FULL_CONNECTION_DONT_SPNEGO
)
1224 cli
->use_spnego
= False
;
1225 else if (flags
& CLI_FULL_CONNECTION_USE_KERBEROS
)
1226 cli
->use_kerberos
= True
;
1228 if (!cli_negprot(cli
)) {
1229 DEBUG(1,("failed negprot\n"));
1230 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1235 if (!cli_session_setup(cli
, user
, password
, strlen(password
)+1,
1236 password
, strlen(password
)+1,
1238 if ((flags
& CLI_FULL_CONNECTION_ANNONYMOUS_FALLBACK
)
1239 && cli_session_setup(cli
, "", "", 0, "", 0, domain
)) {
1241 nt_status
= cli_nt_error(cli
);
1242 DEBUG(1,("failed session setup with %s\n", nt_errstr(nt_status
)));
1244 if (NT_STATUS_IS_OK(nt_status
))
1245 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1251 if (!cli_send_tconX(cli
, service
, service_type
,
1252 (char*)password
, strlen(password
)+1)) {
1253 DEBUG(1,("failed tcon_X with %s\n", nt_errstr(nt_status
)));
1254 nt_status
= cli_nt_error(cli
);
1256 if (NT_STATUS_IS_OK(nt_status
)) {
1257 nt_status
= NT_STATUS_UNSUCCESSFUL
;
1263 init_creds(&creds
, user
, domain
, password
);
1264 cli_init_creds(cli
, &creds
);
1267 return NT_STATUS_OK
;
1270 /****************************************************************************
1271 Attempt a NetBIOS session request, falling back to *SMBSERVER if needed.
1272 ****************************************************************************/
1274 BOOL
attempt_netbios_session_request(struct cli_state
*cli
, char *srchost
, char *desthost
,
1275 struct in_addr
*pdest_ip
)
1277 struct nmb_name calling
, called
;
1279 make_nmb_name(&calling
, srchost
, 0x0);
1282 * If the called name is an IP address
1283 * then use *SMBSERVER immediately.
1286 if(is_ipaddress(desthost
))
1287 make_nmb_name(&called
, "*SMBSERVER", 0x20);
1289 make_nmb_name(&called
, desthost
, 0x20);
1291 if (!cli_session_request(cli
, &calling
, &called
)) {
1292 struct nmb_name smbservername
;
1294 make_nmb_name(&smbservername
, "*SMBSERVER", 0x20);
1297 * If the name wasn't *SMBSERVER then
1298 * try with *SMBSERVER if the first name fails.
1301 if (nmb_name_equal(&called
, &smbservername
)) {
1304 * The name used was *SMBSERVER, don't bother with another name.
1307 DEBUG(0,("attempt_netbios_session_request: %s rejected the session for name *SMBSERVER \
1308 with error %s.\n", desthost
, cli_errstr(cli
) ));
1313 * We need to close the connection here but can't call cli_shutdown as
1314 * will free an allocated cli struct. cli_close_connection was invented
1315 * for this purpose. JRA. Based on work by "Kim R. Pedersen" <krp@filanet.dk>.
1318 cli_close_connection(cli
);
1320 if (!cli_initialise(cli
) ||
1321 !cli_connect(cli
, desthost
, pdest_ip
) ||
1322 !cli_session_request(cli
, &calling
, &smbservername
)) {
1323 DEBUG(0,("attempt_netbios_session_request: %s rejected the session for \
1324 name *SMBSERVER with error %s\n", desthost
, cli_errstr(cli
) ));