xref: /titanic_52/usr/src/lib/libc/port/stdio/getpass.c (revision 6a1af1a67532df169a657cce07140be64bdea084)
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 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #pragma weak _getpass = getpass
31 #pragma weak _getpassphrase = getpassphrase
32 
33 #include "lint.h"
34 #include "file64.h"
35 #include "mtlib.h"
36 #include <stdio.h>
37 #include <sys/types.h>
38 #include <signal.h>
39 #include <unistd.h>
40 #include <stropts.h>
41 #include <termio.h>
42 #include <thread.h>
43 #include <synch.h>
44 #include "libc.h"
45 #include "stdiom.h"
46 #include "tsd.h"
47 
48 static int intrupt;
49 static char *__getpass(const char *, int);
50 
51 #define	MAXPASSWD	256	/* max significant characters in password */
52 #define	SMLPASSWD	8	/* unix standard  characters in password */
53 
54 
55 char *
56 getpass(const char *prompt)
57 {
58 	return ((char *)__getpass(prompt, SMLPASSWD));
59 }
60 
61 char *
62 getpassphrase(const char *prompt)
63 {
64 	return ((char *)__getpass(prompt, MAXPASSWD));
65 }
66 
67 	static void catch(int);
68 
69 static char *
70 __getpass(const char *prompt, int size)
71 {
72 	struct termio ttyb;
73 	unsigned short flags;
74 	char *p;
75 	int c;
76 	FILE	*fi;
77 	char *pbuf = tsdalloc(_T_GETPASS, MAXPASSWD + 1, NULL);
78 	struct sigaction act, osigint, osigtstp;
79 
80 	if (pbuf == NULL ||
81 	    (fi = fopen("/dev/tty", "r+F")) == NULL)
82 		return (NULL);
83 	setbuf(fi, NULL);
84 
85 	intrupt = 0;
86 	act.sa_flags = 0;
87 	act.sa_handler = catch;
88 	(void) sigemptyset(&act.sa_mask);
89 	(void) sigaction(SIGINT, &act, &osigint);	/* trap interrupt */
90 	act.sa_handler = SIG_IGN;
91 	(void) sigaction(SIGTSTP, &act, &osigtstp);	/* ignore stop */
92 	(void) ioctl(fileno(fi), TCGETA, &ttyb);
93 	flags = ttyb.c_lflag;
94 	ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
95 	(void) ioctl(fileno(fi), TCSETAF, &ttyb);
96 
97 	(void) fputs(prompt, fi);
98 	p = pbuf;
99 	while (!intrupt &&
100 	    (c = GETC(fi)) != '\n' && c != '\r' && c != EOF) {
101 		if (p < &pbuf[ size ])
102 			*p++ = (char)c;
103 	}
104 	*p = '\0';
105 	(void) PUTC('\n', fi);
106 
107 	ttyb.c_lflag = flags;
108 	(void) ioctl(fileno(fi), TCSETAW, &ttyb);
109 	(void) sigaction(SIGINT, &osigint, NULL);
110 	(void) sigaction(SIGTSTP, &osigtstp, NULL);
111 	(void) fclose(fi);
112 	if (intrupt) {		/* if interrupted erase the input */
113 		pbuf[0] = '\0';
114 		(void) kill(getpid(), SIGINT);
115 	}
116 	return (pbuf);
117 }
118 
119 /* ARGSUSED */
120 static void
121 catch(int x)
122 {
123 	intrupt = 1;
124 }
125