2 Unix SMB/CIFS implementation.
3 Connect to 445 and 139/nbsesssetup
4 Copyright (C) Volker Lendecke 2010
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "../lib/async_req/async_sock.h"
22 #include "../lib/util/tevent_ntstatus.h"
23 #include "../lib/util/tevent_unix.h"
25 #include "async_smb.h"
26 #include "../libcli/smb/read_smb.h"
27 #include "libsmb/nmblib.h"
29 struct cli_session_request_state
{
30 struct tevent_context
*ev
;
34 uint8_t nb_session_response
;
37 static void cli_session_request_sent(struct tevent_req
*subreq
);
38 static void cli_session_request_recvd(struct tevent_req
*subreq
);
40 static struct tevent_req
*cli_session_request_send(TALLOC_CTX
*mem_ctx
,
41 struct tevent_context
*ev
,
43 const struct nmb_name
*called
,
44 const struct nmb_name
*calling
)
46 struct tevent_req
*req
, *subreq
;
47 struct cli_session_request_state
*state
;
49 req
= tevent_req_create(mem_ctx
, &state
,
50 struct cli_session_request_state
);
57 state
->iov
[1].iov_base
= name_mangle(
58 state
, called
->name
, called
->name_type
);
59 if (tevent_req_nomem(state
->iov
[1].iov_base
, req
)) {
60 return tevent_req_post(req
, ev
);
62 state
->iov
[1].iov_len
= name_len(
63 (unsigned char *)state
->iov
[1].iov_base
,
64 talloc_get_size(state
->iov
[1].iov_base
));
66 state
->iov
[2].iov_base
= name_mangle(
67 state
, calling
->name
, calling
->name_type
);
68 if (tevent_req_nomem(state
->iov
[2].iov_base
, req
)) {
69 return tevent_req_post(req
, ev
);
71 state
->iov
[2].iov_len
= name_len(
72 (unsigned char *)state
->iov
[2].iov_base
,
73 talloc_get_size(state
->iov
[2].iov_base
));
75 _smb_setlen(((char *)&state
->len_hdr
),
76 state
->iov
[1].iov_len
+ state
->iov
[2].iov_len
);
77 SCVAL((char *)&state
->len_hdr
, 0, 0x81);
79 state
->iov
[0].iov_base
= &state
->len_hdr
;
80 state
->iov
[0].iov_len
= sizeof(state
->len_hdr
);
82 subreq
= writev_send(state
, ev
, NULL
, sock
, true, state
->iov
, 3);
83 if (tevent_req_nomem(subreq
, req
)) {
84 return tevent_req_post(req
, ev
);
86 tevent_req_set_callback(subreq
, cli_session_request_sent
, req
);
90 static void cli_session_request_sent(struct tevent_req
*subreq
)
92 struct tevent_req
*req
= tevent_req_callback_data(
93 subreq
, struct tevent_req
);
94 struct cli_session_request_state
*state
= tevent_req_data(
95 req
, struct cli_session_request_state
);
99 ret
= writev_recv(subreq
, &err
);
102 tevent_req_error(req
, err
);
105 subreq
= read_smb_send(state
, state
->ev
, state
->sock
);
106 if (tevent_req_nomem(subreq
, req
)) {
109 tevent_req_set_callback(subreq
, cli_session_request_recvd
, req
);
112 static void cli_session_request_recvd(struct tevent_req
*subreq
)
114 struct tevent_req
*req
= tevent_req_callback_data(
115 subreq
, struct tevent_req
);
116 struct cli_session_request_state
*state
= tevent_req_data(
117 req
, struct cli_session_request_state
);
122 ret
= read_smb_recv(subreq
, talloc_tos(), &buf
, &err
);
130 tevent_req_error(req
, err
);
134 * In case of an error there is more information in the data
135 * portion according to RFC1002. We're not subtle enough to
136 * respond to the different error conditions, so drop the
139 state
->nb_session_response
= CVAL(buf
, 0);
140 tevent_req_done(req
);
143 static bool cli_session_request_recv(struct tevent_req
*req
, int *err
, uint8_t *resp
)
145 struct cli_session_request_state
*state
= tevent_req_data(
146 req
, struct cli_session_request_state
);
148 if (tevent_req_is_unix_error(req
, err
)) {
151 *resp
= state
->nb_session_response
;
155 struct nb_connect_state
{
156 struct tevent_context
*ev
;
157 const struct sockaddr_storage
*addr
;
158 const char *called_name
;
161 struct nmb_name called
;
162 struct nmb_name calling
;
165 static int nb_connect_state_destructor(struct nb_connect_state
*state
);
166 static void nb_connect_connected(struct tevent_req
*subreq
);
167 static void nb_connect_done(struct tevent_req
*subreq
);
169 static struct tevent_req
*nb_connect_send(TALLOC_CTX
*mem_ctx
,
170 struct tevent_context
*ev
,
171 const struct sockaddr_storage
*addr
,
172 const char *called_name
,
174 const char *calling_name
,
177 struct tevent_req
*req
, *subreq
;
178 struct nb_connect_state
*state
;
180 req
= tevent_req_create(mem_ctx
, &state
, struct nb_connect_state
);
185 state
->called_name
= called_name
;
189 make_nmb_name(&state
->called
, called_name
, called_type
);
190 make_nmb_name(&state
->calling
, calling_name
, calling_type
);
192 talloc_set_destructor(state
, nb_connect_state_destructor
);
194 subreq
= open_socket_out_send(state
, ev
, addr
, 139, 5000);
195 if (tevent_req_nomem(subreq
, req
)) {
196 return tevent_req_post(req
, ev
);
198 tevent_req_set_callback(subreq
, nb_connect_connected
, req
);
202 static int nb_connect_state_destructor(struct nb_connect_state
*state
)
204 if (state
->sock
!= -1) {
210 static void nb_connect_connected(struct tevent_req
*subreq
)
212 struct tevent_req
*req
= tevent_req_callback_data(
213 subreq
, struct tevent_req
);
214 struct nb_connect_state
*state
= tevent_req_data(
215 req
, struct nb_connect_state
);
218 status
= open_socket_out_recv(subreq
, &state
->sock
);
220 if (!NT_STATUS_IS_OK(status
)) {
221 tevent_req_nterror(req
, status
);
224 subreq
= cli_session_request_send(state
, state
->ev
, state
->sock
,
225 &state
->called
, &state
->calling
);
226 if (tevent_req_nomem(subreq
, req
)) {
229 tevent_req_set_callback(subreq
, nb_connect_done
, req
);
232 static void nb_connect_done(struct tevent_req
*subreq
)
234 struct tevent_req
*req
= tevent_req_callback_data(
235 subreq
, struct tevent_req
);
236 struct nb_connect_state
*state
= tevent_req_data(
237 req
, struct nb_connect_state
);
242 ret
= cli_session_request_recv(subreq
, &err
, &resp
);
245 tevent_req_nterror(req
, map_nt_error_from_unix(err
));
250 * RFC1002: 0x82 - POSITIVE SESSION RESPONSE
255 * The server did not like our session request
260 if (strequal(state
->called_name
, "*SMBSERVER")) {
262 * Here we could try a name status request and
263 * use the first 0x20 type name.
266 req
, NT_STATUS_RESOURCE_NAME_NOT_FOUND
);
271 * We could be subtle and distinguish between
272 * different failure modes, but what we do here
273 * instead is just retry with *SMBSERVER type 0x20.
275 state
->called_name
= "*SMBSERVER";
276 make_nmb_name(&state
->called
, state
->called_name
, 0x20);
278 subreq
= open_socket_out_send(state
, state
->ev
, state
->addr
,
280 if (tevent_req_nomem(subreq
, req
)) {
283 tevent_req_set_callback(subreq
, nb_connect_connected
, req
);
287 tevent_req_done(req
);
292 static NTSTATUS
nb_connect_recv(struct tevent_req
*req
, int *sock
)
294 struct nb_connect_state
*state
= tevent_req_data(
295 req
, struct nb_connect_state
);
298 if (tevent_req_is_nterror(req
, &status
)) {
306 struct smbsock_connect_state
{
307 struct tevent_context
*ev
;
308 const struct sockaddr_storage
*addr
;
309 const char *called_name
;
311 const char *calling_name
;
312 uint8_t calling_type
;
313 struct tevent_req
*req_139
;
314 struct tevent_req
*req_445
;
319 static int smbsock_connect_state_destructor(
320 struct smbsock_connect_state
*state
);
321 static void smbsock_connect_connected(struct tevent_req
*subreq
);
322 static void smbsock_connect_do_139(struct tevent_req
*subreq
);
324 struct tevent_req
*smbsock_connect_send(TALLOC_CTX
*mem_ctx
,
325 struct tevent_context
*ev
,
326 const struct sockaddr_storage
*addr
,
328 const char *called_name
,
330 const char *calling_name
,
333 struct tevent_req
*req
, *subreq
;
334 struct smbsock_connect_state
*state
;
336 req
= tevent_req_create(mem_ctx
, &state
, struct smbsock_connect_state
);
344 (called_name
!= NULL
) ? called_name
: "*SMBSERVER";
346 (called_type
!= -1) ? called_type
: 0x20;
347 state
->calling_name
=
348 (calling_name
!= NULL
) ? calling_name
: lp_netbios_name();
349 state
->calling_type
=
350 (calling_type
!= -1) ? calling_type
: 0x00;
352 talloc_set_destructor(state
, smbsock_connect_state_destructor
);
355 subreq
= tevent_wakeup_send(state
, ev
, timeval_set(0, 0));
356 if (tevent_req_nomem(subreq
, req
)) {
357 return tevent_req_post(req
, ev
);
359 tevent_req_set_callback(subreq
, smbsock_connect_do_139
, req
);
363 state
->req_445
= open_socket_out_send(state
, ev
, addr
, port
,
365 if (tevent_req_nomem(state
->req_445
, req
)) {
366 return tevent_req_post(req
, ev
);
368 tevent_req_set_callback(
369 state
->req_445
, smbsock_connect_connected
, req
);
377 state
->req_445
= open_socket_out_send(state
, ev
, addr
, 445, 5000);
378 if (tevent_req_nomem(state
->req_445
, req
)) {
379 return tevent_req_post(req
, ev
);
381 tevent_req_set_callback(state
->req_445
, smbsock_connect_connected
,
385 * After 5 msecs, fire the 139 request
387 state
->req_139
= tevent_wakeup_send(
388 state
, ev
, timeval_current_ofs(0, 5000));
389 if (tevent_req_nomem(state
->req_139
, req
)) {
390 TALLOC_FREE(state
->req_445
);
391 return tevent_req_post(req
, ev
);
393 tevent_req_set_callback(state
->req_139
, smbsock_connect_do_139
,
398 static int smbsock_connect_state_destructor(
399 struct smbsock_connect_state
*state
)
401 if (state
->sock
!= -1) {
408 static void smbsock_connect_do_139(struct tevent_req
*subreq
)
410 struct tevent_req
*req
= tevent_req_callback_data(
411 subreq
, struct tevent_req
);
412 struct smbsock_connect_state
*state
= tevent_req_data(
413 req
, struct smbsock_connect_state
);
416 ret
= tevent_wakeup_recv(subreq
);
419 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
422 state
->req_139
= nb_connect_send(state
, state
->ev
, state
->addr
,
426 state
->calling_type
);
427 if (tevent_req_nomem(state
->req_139
, req
)) {
430 tevent_req_set_callback(state
->req_139
, smbsock_connect_connected
,
434 static void smbsock_connect_connected(struct tevent_req
*subreq
)
436 struct tevent_req
*req
= tevent_req_callback_data(
437 subreq
, struct tevent_req
);
438 struct smbsock_connect_state
*state
= tevent_req_data(
439 req
, struct smbsock_connect_state
);
440 struct tevent_req
*unfinished_req
;
443 if (subreq
== state
->req_445
) {
445 status
= open_socket_out_recv(subreq
, &state
->sock
);
446 TALLOC_FREE(state
->req_445
);
447 unfinished_req
= state
->req_139
;
450 } else if (subreq
== state
->req_139
) {
452 status
= nb_connect_recv(subreq
, &state
->sock
);
453 TALLOC_FREE(state
->req_139
);
454 unfinished_req
= state
->req_445
;
458 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
462 if (NT_STATUS_IS_OK(status
)) {
463 TALLOC_FREE(unfinished_req
);
464 state
->req_139
= NULL
;
465 state
->req_445
= NULL
;
466 tevent_req_done(req
);
469 if (unfinished_req
== NULL
) {
471 * Both requests failed
473 tevent_req_nterror(req
, status
);
477 * Do nothing, wait for the second request to come here.
481 NTSTATUS
smbsock_connect_recv(struct tevent_req
*req
, int *sock
,
484 struct smbsock_connect_state
*state
= tevent_req_data(
485 req
, struct smbsock_connect_state
);
488 if (tevent_req_is_nterror(req
, &status
)) {
493 if (ret_port
!= NULL
) {
494 *ret_port
= state
->port
;
499 NTSTATUS
smbsock_connect(const struct sockaddr_storage
*addr
, uint16_t port
,
500 const char *called_name
, int called_type
,
501 const char *calling_name
, int calling_type
,
502 int *pfd
, uint16_t *ret_port
, int sec_timeout
)
504 TALLOC_CTX
*frame
= talloc_stackframe();
505 struct tevent_context
*ev
;
506 struct tevent_req
*req
;
507 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
509 ev
= tevent_context_init(frame
);
513 req
= smbsock_connect_send(frame
, ev
, addr
, port
,
514 called_name
, called_type
,
515 calling_name
, calling_type
);
519 if ((sec_timeout
!= 0) &&
520 !tevent_req_set_endtime(
521 req
, ev
, timeval_current_ofs(sec_timeout
, 0))) {
524 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
527 status
= smbsock_connect_recv(req
, pfd
, ret_port
);
533 struct smbsock_any_connect_state
{
534 struct tevent_context
*ev
;
535 const struct sockaddr_storage
*addrs
;
536 const char **called_names
;
538 const char **calling_names
;
543 struct tevent_req
**requests
;
548 uint16_t chosen_port
;
552 static bool smbsock_any_connect_send_next(
553 struct tevent_req
*req
, struct smbsock_any_connect_state
*state
);
554 static void smbsock_any_connect_trynext(struct tevent_req
*subreq
);
555 static void smbsock_any_connect_connected(struct tevent_req
*subreq
);
557 struct tevent_req
*smbsock_any_connect_send(TALLOC_CTX
*mem_ctx
,
558 struct tevent_context
*ev
,
559 const struct sockaddr_storage
*addrs
,
560 const char **called_names
,
562 const char **calling_names
,
564 size_t num_addrs
, uint16_t port
)
566 struct tevent_req
*req
, *subreq
;
567 struct smbsock_any_connect_state
*state
;
569 req
= tevent_req_create(mem_ctx
, &state
,
570 struct smbsock_any_connect_state
);
575 state
->addrs
= addrs
;
576 state
->num_addrs
= num_addrs
;
577 state
->called_names
= called_names
;
578 state
->called_types
= called_types
;
579 state
->calling_names
= calling_names
;
580 state
->calling_types
= calling_types
;
583 if (num_addrs
== 0) {
584 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
585 return tevent_req_post(req
, ev
);
588 state
->requests
= talloc_zero_array(state
, struct tevent_req
*,
590 if (tevent_req_nomem(state
->requests
, req
)) {
591 return tevent_req_post(req
, ev
);
593 if (!smbsock_any_connect_send_next(req
, state
)) {
594 return tevent_req_post(req
, ev
);
596 if (state
->num_sent
>= state
->num_addrs
) {
599 subreq
= tevent_wakeup_send(state
, ev
, timeval_current_ofs(0, 10000));
600 if (tevent_req_nomem(subreq
, req
)) {
601 return tevent_req_post(req
, ev
);
603 tevent_req_set_callback(subreq
, smbsock_any_connect_trynext
, req
);
607 static void smbsock_any_connect_trynext(struct tevent_req
*subreq
)
609 struct tevent_req
*req
= tevent_req_callback_data(
610 subreq
, struct tevent_req
);
611 struct smbsock_any_connect_state
*state
= tevent_req_data(
612 req
, struct smbsock_any_connect_state
);
615 ret
= tevent_wakeup_recv(subreq
);
618 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
621 if (!smbsock_any_connect_send_next(req
, state
)) {
624 if (state
->num_sent
>= state
->num_addrs
) {
627 subreq
= tevent_wakeup_send(state
, state
->ev
,
628 tevent_timeval_set(0, 10000));
629 if (tevent_req_nomem(subreq
, req
)) {
632 tevent_req_set_callback(subreq
, smbsock_any_connect_trynext
, req
);
635 static bool smbsock_any_connect_send_next(
636 struct tevent_req
*req
, struct smbsock_any_connect_state
*state
)
638 struct tevent_req
*subreq
;
640 if (state
->num_sent
>= state
->num_addrs
) {
641 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
644 subreq
= smbsock_connect_send(
645 state
->requests
, state
->ev
, &state
->addrs
[state
->num_sent
],
647 (state
->called_names
!= NULL
)
648 ? state
->called_names
[state
->num_sent
] : NULL
,
649 (state
->called_types
!= NULL
)
650 ? state
->called_types
[state
->num_sent
] : -1,
651 (state
->calling_names
!= NULL
)
652 ? state
->calling_names
[state
->num_sent
] : NULL
,
653 (state
->calling_types
!= NULL
)
654 ? state
->calling_types
[state
->num_sent
] : -1);
655 if (tevent_req_nomem(subreq
, req
)) {
658 tevent_req_set_callback(subreq
, smbsock_any_connect_connected
, req
);
660 state
->requests
[state
->num_sent
] = subreq
;
661 state
->num_sent
+= 1;
666 static void smbsock_any_connect_connected(struct tevent_req
*subreq
)
668 struct tevent_req
*req
= tevent_req_callback_data(
669 subreq
, struct tevent_req
);
670 struct smbsock_any_connect_state
*state
= tevent_req_data(
671 req
, struct smbsock_any_connect_state
);
674 uint16_t chosen_port
;
676 size_t chosen_index
= 0;
678 for (i
=0; i
<state
->num_sent
; i
++) {
679 if (state
->requests
[i
] == subreq
) {
684 if (i
== state
->num_sent
) {
685 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
689 status
= smbsock_connect_recv(subreq
, &fd
, &chosen_port
);
692 state
->requests
[chosen_index
] = NULL
;
694 if (NT_STATUS_IS_OK(status
)) {
696 * This will kill all the other requests
698 TALLOC_FREE(state
->requests
);
700 state
->chosen_port
= chosen_port
;
701 state
->chosen_index
= chosen_index
;
702 tevent_req_done(req
);
706 state
->num_received
+= 1;
707 if (state
->num_received
< state
->num_addrs
) {
709 * More addrs pending, wait for the others
715 * This is the last response, none succeeded.
717 tevent_req_nterror(req
, status
);
721 NTSTATUS
smbsock_any_connect_recv(struct tevent_req
*req
, int *pfd
,
722 size_t *chosen_index
,
723 uint16_t *chosen_port
)
725 struct smbsock_any_connect_state
*state
= tevent_req_data(
726 req
, struct smbsock_any_connect_state
);
729 if (tevent_req_is_nterror(req
, &status
)) {
733 if (chosen_index
!= NULL
) {
734 *chosen_index
= state
->chosen_index
;
736 if (chosen_port
!= NULL
) {
737 *chosen_port
= state
->chosen_port
;
742 NTSTATUS
smbsock_any_connect(const struct sockaddr_storage
*addrs
,
743 const char **called_names
,
745 const char **calling_names
,
750 int *pfd
, size_t *chosen_index
,
751 uint16_t *chosen_port
)
753 TALLOC_CTX
*frame
= talloc_stackframe();
754 struct tevent_context
*ev
;
755 struct tevent_req
*req
;
756 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
758 ev
= tevent_context_init(frame
);
762 req
= smbsock_any_connect_send(frame
, ev
, addrs
,
763 called_names
, called_types
,
764 calling_names
, calling_types
,
769 if ((sec_timeout
!= 0) &&
770 !tevent_req_set_endtime(
771 req
, ev
, timeval_current_ofs(sec_timeout
, 0))) {
774 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
777 status
= smbsock_any_connect_recv(req
, pfd
, chosen_index
, chosen_port
);