]>
git.cameronkatri.com Git - apple_cmds.git/blob - lib/libtelnet/kerberos.c
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
34 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: src/crypto/telnet/libtelnet/kerberos.c,v 1.3.2.1 2002/04/13 10:59:07 markm Exp $");
41 #define __unused __attribute__((__unused__))
45 static const char sccsid
[] = "@(#)kerberos.c 8.3 (Berkeley) 5/30/95";
49 * Copyright (C) 1990 by the Massachusetts Institute of Technology
51 * Export of this software from the United States of America is assumed
52 * to require a specific license from the United States Government.
53 * It is the responsibility of any person or organization contemplating
54 * export to obtain such a license before exporting.
56 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
57 * distribute this software and its documentation for any purpose and
58 * without fee is hereby granted, provided that the above copyright
59 * notice appear in all copies and that both that copyright notice and
60 * this permission notice appear in supporting documentation, and that
61 * the name of M.I.T. not be used in advertising or publicity pertaining
62 * to distribution of the software without specific, written prior
63 * permission. M.I.T. makes no representations about the suitability of
64 * this software for any purpose. It is provided "as is" without express
65 * or implied warranty.
69 #include <sys/types.h>
70 #include <arpa/telnet.h>
71 #include <des.h> /* BSD wont include this in krb.h, so we do it here */
81 int kerberos4_cksum(unsigned char *, int);
82 int kuserok(AUTH_DAT
*, char *);
84 extern int auth_debug_mode
;
86 static unsigned char str_data
[1024] = { IAC
, SB
, TELOPT_AUTHENTICATION
, 0,
87 AUTHTYPE_KERBEROS_V4
, };
89 #define KRB_AUTH 0 /* Authentication data follows */
90 #define KRB_REJECT 1 /* Rejected (reason might follow) */
91 #define KRB_ACCEPT 2 /* Accepted */
92 #define KRB_CHALLENGE 3 /* Challenge for mutual auth. */
93 #define KRB_RESPONSE 4 /* Response for mutual auth. */
96 static char name
[ANAME_SZ
];
97 static AUTH_DAT adat
= { 0, "", "", "", 0, {}, 0, 0, 0, { 0, "", 0 } };
99 static Block session_key
= { 0 };
100 static des_key_schedule sched
;
101 static Block challenge
= { 0 };
102 #endif /* ENCRYPTION */
104 static char krb_service_name
[] = "rcmd";
105 static char empty
[] = "";
108 Data(Authenticator
*ap
, int type
, const unsigned char *d
, int c
)
110 unsigned char *p
= str_data
+ 4;
111 const unsigned char *cd
= d
;
116 if (auth_debug_mode
) {
117 printf("%s:%d: [%d] (%d)",
118 str_data
[3] == TELQUAL_IS
? ">>>IS" : ">>>REPLY",
128 if ((*p
++ = *cd
++) == IAC
)
133 if (str_data
[3] == TELQUAL_IS
)
134 printsub('>', &str_data
[2], p
- (&str_data
[2]));
135 return(net_write(str_data
, p
- str_data
));
139 kerberos4_init(Authenticator
*ap __unused
, int server
)
144 str_data
[3] = TELQUAL_REPLY
;
145 if ((fp
= fopen(KEYFILE
, "r")) == NULL
)
149 str_data
[3] = TELQUAL_IS
;
154 char dst_realm_buf
[REALM_SZ
], *dest_realm
= NULL
;
155 int dst_realm_sz
= REALM_SZ
;
158 kerberos4_send(Authenticator
*ap
)
161 char instance
[INST_SZ
];
166 printf("[ Trying KERBEROS4 ... ]\n");
167 if (!UserNameRequested
) {
168 if (auth_debug_mode
) {
169 printf("Kerberos V4: no user name supplied\r\n");
174 memset(instance
, 0, sizeof(instance
));
176 if ((realm
= krb_get_phost(RemoteHostName
)))
177 strncpy(instance
, realm
, sizeof(instance
));
179 instance
[sizeof(instance
)-1] = '\0';
181 realm
= dest_realm
? dest_realm
: krb_realmofhost(RemoteHostName
);
184 printf("Kerberos V4: no realm for %s\r\n", RemoteHostName
);
187 if ((r
= krb_mk_req(&lauth
, krb_service_name
, instance
, realm
, 0L))) {
188 printf("mk_req failed: %s\r\n", krb_err_txt
[r
]);
191 if ((r
= krb_get_cred(krb_service_name
, instance
, realm
, &cred
))) {
192 printf("get_cred failed: %s\r\n", krb_err_txt
[r
]);
195 if (!auth_sendname(UserNameRequested
, strlen(UserNameRequested
))) {
197 printf("Not enough room for user name\r\n");
201 printf("Sent %d bytes of authentication data\r\n", lauth
.length
);
202 if (!Data(ap
, KRB_AUTH
, (void *)lauth
.dat
, lauth
.length
)) {
204 printf("Not enough room for authentication data\r\n");
209 * If we are doing mutual authentication, get set up to send
210 * the challenge, and verify it when the response comes back.
212 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) {
215 des_key_sched(cred
.session
, sched
);
216 des_init_random_number_generator(cred
.session
);
217 des_new_random_key(session_key
);
218 des_ecb_encrypt(&session_key
, &session_key
, sched
, 0);
219 des_ecb_encrypt(&session_key
, &challenge
, sched
, 0);
221 * Increment the challenge by 1, and encrypt it for
224 for (i
= 7; i
>= 0; --i
) {
226 x
= (unsigned int)challenge
[i
] + 1;
227 challenge
[i
] = x
; /* ignore overflow */
228 if (x
< 256) /* if no overflow, all done */
231 des_ecb_encrypt(&challenge
, &challenge
, sched
, 1);
233 #endif /* ENCRYPTION */
235 if (auth_debug_mode
) {
236 printf("CK: %d:", kerberos4_cksum(lauth
.dat
, lauth
.length
));
237 printd(lauth
.dat
, lauth
.length
);
239 printf("Sent Kerberos V4 credentials to server\r\n");
245 kerberos4_is(Authenticator
*ap
, unsigned char *data
, int cnt
)
250 #endif /* ENCRYPTION */
251 char realm
[REALM_SZ
];
252 char instance
[INST_SZ
];
259 if (krb_get_lrealm(realm
, 1) != KSUCCESS
) {
260 Data(ap
, KRB_REJECT
, "No local V4 Realm.", -1);
261 auth_finished(ap
, AUTH_REJECT
);
263 printf("No local realm\r\n");
266 memmove((void *)auth
.dat
, (void *)data
, auth
.length
= cnt
);
267 if (auth_debug_mode
) {
268 printf("Got %d bytes of authentication data\r\n", cnt
);
269 printf("CK: %d:", kerberos4_cksum(auth
.dat
, auth
.length
));
270 printd(auth
.dat
, auth
.length
);
273 instance
[0] = '*'; instance
[1] = 0;
274 if ((r
= krb_rd_req(&auth
, krb_service_name
,
275 instance
, 0, &adat
, empty
))) {
277 printf("Kerberos failed him as %s\r\n", name
);
278 Data(ap
, KRB_REJECT
, krb_err_txt
[r
], -1);
279 auth_finished(ap
, AUTH_REJECT
);
283 memmove((void *)session_key
, (void *)adat
.session
, sizeof(Block
));
284 #endif /* ENCRYPTION */
285 strncpy (name
, adat
.pname
, ANAME_SZ
-1);
286 name
[ANAME_SZ
-1] = '\0';
288 if (UserNameRequested
&& !kuserok(&adat
, UserNameRequested
))
289 Data(ap
, KRB_ACCEPT
, NULL
, 0);
291 Data(ap
, KRB_REJECT
, "user is not authorized", -1);
292 auth_finished(ap
, AUTH_USER
);
297 Data(ap
, KRB_RESPONSE
, NULL
, 0);
298 #else /* ENCRYPTION */
299 if (!VALIDKEY(session_key
)) {
301 * We don't have a valid session key, so just
302 * send back a response with an empty session
305 Data(ap
, KRB_RESPONSE
, NULL
, 0);
310 * Initialize the random number generator since it's
311 * used later on by the encryption routine.
313 des_init_random_number_generator(session_key
);
314 des_key_sched(session_key
, sched
);
315 memmove((void *)datablock
, (void *)data
, sizeof(Block
));
317 * Take the received encrypted challenge, and encrypt
318 * it again to get a unique session_key for the
321 des_ecb_encrypt(&datablock
, &session_key
, sched
, 1);
324 skey
.data
= session_key
;
325 encrypt_session_key(&skey
, 1);
327 * Now decrypt the received encrypted challenge,
328 * increment by one, re-encrypt it and send it back.
330 des_ecb_encrypt(&datablock
, &challenge
, sched
, 0);
331 for (r
= 7; r
>= 0; r
--) {
333 t
= (unsigned int)challenge
[r
] + 1;
334 challenge
[r
] = t
; /* ignore overflow */
335 if (t
< 256) /* if no overflow, all done */
338 des_ecb_encrypt(&challenge
, &challenge
, sched
, 1);
339 Data(ap
, KRB_RESPONSE
, challenge
, sizeof(challenge
));
340 #endif /* ENCRYPTION */
345 printf("Unknown Kerberos option %d\r\n", data
[-1]);
346 Data(ap
, KRB_REJECT
, NULL
, 0);
352 kerberos4_reply(Authenticator
*ap
, unsigned char *data
, int cnt
)
356 #endif /* ENCRYPTION */
363 printf("[ Kerberos V4 refuses authentication because %.*s ]\r\n",
366 printf("[ Kerberos V4 refuses authentication ]\r\n");
370 printf("[ Kerberos V4 accepts you ]\n");
371 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) {
373 * Send over the encrypted challenge.
376 Data(ap
, KRB_CHALLENGE
, NULL
, 0);
377 #else /* ENCRYPTION */
378 Data(ap
, KRB_CHALLENGE
, session_key
,
379 sizeof(session_key
));
380 des_ecb_encrypt(&session_key
, &session_key
, sched
, 1);
383 skey
.data
= session_key
;
384 encrypt_session_key(&skey
, 0);
385 #endif /* ENCRYPTION */
388 auth_finished(ap
, AUTH_USER
);
393 * Verify that the response to the challenge is correct.
395 if ((cnt
!= sizeof(Block
)) ||
396 (0 != memcmp((void *)data
, (void *)challenge
,
399 #endif /* ENCRYPTION */
400 printf("[ Kerberos V4 challenge failed!!! ]\r\n");
405 printf("[ Kerberos V4 challenge successful ]\r\n");
406 auth_finished(ap
, AUTH_USER
);
407 #endif /* ENCRYPTION */
411 printf("Unknown Kerberos option %d\r\n", data
[-1]);
417 kerberos4_status(Authenticator
*ap __unused
, char *nam
, int level
)
419 if (level
< AUTH_USER
)
422 if (UserNameRequested
&& !kuserok(&adat
, UserNameRequested
)) {
423 strcpy(nam
, UserNameRequested
);
429 #define BUMP(buf, len) while (*(buf)) {++(buf), --(len);}
430 #define ADDC(buf, len, c) if ((len) > 0) {*(buf)++ = (c); --(len);}
433 kerberos4_printsub(unsigned char *data
, int cnt
, unsigned char *buf
, int buflen
)
438 buf
[buflen
-1] = '\0'; /* make sure its NULL terminated */
442 case KRB_REJECT
: /* Rejected (reason might follow) */
443 strncpy((char *)buf
, " REJECT ", buflen
);
446 case KRB_ACCEPT
: /* Accepted (name might follow) */
447 strncpy((char *)buf
, " ACCEPT ", buflen
);
452 ADDC(buf
, buflen
, '"');
453 for (i
= 4; i
< cnt
; i
++)
454 ADDC(buf
, buflen
, data
[i
]);
455 ADDC(buf
, buflen
, '"');
456 ADDC(buf
, buflen
, '\0');
459 case KRB_AUTH
: /* Authentication data follows */
460 strncpy((char *)buf
, " AUTH", buflen
);
464 strncpy((char *)buf
, " CHALLENGE", buflen
);
468 strncpy((char *)buf
, " RESPONSE", buflen
);
472 sprintf(lbuf
, " %d (unknown)", data
[3]);
473 strncpy((char *)buf
, lbuf
, buflen
);
476 for (i
= 4; i
< cnt
; i
++) {
477 sprintf(lbuf
, " %d", data
[i
]);
478 strncpy((char *)buf
, lbuf
, buflen
);
486 kerberos4_cksum(unsigned char *d
, int n
)
491 * A comment is probably needed here for those not
492 * well versed in the "C" language. Yes, this is
493 * supposed to be a "switch" with the body of the
494 * "switch" being a "while" statement. The whole
495 * purpose of the switch is to allow us to jump into
496 * the middle of the while() loop, and then not have
497 * to do any more switch()s.
499 * Some compilers will spit out a warning message
500 * about the loop not being entered at the top.
505 ck
^= (int)*d
++ << 24;
508 ck
^= (int)*d
++ << 16;
511 ck
^= (int)*d
++ << 8;