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