2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: src/contrib/telnet/libtelnet/krb4encpwd.c,v 1.7 2001/11/30 22:28:07 markm Exp $");
39 static char sccsid
[] = "@(#)krb4encpwd.c 8.3 (Berkeley) 5/30/95";
45 * COPYRIGHT (C) 1990 DIGITAL EQUIPMENT CORPORATION
48 * "Digital Equipment Corporation authorizes the reproduction,
49 * distribution and modification of this software subject to the following
52 * 1. Any partial or whole copy of this software, or any modification
53 * thereof, must include this copyright notice in its entirety.
55 * 2. This software is supplied "as is" with no warranty of any kind,
56 * expressed or implied, for any purpose, including any warranty of fitness
57 * or merchantibility. DIGITAL assumes no responsibility for the use or
58 * reliability of this software, nor promises to provide any form of
59 * support for it on any basis.
61 * 3. Distribution of this software is authorized only if no profit or
62 * remuneration of any kind is received in exchange for such distribution.
64 * 4. This software produces public key authentication certificates
65 * bearing an expiration date established by DIGITAL and RSA Data
66 * Security, Inc. It may cease to generate certificates after the expiration
67 * date. Any modification of this software that changes or defeats
68 * the expiration date or its effect is unauthorized.
70 * 5. Software that will renew or extend the expiration date of
71 * authentication certificates produced by this software may be obtained
72 * from RSA Data Security, Inc., 10 Twin Dolphin Drive, Redwood City, CA
73 * 94065, (415)595-8782, or from DIGITAL"
77 #include <sys/types.h>
78 #include <openssl/des.h>
79 #include <arpa/telnet.h>
90 int krb_mk_encpwd_req(KTEXT
, char *, char *, char *, char *, char *, char *);
91 int krb_rd_encpwd_req(KTEXT
, char *, char *, u_long
, AUTH_DAT
*, char *, char *, char *, char *);
93 extern auth_debug_mode
;
95 static unsigned char str_data
[1024] = { IAC
, SB
, TELOPT_AUTHENTICATION
, 0,
96 AUTHTYPE_KRB4_ENCPWD
, };
97 static unsigned char str_name
[1024] = { IAC
, SB
, TELOPT_AUTHENTICATION
,
100 #define KRB4_ENCPWD_AUTH 0 /* Authentication data follows */
101 #define KRB4_ENCPWD_REJECT 1 /* Rejected (reason might follow) */
102 #define KRB4_ENCPWD_ACCEPT 2 /* Accepted */
103 #define KRB4_ENCPWD_CHALLENGE 3 /* Challenge for mutual auth. */
104 #define KRB4_ENCPWD_ACK 4 /* Acknowledge */
106 #define KRB_SERVICE_NAME "rcmd"
108 static KTEXT_ST auth
;
109 static char name
[ANAME_SZ
];
110 static char user_passwd
[ANAME_SZ
];
111 static AUTH_DAT adat
= { 0 };
113 static Block session_key
= { 0 };
114 #endif /* ENCRYPTION */
115 static char challenge
[REALM_SZ
];
124 unsigned char *p
= str_data
+ 4;
125 unsigned char *cd
= (unsigned char *)d
;
128 c
= strlen((char *)cd
);
131 printf("%s:%d: [%d] (%d)",
132 str_data
[3] == TELQUAL_IS
? ">>>IS" : ">>>REPLY",
142 if ((*p
++ = *cd
++) == IAC
)
147 if (str_data
[3] == TELQUAL_IS
)
148 printsub('>', &str_data
[2], p
- (&str_data
[2]));
149 return(net_write(str_data
, p
- str_data
));
153 krb4encpwd_init(ap
, server
)
157 char hostname
[80], *cp
, *realm
;
161 str_data
[3] = TELQUAL_REPLY
;
163 str_data
[3] = TELQUAL_IS
;
164 gethostname(hostname
, sizeof(hostname
));
165 realm
= krb_realmofhost(hostname
);
166 cp
= strchr(hostname
, '.');
167 if (*cp
!= NULL
) *cp
= NULL
;
168 if (read_service_key(KRB_SERVICE_NAME
, hostname
, realm
, 0,
169 KEYFILE
, (char *)skey
)) {
181 printf("[ Trying KRB4ENCPWD ... ]\n");
182 if (!UserNameRequested
) {
185 if (!auth_sendname(UserNameRequested
, strlen(UserNameRequested
))) {
189 if (!Data(ap
, KRB4_ENCPWD_ACK
, (void *)NULL
, 0)) {
197 krb4encpwd_is(ap
, data
, cnt
)
204 char r_passwd
[ANAME_SZ
], r_user
[ANAME_SZ
];
205 char lhostname
[ANAME_SZ
], *cp
;
212 case KRB4_ENCPWD_AUTH
:
213 memmove((void *)auth
.dat
, (void *)data
, auth
.length
= cnt
);
215 gethostname(lhostname
, sizeof(lhostname
));
216 if ((cp
= strchr(lhostname
, '.')) != 0) *cp
= '\0';
218 if (r
= krb_rd_encpwd_req(&auth
, KRB_SERVICE_NAME
, lhostname
, 0, &adat
, NULL
, challenge
, r_user
, r_passwd
)) {
219 Data(ap
, KRB4_ENCPWD_REJECT
, (void *)"Auth failed", -1);
220 auth_finished(ap
, AUTH_REJECT
);
223 auth_encrypt_userpwd(r_passwd
);
224 if (passwdok(UserNameRequested
, UserPassword
) == 0) {
226 * illegal username and password
228 Data(ap
, KRB4_ENCPWD_REJECT
, (void *)"Illegal password", -1);
229 auth_finished(ap
, AUTH_REJECT
);
233 memmove((void *)session_key
, (void *)adat
.session
, sizeof(Block
));
234 Data(ap
, KRB4_ENCPWD_ACCEPT
, (void *)0, 0);
235 auth_finished(ap
, AUTH_USER
);
238 case KRB4_ENCPWD_CHALLENGE
:
240 * Take the received random challenge text and save
241 * for future authentication.
243 memmove((void *)challenge
, (void *)data
, sizeof(Block
));
247 case KRB4_ENCPWD_ACK
:
249 * Receive ack, if mutual then send random challenge
253 * If we are doing mutual authentication, get set up to send
254 * the challenge, and verify it when the response comes back.
257 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) {
261 sprintf(challenge
, "%x", now
);
262 Data(ap
, KRB4_ENCPWD_CHALLENGE
, (void *)challenge
, strlen(challenge
));
267 Data(ap
, KRB4_ENCPWD_REJECT
, 0, 0);
274 krb4encpwd_reply(ap
, data
, cnt
)
284 char randchal
[REALM_SZ
], instance
[ANAME_SZ
], *cp
;
285 char hostname
[80], *realm
;
290 case KRB4_ENCPWD_REJECT
:
292 printf("[ KRB4_ENCPWD refuses authentication because %.*s ]\r\n",
295 printf("[ KRB4_ENCPWD refuses authentication ]\r\n");
298 case KRB4_ENCPWD_ACCEPT
:
299 printf("[ KRB4_ENCPWD accepts you ]\n");
300 auth_finished(ap
, AUTH_USER
);
302 case KRB4_ENCPWD_CHALLENGE
:
304 * Verify that the response to the challenge is correct.
307 gethostname(hostname
, sizeof(hostname
));
308 realm
= krb_realmofhost(hostname
);
309 memmove((void *)challenge
, (void *)data
, cnt
);
310 memset(user_passwd
, 0, sizeof(user_passwd
));
311 local_des_read_pw_string(user_passwd
, sizeof(user_passwd
)-1, "Password: ", 0);
312 UserPassword
= user_passwd
;
313 Challenge
= challenge
;
314 strcpy(instance
, RemoteHostName
);
315 if ((cp
= strchr(instance
, '.')) != 0) *cp
= '\0';
317 if (r
= krb_mk_encpwd_req(&krb_token
, KRB_SERVICE_NAME
, instance
, realm
, Challenge
, UserNameRequested
, user_passwd
)) {
318 krb_token
.length
= 0;
321 if (!Data(ap
, KRB4_ENCPWD_AUTH
, (void *)krb_token
.dat
, krb_token
.length
)) {
333 krb4encpwd_status(ap
, name
, level
)
339 if (level
< AUTH_USER
)
342 if (UserNameRequested
&& passwdok(UserNameRequested
, UserPassword
)) {
343 strcpy(name
, UserNameRequested
);
350 #define BUMP(buf, len) while (*(buf)) {++(buf), --(len);}
351 #define ADDC(buf, len, c) if ((len) > 0) {*(buf)++ = (c); --(len);}
354 krb4encpwd_printsub(data
, cnt
, buf
, buflen
)
355 unsigned char *data
, *buf
;
361 buf
[buflen
-1] = '\0'; /* make sure its NULL terminated */
365 case KRB4_ENCPWD_REJECT
: /* Rejected (reason might follow) */
366 strncpy((char *)buf
, " REJECT ", buflen
);
369 case KRB4_ENCPWD_ACCEPT
: /* Accepted (name might follow) */
370 strncpy((char *)buf
, " ACCEPT ", buflen
);
375 ADDC(buf
, buflen
, '"');
376 for (i
= 4; i
< cnt
; i
++)
377 ADDC(buf
, buflen
, data
[i
]);
378 ADDC(buf
, buflen
, '"');
379 ADDC(buf
, buflen
, '\0');
382 case KRB4_ENCPWD_AUTH
: /* Authentication data follows */
383 strncpy((char *)buf
, " AUTH", buflen
);
386 case KRB4_ENCPWD_CHALLENGE
:
387 strncpy((char *)buf
, " CHALLENGE", buflen
);
390 case KRB4_ENCPWD_ACK
:
391 strncpy((char *)buf
, " ACK", buflen
);
395 sprintf(lbuf
, " %d (unknown)", data
[3]);
396 strncpy((char *)buf
, lbuf
, buflen
);
399 for (i
= 4; i
< cnt
; i
++) {
400 sprintf(lbuf
, " %d", data
[i
]);
401 strncpy((char *)buf
, lbuf
, buflen
);
408 int passwdok(name
, passwd
)
414 int passwdok_status
= 0;
416 if (pwd
= getpwnam(name
))
417 salt
= pwd
->pw_passwd
;
420 p
= crypt(passwd
, salt
);
422 if (pwd
&& !strcmp(p
, pwd
->pw_passwd
)) {
424 } else passwdok_status
= 0;
425 return(passwdok_status
);