File: crypt.c

package info (click to toggle)
xspread 3.1.1c-9
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,836 kB
  • ctags: 1,426
  • sloc: ansic: 19,342; yacc: 499; lisp: 231; makefile: 198; sh: 28; sed: 4
file content (281 lines) | stat: -rw-r--r-- 6,147 bytes parent folder | download | duplicates (2)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * Encryption utilites
 * Bradley Williams	
 * {allegra,ihnp4,uiucdcs,ctvax}!convex!williams
 * $Revision: 1.1 $
 */
#include <config.h>

#if !defined(VMS) && !defined(MSDOS) && defined(CRYPT_PATH)

#include <curses.h>

#ifdef HAVE_X11_X_H
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "scXstuff.h"
#endif /* HAVE_X11_X_H */

#if defined(BSD42) || defined(BSD43) && !defined(SYSV)
#include <sys/types.h>
#include <sys/file.h>
#else
#include <fcntl.h>
#endif

#include "sc.h"
#include <signal.h>

char        *getpass();

#ifdef SYSV3
void exit();
#endif

int         Crypt = 0;		/* Set the default not to use crypt? */

void
creadfile (save, eraseflg)
char *save;
int  eraseflg;
{
    register FILE *f = NULL;
    int pipefd[2];
    int fildes;
    int pid;
    char cryptcommand[30];
#ifdef HAVE_X11_X_H
    char *key, prompt[20];
#endif

#ifndef DES
    strcpy(cryptcommand, "crypt ");
#else
    strcpy(cryptcommand, "des -b -d -k ");
#endif

    if (eraseflg && strcmp(save, curfile) && modcheck(" first")) 
	return;

    if ((fildes = open(findhome(save), O_RDONLY, 0)) < 0)
    {
	sprintf(stringbuf, "Can't read file \"%s\"", save);
	scerror(stringbuf);
	return;
    }

    if (eraseflg) 
	erasedb();

    if (pipe(pipefd) < 0) {
	scerror("Can't make pipe to child");
	return;
    }

#ifdef HAVE_X11_X_H
    if (using_X)
    {
	/*  Get a non-null key value
	 * 
	 */
	strcpy(prompt, "Enter key:");
	while(strlen(key = get_str(prompt, 9)) == 0)
		strcpy(prompt, "Enter key:");

	/*  Append the key to the crypt/des command
	 *
	 */
	strcat(cryptcommand, key);
    } else
#endif /* HAVE_X11_X_H */
    {	deraw();
	(void) strcat(cryptcommand, getpass("Enter key:"));
	goraw();
    } /* HAVE_X11_X_H, end curses */

    if ((pid=fork()) == 0)			  /* if child  */
    {
	(void) close (0);		  /* close stdin */
	(void) close (1);		  /* close stdout */
	(void) close (pipefd[0]);	  /* close pipe input */
	(void) dup (fildes);		  /* standard in from file */
	(void) dup (pipefd[1]);		  /* connect to pipe */
	(void) fprintf (stderr, " ");
	(void) execl ("/bin/sh", "sh", "-c", cryptcommand, 0);
	(void) fprintf (stderr, "execl failed in creadfile() running %s \n",
			cryptcommand);
	exit (-127);
    }
    else				  /* else parent */
    {
	(void) close (fildes);
	(void) close (pipefd[1]);	  /* close pipe output */
	if ((f = fdopen (pipefd[0], "r")) == (FILE *)0)
	{
	    (void) kill (pid, -9);
	    sprintf(stringbuf, "Can't fdopen file \"%s\"", save);
	    scerror(stringbuf);
	    (void) close (pipefd[0]);
	    return;
	}
    }

    loading++;
    while (fgets(line,sizeof line,f)) {
	linelim = 0;
	if (line[0] != '#') (void) yyparse ();
    }
    --loading;
    (void) fclose (f);
    (void) close (pipefd[0]);
    while (pid != wait(&fildes)) /**/;
    linelim = -1;
    modflg++;
    if (eraseflg) {
	(void) strcpy (curfile, save);
	modflg = 0;
    }
    EvalAll();
}

int
cwritefile (fname, r0, c0, rn, cn)
char *fname;
int r0, c0, rn, cn;
{
    register FILE *f;
    int pipefd[2];
    int fildes;
    int pid;
    char save[PATHLEN];
    char *fn;
    char *busave;
    char cryptcommand[30];
#ifdef HAVE_X11_X_H
    char key1[10], key2[10], *key, prompt[20];
#endif

#ifndef DES
    strcpy(cryptcommand, "crypt ");
#else
    strcpy(cryptcommand, "des -b -e -k ");
#endif

    if (*fname == '\0') 
	fname = &curfile[0];

    fn = fname;
    while (*fn && (*fn == ' '))  /* Skip leading blanks */
	fn++;

    if ( *fn == '|' ) {
	scerror("Can't have encrypted pipe");
	return(-1);
    }

    (void) strcpy(save, fname);

    busave = findhome(save);
#ifdef DOBACKUPS
    if (!backup_file(busave) &&
	(yn_ask("Could not create backup copy, Save anyhow?: (y,n)") != 1))
		return(0);
#endif
    if ((fildes = open (busave, O_TRUNC|O_WRONLY|O_CREAT, 0600)) < 0)
    {
	sprintf(stringbuf, "Can't create file \"%s\"", save);
	scerror(stringbuf);
	return(-1);
    }

    if (pipe (pipefd) < 0) {
	scerror("Can't make pipe to child\n");
	return(-1);
    }

#ifdef HAVE_X11_X_H
     if (using_X)
     {
	/* added [jclark:19920930.1259CST]
	 *
	 * Neither `crypt' nor `des' double-prompt for the encryption key.
	 * If you mistype the key, and aren't sure what you've typed, you're
	 * screwed.  This code prompts for a key, requires that it be confirmed,
	 * and then passes the key to the encryption program.
	 *
	 */
 
	/* get a non-null key value
	 *
	 */
	strcpy(prompt, "Enter Key:");
	while (strlen(key = get_str(prompt,9)) == 0) 
		strcpy(prompt, "Enter Key:");

	strcpy(key1, key);
 
	/* require the key to be re-typed
	 *
	 */
	strcpy(prompt, "Confirm Key:");
	while (strlen(key = get_str(prompt,9)) == 0) 
		strcpy(prompt, "Confirm Key:");

	strcpy(key2, key);

	/* if the keys don't match, Quit!
	 *
	 */
	if (strcmp(key1, key2) != 0) {
		scerror("Keys do not match!");
		return(-1);
	}
 
	/* append the key to the crypt/des command */
	strcat(cryptcommand, key1);
     } else
#endif /* HAVE_X11_X_H */
     {	deraw();
	(void) strcat(cryptcommand, getpass("Enter key:"));
	goraw();
     } /* HAVE_X11_X_H, end curses */

    if ((pid=fork()) == 0)			  /* if child  */
    {
	(void) close (0);			  /* close stdin */
	(void) close (1);			  /* close stdout */
	(void) close (pipefd[1]);		  /* close pipe output */
	(void) dup (pipefd[0]);			  /* connect to pipe input */
	(void) dup (fildes);			  /* standard out to file */
	(void) fprintf (stderr, " ");
	(void) execl ("/bin/sh", "sh", "-c", cryptcommand, 0);
	(void) fprintf (stderr, "execl failure in cwritefile doing %s \n",
			cryptcommand);
	exit (-127);
    }
		/*  Else if parent */
    (void) close (fildes);
    (void) close (pipefd[0]);		  /* close pipe input */
    if (NULL == ( f = fdopen (pipefd[1], "w")))
    {
	(void) kill (pid, -9);
	sprintf(stringbuf, "Can't fdopen file \"%s\"", save);
	scerror(stringbuf);
	(void) close (pipefd[1]);
	return(-1);
    }
    else
    {
	write_fd(f, r0, c0, rn, cn);
	(void) fclose (f);
	(void)  close (pipefd[1]);
	while (pid != wait(&fildes)) /**/;
	(void) strcpy(curfile, save);
	modflg = 0;
	sprintf(stringbuf, "File \"%s\" written (encrypted).", curfile);
	scerror(stringbuf);
    }
    return(0);
}

#endif /* CRYPT */