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/>.
27 {PROTOCOL_CORE,"PC NETWORK PROGRAM 1.0"},
28 {PROTOCOL_COREPLUS,"MICROSOFT NETWORKS 1.03"},
29 {PROTOCOL_LANMAN1,"MICROSOFT NETWORKS 3.0"},
30 {PROTOCOL_LANMAN1,"LANMAN1.0"},
31 {PROTOCOL_LANMAN2,"LM1.2X002"},
32 {PROTOCOL_LANMAN2,"DOS LANMAN2.1"},
33 {PROTOCOL_LANMAN2,"LANMAN2.1"},
34 {PROTOCOL_LANMAN2,"Samba"},
35 {PROTOCOL_NT1,"NT LANMAN 1.0"},
36 {PROTOCOL_NT1,"NT LM 0.12"},
41 * Set the user session key for a connection
42 * @param cli The cli structure to add it too
43 * @param session_key The session key used. (A copy of this is taken for the cli struct)
47 static void cli_set_session_key (struct cli_state *cli, const DATA_BLOB session_key)
49 cli->user_session_key = data_blob(session_key.data, session_key.length);
52 /****************************************************************************
53 Do an old lanman2 style session setup.
54 ****************************************************************************/
56 static NTSTATUS cli_session_setup_lanman2(struct cli_state *cli,
58 const char *pass, size_t passlen,
59 const char *workgroup)
61 DATA_BLOB session_key = data_blob_null;
62 DATA_BLOB lm_response = data_blob_null;
66 if (passlen > sizeof(pword)-1) {
67 return NT_STATUS_INVALID_PARAMETER;
70 /* LANMAN servers predate NT status codes and Unicode and ignore those
71 smb flags so we must disable the corresponding default capabilities
72 that would otherwise cause the Unicode and NT Status flags to be
73 set (and even returned by the server) */
75 cli->capabilities &= ~(CAP_UNICODE | CAP_STATUS32);
77 /* if in share level security then don't send a password now */
78 if (!(cli->sec_mode & NEGOTIATE_SECURITY_USER_LEVEL))
81 if (passlen > 0 && (cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) && passlen != 24) {
82 /* Encrypted mode needed, and non encrypted password supplied. */
83 lm_response = data_blob(NULL, 24);
84 if (!SMBencrypt(pass, cli->secblob.data,(uchar *)lm_response.data)) {
85 DEBUG(1, ("Password is > 14 chars in length, and is therefore incompatible with Lanman authentication\n"));
86 return NT_STATUS_ACCESS_DENIED;
88 } else if ((cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) && passlen == 24) {
89 /* Encrypted mode needed, and encrypted password supplied. */
90 lm_response = data_blob(pass, passlen);
91 } else if (passlen > 0) {
92 /* Plaintext mode needed, assume plaintext supplied. */
93 passlen = clistr_push(cli, pword, pass, sizeof(pword), STR_TERMINATE);
94 lm_response = data_blob(pass, passlen);
97 /* send a session setup command */
98 memset(cli->outbuf,'\0',smb_size);
99 set_message(cli->outbuf,10, 0, True);
100 SCVAL(cli->outbuf,smb_com,SMBsesssetupX);
101 cli_setup_packet(cli);
103 SCVAL(cli->outbuf,smb_vwv0,0xFF);
104 SSVAL(cli->outbuf,smb_vwv2,cli->max_xmit);
105 SSVAL(cli->outbuf,smb_vwv3,2);
106 SSVAL(cli->outbuf,smb_vwv4,1);
107 SIVAL(cli->outbuf,smb_vwv5,cli->sesskey);
108 SSVAL(cli->outbuf,smb_vwv7,lm_response.length);
110 p = smb_buf(cli->outbuf);
111 memcpy(p,lm_response.data,lm_response.length);
112 p += lm_response.length;
113 p += clistr_push(cli, p, user, -1, STR_TERMINATE|STR_UPPER);
114 p += clistr_push(cli, p, workgroup, -1, STR_TERMINATE|STR_UPPER);
115 p += clistr_push(cli, p, "Unix", -1, STR_TERMINATE);
116 p += clistr_push(cli, p, "Samba", -1, STR_TERMINATE);
117 cli_setup_bcc(cli, p);
119 if (!cli_send_smb(cli) || !cli_receive_smb(cli)) {
120 return cli_nt_error(cli);
123 show_msg(cli->inbuf);
125 if (cli_is_error(cli)) {
126 return cli_nt_error(cli);
129 /* use the returned vuid from now on */
130 cli->vuid = SVAL(cli->inbuf,smb_uid);
131 fstrcpy(cli->user_name, user);
133 if (session_key.data) {
134 /* Have plaintext orginal */
135 cli_set_session_key(cli, session_key);
141 /****************************************************************************
142 Work out suitable capabilities to offer the server.
143 ****************************************************************************/
145 static uint32 cli_session_setup_capabilities(struct cli_state *cli)
147 uint32 capabilities = CAP_NT_SMBS;
149 if (!cli->force_dos_errors)
150 capabilities |= CAP_STATUS32;
152 if (cli->use_level_II_oplocks)
153 capabilities |= CAP_LEVEL_II_OPLOCKS;
155 capabilities |= (cli->capabilities & (CAP_UNICODE|CAP_LARGE_FILES|CAP_LARGE_READX|CAP_LARGE_WRITEX|CAP_DFS));
159 /****************************************************************************
160 Do a NT1 guest session setup.
161 ****************************************************************************/
163 static NTSTATUS cli_session_setup_guest(struct cli_state *cli)
166 uint32 capabilities = cli_session_setup_capabilities(cli);
168 memset(cli->outbuf, '\0', smb_size);
169 set_message(cli->outbuf,13,0,True);
170 SCVAL(cli->outbuf,smb_com,SMBsesssetupX);
171 cli_setup_packet(cli);
173 SCVAL(cli->outbuf,smb_vwv0,0xFF);
174 SSVAL(cli->outbuf,smb_vwv2,CLI_BUFFER_SIZE);
175 SSVAL(cli->outbuf,smb_vwv3,2);
176 SSVAL(cli->outbuf,smb_vwv4,cli->pid);
177 SIVAL(cli->outbuf,smb_vwv5,cli->sesskey);
178 SSVAL(cli->outbuf,smb_vwv7,0);
179 SSVAL(cli->outbuf,smb_vwv8,0);
180 SIVAL(cli->outbuf,smb_vwv11,capabilities);
181 p = smb_buf(cli->outbuf);
182 p += clistr_push(cli, p, "", -1, STR_TERMINATE); /* username */
183 p += clistr_push(cli, p, "", -1, STR_TERMINATE); /* workgroup */
184 p += clistr_push(cli, p, "Unix", -1, STR_TERMINATE);
185 p += clistr_push(cli, p, "Samba", -1, STR_TERMINATE);
186 cli_setup_bcc(cli, p);
188 if (!cli_send_smb(cli) || !cli_receive_smb(cli)) {
189 return cli_nt_error(cli);
192 show_msg(cli->inbuf);
194 if (cli_is_error(cli)) {
195 return cli_nt_error(cli);
198 cli->vuid = SVAL(cli->inbuf,smb_uid);
200 p = smb_buf(cli->inbuf);
201 p += clistr_pull(cli, cli->server_os, p, sizeof(fstring), -1, STR_TERMINATE);
202 p += clistr_pull(cli, cli->server_type, p, sizeof(fstring), -1, STR_TERMINATE);
203 p += clistr_pull(cli, cli->server_domain, p, sizeof(fstring), -1, STR_TERMINATE);
205 if (strstr(cli->server_type, "Samba")) {
206 cli->is_samba = True;
209 fstrcpy(cli->user_name, "");
214 /****************************************************************************
215 Do a NT1 plaintext session setup.
216 ****************************************************************************/
218 static NTSTATUS cli_session_setup_plaintext(struct cli_state *cli,
219 const char *user, const char *pass,
220 const char *workgroup)
222 uint32 capabilities = cli_session_setup_capabilities(cli);
226 fstr_sprintf( lanman, "Samba %s", SAMBA_VERSION_STRING);
228 memset(cli->outbuf, '\0', smb_size);
229 set_message(cli->outbuf,13,0,True);
230 SCVAL(cli->outbuf,smb_com,SMBsesssetupX);
231 cli_setup_packet(cli);
233 SCVAL(cli->outbuf,smb_vwv0,0xFF);
234 SSVAL(cli->outbuf,smb_vwv2,CLI_BUFFER_SIZE);
235 SSVAL(cli->outbuf,smb_vwv3,2);
236 SSVAL(cli->outbuf,smb_vwv4,cli->pid);
237 SIVAL(cli->outbuf,smb_vwv5,cli->sesskey);
238 SSVAL(cli->outbuf,smb_vwv8,0);
239 SIVAL(cli->outbuf,smb_vwv11,capabilities);
240 p = smb_buf(cli->outbuf);
242 /* check wether to send the ASCII or UNICODE version of the password */
244 if ( (capabilities & CAP_UNICODE) == 0 ) {
245 p += clistr_push(cli, p, pass, -1, STR_TERMINATE); /* password */
246 SSVAL(cli->outbuf,smb_vwv7,PTR_DIFF(p, smb_buf(cli->outbuf)));
249 p += clistr_push(cli, p, pass, -1, STR_UNICODE|STR_TERMINATE); /* unicode password */
250 SSVAL(cli->outbuf,smb_vwv8,PTR_DIFF(p, smb_buf(cli->outbuf)));
253 p += clistr_push(cli, p, user, -1, STR_TERMINATE); /* username */
254 p += clistr_push(cli, p, workgroup, -1, STR_TERMINATE); /* workgroup */
255 p += clistr_push(cli, p, "Unix", -1, STR_TERMINATE);
256 p += clistr_push(cli, p, lanman, -1, STR_TERMINATE);
257 cli_setup_bcc(cli, p);
259 if (!cli_send_smb(cli) || !cli_receive_smb(cli)) {
260 return cli_nt_error(cli);
263 show_msg(cli->inbuf);
265 if (cli_is_error(cli)) {
266 return cli_nt_error(cli);
269 cli->vuid = SVAL(cli->inbuf,smb_uid);
270 p = smb_buf(cli->inbuf);
271 p += clistr_pull(cli, cli->server_os, p, sizeof(fstring), -1, STR_TERMINATE);
272 p += clistr_pull(cli, cli->server_type, p, sizeof(fstring), -1, STR_TERMINATE);
273 p += clistr_pull(cli, cli->server_domain, p, sizeof(fstring), -1, STR_TERMINATE);
274 fstrcpy(cli->user_name, user);
276 if (strstr(cli->server_type, "Samba")) {
277 cli->is_samba = True;
283 /****************************************************************************
284 do a NT1 NTLM/LM encrypted session setup - for when extended security
286 @param cli client state to create do session setup on
288 @param pass *either* cleartext password (passlen !=24) or LM response.
289 @param ntpass NT response, implies ntpasslen >=24, implies pass is not clear
290 @param workgroup The user's domain.
291 ****************************************************************************/
293 static NTSTATUS cli_session_setup_nt1(struct cli_state *cli, const char *user,
294 const char *pass, size_t passlen,
295 const char *ntpass, size_t ntpasslen,
296 const char *workgroup)
298 uint32 capabilities = cli_session_setup_capabilities(cli);
299 DATA_BLOB lm_response = data_blob_null;
300 DATA_BLOB nt_response = data_blob_null;
301 DATA_BLOB session_key = data_blob_null;
306 /* do nothing - guest login */
307 } else if (passlen != 24) {
308 if (lp_client_ntlmv2_auth()) {
309 DATA_BLOB server_chal;
310 DATA_BLOB names_blob;
311 server_chal = data_blob(cli->secblob.data, MIN(cli->secblob.length, 8));
313 /* note that the 'workgroup' here is a best guess - we don't know
314 the server's domain at this point. The 'server name' is also
317 names_blob = NTLMv2_generate_names_blob(cli->called.name, workgroup);
319 if (!SMBNTLMv2encrypt(user, workgroup, pass, &server_chal,
321 &lm_response, &nt_response, &session_key)) {
322 data_blob_free(&names_blob);
323 data_blob_free(&server_chal);
324 return NT_STATUS_ACCESS_DENIED;
326 data_blob_free(&names_blob);
327 data_blob_free(&server_chal);
331 E_md4hash(pass, nt_hash);
334 nt_response = data_blob_null;
336 nt_response = data_blob(NULL, 24);
337 SMBNTencrypt(pass,cli->secblob.data,nt_response.data);
339 /* non encrypted password supplied. Ignore ntpass. */
340 if (lp_client_lanman_auth()) {
341 lm_response = data_blob(NULL, 24);
342 if (!SMBencrypt(pass,cli->secblob.data, lm_response.data)) {
343 /* Oops, the LM response is invalid, just put
344 the NT response there instead */
345 data_blob_free(&lm_response);
346 lm_response = data_blob(nt_response.data, nt_response.length);
349 /* LM disabled, place NT# in LM field instead */
350 lm_response = data_blob(nt_response.data, nt_response.length);
353 session_key = data_blob(NULL, 16);
355 E_deshash(pass, session_key.data);
356 memset(&session_key.data[8], '\0', 8);
358 SMBsesskeygen_ntv1(nt_hash, NULL, session_key.data);
362 cli_simple_set_signing(cli, session_key, lm_response);
364 cli_simple_set_signing(cli, session_key, nt_response);
367 /* pre-encrypted password supplied. Only used for
368 security=server, can't do
369 signing because we don't have original key */
371 lm_response = data_blob(pass, passlen);
372 nt_response = data_blob(ntpass, ntpasslen);
375 /* send a session setup command */
376 memset(cli->outbuf,'\0',smb_size);
378 set_message(cli->outbuf,13,0,True);
379 SCVAL(cli->outbuf,smb_com,SMBsesssetupX);
380 cli_setup_packet(cli);
382 SCVAL(cli->outbuf,smb_vwv0,0xFF);
383 SSVAL(cli->outbuf,smb_vwv2,CLI_BUFFER_SIZE);
384 SSVAL(cli->outbuf,smb_vwv3,2);
385 SSVAL(cli->outbuf,smb_vwv4,cli->pid);
386 SIVAL(cli->outbuf,smb_vwv5,cli->sesskey);
387 SSVAL(cli->outbuf,smb_vwv7,lm_response.length);
388 SSVAL(cli->outbuf,smb_vwv8,nt_response.length);
389 SIVAL(cli->outbuf,smb_vwv11,capabilities);
390 p = smb_buf(cli->outbuf);
391 if (lm_response.length) {
392 memcpy(p,lm_response.data, lm_response.length); p += lm_response.length;
394 if (nt_response.length) {
395 memcpy(p,nt_response.data, nt_response.length); p += nt_response.length;
397 p += clistr_push(cli, p, user, -1, STR_TERMINATE);
399 /* Upper case here might help some NTLMv2 implementations */
400 p += clistr_push(cli, p, workgroup, -1, STR_TERMINATE|STR_UPPER);
401 p += clistr_push(cli, p, "Unix", -1, STR_TERMINATE);
402 p += clistr_push(cli, p, "Samba", -1, STR_TERMINATE);
403 cli_setup_bcc(cli, p);
405 if (!cli_send_smb(cli) || !cli_receive_smb(cli)) {
406 result = cli_nt_error(cli);
410 /* show_msg(cli->inbuf); */
412 if (cli_is_error(cli)) {
413 result = cli_nt_error(cli);
417 /* use the returned vuid from now on */
418 cli->vuid = SVAL(cli->inbuf,smb_uid);
420 p = smb_buf(cli->inbuf);
421 p += clistr_pull(cli, cli->server_os, p, sizeof(fstring), -1, STR_TERMINATE);
422 p += clistr_pull(cli, cli->server_type, p, sizeof(fstring), -1, STR_TERMINATE);
423 p += clistr_pull(cli, cli->server_domain, p, sizeof(fstring), -1, STR_TERMINATE);
425 if (strstr(cli->server_type, "Samba")) {
426 cli->is_samba = True;
429 fstrcpy(cli->user_name, user);
431 if (session_key.data) {
432 /* Have plaintext orginal */
433 cli_set_session_key(cli, session_key);
436 result = NT_STATUS_OK;
438 data_blob_free(&lm_response);
439 data_blob_free(&nt_response);
440 data_blob_free(&session_key);
444 /****************************************************************************
445 Send a extended security session setup blob
446 ****************************************************************************/
448 static bool cli_session_setup_blob_send(struct cli_state *cli, DATA_BLOB blob)
450 uint32 capabilities = cli_session_setup_capabilities(cli);
453 capabilities |= CAP_EXTENDED_SECURITY;
455 /* send a session setup command */
456 memset(cli->outbuf,'\0',smb_size);
458 set_message(cli->outbuf,12,0,True);
459 SCVAL(cli->outbuf,smb_com,SMBsesssetupX);
461 cli_setup_packet(cli);
463 SCVAL(cli->outbuf,smb_vwv0,0xFF);
464 SSVAL(cli->outbuf,smb_vwv2,CLI_BUFFER_SIZE);
465 SSVAL(cli->outbuf,smb_vwv3,2);
466 SSVAL(cli->outbuf,smb_vwv4,1);
467 SIVAL(cli->outbuf,smb_vwv5,0);
468 SSVAL(cli->outbuf,smb_vwv7,blob.length);
469 SIVAL(cli->outbuf,smb_vwv10,capabilities);
470 p = smb_buf(cli->outbuf);
471 memcpy(p, blob.data, blob.length);
473 p += clistr_push(cli, p, "Unix", -1, STR_TERMINATE);
474 p += clistr_push(cli, p, "Samba", -1, STR_TERMINATE);
475 cli_setup_bcc(cli, p);
476 return cli_send_smb(cli);
479 /****************************************************************************
480 Send a extended security session setup blob, returning a reply blob.
481 ****************************************************************************/
483 static DATA_BLOB cli_session_setup_blob_receive(struct cli_state *cli)
485 DATA_BLOB blob2 = data_blob_null;
489 if (!cli_receive_smb(cli))
492 show_msg(cli->inbuf);
494 if (cli_is_error(cli) && !NT_STATUS_EQUAL(cli_nt_error(cli),
495 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
499 /* use the returned vuid from now on */
500 cli->vuid = SVAL(cli->inbuf,smb_uid);
502 p = smb_buf(cli->inbuf);
504 blob2 = data_blob(p, SVAL(cli->inbuf, smb_vwv3));
507 p += clistr_pull(cli, cli->server_os, p, sizeof(fstring), -1, STR_TERMINATE);
509 /* w2k with kerberos doesn't properly null terminate this field */
510 len = smb_buflen(cli->inbuf) - PTR_DIFF(p, smb_buf(cli->inbuf));
511 p += clistr_pull(cli, cli->server_type, p, sizeof(fstring), len, 0);
517 /****************************************************************************
518 Send a extended security session setup blob, returning a reply blob.
519 ****************************************************************************/
521 /* The following is calculated from :
523 * (smb_wcnt * 2) = 24 (smb_wcnt == 12 in cli_session_setup_blob_send() )
524 * (strlen("Unix") + 1 + strlen("Samba") + 1) * 2 = 22 (unicode strings at
528 #define BASE_SESSSETUP_BLOB_PACKET_SIZE (35 + 24 + 22)
530 static bool cli_session_setup_blob(struct cli_state *cli, DATA_BLOB blob, DATA_BLOB session_key_krb5)
532 int32 remaining = blob.length;
534 DATA_BLOB send_blob = data_blob_null;
535 int32 max_blob_size = 0;
536 DATA_BLOB receive_blob = data_blob_null;
538 if (cli->max_xmit < BASE_SESSSETUP_BLOB_PACKET_SIZE + 1) {
539 DEBUG(0,("cli_session_setup_blob: cli->max_xmit too small "
540 "(was %u, need minimum %u)\n",
541 (unsigned int)cli->max_xmit,
542 BASE_SESSSETUP_BLOB_PACKET_SIZE));
543 cli_set_nt_error(cli, NT_STATUS_INVALID_PARAMETER);
547 max_blob_size = cli->max_xmit - BASE_SESSSETUP_BLOB_PACKET_SIZE;
549 while ( remaining > 0) {
550 if (remaining >= max_blob_size) {
551 send_blob.length = max_blob_size;
552 remaining -= max_blob_size;
554 DATA_BLOB null_blob = data_blob_null;
556 send_blob.length = remaining;
559 /* This is the last packet in the sequence - turn signing on. */
560 cli_simple_set_signing(cli, session_key_krb5, null_blob);
563 send_blob.data = &blob.data[cur];
564 cur += send_blob.length;
566 DEBUG(10, ("cli_session_setup_blob: Remaining (%u) sending (%u) current (%u)\n",
567 (unsigned int)remaining,
568 (unsigned int)send_blob.length,
569 (unsigned int)cur ));
571 if (!cli_session_setup_blob_send(cli, send_blob)) {
572 DEBUG(0, ("cli_session_setup_blob: send failed\n"));
576 receive_blob = cli_session_setup_blob_receive(cli);
577 data_blob_free(&receive_blob);
579 if (cli_is_error(cli) &&
580 !NT_STATUS_EQUAL( cli_get_nt_error(cli),
581 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
582 DEBUG(0, ("cli_session_setup_blob: recieve failed (%s)\n",
583 nt_errstr(cli_get_nt_error(cli)) ));
592 /****************************************************************************
593 Use in-memory credentials cache
594 ****************************************************************************/
596 static void use_in_memory_ccache(void) {
597 setenv(KRB5_ENV_CCNAME, "MEMORY:cliconnect", 1);
600 /****************************************************************************
601 Do a spnego/kerberos encrypted session setup.
602 ****************************************************************************/
604 static ADS_STATUS cli_session_setup_kerberos(struct cli_state *cli, const char *principal, const char *workgroup)
606 DATA_BLOB negTokenTarg;
607 DATA_BLOB session_key_krb5;
610 DEBUG(2,("Doing kerberos session setup\n"));
612 /* generate the encapsulated kerberos5 ticket */
613 rc = spnego_gen_negTokenTarg(principal, 0, &negTokenTarg, &session_key_krb5, 0, NULL);
616 DEBUG(1, ("cli_session_setup_kerberos: spnego_gen_negTokenTarg failed: %s\n",
618 return ADS_ERROR_KRB5(rc);
622 file_save("negTokenTarg.dat", negTokenTarg.data, negTokenTarg.length);
625 if (!cli_session_setup_blob(cli, negTokenTarg, session_key_krb5)) {
626 data_blob_free(&negTokenTarg);
627 data_blob_free(&session_key_krb5);
628 ADS_ERROR_NT(cli_nt_error(cli));
631 cli_set_session_key(cli, session_key_krb5);
633 data_blob_free(&negTokenTarg);
634 data_blob_free(&session_key_krb5);
636 if (cli_is_error(cli)) {
637 if (NT_STATUS_IS_OK(cli_nt_error(cli))) {
638 return ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
641 return ADS_ERROR_NT(cli_nt_error(cli));
643 #endif /* HAVE_KRB5 */
646 /****************************************************************************
647 Do a spnego/NTLMSSP encrypted session setup.
648 ****************************************************************************/
650 static NTSTATUS cli_session_setup_ntlmssp(struct cli_state *cli, const char *user,
651 const char *pass, const char *domain)
653 struct ntlmssp_state *ntlmssp_state;
657 DATA_BLOB blob = data_blob_null;
658 DATA_BLOB blob_in = data_blob_null;
659 DATA_BLOB blob_out = data_blob_null;
661 cli_temp_set_signing(cli);
663 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_client_start(&ntlmssp_state))) {
666 ntlmssp_want_feature(ntlmssp_state, NTLMSSP_FEATURE_SESSION_KEY);
668 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_username(ntlmssp_state, user))) {
671 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_domain(ntlmssp_state, domain))) {
674 if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_password(ntlmssp_state, pass))) {
679 nt_status = ntlmssp_update(ntlmssp_state,
681 data_blob_free(&blob_in);
682 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED) || NT_STATUS_IS_OK(nt_status)) {
684 /* and wrap it in a SPNEGO wrapper */
685 msg1 = gen_negTokenInit(OID_NTLMSSP, blob_out);
687 /* wrap it in SPNEGO */
688 msg1 = spnego_gen_auth(blob_out);
691 /* now send that blob on its way */
692 if (!cli_session_setup_blob_send(cli, msg1)) {
693 DEBUG(3, ("Failed to send NTLMSSP/SPNEGO blob to server!\n"));
694 nt_status = NT_STATUS_UNSUCCESSFUL;
696 blob = cli_session_setup_blob_receive(cli);
698 nt_status = cli_nt_error(cli);
699 if (cli_is_error(cli) && NT_STATUS_IS_OK(nt_status)) {
700 if (cli->smb_rw_error == SMB_READ_BAD_SIG) {
701 nt_status = NT_STATUS_ACCESS_DENIED;
703 nt_status = NT_STATUS_UNSUCCESSFUL;
707 data_blob_free(&msg1);
711 if (NT_STATUS_IS_OK(nt_status)) {
712 nt_status = NT_STATUS_UNSUCCESSFUL;
714 } else if ((turn == 1) &&
715 NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
716 DATA_BLOB tmp_blob = data_blob_null;
717 /* the server might give us back two challenges */
718 if (!spnego_parse_challenge(blob, &blob_in,
720 DEBUG(3,("Failed to parse challenges\n"));
721 nt_status = NT_STATUS_INVALID_PARAMETER;
723 data_blob_free(&tmp_blob);
725 if (!spnego_parse_auth_response(blob, nt_status, OID_NTLMSSP,
727 DEBUG(3,("Failed to parse auth response\n"));
728 if (NT_STATUS_IS_OK(nt_status)
729 || NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED))
730 nt_status = NT_STATUS_INVALID_PARAMETER;
733 data_blob_free(&blob);
734 data_blob_free(&blob_out);
736 } while (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED));
738 data_blob_free(&blob_in);
740 if (NT_STATUS_IS_OK(nt_status)) {
742 DATA_BLOB key = data_blob(ntlmssp_state->session_key.data,
743 ntlmssp_state->session_key.length);
744 DATA_BLOB null_blob = data_blob_null;
747 fstrcpy(cli->server_domain, ntlmssp_state->server_domain);
748 cli_set_session_key(cli, ntlmssp_state->session_key);
750 res = cli_simple_set_signing(cli, key, null_blob);
752 data_blob_free(&key);
756 /* 'resign' the last message, so we get the right sequence numbers
757 for checking the first reply from the server */
758 cli_calculate_sign_mac(cli);
760 if (!cli_check_sign_mac(cli)) {
761 nt_status = NT_STATUS_ACCESS_DENIED;
766 /* we have a reference conter on ntlmssp_state, if we are signing
767 then the state will be kept by the signing engine */
769 ntlmssp_end(&ntlmssp_state);
771 if (!NT_STATUS_IS_OK(nt_status)) {
777 /****************************************************************************
778 Do a spnego encrypted session setup.
779 ****************************************************************************/
781 ADS_STATUS cli_session_setup_spnego(struct cli_state *cli, const char *user,
782 const char *pass, const char *domain)
785 char *OIDs[ASN1_MAX_OIDS];
787 bool got_kerberos_mechanism = False;
790 DEBUG(3,("Doing spnego session setup (blob length=%lu)\n", (unsigned long)cli->secblob.length));
792 /* the server might not even do spnego */
793 if (cli->secblob.length <= 16) {
794 DEBUG(3,("server didn't supply a full spnego negprot\n"));
799 file_save("negprot.dat", cli->secblob.data, cli->secblob.length);
802 /* there is 16 bytes of GUID before the real spnego packet starts */
803 blob = data_blob(cli->secblob.data+16, cli->secblob.length-16);
805 /* the server sent us the first part of the SPNEGO exchange in the negprot
807 if (!spnego_parse_negTokenInit(blob, OIDs, &principal)) {
808 data_blob_free(&blob);
809 return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
811 data_blob_free(&blob);
813 /* make sure the server understands kerberos */
814 for (i=0;OIDs[i];i++) {
815 DEBUG(3,("got OID=%s\n", OIDs[i]));
816 if (strcmp(OIDs[i], OID_KERBEROS5_OLD) == 0 ||
817 strcmp(OIDs[i], OID_KERBEROS5) == 0) {
818 got_kerberos_mechanism = True;
823 DEBUG(3,("got principal=%s\n", principal ? principal : "<null>"));
825 if (got_kerberos_mechanism && (principal == NULL)) {
827 * It is WRONG to depend on the principal sent in the negprot
828 * reply, but right now we do it. So for safety (don't
829 * segfault later) disable Kerberos when no principal was
832 DEBUG(1, ("Kerberos mech was offered, but no principal was "
833 "sent, disabling Kerberos\n"));
834 cli->use_kerberos = False;
837 fstrcpy(cli->user_name, user);
840 /* If password is set we reauthenticate to kerberos server
841 * and do not store results */
843 if (got_kerberos_mechanism && cli->use_kerberos) {
849 use_in_memory_ccache();
850 ret = kerberos_kinit_password(user, pass, 0 /* no time correction for now */, NULL);
853 SAFE_FREE(principal);
854 DEBUG(0, ("Kinit failed: %s\n", error_message(ret)));
855 if (cli->fallback_after_kerberos)
857 return ADS_ERROR_KRB5(ret);
861 rc = cli_session_setup_kerberos(cli, principal, domain);
862 if (ADS_ERR_OK(rc) || !cli->fallback_after_kerberos) {
863 SAFE_FREE(principal);
869 SAFE_FREE(principal);
873 return ADS_ERROR_NT(cli_session_setup_ntlmssp(cli, user, pass, domain));
876 /****************************************************************************
877 Send a session setup. The username and workgroup is in UNIX character
878 format and must be converted to DOS codepage format before sending. If the
879 password is in plaintext, the same should be done.
880 ****************************************************************************/
882 NTSTATUS cli_session_setup(struct cli_state *cli,
884 const char *pass, int passlen,
885 const char *ntpass, int ntpasslen,
886 const char *workgroup)
892 fstrcpy(user2, user);
901 /* allow for workgroups as part of the username */
902 if ((p=strchr_m(user2,'\\')) || (p=strchr_m(user2,'/')) ||
903 (p=strchr_m(user2,*lp_winbind_separator()))) {
909 if (cli->protocol < PROTOCOL_LANMAN1) {
913 /* now work out what sort of session setup we are going to
914 do. I have split this into separate functions to make the
915 flow a bit easier to understand (tridge) */
917 /* if its an older server then we have to use the older request format */
919 if (cli->protocol < PROTOCOL_NT1) {
920 if (!lp_client_lanman_auth() && passlen != 24 && (*pass)) {
921 DEBUG(1, ("Server requested LM password but 'client lanman auth'"
923 return NT_STATUS_ACCESS_DENIED;
926 if ((cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) == 0 &&
927 !lp_client_plaintext_auth() && (*pass)) {
928 DEBUG(1, ("Server requested plaintext password but 'client use plaintext auth'"
930 return NT_STATUS_ACCESS_DENIED;
933 return cli_session_setup_lanman2(cli, user, pass, passlen,
937 /* if no user is supplied then we have to do an anonymous connection.
938 passwords are ignored */
941 return cli_session_setup_guest(cli);
943 /* if the server is share level then send a plaintext null
944 password at this point. The password is sent in the tree
947 if ((cli->sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) == 0)
948 return cli_session_setup_plaintext(cli, user, "", workgroup);
950 /* if the server doesn't support encryption then we have to use
951 plaintext. The second password is ignored */
953 if ((cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) == 0) {
954 if (!lp_client_plaintext_auth() && (*pass)) {
955 DEBUG(1, ("Server requested plaintext password but 'client use plaintext auth'"
957 return NT_STATUS_ACCESS_DENIED;
959 return cli_session_setup_plaintext(cli, user, pass, workgroup);
962 /* if the server supports extended security then use SPNEGO */
964 if (cli->capabilities & CAP_EXTENDED_SECURITY) {
965 ADS_STATUS status = cli_session_setup_spnego(cli, user, pass, workgroup);
966 if (!ADS_ERR_OK(status)) {
967 DEBUG(3, ("SPNEGO login failed: %s\n", ads_errstr(status)));
968 return ads_ntstatus(status);
973 /* otherwise do a NT1 style session setup */
974 status = cli_session_setup_nt1(cli, user, pass, passlen,
975 ntpass, ntpasslen, workgroup);
976 if (!NT_STATUS_IS_OK(status)) {
977 DEBUG(3,("cli_session_setup: NT1 session setup "
978 "failed: %s\n", nt_errstr(status)));
983 if (strstr(cli->server_type, "Samba")) {
984 cli->is_samba = True;
991 /****************************************************************************
993 *****************************************************************************/
995 bool cli_ulogoff(struct cli_state *cli)
997 memset(cli->outbuf,'\0',smb_size);
998 set_message(cli->outbuf,2,0,True);
999 SCVAL(cli->outbuf,smb_com,SMBulogoffX);
1000 cli_setup_packet(cli);
1001 SSVAL(cli->outbuf,smb_vwv0,0xFF);
1002 SSVAL(cli->outbuf,smb_vwv2,0); /* no additional info */
1005 if (!cli_receive_smb(cli))
1008 if (cli_is_error(cli)) {
1016 /****************************************************************************
1018 ****************************************************************************/
1020 bool cli_send_tconX(struct cli_state *cli,
1021 const char *share, const char *dev, const char *pass, int passlen)
1023 fstring fullshare, pword;
1025 memset(cli->outbuf,'\0',smb_size);
1026 memset(cli->inbuf,'\0',smb_size);
1028 fstrcpy(cli->share, share);
1030 /* in user level security don't send a password now */
1031 if (cli->sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) {
1035 DEBUG(1, ("Server not using user level security and no password supplied.\n"));
1039 if ((cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) &&
1040 *pass && passlen != 24) {
1041 if (!lp_client_lanman_auth()) {
1042 DEBUG(1, ("Server requested LANMAN password (share-level security) but 'client use lanman auth'"
1048 * Non-encrypted passwords - convert to DOS codepage before encryption.
1051 SMBencrypt(pass,cli->secblob.data,(uchar *)pword);
1053 if((cli->sec_mode & (NEGOTIATE_SECURITY_USER_LEVEL|NEGOTIATE_SECURITY_CHALLENGE_RESPONSE)) == 0) {
1054 if (!lp_client_plaintext_auth() && (*pass)) {
1055 DEBUG(1, ("Server requested plaintext password but 'client use plaintext auth'"
1061 * Non-encrypted passwords - convert to DOS codepage before using.
1063 passlen = clistr_push(cli, pword, pass, sizeof(pword), STR_TERMINATE);
1067 memcpy(pword, pass, passlen);
1072 slprintf(fullshare, sizeof(fullshare)-1,
1073 "\\\\%s\\%s", cli->desthost, share);
1075 set_message(cli->outbuf,4, 0, True);
1076 SCVAL(cli->outbuf,smb_com,SMBtconX);
1077 cli_setup_packet(cli);
1079 SSVAL(cli->outbuf,smb_vwv0,0xFF);
1080 SSVAL(cli->outbuf,smb_vwv2,TCONX_FLAG_EXTENDED_RESPONSE);
1081 SSVAL(cli->outbuf,smb_vwv3,passlen);
1083 p = smb_buf(cli->outbuf);
1085 memcpy(p,pword,passlen);
1088 p += clistr_push(cli, p, fullshare, -1, STR_TERMINATE |STR_UPPER);
1089 p += clistr_push(cli, p, dev, -1, STR_TERMINATE |STR_UPPER | STR_ASCII);
1091 cli_setup_bcc(cli, p);
1094 if (!cli_receive_smb(cli))
1097 if (cli_is_error(cli))
1100 clistr_pull(cli, cli->dev, smb_buf(cli->inbuf), sizeof(fstring), -1, STR_TERMINATE|STR_ASCII);
1102 if (cli->protocol >= PROTOCOL_NT1 &&
1103 smb_buflen(cli->inbuf) == 3) {
1104 /* almost certainly win95 - enable bug fixes */
1108 /* Make sure that we have the optional support 16-bit field. WCT > 2 */
1109 /* Avoids issues when connecting to Win9x boxes sharing files */
1111 cli->dfsroot = False;
1112 if ( (CVAL(cli->inbuf, smb_wct))>2 && cli->protocol >= PROTOCOL_LANMAN2 )
1113 cli->dfsroot = (SVAL( cli->inbuf, smb_vwv2 ) & SMB_SHARE_IN_DFS) ? True : False;
1115 cli->cnum = SVAL(cli->inbuf,smb_tid);
1119 /****************************************************************************
1120 Send a tree disconnect.
1121 ****************************************************************************/
1123 bool cli_tdis(struct cli_state *cli)
1125 memset(cli->outbuf,'\0',smb_size);
1126 set_message(cli->outbuf,0,0,True);
1127 SCVAL(cli->outbuf,smb_com,SMBtdis);
1128 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1129 cli_setup_packet(cli);
1132 if (!cli_receive_smb(cli))
1135 if (cli_is_error(cli)) {
1143 /****************************************************************************
1144 Send a negprot command.
1145 ****************************************************************************/
1147 void cli_negprot_send(struct cli_state *cli)
1152 if (cli->protocol < PROTOCOL_NT1)
1153 cli->use_spnego = False;
1155 memset(cli->outbuf,'\0',smb_size);
1157 /* setup the protocol strings */
1158 set_message(cli->outbuf,0,0,True);
1160 p = smb_buf(cli->outbuf);
1162 prots[numprots].name && prots[numprots].prot<=cli->protocol;
1165 p += clistr_push(cli, p, prots[numprots].name, -1, STR_TERMINATE);
1168 SCVAL(cli->outbuf,smb_com,SMBnegprot);
1169 cli_setup_bcc(cli, p);
1170 cli_setup_packet(cli);
1172 SCVAL(smb_buf(cli->outbuf),0,2);
1177 /****************************************************************************
1178 Send a negprot command.
1179 ****************************************************************************/
1181 bool cli_negprot(struct cli_state *cli)
1187 if (cli->protocol < PROTOCOL_NT1)
1188 cli->use_spnego = False;
1190 memset(cli->outbuf,'\0',smb_size);
1192 /* setup the protocol strings */
1193 for (plength=0,numprots=0;
1194 prots[numprots].name && prots[numprots].prot<=cli->protocol;
1196 plength += strlen(prots[numprots].name)+2;
1198 set_message(cli->outbuf,0,plength,True);
1200 p = smb_buf(cli->outbuf);
1202 prots[numprots].name && prots[numprots].prot<=cli->protocol;
1205 p += clistr_push(cli, p, prots[numprots].name, -1, STR_TERMINATE);
1208 SCVAL(cli->outbuf,smb_com,SMBnegprot);
1209 cli_setup_packet(cli);
1211 SCVAL(smb_buf(cli->outbuf),0,2);
1214 if (!cli_receive_smb(cli))
1217 show_msg(cli->inbuf);
1219 if (cli_is_error(cli) ||
1220 ((int)SVAL(cli->inbuf,smb_vwv0) >= numprots)) {
1224 cli->protocol = prots[SVAL(cli->inbuf,smb_vwv0)].prot;
1226 if ((cli->protocol < PROTOCOL_NT1) && cli->sign_info.mandatory_signing) {
1227 DEBUG(0,("cli_negprot: SMB signing is mandatory and the selected protocol level doesn't support it.\n"));
1231 if (cli->protocol >= PROTOCOL_NT1) {
1234 cli->sec_mode = CVAL(cli->inbuf,smb_vwv1);
1235 cli->max_mux = SVAL(cli->inbuf, smb_vwv1+1);
1236 cli->max_xmit = IVAL(cli->inbuf,smb_vwv3+1);
1237 cli->sesskey = IVAL(cli->inbuf,smb_vwv7+1);
1238 cli->serverzone = SVALS(cli->inbuf,smb_vwv15+1);
1239 cli->serverzone *= 60;
1240 /* this time arrives in real GMT */
1241 ts = interpret_long_date(cli->inbuf+smb_vwv11+1);
1242 cli->servertime = ts.tv_sec;
1243 cli->secblob = data_blob(smb_buf(cli->inbuf),smb_buflen(cli->inbuf));
1244 cli->capabilities = IVAL(cli->inbuf,smb_vwv9+1);
1245 if (cli->capabilities & CAP_RAW_MODE) {
1246 cli->readbraw_supported = True;
1247 cli->writebraw_supported = True;
1249 /* work out if they sent us a workgroup */
1250 if (!(cli->capabilities & CAP_EXTENDED_SECURITY) &&
1251 smb_buflen(cli->inbuf) > 8) {
1252 clistr_pull(cli, cli->server_domain,
1253 smb_buf(cli->inbuf)+8, sizeof(cli->server_domain),
1254 smb_buflen(cli->inbuf)-8, STR_UNICODE|STR_NOALIGN);
1258 * As signing is slow we only turn it on if either the client or
1259 * the server require it. JRA.
1262 if (cli->sec_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
1263 /* Fail if server says signing is mandatory and we don't want to support it. */
1264 if (!cli->sign_info.allow_smb_signing) {
1265 DEBUG(0,("cli_negprot: SMB signing is mandatory and we have disabled it.\n"));
1268 cli->sign_info.negotiated_smb_signing = True;
1269 cli->sign_info.mandatory_signing = True;
1270 } else if (cli->sign_info.mandatory_signing && cli->sign_info.allow_smb_signing) {
1271 /* Fail if client says signing is mandatory and the server doesn't support it. */
1272 if (!(cli->sec_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED)) {
1273 DEBUG(1,("cli_negprot: SMB signing is mandatory and the server doesn't support it.\n"));
1276 cli->sign_info.negotiated_smb_signing = True;
1277 cli->sign_info.mandatory_signing = True;
1278 } else if (cli->sec_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
1279 cli->sign_info.negotiated_smb_signing = True;
1282 if (cli->capabilities & (CAP_LARGE_READX|CAP_LARGE_WRITEX)) {
1283 SAFE_FREE(cli->outbuf);
1284 SAFE_FREE(cli->inbuf);
1285 cli->outbuf = (char *)SMB_MALLOC(CLI_SAMBA_MAX_LARGE_READX_SIZE+SAFETY_MARGIN);
1286 cli->inbuf = (char *)SMB_MALLOC(CLI_SAMBA_MAX_LARGE_READX_SIZE+SAFETY_MARGIN);
1287 cli->bufsize = CLI_SAMBA_MAX_LARGE_READX_SIZE;
1290 } else if (cli->protocol >= PROTOCOL_LANMAN1) {
1291 cli->use_spnego = False;
1292 cli->sec_mode = SVAL(cli->inbuf,smb_vwv1);
1293 cli->max_xmit = SVAL(cli->inbuf,smb_vwv2);
1294 cli->max_mux = SVAL(cli->inbuf, smb_vwv3);
1295 cli->sesskey = IVAL(cli->inbuf,smb_vwv6);
1296 cli->serverzone = SVALS(cli->inbuf,smb_vwv10);
1297 cli->serverzone *= 60;
1298 /* this time is converted to GMT by make_unix_date */
1299 cli->servertime = cli_make_unix_date(cli,cli->inbuf+smb_vwv8);
1300 cli->readbraw_supported = ((SVAL(cli->inbuf,smb_vwv5) & 0x1) != 0);
1301 cli->writebraw_supported = ((SVAL(cli->inbuf,smb_vwv5) & 0x2) != 0);
1302 cli->secblob = data_blob(smb_buf(cli->inbuf),smb_buflen(cli->inbuf));
1304 /* the old core protocol */
1305 cli->use_spnego = False;
1307 cli->serverzone = get_time_zone(time(NULL));
1310 cli->max_xmit = MIN(cli->max_xmit, CLI_BUFFER_SIZE);
1312 /* a way to force ascii SMB */
1313 if (getenv("CLI_FORCE_ASCII"))
1314 cli->capabilities &= ~CAP_UNICODE;
1319 /****************************************************************************
1320 Send a session request. See rfc1002.txt 4.3 and 4.3.2.
1321 ****************************************************************************/
1323 bool cli_session_request(struct cli_state *cli,
1324 struct nmb_name *calling, struct nmb_name *called)
1329 memcpy(&(cli->calling), calling, sizeof(*calling));
1330 memcpy(&(cli->called ), called , sizeof(*called ));
1332 /* put in the destination name */
1333 p = cli->outbuf+len;
1334 name_mangle(cli->called .name, p, cli->called .name_type);
1338 p = cli->outbuf+len;
1339 name_mangle(cli->calling.name, p, cli->calling.name_type);
1342 /* 445 doesn't have session request */
1343 if (cli->port == 445)
1346 /* send a session request (RFC 1002) */
1347 /* setup the packet length
1348 * Remove four bytes from the length count, since the length
1349 * field in the NBT Session Service header counts the number
1350 * of bytes which follow. The cli_send_smb() function knows
1351 * about this and accounts for those four bytes.
1355 _smb_setlen(cli->outbuf,len);
1356 SCVAL(cli->outbuf,0,0x81);
1359 DEBUG(5,("Sent session request\n"));
1361 if (!cli_receive_smb(cli))
1364 if (CVAL(cli->inbuf,0) == 0x84) {
1365 /* C. Hoch 9/14/95 Start */
1366 /* For information, here is the response structure.
1367 * We do the byte-twiddling to for portability.
1368 struct RetargetResponse{
1370 unsigned char flags;
1376 uint16_t port = (CVAL(cli->inbuf,8)<<8)+CVAL(cli->inbuf,9);
1377 struct in_addr dest_ip;
1379 /* SESSION RETARGET */
1380 putip((char *)&dest_ip,cli->inbuf+4);
1381 in_addr_to_sockaddr_storage(&cli->dest_ss, dest_ip);
1383 cli->fd = open_socket_out(SOCK_STREAM,
1386 LONG_CONNECT_TIMEOUT);
1390 DEBUG(3,("Retargeted\n"));
1392 set_socket_options(cli->fd, lp_socket_options());
1399 DEBUG(0,("Retarget recursion - failing\n"));
1403 ret = cli_session_request(cli, calling, called);
1407 } /* C. Hoch 9/14/95 End */
1409 if (CVAL(cli->inbuf,0) != 0x82) {
1410 /* This is the wrong place to put the error... JRA. */
1411 cli->rap_error = CVAL(cli->inbuf,4);
1417 /****************************************************************************
1418 Open the client sockets.
1419 ****************************************************************************/
1421 NTSTATUS cli_connect(struct cli_state *cli,
1423 struct sockaddr_storage *dest_ss)
1426 int name_type = 0x20;
1429 /* reasonable default hostname */
1431 host = "*SMBSERVER";
1434 fstrcpy(cli->desthost, host);
1436 /* allow hostnames of the form NAME#xx and do a netbios lookup */
1437 if ((p = strchr(cli->desthost, '#'))) {
1438 name_type = strtol(p+1, NULL, 16);
1442 if (!dest_ss || is_zero_addr(dest_ss)) {
1443 if (!resolve_name(cli->desthost, &cli->dest_ss, name_type)) {
1444 return NT_STATUS_BAD_NETWORK_NAME;
1447 *dest_ss = cli->dest_ss;
1450 cli->dest_ss = *dest_ss;
1453 if (getenv("LIBSMB_PROG")) {
1454 cli->fd = sock_exec(getenv("LIBSMB_PROG"));
1456 /* try 445 first, then 139 */
1457 uint16_t port = cli->port?cli->port:445;
1458 cli->fd = open_socket_out(SOCK_STREAM, &cli->dest_ss,
1459 port, cli->timeout);
1460 if (cli->fd == -1 && cli->port == 0) {
1462 cli->fd = open_socket_out(SOCK_STREAM, &cli->dest_ss,
1463 port, cli->timeout);
1465 if (cli->fd != -1) {
1469 if (cli->fd == -1) {
1470 char addr[INET6_ADDRSTRLEN];
1472 print_sockaddr(addr, sizeof(addr), dest_ss);
1474 DEBUG(1,("Error connecting to %s (%s)\n",
1475 dest_ss?addr:host,strerror(errno)));
1476 return map_nt_error_from_unix(errno);
1479 set_socket_options(cli->fd, lp_socket_options());
1481 return NT_STATUS_OK;
1485 establishes a connection to after the negprot.
1486 @param output_cli A fully initialised cli structure, non-null only on success
1487 @param dest_host The netbios name of the remote host
1488 @param dest_ss (optional) The the destination IP, NULL for name based lookup
1489 @param port (optional) The destination port (0 for default)
1490 @param retry bool. Did this connection fail with a retryable error ?
1493 NTSTATUS cli_start_connection(struct cli_state **output_cli,
1494 const char *my_name,
1495 const char *dest_host,
1496 struct sockaddr_storage *dest_ss, int port,
1497 int signing_state, int flags,
1501 struct nmb_name calling;
1502 struct nmb_name called;
1503 struct cli_state *cli;
1504 struct sockaddr_storage ss;
1510 my_name = global_myname();
1512 if (!(cli = cli_initialise())) {
1513 return NT_STATUS_NO_MEMORY;
1516 make_nmb_name(&calling, my_name, 0x0);
1517 make_nmb_name(&called , dest_host, 0x20);
1519 if (cli_set_port(cli, port) != port) {
1521 return NT_STATUS_UNSUCCESSFUL;
1524 cli_set_timeout(cli, 10000); /* 10 seconds. */
1534 DEBUG(3,("Connecting to host=%s\n", dest_host));
1536 nt_status = cli_connect(cli, dest_host, &ss);
1537 if (!NT_STATUS_IS_OK(nt_status)) {
1538 char addr[INET6_ADDRSTRLEN];
1539 print_sockaddr(addr, sizeof(addr), &ss);
1540 DEBUG(1,("cli_start_connection: failed to connect to %s (%s). Error %s\n",
1541 nmb_namestr(&called), addr, nt_errstr(nt_status) ));
1549 if (!cli_session_request(cli, &calling, &called)) {
1551 DEBUG(1,("session request to %s failed (%s)\n",
1552 called.name, cli_errstr(cli)));
1553 if ((p=strchr(called.name, '.')) && !is_ipaddress(called.name)) {
1557 if (strcmp(called.name, "*SMBSERVER")) {
1558 make_nmb_name(&called , "*SMBSERVER", 0x20);
1561 return NT_STATUS_BAD_NETWORK_NAME;
1564 cli_setup_signing_state(cli, signing_state);
1566 if (flags & CLI_FULL_CONNECTION_DONT_SPNEGO)
1567 cli->use_spnego = False;
1568 else if (flags & CLI_FULL_CONNECTION_USE_KERBEROS)
1569 cli->use_kerberos = True;
1571 if (!cli_negprot(cli)) {
1572 DEBUG(1,("failed negprot\n"));
1573 nt_status = cli_nt_error(cli);
1574 if (NT_STATUS_IS_OK(nt_status)) {
1575 nt_status = NT_STATUS_UNSUCCESSFUL;
1582 return NT_STATUS_OK;
1587 establishes a connection right up to doing tconX, password specified.
1588 @param output_cli A fully initialised cli structure, non-null only on success
1589 @param dest_host The netbios name of the remote host
1590 @param dest_ip (optional) The the destination IP, NULL for name based lookup
1591 @param port (optional) The destination port (0 for default)
1592 @param service (optional) The share to make the connection to. Should be 'unqualified' in any way.
1593 @param service_type The 'type' of serivice.
1594 @param user Username, unix string
1595 @param domain User's domain
1596 @param password User's password, unencrypted unix string.
1597 @param retry bool. Did this connection fail with a retryable error ?
1600 NTSTATUS cli_full_connection(struct cli_state **output_cli,
1601 const char *my_name,
1602 const char *dest_host,
1603 struct sockaddr_storage *dest_ss, int port,
1604 const char *service, const char *service_type,
1605 const char *user, const char *domain,
1606 const char *password, int flags,
1611 struct cli_state *cli = NULL;
1612 int pw_len = password ? strlen(password)+1 : 0;
1616 if (password == NULL) {
1620 nt_status = cli_start_connection(&cli, my_name, dest_host,
1621 dest_ss, port, signing_state,
1624 if (!NT_STATUS_IS_OK(nt_status)) {
1628 nt_status = cli_session_setup(cli, user, password, pw_len, password,
1630 if (!NT_STATUS_IS_OK(nt_status)) {
1632 if (!(flags & CLI_FULL_CONNECTION_ANONYMOUS_FALLBACK)) {
1633 DEBUG(1,("failed session setup with %s\n",
1634 nt_errstr(nt_status)));
1639 nt_status = cli_session_setup(cli, "", "", 0, "", 0, domain);
1640 if (!NT_STATUS_IS_OK(nt_status)) {
1641 DEBUG(1,("anonymous failed session setup with %s\n",
1642 nt_errstr(nt_status)));
1649 if (!cli_send_tconX(cli, service, service_type, password, pw_len)) {
1650 nt_status = cli_nt_error(cli);
1651 DEBUG(1,("failed tcon_X with %s\n", nt_errstr(nt_status)));
1653 if (NT_STATUS_IS_OK(nt_status)) {
1654 nt_status = NT_STATUS_UNSUCCESSFUL;
1660 cli_init_creds(cli, user, domain, password);
1663 return NT_STATUS_OK;
1666 /****************************************************************************
1667 Attempt a NetBIOS session request, falling back to *SMBSERVER if needed.
1668 ****************************************************************************/
1670 bool attempt_netbios_session_request(struct cli_state **ppcli, const char *srchost, const char *desthost,
1671 struct sockaddr_storage *pdest_ss)
1673 struct nmb_name calling, called;
1675 make_nmb_name(&calling, srchost, 0x0);
1678 * If the called name is an IP address
1679 * then use *SMBSERVER immediately.
1682 if(is_ipaddress(desthost)) {
1683 make_nmb_name(&called, "*SMBSERVER", 0x20);
1685 make_nmb_name(&called, desthost, 0x20);
1688 if (!cli_session_request(*ppcli, &calling, &called)) {
1690 struct nmb_name smbservername;
1692 make_nmb_name(&smbservername , "*SMBSERVER", 0x20);
1695 * If the name wasn't *SMBSERVER then
1696 * try with *SMBSERVER if the first name fails.
1699 if (nmb_name_equal(&called, &smbservername)) {
1702 * The name used was *SMBSERVER, don't bother with another name.
1705 DEBUG(0,("attempt_netbios_session_request: %s rejected the session for name *SMBSERVER \
1706 with error %s.\n", desthost, cli_errstr(*ppcli) ));
1711 cli_shutdown(*ppcli);
1713 *ppcli = cli_initialise();
1715 /* Out of memory... */
1719 status = cli_connect(*ppcli, desthost, pdest_ss);
1720 if (!NT_STATUS_IS_OK(status) ||
1721 !cli_session_request(*ppcli, &calling, &smbservername)) {
1722 DEBUG(0,("attempt_netbios_session_request: %s rejected the session for \
1723 name *SMBSERVER with error %s\n", desthost, cli_errstr(*ppcli) ));
1731 /****************************************************************************
1732 Send an old style tcon.
1733 ****************************************************************************/
1734 NTSTATUS cli_raw_tcon(struct cli_state *cli,
1735 const char *service, const char *pass, const char *dev,
1736 uint16 *max_xmit, uint16 *tid)
1740 if (!lp_client_plaintext_auth() && (*pass)) {
1741 DEBUG(1, ("Server requested plaintext password but 'client use plaintext auth'"
1743 return NT_STATUS_ACCESS_DENIED;
1746 memset(cli->outbuf,'\0',smb_size);
1747 memset(cli->inbuf,'\0',smb_size);
1749 set_message(cli->outbuf, 0, 0, True);
1750 SCVAL(cli->outbuf,smb_com,SMBtcon);
1751 cli_setup_packet(cli);
1753 p = smb_buf(cli->outbuf);
1754 *p++ = 4; p += clistr_push(cli, p, service, -1, STR_TERMINATE | STR_NOALIGN);
1755 *p++ = 4; p += clistr_push(cli, p, pass, -1, STR_TERMINATE | STR_NOALIGN);
1756 *p++ = 4; p += clistr_push(cli, p, dev, -1, STR_TERMINATE | STR_NOALIGN);
1758 cli_setup_bcc(cli, p);
1761 if (!cli_receive_smb(cli)) {
1762 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
1765 if (cli_is_error(cli)) {
1766 return cli_nt_error(cli);
1769 *max_xmit = SVAL(cli->inbuf, smb_vwv0);
1770 *tid = SVAL(cli->inbuf, smb_vwv1);
1772 return NT_STATUS_OK;
1775 /* Return a cli_state pointing at the IPC$ share for the given server */
1777 struct cli_state *get_ipc_connect(char *server,
1778 struct sockaddr_storage *server_ss,
1779 struct user_auth_info *user_info)
1781 struct cli_state *cli;
1784 nt_status = cli_full_connection(&cli, NULL, server, server_ss, 0, "IPC$", "IPC",
1785 user_info->username, lp_workgroup(), user_info->password,
1786 CLI_FULL_CONNECTION_ANONYMOUS_FALLBACK, Undefined, NULL);
1788 if (NT_STATUS_IS_OK(nt_status)) {
1790 } else if (is_ipaddress(server)) {
1791 /* windows 9* needs a correct NMB name for connections */
1792 fstring remote_name;
1794 if (name_status_find("*", 0, 0, server_ss, remote_name)) {
1795 cli = get_ipc_connect(remote_name, server_ss, user_info);
1804 * Given the IP address of a master browser on the network, return its
1805 * workgroup and connect to it.
1807 * This function is provided to allow additional processing beyond what
1808 * get_ipc_connect_master_ip_bcast() does, e.g. to retrieve the list of master
1809 * browsers and obtain each master browsers' list of domains (in case the
1810 * first master browser is recently on the network and has not yet
1811 * synchronized with other master browsers and therefore does not yet have the
1812 * entire network browse list)
1815 struct cli_state *get_ipc_connect_master_ip(TALLOC_CTX *ctx,
1816 struct ip_service *mb_ip,
1817 struct user_auth_info *user_info,
1818 char **pp_workgroup_out)
1820 char addr[INET6_ADDRSTRLEN];
1822 struct cli_state *cli;
1823 struct sockaddr_storage server_ss;
1825 *pp_workgroup_out = NULL;
1827 print_sockaddr(addr, sizeof(addr), &mb_ip->ss);
1828 DEBUG(99, ("Looking up name of master browser %s\n",
1832 * Do a name status query to find out the name of the master browser.
1833 * We use <01><02>__MSBROWSE__<02>#01 if *#00 fails because a domain
1834 * master browser will not respond to a wildcard query (or, at least,
1835 * an NT4 server acting as the domain master browser will not).
1837 * We might be able to use ONLY the query on MSBROWSE, but that's not
1838 * yet been tested with all Windows versions, so until it is, leave
1839 * the original wildcard query as the first choice and fall back to
1840 * MSBROWSE if the wildcard query fails.
1842 if (!name_status_find("*", 0, 0x1d, &mb_ip->ss, name) &&
1843 !name_status_find(MSBROWSE, 1, 0x1d, &mb_ip->ss, name)) {
1845 DEBUG(99, ("Could not retrieve name status for %s\n",
1850 if (!find_master_ip(name, &server_ss)) {
1851 DEBUG(99, ("Could not find master ip for %s\n", name));
1855 *pp_workgroup_out = talloc_strdup(ctx, name);
1857 DEBUG(4, ("found master browser %s, %s\n", name, addr));
1859 print_sockaddr(addr, sizeof(addr), &server_ss);
1860 cli = get_ipc_connect(addr, &server_ss, user_info);
1866 * Return the IP address and workgroup of a master browser on the network, and
1870 struct cli_state *get_ipc_connect_master_ip_bcast(TALLOC_CTX *ctx,
1871 struct user_auth_info *user_info,
1872 char **pp_workgroup_out)
1874 struct ip_service *ip_list;
1875 struct cli_state *cli;
1878 *pp_workgroup_out = NULL;
1880 DEBUG(99, ("Do broadcast lookup for workgroups on local network\n"));
1882 /* Go looking for workgroups by broadcasting on the local network */
1884 if (!NT_STATUS_IS_OK(name_resolve_bcast(MSBROWSE, 1, &ip_list,
1886 DEBUG(99, ("No master browsers responded\n"));
1890 for (i = 0; i < count; i++) {
1891 char addr[INET6_ADDRSTRLEN];
1892 print_sockaddr(addr, sizeof(addr), &ip_list[i].ss);
1893 DEBUG(99, ("Found master browser %s\n", addr));
1895 cli = get_ipc_connect_master_ip(ctx, &ip_list[i],
1896 user_info, pp_workgroup_out);