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
29 * $FreeBSD: src/crypto/telnet/libtelnet/sra.c,v 1.1.2.7 2002/05/16 08:46:49 markm Exp $
34 #include <sys/types.h>
35 #include <arpa/telnet.h>
44 #include <security/pam_appl.h>
54 char pka[HEXKEYBYTES+1], ska[HEXKEYBYTES+1], pkb[HEXKEYBYTES+1];
55 char *user, *pass, *xuser, *xpass;
59 extern int auth_debug_mode;
62 static int sra_valid = 0;
63 static int passwd_sent = 0;
65 static unsigned char str_data[1024] = { IAC, SB, TELOPT_AUTHENTICATION, 0,
69 #define XSMALL_LEN 513
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 = malloc(SMALL_LEN);
121 xuser = malloc(XSMALL_LEN);
122 pass = malloc(SMALL_LEN);
123 xpass = malloc(XSMALL_LEN);
125 if (user == NULL || xuser == NULL || pass == NULL || xpass == NULL)
126 return 0; /* malloc failed */
134 /* client received a go-ahead for sra */
136 sra_send(Authenticator *ap)
141 printf("Sent PKA to server.\r\n" );
142 printf("Trying SRA secure login:\r\n");
143 if (!Data(ap, SRA_KEY, (void *)pka, HEXKEYBYTES)) {
145 printf("Not enough room for authentication data\r\n");
152 /* server received an IS -- could be SRA KEY, USER, or PASS */
154 sra_is(Authenticator *ap, unsigned char *data, int cnt)
164 if (cnt < HEXKEYBYTES) {
165 Data(ap, SRA_REJECT, (void *)0, 0);
166 auth_finished(ap, AUTH_USER);
167 if (auth_debug_mode) {
168 printf("SRA user rejected for bad PKB\r\n");
173 printf("Sent pka\r\n");
174 if (!Data(ap, SRA_KEY, (void *)pka, HEXKEYBYTES)) {
176 printf("Not enough room\r\n");
179 memcpy(pkb,data,HEXKEYBYTES);
180 pkb[HEXKEYBYTES] = '\0';
181 common_key(ska,pkb,&ik,&ck);
186 if (cnt > XSMALL_LEN - 1) /* Attempted buffer overflow */
188 memcpy(xuser,data,cnt);
190 pk_decode(xuser,user,&ck);
191 auth_encrypt_user(user);
192 Data(ap, SRA_CONTINUE, (void *)0, 0);
197 if (cnt > XSMALL_LEN - 1) /* Attempted buffer overflow */
200 memcpy(xpass,data,cnt);
202 pk_decode(xpass,pass,&ck);
204 /* check user's password */
205 valid = check_user(user,pass);
208 Data(ap, SRA_ACCEPT, (void *)0, 0);
212 encrypt_session_key(&skey, 1);
215 auth_finished(ap, AUTH_VALID);
216 if (auth_debug_mode) {
217 printf("SRA user accepted\r\n");
221 Data(ap, SRA_CONTINUE, (void *)0, 0);
223 Data(ap, SRA_REJECT, (void *)0, 0);
225 auth_finished(ap, AUTH_REJECT);
227 if (auth_debug_mode) {
228 printf("SRA user failed\r\n");
235 printf("Unknown SRA option %d\r\n", data[-1]);
238 Data(ap, SRA_REJECT, 0, 0);
240 auth_finished(ap, AUTH_REJECT);
243 /* client received REPLY -- could be SRA KEY, CONTINUE, ACCEPT, or REJECT */
245 sra_reply(Authenticator *ap, unsigned char *data, int cnt)
247 char uprompt[SMALL_LEN], tuser[SMALL_LEN];
256 /* calculate common key */
257 if (cnt < HEXKEYBYTES) {
258 if (auth_debug_mode) {
259 printf("SRA user rejected for bad PKB\r\n");
263 memcpy(pkb,data,HEXKEYBYTES);
264 pkb[HEXKEYBYTES] = '\0';
266 common_key(ska,pkb,&ik,&ck);
271 memset(tuser,0,sizeof(tuser));
272 sprintf(uprompt,"User (%s): ",UserNameRequested);
273 if (telnet_gets(uprompt, tuser, SMALL_LEN - 1, 1) == NULL) {
277 if (tuser[0] == '\n' || tuser[0] == '\r' )
278 strlcpy(user, UserNameRequested, SMALL_LEN);
280 /* telnet_gets leaves the newline on */
281 for(i = 0; i < sizeof(tuser); i++) {
282 if (tuser[i] == '\n') {
287 strlcpy(user, tuser, SMALL_LEN);
289 pk_encode(user,xuser,&ck);
293 printf("Sent KAB(U)\r\n");
294 if (!Data(ap, SRA_USER, xuser, strlen(xuser))) {
296 printf("Not enough room\r\n");
304 printf("[ SRA login failed ]\r\n");
307 /* encode password */
308 memset(pass, 0, SMALL_LEN);
309 if (telnet_gets("Password: ", pass, SMALL_LEN-1, 0) == NULL) {
313 pk_encode(pass, xpass, &ck);
316 printf("Sent KAB(P)\r\n");
317 if (!Data(ap, SRA_PASS, xpass, strlen(xpass))) {
319 printf("Not enough room\r\n");
326 printf("[ SRA refuses authentication ]\r\n");
327 printf("Trying plaintext login:\r\n");
328 auth_finished(0,AUTH_REJECT);
332 printf("[ SRA accepts you ]\r\n");
336 encrypt_session_key(&skey, 0);
338 auth_finished(ap, AUTH_VALID);
342 printf("Unknown SRA option %d\r\n", data[-1]);
348 sra_status(Authenticator *ap __unused, char *name, int level)
350 if (level < AUTH_USER)
352 if (UserNameRequested && sra_valid) {
353 strcpy(name, UserNameRequested);
359 #define BUMP(buf, len) while (*(buf)) {++(buf), --(len);}
360 #define ADDC(buf, len, c) if ((len) > 0) {*(buf)++ = (c); --(len);}
363 sra_printsub(unsigned char *data, int cnt, unsigned char *ubuf, int buflen)
365 char lbuf[32], *buf = (char *)ubuf;
368 buf[buflen-1] = '\0'; /* make sure its NULL terminated */
374 strncpy(buf, " CONTINUE ", buflen);
377 case SRA_REJECT: /* Rejected (reason might follow) */
378 strncpy(buf, " REJECT ", buflen);
381 case SRA_ACCEPT: /* Accepted (name might follow) */
382 strncpy(buf, " ACCEPT ", buflen);
388 ADDC(buf, buflen, '"');
389 for (i = 4; i < cnt; i++)
390 ADDC(buf, buflen, data[i]);
391 ADDC(buf, buflen, '"');
392 ADDC(buf, buflen, '\0');
395 case SRA_KEY: /* Authentication data follows */
396 strncpy(buf, " KEY ", buflen);
400 strncpy(buf, " USER ", buflen);
404 strncpy(buf, " PASS ", buflen);
408 snprintf(lbuf, sizeof(lbuf), " %d (unknown)", data[3]);
409 strncpy(buf, lbuf, buflen);
412 for (i = 4; i < cnt; i++) {
413 snprintf(lbuf, sizeof(lbuf), " %d", data[i]);
414 strncpy(buf, lbuf, buflen);
422 isroot(const char *usr)
426 if ((pwd=getpwnam(usr))==NULL)
428 return (!pwd->pw_uid);
436 return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE);
441 check_user(char *name, char *cred)
443 char *xpasswd, *salt;
445 if (isroot(name) && !rootterm(line))
447 crypt("AA","*"); /* Waste some time to simulate success */
451 if (pw = sgetpwnam(name)) {
452 if (pw->pw_shell == NULL) {
457 salt = pw->pw_passwd;
458 xpasswd = crypt(cred, salt);
459 /* The strcmp does not catch null passwords! */
460 if (pw == NULL || *pw->pw_passwd == '\0' ||
461 strcmp(xpasswd, pw->pw_passwd)) {
472 * The following is stolen from ftpd, which stole it from the imap-uw
473 * PAM module and login.c. It is needed because we can't really
474 * "converse" with the user, having already gone to the trouble of
475 * getting their username and password through an encrypted channel.
478 #define COPY_STRING(s) (s ? strdup(s) : NULL)
484 typedef struct cred_t cred_t;
487 auth_conv(int num_msg, const struct pam_message **msg,
488 struct pam_response **resp, void *appdata)
491 cred_t *cred = appdata;
492 struct pam_response *reply = malloc(sizeof(*reply) * num_msg);
497 for (i = 0; i < num_msg; i++) {
498 switch (msg[i]->msg_style) {
499 case PAM_PROMPT_ECHO_ON: /* assume want user name */
500 reply[i].resp_retcode = PAM_SUCCESS;
501 reply[i].resp = COPY_STRING(cred->uname);
502 /* PAM frees resp. */
504 case PAM_PROMPT_ECHO_OFF: /* assume want password */
505 reply[i].resp_retcode = PAM_SUCCESS;
506 reply[i].resp = COPY_STRING(cred->pass);
507 /* PAM frees resp. */
511 reply[i].resp_retcode = PAM_SUCCESS;
512 reply[i].resp = NULL;
514 default: /* unknown message style */
525 * The PAM version as a side effect may put a new username in *name.
528 check_user(char *name, char *cred)
530 pam_handle_t *pamh = NULL;
534 cred_t auth_cred = { name, cred };
535 struct pam_conv conv = { &auth_conv, &auth_cred };
537 e = pam_start("telnetd", name, &conv, &pamh);
538 if (e != PAM_SUCCESS) {
539 syslog(LOG_ERR, "pam_start: %s", pam_strerror(pamh, e));
543 #if 0 /* Where can we find this value? */
544 e = pam_set_item(pamh, PAM_RHOST, remotehost);
545 if (e != PAM_SUCCESS) {
546 syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s",
547 pam_strerror(pamh, e));
552 e = pam_authenticate(pamh, 0);
556 * With PAM we support the concept of a "template"
557 * user. The user enters a login name which is
558 * authenticated by PAM, usually via a remote service
559 * such as RADIUS or TACACS+. If authentication
560 * succeeds, a different but related "template" name
561 * is used for setting the credentials, shell, and
562 * home directory. The name the user enters need only
563 * exist on the remote authentication server, but the
564 * template name must be present in the local password
567 * This is supported by two various mechanisms in the
568 * individual modules. However, from the application's
569 * point of view, the template user is always passed
570 * back as a changed value of the PAM_USER item.
572 if ((e = pam_get_item(pamh, PAM_USER, &item)) ==
574 strlcpy(name, item, SMALL_LEN);
576 syslog(LOG_ERR, "Couldn't get PAM_USER: %s",
577 pam_strerror(pamh, e));
578 if (isroot(name) && !rootterm(line))
585 case PAM_USER_UNKNOWN:
591 syslog(LOG_ERR, "auth_pam: %s", pam_strerror(pamh, e));
596 if ((e = pam_end(pamh, e)) != PAM_SUCCESS) {
597 syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
605 #endif /* ENCRYPTION */