home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume29 / libdes / part01 / read_password.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-04  |  2.6 KB  |  149 lines

  1. /* read_password.c */
  2. /* Copyright (C) 1992 Eric Young - see COPYING for more details */
  3. #include "des_local.h"
  4. #include <string.h>
  5. #include <signal.h>
  6. #include <sgtty.h>
  7. #include <sys/ioctl.h>
  8. #include <setjmp.h>
  9.  
  10. static int read_pw();
  11. static void recsig();
  12. static void pushsig();
  13. static void popsig();
  14.  
  15. static void (*savsig[NSIG])();
  16. static jmp_buf save;
  17.  
  18. int des_read_password(key,prompt,verify)
  19. des_cblock *key;
  20. char *prompt;
  21. int verify;
  22.     {
  23.     int ok;
  24.     char buf[BUFSIZ],buff[BUFSIZ];
  25.  
  26.     if ((ok=read_pw(buf,buff,BUFSIZ,prompt,verify)) == 0)
  27.         des_string_to_key(buf,key);
  28.     bzero(buf,BUFSIZ);
  29.     bzero(buff,BUFSIZ);
  30.     return(ok);
  31.     }
  32.  
  33. int des_read_pw_string(buf,length,prompt,verify)
  34. char *buf;
  35. int length;
  36. char *prompt;
  37. int verify;
  38.     {
  39.     char buff[BUFSIZ];
  40.     int ret;
  41.  
  42.     ret=read_pw(buf,buff,(length>BUFSIZ)?BUFSIZ:length,prompt,verify);
  43.     bzero(buff,BUFSIZ);
  44.     return(ret);
  45.     }
  46.  
  47. static read_till_nl(in)
  48. FILE *in;
  49.     {
  50. #define SIZE 4
  51.     char buf[SIZE+1];
  52.  
  53.     do    {
  54.         fgets(buf,SIZE,in);
  55.         } while (index(buf,'\n') == NULL);
  56.     }
  57.  
  58. /* return 0 if ok, 1 (or -1) otherwise */
  59. static int read_pw(buf,buff,size,prompt,verify)
  60. char *buf,*buff;
  61. int size;
  62. char *prompt;
  63. int verify;
  64.     {
  65.     struct sgttyb tty_orig,tty_new;
  66.     int ok=0;
  67.     char *p;
  68.     int ps=0;
  69.     FILE *tty;
  70.  
  71.     if ((tty=fopen("/dev/tty","r")) == NULL)
  72.         tty=stdin;
  73.     if (ioctl(fileno(tty),TIOCGETP,(char *)&tty_orig) == -1)
  74.         return(-1);
  75.     bcopy(&(tty_orig),&(tty_new),sizeof(tty_orig));
  76.     if (setjmp(save))
  77.         {
  78.         ok=0;
  79.         goto error;
  80.         }
  81.     pushsig();
  82.     ps=1;
  83.     tty_new.sg_flags &= ~ECHO;
  84.     if (ioctl(fileno(tty),TIOCSETP,(char *)&tty_new) == -1)
  85.         return(-1);
  86.     ps=2;
  87.  
  88.     while (!ok)
  89.         {
  90.         fputs(prompt,stderr);
  91.         fflush(stderr);
  92.  
  93.         buf[0]='\0';
  94.         fgets(buf,size,tty);
  95.         if (feof(tty)) goto error;
  96.         if ((p=(char *)index(buf,'\n')) != NULL)
  97.             *p='\0';
  98.         else    read_till_nl(tty);
  99.         if (verify)
  100.             {
  101.             fprintf(stderr,"\nVerifying password %s",prompt);
  102.             fflush(stderr);
  103.             buff[0]='\0';
  104.             fgets(buff,size,tty);
  105.             if (feof(tty)) goto error;
  106.             if ((p=(char *)index(buff,'\n')) != NULL)
  107.                 *p='\0';
  108.             else    read_till_nl(tty);
  109.                 
  110.             if (strcmp(buf,buff) != 0)
  111.                 {
  112.                 fprintf(stderr,"\nVerify failure - try again\n");
  113.                 fflush(stderr);
  114.                 continue;
  115.                 }
  116.             }
  117.         ok=1;
  118.         }
  119.  
  120. error:
  121.     fprintf(stderr,"\n");
  122.     /* What can we do if there is an error? */
  123.     if (ps >= 2) ioctl(fileno(tty),TIOCSETP,(char *)&tty_orig);
  124.     if (ps >= 1) popsig();
  125.     if (stdin != tty) fclose(tty);
  126.     return(!ok);
  127.     }
  128.  
  129. static void pushsig()
  130.     {
  131.     int i;
  132.  
  133.     for (i=0; i<NSIG; i++)
  134.         savsig[i]=signal(i,recsig);
  135.     }
  136.  
  137. static void popsig()
  138.     {
  139.     int i;
  140.  
  141.     for (i=0; i<NSIG; i++)
  142.         signal(i,savsig[i]);
  143.     }
  144.  
  145. static void recsig()
  146.     {
  147.     longjmp(save,1);
  148.     }
  149.