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