File: crypt.c

package info (click to toggle)
ircii 20210328-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,644 kB
  • sloc: ansic: 42,273; makefile: 860; sh: 524; perl: 348
file content (468 lines) | stat: -rw-r--r-- 11,362 bytes parent folder | download
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
 * crypt.c: handles some encryption of messages stuff. 
 *
 * Written By Michael Sandrof
 *
 * Copyright (c) 1990 Michael Sandrof.
 * Copyright (c) 1991, 1992 Troy Rollo.
 * Copyright (c) 1992-2014 Matthew R. Green.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "irc.h"
IRCII_RCSID("@(#)$eterna: crypt.c,v 1.84 2021/02/27 23:03:57 mrg Exp $");

#include "irccrypt.h"
#include "vars.h"
#include "ircaux.h"
#include "list.h"
#include "ctcp.h"
#include "output.h"
#include "newio.h"

#include <sys/wait.h>

static	void	add_to_crypt(u_char *, u_char *, CryptFunc, CryptFunc, u_char *);
static	int	remove_crypt(u_char *);
static	u_char	*do_crypt(u_char *, crypt_key *, int, u_char **);
static	void	crypt_get_random_data(u_char *, size_t);

#include "cast.c"
#if 0
#include "aes.c"
#endif

/*
 * Crypt: the crypt list structure,  consists of the nickname, and the
 * encryption key 
 */
typedef struct	CryptStru
{
	struct	CryptStru *next;
	u_char	*nick;
	crypt_key	*key;
}	Crypt;

/* crypt_list: the list of nicknames and encryption keys */
static	Crypt	*crypt_list = NULL;

/*
 * add_to_crypt: adds the nickname and key pair to the crypt_list.  If the
 * nickname is already in the list, then the key is changed the the supplied
 * key. 
 */
static	void
add_to_crypt(u_char *nick, u_char *keystr, CryptFunc enc, CryptFunc dec, u_char *type)
{
	Crypt	*new;

	if ((new = (Crypt *) remove_from_list((List **)(void *)&crypt_list, nick)) != NULL)
	{
		new_free(&(new->nick));
		memset(new->key->key, 0, my_strlen(new->key->key));		/* wipe it out */
		new_free(&(new->key->key));
		/* XXX destroy cookie first? */
		new_free(&(new->key->cookie));
		new_free(&(new->key));
		new_free(&new);
	}
	new = new_malloc(sizeof *new);
	new->key = new_malloc(sizeof(*new->key));
	new->nick = NULL;
	new->key->key = NULL;
	new->key->type = type;
	malloc_strcpy(&(new->nick), nick);
	malloc_strcpy(&(new->key->key), keystr);
	new->key->crypt = enc;
	new->key->decrypt = dec;
	new->key->cookie = NULL;
	add_to_list((List **)(void *)&crypt_list, (List *) new);
}

/*
 * remove_crypt: removes the given nickname from the crypt_list, returning 0
 * if successful, and 1 if not (because the nickname wasn't in the list) 
 */
static	int
remove_crypt(u_char *nick)
{
	Crypt	*tmp;

	if ((tmp = (Crypt *) list_lookup((List **)(void *)&crypt_list, nick, 0, REMOVE_FROM_LIST)) != NULL)
	{
		new_free(&tmp->nick);
		memset(tmp->key->key, 0, my_strlen(tmp->key->key));		/* wipe it out */
		new_free(&tmp->key->key);
		new_free(&tmp->key->cookie);
		new_free(&tmp->key);
		new_free(&tmp);
		return (0);
	}
	return (1);
}

/*
 * is_crypted: looks up nick in the crypt_list and returns the encryption key
 * if found in the list.  If not found in the crypt_list, null is returned. 
 */
crypt_key *
is_crypted(u_char *nick)
{
	Crypt	*tmp;

	if (!crypt_list)
		return NULL;
	if ((tmp = (Crypt *) list_lookup((List **)(void *)&crypt_list, nick, 0, 0)) != NULL)
		return (tmp->key);
	return NULL;
}

/*
 * encrypt_cmd: the ENCRYPT command.  Adds the given nickname and key to the
 * encrypt list, or removes it, or list the list, you know. 
 */
void
encrypt_cmd(u_char *command, u_char *args, u_char *subargs)
{
	/* XXX this is getting really, really gross */
	CryptFunc enc = DEFAULT_CRYPTER, dec = DEFAULT_DECRYPTER;
	u_char	*type = UP(DEFAULT_CRYPTYPE);
	u_char	*nick;
	int	showkeys = 0;

restart:
	if ((nick = next_arg(args, &args)) != NULL)
	{
		size_t len = my_strlen(nick);

		if (my_strnicmp(nick, UP("-showkeys"), len) == 0)
		{
			showkeys = 1;
			goto restart;
		}
		else if (my_strnicmp(nick, UP("-cast"), len) == 0)
		{
			enc = cast_encrypt_str;
			dec = cast_decrypt_str;
			type = UP(CAST_STRING);
			goto restart;
		}
#if 0
		else if (my_strnicmp(nick, UP("-aes"), len) == 0)
		{
			enc = aes_encrypt_str;
			dec = aes_decrypt_str;
			type = UP(AES_STRING);
			goto restart;
		}
#endif

		if (args && *args)
		{
			add_to_crypt(nick, args, enc, dec, type);
			say("%s added to the %s crypt", nick, type);
		}
		else
		{
			if (remove_crypt(nick))
				say("No such nickname in the crypt: %s", nick);
			else
				say("%s removed from the crypt", nick);
		}
	}
	else
	{
		if (crypt_list)
		{
			Crypt	*tmp;

			say("The crypt:");
			for (tmp = crypt_list; tmp; tmp = tmp->next)
				if (showkeys)
					put_it("%s with key \"%s\" type %s", tmp->nick, tmp->key->key, tmp->key->type);
				else
					put_it("%s type %s", tmp->nick, tmp->key->type);
		}
		else
			say("The crypt is empty");
	}
}

static	u_char	*
do_crypt(u_char *str, crypt_key *key, int flag, u_char **type)
{
	int	in[2],
		out[2];
	size_t	c;
	ssize_t	sc;
	u_char	lbuf[CRYPT_BUFFER_SIZE];
	u_char	*ptr = NULL,
		*crypt_program,
		*encrypt_program,
		*decrypt_program,
		*crypt_str;

	encrypt_program = get_string_var(ENCRYPT_PROGRAM_VAR);
	decrypt_program = get_string_var(DECRYPT_PROGRAM_VAR);
	if ((flag && encrypt_program) || (!flag && decrypt_program))
	{
#ifdef DAEMON_UID
		if (DAEMON_UID == getuid())
		{
			say("ENCRYPT_PROGRAM not available from daemon mode");
			return NULL;
		}
#endif /* DAEMON_ID */
		in[0] = in[1] = -1;
		out[0] = out[1] = -1;
		if (flag)
		{
			crypt_str = UP("encryption");
			crypt_program = encrypt_program;
		}
		else
		{
			crypt_str = UP("decryption");
			crypt_program = decrypt_program;
		}
		if (access(CP(crypt_program), X_OK))
		{
			say("Unable to execute %s program: %s", crypt_str, crypt_program);
			return (NULL);
		}
		c = my_strlen(str);
		if (!flag)
			ptr = ctcp_unquote_it(str, &c);
		else 
			malloc_strcpy(&ptr, str);
		if (pipe(in) || pipe(out))
		{
			say("Unable to start %s process: %s", crypt_str, strerror(errno));
			if (in[0] != -1)
			{
				new_close(in[0]);
				new_close(in[1]);
			}
			if (out[0] != -1)
			{
				new_close(out[0]);
				new_close(out[1]);
			}
		}
		switch (fork())
		{
		case -1:
			say("Unable to start %s process: %s", crypt_str, strerror(errno));
			return (NULL);
		case 0:
			MY_SIGNAL(SIGINT, (sigfunc *) SIG_IGN, 0);
			dup2(out[1], 1);
			dup2(in[0], 0);
			new_close(out[0]);
			new_close(out[1]);
			new_close(in[0]);
			new_close(in[1]);
			(void)setgid(getgid());
			(void)setuid(getuid());
			if (get_int_var(OLD_ENCRYPT_PROGRAM_VAR))
				execl(CP(crypt_program), CP(crypt_program), key->key, NULL);
			else
				execl(CP(crypt_program), CP(crypt_program), NULL);
			exit(0);
		default:
			new_close(out[1]);
			new_close(in[0]);
			if (get_int_var(OLD_ENCRYPT_PROGRAM_VAR) == 0)
			{
				(void)write(in[1], key->key, my_strlen(key->key));
				(void)write(in[1], "\n", 1);
			}
			(void)write(in[1], ptr, c);
			new_close(in[1]);
			sc = read(out[0], lbuf, sizeof lbuf);
			wait(NULL);
			if (sc > 0)
				lbuf[sc] = '\0';
			new_close(out[0]);
			break;
		}
		new_free(&ptr);
		if (flag)
			ptr = ctcp_quote_it(lbuf, my_strlen(lbuf));
		else
			malloc_strcpy(&ptr, lbuf);
	}
	else
	{
		c = my_strlen(str);
		if (flag)
		{
			if (key->crypt(key, &str, &c) != 0)
			{
				yell("--- do_crypt(): crypto encrypt failed");
				return 0;
			}
			ptr = ctcp_quote_it(str, c);
		}
		else
		{
			ptr = ctcp_unquote_it(str, &c);
			if (key->decrypt(key, &ptr, &c) != 0)
			{
				yell("--- do_crypt(): crypto decrypt failed");
				return 0;
			}
		}
	}
	if (type)
		*type = key->type;
	return (ptr);
}

/*
 * crypt_msg: Executes the encryption program on the given string with the
 * given key.  If flag is true, the string is encrypted and the returned
 * string is ready to be sent over irc.  If flag is false, the string is
 * decrypted and the returned string should be readable.
 */
u_char	*
crypt_msg(u_char *str, crypt_key *key, int flag)
{
	static	u_char	lbuf[CRYPT_BUFFER_SIZE];
	u_char	*ptr,
		*rest,
		*type;
	int	on = 1;

	if (flag)
	{
		*lbuf = '\0';
		while ((rest = my_index(str, '\005')) != NULL)
		{
			*(rest++) = '\0';
			if (on && *str && (ptr = do_crypt(str, key, flag, &type)))
			{
				snprintf(CP(lbuf), sizeof lbuf, "%c%.30s ", CTCP_DELIM_CHAR, type);
				my_strmcat(lbuf, ptr, sizeof lbuf);
				my_strmcat(lbuf, CTCP_DELIM_STR, sizeof lbuf);
				new_free(&ptr);
			}
			else
				my_strmcat(lbuf, str, sizeof lbuf);
			on = !on;
			str = rest;
		}
		if (on && (ptr = do_crypt(str, key, flag, &type)))
		{
			snprintf(CP(lbuf), sizeof lbuf, "%c%.30s ", CTCP_DELIM_CHAR, type);
			my_strmcat(lbuf, ptr, sizeof lbuf);
			my_strmcat(lbuf, CTCP_DELIM_STR, sizeof lbuf);
			new_free(&ptr);
		}
		else
			my_strmcat(lbuf, str, sizeof lbuf);
	}
	else
	{
		if ((ptr = do_crypt(str, key, flag, &type)) != NULL)
		{
			my_strmcpy(lbuf, ptr, sizeof lbuf);
			new_free(&ptr);
		}
		else
			my_strmcat(lbuf, str, sizeof lbuf);
	}
	return (lbuf);
}

#ifdef HAVE_DEV_RANDOM
static void alarmer(int);

static void
alarmer(int dummy)
{
}
#endif

static void
crypt_get_random_data(u_char *buf, size_t buflen)
{
	size_t	i;
#ifdef HAVE_DEV_RANDOM
	static	int	devrndfd = -1;
	size_t	remain = buflen;

	if (devrndfd == -1)
	{
		devrndfd = open(DEV_RANDOM_PATH, O_RDONLY);

		if (devrndfd == -1)
		{
			static int first = 1;

			if (first)
			{
				first = 0;
				yell("--- HELP!!!! crypt_dev_random_byte: can not open %s: %s",
				    DEV_RANDOM_PATH, strerror(errno));
				goto do_random_instead_no_alarm;
			}
		}
	}
	if (devrndfd == -2)
		goto do_random_instead_no_alarm;

	(void)MY_SIGNAL(SIGALRM, (sigfunc *) alarmer, 0);
	for (; remain > 0;) {
		alarm(2);
		ssize_t rv = read(devrndfd, buf, remain);
		alarm(0);
		if (rv > 0) {
			remain -= rv;
			continue;
		}
		(void)MY_SIGNAL(SIGALRM, (sigfunc *) SIG_DFL, 0);
		/* if we were just interrupted, don't bail on /dev/random */
		if (errno == EINTR)
			yell("--- crypt_dev_random_byte: interrupt -- "
			     "using random()");
		else
			yell("--- HELP!  crypt_dev_random_byte(): read of %zu "
			     "bytes on %s failed: %s",
			     buflen, DEV_RANDOM_PATH, strerror(errno));
		yell("--- using random()");
		break;
	}

	alarm(0);
	(void)MY_SIGNAL(SIGALRM, (sigfunc *) SIG_DFL, 0);
do_random_instead_no_alarm:
#endif
	for (i = 0; i < buflen; i++)
	{
		buf[i] = (random() & 255);
	}
}