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