PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/ncat/ncat_connect.c

https://gitlab.com/g10h4ck/nmap-gsoc2015
C | 1280 lines | 898 code | 178 blank | 204 comment | 276 complexity | f285873fe4dbc7902b6588628f1a1082 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, Apache-2.0, LGPL-2.0, LGPL-2.1, MIT
  1. /***************************************************************************
  2. * ncat_connect.c -- Ncat connect mode. *
  3. ***********************IMPORTANT NMAP LICENSE TERMS************************
  4. * *
  5. * The Nmap Security Scanner is (C) 1996-2015 Insecure.Com LLC. Nmap is *
  6. * also a registered trademark of Insecure.Com LLC. This program is free *
  7. * software; you may redistribute and/or modify it under the terms of the *
  8. * GNU General Public License as published by the Free Software *
  9. * Foundation; Version 2 ("GPL"), BUT ONLY WITH ALL OF THE CLARIFICATIONS *
  10. * AND EXCEPTIONS DESCRIBED HEREIN. This guarantees your right to use, *
  11. * modify, and redistribute this software under certain conditions. If *
  12. * you wish to embed Nmap technology into proprietary software, we sell *
  13. * alternative licenses (contact sales@nmap.com). Dozens of software *
  14. * vendors already license Nmap technology such as host discovery, port *
  15. * scanning, OS detection, version detection, and the Nmap Scripting *
  16. * Engine. *
  17. * *
  18. * Note that the GPL places important restrictions on "derivative works", *
  19. * yet it does not provide a detailed definition of that term. To avoid *
  20. * misunderstandings, we interpret that term as broadly as copyright law *
  21. * allows. For example, we consider an application to constitute a *
  22. * derivative work for the purpose of this license if it does any of the *
  23. * following with any software or content covered by this license *
  24. * ("Covered Software"): *
  25. * *
  26. * o Integrates source code from Covered Software. *
  27. * *
  28. * o Reads or includes copyrighted data files, such as Nmap's nmap-os-db *
  29. * or nmap-service-probes. *
  30. * *
  31. * o Is designed specifically to execute Covered Software and parse the *
  32. * results (as opposed to typical shell or execution-menu apps, which will *
  33. * execute anything you tell them to). *
  34. * *
  35. * o Includes Covered Software in a proprietary executable installer. The *
  36. * installers produced by InstallShield are an example of this. Including *
  37. * Nmap with other software in compressed or archival form does not *
  38. * trigger this provision, provided appropriate open source decompression *
  39. * or de-archiving software is widely available for no charge. For the *
  40. * purposes of this license, an installer is considered to include Covered *
  41. * Software even if it actually retrieves a copy of Covered Software from *
  42. * another source during runtime (such as by downloading it from the *
  43. * Internet). *
  44. * *
  45. * o Links (statically or dynamically) to a library which does any of the *
  46. * above. *
  47. * *
  48. * o Executes a helper program, module, or script to do any of the above. *
  49. * *
  50. * This list is not exclusive, but is meant to clarify our interpretation *
  51. * of derived works with some common examples. Other people may interpret *
  52. * the plain GPL differently, so we consider this a special exception to *
  53. * the GPL that we apply to Covered Software. Works which meet any of *
  54. * these conditions must conform to all of the terms of this license, *
  55. * particularly including the GPL Section 3 requirements of providing *
  56. * source code and allowing free redistribution of the work as a whole. *
  57. * *
  58. * As another special exception to the GPL terms, Insecure.Com LLC grants *
  59. * permission to link the code of this program with any version of the *
  60. * OpenSSL library which is distributed under a license identical to that *
  61. * listed in the included docs/licenses/OpenSSL.txt file, and distribute *
  62. * linked combinations including the two. *
  63. * *
  64. * Any redistribution of Covered Software, including any derived works, *
  65. * must obey and carry forward all of the terms of this license, including *
  66. * obeying all GPL rules and restrictions. For example, source code of *
  67. * the whole work must be provided and free redistribution must be *
  68. * allowed. All GPL references to "this License", are to be treated as *
  69. * including the terms and conditions of this license text as well. *
  70. * *
  71. * Because this license imposes special exceptions to the GPL, Covered *
  72. * Work may not be combined (even as part of a larger work) with plain GPL *
  73. * software. The terms, conditions, and exceptions of this license must *
  74. * be included as well. This license is incompatible with some other open *
  75. * source licenses as well. In some cases we can relicense portions of *
  76. * Nmap or grant special permissions to use it in other open source *
  77. * software. Please contact fyodor@nmap.org with any such requests. *
  78. * Similarly, we don't incorporate incompatible open source software into *
  79. * Covered Software without special permission from the copyright holders. *
  80. * *
  81. * If you have any questions about the licensing restrictions on using *
  82. * Nmap in other works, are happy to help. As mentioned above, we also *
  83. * offer alternative license to integrate Nmap into proprietary *
  84. * applications and appliances. These contracts have been sold to dozens *
  85. * of software vendors, and generally include a perpetual license as well *
  86. * as providing for priority support and updates. They also fund the *
  87. * continued development of Nmap. Please email sales@nmap.com for further *
  88. * information. *
  89. * *
  90. * If you have received a written license agreement or contract for *
  91. * Covered Software stating terms other than these, you may choose to use *
  92. * and redistribute Covered Software under those terms instead of these. *
  93. * *
  94. * Source is provided to this software because we believe users have a *
  95. * right to know exactly what a program is going to do before they run it. *
  96. * This also allows you to audit the software for security holes. *
  97. * *
  98. * Source code also allows you to port Nmap to new platforms, fix bugs, *
  99. * and add new features. You are highly encouraged to send your changes *
  100. * to the dev@nmap.org mailing list for possible incorporation into the *
  101. * main distribution. By sending these changes to Fyodor or one of the *
  102. * Insecure.Org development mailing lists, or checking them into the Nmap *
  103. * source code repository, it is understood (unless you specify otherwise) *
  104. * that you are offering the Nmap Project (Insecure.Com LLC) the *
  105. * unlimited, non-exclusive right to reuse, modify, and relicense the *
  106. * code. Nmap will always be available Open Source, but this is important *
  107. * because the inability to relicense code has caused devastating problems *
  108. * for other Free Software projects (such as KDE and NASM). We also *
  109. * occasionally relicense the code to third parties as discussed above. *
  110. * If you wish to specify special license conditions of your *
  111. * contributions, just say so when you send them. *
  112. * *
  113. * This program is distributed in the hope that it will be useful, but *
  114. * WITHOUT ANY WARRANTY; without even the implied warranty of *
  115. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Nmap *
  116. * license file for more details (it's in a COPYING file included with *
  117. * Nmap, and also available from https://svn.nmap.org/nmap/COPYING) *
  118. * *
  119. ***************************************************************************/
  120. /* $Id$ */
  121. #include "base64.h"
  122. #include "nsock.h"
  123. #include "ncat.h"
  124. #include "util.h"
  125. #include "sys_wrap.h"
  126. #include "nbase.h"
  127. #include "http.h"
  128. #ifndef WIN32
  129. #include <unistd.h>
  130. #include <netdb.h>
  131. #endif
  132. #include <stdlib.h>
  133. #include <string.h>
  134. #include <stdio.h>
  135. #ifdef HAVE_OPENSSL
  136. #include <openssl/ssl.h>
  137. #include <openssl/err.h>
  138. #endif
  139. #ifdef WIN32
  140. /* Define missing constant for shutdown(2).
  141. * See:
  142. * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740481%28v=vs.85%29.aspx
  143. */
  144. #define SHUT_WR SD_SEND
  145. #endif
  146. struct conn_state {
  147. nsock_iod sock_nsi;
  148. nsock_iod stdin_nsi;
  149. nsock_event_id idle_timer_event_id;
  150. int crlf_state;
  151. };
  152. static struct conn_state cs = {
  153. NULL,
  154. NULL,
  155. 0,
  156. 0
  157. };
  158. static void connect_handler(nsock_pool nsp, nsock_event evt, void *data);
  159. static void post_connect(nsock_pool nsp, nsock_iod iod);
  160. static void read_stdin_handler(nsock_pool nsp, nsock_event evt, void *data);
  161. static void read_socket_handler(nsock_pool nsp, nsock_event evt, void *data);
  162. static void write_socket_handler(nsock_pool nsp, nsock_event evt, void *data);
  163. static void idle_timer_handler(nsock_pool nsp, nsock_event evt, void *data);
  164. static void refresh_idle_timer(nsock_pool nsp);
  165. #ifdef HAVE_OPENSSL
  166. /* This callback is called for every certificate in a chain. ok is true if
  167. OpenSSL's internal verification has verified the certificate. We don't change
  168. anything about the verification, we only need access to the certificates to
  169. provide diagnostics. */
  170. static int verify_callback(int ok, X509_STORE_CTX *store)
  171. {
  172. X509 *cert = X509_STORE_CTX_get_current_cert(store);
  173. int err = X509_STORE_CTX_get_error(store);
  174. /* Print the subject, issuer, and fingerprint depending on the verbosity
  175. level. */
  176. if ((!ok && o.verbose) || o.debug > 1) {
  177. char digest_buf[SHA1_STRING_LENGTH + 1];
  178. char *fp;
  179. loguser("Subject: ");
  180. X509_NAME_print_ex_fp(stderr, X509_get_subject_name(cert), 0, XN_FLAG_COMPAT);
  181. loguser_noprefix("\n");
  182. loguser("Issuer: ");
  183. X509_NAME_print_ex_fp(stderr, X509_get_issuer_name(cert), 0, XN_FLAG_COMPAT);
  184. loguser_noprefix("\n");
  185. fp = ssl_cert_fp_str_sha1(cert, digest_buf, sizeof(digest_buf));
  186. ncat_assert(fp == digest_buf);
  187. loguser("SHA-1 fingerprint: %s\n", digest_buf);
  188. }
  189. if (!ok && o.verbose) {
  190. loguser("Certificate verification failed (%s).\n",
  191. X509_verify_cert_error_string(err));
  192. }
  193. return ok;
  194. }
  195. static void set_ssl_ctx_options(SSL_CTX *ctx)
  196. {
  197. if (o.ssltrustfile == NULL) {
  198. ssl_load_default_ca_certs(ctx);
  199. } else {
  200. if (o.debug)
  201. logdebug("Using trusted CA certificates from %s.\n", o.ssltrustfile);
  202. if (SSL_CTX_load_verify_locations(ctx, o.ssltrustfile, NULL) != 1) {
  203. bye("Could not load trusted certificates from %s.\n%s",
  204. o.ssltrustfile, ERR_error_string(ERR_get_error(), NULL));
  205. }
  206. }
  207. if (o.sslverify) {
  208. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, verify_callback);
  209. } else {
  210. /* Still check verification status and report it */
  211. SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, verify_callback);
  212. if (o.ssl && o.debug)
  213. logdebug("Not doing certificate verification.\n");
  214. }
  215. if (o.sslcert != NULL && o.sslkey != NULL) {
  216. if (SSL_CTX_use_certificate_file(ctx, o.sslcert, SSL_FILETYPE_PEM) != 1)
  217. bye("SSL_CTX_use_certificate_file(): %s.", ERR_error_string(ERR_get_error(), NULL));
  218. if (SSL_CTX_use_PrivateKey_file(ctx, o.sslkey, SSL_FILETYPE_PEM) != 1)
  219. bye("SSL_CTX_use_Privatekey_file(): %s.", ERR_error_string(ERR_get_error(), NULL));
  220. } else {
  221. if ((o.sslcert == NULL) != (o.sslkey == NULL))
  222. bye("The --ssl-key and --ssl-cert options must be used together.");
  223. }
  224. }
  225. #endif
  226. /* Depending on verbosity, print a message that a connection was established. */
  227. static void connect_report(nsock_iod nsi)
  228. {
  229. union sockaddr_u peer;
  230. zmem(&peer, sizeof(peer.storage));
  231. nsock_iod_get_communication_info(nsi, NULL, NULL, NULL, &peer.sockaddr,
  232. sizeof(peer.storage));
  233. if (o.verbose) {
  234. #ifdef HAVE_OPENSSL
  235. if (nsock_iod_check_ssl(nsi)) {
  236. X509 *cert;
  237. X509_NAME *subject;
  238. char digest_buf[SHA1_STRING_LENGTH + 1];
  239. char *fp;
  240. loguser("SSL connection to %s:%hu.", inet_socktop(&peer),
  241. nsock_iod_get_peerport(nsi));
  242. cert = SSL_get_peer_certificate((SSL *)nsock_iod_get_ssl(nsi));
  243. ncat_assert(cert != NULL);
  244. subject = X509_get_subject_name(cert);
  245. if (subject != NULL) {
  246. char buf[256];
  247. int n;
  248. n = X509_NAME_get_text_by_NID(subject, NID_organizationName, buf, sizeof(buf));
  249. if (n >= 0 && n <= sizeof(buf) - 1)
  250. loguser_noprefix(" %s", buf);
  251. }
  252. loguser_noprefix("\n");
  253. fp = ssl_cert_fp_str_sha1(cert, digest_buf, sizeof(digest_buf));
  254. ncat_assert(fp == digest_buf);
  255. loguser("SHA-1 fingerprint: %s\n", digest_buf);
  256. } else {
  257. #if HAVE_SYS_UN_H
  258. if (peer.sockaddr.sa_family == AF_UNIX)
  259. loguser("Connected to %s.\n", peer.un.sun_path);
  260. else
  261. #endif
  262. loguser("Connected to %s:%hu.\n", inet_socktop(&peer),
  263. nsock_iod_get_peerport(nsi));
  264. }
  265. #else
  266. #if HAVE_SYS_UN_H
  267. if (peer.sockaddr.sa_family == AF_UNIX)
  268. loguser("Connected to %s.\n", peer.un.sun_path);
  269. else
  270. #endif
  271. loguser("Connected to %s:%hu.\n", inet_socktop(&peer),
  272. nsock_iod_get_peerport(nsi));
  273. #endif
  274. }
  275. }
  276. /* Just like inet_socktop, but it puts IPv6 addresses in square brackets. */
  277. static const char *sock_to_url(char *host_str, unsigned short port)
  278. {
  279. static char buf[512];
  280. switch(getaddrfamily(host_str)) {
  281. case -1:
  282. case 1:
  283. Snprintf(buf, sizeof(buf), "%s:%hu", host_str, port);
  284. break;
  285. case 2:
  286. Snprintf(buf, sizeof(buf), "[%s]:%hu]", host_str, port);
  287. }
  288. return buf;
  289. }
  290. static int append_connect_request_line(char **buf, size_t *size, size_t *offset,
  291. char* host_str,unsigned short port)
  292. {
  293. return strbuf_sprintf(buf, size, offset, "CONNECT %s HTTP/1.0\r\n",
  294. sock_to_url(host_str,port));
  295. }
  296. static char *http_connect_request(char* host_str, unsigned short port, int *n)
  297. {
  298. char *buf = NULL;
  299. size_t size = 0, offset = 0;
  300. append_connect_request_line(&buf, &size, &offset, host_str, port);
  301. strbuf_append_str(&buf, &size, &offset, "\r\n");
  302. *n = offset;
  303. return buf;
  304. }
  305. static char *http_connect_request_auth(char* host_str, unsigned short port, int *n,
  306. struct http_challenge *challenge)
  307. {
  308. char *buf = NULL;
  309. size_t size = 0, offset = 0;
  310. append_connect_request_line(&buf, &size, &offset, host_str, port);
  311. strbuf_append_str(&buf, &size, &offset, "Proxy-Authorization:");
  312. if (challenge->scheme == AUTH_BASIC) {
  313. char *auth_str;
  314. auth_str = b64enc((unsigned char *) o.proxy_auth, strlen(o.proxy_auth));
  315. strbuf_sprintf(&buf, &size, &offset, " Basic %s\r\n", auth_str);
  316. free(auth_str);
  317. #if HAVE_HTTP_DIGEST
  318. } else if (challenge->scheme == AUTH_DIGEST) {
  319. char *proxy_auth;
  320. char *username, *password;
  321. char *response_hdr;
  322. /* Split up the proxy auth argument. */
  323. proxy_auth = Strdup(o.proxy_auth);
  324. username = strtok(proxy_auth, ":");
  325. password = strtok(NULL, ":");
  326. if (password == NULL) {
  327. free(proxy_auth);
  328. return NULL;
  329. }
  330. response_hdr = http_digest_proxy_authorization(challenge,
  331. username, password, "CONNECT", sock_to_url(o.target,o.portno));
  332. if (response_hdr == NULL) {
  333. free(proxy_auth);
  334. return NULL;
  335. }
  336. strbuf_append_str(&buf, &size, &offset, response_hdr);
  337. free(proxy_auth);
  338. free(response_hdr);
  339. #endif
  340. } else {
  341. bye("Unknown authentication type.");
  342. }
  343. strbuf_append_str(&buf, &size, &offset, "\r\n");
  344. *n = offset;
  345. return buf;
  346. }
  347. /* Return a usable socket descriptor after proxy negotiation, or -1 on any
  348. error. If any bytes are received through the proxy after negotiation, they
  349. are written to stdout. */
  350. static int do_proxy_http(void)
  351. {
  352. struct socket_buffer sockbuf;
  353. char *request;
  354. char *status_line, *header;
  355. char *remainder;
  356. size_t len;
  357. int sd, code;
  358. int n;
  359. sd = do_connect(SOCK_STREAM);
  360. if (sd == -1) {
  361. loguser("Proxy connection failed: %s.\n", socket_strerror(socket_errno()));
  362. return -1;
  363. }
  364. status_line = NULL;
  365. header = NULL;
  366. /* First try a request with no authentication. */
  367. request = http_connect_request(o.target,o.portno, &n);
  368. if (send(sd, request, n, 0) < 0) {
  369. loguser("Error sending proxy request: %s.\n", socket_strerror(socket_errno()));
  370. free(request);
  371. return -1;
  372. }
  373. free(request);
  374. socket_buffer_init(&sockbuf, sd);
  375. if (http_read_status_line(&sockbuf, &status_line) != 0) {
  376. loguser("Error reading proxy response Status-Line.\n");
  377. goto bail;
  378. }
  379. code = http_parse_status_line_code(status_line);
  380. if (o.debug)
  381. logdebug("Proxy returned status code %d.\n", code);
  382. free(status_line);
  383. status_line = NULL;
  384. if (http_read_header(&sockbuf, &header) != 0) {
  385. loguser("Error reading proxy response header.\n");
  386. goto bail;
  387. }
  388. if (code == 407 && o.proxy_auth != NULL) {
  389. struct http_header *h;
  390. struct http_challenge challenge;
  391. close(sd);
  392. sd = -1;
  393. if (http_parse_header(&h, header) != 0) {
  394. loguser("Error parsing proxy response header.\n");
  395. goto bail;
  396. }
  397. free(header);
  398. header = NULL;
  399. if (http_header_get_proxy_challenge(h, &challenge) == NULL) {
  400. loguser("Error getting Proxy-Authenticate challenge.\n");
  401. http_header_free(h);
  402. goto bail;
  403. }
  404. http_header_free(h);
  405. sd = do_connect(SOCK_STREAM);
  406. if (sd == -1) {
  407. loguser("Proxy reconnection failed: %s.\n", socket_strerror(socket_errno()));
  408. goto bail;
  409. }
  410. request = http_connect_request_auth(o.target,o.portno, &n, &challenge);
  411. if (request == NULL) {
  412. loguser("Error building Proxy-Authorization header.\n");
  413. http_challenge_free(&challenge);
  414. goto bail;
  415. }
  416. if (o.debug)
  417. logdebug("Reconnection header:\n%s", request);
  418. if (send(sd, request, n, 0) < 0) {
  419. loguser("Error sending proxy request: %s.\n", socket_strerror(socket_errno()));
  420. free(request);
  421. http_challenge_free(&challenge);
  422. goto bail;
  423. }
  424. free(request);
  425. http_challenge_free(&challenge);
  426. socket_buffer_init(&sockbuf, sd);
  427. if (http_read_status_line(&sockbuf, &status_line) != 0) {
  428. loguser("Error reading proxy response Status-Line.\n");
  429. goto bail;
  430. }
  431. code = http_parse_status_line_code(status_line);
  432. if (o.debug)
  433. logdebug("Proxy returned status code %d.\n", code);
  434. free(status_line);
  435. status_line = NULL;
  436. if (http_read_header(&sockbuf, &header) != 0) {
  437. loguser("Error reading proxy response header.\n");
  438. goto bail;
  439. }
  440. }
  441. free(header);
  442. header = NULL;
  443. if (code != 200) {
  444. loguser("Proxy returned status code %d.\n", code);
  445. return -1;
  446. }
  447. remainder = socket_buffer_remainder(&sockbuf, &len);
  448. Write(STDOUT_FILENO, remainder, len);
  449. return sd;
  450. bail:
  451. if (sd != -1)
  452. close(sd);
  453. if (status_line != NULL)
  454. free(status_line);
  455. if (header != NULL)
  456. free(header);
  457. return -1;
  458. }
  459. /* SOCKS4a support
  460. * Return a usable socket descriptor after
  461. * proxy negotiation, or -1 on any error.
  462. */
  463. static int do_proxy_socks4(void)
  464. {
  465. struct socket_buffer stateful_buf;
  466. struct socks4_data socks4msg;
  467. char socksbuf[8];
  468. int sd,len = 9;
  469. sd = do_connect(SOCK_STREAM);
  470. if (sd == -1) {
  471. loguser("Proxy connection failed: %s.\n", socket_strerror(socket_errno()));
  472. return sd;
  473. }
  474. socket_buffer_init(&stateful_buf, sd);
  475. if (o.verbose) {
  476. loguser("Connected to proxy %s:%hu\n", inet_socktop(&targetss),
  477. inet_port(&targetss));
  478. }
  479. /* Fill the socks4_data struct */
  480. zmem(&socks4msg, sizeof(socks4msg));
  481. socks4msg.version = SOCKS4_VERSION;
  482. socks4msg.type = SOCKS_CONNECT;
  483. socks4msg.port = htons(o.portno);
  484. switch(getaddrfamily(o.target)) {
  485. case 1: // IPv4 address family
  486. socks4msg.address = inet_addr(o.target);
  487. if (o.proxy_auth){
  488. memcpy(socks4msg.data, o.proxy_auth, strlen(o.proxy_auth));
  489. len += strlen(o.proxy_auth);
  490. }
  491. break;
  492. case 2: // IPv6 address family
  493. loguser("Error: IPv6 addresses are not supported with Socks4.\n");
  494. close(sd);
  495. return -1;
  496. case -1: // fqdn
  497. socks4msg.address = inet_addr("0.0.0.1");
  498. if (strlen(o.target) > SOCKS_BUFF_SIZE-2) {
  499. loguser("Error: host name is too long.\n");
  500. close(sd);
  501. return -1;
  502. }
  503. if (o.proxy_auth){
  504. if (strlen(o.target)+strlen(o.proxy_auth) > SOCKS_BUFF_SIZE-2) {
  505. loguser("Error: host name and username are too long.\n");
  506. close(sd);
  507. return -1;
  508. }
  509. Strncpy(socks4msg.data,o.proxy_auth,sizeof(socks4msg.data));
  510. len += strlen(o.proxy_auth);
  511. }
  512. memcpy(socks4msg.data+(len-8), o.target, strlen(o.target));
  513. len += strlen(o.target)+1;
  514. }
  515. if (send(sd, (char *) &socks4msg, len, 0) < 0) {
  516. loguser("Error: sending proxy request: %s.\n", socket_strerror(socket_errno()));
  517. close(sd);
  518. return -1;
  519. }
  520. /* The size of the socks4 response is 8 bytes. So read exactly
  521. 8 bytes from the buffer */
  522. if (socket_buffer_readcount(&stateful_buf, socksbuf, 8) < 0) {
  523. loguser("Error: short response from proxy.\n");
  524. close(sd);
  525. return -1;
  526. }
  527. if (sd != -1 && socksbuf[1] != SOCKS4_CONN_ACC) {
  528. loguser("Proxy connection failed.\n");
  529. close(sd);
  530. return -1;
  531. }
  532. return sd;
  533. }
  534. /* SOCKS5 support
  535. * Return a usable socket descriptor after
  536. * proxy negotiation, or -1 on any error.
  537. */
  538. static int do_proxy_socks5(void)
  539. {
  540. struct socket_buffer stateful_buf;
  541. struct socks5_connect socks5msg;
  542. uint32_t inetaddr;
  543. char inet6addr[16];
  544. unsigned short proxyport = htons(o.portno);
  545. char socksbuf[8];
  546. int sd,len,lenfqdn;
  547. struct socks5_request socks5msg2;
  548. struct socks5_auth socks5auth;
  549. char *proxy_auth;
  550. char *username;
  551. char *password;
  552. sd = do_connect(SOCK_STREAM);
  553. if (sd == -1) {
  554. loguser("Proxy connection failed: %s.\n", socket_strerror(socket_errno()));
  555. return sd;
  556. }
  557. socket_buffer_init(&stateful_buf, sd);
  558. if (o.verbose) {
  559. loguser("Connected to proxy %s:%hu\n", inet_socktop(&targetss),
  560. inet_port(&targetss));
  561. }
  562. zmem(&socks5msg,sizeof(socks5msg));
  563. socks5msg.ver = SOCKS5_VERSION;
  564. socks5msg.nmethods = 1;
  565. socks5msg.methods[0] = SOCKS5_AUTH_NONE;
  566. len = 3;
  567. if (o.proxy_auth){
  568. socks5msg.nmethods ++;
  569. socks5msg.methods[1] = SOCKS5_AUTH_USERPASS;
  570. len ++;
  571. }
  572. if (send(sd, (char *) &socks5msg, len, 0) < 0) {
  573. loguser("Error: proxy request: %s.\n", socket_strerror(socket_errno()));
  574. close(sd);
  575. return -1;
  576. }
  577. /* first response just two bytes, version and auth method */
  578. if (socket_buffer_readcount(&stateful_buf, socksbuf, 2) < 0) {
  579. loguser("Error: malformed first response from proxy.\n");
  580. close(sd);
  581. return -1;
  582. }
  583. if (socksbuf[0] != 5){
  584. loguser("Error: got wrong server version in response.\n");
  585. close(sd);
  586. return -1;
  587. }
  588. switch(socksbuf[1]) {
  589. case SOCKS5_AUTH_NONE:
  590. if (o.verbose)
  591. loguser("No authentication needed.\n");
  592. break;
  593. case SOCKS5_AUTH_GSSAPI:
  594. loguser("GSSAPI authentication method not supported.\n");
  595. close(sd);
  596. return -1;
  597. case SOCKS5_AUTH_USERPASS:
  598. if (o.verbose)
  599. loguser("Doing username and password authentication.\n");
  600. if(!o.proxy_auth){
  601. loguser("Error: proxy requested to do authentication, but no credentials were provided.\n");
  602. close(sd);
  603. return -1;
  604. }
  605. if (strlen(o.proxy_auth) > SOCKS_BUFF_SIZE-2){
  606. loguser("Error: username and password are too long to fit into buffer.\n");
  607. close(sd);
  608. return -1;
  609. }
  610. /* Split up the proxy auth argument. */
  611. proxy_auth = Strdup(o.proxy_auth);
  612. username = strtok(proxy_auth, ":");
  613. password = strtok(NULL, ":");
  614. if (password == NULL || username == NULL) {
  615. free(proxy_auth);
  616. loguser("Error: empty username or password.\n");
  617. close(sd);
  618. return -1;
  619. }
  620. /*
  621. * For username/password authentication the client's authentication request is
  622. * field 1: version number, 1 byte (must be 0x01 -- version of subnegotiation)
  623. * field 2: username length, 1 byte
  624. * field 3: username
  625. * field 4: password length, 1 byte
  626. * field 5: password
  627. *
  628. * Server response for username/password authentication:
  629. * field 1: version, 1 byte
  630. * field 2: status code, 1 byte.
  631. * 0x00 = success
  632. * any other value = failure, connection must be closed
  633. */
  634. socks5auth.ver = 1;
  635. socks5auth.data[0] = strlen(username);
  636. memcpy(socks5auth.data+1,username,strlen(username));
  637. len = 2 + strlen(username); // (version + strlen) + username
  638. socks5auth.data[len-1]=strlen(password);
  639. memcpy(socks5auth.data+len,password,strlen(password));
  640. len += 1 + strlen(password);
  641. if (send(sd, (char *) &socks5auth, len, 0) < 0) {
  642. loguser("Error: sending proxy authentication.\n");
  643. close(sd);
  644. return -1;
  645. }
  646. if (socket_buffer_readcount(&stateful_buf, socksbuf, 2) < 0) {
  647. loguser("Error: malformed proxy authentication response.\n");
  648. close(sd);
  649. return -1;
  650. }
  651. if (socksbuf[0] != 1 || socksbuf[1] != 0) {
  652. loguser("Error: authentication failed.\n");
  653. close(sd);
  654. return -1;
  655. }
  656. break;
  657. default:
  658. loguser("Error - can't choose any authentication method.\n");
  659. close(sd);
  660. return -1;
  661. }
  662. zmem(&socks5msg2,sizeof(socks5msg2));
  663. socks5msg2.ver = SOCKS5_VERSION;
  664. socks5msg2.cmd = SOCKS_CONNECT;
  665. socks5msg2.rsv = 0;
  666. switch(getaddrfamily(o.target)) {
  667. case 1: // IPv4 address family
  668. socks5msg2.atyp = SOCKS5_ATYP_IPv4;
  669. inetaddr = inet_addr(o.target);
  670. memcpy(socks5msg2.dst, &inetaddr, 4);
  671. len = 4;
  672. break;
  673. case 2: // IPv6 address family
  674. socks5msg2.atyp = SOCKS5_ATYP_IPv6;
  675. inet_pton(AF_INET6,o.target,&inet6addr);
  676. memcpy(socks5msg2.dst, inet6addr,16);
  677. len = 16;
  678. break;
  679. case -1: // FQDN
  680. socks5msg2.atyp = SOCKS5_ATYP_NAME;
  681. lenfqdn=strlen(o.target);
  682. if (lenfqdn > SOCKS_BUFF_SIZE-5){
  683. loguser("Error: host name too long.\n");
  684. close(sd);
  685. return -1;
  686. }
  687. socks5msg2.dst[0]=lenfqdn;
  688. memcpy(socks5msg2.dst+1,o.target,lenfqdn);
  689. len = 1 + lenfqdn;
  690. }
  691. memcpy(socks5msg2.dst+len, &proxyport, sizeof(proxyport));
  692. len += 2 + 1 + 3;
  693. if (len > sizeof(socks5msg2)){
  694. loguser("Error: address information too large.\n");
  695. close(sd);
  696. return -1;
  697. }
  698. if (send(sd, (char *) &socks5msg2, len, 0) < 0) {
  699. loguser("Error: sending proxy request: %s.\n", socket_strerror(socket_errno()));
  700. close(sd);
  701. return -1;
  702. }
  703. /* TODO just two bytes for now, need to read more for bind */
  704. if (socket_buffer_readcount(&stateful_buf, socksbuf, 2) < 0) {
  705. loguser("Error: malformed second response from proxy.\n");
  706. close(sd);
  707. return -1;
  708. }
  709. switch(socksbuf[1]) {
  710. case 0:
  711. if (o.verbose)
  712. loguser("connection succeeded.\n");
  713. break;
  714. case 1:
  715. loguser("Error: general SOCKS server failure.\n");
  716. close(sd);
  717. return -1;
  718. case 2:
  719. loguser("Error: connection not allowed by ruleset.\n");
  720. close(sd);
  721. return -1;
  722. case 3:
  723. loguser("Error: Network unreachable.\n");
  724. close(sd);
  725. return -1;
  726. case 4:
  727. loguser("Error: Host unreachable.\n");
  728. close(sd);
  729. return -1;
  730. case 5:
  731. loguser("Error: Connection refused.\n");
  732. close(sd);
  733. return -1;
  734. case 6:
  735. loguser("Error: TTL expired.\n");
  736. close(sd);
  737. return -1;
  738. case 7:
  739. loguser("Error: Command not supported.\n");
  740. close(sd);
  741. return -1;
  742. case 8:
  743. loguser("Error: Address type not supported.\n");
  744. close(sd);
  745. return -1;
  746. default:
  747. loguser("Error: unassigned value in the reply.\n");
  748. close(sd);
  749. return -1;
  750. }
  751. return(sd);
  752. }
  753. int ncat_connect(void)
  754. {
  755. nsock_pool mypool;
  756. int rc;
  757. /* Unless explicitly asked not to do so, ncat uses the
  758. * fallback nsock engine to maximize compatibility between
  759. * operating systems and the different use cases.
  760. */
  761. if (!o.nsock_engine)
  762. nsock_set_default_engine("select");
  763. /* Create an nsock pool */
  764. if ((mypool = nsock_pool_new(NULL)) == NULL)
  765. bye("Failed to create nsock_pool.");
  766. if (o.debug >= 6)
  767. nsock_set_loglevel(NSOCK_LOG_DBG_ALL);
  768. else if (o.debug >= 3)
  769. nsock_set_loglevel(NSOCK_LOG_DBG);
  770. else if (o.debug >= 1)
  771. nsock_set_loglevel(NSOCK_LOG_INFO);
  772. else
  773. nsock_set_loglevel(NSOCK_LOG_ERROR);
  774. /* Allow connections to broadcast addresses. */
  775. nsock_pool_set_broadcast(mypool, 1);
  776. #ifdef HAVE_OPENSSL
  777. set_ssl_ctx_options((SSL_CTX *) nsock_pool_ssl_init(mypool, 0));
  778. #endif
  779. if (!o.proxytype) {
  780. /* A non-proxy connection. Create an iod for a new socket. */
  781. cs.sock_nsi = nsock_iod_new(mypool, NULL);
  782. if (cs.sock_nsi == NULL)
  783. bye("Failed to create nsock_iod.");
  784. if (nsock_iod_set_hostname(cs.sock_nsi, o.target) == -1)
  785. bye("Failed to set hostname on iod.");
  786. #if HAVE_SYS_UN_H
  787. /* For DGRAM UNIX socket we have to use source socket */
  788. if (o.af == AF_UNIX && o.proto == IPPROTO_UDP)
  789. {
  790. if (srcaddr.storage.ss_family != AF_UNIX) {
  791. char *tmp_name = NULL;
  792. /* If no source socket was specified, we have to create temporary one. */
  793. if ((tmp_name = tempnam(NULL, "ncat.")) == NULL)
  794. bye("Failed to create name for temporary DGRAM source Unix domain socket (tempnam).");
  795. srcaddr.un.sun_family = AF_UNIX;
  796. strncpy(srcaddr.un.sun_path, tmp_name, sizeof(srcaddr.un.sun_path));
  797. free (tmp_name);
  798. }
  799. nsock_iod_set_localaddr(cs.sock_nsi, &srcaddr.storage,
  800. SUN_LEN((struct sockaddr_un *)&srcaddr.storage));
  801. if (o.verbose)
  802. loguser("[%s] used as source DGRAM Unix domain socket.\n", srcaddr.un.sun_path);
  803. }
  804. else
  805. #endif
  806. switch (srcaddr.storage.ss_family) {
  807. case AF_UNSPEC:
  808. break;
  809. case AF_INET:
  810. nsock_iod_set_localaddr(cs.sock_nsi, &srcaddr.storage,
  811. sizeof(srcaddr.in));
  812. break;
  813. #ifdef AF_INET6
  814. case AF_INET6:
  815. nsock_iod_set_localaddr(cs.sock_nsi, &srcaddr.storage,
  816. sizeof(srcaddr.in6));
  817. break;
  818. #endif
  819. #if HAVE_SYS_UN_H
  820. case AF_UNIX:
  821. nsock_iod_set_localaddr(cs.sock_nsi, &srcaddr.storage,
  822. SUN_LEN((struct sockaddr_un *)&srcaddr.storage));
  823. break;
  824. #endif
  825. default:
  826. nsock_iod_set_localaddr(cs.sock_nsi, &srcaddr.storage,
  827. sizeof(srcaddr.storage));
  828. break;
  829. }
  830. if (o.numsrcrtes) {
  831. unsigned char *ipopts = NULL;
  832. size_t ipoptslen = 0;
  833. if (o.af != AF_INET)
  834. bye("Sorry, -g can only currently be used with IPv4.");
  835. ipopts = buildsrcrte(targetss.in.sin_addr, o.srcrtes, o.numsrcrtes, o.srcrteptr, &ipoptslen);
  836. nsock_iod_set_ipoptions(cs.sock_nsi, ipopts, ipoptslen);
  837. free(ipopts); /* Nsock has its own copy */
  838. }
  839. #if HAVE_SYS_UN_H
  840. if (o.af == AF_UNIX) {
  841. if (o.proto == IPPROTO_UDP) {
  842. nsock_connect_unixsock_datagram(mypool, cs.sock_nsi, connect_handler, NULL,
  843. &targetss.sockaddr,
  844. SUN_LEN((struct sockaddr_un *)&targetss.sockaddr));
  845. } else {
  846. nsock_connect_unixsock_stream(mypool, cs.sock_nsi, connect_handler, o.conntimeout,
  847. NULL, &targetss.sockaddr,
  848. SUN_LEN((struct sockaddr_un *)&targetss.sockaddr));
  849. }
  850. } else
  851. #endif
  852. if (o.proto == IPPROTO_UDP) {
  853. nsock_connect_udp(mypool, cs.sock_nsi, connect_handler,
  854. NULL, &targetss.sockaddr, targetsslen,
  855. inet_port(&targetss));
  856. }
  857. #ifdef HAVE_OPENSSL
  858. else if (o.proto == IPPROTO_SCTP && o.ssl) {
  859. nsock_connect_ssl(mypool, cs.sock_nsi, connect_handler,
  860. o.conntimeout, NULL,
  861. &targetss.sockaddr, targetsslen,
  862. IPPROTO_SCTP, inet_port(&targetss),
  863. NULL);
  864. }
  865. #endif
  866. else if (o.proto == IPPROTO_SCTP) {
  867. nsock_connect_sctp(mypool, cs.sock_nsi, connect_handler,
  868. o.conntimeout, NULL,
  869. &targetss.sockaddr, targetsslen,
  870. inet_port(&targetss));
  871. }
  872. #ifdef HAVE_OPENSSL
  873. else if (o.ssl) {
  874. nsock_connect_ssl(mypool, cs.sock_nsi, connect_handler,
  875. o.conntimeout, NULL,
  876. &targetss.sockaddr, targetsslen,
  877. IPPROTO_TCP, inet_port(&targetss),
  878. NULL);
  879. }
  880. #endif
  881. else {
  882. nsock_connect_tcp(mypool, cs.sock_nsi, connect_handler,
  883. o.conntimeout, NULL,
  884. &targetss.sockaddr, targetsslen,
  885. inet_port(&targetss));
  886. }
  887. } else {
  888. /* A proxy connection. */
  889. static int connect_socket;
  890. if (strcmp(o.proxytype, "http") == 0) {
  891. connect_socket = do_proxy_http();
  892. } else if (strcmp(o.proxytype, "socks4") == 0) {
  893. connect_socket = do_proxy_socks4();
  894. } else if (strcmp(o.proxytype, "socks5") == 0) {
  895. connect_socket = do_proxy_socks5();
  896. }
  897. if (connect_socket == -1)
  898. return 1;
  899. /* Clear out whatever is left in the socket buffer which may be
  900. already sent by proxy server along with http response headers. */
  901. //line = socket_buffer_remainder(&stateful_buf, &n);
  902. /* Write the leftover data to stdout. */
  903. //Write(STDOUT_FILENO, line, n);
  904. /* Once the proxy negotiation is done, Nsock takes control of the
  905. socket. */
  906. cs.sock_nsi = nsock_iod_new2(mypool, connect_socket, NULL);
  907. /* Create IOD for nsp->stdin */
  908. if ((cs.stdin_nsi = nsock_iod_new2(mypool, 0, NULL)) == NULL)
  909. bye("Failed to create stdin nsiod.");
  910. post_connect(mypool, cs.sock_nsi);
  911. }
  912. /* connect */
  913. rc = nsock_loop(mypool, -1);
  914. if (o.verbose) {
  915. struct timeval end_time;
  916. double time;
  917. gettimeofday(&end_time, NULL);
  918. time = TIMEVAL_MSEC_SUBTRACT(end_time, start_time) / 1000.0;
  919. loguser("%lu bytes sent, %lu bytes received in %.2f seconds.\n",
  920. nsock_iod_get_write_count(cs.sock_nsi),
  921. nsock_iod_get_read_count(cs.sock_nsi), time);
  922. }
  923. #if HAVE_SYS_UN_H
  924. if (o.af == AF_UNIX && o.proto == IPPROTO_UDP) {
  925. if (o.verbose)
  926. loguser("Deleting source DGRAM Unix domain socket. [%s]\n", srcaddr.un.sun_path);
  927. unlink(srcaddr.un.sun_path);
  928. }
  929. #endif
  930. nsock_pool_delete(mypool);
  931. return rc == NSOCK_LOOP_ERROR ? 1 : 0;
  932. }
  933. static void connect_handler(nsock_pool nsp, nsock_event evt, void *data)
  934. {
  935. enum nse_status status = nse_status(evt);
  936. enum nse_type type = nse_type(evt);
  937. ncat_assert(type == NSE_TYPE_CONNECT || type == NSE_TYPE_CONNECT_SSL);
  938. if (status == NSE_STATUS_ERROR) {
  939. loguser("%s.\n", socket_strerror(nse_errorcode(evt)));
  940. exit(1);
  941. } else if (status == NSE_STATUS_TIMEOUT) {
  942. loguser("%s.\n", socket_strerror(ETIMEDOUT));
  943. exit(1);
  944. } else {
  945. ncat_assert(status == NSE_STATUS_SUCCESS);
  946. }
  947. #ifdef HAVE_OPENSSL
  948. if (nsock_iod_check_ssl(cs.sock_nsi)) {
  949. /* Check the domain name. ssl_post_connect_check prints an
  950. error message if appropriate. */
  951. if (!ssl_post_connect_check((SSL *)nsock_iod_get_ssl(cs.sock_nsi), o.target))
  952. bye("Certificate verification error.");
  953. }
  954. #endif
  955. connect_report(cs.sock_nsi);
  956. /* Create IOD for nsp->stdin */
  957. if ((cs.stdin_nsi = nsock_iod_new2(nsp, 0, NULL)) == NULL)
  958. bye("Failed to create stdin nsiod.");
  959. post_connect(nsp, nse_iod(evt));
  960. }
  961. /* Handle --exec if appropriate, otherwise start the initial read events and set
  962. the idle timeout. */
  963. static void post_connect(nsock_pool nsp, nsock_iod iod)
  964. {
  965. /* Command to execute. */
  966. if (o.cmdexec) {
  967. struct fdinfo info;
  968. info.fd = nsock_iod_get_sd(iod);
  969. #ifdef HAVE_OPENSSL
  970. info.ssl = (SSL *)nsock_iod_get_ssl(iod);
  971. #endif
  972. /* Convert Nsock's non-blocking socket to an ordinary blocking one. It's
  973. possible for a program to write fast enough that it will get an
  974. EAGAIN on write on a non-blocking socket. */
  975. block_socket(info.fd);
  976. netexec(&info, o.cmdexec);
  977. }
  978. /* Start the initial reads. */
  979. if (!o.sendonly)
  980. nsock_read(nsp, cs.sock_nsi, read_socket_handler, -1, NULL);
  981. if (!o.recvonly)
  982. nsock_readbytes(nsp, cs.stdin_nsi, read_stdin_handler, -1, NULL, 0);
  983. /* The --idle-timeout option says to exit after a certain period of
  984. inactivity. We start a timer here and reset it on every read event; see
  985. refresh_idle_timer. */
  986. if (o.idletimeout > 0) {
  987. cs.idle_timer_event_id =
  988. nsock_timer_create(nsp, idle_timer_handler, o.idletimeout, NULL);
  989. }
  990. }
  991. static void read_stdin_handler(nsock_pool nsp, nsock_event evt, void *data)
  992. {
  993. enum nse_status status = nse_status(evt);
  994. enum nse_type type = nse_type(evt);
  995. char *buf, *tmp = NULL;
  996. int nbytes;
  997. ncat_assert(type == NSE_TYPE_READ);
  998. if (status == NSE_STATUS_EOF) {
  999. shutdown(nsock_iod_get_sd(cs.sock_nsi), SHUT_WR);
  1000. /* In --send-only mode or non-TCP mode, exit after EOF on stdin. */
  1001. if (o.proto != IPPROTO_TCP || (o.proto == IPPROTO_TCP && o.sendonly))
  1002. nsock_loop_quit(nsp);
  1003. return;
  1004. } else if (status == NSE_STATUS_ERROR) {
  1005. loguser("%s.\n", socket_strerror(nse_errorcode(evt)));
  1006. exit(1);
  1007. } else if (status == NSE_STATUS_TIMEOUT) {
  1008. loguser("%s.\n", socket_strerror(ETIMEDOUT));
  1009. exit(1);
  1010. } else if (status == NSE_STATUS_CANCELLED || status == NSE_STATUS_KILL) {
  1011. return;
  1012. } else {
  1013. ncat_assert(status == NSE_STATUS_SUCCESS);
  1014. }
  1015. buf = nse_readbuf(evt, &nbytes);
  1016. /* read from stdin */
  1017. if (o.linedelay)
  1018. ncat_delay_timer(o.linedelay);
  1019. if (o.crlf) {
  1020. if (fix_line_endings(buf, &nbytes, &tmp, &cs.crlf_state))
  1021. buf = tmp;
  1022. }
  1023. nsock_write(nsp, cs.sock_nsi, write_socket_handler, -1, NULL, buf, nbytes);
  1024. ncat_log_send(buf, nbytes);
  1025. if (tmp)
  1026. free(tmp);
  1027. refresh_idle_timer(nsp);
  1028. }
  1029. static void read_socket_handler(nsock_pool nsp, nsock_event evt, void *data)
  1030. {
  1031. enum nse_status status = nse_status(evt);
  1032. enum nse_type type = nse_type(evt);
  1033. char *buf;
  1034. int nbytes;
  1035. ncat_assert(type == NSE_TYPE_READ);
  1036. if (status == NSE_STATUS_EOF) {
  1037. Close(STDOUT_FILENO);
  1038. /* In --recv-only mode or non-TCP mode, exit after EOF on the socket. */
  1039. if (o.proto != IPPROTO_TCP || (o.proto == IPPROTO_TCP && o.recvonly))
  1040. nsock_loop_quit(nsp);
  1041. return;
  1042. } else if (status == NSE_STATUS_ERROR) {
  1043. loguser("%s.\n", socket_strerror(nse_errorcode(evt)));
  1044. exit(1);
  1045. } else if (status == NSE_STATUS_TIMEOUT) {
  1046. loguser("%s.\n", socket_strerror(ETIMEDOUT));
  1047. exit(1);
  1048. } else if (status == NSE_STATUS_CANCELLED || status == NSE_STATUS_KILL) {
  1049. return;
  1050. } else {
  1051. ncat_assert(status == NSE_STATUS_SUCCESS);
  1052. }
  1053. buf = nse_readbuf(evt, &nbytes);
  1054. if (o.linedelay)
  1055. ncat_delay_timer(o.linedelay);
  1056. if (o.telnet)
  1057. dotelnet(nsock_iod_get_sd(nse_iod(evt)), (unsigned char *) buf, nbytes);
  1058. /* Write socket data to stdout */
  1059. Write(STDOUT_FILENO, buf, nbytes);
  1060. ncat_log_recv(buf, nbytes);
  1061. nsock_readbytes(nsp, cs.sock_nsi, read_socket_handler, -1, NULL, 0);
  1062. refresh_idle_timer(nsp);
  1063. }
  1064. static void write_socket_handler(nsock_pool nsp, nsock_event evt, void *data)
  1065. {
  1066. enum nse_status status = nse_status(evt);
  1067. enum nse_type type = nse_type(evt);
  1068. ncat_assert(type == NSE_TYPE_WRITE);
  1069. if (status == NSE_STATUS_ERROR) {
  1070. loguser("%s.\n", socket_strerror(nse_errorcode(evt)));
  1071. exit(1);
  1072. } else if (status == NSE_STATUS_TIMEOUT) {
  1073. loguser("%s.\n", socket_strerror(ETIMEDOUT));
  1074. exit(1);
  1075. } else if (status == NSE_STATUS_CANCELLED || status == NSE_STATUS_KILL) {
  1076. return;
  1077. } else {
  1078. ncat_assert(status == NSE_STATUS_SUCCESS);
  1079. }
  1080. /* The write to the socket was successful. Allow reading more from stdin
  1081. now. */
  1082. nsock_readbytes(nsp, cs.stdin_nsi, read_stdin_handler, -1, NULL, 0);
  1083. }
  1084. static void idle_timer_handler(nsock_pool nsp, nsock_event evt, void *data)
  1085. {
  1086. enum nse_status status = nse_status(evt);
  1087. enum nse_type type = nse_type(evt);
  1088. ncat_assert(type == NSE_TYPE_TIMER);
  1089. if (status == NSE_STATUS_CANCELLED || status == NSE_STATUS_KILL)
  1090. return;
  1091. ncat_assert(status == NSE_STATUS_SUCCESS);
  1092. loguser("Idle timeout expired (%d ms).\n", o.idletimeout);
  1093. exit(1);
  1094. }
  1095. static void refresh_idle_timer(nsock_pool nsp)
  1096. {
  1097. if (o.idletimeout <= 0)
  1098. return;
  1099. nsock_event_cancel(nsp, cs.idle_timer_event_id, 0);
  1100. cs.idle_timer_event_id =
  1101. nsock_timer_create(nsp, idle_timer_handler, o.idletimeout, NULL);
  1102. }