Home | History | Annotate | Download | only in stdio
      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 2008 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 ident	"%Z%%M%	%I%	%E% SMI"
     31 
     32 #pragma weak _getpass = getpass
     33 #pragma weak _getpassphrase = getpassphrase
     34 
     35 #include "lint.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 int intrupt;
     51 static char *__getpass(const char *, int);
     52 
     53 #define	MAXPASSWD	256	/* max significant characters in password */
     54 #define	SMLPASSWD	8	/* unix standard  characters in password */
     55 
     56 
     57 char *
     58 getpass(const char *prompt)
     59 {
     60 	return ((char *)__getpass(prompt, SMLPASSWD));
     61 }
     62 
     63 char *
     64 getpassphrase(const char *prompt)
     65 {
     66 	return ((char *)__getpass(prompt, MAXPASSWD));
     67 }
     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 	static void catch(int);
     80 
     81 	if (pbuf == NULL ||
     82 	    (fi = fopen("/dev/tty", "r+F")) == NULL)
     83 		return (NULL);
     84 	setbuf(fi, NULL);
     85 
     86 	intrupt = 0;
     87 	act.sa_flags = 0;
     88 	act.sa_handler = catch;
     89 	(void) sigemptyset(&act.sa_mask);
     90 	(void) sigaction(SIGINT, &act, &osigint);	/* trap interrupt */
     91 	act.sa_handler = SIG_IGN;
     92 	(void) sigaction(SIGTSTP, &act, &osigtstp);	/* ignore stop */
     93 	(void) ioctl(fileno(fi), TCGETA, &ttyb);
     94 	flags = ttyb.c_lflag;
     95 	ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
     96 	(void) ioctl(fileno(fi), TCSETAF, &ttyb);
     97 
     98 	(void) fputs(prompt, fi);
     99 	p = pbuf;
    100 	while (!intrupt &&
    101 	    (c = GETC(fi)) != '\n' && c != '\r' && c != EOF) {
    102 		if (p < &pbuf[ size ])
    103 			*p++ = (char)c;
    104 	}
    105 	*p = '\0';
    106 	(void) PUTC('\n', fi);
    107 
    108 	ttyb.c_lflag = flags;
    109 	(void) ioctl(fileno(fi), TCSETAW, &ttyb);
    110 	(void) sigaction(SIGINT, &osigint, NULL);
    111 	(void) sigaction(SIGTSTP, &osigtstp, NULL);
    112 	(void) fclose(fi);
    113 	if (intrupt) {		/* if interrupted erase the input */
    114 		pbuf[0] = '\0';
    115 		(void) kill(getpid(), SIGINT);
    116 	}
    117 	return (pbuf);
    118 }
    119 
    120 /* ARGSUSED */
    121 static void
    122 catch(int x)
    123 {
    124 	intrupt = 1;
    125 }
    126