home *** CD-ROM | disk | FTP | other *** search
/ pc.louisiana.edu/pub/unix/ / Louisiana_UNIX.tar / Louisiana_UNIX / xspread3.0.zoo / crypt.c < prev    next >
C/C++ Source or Header  |  1994-03-25  |  6KB  |  270 lines

  1. /*
  2.  * Encryption utilites
  3.  * Bradley Williams    
  4.  * {allegra,ihnp4,uiucdcs,ctvax}!convex!williams
  5.  * $Revision: 6.21 A $
  6.  */
  7. #include "config.h"
  8.  
  9. #if !defined(VMS) && !defined(MSDOS) && defined(CRYPT_PATH)
  10.  
  11. #include <stdio.h>
  12. #ifdef DOINGX
  13. #include <X11/Xlib.h>
  14. #include <X11/Xutil.h>
  15. #else
  16. #include <curses.h>
  17. #endif
  18.  
  19. #if defined(BSD42) || defined(BSD43)
  20. #include <sys/types.h>
  21. #include <sys/file.h>
  22. #else
  23. #include <fcntl.h>
  24. #endif
  25.  
  26. #include "sc.h"
  27.  
  28. char        *getpass();
  29.  
  30. #ifdef SYSV3
  31. void exit();
  32. #endif
  33.  
  34. int         Crypt = 0;        /* Set the default not to use crypt? */
  35.  
  36. void
  37. creadfile (save, eraseflg)
  38. char *save;
  39. int  eraseflg;
  40. {
  41.     register FILE *f = NULL;
  42.     int pipefd[2];
  43.     int fildes;
  44.     int pid;
  45.     char *key, prompt[20];
  46.     char cryptcommand[30];
  47.  
  48. #ifndef DES
  49.     strcpy(cryptcommand, "crypt ");
  50. #else
  51.     strcpy(cryptcommand, "des -b -d -k ");
  52. #endif
  53.  
  54.     if (eraseflg && strcmp(save, curfile) && modcheck(" first")) 
  55.     return;
  56.  
  57.     if ((fildes = open(findhome(save), O_RDONLY, 0)) < 0)
  58.     {
  59.     sprintf(stringbuf, "Can't read file \"%s\"", save);
  60.     error(stringbuf);
  61.     return;
  62.     }
  63.  
  64.     if (eraseflg) 
  65.     erasedb();
  66.  
  67.     if (pipe(pipefd) < 0) {
  68.     error("Can't make pipe to child");
  69.     return;
  70.     }
  71.  
  72. #ifndef DOINGX
  73.     deraw();
  74.     (void) strcat(cryptcommand, getpass("Enter key:"));
  75.     goraw();
  76. #else
  77.     /*  Get a non-null key value
  78.      * 
  79.      */
  80.     strcpy(prompt, "Enter key:");
  81.     while(strlen(key = get_str(prompt, 9)) == 0)
  82.        strcpy(prompt, "Enter key:");
  83.  
  84.     /*  Append the key to the crypt/des command
  85.      *
  86.      */
  87.     strcat(cryptcommand, key);
  88. #endif
  89.  
  90.     if ((pid=fork()) == 0)              /* if child  */
  91.     {
  92.     (void) close (0);          /* close stdin */
  93.     (void) close (1);          /* close stdout */
  94.     (void) close (pipefd[0]);      /* close pipe input */
  95.     (void) dup (fildes);          /* standard in from file */
  96.     (void) dup (pipefd[1]);          /* connect to pipe */
  97.     (void) fprintf (stderr, " ");
  98.     (void) execl ("/bin/sh", "sh", "-c", cryptcommand, 0);
  99.     (void) fprintf (stderr, "execl failed in creadfile() running %s \n",
  100.             cryptcommand);
  101.     exit (-127);
  102.     }
  103.     else                  /* else parent */
  104.     {
  105.     (void) close (fildes);
  106.     (void) close (pipefd[1]);      /* close pipe output */
  107.     if ((f = fdopen (pipefd[0], "r")) == (FILE *)0)
  108.     {
  109.         (void) kill (pid, -9);
  110.         sprintf(stringbuf, "Can't fdopen file \"%s\"", save);
  111.         error(stringbuf);
  112.         (void) close (pipefd[0]);
  113.         return;
  114.     }
  115.     }
  116.  
  117.     loading++;
  118.     while (fgets(line,sizeof line,f)) {
  119.     linelim = 0;
  120.     if (line[0] != '#') (void) yyparse ();
  121.     }
  122.     --loading;
  123.     (void) fclose (f);
  124.     (void) close (pipefd[0]);
  125.     while (pid != wait(&fildes)) /**/;
  126.     linelim = -1;
  127.     modflg++;
  128.     if (eraseflg) {
  129.     (void) strcpy (curfile, save);
  130.     modflg = 0;
  131.     }
  132.     EvalAll();
  133. }
  134.  
  135. int
  136. cwritefile (fname, r0, c0, rn, cn)
  137. char *fname;
  138. int r0, c0, rn, cn;
  139. {
  140.     register FILE *f;
  141.     int pipefd[2];
  142.     int fildes;
  143.     int pid;
  144.     char save[PATHLEN];
  145.     char *fn;
  146.     char *busave;
  147.     char key1[10], key2[10], *key, prompt[20];
  148.     char cryptcommand[30];
  149.  
  150. #ifndef DES
  151.     strcpy(cryptcommand, "crypt ");
  152. #else
  153.     strcpy(cryptcommand, "des -b -e -k ");
  154. #endif
  155.  
  156.     if (*fname == '\0') 
  157.     fname = &curfile[0];
  158.  
  159.     fn = fname;
  160.     while (*fn && (*fn == ' '))  /* Skip leading blanks */
  161.     fn++;
  162.  
  163.     if ( *fn == '|' ) {
  164.     error ("Can't have encrypted pipe");
  165.     return(-1);
  166.     }
  167.  
  168.     (void) strcpy(save, fname);
  169.  
  170.     busave = findhome(save);
  171. #ifdef DOBACKUPS
  172.     if (!backup_file(busave) &&
  173.     (yn_ask("Could not create backup copy, Save anyhow?: (y,n)") != 1))
  174.         return(0);
  175. #endif
  176.     if ((fildes = open (busave, O_TRUNC|O_WRONLY|O_CREAT, 0600)) < 0)
  177.     {
  178.     sprintf(stringbuf, "Can't create file \"%s\"", save);
  179.     error(stringbuf);
  180.     return(-1);
  181.     }
  182.  
  183.     if (pipe (pipefd) < 0) {
  184.     error ("Can't make pipe to child\n");
  185.     return(-1);
  186.     }
  187.  
  188. #ifndef DOINGX
  189.     deraw();
  190.     (void) strcat(cryptcommand, getpass("Enter key:"));
  191.     goraw();
  192. #else
  193.      /* added [jclark:19920930.1259CST]
  194.       *
  195.       * Neither `crypt' nor `des' double-prompt for the encryption key.
  196.       * If you mistype the key, and aren't sure what you've typed, you're
  197.       * screwed.  This code prompts for a key, requires that it be confirmed,
  198.       * and then passes the key to the encryption program.
  199.       *
  200.       */
  201.  
  202.      /* get a non-null key value
  203.       *
  204.       */
  205.      strcpy(prompt, "Enter Key:");
  206.      while (strlen(key = get_str(prompt,9)) == 0) 
  207.            strcpy(prompt, "Enter Key:");
  208.  
  209.      strcpy(key1, key);
  210.  
  211.      /* require the key to be re-typed
  212.       *
  213.       */
  214.      strcpy(prompt, "Confirm Key:");
  215.      while (strlen(key = get_str(prompt,9)) == 0) 
  216.         strcpy(prompt, "Confirm Key:");
  217.  
  218.      strcpy(key2, key);
  219.  
  220.      /* if the keys don't match, Quit!
  221.       *
  222.       */
  223.      if (strcmp(key1, key2) != 0) {
  224.          error("Keys do not match!");
  225.          return(-1);
  226.      }
  227.  
  228.      strcat(cryptcommand, key1);  /* append the key to the crypt/des command */
  229. #endif
  230.  
  231.     if ((pid=fork()) == 0)              /* if child  */
  232.     {
  233.     (void) close (0);              /* close stdin */
  234.     (void) close (1);              /* close stdout */
  235.     (void) close (pipefd[1]);          /* close pipe output */
  236.     (void) dup (pipefd[0]);              /* connect to pipe input */
  237.     (void) dup (fildes);              /* standard out to file */
  238.     (void) fprintf (stderr, " ");
  239.     (void) execl ("/bin/sh", "sh", "-c", cryptcommand, 0);
  240.     (void) fprintf (stderr, "execl failure in cwritefile doing %s \n",
  241.             cryptcommand);
  242.     exit (-127);
  243.     }
  244.         /*  Else if parent */
  245.     (void) close (fildes);
  246.     (void) close (pipefd[0]);          /* close pipe input */
  247.     if (NULL == ( f = fdopen (pipefd[1], "w")))
  248.     {
  249.     (void) kill (pid, -9);
  250.     sprintf(stringbuf, "Can't fdopen file \"%s\"", save);
  251.     error(stringbuf);
  252.     (void) close (pipefd[1]);
  253.     return(-1);
  254.     }
  255.     else
  256.     {
  257.     write_fd(f, r0, c0, rn, cn);
  258.     (void) fclose (f);
  259.     (void)  close (pipefd[1]);
  260.     while (pid != wait(&fildes)) /**/;
  261.     (void) strcpy(curfile, save);
  262.     modflg = 0;
  263.     sprintf(stringbuf, "File \"%s\" written (encrypted).", curfile);
  264.     error(stringbuf);
  265.     }
  266.     return(0);
  267. }
  268.  
  269. #endif /* CRYPT */
  270.