tunala.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. #if defined(NO_BUFFER) || defined(NO_IP) || defined(NO_OPENSSL)
  2. # error "Badness, NO_BUFFER, NO_IP or NO_OPENSSL is defined, turn them *off*"
  3. #endif
  4. /* Include our bits'n'pieces */
  5. #include "tunala.h"
  6. /********************************************/
  7. /* Our local types that specify our "world" */
  8. /********************************************/
  9. /*
  10. * These represent running "tunnels". Eg. if you wanted to do SSL in a
  11. * "message-passing" scanario, the "int" file-descriptors might be replaced
  12. * by thread or process IDs, and the "select" code might be replaced by
  13. * message handling code. Whatever.
  14. */
  15. typedef struct _tunala_item_t {
  16. /*
  17. * The underlying SSL state machine. This is a data-only processing unit
  18. * and we communicate with it by talking to its four "buffers".
  19. */
  20. state_machine_t sm;
  21. /*
  22. * The file-descriptors for the "dirty" (encrypted) side of the SSL
  23. * setup. In actuality, this is typically a socket and both values are
  24. * identical.
  25. */
  26. int dirty_read, dirty_send;
  27. /*
  28. * The file-descriptors for the "clean" (unencrypted) side of the SSL
  29. * setup. These could be stdin/stdout, a socket (both values the same),
  30. * or whatever you like.
  31. */
  32. int clean_read, clean_send;
  33. } tunala_item_t;
  34. /*
  35. * This structure is used as the data for running the main loop. Namely, in a
  36. * network format such as this, it is stuff for select() - but as pointed out,
  37. * when moving the real-world to somewhere else, this might be replaced by
  38. * something entirely different. It's basically the stuff that controls when
  39. * it's time to do some "work".
  40. */
  41. typedef struct _select_sets_t {
  42. int max; /* As required as the first argument to
  43. * select() */
  44. fd_set reads, sends, excepts; /* As passed to select() */
  45. } select_sets_t;
  46. typedef struct _tunala_selector_t {
  47. select_sets_t last_selected; /* Results of the last select() */
  48. select_sets_t next_select; /* What we'll next select on */
  49. } tunala_selector_t;
  50. /*
  51. * This structure is *everything*. We do it to avoid the use of globals so
  52. * that, for example, it would be easier to shift things around between
  53. * async-IO, thread-based, or multi-fork()ed (or combinations thereof).
  54. */
  55. typedef struct _tunala_world_t {
  56. /* The file-descriptor we "listen" on for new connections */
  57. int listen_fd;
  58. /* The array of tunnels */
  59. tunala_item_t *tunnels;
  60. /* the number of tunnels in use and allocated, respectively */
  61. unsigned int tunnels_used, tunnels_size;
  62. /* Our outside "loop" context stuff */
  63. tunala_selector_t selector;
  64. /*
  65. * Our SSL_CTX, which is configured as the SSL client or server and has
  66. * the various cert-settings and callbacks configured.
  67. */
  68. SSL_CTX *ssl_ctx;
  69. /*
  70. * Simple flag with complex logic :-) Indicates whether we're an SSL
  71. * server or an SSL client.
  72. */
  73. int server_mode;
  74. } tunala_world_t;
  75. /*****************************/
  76. /* Internal static functions */
  77. /*****************************/
  78. static SSL_CTX *initialise_ssl_ctx(int server_mode, const char *engine_id,
  79. const char *CAfile, const char *cert,
  80. const char *key, const char *dcert,
  81. const char *dkey, const char *cipher_list,
  82. const char *dh_file,
  83. const char *dh_special, int tmp_rsa,
  84. int ctx_options, int out_state,
  85. int out_verify, int verify_mode,
  86. unsigned int verify_depth);
  87. static void selector_init(tunala_selector_t * selector);
  88. static void selector_add_listener(tunala_selector_t * selector, int fd);
  89. static void selector_add_tunala(tunala_selector_t * selector,
  90. tunala_item_t * t);
  91. static int selector_select(tunala_selector_t * selector);
  92. /*
  93. * This returns -1 for error, 0 for no new connections, or 1 for success, in
  94. * which case *newfd is populated.
  95. */
  96. static int selector_get_listener(tunala_selector_t * selector, int fd,
  97. int *newfd);
  98. static int tunala_world_new_item(tunala_world_t * world, int fd,
  99. const char *ip, unsigned short port,
  100. int flipped);
  101. static void tunala_world_del_item(tunala_world_t * world, unsigned int idx);
  102. static int tunala_item_io(tunala_selector_t * selector, tunala_item_t * item);
  103. /*********************************************/
  104. /* MAIN FUNCTION (and its utility functions) */
  105. /*********************************************/
  106. static const char *def_proxyhost = "127.0.0.1:443";
  107. static const char *def_listenhost = "127.0.0.1:8080";
  108. static int def_max_tunnels = 50;
  109. static const char *def_cacert = NULL;
  110. static const char *def_cert = NULL;
  111. static const char *def_key = NULL;
  112. static const char *def_dcert = NULL;
  113. static const char *def_dkey = NULL;
  114. static const char *def_engine_id = NULL;
  115. static int def_server_mode = 0;
  116. static int def_flipped = 0;
  117. static const char *def_cipher_list = NULL;
  118. static const char *def_dh_file = NULL;
  119. static const char *def_dh_special = NULL;
  120. static int def_tmp_rsa = 1;
  121. static int def_ctx_options = 0;
  122. static int def_verify_mode = 0;
  123. static unsigned int def_verify_depth = 10;
  124. static int def_out_state = 0;
  125. static unsigned int def_out_verify = 0;
  126. static int def_out_totals = 0;
  127. static int def_out_conns = 0;
  128. static const char *helpstring =
  129. "\n'Tunala' (A tunneler with a New Zealand accent)\n"
  130. "Usage: tunala [options], where options are from;\n"
  131. " -listen [host:]<port> (default = 127.0.0.1:8080)\n"
  132. " -proxy <host>:<port> (default = 127.0.0.1:443)\n"
  133. " -maxtunnels <num> (default = 50)\n"
  134. " -cacert <path|NULL> (default = NULL)\n"
  135. " -cert <path|NULL> (default = NULL)\n"
  136. " -key <path|NULL> (default = whatever '-cert' is)\n"
  137. " -dcert <path|NULL> (usually for DSA, default = NULL)\n"
  138. " -dkey <path|NULL> (usually for DSA, default = whatever '-dcert' is)\n"
  139. " -engine <id|NULL> (default = NULL)\n"
  140. " -server <0|1> (default = 0, ie. an SSL client)\n"
  141. " -flipped <0|1> (makes SSL servers be network clients, and vice versa)\n"
  142. " -cipher <list> (specifies cipher list to use)\n"
  143. " -dh_file <path> (a PEM file containing DH parameters to use)\n"
  144. " -dh_special <NULL|generate|standard> (see below: def=NULL)\n"
  145. " -no_tmp_rsa (don't generate temporary RSA keys)\n"
  146. " -no_ssl2 (disable SSLv2)\n"
  147. " -no_ssl3 (disable SSLv3)\n"
  148. " -no_tls1 (disable TLSv1)\n"
  149. " -v_peer (verify the peer certificate)\n"
  150. " -v_strict (do not continue if peer doesn't authenticate)\n"
  151. " -v_once (no verification in renegotiates)\n"
  152. " -v_depth <num> (limit certificate chain depth, default = 10)\n"
  153. " -out_conns (prints client connections and disconnections)\n"
  154. " -out_state (prints SSL handshake states)\n"
  155. " -out_verify <0|1|2|3> (prints certificate verification states: def=1)\n"
  156. " -out_totals (prints out byte-totals when a tunnel closes)\n"
  157. " -<h|help|?> (displays this help screen)\n"
  158. "Notes:\n"
  159. "(1) It is recommended to specify a cert+key when operating as an SSL server.\n"
  160. " If you only specify '-cert', the same file must contain a matching\n"
  161. " private key.\n"
  162. "(2) Either dh_file or dh_special can be used to specify where DH parameters\n"
  163. " will be obtained from (or '-dh_special NULL' for the default choice) but\n"
  164. " you cannot specify both. For dh_special, 'generate' will create new DH\n"
  165. " parameters on startup, and 'standard' will use embedded parameters\n"
  166. " instead.\n"
  167. "(3) Normally an ssl client connects to an ssl server - so that an 'ssl client\n"
  168. " tunala' listens for 'clean' client connections and proxies ssl, and an\n"
  169. " 'ssl server tunala' listens for ssl connections and proxies 'clean'. With\n"
  170. " '-flipped 1', this behaviour is reversed so that an 'ssl server tunala'\n"
  171. " listens for clean client connections and proxies ssl (but participating\n"
  172. " as an ssl *server* in the SSL/TLS protocol), and an 'ssl client tunala'\n"
  173. " listens for ssl connections (participating as an ssl *client* in the\n"
  174. " SSL/TLS protocol) and proxies 'clean' to the end destination. This can\n"
  175. " be useful for allowing network access to 'servers' where only the server\n"
  176. " needs to authenticate the client (ie. the other way is not required).\n"
  177. " Even with client and server authentication, this 'technique' mitigates\n"
  178. " some DoS (denial-of-service) potential as it will be the network client\n"
  179. " having to perform the first private key operation rather than the other\n"
  180. " way round.\n"
  181. "(4) The 'technique' used by setting '-flipped 1' is probably compatible with\n"
  182. " absolutely nothing except another complimentary instance of 'tunala'\n"
  183. " running with '-flipped 1'. :-)\n";
  184. /*
  185. * Default DH parameters for use with "-dh_special standard" ... stolen
  186. * striaght from s_server.
  187. */
  188. static unsigned char dh512_p[] = {
  189. 0xDA, 0x58, 0x3C, 0x16, 0xD9, 0x85, 0x22, 0x89, 0xD0, 0xE4, 0xAF, 0x75,
  190. 0x6F, 0x4C, 0xCA, 0x92, 0xDD, 0x4B, 0xE5, 0x33, 0xB8, 0x04, 0xFB, 0x0F,
  191. 0xED, 0x94, 0xEF, 0x9C, 0x8A, 0x44, 0x03, 0xED, 0x57, 0x46, 0x50, 0xD3,
  192. 0x69, 0x99, 0xDB, 0x29, 0xD7, 0x76, 0x27, 0x6B, 0xA2, 0xD3, 0xD4, 0x12,
  193. 0xE2, 0x18, 0xF4, 0xDD, 0x1E, 0x08, 0x4C, 0xF6, 0xD8, 0x00, 0x3E, 0x7C,
  194. 0x47, 0x74, 0xE8, 0x33,
  195. };
  196. static unsigned char dh512_g[] = {
  197. 0x02,
  198. };
  199. /*
  200. * And the function that parses the above "standard" parameters, again,
  201. * straight out of s_server.
  202. */
  203. static DH *get_dh512(void)
  204. {
  205. DH *dh = NULL;
  206. if ((dh = DH_new()) == NULL)
  207. return (NULL);
  208. dh->p = BN_bin2bn(dh512_p, sizeof(dh512_p), NULL);
  209. dh->g = BN_bin2bn(dh512_g, sizeof(dh512_g), NULL);
  210. if ((dh->p == NULL) || (dh->g == NULL))
  211. return (NULL);
  212. return (dh);
  213. }
  214. /* Various help/error messages used by main() */
  215. static int usage(const char *errstr, int isunknownarg)
  216. {
  217. if (isunknownarg)
  218. fprintf(stderr, "Error: unknown argument '%s'\n", errstr);
  219. else
  220. fprintf(stderr, "Error: %s\n", errstr);
  221. fprintf(stderr, "%s\n", helpstring);
  222. return 1;
  223. }
  224. static int err_str0(const char *str0)
  225. {
  226. fprintf(stderr, "%s\n", str0);
  227. return 1;
  228. }
  229. static int err_str1(const char *fmt, const char *str1)
  230. {
  231. fprintf(stderr, fmt, str1);
  232. fprintf(stderr, "\n");
  233. return 1;
  234. }
  235. static int parse_max_tunnels(const char *s, unsigned int *maxtunnels)
  236. {
  237. unsigned long l;
  238. if (!int_strtoul(s, &l) || (l < 1) || (l > 1024)) {
  239. fprintf(stderr, "Error, '%s' is an invalid value for "
  240. "maxtunnels\n", s);
  241. return 0;
  242. }
  243. *maxtunnels = (unsigned int)l;
  244. return 1;
  245. }
  246. static int parse_server_mode(const char *s, int *servermode)
  247. {
  248. unsigned long l;
  249. if (!int_strtoul(s, &l) || (l > 1)) {
  250. fprintf(stderr, "Error, '%s' is an invalid value for the "
  251. "server mode\n", s);
  252. return 0;
  253. }
  254. *servermode = (int)l;
  255. return 1;
  256. }
  257. static int parse_dh_special(const char *s, const char **dh_special)
  258. {
  259. if ((strcmp(s, "NULL") == 0) || (strcmp(s, "generate") == 0) ||
  260. (strcmp(s, "standard") == 0)) {
  261. *dh_special = s;
  262. return 1;
  263. }
  264. fprintf(stderr, "Error, '%s' is an invalid value for 'dh_special'\n", s);
  265. return 0;
  266. }
  267. static int parse_verify_level(const char *s, unsigned int *verify_level)
  268. {
  269. unsigned long l;
  270. if (!int_strtoul(s, &l) || (l > 3)) {
  271. fprintf(stderr, "Error, '%s' is an invalid value for "
  272. "out_verify\n", s);
  273. return 0;
  274. }
  275. *verify_level = (unsigned int)l;
  276. return 1;
  277. }
  278. static int parse_verify_depth(const char *s, unsigned int *verify_depth)
  279. {
  280. unsigned long l;
  281. if (!int_strtoul(s, &l) || (l < 1) || (l > 50)) {
  282. fprintf(stderr, "Error, '%s' is an invalid value for "
  283. "verify_depth\n", s);
  284. return 0;
  285. }
  286. *verify_depth = (unsigned int)l;
  287. return 1;
  288. }
  289. /* Some fprintf format strings used when tunnels close */
  290. static const char *io_stats_dirty =
  291. " SSL traffic; %8lu bytes in, %8lu bytes out\n";
  292. static const char *io_stats_clean =
  293. " clear traffic; %8lu bytes in, %8lu bytes out\n";
  294. int main(int argc, char *argv[])
  295. {
  296. unsigned int loop;
  297. int newfd;
  298. tunala_world_t world;
  299. tunala_item_t *t_item;
  300. const char *proxy_ip;
  301. unsigned short proxy_port;
  302. /* Overridables */
  303. const char *proxyhost = def_proxyhost;
  304. const char *listenhost = def_listenhost;
  305. unsigned int max_tunnels = def_max_tunnels;
  306. const char *cacert = def_cacert;
  307. const char *cert = def_cert;
  308. const char *key = def_key;
  309. const char *dcert = def_dcert;
  310. const char *dkey = def_dkey;
  311. const char *engine_id = def_engine_id;
  312. int server_mode = def_server_mode;
  313. int flipped = def_flipped;
  314. const char *cipher_list = def_cipher_list;
  315. const char *dh_file = def_dh_file;
  316. const char *dh_special = def_dh_special;
  317. int tmp_rsa = def_tmp_rsa;
  318. int ctx_options = def_ctx_options;
  319. int verify_mode = def_verify_mode;
  320. unsigned int verify_depth = def_verify_depth;
  321. int out_state = def_out_state;
  322. unsigned int out_verify = def_out_verify;
  323. int out_totals = def_out_totals;
  324. int out_conns = def_out_conns;
  325. /* Parse command-line arguments */
  326. next_arg:
  327. argc--;
  328. argv++;
  329. if (argc > 0) {
  330. if (strcmp(*argv, "-listen") == 0) {
  331. if (argc < 2)
  332. return usage("-listen requires an argument", 0);
  333. argc--;
  334. argv++;
  335. listenhost = *argv;
  336. goto next_arg;
  337. } else if (strcmp(*argv, "-proxy") == 0) {
  338. if (argc < 2)
  339. return usage("-proxy requires an argument", 0);
  340. argc--;
  341. argv++;
  342. proxyhost = *argv;
  343. goto next_arg;
  344. } else if (strcmp(*argv, "-maxtunnels") == 0) {
  345. if (argc < 2)
  346. return usage("-maxtunnels requires an argument", 0);
  347. argc--;
  348. argv++;
  349. if (!parse_max_tunnels(*argv, &max_tunnels))
  350. return 1;
  351. goto next_arg;
  352. } else if (strcmp(*argv, "-cacert") == 0) {
  353. if (argc < 2)
  354. return usage("-cacert requires an argument", 0);
  355. argc--;
  356. argv++;
  357. if (strcmp(*argv, "NULL") == 0)
  358. cacert = NULL;
  359. else
  360. cacert = *argv;
  361. goto next_arg;
  362. } else if (strcmp(*argv, "-cert") == 0) {
  363. if (argc < 2)
  364. return usage("-cert requires an argument", 0);
  365. argc--;
  366. argv++;
  367. if (strcmp(*argv, "NULL") == 0)
  368. cert = NULL;
  369. else
  370. cert = *argv;
  371. goto next_arg;
  372. } else if (strcmp(*argv, "-key") == 0) {
  373. if (argc < 2)
  374. return usage("-key requires an argument", 0);
  375. argc--;
  376. argv++;
  377. if (strcmp(*argv, "NULL") == 0)
  378. key = NULL;
  379. else
  380. key = *argv;
  381. goto next_arg;
  382. } else if (strcmp(*argv, "-dcert") == 0) {
  383. if (argc < 2)
  384. return usage("-dcert requires an argument", 0);
  385. argc--;
  386. argv++;
  387. if (strcmp(*argv, "NULL") == 0)
  388. dcert = NULL;
  389. else
  390. dcert = *argv;
  391. goto next_arg;
  392. } else if (strcmp(*argv, "-dkey") == 0) {
  393. if (argc < 2)
  394. return usage("-dkey requires an argument", 0);
  395. argc--;
  396. argv++;
  397. if (strcmp(*argv, "NULL") == 0)
  398. dkey = NULL;
  399. else
  400. dkey = *argv;
  401. goto next_arg;
  402. } else if (strcmp(*argv, "-engine") == 0) {
  403. if (argc < 2)
  404. return usage("-engine requires an argument", 0);
  405. argc--;
  406. argv++;
  407. engine_id = *argv;
  408. goto next_arg;
  409. } else if (strcmp(*argv, "-server") == 0) {
  410. if (argc < 2)
  411. return usage("-server requires an argument", 0);
  412. argc--;
  413. argv++;
  414. if (!parse_server_mode(*argv, &server_mode))
  415. return 1;
  416. goto next_arg;
  417. } else if (strcmp(*argv, "-flipped") == 0) {
  418. if (argc < 2)
  419. return usage("-flipped requires an argument", 0);
  420. argc--;
  421. argv++;
  422. if (!parse_server_mode(*argv, &flipped))
  423. return 1;
  424. goto next_arg;
  425. } else if (strcmp(*argv, "-cipher") == 0) {
  426. if (argc < 2)
  427. return usage("-cipher requires an argument", 0);
  428. argc--;
  429. argv++;
  430. cipher_list = *argv;
  431. goto next_arg;
  432. } else if (strcmp(*argv, "-dh_file") == 0) {
  433. if (argc < 2)
  434. return usage("-dh_file requires an argument", 0);
  435. if (dh_special)
  436. return usage("cannot mix -dh_file with " "-dh_special", 0);
  437. argc--;
  438. argv++;
  439. dh_file = *argv;
  440. goto next_arg;
  441. } else if (strcmp(*argv, "-dh_special") == 0) {
  442. if (argc < 2)
  443. return usage("-dh_special requires an argument", 0);
  444. if (dh_file)
  445. return usage("cannot mix -dh_file with " "-dh_special", 0);
  446. argc--;
  447. argv++;
  448. if (!parse_dh_special(*argv, &dh_special))
  449. return 1;
  450. goto next_arg;
  451. } else if (strcmp(*argv, "-no_tmp_rsa") == 0) {
  452. tmp_rsa = 0;
  453. goto next_arg;
  454. } else if (strcmp(*argv, "-no_ssl2") == 0) {
  455. ctx_options |= SSL_OP_NO_SSLv2;
  456. goto next_arg;
  457. } else if (strcmp(*argv, "-no_ssl3") == 0) {
  458. ctx_options |= SSL_OP_NO_SSLv3;
  459. goto next_arg;
  460. } else if (strcmp(*argv, "-no_tls1") == 0) {
  461. ctx_options |= SSL_OP_NO_TLSv1;
  462. goto next_arg;
  463. } else if (strcmp(*argv, "-v_peer") == 0) {
  464. verify_mode |= SSL_VERIFY_PEER;
  465. goto next_arg;
  466. } else if (strcmp(*argv, "-v_strict") == 0) {
  467. verify_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
  468. goto next_arg;
  469. } else if (strcmp(*argv, "-v_once") == 0) {
  470. verify_mode |= SSL_VERIFY_CLIENT_ONCE;
  471. goto next_arg;
  472. } else if (strcmp(*argv, "-v_depth") == 0) {
  473. if (argc < 2)
  474. return usage("-v_depth requires an argument", 0);
  475. argc--;
  476. argv++;
  477. if (!parse_verify_depth(*argv, &verify_depth))
  478. return 1;
  479. goto next_arg;
  480. } else if (strcmp(*argv, "-out_state") == 0) {
  481. out_state = 1;
  482. goto next_arg;
  483. } else if (strcmp(*argv, "-out_verify") == 0) {
  484. if (argc < 2)
  485. return usage("-out_verify requires an argument", 0);
  486. argc--;
  487. argv++;
  488. if (!parse_verify_level(*argv, &out_verify))
  489. return 1;
  490. goto next_arg;
  491. } else if (strcmp(*argv, "-out_totals") == 0) {
  492. out_totals = 1;
  493. goto next_arg;
  494. } else if (strcmp(*argv, "-out_conns") == 0) {
  495. out_conns = 1;
  496. goto next_arg;
  497. } else if ((strcmp(*argv, "-h") == 0) ||
  498. (strcmp(*argv, "-help") == 0) ||
  499. (strcmp(*argv, "-?") == 0)) {
  500. fprintf(stderr, "%s\n", helpstring);
  501. return 0;
  502. } else
  503. return usage(*argv, 1);
  504. }
  505. /* Run any sanity checks we want here */
  506. if (!cert && !dcert && server_mode)
  507. fprintf(stderr, "WARNING: you are running an SSL server without "
  508. "a certificate - this may not work!\n");
  509. /* Initialise network stuff */
  510. if (!ip_initialise())
  511. return err_str0("ip_initialise failed");
  512. /* Create the SSL_CTX */
  513. if ((world.ssl_ctx = initialise_ssl_ctx(server_mode, engine_id,
  514. cacert, cert, key, dcert, dkey,
  515. cipher_list, dh_file, dh_special,
  516. tmp_rsa, ctx_options, out_state,
  517. out_verify, verify_mode,
  518. verify_depth)) == NULL)
  519. return err_str1("initialise_ssl_ctx(engine_id=%s) failed",
  520. (engine_id == NULL) ? "NULL" : engine_id);
  521. if (engine_id)
  522. fprintf(stderr, "Info, engine '%s' initialised\n", engine_id);
  523. /* Create the listener */
  524. if ((world.listen_fd = ip_create_listener(listenhost)) == -1)
  525. return err_str1("ip_create_listener(%s) failed", listenhost);
  526. fprintf(stderr, "Info, listening on '%s'\n", listenhost);
  527. if (!ip_parse_address(proxyhost, &proxy_ip, &proxy_port, 0))
  528. return err_str1("ip_parse_address(%s) failed", proxyhost);
  529. fprintf(stderr, "Info, proxying to '%s' (%d.%d.%d.%d:%d)\n", proxyhost,
  530. (int)proxy_ip[0], (int)proxy_ip[1],
  531. (int)proxy_ip[2], (int)proxy_ip[3], (int)proxy_port);
  532. fprintf(stderr, "Info, set maxtunnels to %d\n", (int)max_tunnels);
  533. fprintf(stderr, "Info, set to operate as an SSL %s\n",
  534. (server_mode ? "server" : "client"));
  535. /* Initialise the rest of the stuff */
  536. world.tunnels_used = world.tunnels_size = 0;
  537. world.tunnels = NULL;
  538. world.server_mode = server_mode;
  539. selector_init(&world.selector);
  540. /* We're ready to loop */
  541. main_loop:
  542. /* Should we listen for *new* tunnels? */
  543. if (world.tunnels_used < max_tunnels)
  544. selector_add_listener(&world.selector, world.listen_fd);
  545. /* We should add in our existing tunnels */
  546. for (loop = 0; loop < world.tunnels_used; loop++)
  547. selector_add_tunala(&world.selector, world.tunnels + loop);
  548. /* Now do the select */
  549. switch (selector_select(&world.selector)) {
  550. case -1:
  551. if (errno != EINTR) {
  552. fprintf(stderr, "selector_select returned a " "badness error.\n");
  553. goto shouldnt_happen;
  554. }
  555. fprintf(stderr, "Warn, selector interrupted by a signal\n");
  556. goto main_loop;
  557. case 0:
  558. fprintf(stderr, "Warn, selector_select returned 0 - signal?" "?\n");
  559. goto main_loop;
  560. default:
  561. break;
  562. }
  563. /* Accept new connection if we should and can */
  564. if ((world.tunnels_used < max_tunnels)
  565. && (selector_get_listener(&world.selector, world.listen_fd, &newfd) ==
  566. 1)) {
  567. /* We have a new connection */
  568. if (!tunala_world_new_item(&world, newfd, proxy_ip,
  569. proxy_port, flipped))
  570. fprintf(stderr, "tunala_world_new_item failed\n");
  571. else if (out_conns)
  572. fprintf(stderr, "Info, new tunnel opened, now up to "
  573. "%d\n", world.tunnels_used);
  574. }
  575. /*
  576. * Give each tunnel its moment, note the while loop is because it makes
  577. * the logic easier than with "for" to deal with an array that may shift
  578. * because of deletes.
  579. */
  580. loop = 0;
  581. t_item = world.tunnels;
  582. while (loop < world.tunnels_used) {
  583. if (!tunala_item_io(&world.selector, t_item)) {
  584. /*
  585. * We're closing whether for reasons of an error or a natural
  586. * close. Don't increment loop or t_item because the next item is
  587. * moving to us!
  588. */
  589. if (!out_totals)
  590. goto skip_totals;
  591. fprintf(stderr, "Tunnel closing, traffic stats follow\n");
  592. /* Display the encrypted (over the network) stats */
  593. fprintf(stderr, io_stats_dirty,
  594. buffer_total_in(state_machine_get_buffer
  595. (&t_item->sm, SM_DIRTY_IN)),
  596. buffer_total_out(state_machine_get_buffer
  597. (&t_item->sm, SM_DIRTY_OUT)));
  598. /*
  599. * Display the local (tunnelled) stats. NB: Data we *receive* is
  600. * data sent *out* of the state_machine on its 'clean' side.
  601. * Hence the apparent back-to-front OUT/IN mixup here :-)
  602. */
  603. fprintf(stderr, io_stats_clean,
  604. buffer_total_out(state_machine_get_buffer
  605. (&t_item->sm, SM_CLEAN_OUT)),
  606. buffer_total_in(state_machine_get_buffer
  607. (&t_item->sm, SM_CLEAN_IN)));
  608. skip_totals:
  609. tunala_world_del_item(&world, loop);
  610. if (out_conns)
  611. fprintf(stderr, "Info, tunnel closed, down to %d\n",
  612. world.tunnels_used);
  613. } else {
  614. /* Move to the next item */
  615. loop++;
  616. t_item++;
  617. }
  618. }
  619. goto main_loop;
  620. /* Should never get here */
  621. shouldnt_happen:
  622. abort();
  623. return 1;
  624. }
  625. /****************/
  626. /* OpenSSL bits */
  627. /****************/
  628. static int ctx_set_cert(SSL_CTX *ctx, const char *cert, const char *key)
  629. {
  630. FILE *fp = NULL;
  631. X509 *x509 = NULL;
  632. EVP_PKEY *pkey = NULL;
  633. int toret = 0; /* Assume an error */
  634. /* cert */
  635. if (cert) {
  636. if ((fp = fopen(cert, "r")) == NULL) {
  637. fprintf(stderr, "Error opening cert file '%s'\n", cert);
  638. goto err;
  639. }
  640. if (!PEM_read_X509(fp, &x509, NULL, NULL)) {
  641. fprintf(stderr, "Error reading PEM cert from '%s'\n", cert);
  642. goto err;
  643. }
  644. if (!SSL_CTX_use_certificate(ctx, x509)) {
  645. fprintf(stderr, "Error, cert in '%s' can not be used\n", cert);
  646. goto err;
  647. }
  648. /* Clear the FILE* for reuse in the "key" code */
  649. fclose(fp);
  650. fp = NULL;
  651. fprintf(stderr, "Info, operating with cert in '%s'\n", cert);
  652. /*
  653. * If a cert was given without matching key, we assume the same file
  654. * contains the required key.
  655. */
  656. if (!key)
  657. key = cert;
  658. } else {
  659. if (key)
  660. fprintf(stderr, "Error, can't specify a key without a "
  661. "corresponding certificate\n");
  662. else
  663. fprintf(stderr, "Error, ctx_set_cert called with " "NULLs!\n");
  664. goto err;
  665. }
  666. /* key */
  667. if (key) {
  668. if ((fp = fopen(key, "r")) == NULL) {
  669. fprintf(stderr, "Error opening key file '%s'\n", key);
  670. goto err;
  671. }
  672. if (!PEM_read_PrivateKey(fp, &pkey, NULL, NULL)) {
  673. fprintf(stderr, "Error reading PEM key from '%s'\n", key);
  674. goto err;
  675. }
  676. if (!SSL_CTX_use_PrivateKey(ctx, pkey)) {
  677. fprintf(stderr, "Error, key in '%s' can not be used\n", key);
  678. goto err;
  679. }
  680. fprintf(stderr, "Info, operating with key in '%s'\n", key);
  681. } else
  682. fprintf(stderr, "Info, operating without a cert or key\n");
  683. /* Success */
  684. toret = 1;
  685. err:
  686. if (x509)
  687. X509_free(x509);
  688. if (pkey)
  689. EVP_PKEY_free(pkey);
  690. if (fp)
  691. fclose(fp);
  692. return toret;
  693. }
  694. static int ctx_set_dh(SSL_CTX *ctx, const char *dh_file,
  695. const char *dh_special)
  696. {
  697. DH *dh = NULL;
  698. FILE *fp = NULL;
  699. if (dh_special) {
  700. if (strcmp(dh_special, "NULL") == 0)
  701. return 1;
  702. if (strcmp(dh_special, "standard") == 0) {
  703. if ((dh = get_dh512()) == NULL) {
  704. fprintf(stderr, "Error, can't parse 'standard'"
  705. " DH parameters\n");
  706. return 0;
  707. }
  708. fprintf(stderr, "Info, using 'standard' DH parameters\n");
  709. goto do_it;
  710. }
  711. if (strcmp(dh_special, "generate") != 0)
  712. /*
  713. * This shouldn't happen - screening values is handled in main().
  714. */
  715. abort();
  716. fprintf(stderr, "Info, generating DH parameters ... ");
  717. fflush(stderr);
  718. if (!(dh = DH_new()) || !DH_generate_parameters_ex(dh, 512,
  719. DH_GENERATOR_5,
  720. NULL)) {
  721. fprintf(stderr, "error!\n");
  722. if (dh)
  723. DH_free(dh);
  724. return 0;
  725. }
  726. fprintf(stderr, "complete\n");
  727. goto do_it;
  728. }
  729. /* So, we're loading dh_file */
  730. if ((fp = fopen(dh_file, "r")) == NULL) {
  731. fprintf(stderr, "Error, couldn't open '%s' for DH parameters\n",
  732. dh_file);
  733. return 0;
  734. }
  735. dh = PEM_read_DHparams(fp, NULL, NULL, NULL);
  736. fclose(fp);
  737. if (dh == NULL) {
  738. fprintf(stderr, "Error, could not parse DH parameters from '%s'\n",
  739. dh_file);
  740. return 0;
  741. }
  742. fprintf(stderr, "Info, using DH parameters from file '%s'\n", dh_file);
  743. do_it:
  744. SSL_CTX_set_tmp_dh(ctx, dh);
  745. DH_free(dh);
  746. return 1;
  747. }
  748. static SSL_CTX *initialise_ssl_ctx(int server_mode, const char *engine_id,
  749. const char *CAfile, const char *cert,
  750. const char *key, const char *dcert,
  751. const char *dkey, const char *cipher_list,
  752. const char *dh_file,
  753. const char *dh_special, int tmp_rsa,
  754. int ctx_options, int out_state,
  755. int out_verify, int verify_mode,
  756. unsigned int verify_depth)
  757. {
  758. SSL_CTX *ctx = NULL, *ret = NULL;
  759. const SSL_METHOD *meth;
  760. ENGINE *e = NULL;
  761. OpenSSL_add_ssl_algorithms();
  762. SSL_load_error_strings();
  763. meth = (server_mode ? SSLv23_server_method() : SSLv23_client_method());
  764. if (meth == NULL)
  765. goto err;
  766. if (engine_id) {
  767. ENGINE_load_builtin_engines();
  768. if ((e = ENGINE_by_id(engine_id)) == NULL) {
  769. fprintf(stderr, "Error obtaining '%s' engine, openssl "
  770. "errors follow\n", engine_id);
  771. goto err;
  772. }
  773. if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) {
  774. fprintf(stderr, "Error assigning '%s' engine, openssl "
  775. "errors follow\n", engine_id);
  776. goto err;
  777. }
  778. ENGINE_free(e);
  779. }
  780. if ((ctx = SSL_CTX_new(meth)) == NULL)
  781. goto err;
  782. /* cacert */
  783. if (CAfile) {
  784. if (!X509_STORE_load_locations(SSL_CTX_get_cert_store(ctx),
  785. CAfile, NULL)) {
  786. fprintf(stderr, "Error loading CA cert(s) in '%s'\n", CAfile);
  787. goto err;
  788. }
  789. fprintf(stderr, "Info, operating with CA cert(s) in '%s'\n", CAfile);
  790. } else
  791. fprintf(stderr, "Info, operating without a CA cert(-list)\n");
  792. if (!SSL_CTX_set_default_verify_paths(ctx)) {
  793. fprintf(stderr, "Error setting default verify paths\n");
  794. goto err;
  795. }
  796. /* cert and key */
  797. if ((cert || key) && !ctx_set_cert(ctx, cert, key))
  798. goto err;
  799. /* dcert and dkey */
  800. if ((dcert || dkey) && !ctx_set_cert(ctx, dcert, dkey))
  801. goto err;
  802. /* temporary RSA key generation */
  803. if (tmp_rsa)
  804. SSL_CTX_set_tmp_rsa_callback(ctx, cb_generate_tmp_rsa);
  805. /* cipher_list */
  806. if (cipher_list) {
  807. if (!SSL_CTX_set_cipher_list(ctx, cipher_list)) {
  808. fprintf(stderr, "Error setting cipher list '%s'\n", cipher_list);
  809. goto err;
  810. }
  811. fprintf(stderr, "Info, set cipher list '%s'\n", cipher_list);
  812. } else
  813. fprintf(stderr, "Info, operating with default cipher list\n");
  814. /* dh_file & dh_special */
  815. if ((dh_file || dh_special) && !ctx_set_dh(ctx, dh_file, dh_special))
  816. goto err;
  817. /* ctx_options */
  818. SSL_CTX_set_options(ctx, ctx_options);
  819. /* out_state (output of SSL handshake states to screen). */
  820. if (out_state)
  821. cb_ssl_info_set_output(stderr);
  822. /* out_verify */
  823. if (out_verify > 0) {
  824. cb_ssl_verify_set_output(stderr);
  825. cb_ssl_verify_set_level(out_verify);
  826. }
  827. /* verify_depth */
  828. cb_ssl_verify_set_depth(verify_depth);
  829. /* Success! (includes setting verify_mode) */
  830. SSL_CTX_set_info_callback(ctx, cb_ssl_info);
  831. SSL_CTX_set_verify(ctx, verify_mode, cb_ssl_verify);
  832. ret = ctx;
  833. err:
  834. if (!ret) {
  835. ERR_print_errors_fp(stderr);
  836. if (ctx)
  837. SSL_CTX_free(ctx);
  838. }
  839. return ret;
  840. }
  841. /*****************/
  842. /* Selector bits */
  843. /*****************/
  844. static void selector_sets_init(select_sets_t * s)
  845. {
  846. s->max = 0;
  847. FD_ZERO(&s->reads);
  848. FD_ZERO(&s->sends);
  849. FD_ZERO(&s->excepts);
  850. }
  851. static void selector_init(tunala_selector_t * selector)
  852. {
  853. selector_sets_init(&selector->last_selected);
  854. selector_sets_init(&selector->next_select);
  855. }
  856. #define SEL_EXCEPTS 0x00
  857. #define SEL_READS 0x01
  858. #define SEL_SENDS 0x02
  859. static void selector_add_raw_fd(tunala_selector_t * s, int fd, int flags)
  860. {
  861. FD_SET(fd, &s->next_select.excepts);
  862. if (flags & SEL_READS)
  863. FD_SET(fd, &s->next_select.reads);
  864. if (flags & SEL_SENDS)
  865. FD_SET(fd, &s->next_select.sends);
  866. /* Adjust "max" */
  867. if (s->next_select.max < (fd + 1))
  868. s->next_select.max = fd + 1;
  869. }
  870. static void selector_add_listener(tunala_selector_t * selector, int fd)
  871. {
  872. selector_add_raw_fd(selector, fd, SEL_READS);
  873. }
  874. static void selector_add_tunala(tunala_selector_t * s, tunala_item_t * t)
  875. {
  876. /* Set clean read if sm.clean_in is not full */
  877. if (t->clean_read != -1) {
  878. selector_add_raw_fd(s, t->clean_read,
  879. (buffer_full(state_machine_get_buffer(&t->sm,
  880. SM_CLEAN_IN))
  881. ? SEL_EXCEPTS : SEL_READS));
  882. }
  883. /* Set clean send if sm.clean_out is not empty */
  884. if (t->clean_send != -1) {
  885. selector_add_raw_fd(s, t->clean_send,
  886. (buffer_empty(state_machine_get_buffer(&t->sm,
  887. SM_CLEAN_OUT))
  888. ? SEL_EXCEPTS : SEL_SENDS));
  889. }
  890. /* Set dirty read if sm.dirty_in is not full */
  891. if (t->dirty_read != -1) {
  892. selector_add_raw_fd(s, t->dirty_read,
  893. (buffer_full(state_machine_get_buffer(&t->sm,
  894. SM_DIRTY_IN))
  895. ? SEL_EXCEPTS : SEL_READS));
  896. }
  897. /* Set dirty send if sm.dirty_out is not empty */
  898. if (t->dirty_send != -1) {
  899. selector_add_raw_fd(s, t->dirty_send,
  900. (buffer_empty(state_machine_get_buffer(&t->sm,
  901. SM_DIRTY_OUT))
  902. ? SEL_EXCEPTS : SEL_SENDS));
  903. }
  904. }
  905. static int selector_select(tunala_selector_t * selector)
  906. {
  907. memcpy(&selector->last_selected, &selector->next_select,
  908. sizeof(select_sets_t));
  909. selector_sets_init(&selector->next_select);
  910. return select(selector->last_selected.max,
  911. &selector->last_selected.reads,
  912. &selector->last_selected.sends,
  913. &selector->last_selected.excepts, NULL);
  914. }
  915. /*
  916. * This returns -1 for error, 0 for no new connections, or 1 for success, in
  917. * which case *newfd is populated.
  918. */
  919. static int selector_get_listener(tunala_selector_t * selector, int fd,
  920. int *newfd)
  921. {
  922. if (FD_ISSET(fd, &selector->last_selected.excepts))
  923. return -1;
  924. if (!FD_ISSET(fd, &selector->last_selected.reads))
  925. return 0;
  926. if ((*newfd = ip_accept_connection(fd)) == -1)
  927. return -1;
  928. return 1;
  929. }
  930. /************************/
  931. /* "Tunala" world stuff */
  932. /************************/
  933. static int tunala_world_make_room(tunala_world_t * world)
  934. {
  935. unsigned int newsize;
  936. tunala_item_t *newarray;
  937. if (world->tunnels_used < world->tunnels_size)
  938. return 1;
  939. newsize = (world->tunnels_size == 0 ? 16 :
  940. ((world->tunnels_size * 3) / 2));
  941. if ((newarray = malloc(newsize * sizeof(tunala_item_t))) == NULL)
  942. return 0;
  943. memset(newarray, 0, newsize * sizeof(tunala_item_t));
  944. if (world->tunnels_used > 0)
  945. memcpy(newarray, world->tunnels,
  946. world->tunnels_used * sizeof(tunala_item_t));
  947. if (world->tunnels_size > 0)
  948. free(world->tunnels);
  949. /* migrate */
  950. world->tunnels = newarray;
  951. world->tunnels_size = newsize;
  952. return 1;
  953. }
  954. static int tunala_world_new_item(tunala_world_t * world, int fd,
  955. const char *ip, unsigned short port,
  956. int flipped)
  957. {
  958. tunala_item_t *item;
  959. int newfd;
  960. SSL *new_ssl = NULL;
  961. if (!tunala_world_make_room(world))
  962. return 0;
  963. if ((new_ssl = SSL_new(world->ssl_ctx)) == NULL) {
  964. fprintf(stderr, "Error creating new SSL\n");
  965. ERR_print_errors_fp(stderr);
  966. return 0;
  967. }
  968. item = world->tunnels + (world->tunnels_used++);
  969. state_machine_init(&item->sm);
  970. item->clean_read = item->clean_send =
  971. item->dirty_read = item->dirty_send = -1;
  972. if ((newfd = ip_create_connection_split(ip, port)) == -1)
  973. goto err;
  974. /*
  975. * Which way round? If we're a server, "fd" is the dirty side and the
  976. * connection we open is the clean one. For a client, it's the other way
  977. * around. Unless, of course, we're "flipped" in which case everything
  978. * gets reversed. :-)
  979. */
  980. if ((world->server_mode && !flipped) || (!world->server_mode && flipped)) {
  981. item->dirty_read = item->dirty_send = fd;
  982. item->clean_read = item->clean_send = newfd;
  983. } else {
  984. item->clean_read = item->clean_send = fd;
  985. item->dirty_read = item->dirty_send = newfd;
  986. }
  987. /*
  988. * We use the SSL's "app_data" to indicate a call-back induced "kill"
  989. */
  990. SSL_set_app_data(new_ssl, NULL);
  991. if (!state_machine_set_SSL(&item->sm, new_ssl, world->server_mode))
  992. goto err;
  993. return 1;
  994. err:
  995. tunala_world_del_item(world, world->tunnels_used - 1);
  996. return 0;
  997. }
  998. static void tunala_world_del_item(tunala_world_t * world, unsigned int idx)
  999. {
  1000. tunala_item_t *item = world->tunnels + idx;
  1001. if (item->clean_read != -1)
  1002. close(item->clean_read);
  1003. if (item->clean_send != item->clean_read)
  1004. close(item->clean_send);
  1005. item->clean_read = item->clean_send = -1;
  1006. if (item->dirty_read != -1)
  1007. close(item->dirty_read);
  1008. if (item->dirty_send != item->dirty_read)
  1009. close(item->dirty_send);
  1010. item->dirty_read = item->dirty_send = -1;
  1011. state_machine_close(&item->sm);
  1012. /* OK, now we fix the item array */
  1013. if (idx + 1 < world->tunnels_used)
  1014. /* We need to scroll entries to the left */
  1015. memmove(world->tunnels + idx,
  1016. world->tunnels + (idx + 1),
  1017. (world->tunnels_used - (idx + 1)) * sizeof(tunala_item_t));
  1018. world->tunnels_used--;
  1019. }
  1020. static int tunala_item_io(tunala_selector_t * selector, tunala_item_t * item)
  1021. {
  1022. int c_r, c_s, d_r, d_s; /* Four boolean flags */
  1023. /* Take ourselves out of the gene-pool if there was an except */
  1024. if ((item->clean_read != -1) && FD_ISSET(item->clean_read,
  1025. &selector->
  1026. last_selected.excepts))
  1027. return 0;
  1028. if ((item->clean_send != -1) && FD_ISSET(item->clean_send,
  1029. &selector->
  1030. last_selected.excepts))
  1031. return 0;
  1032. if ((item->dirty_read != -1) && FD_ISSET(item->dirty_read,
  1033. &selector->
  1034. last_selected.excepts))
  1035. return 0;
  1036. if ((item->dirty_send != -1) && FD_ISSET(item->dirty_send,
  1037. &selector->
  1038. last_selected.excepts))
  1039. return 0;
  1040. /* Grab our 4 IO flags */
  1041. c_r = c_s = d_r = d_s = 0;
  1042. if (item->clean_read != -1)
  1043. c_r = FD_ISSET(item->clean_read, &selector->last_selected.reads);
  1044. if (item->clean_send != -1)
  1045. c_s = FD_ISSET(item->clean_send, &selector->last_selected.sends);
  1046. if (item->dirty_read != -1)
  1047. d_r = FD_ISSET(item->dirty_read, &selector->last_selected.reads);
  1048. if (item->dirty_send != -1)
  1049. d_s = FD_ISSET(item->dirty_send, &selector->last_selected.sends);
  1050. /* If no IO has happened for us, skip needless data looping */
  1051. if (!c_r && !c_s && !d_r && !d_s)
  1052. return 1;
  1053. if (c_r)
  1054. c_r = (buffer_from_fd(state_machine_get_buffer(&item->sm,
  1055. SM_CLEAN_IN),
  1056. item->clean_read) <= 0);
  1057. if (c_s)
  1058. c_s = (buffer_to_fd(state_machine_get_buffer(&item->sm,
  1059. SM_CLEAN_OUT),
  1060. item->clean_send) <= 0);
  1061. if (d_r)
  1062. d_r = (buffer_from_fd(state_machine_get_buffer(&item->sm,
  1063. SM_DIRTY_IN),
  1064. item->dirty_read) <= 0);
  1065. if (d_s)
  1066. d_s = (buffer_to_fd(state_machine_get_buffer(&item->sm,
  1067. SM_DIRTY_OUT),
  1068. item->dirty_send) <= 0);
  1069. /* If any of the flags is non-zero, that means they need closing */
  1070. if (c_r) {
  1071. close(item->clean_read);
  1072. if (item->clean_send == item->clean_read)
  1073. item->clean_send = -1;
  1074. item->clean_read = -1;
  1075. }
  1076. if (c_s && (item->clean_send != -1)) {
  1077. close(item->clean_send);
  1078. if (item->clean_send == item->clean_read)
  1079. item->clean_read = -1;
  1080. item->clean_send = -1;
  1081. }
  1082. if (d_r) {
  1083. close(item->dirty_read);
  1084. if (item->dirty_send == item->dirty_read)
  1085. item->dirty_send = -1;
  1086. item->dirty_read = -1;
  1087. }
  1088. if (d_s && (item->dirty_send != -1)) {
  1089. close(item->dirty_send);
  1090. if (item->dirty_send == item->dirty_read)
  1091. item->dirty_read = -1;
  1092. item->dirty_send = -1;
  1093. }
  1094. /*
  1095. * This function name is attributed to the term donated by David Schwartz
  1096. * on openssl-dev, message-ID:
  1097. * <NCBBLIEPOCNJOAEKBEAKEEDGLIAA.davids@webmaster.com>. :-)
  1098. */
  1099. if (!state_machine_churn(&item->sm))
  1100. /*
  1101. * If the SSL closes, it will also zero-out the _in buffers and will
  1102. * in future process just outgoing data. As and when the outgoing
  1103. * data has gone, it will return zero here to tell us to bail out.
  1104. */
  1105. return 0;
  1106. /* Otherwise, we return zero if both sides are dead. */
  1107. if (((item->clean_read == -1) || (item->clean_send == -1)) &&
  1108. ((item->dirty_read == -1) || (item->dirty_send == -1)))
  1109. return 0;
  1110. /*
  1111. * If only one side closed, notify the SSL of this so it can take
  1112. * appropriate action.
  1113. */
  1114. if ((item->clean_read == -1) || (item->clean_send == -1)) {
  1115. if (!state_machine_close_clean(&item->sm))
  1116. return 0;
  1117. }
  1118. if ((item->dirty_read == -1) || (item->dirty_send == -1)) {
  1119. if (!state_machine_close_dirty(&item->sm))
  1120. return 0;
  1121. }
  1122. return 1;
  1123. }