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/util/tevent_ntstatus.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, int sec_timeout)
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 ((sec_timeout != 0) &&
389 !tevent_req_set_endtime(
390 req, ev, timeval_current_ofs(sec_timeout, 0))) {
393 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
396 status = smbsock_connect_recv(req, pfd, ret_port);
402 struct smbsock_any_connect_state {
403 struct tevent_context *ev;
404 const struct sockaddr_storage *addrs;
405 const char **called_names;
407 const char **calling_names;
412 struct tevent_req **requests;
417 uint16_t chosen_port;
421 static bool smbsock_any_connect_send_next(
422 struct tevent_req *req, struct smbsock_any_connect_state *state);
423 static void smbsock_any_connect_trynext(struct tevent_req *subreq);
424 static void smbsock_any_connect_connected(struct tevent_req *subreq);
426 struct tevent_req *smbsock_any_connect_send(TALLOC_CTX *mem_ctx,
427 struct tevent_context *ev,
428 const struct sockaddr_storage *addrs,
429 const char **called_names,
431 const char **calling_names,
433 size_t num_addrs, uint16_t port)
435 struct tevent_req *req, *subreq;
436 struct smbsock_any_connect_state *state;
438 req = tevent_req_create(mem_ctx, &state,
439 struct smbsock_any_connect_state);
444 state->addrs = addrs;
445 state->num_addrs = num_addrs;
446 state->called_names = called_names;
447 state->called_types = called_types;
448 state->calling_names = calling_names;
449 state->calling_types = calling_types;
452 if (num_addrs == 0) {
453 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
454 return tevent_req_post(req, ev);
457 state->requests = talloc_zero_array(state, struct tevent_req *,
459 if (tevent_req_nomem(state->requests, req)) {
460 return tevent_req_post(req, ev);
462 if (!smbsock_any_connect_send_next(req, state)) {
463 return tevent_req_post(req, ev);
465 if (state->num_sent >= state->num_addrs) {
468 subreq = tevent_wakeup_send(state, ev, timeval_current_ofs(0, 10000));
469 if (tevent_req_nomem(subreq, req)) {
470 return tevent_req_post(req, ev);
472 tevent_req_set_callback(subreq, smbsock_any_connect_trynext, req);
476 static void smbsock_any_connect_trynext(struct tevent_req *subreq)
478 struct tevent_req *req = tevent_req_callback_data(
479 subreq, struct tevent_req);
480 struct smbsock_any_connect_state *state = tevent_req_data(
481 req, struct smbsock_any_connect_state);
484 ret = tevent_wakeup_recv(subreq);
487 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
490 if (!smbsock_any_connect_send_next(req, state)) {
493 if (state->num_sent >= state->num_addrs) {
496 subreq = tevent_wakeup_send(state, state->ev,
497 tevent_timeval_set(0, 10000));
498 if (tevent_req_nomem(subreq, req)) {
501 tevent_req_set_callback(subreq, smbsock_any_connect_trynext, req);
504 static bool smbsock_any_connect_send_next(
505 struct tevent_req *req, struct smbsock_any_connect_state *state)
507 struct tevent_req *subreq;
509 if (state->num_sent >= state->num_addrs) {
510 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
513 subreq = smbsock_connect_send(
514 state->requests, state->ev, &state->addrs[state->num_sent],
516 (state->called_names != NULL)
517 ? state->called_names[state->num_sent] : NULL,
518 (state->called_types != NULL)
519 ? state->called_types[state->num_sent] : -1,
520 (state->calling_names != NULL)
521 ? state->calling_names[state->num_sent] : NULL,
522 (state->calling_types != NULL)
523 ? state->calling_types[state->num_sent] : -1);
524 if (tevent_req_nomem(subreq, req)) {
527 tevent_req_set_callback(subreq, smbsock_any_connect_connected, req);
529 state->requests[state->num_sent] = subreq;
530 state->num_sent += 1;
535 static void smbsock_any_connect_connected(struct tevent_req *subreq)
537 struct tevent_req *req = tevent_req_callback_data(
538 subreq, struct tevent_req);
539 struct smbsock_any_connect_state *state = tevent_req_data(
540 req, struct smbsock_any_connect_state);
543 uint16_t chosen_port;
545 size_t chosen_index = 0;
547 for (i=0; i<state->num_sent; i++) {
548 if (state->requests[i] == subreq) {
553 if (i == state->num_sent) {
554 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
558 status = smbsock_connect_recv(subreq, &fd, &chosen_port);
561 state->requests[chosen_index] = NULL;
563 if (NT_STATUS_IS_OK(status)) {
565 * This will kill all the other requests
567 TALLOC_FREE(state->requests);
569 state->chosen_port = chosen_port;
570 state->chosen_index = chosen_index;
571 tevent_req_done(req);
575 state->num_received += 1;
576 if (state->num_received <= state->num_addrs) {
578 * More addrs pending, wait for the others
584 * This is the last response, none succeeded.
586 tevent_req_nterror(req, status);
590 NTSTATUS smbsock_any_connect_recv(struct tevent_req *req, int *pfd,
591 size_t *chosen_index,
592 uint16_t *chosen_port)
594 struct smbsock_any_connect_state *state = tevent_req_data(
595 req, struct smbsock_any_connect_state);
598 if (tevent_req_is_nterror(req, &status)) {
602 if (chosen_index != NULL) {
603 *chosen_index = state->chosen_index;
605 if (chosen_port != NULL) {
606 *chosen_port = state->chosen_port;
611 NTSTATUS smbsock_any_connect(const struct sockaddr_storage *addrs,
612 const char **called_names,
614 const char **calling_names,
619 int *pfd, size_t *chosen_index,
620 uint16_t *chosen_port)
622 TALLOC_CTX *frame = talloc_stackframe();
623 struct event_context *ev;
624 struct tevent_req *req;
625 NTSTATUS status = NT_STATUS_NO_MEMORY;
627 ev = event_context_init(frame);
631 req = smbsock_any_connect_send(frame, ev, addrs,
632 called_names, called_types,
633 calling_names, calling_types,
638 if ((sec_timeout != 0) &&
639 !tevent_req_set_endtime(
640 req, ev, timeval_current_ofs(sec_timeout, 0))) {
643 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
646 status = smbsock_any_connect_recv(req, pfd, chosen_index, chosen_port);