forked from n-t-roff/sc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypt.c
181 lines (157 loc) · 4.92 KB
/
crypt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* SC A Table Calculator
* Encryption utilites
*
* Bradley Williams
* {allegra,ihnp4,uiucdcs,ctvax}!convex!williams
* updated by Charlie Gordon: June, 2021
*
* $Revision: 9.1 $
*/
#include <sys/file.h>
#include <fcntl.h>
#include "sc.h"
#ifndef NOCRYPT
int Crypt = 0;
#define MAXKEYWORDSIZE 30
static char KeyWord[MAXKEYWORDSIZE];
int creadfile(sheet_t *sp, const char *fname, int eraseflg) {
char save[FBUFLEN];
char buf[FBUFLEN];
FILE *f;
int pipefd[2];
int fildes;
int pid;
char *p;
pstrcpy(save, sizeof save, fname);
if (eraseflg && strcmp(fname, sp->curfile) && modcheck(" first"))
return 0;
if ((fildes = open(findhome(save, sizeof save), O_RDONLY, 0)) < 0) {
error("Cannot read file \"%s\"", save);
return 0;
}
if (eraseflg) {
erasedb(sp);
load_scrc(sp);
}
if (pipe(pipefd) < 0) {
error("Cannot make pipe to child");
return 0;
}
screen_deraw(1);
pstrcpy(KeyWord, sizeof KeyWord, getpass("Enter key:"));
screen_goraw();
if ((pid = fork()) == 0) { /* if child */
close(0); /* close stdin */
close(1); /* close stdout */
close(pipefd[0]); /* close pipe input */
dup(fildes); /* standard in from file */
dup(pipefd[1]); /* connect to pipe */
fprintf(stderr, " ");
execl(CRYPT_PATH, "crypt", KeyWord, 0);
fprintf(stderr, "execl(%s, \"crypt\", %s, 0) in creadfile() failed",
CRYPT_PATH, KeyWord);
exit(-127);
} else { /* else parent */
close(fildes);
close(pipefd[1]); /* close pipe output */
if ((f = fdopen(pipefd[0], "r")) == NULL) {
kill(pid, 9);
error("Cannot fdopen file \"%s\"", save);
close(pipefd[0]);
return 0;
}
}
loading++;
while (fgets(buf, sizeof buf, f)) {
p = buf;
while (*p == ' ') {
/* skip initial blanks */
p++;
}
if (*p == '#' || *p == '\0' || *p == '\n') {
/* ignore comments and blank lines */
continue;
}
parse_line(buf);
}
--loading;
if (fclose(f)) {
error("fclose(pipefd): %s", strerror(errno));
}
close(pipefd[0]);
while (pid != wait(&fildes))
continue;
if (eraseflg) {
pstrcpy(sp->curfile, sizeof sp->curfile, save);
sp->modflg = 0;
}
return 1;
}
int cwritefile(sheet_t *sp, const char *fname, rangeref_t rr, int dcp_flags) {
char path[PATHLEN];
FILE *f;
int pipefd[2];
int fildes;
int pid;
char *fn;
if (*fname == '\0') fname = sp->curfile;
fn = fname;
while (*fn && (*fn == ' ')) /* Skip leading blanks */
fn++;
if (*fn == '|') {
error("Cannot have encrypted pipe");
return -1;
}
pstrcpy(path, sizeof path, fname);
findhome(path, sizeof path);
if (dobackups && !backup_file(path) &&
(yn_ask("Could not create backup copy, Save anyway?: (y,n)") != 1))
return 0;
if ((fildes = open(path, O_TRUNC|O_WRONLY|O_CREAT, 0600)) < 0) {
error("Cannot create file \"%s\"", path);
return -1;
}
if (pipe(pipefd) < 0) {
error("Cannot make pipe to child\n");
return -1;
}
if (KeyWord[0] == '\0') {
screen_deraw(1);
pstrcpy(KeyWord, sizeof KeyWord, getpass("Enter key:"));
screen_goraw();
}
if ((pid = fork()) == 0) { /* if child */
close(0); /* close stdin */
close(1); /* close stdout */
close(pipefd[1]); /* close pipe output */
dup(pipefd[0]); /* connect to pipe input */
dup(fildes); /* standard out to file */
fprintf(stderr, " ");
execl(CRYPT_PATH, "crypt", KeyWord, 0);
fprintf(stderr, "execl(%s, \"crypt\", %s, 0) in cwritefile() failed",
CRYPT_PATH, KeyWord);
exit(-127);
} else { /* else parent */
close(fildes);
close(pipefd[0]); /* close pipe input */
f = fdopen(pipefd[1], "w");
if (f == 0) {
kill(pid, -9);
error("Cannot fdopen file \"%s\"", path);
close(pipefd[1]);
return -1;
}
}
write_fd(f, rr, dcp_flags);
if (fclose(f) == EOF) {
error("fclose(pipefd): %s", strerror(errno));
}
close(pipefd[1]);
while (pid != wait(&fildes))
continue;
error("File \"%s\" written (encrypted).", path);
pstrcpy(sp->curfile, sizeof sp->curfile, path);
sp->modflg = 0;
return 0;
}
#endif /* NOCRYPT */