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 "async_smb.h"
24 #include "libsmb/nmblib.h"
26 struct nb_connect_state {
27 struct tevent_context *ev;
28 const struct sockaddr_storage *addr;
29 const char *called_name;
32 struct nmb_name called;
33 struct nmb_name calling;
36 static int nb_connect_state_destructor(struct nb_connect_state *state);
37 static void nb_connect_connected(struct tevent_req *subreq);
38 static void nb_connect_done(struct tevent_req *subreq);
40 static struct tevent_req *nb_connect_send(TALLOC_CTX *mem_ctx,
41 struct tevent_context *ev,
42 const struct sockaddr_storage *addr,
43 const char *called_name,
45 const char *calling_name,
48 struct tevent_req *req, *subreq;
49 struct nb_connect_state *state;
51 req = tevent_req_create(mem_ctx, &state, struct nb_connect_state);
56 state->called_name = called_name;
60 make_nmb_name(&state->called, called_name, called_type);
61 make_nmb_name(&state->calling, calling_name, calling_type);
63 talloc_set_destructor(state, nb_connect_state_destructor);
65 subreq = open_socket_out_send(state, ev, addr, 139, 5000);
66 if (tevent_req_nomem(subreq, req)) {
67 return tevent_req_post(req, ev);
69 tevent_req_set_callback(subreq, nb_connect_connected, req);
73 static int nb_connect_state_destructor(struct nb_connect_state *state)
75 if (state->sock != -1) {
81 static void nb_connect_connected(struct tevent_req *subreq)
83 struct tevent_req *req = tevent_req_callback_data(
84 subreq, struct tevent_req);
85 struct nb_connect_state *state = tevent_req_data(
86 req, struct nb_connect_state);
89 status = open_socket_out_recv(subreq, &state->sock);
91 if (!NT_STATUS_IS_OK(status)) {
92 tevent_req_nterror(req, status);
95 subreq = cli_session_request_send(state, state->ev, state->sock,
96 &state->called, &state->calling);
97 if (tevent_req_nomem(subreq, req)) {
100 tevent_req_set_callback(subreq, nb_connect_done, req);
103 static void nb_connect_done(struct tevent_req *subreq)
105 struct tevent_req *req = tevent_req_callback_data(
106 subreq, struct tevent_req);
107 struct nb_connect_state *state = tevent_req_data(
108 req, struct nb_connect_state);
113 ret = cli_session_request_recv(subreq, &err, &resp);
116 tevent_req_nterror(req, map_nt_error_from_unix(err));
121 * RFC1002: 0x82 - POSITIVE SESSION RESPONSE
126 * The server did not like our session request
131 if (strequal(state->called_name, "*SMBSERVER")) {
133 * Here we could try a name status request and
134 * use the first 0x20 type name.
137 req, NT_STATUS_RESOURCE_NAME_NOT_FOUND);
142 * We could be subtle and distinguish between
143 * different failure modes, but what we do here
144 * instead is just retry with *SMBSERVER type 0x20.
146 state->called_name = "*SMBSERVER";
147 make_nmb_name(&state->called, state->called_name, 0x20);
149 subreq = open_socket_out_send(state, state->ev, state->addr,
151 if (tevent_req_nomem(subreq, req)) {
154 tevent_req_set_callback(subreq, nb_connect_connected, req);
158 tevent_req_done(req);
163 static NTSTATUS nb_connect_recv(struct tevent_req *req, int *sock)
165 struct nb_connect_state *state = tevent_req_data(
166 req, struct nb_connect_state);
169 if (tevent_req_is_nterror(req, &status)) {
177 struct smbsock_connect_state {
178 struct tevent_context *ev;
179 const struct sockaddr_storage *addr;
180 const char *called_name;
182 const char *calling_name;
183 uint8_t calling_type;
184 struct tevent_req *req_139;
185 struct tevent_req *req_445;
190 static int smbsock_connect_state_destructor(
191 struct smbsock_connect_state *state);
192 static void smbsock_connect_connected(struct tevent_req *subreq);
193 static void smbsock_connect_do_139(struct tevent_req *subreq);
195 struct tevent_req *smbsock_connect_send(TALLOC_CTX *mem_ctx,
196 struct tevent_context *ev,
197 const struct sockaddr_storage *addr,
199 const char *called_name,
201 const char *calling_name,
204 struct tevent_req *req, *subreq;
205 struct smbsock_connect_state *state;
207 req = tevent_req_create(mem_ctx, &state, struct smbsock_connect_state);
215 (called_name != NULL) ? called_name : "*SMBSERVER";
217 (called_type != -1) ? called_type : 0x20;
218 state->calling_name =
219 (calling_name != NULL) ? calling_name : global_myname();
220 state->calling_type =
221 (calling_type != -1) ? calling_type : 0x00;
223 talloc_set_destructor(state, smbsock_connect_state_destructor);
226 subreq = tevent_wakeup_send(state, ev, timeval_set(0, 0));
227 if (tevent_req_nomem(subreq, req)) {
228 return tevent_req_post(req, ev);
230 tevent_req_set_callback(subreq, smbsock_connect_do_139, req);
234 state->req_445 = open_socket_out_send(state, ev, addr, port,
236 if (tevent_req_nomem(state->req_445, req)) {
237 return tevent_req_post(req, ev);
239 tevent_req_set_callback(
240 state->req_445, smbsock_connect_connected, req);
248 state->req_445 = open_socket_out_send(state, ev, addr, 445, 5000);
249 if (tevent_req_nomem(state->req_445, req)) {
250 return tevent_req_post(req, ev);
252 tevent_req_set_callback(state->req_445, smbsock_connect_connected,
256 * After 5 msecs, fire the 139 request
258 state->req_139 = tevent_wakeup_send(
259 state, ev, timeval_current_ofs(0, 5000));
260 if (tevent_req_nomem(state->req_139, req)) {
261 TALLOC_FREE(state->req_445);
262 return tevent_req_post(req, ev);
264 tevent_req_set_callback(state->req_139, smbsock_connect_do_139,
269 static int smbsock_connect_state_destructor(
270 struct smbsock_connect_state *state)
272 if (state->sock != -1) {
278 static void smbsock_connect_do_139(struct tevent_req *subreq)
280 struct tevent_req *req = tevent_req_callback_data(
281 subreq, struct tevent_req);
282 struct smbsock_connect_state *state = tevent_req_data(
283 req, struct smbsock_connect_state);
286 ret = tevent_wakeup_recv(subreq);
289 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
292 state->req_139 = nb_connect_send(state, state->ev, state->addr,
296 state->calling_type);
297 if (tevent_req_nomem(state->req_139, req)) {
300 tevent_req_set_callback(state->req_139, smbsock_connect_connected,
304 static void smbsock_connect_connected(struct tevent_req *subreq)
306 struct tevent_req *req = tevent_req_callback_data(
307 subreq, struct tevent_req);
308 struct smbsock_connect_state *state = tevent_req_data(
309 req, struct smbsock_connect_state);
310 struct tevent_req *unfinished_req;
313 if (subreq == state->req_445) {
315 status = open_socket_out_recv(subreq, &state->sock);
316 TALLOC_FREE(state->req_445);
317 unfinished_req = state->req_139;
320 } else if (subreq == state->req_139) {
322 status = nb_connect_recv(subreq, &state->sock);
323 TALLOC_FREE(state->req_139);
324 unfinished_req = state->req_445;
328 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
332 if (NT_STATUS_IS_OK(status)) {
333 TALLOC_FREE(unfinished_req);
334 state->req_139 = NULL;
335 state->req_445 = NULL;
336 tevent_req_done(req);
339 if (unfinished_req == NULL) {
341 * Both requests failed
343 tevent_req_nterror(req, status);
347 * Do nothing, wait for the second request to come here.
351 NTSTATUS smbsock_connect_recv(struct tevent_req *req, int *sock,
354 struct smbsock_connect_state *state = tevent_req_data(
355 req, struct smbsock_connect_state);
358 if (tevent_req_is_nterror(req, &status)) {
363 if (ret_port != NULL) {
364 *ret_port = state->port;
369 NTSTATUS smbsock_connect(const struct sockaddr_storage *addr, uint16_t port,
370 const char *called_name, int called_type,
371 const char *calling_name, int calling_type,
372 int *pfd, uint16_t *ret_port, int sec_timeout)
374 TALLOC_CTX *frame = talloc_stackframe();
375 struct event_context *ev;
376 struct tevent_req *req;
377 NTSTATUS status = NT_STATUS_NO_MEMORY;
379 ev = event_context_init(frame);
383 req = smbsock_connect_send(frame, ev, addr, port,
384 called_name, called_type,
385 calling_name, calling_type);
389 if ((sec_timeout != 0) &&
390 !tevent_req_set_endtime(
391 req, ev, timeval_current_ofs(sec_timeout, 0))) {
394 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
397 status = smbsock_connect_recv(req, pfd, ret_port);
403 struct smbsock_any_connect_state {
404 struct tevent_context *ev;
405 const struct sockaddr_storage *addrs;
406 const char **called_names;
408 const char **calling_names;
413 struct tevent_req **requests;
418 uint16_t chosen_port;
422 static bool smbsock_any_connect_send_next(
423 struct tevent_req *req, struct smbsock_any_connect_state *state);
424 static void smbsock_any_connect_trynext(struct tevent_req *subreq);
425 static void smbsock_any_connect_connected(struct tevent_req *subreq);
427 struct tevent_req *smbsock_any_connect_send(TALLOC_CTX *mem_ctx,
428 struct tevent_context *ev,
429 const struct sockaddr_storage *addrs,
430 const char **called_names,
432 const char **calling_names,
434 size_t num_addrs, uint16_t port)
436 struct tevent_req *req, *subreq;
437 struct smbsock_any_connect_state *state;
439 req = tevent_req_create(mem_ctx, &state,
440 struct smbsock_any_connect_state);
445 state->addrs = addrs;
446 state->num_addrs = num_addrs;
447 state->called_names = called_names;
448 state->called_types = called_types;
449 state->calling_names = calling_names;
450 state->calling_types = calling_types;
453 if (num_addrs == 0) {
454 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
455 return tevent_req_post(req, ev);
458 state->requests = talloc_zero_array(state, struct tevent_req *,
460 if (tevent_req_nomem(state->requests, req)) {
461 return tevent_req_post(req, ev);
463 if (!smbsock_any_connect_send_next(req, state)) {
464 return tevent_req_post(req, ev);
466 if (state->num_sent >= state->num_addrs) {
469 subreq = tevent_wakeup_send(state, ev, timeval_current_ofs(0, 10000));
470 if (tevent_req_nomem(subreq, req)) {
471 return tevent_req_post(req, ev);
473 tevent_req_set_callback(subreq, smbsock_any_connect_trynext, req);
477 static void smbsock_any_connect_trynext(struct tevent_req *subreq)
479 struct tevent_req *req = tevent_req_callback_data(
480 subreq, struct tevent_req);
481 struct smbsock_any_connect_state *state = tevent_req_data(
482 req, struct smbsock_any_connect_state);
485 ret = tevent_wakeup_recv(subreq);
488 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
491 if (!smbsock_any_connect_send_next(req, state)) {
494 if (state->num_sent >= state->num_addrs) {
497 subreq = tevent_wakeup_send(state, state->ev,
498 tevent_timeval_set(0, 10000));
499 if (tevent_req_nomem(subreq, req)) {
502 tevent_req_set_callback(subreq, smbsock_any_connect_trynext, req);
505 static bool smbsock_any_connect_send_next(
506 struct tevent_req *req, struct smbsock_any_connect_state *state)
508 struct tevent_req *subreq;
510 if (state->num_sent >= state->num_addrs) {
511 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
514 subreq = smbsock_connect_send(
515 state->requests, state->ev, &state->addrs[state->num_sent],
517 (state->called_names != NULL)
518 ? state->called_names[state->num_sent] : NULL,
519 (state->called_types != NULL)
520 ? state->called_types[state->num_sent] : -1,
521 (state->calling_names != NULL)
522 ? state->calling_names[state->num_sent] : NULL,
523 (state->calling_types != NULL)
524 ? state->calling_types[state->num_sent] : -1);
525 if (tevent_req_nomem(subreq, req)) {
528 tevent_req_set_callback(subreq, smbsock_any_connect_connected, req);
530 state->requests[state->num_sent] = subreq;
531 state->num_sent += 1;
536 static void smbsock_any_connect_connected(struct tevent_req *subreq)
538 struct tevent_req *req = tevent_req_callback_data(
539 subreq, struct tevent_req);
540 struct smbsock_any_connect_state *state = tevent_req_data(
541 req, struct smbsock_any_connect_state);
544 uint16_t chosen_port;
546 size_t chosen_index = 0;
548 for (i=0; i<state->num_sent; i++) {
549 if (state->requests[i] == subreq) {
554 if (i == state->num_sent) {
555 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
559 status = smbsock_connect_recv(subreq, &fd, &chosen_port);
562 state->requests[chosen_index] = NULL;
564 if (NT_STATUS_IS_OK(status)) {
566 * This will kill all the other requests
568 TALLOC_FREE(state->requests);
570 state->chosen_port = chosen_port;
571 state->chosen_index = chosen_index;
572 tevent_req_done(req);
576 state->num_received += 1;
577 if (state->num_received <= state->num_addrs) {
579 * More addrs pending, wait for the others
585 * This is the last response, none succeeded.
587 tevent_req_nterror(req, status);
591 NTSTATUS smbsock_any_connect_recv(struct tevent_req *req, int *pfd,
592 size_t *chosen_index,
593 uint16_t *chosen_port)
595 struct smbsock_any_connect_state *state = tevent_req_data(
596 req, struct smbsock_any_connect_state);
599 if (tevent_req_is_nterror(req, &status)) {
603 if (chosen_index != NULL) {
604 *chosen_index = state->chosen_index;
606 if (chosen_port != NULL) {
607 *chosen_port = state->chosen_port;
612 NTSTATUS smbsock_any_connect(const struct sockaddr_storage *addrs,
613 const char **called_names,
615 const char **calling_names,
620 int *pfd, size_t *chosen_index,
621 uint16_t *chosen_port)
623 TALLOC_CTX *frame = talloc_stackframe();
624 struct event_context *ev;
625 struct tevent_req *req;
626 NTSTATUS status = NT_STATUS_NO_MEMORY;
628 ev = event_context_init(frame);
632 req = smbsock_any_connect_send(frame, ev, addrs,
633 called_names, called_types,
634 calling_names, calling_types,
639 if ((sec_timeout != 0) &&
640 !tevent_req_set_endtime(
641 req, ev, timeval_current_ofs(sec_timeout, 0))) {
644 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
647 status = smbsock_any_connect_recv(req, pfd, chosen_index, chosen_port);