Initial import from FreeBSD RELENG_4:
[dragonfly.git] / crypto / kerberosIV / appl / telnet / libtelnet / krb4encpwd.c
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  * 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.
20  *
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.
32  */
33 /* $FreeBSD: src/crypto/kerberosIV/appl/telnet/libtelnet/krb4encpwd.c,v 1.2.2.2 2003/02/13 21:34:35 nectar Exp $ */
34
35 #include <config.h>
36
37 RCSID("$Id: krb4encpwd.c,v 1.18 1999/09/16 20:41:34 assar Exp $");
38
39 #ifdef  KRB4_ENCPWD
40 /*
41  * COPYRIGHT (C) 1990 DIGITAL EQUIPMENT CORPORATION
42  * ALL RIGHTS RESERVED
43  *
44  * "Digital Equipment Corporation authorizes the reproduction,
45  * distribution and modification of this software subject to the following
46  * restrictions:
47  *
48  * 1.  Any partial or whole copy of this software, or any modification
49  * thereof, must include this copyright notice in its entirety.
50  *
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.
56  *
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.
59  *
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.
65  *
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"
70  *
71  */
72
73 #include <sys/types.h>
74 #include <arpa/telnet.h>
75 #include <pwd.h>
76 #include <stdio.h>
77
78 #define OPENSSL_DES_LIBDES_COMPATIBILITY
79 #include <openssl/des.h>
80 #include <krb.h>
81 #include <stdlib.h>
82 #include <string.h>
83 #ifdef SOCKS
84 #include <socks.h>
85 #endif
86
87 #include "encrypt.h"
88 #include "auth.h"
89 #include "misc.h"
90
91 int krb_mk_encpwd_req (KTEXT, char *, char *, char *, char *, char *, char *);
92 int krb_rd_encpwd_req (KTEXT, char *, char *, u_long, AUTH_DAT *, char *, char *, char *, char *);
93
94 extern auth_debug_mode;
95
96 static unsigned char str_data[1024] = { IAC, SB, TELOPT_AUTHENTICATION, 0,
97                                         AUTHTYPE_KRB4_ENCPWD, };
98 static unsigned char str_name[1024] = { IAC, SB, TELOPT_AUTHENTICATION,
99                                         TELQUAL_NAME, };
100
101 #define KRB4_ENCPWD_AUTH        0       /* Authentication data follows */
102 #define KRB4_ENCPWD_REJECT      1       /* Rejected (reason might follow) */
103 #define KRB4_ENCPWD_ACCEPT      2       /* Accepted */
104 #define KRB4_ENCPWD_CHALLENGE   3       /* Challenge for mutual auth. */
105 #define KRB4_ENCPWD_ACK         4       /* Acknowledge */
106
107 #define KRB_SERVICE_NAME    "rcmd"
108
109 static  KTEXT_ST auth;
110 static  char name[ANAME_SZ];
111 static  char user_passwd[ANAME_SZ];
112 static  AUTH_DAT adat = { 0 };
113 static des_key_schedule sched;
114 static char  challenge[REALM_SZ];
115
116         static int
117 Data(ap, type, d, c)
118         Authenticator *ap;
119         int type;
120         void *d;
121         int c;
122 {
123         unsigned char *p = str_data + 4;
124         unsigned char *cd = (unsigned char *)d;
125
126         if (c == -1)
127                 c = strlen(cd);
128
129         if (0) {
130                 printf("%s:%d: [%d] (%d)",
131                         str_data[3] == TELQUAL_IS ? ">>>IS" : ">>>REPLY",
132                         str_data[3],
133                         type, c);
134                 printd(d, c);
135                 printf("\r\n");
136         }
137         *p++ = ap->type;
138         *p++ = ap->way;
139         *p++ = type;
140         while (c-- > 0) {
141                 if ((*p++ = *cd++) == IAC)
142                         *p++ = IAC;
143         }
144         *p++ = IAC;
145         *p++ = SE;
146         if (str_data[3] == TELQUAL_IS)
147                 printsub('>', &str_data[2], p - (&str_data[2]));
148         return(telnet_net_write(str_data, p - str_data));
149 }
150
151         int
152 krb4encpwd_init(ap, server)
153         Authenticator *ap;
154         int server;
155 {
156         char hostname[80], *cp, *realm;
157         des_clock skey;
158
159         if (server) {
160                 str_data[3] = TELQUAL_REPLY;
161         } else {
162                 str_data[3] = TELQUAL_IS;
163                 gethostname(hostname, sizeof(hostname));
164                 realm = krb_realmofhost(hostname);
165                 cp = strchr(hostname, '.');
166                 if (*cp != NULL) *cp = NULL;
167                 if (read_service_key(KRB_SERVICE_NAME, hostname, realm, 0,
168                                         KEYFILE, (char *)skey)) {
169                   return(0);
170                 }
171         }
172         return(1);
173 }
174
175         int
176 krb4encpwd_send(ap)
177         Authenticator *ap;
178 {
179
180         printf("[ Trying KRB4ENCPWD ... ]\r\n");
181         if (!UserNameRequested) {
182                 return(0);
183         }
184         if (!auth_sendname(UserNameRequested, strlen(UserNameRequested))) {
185                 return(0);
186         }
187
188         if (!Data(ap, KRB4_ENCPWD_ACK, NULL, 0)) {
189                 return(0);
190         }
191
192         return(1);
193 }
194
195         void
196 krb4encpwd_is(ap, data, cnt)
197         Authenticator *ap;
198         unsigned char *data;
199         int cnt;
200 {
201         Session_Key skey;
202         des_cblock datablock;
203         char  r_passwd[ANAME_SZ], r_user[ANAME_SZ];
204         char  lhostname[ANAME_SZ], *cp;
205         int r;
206         time_t now;
207
208         if (cnt-- < 1)
209                 return;
210         switch (*data++) {
211         case KRB4_ENCPWD_AUTH:
212                 memmove(auth.dat, data, auth.length = cnt);
213
214                 gethostname(lhostname, sizeof(lhostname));
215                 if ((cp = strchr(lhostname, '.')) != 0)  *cp = '\0';
216
217                 if (r = krb_rd_encpwd_req(&auth, KRB_SERVICE_NAME, lhostname, 0, &adat, NULL, challenge, r_user, r_passwd)) {
218                         Data(ap, KRB4_ENCPWD_REJECT, "Auth failed", -1);
219                         auth_finished(ap, AUTH_REJECT);
220                         return;
221                 }
222                 auth_encrypt_userpwd(r_passwd);
223                 if (passwdok(UserNameRequested, UserPassword) == 0) {
224                   /*
225                    *  illegal username and password
226                    */
227                   Data(ap, KRB4_ENCPWD_REJECT, "Illegal password", -1);
228                   auth_finished(ap, AUTH_REJECT);
229                   return;
230                 }
231
232                 memmove(session_key, adat.session, sizeof(des_cblock));
233                 Data(ap, KRB4_ENCPWD_ACCEPT, 0, 0);
234                 auth_finished(ap, AUTH_USER);
235                 break;
236
237         case KRB4_ENCPWD_CHALLENGE:
238                 /*
239                  *  Take the received random challenge text and save
240                  *  for future authentication.
241                  */
242                 memmove(challenge, data, sizeof(des_cblock));
243                 break;
244
245
246         case KRB4_ENCPWD_ACK:
247                 /*
248                  *  Receive ack, if mutual then send random challenge
249                  */
250
251                 /*
252                  * If we are doing mutual authentication, get set up to send
253                  * the challenge, and verify it when the response comes back.
254                  */
255
256                 if ((ap->way & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) {
257                   int i;
258
259                   time(&now);
260                   snprintf(challenge, sizeof(challenge), "%x", now);
261                   Data(ap, KRB4_ENCPWD_CHALLENGE, challenge, strlen(challenge));
262                 }
263                 break;
264
265         default:
266                 Data(ap, KRB4_ENCPWD_REJECT, 0, 0);
267                 break;
268         }
269 }
270
271
272         void
273 krb4encpwd_reply(ap, data, cnt)
274         Authenticator *ap;
275         unsigned char *data;
276         int cnt;
277 {
278         Session_Key skey;
279         KTEXT_ST krb_token;
280         des_cblock enckey;
281         CREDENTIALS cred;
282         int r;
283         char    randchal[REALM_SZ], instance[ANAME_SZ], *cp;
284         char    hostname[80], *realm;
285
286         if (cnt-- < 1)
287                 return;
288         switch (*data++) {
289         case KRB4_ENCPWD_REJECT:
290                 if (cnt > 0) {
291                         printf("[ KRB4_ENCPWD refuses authentication because %.*s ]\r\n",
292                                 cnt, data);
293                 } else
294                         printf("[ KRB4_ENCPWD refuses authentication ]\r\n");
295                 auth_send_retry();
296                 return;
297         case KRB4_ENCPWD_ACCEPT:
298                 printf("[ KRB4_ENCPWD accepts you ]\r\n");
299                 auth_finished(ap, AUTH_USER);
300                 return;
301         case KRB4_ENCPWD_CHALLENGE:
302                 /*
303                  * Verify that the response to the challenge is correct.
304                  */
305
306                 gethostname(hostname, sizeof(hostname));
307                 realm = krb_realmofhost(hostname);
308                 memmove(challenge, data, cnt);
309                 memset(user_passwd, 0, sizeof(user_passwd));
310                 des_read_pw_string(user_passwd, sizeof(user_passwd)-1, "Password: ", 0);
311                 UserPassword = user_passwd;
312                 Challenge = challenge;
313                 strlcpy(instance, RemoteHostName, sizeof(instance));
314                 if ((cp = strchr(instance, '.')) != 0)  *cp = '\0';
315
316                 if (r = krb_mk_encpwd_req(&krb_token, KRB_SERVICE_NAME, instance, realm, Challenge, UserNameRequested, user_passwd)) {
317                   krb_token.length = 0;
318                 }
319
320                 if (!Data(ap, KRB4_ENCPWD_AUTH, krb_token.dat, krb_token.length)) {
321                   return;
322                 }
323
324                 break;
325
326         default:
327                 return;
328         }
329 }
330
331         int
332 krb4encpwd_status(ap, name, name_sz, level)
333         Authenticator *ap;
334         char *name;
335         size_t name_sz;
336         int level;
337 {
338
339         if (level < AUTH_USER)
340                 return(level);
341
342         if (UserNameRequested && passwdok(UserNameRequested, UserPassword)) {
343                 strlcpy(name, UserNameRequested, name_sz);
344                 return(AUTH_VALID);
345         } else {
346                 return(AUTH_USER);
347         }
348 }
349
350 #define BUMP(buf, len)          while (*(buf)) {++(buf), --(len);}
351 #define ADDC(buf, len, c)       if ((len) > 0) {*(buf)++ = (c); --(len);}
352
353         void
354 krb4encpwd_printsub(data, cnt, buf, buflen)
355         unsigned char *data, *buf;
356         int cnt, buflen;
357 {
358         int i;
359
360         buf[buflen-1] = '\0';           /* make sure its NULL terminated */
361         buflen -= 1;
362
363         switch(data[3]) {
364         case KRB4_ENCPWD_REJECT:        /* Rejected (reason might follow) */
365                 strlcpy((char *)buf, " REJECT ", buflen);
366                 goto common;
367
368         case KRB4_ENCPWD_ACCEPT:        /* Accepted (name might follow) */
369                 strlcpy((char *)buf, " ACCEPT ", buflen);
370         common:
371                 BUMP(buf, buflen);
372                 if (cnt <= 4)
373                         break;
374                 ADDC(buf, buflen, '"');
375                 for (i = 4; i < cnt; i++)
376                         ADDC(buf, buflen, data[i]);
377                 ADDC(buf, buflen, '"');
378                 ADDC(buf, buflen, '\0');
379                 break;
380
381         case KRB4_ENCPWD_AUTH:          /* Authentication data follows */
382                 strlcpy((char *)buf, " AUTH", buflen);
383                 goto common2;
384
385         case KRB4_ENCPWD_CHALLENGE:
386                 strlcpy((char *)buf, " CHALLENGE", buflen);
387                 goto common2;
388
389         case KRB4_ENCPWD_ACK:
390                 strlcpy((char *)buf, " ACK", buflen);
391                 goto common2;
392
393         default:
394                 snprintf(buf, buflen, " %d (unknown)", data[3]);
395         common2:
396                 BUMP(buf, buflen);
397                 for (i = 4; i < cnt; i++) {
398                         snprintf(buf, buflen, " %d", data[i]);
399                         BUMP(buf, buflen);
400                 }
401                 break;
402         }
403 }
404
405 int passwdok(name, passwd)
406 char *name, *passwd;
407 {
408   char *crypt();
409   char *salt, *p;
410   struct passwd *pwd;
411   int   passwdok_status = 0;
412
413   if (pwd = k_getpwnam(name))
414     salt = pwd->pw_passwd;
415   else salt = "xx";
416
417   p = crypt(passwd, salt);
418
419   if (pwd && !strcmp(p, pwd->pw_passwd)) {
420     passwdok_status = 1;
421   } else passwdok_status = 0;
422   return(passwdok_status);
423 }
424
425 #endif
426
427 #ifdef notdef
428
429 prkey(msg, key)
430         char *msg;
431         unsigned char *key;
432 {
433         int i;
434         printf("%s:", msg);
435         for (i = 0; i < 8; i++)
436                 printf(" %3d", key[i]);
437         printf("\r\n");
438 }
439 #endif