10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51914Scasper * Common Development and Distribution License (the "License").
61914Scasper * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
216812Sraf
220Sstevel@tonic-gate /*
23*9694SScott.Rotondo@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
306812Sraf #pragma weak _getpass = getpass
316812Sraf #pragma weak _getpassphrase = getpassphrase
320Sstevel@tonic-gate
336812Sraf #include "lint.h"
340Sstevel@tonic-gate #include "file64.h"
350Sstevel@tonic-gate #include "mtlib.h"
360Sstevel@tonic-gate #include <stdio.h>
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <signal.h>
390Sstevel@tonic-gate #include <unistd.h>
400Sstevel@tonic-gate #include <stropts.h>
410Sstevel@tonic-gate #include <termio.h>
420Sstevel@tonic-gate #include <thread.h>
430Sstevel@tonic-gate #include <synch.h>
440Sstevel@tonic-gate #include "libc.h"
450Sstevel@tonic-gate #include "stdiom.h"
460Sstevel@tonic-gate #include "tsd.h"
470Sstevel@tonic-gate
480Sstevel@tonic-gate static int intrupt;
490Sstevel@tonic-gate static char *__getpass(const char *, int);
500Sstevel@tonic-gate
510Sstevel@tonic-gate #define MAXPASSWD 256 /* max significant characters in password */
520Sstevel@tonic-gate #define SMLPASSWD 8 /* unix standard characters in password */
530Sstevel@tonic-gate
540Sstevel@tonic-gate
550Sstevel@tonic-gate char *
getpass(const char * prompt)560Sstevel@tonic-gate getpass(const char *prompt)
570Sstevel@tonic-gate {
580Sstevel@tonic-gate return ((char *)__getpass(prompt, SMLPASSWD));
590Sstevel@tonic-gate }
600Sstevel@tonic-gate
610Sstevel@tonic-gate char *
getpassphrase(const char * prompt)620Sstevel@tonic-gate getpassphrase(const char *prompt)
630Sstevel@tonic-gate {
640Sstevel@tonic-gate return ((char *)__getpass(prompt, MAXPASSWD));
650Sstevel@tonic-gate }
660Sstevel@tonic-gate
67*9694SScott.Rotondo@Sun.COM static void catch(int);
68*9694SScott.Rotondo@Sun.COM
690Sstevel@tonic-gate static char *
__getpass(const char * prompt,int size)700Sstevel@tonic-gate __getpass(const char *prompt, int size)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate struct termio ttyb;
730Sstevel@tonic-gate unsigned short flags;
740Sstevel@tonic-gate char *p;
750Sstevel@tonic-gate int c;
760Sstevel@tonic-gate FILE *fi;
770Sstevel@tonic-gate char *pbuf = tsdalloc(_T_GETPASS, MAXPASSWD + 1, NULL);
784199Scraigm struct sigaction act, osigint, osigtstp;
790Sstevel@tonic-gate
800Sstevel@tonic-gate if (pbuf == NULL ||
813611Scraigm (fi = fopen("/dev/tty", "r+F")) == NULL)
820Sstevel@tonic-gate return (NULL);
830Sstevel@tonic-gate setbuf(fi, NULL);
844199Scraigm
850Sstevel@tonic-gate intrupt = 0;
864199Scraigm act.sa_flags = 0;
874199Scraigm act.sa_handler = catch;
884199Scraigm (void) sigemptyset(&act.sa_mask);
894199Scraigm (void) sigaction(SIGINT, &act, &osigint); /* trap interrupt */
904199Scraigm act.sa_handler = SIG_IGN;
914199Scraigm (void) sigaction(SIGTSTP, &act, &osigtstp); /* ignore stop */
924199Scraigm (void) ioctl(fileno(fi), TCGETA, &ttyb);
930Sstevel@tonic-gate flags = ttyb.c_lflag;
940Sstevel@tonic-gate ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
954199Scraigm (void) ioctl(fileno(fi), TCSETAF, &ttyb);
964199Scraigm
973611Scraigm (void) fputs(prompt, fi);
980Sstevel@tonic-gate p = pbuf;
990Sstevel@tonic-gate while (!intrupt &&
1006812Sraf (c = GETC(fi)) != '\n' && c != '\r' && c != EOF) {
1010Sstevel@tonic-gate if (p < &pbuf[ size ])
1020Sstevel@tonic-gate *p++ = (char)c;
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate *p = '\0';
1053611Scraigm (void) PUTC('\n', fi);
1064199Scraigm
1070Sstevel@tonic-gate ttyb.c_lflag = flags;
1084199Scraigm (void) ioctl(fileno(fi), TCSETAW, &ttyb);
1094199Scraigm (void) sigaction(SIGINT, &osigint, NULL);
1104199Scraigm (void) sigaction(SIGTSTP, &osigtstp, NULL);
1110Sstevel@tonic-gate (void) fclose(fi);
1124199Scraigm if (intrupt) { /* if interrupted erase the input */
1134199Scraigm pbuf[0] = '\0';
1140Sstevel@tonic-gate (void) kill(getpid(), SIGINT);
1154199Scraigm }
1160Sstevel@tonic-gate return (pbuf);
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate /* ARGSUSED */
1200Sstevel@tonic-gate static void
catch(int x)1210Sstevel@tonic-gate catch(int x)
1220Sstevel@tonic-gate {
1230Sstevel@tonic-gate intrupt = 1;
1240Sstevel@tonic-gate }
125