s3: Retry *SMBSERVER in nb_connect
[samba.git] / source3 / libsmb / smbsock_connect.c
1 /*
2    Unix SMB/CIFS implementation.
3    Connect to 445 and 139/nbsesssetup
4    Copyright (C) Volker Lendecke 2010
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
21 #include "../lib/async_req/async_sock.h"
22 #include "async_smb.h"
23
24 struct nb_connect_state {
25         struct tevent_context *ev;
26         const struct sockaddr_storage *addr;
27         const char *called_name;
28         int sock;
29
30         struct nmb_name called;
31         struct nmb_name calling;
32 };
33
34 static int nb_connect_state_destructor(struct nb_connect_state *state);
35 static void nb_connect_connected(struct tevent_req *subreq);
36 static void nb_connect_done(struct tevent_req *subreq);
37
38 static struct tevent_req *nb_connect_send(TALLOC_CTX *mem_ctx,
39                                           struct tevent_context *ev,
40                                           const struct sockaddr_storage *addr,
41                                           const char *called_name,
42                                           int called_type,
43                                           const char *calling_name,
44                                           int calling_type)
45 {
46         struct tevent_req *req, *subreq;
47         struct nb_connect_state *state;
48
49         req = tevent_req_create(mem_ctx, &state, struct nb_connect_state);
50         if (req == NULL) {
51                 return NULL;
52         }
53         state->ev = ev;
54         state->called_name = called_name;
55         state->addr = addr;
56
57         state->sock = -1;
58         make_nmb_name(&state->called, called_name, called_type);
59         make_nmb_name(&state->calling, calling_name, calling_type);
60
61         talloc_set_destructor(state, nb_connect_state_destructor);
62
63         subreq = open_socket_out_send(state, ev, addr, 139, 5000);
64         if (tevent_req_nomem(subreq, req)) {
65                 return tevent_req_post(req, ev);
66         }
67         tevent_req_set_callback(subreq, nb_connect_connected, req);
68         return req;
69 }
70
71 static int nb_connect_state_destructor(struct nb_connect_state *state)
72 {
73         if (state->sock != -1) {
74                 close(state->sock);
75         }
76         return 0;
77 }
78
79 static void nb_connect_connected(struct tevent_req *subreq)
80 {
81         struct tevent_req *req = tevent_req_callback_data(
82                 subreq, struct tevent_req);
83         struct nb_connect_state *state = tevent_req_data(
84                 req, struct nb_connect_state);
85         NTSTATUS status;
86
87         status = open_socket_out_recv(subreq, &state->sock);
88         TALLOC_FREE(subreq);
89         if (!NT_STATUS_IS_OK(status)) {
90                 tevent_req_nterror(req, status);
91                 return;
92         }
93         subreq = cli_session_request_send(state, state->ev, state->sock,
94                                           &state->called, &state->calling);
95         if (tevent_req_nomem(subreq, req)) {
96                 return;
97         }
98         tevent_req_set_callback(subreq, nb_connect_done, req);
99 }
100
101 static void nb_connect_done(struct tevent_req *subreq)
102 {
103         struct tevent_req *req = tevent_req_callback_data(
104                 subreq, struct tevent_req);
105         struct nb_connect_state *state = tevent_req_data(
106                 req, struct nb_connect_state);
107         bool ret;
108         int err;
109         uint8_t resp;
110
111         ret = cli_session_request_recv(subreq, &err, &resp);
112         TALLOC_FREE(subreq);
113         if (!ret) {
114                 tevent_req_nterror(req, map_nt_error_from_unix(err));
115                 return;
116         }
117
118         /*
119          * RFC1002: 0x82 - POSITIVE SESSION RESPONSE
120          */
121
122         if (resp != 0x82) {
123                 /*
124                  * The server did not like our session request
125                  */
126                 close(state->sock);
127                 state->sock = -1;
128
129                 if (strequal(state->called_name, "*SMBSERVER")) {
130                         /*
131                          * Here we could try a name status request and
132                          * use the first 0x20 type name.
133                          */
134                         tevent_req_nterror(
135                                 req, NT_STATUS_RESOURCE_NAME_NOT_FOUND);
136                         return;
137                 }
138
139                 /*
140                  * We could be subtle and distinguish between
141                  * different failure modes, but what we do here
142                  * instead is just retry with *SMBSERVER type 0x20.
143                  */
144                 state->called_name = "*SMBSERVER";
145                 make_nmb_name(&state->called, state->called_name, 0x20);
146
147                 subreq = open_socket_out_send(state, state->ev, state->addr,
148                                               139, 5000);
149                 if (tevent_req_nomem(subreq, req)) {
150                         return;
151                 }
152                 tevent_req_set_callback(subreq, nb_connect_connected, req);
153                 return;
154         }
155
156         tevent_req_done(req);
157         return;
158
159 }
160
161 static NTSTATUS nb_connect_recv(struct tevent_req *req, int *sock)
162 {
163         struct nb_connect_state *state = tevent_req_data(
164                 req, struct nb_connect_state);
165         NTSTATUS status;
166
167         if (tevent_req_is_nterror(req, &status)) {
168                 return status;
169         }
170         *sock = state->sock;
171         state->sock = -1;
172         return NT_STATUS_OK;
173 }
174
175 struct smbsock_connect_state {
176         struct tevent_context *ev;
177         const struct sockaddr_storage *addr;
178         const char *called_name;
179         const char *calling_name;
180         struct tevent_req *req_139;
181         struct tevent_req *req_445;
182         int sock;
183         uint16_t port;
184 };
185
186 static int smbsock_connect_state_destructor(
187         struct smbsock_connect_state *state);
188 static void smbsock_connect_connected(struct tevent_req *subreq);
189 static void smbsock_connect_do_139(struct tevent_req *subreq);
190
191 struct tevent_req *smbsock_connect_send(TALLOC_CTX *mem_ctx,
192                                         struct tevent_context *ev,
193                                         const struct sockaddr_storage *addr,
194                                         const char *called_name,
195                                         const char *calling_name)
196 {
197         struct tevent_req *req, *subreq;
198         struct smbsock_connect_state *state;
199
200         req = tevent_req_create(mem_ctx, &state, struct smbsock_connect_state);
201         if (req == NULL) {
202                 return NULL;
203         }
204         state->ev = ev;
205         state->addr = addr;
206         state->sock = -1;
207         state->called_name =
208                 (called_name != NULL) ? called_name : "*SMBSERVER";
209         state->calling_name =
210                 (calling_name != NULL) ? calling_name : global_myname();
211
212         talloc_set_destructor(state, smbsock_connect_state_destructor);
213
214         state->req_445 = open_socket_out_send(state, ev, addr, 445, 5000);
215         if (tevent_req_nomem(state->req_445, req)) {
216                 return tevent_req_post(req, ev);
217         }
218         tevent_req_set_callback(state->req_445, smbsock_connect_connected,
219                                 req);
220
221         /*
222          * After 5 msecs, fire the 139 request
223          */
224         subreq = tevent_wakeup_send(state, ev, timeval_current_ofs(0, 5000));
225         if (tevent_req_nomem(subreq, req)) {
226                 TALLOC_FREE(state->req_445);
227                 return tevent_req_post(req, ev);
228         }
229         tevent_req_set_callback(subreq, smbsock_connect_do_139, req);
230         return req;
231 }
232
233 static int smbsock_connect_state_destructor(
234         struct smbsock_connect_state *state)
235 {
236         if (state->sock != -1) {
237                 close(state->sock);
238         }
239         return 0;
240 }
241
242 static void smbsock_connect_do_139(struct tevent_req *subreq)
243 {
244         struct tevent_req *req = tevent_req_callback_data(
245                 subreq, struct tevent_req);
246         struct smbsock_connect_state *state = tevent_req_data(
247                 req, struct smbsock_connect_state);
248         bool ret;
249
250         ret = tevent_wakeup_recv(subreq);
251         TALLOC_FREE(subreq);
252         if (!ret) {
253                 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
254                 return;
255         }
256         state->req_139 = nb_connect_send(state, state->ev, state->addr,
257                                          state->called_name, 0x20,
258                                          state->calling_name, 0x0);
259         if (tevent_req_nomem(state->req_139, req)) {
260                 return;
261         }
262         tevent_req_set_callback(state->req_139, smbsock_connect_connected,
263                                 req);
264 }
265
266 static void smbsock_connect_connected(struct tevent_req *subreq)
267 {
268         struct tevent_req *req = tevent_req_callback_data(
269                 subreq, struct tevent_req);
270         struct smbsock_connect_state *state = tevent_req_data(
271                 req, struct smbsock_connect_state);
272         struct tevent_req *unfinished_req;
273         NTSTATUS status;
274
275         if (subreq == state->req_445) {
276
277                 status = open_socket_out_recv(subreq, &state->sock);
278                 TALLOC_FREE(state->req_445);
279                 unfinished_req = state->req_139;
280                 state->port = 445;
281
282         } else if (subreq == state->req_139) {
283
284                 status = nb_connect_recv(subreq, &state->sock);
285                 TALLOC_FREE(state->req_139);
286                 unfinished_req = state->req_445;
287                 state->port = 139;
288
289         } else {
290                 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
291                 return;
292         }
293
294         if (NT_STATUS_IS_OK(status)) {
295                 TALLOC_FREE(unfinished_req);
296                 state->req_139 = NULL;
297                 state->req_445 = NULL;
298                 tevent_req_done(req);
299                 return;
300         }
301         if (unfinished_req == NULL) {
302                 /*
303                  * Both requests failed
304                  */
305                 tevent_req_nterror(req, status);
306                 return;
307         }
308         /*
309          * Do nothing, wait for the second request to come here.
310          */
311 }
312
313 NTSTATUS smbsock_connect_recv(struct tevent_req *req, int *sock,
314                           uint16_t *port)
315 {
316         struct smbsock_connect_state *state = tevent_req_data(
317                 req, struct smbsock_connect_state);
318         NTSTATUS status;
319
320         if (tevent_req_is_nterror(req, &status)) {
321                 return status;
322         }
323         *sock = state->sock;
324         state->sock = -1;
325         if (port != NULL) {
326                 *port = state->port;
327         }
328         return NT_STATUS_OK;
329 }
330
331 NTSTATUS smbsock_connect(const struct sockaddr_storage *addr,
332                          const char *called_name, const char *calling_name,
333                          int *pfd, uint16_t *port)
334 {
335         TALLOC_CTX *frame = talloc_stackframe();
336         struct event_context *ev;
337         struct tevent_req *req;
338         NTSTATUS status = NT_STATUS_NO_MEMORY;
339
340         ev = event_context_init(frame);
341         if (ev == NULL) {
342                 goto fail;
343         }
344         req = smbsock_connect_send(frame, ev, addr, called_name, calling_name);
345         if (req == NULL) {
346                 goto fail;
347         }
348         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
349                 goto fail;
350         }
351         status = smbsock_connect_recv(req, pfd, port);
352  fail:
353         TALLOC_FREE(frame);
354         return status;
355 }
356
357 struct smbsock_any_connect_state {
358         struct tevent_context *ev;
359         const struct sockaddr_storage *addrs;
360         const char **called_names;
361         size_t num_addrs;
362
363         struct tevent_req **requests;
364         size_t num_sent;
365         size_t num_received;
366
367         int fd;
368         uint16_t port;
369         size_t chosen_index;
370 };
371
372 static bool smbsock_any_connect_send_next(
373         struct tevent_req *req, struct smbsock_any_connect_state *state);
374 static void smbsock_any_connect_trynext(struct tevent_req *subreq);
375 static void smbsock_any_connect_connected(struct tevent_req *subreq);
376
377 struct tevent_req *smbsock_any_connect_send(TALLOC_CTX *mem_ctx,
378                                             struct tevent_context *ev,
379                                             const struct sockaddr_storage *addrs,
380                                             const char **called_names,
381                                             size_t num_addrs)
382 {
383         struct tevent_req *req, *subreq;
384         struct smbsock_any_connect_state *state;
385
386         req = tevent_req_create(mem_ctx, &state,
387                                 struct smbsock_any_connect_state);
388         if (req == NULL) {
389                 return NULL;
390         }
391         state->ev = ev;
392         state->addrs = addrs;
393         state->num_addrs = num_addrs;
394         state->called_names = called_names;
395
396         if (num_addrs == 0) {
397                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
398                 return tevent_req_post(req, ev);
399         }
400
401         state->requests = talloc_zero_array(state, struct tevent_req *,
402                                             num_addrs);
403         if (tevent_req_nomem(state->requests, req)) {
404                 return tevent_req_post(req, ev);
405         }
406         if (!smbsock_any_connect_send_next(req, state)) {
407                 return tevent_req_post(req, ev);
408         }
409         if (state->num_sent >= state->num_addrs) {
410                 return req;
411         }
412         subreq = tevent_wakeup_send(state, ev, timeval_current_ofs(0, 10000));
413         if (tevent_req_nomem(subreq, req)) {
414                 return tevent_req_post(req, ev);
415         }
416         tevent_req_set_callback(subreq, smbsock_any_connect_trynext, req);
417         return req;
418 }
419
420 static void smbsock_any_connect_trynext(struct tevent_req *subreq)
421 {
422         struct tevent_req *req = tevent_req_callback_data(
423                 subreq, struct tevent_req);
424         struct smbsock_any_connect_state *state = tevent_req_data(
425                 req, struct smbsock_any_connect_state);
426         bool ret;
427
428         ret = tevent_wakeup_recv(subreq);
429         TALLOC_FREE(subreq);
430         if (!ret) {
431                 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
432                 return;
433         }
434         if (!smbsock_any_connect_send_next(req, state)) {
435                 return;
436         }
437         if (state->num_sent >= state->num_addrs) {
438                 return;
439         }
440         subreq = tevent_wakeup_send(state, state->ev,
441                                     tevent_timeval_set(0, 10000));
442         if (tevent_req_nomem(subreq, req)) {
443                 return;
444         }
445         tevent_req_set_callback(subreq, smbsock_any_connect_trynext, req);
446 }
447
448 static bool smbsock_any_connect_send_next(
449         struct tevent_req *req, struct smbsock_any_connect_state *state)
450 {
451         struct tevent_req *subreq;
452
453         if (state->num_sent >= state->num_addrs) {
454                 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
455                 return false;
456         }
457         subreq = smbsock_connect_send(
458                 state->requests, state->ev, &state->addrs[state->num_sent],
459                 (state->called_names != NULL)
460                 ? state->called_names[state->num_sent] : NULL,
461                 NULL);
462         if (tevent_req_nomem(subreq, req)) {
463                 return false;
464         }
465         tevent_req_set_callback(subreq, smbsock_any_connect_connected, req);
466
467         state->requests[state->num_sent] = subreq;
468         state->num_sent += 1;
469
470         return true;
471 }
472
473 static void smbsock_any_connect_connected(struct tevent_req *subreq)
474 {
475         struct tevent_req *req = tevent_req_callback_data(
476                 subreq, struct tevent_req);
477         struct smbsock_any_connect_state *state = tevent_req_data(
478                 req, struct smbsock_any_connect_state);
479         NTSTATUS status;
480         int fd;
481         uint16_t port;
482         size_t i;
483         size_t chosen_index = 0;
484
485         for (i=0; i<state->num_sent; i++) {
486                 if (state->requests[i] == subreq) {
487                         chosen_index = i;
488                         break;
489                 }
490         }
491         if (i == state->num_sent) {
492                 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
493                 return;
494         }
495
496         status = smbsock_connect_recv(subreq, &fd, &port);
497
498         TALLOC_FREE(subreq);
499         state->requests[chosen_index] = NULL;
500
501         if (NT_STATUS_IS_OK(status)) {
502                 /*
503                  * This will kill all the other requests
504                  */
505                 TALLOC_FREE(state->requests);
506                 state->fd = fd;
507                 state->port = port;
508                 state->chosen_index = chosen_index;
509                 tevent_req_done(req);
510                 return;
511         }
512
513         state->num_received += 1;
514         if (state->num_received <= state->num_addrs) {
515                 /*
516                  * More addrs pending, wait for the others
517                  */
518                 return;
519         }
520
521         /*
522          * This is the last response, none succeeded.
523          */
524         tevent_req_nterror(req, status);
525         return;
526 }
527
528 NTSTATUS smbsock_any_connect_recv(struct tevent_req *req, int *pfd,
529                                   size_t *chosen_index, uint16_t *port)
530 {
531         struct smbsock_any_connect_state *state = tevent_req_data(
532                 req, struct smbsock_any_connect_state);
533         NTSTATUS status;
534
535         if (tevent_req_is_nterror(req, &status)) {
536                 return status;
537         }
538         *pfd = state->fd;
539         if (chosen_index != NULL) {
540                 *chosen_index = state->chosen_index;
541         }
542         if (port != NULL) {
543                 *port = state->port;
544         }
545         return NT_STATUS_OK;
546 }
547
548 NTSTATUS smbsock_any_connect(const struct sockaddr_storage *addrs,
549                              const char **called_names, size_t num_addrs,
550                              int *pfd, size_t *chosen_index, uint16_t *port)
551 {
552         TALLOC_CTX *frame = talloc_stackframe();
553         struct event_context *ev;
554         struct tevent_req *req;
555         NTSTATUS status = NT_STATUS_NO_MEMORY;
556
557         ev = event_context_init(frame);
558         if (ev == NULL) {
559                 goto fail;
560         }
561         req = smbsock_any_connect_send(frame, ev, addrs, called_names,
562                                        num_addrs);
563         if (req == NULL) {
564                 goto fail;
565         }
566         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
567                 goto fail;
568         }
569         status = smbsock_any_connect_recv(req, pfd, chosen_index, port);
570  fail:
571         TALLOC_FREE(frame);
572         return status;
573 }