⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kerberos5.c

📁 经典的unix下telnetd代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*     $OpenBSD: kerberos5.c,v 1.2 2003/06/02 19:38:25 millert Exp $       *//*- * 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. 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. *//* * This source code is no longer held under any constraint of USA * `cryptographic laws' since it was exported legally.  The cryptographic * functions were removed from the code and a "Bones" distribution was * made.  A Commodity Jurisdiction Request #012-94 was filed with the * USA State Department, who handed it to the Commerce department.  The * code was determined to fall under General License GTDA under ECCN 5D96G, * and hence exportable.  The cryptographic interfaces were re-added by Eric * Young, and then KTH proceeded to maintain the code in the free world. * *//* * 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. *//* $KTH: kerberos5.c,v 1.47 2001/01/09 18:45:33 assar Exp $ */#ifdef	KRB5#include <arpa/telnet.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <netdb.h>#include <ctype.h>#include <pwd.h>#include <errno.h>#define Authenticator k5_Authenticator#include <kerberosV/krb5.h>#undef Authenticator#include <err.h>#include "encrypt.h"#include "auth.h"#include "misc.h"#if defined(DCE)int dfsk5ok = 0;int dfspag = 0;int dfsfwd = 0;#endifint forward_flags = 0;  /* Flags get set in telnet/main.c on -f and -F */int forward(int);int forwardable(int);/* 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 (Authenticator *);static unsigned char str_data[1024] = { IAC, SB, TELOPT_AUTHENTICATION, 0,			  		AUTHTYPE_KERBEROS_V5, };#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. */#define KRB_FORWARD     	4       /* Forwarded credentials follow */#define KRB_FORWARD_ACCEPT     	5       /* Forwarded credentials accepted */#define KRB_FORWARD_REJECT     	6       /* Forwarded credentials rejected */static	krb5_data auth;static  krb5_ticket *ticket;static krb5_context context;static krb5_auth_context auth_context;intcheck_krb5_tickets(){    krb5_error_code ret;    krb5_context context;    krb5_ccache ccache;    krb5_principal principal;    int retval = 1;    ret = krb5_init_context(&context);    if(ret)	errx(1, "krb5_init_context failt: %d", ret);    ret = krb5_cc_default(context, &ccache);    if(ret)	errx(1, "krb5_cc_default: %d", ret);    ret = krb5_cc_get_principal (context, ccache, &principal);    switch(ret) {    case ENOENT:	retval = 0;	goto done;    case 0:	retval = 1;	goto done;    default:	errx(1, "krb5_cc_get_principal: %d", ret);	break;    } done:    krb5_free_context(context);    return retval;}static intData(Authenticator *ap, int type, void *d, int c){    unsigned char *p = str_data + 4;    unsigned char *cd = (unsigned char *)d;    if (c == -1)	c = strlen(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(telnet_net_write(str_data, p - str_data));}intkerberos5_init(Authenticator *ap, int server){    krb5_error_code ret;    ret = krb5_init_context(&context);    if (ret)	return 0;    if (server) {	krb5_keytab kt;	krb5_kt_cursor cursor;	ret = krb5_kt_default(context, &kt);	if (ret)	    return 0;	ret = krb5_kt_start_seq_get (context, kt, &cursor);	if (ret) {	    krb5_kt_close (context, kt);	    return 0;	}	krb5_kt_end_seq_get (context, kt, &cursor);	krb5_kt_close (context, kt);	str_data[3] = TELQUAL_REPLY;    } else	str_data[3] = TELQUAL_IS;    return(1);}extern int net;static intkerberos5_send(char *name, Authenticator *ap){    krb5_error_code ret;    krb5_ccache ccache;    int ap_opts;    krb5_data cksum_data;    char foo[2];    if(check_krb5_tickets() != 1)	return 0;        if (!UserNameRequested) {	if (auth_debug_mode) {	    printf("Kerberos V5: no user name supplied\r\n");	}	return(0);    }        ret = krb5_cc_default(context, &ccache);    if (ret) {	if (auth_debug_mode) {	    printf("Kerberos V5: could not get default ccache: %s\r\n",		   krb5_get_err_text (context, ret));	}	return 0;    }	    if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL)	ap_opts = AP_OPTS_MUTUAL_REQUIRED;    else	ap_opts = 0;        ap_opts |= AP_OPTS_USE_SUBKEY;        ret = krb5_auth_con_init (context, &auth_context);    if (ret) {	if (auth_debug_mode) {	    printf("Kerberos V5: krb5_auth_con_init failed (%s)\r\n",		   krb5_get_err_text(context, ret));	}	return(0);    }    ret = krb5_auth_con_setaddrs_from_fd (context,					  auth_context,					  &net);    if (ret) {	if (auth_debug_mode) {	    printf ("Kerberos V5:"		    " krb5_auth_con_setaddrs_from_fd failed (%s)\r\n",		    krb5_get_err_text(context, ret));	}	return(0);    }    krb5_auth_con_setkeytype (context, auth_context, KEYTYPE_DES);    foo[0] = ap->type;    foo[1] = ap->way;    cksum_data.length = sizeof(foo);    cksum_data.data   = foo;    {	krb5_principal service;	char sname[128];	ret = krb5_sname_to_principal (context,				       RemoteHostName,				       NULL,				       KRB5_NT_SRV_HST,				       &service);	if(ret) {	    if (auth_debug_mode) {		printf ("Kerberos V5:"			" krb5_sname_to_principal(%s) failed (%s)\r\n",			RemoteHostName, krb5_get_err_text(context, ret));	    }	    return 0;	}	ret = krb5_unparse_name_fixed(context, service, sname, sizeof(sname));	if(ret) {	    if (auth_debug_mode) {		printf ("Kerberos V5:"			" krb5_unparse_name_fixed failed (%s)\r\n",			krb5_get_err_text(context, ret));	    }	    return 0;	}	printf("[ Trying %s (%s)... ]\r\n", name, sname);	ret = krb5_mk_req_exact(context, &auth_context, ap_opts,				service, 				&cksum_data, ccache, &auth);	krb5_free_principal (context, service);    }    if (ret) {	if (1 || auth_debug_mode) {	    printf("Kerberos V5: mk_req failed (%s)\r\n",		   krb5_get_err_text(context, ret));	}	return(0);    }    if (!auth_sendname((unsigned char *)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);}intkerberos5_send_mutual(Authenticator *ap){    return kerberos5_send("mutual KERBEROS5", ap);}intkerberos5_send_oneway(Authenticator *ap){    return kerberos5_send("KERBEROS5", ap);}voidkerberos5_is(Authenticator *ap, unsigned char *data, int cnt){    krb5_error_code ret;    krb5_data outbuf;    krb5_keyblock *key_block;    char *name;    krb5_principal server;    int zero = 0;    if (cnt-- < 1)	return;    switch (*data++) {    case KRB_AUTH:	auth.data = (char *)data;	auth.length = cnt;	auth_context = NULL;	ret = krb5_auth_con_init (context, &auth_context);	if (ret) {	    Data(ap, KRB_REJECT, "krb5_auth_con_init failed", -1);	    auth_finished(ap, AUTH_REJECT);	    if (auth_debug_mode)		printf("Kerberos V5: krb5_auth_con_init failed (%s)\r\n",		       krb5_get_err_text(context, ret));	    return;	}	ret = krb5_auth_con_setaddrs_from_fd (context,					      auth_context,					      &zero);	if (ret) {	    Data(ap, KRB_REJECT, "krb5_auth_con_setaddrs_from_fd failed", -1);	    auth_finished(ap, AUTH_REJECT);	    if (auth_debug_mode)		printf("Kerberos V5: "		       "krb5_auth_con_setaddrs_from_fd failed (%s)\r\n",		       krb5_get_err_text(context, ret));	    return;	}	ret = krb5_sock_to_principal (context,				      0,				      "host",				      KRB5_NT_SRV_HST,				      &server);	if (ret) {	    Data(ap, KRB_REJECT, "krb5_sock_to_principal failed", -1);	    auth_finished(ap, AUTH_REJECT);	    if (auth_debug_mode)		printf("Kerberos V5: "		       "krb5_sock_to_principal failed (%s)\r\n",		       krb5_get_err_text(context, ret));	    return;	}	ret = krb5_rd_req(context,			  &auth_context,			  &auth, 			  server,			  NULL,			  NULL,			  &ticket);	krb5_free_principal (context, server);	if (ret) {	    char *errbuf;	    asprintf(&errbuf,		     "Read req failed: %s",		     krb5_get_err_text(context, ret));	    Data(ap, KRB_REJECT, errbuf, -1);	    if (auth_debug_mode)		printf("%s\r\n", errbuf);	    free (errbuf);	    return;	}		{	    char foo[2];	    	    foo[0] = ap->type;	    foo[1] = ap->way;	    	    ret = krb5_verify_authenticator_checksum(context,						     auth_context,						     foo, 						     sizeof(foo));	    if (ret) {		char *errbuf;		asprintf(&errbuf, "Bad checksum: %s", 			 krb5_get_err_text(context, ret));		Data(ap, KRB_REJECT, errbuf, -1);		if (auth_debug_mode)		    printf ("%s\r\n", errbuf);		free(errbuf);		return;	    }	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -