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 | /* The 'owl_zcrypt_decrypt' function was written by kretch for Owl. |
---|
70 | * Decrypt the message in 'in' on class 'class' and instance |
---|
71 | * 'instance' and leave the result in 'out'. Out must be a buffer |
---|
72 | * allocated by the caller. |
---|
73 | * |
---|
74 | * return 0 on success, otherwise -1 |
---|
75 | */ |
---|
76 | int owl_zcrypt_decrypt(char *out, char *in, char *class, char *instance) { |
---|
77 | char *fname, keystring[MAX_KEY], *inptr, *endptr; |
---|
78 | FILE *fkey; |
---|
79 | des_cblock key; |
---|
80 | des_key_schedule schedule; |
---|
81 | char input[8], output[9]; |
---|
82 | int i, c1, c2; |
---|
83 | |
---|
84 | fname=GetZephyrVarKeyFile("zcrypt", class, instance); |
---|
85 | if (!fname) return(-1); |
---|
86 | fkey=fopen(fname, "r"); |
---|
87 | if (!fkey) return(-1); |
---|
88 | fgets(keystring, MAX_KEY-1, fkey); |
---|
89 | fclose(fkey); |
---|
90 | |
---|
91 | strcpy(out, ""); |
---|
92 | |
---|
93 | output[0] = '\0'; /* In case no message at all */ |
---|
94 | output[8] = '\0'; /* NULL at end will limit string length to 8 */ |
---|
95 | |
---|
96 | des_string_to_key(keystring, key); |
---|
97 | des_key_sched(key, schedule); |
---|
98 | |
---|
99 | inptr=in; |
---|
100 | endptr=in+strlen(in)-1; |
---|
101 | while (inptr<endptr) { |
---|
102 | for (i=0; i<8; i++) { |
---|
103 | c1=(inptr[0])-BASE_CODE; |
---|
104 | c2=(inptr[1])-BASE_CODE; |
---|
105 | input[i]=c1 * 0x10 + c2; |
---|
106 | inptr+=2; |
---|
107 | } |
---|
108 | des_ecb_encrypt(input, output, schedule, FALSE); |
---|
109 | strcat(out, output); |
---|
110 | } |
---|
111 | |
---|
112 | if (output[0]) { |
---|
113 | if (output[strlen(output)-1] != '\n') { |
---|
114 | strcat(out, "\n"); |
---|
115 | } |
---|
116 | } else { |
---|
117 | strcat(out, "\n"); |
---|
118 | } |
---|
119 | return(0); |
---|
120 | } |
---|
121 | |
---|
122 | int owl_zcrypt_encrypt(char *out, char *in, char *class, char *instance) { |
---|
123 | /* static int do_encrypt(char *keystring, int zephyr, char *class, char *instance, ZWRITEOPTIONS *zoptions, char* keyfile) { */ |
---|
124 | char *fname, keystring[MAX_KEY]; |
---|
125 | FILE *fkey; |
---|
126 | des_cblock key; |
---|
127 | des_key_schedule schedule; |
---|
128 | char input[8], output[8]; |
---|
129 | int size, length, i; |
---|
130 | char *inbuff = NULL, *inptr; |
---|
131 | int use_buffer = FALSE; |
---|
132 | int num_blocks=0, last_block_size=0; |
---|
133 | |
---|
134 | fname=GetZephyrVarKeyFile("zcrypt", class, instance); |
---|
135 | if (!fname) return(-1); |
---|
136 | fkey=fopen(fname, "r"); |
---|
137 | if (!fkey) return(-1); |
---|
138 | fgets(keystring, MAX_KEY-1, fkey); |
---|
139 | fclose(fkey); |
---|
140 | |
---|
141 | des_string_to_key(keystring, key); |
---|
142 | des_key_sched(key, schedule); |
---|
143 | |
---|
144 | inbuff=in; |
---|
145 | length=strlen(inbuff); |
---|
146 | num_blocks=(length+7)/8; |
---|
147 | last_block_size=((length+7)%8)+1; |
---|
148 | use_buffer=TRUE; |
---|
149 | |
---|
150 | strcpy(out, ""); |
---|
151 | |
---|
152 | inptr=inbuff; |
---|
153 | while (TRUE) { |
---|
154 | /* Get 8 bytes from buffer */ |
---|
155 | if (num_blocks > 1) { |
---|
156 | size = 8; |
---|
157 | memcpy(input, inptr, size); |
---|
158 | inptr+=8; |
---|
159 | num_blocks--; |
---|
160 | } else if (num_blocks == 1) { |
---|
161 | size=last_block_size; |
---|
162 | memcpy(input, inptr, size); |
---|
163 | num_blocks--; |
---|
164 | } else { |
---|
165 | size=0; |
---|
166 | } |
---|
167 | |
---|
168 | /* Check for EOF and pad the string to 8 chars, if needed */ |
---|
169 | if (size == 0) break; /* END OF INPUT: BREAK FROM while LOOP! */ |
---|
170 | |
---|
171 | if (size<8) memset(input + size, 0, 8 - size); |
---|
172 | |
---|
173 | /* Encrypt and output the block */ |
---|
174 | des_ecb_encrypt(input, output, schedule, TRUE); |
---|
175 | |
---|
176 | for (i = 0; i < 8; i++) { |
---|
177 | sprintf(out + strlen(out), "%c", ((output[i] & 0xf0) >> 4) + BASE_CODE); |
---|
178 | sprintf(out + strlen(out), "%c", (output[i] & 0x0f) + BASE_CODE); |
---|
179 | } |
---|
180 | |
---|
181 | if (size < 8) break; |
---|
182 | } |
---|
183 | return(0); |
---|
184 | } |
---|
185 | |
---|
186 | |
---|
187 | #define MAX_BUFF 258 |
---|
188 | #define MAX_SEARCH 3 |
---|
189 | /* Find the class/instance in the .crypt-table */ |
---|
190 | char *GetZephyrVarKeyFile(char *whoami, char *class, char *instance) { |
---|
191 | char *keyfile = NULL; |
---|
192 | char *varname[MAX_SEARCH]; |
---|
193 | int length[MAX_SEARCH], i; |
---|
194 | char buffer[MAX_BUFF]; |
---|
195 | char *filename; |
---|
196 | char result[MAX_SEARCH][MAX_BUFF]; |
---|
197 | int numsearch = 0; |
---|
198 | FILE *fsearch; |
---|
199 | |
---|
200 | memset(varname, 0, sizeof(varname)); |
---|
201 | |
---|
202 | /* Determine names to look for in .crypt-table */ |
---|
203 | if (instance) { |
---|
204 | varname[numsearch++] = owl_sprintf("crypt-%s-%s:", (class?class:"message"), instance); |
---|
205 | } |
---|
206 | if (class) { |
---|
207 | varname[numsearch++] = owl_sprintf("crypt-%s:", class); |
---|
208 | } |
---|
209 | varname[numsearch++] = owl_strdup("crypt-default:"); |
---|
210 | |
---|
211 | /* Setup the result array, and determine string lengths */ |
---|
212 | for (i = 0; i < numsearch; i++) { |
---|
213 | result[i][0] = '\0'; |
---|
214 | length[i] = strlen(varname[i]); |
---|
215 | } |
---|
216 | |
---|
217 | /* Open~/.crypt-table */ |
---|
218 | filename = owl_sprintf("%s/.crypt-table", getenv("HOME")); |
---|
219 | fsearch = fopen(filename, "r"); |
---|
220 | if (fsearch) { |
---|
221 | /* Scan file for a match */ |
---|
222 | while (!feof(fsearch)) { |
---|
223 | fgets(buffer, MAX_BUFF - 3, fsearch); |
---|
224 | for (i = 0; i < numsearch; i++) { |
---|
225 | if (strncasecmp(varname[i], buffer, length[i]) == 0) { |
---|
226 | int j; |
---|
227 | for (j = length[i]; buffer[j] == ' '; j++) |
---|
228 | ; |
---|
229 | strcpy(result[i], &buffer[j]); |
---|
230 | if (*result[i]) { |
---|
231 | if (result[i][strlen(result[i])-1] == '\n') { |
---|
232 | result[i][strlen(result[i])-1] = '\0'; |
---|
233 | } |
---|
234 | } |
---|
235 | } |
---|
236 | } |
---|
237 | } |
---|
238 | |
---|
239 | /* Pick the "best" match found */ |
---|
240 | keyfile = NULL; |
---|
241 | for (i = 0; i < numsearch; i++) { |
---|
242 | if (*result[i]) { |
---|
243 | keyfile = result[i]; |
---|
244 | break; |
---|
245 | } |
---|
246 | } |
---|
247 | |
---|
248 | if (keyfile == NULL) { |
---|
249 | /* printf("Could not find key table entry.\n"); */ |
---|
250 | } else { |
---|
251 | /* Prepare result to be returned */ |
---|
252 | char *temp = keyfile; |
---|
253 | keyfile = (char *)owl_malloc(strlen(temp) + 1); |
---|
254 | if (keyfile) { |
---|
255 | strcpy(keyfile, temp); |
---|
256 | } else { |
---|
257 | /* printf("Memory allocation error.\n"); */ |
---|
258 | } |
---|
259 | } |
---|
260 | |
---|
261 | fclose(fsearch); |
---|
262 | } else { |
---|
263 | /* printf("Could not open key table file: %s\n", filename); */ |
---|
264 | } |
---|
265 | |
---|
266 | for(i = 0; i < MAX_SEARCH; i++) { |
---|
267 | owl_free(varname[i]); |
---|
268 | } |
---|
269 | |
---|
270 | owl_free(filename); |
---|
271 | |
---|
272 | return(keyfile); |
---|
273 | } |
---|
274 | |
---|
275 | static pid_t zephyrpipe_pid = 0; |
---|
276 | |
---|
277 | #endif |
---|