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. 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.
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
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 $
35 * COPYRIGHT (C) 1990 DIGITAL EQUIPMENT CORPORATION
38 * "Digital Equipment Corporation authorizes the reproduction,
39 * distribution and modification of this software subject to the following
42 * 1. Any partial or whole copy of this software, or any modification
43 * thereof, must include this copyright notice in its entirety.
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.
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.
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.
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"
67 #include <sys/types.h>
68 #include <openssl/des.h>
69 #include <arpa/telnet.h>
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 *);
83 extern auth_debug_mode
;
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
,
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 */
96 #define KRB_SERVICE_NAME "rcmd"
99 static char name
[ANAME_SZ
];
100 static char user_passwd
[ANAME_SZ
];
101 static AUTH_DAT adat
= { 0 };
103 static Block session_key
= { 0 };
104 #endif /* ENCRYPTION */
105 static char challenge
[REALM_SZ
];
114 unsigned char *p
= str_data
+ 4;
115 unsigned char *cd
= (unsigned char *)d
;
118 c
= strlen((char *)cd
);
121 printf("%s:%d: [%d] (%d)",
122 str_data
[3] == TELQUAL_IS
? ">>>IS" : ">>>REPLY",
132 if ((*p
++ = *cd
++) == IAC
)
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
));
143 krb4encpwd_init(ap
, server
)
147 char hostname
[80], *cp
, *realm
;
151 str_data
[3] = TELQUAL_REPLY
;
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
)) {
171 printf("[ Trying KRB4ENCPWD ... ]\n");
172 if (!UserNameRequested
) {
175 if (!auth_sendname(UserNameRequested
, strlen(UserNameRequested
))) {
179 if (!Data(ap
, KRB4_ENCPWD_ACK
, NULL
, 0)) {
187 krb4encpwd_is(ap
, data
, cnt
)
192 char r_passwd
[ANAME_SZ
], r_user
[ANAME_SZ
];
193 char lhostname
[ANAME_SZ
], *cp
;
200 case KRB4_ENCPWD_AUTH
:
201 memmove((void *)auth
.dat
, (void *)data
, auth
.length
= cnt
);
203 gethostname(lhostname
, sizeof(lhostname
));
204 if ((cp
= strchr(lhostname
, '.')) != NULL
) *cp
= '\0';
206 if (r
= krb_rd_encpwd_req(&auth
, KRB_SERVICE_NAME
, lhostname
, 0, &adat
, NULL
, challenge
, r_user
, r_passwd
)) {
207 Data(ap
, KRB4_ENCPWD_REJECT
, (void *)"Auth failed", -1);
208 auth_finished(ap
, AUTH_REJECT
);
211 auth_encrypt_userpwd(r_passwd
);
212 if (passwdok(UserNameRequested
, UserPassword
) == 0) {
214 * illegal username and password
216 Data(ap
, KRB4_ENCPWD_REJECT
, (void *)"Illegal password", -1);
217 auth_finished(ap
, AUTH_REJECT
);
221 memmove((void *)session_key
, (void *)adat
.session
, sizeof(Block
));
222 Data(ap
, KRB4_ENCPWD_ACCEPT
, (void *)0, 0);
223 auth_finished(ap
, AUTH_USER
);
226 case KRB4_ENCPWD_CHALLENGE
:
228 * Take the received random challenge text and save
229 * for future authentication.
231 memmove((void *)challenge
, (void *)data
, sizeof(Block
));
235 case KRB4_ENCPWD_ACK
:
237 * Receive ack, if mutual then send random challenge
241 * If we are doing mutual authentication, get set up to send
242 * the challenge, and verify it when the response comes back.
245 if ((ap
->way
& AUTH_HOW_MASK
) == AUTH_HOW_MUTUAL
) {
247 sprintf(challenge
, "%x", now
);
248 Data(ap
, KRB4_ENCPWD_CHALLENGE
, (void *)challenge
, strlen(challenge
));
253 Data(ap
, KRB4_ENCPWD_REJECT
, 0, 0);
260 krb4encpwd_reply(ap
, data
, cnt
)
267 char instance
[ANAME_SZ
], *cp
;
268 char hostname
[80], *realm
;
273 case KRB4_ENCPWD_REJECT
:
275 printf("[ KRB4_ENCPWD refuses authentication because %.*s ]\r\n",
278 printf("[ KRB4_ENCPWD refuses authentication ]\r\n");
281 case KRB4_ENCPWD_ACCEPT
:
282 printf("[ KRB4_ENCPWD accepts you ]\n");
283 auth_finished(ap
, AUTH_USER
);
285 case KRB4_ENCPWD_CHALLENGE
:
287 * Verify that the response to the challenge is correct.
290 gethostname(hostname
, sizeof(hostname
));
291 realm
= krb_realmofhost(hostname
);
292 memmove((void *)challenge
, (void *)data
, cnt
);
293 memset(user_passwd
, 0, sizeof(user_passwd
));
294 local_des_read_pw_string(user_passwd
, sizeof(user_passwd
)-1, "Password: ", 0);
295 UserPassword
= user_passwd
;
296 Challenge
= challenge
;
297 strcpy(instance
, RemoteHostName
);
298 if ((cp
= strchr(instance
, '.')) != NULL
) *cp
= '\0';
300 if (r
= krb_mk_encpwd_req(&krb_token
, KRB_SERVICE_NAME
, instance
, realm
, Challenge
, UserNameRequested
, user_passwd
)) {
301 krb_token
.length
= 0;
304 if (!Data(ap
, KRB4_ENCPWD_AUTH
, (void *)krb_token
.dat
, krb_token
.length
)) {
316 krb4encpwd_status(ap
, name
, level
)
322 if (level
< AUTH_USER
)
325 if (UserNameRequested
&& passwdok(UserNameRequested
, UserPassword
)) {
326 strcpy(name
, UserNameRequested
);
333 #define BUMP(buf, len) while (*(buf)) {++(buf), --(len);}
334 #define ADDC(buf, len, c) if ((len) > 0) {*(buf)++ = (c); --(len);}
337 krb4encpwd_printsub(data
, cnt
, buf
, buflen
)
338 unsigned char *data
, *buf
;
344 buf
[buflen
-1] = '\0'; /* make sure its NULL terminated */
348 case KRB4_ENCPWD_REJECT
: /* Rejected (reason might follow) */
349 strncpy((char *)buf
, " REJECT ", buflen
);
352 case KRB4_ENCPWD_ACCEPT
: /* Accepted (name might follow) */
353 strncpy((char *)buf
, " ACCEPT ", buflen
);
358 ADDC(buf
, buflen
, '"');
359 for (i
= 4; i
< cnt
; i
++)
360 ADDC(buf
, buflen
, data
[i
]);
361 ADDC(buf
, buflen
, '"');
362 ADDC(buf
, buflen
, '\0');
365 case KRB4_ENCPWD_AUTH
: /* Authentication data follows */
366 strncpy((char *)buf
, " AUTH", buflen
);
369 case KRB4_ENCPWD_CHALLENGE
:
370 strncpy((char *)buf
, " CHALLENGE", buflen
);
373 case KRB4_ENCPWD_ACK
:
374 strncpy((char *)buf
, " ACK", buflen
);
378 sprintf(lbuf
, " %d (unknown)", data
[3]);
379 strncpy((char *)buf
, lbuf
, buflen
);
382 for (i
= 4; i
< cnt
; i
++) {
383 sprintf(lbuf
, " %d", data
[i
]);
384 strncpy((char *)buf
, lbuf
, buflen
);
391 int passwdok(name
, passwd
)
397 int passwdok_status
= 0;
399 if (pwd
= getpwnam(name
))
400 salt
= pwd
->pw_passwd
;
403 p
= crypt(passwd
, salt
);
405 if (pwd
&& !strcmp(p
, pwd
->pw_passwd
)) {
407 } else passwdok_status
= 0;
408 return(passwdok_status
);