| 1 |
/* |
| 2 |
Copyright (C) 1993-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Inetutils. |
| 5 |
|
| 6 |
GNU Inetutils is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Inetutils is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see `http://www.gnu.org/licenses/'. */ |
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
|
| 21 |
#ifdef KRB5 |
| 22 |
# include <stdlib.h> |
| 23 |
# include <stdio.h> |
| 24 |
# include <arpa/telnet.h> |
| 25 |
# ifdef HAVE_KRB5_H |
| 26 |
# include <krb5.h> |
| 27 |
# endif |
| 28 |
# include <assert.h> |
| 29 |
|
| 30 |
# ifdef HAVE_COM_ERR_H |
| 31 |
# include <com_err.h> |
| 32 |
# endif |
| 33 |
# include <netdb.h> |
| 34 |
# include <ctype.h> |
| 35 |
# include <syslog.h> |
| 36 |
# include <string.h> |
| 37 |
# include <attribute.h> |
| 38 |
|
| 39 |
# include "auth.h" |
| 40 |
# include "misc.h" |
| 41 |
|
| 42 |
# ifndef KRB5_ENV_CCNAME |
| 43 |
# define KRB5_ENV_CCNAME "KRB5CCNAME" |
| 44 |
# endif |
| 45 |
|
| 46 |
# ifdef ENCRYPTION |
| 47 |
# include "encrypt.h" |
| 48 |
# endif |
| 49 |
|
| 50 |
# ifdef FORWARD |
| 51 |
/* FIXME: This is set directly from telnet/main.c */ |
| 52 |
int forward_flags = 0; |
| 53 |
extern int net; |
| 54 |
/*FIXME*/ void kerberos5_forward (); |
| 55 |
# endif/* FORWARD */ |
| 56 |
|
| 57 |
static unsigned char str_data[2048] = { IAC, SB, TELOPT_AUTHENTICATION, 0, |
| 58 |
AUTHTYPE_KERBEROS_V5, |
| 59 |
}; |
| 60 |
|
| 61 |
# define KRB_AUTH 0 /* Authentication data follows */ |
| 62 |
# define KRB_REJECT 1 /* Rejected (reason might follow) */ |
| 63 |
# define KRB_ACCEPT 2 /* Accepted */ |
| 64 |
# define KRB_RESPONSE 3 /* Response for mutual auth. */ |
| 65 |
# define KRB_FORWARD 4 /* Forwarded credentials follow */ |
| 66 |
# define KRB_FORWARD_ACCEPT 5 /* Forwarded credentials accepted */ |
| 67 |
# define KRB_FORWARD_REJECT 6 /* Forwarded credentials rejected */ |
| 68 |
|
| 69 |
krb5_auth_context auth_context = 0; |
| 70 |
krb5_context telnet_context = 0; |
| 71 |
|
| 72 |
static krb5_data auth; /* session key for telnet */ |
| 73 |
static krb5_ticket *ticket = NULL; /* telnet matches the AP_REQ and |
| 74 |
AP_REP with this */ |
| 75 |
|
| 76 |
krb5_keyblock *session_key = 0; |
| 77 |
char *telnet_srvtab = NULL; |
| 78 |
char *dest_realm = NULL; |
| 79 |
|
| 80 |
# define DEBUG(c) if (auth_debug_mode) printf c |
| 81 |
|
| 82 |
static int |
| 83 |
Data (TN_Authenticator *ap, int type, krb5_pointer d, int c) |
| 84 |
{ |
| 85 |
unsigned char *p = str_data + 4; |
| 86 |
unsigned char *cd = (unsigned char *) d; |
| 87 |
|
| 88 |
if (c == -1) |
| 89 |
c = strlen ((char *) cd); |
| 90 |
|
| 91 |
if (auth_debug_mode) |
| 92 |
{ |
| 93 |
printf ("%s:%d: [%d] (%d)", |
| 94 |
str_data[3] == TELQUAL_IS ? ">>>IS" : ">>>REPLY", |
| 95 |
str_data[3], type, c); |
| 96 |
printd (d, c); |
| 97 |
printf ("\r\n"); |
| 98 |
} |
| 99 |
|
| 100 |
*p++ = ap->type; |
| 101 |
*p++ = ap->way; |
| 102 |
*p++ = type; |
| 103 |
|
| 104 |
while (c-- > 0) |
| 105 |
{ |
| 106 |
if ((*p++ = *cd++) == IAC) |
| 107 |
*p++ = IAC; |
| 108 |
} |
| 109 |
*p++ = IAC; |
| 110 |
*p++ = SE; |
| 111 |
return (net_write (str_data, p - str_data)); |
| 112 |
} |
| 113 |
|
| 114 |
/* FIXME: Reverse return code! */ |
| 115 |
int |
| 116 |
kerberos5_init (TN_Authenticator *ap MAYBE_UNUSED, int server) |
| 117 |
{ |
| 118 |
str_data[3] = server ? TELQUAL_REPLY : TELQUAL_IS; |
| 119 |
if (telnet_context == 0 && krb5_init_context (&telnet_context)) |
| 120 |
return 0; |
| 121 |
return 1; |
| 122 |
} |
| 123 |
|
| 124 |
void |
| 125 |
kerberos5_cleanup (void) |
| 126 |
{ |
| 127 |
krb5_ccache ccache; |
| 128 |
char *ccname; |
| 129 |
|
| 130 |
if (telnet_context == 0) |
| 131 |
return; |
| 132 |
|
| 133 |
ccname = getenv (KRB5_ENV_CCNAME); |
| 134 |
if (ccname) |
| 135 |
{ |
| 136 |
if (!krb5_cc_resolve (telnet_context, ccname, &ccache)) |
| 137 |
krb5_cc_destroy (telnet_context, ccache); |
| 138 |
} |
| 139 |
|
| 140 |
krb5_free_context (telnet_context); |
| 141 |
telnet_context = 0; |
| 142 |
} |
| 143 |
|
| 144 |
# ifdef ENCRYPTION |
| 145 |
|
| 146 |
void |
| 147 |
encryption_init (krb5_creds *creds) |
| 148 |
{ |
| 149 |
krb5_keyblock *newkey = 0; |
| 150 |
|
| 151 |
krb5_auth_con_getsendsubkey (telnet_context, auth_context, &newkey); |
| 152 |
if (session_key) |
| 153 |
{ |
| 154 |
krb5_free_keyblock (telnet_context, session_key); |
| 155 |
session_key = 0; |
| 156 |
} |
| 157 |
|
| 158 |
if (newkey) |
| 159 |
{ |
| 160 |
switch (newkey->enctype) |
| 161 |
{ |
| 162 |
case ENCTYPE_DES_CBC_CRC: |
| 163 |
case ENCTYPE_DES_CBC_MD5: |
| 164 |
krb5_copy_keyblock (telnet_context, newkey, &session_key); |
| 165 |
break; |
| 166 |
|
| 167 |
default: |
| 168 |
switch (creds->keyblock.enctype) |
| 169 |
{ |
| 170 |
case ENCTYPE_DES_CBC_CRC: |
| 171 |
case ENCTYPE_DES_CBC_MD5: |
| 172 |
krb5_copy_keyblock (telnet_context, &creds->keyblock, |
| 173 |
&session_key); |
| 174 |
break; |
| 175 |
|
| 176 |
default: |
| 177 |
DEBUG (("can't determine which keyblock to use")); |
| 178 |
/*FIXME: abort? */ |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
krb5_free_keyblock (telnet_context, newkey); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
# else |
| 187 |
# define encryption_init(c) |
| 188 |
# endif |
| 189 |
|
| 190 |
int |
| 191 |
kerberos5_send (TN_Authenticator *ap) |
| 192 |
{ |
| 193 |
krb5_error_code r; |
| 194 |
krb5_ccache ccache; |
| 195 |
krb5_creds creds; |
| 196 |
krb5_creds *new_creds = 0; |
| 197 |
int ap_opts; |
| 198 |
char type_check[2]; |
| 199 |
krb5_data check_data; |
| 200 |
|
| 201 |
if (!UserNameRequested) |
| 202 |
{ |
| 203 |
DEBUG (("telnet: Kerberos V5: no user name supplied\r\n")); |
| 204 |
return 0; |
| 205 |
} |
| 206 |
|
| 207 |
if ((r = krb5_cc_default (telnet_context, &ccache))) |
| 208 |
{ |
| 209 |
DEBUG (("telnet: Kerberos V5: could not get default ccache\r\n")); |
| 210 |
return 0; |
| 211 |
} |
| 212 |
|
| 213 |
memset (&creds, 0, sizeof (creds)); |
| 214 |
if ((r = krb5_sname_to_principal (telnet_context, RemoteHostName, |
| 215 |
"host", KRB5_NT_SRV_HST, &creds.server))) |
| 216 |
{ |
| 217 |
DEBUG (("telnet: Kerberos V5: error while constructing service name: %s\r\n", error_message (r))); |
| 218 |
return 0; |
| 219 |
} |
| 220 |
|
| 221 |
if (dest_realm) |
| 222 |
{ |
| 223 |
krb5_data rdata; |
| 224 |
|
| 225 |
rdata.length = strlen (dest_realm); |
| 226 |
rdata.data = malloc (rdata.length + 1); |
| 227 |
if (rdata.data == NULL) |
| 228 |
{ |
| 229 |
DEBUG (("telnet: Kerberos V5: could not allocate memory\r\n")); |
| 230 |
return 0; |
| 231 |
} |
| 232 |
strcpy (rdata.data, dest_realm); |
| 233 |
krb5_princ_set_realm (telnet_context, creds.server, &rdata); |
| 234 |
} |
| 235 |
|
| 236 |
if ((r = krb5_cc_get_principal (telnet_context, ccache, &creds.client))) |
| 237 |
{ |
| 238 |
DEBUG (("telnet: Kerberos V5: failure on principal (%s)\r\n", |
| 239 |
error_message (r))); |
| 240 |
krb5_free_cred_contents (telnet_context, &creds); |
| 241 |
return 0; |
| 242 |
} |
| 243 |
|
| 244 |
creds.keyblock.enctype = ENCTYPE_DES_CBC_CRC; |
| 245 |
if ((r = krb5_get_credentials (telnet_context, 0, |
| 246 |
ccache, &creds, &new_creds))) |
| 247 |
{ |
| 248 |
DEBUG (("telnet: Kerberos V5: failure on credentials(%s)\r\n", |
| 249 |
error_message (r))); |
| 250 |
krb5_free_cred_contents (telnet_context, &creds); |
| 251 |
return 0; |
| 252 |
} |
| 253 |
|
| 254 |
if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) |
| 255 |
ap_opts = AP_OPTS_MUTUAL_REQUIRED; |
| 256 |
else |
| 257 |
ap_opts = 0; |
| 258 |
|
| 259 |
# ifdef ENCRYPTION |
| 260 |
ap_opts |= AP_OPTS_USE_SUBKEY; |
| 261 |
# endif |
| 262 |
|
| 263 |
if (auth_context) |
| 264 |
{ |
| 265 |
krb5_auth_con_free (telnet_context, auth_context); |
| 266 |
auth_context = 0; |
| 267 |
} |
| 268 |
|
| 269 |
if ((r = krb5_auth_con_init (telnet_context, &auth_context))) |
| 270 |
{ |
| 271 |
DEBUG (("Kerberos V5: failed to init auth_context (%s)\r\n", |
| 272 |
error_message (r))); |
| 273 |
return 0; |
| 274 |
} |
| 275 |
|
| 276 |
krb5_auth_con_setflags (telnet_context, auth_context, |
| 277 |
KRB5_AUTH_CONTEXT_RET_TIME); |
| 278 |
|
| 279 |
type_check[0] = ap->type; |
| 280 |
type_check[1] = ap->way; |
| 281 |
check_data.magic = KV5M_DATA; |
| 282 |
check_data.length = 2; |
| 283 |
check_data.data = (char *) &type_check; |
| 284 |
|
| 285 |
r = krb5_mk_req_extended (telnet_context, &auth_context, ap_opts, |
| 286 |
&check_data, new_creds, &auth); |
| 287 |
|
| 288 |
encryption_init (new_creds); |
| 289 |
|
| 290 |
krb5_free_cred_contents (telnet_context, &creds); |
| 291 |
krb5_free_creds (telnet_context, new_creds); |
| 292 |
if (r) |
| 293 |
{ |
| 294 |
DEBUG (("telnet: Kerberos V5: mk_req failed (%s)\r\n", |
| 295 |
error_message (r))); |
| 296 |
return 0; |
| 297 |
} |
| 298 |
|
| 299 |
if (!auth_sendname (UserNameRequested, strlen (UserNameRequested))) |
| 300 |
{ |
| 301 |
DEBUG (("telnet: Not enough room for user name\r\n")); |
| 302 |
return 0; |
| 303 |
} |
| 304 |
|
| 305 |
if (!Data (ap, KRB_AUTH, auth.data, auth.length)) |
| 306 |
{ |
| 307 |
DEBUG (("telnet: Not enough room for authentication data\r\n")); |
| 308 |
return 0; |
| 309 |
} |
| 310 |
|
| 311 |
DEBUG (("telnet: Sent Kerberos V5 credentials to server\r\n")); |
| 312 |
|
| 313 |
return 1; |
| 314 |
} |
| 315 |
|
| 316 |
# ifdef ENCRYPTION |
| 317 |
void |
| 318 |
telnet_encrypt_key (Session_Key *skey) |
| 319 |
{ |
| 320 |
if (session_key) |
| 321 |
{ |
| 322 |
skey->type = SK_DES; |
| 323 |
skey->length = 8; |
| 324 |
skey->data = session_key->contents; |
| 325 |
encrypt_session_key (skey, 0); |
| 326 |
} |
| 327 |
} |
| 328 |
# else |
| 329 |
# define telnet_encrypt_key(s) |
| 330 |
# endif |
| 331 |
|
| 332 |
void |
| 333 |
kerberos5_reply (TN_Authenticator *ap, unsigned char *data, int cnt) |
| 334 |
{ |
| 335 |
# ifdef ENCRYPTION |
| 336 |
Session_Key skey; |
| 337 |
# endif |
| 338 |
static int mutual_complete = 0; |
| 339 |
|
| 340 |
if (cnt-- < 1) |
| 341 |
return; |
| 342 |
|
| 343 |
switch (*data++) |
| 344 |
{ |
| 345 |
case KRB_REJECT: |
| 346 |
if (cnt > 0) |
| 347 |
printf ("[ Kerberos V5 refuses authentication because %.*s ]\r\n", |
| 348 |
cnt, data); |
| 349 |
else |
| 350 |
printf ("[ Kerberos V5 refuses authentication ]\r\n"); |
| 351 |
auth_send_retry (); |
| 352 |
return; |
| 353 |
|
| 354 |
case KRB_ACCEPT: |
| 355 |
if (!mutual_complete) |
| 356 |
{ |
| 357 |
if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) |
| 358 |
{ |
| 359 |
printf |
| 360 |
("[ Kerberos V5 accepted you, but didn't provide mutual authentication! ]\r\n"); |
| 361 |
auth_send_retry (); |
| 362 |
break; |
| 363 |
} |
| 364 |
telnet_encrypt_key (&skey); |
| 365 |
} |
| 366 |
|
| 367 |
if (cnt) |
| 368 |
printf ("[ Kerberos V5 accepts you as ``%.*s''%s ]\r\n", cnt, data, |
| 369 |
mutual_complete ? |
| 370 |
" (server authenticated)" : " (server NOT authenticated)"); |
| 371 |
else |
| 372 |
printf ("[ Kerberos V5 accepts you ]\r\n"); |
| 373 |
auth_finished (ap, AUTH_USER); |
| 374 |
# ifdef FORWARD |
| 375 |
if (forward_flags & OPTS_FORWARD_CREDS) |
| 376 |
kerberos5_forward (ap); |
| 377 |
# endif |
| 378 |
break; |
| 379 |
|
| 380 |
case KRB_RESPONSE: |
| 381 |
if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) |
| 382 |
{ |
| 383 |
krb5_ap_rep_enc_part *reply; |
| 384 |
krb5_data inbuf; |
| 385 |
krb5_error_code r; |
| 386 |
|
| 387 |
inbuf.length = cnt; |
| 388 |
inbuf.data = (char *) data; |
| 389 |
|
| 390 |
if ((r = krb5_rd_rep (telnet_context, auth_context, &inbuf, |
| 391 |
&reply))) |
| 392 |
{ |
| 393 |
printf ("[ Mutual authentication failed: %s ]\r\n", |
| 394 |
error_message (r)); |
| 395 |
auth_send_retry (); |
| 396 |
break; |
| 397 |
} |
| 398 |
|
| 399 |
krb5_free_ap_rep_enc_part (telnet_context, reply); |
| 400 |
telnet_encrypt_key (&skey); |
| 401 |
mutual_complete = 1; |
| 402 |
} |
| 403 |
break; |
| 404 |
|
| 405 |
# ifdef FORWARD |
| 406 |
case KRB_FORWARD_ACCEPT: |
| 407 |
printf ("[ Kerberos V5 accepted forwarded credentials ]\r\n"); |
| 408 |
break; |
| 409 |
|
| 410 |
case KRB_FORWARD_REJECT: |
| 411 |
printf |
| 412 |
("[ Kerberos V5 refuses forwarded credentials because %.*s ]\r\n", |
| 413 |
cnt, data); |
| 414 |
break; |
| 415 |
# endif/* FORWARD */ |
| 416 |
|
| 417 |
default: |
| 418 |
DEBUG (("Unknown Kerberos option %d\r\n", data[-1])); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
int |
| 423 |
kerberos5_status (TN_Authenticator *ap MAYBE_UNUSED, |
| 424 |
char *name, size_t len, int level) |
| 425 |
{ |
| 426 |
if (level < AUTH_USER) |
| 427 |
return level; |
| 428 |
|
| 429 |
if (UserNameRequested |
| 430 |
&& krb5_kuserok (telnet_context, ticket->enc_part2->client, |
| 431 |
UserNameRequested)) |
| 432 |
{ |
| 433 |
/* FIXME: Check buffer length */ |
| 434 |
strncpy (name, UserNameRequested, len); |
| 435 |
return AUTH_VALID; |
| 436 |
} |
| 437 |
return AUTH_USER; |
| 438 |
} |
| 439 |
|
| 440 |
static int |
| 441 |
kerberos5_is_auth (TN_Authenticator *ap, unsigned char *data, int cnt, |
| 442 |
char *errbuf, int errbuflen) |
| 443 |
{ |
| 444 |
int r = 0; |
| 445 |
krb5_keytab keytabid = 0; |
| 446 |
krb5_authenticator *authenticator; |
| 447 |
char *name; |
| 448 |
krb5_data outbuf; |
| 449 |
krb5_keyblock *newkey = NULL; |
| 450 |
krb5_principal server; |
| 451 |
|
| 452 |
# ifdef ENCRYPTION |
| 453 |
Session_Key skey; |
| 454 |
# endif |
| 455 |
|
| 456 |
auth.data = (char *) data; |
| 457 |
auth.length = cnt; |
| 458 |
|
| 459 |
if (!r && !auth_context) |
| 460 |
r = krb5_auth_con_init (telnet_context, &auth_context); |
| 461 |
if (!r) |
| 462 |
{ |
| 463 |
krb5_rcache rcache; |
| 464 |
|
| 465 |
r = krb5_auth_con_getrcache (telnet_context, auth_context, &rcache); |
| 466 |
if (!r && !rcache) |
| 467 |
{ |
| 468 |
r = krb5_sname_to_principal (telnet_context, 0, 0, |
| 469 |
KRB5_NT_SRV_HST, &server); |
| 470 |
if (!r) |
| 471 |
{ |
| 472 |
r = krb5_get_server_rcache (telnet_context, |
| 473 |
krb5_princ_component |
| 474 |
(telnet_context, server, 0), |
| 475 |
&rcache); |
| 476 |
krb5_free_principal (telnet_context, server); |
| 477 |
} |
| 478 |
} |
| 479 |
if (!r) |
| 480 |
r = krb5_auth_con_setrcache (telnet_context, auth_context, rcache); |
| 481 |
} |
| 482 |
|
| 483 |
if (!r && telnet_srvtab) |
| 484 |
r = krb5_kt_resolve (telnet_context, telnet_srvtab, &keytabid); |
| 485 |
if (!r) |
| 486 |
r = krb5_rd_req (telnet_context, &auth_context, &auth, |
| 487 |
NULL, keytabid, NULL, &ticket); |
| 488 |
if (r) |
| 489 |
{ |
| 490 |
snprintf (errbuf, errbuflen, "krb5_rd_req failed: %s", |
| 491 |
error_message (r)); |
| 492 |
return r; |
| 493 |
} |
| 494 |
|
| 495 |
/* 256 bytes should be much larger than any reasonable |
| 496 |
first component of a service name especially since |
| 497 |
the default is of length 4. */ |
| 498 |
if (krb5_princ_component (telnet_context, ticket->server, 0)->length < 256) |
| 499 |
{ |
| 500 |
char princ[256]; |
| 501 |
strncpy (princ, |
| 502 |
krb5_princ_component (telnet_context, ticket->server, 0)->data, |
| 503 |
krb5_princ_component (telnet_context, ticket->server, |
| 504 |
0)->length); |
| 505 |
princ[krb5_princ_component (telnet_context, ticket->server, 0)->length] |
| 506 |
= '\0'; |
| 507 |
if (strcmp ("host", princ)) |
| 508 |
{ |
| 509 |
snprintf (errbuf, errbuflen, |
| 510 |
"incorrect service name: \"%s\" != \"host\"", princ); |
| 511 |
return 1; |
| 512 |
} |
| 513 |
} |
| 514 |
else |
| 515 |
{ |
| 516 |
strncpy (errbuf, "service name too long", errbuflen); |
| 517 |
return 1; |
| 518 |
} |
| 519 |
|
| 520 |
r = krb5_auth_con_getauthenticator (telnet_context, |
| 521 |
auth_context, &authenticator); |
| 522 |
if (r) |
| 523 |
{ |
| 524 |
snprintf (errbuf, errbuflen, |
| 525 |
"krb5_auth_con_getauthenticator failed: %s", |
| 526 |
error_message (r)); |
| 527 |
return 1; |
| 528 |
} |
| 529 |
|
| 530 |
# ifdef AUTH_ENCRYPT_MASK |
| 531 |
if ((ap->way & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_ON |
| 532 |
&& !authenticator->checksum) |
| 533 |
{ |
| 534 |
snprintf (errbuf, errbuflen, |
| 535 |
"authenticator is missing required checksum"); |
| 536 |
return 1; |
| 537 |
} |
| 538 |
# endif |
| 539 |
|
| 540 |
if (authenticator->checksum) |
| 541 |
{ |
| 542 |
char type_check[2]; |
| 543 |
krb5_checksum *cksum = authenticator->checksum; |
| 544 |
krb5_keyblock *key; |
| 545 |
krb5_boolean valid; |
| 546 |
|
| 547 |
type_check[0] = ap->type; |
| 548 |
type_check[1] = ap->way; |
| 549 |
|
| 550 |
r = krb5_auth_con_getkey (telnet_context, auth_context, &key); |
| 551 |
if (r) |
| 552 |
{ |
| 553 |
snprintf (errbuf, errbuflen, |
| 554 |
"krb5_auth_con_getkey failed: %s", error_message (r)); |
| 555 |
return 1; |
| 556 |
} |
| 557 |
|
| 558 |
# if 1 |
| 559 |
/* XXX: Obsolete interface. Remove after investigation. */ |
| 560 |
r = krb5_verify_checksum (telnet_context, |
| 561 |
cksum->checksum_type, cksum, |
| 562 |
&type_check, 2, key->contents, key->length); |
| 563 |
krb5_free_keyblock (telnet_context, key); |
| 564 |
|
| 565 |
if (r) |
| 566 |
{ |
| 567 |
snprintf (errbuf, errbuflen, |
| 568 |
"checksum verification failed: %s", error_message (r)); |
| 569 |
return 1; |
| 570 |
} |
| 571 |
# else |
| 572 |
/* Incomplete call! |
| 573 |
* |
| 574 |
* XXX: Establish replacement for the preceding call. |
| 575 |
* It is no longer present in all implementations. |
| 576 |
*/ |
| 577 |
r = krb5_c_verify_checksum (telnet_context, key, |
| 578 |
/* usage */ , /* data */ , |
| 579 |
cksum, &valid); |
| 580 |
krb5_free_keyblock (telnet_context, key); |
| 581 |
|
| 582 |
if (r || !valid) |
| 583 |
{ |
| 584 |
snprintf (errbuf, errbuflen, |
| 585 |
"checksum verification failed: %s", error_message (r)); |
| 586 |
return 1; |
| 587 |
} |
| 588 |
# endif |
| 589 |
} |
| 590 |
|
| 591 |
krb5_free_authenticator (telnet_context, authenticator); |
| 592 |
if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) |
| 593 |
{ |
| 594 |
if ((r = krb5_mk_rep (telnet_context, auth_context, &outbuf))) |
| 595 |
{ |
| 596 |
snprintf (errbuf, errbuflen, "Make reply failed: %s", |
| 597 |
error_message (r)); |
| 598 |
return 1; |
| 599 |
} |
| 600 |
|
| 601 |
Data (ap, KRB_RESPONSE, outbuf.data, outbuf.length); |
| 602 |
} |
| 603 |
|
| 604 |
if (krb5_unparse_name (telnet_context, ticket->enc_part2->client, &name)) |
| 605 |
name = 0; |
| 606 |
|
| 607 |
Data (ap, KRB_ACCEPT, name, name ? -1 : 0); |
| 608 |
DEBUG (("telnetd: Kerberos5 identifies him as ``%s''\r\n", |
| 609 |
name ? name : "")); |
| 610 |
auth_finished (ap, AUTH_USER); |
| 611 |
|
| 612 |
free (name); |
| 613 |
krb5_auth_con_getrecvsubkey (telnet_context, auth_context, &newkey); |
| 614 |
|
| 615 |
if (session_key) |
| 616 |
{ |
| 617 |
krb5_free_keyblock (telnet_context, session_key); |
| 618 |
session_key = 0; |
| 619 |
} |
| 620 |
|
| 621 |
if (newkey) |
| 622 |
{ |
| 623 |
krb5_copy_keyblock (telnet_context, newkey, &session_key); |
| 624 |
krb5_free_keyblock (telnet_context, newkey); |
| 625 |
} |
| 626 |
else |
| 627 |
{ |
| 628 |
krb5_copy_keyblock (telnet_context, ticket->enc_part2->session, |
| 629 |
&session_key); |
| 630 |
} |
| 631 |
telnet_encrypt_key (&skey); |
| 632 |
return 0; |
| 633 |
} |
| 634 |
|
| 635 |
# ifdef FORWARD |
| 636 |
static int |
| 637 |
kerberos5_is_forward (TN_Authenticator *ap, unsigned char *data, int cnt, |
| 638 |
char *errbuf, int errbuflen) |
| 639 |
{ |
| 640 |
int r = 0; |
| 641 |
krb5_data inbuf; |
| 642 |
|
| 643 |
inbuf.length = cnt; |
| 644 |
inbuf.data = (char *) data; |
| 645 |
if ((r = krb5_auth_con_genaddrs (telnet_context, auth_context, |
| 646 |
net, |
| 647 |
KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR)) |
| 648 |
|| (r = rd_and_store_for_creds (telnet_context, auth_context, |
| 649 |
&inbuf, ticket))) |
| 650 |
{ |
| 651 |
snprintf (errbuf, errbuflen, "Read forwarded creds failed: %s", |
| 652 |
error_message (r)); |
| 653 |
Data (ap, KRB_FORWARD_REJECT, errbuf, -1); |
| 654 |
DEBUG (("Could not read forwarded credentials\r\n")); |
| 655 |
} |
| 656 |
else |
| 657 |
{ |
| 658 |
Data (ap, KRB_FORWARD_ACCEPT, 0, 0); |
| 659 |
DEBUG (("Forwarded credentials obtained\r\n")); |
| 660 |
} |
| 661 |
return r; |
| 662 |
} |
| 663 |
# else |
| 664 |
# define kerberos5_is_forward(ap,data,cnt,errbuf,errbuflen) 0 |
| 665 |
# endif |
| 666 |
|
| 667 |
void |
| 668 |
kerberos5_is (TN_Authenticator *ap, unsigned char *data, int cnt) |
| 669 |
{ |
| 670 |
int r = 0; |
| 671 |
char errbuf[512]; |
| 672 |
|
| 673 |
if (cnt-- < 1) |
| 674 |
return; |
| 675 |
errbuf[0] = 0; |
| 676 |
switch (*data++) |
| 677 |
{ |
| 678 |
case KRB_AUTH: |
| 679 |
r = kerberos5_is_auth (ap, data, cnt, errbuf, sizeof errbuf); |
| 680 |
break; |
| 681 |
|
| 682 |
case KRB_FORWARD: |
| 683 |
r = kerberos5_is_forward (ap, data, cnt, errbuf, sizeof errbuf); |
| 684 |
break; |
| 685 |
|
| 686 |
default: |
| 687 |
DEBUG (("Unknown Kerberos option %d\r\n", data[-1])); |
| 688 |
Data (ap, KRB_REJECT, 0, 0); |
| 689 |
break; |
| 690 |
} |
| 691 |
|
| 692 |
if (r) |
| 693 |
{ |
| 694 |
if (!errbuf[0]) |
| 695 |
snprintf (errbuf, sizeof errbuf, |
| 696 |
"kerberos_is: %s", error_message (r)); |
| 697 |
Data (ap, KRB_REJECT, errbuf, -1); |
| 698 |
DEBUG (("%s\r\n", errbuf)); |
| 699 |
syslog (LOG_ERR, "%s", errbuf); |
| 700 |
if (auth_context) |
| 701 |
{ |
| 702 |
krb5_auth_con_free (telnet_context, auth_context); |
| 703 |
auth_context = 0; |
| 704 |
} |
| 705 |
} |
| 706 |
} |
| 707 |
|
| 708 |
static char * |
| 709 |
req_type_str (int type) |
| 710 |
{ |
| 711 |
switch (type) |
| 712 |
{ |
| 713 |
case KRB_REJECT: |
| 714 |
return "REJECT"; |
| 715 |
|
| 716 |
case KRB_ACCEPT: |
| 717 |
return "ACCEPT"; |
| 718 |
|
| 719 |
case KRB_AUTH: |
| 720 |
return "AUTH"; |
| 721 |
|
| 722 |
case KRB_RESPONSE: |
| 723 |
return "RESPONSE"; |
| 724 |
|
| 725 |
case KRB_FORWARD: |
| 726 |
return "FORWARD"; |
| 727 |
|
| 728 |
case KRB_FORWARD_ACCEPT: |
| 729 |
return "FORWARD_ACCEPT"; |
| 730 |
|
| 731 |
case KRB_FORWARD_REJECT: |
| 732 |
return "FORWARD_REJECT"; |
| 733 |
|
| 734 |
} |
| 735 |
return NULL; |
| 736 |
} |
| 737 |
|
| 738 |
# define ADDC(p,l,c) if ((l) > 0) {*(p)++ = (c); --(l);} |
| 739 |
|
| 740 |
void |
| 741 |
kerberos5_printsub (unsigned char *data, int cnt, char *buf, int buflen) |
| 742 |
{ |
| 743 |
char *p; |
| 744 |
int i; |
| 745 |
|
| 746 |
buf[buflen - 1] = '\0'; /* make sure its NULL terminated */ |
| 747 |
buflen -= 1; |
| 748 |
|
| 749 |
p = req_type_str (data[3]); |
| 750 |
if (!p) |
| 751 |
{ |
| 752 |
int l = snprintf (buf, buflen, " %d (unknown)", data[3]); |
| 753 |
buf += l; |
| 754 |
buflen -= l; |
| 755 |
} |
| 756 |
else |
| 757 |
{ |
| 758 |
while (buflen > 0 && (*buf++ = *p++) != 0) |
| 759 |
buflen--; |
| 760 |
} |
| 761 |
|
| 762 |
switch (data[3]) |
| 763 |
{ |
| 764 |
case KRB_REJECT: /* Rejected (reason might follow) */ |
| 765 |
case KRB_ACCEPT: /* Accepted (username might follow) */ |
| 766 |
if (cnt <= 4) |
| 767 |
break; |
| 768 |
ADDC (buf, buflen, '"'); |
| 769 |
for (i = 4; i < cnt; i++) |
| 770 |
ADDC (buf, buflen, data[i]); |
| 771 |
ADDC (buf, buflen, '"'); |
| 772 |
ADDC (buf, buflen, '\0'); |
| 773 |
break; |
| 774 |
|
| 775 |
case KRB_AUTH: |
| 776 |
case KRB_RESPONSE: |
| 777 |
case KRB_FORWARD: |
| 778 |
case KRB_FORWARD_ACCEPT: |
| 779 |
case KRB_FORWARD_REJECT: |
| 780 |
for (i = 4; buflen > 0 && i < cnt; i++) |
| 781 |
{ |
| 782 |
int l = snprintf (buf, buflen, " %d", data[i]); |
| 783 |
buf += l; |
| 784 |
buflen -= l; |
| 785 |
} |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
# ifdef FORWARD |
| 790 |
|
| 791 |
void |
| 792 |
kerberos5_forward (TN_Authenticator *ap) |
| 793 |
{ |
| 794 |
krb5_error_code r; |
| 795 |
krb5_ccache ccache; |
| 796 |
krb5_principal client = 0; |
| 797 |
krb5_principal server = 0; |
| 798 |
krb5_data forw_creds; |
| 799 |
|
| 800 |
forw_creds.data = 0; |
| 801 |
|
| 802 |
if ((r = krb5_cc_default (telnet_context, &ccache))) |
| 803 |
{ |
| 804 |
DEBUG (("Kerberos V5: could not get default ccache - %s\r\n", |
| 805 |
error_message (r))); |
| 806 |
return; |
| 807 |
} |
| 808 |
|
| 809 |
for (;;) /* Fake loop */ |
| 810 |
{ |
| 811 |
if ((r = krb5_cc_get_principal (telnet_context, ccache, &client))) |
| 812 |
{ |
| 813 |
DEBUG (("Kerberos V5: could not get default principal - %s\r\n", |
| 814 |
error_message (r))); |
| 815 |
break; |
| 816 |
} |
| 817 |
if ((r = |
| 818 |
krb5_sname_to_principal (telnet_context, RemoteHostName, "host", |
| 819 |
KRB5_NT_SRV_HST, &server))) |
| 820 |
{ |
| 821 |
DEBUG (("Kerberos V5: could not make server principal - %s\r\n", |
| 822 |
error_message (r))); |
| 823 |
break; |
| 824 |
} |
| 825 |
if ((r = krb5_auth_con_genaddrs (telnet_context, auth_context, net, |
| 826 |
KRB5_AUTH_CONTEXT_GENERATE_LOCAL_FULL_ADDR))) |
| 827 |
{ |
| 828 |
DEBUG (("Kerberos V5: could not gen local full address - %s\r\n", |
| 829 |
error_message (r))); |
| 830 |
break; |
| 831 |
} |
| 832 |
if ((r = krb5_fwd_tgt_creds (telnet_context, auth_context, 0, client, |
| 833 |
server, ccache, |
| 834 |
forward_flags & OPTS_FORWARDABLE_CREDS, |
| 835 |
&forw_creds))) |
| 836 |
{ |
| 837 |
DEBUG (("Kerberos V5: error getting forwarded creds - %s\r\n", |
| 838 |
error_message (r))); |
| 839 |
break; |
| 840 |
} |
| 841 |
|
| 842 |
/* Send forwarded credentials */ |
| 843 |
if (!Data (ap, KRB_FORWARD, forw_creds.data, forw_creds.length)) |
| 844 |
{ |
| 845 |
DEBUG (("Not enough room for authentication data\r\n")); |
| 846 |
} |
| 847 |
else |
| 848 |
{ |
| 849 |
DEBUG (("Forwarded local Kerberos V5 credentials to server\r\n")); |
| 850 |
} |
| 851 |
break; |
| 852 |
} |
| 853 |
|
| 854 |
if (client) |
| 855 |
krb5_free_principal (telnet_context, client); |
| 856 |
if (server) |
| 857 |
krb5_free_principal (telnet_context, server); |
| 858 |
free (forw_creds.data); |
| 859 |
krb5_cc_close (telnet_context, ccache); |
| 860 |
} |
| 861 |
# endif |
| 862 |
|
| 863 |
#endif /* KRB5 */ |