1 /* getpass.c -- get password from user */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2018 The OpenLDAP Foundation.
6 * Portions Copyright 1998-2003 Kurt D. Zeilenga.
7 * Portions Copyright 2009 Howard Chu.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
18 /* Portions Copyright (c) 1992, 1993 Regents of the University of Michigan.
19 * All rights reserved.
21 * Redistribution and use in source and binary forms are permitted
22 * provided that this notice is preserved and that due credit is given
23 * to the University of Michigan at Ann Arbor. The name of the University
24 * may not be used to endorse or promote products derived from this
25 * software without specific prior written permission. This software
26 * is provided ``as is'' without express or implied warranty.
28 /* This work was originally developed by the University of Michigan
29 * and distributed as part of U-MICH LDAP. It was adapted for use in
30 * -llutil by Kurt D. Zeilenga and subsequently rewritten by Howard Chu.
37 #include <ac/stdlib.h>
40 #include <ac/signal.h>
41 #include <ac/string.h>
42 #include <ac/termios.h>
44 #include <ac/unistd.h>
46 #ifndef HAVE_GETPASSPHRASE
59 #include "ldap_defaults.h"
66 #define TTY "/dev/tty"
70 lutil_getpass( const char *prompt )
72 static char pbuf[PBUF];
76 #if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H)
79 RETSIGTYPE (*sig)( int sig );
82 if( prompt == NULL ) prompt = _("Password: ");
86 printf("->getpass(%s)\n", prompt);
89 #if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H)
90 if ((fi = fopen(TTY, "r")) == NULL)
93 setbuf(fi, (char *)NULL);
95 if (GETATTR(fileno(fi), &ttyb) < 0)
97 sig = SIGNAL (SIGINT, SIG_IGN);
98 flags = GETFLAGS( ttyb );
99 SETFLAGS( ttyb, flags & ~ECHO );
100 if (SETATTR(fileno(fi), &ttyb) < 0)
106 fprintf(stderr, "%s", prompt);
109 while ( (c = getc(fi)) != EOF && c != '\n' && c != '\r' )
110 if ( i < (sizeof(pbuf)-1) )
112 #if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H)
115 fprintf(stderr, "\n");
117 SETFLAGS( ttyb, flags );
118 if (SETATTR(fileno(fi), &ttyb) < 0)
120 (void) SIGNAL (SIGINT, sig);
130 #endif /* !NEED_GETPASSPHRASE */