Add vfs(7) manpage.
[dragonfly.git] / lib / libtelnet / krb4encpwd.c
blobcb22b8767eac68b70cc63902637326701dd19a97
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. 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
27 * SUCH DAMAGE.
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 $
33 #ifdef KRB4_ENCPWD
35 * COPYRIGHT (C) 1990 DIGITAL EQUIPMENT CORPORATION
36 * ALL RIGHTS RESERVED
38 * "Digital Equipment Corporation authorizes the reproduction,
39 * distribution and modification of this software subject to the following
40 * restrictions:
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>
70 #include <krb.h>
71 #include <pwd.h>
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <string.h>
76 #include "encrypt.h"
77 #include "auth.h"
78 #include "misc.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,
88 TELQUAL_NAME, };
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"
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];
107 static int
108 Data(ap, type, d, c)
109 Authenticator *ap;
110 int type;
111 void *d;
112 int c;
114 unsigned char *p = str_data + 4;
115 unsigned char *cd = (unsigned char *)d;
117 if (c == -1)
118 c = strlen((char *)cd);
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");
128 *p++ = ap->type;
129 *p++ = ap->way;
130 *p++ = type;
131 while (c-- > 0) {
132 if ((*p++ = *cd++) == IAC)
133 *p++ = IAC;
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));
143 krb4encpwd_init(ap, server)
144 Authenticator *ap;
145 int server;
147 char hostname[80], *cp, *realm;
148 C_Block skey;
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);
163 return(1);
167 krb4encpwd_send(ap)
168 Authenticator *ap;
171 printf("[ Trying KRB4ENCPWD ... ]\n");
172 if (!UserNameRequested) {
173 return(0);
175 if (!auth_sendname(UserNameRequested, strlen(UserNameRequested))) {
176 return(0);
179 if (!Data(ap, KRB4_ENCPWD_ACK, NULL, 0)) {
180 return(0);
183 return(1);
186 void
187 krb4encpwd_is(ap, data, cnt)
188 Authenticator *ap;
189 unsigned char *data;
190 int cnt;
192 char r_passwd[ANAME_SZ], r_user[ANAME_SZ];
193 char lhostname[ANAME_SZ], *cp;
194 int r;
195 time_t now;
197 if (cnt-- < 1)
198 return;
199 switch (*data++) {
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);
209 return;
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);
218 return;
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);
224 break;
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));
232 break;
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) {
246 time(&now);
247 sprintf(challenge, "%x", now);
248 Data(ap, KRB4_ENCPWD_CHALLENGE, (void *)challenge, strlen(challenge));
250 break;
252 default:
253 Data(ap, KRB4_ENCPWD_REJECT, 0, 0);
254 break;
259 void
260 krb4encpwd_reply(ap, data, cnt)
261 Authenticator *ap;
262 unsigned char *data;
263 int cnt;
265 KTEXT_ST krb_token;
266 int r;
267 char instance[ANAME_SZ], *cp;
268 char hostname[80], *realm;
270 if (cnt-- < 1)
271 return;
272 switch (*data++) {
273 case KRB4_ENCPWD_REJECT:
274 if (cnt > 0) {
275 printf("[ KRB4_ENCPWD refuses authentication because %.*s ]\r\n",
276 cnt, data);
277 } else
278 printf("[ KRB4_ENCPWD refuses authentication ]\r\n");
279 auth_send_retry();
280 return;
281 case KRB4_ENCPWD_ACCEPT:
282 printf("[ KRB4_ENCPWD accepts you ]\n");
283 auth_finished(ap, AUTH_USER);
284 return;
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)) {
305 return;
308 break;
310 default:
311 return;
316 krb4encpwd_status(ap, name, level)
317 Authenticator *ap;
318 char *name;
319 int level;
322 if (level < AUTH_USER)
323 return(level);
325 if (UserNameRequested && passwdok(UserNameRequested, UserPassword)) {
326 strcpy(name, UserNameRequested);
327 return(AUTH_VALID);
328 } else {
329 return(AUTH_USER);
333 #define BUMP(buf, len) while (*(buf)) {++(buf), --(len);}
334 #define ADDC(buf, len, c) if ((len) > 0) {*(buf)++ = (c); --(len);}
336 void
337 krb4encpwd_printsub(data, cnt, buf, buflen)
338 unsigned char *data, *buf;
339 int cnt, buflen;
341 char lbuf[32];
342 register int i;
344 buf[buflen-1] = '\0'; /* make sure its NULL terminated */
345 buflen -= 1;
347 switch(data[3]) {
348 case KRB4_ENCPWD_REJECT: /* Rejected (reason might follow) */
349 strncpy((char *)buf, " REJECT ", buflen);
350 goto common;
352 case KRB4_ENCPWD_ACCEPT: /* Accepted (name might follow) */
353 strncpy((char *)buf, " ACCEPT ", buflen);
354 common:
355 BUMP(buf, buflen);
356 if (cnt <= 4)
357 break;
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');
363 break;
365 case KRB4_ENCPWD_AUTH: /* Authentication data follows */
366 strncpy((char *)buf, " AUTH", buflen);
367 goto common2;
369 case KRB4_ENCPWD_CHALLENGE:
370 strncpy((char *)buf, " CHALLENGE", buflen);
371 goto common2;
373 case KRB4_ENCPWD_ACK:
374 strncpy((char *)buf, " ACK", buflen);
375 goto common2;
377 default:
378 sprintf(lbuf, " %d (unknown)", data[3]);
379 strncpy((char *)buf, lbuf, buflen);
380 common2:
381 BUMP(buf, buflen);
382 for (i = 4; i < cnt; i++) {
383 sprintf(lbuf, " %d", data[i]);
384 strncpy((char *)buf, lbuf, buflen);
385 BUMP(buf, buflen);
387 break;
391 int passwdok(name, passwd)
392 char *name, *passwd;
394 char *crypt();
395 char *salt, *p;
396 struct passwd *pwd;
397 int passwdok_status = 0;
399 if (pwd = getpwnam(name))
400 salt = pwd->pw_passwd;
401 else salt = "xx";
403 p = crypt(passwd, salt);
405 if (pwd && !strcmp(p, pwd->pw_passwd)) {
406 passwdok_status = 1;
407 } else passwdok_status = 0;
408 return(passwdok_status);
411 #endif