home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ldapsdk.zip / libraries / liblutil / getpass.c < prev    next >
C/C++ Source or Header  |  2000-06-14  |  4KB  |  164 lines

  1. /* $OpenLDAP: pkg/ldap/libraries/liblutil/getpass.c,v 1.5.6.3 2000/06/13 17:57:25 kurt Exp $ */
  2. /*
  3.  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
  4.  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  5.  */
  6. /*
  7.  * Copyright (c) 1992, 1993  Regents of the University of Michigan.
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms are permitted
  11.  * provided that this notice is preserved and that due credit is given
  12.  * to the University of Michigan at Ann Arbor. The name of the University
  13.  * may not be used to endorse or promote products derived from this
  14.  * software without specific prior written permission. This software
  15.  * is provided ``as is'' without express or implied warranty.
  16.  */
  17.  
  18. #include "portable.h"
  19.  
  20. #include <stdio.h>
  21.  
  22. #include <ac/stdlib.h>
  23.  
  24. #include <ac/ctype.h>
  25. #include <ac/signal.h>
  26. #include <ac/string.h>
  27. #include <ac/termios.h>
  28. #include <ac/time.h>
  29. #include <ac/unistd.h>
  30.  
  31. #ifdef NEED_GETPASSPHRASE
  32.  
  33. #ifdef HAVE_FCNTL_H
  34. #include <fcntl.h>
  35. #endif
  36.  
  37. #ifdef HAVE_CONIO_H
  38. #include <conio.h>
  39. #endif
  40.  
  41. #include <lber.h>
  42. #include <ldap.h>
  43.  
  44. #include "ldap_defaults.h"
  45.  
  46. char *
  47. lutil_getpass( const char *prompt )
  48. {
  49. #if !defined(HAVE_POSIX_TERMIOS) && !defined(HAVE_SGTTY_H)
  50.     static char buf[256];
  51.     int i, c;
  52.  
  53.     if( prompt == NULL ) prompt = "Password: ";
  54.  
  55. #ifdef DEBUG
  56.     if (debug & D_TRACE)
  57.         printf("->getpass(%s)\n", prompt);
  58. #endif
  59.  
  60.     printf("%s", prompt);
  61.     i = 0;
  62.     while ( (c = getch()) != EOF && c != '\n' && c != '\r' )
  63.         buf[i++] = c;
  64.     if ( c == EOF )
  65.         return( NULL );
  66.     buf[i] = '\0';
  67.     return (buf);
  68. #else
  69.     int no_pass = 0;
  70.     char i, j, k;
  71.     TERMIO_TYPE ttyb;
  72.     TERMFLAG_TYPE flags;
  73.     static char pbuf[513];
  74.     register char *p;
  75.     register int c;
  76.     FILE *fi;
  77.     RETSIGTYPE (*sig)( int sig );
  78.  
  79.     if( prompt == NULL ) prompt = "Password: ";
  80.  
  81. #ifdef DEBUG
  82.     if (debug & D_TRACE)
  83.         printf("->getpass(%s)\n", prompt);
  84. #endif
  85.     /*
  86.      *  Stolen from the getpass() routine.  Can't use the plain
  87.      *  getpass() for two reasons.  One is that LDAP passwords
  88.      *  can be really, really long - much longer than 8 chars.
  89.      *  The second is that we like to make this client available
  90.      *  out of inetd via a Merit asynch port, and we need to be
  91.      *  able to do telnet control codes to turn on and off line
  92.      *  blanking.
  93.      */
  94.     if ((fi = fdopen(open("/dev/tty", 2), "r")) == NULL)
  95.         fi = stdin;
  96.     else
  97.         setbuf(fi, (char *)NULL);
  98.     sig = SIGNAL (SIGINT, SIG_IGN);
  99.     if (fi != stdin) {
  100.         if (GETATTR(fileno(fi), &ttyb) < 0)
  101.             perror("GETATTR");
  102.     }
  103.     flags = GETFLAGS( ttyb );
  104.     SETFLAGS( ttyb, flags & ~ECHO );
  105.     if (fi != stdin) {
  106.         if (SETATTR(fileno(fi), &ttyb) < 0)
  107.             perror("SETATTR");
  108.     }
  109.  
  110.     /*  blank the line if through Merit */
  111.     if (fi == stdin) {
  112.         printf("%c%c%c", 255, 251, 1);
  113.         fflush(stdout);
  114.         (void) scanf("%c%c%c", &i, &j, &k);
  115.         fflush(stdin);
  116.     }
  117.  
  118.     /* fetch the password */
  119.     fprintf(stdout, "%s", prompt); 
  120.     fflush(stdout);
  121.     for (p=pbuf; (c = getc(fi))!='\n' && c!=EOF;) {
  122.         if (c == '\r')
  123.             break;
  124.         if (p < &pbuf[512])
  125.             *p++ = c;
  126.     }
  127.     if (c == EOF)
  128.         no_pass = 1;
  129.     else {
  130.         *p = '\0';
  131.         if (*(p - 1) == '\r')
  132.             *(p - 1) = '\0';
  133.     }
  134.  
  135.     /*  unblank the line if through Merit */
  136.     if (fi == stdin) {
  137.         printf("%c%c%c", 255, 252, 1);
  138.         fflush(stdout);
  139.         (void) scanf("%c%c%c", &i, &j, &k);
  140.         fflush(stdin);
  141.         printf("\n"); fflush(stdout);
  142.     }
  143.     fprintf(stdout, "\n"); 
  144.     fflush(stdout);
  145.  
  146.     /* tidy up */
  147.     SETFLAGS( ttyb, flags );
  148.     if (fi != stdin) {
  149.         if (SETATTR(fileno(fi), &ttyb) < 0)
  150.             perror("SETATTR");
  151.     }
  152.     (void) SIGNAL (SIGINT, sig);
  153.     if (fi != stdin)
  154.         (void) fclose(fi);
  155.     else
  156.         i = getchar();
  157.     if (no_pass)
  158.         return(NULL);
  159.     return(pbuf);
  160. #endif
  161. }
  162.  
  163. #endif /* !NEED_GETPASSPHRASE */
  164.