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 $");
37 #include <sys/types.h>
38 #include <arpa/telnet.h>
47 #include <security/pam_appl.h>
57 char pka
[HEXKEYBYTES
+1], ska
[HEXKEYBYTES
+1], pkb
[HEXKEYBYTES
+1];
58 char *user
, *pass
, *xuser
, *xpass
;
62 extern int auth_debug_mode
;
65 static int sra_valid
= 0;
66 static int passwd_sent
= 0;
68 static unsigned char str_data
[1024] = { IAC
, SB
, TELOPT_AUTHENTICATION
, 0,
73 #define SRA_CONTINUE 2
78 static int check_user(char *, char *);
80 /* support routine to send out authentication message */
82 Data(Authenticator
*ap
, int type
, void *d
, int c
)
84 unsigned char *p
= str_data
+ 4;
85 unsigned char *cd
= (unsigned char *)d
;
88 c
= strlen((char *)cd
);
90 if (auth_debug_mode
) {
91 printf("%s:%d: [%d] (%d)",
92 str_data
[3] == TELQUAL_IS
? ">>>IS" : ">>>REPLY",
102 if ((*p
++ = *cd
++) == IAC
)
107 if (str_data
[3] == TELQUAL_IS
)
108 printsub('>', &str_data
[2], p
- (&str_data
[2]));
109 return(net_write(str_data
, p
- str_data
));
113 sra_init(Authenticator
*ap __unused
, int server
)
116 str_data
[3] = TELQUAL_REPLY
;
118 str_data
[3] = TELQUAL_IS
;
120 user
= (char *)malloc(256);
121 xuser
= (char *)malloc(513);
122 pass
= (char *)malloc(256);
123 xpass
= (char *)malloc(513);
125 if (user
== NULL
|| xuser
== NULL
|| pass
== NULL
|| xpass
==
127 return 0; /* malloc failed */
135 /* client received a go-ahead for sra */
137 sra_send(Authenticator
*ap
)
142 printf("Sent PKA to server.\r\n" );
143 printf("Trying SRA secure login:\r\n");
144 if (!Data(ap
, SRA_KEY
, (void *)pka
, HEXKEYBYTES
)) {
146 printf("Not enough room for authentication data\r\n");
153 /* server received an IS -- could be SRA KEY, USER, or PASS */
155 sra_is(Authenticator
*ap
, unsigned char *data
, int cnt
)
165 if (cnt
< HEXKEYBYTES
) {
166 Data(ap
, SRA_REJECT
, (void *)0, 0);
167 auth_finished(ap
, AUTH_USER
);
168 if (auth_debug_mode
) {
169 printf("SRA user rejected for bad PKB\r\n");
174 printf("Sent pka\r\n");
175 if (!Data(ap
, SRA_KEY
, (void *)pka
, HEXKEYBYTES
)) {
177 printf("Not enough room\r\n");
180 memcpy(pkb
,data
,HEXKEYBYTES
);
181 pkb
[HEXKEYBYTES
] = '\0';
182 common_key(ska
,pkb
,&ik
,&ck
);
187 if (cnt
> 512) /* Attempted buffer overflow */
189 memcpy(xuser
,data
,cnt
);
191 pk_decode(xuser
,user
,&ck
);
192 auth_encrypt_user(user
);
193 Data(ap
, SRA_CONTINUE
, (void *)0, 0);
198 if (cnt
> 512) /* Attempted buffer overflow */
201 memcpy(xpass
,data
,cnt
);
203 pk_decode(xpass
,pass
,&ck
);
205 /* check user's password */
206 valid
= check_user(user
,pass
);
209 Data(ap
, SRA_ACCEPT
, (void *)0, 0);
213 encrypt_session_key(&skey
, 1);
216 auth_finished(ap
, AUTH_VALID
);
217 if (auth_debug_mode
) {
218 printf("SRA user accepted\r\n");
222 Data(ap
, SRA_CONTINUE
, (void *)0, 0);
224 Data(ap, SRA_REJECT, (void *)0, 0);
226 auth_finished(ap, AUTH_REJECT);
228 if (auth_debug_mode
) {
229 printf("SRA user failed\r\n");
236 printf("Unknown SRA option %d\r\n", data
[-1]);
239 Data(ap
, SRA_REJECT
, 0, 0);
241 auth_finished(ap
, AUTH_REJECT
);
244 /* client received REPLY -- could be SRA KEY, CONTINUE, ACCEPT, or REJECT */
246 sra_reply(Authenticator
*ap
, unsigned char *data
, int cnt
)
248 char uprompt
[256],tuser
[256];
257 /* calculate common key */
258 if (cnt
< HEXKEYBYTES
) {
259 if (auth_debug_mode
) {
260 printf("SRA user rejected for bad PKB\r\n");
264 memcpy(pkb
,data
,HEXKEYBYTES
);
265 pkb
[HEXKEYBYTES
] = '\0';
267 common_key(ska
,pkb
,&ik
,&ck
);
272 memset(tuser
,0,sizeof(tuser
));
273 sprintf(uprompt
,"User (%s): ",UserNameRequested
);
274 telnet_gets(uprompt
,tuser
,255,1);
275 if (tuser
[0] == '\n' || tuser
[0] == '\r' )
276 strcpy(user
,UserNameRequested
);
278 /* telnet_gets leaves the newline on */
279 for(i
=0;i
<sizeof(tuser
);i
++) {
280 if (tuser
[i
] == '\n') {
287 pk_encode(user
,xuser
,&ck
);
291 printf("Sent KAB(U)\r\n");
292 if (!Data(ap
, SRA_USER
, (void *)xuser
, strlen(xuser
))) {
294 printf("Not enough room\r\n");
302 printf("[ SRA login failed ]\r\n");
305 /* encode password */
306 memset(pass
,0,sizeof(pass
));
307 telnet_gets("Password: ",pass
,255,0);
308 pk_encode(pass
,xpass
,&ck
);
311 printf("Sent KAB(P)\r\n");
312 if (!Data(ap
, SRA_PASS
, (void *)xpass
, strlen(xpass
))) {
314 printf("Not enough room\r\n");
321 printf("[ SRA refuses authentication ]\r\n");
322 printf("Trying plaintext login:\r\n");
323 auth_finished(0,AUTH_REJECT
);
327 printf("[ SRA accepts you ]\r\n");
331 encrypt_session_key(&skey
, 0);
333 auth_finished(ap
, AUTH_VALID
);
337 printf("Unknown SRA option %d\r\n", data
[-1]);
343 sra_status(Authenticator
*ap __unused
, char *name
, int level
)
345 if (level
< AUTH_USER
)
347 if (UserNameRequested
&& sra_valid
) {
348 strcpy(name
, UserNameRequested
);
354 #define BUMP(buf, len) while (*(buf)) {++(buf), --(len);}
355 #define ADDC(buf, len, c) if ((len) > 0) {*(buf)++ = (c); --(len);}
358 sra_printsub(unsigned char *data
, int cnt
, unsigned char *buf
, int buflen
)
363 buf
[buflen
-1] = '\0'; /* make sure its NULL terminated */
369 strncpy((char *)buf
, " CONTINUE ", buflen
);
372 case SRA_REJECT
: /* Rejected (reason might follow) */
373 strncpy((char *)buf
, " REJECT ", buflen
);
376 case SRA_ACCEPT
: /* Accepted (name might follow) */
377 strncpy((char *)buf
, " ACCEPT ", buflen
);
383 ADDC(buf
, buflen
, '"');
384 for (i
= 4; i
< cnt
; i
++)
385 ADDC(buf
, buflen
, data
[i
]);
386 ADDC(buf
, buflen
, '"');
387 ADDC(buf
, buflen
, '\0');
390 case SRA_KEY
: /* Authentication data follows */
391 strncpy((char *)buf
, " KEY ", buflen
);
395 strncpy((char *)buf
, " USER ", buflen
);
399 strncpy((char *)buf
, " PASS ", buflen
);
403 sprintf(lbuf
, " %d (unknown)", data
[3]);
404 strncpy((char *)buf
, lbuf
, buflen
);
407 for (i
= 4; i
< cnt
; i
++) {
408 sprintf(lbuf
, " %d", data
[i
]);
409 strncpy((char *)buf
, lbuf
, buflen
);
417 isroot(const char *usr
)
421 if ((pwd
=getpwnam(usr
))==NULL
)
423 return (!pwd
->pw_uid
);
431 return ((t
= getttynam(ttyn
)) && t
->ty_status
& TTY_SECURE
);
436 check_user(char *name
, char *cred
)
439 char *xpasswd
, *salt
;
441 if (isroot(name
) && !rootterm(line
))
443 crypt("AA","*"); /* Waste some time to simulate success */
447 if (pw
= sgetpwnam(name
)) {
448 if (pw
->pw_shell
== NULL
) {
449 pw
= (struct passwd
*) NULL
;
453 salt
= pw
->pw_passwd
;
454 xpasswd
= crypt(cred
, salt
);
455 /* The strcmp does not catch null passwords! */
456 if (pw
== NULL
|| *pw
->pw_passwd
== '\0' ||
457 strcmp(xpasswd
, pw
->pw_passwd
)) {
458 pw
= (struct passwd
*) NULL
;
468 * The following is stolen from ftpd, which stole it from the imap-uw
469 * PAM module and login.c. It is needed because we can't really
470 * "converse" with the user, having already gone to the trouble of
471 * getting their username and password through an encrypted channel.
474 #define COPY_STRING(s) (s ? strdup(s):NULL)
480 typedef struct cred_t cred_t
;
483 auth_conv(int num_msg
, const struct pam_message
**msg
, struct pam_response
**resp
, void *appdata
)
486 cred_t
*cred
= (cred_t
*) appdata
;
487 struct pam_response
*reply
=
488 malloc(sizeof(struct pam_response
) * num_msg
);
493 for (i
= 0; i
< num_msg
; i
++) {
494 switch (msg
[i
]->msg_style
) {
495 case PAM_PROMPT_ECHO_ON
: /* assume want user name */
496 reply
[i
].resp_retcode
= PAM_SUCCESS
;
497 reply
[i
].resp
= COPY_STRING(cred
->uname
);
498 /* PAM frees resp. */
500 case PAM_PROMPT_ECHO_OFF
: /* assume want password */
501 reply
[i
].resp_retcode
= PAM_SUCCESS
;
502 reply
[i
].resp
= COPY_STRING(cred
->pass
);
503 /* PAM frees resp. */
507 reply
[i
].resp_retcode
= PAM_SUCCESS
;
508 reply
[i
].resp
= NULL
;
510 default: /* unknown message style */
521 * The PAM version as a side effect may put a new username in *name.
524 check_user(char *name
, char *cred
)
526 pam_handle_t
*pamh
= NULL
;
530 cred_t auth_cred
= { name
, cred
};
531 struct pam_conv conv
= { &auth_conv
, &auth_cred
};
533 e
= pam_start("telnetd", name
, &conv
, &pamh
);
534 if (e
!= PAM_SUCCESS
) {
535 syslog(LOG_ERR
, "pam_start: %s", pam_strerror(pamh
, e
));
539 #if 0 /* Where can we find this value? */
540 e
= pam_set_item(pamh
, PAM_RHOST
, remotehost
);
541 if (e
!= PAM_SUCCESS
) {
542 syslog(LOG_ERR
, "pam_set_item(PAM_RHOST): %s",
543 pam_strerror(pamh
, e
));
548 e
= pam_authenticate(pamh
, 0);
552 * With PAM we support the concept of a "template"
553 * user. The user enters a login name which is
554 * authenticated by PAM, usually via a remote service
555 * such as RADIUS or TACACS+. If authentication
556 * succeeds, a different but related "template" name
557 * is used for setting the credentials, shell, and
558 * home directory. The name the user enters need only
559 * exist on the remote authentication server, but the
560 * template name must be present in the local password
563 * This is supported by two various mechanisms in the
564 * individual modules. However, from the application's
565 * point of view, the template user is always passed
566 * back as a changed value of the PAM_USER item.
568 if ((e
= pam_get_item(pamh
, PAM_USER
, &item
)) ==
572 syslog(LOG_ERR
, "Couldn't get PAM_USER: %s",
573 pam_strerror(pamh
, e
));
574 if (isroot(name
) && !rootterm(line
))
581 case PAM_USER_UNKNOWN
:
587 syslog(LOG_ERR
, "auth_pam: %s", pam_strerror(pamh
, e
));
592 if ((e
= pam_end(pamh
, e
)) != PAM_SUCCESS
) {
593 syslog(LOG_ERR
, "pam_end: %s", pam_strerror(pamh
, e
));
601 #endif /* ENCRYPTION */