📄 kerberos5.c
字号:
/* * $Source: /mit/krb5/.cvsroot/src/appl/telnet/libtelnet/kerberos5.c,v $ * $Author: tytso $ * $Id: kerberos5.c,v 5.3 1993/09/01 03:00:12 tytso Exp $ */#if !defined(lint) && !defined(SABER)static#ifdef __STDC__const#endifchar rcsid_kerberos5_c[] = "$Id: kerberos5.c,v 5.3 1993/09/01 03:00:12 tytso Exp $";#endif /* lint *//*- * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */#ifndef lintstatic char sccsid[] = "@(#)kerberos5.c 8.2 (Berkeley) 12/15/93";#endif /* not lint *//* * Copyright (C) 1990 by the Massachusetts Institute of Technology * * Export of this software from the United States of America may * require a specific license from the United States Government. * It is the responsibility of any person or organization contemplating * export to obtain such a license before exporting. * * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and * distribute this software and its documentation for any purpose and * without fee is hereby granted, provided that the above copyright * notice appear in all copies and that both that copyright notice and * this permission notice appear in supporting documentation, and that * the name of M.I.T. not be used in advertising or publicity pertaining * to distribution of the software without specific, written prior * permission. M.I.T. makes no representations about the suitability of * this software for any purpose. It is provided "as is" without express * or implied warranty. */#ifdef KRB5#include <arpa/telnet.h>#include <stdio.h>#include <krb5/krb5.h>#include <krb5/asn1.h>#include <krb5/crc-32.h>#include <krb5/los-proto.h>#include <krb5/ext-proto.h>#include <com_err.h>#include <netdb.h>#include <ctype.h>/* kerberos 5 include files (ext-proto.h) will get an appropriate stdlib.h and string.h/strings.h */ #include "encrypt.h"#include "auth.h"#include "misc.h"extern auth_debug_mode;#ifdef FORWARDint forward_flags = 0; /* Flags get set in telnet/main.c on -f and -F *//* These values need to be the same as those defined in telnet/main.c. *//* Either define them in both places, or put in some common header file. */#define OPTS_FORWARD_CREDS 0x00000002#define OPTS_FORWARDABLE_CREDS 0x00000001void kerberos5_forward();#endif /* FORWARD */static unsigned char str_data[1024] = { IAC, SB, TELOPT_AUTHENTICATION, 0, AUTHTYPE_KERBEROS_V5, };/*static unsigned char str_name[1024] = { IAC, SB, TELOPT_AUTHENTICATION, TELQUAL_NAME, };*/#define KRB_AUTH 0 /* Authentication data follows */#define KRB_REJECT 1 /* Rejected (reason might follow) */#define KRB_ACCEPT 2 /* Accepted */#define KRB_RESPONSE 3 /* Response for mutual auth. */#ifdef FORWARD#define KRB_FORWARD 4 /* Forwarded credentials follow */#define KRB_FORWARD_ACCEPT 5 /* Forwarded credentials accepted */#define KRB_FORWARD_REJECT 6 /* Forwarded credentials rejected */#endif /* FORWARD */static krb5_data auth; /* telnetd gets session key from here */static krb5_tkt_authent *authdat = NULL;/* telnet matches the AP_REQ and AP_REP with this */static krb5_authenticator authenticator;/* some compilers can't hack void *, so we use the Kerberos krb5_pointer, which is either void * or char *, depending on the compiler. */#define Voidptr krb5_pointerBlock session_key; static intData(ap, type, d, c) Authenticator *ap; int type; Voidptr d; int c;{ unsigned char *p = str_data + 4; unsigned char *cd = (unsigned char *)d; if (c == -1) c = strlen((char *)cd); if (auth_debug_mode) { printf("%s:%d: [%d] (%d)", str_data[3] == TELQUAL_IS ? ">>>IS" : ">>>REPLY", str_data[3], type, c); printd(d, c); printf("\r\n"); } *p++ = ap->type; *p++ = ap->way; *p++ = type; while (c-- > 0) { if ((*p++ = *cd++) == IAC) *p++ = IAC; } *p++ = IAC; *p++ = SE; if (str_data[3] == TELQUAL_IS) printsub('>', &str_data[2], p - &str_data[2]); return(net_write(str_data, p - str_data));} intkerberos5_init(ap, server) Authenticator *ap; int server;{ if (server) str_data[3] = TELQUAL_REPLY; else str_data[3] = TELQUAL_IS; krb5_init_ets(); return(1);} intkerberos5_send(ap) Authenticator *ap;{ char **realms; char *name; char *p1, *p2; krb5_checksum ksum; krb5_octet sum[CRC32_CKSUM_LENGTH]; krb5_principal server; krb5_error_code r; krb5_ccache ccache; krb5_creds creds; /* telnet gets session key from here */ extern krb5_flags krb5_kdc_default_options; int ap_opts;#ifdef ENCRYPTION krb5_keyblock *newkey = 0;#endif /* ENCRYPTION */ ksum.checksum_type = CKSUMTYPE_CRC32; ksum.contents = sum; ksum.length = sizeof(sum); bzero((Voidptr )sum, sizeof(sum)); if (!UserNameRequested) { if (auth_debug_mode) { printf("Kerberos V5: no user name supplied\r\n"); } return(0); } if (r = krb5_cc_default(&ccache)) { if (auth_debug_mode) { printf("Kerberos V5: could not get default ccache\r\n"); } return(0); } if ((name = malloc(strlen(RemoteHostName)+1)) == NULL) { if (auth_debug_mode) printf("Out of memory for hostname in Kerberos V5\r\n"); return(0); } if (r = krb5_get_host_realm(RemoteHostName, &realms)) { if (auth_debug_mode) printf("Kerberos V5: no realm for %s\r\n", RemoteHostName); free(name); return(0); } p1 = RemoteHostName; p2 = name; while (*p2 = *p1++) { if (isupper(*p2)) *p2 |= 040; ++p2; } if (r = krb5_build_principal_ext(&server, strlen(realms[0]), realms[0], 4, "host", p2 - name, name, 0)) { if (auth_debug_mode) { printf("Kerberos V5: failure setting up principal (%s)\r\n", error_message(r)); } free(name); krb5_free_host_realm(realms); return(0); } bzero((char *)&creds, sizeof(creds)); creds.server = server; if (r = krb5_cc_get_principal(ccache, &creds.client)) { if (auth_debug_mode) { printf("Kerberos V5: failure on principal (%s)\r\n", error_message(r)); } free(name); krb5_free_principal(server); krb5_free_host_realm(realms); return(0); } if (r = krb5_get_credentials(krb5_kdc_default_options, ccache, &creds)) { if (auth_debug_mode) { printf("Kerberos V5: failure on credentials(%d)\r\n",r); } free(name); krb5_free_host_realm(realms); krb5_free_principal(server); return(0); } if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ap_opts = AP_OPTS_MUTUAL_REQUIRED; else ap_opts = 0; r = krb5_mk_req_extended(ap_opts, &ksum, krb5_kdc_default_options, 0,#ifdef ENCRYPTION &newkey,#else /* ENCRYPTION */ 0,#endif /* ENCRYPTION */ ccache, &creds, &authenticator, &auth); /* don't let the key get freed if we clean up the authenticator */ authenticator.subkey = 0; free(name); krb5_free_host_realm(realms); krb5_free_principal(server);#ifdef ENCRYPTION if (newkey) { /* keep the key in our private storage, but don't use it yet---see kerberos5_reply() below */ if (newkey->keytype != KEYTYPE_DES) { if (creds.keyblock.keytype == KEYTYPE_DES) /* use the session key in credentials instead */ memcpy((char *)session_key, (char *)creds.keyblock.contents, sizeof(Block)); else /* XXX ? */; } else { memcpy((char *)session_key, (char *)newkey->contents, sizeof(Block)); } krb5_free_keyblock(newkey); }#endif /* ENCRYPTION */ if (r) { if (auth_debug_mode) { printf("Kerberos V5: mk_req failed (%s)\r\n", error_message(r)); } return(0); } if (!auth_sendname(UserNameRequested, strlen(UserNameRequested))) { if (auth_debug_mode) printf("Not enough room for user name\r\n"); return(0); } if (!Data(ap, KRB_AUTH, auth.data, auth.length)) { if (auth_debug_mode) printf("Not enough room for authentication data\r\n"); return(0); } if (auth_debug_mode) { printf("Sent Kerberos V5 credentials to server\r\n"); } return(1);} voidkerberos5_is(ap, data, cnt) Authenticator *ap; unsigned char *data; int cnt;{ int r; struct hostent *hp; char *p1, *p2; static char *realm = NULL; krb5_principal server; krb5_ap_rep_enc_part reply; krb5_data outbuf;#ifdef ENCRYPTION Session_Key skey;#endif /* ENCRYPTION */ char *name; char *getenv(); krb5_data inbuf; if (cnt-- < 1) return; switch (*data++) { case KRB_AUTH: auth.data = (char *)data; auth.length = cnt; if (!(hp = gethostbyname(LocalHostName))) { if (auth_debug_mode) printf("Cannot resolve local host name\r\n"); Data(ap, KRB_REJECT, "Unknown local hostname.", -1); auth_finished(ap, AUTH_REJECT); return; } if (!realm && (krb5_get_default_realm(&realm))) { if (auth_debug_mode) printf("Could not get default realm\r\n"); Data(ap, KRB_REJECT, "Could not get default realm.", -1); auth_finished(ap, AUTH_REJECT);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -