Path: blob/main/crypto/heimdal/appl/telnet/libtelnet/spx.c
34907 views
/*-1* Copyright (c) 1992, 19932* The Regents of the University of California. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. All advertising materials mentioning features or use of this software13* must display the following acknowledgement:14* This product includes software developed by the University of15* California, Berkeley and its contributors.16* 4. Neither the name of the University nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include <config.h>3435RCSID("$Id$");3637#ifdef SPX38/*39* COPYRIGHT (C) 1990 DIGITAL EQUIPMENT CORPORATION40* ALL RIGHTS RESERVED41*42* "Digital Equipment Corporation authorizes the reproduction,43* distribution and modification of this software subject to the following44* restrictions:45*46* 1. Any partial or whole copy of this software, or any modification47* thereof, must include this copyright notice in its entirety.48*49* 2. This software is supplied "as is" with no warranty of any kind,50* expressed or implied, for any purpose, including any warranty of fitness51* or merchantibility. DIGITAL assumes no responsibility for the use or52* reliability of this software, nor promises to provide any form of53* support for it on any basis.54*55* 3. Distribution of this software is authorized only if no profit or56* remuneration of any kind is received in exchange for such distribution.57*58* 4. This software produces public key authentication certificates59* bearing an expiration date established by DIGITAL and RSA Data60* Security, Inc. It may cease to generate certificates after the expiration61* date. Any modification of this software that changes or defeats62* the expiration date or its effect is unauthorized.63*64* 5. Software that will renew or extend the expiration date of65* authentication certificates produced by this software may be obtained66* from RSA Data Security, Inc., 10 Twin Dolphin Drive, Redwood City, CA67* 94065, (415)595-8782, or from DIGITAL"68*69*/7071#ifdef HAVE_SYS_TYPES_H72#include <sys/types.h>73#endif74#ifdef HAVE_ARPA_TELNET_H75#include <arpa/telnet.h>76#endif77#include <stdio.h>78#include "gssapi_defs.h"79#include <stdlib.h>80#include <string.h>8182#include <pwd.h>83#ifdef SOCKS84#include <socks.h>85#endif8687#include "encrypt.h"88#include "auth.h"89#include "misc.h"9091extern auth_debug_mode;9293static unsigned char str_data[1024] = { IAC, SB, TELOPT_AUTHENTICATION, 0,94AUTHTYPE_SPX, };95static unsigned char str_name[1024] = { IAC, SB, TELOPT_AUTHENTICATION,96TELQUAL_NAME, };9798#define SPX_AUTH 0 /* Authentication data follows */99#define SPX_REJECT 1 /* Rejected (reason might follow) */100#define SPX_ACCEPT 2 /* Accepted */101102static des_key_schedule sched;103static des_cblock challenge = { 0 };104105106/*******************************************************************/107108gss_OID_set actual_mechs;109gss_OID actual_mech_type, output_name_type;110int major_status, status, msg_ctx = 0, new_status;111int req_flags = 0, ret_flags, lifetime_rec;112gss_cred_id_t gss_cred_handle;113gss_ctx_id_t actual_ctxhandle, context_handle;114gss_buffer_desc output_token, input_token, input_name_buffer;115gss_buffer_desc status_string;116gss_name_t desired_targname, src_name;117gss_channel_bindings input_chan_bindings;118char lhostname[GSS_C_MAX_PRINTABLE_NAME];119char targ_printable[GSS_C_MAX_PRINTABLE_NAME];120int to_addr=0, from_addr=0;121char *address;122gss_buffer_desc fullname_buffer;123gss_OID fullname_type;124gss_cred_id_t gss_delegated_cred_handle;125126/*******************************************************************/127128129130static int131Data(ap, type, d, c)132Authenticator *ap;133int type;134void *d;135int c;136{137unsigned char *p = str_data + 4;138unsigned char *cd = (unsigned char *)d;139140if (c == -1)141c = strlen((char *)cd);142143if (0) {144printf("%s:%d: [%d] (%d)",145str_data[3] == TELQUAL_IS ? ">>>IS" : ">>>REPLY",146str_data[3],147type, c);148printd(d, c);149printf("\r\n");150}151*p++ = ap->type;152*p++ = ap->way;153*p++ = type;154while (c-- > 0) {155if ((*p++ = *cd++) == IAC)156*p++ = IAC;157}158*p++ = IAC;159*p++ = SE;160if (str_data[3] == TELQUAL_IS)161printsub('>', &str_data[2], p - (&str_data[2]));162return(telnet_net_write(str_data, p - str_data));163}164165int166spx_init(ap, server)167Authenticator *ap;168int server;169{170gss_cred_id_t tmp_cred_handle;171172if (server) {173str_data[3] = TELQUAL_REPLY;174gethostname(lhostname, sizeof(lhostname));175snprintf (targ_printable, sizeof(targ_printable),176"SERVICE:rcmd@%s", lhostname);177input_name_buffer.length = strlen(targ_printable);178input_name_buffer.value = targ_printable;179major_status = gss_import_name(&status,180&input_name_buffer,181GSS_C_NULL_OID,182&desired_targname);183major_status = gss_acquire_cred(&status,184desired_targname,1850,186GSS_C_NULL_OID_SET,187GSS_C_ACCEPT,188&tmp_cred_handle,189&actual_mechs,190&lifetime_rec);191if (major_status != GSS_S_COMPLETE) return(0);192} else {193str_data[3] = TELQUAL_IS;194}195return(1);196}197198int199spx_send(ap)200Authenticator *ap;201{202des_cblock enckey;203int r;204205gss_OID actual_mech_type, output_name_type;206int msg_ctx = 0, new_status, status;207int req_flags = 0, ret_flags, lifetime_rec, major_status;208gss_buffer_desc output_token, input_token, input_name_buffer;209gss_buffer_desc output_name_buffer, status_string;210gss_name_t desired_targname;211gss_channel_bindings input_chan_bindings;212char targ_printable[GSS_C_MAX_PRINTABLE_NAME];213int from_addr=0, to_addr=0, myhostlen, j;214int deleg_flag=1, mutual_flag=0, replay_flag=0, seq_flag=0;215char *address;216217printf("[ Trying SPX ... ]\r\n");218snprintf (targ_printable, sizeof(targ_printable),219"SERVICE:rcmd@%s", RemoteHostName);220221input_name_buffer.length = strlen(targ_printable);222input_name_buffer.value = targ_printable;223224if (!UserNameRequested) {225return(0);226}227228major_status = gss_import_name(&status,229&input_name_buffer,230GSS_C_NULL_OID,231&desired_targname);232233234major_status = gss_display_name(&status,235desired_targname,236&output_name_buffer,237&output_name_type);238239printf("target is '%.*s'\n", (int)output_name_buffer.length,240(char*)output_name_buffer.value);241fflush(stdout);242243major_status = gss_release_buffer(&status, &output_name_buffer);244245input_chan_bindings = (gss_channel_bindings)246malloc(sizeof(gss_channel_bindings_desc));247248input_chan_bindings->initiator_addrtype = GSS_C_AF_INET;249input_chan_bindings->initiator_address.length = 4;250address = (char *) malloc(4);251input_chan_bindings->initiator_address.value = (char *) address;252address[0] = ((from_addr & 0xff000000) >> 24);253address[1] = ((from_addr & 0xff0000) >> 16);254address[2] = ((from_addr & 0xff00) >> 8);255address[3] = (from_addr & 0xff);256input_chan_bindings->acceptor_addrtype = GSS_C_AF_INET;257input_chan_bindings->acceptor_address.length = 4;258address = (char *) malloc(4);259input_chan_bindings->acceptor_address.value = (char *) address;260address[0] = ((to_addr & 0xff000000) >> 24);261address[1] = ((to_addr & 0xff0000) >> 16);262address[2] = ((to_addr & 0xff00) >> 8);263address[3] = (to_addr & 0xff);264input_chan_bindings->application_data.length = 0;265266req_flags = 0;267if (deleg_flag) req_flags = req_flags | 1;268if (mutual_flag) req_flags = req_flags | 2;269if (replay_flag) req_flags = req_flags | 4;270if (seq_flag) req_flags = req_flags | 8;271272major_status = gss_init_sec_context(&status, /* minor status */273GSS_C_NO_CREDENTIAL, /* cred handle */274&actual_ctxhandle, /* ctx handle */275desired_targname, /* target name */276GSS_C_NULL_OID, /* mech type */277req_flags, /* req flags */2780, /* time req */279input_chan_bindings, /* chan binding */280GSS_C_NO_BUFFER, /* input token */281&actual_mech_type, /* actual mech */282&output_token, /* output token */283&ret_flags, /* ret flags */284&lifetime_rec); /* time rec */285286if ((major_status != GSS_S_COMPLETE) &&287(major_status != GSS_S_CONTINUE_NEEDED)) {288gss_display_status(&new_status,289status,290GSS_C_MECH_CODE,291GSS_C_NULL_OID,292&msg_ctx,293&status_string);294printf("%.*s\n", (int)status_string.length,295(char*)status_string.value);296return(0);297}298299if (!auth_sendname(UserNameRequested, strlen(UserNameRequested))) {300return(0);301}302303if (!Data(ap, SPX_AUTH, output_token.value, output_token.length)) {304return(0);305}306307return(1);308}309310void311spx_is(ap, data, cnt)312Authenticator *ap;313unsigned char *data;314int cnt;315{316Session_Key skey;317des_cblock datablock;318int r;319320if (cnt-- < 1)321return;322switch (*data++) {323case SPX_AUTH:324input_token.length = cnt;325input_token.value = (char *) data;326327gethostname(lhostname, sizeof(lhostname));328329snprintf(targ_printable, sizeof(targ_printable),330"SERVICE:rcmd@%s", lhostname);331332input_name_buffer.length = strlen(targ_printable);333input_name_buffer.value = targ_printable;334335major_status = gss_import_name(&status,336&input_name_buffer,337GSS_C_NULL_OID,338&desired_targname);339340major_status = gss_acquire_cred(&status,341desired_targname,3420,343GSS_C_NULL_OID_SET,344GSS_C_ACCEPT,345&gss_cred_handle,346&actual_mechs,347&lifetime_rec);348349major_status = gss_release_name(&status, desired_targname);350351input_chan_bindings = (gss_channel_bindings)352malloc(sizeof(gss_channel_bindings_desc));353354input_chan_bindings->initiator_addrtype = GSS_C_AF_INET;355input_chan_bindings->initiator_address.length = 4;356address = (char *) malloc(4);357input_chan_bindings->initiator_address.value = (char *) address;358address[0] = ((from_addr & 0xff000000) >> 24);359address[1] = ((from_addr & 0xff0000) >> 16);360address[2] = ((from_addr & 0xff00) >> 8);361address[3] = (from_addr & 0xff);362input_chan_bindings->acceptor_addrtype = GSS_C_AF_INET;363input_chan_bindings->acceptor_address.length = 4;364address = (char *) malloc(4);365input_chan_bindings->acceptor_address.value = (char *) address;366address[0] = ((to_addr & 0xff000000) >> 24);367address[1] = ((to_addr & 0xff0000) >> 16);368address[2] = ((to_addr & 0xff00) >> 8);369address[3] = (to_addr & 0xff);370input_chan_bindings->application_data.length = 0;371372major_status = gss_accept_sec_context(&status,373&context_handle,374gss_cred_handle,375&input_token,376input_chan_bindings,377&src_name,378&actual_mech_type,379&output_token,380&ret_flags,381&lifetime_rec,382&gss_delegated_cred_handle);383384385if (major_status != GSS_S_COMPLETE) {386387major_status = gss_display_name(&status,388src_name,389&fullname_buffer,390&fullname_type);391Data(ap, SPX_REJECT, "auth failed", -1);392auth_finished(ap, AUTH_REJECT);393return;394}395396major_status = gss_display_name(&status,397src_name,398&fullname_buffer,399&fullname_type);400401402Data(ap, SPX_ACCEPT, output_token.value, output_token.length);403auth_finished(ap, AUTH_USER);404break;405406default:407Data(ap, SPX_REJECT, 0, 0);408break;409}410}411412413void414spx_reply(ap, data, cnt)415Authenticator *ap;416unsigned char *data;417int cnt;418{419Session_Key skey;420421if (cnt-- < 1)422return;423switch (*data++) {424case SPX_REJECT:425if (cnt > 0) {426printf("[ SPX refuses authentication because %.*s ]\r\n",427cnt, data);428} else429printf("[ SPX refuses authentication ]\r\n");430auth_send_retry();431return;432case SPX_ACCEPT:433printf("[ SPX accepts you ]\r\n");434if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) {435/*436* Send over the encrypted challenge.437*/438input_token.value = (char *) data;439input_token.length = cnt;440441major_status = gss_init_sec_context(&status, /* minor stat */442GSS_C_NO_CREDENTIAL, /* cred handle */443&actual_ctxhandle, /* ctx handle */444desired_targname, /* target name */445GSS_C_NULL_OID, /* mech type */446req_flags, /* req flags */4470, /* time req */448input_chan_bindings, /* chan binding */449&input_token, /* input token */450&actual_mech_type, /* actual mech */451&output_token, /* output token */452&ret_flags, /* ret flags */453&lifetime_rec); /* time rec */454455if (major_status != GSS_S_COMPLETE) {456gss_display_status(&new_status,457status,458GSS_C_MECH_CODE,459GSS_C_NULL_OID,460&msg_ctx,461&status_string);462printf("[ SPX mutual response fails ... '%.*s' ]\r\n",463(int)status_string.length,464(char*)status_string.value);465auth_send_retry();466return;467}468}469auth_finished(ap, AUTH_USER);470return;471472default:473return;474}475}476477int478spx_status(ap, name, name_sz, level)479Authenticator *ap;480char *name;481size_t name_sz;482int level;483{484485gss_buffer_desc fullname_buffer, acl_file_buffer;486gss_OID fullname_type;487char acl_file[160], fullname[160];488int major_status, status = 0;489struct passwd *pwd;490491/*492* hard code fullname to493* "SPX:/C=US/O=Digital/OU=LKG/OU=Sphinx/OU=Users/CN=Kannan Alagappan"494* and acl_file to "~kannan/.sphinx"495*/496497pwd = k_getpwnam(UserNameRequested);498if (pwd == NULL) {499return(AUTH_USER); /* not authenticated */500}501502snprintf (acl_file, sizeof(acl_file),503"%s/.sphinx", pwd->pw_dir);504505acl_file_buffer.value = acl_file;506acl_file_buffer.length = strlen(acl_file);507508major_status = gss_display_name(&status,509src_name,510&fullname_buffer,511&fullname_type);512513if (level < AUTH_USER)514return(level);515516major_status = gss__check_acl(&status, &fullname_buffer,517&acl_file_buffer);518519if (major_status == GSS_S_COMPLETE) {520strlcpy(name, UserNameRequested, name_sz);521return(AUTH_VALID);522} else {523return(AUTH_USER);524}525526}527528#define BUMP(buf, len) while (*(buf)) {++(buf), --(len);}529#define ADDC(buf, len, c) if ((len) > 0) {*(buf)++ = (c); --(len);}530531void532spx_printsub(unsigned char *data, size_t cnt,533unsigned char *buf, size_t buflen)534{535size_t i;536537buf[buflen-1] = '\0'; /* make sure it's NULL terminated */538buflen -= 1;539540switch(data[3]) {541case SPX_REJECT: /* Rejected (reason might follow) */542strlcpy((char *)buf, " REJECT ", buflen);543goto common;544545case SPX_ACCEPT: /* Accepted (name might follow) */546strlcpy((char *)buf, " ACCEPT ", buflen);547common:548BUMP(buf, buflen);549if (cnt <= 4)550break;551ADDC(buf, buflen, '"');552for (i = 4; i < cnt; i++)553ADDC(buf, buflen, data[i]);554ADDC(buf, buflen, '"');555ADDC(buf, buflen, '\0');556break;557558case SPX_AUTH: /* Authentication data follows */559strlcpy((char *)buf, " AUTH", buflen);560goto common2;561562default:563snprintf(buf, buflen, " %d (unknown)", data[3]);564common2:565BUMP(buf, buflen);566for (i = 4; i < cnt; i++) {567snprintf(buf, buflen, " %d", data[i]);568BUMP(buf, buflen);569}570break;571}572}573574#endif575576#ifdef notdef577578prkey(msg, key)579char *msg;580unsigned char *key;581{582int i;583printf("%s:", msg);584for (i = 0; i < 8; i++)585printf(" %3d", key[i]);586printf("\r\n");587}588#endif589590591