home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Product / Product.zip / sc621_3.zip / src / crypt.c < prev    next >
C/C++ Source or Header  |  1992-06-01  |  4KB  |  194 lines

  1. /*
  2.  * Encryption utilites
  3.  * Bradley Williams    
  4.  * {allegra,ihnp4,uiucdcs,ctvax}!convex!williams
  5.  * $Revision: 6.21 $
  6.  */
  7.  
  8. #if !defined(VMS) && !defined(MSDOS) && defined(CRYPT_PATH)
  9.  
  10. #include <stdio.h>
  11. #include <curses.h>
  12.  
  13. #if defined(BSD42) || defined(BSD43)
  14. #include <sys/types.h>
  15. #include <sys/file.h>
  16. #else
  17. #include <fcntl.h>
  18. #endif
  19.  
  20. #include "sc.h"
  21.  
  22. char        *getpass();
  23.  
  24. #ifdef SYSV3
  25. void exit();
  26. #endif
  27.  
  28. int         Crypt = 0;
  29. #define MAXKEYWORDSIZE 30
  30. char        KeyWord[MAXKEYWORDSIZE] = {""};
  31.  
  32. void
  33. creadfile (save, eraseflg)
  34. char *save;
  35. int  eraseflg;
  36. {
  37.     register FILE *f;
  38.     int pipefd[2];
  39.     int fildes;
  40.     int pid;
  41.  
  42.     if (eraseflg && strcmp(save, curfile) && modcheck(" first")) return;
  43.  
  44.     if ((fildes = open(findhome(save), O_RDONLY, 0)) < 0)
  45.     {
  46.     error ("Can't read file \"%s\"", save);
  47.     return;
  48.     }
  49.  
  50.     if (eraseflg) erasedb ();
  51.  
  52.     if (pipe(pipefd) < 0) {
  53.     error("Can't make pipe to child");
  54.     return;
  55.     }
  56.  
  57.     deraw();
  58.     (void) strcpy(KeyWord, getpass("Enter key:"));
  59.     goraw();
  60.  
  61.     if ((pid=fork()) == 0)              /* if child  */
  62.     {
  63.     (void) close (0);          /* close stdin */
  64.     (void) close (1);          /* close stdout */
  65.     (void) close (pipefd[0]);      /* close pipe input */
  66.     (void) dup (fildes);          /* standard in from file */
  67.     (void) dup (pipefd[1]);          /* connect to pipe */
  68.     (void) fprintf (stderr, " ");
  69.     (void) execl (CRYPT_PATH, "crypt", KeyWord, 0);
  70.     (void) fprintf (stderr, "execl(%s, \"crypt\", %s, 0) in creadfile() failed",
  71.             CRYPT_PATH, KeyWord);
  72.     exit (-127);
  73.     }
  74.     else                  /* else parent */
  75.     {
  76.     (void) close (fildes);
  77.     (void) close (pipefd[1]);      /* close pipe output */
  78.     if ((f = fdopen (pipefd[0], "r")) == (FILE *)0)
  79.     {
  80.         (void) kill (pid, -9);
  81.         error ("Can't fdopen file \"%s\"", save);
  82.         (void) close (pipefd[0]);
  83.         return;
  84.     }
  85.     }
  86.  
  87.     loading++;
  88.     while (fgets(line,sizeof line,f)) {
  89.     linelim = 0;
  90.     if (line[0] != '#') (void) yyparse ();
  91.     }
  92.     --loading;
  93.     (void) fclose (f);
  94.     (void) close (pipefd[0]);
  95.     while (pid != wait(&fildes)) /**/;
  96.     linelim = -1;
  97.     modflg++;
  98.     if (eraseflg) {
  99.     (void) strcpy (curfile, save);
  100.     modflg = 0;
  101.     }
  102.     EvalAll();
  103. }
  104.  
  105. int
  106. cwritefile (fname, r0, c0, rn, cn)
  107. char *fname;
  108. int r0, c0, rn, cn;
  109. {
  110.     register FILE *f;
  111.     int pipefd[2];
  112.     int fildes;
  113.     int pid;
  114.     char save[PATHLEN];
  115.     char *fn;
  116.     char *busave;
  117.  
  118.     if (*fname == '\0') fname = &curfile[0];
  119.  
  120.     fn = fname;
  121.     while (*fn && (*fn == ' '))  /* Skip leading blanks */
  122.     fn++;
  123.  
  124.     if ( *fn == '|' ) {
  125.     error ("Can't have encrypted pipe");
  126.     return(-1);
  127.     }
  128.  
  129.     (void) strcpy(save,fname);
  130.  
  131.     busave = findhome(save);
  132. #ifdef DOBACKUPS
  133.     if (!backup_file(busave) &&
  134.     (yn_ask("Could not create backup copy, Save anyhow?: (y,n)") != 1))
  135.         return(0);
  136. #endif
  137.     if ((fildes = open (busave, O_TRUNC|O_WRONLY|O_CREAT, 0600)) < 0)
  138.     {
  139.     error ("Can't create file \"%s\"", save);
  140.     return(-1);
  141.     }
  142.  
  143.     if (pipe (pipefd) < 0) {
  144.     error ("Can't make pipe to child\n");
  145.     return(-1);
  146.     }
  147.  
  148.     if (KeyWord[0] == '\0') {
  149.     deraw();
  150.     (void) strcpy(KeyWord, getpass("Enter key:"));
  151.     goraw();
  152.     }
  153.  
  154.     if ((pid=fork()) == 0)              /* if child  */
  155.     {
  156.     (void) close (0);              /* close stdin */
  157.     (void) close (1);              /* close stdout */
  158.     (void) close (pipefd[1]);          /* close pipe output */
  159.     (void) dup (pipefd[0]);              /* connect to pipe input */
  160.     (void) dup (fildes);              /* standard out to file */
  161.     (void) fprintf (stderr, " ");
  162.     (void) execl (CRYPT_PATH, "crypt", KeyWord, 0);
  163.     (void) fprintf (stderr, "execl(%s, \"crypt\", %s, 0) in cwritefile() failed",
  164.             CRYPT_PATH, KeyWord);
  165.     exit (-127);
  166.     }
  167.     else                  /* else parent */
  168.     {
  169.     (void) close (fildes);
  170.     (void) close (pipefd[0]);          /* close pipe input */
  171.     f = fdopen (pipefd[1], "w");
  172.     if (f == 0)
  173.     {
  174.         (void) kill (pid, -9);
  175.         error ("Can't fdopen file \"%s\"", save);
  176.         (void) close (pipefd[1]);
  177.         return(-1);
  178.     }
  179.     }
  180.  
  181.     write_fd(f, r0, c0, rn, cn);
  182.  
  183.     (void) fclose (f);
  184.     (void) close (pipefd[1]);
  185.     while (pid != wait(&fildes)) /**/;
  186.     (void) strcpy(curfile,save);
  187.  
  188.     modflg = 0;
  189.     error ("File \"%s\" written (encrypted).", curfile);
  190.     return(0);
  191. }
  192.  
  193. #endif /* CRYPT_PATH */
  194.