17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5004388ebScasper * Common Development and Distribution License (the "License").
6004388ebScasper * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217257d1b4Sraf
227c478bd9Sstevel@tonic-gate /*
23*d67944fbSScott Rotondo * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307257d1b4Sraf #pragma weak _getpass = getpass
317257d1b4Sraf #pragma weak _getpassphrase = getpassphrase
327c478bd9Sstevel@tonic-gate
337257d1b4Sraf #include "lint.h"
347c478bd9Sstevel@tonic-gate #include "file64.h"
357c478bd9Sstevel@tonic-gate #include "mtlib.h"
367c478bd9Sstevel@tonic-gate #include <stdio.h>
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <signal.h>
397c478bd9Sstevel@tonic-gate #include <unistd.h>
407c478bd9Sstevel@tonic-gate #include <stropts.h>
417c478bd9Sstevel@tonic-gate #include <termio.h>
427c478bd9Sstevel@tonic-gate #include <thread.h>
437c478bd9Sstevel@tonic-gate #include <synch.h>
447c478bd9Sstevel@tonic-gate #include "libc.h"
457c478bd9Sstevel@tonic-gate #include "stdiom.h"
467c478bd9Sstevel@tonic-gate #include "tsd.h"
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate static int intrupt;
497c478bd9Sstevel@tonic-gate static char *__getpass(const char *, int);
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate #define MAXPASSWD 256 /* max significant characters in password */
527c478bd9Sstevel@tonic-gate #define SMLPASSWD 8 /* unix standard characters in password */
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate char *
getpass(const char * prompt)567c478bd9Sstevel@tonic-gate getpass(const char *prompt)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate return ((char *)__getpass(prompt, SMLPASSWD));
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate char *
getpassphrase(const char * prompt)627c478bd9Sstevel@tonic-gate getpassphrase(const char *prompt)
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate return ((char *)__getpass(prompt, MAXPASSWD));
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate
67*d67944fbSScott Rotondo static void catch(int);
68*d67944fbSScott Rotondo
697c478bd9Sstevel@tonic-gate static char *
__getpass(const char * prompt,int size)707c478bd9Sstevel@tonic-gate __getpass(const char *prompt, int size)
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate struct termio ttyb;
737c478bd9Sstevel@tonic-gate unsigned short flags;
747c478bd9Sstevel@tonic-gate char *p;
757c478bd9Sstevel@tonic-gate int c;
767c478bd9Sstevel@tonic-gate FILE *fi;
777c478bd9Sstevel@tonic-gate char *pbuf = tsdalloc(_T_GETPASS, MAXPASSWD + 1, NULL);
78cf171da2Scraigm struct sigaction act, osigint, osigtstp;
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate if (pbuf == NULL ||
81b952dcaaScraigm (fi = fopen("/dev/tty", "r+F")) == NULL)
827c478bd9Sstevel@tonic-gate return (NULL);
837c478bd9Sstevel@tonic-gate setbuf(fi, NULL);
84cf171da2Scraigm
857c478bd9Sstevel@tonic-gate intrupt = 0;
86cf171da2Scraigm act.sa_flags = 0;
87cf171da2Scraigm act.sa_handler = catch;
88cf171da2Scraigm (void) sigemptyset(&act.sa_mask);
89cf171da2Scraigm (void) sigaction(SIGINT, &act, &osigint); /* trap interrupt */
90cf171da2Scraigm act.sa_handler = SIG_IGN;
91cf171da2Scraigm (void) sigaction(SIGTSTP, &act, &osigtstp); /* ignore stop */
92cf171da2Scraigm (void) ioctl(fileno(fi), TCGETA, &ttyb);
937c478bd9Sstevel@tonic-gate flags = ttyb.c_lflag;
947c478bd9Sstevel@tonic-gate ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
95cf171da2Scraigm (void) ioctl(fileno(fi), TCSETAF, &ttyb);
96cf171da2Scraigm
97b952dcaaScraigm (void) fputs(prompt, fi);
987c478bd9Sstevel@tonic-gate p = pbuf;
997c478bd9Sstevel@tonic-gate while (!intrupt &&
1007c478bd9Sstevel@tonic-gate (c = GETC(fi)) != '\n' && c != '\r' && c != EOF) {
1017c478bd9Sstevel@tonic-gate if (p < &pbuf[ size ])
1027c478bd9Sstevel@tonic-gate *p++ = (char)c;
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate *p = '\0';
105b952dcaaScraigm (void) PUTC('\n', fi);
106cf171da2Scraigm
1077c478bd9Sstevel@tonic-gate ttyb.c_lflag = flags;
108cf171da2Scraigm (void) ioctl(fileno(fi), TCSETAW, &ttyb);
109cf171da2Scraigm (void) sigaction(SIGINT, &osigint, NULL);
110cf171da2Scraigm (void) sigaction(SIGTSTP, &osigtstp, NULL);
1117c478bd9Sstevel@tonic-gate (void) fclose(fi);
112cf171da2Scraigm if (intrupt) { /* if interrupted erase the input */
113cf171da2Scraigm pbuf[0] = '\0';
1147c478bd9Sstevel@tonic-gate (void) kill(getpid(), SIGINT);
115cf171da2Scraigm }
1167c478bd9Sstevel@tonic-gate return (pbuf);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate /* ARGSUSED */
1207c478bd9Sstevel@tonic-gate static void
catch(int x)1217c478bd9Sstevel@tonic-gate catch(int x)
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate intrupt = 1;
1247c478bd9Sstevel@tonic-gate }
125