home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / alt / hackers / 1165 < prev    next >
Encoding:
Text File  |  1992-07-25  |  20.6 KB  |  702 lines

  1. Newsgroups: alt.hackers
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!csa!kibirev
  3. From: kibirev@csa.bu.edu (oleg kibirev)
  4. Subject: Debugging program on mailing machine
  5. Organization: Computer Science Department, Boston University, Boston, MA, USA
  6. Originator: kibirev@csa
  7. Message-ID: <1992Jul25.012355.11106@ctr.columbia.edu>
  8. Sender: news@ctr.columbia.edu (The Daily Lose)
  9. Approved: The King Of Flame Wars
  10. Date: Sat, 25 Jul 1992 01:23:55 GMT
  11. X-Posted-From: csa.bu.edu
  12. X-Posted-Through: sol.ctr.columbia.edu
  13. Lines: 687
  14.  
  15.  
  16. As I already mentioned in some ObHack, I am writing a program to allow access
  17. to Internet host (shell, maybe even line editor like ed(1)) via e-mail only
  18. (if you have nothing else). Anyway, the problem was that we have centrilized
  19. mailing host that delivers letters through NFS. It's incompatible with my host
  20. and can't execute programs compiled here. And, I can't login to mailing machine
  21. directly (/bin/nologin).
  22.  
  23. I managed to compile a program for correct CPU by having sendmail run a shell
  24. script that does it. But, alas, gdb can't be used this way. I can't imagine my
  25. life w/o gdb, so here is a solution: I wrote a daemon that recieves all mail and
  26. looks for a special subject. It then accepts your connection to a well-known 
  27. port and starts session similar to rsh but with tty on remote host (so that I can
  28. ^Z and ^C things). 
  29.  
  30. Not much fun? Well, you can do some interesting (but harmless) things with this 
  31. program (Shhh... I didn't say I'll approve this, but for your information only :) )
  32.  
  33.  * If you have a cluster of hosts connected with [N,A]FS but with no centrilized
  34. mail delivery, you can use mhook @hostname to explore all of them this way.
  35.  
  36.  * mhookd niether makes an entry in /etc/[u,w]tmp nor chowns tty (I wish I could...).
  37. So, you can use it for stealth logins (last login time is not modified either).
  38.  
  39. Programs below may need to be hacked a bit to work under UNIXes other than SunOS.
  40.  
  41. Happy hacking,
  42.                                   Oleg Kibirev
  43. ---------mhookd.c-------------------------------------------------------
  44.  
  45. /*************************************************************************************** 
  46.  * Connection daemon. This program scans incoming  mail for subject "MailHook-Session" *
  47.  * (case-sensative). When found, it triggers by binding a socket to port 7815 of local *
  48.  * host and  accepting  connection  from mhook. It then  opens a pseudo-terminal, sets *
  49.  * its parameters to match peers tty and runs user's login shell, passing it data from *
  50.  * the socket.                                           *
  51.  *                                               *
  52.  * To activate, create the following ~/.forward on target host:                   *
  53.  * \yourname,"|/your/home/dir/mhookd"                               *     
  54.  *                                                                                     *
  55.  * Compiling:                                                                          *
  56.  * cc -o mhookd -O mhookd.c                                                            *
  57.  * strip mhookd                                                                        *
  58.  *                                                                                     *
  59.  * If you can't directly  login  to target  host, but can put .forward to it  (through *
  60.  * NFS), you can place these  lines  in a  shell  script  and have  sendmail on target *
  61.  * pipe the letter through this script.                                *
  62.  ***************************************************************************************/
  63.  
  64.      char copyright[]="@(#) Copyleft 1992 Oleg Kibirev.\n";
  65.     static char sccsid[]="@(#)mhookd.c 1.0 07/24/92";
  66.  
  67. #    include "stdio.h"
  68. #    include "sys/types.h"
  69. #    include "sys/socket.h"
  70. #    include "sys/dir.h"
  71. #    include "netinet/in.h"
  72. #    include "string.h"
  73. #    include "sys/time.h"
  74. #    include "sys/resource.h"
  75. #    include "sys/stat.h"
  76. #    include "sys/ioctl.h"
  77. #    include "fcntl.h"
  78. #    include "signal.h"
  79. #    include "netdb.h"
  80. #    include "sys/wait.h"
  81. #    include "pwd.h"
  82. #    include "unistd.h"
  83.  
  84.     char *crypt(); /* Surprisingly, not found in SunOS 4.1 include files */
  85.  
  86. #    define ltrim(s) (s+strspn(s," \t\b\a\r\n")) 
  87.  
  88.     static char from[128];
  89.     static char buf[1024];
  90.     static int kill0,kill1;
  91.  
  92.     extern int errno;
  93.     extern char *sys_errlist[];
  94.     extern int sys_nerr;
  95.  
  96.     int chkpty(pty) /* Selects files for dir. list function */
  97.       struct direct *pty;
  98.       {
  99.       return(!strncmp(pty->d_name,"pty",3));
  100.       }
  101.  
  102. /*    The most annoying problem to debug was processes hanging around after
  103.     shell exits. Before long, I have figured out that read on a hang
  104.     socket will wait forever rather than sending SIGPIPE or at least
  105.     returning an error. So will read on a tty with closed pty counterpart.
  106.     Time for (really) brute force approach: make sure that when a process
  107.     dies, it takes relitives with it. 
  108. */
  109.  
  110.         void die()
  111.       {
  112.       if(kill0)
  113.             kill(kill0,SIGHUP); /* Soft kill-let an editor save text, etc. */
  114.             
  115.           if(kill1)
  116.         kill(kill1,SIGHUP);
  117.  
  118.       exit(0);
  119.       }
  120.  
  121. /*    Since we run detached, there is nobody to witness perror(). Here is a
  122.     replacement that sends mail back to originator.
  123. */
  124.  
  125.     void p_error(msg)
  126.       char *msg;
  127.       {
  128.       static char letter[]="Subject: MailHook: %s: %s\nTo: %s\n.\n";
  129.       static int fd[2];      
  130.       int pid;
  131.       
  132.       if(!*from) /* We haven't figured out who is calling us yet... */
  133.         die();
  134.       
  135.       sprintf(buf,letter,msg,(errno<=sys_nerr)?sys_errlist[errno]:"Unknown error",from);
  136.       if(pipe(fd) || (pid=fork())<0)
  137.         die(); /* Nothing else can be done to let them know */
  138.  
  139.       if(pid) 
  140.         {
  141.         write(fd[1],buf,strlen(buf)); /* Pipe message to sendmail */
  142.         close(fd[1]); /* This should force EOF on read */
  143.         wait(NULL);
  144.         }
  145.       else
  146.         {
  147.         dup2(fd[0],0);
  148.         dup2(open("/dev/null",O_WRONLY),1); /* Don't really care if this fails */
  149.         dup2(1,2);
  150.         execl("/usr/lib/sendmail","/usr/lib/sendmail","-t",NULL); /* Tell sendmail that target is in "To:" line */
  151.         }
  152.       die();
  153.       }
  154.         
  155.     main(argc,argv)
  156.       int argc;
  157.       char **argv;
  158.       {
  159.       int true=1,i,s,master,slave,death=0;
  160.       char *p;
  161.       static struct rlimit rlp;
  162.       static struct sockaddr_in myname={AF_INET,7815};
  163.       static struct winsize win;
  164.       struct passwd *user;
  165.       struct direct **ptys;
  166.  
  167.       while(fgets(buf,1023,stdin))
  168.         {
  169.         p=ltrim(buf);
  170.         if(!*p)
  171.           break; 
  172.           
  173.         if(!strncasecmp(p,"From:",5))
  174.           {
  175.           strncpy(from,ltrim(p+5),127);
  176.           continue;
  177.           }
  178.  
  179.         if(strncasecmp(p,"Subject:",8))
  180.           continue;
  181.         if(!strncmp(p=ltrim(p+8),"MailHook-Session",16))
  182.           goto run;
  183.         
  184.         /* Sending mail with Subject: MailHook-Cleanup is a quick way to
  185.            get rid of all processes on target host. Helps a lot when your
  186.            pgm fork()s the system */
  187.  
  188.         if(!strncmp(p,"MailHook-Clean",14))
  189.           {
  190.           death=1;
  191.           goto run;
  192.           }
  193.  
  194.         break;
  195.         }
  196.       exit(0);
  197.   
  198.       
  199. run:;
  200. #ifdef xyzzy
  201.       if(fork()) /* Let sendmail go */
  202.         exit(0);
  203. #endif        
  204.           signal(SIGPIPE,die); /* redefine deadly signals to kill all mhookd's, not */
  205.       signal(SIGSEGV,die); /* just one that triggered it */
  206.       signal(SIGBUS,die);
  207.  
  208.       signal(SIGXCPU,SIG_IGN); 
  209.       signal(SIGQUIT,SIG_IGN);
  210.       signal(SIGTSTP,SIG_IGN); /* No good for us to get stopped */
  211.       signal(SIGTTOU,SIG_IGN);
  212.  
  213.       umask(022); /* Don't make files world-writable ... */
  214.       getrlimit(RLIMIT_CORE,&rlp);
  215.       rlp.rlim_cur=0;
  216.       setrlimit(RLIMIT_CORE,&rlp); /* ... or dump core if we shall be murdered */
  217.       while(*argv) 
  218.         *(argv++)=NULL;
  219.  
  220.           close(0);
  221.       if((s=socket(PF_INET,SOCK_STREAM,0))<0)
  222.         p_error("socket()");
  223.  
  224.       gethostname(buf,1024);
  225.  
  226.       /* I wonder how compatible would be just using localhost (127.0.0.1) */
  227.       (myname.sin_addr).s_addr=**(unsigned long **)(gethostbyname(buf)->h_addr_list);
  228.       if(bind(s,&myname,sizeof(myname)))
  229.         p_error("bind()");
  230.  
  231.       if(listen(s,1))
  232.         p_error("listen()");
  233.  
  234.       /* Won't wait forever for them to answer. The net/mhook could get hang */
  235.        
  236.       signal(SIGALRM,die);
  237.       alarm(4*60);
  238.       argc=sizeof(myname);
  239.       while((i=accept(s,&myname,&argc))<0);
  240.       close(s); /* Release "well-know port" - now ready for another connection */
  241.       /* By the way, change port number to smth large, random and different from */
  242.       /* 7518, so that you don't have clashes with other users on your host running */
  243.       /* this program. */      
  244.  
  245.       /* We are about to launch user's shell and don't know yet who is another party. */
  246.       /* The first thing is to ask their password. mhook will get and send it. It */
  247.       /* should be at most 8 chars and zero-padded. Rather hard to enter using standard */
  248.       /* telnet (bug?)                                        */
  249.  
  250.       /* When I give this program to hackers less experienced than you, I also include */
  251.       /* explicit check for my own password                           */
  252.  
  253.       if(read(i,buf,8)!=8) 
  254.         exit(0);
  255.  
  256.       /* To make life harder to someone without a clue trying to figure out */
  257.       /* What mhookd in my .forward does, I don't report  any errors found   */
  258.       /* when checking password */
  259.  
  260.       alarm(0);
  261.       setsockopt(i,SOL_SOCKET,SO_KEEPALIVE,&true,sizeof(int)); /* Close hang connection */
  262.       setsockopt(i,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int)); /* Ok to bind 7518 again */
  263.  
  264.       if(!(user=getpwuid(getuid())))
  265.         exit(0); /* I am confused */
  266.  
  267.       buf[8]='\0';
  268.       buf[9]=user->pw_passwd[0]; /* Salt from my password */
  269.       buf[10]=user->pw_passwd[1];
  270.       buf[11]='\0';
  271.       if(strcmp(crypt(buf,buf+9),user->pw_passwd))
  272.         exit(0); /* Shame on you! */
  273.  
  274.       strcpy(buf,"Password"); /* Don't like my text password hanging around */
  275.       if(death)
  276.         kill(9,-1); /* Pity! */
  277.       
  278.       if((s=scandir("/dev/",&ptys,chkpty,NULL))<0) /* Get list of ptys */
  279.         p_error("scandir");
  280.  
  281.       strcpy(buf,"/dev/");
  282.       while(s--)
  283.         {
  284.         strcpy(buf+5,(*(ptys++))->d_name);
  285.         if((master=open(buf,O_RDWR))<0)
  286.           continue;
  287.         buf[5]='t';
  288.         if((slave=open(buf,O_RDWR))<0)
  289.           {
  290.           close(master); /* Shouldn't happen, but it does */
  291.           continue;
  292.           }
  293.         goto pty_ok;
  294.         }
  295.       p_error("open pty");
  296.        
  297.       /* Set pseudo terminal like their own. Format of ioctl structures */
  298.       /* appears to be incompatible across different UNIXes, so I use   */
  299.       /* /bin/stty that seams to work better. By now, the client have sent us */
  300.       /* output of stty -g. Note that it's report doesn't include window size */
  301.       /* so I had to transmit this structure alone. */
  302.  
  303. pty_ok:      if(read(i,&win,sizeof(win))!=sizeof(win))
  304.         goto noparms;
  305.       
  306.       
  307.           if(read(i,buf+64,1023-64)<0) /* the first few bytes of buf still hold the name */
  308. noparms:    p_error("mhook: read tty parms"); /* of slave tty */
  309.  
  310.       if(p=strchr(buf+64,'\n'))
  311.         *p='\0';
  312.  
  313.       if(!(kill0=fork()))
  314.         {
  315.             kill0=getppid(); /* Processes are ready to kill each other */
  316.         dup2(slave,0);   /* if anything happens                    */
  317.             dup2(slave,1);
  318.         dup2(slave,2);  /* Not that stty doesn't need controlling tty */
  319.         execl("/bin/stty","/bin/stty",buf+64,NULL); /* It just uses stdout */
  320.         _exit(0); 
  321.         }
  322.           else if(kill0>0) /* If stty fails, we can still live with it, so just */
  323.         wait(NULL);    /* go on if fork() is negative */
  324.  
  325.       signal(SIGCHLD,die); /* couldn't do it before because of above fork() */
  326.  
  327.  
  328. /*             Server (mhookd)         Socket       Client (mhook)
  329.           ____________              pair            
  330.          /Shell Output\____________\___         |              ______
  331.          \____________/            /   \        |       __\___/Output\__\___ ########
  332.          /       ^      \                 \______ | _____/  /   \______/  /    # Your #
  333.         /       |       \                 ______ | _____        _____     # Local#
  334.      _____     |     ___________        /       |      \__/___/Input\___/___ # tty  #
  335.     /Shell\       |    /Shell Input\__/___/        |         \   \_____/   \    ########
  336.     \_____/    |    \___________/  \            |               
  337.           ^ |      |        |                       |
  338.       | v       \____   v                       |
  339.        #########         #########                  |
  340.        # ttyXX ########### ptyXX #                  |
  341.        #########         #########                  |
  342.  
  343. *** Flame on
  344. UNIX is not right requiring multitasking just to redirect I/O. 4 out of
  345. 5 processes here are doing nothing but reading one descriptor and writing another.
  346. Under DOS, the above would be done just by intercepting INT 21H. Supporting 
  347. multiple processes is sure a good thing, but not enforcing them 
  348. *** Flame off
  349.      
  350. */     
  351.  
  352.       if((kill0=fork())<0)
  353.         {
  354.         kill0=0;
  355.         p_error("fork()");
  356.         }
  357.  
  358.       if(!kill0)
  359.         {
  360.         kill0=getppid();
  361.         setsid(); /* Try TIOCNOTTY ioctl with BSD-derived UNIXes that don't have this */
  362.  
  363.         /* Note that I have to open another descripor for slave although I already have one */
  364.         /* Simple dup2 won't  assign controlling tty                    */
  365.  
  366.         if((s=open(buf,O_RDWR))<0)
  367.           p_error("open");
  368.  
  369.         close(master); /* Try to be compatible with in.telnetd and pass only 3 descriptors */
  370.         close(i);       /* to the shell                               */
  371.  
  372.             dup2(s,0);
  373.         dup2(s,1);
  374.         dup2(s,2);
  375.         close(s);
  376.  
  377.         /* The nightmare: be sure to close the old descriptor only after the new   */
  378.         /* one is opened. If you don't, the process will get "device busy" (???) on*/
  379.         /* open - that is despite the fact that our parent still has a handle      */
  380.  
  381.         close(slave);
  382.  
  383.         ioctl(0,TIOCSWINSZ,&win);
  384.  
  385.         /* Make sure that they start in readable dir. *csh get's very confused if not */
  386.         /* (csh: cwd: can't stat\nConnection closed by foreign host)          */
  387.  
  388.         if(!access(user->pw_dir,R_OK|W_OK|X_OK) && chdir(user->pw_dir))
  389.           chdir("/");
  390.  
  391.         for(i=0;i<32;i++) 
  392.           signal(i,SIG_DFL);
  393.           
  394.         /* Won't run /bin/nologin or /dev/null. If I don't know your shell, I will */
  395.         /* choose my own. Add your local shells, like /bin/ksh, as another strcmp  */
  396.  
  397.         if(!strcmp(user->pw_shell,"/bin/sh")||!strcmp(user->pw_shell,"/bin/csh")||!strcmp(user->pw_shell,"/bin/tcsh")||
  398.            !strcmp(user->pw_shell,"/usr/local/bin/tcsh"))
  399.           {
  400.           strcpy(buf,user->pw_shell);
  401.           *buf='-';
  402.           execl(user->pw_shell,buf,"-f",NULL);
  403.           }
  404.     
  405.         execl("/bin/csh","-sh","-f",NULL); 
  406.         execl("/bin/sh","-h","-f",NULL); /* ;? */
  407.         die();                 /* :( */
  408.         }
  409.  
  410.       if((kill1=fork())<0)
  411.         {
  412.         kill1=0;
  413.         die();      
  414.         }
  415.  
  416.       if(!kill1)
  417.         {
  418.         close(slave);
  419.         kill1=getppid();
  420.         while((s=read(i,buf,1024))>0 && write(master,buf,s)==s); /* socket->shell input */
  421.         die();
  422.         }
  423.  
  424.       close(slave);
  425.       while((s=read(master,buf,1024))>0 && write(i,buf,s)==s); /* shell output->socket */
  426.       die();
  427.       }
  428.           
  429. ---------mhook.c--------------------------------------------------
  430.  
  431. /****************************************************************************
  432.  * This program talks to mhookd. It sends mail to specified target and wait *
  433.  * for deamon activated my sendmail to bind port 7815. After connection  is *
  434.  * established  it's blindly copying output to the socket. Bthw, this means *
  435.  * that it won't close connection by itself; server must do it. But doesn't *
  436.  * rlogin? Also,  since I use this program only to run *cc and gdb in emacs *
  437.  * shell window,  it's  Ok for me  -- add  escape  sequence if you need it. *
  438.  * BUG: closed connection is detected only after you press Enter.           *
  439.  *                                                                          *
  440.  * Usage:  mhook  user@host  to connect as  this  identity. User's .forward *
  441.  * must pipe  output  through mhookd.  If  either  (or both)  part's of the *
  442.  * address are omited,  caller's uid  and  host  are  used  instead. Use -k *
  443.  * to quickly kill all your  processes on  target  machine. You may have to *
  444.  * to wait a while for connection, depending on your mailing system.        *
  445.  ****************************************************************************/
  446.  
  447.      char copyright[]="@(#) Copyleft 1992 Oleg Kibirev.\n";
  448.     static char sccsid[]="@(#)mhook.c 1.0 07/24/92";
  449.  
  450. #    include "stdio.h"
  451. #    include "errno.h"
  452. #    include "netdb.h"
  453. #    include "signal.h"
  454. #    include "sys/types.h"
  455. #    include "sys/socket.h"
  456. #    include "sysexits.h"
  457. #    include "sys/stat.h"
  458. #    include "sys/ioctl.h"
  459. #    include "fcntl.h"
  460. #    include "netinet/in.h"
  461. #    include "pwd.h"
  462. #    include "string.h"
  463.  
  464.     extern int errno;
  465.     static char me[128];
  466.     static struct sockaddr_in target={AF_INET,7815};
  467.     static struct hostent *host;
  468.     static char buf[1024];
  469.     static int killme=0;
  470.     static struct sgttyb mode,patch;
  471.     static struct winsize win;
  472.  
  473.     void die() /* The same brother-killing... */
  474.       {
  475.       ioctl(2,TIOCSETP,&mode);
  476.       /* Well, the message below is correct in MOST cases */
  477.       fputs("\nConnection closed by foreign host\n",stderr);
  478.  
  479.       if(killme)
  480.         kill(killme,SIGKILL);
  481.  
  482.       _exit(0);
  483.       }
  484.  
  485.     void myname()
  486.       {
  487.       struct passwd *mypass;
  488.       if(!(mypass=getpwuid(getuid())))
  489.         {
  490.         perror("mhook: getpwuid");
  491.         fputs("mhook: If you don't have an account, specify destination explicitely\n",stderr); /* :) */
  492.         exit(1);
  493.         }
  494.           strcpy(me,mypass->pw_name);
  495.       }        
  496.  
  497.     void myhost()
  498.       {
  499.       if(gethostname(buf,1024) || !(host=gethostbyname(buf)))
  500.         {
  501.         fputs("mhook: Our host doesn't know itself\n",stderr);
  502.         exit(1);
  503.         }
  504.           strcat(me,"@");
  505.       strcat(me,buf);
  506.       target.sin_addr=*(struct in_addr *)*(host->h_addr_list);
  507.       }
  508.  
  509.     main(argc,argv)
  510.       int argc;
  511.       char **argv;
  512.       {
  513.       static char message[]="To: %s\nSubject: %s\n\n.\n";
  514.       char *subject="MailHook-Session";
  515.       int death=0,s,i,true=1;
  516.       char *p;
  517.       static int fd[2];
  518.  
  519.       /* Some awfull code to process options. Niether I care about this part being optimal */
  520.       if(argc>3)
  521.         goto syntax;
  522.  
  523.       if(argc==3)
  524.         {
  525.         if(!strcmp(argv[1],"-k"))
  526.           {
  527.           argv++;
  528.           goto killmode;
  529.           }
  530.         if(!strcmp(argv[2],"-k"))
  531.           {
  532. killmode:     argc--;
  533.           subject="MailHook-Cleanup";
  534.           death=1;
  535.           }
  536.         else
  537.           {
  538. syntax:          fputs("Usage: mhook [-k] [user@hostname]\n",stderr);
  539.           exit(1);
  540.           }
  541.         }        
  542.       
  543.       if(argc!=2)               
  544.         {
  545.         myname();
  546.         myhost();
  547.         }
  548.       else
  549.         {
  550.         if(!(p=strchr(argv[1],'@')))
  551.           {
  552.           strcpy(me,argv[1]);
  553.           myhost();
  554.           }
  555.             else if(*argv[1]=='@')
  556.           {
  557.           myname();
  558. sethost:      if(!(host=gethostbyname(argv[1]+1)))
  559.             {
  560.         perror("mhook: host lookup");
  561.         exit(1);
  562.         }
  563.           strcat(me,argv[1]);
  564.           target.sin_addr=*(struct in_addr *)*(host->h_addr_list);
  565.           }
  566.             else
  567.           {
  568.           *p='\0';
  569.           strcpy(me,argv[1]);
  570.           *p='@';
  571.           argv[1]=p;
  572.           goto sethost;
  573.           }
  574.             }
  575.    
  576.         if(pipe(fd)<0)
  577.        {
  578.        perror("mhook: pipe");
  579.        exit(1);
  580.        }
  581.  
  582.      fputs("Sending mail to remote host\n",stdout);
  583.         if((i=fork())<0)
  584.        {
  585.        perror("mhost: fork()");
  586.        exit(1);
  587.        }
  588.  
  589.      if(!i)
  590.        {
  591.        dup2(fd[0],0);
  592.        execl("/usr/lib/sendmail","/usr/lib/sendmail","-t",NULL); /* Let remote host know I want to talk */
  593.        exit(EX_UNAVAILABLE);
  594.        }
  595.          else
  596.        {
  597.        sprintf(buf,message,me,subject);
  598.        write(fd[1],buf,strlen(buf));
  599.        close(fd[1]);
  600.        wait(&i);
  601.        if((i!=EX_OK<<8)&&(i!=EX_TEMPFAIL<<8))
  602.          {
  603.          fputs("mhook: Can't send mail to your target\n",stderr);
  604.          exit(1);
  605.          }             
  606.            }
  607.  
  608.     ioctl(2,TIOCGETP,&mode);
  609.  
  610.     signal(SIGPIPE,die);
  611.     signal(SIGBUS,die);
  612.     signal(SIGSEGV,die);
  613.     signal(SIGSYS,die);
  614.     signal(SIGHUP,die);
  615.     
  616.     fputs("Waiting your site to respond\n",stdout);
  617.     /* I have no idea how long it may take. On our system it's a few seconds    */
  618.     /* But if mail goes through some host currently down, I will wait for hours */
  619.     /* days and centuries. Just ^C me to give up.                        */
  620.  
  621.     for(;;) /* eternity here */
  622.       {
  623.           if((s=socket(PF_INET,SOCK_STREAM,0))<0)
  624.         {
  625.         perror("mhost: socket");
  626.         exit(1);
  627.         }
  628.  
  629.       setsockopt(s,SOL_SOCKET,SO_KEEPALIVE,&true,sizeof(int));        
  630.     
  631.           if(connect(s,&target,sizeof(target))>=0)
  632.         break;
  633.  
  634.       close(s); /* socket may be spoiled if connection is refused */
  635.       if(errno!=ETIMEDOUT && errno!=ECONNREFUSED)
  636.         {
  637.         perror("mhook: connect");
  638.         exit(1);
  639.         }     
  640.       }
  641.   
  642.     fputs("Connection established\n",stdout);
  643.     if(write(s,getpass("Password: "),8)!=8)
  644.       {
  645.       fputs("Unable to send password\n");
  646.       exit(1);
  647.       }
  648.       
  649.         if(death)
  650.       exit(0);
  651.  
  652.     signal(SIGQUIT,SIG_IGN); /* ^C and ^Z remote host, not me! */
  653.     signal(SIGTSTP,SIG_IGN);      
  654.     patch=mode; /* Let the server do all I/O processing */
  655.     patch.sg_flags=(patch.sg_flags|RAW)&~ECHO;
  656.     
  657.  
  658.     ioctl(2,TIOCGWINSZ,&win);
  659.     if(write(s,&win,sizeof(win))!=sizeof(win))
  660.       {
  661.       perror("mhook: write()");
  662.       die();
  663.       }
  664.  
  665.     if((killme=fork())<0)
  666.       perror("mhook:fork()");
  667.  
  668.     if(!killme)
  669.       {
  670.       killme=getppid();
  671.       dup2(s,1);
  672.       execl("/bin/stty","/bin/stty","-g",NULL);
  673.       perror("mhook: execl()");
  674.       die();
  675.       }
  676.  
  677.     wait(NULL);
  678.     ioctl(2,TIOCSETP,&patch);
  679.  
  680.     if((killme=fork())<0)
  681.       {
  682. nofork:      killme=0;
  683.       perror("mhook: fork()");
  684.       die();
  685.       }
  686.  
  687.     if(!killme)
  688.       {
  689.       killme=getppid();
  690.       while((i=read(0,buf,1024))>0 && write(s,buf,i)==i);
  691.       die();
  692.       }
  693.  
  694.         while((i=read(s,buf,1024))>0 && write(1,buf,i)==i);
  695.     die();
  696.     }
  697.             
  698.     
  699.         
  700.  
  701.  
  702.