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
;
160 struct tevent_req
*session_subreq
;
161 struct nmb_name called
;
162 struct nmb_name calling
;
165 static void nb_connect_cleanup(struct tevent_req
*req
,
166 enum tevent_req_state req_state
);
167 static void nb_connect_connected(struct tevent_req
*subreq
);
168 static void nb_connect_done(struct tevent_req
*subreq
);
170 static struct tevent_req
*nb_connect_send(TALLOC_CTX
*mem_ctx
,
171 struct tevent_context
*ev
,
172 const struct sockaddr_storage
*addr
,
173 const char *called_name
,
175 const char *calling_name
,
178 struct tevent_req
*req
, *subreq
;
179 struct nb_connect_state
*state
;
181 req
= tevent_req_create(mem_ctx
, &state
, struct nb_connect_state
);
186 state
->called_name
= called_name
;
190 make_nmb_name(&state
->called
, called_name
, called_type
);
191 make_nmb_name(&state
->calling
, calling_name
, calling_type
);
193 tevent_req_set_cleanup_fn(req
, nb_connect_cleanup
);
195 subreq
= open_socket_out_send(state
, ev
, addr
, NBT_SMB_PORT
, 5000);
196 if (tevent_req_nomem(subreq
, req
)) {
197 return tevent_req_post(req
, ev
);
199 tevent_req_set_callback(subreq
, nb_connect_connected
, req
);
203 static void nb_connect_cleanup(struct tevent_req
*req
,
204 enum tevent_req_state req_state
)
206 struct nb_connect_state
*state
= tevent_req_data(
207 req
, struct nb_connect_state
);
210 * we need to free a pending request before closing the
211 * socket, see bug #11141
213 TALLOC_FREE(state
->session_subreq
);
215 if (req_state
== TEVENT_REQ_DONE
) {
217 * we keep the socket open for the caller to use
222 if (state
->sock
!= -1) {
230 static void nb_connect_connected(struct tevent_req
*subreq
)
232 struct tevent_req
*req
= tevent_req_callback_data(
233 subreq
, struct tevent_req
);
234 struct nb_connect_state
*state
= tevent_req_data(
235 req
, struct nb_connect_state
);
238 status
= open_socket_out_recv(subreq
, &state
->sock
);
240 if (tevent_req_nterror(req
, status
)) {
243 subreq
= cli_session_request_send(state
, state
->ev
, state
->sock
,
244 &state
->called
, &state
->calling
);
245 if (tevent_req_nomem(subreq
, req
)) {
248 tevent_req_set_callback(subreq
, nb_connect_done
, req
);
249 state
->session_subreq
= subreq
;
252 static void nb_connect_done(struct tevent_req
*subreq
)
254 struct tevent_req
*req
= tevent_req_callback_data(
255 subreq
, struct tevent_req
);
256 struct nb_connect_state
*state
= tevent_req_data(
257 req
, struct nb_connect_state
);
262 state
->session_subreq
= NULL
;
264 ret
= cli_session_request_recv(subreq
, &err
, &resp
);
267 tevent_req_nterror(req
, map_nt_error_from_unix(err
));
272 * RFC1002: 0x82 - POSITIVE SESSION RESPONSE
277 * The server did not like our session request
282 if (strequal(state
->called_name
, "*SMBSERVER")) {
284 * Here we could try a name status request and
285 * use the first 0x20 type name.
288 req
, NT_STATUS_RESOURCE_NAME_NOT_FOUND
);
293 * We could be subtle and distinguish between
294 * different failure modes, but what we do here
295 * instead is just retry with *SMBSERVER type 0x20.
297 state
->called_name
= "*SMBSERVER";
298 make_nmb_name(&state
->called
, state
->called_name
, 0x20);
300 subreq
= open_socket_out_send(state
, state
->ev
, state
->addr
,
302 if (tevent_req_nomem(subreq
, req
)) {
305 tevent_req_set_callback(subreq
, nb_connect_connected
, req
);
309 tevent_req_done(req
);
313 static NTSTATUS
nb_connect_recv(struct tevent_req
*req
, int *sock
)
315 struct nb_connect_state
*state
= tevent_req_data(
316 req
, struct nb_connect_state
);
319 if (tevent_req_is_nterror(req
, &status
)) {
320 tevent_req_received(req
);
325 tevent_req_received(req
);
329 struct smbsock_connect_state
{
330 struct tevent_context
*ev
;
331 const struct sockaddr_storage
*addr
;
332 const char *called_name
;
334 const char *calling_name
;
335 uint8_t calling_type
;
336 struct tevent_req
*req_139
;
337 struct tevent_req
*req_445
;
342 static void smbsock_connect_cleanup(struct tevent_req
*req
,
343 enum tevent_req_state req_state
);
344 static void smbsock_connect_connected(struct tevent_req
*subreq
);
345 static void smbsock_connect_do_139(struct tevent_req
*subreq
);
347 struct tevent_req
*smbsock_connect_send(TALLOC_CTX
*mem_ctx
,
348 struct tevent_context
*ev
,
349 const struct sockaddr_storage
*addr
,
351 const char *called_name
,
353 const char *calling_name
,
356 struct tevent_req
*req
;
357 struct smbsock_connect_state
*state
;
359 req
= tevent_req_create(mem_ctx
, &state
, struct smbsock_connect_state
);
367 (called_name
!= NULL
) ? called_name
: "*SMBSERVER";
369 (called_type
!= -1) ? called_type
: 0x20;
370 state
->calling_name
=
371 (calling_name
!= NULL
) ? calling_name
: lp_netbios_name();
372 state
->calling_type
=
373 (calling_type
!= -1) ? calling_type
: 0x00;
375 tevent_req_set_cleanup_fn(req
, smbsock_connect_cleanup
);
377 if (port
== NBT_SMB_PORT
) {
378 if (lp_disable_netbios()) {
379 tevent_req_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
380 return tevent_req_post(req
, ev
);
383 state
->req_139
= nb_connect_send(state
, state
->ev
, state
->addr
,
387 state
->calling_type
);
388 if (tevent_req_nomem(state
->req_139
, req
)) {
389 return tevent_req_post(req
, ev
);
391 tevent_req_set_callback(
392 state
->req_139
, smbsock_connect_connected
, req
);
396 state
->req_445
= open_socket_out_send(state
, ev
, addr
, port
,
398 if (tevent_req_nomem(state
->req_445
, req
)) {
399 return tevent_req_post(req
, ev
);
401 tevent_req_set_callback(
402 state
->req_445
, smbsock_connect_connected
, req
);
410 state
->req_445
= open_socket_out_send(state
, ev
, addr
, TCP_SMB_PORT
, 5000);
411 if (tevent_req_nomem(state
->req_445
, req
)) {
412 return tevent_req_post(req
, ev
);
414 tevent_req_set_callback(state
->req_445
, smbsock_connect_connected
,
418 * Check for disable_netbios
420 if (lp_disable_netbios()) {
425 * After 5 msecs, fire the 139 (NBT) request
427 state
->req_139
= tevent_wakeup_send(
428 state
, ev
, timeval_current_ofs(0, 5000));
429 if (tevent_req_nomem(state
->req_139
, req
)) {
430 TALLOC_FREE(state
->req_445
);
431 return tevent_req_post(req
, ev
);
433 tevent_req_set_callback(state
->req_139
, smbsock_connect_do_139
,
438 static void smbsock_connect_cleanup(struct tevent_req
*req
,
439 enum tevent_req_state req_state
)
441 struct smbsock_connect_state
*state
= tevent_req_data(
442 req
, struct smbsock_connect_state
);
445 * we need to free a pending request before closing the
446 * socket, see bug #11141
448 TALLOC_FREE(state
->req_445
);
449 TALLOC_FREE(state
->req_139
);
451 if (req_state
== TEVENT_REQ_DONE
) {
453 * we keep the socket open for the caller to use
458 if (state
->sock
!= -1) {
466 static void smbsock_connect_do_139(struct tevent_req
*subreq
)
468 struct tevent_req
*req
= tevent_req_callback_data(
469 subreq
, struct tevent_req
);
470 struct smbsock_connect_state
*state
= tevent_req_data(
471 req
, struct smbsock_connect_state
);
474 ret
= tevent_wakeup_recv(subreq
);
477 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
480 state
->req_139
= nb_connect_send(state
, state
->ev
, state
->addr
,
484 state
->calling_type
);
485 if (tevent_req_nomem(state
->req_139
, req
)) {
488 tevent_req_set_callback(state
->req_139
, smbsock_connect_connected
,
492 static void smbsock_connect_connected(struct tevent_req
*subreq
)
494 struct tevent_req
*req
= tevent_req_callback_data(
495 subreq
, struct tevent_req
);
496 struct smbsock_connect_state
*state
= tevent_req_data(
497 req
, struct smbsock_connect_state
);
498 struct tevent_req
*unfinished_req
;
501 if (subreq
== state
->req_445
) {
503 status
= open_socket_out_recv(subreq
, &state
->sock
);
504 TALLOC_FREE(state
->req_445
);
505 unfinished_req
= state
->req_139
;
506 state
->port
= TCP_SMB_PORT
;
508 } else if (subreq
== state
->req_139
) {
510 status
= nb_connect_recv(subreq
, &state
->sock
);
511 TALLOC_FREE(state
->req_139
);
512 unfinished_req
= state
->req_445
;
513 state
->port
= NBT_SMB_PORT
;
516 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
520 if (NT_STATUS_IS_OK(status
)) {
521 TALLOC_FREE(unfinished_req
);
522 state
->req_139
= NULL
;
523 state
->req_445
= NULL
;
524 tevent_req_done(req
);
527 if (unfinished_req
== NULL
) {
529 * Both requests failed
531 tevent_req_nterror(req
, status
);
535 * Do nothing, wait for the second request to come here.
539 NTSTATUS
smbsock_connect_recv(struct tevent_req
*req
, int *sock
,
542 struct smbsock_connect_state
*state
= tevent_req_data(
543 req
, struct smbsock_connect_state
);
546 if (tevent_req_is_nterror(req
, &status
)) {
547 tevent_req_received(req
);
552 if (ret_port
!= NULL
) {
553 *ret_port
= state
->port
;
555 tevent_req_received(req
);
559 NTSTATUS
smbsock_connect(const struct sockaddr_storage
*addr
, uint16_t port
,
560 const char *called_name
, int called_type
,
561 const char *calling_name
, int calling_type
,
562 int *pfd
, uint16_t *ret_port
, int sec_timeout
)
564 TALLOC_CTX
*frame
= talloc_stackframe();
565 struct tevent_context
*ev
;
566 struct tevent_req
*req
;
567 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
569 ev
= samba_tevent_context_init(frame
);
573 req
= smbsock_connect_send(frame
, ev
, addr
, port
,
574 called_name
, called_type
,
575 calling_name
, calling_type
);
579 if ((sec_timeout
!= 0) &&
580 !tevent_req_set_endtime(
581 req
, ev
, timeval_current_ofs(sec_timeout
, 0))) {
584 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
587 status
= smbsock_connect_recv(req
, pfd
, ret_port
);
593 struct smbsock_any_connect_state
{
594 struct tevent_context
*ev
;
595 const struct sockaddr_storage
*addrs
;
596 const char **called_names
;
598 const char **calling_names
;
603 struct tevent_req
**requests
;
608 uint16_t chosen_port
;
612 static void smbsock_any_connect_cleanup(struct tevent_req
*req
,
613 enum tevent_req_state req_state
);
614 static bool smbsock_any_connect_send_next(
615 struct tevent_req
*req
, struct smbsock_any_connect_state
*state
);
616 static void smbsock_any_connect_trynext(struct tevent_req
*subreq
);
617 static void smbsock_any_connect_connected(struct tevent_req
*subreq
);
619 struct tevent_req
*smbsock_any_connect_send(TALLOC_CTX
*mem_ctx
,
620 struct tevent_context
*ev
,
621 const struct sockaddr_storage
*addrs
,
622 const char **called_names
,
624 const char **calling_names
,
626 size_t num_addrs
, uint16_t port
)
628 struct tevent_req
*req
, *subreq
;
629 struct smbsock_any_connect_state
*state
;
631 req
= tevent_req_create(mem_ctx
, &state
,
632 struct smbsock_any_connect_state
);
637 state
->addrs
= addrs
;
638 state
->num_addrs
= num_addrs
;
639 state
->called_names
= called_names
;
640 state
->called_types
= called_types
;
641 state
->calling_names
= calling_names
;
642 state
->calling_types
= calling_types
;
646 tevent_req_set_cleanup_fn(req
, smbsock_any_connect_cleanup
);
648 if (num_addrs
== 0) {
649 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
650 return tevent_req_post(req
, ev
);
653 state
->requests
= talloc_zero_array(state
, struct tevent_req
*,
655 if (tevent_req_nomem(state
->requests
, req
)) {
656 return tevent_req_post(req
, ev
);
658 if (!smbsock_any_connect_send_next(req
, state
)) {
659 return tevent_req_post(req
, ev
);
661 if (state
->num_sent
>= state
->num_addrs
) {
664 subreq
= tevent_wakeup_send(state
, ev
, timeval_current_ofs(0, 10000));
665 if (tevent_req_nomem(subreq
, req
)) {
666 return tevent_req_post(req
, ev
);
668 tevent_req_set_callback(subreq
, smbsock_any_connect_trynext
, req
);
672 static void smbsock_any_connect_cleanup(struct tevent_req
*req
,
673 enum tevent_req_state req_state
)
675 struct smbsock_any_connect_state
*state
= tevent_req_data(
676 req
, struct smbsock_any_connect_state
);
678 TALLOC_FREE(state
->requests
);
680 if (req_state
== TEVENT_REQ_DONE
) {
682 * Keep the socket open for the caller.
687 if (state
->fd
!= -1) {
693 static void smbsock_any_connect_trynext(struct tevent_req
*subreq
)
695 struct tevent_req
*req
= tevent_req_callback_data(
696 subreq
, struct tevent_req
);
697 struct smbsock_any_connect_state
*state
= tevent_req_data(
698 req
, struct smbsock_any_connect_state
);
701 ret
= tevent_wakeup_recv(subreq
);
704 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
707 if (!smbsock_any_connect_send_next(req
, state
)) {
710 if (state
->num_sent
>= state
->num_addrs
) {
713 subreq
= tevent_wakeup_send(state
, state
->ev
,
714 tevent_timeval_set(0, 10000));
715 if (tevent_req_nomem(subreq
, req
)) {
718 tevent_req_set_callback(subreq
, smbsock_any_connect_trynext
, req
);
721 static bool smbsock_any_connect_send_next(
722 struct tevent_req
*req
, struct smbsock_any_connect_state
*state
)
724 struct tevent_req
*subreq
;
726 if (state
->num_sent
>= state
->num_addrs
) {
727 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
730 subreq
= smbsock_connect_send(
731 state
->requests
, state
->ev
, &state
->addrs
[state
->num_sent
],
733 (state
->called_names
!= NULL
)
734 ? state
->called_names
[state
->num_sent
] : NULL
,
735 (state
->called_types
!= NULL
)
736 ? state
->called_types
[state
->num_sent
] : -1,
737 (state
->calling_names
!= NULL
)
738 ? state
->calling_names
[state
->num_sent
] : NULL
,
739 (state
->calling_types
!= NULL
)
740 ? state
->calling_types
[state
->num_sent
] : -1);
741 if (tevent_req_nomem(subreq
, req
)) {
744 tevent_req_set_callback(subreq
, smbsock_any_connect_connected
, req
);
746 state
->requests
[state
->num_sent
] = subreq
;
747 state
->num_sent
+= 1;
752 static void smbsock_any_connect_connected(struct tevent_req
*subreq
)
754 struct tevent_req
*req
= tevent_req_callback_data(
755 subreq
, struct tevent_req
);
756 struct smbsock_any_connect_state
*state
= tevent_req_data(
757 req
, struct smbsock_any_connect_state
);
760 uint16_t chosen_port
= 0;
762 size_t chosen_index
= 0;
764 for (i
=0; i
<state
->num_sent
; i
++) {
765 if (state
->requests
[i
] == subreq
) {
770 if (i
== state
->num_sent
) {
771 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
775 status
= smbsock_connect_recv(subreq
, &fd
, &chosen_port
);
778 state
->requests
[chosen_index
] = NULL
;
780 if (NT_STATUS_IS_OK(status
)) {
782 * tevent_req_done() will kill all the other requests
783 * via smbsock_any_connect_cleanup().
786 state
->chosen_port
= chosen_port
;
787 state
->chosen_index
= chosen_index
;
788 tevent_req_done(req
);
792 state
->num_received
+= 1;
793 if (state
->num_received
< state
->num_addrs
) {
795 * More addrs pending, wait for the others
801 * This is the last response, none succeeded.
803 tevent_req_nterror(req
, status
);
807 NTSTATUS
smbsock_any_connect_recv(struct tevent_req
*req
, int *pfd
,
808 size_t *chosen_index
,
809 uint16_t *chosen_port
)
811 struct smbsock_any_connect_state
*state
= tevent_req_data(
812 req
, struct smbsock_any_connect_state
);
815 if (tevent_req_is_nterror(req
, &status
)) {
816 tevent_req_received(req
);
821 if (chosen_index
!= NULL
) {
822 *chosen_index
= state
->chosen_index
;
824 if (chosen_port
!= NULL
) {
825 *chosen_port
= state
->chosen_port
;
827 tevent_req_received(req
);
831 NTSTATUS
smbsock_any_connect(const struct sockaddr_storage
*addrs
,
832 const char **called_names
,
834 const char **calling_names
,
839 int *pfd
, size_t *chosen_index
,
840 uint16_t *chosen_port
)
842 TALLOC_CTX
*frame
= talloc_stackframe();
843 struct tevent_context
*ev
;
844 struct tevent_req
*req
;
845 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
847 ev
= samba_tevent_context_init(frame
);
851 req
= smbsock_any_connect_send(frame
, ev
, addrs
,
852 called_names
, called_types
,
853 calling_names
, calling_types
,
858 if ((sec_timeout
!= 0) &&
859 !tevent_req_set_endtime(
860 req
, ev
, timeval_current_ofs(sec_timeout
, 0))) {
863 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
866 status
= smbsock_any_connect_recv(req
, pfd
, chosen_index
, chosen_port
);