summaryrefslogtreecommitdiff
path: root/usr/src/cmd/cmd-inet/usr.bin/telnet/auth.c
blob: c56ec5cc76e2331e0ad3e9ce0919617279f6f716 (plain)
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
 * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * usr/src/cmd/cmd-inet/usr.bin/telnet/auth.c
 */

/*
 * Copyright (c) 1991, 1993
 *	The Regents of the University of California.  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. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
 */

/* based on @(#)auth.c	8.1 (Berkeley) 6/4/93 */

/*
 * Copyright (C) 1990 by the Massachusetts Institute of Technology
 *
 * Export of this software from the United States of America may
 * require a specific license from the United States Government.
 * It is the responsibility of any person or organization contemplating
 * export to obtain such a license before exporting.
 *
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 */


#include <stdio.h>
#include <sys/types.h>
#include <signal.h>

#define	AUTHTYPE_NAMES		/* this is needed for arpa/telnet.h */
#include <arpa/telnet.h>

#ifdef	__STDC__
#include <stdlib.h>
#endif

#include <string.h>

#include "externs.h"
#include "encrypt.h"
#include "auth.h"

#define	typemask(x)	((x) > 0 ? 1 << ((x)-1) : 0)

static int auth_onoff(const char *type, boolean_t on);
static void auth_gen_printsub(uchar_t *, uint_t, uchar_t *, uint_t);

boolean_t	auth_debug_mode = B_FALSE;
boolean_t	auth_has_failed = B_FALSE;
boolean_t	auth_enable_encrypt = B_FALSE;

static 	char		*Name = "Noname";
static	Authenticator	*authenticated = NULL;
static	uchar_t		_auth_send_data[BUFSIZ];
static	uchar_t		*auth_send_data;
static	int		auth_send_cnt = 0;

/*
 * Authentication types supported.  Note that these are stored
 * in priority order, i.e. try the first one first.
 */
static	Authenticator authenticators[] = {
	{ AUTHTYPE_KERBEROS_V5,
		AUTH_WHO_CLIENT|AUTH_HOW_MUTUAL|AUTH_ENCRYPT_ON,
				kerberos5_init,
				kerberos5_send,
				kerberos5_reply,
				kerberos5_status,
				kerberos5_printsub },
	{ AUTHTYPE_KERBEROS_V5, AUTH_WHO_CLIENT|AUTH_HOW_MUTUAL,
				kerberos5_init,
				kerberos5_send,
				kerberos5_reply,
				kerberos5_status,
				kerberos5_printsub },
	{ AUTHTYPE_KERBEROS_V5, AUTH_WHO_CLIENT|AUTH_HOW_ONE_WAY,
				kerberos5_init,
				kerberos5_send,
				kerberos5_reply,
				kerberos5_status,
				kerberos5_printsub },
	{ 0, },
};

static Authenticator NoAuth = { 0 };

static uint_t	i_support = 0;
static uint_t	i_wont_support = 0;

/*
 * Traverse the Authenticator array until we find the authentication type
 * and matching direction we are looking for.  Return a pointer into the
 * Authenticator type array.
 *
 * Returns:	0 - type not found (error)
 * 		nonzero - pointer to authenticator
 */
static Authenticator *
findauthenticator(int type, int way)
{
	Authenticator *ap = authenticators;

	while (ap->type && (ap->type != type || ap->way != way))
		++ap;
	return (ap->type ? ap : NULL);
}

/*
 * For each authentication type in the Authenticator array,
 * call the associated init routine, and update the i_support bitfield.
 */
void
auth_init(const char *name)
{
	Authenticator *ap = authenticators;

	Name = name ? strdup(name) : "Noname";

	i_support = 0;
	authenticated = NULL;
	while (ap->type) {
		if (!ap->init || (*ap->init)(ap)) {
			i_support |= typemask(ap->type);
			if (auth_debug_mode)
			    (void) printf(gettext
				(">>>%s: I support auth type %d %d\r\n"),
				Name, ap->type, ap->way);
		}
		++ap;
	}
}

/*
 * Search the Authenticator array for the authentication type 'name',
 * and disable this type by updating the i_wont_support bitfield.
 */
void
auth_disable_name(const char *name)
{
	uint_t x;
	for (x = 0; x < AUTHTYPE_CNT; ++x) {
		if (!strcasecmp(name, AUTHTYPE_NAME(x))) {
			i_wont_support |= typemask(x);
			break;
		}
	}

	if (!i_wont_support)
		(void) printf(
			gettext("%s : invalid authentication type\n"),
			name);
}

/*
 * Search the Authenticator array for the authentication type given
 * by the character string 'type', and return its integer bitmask
 * in maskp.
 *
 * Returns:	1 - no error
 *		0 - type not found (error)
 */
static int
getauthmask(const char *type, uint_t *maskp)
{
	uint_t x;

	if (!strcasecmp(type, AUTHTYPE_NAME(0))) {
		*maskp = (uint_t)-1;
		return (1);
	}

	for (x = 1; x < AUTHTYPE_CNT; ++x) {
		if (!strcasecmp(type, AUTHTYPE_NAME(x))) {
			*maskp = typemask(x);
			return (1);
		}
	}
	return (0);
}

int
auth_enable(char *type)
{
	return (auth_onoff(type, B_TRUE));
}

int
auth_disable(char *type)
{
	return (auth_onoff(type, B_FALSE));
}

/*
 * Responds to the 'auth enable <option>' and 'auth disable <option>' commands.
 *
 * If <option> is:
 *	- a valid authentication type, turns support on / off
 *	- "?" or "help", print a usage message
 *	- not recognized, print an error message.
 *
 * Returns:	1 - no error, authentication is enabled or disabled
 *		0 - error, or help requested
 */
static int
auth_onoff(const char *type, boolean_t on)
{
	uint_t i, mask = 0;
	Authenticator *ap;

	if (!strcasecmp(type, "?") || !strcasecmp(type, "help")) {
		(void) printf(on ?
			gettext("auth enable 'type'\n") :
			gettext("auth disable 'type'\n"));
		(void) printf(
			gettext("Where 'type' is one of:\n"));
		(void) printf("\t%s\n", AUTHTYPE_NAME(0));
		for (ap = authenticators; ap->type; ap++) {
			if ((mask & (i = typemask(ap->type))) != 0)
				continue;
			mask |= i;
			(void) printf("\t%s\n", AUTHTYPE_NAME(ap->type));
		}
		return (0);
	}

	if (!getauthmask(type, &mask)) {
		(void) printf(
			gettext("%s: invalid authentication type\n"), type);
		return (0);
	}
	if (on)
		i_wont_support &= ~mask;
	else
		i_wont_support |= mask;
	return (1);
}

/*
 * Responds to the 'toggle authdebug' command.
 *
 * Returns:	1 - always
 */
int
auth_togdebug(int on)
{
	if (on < 0)
		auth_debug_mode = !auth_debug_mode;
	else
		auth_debug_mode = on > 0 ? B_TRUE : B_FALSE;
	(void) printf(auth_debug_mode ?
		gettext("auth debugging enabled\n") :
		gettext("auth debugging disabled\n"));
	return (1);
}

/*
 * Responds to the 'auth status' command.
 * Traverses the authenticator array and prints enabled or disabled for
 * each authentication type, depencing on the i_wont_support bitfield.
 *
 * Returns:	1 - always
 */
int
auth_status(void)
{
	Authenticator *ap;
	uint_t i, mask;

	if (i_wont_support == (uint_t)-1)
		(void) printf(gettext("Authentication disabled\n"));
	else
		(void) printf(gettext("Authentication enabled\n"));

	mask = 0;
	for (ap = authenticators; ap->type; ap++) {
		if ((mask & (i = typemask(ap->type))) != 0)
			continue;
		mask |= i;
		(void) printf("%s: %s\n", AUTHTYPE_NAME(ap->type),
			(i_wont_support & typemask(ap->type)) ?
			gettext("disabled") : gettext("enabled"));
	}
	return (1);
}

/*
 * This is called when an AUTH SEND is received.
 * data is a list of authentication mechanisms we support
 */
void
auth_send(uchar_t *data, int cnt)
{

	if (auth_debug_mode) {
		(void) printf(gettext(">>>%s: auth_send got:"), Name);
		printd(data, cnt);
		(void) printf("\r\n");
	}

	/*
	 * Save the list of authentication mechanisms
	 */
	auth_send_cnt = cnt;
	if (auth_send_cnt > sizeof (_auth_send_data))
	    auth_send_cnt = sizeof (_auth_send_data);
	(void) memcpy((void *)_auth_send_data, (void *)data, auth_send_cnt);
	auth_send_data = _auth_send_data;

	auth_send_retry();
}

/*
 * Try the next authentication mechanism on the list, and see if it
 * works.
 */
void
auth_send_retry(void)
{
	Authenticator *ap;
	static uchar_t str_none[] = { IAC, SB, TELOPT_AUTHENTICATION,
		TELQUAL_IS, AUTHTYPE_NULL, 0, IAC, SE };

	for (; (auth_send_cnt -= 2) >= 0; auth_send_data += 2) {
	    if (auth_debug_mode)
		(void) printf(
			gettext(">>>%s: Remote host supports %d\r\n"),
			Name, *auth_send_data);
	    if (!(i_support & typemask(*auth_send_data)))
		continue;
	    if (i_wont_support & typemask(*auth_send_data))
		continue;
	    ap = findauthenticator(auth_send_data[0], auth_send_data[1]);
	    if (!ap || !ap->send)
		continue;
	    if ((ap->way & AUTH_ENCRYPT_MASK) && !auth_enable_encrypt)
		continue;

	    if (auth_debug_mode)
		(void) printf(
			gettext(">>>%s: Trying %d %d\r\n"), Name,
			auth_send_data[0], auth_send_data[1]);
	    if ((*ap->send)(ap)) {
		/*
		 * Okay, we found one we like and did it.  we can go
		 * home now.
		 */
		if (auth_debug_mode)
		    (void) printf(gettext(">>>%s: Using type %d\r\n"),
			Name, *auth_send_data);
		auth_send_data += 2;
		return;
	    }
	}
	(void) net_write(str_none, sizeof (str_none));
	printsub('>', &str_none[2], sizeof (str_none) - 2);
	if (auth_debug_mode)
		(void) printf(
			gettext(">>>%s: Sent failure message\r\n"), Name);
	auth_finished(0, AUTH_REJECT);
	auth_has_failed = B_TRUE;
}

void
auth_reply(uchar_t *data, int cnt)
{
	Authenticator *ap;

	if (cnt < 2)
		return;

	if (ap = findauthenticator(data[0], data[1])) {
		if (ap->reply)
			(*ap->reply)(ap, data+2, cnt-2);
	} else if (auth_debug_mode)
		(void) printf(gettext
			(">>>%s: Invalid authentication in SEND: %d\r\n"),
			Name, *data);
}

int
auth_sendname(uchar_t *cp, int len)
{
	static uchar_t str_request[AUTH_NAME_BUFSIZ + 6] = { IAC, SB,
		TELOPT_AUTHENTICATION, TELQUAL_NAME, };
	register uchar_t *e = str_request + 4;
	register uchar_t *ee = &str_request[sizeof (str_request) - 2];

	while (--len >= 0) {
		if ((*e++ = *cp++) == IAC)
			*e++ = IAC;
		if (e >= ee)
			return (0);
	}
	*e++ = IAC;
	*e++ = SE;
	(void) net_write(str_request, e - str_request);
	printsub('>', &str_request[2], e - &str_request[2]);
	return (1);
}

/* ARGSUSED */
void
auth_finished(Authenticator *ap, int result)
{
	authenticated = ap;
	if (authenticated == NULL)
		authenticated = &NoAuth;
}

void
auth_printsub(uchar_t *data, uint_t cnt, uchar_t *buf, uint_t buflen)
{
	Authenticator *ap;

	ap = findauthenticator(data[1], data[2]);
	if (ap && ap->printsub)
		(*ap->printsub)(data, cnt, buf, buflen);
	else
		auth_gen_printsub(data, cnt, buf, buflen);
}

static void
auth_gen_printsub(uchar_t *data, uint_t cnt, uchar_t *buf, uint_t buflen)
{
	register uchar_t *cp;
	uchar_t lbuf[AUTH_LBUF_BUFSIZ];

	if (buflen < 2)
		return;
	cnt = (cnt > 3) ? cnt - 3 : 0;
	data += 3;
	buf[buflen - 1] = '\0';
	buf[buflen - 2] = '*';
	buflen -= 2;
	for (; cnt > 0; cnt--, data++) {
		(void) snprintf((char *)lbuf, AUTH_LBUF_BUFSIZ, " %d", *data);
		for (cp = lbuf; (*cp != '\0') && (buflen > 0); --buflen)
			*buf++ = *cp++;
		if (buflen == 0)
			return;
	}
	*buf = '\0';
}