xref: /titanic_50/usr/src/lib/libc/port/stdio/getpass.c (revision b8bf75cb74c7cf11bde9dc90fb021429968d7654)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*	Copyright (c) 1988 AT&T	*/
29 /*	  All Rights Reserved  	*/
30 
31 
32 #pragma weak getpass = _getpass
33 #pragma weak getpassphrase = _getpassphrase
34 
35 #include "synonyms.h"
36 #include "file64.h"
37 #include "mtlib.h"
38 #include <stdio.h>
39 #include <sys/types.h>
40 #include <signal.h>
41 #include <unistd.h>
42 #include <stropts.h>
43 #include <termio.h>
44 #include <thread.h>
45 #include <synch.h>
46 #include "libc.h"
47 #include "stdiom.h"
48 #include "tsd.h"
49 
50 static void catch(int);
51 static int intrupt;
52 static char *__getpass(const char *, int);
53 
54 #define	MAXPASSWD	256	/* max significant characters in password */
55 #define	SMLPASSWD	8	/* unix standard  characters in password */
56 
57 
58 char *
59 getpass(const char *prompt)
60 {
61 	return ((char *)__getpass(prompt, SMLPASSWD));
62 }
63 
64 char *
65 getpassphrase(const char *prompt)
66 {
67 	return ((char *)__getpass(prompt, MAXPASSWD));
68 }
69 
70 static char *
71 __getpass(const char *prompt, int size)
72 {
73 	struct termio ttyb;
74 	unsigned short flags;
75 	char *p;
76 	int c;
77 	FILE	*fi;
78 	char *pbuf = tsdalloc(_T_GETPASS, MAXPASSWD + 1, NULL);
79 	void	(*sig)(int);
80 	rmutex_t *lk;
81 
82 	if (pbuf == NULL ||
83 	    (fi = fopen("/dev/tty", "rF")) == NULL)
84 		return (NULL);
85 	setbuf(fi, NULL);
86 	sig = signal(SIGINT, catch);
87 	intrupt = 0;
88 	(void) ioctl(FILENO(fi), TCGETA, &ttyb);
89 	flags = ttyb.c_lflag;
90 	ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
91 	(void) ioctl(FILENO(fi), TCSETAF, &ttyb);
92 	FLOCKFILE(lk, stderr);
93 	(void) fputs(prompt, stderr);
94 	p = pbuf;
95 	while (!intrupt &&
96 		(c = GETC(fi)) != '\n' && c != '\r' && c != EOF) {
97 		if (p < &pbuf[ size ])
98 			*p++ = (char)c;
99 	}
100 	*p = '\0';
101 	ttyb.c_lflag = flags;
102 	(void) ioctl(FILENO(fi), TCSETAW, &ttyb);
103 	(void) PUTC('\n', stderr);
104 	FUNLOCKFILE(lk);
105 	(void) signal(SIGINT, sig);
106 	(void) fclose(fi);
107 	if (intrupt)
108 		(void) kill(getpid(), SIGINT);
109 	return (pbuf);
110 }
111 
112 /* ARGSUSED */
113 static void
114 catch(int x)
115 {
116 	intrupt = 1;
117 }
118