bss_acpt.c 16 KB

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