2003-11-26 07:17:08 +08:00
|
|
|
/* getpass.c -- get password from user */
|
1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
2003-11-26 07:17:08 +08:00
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2013-01-03 02:20:30 +08:00
|
|
|
* Copyright 1998-2013 The OpenLDAP Foundation.
|
2003-11-26 07:17:08 +08:00
|
|
|
* Portions Copyright 1998-2003 Kurt D. Zeilenga.
|
2009-07-22 14:34:37 +08:00
|
|
|
* Portions Copyright 2009 Howard Chu.
|
2003-11-26 07:17:08 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
1999-08-18 03:00:59 +08:00
|
|
|
*/
|
2003-11-26 07:17:08 +08:00
|
|
|
/* Portions Copyright (c) 1992, 1993 Regents of the University of Michigan.
|
1999-02-12 05:16:03 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms are permitted
|
|
|
|
* provided that this notice is preserved and that due credit is given
|
|
|
|
* to the University of Michigan at Ann Arbor. The name of the University
|
|
|
|
* may not be used to endorse or promote products derived from this
|
|
|
|
* software without specific prior written permission. This software
|
|
|
|
* is provided ``as is'' without express or implied warranty.
|
|
|
|
*/
|
2003-11-26 07:17:08 +08:00
|
|
|
/* This work was originally developed by the University of Michigan
|
|
|
|
* and distributed as part of U-MICH LDAP. It was adapted for use in
|
2009-07-22 14:34:37 +08:00
|
|
|
* -llutil by Kurt D. Zeilenga and subsequently rewritten by Howard Chu.
|
2003-11-26 07:17:08 +08:00
|
|
|
*/
|
1999-02-12 05:16:03 +08:00
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
1999-06-03 08:37:44 +08:00
|
|
|
|
|
|
|
#include <ac/stdlib.h>
|
1999-02-12 05:16:03 +08:00
|
|
|
|
|
|
|
#include <ac/ctype.h>
|
|
|
|
#include <ac/signal.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/termios.h>
|
|
|
|
#include <ac/time.h>
|
|
|
|
#include <ac/unistd.h>
|
|
|
|
|
2009-07-22 14:34:37 +08:00
|
|
|
#ifndef HAVE_GETPASSPHRASE
|
2000-05-20 02:41:26 +08:00
|
|
|
|
1999-02-12 05:16:03 +08:00
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_CONIO_H
|
|
|
|
#include <conio.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <lber.h>
|
|
|
|
#include <ldap.h>
|
1999-08-04 02:14:24 +08:00
|
|
|
|
|
|
|
#include "ldap_defaults.h"
|
1999-02-12 05:16:03 +08:00
|
|
|
|
2009-07-22 14:34:37 +08:00
|
|
|
#define PBUF 512
|
|
|
|
|
|
|
|
#ifdef HAVE_WINSOCK
|
|
|
|
#define TTY "con:"
|
|
|
|
#else
|
|
|
|
#define TTY "/dev/tty"
|
|
|
|
#endif
|
|
|
|
|
1999-02-12 05:16:03 +08:00
|
|
|
char *
|
2000-05-20 02:41:26 +08:00
|
|
|
lutil_getpass( const char *prompt )
|
1999-02-12 05:16:03 +08:00
|
|
|
{
|
2009-07-22 14:34:37 +08:00
|
|
|
static char pbuf[PBUF];
|
|
|
|
FILE *fi;
|
2009-08-22 01:06:22 +08:00
|
|
|
int c;
|
|
|
|
unsigned i;
|
2009-07-22 14:34:37 +08:00
|
|
|
#if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H)
|
1999-02-12 05:16:03 +08:00
|
|
|
TERMIO_TYPE ttyb;
|
|
|
|
TERMFLAG_TYPE flags;
|
|
|
|
RETSIGTYPE (*sig)( int sig );
|
2009-07-22 14:34:37 +08:00
|
|
|
#endif
|
1999-02-12 05:16:03 +08:00
|
|
|
|
2003-04-06 14:47:31 +08:00
|
|
|
if( prompt == NULL ) prompt = _("Password: ");
|
2000-05-20 02:41:26 +08:00
|
|
|
|
1999-02-12 05:16:03 +08:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (debug & D_TRACE)
|
|
|
|
printf("->getpass(%s)\n", prompt);
|
|
|
|
#endif
|
2009-07-22 14:34:37 +08:00
|
|
|
|
|
|
|
#if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H)
|
|
|
|
if ((fi = fopen(TTY, "r")) == NULL)
|
1999-02-12 05:16:03 +08:00
|
|
|
fi = stdin;
|
|
|
|
else
|
|
|
|
setbuf(fi, (char *)NULL);
|
|
|
|
if (fi != stdin) {
|
|
|
|
if (GETATTR(fileno(fi), &ttyb) < 0)
|
|
|
|
perror("GETATTR");
|
2009-07-22 14:34:37 +08:00
|
|
|
sig = SIGNAL (SIGINT, SIG_IGN);
|
|
|
|
flags = GETFLAGS( ttyb );
|
|
|
|
SETFLAGS( ttyb, flags & ~ECHO );
|
1999-02-12 05:16:03 +08:00
|
|
|
if (SETATTR(fileno(fi), &ttyb) < 0)
|
|
|
|
perror("SETATTR");
|
|
|
|
}
|
2009-07-22 14:34:37 +08:00
|
|
|
#else
|
|
|
|
fi = stdin;
|
|
|
|
#endif
|
2010-11-12 12:15:23 +08:00
|
|
|
fprintf(stderr, "%s", prompt);
|
|
|
|
fflush(stderr);
|
2009-07-22 14:34:37 +08:00
|
|
|
i = 0;
|
|
|
|
while ( (c = getc(fi)) != EOF && c != '\n' && c != '\r' )
|
|
|
|
if ( i < (sizeof(pbuf)-1) )
|
|
|
|
pbuf[i++] = c;
|
|
|
|
#if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H)
|
1999-02-12 05:16:03 +08:00
|
|
|
/* tidy up */
|
|
|
|
if (fi != stdin) {
|
2010-11-12 12:15:23 +08:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
fflush(stderr);
|
2009-07-22 14:34:37 +08:00
|
|
|
SETFLAGS( ttyb, flags );
|
1999-02-12 05:16:03 +08:00
|
|
|
if (SETATTR(fileno(fi), &ttyb) < 0)
|
|
|
|
perror("SETATTR");
|
2009-07-22 14:34:37 +08:00
|
|
|
(void) SIGNAL (SIGINT, sig);
|
1999-02-12 05:16:03 +08:00
|
|
|
(void) fclose(fi);
|
2009-07-22 14:34:37 +08:00
|
|
|
}
|
1999-06-06 04:14:02 +08:00
|
|
|
#endif
|
2009-07-22 14:34:37 +08:00
|
|
|
if ( c == EOF )
|
|
|
|
return( NULL );
|
|
|
|
pbuf[i] = '\0';
|
|
|
|
return (pbuf);
|
1999-02-12 05:16:03 +08:00
|
|
|
}
|
|
|
|
|
2000-05-20 02:41:26 +08:00
|
|
|
#endif /* !NEED_GETPASSPHRASE */
|