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
30 * $FreeBSD: src/crypto/telnet/libtelnet/sra.c,v 1.1.2.7 2002/05/16 08:46:49 markm Exp $
31 * $DragonFly: src/crypto/telnet/libtelnet/sra.c,v 1.3 2006/01/17 23:50:34 dillon Exp $
36 #include <sys/types.h>
37 #include <arpa/telnet.h>
46 #include <security/pam_appl.h>
56 char pka[HEXKEYBYTES+1], ska[HEXKEYBYTES+1], pkb[HEXKEYBYTES+1];
57 char *user, *pass, *xuser, *xpass;
63 extern int auth_debug_mode;
66 static int sra_valid = 0;
67 static int passwd_sent = 0;
69 static unsigned char str_data[1024] = { IAC, SB, TELOPT_AUTHENTICATION, 0,
74 #define SRA_CONTINUE 2
79 static int check_user(char *, char *);
81 /* support routine to send out authentication message */
83 Data(Authenticator *ap, int type, void *d, int c)
85 unsigned char *p = str_data + 4;
86 unsigned char *cd = (unsigned char *)d;
89 c = strlen((char *)cd);
91 if (auth_debug_mode) {
92 printf("%s:%d: [%d] (%d)",
93 str_data[3] == TELQUAL_IS ? ">>>IS" : ">>>REPLY",
103 if ((*p++ = *cd++) == IAC)
108 if (str_data[3] == TELQUAL_IS)
109 printsub('>', &str_data[2], p - (&str_data[2]));
110 return(net_write(str_data, p - str_data));
114 sra_init(Authenticator *ap __unused, int server)
117 str_data[3] = TELQUAL_REPLY;
119 str_data[3] = TELQUAL_IS;
121 user = (char *)malloc(256);
122 xuser = (char *)malloc(513);
123 pass = (char *)malloc(PASS_SIZE);
124 xpass = (char *)malloc(513);
126 if (user == NULL || xuser == NULL || pass == NULL || xpass ==
128 return 0; /* malloc failed */
136 /* client received a go-ahead for sra */
138 sra_send(Authenticator *ap)
143 printf("Sent PKA to server.\r\n" );
144 printf("Trying SRA secure login:\r\n");
145 if (!Data(ap, SRA_KEY, (void *)pka, HEXKEYBYTES)) {
147 printf("Not enough room for authentication data\r\n");
154 /* server received an IS -- could be SRA KEY, USER, or PASS */
156 sra_is(Authenticator *ap, unsigned char *data, int cnt)
166 if (cnt < HEXKEYBYTES) {
167 Data(ap, SRA_REJECT, (void *)0, 0);
168 auth_finished(ap, AUTH_USER);
169 if (auth_debug_mode) {
170 printf("SRA user rejected for bad PKB\r\n");
175 printf("Sent pka\r\n");
176 if (!Data(ap, SRA_KEY, (void *)pka, HEXKEYBYTES)) {
178 printf("Not enough room\r\n");
181 memcpy(pkb,data,HEXKEYBYTES);
182 pkb[HEXKEYBYTES] = '\0';
183 common_key(ska,pkb,&ik,&ck);
188 if (cnt > 512) /* Attempted buffer overflow */
190 memcpy(xuser,data,cnt);
192 pk_decode(xuser,user,&ck);
193 auth_encrypt_user(user);
194 Data(ap, SRA_CONTINUE, (void *)0, 0);
199 if (cnt > 512) /* Attempted buffer overflow */
202 memcpy(xpass,data,cnt);
204 pk_decode(xpass,pass,&ck);
206 /* check user's password */
207 valid = check_user(user,pass);
210 Data(ap, SRA_ACCEPT, (void *)0, 0);
214 encrypt_session_key(&skey, 1);
217 auth_finished(ap, AUTH_VALID);
218 if (auth_debug_mode) {
219 printf("SRA user accepted\r\n");
223 Data(ap, SRA_CONTINUE, (void *)0, 0);
225 Data(ap, SRA_REJECT, (void *)0, 0);
227 auth_finished(ap, AUTH_REJECT);
229 if (auth_debug_mode) {
230 printf("SRA user failed\r\n");
237 printf("Unknown SRA option %d\r\n", data[-1]);
240 Data(ap, SRA_REJECT, 0, 0);
242 auth_finished(ap, AUTH_REJECT);
245 /* client received REPLY -- could be SRA KEY, CONTINUE, ACCEPT, or REJECT */
247 sra_reply(Authenticator *ap, unsigned char *data, int cnt)
249 char uprompt[256],tuser[256];
258 /* calculate common key */
259 if (cnt < HEXKEYBYTES) {
260 if (auth_debug_mode) {
261 printf("SRA user rejected for bad PKB\r\n");
265 memcpy(pkb,data,HEXKEYBYTES);
266 pkb[HEXKEYBYTES] = '\0';
268 common_key(ska,pkb,&ik,&ck);
273 memset(tuser,0,sizeof(tuser));
274 sprintf(uprompt,"User (%s): ",UserNameRequested);
275 telnet_gets(uprompt,tuser,255,1);
276 if (tuser[0] == '\n' || tuser[0] == '\r' )
277 strcpy(user,UserNameRequested);
279 /* telnet_gets leaves the newline on */
280 for(i=0;i<sizeof(tuser);i++) {
281 if (tuser[i] == '\n') {
288 pk_encode(user,xuser,&ck);
292 printf("Sent KAB(U)\r\n");
293 if (!Data(ap, SRA_USER, (void *)xuser, strlen(xuser))) {
295 printf("Not enough room\r\n");
303 printf("[ SRA login failed ]\r\n");
306 /* encode password */
307 memset(pass,0,PASS_SIZE);
308 telnet_gets("Password: ",pass,PASS_SIZE-1,0);
309 pk_encode(pass,xpass,&ck);
312 printf("Sent KAB(P)\r\n");
313 if (!Data(ap, SRA_PASS, (void *)xpass, strlen(xpass))) {
315 printf("Not enough room\r\n");
322 printf("[ SRA refuses authentication ]\r\n");
323 printf("Trying plaintext login:\r\n");
324 auth_finished(0,AUTH_REJECT);
328 printf("[ SRA accepts you ]\r\n");
332 encrypt_session_key(&skey, 0);
334 auth_finished(ap, AUTH_VALID);
338 printf("Unknown SRA option %d\r\n", data[-1]);
344 sra_status(Authenticator *ap __unused, char *name, int level)
346 if (level < AUTH_USER)
348 if (UserNameRequested && sra_valid) {
349 strcpy(name, UserNameRequested);
355 #define BUMP(buf, len) while (*(buf)) {++(buf), --(len);}
356 #define ADDC(buf, len, c) if ((len) > 0) {*(buf)++ = (c); --(len);}
359 sra_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
364 buf[buflen-1] = '\0'; /* make sure its NULL terminated */
370 strncpy((char *)buf, " CONTINUE ", buflen);
373 case SRA_REJECT: /* Rejected (reason might follow) */
374 strncpy((char *)buf, " REJECT ", buflen);
377 case SRA_ACCEPT: /* Accepted (name might follow) */
378 strncpy((char *)buf, " ACCEPT ", buflen);
384 ADDC(buf, buflen, '"');
385 for (i = 4; i < cnt; i++)
386 ADDC(buf, buflen, data[i]);
387 ADDC(buf, buflen, '"');
388 ADDC(buf, buflen, '\0');
391 case SRA_KEY: /* Authentication data follows */
392 strncpy((char *)buf, " KEY ", buflen);
396 strncpy((char *)buf, " USER ", buflen);
400 strncpy((char *)buf, " PASS ", buflen);
404 sprintf(lbuf, " %d (unknown)", data[3]);
405 strncpy((char *)buf, lbuf, buflen);
408 for (i = 4; i < cnt; i++) {
409 sprintf(lbuf, " %d", data[i]);
410 strncpy((char *)buf, lbuf, buflen);
418 isroot(const char *usr)
422 if ((pwd=getpwnam(usr))==NULL)
424 return (!pwd->pw_uid);
432 return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE);
437 check_user(char *name, char *cred)
440 char *xpasswd, *salt;
442 if (isroot(name) && !rootterm(line))
444 crypt("AA","*"); /* Waste some time to simulate success */
448 if (pw = sgetpwnam(name)) {
449 if (pw->pw_shell == NULL) {
450 pw = (struct passwd *) NULL;
454 salt = pw->pw_passwd;
455 xpasswd = crypt(cred, salt);
456 /* The strcmp does not catch null passwords! */
457 if (pw == NULL || *pw->pw_passwd == '\0' ||
458 strcmp(xpasswd, pw->pw_passwd)) {
459 pw = (struct passwd *) NULL;
469 * The following is stolen from ftpd, which stole it from the imap-uw
470 * PAM module and login.c. It is needed because we can't really
471 * "converse" with the user, having already gone to the trouble of
472 * getting their username and password through an encrypted channel.
475 #define COPY_STRING(s) (s ? strdup(s):NULL)
481 typedef struct cred_t cred_t;
484 auth_conv(int num_msg, const struct pam_message **msg, struct pam_response **resp, void *appdata)
487 cred_t *cred = (cred_t *) appdata;
488 struct pam_response *reply =
489 malloc(sizeof(struct pam_response) * num_msg);
494 for (i = 0; i < num_msg; i++) {
495 switch (msg[i]->msg_style) {
496 case PAM_PROMPT_ECHO_ON: /* assume want user name */
497 reply[i].resp_retcode = PAM_SUCCESS;
498 reply[i].resp = COPY_STRING(cred->uname);
499 /* PAM frees resp. */
501 case PAM_PROMPT_ECHO_OFF: /* assume want password */
502 reply[i].resp_retcode = PAM_SUCCESS;
503 reply[i].resp = COPY_STRING(cred->pass);
504 /* PAM frees resp. */
508 reply[i].resp_retcode = PAM_SUCCESS;
509 reply[i].resp = NULL;
511 default: /* unknown message style */
522 * The PAM version as a side effect may put a new username in *name.
525 check_user(char *name, char *cred)
527 pam_handle_t *pamh = NULL;
531 cred_t auth_cred = { name, cred };
532 struct pam_conv conv = { &auth_conv, &auth_cred };
534 e = pam_start("telnetd", name, &conv, &pamh);
535 if (e != PAM_SUCCESS) {
536 syslog(LOG_ERR, "pam_start: %s", pam_strerror(pamh, e));
540 #if 0 /* Where can we find this value? */
541 e = pam_set_item(pamh, PAM_RHOST, remotehost);
542 if (e != PAM_SUCCESS) {
543 syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s",
544 pam_strerror(pamh, e));
549 e = pam_authenticate(pamh, 0);
553 * With PAM we support the concept of a "template"
554 * user. The user enters a login name which is
555 * authenticated by PAM, usually via a remote service
556 * such as RADIUS or TACACS+. If authentication
557 * succeeds, a different but related "template" name
558 * is used for setting the credentials, shell, and
559 * home directory. The name the user enters need only
560 * exist on the remote authentication server, but the
561 * template name must be present in the local password
564 * This is supported by two various mechanisms in the
565 * individual modules. However, from the application's
566 * point of view, the template user is always passed
567 * back as a changed value of the PAM_USER item.
569 if ((e = pam_get_item(pamh, PAM_USER, &item)) ==
573 syslog(LOG_ERR, "Couldn't get PAM_USER: %s",
574 pam_strerror(pamh, e));
575 if (isroot(name) && !rootterm(line))
582 case PAM_USER_UNKNOWN:
588 syslog(LOG_ERR, "auth_pam: %s", pam_strerror(pamh, e));
593 if ((e = pam_end(pamh, e)) != PAM_SUCCESS) {
594 syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e));
602 #endif /* ENCRYPTION */