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