xref: /titanic_50/usr/src/lib/libc/port/stdio/getpass.c (revision e127a3e717f822eb855235fa3bd08235b2cf533d)
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 2007 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 
81 	if (pbuf == NULL ||
82 	    (fi = fopen("/dev/tty", "r+F")) == NULL)
83 		return (NULL);
84 	setbuf(fi, NULL);
85 	sig = signal(SIGINT, catch);
86 	intrupt = 0;
87 	(void) ioctl(FILENO(fi), TCGETA, &ttyb);
88 	flags = ttyb.c_lflag;
89 	ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
90 	(void) ioctl(FILENO(fi), TCSETAF, &ttyb);
91 	(void) fputs(prompt, fi);
92 	p = pbuf;
93 	while (!intrupt &&
94 		(c = GETC(fi)) != '\n' && c != '\r' && c != EOF) {
95 		if (p < &pbuf[ size ])
96 			*p++ = (char)c;
97 	}
98 	*p = '\0';
99 	(void) PUTC('\n', fi);
100 	ttyb.c_lflag = flags;
101 	(void) ioctl(FILENO(fi), TCSETAW, &ttyb);
102 	(void) signal(SIGINT, sig);
103 	(void) fclose(fi);
104 	if (intrupt)
105 		(void) kill(getpid(), SIGINT);
106 	return (pbuf);
107 }
108 
109 /* ARGSUSED */
110 static void
111 catch(int x)
112 {
113 	intrupt = 1;
114 }
115