2 * Copyright (c) 1991, 1993
3 * Dave Safford. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
31 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD: src/contrib/telnet/libtelnet/sra.c,v 1.16 2002/05/06 09:48:02 markm Exp $");
35 __RCSID("$NetBSD: sra.c,v 1.7 2005/10/25 22:03:34 christos Exp $");
40 #include <sys/types.h>
41 #include <arpa/telnet.h>
51 #include <security/pam_appl.h>
61 char pka
[HEXKEYBYTES
+1], ska
[HEXKEYBYTES
+1], pkb
[HEXKEYBYTES
+1];
62 char *user
, *pass
, *xuser
, *xpass
;
63 char *passprompt
, *xpassprompt
;
67 extern int auth_debug_mode
;
68 extern char *line
; /* see sys_term.c */
70 static int sra_valid
= 0;
71 static int passwd_sent
= 0;
73 static unsigned char str_data
[1024] = { IAC
, SB
, TELOPT_AUTHENTICATION
, 0,
78 #define SRA_CONTINUE 2
83 static int check_user(char *, char *);
85 /* support routine to send out authentication message */
87 Data(Authenticator
*ap
, int type
, void *d
, int c
)
89 unsigned char *p
= str_data
+ 4;
90 unsigned char *cd
= (unsigned char *)d
;
93 c
= strlen((char *)cd
);
95 if (auth_debug_mode
) {
96 printf("%s:%d: [%d] (%d)",
97 str_data
[3] == TELQUAL_IS
? ">>>IS" : ">>>REPLY",
107 if ((*p
++ = *cd
++) == IAC
)
112 if (str_data
[3] == TELQUAL_IS
)
113 printsub('>', &str_data
[2], p
- (&str_data
[2]));
114 return(telnet_net_write(str_data
, p
- str_data
));
118 sra_init(Authenticator
*ap __unused
, int server
)
121 str_data
[3] = TELQUAL_REPLY
;
123 str_data
[3] = TELQUAL_IS
;
125 user
= (char *)malloc(256);
126 xuser
= (char *)malloc(513);
127 pass
= (char *)malloc(256);
128 xpass
= (char *)malloc(513);
129 passprompt
= (char *)malloc(256);
130 xpassprompt
= (char *)malloc(513);
132 if (user
== NULL
|| xuser
== NULL
|| pass
== NULL
|| xpass
==
133 NULL
|| passprompt
== NULL
|| xpassprompt
== NULL
)
134 return 0; /* malloc failed */
142 /* client received a go-ahead for sra */
144 sra_send(Authenticator
*ap
)
149 printf("Sent PKA to server.\r\n" );
150 printf("Trying SRA secure login:\r\n");
151 if (!Data(ap
, SRA_KEY
, (void *)pka
, HEXKEYBYTES
)) {
153 printf("Not enough room for authentication data\r\n");
160 /* server received an IS -- could be SRA KEY, USER, or PASS */
162 sra_is(Authenticator
*ap
, unsigned char *data
, int cnt
)
172 if (cnt
< HEXKEYBYTES
) {
173 Data(ap
, SRA_REJECT
, (void *)0, 0);
174 auth_finished(ap
, AUTH_USER
);
175 if (auth_debug_mode
) {
176 printf("SRA user rejected for bad PKB\r\n");
181 printf("Sent pka\r\n");
182 if (!Data(ap
, SRA_KEY
, (void *)pka
, HEXKEYBYTES
)) {
184 printf("Not enough room\r\n");
187 memcpy(pkb
,data
,HEXKEYBYTES
);
188 pkb
[HEXKEYBYTES
] = '\0';
189 common_key(ska
,pkb
,&ik
,&ck
);
194 if (cnt
> 512) /* Attempted buffer overflow */
196 memcpy(xuser
,data
,cnt
);
198 pk_decode(xuser
,user
,&ck
);
199 auth_encrypt_user(user
);
201 (void)check_user(user
, "*");
203 pk_encode(passprompt
,xpassprompt
,&ck
);
204 Data(ap
, SRA_CONTINUE
, (void *)xpassprompt
, 512);
209 if (cnt
> 512) /* Attempted buffer overflow */
212 memcpy(xpass
,data
,cnt
);
214 pk_decode(xpass
,pass
,&ck
);
216 /* check user's password */
217 valid
= check_user(user
,pass
);
220 /* PAM (via check_user()) may have changed 'user' */
221 auth_encrypt_user(user
);
222 Data(ap
, SRA_ACCEPT
, (void *)0, 0);
226 encrypt_session_key(&skey
, 1);
229 auth_finished(ap
, AUTH_VALID
);
230 if (auth_debug_mode
) {
231 printf("SRA user accepted\r\n");
235 pk_encode(passprompt
,xpassprompt
,&ck
);
236 Data(ap
, SRA_CONTINUE
, (void *)xpassprompt
, 512);
238 Data(ap, SRA_REJECT, (void *)0, 0);
240 auth_finished(ap, AUTH_REJECT);
242 if (auth_debug_mode
) {
243 printf("SRA user failed\r\n");
250 printf("Unknown SRA option %d\r\n", data
[-1]);
253 Data(ap
, SRA_REJECT
, 0, 0);
255 auth_finished(ap
, AUTH_REJECT
);
258 /* client received REPLY -- could be SRA KEY, CONTINUE, ACCEPT, or REJECT */
260 sra_reply(Authenticator
*ap
, unsigned char *data
, int cnt
)
262 char uprompt
[256],tuser
[256];
271 /* calculate common key */
272 if (cnt
< HEXKEYBYTES
) {
273 if (auth_debug_mode
) {
274 printf("SRA user rejected for bad PKB\r\n");
278 memcpy(pkb
,data
,HEXKEYBYTES
);
279 pkb
[HEXKEYBYTES
] = '\0';
281 common_key(ska
,pkb
,&ik
,&ck
);
286 memset(tuser
,0,sizeof(tuser
));
287 sprintf(uprompt
,"User (%s): ",UserNameRequested
);
288 if (telnet_gets(uprompt
,tuser
,255,1) == NULL
) {
292 if (tuser
[0] == '\n' || tuser
[0] == '\r' )
293 strcpy(user
,UserNameRequested
);
295 /* telnet_gets leaves the newline on */
296 for(i
=0;i
<sizeof(tuser
);i
++) {
297 if (tuser
[i
] == '\n') {
304 pk_encode(user
,xuser
,&ck
);
308 printf("Sent KAB(U)\r\n");
309 if (!Data(ap
, SRA_USER
, (void *)xuser
, strlen(xuser
))) {
311 printf("Not enough room\r\n");
319 printf("[ SRA login failed ]\r\n");
324 } else if (cnt
> 0) {
325 (void)memcpy(xpassprompt
,data
,cnt
);
326 pk_decode(xpassprompt
, passprompt
, &ck
);
328 (void)strcpy(passprompt
, "Password: ");
330 /* encode password */
331 memset(pass
,0,sizeof(pass
));
332 if (telnet_gets(passprompt
,pass
,255,0) == NULL
) {
336 pk_encode(pass
,xpass
,&ck
);
339 printf("Sent KAB(P)\r\n");
340 if (!Data(ap
, SRA_PASS
, (void *)xpass
, strlen(xpass
))) {
342 printf("Not enough room\r\n");
349 printf("[ SRA refuses authentication ]\r\n");
350 printf("Trying plaintext login:\r\n");
351 auth_finished(0,AUTH_REJECT
);
355 printf("[ SRA accepts you ]\r\n");
359 encrypt_session_key(&skey
, 0);
361 auth_finished(ap
, AUTH_VALID
);
365 printf("Unknown SRA option %d\r\n", data
[-1]);
371 sra_status(Authenticator
*ap __unused
, char *name
, size_t len
, int level
)
373 if (level
< AUTH_USER
)
375 if (UserNameRequested
&& sra_valid
) {
376 strlcpy(name
, UserNameRequested
, len
);
382 #define BUMP(buf, len) while (*(buf)) {++(buf), --(len);}
383 #define ADDC(buf, len, c) if ((len) > 0) {*(buf)++ = (c); --(len);}
386 sra_printsub(unsigned char *data
, int cnt
, unsigned char *buf
, int buflen
)
391 buf
[buflen
-1] = '\0'; /* make sure its NULL terminated */
397 strncpy((char *)buf
, " CONTINUE ", buflen
);
400 case SRA_REJECT
: /* Rejected (reason might follow) */
401 strncpy((char *)buf
, " REJECT ", buflen
);
404 case SRA_ACCEPT
: /* Accepted (name might follow) */
405 strncpy((char *)buf
, " ACCEPT ", buflen
);
411 ADDC(buf
, buflen
, '"');
412 for (i
= 4; i
< cnt
; i
++)
413 ADDC(buf
, buflen
, data
[i
]);
414 ADDC(buf
, buflen
, '"');
415 ADDC(buf
, buflen
, '\0');
418 case SRA_KEY
: /* Authentication data follows */
419 strncpy((char *)buf
, " KEY ", buflen
);
423 strncpy((char *)buf
, " USER ", buflen
);
427 strncpy((char *)buf
, " PASS ", buflen
);
431 sprintf(lbuf
, " %d (unknown)", data
[3]);
432 strncpy((char *)buf
, lbuf
, buflen
);
435 for (i
= 4; i
< cnt
; i
++) {
436 sprintf(lbuf
, " %d", data
[i
]);
437 strncpy((char *)buf
, lbuf
, buflen
);
446 isroot(const char *usr
)
448 struct passwd pws
, *pwd
;
451 if (getpwnam_r(usr
, &pws
, pwbuf
, sizeof(pwbuf
), &pwd
) != 0 ||
454 return (!pwd
->pw_uid
);
458 rootterm(const char *ttyname
)
464 if (strncmp(ttyn
, _PATH_DEV
, sizeof(_PATH_DEV
)-1) == 0)
465 ttyn
+= sizeof(_PATH_DEV
) - 1;
467 return ((t
= getttynam(ttyn
)) && t
->ty_status
& TTY_SECURE
);
471 check_user(char *name
, char *cred
)
473 struct passwd pws
, *pw
;
475 char *xpasswd
, *salt
;
477 if (isroot(name
) && !rootterm(line
))
479 crypt("AA","*"); /* Waste some time to simulate success */
483 if (getpwnam_r(name
, &pws
, pwbuf
, sizeof(pwbuf
), &pw
) == 0 &&
485 if (pw
->pw_shell
== NULL
) {
489 salt
= pw
->pw_passwd
;
490 xpasswd
= crypt(cred
, salt
);
491 /* The strcmp does not catch null passwords! */
492 if (*pw
->pw_passwd
== '\0' || strcmp(xpasswd
, pw
->pw_passwd
)) {
502 * The following is stolen from ftpd, which stole it from the imap-uw
503 * PAM module and login.c. It is needed because we can't really
504 * "converse" with the user, having already gone to the trouble of
505 * getting their username and password through an encrypted channel.
508 #define COPY_STRING(s) (s ? strdup(s):NULL)
514 typedef struct cred_t cred_t
;
517 auth_conv(int num_msg
, const struct pam_message
**msg
, struct pam_response
**resp
, void *appdata
)
520 cred_t
*cred
= (cred_t
*) appdata
;
521 struct pam_response
*reply
=
522 malloc(sizeof(struct pam_response
) * num_msg
);
527 for (i
= 0; i
< num_msg
; i
++) {
528 switch (msg
[i
]->msg_style
) {
529 case PAM_PROMPT_ECHO_ON
: /* assume want user name */
530 reply
[i
].resp_retcode
= PAM_SUCCESS
;
531 reply
[i
].resp
= COPY_STRING(cred
->uname
);
532 /* PAM frees resp. */
534 case PAM_PROMPT_ECHO_OFF
: /* assume want password */
535 (void)strlcpy(passprompt
, msg
[i
]->msg
, 256);
536 reply
[i
].resp_retcode
= PAM_SUCCESS
;
537 reply
[i
].resp
= COPY_STRING(cred
->pass
);
538 /* PAM frees resp. */
542 reply
[i
].resp_retcode
= PAM_SUCCESS
;
543 reply
[i
].resp
= NULL
;
545 default: /* unknown message style */
556 * The PAM version as a side effect may put a new username in *name.
559 check_user(char *name
, char *cred
)
561 pam_handle_t
*pamh
= NULL
;
565 cred_t auth_cred
= { name
, cred
};
566 struct pam_conv conv
= { &auth_conv
, &auth_cred
};
568 e
= pam_start("telnetd", name
, &conv
, &pamh
);
569 if (e
!= PAM_SUCCESS
) {
570 syslog(LOG_ERR
, "pam_start: %s", pam_strerror(pamh
, e
));
574 #if 0 /* Where can we find this value? */
575 e
= pam_set_item(pamh
, PAM_RHOST
, remotehost
);
576 if (e
!= PAM_SUCCESS
) {
577 syslog(LOG_ERR
, "pam_set_item(PAM_RHOST): %s",
578 pam_strerror(pamh
, e
));
583 e
= pam_authenticate(pamh
, 0);
587 * With PAM we support the concept of a "template"
588 * user. The user enters a login name which is
589 * authenticated by PAM, usually via a remote service
590 * such as RADIUS or TACACS+. If authentication
591 * succeeds, a different but related "template" name
592 * is used for setting the credentials, shell, and
593 * home directory. The name the user enters need only
594 * exist on the remote authentication server, but the
595 * template name must be present in the local password
598 * This is supported by two various mechanisms in the
599 * individual modules. However, from the application's
600 * point of view, the template user is always passed
601 * back as a changed value of the PAM_USER item.
603 if ((e
= pam_get_item(pamh
, PAM_USER
, &item
)) ==
607 syslog(LOG_ERR
, "Couldn't get PAM_USER: %s",
608 pam_strerror(pamh
, e
));
609 #if 0 /* pam_securetty(8) should be used to enforce this */
610 if (isroot(name
) && !rootterm(line
))
618 case PAM_USER_UNKNOWN
:
624 syslog(LOG_ERR
, "auth_pam: %s", pam_strerror(pamh
, e
));
629 if ((e
= pam_end(pamh
, e
)) != PAM_SUCCESS
) {
630 syslog(LOG_ERR
, "pam_end: %s", pam_strerror(pamh
, e
));
638 #endif /* ENCRYPTION */