bss_acpt.c 16 KB

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