1 /* $NetBSD: getpass.c,v 1.2 2020/08/11 13:15:39 christos Exp $ */ 2 3 /* getpass.c -- get password from user */ 4 /* $OpenLDAP$ */ 5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 6 * 7 * Copyright 1998-2020 The OpenLDAP Foundation. 8 * Portions Copyright 1998-2003 Kurt D. Zeilenga. 9 * Portions Copyright 2009 Howard Chu. 10 * All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted only as authorized by the OpenLDAP 14 * Public License. 15 * 16 * A copy of this license is available in the file LICENSE in the 17 * top-level directory of the distribution or, alternatively, at 18 * <http://www.OpenLDAP.org/license.html>. 19 */ 20 /* Portions Copyright (c) 1992, 1993 Regents of the University of Michigan. 21 * All rights reserved. 22 * 23 * Redistribution and use in source and binary forms are permitted 24 * provided that this notice is preserved and that due credit is given 25 * to the University of Michigan at Ann Arbor. The name of the University 26 * may not be used to endorse or promote products derived from this 27 * software without specific prior written permission. This software 28 * is provided ``as is'' without express or implied warranty. 29 */ 30 /* This work was originally developed by the University of Michigan 31 * and distributed as part of U-MICH LDAP. It was adapted for use in 32 * -llutil by Kurt D. Zeilenga and subsequently rewritten by Howard Chu. 33 */ 34 35 #include <sys/cdefs.h> 36 __RCSID("$NetBSD: getpass.c,v 1.2 2020/08/11 13:15:39 christos Exp $"); 37 38 #include "portable.h" 39 40 #include <stdio.h> 41 42 #include <ac/stdlib.h> 43 44 #include <ac/ctype.h> 45 #include <ac/signal.h> 46 #include <ac/string.h> 47 #include <ac/termios.h> 48 #include <ac/time.h> 49 #include <ac/unistd.h> 50 51 #ifndef HAVE_GETPASSPHRASE 52 53 #ifdef HAVE_FCNTL_H 54 #include <fcntl.h> 55 #endif 56 57 #ifdef HAVE_CONIO_H 58 #include <conio.h> 59 #endif 60 61 #include <lber.h> 62 #include <ldap.h> 63 64 #include "ldap_defaults.h" 65 66 #define PBUF 512 67 68 #ifdef HAVE_WINSOCK 69 #define TTY "con:" 70 #else 71 #define TTY "/dev/tty" 72 #endif 73 74 char * 75 lutil_getpass( const char *prompt ) 76 { 77 static char pbuf[PBUF]; 78 FILE *fi; 79 int c; 80 unsigned i; 81 #if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H) 82 TERMIO_TYPE ttyb; 83 TERMFLAG_TYPE flags; 84 RETSIGTYPE (*sig)( int sig ); 85 #endif 86 87 if( prompt == NULL ) prompt = _("Password: "); 88 89 #ifdef DEBUG 90 if (debug & D_TRACE) 91 printf("->getpass(%s)\n", prompt); 92 #endif 93 94 #if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H) 95 if ((fi = fopen(TTY, "r")) == NULL) 96 fi = stdin; 97 else 98 setbuf(fi, (char *)NULL); 99 if (fi != stdin) { 100 if (GETATTR(fileno(fi), &ttyb) < 0) 101 perror("GETATTR"); 102 sig = SIGNAL (SIGINT, SIG_IGN); 103 flags = GETFLAGS( ttyb ); 104 SETFLAGS( ttyb, flags & ~ECHO ); 105 if (SETATTR(fileno(fi), &ttyb) < 0) 106 perror("SETATTR"); 107 } 108 #else 109 fi = stdin; 110 #endif 111 fprintf(stderr, "%s", prompt); 112 fflush(stderr); 113 i = 0; 114 while ( (c = getc(fi)) != EOF && c != '\n' && c != '\r' ) 115 if ( i < (sizeof(pbuf)-1) ) 116 pbuf[i++] = c; 117 #if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H) 118 /* tidy up */ 119 if (fi != stdin) { 120 fprintf(stderr, "\n"); 121 fflush(stderr); 122 SETFLAGS( ttyb, flags ); 123 if (SETATTR(fileno(fi), &ttyb) < 0) 124 perror("SETATTR"); 125 (void) SIGNAL (SIGINT, sig); 126 (void) fclose(fi); 127 } 128 #endif 129 if ( c == EOF ) 130 return( NULL ); 131 pbuf[i] = '\0'; 132 return (pbuf); 133 } 134 135 #endif /* !NEED_GETPASSPHRASE */ 136