s3-includes: finally only include client.h when libsmb is used.
[bbaumbach/samba-autobuild/.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/util/tevent_ntstatus.h"
22 #include "client.h"
23 #include "async_smb.h"
24 #include "libsmb/nmblib.h"
25
26 struct nb_connect_state {
27         struct tevent_context *ev;
28         const struct sockaddr_storage *addr;
29         const char *called_name;
30         int sock;
31
32         struct nmb_name called;
33         struct nmb_name calling;
34 };
35
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);
39
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,
44                                           int called_type,
45                                           const char *calling_name,
46                                           int calling_type)
47 {
48         struct tevent_req *req, *subreq;
49         struct nb_connect_state *state;
50
51         req = tevent_req_create(mem_ctx, &state, struct nb_connect_state);
52         if (req == NULL) {
53                 return NULL;
54         }
55         state->ev = ev;
56         state->called_name = called_name;
57         state->addr = addr;
58
59         state->sock = -1;
60         make_nmb_name(&state->called, called_name, called_type);
61         make_nmb_name(&state->calling, calling_name, calling_type);
62
63         talloc_set_destructor(state, nb_connect_state_destructor);
64
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);
68         }
69         tevent_req_set_callback(subreq, nb_connect_connected, req);
70         return req;
71 }
72
73 static int nb_connect_state_destructor(struct nb_connect_state *state)
74 {
75         if (state->sock != -1) {
76                 close(state->sock);
77         }
78         return 0;
79 }
80
81 static void nb_connect_connected(struct tevent_req *subreq)
82 {
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);
87         NTSTATUS status;
88
89         status = open_socket_out_recv(subreq, &state->sock);
90         TALLOC_FREE(subreq);
91         if (!NT_STATUS_IS_OK(status)) {
92                 tevent_req_nterror(req, status);
93                 return;
94         }
95         subreq = cli_session_request_send(state, state->ev, state->sock,
96                                           &state->called, &state->calling);
97         if (tevent_req_nomem(subreq, req)) {
98                 return;
99         }
100         tevent_req_set_callback(subreq, nb_connect_done, req);
101 }
102
103 static void nb_connect_done(struct tevent_req *subreq)
104 {
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);
109         bool ret;
110         int err;
111         uint8_t resp;
112
113         ret = cli_session_request_recv(subreq, &err, &resp);
114         TALLOC_FREE(subreq);
115         if (!ret) {
116                 tevent_req_nterror(req, map_nt_error_from_unix(err));
117                 return;
118         }
119
120         /*
121          * RFC1002: 0x82 - POSITIVE SESSION RESPONSE
122          */
123
124         if (resp != 0x82) {
125                 /*
126                  * The server did not like our session request
127                  */
128                 close(state->sock);
129                 state->sock = -1;
130
131                 if (strequal(state->called_name, "*SMBSERVER")) {
132                         /*
133                          * Here we could try a name status request and
134                          * use the first 0x20 type name.
135                          */
136                         tevent_req_nterror(
137                                 req, NT_STATUS_RESOURCE_NAME_NOT_FOUND);
138                         return;
139                 }
140
141                 /*
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.
145                  */
146                 state->called_name = "*SMBSERVER";
147                 make_nmb_name(&state->called, state->called_name, 0x20);
148
149                 subreq = open_socket_out_send(state, state->ev, state->addr,
150                                               139, 5000);
151                 if (tevent_req_nomem(subreq, req)) {
152                         return;
153                 }
154                 tevent_req_set_callback(subreq, nb_connect_connected, req);
155                 return;
156         }
157
158         tevent_req_done(req);
159         return;
160
161 }
162
163 static NTSTATUS nb_connect_recv(struct tevent_req *req, int *sock)
164 {
165         struct nb_connect_state *state = tevent_req_data(
166                 req, struct nb_connect_state);
167         NTSTATUS status;
168
169         if (tevent_req_is_nterror(req, &status)) {
170                 return status;
171         }
172         *sock = state->sock;
173         state->sock = -1;
174         return NT_STATUS_OK;
175 }
176
177 struct smbsock_connect_state {
178         struct tevent_context *ev;
179         const struct sockaddr_storage *addr;
180         const char *called_name;
181         uint8_t called_type;
182         const char *calling_name;
183         uint8_t calling_type;
184         struct tevent_req *req_139;
185         struct tevent_req *req_445;
186         int sock;
187         uint16_t port;
188 };
189
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);
194
195 struct tevent_req *smbsock_connect_send(TALLOC_CTX *mem_ctx,
196                                         struct tevent_context *ev,
197                                         const struct sockaddr_storage *addr,
198                                         uint16_t port,
199                                         const char *called_name,
200                                         int called_type,
201                                         const char *calling_name,
202                                         int calling_type)
203 {
204         struct tevent_req *req, *subreq;
205         struct smbsock_connect_state *state;
206
207         req = tevent_req_create(mem_ctx, &state, struct smbsock_connect_state);
208         if (req == NULL) {
209                 return NULL;
210         }
211         state->ev = ev;
212         state->addr = addr;
213         state->sock = -1;
214         state->called_name =
215                 (called_name != NULL) ? called_name : "*SMBSERVER";
216         state->called_type =
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;
222
223         talloc_set_destructor(state, smbsock_connect_state_destructor);
224
225         if (port == 139) {
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);
229                 }
230                 tevent_req_set_callback(subreq, smbsock_connect_do_139, req);
231                 return req;
232         }
233         if (port != 0) {
234                 state->req_445 = open_socket_out_send(state, ev, addr, port,
235                                                       5000);
236                 if (tevent_req_nomem(state->req_445, req)) {
237                         return tevent_req_post(req, ev);
238                 }
239                 tevent_req_set_callback(
240                         state->req_445, smbsock_connect_connected, req);
241                 return req;
242         }
243
244         /*
245          * port==0, try both
246          */
247
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);
251         }
252         tevent_req_set_callback(state->req_445, smbsock_connect_connected,
253                                 req);
254
255         /*
256          * After 5 msecs, fire the 139 request
257          */
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);
263         }
264         tevent_req_set_callback(state->req_139, smbsock_connect_do_139,
265                                 req);
266         return req;
267 }
268
269 static int smbsock_connect_state_destructor(
270         struct smbsock_connect_state *state)
271 {
272         if (state->sock != -1) {
273                 close(state->sock);
274         }
275         return 0;
276 }
277
278 static void smbsock_connect_do_139(struct tevent_req *subreq)
279 {
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);
284         bool ret;
285
286         ret = tevent_wakeup_recv(subreq);
287         TALLOC_FREE(subreq);
288         if (!ret) {
289                 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
290                 return;
291         }
292         state->req_139 = nb_connect_send(state, state->ev, state->addr,
293                                          state->called_name,
294                                          state->called_type,
295                                          state->calling_name,
296                                          state->calling_type);
297         if (tevent_req_nomem(state->req_139, req)) {
298                 return;
299         }
300         tevent_req_set_callback(state->req_139, smbsock_connect_connected,
301                                 req);
302 }
303
304 static void smbsock_connect_connected(struct tevent_req *subreq)
305 {
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;
311         NTSTATUS status;
312
313         if (subreq == state->req_445) {
314
315                 status = open_socket_out_recv(subreq, &state->sock);
316                 TALLOC_FREE(state->req_445);
317                 unfinished_req = state->req_139;
318                 state->port = 445;
319
320         } else if (subreq == state->req_139) {
321
322                 status = nb_connect_recv(subreq, &state->sock);
323                 TALLOC_FREE(state->req_139);
324                 unfinished_req = state->req_445;
325                 state->port = 139;
326
327         } else {
328                 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
329                 return;
330         }
331
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);
337                 return;
338         }
339         if (unfinished_req == NULL) {
340                 /*
341                  * Both requests failed
342                  */
343                 tevent_req_nterror(req, status);
344                 return;
345         }
346         /*
347          * Do nothing, wait for the second request to come here.
348          */
349 }
350
351 NTSTATUS smbsock_connect_recv(struct tevent_req *req, int *sock,
352                               uint16_t *ret_port)
353 {
354         struct smbsock_connect_state *state = tevent_req_data(
355                 req, struct smbsock_connect_state);
356         NTSTATUS status;
357
358         if (tevent_req_is_nterror(req, &status)) {
359                 return status;
360         }
361         *sock = state->sock;
362         state->sock = -1;
363         if (ret_port != NULL) {
364                 *ret_port = state->port;
365         }
366         return NT_STATUS_OK;
367 }
368
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)
373 {
374         TALLOC_CTX *frame = talloc_stackframe();
375         struct event_context *ev;
376         struct tevent_req *req;
377         NTSTATUS status = NT_STATUS_NO_MEMORY;
378
379         ev = event_context_init(frame);
380         if (ev == NULL) {
381                 goto fail;
382         }
383         req = smbsock_connect_send(frame, ev, addr, port,
384                                    called_name, called_type,
385                                    calling_name, calling_type);
386         if (req == NULL) {
387                 goto fail;
388         }
389         if ((sec_timeout != 0) &&
390             !tevent_req_set_endtime(
391                     req, ev, timeval_current_ofs(sec_timeout, 0))) {
392                 goto fail;
393         }
394         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
395                 goto fail;
396         }
397         status = smbsock_connect_recv(req, pfd, ret_port);
398  fail:
399         TALLOC_FREE(frame);
400         return status;
401 }
402
403 struct smbsock_any_connect_state {
404         struct tevent_context *ev;
405         const struct sockaddr_storage *addrs;
406         const char **called_names;
407         int *called_types;
408         const char **calling_names;
409         int *calling_types;
410         size_t num_addrs;
411         uint16_t port;
412
413         struct tevent_req **requests;
414         size_t num_sent;
415         size_t num_received;
416
417         int fd;
418         uint16_t chosen_port;
419         size_t chosen_index;
420 };
421
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);
426
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,
431                                             int *called_types,
432                                             const char **calling_names,
433                                             int *calling_types,
434                                             size_t num_addrs, uint16_t port)
435 {
436         struct tevent_req *req, *subreq;
437         struct smbsock_any_connect_state *state;
438
439         req = tevent_req_create(mem_ctx, &state,
440                                 struct smbsock_any_connect_state);
441         if (req == NULL) {
442                 return NULL;
443         }
444         state->ev = ev;
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;
451         state->port = port;
452
453         if (num_addrs == 0) {
454                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
455                 return tevent_req_post(req, ev);
456         }
457
458         state->requests = talloc_zero_array(state, struct tevent_req *,
459                                             num_addrs);
460         if (tevent_req_nomem(state->requests, req)) {
461                 return tevent_req_post(req, ev);
462         }
463         if (!smbsock_any_connect_send_next(req, state)) {
464                 return tevent_req_post(req, ev);
465         }
466         if (state->num_sent >= state->num_addrs) {
467                 return req;
468         }
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);
472         }
473         tevent_req_set_callback(subreq, smbsock_any_connect_trynext, req);
474         return req;
475 }
476
477 static void smbsock_any_connect_trynext(struct tevent_req *subreq)
478 {
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);
483         bool ret;
484
485         ret = tevent_wakeup_recv(subreq);
486         TALLOC_FREE(subreq);
487         if (!ret) {
488                 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
489                 return;
490         }
491         if (!smbsock_any_connect_send_next(req, state)) {
492                 return;
493         }
494         if (state->num_sent >= state->num_addrs) {
495                 return;
496         }
497         subreq = tevent_wakeup_send(state, state->ev,
498                                     tevent_timeval_set(0, 10000));
499         if (tevent_req_nomem(subreq, req)) {
500                 return;
501         }
502         tevent_req_set_callback(subreq, smbsock_any_connect_trynext, req);
503 }
504
505 static bool smbsock_any_connect_send_next(
506         struct tevent_req *req, struct smbsock_any_connect_state *state)
507 {
508         struct tevent_req *subreq;
509
510         if (state->num_sent >= state->num_addrs) {
511                 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
512                 return false;
513         }
514         subreq = smbsock_connect_send(
515                 state->requests, state->ev, &state->addrs[state->num_sent],
516                 state->port,
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)) {
526                 return false;
527         }
528         tevent_req_set_callback(subreq, smbsock_any_connect_connected, req);
529
530         state->requests[state->num_sent] = subreq;
531         state->num_sent += 1;
532
533         return true;
534 }
535
536 static void smbsock_any_connect_connected(struct tevent_req *subreq)
537 {
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);
542         NTSTATUS status;
543         int fd;
544         uint16_t chosen_port;
545         size_t i;
546         size_t chosen_index = 0;
547
548         for (i=0; i<state->num_sent; i++) {
549                 if (state->requests[i] == subreq) {
550                         chosen_index = i;
551                         break;
552                 }
553         }
554         if (i == state->num_sent) {
555                 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
556                 return;
557         }
558
559         status = smbsock_connect_recv(subreq, &fd, &chosen_port);
560
561         TALLOC_FREE(subreq);
562         state->requests[chosen_index] = NULL;
563
564         if (NT_STATUS_IS_OK(status)) {
565                 /*
566                  * This will kill all the other requests
567                  */
568                 TALLOC_FREE(state->requests);
569                 state->fd = fd;
570                 state->chosen_port = chosen_port;
571                 state->chosen_index = chosen_index;
572                 tevent_req_done(req);
573                 return;
574         }
575
576         state->num_received += 1;
577         if (state->num_received <= state->num_addrs) {
578                 /*
579                  * More addrs pending, wait for the others
580                  */
581                 return;
582         }
583
584         /*
585          * This is the last response, none succeeded.
586          */
587         tevent_req_nterror(req, status);
588         return;
589 }
590
591 NTSTATUS smbsock_any_connect_recv(struct tevent_req *req, int *pfd,
592                                   size_t *chosen_index,
593                                   uint16_t *chosen_port)
594 {
595         struct smbsock_any_connect_state *state = tevent_req_data(
596                 req, struct smbsock_any_connect_state);
597         NTSTATUS status;
598
599         if (tevent_req_is_nterror(req, &status)) {
600                 return status;
601         }
602         *pfd = state->fd;
603         if (chosen_index != NULL) {
604                 *chosen_index = state->chosen_index;
605         }
606         if (chosen_port != NULL) {
607                 *chosen_port = state->chosen_port;
608         }
609         return NT_STATUS_OK;
610 }
611
612 NTSTATUS smbsock_any_connect(const struct sockaddr_storage *addrs,
613                              const char **called_names,
614                              int *called_types,
615                              const char **calling_names,
616                              int *calling_types,
617                              size_t num_addrs,
618                              uint16_t port,
619                              int sec_timeout,
620                              int *pfd, size_t *chosen_index,
621                              uint16_t *chosen_port)
622 {
623         TALLOC_CTX *frame = talloc_stackframe();
624         struct event_context *ev;
625         struct tevent_req *req;
626         NTSTATUS status = NT_STATUS_NO_MEMORY;
627
628         ev = event_context_init(frame);
629         if (ev == NULL) {
630                 goto fail;
631         }
632         req = smbsock_any_connect_send(frame, ev, addrs,
633                                        called_names, called_types,
634                                        calling_names, calling_types,
635                                        num_addrs, port);
636         if (req == NULL) {
637                 goto fail;
638         }
639         if ((sec_timeout != 0) &&
640             !tevent_req_set_endtime(
641                     req, ev, timeval_current_ofs(sec_timeout, 0))) {
642                 goto fail;
643         }
644         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
645                 goto fail;
646         }
647         status = smbsock_any_connect_recv(req, pfd, chosen_index, chosen_port);
648  fail:
649         TALLOC_FREE(frame);
650         return status;
651 }