~ubuntu-branches/ubuntu/maverick/krb5/maverick

« back to all changes in this revision

Viewing changes to src/appl/telnet/libtelnet/kerberos.c

  • Committer: Bazaar Package Importer
  • Author(s): Sam Hartman
  • Date: 2009-05-07 16:16:34 UTC
  • mfrom: (13.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20090507161634-xqyk0s9na0le4flj
Tags: 1.7dfsg~beta1-4
When  decrypting the TGS response fails with the subkey, try with the
session key to work around Heimdal bug, Closes: #527353 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*-
2
 
 * Copyright (c) 1991, 1993
3
 
 *      The Regents of the University of California.  All rights reserved.
4
 
 *
5
 
 * Redistribution and use in source and binary forms, with or without
6
 
 * modification, are permitted provided that the following conditions
7
 
 * are met:
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.
20
 
 *
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
31
 
 * SUCH DAMAGE.
32
 
 */
33
 
 
34
 
/* based on @(#)kerberos.c      8.1 (Berkeley) 6/4/93 */
35
 
 
36
 
/*
37
 
 * Copyright (C) 1990 by the Massachusetts Institute of Technology
38
 
 *
39
 
 * Export of this software from the United States of America may
40
 
 * require a specific license from the United States Government.
41
 
 * It is the responsibility of any person or organization contemplating
42
 
 * export to obtain such a license before exporting.
43
 
 *
44
 
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
45
 
 * distribute this software and its documentation for any purpose and
46
 
 * without fee is hereby granted, provided that the above copyright
47
 
 * notice appear in all copies and that both that copyright notice and
48
 
 * this permission notice appear in supporting documentation, and that
49
 
 * the name of M.I.T. not be used in advertising or publicity pertaining
50
 
 * to distribution of the software without specific, written prior
51
 
 * permission.  Furthermore if you modify this software you must label
52
 
 * your software as modified software and not distribute it in such a
53
 
 * fashion that it might be confused with the original M.I.T. software.
54
 
 * M.I.T. makes no representations about the suitability of
55
 
 * this software for any purpose.  It is provided "as is" without express
56
 
 * or implied warranty.
57
 
 */
58
 
 
59
 
/*
60
 
 * Copyright (C) 1998 by the FundsXpress, INC.
61
 
 * 
62
 
 * All rights reserved.
63
 
 * 
64
 
 * Export of this software from the United States of America may require
65
 
 * a specific license from the United States Government.  It is the
66
 
 * responsibility of any person or organization contemplating export to
67
 
 * obtain such a license before exporting.
68
 
 * 
69
 
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
70
 
 * distribute this software and its documentation for any purpose and
71
 
 * without fee is hereby granted, provided that the above copyright
72
 
 * notice appear in all copies and that both that copyright notice and
73
 
 * this permission notice appear in supporting documentation, and that
74
 
 * the name of FundsXpress. not be used in advertising or publicity pertaining
75
 
 * to distribution of the software without specific, written prior
76
 
 * permission.  FundsXpress makes no representations about the suitability of
77
 
 * this software for any purpose.  It is provided "as is" without express
78
 
 * or implied warranty.
79
 
 * 
80
 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
81
 
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
82
 
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
83
 
 */
84
 
 
85
 
#ifdef  KRB4
86
 
/* this code must be compiled in the krb5 tree.  disgustingly, there
87
 
   is code in here which declares structures which happen to mirror
88
 
   the krb4 des structures.  I didn't want to rototill this *completely*
89
 
   so this is how it's going to work. --marc */
90
 
#include <krb5.h>
91
 
#include <sys/types.h>
92
 
#include <errno.h>
93
 
#include <arpa/telnet.h>
94
 
#include <stdio.h>
95
 
#include <des.h>        /* BSD wont include this in krb.h, so we do it here */
96
 
#include <krb.h>
97
 
#ifdef  __STDC__
98
 
#include <stdlib.h>
99
 
#endif
100
 
#ifdef  HAVE_STRING_H
101
 
#include <string.h>
102
 
#else
103
 
#include <strings.h>
104
 
#endif
105
 
 
106
 
#include "encrypt.h"
107
 
#include "auth.h"
108
 
#include "misc.h"
109
 
 
110
 
extern int auth_debug_mode;
111
 
extern krb5_context telnet_context;
112
 
 
113
 
int kerberos4_cksum (unsigned char *, int);
114
 
 
115
 
static unsigned char str_data[1024] = { IAC, SB, TELOPT_AUTHENTICATION, 0,
116
 
                                        AUTHTYPE_KERBEROS_V4, };
117
 
#if 0
118
 
static unsigned char str_name[1024] = { IAC, SB, TELOPT_AUTHENTICATION,
119
 
                                        TELQUAL_NAME, };
120
 
#endif
121
 
 
122
 
#define KRB_AUTH        0               /* Authentication data follows */
123
 
#define KRB_REJECT      1               /* Rejected (reason might follow) */
124
 
#define KRB_ACCEPT      2               /* Accepted */
125
 
#define KRB_CHALLENGE   3               /* Challenge for mutual auth. */
126
 
#define KRB_RESPONSE    4               /* Response for mutual auth. */
127
 
 
128
 
#define KRB_SERVICE_NAME   "rcmd"
129
 
 
130
 
static  KTEXT_ST auth;
131
 
static  char name[ANAME_SZ];
132
 
static  AUTH_DAT adat = { 0 };
133
 
#ifdef  ENCRYPTION
134
 
static Block    session_key     = { 0 };
135
 
static krb5_keyblock krbkey;
136
 
static Block    challenge       = { 0 };
137
 
#endif  /* ENCRYPTION */
138
 
 
139
 
        static int
140
 
Data(ap, type, d, c)
141
 
        Authenticator *ap;
142
 
        int type;
143
 
        const void *d;
144
 
        int c;
145
 
{
146
 
        unsigned char *p = str_data + 4;
147
 
        const unsigned char *cd = (const unsigned char *)d;
148
 
        size_t spaceleft = sizeof(str_data) - 4;
149
 
        if (c == -1)
150
 
                c = strlen((const char *)cd);
151
 
 
152
 
        if (auth_debug_mode) {
153
 
                printf("%s:%d: [%d] (%d)",
154
 
                        str_data[3] == TELQUAL_IS ? ">>>IS" : ">>>REPLY",
155
 
                        str_data[3],
156
 
                        type, c);
157
 
                printd(d, c);
158
 
                printf("\r\n");
159
 
        }
160
 
        *p++ = ap->type;
161
 
        *p++ = ap->way;
162
 
        *p++ = type;
163
 
        spaceleft -= 3;
164
 
        while (c-- > 0) {
165
 
                if ((*p++ = *cd++) == IAC) {
166
 
                        *p++ = IAC;
167
 
                        spaceleft--;
168
 
                }
169
 
                if ((--spaceleft < 4) && c) {
170
 
                        errno = ENOMEM;
171
 
                        return -1;
172
 
                }
173
 
        }
174
 
        *p++ = IAC;
175
 
        *p++ = SE;
176
 
        if (str_data[3] == TELQUAL_IS)
177
 
                printsub('>', &str_data[2], p - (&str_data[2]));
178
 
        return(net_write(str_data, p - str_data));
179
 
}
180
 
 
181
 
        int
182
 
kerberos4_init(ap, server)
183
 
        Authenticator *ap;
184
 
        int server;
185
 
{
186
 
        FILE *fp;
187
 
 
188
 
        if (server) {
189
 
                str_data[3] = TELQUAL_REPLY;
190
 
                if ((fp = fopen(KEYFILE, "r")) == NULL)
191
 
                        return(0);
192
 
                fclose(fp);
193
 
        } else {
194
 
                str_data[3] = TELQUAL_IS;
195
 
        }
196
 
 
197
 
        kerberos5_init(NULL, server);
198
 
 
199
 
        return(1);
200
 
}
201
 
 
202
 
char dst_realm_buf[REALM_SZ], *dest_realm = NULL;
203
 
unsigned int dst_realm_sz = REALM_SZ;
204
 
 
205
 
        int
206
 
kerberos4_send(ap)
207
 
        Authenticator *ap;
208
 
{
209
 
        KTEXT_ST kauth;
210
 
        char instance[INST_SZ];
211
 
        char *realm;
212
 
        char *krb_realmofhost();
213
 
        char *krb_get_phost();
214
 
        CREDENTIALS cred;
215
 
        int r;
216
 
#ifdef ENCRYPTION
217
 
        krb5_data data;
218
 
        krb5_enc_data encdata;
219
 
        krb5_error_code code;
220
 
        krb5_keyblock rand_key;
221
 
#endif
222
 
 
223
 
        printf("[ Trying KERBEROS4 ... ]\r\n"); 
224
 
        if (!UserNameRequested) {
225
 
                if (auth_debug_mode) {
226
 
                        printf("Kerberos V4: no user name supplied\r\n");
227
 
                }
228
 
                return(0);
229
 
        }
230
 
 
231
 
        memset(instance, 0, sizeof(instance));
232
 
 
233
 
        if ((realm = krb_get_phost(RemoteHostName)))
234
 
                strncpy(instance, realm, sizeof(instance));
235
 
 
236
 
        instance[sizeof(instance)-1] = '\0';
237
 
 
238
 
        realm = dest_realm ? dest_realm : krb_realmofhost(RemoteHostName);
239
 
 
240
 
        if (!realm) {
241
 
                printf("Kerberos V4: no realm for %s\r\n", RemoteHostName);
242
 
                return(0);
243
 
        }
244
 
        if ((r = krb_mk_req(&kauth, KRB_SERVICE_NAME, instance, realm, 0))) {
245
 
                printf("mk_req failed: %s\r\n", krb_get_err_text(r));
246
 
                return(0);
247
 
        }
248
 
        if ((r = krb_get_cred(KRB_SERVICE_NAME, instance, realm, &cred))) {
249
 
                printf("get_cred failed: %s\r\n", krb_get_err_text(r));
250
 
                return(0);
251
 
        }
252
 
        if (!auth_sendname(UserNameRequested, strlen(UserNameRequested))) {
253
 
                if (auth_debug_mode)
254
 
                        printf("Not enough room for user name\r\n");
255
 
                return(0);
256
 
        }
257
 
        if (auth_debug_mode)
258
 
                printf("Sent %d bytes of authentication data\r\n", kauth.length);
259
 
        if (!Data(ap, KRB_AUTH, (void *)kauth.dat, kauth.length)) {
260
 
                if (auth_debug_mode)
261
 
                        printf("Not enough room for authentication data\r\n");
262
 
                return(0);
263
 
        }
264
 
#ifdef  ENCRYPTION
265
 
        /*
266
 
         * If we are doing mutual authentication, get set up to send
267
 
         * the challenge, and verify it when the response comes back.
268
 
         */
269
 
        if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) {
270
 
                register int i;
271
 
 
272
 
                data.data = cred.session;
273
 
                data.length = 8; /* sizeof(cred.session) */;
274
 
 
275
 
                if ((code = krb5_c_random_seed(telnet_context, &data))) {
276
 
                    com_err("libtelnet", code,
277
 
                            "while seeding random number generator");
278
 
                    return(0);
279
 
                }
280
 
 
281
 
                if ((code = krb5_c_make_random_key(telnet_context,
282
 
                                                   ENCTYPE_DES_CBC_RAW,
283
 
                                                   &rand_key))) {
284
 
                    com_err("libtelnet", code,
285
 
                            "while creating random session key");
286
 
                    return(0);
287
 
                }
288
 
 
289
 
                /* the krb4 code uses ecb mode, but on a single block
290
 
                   with a zero ivec, ecb and cbc are the same */
291
 
                krbkey.enctype = ENCTYPE_DES_CBC_RAW;
292
 
                krbkey.length = 8;
293
 
                krbkey.contents = cred.session;
294
 
 
295
 
                encdata.ciphertext.data = rand_key.contents;
296
 
                encdata.ciphertext.length = rand_key.length;
297
 
                encdata.enctype = ENCTYPE_UNKNOWN;
298
 
 
299
 
                data.data = session_key;
300
 
                data.length = 8;
301
 
 
302
 
                code = krb5_c_decrypt(telnet_context, &krbkey, 0, 0,
303
 
                                      &encdata, &data);
304
 
 
305
 
                krb5_free_keyblock_contents(telnet_context, &rand_key);
306
 
 
307
 
                if (code) {
308
 
                    com_err("libtelnet", code, "while encrypting random key");
309
 
                    return(0);
310
 
                }
311
 
 
312
 
                encdata.ciphertext.data = session_key;
313
 
                encdata.ciphertext.length = 8;
314
 
                encdata.enctype = ENCTYPE_UNKNOWN;
315
 
 
316
 
                data.data = challenge;
317
 
                data.length = 8;
318
 
 
319
 
                code = krb5_c_decrypt(telnet_context, &krbkey, 0, 0,
320
 
                                      &encdata, &data);
321
 
 
322
 
                /*
323
 
                 * Increment the challenge by 1, and encrypt it for
324
 
                 * later comparison.
325
 
                 */
326
 
                for (i = 7; i >= 0; --i) {
327
 
                        register int x;
328
 
                        x = (unsigned int)challenge[i] + 1;
329
 
                        challenge[i] = x;       /* ignore overflow */
330
 
                        if (x < 256)            /* if no overflow, all done */
331
 
                                break;
332
 
                }
333
 
 
334
 
                data.data = challenge;
335
 
                data.length = 8;
336
 
 
337
 
                encdata.ciphertext.data = challenge;
338
 
                encdata.ciphertext.length = 8;
339
 
                encdata.enctype = ENCTYPE_UNKNOWN;
340
 
 
341
 
                if ((code = krb5_c_encrypt(telnet_context, &krbkey, 0, 0, 
342
 
                                           &data, &encdata))) {
343
 
                    com_err("libtelnet", code, "while encrypting random key");
344
 
                    return(0);
345
 
                }
346
 
        }
347
 
#endif  /* ENCRYPTION */
348
 
        
349
 
        if (auth_debug_mode) {
350
 
                printf("CK: %d:", kerberos4_cksum(kauth.dat, kauth.length));
351
 
                printd(kauth.dat, kauth.length);
352
 
                printf("\r\n");
353
 
                printf("Sent Kerberos V4 credentials to server\r\n");
354
 
        }
355
 
        return(1);
356
 
}
357
 
 
358
 
        void
359
 
kerberos4_is(ap, data, cnt)
360
 
        Authenticator *ap;
361
 
        unsigned char *data;
362
 
        int cnt;
363
 
{
364
 
#ifdef  ENCRYPTION
365
 
        Session_Key skey;
366
 
        Block datablock, tmpkey;
367
 
        krb5_data kdata;
368
 
        krb5_enc_data encdata;
369
 
        krb5_error_code code;
370
 
#endif  /* ENCRYPTION */
371
 
        char realm[REALM_SZ];
372
 
        char instance[INST_SZ];
373
 
        int r;
374
 
 
375
 
        if (cnt-- < 1)
376
 
                return;
377
 
        switch (*data++) {
378
 
        case KRB_AUTH:
379
 
                if (krb_get_lrealm(realm, 1) != KSUCCESS) {
380
 
                        Data(ap, KRB_REJECT, (void *)"No local V4 Realm.", -1);
381
 
                        auth_finished(ap, AUTH_REJECT);
382
 
                        if (auth_debug_mode)
383
 
                                printf("No local realm\r\n");
384
 
                        return;
385
 
                }
386
 
                memcpy((void *)auth.dat, (void *)data, auth.length = cnt);
387
 
                if (auth_debug_mode) {
388
 
                        printf("Got %d bytes of authentication data\r\n", cnt);
389
 
                        printf("CK: %d:", kerberos4_cksum(auth.dat, auth.length));
390
 
                        printd(auth.dat, auth.length);
391
 
                        printf("\r\n");
392
 
                }
393
 
                instance[0] = '*'; instance[1] = 0;
394
 
                if ((r = krb_rd_req(&auth, KRB_SERVICE_NAME,
395
 
                                    instance, 0, &adat, ""))) {
396
 
                        if (auth_debug_mode)
397
 
                                printf("Kerberos failed him as %s\r\n", name);
398
 
                        Data(ap, KRB_REJECT, (const void *)krb_get_err_text(r), -1);
399
 
                        auth_finished(ap, AUTH_REJECT);
400
 
                        return;
401
 
                }
402
 
#ifdef  ENCRYPTION
403
 
                memcpy((void *)session_key, (void *)adat.session, sizeof(Block));
404
 
#endif  /* ENCRYPTION */
405
 
                krb_kntoln(&adat, name);
406
 
 
407
 
                if (UserNameRequested && !kuserok(&adat, UserNameRequested))
408
 
                        Data(ap, KRB_ACCEPT, (void *)0, 0);
409
 
                else
410
 
                        Data(ap, KRB_REJECT,
411
 
                                (void *)"user is not authorized", -1);
412
 
                auth_finished(ap, AUTH_USER);
413
 
                break;
414
 
 
415
 
        case KRB_CHALLENGE:
416
 
#ifndef ENCRYPTION
417
 
                Data(ap, KRB_RESPONSE, (void *)0, 0);
418
 
#else   /* ENCRYPTION */
419
 
                if (!VALIDKEY(session_key)) {
420
 
                        /*
421
 
                         * We don't have a valid session key, so just
422
 
                         * send back a response with an empty session
423
 
                         * key.
424
 
                         */
425
 
                        Data(ap, KRB_RESPONSE, (void *)0, 0);
426
 
                        break;
427
 
                }
428
 
 
429
 
                /*
430
 
                 * Initialize the random number generator since it's
431
 
                 * used later on by the encryption routine.
432
 
                 */
433
 
 
434
 
                kdata.data = session_key;
435
 
                kdata.length = 8;
436
 
 
437
 
                if ((code = krb5_c_random_seed(telnet_context, &kdata))) {
438
 
                    com_err("libtelnet", code,
439
 
                            "while seeding random number generator");
440
 
                    return;
441
 
                }
442
 
 
443
 
                memcpy((void *)datablock, (void *)data, sizeof(Block));
444
 
                /*
445
 
                 * Take the received encrypted challenge, and encrypt
446
 
                 * it again to get a unique session_key for the
447
 
                 * ENCRYPT option.
448
 
                 */
449
 
                krbkey.enctype = ENCTYPE_DES_CBC_RAW;
450
 
                krbkey.length = 8;
451
 
                krbkey.contents = session_key;
452
 
 
453
 
                kdata.data = datablock;
454
 
                kdata.length = 8;
455
 
 
456
 
                encdata.ciphertext.data = tmpkey;
457
 
                encdata.ciphertext.length = 8;
458
 
                encdata.enctype = ENCTYPE_UNKNOWN;
459
 
 
460
 
                if ((code = krb5_c_encrypt(telnet_context, &krbkey, 0, 0,
461
 
                                           &kdata, &encdata))) {
462
 
                    com_err("libtelnet", code, "while encrypting random key");
463
 
                    return;
464
 
                }
465
 
 
466
 
                skey.type = SK_DES;
467
 
                skey.length = 8;
468
 
                skey.data = tmpkey;
469
 
                encrypt_session_key(&skey, 1);
470
 
                /*
471
 
                 * Now decrypt the received encrypted challenge,
472
 
                 * increment by one, re-encrypt it and send it back.
473
 
                 */
474
 
                encdata.ciphertext.data = datablock;
475
 
                encdata.ciphertext.length = 8;
476
 
                encdata.enctype = ENCTYPE_UNKNOWN;
477
 
 
478
 
                kdata.data = challenge;
479
 
                kdata.length = 8;
480
 
 
481
 
                if ((code = krb5_c_decrypt(telnet_context, &krbkey, 0, 0, 
482
 
                                           &encdata, &kdata))) {
483
 
                    com_err("libtelnet", code, "while decrypting challenge");
484
 
                    return;
485
 
                }
486
 
 
487
 
                for (r = 7; r >= 0; r--) {
488
 
                        register int t;
489
 
                        t = (unsigned int)challenge[r] + 1;
490
 
                        challenge[r] = t;       /* ignore overflow */
491
 
                        if (t < 256)            /* if no overflow, all done */
492
 
                                break;
493
 
                }
494
 
 
495
 
                kdata.data = challenge;
496
 
                kdata.length = 8;
497
 
 
498
 
                encdata.ciphertext.data = challenge;
499
 
                encdata.ciphertext.length = 8;
500
 
                encdata.enctype = ENCTYPE_UNKNOWN;
501
 
 
502
 
                if ((code = krb5_c_encrypt(telnet_context, &krbkey, 0, 0,
503
 
                                           &kdata, &encdata))) {
504
 
                    com_err("libtelnet", code, "while decrypting challenge");
505
 
                    return;
506
 
                }
507
 
 
508
 
                Data(ap, KRB_RESPONSE, (void *)challenge, sizeof(challenge));
509
 
#endif  /* ENCRYPTION */
510
 
                break;
511
 
 
512
 
        default:
513
 
                if (auth_debug_mode)
514
 
                        printf("Unknown Kerberos option %d\r\n", data[-1]);
515
 
                Data(ap, KRB_REJECT, 0, 0);
516
 
                break;
517
 
        }
518
 
}
519
 
 
520
 
        void
521
 
kerberos4_reply(ap, data, cnt)
522
 
        Authenticator *ap;
523
 
        unsigned char *data;
524
 
        int cnt;
525
 
{
526
 
#ifdef  ENCRYPTION
527
 
        Session_Key skey;
528
 
        krb5_data kdata;
529
 
        krb5_enc_data encdata;
530
 
        krb5_error_code code;
531
 
 
532
 
#endif  /* ENCRYPTION */
533
 
 
534
 
        if (cnt-- < 1)
535
 
                return;
536
 
        switch (*data++) {
537
 
        case KRB_REJECT:
538
 
                if (cnt > 0) {
539
 
                        printf("[ Kerberos V4 refuses authentication because %.*s ]\r\n",
540
 
                                cnt, data);
541
 
                } else
542
 
                        printf("[ Kerberos V4 refuses authentication ]\r\n");
543
 
                auth_send_retry();
544
 
                return;
545
 
        case KRB_ACCEPT:
546
 
                printf("[ Kerberos V4 accepts you ]\r\n");
547
 
                if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) {
548
 
                        /*
549
 
                         * Send over the encrypted challenge.
550
 
                         */
551
 
#ifndef ENCRYPTION
552
 
                        Data(ap, KRB_CHALLENGE, (void *)0, 0);
553
 
#else   /* ENCRYPTION */
554
 
                        Data(ap, KRB_CHALLENGE, (void *)session_key,
555
 
                                                sizeof(session_key));
556
 
 
557
 
                        kdata.data = session_key;
558
 
                        kdata.length = 8;
559
 
 
560
 
                        encdata.ciphertext.data = session_key;
561
 
                        encdata.ciphertext.length = 8;
562
 
                        encdata.enctype = ENCTYPE_UNKNOWN;
563
 
 
564
 
                        if ((code = krb5_c_encrypt(telnet_context, &krbkey,
565
 
                                                   0, 0, &kdata, &encdata))) {
566
 
                                com_err("libtelnet", code,
567
 
                                        "while encrypting session_key");
568
 
                                return;
569
 
                        }
570
 
 
571
 
                        skey.type = SK_DES;
572
 
                        skey.length = 8;
573
 
                        skey.data = session_key;
574
 
                        encrypt_session_key(&skey, 0);
575
 
#endif  /* ENCRYPTION */
576
 
                        return;
577
 
                }
578
 
                auth_finished(ap, AUTH_USER);
579
 
                return;
580
 
        case KRB_RESPONSE:
581
 
#ifdef  ENCRYPTION
582
 
                /*
583
 
                 * Verify that the response to the challenge is correct.
584
 
                 */
585
 
                if ((cnt != sizeof(Block)) ||
586
 
                    (0 != memcmp((void *)data, (void *)challenge,
587
 
                                                sizeof(challenge))))
588
 
                {
589
 
#endif  /* ENCRYPTION */
590
 
                        printf("[ Kerberos V4 challenge failed!!! ]\r\n");
591
 
                        auth_send_retry();
592
 
                        return;
593
 
#ifdef  ENCRYPTION
594
 
                }
595
 
                printf("[ Kerberos V4 challenge successful ]\r\n");
596
 
                auth_finished(ap, AUTH_USER);
597
 
#endif  /* ENCRYPTION */
598
 
                break;
599
 
        default:
600
 
                if (auth_debug_mode)
601
 
                        printf("Unknown Kerberos option %d\r\n", data[-1]);
602
 
                return;
603
 
        }
604
 
}
605
 
 
606
 
        int
607
 
kerberos4_status(ap, kname, level)
608
 
        Authenticator *ap;
609
 
        char *kname;
610
 
        int level;
611
 
{
612
 
        if (level < AUTH_USER)
613
 
                return(level);
614
 
 
615
 
        /*
616
 
         * Always copy in UserNameRequested if the authentication
617
 
         * is valid, because the higher level routines need it.
618
 
         */
619
 
        if (UserNameRequested) {
620
 
                /* the name buffer comes from telnetd/telnetd{-ktd}.c */
621
 
                strncpy(kname, UserNameRequested, 255);
622
 
                kname[255] = '\0';
623
 
        }
624
 
 
625
 
        if (UserNameRequested && !kuserok(&adat, UserNameRequested)) {
626
 
                return(AUTH_VALID);
627
 
        } else
628
 
                return(AUTH_USER);
629
 
}
630
 
 
631
 
#define BUMP(buf, len)          while (*(buf)) {++(buf), --(len);}
632
 
#define ADDC(buf, len, c)       if ((len) > 0) {*(buf)++ = (c); --(len);}
633
 
 
634
 
        void
635
 
kerberos4_printsub(data, cnt, buf, buflen)
636
 
        unsigned char *data, *buf;
637
 
        int cnt;
638
 
        unsigned int buflen;
639
 
{
640
 
        char lbuf[32];
641
 
        register int i;
642
 
 
643
 
        buf[buflen-1] = '\0';           /* make sure its NULL terminated */
644
 
        buflen -= 1;
645
 
 
646
 
        switch(data[3]) {
647
 
        case KRB_REJECT:                /* Rejected (reason might follow) */
648
 
                strncpy((char *)buf, " REJECT ", buflen);
649
 
                goto common;
650
 
 
651
 
        case KRB_ACCEPT:                /* Accepted (name might follow) */
652
 
                strncpy((char *)buf, " ACCEPT ", buflen);
653
 
        common:
654
 
                BUMP(buf, buflen);
655
 
                if (cnt <= 4)
656
 
                        break;
657
 
                ADDC(buf, buflen, '"');
658
 
                for (i = 4; i < cnt; i++)
659
 
                        ADDC(buf, buflen, data[i]);
660
 
                ADDC(buf, buflen, '"');
661
 
                ADDC(buf, buflen, '\0');
662
 
                break;
663
 
 
664
 
        case KRB_AUTH:                  /* Authentication data follows */
665
 
                strncpy((char *)buf, " AUTH", buflen);
666
 
                goto common2;
667
 
 
668
 
        case KRB_CHALLENGE:
669
 
                strncpy((char *)buf, " CHALLENGE", buflen);
670
 
                goto common2;
671
 
 
672
 
        case KRB_RESPONSE:
673
 
                strncpy((char *)buf, " RESPONSE", buflen);
674
 
                goto common2;
675
 
 
676
 
        default:
677
 
                sprintf(lbuf, " %d (unknown)", data[3]);
678
 
                strncpy((char *)buf, lbuf, buflen);
679
 
        common2:
680
 
                BUMP(buf, buflen);
681
 
                for (i = 4; i < cnt; i++) {
682
 
                        sprintf(lbuf, " %d", data[i]);
683
 
                        strncpy((char *)buf, lbuf, buflen);
684
 
                        BUMP(buf, buflen);
685
 
                }
686
 
                break;
687
 
        }
688
 
}
689
 
 
690
 
        int
691
 
kerberos4_cksum(d, n)
692
 
        unsigned char *d;
693
 
        int n;
694
 
{
695
 
        int ck = 0;
696
 
 
697
 
        /*
698
 
         * A comment is probably needed here for those not
699
 
         * well versed in the "C" language.  Yes, this is
700
 
         * supposed to be a "switch" with the body of the
701
 
         * "switch" being a "while" statement.  The whole
702
 
         * purpose of the switch is to allow us to jump into
703
 
         * the middle of the while() loop, and then not have
704
 
         * to do any more switch()s.
705
 
         *
706
 
         * Some compilers will spit out a warning message
707
 
         * about the loop not being entered at the top.
708
 
         */
709
 
        switch (n&03)
710
 
        while (n > 0) {
711
 
        case 0:
712
 
                ck ^= (int)*d++ << 24;
713
 
                --n;
714
 
        case 3:
715
 
                ck ^= (int)*d++ << 16;
716
 
                --n;
717
 
        case 2:
718
 
                ck ^= (int)*d++ << 8;
719
 
                --n;
720
 
        case 1:
721
 
                ck ^= (int)*d++;
722
 
                --n;
723
 
        }
724
 
        return(ck);
725
 
}
726
 
#else
727
 
#include <krb5.h>
728
 
#include <errno.h>
729
 
 
730
 
#endif
731
 
 
732
 
#ifdef notdef
733
 
 
734
 
prkey(msg, key)
735
 
        char *msg;
736
 
        unsigned char *key;
737
 
{
738
 
        register int i;
739
 
        printf("%s:", msg);
740
 
        for (i = 0; i < 8; i++)
741
 
                printf(" %3d", key[i]);
742
 
        printf("\r\n");
743
 
}
744
 
#endif