1 | /* This file is stolen and slightly modified code */ |
---|
2 | |
---|
3 | /* zcrypt.c -- Read in a data stream from stdin & dump a decrypted/encrypted * |
---|
4 | * datastream. Reads the string to make the key from from the first * |
---|
5 | * parameter. Encrypts or decrypts according to -d or -e flag. (-e is * |
---|
6 | * default.) Will invoke zwrite if the -c option is provided for * |
---|
7 | * encryption. If a zephyr class is specified & the keyfile name omitted * |
---|
8 | * the ~/.crypt-table will be checked for "crypt-classname" and then * |
---|
9 | * "crypt-default" for the keyfile name. */ |
---|
10 | |
---|
11 | static const char fileIdent[] = "$Id$"; |
---|
12 | |
---|
13 | #include <stdio.h> |
---|
14 | #include <stdlib.h> |
---|
15 | #include <string.h> |
---|
16 | #include <sys/types.h> |
---|
17 | #include <sys/wait.h> |
---|
18 | #include "owl.h" |
---|
19 | |
---|
20 | #ifdef OWL_ENABLE_ZCRYPT |
---|
21 | |
---|
22 | #define BASE_CODE 70 |
---|
23 | #define LAST_CODE (BASE_CODE + 15) |
---|
24 | #define OUTPUT_BLOCK_SIZE 16 |
---|
25 | #include <unistd.h> |
---|
26 | #include <sys/types.h> |
---|
27 | #include <des.h> |
---|
28 | |
---|
29 | #define MAX_KEY 128 |
---|
30 | |
---|
31 | #ifndef TRUE |
---|
32 | #define TRUE -1 |
---|
33 | #endif |
---|
34 | #ifndef FALSE |
---|
35 | #define FALSE 0 |
---|
36 | #endif |
---|
37 | |
---|
38 | #define ZWRITE_OPT_NOAUTH (1<<0) |
---|
39 | #define ZWRITE_OPT_SIGNATURE (1<<1) |
---|
40 | #define ZWRITE_OPT_IGNOREVARS (1<<2) |
---|
41 | #define ZWRITE_OPT_VERBOSE (1<<3) |
---|
42 | #define ZWRITE_OPT_QUIET (1<<4) |
---|
43 | #define ZCRYPT_OPT_MESSAGE (1<<5) |
---|
44 | #define ZCRYPT_OPT_IGNOREDOT (1<<6) |
---|
45 | |
---|
46 | typedef struct |
---|
47 | { |
---|
48 | int flags; |
---|
49 | char *signature; |
---|
50 | char *message; |
---|
51 | } ZWRITEOPTIONS; |
---|
52 | |
---|
53 | char *GetZephyrVarKeyFile(char *whoami, char *class, char *instance); |
---|
54 | char *BuildArgString(char **argv, int start, int end); |
---|
55 | static int do_encrypt(char *keystring, int zephyr, char *class, char *instance, ZWRITEOPTIONS *zoptions, char* keyfile); |
---|
56 | int do_decrypt(char *keystring); |
---|
57 | |
---|
58 | #ifndef HAVE_DES_ECB_ENCRYPT_PROTO |
---|
59 | int des_ecb_encrypt(char [], char [], des_key_schedule, int); |
---|
60 | #endif |
---|
61 | |
---|
62 | #define M_NONE 0 |
---|
63 | #define M_ZEPHYR_ENCRYPT 1 |
---|
64 | #define M_DECRYPT 2 |
---|
65 | #define M_ENCRYPT 3 |
---|
66 | #define M_RANDOMIZE 4 |
---|
67 | #define M_SETKEY 5 |
---|
68 | |
---|
69 | int zcrypt(int argc, char *argv[]) { |
---|
70 | char *fname = NULL; |
---|
71 | int error = FALSE; |
---|
72 | int zephyr = FALSE; |
---|
73 | char *class = NULL, *instance = NULL; |
---|
74 | int mode = M_NONE; |
---|
75 | |
---|
76 | extern int optind, opterr; |
---|
77 | extern char *optarg; |
---|
78 | char c; |
---|
79 | |
---|
80 | int messageflag = FALSE; |
---|
81 | ZWRITEOPTIONS zoptions; |
---|
82 | zoptions.flags = 0; |
---|
83 | |
---|
84 | while ((c = getopt(argc, argv, "ZDERSF:c:i:advqtluons:f:m")) != (char)EOF) { |
---|
85 | switch(c) { |
---|
86 | case 'Z': |
---|
87 | /* Zephyr encrypt */ |
---|
88 | mode = M_ZEPHYR_ENCRYPT; |
---|
89 | break; |
---|
90 | case 'D': |
---|
91 | /* Decrypt */ |
---|
92 | mode = M_DECRYPT; |
---|
93 | break; |
---|
94 | case 'E': |
---|
95 | /* Encrypt */ |
---|
96 | mode = M_ENCRYPT; |
---|
97 | break; |
---|
98 | case 'R': |
---|
99 | /* Randomize the keyfile */ |
---|
100 | mode = M_RANDOMIZE; |
---|
101 | break; |
---|
102 | case 'S': |
---|
103 | /* Set a new key value from stdin */ |
---|
104 | mode = M_SETKEY; |
---|
105 | break; |
---|
106 | case 'F': |
---|
107 | /* Specify the keyfile explicitly */ |
---|
108 | if (fname != NULL) error = TRUE; |
---|
109 | fname = optarg; |
---|
110 | break; |
---|
111 | case 'c': |
---|
112 | /* Zwrite/zcrypt: class name */ |
---|
113 | if (class != NULL) error = TRUE; |
---|
114 | class = optarg; |
---|
115 | break; |
---|
116 | case 'i': |
---|
117 | /* Zwrite/zcrypt: instance name */ |
---|
118 | if (instance != NULL) error = TRUE; |
---|
119 | instance = optarg; |
---|
120 | break; |
---|
121 | case 'a': |
---|
122 | /* Zwrite: authenticate (default) */ |
---|
123 | zoptions.flags &= ~ZWRITE_OPT_NOAUTH; |
---|
124 | break; |
---|
125 | case 'd': |
---|
126 | /* Zwrite: do not authenticate */ |
---|
127 | zoptions.flags |= ZWRITE_OPT_NOAUTH; |
---|
128 | break; |
---|
129 | case 'v': |
---|
130 | /* Zwrite: verbose */ |
---|
131 | zoptions.flags |= ZWRITE_OPT_VERBOSE; |
---|
132 | break; |
---|
133 | case 'q': |
---|
134 | /* Zwrite: quiet */ |
---|
135 | zoptions.flags |= ZWRITE_OPT_QUIET; |
---|
136 | break; |
---|
137 | case 't': |
---|
138 | /* Zwrite: no expand tabs (ignored) */ |
---|
139 | break; |
---|
140 | case 'l': |
---|
141 | /* Zwrite: ignore '.' on a line by itself (ignored) */ |
---|
142 | zoptions.flags |= ZCRYPT_OPT_IGNOREDOT; |
---|
143 | break; |
---|
144 | case 'u': |
---|
145 | /* Zwrite: urgent message */ |
---|
146 | instance = "URGENT"; |
---|
147 | break; |
---|
148 | case 'o': |
---|
149 | /* Zwrite: ignore zephyr variables zwrite-class, zwrite-inst, */ |
---|
150 | /* zwrite-opcode */ |
---|
151 | zoptions.flags |= ZWRITE_OPT_IGNOREVARS; |
---|
152 | break; |
---|
153 | case 'n': |
---|
154 | /* Zwrite: prevent PING message (always used) */ |
---|
155 | break; |
---|
156 | case 's': |
---|
157 | /* Zwrite: signature */ |
---|
158 | zoptions.flags |= ZWRITE_OPT_SIGNATURE; |
---|
159 | zoptions.signature = optarg; |
---|
160 | break; |
---|
161 | case 'f': |
---|
162 | /* Zwrite: file system specification (ignored) */ |
---|
163 | break; |
---|
164 | case 'm': |
---|
165 | /* Message on rest of line */ |
---|
166 | messageflag = TRUE; |
---|
167 | break; |
---|
168 | case '?': |
---|
169 | error = TRUE; |
---|
170 | break; |
---|
171 | } |
---|
172 | if (error || messageflag) break; |
---|
173 | } |
---|
174 | |
---|
175 | if (class != NULL || instance != NULL) { |
---|
176 | zephyr = TRUE; |
---|
177 | } |
---|
178 | |
---|
179 | if (messageflag) { |
---|
180 | zoptions.flags |= ZCRYPT_OPT_MESSAGE; |
---|
181 | zoptions.message = BuildArgString(argv, optind, argc); |
---|
182 | if (!zoptions.message) |
---|
183 | { |
---|
184 | printf("Memory allocation error.\n"); |
---|
185 | error = TRUE; |
---|
186 | } |
---|
187 | } else if (optind < argc) { |
---|
188 | error = TRUE; |
---|
189 | } |
---|
190 | |
---|
191 | if (mode == M_NONE) mode = (zephyr?M_ZEPHYR_ENCRYPT:M_ENCRYPT); |
---|
192 | |
---|
193 | if (mode == M_ZEPHYR_ENCRYPT && !zephyr) error = TRUE; |
---|
194 | |
---|
195 | if (!error && fname == NULL && (class != NULL || instance != NULL)) { |
---|
196 | fname = GetZephyrVarKeyFile(argv[0], class, instance); |
---|
197 | } |
---|
198 | |
---|
199 | |
---|
200 | if (error || fname == NULL) { |
---|
201 | printf("Usage: %s [-Z|-D|-E|-R|-S] [-F Keyfile] [-c class] [-i instance]\n", argv[0]); |
---|
202 | printf(" [-advqtluon] [-s signature] [-f arg] [-m message]\n"); |
---|
203 | printf(" One or more of class, instance, and keyfile must be specified.\n"); |
---|
204 | } else { |
---|
205 | if (mode == M_RANDOMIZE) { |
---|
206 | /* Choose a new, random key */ |
---|
207 | /* |
---|
208 | FILE *fkey = fopen(fname, "w"); |
---|
209 | if (!fkey) |
---|
210 | printf("Could not open key file for writing: %s\n", fname); |
---|
211 | else |
---|
212 | { |
---|
213 | char string[100]; |
---|
214 | fputs(fkey, string); |
---|
215 | fclose(fkey); |
---|
216 | } |
---|
217 | */ |
---|
218 | printf("Feature not yet implemented.\n"); |
---|
219 | } else if (mode == M_SETKEY) { |
---|
220 | /* Set a new, user-entered key */ |
---|
221 | char newkey[MAX_KEY]; |
---|
222 | FILE *fkey; |
---|
223 | |
---|
224 | if (isatty(0)) { |
---|
225 | printf("Enter new key: "); |
---|
226 | /* Really should read without echo!!! */ |
---|
227 | fgets(newkey, MAX_KEY - 1, stdin); |
---|
228 | } else { |
---|
229 | fgets(newkey, MAX_KEY - 1, stdin); |
---|
230 | } |
---|
231 | |
---|
232 | fkey = fopen(fname, "w"); |
---|
233 | if (!fkey) { |
---|
234 | printf("Could not open key file for writing: %s\n", fname); |
---|
235 | } else { |
---|
236 | if (fputs(newkey, fkey) != strlen(newkey) || putc('\n', fkey) != '\n') { |
---|
237 | printf("Error writing to key file.\n"); |
---|
238 | fclose(fkey); |
---|
239 | } else { |
---|
240 | fclose(fkey); |
---|
241 | printf("Key update complete.\n"); |
---|
242 | } |
---|
243 | } |
---|
244 | } else { |
---|
245 | /* Encrypt/decrypt */ |
---|
246 | FILE *fkey = fopen(fname, "r"); |
---|
247 | if (!fkey) { |
---|
248 | printf("Could not open key file: %s\n", fname); |
---|
249 | } else { |
---|
250 | char keystring[MAX_KEY]; |
---|
251 | fgets(keystring, MAX_KEY-1, fkey); |
---|
252 | if (mode == M_ZEPHYR_ENCRYPT || mode == M_ENCRYPT) { |
---|
253 | do_encrypt(keystring, (mode == M_ZEPHYR_ENCRYPT), class, instance, |
---|
254 | &zoptions, fname); |
---|
255 | } else { |
---|
256 | do_decrypt(keystring); |
---|
257 | } |
---|
258 | fclose(fkey); |
---|
259 | } |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | /* Always print the **END** message if -D is specified. */ |
---|
264 | if (mode == M_DECRYPT) printf("**END**\n"); |
---|
265 | |
---|
266 | exit(0); |
---|
267 | } |
---|
268 | |
---|
269 | /* The 'owl_zcrypt_decrypt' function was written by kretch for Owl. |
---|
270 | * Decrypt the message in 'in' on class 'class' and instance |
---|
271 | * 'instance' and leave the result in 'out'. Out must be a buffer |
---|
272 | * allocated by the caller. |
---|
273 | * |
---|
274 | * return 0 on success, otherwise -1 |
---|
275 | */ |
---|
276 | int owl_zcrypt_decrypt(char *out, char *in, char *class, char *instance) { |
---|
277 | char *fname, keystring[MAX_KEY], *inptr, *endptr; |
---|
278 | FILE *fkey; |
---|
279 | des_cblock key; |
---|
280 | des_key_schedule schedule; |
---|
281 | char input[8], output[9]; |
---|
282 | int i, c1, c2; |
---|
283 | |
---|
284 | fname=GetZephyrVarKeyFile("zcrypt", class, instance); |
---|
285 | if (!fname) return(-1); |
---|
286 | fkey=fopen(fname, "r"); |
---|
287 | if (!fkey) return(-1); |
---|
288 | fgets(keystring, MAX_KEY-1, fkey); |
---|
289 | fclose(fkey); |
---|
290 | |
---|
291 | strcpy(out, ""); |
---|
292 | |
---|
293 | output[0] = '\0'; /* In case no message at all */ |
---|
294 | output[8] = '\0'; /* NULL at end will limit string length to 8 */ |
---|
295 | |
---|
296 | des_string_to_key(keystring, key); |
---|
297 | des_key_sched(key, schedule); |
---|
298 | |
---|
299 | inptr=in; |
---|
300 | endptr=in+strlen(in)-1; |
---|
301 | while (inptr<endptr) { |
---|
302 | for (i=0; i<8; i++) { |
---|
303 | c1=(inptr[0])-BASE_CODE; |
---|
304 | c2=(inptr[1])-BASE_CODE; |
---|
305 | input[i]=c1 * 0x10 + c2; |
---|
306 | inptr+=2; |
---|
307 | } |
---|
308 | des_ecb_encrypt(input, output, schedule, FALSE); |
---|
309 | strcat(out, output); |
---|
310 | } |
---|
311 | |
---|
312 | if (output[0]) { |
---|
313 | if (output[strlen(output)-1] != '\n') { |
---|
314 | strcat(out, "\n"); |
---|
315 | } |
---|
316 | } else { |
---|
317 | strcat(out, "\n"); |
---|
318 | } |
---|
319 | return(0); |
---|
320 | } |
---|
321 | |
---|
322 | int owl_zcrypt_encrypt(char *out, char *in, char *class, char *instance) { |
---|
323 | /* static int do_encrypt(char *keystring, int zephyr, char *class, char *instance, ZWRITEOPTIONS *zoptions, char* keyfile) { */ |
---|
324 | char *fname, keystring[MAX_KEY]; |
---|
325 | FILE *fkey; |
---|
326 | des_cblock key; |
---|
327 | des_key_schedule schedule; |
---|
328 | char input[8], output[8]; |
---|
329 | int size, length, i; |
---|
330 | char *inbuff = NULL, *inptr; |
---|
331 | int use_buffer = FALSE; |
---|
332 | int num_blocks=0, last_block_size=0; |
---|
333 | |
---|
334 | fname=GetZephyrVarKeyFile("zcrypt", class, instance); |
---|
335 | if (!fname) return(-1); |
---|
336 | fkey=fopen(fname, "r"); |
---|
337 | if (!fkey) return(-1); |
---|
338 | fgets(keystring, MAX_KEY-1, fkey); |
---|
339 | fclose(fkey); |
---|
340 | |
---|
341 | des_string_to_key(keystring, key); |
---|
342 | des_key_sched(key, schedule); |
---|
343 | |
---|
344 | inbuff=in; |
---|
345 | length=strlen(inbuff); |
---|
346 | num_blocks=(length+7)/8; |
---|
347 | last_block_size=((length+7)%8)+1; |
---|
348 | use_buffer=TRUE; |
---|
349 | |
---|
350 | strcpy(out, ""); |
---|
351 | |
---|
352 | inptr=inbuff; |
---|
353 | while (TRUE) { |
---|
354 | /* Get 8 bytes from buffer */ |
---|
355 | if (num_blocks > 1) { |
---|
356 | size = 8; |
---|
357 | memcpy(input, inptr, size); |
---|
358 | inptr+=8; |
---|
359 | num_blocks--; |
---|
360 | } else if (num_blocks == 1) { |
---|
361 | size=last_block_size; |
---|
362 | memcpy(input, inptr, size); |
---|
363 | num_blocks--; |
---|
364 | } else { |
---|
365 | size=0; |
---|
366 | } |
---|
367 | |
---|
368 | /* Check for EOF and pad the string to 8 chars, if needed */ |
---|
369 | if (size == 0) break; /* END OF INPUT: BREAK FROM while LOOP! */ |
---|
370 | |
---|
371 | if (size<8) memset(input + size, 0, 8 - size); |
---|
372 | |
---|
373 | /* Encrypt and output the block */ |
---|
374 | des_ecb_encrypt(input, output, schedule, TRUE); |
---|
375 | |
---|
376 | for (i = 0; i < 8; i++) { |
---|
377 | sprintf(out, "%s%c", out, ((output[i] & 0xf0) >> 4) + BASE_CODE); |
---|
378 | sprintf(out, "%s%c", out, (output[i] & 0x0f) + BASE_CODE); |
---|
379 | } |
---|
380 | |
---|
381 | if (size < 8) break; |
---|
382 | } |
---|
383 | return(0); |
---|
384 | } |
---|
385 | |
---|
386 | /* Build a space-separated string from argv from elements between start * |
---|
387 | * and end - 1. malloc()'s the returned string. */ |
---|
388 | char *BuildArgString(char **argv, int start, int end) { |
---|
389 | int len = 1; |
---|
390 | int i; |
---|
391 | char *result; |
---|
392 | |
---|
393 | /* Compute the length of the string. (Plus 1 or 2) */ |
---|
394 | for (i = start; i < end; i++) { |
---|
395 | len += strlen(argv[i]) + 1; |
---|
396 | } |
---|
397 | |
---|
398 | /* Allocate memory */ |
---|
399 | result = (char *)malloc(len); |
---|
400 | if (result) { |
---|
401 | /* Build the string */ |
---|
402 | char *ptr = result; |
---|
403 | /* Start with an empty string, in case nothing is copied. */ |
---|
404 | *ptr = '\0'; |
---|
405 | /* Copy the arguments */ |
---|
406 | for (i = start; i < end; i++) { |
---|
407 | char *temp = argv[i]; |
---|
408 | /* Add a space, if not the first argument */ |
---|
409 | if (i != start) *ptr++ = ' '; |
---|
410 | /* Copy argv[i], leaving ptr pointing to the '\0' copied from temp */ |
---|
411 | while ((*ptr = *temp++)!=0) { |
---|
412 | ptr++; |
---|
413 | } |
---|
414 | } |
---|
415 | } |
---|
416 | |
---|
417 | return(result); |
---|
418 | } |
---|
419 | |
---|
420 | #define MAX_BUFF 258 |
---|
421 | #define MAX_SEARCH 3 |
---|
422 | /* Find the class/instance in the .crypt-table */ |
---|
423 | char *GetZephyrVarKeyFile(char *whoami, char *class, char *instance) { |
---|
424 | char *keyfile = NULL; |
---|
425 | char varname[MAX_SEARCH][128]; |
---|
426 | int length[MAX_SEARCH], i; |
---|
427 | char buffer[MAX_BUFF]; |
---|
428 | char filename[MAX_BUFF]; |
---|
429 | char result[MAX_SEARCH][MAX_BUFF]; |
---|
430 | int numsearch = 0; |
---|
431 | FILE *fsearch; |
---|
432 | |
---|
433 | /* Determine names to look for in .crypt-table */ |
---|
434 | if (instance) { |
---|
435 | sprintf(varname[numsearch++], "crypt-%s-%s:", (class?class:"message"), instance); |
---|
436 | } |
---|
437 | if (class) { |
---|
438 | sprintf(varname[numsearch++], "crypt-%s:", class); |
---|
439 | } |
---|
440 | sprintf(varname[numsearch++], "crypt-default:"); |
---|
441 | |
---|
442 | /* Setup the result array, and determine string lengths */ |
---|
443 | for (i = 0; i < numsearch; i++) { |
---|
444 | result[i][0] = '\0'; |
---|
445 | length[i] = strlen(varname[i]); |
---|
446 | } |
---|
447 | |
---|
448 | /* Open~/.crypt-table */ |
---|
449 | sprintf(filename, "%s/.crypt-table", getenv("HOME")); |
---|
450 | fsearch = fopen(filename, "r"); |
---|
451 | if (fsearch) { |
---|
452 | /* Scan file for a match */ |
---|
453 | while (!feof(fsearch)) { |
---|
454 | fgets(buffer, MAX_BUFF - 3, fsearch); |
---|
455 | for (i = 0; i < numsearch; i++) { |
---|
456 | if (strncasecmp(varname[i], buffer, length[i]) == 0) { |
---|
457 | int j; |
---|
458 | for (j = length[i]; buffer[j] == ' '; j++) |
---|
459 | ; |
---|
460 | strcpy(result[i], &buffer[j]); |
---|
461 | if (*result[i]) { |
---|
462 | if (result[i][strlen(result[i])-1] == '\n') { |
---|
463 | result[i][strlen(result[i])-1] = '\0'; |
---|
464 | } |
---|
465 | } |
---|
466 | } |
---|
467 | } |
---|
468 | } |
---|
469 | |
---|
470 | /* Pick the "best" match found */ |
---|
471 | keyfile = NULL; |
---|
472 | for (i = 0; i < numsearch; i++) { |
---|
473 | if (*result[i]) { |
---|
474 | keyfile = result[i]; |
---|
475 | break; |
---|
476 | } |
---|
477 | } |
---|
478 | |
---|
479 | if (keyfile == NULL) { |
---|
480 | /* printf("Could not find key table entry.\n"); */ |
---|
481 | } else { |
---|
482 | /* Prepare result to be returned */ |
---|
483 | char *temp = keyfile; |
---|
484 | keyfile = (char *)malloc(strlen(temp) + 1); |
---|
485 | if (keyfile) { |
---|
486 | strcpy(keyfile, temp); |
---|
487 | } else { |
---|
488 | /* printf("Memory allocation error.\n"); */ |
---|
489 | } |
---|
490 | } |
---|
491 | |
---|
492 | fclose(fsearch); |
---|
493 | } else { |
---|
494 | /* printf("Could not open key table file: %s\n", filename); */ |
---|
495 | } |
---|
496 | |
---|
497 | return(keyfile); |
---|
498 | } |
---|
499 | |
---|
500 | static pid_t zephyrpipe_pid = 0; |
---|
501 | |
---|
502 | /* Open a pipe to zwrite */ |
---|
503 | static FILE *GetZephyrPipe(char *class, char *instance, ZWRITEOPTIONS *zoptions) { |
---|
504 | int fildes[2]; |
---|
505 | pid_t pid; |
---|
506 | FILE *result; |
---|
507 | char *argv[20]; |
---|
508 | int argc = 0; |
---|
509 | |
---|
510 | if (pipe(fildes) < 0) return(NULL); |
---|
511 | pid = fork(); |
---|
512 | |
---|
513 | if (pid < 0) { |
---|
514 | /* Error: clean up */ |
---|
515 | close(fildes[0]); |
---|
516 | close(fildes[1]); |
---|
517 | result = NULL; |
---|
518 | } else if (pid == 0) { |
---|
519 | /* Setup child process */ |
---|
520 | argv[argc++] = "zwrite"; |
---|
521 | argv[argc++] = "-n"; /* Always send without ping */ |
---|
522 | if (class) { |
---|
523 | argv[argc++] = "-c"; |
---|
524 | argv[argc++] = class; |
---|
525 | } |
---|
526 | if (instance) { |
---|
527 | argv[argc++] = "-i"; |
---|
528 | argv[argc++] = instance; |
---|
529 | } |
---|
530 | if (zoptions->flags & ZWRITE_OPT_NOAUTH) argv[argc++] = "-d"; |
---|
531 | if (zoptions->flags & ZWRITE_OPT_QUIET) argv[argc++] = "-q"; |
---|
532 | if (zoptions->flags & ZWRITE_OPT_VERBOSE) argv[argc++] = "-v"; |
---|
533 | if (zoptions->flags & ZWRITE_OPT_SIGNATURE) { |
---|
534 | argv[argc++] = "-s"; |
---|
535 | argv[argc++] = zoptions->signature; |
---|
536 | } |
---|
537 | argv[argc++] = "-O"; |
---|
538 | argv[argc++] = "crypt"; |
---|
539 | argv[argc] = NULL; |
---|
540 | close(fildes[1]); |
---|
541 | if (fildes[0] != STDIN_FILENO) { |
---|
542 | if (dup2(fildes[0], STDIN_FILENO) != STDIN_FILENO) exit(0); |
---|
543 | close(fildes[0]); |
---|
544 | } |
---|
545 | close(fildes[0]); |
---|
546 | execvp(argv[0], argv); |
---|
547 | printf("Exec error: could not run zwrite\n"); |
---|
548 | exit(0); |
---|
549 | } else { |
---|
550 | close(fildes[0]); |
---|
551 | /* Create a FILE * for the zwrite pipe */ |
---|
552 | result = (FILE *)fdopen(fildes[1], "w"); |
---|
553 | zephyrpipe_pid = pid; |
---|
554 | } |
---|
555 | |
---|
556 | return(result); |
---|
557 | } |
---|
558 | |
---|
559 | /* Close the pipe to zwrite */ |
---|
560 | void CloseZephyrPipe(FILE *pipe) { |
---|
561 | fclose(pipe); |
---|
562 | waitpid(zephyrpipe_pid, NULL, 0); |
---|
563 | zephyrpipe_pid = 0; |
---|
564 | } |
---|
565 | |
---|
566 | #define MAX_RESULT 2048 |
---|
567 | |
---|
568 | |
---|
569 | void block_to_ascii(char *output, FILE *outfile) { |
---|
570 | int i; |
---|
571 | for (i = 0; i < 8; i++) { |
---|
572 | putc(((output[i] & 0xf0) >> 4) + BASE_CODE, outfile); |
---|
573 | putc( (output[i] & 0x0f) + BASE_CODE, outfile); |
---|
574 | } |
---|
575 | } |
---|
576 | |
---|
577 | #define MAX_LINE 128 |
---|
578 | |
---|
579 | /* Encrypt stdin, with prompt if isatty, and send to stdout, or to zwrite |
---|
580 | if zephyr is set. */ |
---|
581 | static int do_encrypt(char *keystring, int zephyr, char *class, char *instance, ZWRITEOPTIONS *zoptions, char* keyfile) { |
---|
582 | des_cblock key; |
---|
583 | des_key_schedule schedule; |
---|
584 | char input[8], output[8]; |
---|
585 | int size; |
---|
586 | FILE *outfile = stdout; |
---|
587 | int error = FALSE; |
---|
588 | char *inbuff = NULL, *inptr; |
---|
589 | int freein = FALSE; |
---|
590 | int use_buffer = FALSE; |
---|
591 | int num_blocks=0, last_block_size=0; |
---|
592 | |
---|
593 | des_string_to_key(keystring, key); |
---|
594 | des_key_sched(key, schedule); |
---|
595 | |
---|
596 | if (zephyr) { |
---|
597 | if (zoptions->flags & ZCRYPT_OPT_MESSAGE) { |
---|
598 | /* Use the -m message */ |
---|
599 | int length; |
---|
600 | inbuff = zoptions->message; |
---|
601 | length = strlen(inbuff); |
---|
602 | num_blocks = (length + 7) / 8; |
---|
603 | last_block_size = ((length + 7) % 8) + 1; |
---|
604 | use_buffer = TRUE; |
---|
605 | } else if (isatty(0)) { |
---|
606 | /* tty input, so show the "Type your message now..." message */ |
---|
607 | if (zoptions->flags & ZCRYPT_OPT_IGNOREDOT) { |
---|
608 | printf("Type your message now. End with the end-of-file character.\n"); |
---|
609 | } else { |
---|
610 | printf("Type your message now. End with control-D or a dot on a line by itself.\n"); |
---|
611 | } |
---|
612 | use_buffer = TRUE; |
---|
613 | if ((inptr = inbuff = (char *)malloc(MAX_RESULT)) == NULL) { |
---|
614 | printf("Memory allocation error\n"); |
---|
615 | return FALSE; |
---|
616 | } |
---|
617 | while (inptr - inbuff < MAX_RESULT - MAX_LINE - 20) { |
---|
618 | fgets(inptr, MAX_LINE, stdin); |
---|
619 | if (inptr[0]) { |
---|
620 | if (inptr[0] == '.' && inptr[1] == '\n' && |
---|
621 | !(zoptions->flags & ZCRYPT_OPT_IGNOREDOT)) { |
---|
622 | inptr[0] = '\0'; |
---|
623 | break; |
---|
624 | } else { |
---|
625 | inptr += strlen(inptr); |
---|
626 | } |
---|
627 | } else { |
---|
628 | break; |
---|
629 | } |
---|
630 | } |
---|
631 | num_blocks = (inptr - inbuff + 7) / 8; |
---|
632 | last_block_size = ((inptr - inbuff + 7) % 8) + 1; |
---|
633 | freein = TRUE; |
---|
634 | } |
---|
635 | |
---|
636 | /* if (zephyr) */ |
---|
637 | outfile = GetZephyrPipe(class, instance, zoptions); |
---|
638 | if (!outfile) { |
---|
639 | printf("Could not run zwrite\n"); |
---|
640 | if (freein && inbuff) { |
---|
641 | free(inbuff); |
---|
642 | } |
---|
643 | return(FALSE); |
---|
644 | } |
---|
645 | } |
---|
646 | |
---|
647 | inptr = inbuff; |
---|
648 | |
---|
649 | /* Encrypt the input (inbuff or stdin) and send it to outfile */ |
---|
650 | while (TRUE) { |
---|
651 | if (use_buffer) { |
---|
652 | /* Get 8 bytes from buffer */ |
---|
653 | if (num_blocks > 1) { |
---|
654 | size = 8; |
---|
655 | memcpy(input, inptr, size); |
---|
656 | inptr += 8; |
---|
657 | num_blocks--; |
---|
658 | } else if (num_blocks == 1) { |
---|
659 | size = last_block_size; |
---|
660 | memcpy(input, inptr, size); |
---|
661 | num_blocks--; |
---|
662 | } else { |
---|
663 | size = 0; |
---|
664 | } |
---|
665 | } else { |
---|
666 | /* Get 8 bytes from stdin */ |
---|
667 | size = fread(input, 1, 8, stdin); |
---|
668 | } |
---|
669 | |
---|
670 | /* Check for EOF and pad the string to 8 chars, if needed */ |
---|
671 | if (size == 0) break; /* END OF INPUT: BREAK FROM while LOOP! */ |
---|
672 | |
---|
673 | if (size < 8) memset(input + size, 0, 8 - size); |
---|
674 | |
---|
675 | /* Encrypt and output the block */ |
---|
676 | des_ecb_encrypt(input, output, schedule, TRUE); |
---|
677 | block_to_ascii(output, outfile); |
---|
678 | |
---|
679 | if (size < 8) break; |
---|
680 | } |
---|
681 | |
---|
682 | /* Close out the output */ |
---|
683 | if (!error) putc('\n', outfile); |
---|
684 | if (zephyr) CloseZephyrPipe(outfile); |
---|
685 | |
---|
686 | /* Free the input buffer, if necessary */ |
---|
687 | if (freein && inbuff) free(inbuff); |
---|
688 | |
---|
689 | return(!error); |
---|
690 | } |
---|
691 | |
---|
692 | /* Read a half-byte from stdin, skipping invalid characters. Returns -1 |
---|
693 | if at EOF or file error */ |
---|
694 | int read_ascii_nybble() { |
---|
695 | char c; |
---|
696 | |
---|
697 | while (TRUE) { |
---|
698 | if (fread(&c, 1, 1, stdin) == 0) { |
---|
699 | return(-1); |
---|
700 | } else if (c >= BASE_CODE && c <= LAST_CODE) { |
---|
701 | return(c - BASE_CODE); |
---|
702 | } |
---|
703 | } |
---|
704 | } |
---|
705 | |
---|
706 | /* Read both halves of the byte and return the single byte. Returns -1 |
---|
707 | if at EOF or file error. */ |
---|
708 | int read_ascii_byte() { |
---|
709 | int c1, c2; |
---|
710 | c1 = read_ascii_nybble(); |
---|
711 | if (c1 >= 0) { |
---|
712 | c2 = read_ascii_nybble(); |
---|
713 | if (c2 >= 0) { |
---|
714 | return c1 * 0x10 + c2; |
---|
715 | } |
---|
716 | } |
---|
717 | return(-1); |
---|
718 | } |
---|
719 | |
---|
720 | /* Read an 8-byte DES block from stdin */ |
---|
721 | int read_ascii_block(char *input) { |
---|
722 | int c, i; |
---|
723 | |
---|
724 | for (i = 0; i < 8; i++) { |
---|
725 | c = read_ascii_byte(); |
---|
726 | if (c < 0) return(FALSE); |
---|
727 | input[i] = c; |
---|
728 | } |
---|
729 | |
---|
730 | return(TRUE); |
---|
731 | } |
---|
732 | |
---|
733 | /* Decrypt stdin */ |
---|
734 | int do_decrypt(char *keystring) { |
---|
735 | des_cblock key; |
---|
736 | des_key_schedule schedule; |
---|
737 | char input[8], output[9]; |
---|
738 | |
---|
739 | output[0] = '\0'; /* In case no message at all */ |
---|
740 | output[8] = '\0'; /* NULL at end will limit string length to 8 */ |
---|
741 | |
---|
742 | des_string_to_key(keystring, key); |
---|
743 | des_key_sched(key, schedule); |
---|
744 | |
---|
745 | while (read_ascii_block(input)) { |
---|
746 | des_ecb_encrypt(input, output, schedule, FALSE); |
---|
747 | printf("%s", output); |
---|
748 | } |
---|
749 | |
---|
750 | if (output[0]) { |
---|
751 | if (output[strlen(output)-1] != '\n') { |
---|
752 | printf("\n"); |
---|
753 | } |
---|
754 | } else { |
---|
755 | printf("\n"); |
---|
756 | } |
---|
757 | return(0); |
---|
758 | } |
---|
759 | |
---|
760 | #endif |
---|