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