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