bss_acpt.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /*
  2. * Copyright 1995-2020 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. #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. SYSerr(SYS_F_SOCKET, get_last_socket_error());
  204. ERR_add_error_data(4,
  205. "hostname=", c->param_addr,
  206. " service=", c->param_serv);
  207. BIOerr(BIO_F_ACPT_STATE, BIO_R_UNABLE_TO_CREATE_SOCKET);
  208. goto exit_loop;
  209. }
  210. c->accept_sock = s;
  211. b->num = s;
  212. c->state = ACPT_S_LISTEN;
  213. s = -1;
  214. break;
  215. case ACPT_S_LISTEN:
  216. {
  217. if (!BIO_listen(c->accept_sock,
  218. BIO_ADDRINFO_address(c->addr_iter),
  219. c->bind_mode)) {
  220. BIO_closesocket(c->accept_sock);
  221. goto exit_loop;
  222. }
  223. }
  224. {
  225. union BIO_sock_info_u info;
  226. info.addr = &c->cache_accepting_addr;
  227. if (!BIO_sock_info(c->accept_sock, BIO_SOCK_INFO_ADDRESS,
  228. &info)) {
  229. BIO_closesocket(c->accept_sock);
  230. goto exit_loop;
  231. }
  232. }
  233. c->cache_accepting_name =
  234. BIO_ADDR_hostname_string(&c->cache_accepting_addr, 1);
  235. c->cache_accepting_serv =
  236. BIO_ADDR_service_string(&c->cache_accepting_addr, 1);
  237. c->state = ACPT_S_ACCEPT;
  238. s = -1;
  239. ret = 1;
  240. goto end;
  241. case ACPT_S_ACCEPT:
  242. if (b->next_bio != NULL) {
  243. c->state = ACPT_S_OK;
  244. break;
  245. }
  246. BIO_clear_retry_flags(b);
  247. b->retry_reason = 0;
  248. OPENSSL_free(c->cache_peer_name);
  249. c->cache_peer_name = NULL;
  250. OPENSSL_free(c->cache_peer_serv);
  251. c->cache_peer_serv = NULL;
  252. s = BIO_accept_ex(c->accept_sock, &c->cache_peer_addr,
  253. c->accepted_mode);
  254. /* If the returned socket is invalid, this might still be
  255. * retryable
  256. */
  257. if (s < 0) {
  258. if (BIO_sock_should_retry(s)) {
  259. BIO_set_retry_special(b);
  260. b->retry_reason = BIO_RR_ACCEPT;
  261. goto end;
  262. }
  263. }
  264. /* If it wasn't retryable, we fail */
  265. if (s < 0) {
  266. ret = s;
  267. goto exit_loop;
  268. }
  269. bio = BIO_new_socket(s, BIO_CLOSE);
  270. if (bio == NULL)
  271. goto exit_loop;
  272. BIO_set_callback(bio, BIO_get_callback(b));
  273. BIO_set_callback_arg(bio, BIO_get_callback_arg(b));
  274. /*
  275. * If the accept BIO has an bio_chain, we dup it and put the new
  276. * socket at the end.
  277. */
  278. if (c->bio_chain != NULL) {
  279. if ((dbio = BIO_dup_chain(c->bio_chain)) == NULL)
  280. goto exit_loop;
  281. if (!BIO_push(dbio, bio))
  282. goto exit_loop;
  283. bio = dbio;
  284. }
  285. if (BIO_push(b, bio) == NULL)
  286. goto exit_loop;
  287. c->cache_peer_name =
  288. BIO_ADDR_hostname_string(&c->cache_peer_addr, 1);
  289. c->cache_peer_serv =
  290. BIO_ADDR_service_string(&c->cache_peer_addr, 1);
  291. c->state = ACPT_S_OK;
  292. bio = NULL;
  293. ret = 1;
  294. goto end;
  295. case ACPT_S_OK:
  296. if (b->next_bio == NULL) {
  297. c->state = ACPT_S_ACCEPT;
  298. break;
  299. }
  300. ret = 1;
  301. goto end;
  302. default:
  303. ret = 0;
  304. goto end;
  305. }
  306. }
  307. exit_loop:
  308. if (bio != NULL)
  309. BIO_free(bio);
  310. else if (s >= 0)
  311. BIO_closesocket(s);
  312. end:
  313. return ret;
  314. }
  315. static int acpt_read(BIO *b, char *out, int outl)
  316. {
  317. int ret = 0;
  318. BIO_ACCEPT *data;
  319. BIO_clear_retry_flags(b);
  320. data = (BIO_ACCEPT *)b->ptr;
  321. while (b->next_bio == NULL) {
  322. ret = acpt_state(b, data);
  323. if (ret <= 0)
  324. return ret;
  325. }
  326. ret = BIO_read(b->next_bio, out, outl);
  327. BIO_copy_next_retry(b);
  328. return ret;
  329. }
  330. static int acpt_write(BIO *b, const char *in, int inl)
  331. {
  332. int ret;
  333. BIO_ACCEPT *data;
  334. BIO_clear_retry_flags(b);
  335. data = (BIO_ACCEPT *)b->ptr;
  336. while (b->next_bio == NULL) {
  337. ret = acpt_state(b, data);
  338. if (ret <= 0)
  339. return ret;
  340. }
  341. ret = BIO_write(b->next_bio, in, inl);
  342. BIO_copy_next_retry(b);
  343. return ret;
  344. }
  345. static long acpt_ctrl(BIO *b, int cmd, long num, void *ptr)
  346. {
  347. int *ip;
  348. long ret = 1;
  349. BIO_ACCEPT *data;
  350. char **pp;
  351. data = (BIO_ACCEPT *)b->ptr;
  352. switch (cmd) {
  353. case BIO_CTRL_RESET:
  354. ret = 0;
  355. data->state = ACPT_S_BEFORE;
  356. acpt_close_socket(b);
  357. BIO_ADDRINFO_free(data->addr_first);
  358. data->addr_first = NULL;
  359. b->flags = 0;
  360. break;
  361. case BIO_C_DO_STATE_MACHINE:
  362. /* use this one to start the connection */
  363. ret = (long)acpt_state(b, data);
  364. break;
  365. case BIO_C_SET_ACCEPT:
  366. if (ptr != NULL) {
  367. if (num == 0) {
  368. char *hold_serv = data->param_serv;
  369. /* We affect the hostname regardless. However, the input
  370. * string might contain a host:service spec, so we must
  371. * parse it, which might or might not affect the service
  372. */
  373. OPENSSL_free(data->param_addr);
  374. data->param_addr = NULL;
  375. ret = BIO_parse_hostserv(ptr,
  376. &data->param_addr,
  377. &data->param_serv,
  378. BIO_PARSE_PRIO_SERV);
  379. if (hold_serv != data->param_serv)
  380. OPENSSL_free(hold_serv);
  381. b->init = 1;
  382. } else if (num == 1) {
  383. OPENSSL_free(data->param_serv);
  384. if ((data->param_serv = OPENSSL_strdup(ptr)) == NULL)
  385. ret = 0;
  386. else
  387. b->init = 1;
  388. } else if (num == 2) {
  389. data->bind_mode |= BIO_SOCK_NONBLOCK;
  390. } else if (num == 3) {
  391. BIO_free(data->bio_chain);
  392. data->bio_chain = (BIO *)ptr;
  393. } else if (num == 4) {
  394. data->accept_family = *(int *)ptr;
  395. }
  396. } else {
  397. if (num == 2) {
  398. data->bind_mode &= ~BIO_SOCK_NONBLOCK;
  399. }
  400. }
  401. break;
  402. case BIO_C_SET_NBIO:
  403. if (num != 0)
  404. data->accepted_mode |= BIO_SOCK_NONBLOCK;
  405. else
  406. data->accepted_mode &= ~BIO_SOCK_NONBLOCK;
  407. break;
  408. case BIO_C_SET_FD:
  409. b->num = *((int *)ptr);
  410. data->accept_sock = b->num;
  411. data->state = ACPT_S_ACCEPT;
  412. b->shutdown = (int)num;
  413. b->init = 1;
  414. break;
  415. case BIO_C_GET_FD:
  416. if (b->init) {
  417. ip = (int *)ptr;
  418. if (ip != NULL)
  419. *ip = data->accept_sock;
  420. ret = data->accept_sock;
  421. } else
  422. ret = -1;
  423. break;
  424. case BIO_C_GET_ACCEPT:
  425. if (b->init) {
  426. if (num == 0 && ptr != NULL) {
  427. pp = (char **)ptr;
  428. *pp = data->cache_accepting_name;
  429. } else if (num == 1 && ptr != NULL) {
  430. pp = (char **)ptr;
  431. *pp = data->cache_accepting_serv;
  432. } else if (num == 2 && ptr != NULL) {
  433. pp = (char **)ptr;
  434. *pp = data->cache_peer_name;
  435. } else if (num == 3 && ptr != NULL) {
  436. pp = (char **)ptr;
  437. *pp = data->cache_peer_serv;
  438. } else if (num == 4) {
  439. switch (BIO_ADDRINFO_family(data->addr_iter)) {
  440. #ifdef AF_INET6
  441. case AF_INET6:
  442. ret = BIO_FAMILY_IPV6;
  443. break;
  444. #endif
  445. case AF_INET:
  446. ret = BIO_FAMILY_IPV4;
  447. break;
  448. case 0:
  449. ret = data->accept_family;
  450. break;
  451. default:
  452. ret = -1;
  453. break;
  454. }
  455. } else
  456. ret = -1;
  457. } else
  458. ret = -1;
  459. break;
  460. case BIO_CTRL_GET_CLOSE:
  461. ret = b->shutdown;
  462. break;
  463. case BIO_CTRL_SET_CLOSE:
  464. b->shutdown = (int)num;
  465. break;
  466. case BIO_CTRL_PENDING:
  467. case BIO_CTRL_WPENDING:
  468. ret = 0;
  469. break;
  470. case BIO_CTRL_FLUSH:
  471. break;
  472. case BIO_C_SET_BIND_MODE:
  473. data->bind_mode = (int)num;
  474. break;
  475. case BIO_C_GET_BIND_MODE:
  476. ret = (long)data->bind_mode;
  477. break;
  478. case BIO_CTRL_DUP:
  479. break;
  480. case BIO_CTRL_EOF:
  481. if (b->next_bio == NULL)
  482. ret = 0;
  483. else
  484. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  485. break;
  486. default:
  487. ret = 0;
  488. break;
  489. }
  490. return ret;
  491. }
  492. static int acpt_puts(BIO *bp, const char *str)
  493. {
  494. int n, ret;
  495. n = strlen(str);
  496. ret = acpt_write(bp, str, n);
  497. return ret;
  498. }
  499. BIO *BIO_new_accept(const char *str)
  500. {
  501. BIO *ret;
  502. ret = BIO_new(BIO_s_accept());
  503. if (ret == NULL)
  504. return NULL;
  505. if (BIO_set_accept_name(ret, str))
  506. return ret;
  507. BIO_free(ret);
  508. return NULL;
  509. }
  510. #endif