sra.c revision 1.4 1 /*-
2 * Copyright (c) 1991, 1993
3 * Dave Safford. 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
31 #include <sys/cdefs.h>
32 #ifdef notdef
33 __FBSDID("$FreeBSD: src/contrib/telnet/libtelnet/sra.c,v 1.16 2002/05/06 09:48:02 markm Exp $");
34 #else
35 __RCSID("$NetBSD: sra.c,v 1.4 2005/05/05 02:20:45 lukem Exp $");
36 #endif
37
38 #ifdef SRA
39 #ifdef ENCRYPTION
40 #include <sys/types.h>
41 #include <arpa/telnet.h>
42 #include <paths.h>
43 #include <pwd.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <syslog.h>
48 #include <ttyent.h>
49
50 #ifndef NOPAM
51 #include <security/pam_appl.h>
52 #else
53 #include <unistd.h>
54 #endif
55
56 #include "auth.h"
57 #include "misc.h"
58 #include "encrypt.h"
59 #include "pk.h"
60
61 char pka[HEXKEYBYTES+1], ska[HEXKEYBYTES+1], pkb[HEXKEYBYTES+1];
62 char *user, *pass, *xuser, *xpass;
63 DesData ck;
64 IdeaData ik;
65
66 extern int auth_debug_mode;
67 extern char *line; /* see sys_term.c */
68
69 static int sra_valid = 0;
70 static int passwd_sent = 0;
71
72 static unsigned char str_data[1024] = { IAC, SB, TELOPT_AUTHENTICATION, 0,
73 AUTHTYPE_SRA, };
74
75 #define SRA_KEY 0
76 #define SRA_USER 1
77 #define SRA_CONTINUE 2
78 #define SRA_PASS 3
79 #define SRA_ACCEPT 4
80 #define SRA_REJECT 5
81
82 static int check_user(char *, char *);
83
84 /* support routine to send out authentication message */
85 static int
86 Data(Authenticator *ap, int type, void *d, int c)
87 {
88 unsigned char *p = str_data + 4;
89 unsigned char *cd = (unsigned char *)d;
90
91 if (c == -1)
92 c = strlen((char *)cd);
93
94 if (auth_debug_mode) {
95 printf("%s:%d: [%d] (%d)",
96 str_data[3] == TELQUAL_IS ? ">>>IS" : ">>>REPLY",
97 str_data[3],
98 type, c);
99 printd(d, c);
100 printf("\r\n");
101 }
102 *p++ = ap->type;
103 *p++ = ap->way;
104 *p++ = type;
105 while (c-- > 0) {
106 if ((*p++ = *cd++) == IAC)
107 *p++ = IAC;
108 }
109 *p++ = IAC;
110 *p++ = SE;
111 if (str_data[3] == TELQUAL_IS)
112 printsub('>', &str_data[2], p - (&str_data[2]));
113 return(telnet_net_write(str_data, p - str_data));
114 }
115
116 int
117 sra_init(Authenticator *ap __unused, int server)
118 {
119 if (server)
120 str_data[3] = TELQUAL_REPLY;
121 else
122 str_data[3] = TELQUAL_IS;
123
124 user = (char *)malloc(256);
125 xuser = (char *)malloc(513);
126 pass = (char *)malloc(256);
127 xpass = (char *)malloc(513);
128
129 if (user == NULL || xuser == NULL || pass == NULL || xpass ==
130 NULL)
131 return 0; /* malloc failed */
132
133 passwd_sent = 0;
134
135 genkeys(pka,ska);
136 return(1);
137 }
138
139 /* client received a go-ahead for sra */
140 int
141 sra_send(Authenticator *ap)
142 {
143 /* send PKA */
144
145 if (auth_debug_mode)
146 printf("Sent PKA to server.\r\n" );
147 printf("Trying SRA secure login:\r\n");
148 if (!Data(ap, SRA_KEY, (void *)pka, HEXKEYBYTES)) {
149 if (auth_debug_mode)
150 printf("Not enough room for authentication data\r\n");
151 return(0);
152 }
153
154 return(1);
155 }
156
157 /* server received an IS -- could be SRA KEY, USER, or PASS */
158 void
159 sra_is(Authenticator *ap, unsigned char *data, int cnt)
160 {
161 int valid;
162 Session_Key skey;
163
164 if (cnt-- < 1)
165 goto bad;
166 switch (*data++) {
167
168 case SRA_KEY:
169 if (cnt < HEXKEYBYTES) {
170 Data(ap, SRA_REJECT, (void *)0, 0);
171 auth_finished(ap, AUTH_USER);
172 if (auth_debug_mode) {
173 printf("SRA user rejected for bad PKB\r\n");
174 }
175 return;
176 }
177 if (auth_debug_mode)
178 printf("Sent pka\r\n");
179 if (!Data(ap, SRA_KEY, (void *)pka, HEXKEYBYTES)) {
180 if (auth_debug_mode)
181 printf("Not enough room\r\n");
182 return;
183 }
184 memcpy(pkb,data,HEXKEYBYTES);
185 pkb[HEXKEYBYTES] = '\0';
186 common_key(ska,pkb,&ik,&ck);
187 return;
188
189 case SRA_USER:
190 /* decode KAB(u) */
191 if (cnt > 512) /* Attempted buffer overflow */
192 break;
193 memcpy(xuser,data,cnt);
194 xuser[cnt] = '\0';
195 pk_decode(xuser,user,&ck);
196 auth_encrypt_user(user);
197 Data(ap, SRA_CONTINUE, (void *)0, 0);
198
199 return;
200
201 case SRA_PASS:
202 if (cnt > 512) /* Attempted buffer overflow */
203 break;
204 /* decode KAB(P) */
205 memcpy(xpass,data,cnt);
206 xpass[cnt] = '\0';
207 pk_decode(xpass,pass,&ck);
208
209 /* check user's password */
210 valid = check_user(user,pass);
211
212 if(valid) {
213 Data(ap, SRA_ACCEPT, (void *)0, 0);
214 skey.data = ck;
215 skey.type = SK_DES;
216 skey.length = 8;
217 encrypt_session_key(&skey, 1);
218
219 sra_valid = 1;
220 auth_finished(ap, AUTH_VALID);
221 if (auth_debug_mode) {
222 printf("SRA user accepted\r\n");
223 }
224 }
225 else {
226 Data(ap, SRA_CONTINUE, (void *)0, 0);
227 /*
228 Data(ap, SRA_REJECT, (void *)0, 0);
229 sra_valid = 0;
230 auth_finished(ap, AUTH_REJECT);
231 */
232 if (auth_debug_mode) {
233 printf("SRA user failed\r\n");
234 }
235 }
236 return;
237
238 default:
239 if (auth_debug_mode)
240 printf("Unknown SRA option %d\r\n", data[-1]);
241 }
242 bad:
243 Data(ap, SRA_REJECT, 0, 0);
244 sra_valid = 0;
245 auth_finished(ap, AUTH_REJECT);
246 }
247
248 /* client received REPLY -- could be SRA KEY, CONTINUE, ACCEPT, or REJECT */
249 void
250 sra_reply(Authenticator *ap, unsigned char *data, int cnt)
251 {
252 char uprompt[256],tuser[256];
253 Session_Key skey;
254 size_t i;
255
256 if (cnt-- < 1)
257 return;
258 switch (*data++) {
259
260 case SRA_KEY:
261 /* calculate common key */
262 if (cnt < HEXKEYBYTES) {
263 if (auth_debug_mode) {
264 printf("SRA user rejected for bad PKB\r\n");
265 }
266 return;
267 }
268 memcpy(pkb,data,HEXKEYBYTES);
269 pkb[HEXKEYBYTES] = '\0';
270
271 common_key(ska,pkb,&ik,&ck);
272
273 enc_user:
274
275 /* encode user */
276 memset(tuser,0,sizeof(tuser));
277 sprintf(uprompt,"User (%s): ",UserNameRequested);
278 telnet_gets(uprompt,tuser,255,1);
279 if (tuser[0] == '\n' || tuser[0] == '\r' )
280 strcpy(user,UserNameRequested);
281 else {
282 /* telnet_gets leaves the newline on */
283 for(i=0;i<sizeof(tuser);i++) {
284 if (tuser[i] == '\n') {
285 tuser[i] = '\0';
286 break;
287 }
288 }
289 strcpy(user,tuser);
290 }
291 pk_encode(user,xuser,&ck);
292
293 /* send it off */
294 if (auth_debug_mode)
295 printf("Sent KAB(U)\r\n");
296 if (!Data(ap, SRA_USER, (void *)xuser, strlen(xuser))) {
297 if (auth_debug_mode)
298 printf("Not enough room\r\n");
299 return;
300 }
301 break;
302
303 case SRA_CONTINUE:
304 if (passwd_sent) {
305 passwd_sent = 0;
306 printf("[ SRA login failed ]\r\n");
307 goto enc_user;
308 }
309 /* encode password */
310 memset(pass,0,sizeof(pass));
311 telnet_gets("Password: ",pass,255,0);
312 pk_encode(pass,xpass,&ck);
313 /* send it off */
314 if (auth_debug_mode)
315 printf("Sent KAB(P)\r\n");
316 if (!Data(ap, SRA_PASS, (void *)xpass, strlen(xpass))) {
317 if (auth_debug_mode)
318 printf("Not enough room\r\n");
319 return;
320 }
321 passwd_sent = 1;
322 break;
323
324 case SRA_REJECT:
325 printf("[ SRA refuses authentication ]\r\n");
326 printf("Trying plaintext login:\r\n");
327 auth_finished(0,AUTH_REJECT);
328 return;
329
330 case SRA_ACCEPT:
331 printf("[ SRA accepts you ]\r\n");
332 skey.data = ck;
333 skey.type = SK_DES;
334 skey.length = 8;
335 encrypt_session_key(&skey, 0);
336
337 auth_finished(ap, AUTH_VALID);
338 return;
339 default:
340 if (auth_debug_mode)
341 printf("Unknown SRA option %d\r\n", data[-1]);
342 return;
343 }
344 }
345
346 int
347 sra_status(Authenticator *ap __unused, char *name, size_t len, int level)
348 {
349 if (level < AUTH_USER)
350 return(level);
351 if (UserNameRequested && sra_valid) {
352 strlcpy(name, UserNameRequested, len);
353 return(AUTH_VALID);
354 } else
355 return(AUTH_USER);
356 }
357
358 #define BUMP(buf, len) while (*(buf)) {++(buf), --(len);}
359 #define ADDC(buf, len, c) if ((len) > 0) {*(buf)++ = (c); --(len);}
360
361 void
362 sra_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
363 {
364 char lbuf[32];
365 int i;
366
367 buf[buflen-1] = '\0'; /* make sure its NULL terminated */
368 buflen -= 1;
369
370 switch(data[3]) {
371
372 case SRA_CONTINUE:
373 strncpy((char *)buf, " CONTINUE ", buflen);
374 goto common;
375
376 case SRA_REJECT: /* Rejected (reason might follow) */
377 strncpy((char *)buf, " REJECT ", buflen);
378 goto common;
379
380 case SRA_ACCEPT: /* Accepted (name might follow) */
381 strncpy((char *)buf, " ACCEPT ", buflen);
382
383 common:
384 BUMP(buf, buflen);
385 if (cnt <= 4)
386 break;
387 ADDC(buf, buflen, '"');
388 for (i = 4; i < cnt; i++)
389 ADDC(buf, buflen, data[i]);
390 ADDC(buf, buflen, '"');
391 ADDC(buf, buflen, '\0');
392 break;
393
394 case SRA_KEY: /* Authentication data follows */
395 strncpy((char *)buf, " KEY ", buflen);
396 goto common2;
397
398 case SRA_USER:
399 strncpy((char *)buf, " USER ", buflen);
400 goto common2;
401
402 case SRA_PASS:
403 strncpy((char *)buf, " PASS ", buflen);
404 goto common2;
405
406 default:
407 sprintf(lbuf, " %d (unknown)", data[3]);
408 strncpy((char *)buf, lbuf, buflen);
409 common2:
410 BUMP(buf, buflen);
411 for (i = 4; i < cnt; i++) {
412 sprintf(lbuf, " %d", data[i]);
413 strncpy((char *)buf, lbuf, buflen);
414 BUMP(buf, buflen);
415 }
416 break;
417 }
418 }
419
420 static int
421 isroot(const char *usr)
422 {
423 struct passwd pws, *pwd;
424 char pwbuf[1024];
425
426 if (getpwnam_r(usr, &pws, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
427 pwd == NULL)
428 return 0;
429 return (!pwd->pw_uid);
430 }
431
432 static int
433 rootterm(const char *ttyname)
434 {
435 struct ttyent *t;
436 const char *ttyn;
437
438 ttyn = ttyname;
439 if (strncmp(ttyn, _PATH_DEV, sizeof(_PATH_DEV)-1) == 0)
440 ttyn += sizeof(_PATH_DEV) - 1;
441
442 return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE);
443 }
444
445 #ifdef NOPAM
446 static int
447 check_user(char *name, char *cred)
448 {
449 char *cp;
450 char *xpasswd, *salt;
451
452 if (isroot(name) && !rootterm(line))
453 {
454 crypt("AA","*"); /* Waste some time to simulate success */
455 return(0);
456 }
457
458 if (pw = sgetpwnam(name)) {
459 if (pw->pw_shell == NULL) {
460 pw = (struct passwd *) NULL;
461 return(0);
462 }
463
464 salt = pw->pw_passwd;
465 xpasswd = crypt(cred, salt);
466 /* The strcmp does not catch null passwords! */
467 if (pw == NULL || *pw->pw_passwd == '\0' ||
468 strcmp(xpasswd, pw->pw_passwd)) {
469 pw = (struct passwd *) NULL;
470 return(0);
471 }
472 return(1);
473 }
474 return(0);
475 }
476 #else
477
478 /*
479 * The following is stolen from ftpd, which stole it from the imap-uw
480 * PAM module and login.c. It is needed because we can't really
481 * "converse" with the user, having already gone to the trouble of
482 * getting their username and password through an encrypted channel.
483 */
484
485 #define COPY_STRING(s) (s ? strdup(s):NULL)
486
487 struct cred_t {
488 const char *uname;
489 const char *pass;
490 };
491 typedef struct cred_t cred_t;
492
493 static int
494 auth_conv(int num_msg, const struct pam_message **msg, struct pam_response **resp, void *appdata)
495 {
496 int i;
497 cred_t *cred = (cred_t *) appdata;
498 struct pam_response *reply =
499 malloc(sizeof(struct pam_response) * num_msg);
500
501 if (reply == NULL)
502 return PAM_BUF_ERR;
503
504 for (i = 0; i < num_msg; i++) {
505 switch (msg[i]->msg_style) {
506 case PAM_PROMPT_ECHO_ON: /* assume want user name */
507 reply[i].resp_retcode = PAM_SUCCESS;
508 reply[i].resp = COPY_STRING(cred->uname);
509 /* PAM frees resp. */
510 break;
511 case PAM_PROMPT_ECHO_OFF: /* assume want password */
512 reply[i].resp_retcode = PAM_SUCCESS;
513 reply[i].resp = COPY_STRING(cred->pass);
514 /* PAM frees resp. */
515 break;
516 case PAM_TEXT_INFO:
517 case PAM_ERROR_MSG:
518 reply[i].resp_retcode = PAM_SUCCESS;
519 reply[i].resp = NULL;
520 break;
521 default: /* unknown message style */
522 free(reply);
523 return PAM_CONV_ERR;
524 }
525 }
526
527 *resp = reply;
528 return PAM_SUCCESS;
529 }
530
531 /*
532 * The PAM version as a side effect may put a new username in *name.
533 */
534 static int
535 check_user(char *name, char *cred)
536 {
537 pam_handle_t *pamh = NULL;
538 const void *item;
539 int rval;
540 int e;
541 cred_t auth_cred = { name, cred };
542 struct pam_conv conv = { &auth_conv, &auth_cred };
543
544 e = pam_start("telnetd", name, &conv, &pamh);
545 if (e != PAM_SUCCESS) {
546 syslog(LOG_ERR, "pam_start: %s", pam_strerror(pamh, e));
547 return 0;
548 }
549
550 #if 0 /* Where can we find this value? */
551 e = pam_set_item(pamh, PAM_RHOST, remotehost);
552 if (e != PAM_SUCCESS) {
553 syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s",
554 pam_strerror(pamh, e));
555 return 0;
556 }
557 #endif
558
559 e = pam_authenticate(pamh, 0);
560 switch (e) {
561 case PAM_SUCCESS:
562 /*
563 * With PAM we support the concept of a "template"
564 * user. The user enters a login name which is
565 * authenticated by PAM, usually via a remote service
566 * such as RADIUS or TACACS+. If authentication
567 * succeeds, a different but related "template" name
568 * is used for setting the credentials, shell, and
569 * home directory. The name the user enters need only
570 * exist on the remote authentication server, but the
571 * template name must be present in the local password
572 * database.
573 *
574 * This is supported by two various mechanisms in the
575 * individual modules. However, from the application's
576 * point of view, the template user is always passed
577 * back as a changed value of the PAM_USER item.
578 */
579 if ((e = pam_get_item(pamh, PAM_USER, &item)) ==
580 PAM_SUCCESS) {
581 strcpy(name, item);
582 } else
583 syslog(LOG_ERR, "Couldn't get PAM_USER: %s",
584 pam_strerror(pamh, e));
585 if (isroot(name) && !rootterm(line))
586 rval = 0;
587 else
588 rval = 1;
589 break;
590
591 case PAM_AUTH_ERR:
592 case PAM_USER_UNKNOWN:
593 case PAM_MAXTRIES:
594 rval = 0;
595 break;
596
597 default:
598 syslog(LOG_ERR, "auth_pam: %s", pam_strerror(pamh, e));
599 rval = 0;
600 break;
601 }
602
603 if ((e = pam_end(pamh, e)) != PAM_SUCCESS) {
604 syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
605 rval = 0;
606 }
607 return rval;
608 }
609
610 #endif
611
612 #endif /* ENCRYPTION */
613 #endif /* SRA */
614