2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static const char sccsid
[] = "@(#)auth.c 8.3 (Berkeley) 5/30/95";
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD: src/contrib/telnet/libtelnet/auth.c,v 1.10 2003/05/04 02:54:48 obrien Exp $");
45 * Copyright (C) 1990 by the Massachusetts Institute of Technology
47 * Export of this software from the United States of America is assumed
48 * to require a specific license from the United States Government.
49 * It is the responsibility of any person or organization contemplating
50 * export to obtain such a license before exporting.
52 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
53 * distribute this software and its documentation for any purpose and
54 * without fee is hereby granted, provided that the above copyright
55 * notice appear in all copies and that both that copyright notice and
56 * this permission notice appear in supporting documentation, and that
57 * the name of M.I.T. not be used in advertising or publicity pertaining
58 * to distribution of the software without specific, written prior
59 * permission. M.I.T. makes no representations about the suitability of
60 * this software for any purpose. It is provided "as is" without express
61 * or implied warranty.
67 #include <sys/types.h>
73 #include <arpa/telnet.h>
77 #include "misc-proto.h"
78 #include "auth-proto.h"
80 #define typemask(x) ((x) > 0 ? 1 << ((x)-1) : 0)
83 extern krb4encpwd_init();
84 extern krb4encpwd_send();
85 extern krb4encpwd_is();
86 extern krb4encpwd_reply();
87 extern krb4encpwd_status();
88 extern krb4encpwd_printsub();
92 extern rsaencpwd_init();
93 extern rsaencpwd_send();
94 extern rsaencpwd_is();
95 extern rsaencpwd_reply();
96 extern rsaencpwd_status();
97 extern rsaencpwd_printsub();
100 int auth_debug_mode
= 0;
101 static const char *Name
= "Noname";
102 static int Server
= 0;
103 static Authenticator
*authenticated
= 0;
104 static int authenticating
= 0;
105 static int validuser
= 0;
106 static unsigned char _auth_send_data
[256];
107 static unsigned char *auth_send_data
;
108 static int auth_send_cnt
= 0;
110 int auth_onoff(char *type
, int on
);
111 void auth_encrypt_user(char *name
);
114 * Authentication types supported. Plese note that these are stored
115 * in priority order, i.e. try the first one first.
117 Authenticator authenticators
[] = {
120 { AUTHTYPE_KERBEROS_V5
, AUTH_WHO_CLIENT
|AUTH_HOW_MUTUAL
,
122 kerberos5_send_mutual
,
126 kerberos5_printsub
},
127 # endif /* ENCRYPTION */
128 { AUTHTYPE_KERBEROS_V5
, AUTH_WHO_CLIENT
|AUTH_HOW_ONE_WAY
,
130 kerberos5_send_oneway
,
134 kerberos5_printsub
},
138 { AUTHTYPE_KERBEROS_V4
, AUTH_WHO_CLIENT
|AUTH_HOW_MUTUAL
,
144 kerberos4_printsub
},
145 # endif /* ENCRYPTION */
146 { AUTHTYPE_KERBEROS_V4
, AUTH_WHO_CLIENT
|AUTH_HOW_ONE_WAY
,
152 kerberos4_printsub
},
155 { AUTHTYPE_KRB4_ENCPWD
, AUTH_WHO_CLIENT
|AUTH_HOW_MUTUAL
,
161 krb4encpwd_printsub
},
164 { AUTHTYPE_RSA_ENCPWD
, AUTH_WHO_CLIENT
|AUTH_HOW_ONE_WAY
,
170 rsaencpwd_printsub
},
173 { AUTHTYPE_SRA
, AUTH_WHO_CLIENT
|AUTH_HOW_ONE_WAY
,
182 { 0, 0, 0, 0, 0, 0, 0, 0 },
185 static Authenticator NoAuth
= { 0, 0, 0, 0, 0, 0, 0, 0 };
187 static int i_support
= 0;
188 static int i_wont_support
= 0;
191 findauthenticator(int type
, int way
)
193 Authenticator
*ap
= authenticators
;
195 while (ap
->type
&& (ap
->type
!= type
|| ap
->way
!= way
))
197 return(ap
->type
? ap
: 0);
201 auth_init(const char *name
, int server
)
203 Authenticator
*ap
= authenticators
;
212 if (!ap
->init
|| (*ap
->init
)(ap
, server
)) {
213 i_support
|= typemask(ap
->type
);
215 printf(">>>%s: I support auth type %d %d\r\n",
219 else if (auth_debug_mode
)
220 printf(">>>%s: Init failed: auth type %d %d\r\n",
221 Name
, ap
->type
, ap
->way
);
227 auth_disable_name(char *name
)
230 for (x
= 0; x
< AUTHTYPE_CNT
; ++x
) {
231 if (AUTHTYPE_NAME(x
) && !strcasecmp(name
, AUTHTYPE_NAME(x
))) {
232 i_wont_support
|= typemask(x
);
239 getauthmask(char *type
, int *maskp
)
243 if (AUTHTYPE_NAME(0) && !strcasecmp(type
, AUTHTYPE_NAME(0))) {
248 for (x
= 1; x
< AUTHTYPE_CNT
; ++x
) {
249 if (AUTHTYPE_NAME(x
) && !strcasecmp(type
, AUTHTYPE_NAME(x
))) {
250 *maskp
= typemask(x
);
258 auth_enable(char *type
)
260 return(auth_onoff(type
, 1));
264 auth_disable(char *type
)
266 return(auth_onoff(type
, 0));
270 auth_onoff(char *type
, int on
)
275 if (!strcasecmp(type
, "?") || !strcasecmp(type
, "help")) {
276 printf("auth %s 'type'\n", on
? "enable" : "disable");
277 printf("Where 'type' is one of:\n");
278 printf("\t%s\n", AUTHTYPE_NAME(0));
280 for (ap
= authenticators
; ap
->type
; ap
++) {
281 if ((mask
& (i
= typemask(ap
->type
))) != 0)
284 printf("\t%s\n", AUTHTYPE_NAME(ap
->type
));
289 if (!getauthmask(type
, &mask
)) {
290 printf("%s: invalid authentication type\n", type
);
294 i_wont_support
&= ~mask
;
296 i_wont_support
|= mask
;
301 auth_togdebug(int on
)
304 auth_debug_mode
^= 1;
306 auth_debug_mode
= on
;
307 printf("auth debugging %s\n", auth_debug_mode
? "enabled" : "disabled");
317 if (i_wont_support
== -1)
318 printf("Authentication disabled\n");
320 printf("Authentication enabled\n");
323 for (ap
= authenticators
; ap
->type
; ap
++) {
324 if ((mask
& (i
= typemask(ap
->type
))) != 0)
327 printf("%s: %s\n", AUTHTYPE_NAME(ap
->type
),
328 (i_wont_support
& typemask(ap
->type
)) ?
329 "disabled" : "enabled");
335 * This routine is called by the server to start authentication
341 static unsigned char str_request
[64] = { IAC
, SB
,
342 TELOPT_AUTHENTICATION
,
344 Authenticator
*ap
= authenticators
;
345 unsigned char *e
= str_request
+ 4;
347 if (!authenticating
) {
350 if (i_support
& ~i_wont_support
& typemask(ap
->type
)) {
351 if (auth_debug_mode
) {
352 printf(">>>%s: Sending type %d %d\r\n",
353 Name
, ap
->type
, ap
->way
);
362 net_write(str_request
, e
- str_request
);
363 printsub('>', &str_request
[2], e
- str_request
- 2);
368 * This is called when an AUTH SEND is received.
369 * It should never arrive on the server side (as only the server can
370 * send an AUTH SEND).
371 * You should probably respond to it if you can...
373 * If you want to respond to the types out of order (i.e. even
374 * if he sends LOGIN KERBEROS and you support both, you respond
375 * with KERBEROS instead of LOGIN (which is against what the
376 * protocol says)) you will have to hack this code...
379 auth_send(unsigned char *data
, int cnt
)
382 static unsigned char str_none
[] = { IAC
, SB
, TELOPT_AUTHENTICATION
,
383 TELQUAL_IS
, AUTHTYPE_NULL
, 0,
386 if (auth_debug_mode
) {
387 printf(">>>%s: auth_send called!\r\n", Name
);
392 if (auth_debug_mode
) {
393 printf(">>>%s: auth_send got:", Name
);
394 printd(data
, cnt
); printf("\r\n");
398 * Save the data, if it is new, so that we can continue looking
399 * at it if the authorization we try doesn't work
401 if (data
< _auth_send_data
||
402 data
> _auth_send_data
+ sizeof(_auth_send_data
)) {
403 auth_send_cnt
= (size_t)cnt
> sizeof(_auth_send_data
)
404 ? sizeof(_auth_send_data
)
406 memmove((void *)_auth_send_data
, (void *)data
, auth_send_cnt
);
407 auth_send_data
= _auth_send_data
;
410 * This is probably a no-op, but we just make sure
412 auth_send_data
= data
;
415 while ((auth_send_cnt
-= 2) >= 0) {
417 printf(">>>%s: He supports %d\r\n",
418 Name
, *auth_send_data
);
419 if ((i_support
& ~i_wont_support
) & typemask(*auth_send_data
)) {
420 ap
= findauthenticator(auth_send_data
[0],
422 if (ap
&& ap
->send
) {
424 printf(">>>%s: Trying %d %d\r\n",
425 Name
, auth_send_data
[0],
427 if ((*ap
->send
)(ap
)) {
429 * Okay, we found one we like
431 * we can go home now.
434 printf(">>>%s: Using type %d\r\n",
435 Name
, *auth_send_data
);
441 * just continue on and look for the
442 * next one if we didn't do anything.
447 net_write(str_none
, sizeof(str_none
));
448 printsub('>', &str_none
[2], sizeof(str_none
) - 2);
450 printf(">>>%s: Sent failure message\r\n", Name
);
451 auth_finished(0, AUTH_REJECT
);
455 auth_send_retry(void)
458 * if auth_send_cnt <= 0 then auth_send will end up rejecting
459 * the authentication and informing the other side of this.
461 auth_send(auth_send_data
, auth_send_cnt
);
465 auth_is(unsigned char *data
, int cnt
)
472 if (data
[0] == AUTHTYPE_NULL
) {
473 auth_finished(0, AUTH_REJECT
);
477 if ((ap
= findauthenticator(data
[0], data
[1]))) {
479 (*ap
->is
)(ap
, data
+2, cnt
-2);
480 } else if (auth_debug_mode
)
481 printf(">>>%s: Invalid authentication in IS: %d\r\n",
486 auth_reply(unsigned char *data
, int cnt
)
493 if ((ap
= findauthenticator(data
[0], data
[1]))) {
495 (*ap
->reply
)(ap
, data
+2, cnt
-2);
496 } else if (auth_debug_mode
)
497 printf(">>>%s: Invalid authentication in SEND: %d\r\n",
502 auth_name(unsigned char *data
, int cnt
)
504 unsigned char savename
[256];
508 printf(">>>%s: Empty name in NAME\r\n", Name
);
511 if ((size_t)cnt
> sizeof(savename
) - 1) {
513 printf(">>>%s: Name in NAME (%d) exceeds %d length\r\n",
514 Name
, cnt
, (u_int
)sizeof(savename
)-1);
517 memmove((void *)savename
, (void *)data
, cnt
);
518 savename
[cnt
] = '\0'; /* Null terminate */
520 printf(">>>%s: Got NAME [%s]\r\n", Name
, savename
);
521 auth_encrypt_user(savename
);
525 auth_sendname(unsigned char *cp
, int len
)
527 static unsigned char str_request
[256+6]
528 = { IAC
, SB
, TELOPT_AUTHENTICATION
, TELQUAL_NAME
, };
529 unsigned char *e
= str_request
+ 4;
530 unsigned char *ee
= &str_request
[sizeof(str_request
)-2];
533 if ((*e
++ = *cp
++) == IAC
)
540 net_write(str_request
, e
- str_request
);
541 printsub('>', &str_request
[2], e
- &str_request
[2]);
546 auth_finished(Authenticator
*ap
, int result
)
548 if (!(authenticated
= ap
))
549 authenticated
= &NoAuth
;
555 auth_intr(int sig __unused
)
557 auth_finished(0, AUTH_REJECT
);
561 auth_wait(char *name
)
564 printf(">>>%s: in auth_wait.\r\n", Name
);
566 if (Server
&& !authenticating
)
569 (void) signal(SIGALRM
, auth_intr
);
571 while (!authenticated
)
575 (void) signal(SIGALRM
, SIG_DFL
);
578 * Now check to see if the user is valid or not
580 if (!authenticated
|| authenticated
== &NoAuth
)
583 if (validuser
== AUTH_VALID
)
584 validuser
= AUTH_USER
;
586 if (authenticated
->status
)
587 validuser
= (*authenticated
->status
)(authenticated
,
593 auth_printsub(unsigned char *data
, int cnt
, unsigned char *buf
, int buflen
)
597 if ((ap
= findauthenticator(data
[1], data
[2])) && ap
->printsub
)
598 (*ap
->printsub
)(data
, cnt
, buf
, buflen
);
600 auth_gen_printsub(data
, cnt
, buf
, buflen
);
604 auth_gen_printsub(unsigned char *data
, int cnt
, unsigned char *buf
, int buflen
)
607 unsigned char tbuf
[16];
611 buf
[buflen
-1] = '\0';
614 for (; cnt
> 0; cnt
--, data
++) {
615 sprintf((char *)tbuf
, " %d", *data
);
616 for (cp
= tbuf
; *cp
&& buflen
> 0; --buflen
)