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 "async_smb.h"
23 #include "libsmb/nmblib.h"
25 struct nb_connect_state
{
26 struct tevent_context
*ev
;
27 const struct sockaddr_storage
*addr
;
28 const char *called_name
;
31 struct nmb_name called
;
32 struct nmb_name calling
;
35 static int nb_connect_state_destructor(struct nb_connect_state
*state
);
36 static void nb_connect_connected(struct tevent_req
*subreq
);
37 static void nb_connect_done(struct tevent_req
*subreq
);
39 static struct tevent_req
*nb_connect_send(TALLOC_CTX
*mem_ctx
,
40 struct tevent_context
*ev
,
41 const struct sockaddr_storage
*addr
,
42 const char *called_name
,
44 const char *calling_name
,
47 struct tevent_req
*req
, *subreq
;
48 struct nb_connect_state
*state
;
50 req
= tevent_req_create(mem_ctx
, &state
, struct nb_connect_state
);
55 state
->called_name
= called_name
;
59 make_nmb_name(&state
->called
, called_name
, called_type
);
60 make_nmb_name(&state
->calling
, calling_name
, calling_type
);
62 talloc_set_destructor(state
, nb_connect_state_destructor
);
64 subreq
= open_socket_out_send(state
, ev
, addr
, 139, 5000);
65 if (tevent_req_nomem(subreq
, req
)) {
66 return tevent_req_post(req
, ev
);
68 tevent_req_set_callback(subreq
, nb_connect_connected
, req
);
72 static int nb_connect_state_destructor(struct nb_connect_state
*state
)
74 if (state
->sock
!= -1) {
80 static void nb_connect_connected(struct tevent_req
*subreq
)
82 struct tevent_req
*req
= tevent_req_callback_data(
83 subreq
, struct tevent_req
);
84 struct nb_connect_state
*state
= tevent_req_data(
85 req
, struct nb_connect_state
);
88 status
= open_socket_out_recv(subreq
, &state
->sock
);
90 if (!NT_STATUS_IS_OK(status
)) {
91 tevent_req_nterror(req
, status
);
94 subreq
= cli_session_request_send(state
, state
->ev
, state
->sock
,
95 &state
->called
, &state
->calling
);
96 if (tevent_req_nomem(subreq
, req
)) {
99 tevent_req_set_callback(subreq
, nb_connect_done
, req
);
102 static void nb_connect_done(struct tevent_req
*subreq
)
104 struct tevent_req
*req
= tevent_req_callback_data(
105 subreq
, struct tevent_req
);
106 struct nb_connect_state
*state
= tevent_req_data(
107 req
, struct nb_connect_state
);
112 ret
= cli_session_request_recv(subreq
, &err
, &resp
);
115 tevent_req_nterror(req
, map_nt_error_from_unix(err
));
120 * RFC1002: 0x82 - POSITIVE SESSION RESPONSE
125 * The server did not like our session request
130 if (strequal(state
->called_name
, "*SMBSERVER")) {
132 * Here we could try a name status request and
133 * use the first 0x20 type name.
136 req
, NT_STATUS_RESOURCE_NAME_NOT_FOUND
);
141 * We could be subtle and distinguish between
142 * different failure modes, but what we do here
143 * instead is just retry with *SMBSERVER type 0x20.
145 state
->called_name
= "*SMBSERVER";
146 make_nmb_name(&state
->called
, state
->called_name
, 0x20);
148 subreq
= open_socket_out_send(state
, state
->ev
, state
->addr
,
150 if (tevent_req_nomem(subreq
, req
)) {
153 tevent_req_set_callback(subreq
, nb_connect_connected
, req
);
157 tevent_req_done(req
);
162 static NTSTATUS
nb_connect_recv(struct tevent_req
*req
, int *sock
)
164 struct nb_connect_state
*state
= tevent_req_data(
165 req
, struct nb_connect_state
);
168 if (tevent_req_is_nterror(req
, &status
)) {
176 struct smbsock_connect_state
{
177 struct tevent_context
*ev
;
178 const struct sockaddr_storage
*addr
;
179 const char *called_name
;
181 const char *calling_name
;
182 uint8_t calling_type
;
183 struct tevent_req
*req_139
;
184 struct tevent_req
*req_445
;
189 static int smbsock_connect_state_destructor(
190 struct smbsock_connect_state
*state
);
191 static void smbsock_connect_connected(struct tevent_req
*subreq
);
192 static void smbsock_connect_do_139(struct tevent_req
*subreq
);
194 struct tevent_req
*smbsock_connect_send(TALLOC_CTX
*mem_ctx
,
195 struct tevent_context
*ev
,
196 const struct sockaddr_storage
*addr
,
198 const char *called_name
,
200 const char *calling_name
,
203 struct tevent_req
*req
, *subreq
;
204 struct smbsock_connect_state
*state
;
206 req
= tevent_req_create(mem_ctx
, &state
, struct smbsock_connect_state
);
214 (called_name
!= NULL
) ? called_name
: "*SMBSERVER";
216 (called_type
!= -1) ? called_type
: 0x20;
217 state
->calling_name
=
218 (calling_name
!= NULL
) ? calling_name
: global_myname();
219 state
->calling_type
=
220 (calling_type
!= -1) ? calling_type
: 0x00;
222 talloc_set_destructor(state
, smbsock_connect_state_destructor
);
225 subreq
= tevent_wakeup_send(state
, ev
, timeval_set(0, 0));
226 if (tevent_req_nomem(subreq
, req
)) {
227 return tevent_req_post(req
, ev
);
229 tevent_req_set_callback(subreq
, smbsock_connect_do_139
, req
);
233 state
->req_445
= open_socket_out_send(state
, ev
, addr
, port
,
235 if (tevent_req_nomem(state
->req_445
, req
)) {
236 return tevent_req_post(req
, ev
);
238 tevent_req_set_callback(
239 state
->req_445
, smbsock_connect_connected
, req
);
247 state
->req_445
= open_socket_out_send(state
, ev
, addr
, 445, 5000);
248 if (tevent_req_nomem(state
->req_445
, req
)) {
249 return tevent_req_post(req
, ev
);
251 tevent_req_set_callback(state
->req_445
, smbsock_connect_connected
,
255 * After 5 msecs, fire the 139 request
257 state
->req_139
= tevent_wakeup_send(
258 state
, ev
, timeval_current_ofs(0, 5000));
259 if (tevent_req_nomem(state
->req_139
, req
)) {
260 TALLOC_FREE(state
->req_445
);
261 return tevent_req_post(req
, ev
);
263 tevent_req_set_callback(state
->req_139
, smbsock_connect_do_139
,
268 static int smbsock_connect_state_destructor(
269 struct smbsock_connect_state
*state
)
271 if (state
->sock
!= -1) {
277 static void smbsock_connect_do_139(struct tevent_req
*subreq
)
279 struct tevent_req
*req
= tevent_req_callback_data(
280 subreq
, struct tevent_req
);
281 struct smbsock_connect_state
*state
= tevent_req_data(
282 req
, struct smbsock_connect_state
);
285 ret
= tevent_wakeup_recv(subreq
);
288 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
291 state
->req_139
= nb_connect_send(state
, state
->ev
, state
->addr
,
295 state
->calling_type
);
296 if (tevent_req_nomem(state
->req_139
, req
)) {
299 tevent_req_set_callback(state
->req_139
, smbsock_connect_connected
,
303 static void smbsock_connect_connected(struct tevent_req
*subreq
)
305 struct tevent_req
*req
= tevent_req_callback_data(
306 subreq
, struct tevent_req
);
307 struct smbsock_connect_state
*state
= tevent_req_data(
308 req
, struct smbsock_connect_state
);
309 struct tevent_req
*unfinished_req
;
312 if (subreq
== state
->req_445
) {
314 status
= open_socket_out_recv(subreq
, &state
->sock
);
315 TALLOC_FREE(state
->req_445
);
316 unfinished_req
= state
->req_139
;
319 } else if (subreq
== state
->req_139
) {
321 status
= nb_connect_recv(subreq
, &state
->sock
);
322 TALLOC_FREE(state
->req_139
);
323 unfinished_req
= state
->req_445
;
327 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
331 if (NT_STATUS_IS_OK(status
)) {
332 TALLOC_FREE(unfinished_req
);
333 state
->req_139
= NULL
;
334 state
->req_445
= NULL
;
335 tevent_req_done(req
);
338 if (unfinished_req
== NULL
) {
340 * Both requests failed
342 tevent_req_nterror(req
, status
);
346 * Do nothing, wait for the second request to come here.
350 NTSTATUS
smbsock_connect_recv(struct tevent_req
*req
, int *sock
,
353 struct smbsock_connect_state
*state
= tevent_req_data(
354 req
, struct smbsock_connect_state
);
357 if (tevent_req_is_nterror(req
, &status
)) {
362 if (ret_port
!= NULL
) {
363 *ret_port
= state
->port
;
368 NTSTATUS
smbsock_connect(const struct sockaddr_storage
*addr
, uint16_t port
,
369 const char *called_name
, int called_type
,
370 const char *calling_name
, int calling_type
,
371 int *pfd
, uint16_t *ret_port
)
373 TALLOC_CTX
*frame
= talloc_stackframe();
374 struct event_context
*ev
;
375 struct tevent_req
*req
;
376 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
378 ev
= event_context_init(frame
);
382 req
= smbsock_connect_send(frame
, ev
, addr
, port
,
383 called_name
, called_type
,
384 calling_name
, calling_type
);
388 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
391 status
= smbsock_connect_recv(req
, pfd
, ret_port
);
397 struct smbsock_any_connect_state
{
398 struct tevent_context
*ev
;
399 const struct sockaddr_storage
*addrs
;
400 const char **called_names
;
402 const char **calling_names
;
407 struct tevent_req
**requests
;
412 uint16_t chosen_port
;
416 static bool smbsock_any_connect_send_next(
417 struct tevent_req
*req
, struct smbsock_any_connect_state
*state
);
418 static void smbsock_any_connect_trynext(struct tevent_req
*subreq
);
419 static void smbsock_any_connect_connected(struct tevent_req
*subreq
);
421 struct tevent_req
*smbsock_any_connect_send(TALLOC_CTX
*mem_ctx
,
422 struct tevent_context
*ev
,
423 const struct sockaddr_storage
*addrs
,
424 const char **called_names
,
426 const char **calling_names
,
428 size_t num_addrs
, uint16_t port
)
430 struct tevent_req
*req
, *subreq
;
431 struct smbsock_any_connect_state
*state
;
433 req
= tevent_req_create(mem_ctx
, &state
,
434 struct smbsock_any_connect_state
);
439 state
->addrs
= addrs
;
440 state
->num_addrs
= num_addrs
;
441 state
->called_names
= called_names
;
442 state
->called_types
= called_types
;
443 state
->calling_names
= calling_names
;
444 state
->calling_types
= calling_types
;
447 if (num_addrs
== 0) {
448 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
449 return tevent_req_post(req
, ev
);
452 state
->requests
= talloc_zero_array(state
, struct tevent_req
*,
454 if (tevent_req_nomem(state
->requests
, req
)) {
455 return tevent_req_post(req
, ev
);
457 if (!smbsock_any_connect_send_next(req
, state
)) {
458 return tevent_req_post(req
, ev
);
460 if (state
->num_sent
>= state
->num_addrs
) {
463 subreq
= tevent_wakeup_send(state
, ev
, timeval_current_ofs(0, 10000));
464 if (tevent_req_nomem(subreq
, req
)) {
465 return tevent_req_post(req
, ev
);
467 tevent_req_set_callback(subreq
, smbsock_any_connect_trynext
, req
);
471 static void smbsock_any_connect_trynext(struct tevent_req
*subreq
)
473 struct tevent_req
*req
= tevent_req_callback_data(
474 subreq
, struct tevent_req
);
475 struct smbsock_any_connect_state
*state
= tevent_req_data(
476 req
, struct smbsock_any_connect_state
);
479 ret
= tevent_wakeup_recv(subreq
);
482 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
485 if (!smbsock_any_connect_send_next(req
, state
)) {
488 if (state
->num_sent
>= state
->num_addrs
) {
491 subreq
= tevent_wakeup_send(state
, state
->ev
,
492 tevent_timeval_set(0, 10000));
493 if (tevent_req_nomem(subreq
, req
)) {
496 tevent_req_set_callback(subreq
, smbsock_any_connect_trynext
, req
);
499 static bool smbsock_any_connect_send_next(
500 struct tevent_req
*req
, struct smbsock_any_connect_state
*state
)
502 struct tevent_req
*subreq
;
504 if (state
->num_sent
>= state
->num_addrs
) {
505 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
508 subreq
= smbsock_connect_send(
509 state
->requests
, state
->ev
, &state
->addrs
[state
->num_sent
],
511 (state
->called_names
!= NULL
)
512 ? state
->called_names
[state
->num_sent
] : NULL
,
513 (state
->called_types
!= NULL
)
514 ? state
->called_types
[state
->num_sent
] : -1,
515 (state
->calling_names
!= NULL
)
516 ? state
->calling_names
[state
->num_sent
] : NULL
,
517 (state
->calling_types
!= NULL
)
518 ? state
->calling_types
[state
->num_sent
] : -1);
519 if (tevent_req_nomem(subreq
, req
)) {
522 tevent_req_set_callback(subreq
, smbsock_any_connect_connected
, req
);
524 state
->requests
[state
->num_sent
] = subreq
;
525 state
->num_sent
+= 1;
530 static void smbsock_any_connect_connected(struct tevent_req
*subreq
)
532 struct tevent_req
*req
= tevent_req_callback_data(
533 subreq
, struct tevent_req
);
534 struct smbsock_any_connect_state
*state
= tevent_req_data(
535 req
, struct smbsock_any_connect_state
);
538 uint16_t chosen_port
;
540 size_t chosen_index
= 0;
542 for (i
=0; i
<state
->num_sent
; i
++) {
543 if (state
->requests
[i
] == subreq
) {
548 if (i
== state
->num_sent
) {
549 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
553 status
= smbsock_connect_recv(subreq
, &fd
, &chosen_port
);
556 state
->requests
[chosen_index
] = NULL
;
558 if (NT_STATUS_IS_OK(status
)) {
560 * This will kill all the other requests
562 TALLOC_FREE(state
->requests
);
564 state
->chosen_port
= chosen_port
;
565 state
->chosen_index
= chosen_index
;
566 tevent_req_done(req
);
570 state
->num_received
+= 1;
571 if (state
->num_received
<= state
->num_addrs
) {
573 * More addrs pending, wait for the others
579 * This is the last response, none succeeded.
581 tevent_req_nterror(req
, status
);
585 NTSTATUS
smbsock_any_connect_recv(struct tevent_req
*req
, int *pfd
,
586 size_t *chosen_index
,
587 uint16_t *chosen_port
)
589 struct smbsock_any_connect_state
*state
= tevent_req_data(
590 req
, struct smbsock_any_connect_state
);
593 if (tevent_req_is_nterror(req
, &status
)) {
597 if (chosen_index
!= NULL
) {
598 *chosen_index
= state
->chosen_index
;
600 if (chosen_port
!= NULL
) {
601 *chosen_port
= state
->chosen_port
;
606 NTSTATUS
smbsock_any_connect(const struct sockaddr_storage
*addrs
,
607 const char **called_names
,
609 const char **calling_names
,
613 int *pfd
, size_t *chosen_index
,
614 uint16_t *chosen_port
)
616 TALLOC_CTX
*frame
= talloc_stackframe();
617 struct event_context
*ev
;
618 struct tevent_req
*req
;
619 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
621 ev
= event_context_init(frame
);
625 req
= smbsock_any_connect_send(frame
, ev
, addrs
,
626 called_names
, called_types
,
627 calling_names
, calling_types
,
632 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
635 status
= smbsock_any_connect_recv(req
, pfd
, chosen_index
, chosen_port
);