Commit e33f2aa2ef836059a5151583b7abc52e1e9515e6

Authored by perry.werneck@gmail.com
1 parent 03036832
Exists in master

Falha no handshake SSL estava provocando um segfault

Showing 1 changed file with 42 additions and 76 deletions   Show diff stats
src/lib/telnet.c
@@ -125,7 +125,7 @@ extern struct timeval ds_ts; @@ -125,7 +125,7 @@ extern struct timeval ds_ts;
125 // static int sock = -1; /* active socket */ 125 // static int sock = -1; /* active socket */
126 126
127 #if defined(HAVE_LIBSSL) /*[*/ 127 #if defined(HAVE_LIBSSL) /*[*/
128 -static unsigned long last_ssl_error = 0; 128 +static unsigned long last_ssl_error = !0;
129 #endif 129 #endif
130 130
131 //#if defined(_WIN32) /*[*/ 131 //#if defined(_WIN32) /*[*/
@@ -317,7 +317,7 @@ static void ssl_init(void); @@ -317,7 +317,7 @@ static void ssl_init(void);
317 #else /*][*/ 317 #else /*][*/
318 #define INFO_CONST 318 #define INFO_CONST
319 #endif /*]*/ 319 #endif /*]*/
320 -static void client_info_callback(INFO_CONST SSL *s, int where, int ret); 320 +static void ssl_info_callback(INFO_CONST SSL *s, int where, int ret);
321 static void continue_tls(unsigned char *sbbuf, int len); 321 static void continue_tls(unsigned char *sbbuf, int len);
322 #endif /*]*/ 322 #endif /*]*/
323 323
@@ -418,6 +418,11 @@ LIB3270_EXPORT void popup_a_sockerr(char *fmt, ...) @@ -418,6 +418,11 @@ LIB3270_EXPORT void popup_a_sockerr(char *fmt, ...)
418 } 418 }
419 #endif 419 #endif
420 420
  421 +static int bgconnect(H3270 *h, void *dunno)
  422 +{
  423 + return connect(h3270.sock, &haddr.sa, ha_len);
  424 +}
  425 +
421 /* 426 /*
422 * net_connect 427 * net_connect
423 * Establish a telnet socket to the given host passed as an argument. 428 * Establish a telnet socket to the given host passed as an argument.
@@ -528,16 +533,6 @@ int net_connect(const char *host, char *portname, Boolean ls, Boolean *resolving @@ -528,16 +533,6 @@ int net_connect(const char *host, char *portname, Boolean ls, Boolean *resolving
528 status_resolving(&h3270,0); 533 status_resolving(&h3270,0);
529 } 534 }
530 } else { 535 } else {
531 -/*  
532 -#if defined(LOCAL_PROCESS)  
533 - if (ls) {  
534 - local_process = True;  
535 - } else {  
536 -#endif  
537 -#if defined(LOCAL_PROCESS)  
538 - local_process = False;  
539 -#endif  
540 -*/  
541 status_resolving(&h3270,1); 536 status_resolving(&h3270,1);
542 if (resolve_host_and_port(host, portname, 537 if (resolve_host_and_port(host, portname,
543 &h3270.current_port, &haddr.sa, &ha_len, 538 &h3270.current_port, &haddr.sa, &ha_len,
@@ -547,57 +542,8 @@ int net_connect(const char *host, char *portname, Boolean ls, Boolean *resolving @@ -547,57 +542,8 @@ int net_connect(const char *host, char *portname, Boolean ls, Boolean *resolving
547 return -1; 542 return -1;
548 status_resolving(&h3270,0); 543 status_resolving(&h3270,0);
549 } 544 }
550 -/*  
551 -#if defined(LOCAL_PROCESS)  
552 - }  
553 -#endif  
554 -*/  
555 } 545 }
556 546
557 -/*  
558 -#if defined(LOCAL_PROCESS)  
559 - if (local_process) {  
560 - int amaster;  
561 - struct winsize w;  
562 -  
563 - w.ws_row = XMIT_ROWS;  
564 - w.ws_col = XMIT_COLS;  
565 - w.ws_xpixel = 0;  
566 - w.ws_ypixel = 0;  
567 -  
568 - switch (forkpty(&amaster, NULL, NULL, &w)) {  
569 - case -1: // failed  
570 - popup_an_errno(errno, "forkpty");  
571 - close_fail;  
572 - case 0: // child  
573 - putenv("TERM=xterm");  
574 - if (strchr(host, ' ') != CN) {  
575 - (void) execlp("/bin/sh", "sh", "-c", host,  
576 - NULL);  
577 - } else {  
578 - char *arg1;  
579 -  
580 - arg1 = strrchr(host, '/');  
581 - (void) execlp(host,  
582 - (arg1 == CN) ? host : arg1 + 1,  
583 - NULL);  
584 - }  
585 - perror(host);  
586 - #warning Notify User  
587 - _exit(1);  
588 - break;  
589 - default: // parent  
590 - sock = amaster;  
591 -#if !defined(_WIN32)  
592 - (void) fcntl(sock, F_SETFD, 1);  
593 -#endif  
594 - net_connected();  
595 - host_in3270(CONNECTED_ANSI);  
596 - break;  
597 - }  
598 - } else {  
599 -#endif  
600 -*/  
601 /* create the socket */ 547 /* create the socket */
602 if ((h3270.sock = socket(haddr.sa.sa_family, SOCK_STREAM, 0)) == -1) { 548 if ((h3270.sock = socket(haddr.sa.sa_family, SOCK_STREAM, 0)) == -1) {
603 popup_a_sockerr( N_( "socket" ) ); 549 popup_a_sockerr( N_( "socket" ) );
@@ -624,17 +570,22 @@ int net_connect(const char *host, char *portname, Boolean ls, Boolean *resolving @@ -624,17 +570,22 @@ int net_connect(const char *host, char *portname, Boolean ls, Boolean *resolving
624 #endif /*]*/ 570 #endif /*]*/
625 571
626 /* set the socket to be non-delaying */ 572 /* set the socket to be non-delaying */
627 -#if defined(_WIN32) /*[*/ 573 +/*
  574 +#if defined(_WIN32)
628 if (non_blocking(False) < 0) 575 if (non_blocking(False) < 0)
629 -#else /*][*/ 576 +#else
630 if (non_blocking(True) < 0) 577 if (non_blocking(True) < 0)
631 -#endif /*]*/ 578 +#endif
632 close_fail; 579 close_fail;
  580 +*/
633 581
634 -#if !defined(_WIN32) /*[*/ 582 + if (non_blocking(False) < 0)
  583 + close_fail;
  584 +
  585 +#if !defined(_WIN32)
635 /* don't share the socket with our children */ 586 /* don't share the socket with our children */
636 (void) fcntl(h3270.sock, F_SETFD, 1); 587 (void) fcntl(h3270.sock, F_SETFD, 1);
637 -#endif /*]*/ 588 +#endif
638 589
639 /* init ssl */ 590 /* init ssl */
640 #if defined(HAVE_LIBSSL) /*[*/ 591 #if defined(HAVE_LIBSSL) /*[*/
@@ -645,17 +596,33 @@ int net_connect(const char *host, char *portname, Boolean ls, Boolean *resolving @@ -645,17 +596,33 @@ int net_connect(const char *host, char *portname, Boolean ls, Boolean *resolving
645 596
646 /* connect */ 597 /* connect */
647 status_connecting(&h3270,1); 598 status_connecting(&h3270,1);
  599 +
  600 + if(CallAndWait((int (*)(H3270 *, void *)) bgconnect,&h3270,NULL) == -1)
  601 + {
  602 + Trace("Connect failed: %s (rc=%d)",strerror(socket_errno()),socket_errno());
  603 + popup_a_sockerr( N_( "Can't connect to %s:%d" ),h3270.hostname, h3270.current_port);
  604 + close_fail;
  605 + }
  606 + else
  607 + {
  608 + net_connected(&h3270);
  609 + }
  610 +
  611 +/*
648 if (connect(h3270.sock, &haddr.sa, ha_len) == -1) { 612 if (connect(h3270.sock, &haddr.sa, ha_len) == -1) {
  613 +
  614 + Trace("Connect failed: %s (rc=%d)",strerror(socket_errno()),socket_errno());
  615 +
649 if (socket_errno() == SE_EWOULDBLOCK 616 if (socket_errno() == SE_EWOULDBLOCK
650 -#if defined(SE_EINPROGRESS) /*[*/ 617 +#if defined(SE_EINPROGRESS)
651 || socket_errno() == SE_EINPROGRESS 618 || socket_errno() == SE_EINPROGRESS
652 -#endif /*]*/ 619 +#endif
653 ) { 620 ) {
654 trace_dsn("Connection pending.\n"); 621 trace_dsn("Connection pending.\n");
655 *pending = True; 622 *pending = True;
656 -#if !defined(_WIN32) /*[*/ 623 +#if !defined(_WIN32)
657 output_id = AddOutput(h3270.sock, &h3270, output_possible); 624 output_id = AddOutput(h3270.sock, &h3270, output_possible);
658 -#endif /*]*/ 625 +#endif
659 } else { 626 } else {
660 popup_a_sockerr( N_( "Can't connect to %s:%d" ),h3270.hostname, h3270.current_port); 627 popup_a_sockerr( N_( "Can't connect to %s:%d" ),h3270.hostname, h3270.current_port);
661 close_fail; 628 close_fail;
@@ -665,10 +632,6 @@ int net_connect(const char *host, char *portname, Boolean ls, Boolean *resolving @@ -665,10 +632,6 @@ int net_connect(const char *host, char *portname, Boolean ls, Boolean *resolving
665 close_fail; 632 close_fail;
666 net_connected(&h3270); 633 net_connected(&h3270);
667 } 634 }
668 -/*  
669 -#if defined(LOCAL_PROCESS)  
670 - }  
671 -#endif  
672 */ 635 */
673 636
674 /* set up temporary termtype */ 637 /* set up temporary termtype */
@@ -1943,6 +1906,8 @@ process_eor(void) @@ -1943,6 +1906,8 @@ process_eor(void)
1943 */ 1906 */
1944 void net_exception(H3270 *session) 1907 void net_exception(H3270 *session)
1945 { 1908 {
  1909 + CHECK_SESSION_HANDLE(session);
  1910 +
1946 #if defined(LOCAL_PROCESS) /*[*/ 1911 #if defined(LOCAL_PROCESS) /*[*/
1947 if (local_process) { 1912 if (local_process) {
1948 trace_dsn("RCVD exception\n"); 1913 trace_dsn("RCVD exception\n");
@@ -3231,7 +3196,7 @@ ssl_init(void) @@ -3231,7 +3196,7 @@ ssl_init(void)
3231 } 3196 }
3232 SSL_set_verify(ssl_con, 0/*xxx*/, NULL); 3197 SSL_set_verify(ssl_con, 0/*xxx*/, NULL);
3233 3198
3234 - SSL_CTX_set_info_callback(ssl_ctx, client_info_callback); 3199 + SSL_CTX_set_info_callback(ssl_ctx, ssl_info_callback);
3235 3200
3236 /* XXX: May need to get key file and password. */ 3201 /* XXX: May need to get key file and password. */
3237 if (appres.cert_file) 3202 if (appres.cert_file)
@@ -3254,7 +3219,7 @@ ssl_init(void) @@ -3254,7 +3219,7 @@ ssl_init(void)
3254 } 3219 }
3255 3220
3256 /* Callback for tracing protocol negotiation. */ 3221 /* Callback for tracing protocol negotiation. */
3257 -static void client_info_callback(INFO_CONST SSL *s, int where, int ret) 3222 +static void ssl_info_callback(INFO_CONST SSL *s, int where, int ret)
3258 { 3223 {
3259 if (where == SSL_CB_CONNECT_LOOP) 3224 if (where == SSL_CB_CONNECT_LOOP)
3260 { 3225 {
@@ -3301,6 +3266,7 @@ static void client_info_callback(INFO_CONST SSL *s, int where, int ret) @@ -3301,6 +3266,7 @@ static void client_info_callback(INFO_CONST SSL *s, int where, int ret)
3301 err_buf[0] = '\0'; 3266 err_buf[0] = '\0';
3302 } 3267 }
3303 3268
  3269 + Trace("SSL Connect error in %s\nState: %s\nAlert: %s\n",err_buf,SSL_state_string_long(s),SSL_alert_type_string_long(ret));
3304 trace_dsn("SSL Connect error in %s\nState: %s\nAlert: %s\n",err_buf,SSL_state_string_long(s),SSL_alert_type_string_long(ret)); 3270 trace_dsn("SSL Connect error in %s\nState: %s\nAlert: %s\n",err_buf,SSL_state_string_long(s),SSL_alert_type_string_long(ret));
3305 3271
3306 if(showing) 3272 if(showing)