xref: /freebsd/crypto/heimdal/appl/telnet/libtelnet/kerberos5.c (revision 0cadf2f4d7b9f212800448d915003abd40c2e40d)
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 /*
35  * Copyright (C) 1990 by the Massachusetts Institute of Technology
36  *
37  * Export of this software from the United States of America may
38  * require a specific license from the United States Government.
39  * It is the responsibility of any person or organization contemplating
40  * export to obtain such a license before exporting.
41  *
42  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
43  * distribute this software and its documentation for any purpose and
44  * without fee is hereby granted, provided that the above copyright
45  * notice appear in all copies and that both that copyright notice and
46  * this permission notice appear in supporting documentation, and that
47  * the name of M.I.T. not be used in advertising or publicity pertaining
48  * to distribution of the software without specific, written prior
49  * permission.  M.I.T. makes no representations about the suitability of
50  * this software for any purpose.  It is provided "as is" without express
51  * or implied warranty.
52  */
53 
54 #include <config.h>
55 
56 RCSID("$Id: kerberos5.c,v 1.51 2002/09/02 15:33:20 joda Exp $");
57 
58 #ifdef	KRB5
59 
60 #include <arpa/telnet.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <unistd.h>
65 #include <netdb.h>
66 #include <ctype.h>
67 #include <pwd.h>
68 #define Authenticator k5_Authenticator
69 #include <krb5.h>
70 #undef Authenticator
71 #include <roken.h>
72 #ifdef SOCKS
73 #include <socks.h>
74 #endif
75 
76 
77 #include "encrypt.h"
78 #include "auth.h"
79 #include "misc.h"
80 
81 #if defined(DCE)
82 int dfsk5ok = 0;
83 int dfspag = 0;
84 int dfsfwd = 0;
85 #endif
86 
87 int forward_flags = 0;  /* Flags get set in telnet/main.c on -f and -F */
88 
89 int forward(int);
90 int forwardable(int);
91 
92 /* These values need to be the same as those defined in telnet/main.c. */
93 /* Either define them in both places, or put in some common header file. */
94 #define OPTS_FORWARD_CREDS	0x00000002
95 #define OPTS_FORWARDABLE_CREDS	0x00000001
96 
97 
98 void kerberos5_forward (Authenticator *);
99 
100 static unsigned char str_data[1024] = { IAC, SB, TELOPT_AUTHENTICATION, 0,
101 			  		AUTHTYPE_KERBEROS_V5, };
102 
103 #define	KRB_AUTH		0	/* Authentication data follows */
104 #define	KRB_REJECT		1	/* Rejected (reason might follow) */
105 #define	KRB_ACCEPT		2	/* Accepted */
106 #define	KRB_RESPONSE		3	/* Response for mutual auth. */
107 
108 #define KRB_FORWARD     	4       /* Forwarded credentials follow */
109 #define KRB_FORWARD_ACCEPT     	5       /* Forwarded credentials accepted */
110 #define KRB_FORWARD_REJECT     	6       /* Forwarded credentials rejected */
111 
112 static	krb5_data auth;
113 static  krb5_ticket *ticket;
114 
115 static krb5_context context;
116 static krb5_auth_context auth_context;
117 
118 static int
119 Data(Authenticator *ap, int type, void *d, int c)
120 {
121     unsigned char *p = str_data + 4;
122     unsigned char *cd = (unsigned char *)d;
123 
124     if (c == -1)
125 	c = strlen((char*)cd);
126 
127     if (auth_debug_mode) {
128 	printf("%s:%d: [%d] (%d)",
129 	       str_data[3] == TELQUAL_IS ? ">>>IS" : ">>>REPLY",
130 	       str_data[3],
131 	       type, c);
132 	printd(d, c);
133 	printf("\r\n");
134     }
135     *p++ = ap->type;
136     *p++ = ap->way;
137     *p++ = type;
138     while (c-- > 0) {
139 	if ((*p++ = *cd++) == IAC)
140 	    *p++ = IAC;
141     }
142     *p++ = IAC;
143     *p++ = SE;
144     if (str_data[3] == TELQUAL_IS)
145 	printsub('>', &str_data[2], p - &str_data[2]);
146     return(telnet_net_write(str_data, p - str_data));
147 }
148 
149 int
150 kerberos5_init(Authenticator *ap, int server)
151 {
152     krb5_error_code ret;
153 
154     ret = krb5_init_context(&context);
155     if (ret)
156 	return 0;
157     if (server) {
158 	krb5_keytab kt;
159 	krb5_kt_cursor cursor;
160 
161 	ret = krb5_kt_default(context, &kt);
162 	if (ret)
163 	    return 0;
164 
165 	ret = krb5_kt_start_seq_get (context, kt, &cursor);
166 	if (ret) {
167 	    krb5_kt_close (context, kt);
168 	    return 0;
169 	}
170 	krb5_kt_end_seq_get (context, kt, &cursor);
171 	krb5_kt_close (context, kt);
172 
173 	str_data[3] = TELQUAL_REPLY;
174     } else
175 	str_data[3] = TELQUAL_IS;
176     return(1);
177 }
178 
179 extern int net;
180 static int
181 kerberos5_send(char *name, Authenticator *ap)
182 {
183     krb5_error_code ret;
184     krb5_ccache ccache;
185     int ap_opts;
186     krb5_data cksum_data;
187     char foo[2];
188 
189     if (!UserNameRequested) {
190 	if (auth_debug_mode) {
191 	    printf("Kerberos V5: no user name supplied\r\n");
192 	}
193 	return(0);
194     }
195 
196     ret = krb5_cc_default(context, &ccache);
197     if (ret) {
198 	if (auth_debug_mode) {
199 	    printf("Kerberos V5: could not get default ccache: %s\r\n",
200 		   krb5_get_err_text (context, ret));
201 	}
202 	return 0;
203     }
204 
205     if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL)
206 	ap_opts = AP_OPTS_MUTUAL_REQUIRED;
207     else
208 	ap_opts = 0;
209 
210     ap_opts |= AP_OPTS_USE_SUBKEY;
211 
212     ret = krb5_auth_con_init (context, &auth_context);
213     if (ret) {
214 	if (auth_debug_mode) {
215 	    printf("Kerberos V5: krb5_auth_con_init failed (%s)\r\n",
216 		   krb5_get_err_text(context, ret));
217 	}
218 	return(0);
219     }
220 
221     ret = krb5_auth_con_setaddrs_from_fd (context,
222 					  auth_context,
223 					  &net);
224     if (ret) {
225 	if (auth_debug_mode) {
226 	    printf ("Kerberos V5:"
227 		    " krb5_auth_con_setaddrs_from_fd failed (%s)\r\n",
228 		    krb5_get_err_text(context, ret));
229 	}
230 	return(0);
231     }
232 
233     krb5_auth_con_setkeytype (context, auth_context, KEYTYPE_DES);
234 
235     foo[0] = ap->type;
236     foo[1] = ap->way;
237 
238     cksum_data.length = sizeof(foo);
239     cksum_data.data   = foo;
240 
241 
242     {
243 	krb5_principal service;
244 	char sname[128];
245 
246 
247 	ret = krb5_sname_to_principal (context,
248 				       RemoteHostName,
249 				       NULL,
250 				       KRB5_NT_SRV_HST,
251 				       &service);
252 	if(ret) {
253 	    if (auth_debug_mode) {
254 		printf ("Kerberos V5:"
255 			" krb5_sname_to_principal(%s) failed (%s)\r\n",
256 			RemoteHostName, krb5_get_err_text(context, ret));
257 	    }
258 	    return 0;
259 	}
260 	ret = krb5_unparse_name_fixed(context, service, sname, sizeof(sname));
261 	if(ret) {
262 	    if (auth_debug_mode) {
263 		printf ("Kerberos V5:"
264 			" krb5_unparse_name_fixed failed (%s)\r\n",
265 			krb5_get_err_text(context, ret));
266 	    }
267 	    return 0;
268 	}
269 	printf("[ Trying %s (%s)... ]\r\n", name, sname);
270 	ret = krb5_mk_req_exact(context, &auth_context, ap_opts,
271 				service,
272 				&cksum_data, ccache, &auth);
273 	krb5_free_principal (context, service);
274 
275     }
276     if (ret) {
277 	if (1 || auth_debug_mode) {
278 	    printf("Kerberos V5: mk_req failed (%s)\r\n",
279 		   krb5_get_err_text(context, ret));
280 	}
281 	return(0);
282     }
283 
284     if (!auth_sendname((unsigned char *)UserNameRequested,
285 		       strlen(UserNameRequested))) {
286 	if (auth_debug_mode)
287 	    printf("Not enough room for user name\r\n");
288 	return(0);
289     }
290     if (!Data(ap, KRB_AUTH, auth.data, auth.length)) {
291 	if (auth_debug_mode)
292 	    printf("Not enough room for authentication data\r\n");
293 	return(0);
294     }
295     if (auth_debug_mode) {
296 	printf("Sent Kerberos V5 credentials to server\r\n");
297     }
298     return(1);
299 }
300 
301 int
302 kerberos5_send_mutual(Authenticator *ap)
303 {
304     return kerberos5_send("mutual KERBEROS5", ap);
305 }
306 
307 int
308 kerberos5_send_oneway(Authenticator *ap)
309 {
310     return kerberos5_send("KERBEROS5", ap);
311 }
312 
313 void
314 kerberos5_is(Authenticator *ap, unsigned char *data, int cnt)
315 {
316     krb5_error_code ret;
317     krb5_data outbuf;
318     krb5_keyblock *key_block;
319     char *name;
320     krb5_principal server;
321     int zero = 0;
322 
323     if (cnt-- < 1)
324 	return;
325     switch (*data++) {
326     case KRB_AUTH:
327 	auth.data = (char *)data;
328 	auth.length = cnt;
329 
330 	auth_context = NULL;
331 
332 	ret = krb5_auth_con_init (context, &auth_context);
333 	if (ret) {
334 	    Data(ap, KRB_REJECT, "krb5_auth_con_init failed", -1);
335 	    auth_finished(ap, AUTH_REJECT);
336 	    if (auth_debug_mode)
337 		printf("Kerberos V5: krb5_auth_con_init failed (%s)\r\n",
338 		       krb5_get_err_text(context, ret));
339 	    return;
340 	}
341 
342 	ret = krb5_auth_con_setaddrs_from_fd (context,
343 					      auth_context,
344 					      &zero);
345 	if (ret) {
346 	    Data(ap, KRB_REJECT, "krb5_auth_con_setaddrs_from_fd failed", -1);
347 	    auth_finished(ap, AUTH_REJECT);
348 	    if (auth_debug_mode)
349 		printf("Kerberos V5: "
350 		       "krb5_auth_con_setaddrs_from_fd failed (%s)\r\n",
351 		       krb5_get_err_text(context, ret));
352 	    return;
353 	}
354 
355 	ret = krb5_sock_to_principal (context,
356 				      0,
357 				      "host",
358 				      KRB5_NT_SRV_HST,
359 				      &server);
360 	if (ret) {
361 	    Data(ap, KRB_REJECT, "krb5_sock_to_principal failed", -1);
362 	    auth_finished(ap, AUTH_REJECT);
363 	    if (auth_debug_mode)
364 		printf("Kerberos V5: "
365 		       "krb5_sock_to_principal failed (%s)\r\n",
366 		       krb5_get_err_text(context, ret));
367 	    return;
368 	}
369 
370 	ret = krb5_rd_req(context,
371 			  &auth_context,
372 			  &auth,
373 			  server,
374 			  NULL,
375 			  NULL,
376 			  &ticket);
377 
378 	krb5_free_principal (context, server);
379 	if (ret) {
380 	    char *errbuf;
381 
382 	    asprintf(&errbuf,
383 		     "Read req failed: %s",
384 		     krb5_get_err_text(context, ret));
385 	    Data(ap, KRB_REJECT, errbuf, -1);
386 	    if (auth_debug_mode)
387 		printf("%s\r\n", errbuf);
388 	    free (errbuf);
389 	    return;
390 	}
391 
392 	{
393 	    char foo[2];
394 
395 	    foo[0] = ap->type;
396 	    foo[1] = ap->way;
397 
398 	    ret = krb5_verify_authenticator_checksum(context,
399 						     auth_context,
400 						     foo,
401 						     sizeof(foo));
402 
403 	    if (ret) {
404 		char *errbuf;
405 		asprintf(&errbuf, "Bad checksum: %s",
406 			 krb5_get_err_text(context, ret));
407 		Data(ap, KRB_REJECT, errbuf, -1);
408 		if (auth_debug_mode)
409 		    printf ("%s\r\n", errbuf);
410 		free(errbuf);
411 		return;
412 	    }
413 	}
414 	ret = krb5_auth_con_getremotesubkey (context,
415 					     auth_context,
416 					     &key_block);
417 
418 	if (ret) {
419 	    Data(ap, KRB_REJECT, "krb5_auth_con_getremotesubkey failed", -1);
420 	    auth_finished(ap, AUTH_REJECT);
421 	    if (auth_debug_mode)
422 		printf("Kerberos V5: "
423 		       "krb5_auth_con_getremotesubkey failed (%s)\r\n",
424 		       krb5_get_err_text(context, ret));
425 	    return;
426 	}
427 
428 	if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) {
429 	    ret = krb5_mk_rep(context, auth_context, &outbuf);
430 	    if (ret) {
431 		Data(ap, KRB_REJECT,
432 		     "krb5_mk_rep failed", -1);
433 		auth_finished(ap, AUTH_REJECT);
434 		if (auth_debug_mode)
435 		    printf("Kerberos V5: "
436 			   "krb5_mk_rep failed (%s)\r\n",
437 			   krb5_get_err_text(context, ret));
438 		return;
439 	    }
440 	    Data(ap, KRB_RESPONSE, outbuf.data, outbuf.length);
441 	}
442 	if (krb5_unparse_name(context, ticket->client, &name))
443 	    name = 0;
444 
445 	if(UserNameRequested && krb5_kuserok(context,
446 					     ticket->client,
447 					     UserNameRequested)) {
448 	    Data(ap, KRB_ACCEPT, name, name ? -1 : 0);
449 	    if (auth_debug_mode) {
450 		printf("Kerberos5 identifies him as ``%s''\r\n",
451 		       name ? name : "");
452 	    }
453 
454 	    if(key_block->keytype == ETYPE_DES_CBC_MD5 ||
455 	       key_block->keytype == ETYPE_DES_CBC_MD4 ||
456 	       key_block->keytype == ETYPE_DES_CBC_CRC) {
457 		Session_Key skey;
458 
459 		skey.type = SK_DES;
460 		skey.length = 8;
461 		skey.data = key_block->keyvalue.data;
462 		encrypt_session_key(&skey, 0);
463 	    }
464 
465 	} else {
466 	    char *msg;
467 
468 	    asprintf (&msg, "user `%s' is not authorized to "
469 		      "login as `%s'",
470 		      name ? name : "<unknown>",
471 		      UserNameRequested ? UserNameRequested : "<nobody>");
472 	    if (msg == NULL)
473 		Data(ap, KRB_REJECT, NULL, 0);
474 	    else {
475 		Data(ap, KRB_REJECT, (void *)msg, -1);
476 		free(msg);
477 	    }
478 	    auth_finished (ap, AUTH_REJECT);
479 	    krb5_free_keyblock_contents(context, key_block);
480 	    break;
481 	}
482 	auth_finished(ap, AUTH_USER);
483 	krb5_free_keyblock_contents(context, key_block);
484 
485 	break;
486     case KRB_FORWARD: {
487 	struct passwd *pwd;
488 	char ccname[1024];	/* XXX */
489 	krb5_data inbuf;
490 	krb5_ccache ccache;
491 	inbuf.data = (char *)data;
492 	inbuf.length = cnt;
493 
494 	pwd = getpwnam (UserNameRequested);
495 	if (pwd == NULL)
496 	    break;
497 
498 	snprintf (ccname, sizeof(ccname),
499 		  "FILE:/tmp/krb5cc_%u", pwd->pw_uid);
500 
501 	ret = krb5_cc_resolve (context, ccname, &ccache);
502 	if (ret) {
503 	    if (auth_debug_mode)
504 		printf ("Kerberos V5: could not get ccache: %s\r\n",
505 			krb5_get_err_text(context, ret));
506 	    break;
507 	}
508 
509 	ret = krb5_cc_initialize (context,
510 				  ccache,
511 				  ticket->client);
512 	if (ret) {
513 	    if (auth_debug_mode)
514 		printf ("Kerberos V5: could not init ccache: %s\r\n",
515 			krb5_get_err_text(context, ret));
516 	    break;
517 	}
518 
519 #if defined(DCE)
520 	esetenv("KRB5CCNAME", ccname, 1);
521 #endif
522 	ret = krb5_rd_cred2 (context,
523 			     auth_context,
524 			     ccache,
525 			     &inbuf);
526 	if(ret) {
527 	    char *errbuf;
528 
529 	    asprintf (&errbuf,
530 		      "Read forwarded creds failed: %s",
531 		      krb5_get_err_text (context, ret));
532 	    if(errbuf == NULL)
533 		Data(ap, KRB_FORWARD_REJECT, NULL, 0);
534 	    else
535 		Data(ap, KRB_FORWARD_REJECT, errbuf, -1);
536 	    if (auth_debug_mode)
537 		printf("Could not read forwarded credentials: %s\r\n",
538 		       errbuf);
539 	    free (errbuf);
540 	} else {
541 	    Data(ap, KRB_FORWARD_ACCEPT, 0, 0);
542 #if defined(DCE)
543 	    dfsfwd = 1;
544 #endif
545 	}
546 	chown (ccname + 5, pwd->pw_uid, -1);
547 	if (auth_debug_mode)
548 	    printf("Forwarded credentials obtained\r\n");
549 	break;
550     }
551     default:
552 	if (auth_debug_mode)
553 	    printf("Unknown Kerberos option %d\r\n", data[-1]);
554 	Data(ap, KRB_REJECT, 0, 0);
555 	break;
556     }
557 }
558 
559 void
560 kerberos5_reply(Authenticator *ap, unsigned char *data, int cnt)
561 {
562     static int mutual_complete = 0;
563 
564     if (cnt-- < 1)
565 	return;
566     switch (*data++) {
567     case KRB_REJECT:
568 	if (cnt > 0) {
569 	    printf("[ Kerberos V5 refuses authentication because %.*s ]\r\n",
570 		   cnt, data);
571 	} else
572 	    printf("[ Kerberos V5 refuses authentication ]\r\n");
573 	auth_send_retry();
574 	return;
575     case KRB_ACCEPT: {
576 	krb5_error_code ret;
577 	Session_Key skey;
578 	krb5_keyblock *keyblock;
579 
580 	if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL &&
581 	    !mutual_complete) {
582 	    printf("[ Kerberos V5 accepted you, but didn't provide mutual authentication! ]\r\n");
583 	    auth_send_retry();
584 	    return;
585 	}
586 	if (cnt)
587 	    printf("[ Kerberos V5 accepts you as ``%.*s'' ]\r\n", cnt, data);
588 	else
589 	    printf("[ Kerberos V5 accepts you ]\r\n");
590 
591 	ret = krb5_auth_con_getlocalsubkey (context,
592 					    auth_context,
593 					    &keyblock);
594 	if (ret)
595 	    ret = krb5_auth_con_getkey (context,
596 					auth_context,
597 					&keyblock);
598 	if(ret) {
599 	    printf("[ krb5_auth_con_getkey: %s ]\r\n",
600 		   krb5_get_err_text(context, ret));
601 	    auth_send_retry();
602 	    return;
603 	}
604 
605 	skey.type = SK_DES;
606 	skey.length = 8;
607 	skey.data = keyblock->keyvalue.data;
608 	encrypt_session_key(&skey, 0);
609 	krb5_free_keyblock_contents (context, keyblock);
610 	auth_finished(ap, AUTH_USER);
611 	if (forward_flags & OPTS_FORWARD_CREDS)
612 	    kerberos5_forward(ap);
613 	break;
614     }
615     case KRB_RESPONSE:
616 	if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) {
617 	    /* the rest of the reply should contain a krb_ap_rep */
618 	  krb5_ap_rep_enc_part *reply;
619 	  krb5_data inbuf;
620 	  krb5_error_code ret;
621 
622 	  inbuf.length = cnt;
623 	  inbuf.data = (char *)data;
624 
625 	  ret = krb5_rd_rep(context, auth_context, &inbuf, &reply);
626 	  if (ret) {
627 	      printf("[ Mutual authentication failed: %s ]\r\n",
628 		     krb5_get_err_text (context, ret));
629 	      auth_send_retry();
630 	      return;
631 	  }
632 	  krb5_free_ap_rep_enc_part(context, reply);
633 	  mutual_complete = 1;
634 	}
635 	return;
636     case KRB_FORWARD_ACCEPT:
637 	printf("[ Kerberos V5 accepted forwarded credentials ]\r\n");
638 	return;
639     case KRB_FORWARD_REJECT:
640 	printf("[ Kerberos V5 refuses forwarded credentials because %.*s ]\r\n",
641 	       cnt, data);
642 	return;
643     default:
644 	if (auth_debug_mode)
645 	    printf("Unknown Kerberos option %d\r\n", data[-1]);
646 	return;
647     }
648 }
649 
650 int
651 kerberos5_status(Authenticator *ap, char *name, size_t name_sz, int level)
652 {
653     if (level < AUTH_USER)
654 	return(level);
655 
656     if (UserNameRequested &&
657 	krb5_kuserok(context,
658 		     ticket->client,
659 		     UserNameRequested))
660 	{
661 	    strlcpy(name, UserNameRequested, name_sz);
662 #if defined(DCE)
663 	    dfsk5ok = 1;
664 #endif
665 	    return(AUTH_VALID);
666 	} else
667 	    return(AUTH_USER);
668 }
669 
670 #define	BUMP(buf, len)		while (*(buf)) {++(buf), --(len);}
671 #define	ADDC(buf, len, c)	if ((len) > 0) {*(buf)++ = (c); --(len);}
672 
673 void
674 kerberos5_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
675 {
676     int i;
677 
678     buf[buflen-1] = '\0';		/* make sure its NULL terminated */
679     buflen -= 1;
680 
681     switch(data[3]) {
682     case KRB_REJECT:		/* Rejected (reason might follow) */
683 	strlcpy((char *)buf, " REJECT ", buflen);
684 	goto common;
685 
686     case KRB_ACCEPT:		/* Accepted (name might follow) */
687 	strlcpy((char *)buf, " ACCEPT ", buflen);
688     common:
689 	BUMP(buf, buflen);
690 	if (cnt <= 4)
691 	    break;
692 	ADDC(buf, buflen, '"');
693 	for (i = 4; i < cnt; i++)
694 	    ADDC(buf, buflen, data[i]);
695 	ADDC(buf, buflen, '"');
696 	ADDC(buf, buflen, '\0');
697 	break;
698 
699 
700     case KRB_AUTH:			/* Authentication data follows */
701 	strlcpy((char *)buf, " AUTH", buflen);
702 	goto common2;
703 
704     case KRB_RESPONSE:
705 	strlcpy((char *)buf, " RESPONSE", buflen);
706 	goto common2;
707 
708     case KRB_FORWARD:		/* Forwarded credentials follow */
709 	strlcpy((char *)buf, " FORWARD", buflen);
710 	goto common2;
711 
712     case KRB_FORWARD_ACCEPT:	/* Forwarded credentials accepted */
713 	strlcpy((char *)buf, " FORWARD_ACCEPT", buflen);
714 	goto common2;
715 
716     case KRB_FORWARD_REJECT:	/* Forwarded credentials rejected */
717 	/* (reason might follow) */
718 	strlcpy((char *)buf, " FORWARD_REJECT", buflen);
719 	goto common2;
720 
721     default:
722 	snprintf((char*)buf, buflen, " %d (unknown)", data[3]);
723     common2:
724 	BUMP(buf, buflen);
725 	for (i = 4; i < cnt; i++) {
726 	    snprintf((char*)buf, buflen, " %d", data[i]);
727 	    BUMP(buf, buflen);
728 	}
729 	break;
730     }
731 }
732 
733 void
734 kerberos5_forward(Authenticator *ap)
735 {
736     krb5_error_code ret;
737     krb5_ccache     ccache;
738     krb5_creds      creds;
739     krb5_kdc_flags  flags;
740     krb5_data       out_data;
741     krb5_principal  principal;
742 
743     ret = krb5_cc_default (context, &ccache);
744     if (ret) {
745 	if (auth_debug_mode)
746 	    printf ("KerberosV5: could not get default ccache: %s\r\n",
747 		    krb5_get_err_text (context, ret));
748 	return;
749     }
750 
751     ret = krb5_cc_get_principal (context, ccache, &principal);
752     if (ret) {
753 	if (auth_debug_mode)
754 	    printf ("KerberosV5: could not get principal: %s\r\n",
755 		    krb5_get_err_text (context, ret));
756 	return;
757     }
758 
759     memset (&creds, 0, sizeof(creds));
760 
761     creds.client = principal;
762 
763     ret = krb5_build_principal (context,
764 				&creds.server,
765 				strlen(principal->realm),
766 				principal->realm,
767 				"krbtgt",
768 				principal->realm,
769 				NULL);
770 
771     if (ret) {
772 	if (auth_debug_mode)
773 	    printf ("KerberosV5: could not get principal: %s\r\n",
774 		    krb5_get_err_text (context, ret));
775 	return;
776     }
777 
778     creds.times.endtime = 0;
779 
780     flags.i = 0;
781     flags.b.forwarded = 1;
782     if (forward_flags & OPTS_FORWARDABLE_CREDS)
783 	flags.b.forwardable = 1;
784 
785     ret = krb5_get_forwarded_creds (context,
786 				    auth_context,
787 				    ccache,
788 				    flags.i,
789 				    RemoteHostName,
790 				    &creds,
791 				    &out_data);
792     if (ret) {
793 	if (auth_debug_mode)
794 	    printf ("Kerberos V5: error getting forwarded creds: %s\r\n",
795 		    krb5_get_err_text (context, ret));
796 	return;
797     }
798 
799     if(!Data(ap, KRB_FORWARD, out_data.data, out_data.length)) {
800 	if (auth_debug_mode)
801 	    printf("Not enough room for authentication data\r\n");
802     } else {
803 	if (auth_debug_mode)
804 	    printf("Forwarded local Kerberos V5 credentials to server\r\n");
805     }
806 }
807 
808 #if defined(DCE)
809 /* if this was a K5 authentication try and join a PAG for the user. */
810 void
811 kerberos5_dfspag(void)
812 {
813     if (dfsk5ok) {
814 	dfspag = krb5_dfs_pag(context, dfsfwd, ticket->client,
815 			      UserNameRequested);
816     }
817 }
818 #endif
819 
820 int
821 kerberos5_set_forward(int on)
822 {
823     if(on == 0)
824 	forward_flags &= ~OPTS_FORWARD_CREDS;
825     if(on == 1)
826 	forward_flags |= OPTS_FORWARD_CREDS;
827     if(on == -1)
828 	forward_flags ^= OPTS_FORWARD_CREDS;
829     return 0;
830 }
831 
832 int
833 kerberos5_set_forwardable(int on)
834 {
835     if(on == 0)
836 	forward_flags &= ~OPTS_FORWARDABLE_CREDS;
837     if(on == 1)
838 	forward_flags |= OPTS_FORWARDABLE_CREDS;
839     if(on == -1)
840 	forward_flags ^= OPTS_FORWARDABLE_CREDS;
841     return 0;
842 }
843 
844 #endif /* KRB5 */
845