repo: tlswrap
action: commit
revision: 
path_from: 
revision_from: 5e7bede4fe8da68cf380194762e9cf38298f8780:
path_to: 
revision_to: 
git.thebackupbox.net
tlswrap
git clone git://git.thebackupbox.net/tlswrap
commit 5e7bede4fe8da68cf380194762e9cf38298f8780
Author: epoch 
Date:   Fri Feb 24 05:40:25 2023 +0000

    added TLS disconnection detection. hopefully it wont break things somehow.

diff --git a/tlswrap.c b/tlswrap.c
index aa3994e77c0d2d8a4e357fdf68483b786ace161c..
index ..36d23b0461b61d41e04bd5bc13cb0dc91ba399b7 100644
--- a/tlswrap.c
+++ b/tlswrap.c
@@ -378,24 +378,42 @@ int main(int argc,char *argv[]) {
   int fdmax=0;
   fd_set master;
   fd_set readfs;
+  fd_set errorfs;
   FD_ZERO(&master);
   FD_ZERO(&readfs);
+  FD_ZERO(&errorfs);
   FD_SET(0,&master);//SSL is ready to be read from
   FD_SET(b[0],&master);//subprocess's stdout is ready to be read from
   FD_SET(c[0],&master);//subprocess's stderr
   fdmax=b[0]>c[0]?b[0]:c[0];
-  struct timeval *tout=NULL;
+  struct timeval orig_timeout;
+  struct timeval timeout;
+  orig_timeout.tv_sec=0;
+  orig_timeout.tv_usec=10000;// 1/100th of a second. (10ms) sound good?
   close(a[0]);
   close(b[1]);
   close(c[1]);
+  unsigned int error_code;
+  unsigned int error_code_size = sizeof(error_code);
   //syslog(LOG_DAEMON|LOG_DEBUG,"entering select loop");
   //fprintf(stderr,"made it here\n");
   for(;FD_ISSET(b[0],&master) || FD_ISSET(c[0],&master);) { //a select() brick that reads from ssl and writes to subprocess and reads from subprocess and writes to ssl
     readfs=master;
-    if((j=select(fdmax+1,&readfs,0,0,tout)) == -1 ) {
+    errorfs=master;
+    timeout=orig_timeout;
+    if((j=select(fdmax+1,&readfs,0,&errorfs,&timeout)) == -1 ) {
       //syslog(LOG_DAEMON|LOG_ERR,"giving up. error'd in select");
       break;
     }
+
+    if(recv(0,NULL,1, MSG_PEEK | MSG_DONTWAIT) == 0) { //make sure the TLS is still connected. :D
+      syslog(LOG_DAEMON|LOG_ERR,"TLS connection seems to have dropped unexpectedly.\n");
+      break;
+    }
+
+    if(FD_ISSET(0,&errorfs)) syslog(LOG_DAEMON|LOG_ERR,"select: stdin error");
+    if(FD_ISSET(b[0],&errorfs)) syslog(LOG_DAEMON|LOG_ERR,"select: b[0] error");
+    if(FD_ISSET(c[0],&errorfs)) syslog(LOG_DAEMON|LOG_ERR,"select: c[0] error");
     if(FD_ISSET(0,&readfs)) {
       if((r1=SSL_read(ssl,buffer,sizeof(buffer))) <= 0) {
         syslog(LOG_DAEMON|LOG_DEBUG,"SSL done. %d msg: %s",r1,ERR_error_string(ERR_get_error(),NULL));
@@ -429,7 +447,7 @@ int main(int argc,char *argv[]) {
         //write(2,buffer,r2);
         buffer[r2]=0;//gotta null this off sice we're passing to something that expects a string.
         //fprintf(stderr,"%s",buffer);
-        syslog(LOG_DAEMON|LOG_WARNING,"%s -> %s stderr: %s",ru,su,buffer);
+        syslog(LOG_DAEMON|LOG_WARNING,"%s -> %s stderr of [%s] : %s",ru,su,argv[0],buffer);
       }
     }
   }

-----END OF PAGE-----