Back to index

openldap  2.4.31
getpass.c
Go to the documentation of this file.
00001 /* getpass.c -- get password from user */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1998-2012 The OpenLDAP Foundation.
00006  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
00007  * Portions Copyright 2009 Howard Chu.
00008  * All rights reserved.
00009  *
00010  * Redistribution and use in source and binary forms, with or without
00011  * modification, are permitted only as authorized by the OpenLDAP
00012  * Public License.
00013  *
00014  * A copy of this license is available in the file LICENSE in the
00015  * top-level directory of the distribution or, alternatively, at
00016  * <http://www.OpenLDAP.org/license.html>.
00017  */
00018 /* Portions Copyright (c) 1992, 1993  Regents of the University of Michigan.
00019  * All rights reserved.
00020  *
00021  * Redistribution and use in source and binary forms are permitted
00022  * provided that this notice is preserved and that due credit is given
00023  * to the University of Michigan at Ann Arbor. The name of the University
00024  * may not be used to endorse or promote products derived from this
00025  * software without specific prior written permission. This software
00026  * is provided ``as is'' without express or implied warranty.
00027  */
00028 /* This work was originally developed by the University of Michigan
00029  * and distributed as part of U-MICH LDAP.  It was adapted for use in
00030  * -llutil by Kurt D. Zeilenga and subsequently rewritten by Howard Chu.
00031  */
00032 
00033 #include "portable.h"
00034 
00035 #include <stdio.h>
00036 
00037 #include <ac/stdlib.h>
00038 
00039 #include <ac/ctype.h>
00040 #include <ac/signal.h>
00041 #include <ac/string.h>
00042 #include <ac/termios.h>
00043 #include <ac/time.h>
00044 #include <ac/unistd.h>
00045 
00046 #ifndef HAVE_GETPASSPHRASE
00047 
00048 #ifdef HAVE_FCNTL_H
00049 #include <fcntl.h>
00050 #endif
00051 
00052 #ifdef HAVE_CONIO_H
00053 #include <conio.h>
00054 #endif
00055 
00056 #include <lber.h>
00057 #include <ldap.h>
00058 
00059 #include "ldap_defaults.h"
00060 
00061 #define PBUF  512
00062 
00063 #ifdef HAVE_WINSOCK
00064 #define TTY "con:"
00065 #else
00066 #define TTY "/dev/tty"
00067 #endif
00068 
00069 char *
00070 lutil_getpass( const char *prompt )
00071 {
00072        static char pbuf[PBUF];
00073        FILE *fi;
00074        int c;
00075        unsigned i;
00076 #if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H)
00077        TERMIO_TYPE ttyb;
00078        TERMFLAG_TYPE flags;
00079        RETSIGTYPE (*sig)( int sig );
00080 #endif
00081 
00082        if( prompt == NULL ) prompt = _("Password: ");
00083 
00084 #ifdef DEBUG
00085        if (debug & D_TRACE)
00086               printf("->getpass(%s)\n", prompt);
00087 #endif
00088 
00089 #if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H)
00090        if ((fi = fopen(TTY, "r")) == NULL)
00091               fi = stdin;
00092        else
00093               setbuf(fi, (char *)NULL);
00094        if (fi != stdin) {
00095               if (GETATTR(fileno(fi), &ttyb) < 0)
00096                      perror("GETATTR");
00097               sig = SIGNAL (SIGINT, SIG_IGN);
00098               flags = GETFLAGS( ttyb );
00099               SETFLAGS( ttyb, flags & ~ECHO );
00100               if (SETATTR(fileno(fi), &ttyb) < 0)
00101                      perror("SETATTR");
00102        }
00103 #else
00104        fi = stdin;
00105 #endif
00106        fprintf(stderr, "%s", prompt); 
00107        fflush(stderr);
00108        i = 0;
00109        while ( (c = getc(fi)) != EOF && c != '\n' && c != '\r' )
00110               if ( i < (sizeof(pbuf)-1) )
00111                      pbuf[i++] = c;
00112 #if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H)
00113        /* tidy up */
00114        if (fi != stdin) {
00115               fprintf(stderr, "\n"); 
00116               fflush(stderr);
00117               SETFLAGS( ttyb, flags );
00118               if (SETATTR(fileno(fi), &ttyb) < 0)
00119                      perror("SETATTR");
00120               (void) SIGNAL (SIGINT, sig);
00121               (void) fclose(fi);
00122        }
00123 #endif
00124        if ( c == EOF )
00125               return( NULL );
00126        pbuf[i] = '\0';
00127        return (pbuf);
00128 }
00129 
00130 #endif /* !NEED_GETPASSPHRASE */