1 /* $NetBSD: getpass.c,v 1.4 2024/03/04 17:10:33 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-2021 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.4 2024/03/04 17:10:33 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 #if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H) 90 if ((fi = fopen(TTY, "r")) == NULL) 91 fi = stdin; 92 else 93 setbuf(fi, (char *)NULL); 94 if (fi != stdin) { 95 if (GETATTR(fileno(fi), &ttyb) < 0) 96 perror("GETATTR"); 97 sig = SIGNAL (SIGINT, SIG_IGN); 98 flags = GETFLAGS( ttyb ); 99 SETFLAGS( ttyb, flags & ~ECHO ); 100 if (SETATTR(fileno(fi), &ttyb) < 0) 101 perror("SETATTR"); 102 } 103 #else 104 fi = stdin; 105 #endif 106 fprintf(stderr, "%s", prompt); 107 fflush(stderr); 108 i = 0; 109 while ( (c = getc(fi)) != EOF && c != '\n' && c != '\r' ) 110 if ( i < (sizeof(pbuf)-1) ) 111 pbuf[i++] = c; 112 #if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H) 113 /* tidy up */ 114 if (fi != stdin) { 115 fprintf(stderr, "\n"); 116 fflush(stderr); 117 SETFLAGS( ttyb, flags ); 118 if (SETATTR(fileno(fi), &ttyb) < 0) 119 perror("SETATTR"); 120 (void) SIGNAL (SIGINT, sig); 121 (void) fclose(fi); 122 } 123 #endif 124 if ( c == EOF ) 125 return( NULL ); 126 pbuf[i] = '\0'; 127 return (pbuf); 128 } 129 130 #endif /* !NEED_GETPASSPHRASE */ 131