No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / appl / telnet / libtelnet / krb4encpwd.c
blobf37f1a9ce78342f75175bd2c97ac1342e980ace7
1 /*-
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
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.
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.
34 #include <config.h>
36 __RCSID("$Heimdal: krb4encpwd.c 22071 2007-11-14 20:04:50Z lha $"
37 "$NetBSD$");
39 #ifdef KRB4_ENCPWD
41 * COPYRIGHT (C) 1990 DIGITAL EQUIPMENT CORPORATION
42 * ALL RIGHTS RESERVED
44 * "Digital Equipment Corporation authorizes the reproduction,
45 * distribution and modification of this software subject to the following
46 * restrictions:
48 * 1. Any partial or whole copy of this software, or any modification
49 * thereof, must include this copyright notice in its entirety.
51 * 2. This software is supplied "as is" with no warranty of any kind,
52 * expressed or implied, for any purpose, including any warranty of fitness
53 * or merchantibility. DIGITAL assumes no responsibility for the use or
54 * reliability of this software, nor promises to provide any form of
55 * support for it on any basis.
57 * 3. Distribution of this software is authorized only if no profit or
58 * remuneration of any kind is received in exchange for such distribution.
60 * 4. This software produces public key authentication certificates
61 * bearing an expiration date established by DIGITAL and RSA Data
62 * Security, Inc. It may cease to generate certificates after the expiration
63 * date. Any modification of this software that changes or defeats
64 * the expiration date or its effect is unauthorized.
66 * 5. Software that will renew or extend the expiration date of
67 * authentication certificates produced by this software may be obtained
68 * from RSA Data Security, Inc., 10 Twin Dolphin Drive, Redwood City, CA
69 * 94065, (415)595-8782, or from DIGITAL"
73 #include <sys/types.h>
74 #include <arpa/telnet.h>
75 #include <pwd.h>
76 #include <stdio.h>
78 #include <krb.h>
79 #include <stdlib.h>
80 #include <string.h>
81 #ifdef SOCKS
82 #include <socks.h>
83 #endif
85 #include "encrypt.h"
86 #include "auth.h"
87 #include "misc.h"
89 int krb_mk_encpwd_req (KTEXT, char *, char *, char *, char *, char *, char *);
90 int krb_rd_encpwd_req (KTEXT, char *, char *, u_long, AUTH_DAT *, char *, char *, char *, char *);
92 extern auth_debug_mode;
94 static unsigned char str_data[1024] = { IAC, SB, TELOPT_AUTHENTICATION, 0,
95 AUTHTYPE_KRB4_ENCPWD, };
96 static unsigned char str_name[1024] = { IAC, SB, TELOPT_AUTHENTICATION,
97 TELQUAL_NAME, };
99 #define KRB4_ENCPWD_AUTH 0 /* Authentication data follows */
100 #define KRB4_ENCPWD_REJECT 1 /* Rejected (reason might follow) */
101 #define KRB4_ENCPWD_ACCEPT 2 /* Accepted */
102 #define KRB4_ENCPWD_CHALLENGE 3 /* Challenge for mutual auth. */
103 #define KRB4_ENCPWD_ACK 4 /* Acknowledge */
105 #define KRB_SERVICE_NAME "rcmd"
107 static KTEXT_ST auth;
108 static char name[ANAME_SZ];
109 static char user_passwd[ANAME_SZ];
110 static AUTH_DAT adat = { 0 };
111 static des_key_schedule sched;
112 static char challenge[REALM_SZ];
114 static int
115 Data(ap, type, d, c)
116 Authenticator *ap;
117 int type;
118 void *d;
119 int c;
121 unsigned char *p = str_data + 4;
122 unsigned char *cd = (unsigned char *)d;
124 if (c == -1)
125 c = strlen(cd);
127 if (0) {
128 printf("%s:%d: [%d] (%d)",
129 str_data[3] == TELQUAL_IS ? ">>>IS" : ">>>REPLY",
130 str_data[3],
131 type, c);
132 printd(d, c);
133 printf("\r\n");
135 *p++ = ap->type;
136 *p++ = ap->way;
137 *p++ = type;
138 while (c-- > 0) {
139 if ((*p++ = *cd++) == IAC)
140 *p++ = IAC;
142 *p++ = IAC;
143 *p++ = SE;
144 if (str_data[3] == TELQUAL_IS)
145 printsub('>', &str_data[2], p - (&str_data[2]));
146 return(telnet_net_write(str_data, p - str_data));
150 krb4encpwd_init(ap, server)
151 Authenticator *ap;
152 int server;
154 char hostname[80], *cp, *realm;
155 des_clock skey;
157 if (server) {
158 str_data[3] = TELQUAL_REPLY;
159 } else {
160 str_data[3] = TELQUAL_IS;
161 gethostname(hostname, sizeof(hostname));
162 realm = krb_realmofhost(hostname);
163 cp = strchr(hostname, '.');
164 if (*cp != NULL) *cp = NULL;
165 if (read_service_key(KRB_SERVICE_NAME, hostname, realm, 0,
166 KEYFILE, (char *)skey)) {
167 return(0);
170 return(1);
174 krb4encpwd_send(ap)
175 Authenticator *ap;
178 printf("[ Trying KRB4ENCPWD ... ]\r\n");
179 if (!UserNameRequested) {
180 return(0);
182 if (!auth_sendname(UserNameRequested, strlen(UserNameRequested))) {
183 return(0);
186 if (!Data(ap, KRB4_ENCPWD_ACK, NULL, 0)) {
187 return(0);
190 return(1);
193 void
194 krb4encpwd_is(ap, data, cnt)
195 Authenticator *ap;
196 unsigned char *data;
197 int cnt;
199 Session_Key skey;
200 des_cblock datablock;
201 char r_passwd[ANAME_SZ], r_user[ANAME_SZ];
202 char lhostname[ANAME_SZ], *cp;
203 int r;
204 time_t now;
206 if (cnt-- < 1)
207 return;
208 switch (*data++) {
209 case KRB4_ENCPWD_AUTH:
210 memmove(auth.dat, data, auth.length = cnt);
212 gethostname(lhostname, sizeof(lhostname));
213 if ((cp = strchr(lhostname, '.')) != 0) *cp = '\0';
215 if (r = krb_rd_encpwd_req(&auth, KRB_SERVICE_NAME, lhostname, 0, &adat, NULL, challenge, r_user, r_passwd)) {
216 Data(ap, KRB4_ENCPWD_REJECT, "Auth failed", -1);
217 auth_finished(ap, AUTH_REJECT);
218 return;
220 auth_encrypt_userpwd(r_passwd);
221 if (passwdok(UserNameRequested, UserPassword) == 0) {
223 * illegal username and password
225 Data(ap, KRB4_ENCPWD_REJECT, "Illegal password", -1);
226 auth_finished(ap, AUTH_REJECT);
227 return;
230 memmove(session_key, adat.session, sizeof(des_cblock));
231 Data(ap, KRB4_ENCPWD_ACCEPT, 0, 0);
232 auth_finished(ap, AUTH_USER);
233 break;
235 case KRB4_ENCPWD_CHALLENGE:
237 * Take the received random challenge text and save
238 * for future authentication.
240 memmove(challenge, data, sizeof(des_cblock));
241 break;
244 case KRB4_ENCPWD_ACK:
246 * Receive ack, if mutual then send random challenge
250 * If we are doing mutual authentication, get set up to send
251 * the challenge, and verify it when the response comes back.
254 if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) {
255 int i;
257 time(&now);
258 snprintf(challenge, sizeof(challenge), "%x", now);
259 Data(ap, KRB4_ENCPWD_CHALLENGE, challenge, strlen(challenge));
261 break;
263 default:
264 Data(ap, KRB4_ENCPWD_REJECT, 0, 0);
265 break;
270 void
271 krb4encpwd_reply(ap, data, cnt)
272 Authenticator *ap;
273 unsigned char *data;
274 int cnt;
276 Session_Key skey;
277 KTEXT_ST krb_token;
278 des_cblock enckey;
279 CREDENTIALS cred;
280 int r;
281 char randchal[REALM_SZ], instance[ANAME_SZ], *cp;
282 char hostname[80], *realm;
284 if (cnt-- < 1)
285 return;
286 switch (*data++) {
287 case KRB4_ENCPWD_REJECT:
288 if (cnt > 0) {
289 printf("[ KRB4_ENCPWD refuses authentication because %.*s ]\r\n",
290 cnt, data);
291 } else
292 printf("[ KRB4_ENCPWD refuses authentication ]\r\n");
293 auth_send_retry();
294 return;
295 case KRB4_ENCPWD_ACCEPT:
296 printf("[ KRB4_ENCPWD accepts you ]\r\n");
297 auth_finished(ap, AUTH_USER);
298 return;
299 case KRB4_ENCPWD_CHALLENGE:
301 * Verify that the response to the challenge is correct.
304 gethostname(hostname, sizeof(hostname));
305 realm = krb_realmofhost(hostname);
306 memmove(challenge, data, cnt);
307 memset(user_passwd, 0, sizeof(user_passwd));
308 des_read_pw_string(user_passwd, sizeof(user_passwd)-1, "Password: ", 0);
309 UserPassword = user_passwd;
310 Challenge = challenge;
311 strlcpy(instance, RemoteHostName, sizeof(instance));
312 if ((cp = strchr(instance, '.')) != 0) *cp = '\0';
314 if (r = krb_mk_encpwd_req(&krb_token, KRB_SERVICE_NAME, instance, realm, Challenge, UserNameRequested, user_passwd)) {
315 krb_token.length = 0;
318 if (!Data(ap, KRB4_ENCPWD_AUTH, krb_token.dat, krb_token.length)) {
319 return;
322 break;
324 default:
325 return;
330 krb4encpwd_status(ap, name, name_sz, level)
331 Authenticator *ap;
332 char *name;
333 size_t name_sz;
334 int level;
337 if (level < AUTH_USER)
338 return(level);
340 if (UserNameRequested && passwdok(UserNameRequested, UserPassword)) {
341 strlcpy(name, UserNameRequested, name_sz);
342 return(AUTH_VALID);
343 } else {
344 return(AUTH_USER);
348 #define BUMP(buf, len) while (*(buf)) {++(buf), --(len);}
349 #define ADDC(buf, len, c) if ((len) > 0) {*(buf)++ = (c); --(len);}
351 void
352 krb4encpwd_printsub(data, cnt, buf, buflen)
353 unsigned char *data, *buf;
354 int cnt, buflen;
356 int i;
358 buf[buflen-1] = '\0'; /* make sure it's NULL terminated */
359 buflen -= 1;
361 switch(data[3]) {
362 case KRB4_ENCPWD_REJECT: /* Rejected (reason might follow) */
363 strlcpy((char *)buf, " REJECT ", buflen);
364 goto common;
366 case KRB4_ENCPWD_ACCEPT: /* Accepted (name might follow) */
367 strlcpy((char *)buf, " ACCEPT ", buflen);
368 common:
369 BUMP(buf, buflen);
370 if (cnt <= 4)
371 break;
372 ADDC(buf, buflen, '"');
373 for (i = 4; i < cnt; i++)
374 ADDC(buf, buflen, data[i]);
375 ADDC(buf, buflen, '"');
376 ADDC(buf, buflen, '\0');
377 break;
379 case KRB4_ENCPWD_AUTH: /* Authentication data follows */
380 strlcpy((char *)buf, " AUTH", buflen);
381 goto common2;
383 case KRB4_ENCPWD_CHALLENGE:
384 strlcpy((char *)buf, " CHALLENGE", buflen);
385 goto common2;
387 case KRB4_ENCPWD_ACK:
388 strlcpy((char *)buf, " ACK", buflen);
389 goto common2;
391 default:
392 snprintf(buf, buflen, " %d (unknown)", data[3]);
393 common2:
394 BUMP(buf, buflen);
395 for (i = 4; i < cnt; i++) {
396 snprintf(buf, buflen, " %d", data[i]);
397 BUMP(buf, buflen);
399 break;
403 int passwdok(name, passwd)
404 char *name, *passwd;
406 char *crypt();
407 char *salt, *p;
408 struct passwd *pwd;
409 int passwdok_status = 0;
411 if (pwd = k_getpwnam(name))
412 salt = pwd->pw_passwd;
413 else salt = "xx";
415 p = crypt(passwd, salt);
417 if (pwd && !strcmp(p, pwd->pw_passwd)) {
418 passwdok_status = 1;
419 } else passwdok_status = 0;
420 return(passwdok_status);
423 #endif
425 #ifdef notdef
427 prkey(msg, key)
428 char *msg;
429 unsigned char *key;
431 int i;
432 printf("%s:", msg);
433 for (i = 0; i < 8; i++)
434 printf(" %3d", key[i]);
435 printf("\r\n");
437 #endif