1/*- 2 * Copyright (c) 1992, 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#include <sys/cdefs.h> 35 36#ifdef __FBSDID 37__FBSDID("$FreeBSD: src/crypto/telnet/libtelnet/krb4encpwd.c,v 1.3.2.1 2002/04/13 10:59:07 markm Exp $"); 38#endif 39 40#ifndef lint 41static const char sccsid[] = "@(#)krb4encpwd.c 8.3 (Berkeley) 5/30/95"; 42#endif /* not lint */ 43 44 45#ifdef KRB4_ENCPWD 46/* 47 * COPYRIGHT (C) 1990 DIGITAL EQUIPMENT CORPORATION 48 * ALL RIGHTS RESERVED 49 * 50 * "Digital Equipment Corporation authorizes the reproduction, 51 * distribution and modification of this software subject to the following 52 * restrictions: 53 * 54 * 1. Any partial or whole copy of this software, or any modification 55 * thereof, must include this copyright notice in its entirety. 56 * 57 * 2. This software is supplied "as is" with no warranty of any kind, 58 * expressed or implied, for any purpose, including any warranty of fitness 59 * or merchantibility. DIGITAL assumes no responsibility for the use or 60 * reliability of this software, nor promises to provide any form of 61 * support for it on any basis. 62 * 63 * 3. Distribution of this software is authorized only if no profit or 64 * remuneration of any kind is received in exchange for such distribution. 65 * 66 * 4. This software produces public key authentication certificates 67 * bearing an expiration date established by DIGITAL and RSA Data 68 * Security, Inc. It may cease to generate certificates after the expiration 69 * date. Any modification of this software that changes or defeats 70 * the expiration date or its effect is unauthorized. 71 * 72 * 5. Software that will renew or extend the expiration date of 73 * authentication certificates produced by this software may be obtained 74 * from RSA Data Security, Inc., 10 Twin Dolphin Drive, Redwood City, CA 75 * 94065, (415)595-8782, or from DIGITAL" 76 * 77 */ 78 79#include <sys/types.h> 80#include <openssl/des.h> 81#include <arpa/telnet.h> 82#include <krb.h> 83#include <pwd.h> 84#include <stdio.h> 85#include <stdlib.h> 86#include <string.h> 87 88#include "encrypt.h" 89#include "auth.h" 90#include "misc.h" 91 92int krb_mk_encpwd_req(KTEXT, char *, char *, char *, char *, char *, char *); 93int krb_rd_encpwd_req(KTEXT, char *, char *, u_long, AUTH_DAT *, char *, char *, char *, char *); 94 95extern auth_debug_mode; 96 97static unsigned char str_data[1024] = { IAC, SB, TELOPT_AUTHENTICATION, 0, 98 AUTHTYPE_KRB4_ENCPWD, }; 99static unsigned char str_name[1024] = { IAC, SB, TELOPT_AUTHENTICATION, 100 TELQUAL_NAME, }; 101 102#define KRB4_ENCPWD_AUTH 0 /* Authentication data follows */ 103#define KRB4_ENCPWD_REJECT 1 /* Rejected (reason might follow) */ 104#define KRB4_ENCPWD_ACCEPT 2 /* Accepted */ 105#define KRB4_ENCPWD_CHALLENGE 3 /* Challenge for mutual auth. */ 106#define KRB4_ENCPWD_ACK 4 /* Acknowledge */ 107 108#define KRB_SERVICE_NAME "rcmd" 109 110static KTEXT_ST auth; 111static char name[ANAME_SZ]; 112static char user_passwd[ANAME_SZ]; 113static AUTH_DAT adat = { 0 }; 114#ifdef ENCRYPTION 115static Block session_key = { 0 }; 116#endif /* ENCRYPTION */ 117static char challenge[REALM_SZ]; 118 119 static int 120Data(ap, type, d, c) 121 Authenticator *ap; 122 int type; 123 void *d; 124 int c; 125{ 126 unsigned char *p = str_data + 4; 127 unsigned char *cd = (unsigned char *)d; 128 129 if (c == -1) 130 c = strlen((char *)cd); 131 132 if (0) { 133 printf("%s:%d: [%d] (%d)", 134 str_data[3] == TELQUAL_IS ? ">>>IS" : ">>>REPLY", 135 str_data[3], 136 type, c); 137 printd(d, c); 138 printf("\r\n"); 139 } 140 *p++ = ap->type; 141 *p++ = ap->way; 142 *p++ = type; 143 while (c-- > 0) { 144 if ((*p++ = *cd++) == IAC) 145 *p++ = IAC; 146 } 147 *p++ = IAC; 148 *p++ = SE; 149 if (str_data[3] == TELQUAL_IS) 150 printsub('>', &str_data[2], p - (&str_data[2])); 151 return(net_write(str_data, p - str_data)); 152} 153 154 int 155krb4encpwd_init(ap, server) 156 Authenticator *ap; 157 int server; 158{ 159 char hostname[80], *cp, *realm; 160 C_Block skey; 161 162 if (server) { 163 str_data[3] = TELQUAL_REPLY; 164 } else { 165 str_data[3] = TELQUAL_IS; 166 gethostname(hostname, sizeof(hostname)); 167 realm = krb_realmofhost(hostname); 168 cp = strchr(hostname, '.'); 169 if (*cp != NULL) *cp = NULL; 170 if (read_service_key(KRB_SERVICE_NAME, hostname, realm, 0, 171 KEYFILE, (char *)skey)) { 172 return(0); 173 } 174 } 175 return(1); 176} 177 178 int 179krb4encpwd_send(ap) 180 Authenticator *ap; 181{ 182 183 printf("[ Trying KRB4ENCPWD ... ]\n"); 184 if (!UserNameRequested) { 185 return(0); 186 } 187 if (!auth_sendname(UserNameRequested, strlen(UserNameRequested))) { 188 return(0); 189 } 190 191 if (!Data(ap, KRB4_ENCPWD_ACK, (void *)NULL, 0)) { 192 return(0); 193 } 194 195 return(1); 196} 197 198 void 199krb4encpwd_is(ap, data, cnt) 200 Authenticator *ap; 201 unsigned char *data; 202 int cnt; 203{ 204 Session_Key skey; 205 Block datablock; 206 char r_passwd[ANAME_SZ], r_user[ANAME_SZ]; 207 char lhostname[ANAME_SZ], *cp; 208 int r; 209 time_t now; 210 211 if (cnt-- < 1) 212 return; 213 switch (*data++) { 214 case KRB4_ENCPWD_AUTH: 215 memmove((void *)auth.dat, (void *)data, auth.length = cnt); 216 217 gethostname(lhostname, sizeof(lhostname)); 218 if ((cp = strchr(lhostname, '.')) != 0) *cp = '\0'; 219 220 if (r = krb_rd_encpwd_req(&auth, KRB_SERVICE_NAME, lhostname, 0, &adat, NULL, challenge, r_user, r_passwd)) { 221 Data(ap, KRB4_ENCPWD_REJECT, (void *)"Auth failed", -1); 222 auth_finished(ap, AUTH_REJECT); 223 return; 224 } 225 auth_encrypt_userpwd(r_passwd); 226 if (passwdok(UserNameRequested, UserPassword) == 0) { 227 /* 228 * illegal username and password 229 */ 230 Data(ap, KRB4_ENCPWD_REJECT, (void *)"Illegal password", -1); 231 auth_finished(ap, AUTH_REJECT); 232 return; 233 } 234 235 memmove((void *)session_key, (void *)adat.session, sizeof(Block)); 236 Data(ap, KRB4_ENCPWD_ACCEPT, (void *)0, 0); 237 auth_finished(ap, AUTH_USER); 238 break; 239 240 case KRB4_ENCPWD_CHALLENGE: 241 /* 242 * Take the received random challenge text and save 243 * for future authentication. 244 */ 245 memmove((void *)challenge, (void *)data, sizeof(Block)); 246 break; 247 248 249 case KRB4_ENCPWD_ACK: 250 /* 251 * Receive ack, if mutual then send random challenge 252 */ 253 254 /* 255 * If we are doing mutual authentication, get set up to send 256 * the challenge, and verify it when the response comes back. 257 */ 258 259 if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) { 260 register int i; 261 262 time(&now); 263 sprintf(challenge, "%x", now); 264 Data(ap, KRB4_ENCPWD_CHALLENGE, (void *)challenge, strlen(challenge)); 265 } 266 break; 267 268 default: 269 Data(ap, KRB4_ENCPWD_REJECT, 0, 0); 270 break; 271 } 272} 273 274 275 void 276krb4encpwd_reply(ap, data, cnt) 277 Authenticator *ap; 278 unsigned char *data; 279 int cnt; 280{ 281 Session_Key skey; 282 KTEXT_ST krb_token; 283 Block enckey; 284 CREDENTIALS cred; 285 int r; 286 char randchal[REALM_SZ], instance[ANAME_SZ], *cp; 287 char hostname[80], *realm; 288 289 if (cnt-- < 1) 290 return; 291 switch (*data++) { 292 case KRB4_ENCPWD_REJECT: 293 if (cnt > 0) { 294 printf("[ KRB4_ENCPWD refuses authentication because %.*s ]\r\n", 295 cnt, data); 296 } else 297 printf("[ KRB4_ENCPWD refuses authentication ]\r\n"); 298 auth_send_retry(); 299 return; 300 case KRB4_ENCPWD_ACCEPT: 301 printf("[ KRB4_ENCPWD accepts you ]\n"); 302 auth_finished(ap, AUTH_USER); 303 return; 304 case KRB4_ENCPWD_CHALLENGE: 305 /* 306 * Verify that the response to the challenge is correct. 307 */ 308 309 gethostname(hostname, sizeof(hostname)); 310 realm = krb_realmofhost(hostname); 311 memmove((void *)challenge, (void *)data, cnt); 312 memset(user_passwd, 0, sizeof(user_passwd)); 313 local_des_read_pw_string(user_passwd, sizeof(user_passwd)-1, "Password: ", 0); 314 UserPassword = user_passwd; 315 Challenge = challenge; 316 strcpy(instance, RemoteHostName); 317 if ((cp = strchr(instance, '.')) != 0) *cp = '\0'; 318 319 if (r = krb_mk_encpwd_req(&krb_token, KRB_SERVICE_NAME, instance, realm, Challenge, UserNameRequested, user_passwd)) { 320 krb_token.length = 0; 321 } 322 323 if (!Data(ap, KRB4_ENCPWD_AUTH, (void *)krb_token.dat, krb_token.length)) { 324 return; 325 } 326 327 break; 328 329 default: 330 return; 331 } 332} 333 334 int 335krb4encpwd_status(ap, name, level) 336 Authenticator *ap; 337 char *name; 338 int level; 339{ 340 341 if (level < AUTH_USER) 342 return(level); 343 344 if (UserNameRequested && passwdok(UserNameRequested, UserPassword)) { 345 strcpy(name, UserNameRequested); 346 return(AUTH_VALID); 347 } else { 348 return(AUTH_USER); 349 } 350} 351 352#define BUMP(buf, len) while (*(buf)) {++(buf), --(len);} 353#define ADDC(buf, len, c) if ((len) > 0) {*(buf)++ = (c); --(len);} 354 355 void 356krb4encpwd_printsub(data, cnt, buf, buflen) 357 unsigned char *data, *buf; 358 int cnt, buflen; 359{ 360 char lbuf[32]; 361 register int i; 362 363 buf[buflen-1] = '\0'; /* make sure its NULL terminated */ 364 buflen -= 1; 365 366 switch(data[3]) { 367 case KRB4_ENCPWD_REJECT: /* Rejected (reason might follow) */ 368 strncpy((char *)buf, " REJECT ", buflen); 369 goto common; 370 371 case KRB4_ENCPWD_ACCEPT: /* Accepted (name might follow) */ 372 strncpy((char *)buf, " ACCEPT ", buflen); 373 common: 374 BUMP(buf, buflen); 375 if (cnt <= 4) 376 break; 377 ADDC(buf, buflen, '"'); 378 for (i = 4; i < cnt; i++) 379 ADDC(buf, buflen, data[i]); 380 ADDC(buf, buflen, '"'); 381 ADDC(buf, buflen, '\0'); 382 break; 383 384 case KRB4_ENCPWD_AUTH: /* Authentication data follows */ 385 strncpy((char *)buf, " AUTH", buflen); 386 goto common2; 387 388 case KRB4_ENCPWD_CHALLENGE: 389 strncpy((char *)buf, " CHALLENGE", buflen); 390 goto common2; 391 392 case KRB4_ENCPWD_ACK: 393 strncpy((char *)buf, " ACK", buflen); 394 goto common2; 395 396 default: 397 sprintf(lbuf, " %d (unknown)", data[3]); 398 strncpy((char *)buf, lbuf, buflen); 399 common2: 400 BUMP(buf, buflen); 401 for (i = 4; i < cnt; i++) { 402 sprintf(lbuf, " %d", data[i]); 403 strncpy((char *)buf, lbuf, buflen); 404 BUMP(buf, buflen); 405 } 406 break; 407 } 408} 409 410int passwdok(name, passwd) 411char *name, *passwd; 412{ 413 char *crypt(); 414 char *salt, *p; 415 struct passwd *pwd; 416 int passwdok_status = 0; 417 418 if (pwd = getpwnam(name)) 419 salt = pwd->pw_passwd; 420 else salt = "xx"; 421 422 p = crypt(passwd, salt); 423 424 if (pwd && !strcmp(p, pwd->pw_passwd)) { 425 passwdok_status = 1; 426 } else passwdok_status = 0; 427 return(passwdok_status); 428} 429 430#endif 431