Home | History | Annotate | Line # | Download | only in liblutil
getpass.c revision 1.1.1.5
      1  1.1.1.2     lukem /*	$NetBSD: getpass.c,v 1.1.1.5 2017/02/09 01:46:46 christos 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.5  christos  * Copyright 1998-2016 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.1.5  christos #include <sys/cdefs.h>
     36  1.1.1.5  christos __RCSID("$NetBSD: getpass.c,v 1.1.1.5 2017/02/09 01:46:46 christos Exp $");
     37  1.1.1.5  christos 
     38      1.1     lukem #include "portable.h"
     39      1.1     lukem 
     40      1.1     lukem #include <stdio.h>
     41      1.1     lukem 
     42      1.1     lukem #include <ac/stdlib.h>
     43      1.1     lukem 
     44      1.1     lukem #include <ac/ctype.h>
     45      1.1     lukem #include <ac/signal.h>
     46      1.1     lukem #include <ac/string.h>
     47      1.1     lukem #include <ac/termios.h>
     48      1.1     lukem #include <ac/time.h>
     49      1.1     lukem #include <ac/unistd.h>
     50      1.1     lukem 
     51  1.1.1.2     lukem #ifndef HAVE_GETPASSPHRASE
     52      1.1     lukem 
     53      1.1     lukem #ifdef HAVE_FCNTL_H
     54      1.1     lukem #include <fcntl.h>
     55      1.1     lukem #endif
     56      1.1     lukem 
     57      1.1     lukem #ifdef HAVE_CONIO_H
     58      1.1     lukem #include <conio.h>
     59      1.1     lukem #endif
     60      1.1     lukem 
     61      1.1     lukem #include <lber.h>
     62      1.1     lukem #include <ldap.h>
     63      1.1     lukem 
     64      1.1     lukem #include "ldap_defaults.h"
     65      1.1     lukem 
     66  1.1.1.2     lukem #define PBUF	512
     67      1.1     lukem 
     68  1.1.1.2     lukem #ifdef HAVE_WINSOCK
     69  1.1.1.2     lukem #define TTY "con:"
     70  1.1.1.2     lukem #else
     71  1.1.1.2     lukem #define TTY "/dev/tty"
     72      1.1     lukem #endif
     73      1.1     lukem 
     74  1.1.1.2     lukem char *
     75  1.1.1.2     lukem lutil_getpass( const char *prompt )
     76  1.1.1.2     lukem {
     77  1.1.1.2     lukem 	static char pbuf[PBUF];
     78  1.1.1.2     lukem 	FILE *fi;
     79  1.1.1.2     lukem 	int c;
     80  1.1.1.2     lukem 	unsigned i;
     81  1.1.1.2     lukem #if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H)
     82      1.1     lukem 	TERMIO_TYPE ttyb;
     83      1.1     lukem 	TERMFLAG_TYPE flags;
     84      1.1     lukem 	RETSIGTYPE (*sig)( int sig );
     85  1.1.1.2     lukem #endif
     86      1.1     lukem 
     87      1.1     lukem 	if( prompt == NULL ) prompt = _("Password: ");
     88      1.1     lukem 
     89      1.1     lukem #ifdef DEBUG
     90      1.1     lukem 	if (debug & D_TRACE)
     91      1.1     lukem 		printf("->getpass(%s)\n", prompt);
     92      1.1     lukem #endif
     93  1.1.1.2     lukem 
     94  1.1.1.2     lukem #if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H)
     95  1.1.1.2     lukem 	if ((fi = fopen(TTY, "r")) == NULL)
     96      1.1     lukem 		fi = stdin;
     97      1.1     lukem 	else
     98      1.1     lukem 		setbuf(fi, (char *)NULL);
     99      1.1     lukem 	if (fi != stdin) {
    100      1.1     lukem 		if (GETATTR(fileno(fi), &ttyb) < 0)
    101      1.1     lukem 			perror("GETATTR");
    102  1.1.1.2     lukem 		sig = SIGNAL (SIGINT, SIG_IGN);
    103  1.1.1.2     lukem 		flags = GETFLAGS( ttyb );
    104  1.1.1.2     lukem 		SETFLAGS( ttyb, flags & ~ECHO );
    105      1.1     lukem 		if (SETATTR(fileno(fi), &ttyb) < 0)
    106      1.1     lukem 			perror("SETATTR");
    107      1.1     lukem 	}
    108  1.1.1.2     lukem #else
    109  1.1.1.2     lukem 	fi = stdin;
    110  1.1.1.2     lukem #endif
    111  1.1.1.4      tron 	fprintf(stderr, "%s", prompt);
    112  1.1.1.4      tron 	fflush(stderr);
    113  1.1.1.2     lukem 	i = 0;
    114  1.1.1.2     lukem 	while ( (c = getc(fi)) != EOF && c != '\n' && c != '\r' )
    115  1.1.1.2     lukem 		if ( i < (sizeof(pbuf)-1) )
    116  1.1.1.2     lukem 			pbuf[i++] = c;
    117  1.1.1.2     lukem #if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H)
    118      1.1     lukem 	/* tidy up */
    119      1.1     lukem 	if (fi != stdin) {
    120  1.1.1.4      tron 		fprintf(stderr, "\n");
    121  1.1.1.4      tron 		fflush(stderr);
    122  1.1.1.2     lukem 		SETFLAGS( ttyb, flags );
    123      1.1     lukem 		if (SETATTR(fileno(fi), &ttyb) < 0)
    124      1.1     lukem 			perror("SETATTR");
    125  1.1.1.2     lukem 		(void) SIGNAL (SIGINT, sig);
    126      1.1     lukem 		(void) fclose(fi);
    127  1.1.1.2     lukem 	}
    128      1.1     lukem #endif
    129  1.1.1.2     lukem 	if ( c == EOF )
    130  1.1.1.2     lukem 		return( NULL );
    131  1.1.1.2     lukem 	pbuf[i] = '\0';
    132  1.1.1.2     lukem 	return (pbuf);
    133      1.1     lukem }
    134      1.1     lukem 
    135      1.1     lukem #endif /* !NEED_GETPASSPHRASE */
    136