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