Update copyright year
[oweals/openssl.git] / crypto / bio / bss_acpt.c
1 /*
2  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <errno.h>
12 #include "bio_lcl.h"
13
14 #ifndef OPENSSL_NO_SOCK
15
16 typedef struct bio_accept_st {
17     int state;
18     int accept_family;
19     int bind_mode;     /* Socket mode for BIO_listen */
20     int accepted_mode; /* Socket mode for BIO_accept (set on accepted sock) */
21     char *param_addr;
22     char *param_serv;
23
24     int accept_sock;
25
26     BIO_ADDRINFO *addr_first;
27     const BIO_ADDRINFO *addr_iter;
28     BIO_ADDR cache_accepting_addr;   /* Useful if we asked for port 0 */
29     char *cache_accepting_name, *cache_accepting_serv;
30     BIO_ADDR cache_peer_addr;
31     char *cache_peer_name, *cache_peer_serv;
32
33     BIO *bio_chain;
34 } BIO_ACCEPT;
35
36 static int acpt_write(BIO *h, const char *buf, int num);
37 static int acpt_read(BIO *h, char *buf, int size);
38 static int acpt_puts(BIO *h, const char *str);
39 static long acpt_ctrl(BIO *h, int cmd, long arg1, void *arg2);
40 static int acpt_new(BIO *h);
41 static int acpt_free(BIO *data);
42 static int acpt_state(BIO *b, BIO_ACCEPT *c);
43 static void acpt_close_socket(BIO *data);
44 static BIO_ACCEPT *BIO_ACCEPT_new(void);
45 static void BIO_ACCEPT_free(BIO_ACCEPT *a);
46
47 # define ACPT_S_BEFORE                   1
48 # define ACPT_S_GET_ADDR                 2
49 # define ACPT_S_CREATE_SOCKET            3
50 # define ACPT_S_LISTEN                   4
51 # define ACPT_S_ACCEPT                   5
52 # define ACPT_S_OK                       6
53
54 static const BIO_METHOD methods_acceptp = {
55     BIO_TYPE_ACCEPT,
56     "socket accept",
57     acpt_write,
58     acpt_read,
59     acpt_puts,
60     NULL,                       /* connect_gets,         */
61     acpt_ctrl,
62     acpt_new,
63     acpt_free,
64     NULL,                       /* connect_callback_ctrl */
65 };
66
67 const BIO_METHOD *BIO_s_accept(void)
68 {
69     return (&methods_acceptp);
70 }
71
72 static int acpt_new(BIO *bi)
73 {
74     BIO_ACCEPT *ba;
75
76     bi->init = 0;
77     bi->num = (int)INVALID_SOCKET;
78     bi->flags = 0;
79     if ((ba = BIO_ACCEPT_new()) == NULL)
80         return (0);
81     bi->ptr = (char *)ba;
82     ba->state = ACPT_S_BEFORE;
83     bi->shutdown = 1;
84     return (1);
85 }
86
87 static BIO_ACCEPT *BIO_ACCEPT_new(void)
88 {
89     BIO_ACCEPT *ret;
90
91     if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
92         return (NULL);
93     ret->accept_family = BIO_FAMILY_IPANY;
94     ret->accept_sock = (int)INVALID_SOCKET;
95     return (ret);
96 }
97
98 static void BIO_ACCEPT_free(BIO_ACCEPT *a)
99 {
100     if (a == NULL)
101         return;
102
103     OPENSSL_free(a->param_addr);
104     OPENSSL_free(a->param_serv);
105     BIO_ADDRINFO_free(a->addr_first);
106     OPENSSL_free(a->cache_accepting_name);
107     OPENSSL_free(a->cache_accepting_serv);
108     OPENSSL_free(a->cache_peer_name);
109     OPENSSL_free(a->cache_peer_serv);
110     BIO_free(a->bio_chain);
111     OPENSSL_free(a);
112 }
113
114 static void acpt_close_socket(BIO *bio)
115 {
116     BIO_ACCEPT *c;
117
118     c = (BIO_ACCEPT *)bio->ptr;
119     if (c->accept_sock != (int)INVALID_SOCKET) {
120         shutdown(c->accept_sock, 2);
121         closesocket(c->accept_sock);
122         c->accept_sock = (int)INVALID_SOCKET;
123         bio->num = (int)INVALID_SOCKET;
124     }
125 }
126
127 static int acpt_free(BIO *a)
128 {
129     BIO_ACCEPT *data;
130
131     if (a == NULL)
132         return (0);
133     data = (BIO_ACCEPT *)a->ptr;
134
135     if (a->shutdown) {
136         acpt_close_socket(a);
137         BIO_ACCEPT_free(data);
138         a->ptr = NULL;
139         a->flags = 0;
140         a->init = 0;
141     }
142     return (1);
143 }
144
145 static int acpt_state(BIO *b, BIO_ACCEPT *c)
146 {
147     BIO *bio = NULL, *dbio;
148     int s = -1, ret = -1;
149
150     for (;;) {
151         switch (c->state) {
152         case ACPT_S_BEFORE:
153             if (c->param_addr == NULL && c->param_serv == NULL) {
154                 BIOerr(BIO_F_ACPT_STATE, BIO_R_NO_ACCEPT_ADDR_OR_SERVICE_SPECIFIED);
155                 ERR_add_error_data(4,
156                                    "hostname=", c->param_addr,
157                                    " service=", c->param_serv);
158                 goto exit_loop;
159             }
160
161             /* Because we're starting a new bind, any cached name and serv
162              * are now obsolete and need to be cleaned out.
163              * QUESTION: should this be done in acpt_close_socket() instead?
164              */
165             OPENSSL_free(c->cache_accepting_name);
166             c->cache_accepting_name = NULL;
167             OPENSSL_free(c->cache_accepting_serv);
168             c->cache_accepting_serv = NULL;
169             OPENSSL_free(c->cache_peer_name);
170             c->cache_peer_name = NULL;
171             OPENSSL_free(c->cache_peer_serv);
172             c->cache_peer_serv = NULL;
173
174             c->state = ACPT_S_GET_ADDR;
175             break;
176
177         case ACPT_S_GET_ADDR:
178             {
179                 int family = AF_UNSPEC;
180                 switch (c->accept_family) {
181                 case BIO_FAMILY_IPV6:
182                     if (1) { /* This is a trick we use to avoid bit rot.
183                               * at least the "else" part will always be
184                               * compiled.
185                               */
186 #ifdef AF_INET6
187                         family = AF_INET6;
188                     } else {
189 #endif
190                         BIOerr(BIO_F_ACPT_STATE, BIO_R_UNAVAILABLE_IP_FAMILY);
191                         goto exit_loop;
192                     }
193                     break;
194                 case BIO_FAMILY_IPV4:
195                     family = AF_INET;
196                     break;
197                 case BIO_FAMILY_IPANY:
198                     family = AF_UNSPEC;
199                     break;
200                 default:
201                     BIOerr(BIO_F_ACPT_STATE, BIO_R_UNSUPPORTED_IP_FAMILY);
202                     goto exit_loop;
203                 }
204                 if (BIO_lookup(c->param_addr, c->param_serv, BIO_LOOKUP_SERVER,
205                                family, SOCK_STREAM, &c->addr_first) == 0)
206                     goto exit_loop;
207             }
208             if (c->addr_first == NULL) {
209                 BIOerr(BIO_F_ACPT_STATE, BIO_R_LOOKUP_RETURNED_NOTHING);
210                 goto exit_loop;
211             }
212             /* We're currently not iterating, but set this as preparation
213              * for possible future development in that regard
214              */
215             c->addr_iter = c->addr_first;
216             c->state = ACPT_S_CREATE_SOCKET;
217             break;
218
219         case ACPT_S_CREATE_SOCKET:
220             ret = BIO_socket(BIO_ADDRINFO_family(c->addr_iter),
221                              BIO_ADDRINFO_socktype(c->addr_iter),
222                              BIO_ADDRINFO_protocol(c->addr_iter), 0);
223             if (ret == (int)INVALID_SOCKET) {
224                 SYSerr(SYS_F_SOCKET, get_last_socket_error());
225                 ERR_add_error_data(4,
226                                    "hostname=", c->param_addr,
227                                    " service=", c->param_serv);
228                 BIOerr(BIO_F_ACPT_STATE, BIO_R_UNABLE_TO_CREATE_SOCKET);
229                 goto exit_loop;
230             }
231             c->accept_sock = ret;
232             b->num = ret;
233             c->state = ACPT_S_LISTEN;
234             break;
235
236         case ACPT_S_LISTEN:
237             {
238                 if (!BIO_listen(c->accept_sock,
239                                 BIO_ADDRINFO_address(c->addr_iter),
240                                 c->bind_mode)) {
241                     BIO_closesocket(c->accept_sock);
242                     goto exit_loop;
243                 }
244             }
245
246             {
247                 union BIO_sock_info_u info;
248
249                 info.addr = &c->cache_accepting_addr;
250                 if (!BIO_sock_info(c->accept_sock, BIO_SOCK_INFO_ADDRESS,
251                                    &info)) {
252                     BIO_closesocket(c->accept_sock);
253                     goto exit_loop;
254                 }
255             }
256
257             c->cache_accepting_name =
258                 BIO_ADDR_hostname_string(&c->cache_accepting_addr, 1);
259             c->cache_accepting_serv =
260                 BIO_ADDR_service_string(&c->cache_accepting_addr, 1);
261             c->state = ACPT_S_ACCEPT;
262             s = -1;
263             ret = 1;
264             goto end;
265
266         case ACPT_S_ACCEPT:
267             if (b->next_bio != NULL) {
268                 c->state = ACPT_S_OK;
269                 break;
270             }
271             BIO_clear_retry_flags(b);
272             b->retry_reason = 0;
273
274             OPENSSL_free(c->cache_peer_name);
275             c->cache_peer_name = NULL;
276             OPENSSL_free(c->cache_peer_serv);
277             c->cache_peer_serv = NULL;
278
279             s = BIO_accept_ex(c->accept_sock, &c->cache_peer_addr,
280                               c->accepted_mode);
281
282             /* If the returned socket is invalid, this might still be
283              * retryable
284              */
285             if (s < 0) {
286                 if (BIO_sock_should_retry(s)) {
287                     BIO_set_retry_special(b);
288                     b->retry_reason = BIO_RR_ACCEPT;
289                     goto end;
290                 }
291             }
292
293             /* If it wasn't retryable, we fail */
294             if (s < 0) {
295                 ret = s;
296                 goto exit_loop;
297             }
298
299             bio = BIO_new_socket(s, BIO_CLOSE);
300             if (bio == NULL)
301                 goto exit_loop;
302
303             BIO_set_callback(bio, BIO_get_callback(b));
304             BIO_set_callback_arg(bio, BIO_get_callback_arg(b));
305
306             /*
307              * If the accept BIO has an bio_chain, we dup it and put the new
308              * socket at the end.
309              */
310             if (c->bio_chain != NULL) {
311                 if ((dbio = BIO_dup_chain(c->bio_chain)) == NULL)
312                     goto exit_loop;
313                 if (!BIO_push(dbio, bio))
314                     goto exit_loop;
315                 bio = dbio;
316             }
317             if (BIO_push(b, bio) == NULL)
318                 goto exit_loop;
319
320             c->cache_peer_name =
321                 BIO_ADDR_hostname_string(&c->cache_peer_addr, 1);
322             c->cache_peer_serv =
323                 BIO_ADDR_service_string(&c->cache_peer_addr, 1);
324             c->state = ACPT_S_OK;
325             bio = NULL;
326             ret = 1;
327             goto end;
328
329         case ACPT_S_OK:
330             if (b->next_bio == NULL) {
331                 c->state = ACPT_S_ACCEPT;
332                 break;
333             }
334             ret = 1;
335             goto end;
336
337         default:
338             ret = 0;
339             goto end;
340         }
341     }
342
343   exit_loop:
344     if (bio != NULL)
345         BIO_free(bio);
346     else if (s >= 0)
347         BIO_closesocket(s);
348   end:
349     return ret;
350 }
351
352 static int acpt_read(BIO *b, char *out, int outl)
353 {
354     int ret = 0;
355     BIO_ACCEPT *data;
356
357     BIO_clear_retry_flags(b);
358     data = (BIO_ACCEPT *)b->ptr;
359
360     while (b->next_bio == NULL) {
361         ret = acpt_state(b, data);
362         if (ret <= 0)
363             return (ret);
364     }
365
366     ret = BIO_read(b->next_bio, out, outl);
367     BIO_copy_next_retry(b);
368     return (ret);
369 }
370
371 static int acpt_write(BIO *b, const char *in, int inl)
372 {
373     int ret;
374     BIO_ACCEPT *data;
375
376     BIO_clear_retry_flags(b);
377     data = (BIO_ACCEPT *)b->ptr;
378
379     while (b->next_bio == NULL) {
380         ret = acpt_state(b, data);
381         if (ret <= 0)
382             return (ret);
383     }
384
385     ret = BIO_write(b->next_bio, in, inl);
386     BIO_copy_next_retry(b);
387     return (ret);
388 }
389
390 static long acpt_ctrl(BIO *b, int cmd, long num, void *ptr)
391 {
392     int *ip;
393     long ret = 1;
394     BIO_ACCEPT *data;
395     char **pp;
396
397     data = (BIO_ACCEPT *)b->ptr;
398
399     switch (cmd) {
400     case BIO_CTRL_RESET:
401         ret = 0;
402         data->state = ACPT_S_BEFORE;
403         acpt_close_socket(b);
404         BIO_ADDRINFO_free(data->addr_first);
405         data->addr_first = NULL;
406         b->flags = 0;
407         break;
408     case BIO_C_DO_STATE_MACHINE:
409         /* use this one to start the connection */
410         ret = (long)acpt_state(b, data);
411         break;
412     case BIO_C_SET_ACCEPT:
413         if (ptr != NULL) {
414             if (num == 0) {
415                 char *hold_serv = data->param_serv;
416                 /* We affect the hostname regardless.  However, the input
417                  * string might contain a host:service spec, so we must
418                  * parse it, which might or might not affect the service
419                  */
420                 OPENSSL_free(data->param_addr);
421                 data->param_addr = NULL;
422                 ret = BIO_parse_hostserv(ptr,
423                                          &data->param_addr,
424                                          &data->param_serv,
425                                          BIO_PARSE_PRIO_SERV);
426                 if (hold_serv != data->param_serv)
427                     OPENSSL_free(hold_serv);
428                 b->init = 1;
429             } else if (num == 1) {
430                 OPENSSL_free(data->param_serv);
431                 data->param_serv = BUF_strdup(ptr);
432                 b->init = 1;
433             } else if (num == 2) {
434                 data->bind_mode |= BIO_SOCK_NONBLOCK;
435             } else if (num == 3) {
436                 BIO_free(data->bio_chain);
437                 data->bio_chain = (BIO *)ptr;
438             } else if (num == 4) {
439                 data->accept_family = *(int *)ptr;
440             }
441         } else {
442             if (num == 2) {
443                 data->bind_mode &= ~BIO_SOCK_NONBLOCK;
444             }
445         }
446         break;
447     case BIO_C_SET_NBIO:
448         if (num != 0)
449             data->accepted_mode |= BIO_SOCK_NONBLOCK;
450         else
451             data->accepted_mode &= ~BIO_SOCK_NONBLOCK;
452         break;
453     case BIO_C_SET_FD:
454         b->init = 1;
455         b->num = *((int *)ptr);
456         data->accept_sock = b->num;
457         data->state = ACPT_S_ACCEPT;
458         b->shutdown = (int)num;
459         b->init = 1;
460         break;
461     case BIO_C_GET_FD:
462         if (b->init) {
463             ip = (int *)ptr;
464             if (ip != NULL)
465                 *ip = data->accept_sock;
466             ret = data->accept_sock;
467         } else
468             ret = -1;
469         break;
470     case BIO_C_GET_ACCEPT:
471         if (b->init) {
472             if (num == 0 && ptr != NULL) {
473                 pp = (char **)ptr;
474                 *pp = data->cache_accepting_name;
475             } else if (num == 1 && ptr != NULL) {
476                 pp = (char **)ptr;
477                 *pp = data->cache_accepting_serv;
478             } else if (num == 2 && ptr != NULL) {
479                 pp = (char **)ptr;
480                 *pp = data->cache_peer_name;
481             } else if (num == 3 && ptr != NULL) {
482                 pp = (char **)ptr;
483                 *pp = data->cache_peer_serv;
484             } else if (num == 4) {
485                 switch (BIO_ADDRINFO_family(data->addr_iter)) {
486 #ifdef AF_INET6
487                 case AF_INET6:
488                     ret = BIO_FAMILY_IPV6;
489                     break;
490 #endif
491                 case AF_INET:
492                     ret = BIO_FAMILY_IPV4;
493                     break;
494                 case 0:
495                     ret = data->accept_family;
496                     break;
497                 default:
498                     ret = -1;
499                     break;
500                 }
501             } else
502                 ret = -1;
503         } else
504             ret = -1;
505         break;
506     case BIO_CTRL_GET_CLOSE:
507         ret = b->shutdown;
508         break;
509     case BIO_CTRL_SET_CLOSE:
510         b->shutdown = (int)num;
511         break;
512     case BIO_CTRL_PENDING:
513     case BIO_CTRL_WPENDING:
514         ret = 0;
515         break;
516     case BIO_CTRL_FLUSH:
517         break;
518     case BIO_C_SET_BIND_MODE:
519         data->bind_mode = (int)num;
520         break;
521     case BIO_C_GET_BIND_MODE:
522         ret = (long)data->bind_mode;
523         break;
524     case BIO_CTRL_DUP:
525 /*-     dbio=(BIO *)ptr;
526         if (data->param_port) EAY EAY
527                 BIO_set_port(dbio,data->param_port);
528         if (data->param_hostname)
529                 BIO_set_hostname(dbio,data->param_hostname);
530         BIO_set_nbio(dbio,data->nbio); */
531         break;
532
533     default:
534         ret = 0;
535         break;
536     }
537     return (ret);
538 }
539
540 static int acpt_puts(BIO *bp, const char *str)
541 {
542     int n, ret;
543
544     n = strlen(str);
545     ret = acpt_write(bp, str, n);
546     return (ret);
547 }
548
549 BIO *BIO_new_accept(const char *str)
550 {
551     BIO *ret;
552
553     ret = BIO_new(BIO_s_accept());
554     if (ret == NULL)
555         return (NULL);
556     if (BIO_set_accept_name(ret, str))
557         return (ret);
558     BIO_free(ret);
559     return (NULL);
560 }
561
562 #endif