home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Networking / SambaManager / samba-1.9.17p4 / source / getsmbpass.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-30  |  3.6 KB  |  171 lines

  1. /* Copyright (C) 1992-1997 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. /* Modified to use with samba by Jeremy Allison, 8th July 1995. */
  20.  
  21. #include "includes.h"
  22.  
  23. #ifdef REPLACE_GETPASS
  24.  
  25. #ifdef SYSV_TERMIO 
  26.  
  27. /* SYSTEM V TERMIO HANDLING */
  28.  
  29. static struct termio t;
  30.  
  31. #define ECHO_IS_ON(t) ((t).c_lflag & ECHO)
  32. #define TURN_ECHO_OFF(t) ((t).c_lflag &= ~ECHO)
  33. #define TURN_ECHO_ON(t) ((t).c_lflag |= ECHO)
  34.  
  35. #ifndef TCSAFLUSH
  36. #define TCSAFLUSH 1
  37. #endif
  38.  
  39. #ifndef TCSANOW
  40. #define TCSANOW 0
  41. #endif
  42.  
  43.  int tcgetattr(int fd, struct termio *t)
  44. {
  45.     return ioctl(fd, TCGETA, t);
  46. }
  47.  
  48.  int tcsetattr(int fd, int flags, const struct termio *t)
  49. {
  50.     if(flags & TCSAFLUSH)
  51.         ioctl(fd, TCFLSH, TCIOFLUSH);
  52.     return ioctl(fd, TCSETS, t);
  53. }
  54.  
  55. #else /* SYSV_TERMIO */
  56. #ifdef BSD_TERMIO
  57.  
  58. /* BSD TERMIO HANDLING */
  59.  
  60. static struct sgttyb t;  
  61.  
  62. #define ECHO_IS_ON(t) ((t).sg_flags & ECHO)
  63. #define TURN_ECHO_OFF(t) ((t).sg_flags &= ~ECHO)
  64. #define TURN_ECHO_ON(t) ((t).sg_flags |= ECHO)
  65.  
  66. #ifndef TCSAFLUSH
  67. #define TCSAFLUSH 1
  68. #endif
  69.  
  70. #ifndef TCSANOW
  71. #define TCSANOW 0
  72. #endif
  73.  
  74. #ifdef NEXT3_0
  75. #define tcgetattr ltcgetattr
  76. #define tcsetattr ltcsetattr
  77. #endif
  78.  
  79.  int tcgetattr(int fd, struct sgttyb *t)
  80. {
  81.     return ioctl(fd, TIOCGETP, (char *)t);
  82. }
  83.  
  84.  int tcsetattr(int fd, int flags, const struct sgttyb *t)
  85. {
  86.     return ioctl(fd, TIOCSETP, (char *)t);
  87. }
  88.  
  89. #else /* BSD_TERMIO */
  90.  
  91. /* POSIX TERMIO HANDLING */
  92. #define ECHO_IS_ON(t) ((t).c_lflag & ECHO)
  93. #define TURN_ECHO_OFF(t) ((t).c_lflag &= ~ECHO)
  94. #define TURN_ECHO_ON(t) ((t).c_lflag |= ECHO)
  95.  
  96. static struct termios t;
  97. #endif /* BSD_TERMIO */
  98. #endif /* SYSV_TERMIO */
  99.  
  100. char *getsmbpass(char *prompt)    
  101. {
  102.   FILE *in, *out;
  103.   int echo_off;
  104.   static char buf[256];
  105.   static size_t bufsize = sizeof(buf);
  106.   size_t nread;
  107.  
  108.   /* Catch problematic signals */
  109.   signal(SIGINT, SIGNAL_CAST SIG_IGN);
  110.  
  111.   /* Try to write to and read from the terminal if we can.
  112.      If we can't open the terminal, use stderr and stdin.  */
  113.  
  114.   in = fopen ("/dev/tty", "w+");
  115.   if (in == NULL)
  116.     {
  117.       in = stdin;
  118.       out = stderr;
  119.     }
  120.   else
  121.     out = in;
  122.  
  123.   setvbuf(in, NULL, _IONBF, 0);
  124.  
  125.   /* Turn echoing off if it is on now.  */
  126.  
  127.   if (tcgetattr (fileno (in), &t) == 0)
  128.     {
  129.       if (ECHO_IS_ON(t))
  130.     {
  131.         TURN_ECHO_OFF(t);
  132.         echo_off = tcsetattr (fileno (in), TCSAFLUSH, &t) == 0;
  133.         TURN_ECHO_ON(t);
  134.     }
  135.       else
  136.     echo_off = 0;
  137.     }
  138.   else
  139.     echo_off = 0;
  140.  
  141.   /* Write the prompt.  */
  142.   fputs (prompt, out);
  143.   fflush (out);
  144.  
  145.   /* Read the password.  */
  146.   buf[0] = 0;
  147.   fgets(buf, bufsize, in);
  148.   nread = strlen(buf);
  149.   if (buf[nread - 1] == '\n')
  150.     buf[nread - 1] = '\0';
  151.  
  152.   /* Restore echoing.  */
  153.   if (echo_off)
  154.     (void) tcsetattr (fileno (in), TCSANOW, &t);
  155.  
  156.   if (in != stdin)
  157.     /* We opened the terminal; now close it.  */
  158.     fclose (in);
  159.  
  160.   /* Catch problematic signals */
  161.   signal(SIGINT, SIGNAL_CAST SIG_DFL);
  162.  
  163.   printf("\n");
  164.   return buf;
  165. }
  166.  
  167. #else
  168.  
  169.  void getsmbpasswd_dummy() {;}
  170. #endif
  171.