Home | History | Annotate | Line # | Download | only in liblutil
      1  1.4  christos /*	$NetBSD: getpass.c,v 1.5 2025/09/05 21:16:23 christos Exp $	*/
      2  1.2  christos 
      3  1.1     lukem /* getpass.c -- get password from user */
      4  1.2  christos /* $OpenLDAP$ */
      5  1.1     lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      6  1.1     lukem  *
      7  1.5  christos  * Copyright 1998-2024 The OpenLDAP Foundation.
      8  1.1     lukem  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
      9  1.2  christos  * 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.2  christos  * -llutil by Kurt D. Zeilenga and subsequently rewritten by Howard Chu.
     33  1.1     lukem  */
     34  1.1     lukem 
     35  1.2  christos #include <sys/cdefs.h>
     36  1.4  christos __RCSID("$NetBSD: getpass.c,v 1.5 2025/09/05 21:16:23 christos Exp $");
     37  1.2  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.2  christos #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.2  christos #define PBUF	512
     67  1.2  christos 
     68  1.2  christos #ifdef HAVE_WINSOCK
     69  1.2  christos #define TTY "con:"
     70  1.2  christos #else
     71  1.2  christos #define TTY "/dev/tty"
     72  1.2  christos #endif
     73  1.2  christos 
     74  1.1     lukem char *
     75  1.1     lukem lutil_getpass( const char *prompt )
     76  1.1     lukem {
     77  1.2  christos 	static char pbuf[PBUF];
     78  1.2  christos 	FILE *fi;
     79  1.2  christos 	int c;
     80  1.2  christos 	unsigned i;
     81  1.2  christos #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.2  christos #endif
     86  1.1     lukem 
     87  1.1     lukem 	if( prompt == NULL ) prompt = _("Password: ");
     88  1.1     lukem 
     89  1.2  christos #if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H)
     90  1.2  christos 	if ((fi = fopen(TTY, "r")) == NULL)
     91  1.1     lukem 		fi = stdin;
     92  1.1     lukem 	else
     93  1.1     lukem 		setbuf(fi, (char *)NULL);
     94  1.1     lukem 	if (fi != stdin) {
     95  1.1     lukem 		if (GETATTR(fileno(fi), &ttyb) < 0)
     96  1.1     lukem 			perror("GETATTR");
     97  1.2  christos 		sig = SIGNAL (SIGINT, SIG_IGN);
     98  1.2  christos 		flags = GETFLAGS( ttyb );
     99  1.2  christos 		SETFLAGS( ttyb, flags & ~ECHO );
    100  1.1     lukem 		if (SETATTR(fileno(fi), &ttyb) < 0)
    101  1.1     lukem 			perror("SETATTR");
    102  1.1     lukem 	}
    103  1.2  christos #else
    104  1.2  christos 	fi = stdin;
    105  1.2  christos #endif
    106  1.2  christos 	fprintf(stderr, "%s", prompt);
    107  1.2  christos 	fflush(stderr);
    108  1.2  christos 	i = 0;
    109  1.2  christos 	while ( (c = getc(fi)) != EOF && c != '\n' && c != '\r' )
    110  1.2  christos 		if ( i < (sizeof(pbuf)-1) )
    111  1.2  christos 			pbuf[i++] = c;
    112  1.2  christos #if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H)
    113  1.1     lukem 	/* tidy up */
    114  1.1     lukem 	if (fi != stdin) {
    115  1.2  christos 		fprintf(stderr, "\n");
    116  1.2  christos 		fflush(stderr);
    117  1.2  christos 		SETFLAGS( ttyb, flags );
    118  1.1     lukem 		if (SETATTR(fileno(fi), &ttyb) < 0)
    119  1.1     lukem 			perror("SETATTR");
    120  1.2  christos 		(void) SIGNAL (SIGINT, sig);
    121  1.2  christos 		(void) fclose(fi);
    122  1.1     lukem 	}
    123  1.1     lukem #endif
    124  1.2  christos 	if ( c == EOF )
    125  1.2  christos 		return( NULL );
    126  1.2  christos 	pbuf[i] = '\0';
    127  1.2  christos 	return (pbuf);
    128  1.1     lukem }
    129  1.1     lukem 
    130  1.1     lukem #endif /* !NEED_GETPASSPHRASE */
    131