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