home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff247.lzh / RemoteLogin / pwcheck.c < prev    next >
C/C++ Source or Header  |  1989-09-15  |  6KB  |  269 lines

  1.  
  2. /* Placed in the public domain by the author David Kinzer */
  3.  
  4. #include "exec/types.h"
  5. #include "exec/nodes.h"
  6. #include "exec/lists.h"
  7. #include "exec/ports.h"
  8. #include "exec/libraries.h"
  9. #include "exec/devices.h"
  10. #include "exec/io.h"
  11. #include "devices/serial.h"
  12.  
  13. #include <stdio.h>
  14.  
  15. #define BUFLEN 256
  16. #define LOGNAMELENGTH 30
  17. #define PASSWORDLENGTH 30
  18.  
  19. main(ac,av)
  20. int ac;
  21. char *av[];
  22. {
  23. FILE *pwfile, *banfile;
  24. char buf[BUFLEN],logname[LOGNAMELENGTH],password[PASSWORDLENGTH];
  25. char *pwptr,*comptr;
  26. int commandflag = 0;
  27. int earlyend = 0;  /* error exit flag, not yet used */
  28.  
  29.    if (ac < 2) return;   /* no password file given, error */
  30.    
  31.    pwfile = fopen(av[1],"r");
  32.    if (pwfile) {
  33.       /* password file opened */
  34.       if (!openamigaserial()) {
  35.          /* serial port is available, prompt user */
  36.          
  37.          /* send out banner file, if requested */
  38.          if (ac > 2) {
  39.             banfile = fopen(av[2],"r");
  40.             if (banfile) {
  41.                while (fgets(buf,BUFLEN,banfile)) 
  42.                   writeamigaserial(buf);
  43.                fclose(banfile);
  44.             }
  45.          }
  46.  
  47.          /* send out login prompt */
  48.  
  49.          while(pwfile && !commandflag && !earlyend) {  
  50.  
  51.             /* Prompt for ID */
  52.             writeamigaserial("login: ");
  53.             readamigaserial(logname,LOGNAMELENGTH,1);
  54.             writeamigaserial("password: ");
  55.             readamigaserial(password,PASSWORDLENGTH,0);
  56.                 /* change to -1 for asterisk echo   ^ */
  57.  
  58.             /* check for appropriate response */
  59.             while (fgets(buf,BUFLEN,pwfile)) {
  60.                if (buf[0] == ' ') continue;
  61.                if (buf[0] == '*') continue;
  62.                if (buf[0] == '\0') continue;
  63.                if (buf[0] == '\n') continue;
  64.                
  65.                /* get rid of newline at end of line (borrow unused pointer) */
  66.                pwptr = buf;
  67.                while (*pwptr) {
  68.                   if (*pwptr == '\n')
  69.                      *pwptr = '\0';
  70.                   else
  71.                      pwptr++;
  72.                }
  73.                /* break up line into fields */
  74.                pwptr = buf;
  75.                while (*pwptr) {
  76.                   if (*pwptr == ':') {
  77.                      *pwptr = '\0';
  78.                      pwptr++;
  79.                      break;  /* out of while loop */
  80.                   } else
  81.                      pwptr++;
  82.                 }
  83.                comptr = pwptr;
  84.                while (*comptr) {
  85.                   if (*comptr == ':') {
  86.                      *comptr = '\0';
  87.                      comptr++;
  88.                      break;  /* out of while loop */
  89.                   } else
  90.                      comptr++;
  91.                }
  92.                if (!strcmp(buf,logname) && !strcmp(pwptr,password)) {
  93.                   /* the names match, set up command for exit */
  94.                   commandflag = 1;
  95.                   break;  /* out of file reading while loop */
  96.                }  
  97.             }
  98.             if (!commandflag && !earlyend) {
  99.                /* none of the logins matched, restart process if possible */
  100.                fclose(pwfile);
  101.                pwfile = fopen(av[1],"r");
  102.  
  103.                /* slow down direct key space search for security */
  104.                Delay(150L);
  105.  
  106.                /* inform user of error */
  107.                writeamigaserial("Login incorrect\n");
  108.             }
  109.  
  110.  
  111.          }
  112.     
  113.          closeamigaserial();
  114.       }
  115.  
  116.       if (pwfile) fclose(pwfile);
  117.    }
  118.  
  119.    if (commandflag && *comptr) Execute(comptr,0L,0L);
  120.       
  121.    
  122. }
  123.       
  124.  
  125.  
  126.  
  127.  
  128. /* Standard Amiga Serial Port Routines */
  129.  
  130. static struct Port *mySerPort;
  131. struct IOExtSer *mySerReq;
  132.  
  133. int openamigaserial()
  134. {
  135. int gotport = 0;
  136. int gotextio = 0;
  137. long error;
  138. long OpenDevice();
  139. VOID *CreateExtIO();
  140. struct Port *CreatePort();
  141.  
  142.  
  143.    mySerPort = CreatePort("mySerial",0L);
  144.    if (mySerPort == NULL) 
  145.       goto errorexit;
  146.    gotport = 1;
  147.  
  148.    mySerReq = (struct IOExtSer *)CreateExtIO(mySerPort,
  149.               (long)sizeof(struct IOExtSer));
  150.    if (mySerReq == NULL) 
  151.       goto errorexit;
  152.    gotextio = 1;
  153.  
  154.    error = OpenDevice("serial.device",0L,mySerReq,0L);
  155.    if (error) 
  156.       goto errorexit;
  157.  
  158.    return 0;  /* return success */
  159.  
  160.  
  161. errorexit:
  162.    if (gotextio) 
  163.       DeleteExtIO(mySerReq,sizeof(struct IOExtSer));
  164.    if (gotport)
  165.       DeletePort(mySerPort);
  166.  
  167.    return 1;  /* return error */
  168.  
  169. }
  170.  
  171. closeamigaserial()
  172. {
  173.    CloseDevice(mySerReq);
  174.    DeleteExtIO(mySerReq,sizeof(struct IOExtSer));
  175.    DeletePort(mySerPort);
  176. }
  177.  
  178.  
  179.  
  180. readamigaserial(buf,max,echo)
  181. char *buf;
  182. int max,echo;
  183. {
  184. int i = 0;
  185. int temp;
  186. char c;
  187. char *index();
  188.  
  189.    while (i < max-1) {
  190.       temp = get1();
  191.       if (temp < 0) {
  192.          *buf = '\0';
  193.          return;
  194.       } else
  195.          c = temp;
  196.       if (!c) continue;
  197.       if (index("\r\n\004",c)) {
  198.          *buf = '\0';
  199.          send1('\r');
  200.          send1('\n');
  201.          return;
  202.       } else if (index("\b\377",c)) {
  203.          if (i) {
  204.             i--;
  205.             buf--;
  206.             if (echo) {
  207.                send1('\b');
  208.                send1(' ');
  209.                send1('\b');
  210.             }
  211.          }
  212.       } else {
  213.          *buf++ = c;
  214.          i++;
  215.          if (echo > 0) send1(c);
  216.          if (echo < 0) send1('*');
  217.       }
  218.    }
  219.    /* ran out of room */
  220.    *buf = '\0';
  221. }
  222.  
  223.  
  224.  
  225.  
  226. writeamigaserial(buf)
  227. char *buf;
  228. {
  229.    while (*buf) {
  230.       if (*buf == '\n') 
  231.          send1('\r');
  232.       send1(*buf++);
  233.    }
  234.  
  235. }
  236.  
  237.  
  238.  
  239.  
  240. send1(chr)
  241. char chr;
  242. {
  243. char temp;
  244.  
  245.    temp = chr;   
  246.  
  247.  
  248.    mySerReq->IOSer.io_Data = (APTR)&temp;
  249.    mySerReq->IOSer.io_Length = 1;
  250.    mySerReq->IOSer.io_Command = CMD_WRITE;
  251.    return DoIO(mySerReq);
  252.  
  253. }
  254.  
  255. int get1()
  256. {
  257.    unsigned char temp;
  258.  
  259.    mySerReq->IOSer.io_Data = (APTR)&temp;
  260.    mySerReq->IOSer.io_Length = 1;
  261.    mySerReq->IOSer.io_Command = CMD_READ;
  262.    if (DoIO(mySerReq)) {
  263.       return -1;                   /* error occured, flag with -1 */
  264.    } 
  265.    return temp;
  266.  
  267. }
  268.  
  269.