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  */
210Sstevel@tonic-gate /*
22*3611Scraigm  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
290Sstevel@tonic-gate /*	  All Rights Reserved  	*/
300Sstevel@tonic-gate 
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #pragma weak getpass = _getpass
330Sstevel@tonic-gate #pragma weak getpassphrase = _getpassphrase
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include "synonyms.h"
360Sstevel@tonic-gate #include "file64.h"
370Sstevel@tonic-gate #include "mtlib.h"
380Sstevel@tonic-gate #include <stdio.h>
390Sstevel@tonic-gate #include <sys/types.h>
400Sstevel@tonic-gate #include <signal.h>
410Sstevel@tonic-gate #include <unistd.h>
420Sstevel@tonic-gate #include <stropts.h>
430Sstevel@tonic-gate #include <termio.h>
440Sstevel@tonic-gate #include <thread.h>
450Sstevel@tonic-gate #include <synch.h>
460Sstevel@tonic-gate #include "libc.h"
470Sstevel@tonic-gate #include "stdiom.h"
480Sstevel@tonic-gate #include "tsd.h"
490Sstevel@tonic-gate 
500Sstevel@tonic-gate static void catch(int);
510Sstevel@tonic-gate static int intrupt;
520Sstevel@tonic-gate static char *__getpass(const char *, int);
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #define	MAXPASSWD	256	/* max significant characters in password */
550Sstevel@tonic-gate #define	SMLPASSWD	8	/* unix standard  characters in password */
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 
580Sstevel@tonic-gate char *
590Sstevel@tonic-gate getpass(const char *prompt)
600Sstevel@tonic-gate {
610Sstevel@tonic-gate 	return ((char *)__getpass(prompt, SMLPASSWD));
620Sstevel@tonic-gate }
630Sstevel@tonic-gate 
640Sstevel@tonic-gate char *
650Sstevel@tonic-gate getpassphrase(const char *prompt)
660Sstevel@tonic-gate {
670Sstevel@tonic-gate 	return ((char *)__getpass(prompt, MAXPASSWD));
680Sstevel@tonic-gate }
690Sstevel@tonic-gate 
700Sstevel@tonic-gate static char *
710Sstevel@tonic-gate __getpass(const char *prompt, int size)
720Sstevel@tonic-gate {
730Sstevel@tonic-gate 	struct termio ttyb;
740Sstevel@tonic-gate 	unsigned short flags;
750Sstevel@tonic-gate 	char *p;
760Sstevel@tonic-gate 	int c;
770Sstevel@tonic-gate 	FILE	*fi;
780Sstevel@tonic-gate 	char *pbuf = tsdalloc(_T_GETPASS, MAXPASSWD + 1, NULL);
790Sstevel@tonic-gate 	void	(*sig)(int);
800Sstevel@tonic-gate 	rmutex_t *lk;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	if (pbuf == NULL ||
83*3611Scraigm 	    (fi = fopen("/dev/tty", "r+F")) == NULL)
840Sstevel@tonic-gate 		return (NULL);
850Sstevel@tonic-gate 	setbuf(fi, NULL);
860Sstevel@tonic-gate 	sig = signal(SIGINT, catch);
870Sstevel@tonic-gate 	intrupt = 0;
880Sstevel@tonic-gate 	(void) ioctl(FILENO(fi), TCGETA, &ttyb);
890Sstevel@tonic-gate 	flags = ttyb.c_lflag;
900Sstevel@tonic-gate 	ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
910Sstevel@tonic-gate 	(void) ioctl(FILENO(fi), TCSETAF, &ttyb);
92*3611Scraigm 	(void) fputs(prompt, fi);
930Sstevel@tonic-gate 	p = pbuf;
940Sstevel@tonic-gate 	while (!intrupt &&
950Sstevel@tonic-gate 		(c = GETC(fi)) != '\n' && c != '\r' && c != EOF) {
960Sstevel@tonic-gate 		if (p < &pbuf[ size ])
970Sstevel@tonic-gate 			*p++ = (char)c;
980Sstevel@tonic-gate 	}
990Sstevel@tonic-gate 	*p = '\0';
100*3611Scraigm 	(void) PUTC('\n', fi);
1010Sstevel@tonic-gate 	ttyb.c_lflag = flags;
1020Sstevel@tonic-gate 	(void) ioctl(FILENO(fi), TCSETAW, &ttyb);
1030Sstevel@tonic-gate 	(void) signal(SIGINT, sig);
1040Sstevel@tonic-gate 	(void) fclose(fi);
1050Sstevel@tonic-gate 	if (intrupt)
1060Sstevel@tonic-gate 		(void) kill(getpid(), SIGINT);
1070Sstevel@tonic-gate 	return (pbuf);
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate /* ARGSUSED */
1110Sstevel@tonic-gate static void
1120Sstevel@tonic-gate catch(int x)
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate 	intrupt = 1;
1150Sstevel@tonic-gate }
116