This reverts commit 378c4b221a6be75e1d32cb6fb3a773ce5de6dbda.
[Samba.git] / source3 / libsmb / smbsock_connect.c
blobfd8626f5ad283e638c977cc3cf03136b77ce6dc5
1 /*
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/>.
20 #include "includes.h"
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;
29 int sock;
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,
43 int called_type,
44 const char *calling_name,
45 int calling_type)
47 struct tevent_req *req, *subreq;
48 struct nb_connect_state *state;
50 req = tevent_req_create(mem_ctx, &state, struct nb_connect_state);
51 if (req == NULL) {
52 return NULL;
54 state->ev = ev;
55 state->called_name = called_name;
56 state->addr = addr;
58 state->sock = -1;
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);
69 return req;
72 static int nb_connect_state_destructor(struct nb_connect_state *state)
74 if (state->sock != -1) {
75 close(state->sock);
77 return 0;
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);
86 NTSTATUS status;
88 status = open_socket_out_recv(subreq, &state->sock);
89 TALLOC_FREE(subreq);
90 if (!NT_STATUS_IS_OK(status)) {
91 tevent_req_nterror(req, status);
92 return;
94 subreq = cli_session_request_send(state, state->ev, state->sock,
95 &state->called, &state->calling);
96 if (tevent_req_nomem(subreq, req)) {
97 return;
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);
108 bool ret;
109 int err;
110 uint8_t resp;
112 ret = cli_session_request_recv(subreq, &err, &resp);
113 TALLOC_FREE(subreq);
114 if (!ret) {
115 tevent_req_nterror(req, map_nt_error_from_unix(err));
116 return;
120 * RFC1002: 0x82 - POSITIVE SESSION RESPONSE
123 if (resp != 0x82) {
125 * The server did not like our session request
127 close(state->sock);
128 state->sock = -1;
130 if (strequal(state->called_name, "*SMBSERVER")) {
132 * Here we could try a name status request and
133 * use the first 0x20 type name.
135 tevent_req_nterror(
136 req, NT_STATUS_RESOURCE_NAME_NOT_FOUND);
137 return;
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,
149 139, 5000);
150 if (tevent_req_nomem(subreq, req)) {
151 return;
153 tevent_req_set_callback(subreq, nb_connect_connected, req);
154 return;
157 tevent_req_done(req);
158 return;
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);
166 NTSTATUS status;
168 if (tevent_req_is_nterror(req, &status)) {
169 return status;
171 *sock = state->sock;
172 state->sock = -1;
173 return NT_STATUS_OK;
176 struct smbsock_connect_state {
177 struct tevent_context *ev;
178 const struct sockaddr_storage *addr;
179 const char *called_name;
180 uint8_t called_type;
181 const char *calling_name;
182 uint8_t calling_type;
183 struct tevent_req *req_139;
184 struct tevent_req *req_445;
185 int sock;
186 uint16_t port;
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,
197 uint16_t port,
198 const char *called_name,
199 int called_type,
200 const char *calling_name,
201 int calling_type)
203 struct tevent_req *req, *subreq;
204 struct smbsock_connect_state *state;
206 req = tevent_req_create(mem_ctx, &state, struct smbsock_connect_state);
207 if (req == NULL) {
208 return NULL;
210 state->ev = ev;
211 state->addr = addr;
212 state->sock = -1;
213 state->called_name =
214 (called_name != NULL) ? called_name : "*SMBSERVER";
215 state->called_type =
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);
224 if (port == 139) {
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);
230 return req;
232 if (port != 0) {
233 state->req_445 = open_socket_out_send(state, ev, addr, port,
234 5000);
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);
240 return req;
244 * port==0, try both
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,
252 req);
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,
264 req);
265 return req;
268 static int smbsock_connect_state_destructor(
269 struct smbsock_connect_state *state)
271 if (state->sock != -1) {
272 close(state->sock);
274 return 0;
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);
283 bool ret;
285 ret = tevent_wakeup_recv(subreq);
286 TALLOC_FREE(subreq);
287 if (!ret) {
288 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
289 return;
291 state->req_139 = nb_connect_send(state, state->ev, state->addr,
292 state->called_name,
293 state->called_type,
294 state->calling_name,
295 state->calling_type);
296 if (tevent_req_nomem(state->req_139, req)) {
297 return;
299 tevent_req_set_callback(state->req_139, smbsock_connect_connected,
300 req);
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;
310 NTSTATUS status;
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;
317 state->port = 445;
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;
324 state->port = 139;
326 } else {
327 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
328 return;
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);
336 return;
338 if (unfinished_req == NULL) {
340 * Both requests failed
342 tevent_req_nterror(req, status);
343 return;
346 * Do nothing, wait for the second request to come here.
350 NTSTATUS smbsock_connect_recv(struct tevent_req *req, int *sock,
351 uint16_t *ret_port)
353 struct smbsock_connect_state *state = tevent_req_data(
354 req, struct smbsock_connect_state);
355 NTSTATUS status;
357 if (tevent_req_is_nterror(req, &status)) {
358 return status;
360 *sock = state->sock;
361 state->sock = -1;
362 if (ret_port != NULL) {
363 *ret_port = state->port;
365 return NT_STATUS_OK;
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);
379 if (ev == NULL) {
380 goto fail;
382 req = smbsock_connect_send(frame, ev, addr, port,
383 called_name, called_type,
384 calling_name, calling_type);
385 if (req == NULL) {
386 goto fail;
388 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
389 goto fail;
391 status = smbsock_connect_recv(req, pfd, ret_port);
392 fail:
393 TALLOC_FREE(frame);
394 return status;
397 struct smbsock_any_connect_state {
398 struct tevent_context *ev;
399 const struct sockaddr_storage *addrs;
400 const char **called_names;
401 int *called_types;
402 const char **calling_names;
403 int *calling_types;
404 size_t num_addrs;
405 uint16_t port;
407 struct tevent_req **requests;
408 size_t num_sent;
409 size_t num_received;
411 int fd;
412 uint16_t chosen_port;
413 size_t chosen_index;
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,
425 int *called_types,
426 const char **calling_names,
427 int *calling_types,
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);
435 if (req == NULL) {
436 return NULL;
438 state->ev = ev;
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;
445 state->port = port;
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 *,
453 num_addrs);
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) {
461 return req;
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);
468 return 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);
477 bool ret;
479 ret = tevent_wakeup_recv(subreq);
480 TALLOC_FREE(subreq);
481 if (!ret) {
482 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
483 return;
485 if (!smbsock_any_connect_send_next(req, state)) {
486 return;
488 if (state->num_sent >= state->num_addrs) {
489 return;
491 subreq = tevent_wakeup_send(state, state->ev,
492 tevent_timeval_set(0, 10000));
493 if (tevent_req_nomem(subreq, req)) {
494 return;
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);
506 return false;
508 subreq = smbsock_connect_send(
509 state->requests, state->ev, &state->addrs[state->num_sent],
510 state->port,
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)) {
520 return false;
522 tevent_req_set_callback(subreq, smbsock_any_connect_connected, req);
524 state->requests[state->num_sent] = subreq;
525 state->num_sent += 1;
527 return true;
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);
536 NTSTATUS status;
537 int fd;
538 uint16_t chosen_port;
539 size_t i;
540 size_t chosen_index = 0;
542 for (i=0; i<state->num_sent; i++) {
543 if (state->requests[i] == subreq) {
544 chosen_index = i;
545 break;
548 if (i == state->num_sent) {
549 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
550 return;
553 status = smbsock_connect_recv(subreq, &fd, &chosen_port);
555 TALLOC_FREE(subreq);
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);
563 state->fd = fd;
564 state->chosen_port = chosen_port;
565 state->chosen_index = chosen_index;
566 tevent_req_done(req);
567 return;
570 state->num_received += 1;
571 if (state->num_received <= state->num_addrs) {
573 * More addrs pending, wait for the others
575 return;
579 * This is the last response, none succeeded.
581 tevent_req_nterror(req, status);
582 return;
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);
591 NTSTATUS status;
593 if (tevent_req_is_nterror(req, &status)) {
594 return status;
596 *pfd = state->fd;
597 if (chosen_index != NULL) {
598 *chosen_index = state->chosen_index;
600 if (chosen_port != NULL) {
601 *chosen_port = state->chosen_port;
603 return NT_STATUS_OK;
606 NTSTATUS smbsock_any_connect(const struct sockaddr_storage *addrs,
607 const char **called_names,
608 int *called_types,
609 const char **calling_names,
610 int *calling_types,
611 size_t num_addrs,
612 uint16_t port,
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);
622 if (ev == NULL) {
623 goto fail;
625 req = smbsock_any_connect_send(frame, ev, addrs,
626 called_names, called_types,
627 calling_names, calling_types,
628 num_addrs, port);
629 if (req == NULL) {
630 goto fail;
632 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
633 goto fail;
635 status = smbsock_any_connect_recv(req, pfd, chosen_index, chosen_port);
636 fail:
637 TALLOC_FREE(frame);
638 return status;