lib/replace/getpass.c

説明を見る。
00001 /* Copyright (C) 1992-1998 Free Software Foundation, Inc.
00002 This file is part of the GNU C Library.
00003 
00004 The GNU C Library is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU Library General Public License as
00006 published by the Free Software Foundation; either version 2 of the
00007 License, or (at your option) any later version.
00008 
00009 The GNU C Library is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 Library General Public License for more details.
00013 
00014 You should have received a copy of the GNU Library General Public
00015 License along with the GNU C Library; see the file COPYING.LIB.  If
00016 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
00017 Cambridge, MA 02139, USA.  */
00018 
00019 /* Modified to use with samba by Jeremy Allison, 8th July 1995. */
00020 
00021 #include "replace.h"
00022 
00023 #if defined(REPLACE_GETPASS_BY_GETPASSPHRASE)
00024 
00025 #if defined(HAVE_STDIO_H)
00026 #include <stdio.h>
00027 #endif
00028 
00029 char *getsmbpass(const char *prompt)
00030 {
00031         return getpassphrase(prompt);
00032 }
00033 
00034 #else /* !REPLACE_GETPASS_BY_GETPASSPHRASE */
00035 
00036 #if defined(HAVE_TERMIOS_H)
00037 /* POSIX terminal handling. */
00038 #include <termios.h>
00039 #elif defined(HAVE_TERMIO_H)
00040 /* Older SYSV terminal handling - don't use if we can avoid it. */
00041 #include <termio.h>
00042 #elif defined(HAVE_SYS_TERMIO_H)
00043 /* Older SYSV terminal handling - don't use if we can avoid it. */
00044 #include <sys/termio.h>
00045 #endif
00046 
00047 #ifdef HAVE_SYS_WAIT_H
00048 #include <sys/wait.h>
00049 #endif
00050 
00051 /*
00052  * Define additional missing types
00053  */
00054 #ifndef HAVE_SIG_ATOMIC_T_TYPE
00055 typedef int sig_atomic_t;
00056 #endif
00057 
00058 #ifndef SIGCLD
00059 #define SIGCLD SIGCHLD
00060 #endif
00061 
00062 #ifndef SIGNAL_CAST
00063 #define SIGNAL_CAST (RETSIGTYPE (*)(int))
00064 #endif
00065 
00066 #ifdef REPLACE_GETPASS
00067 
00068 #ifdef SYSV_TERMIO 
00069 
00070 /* SYSTEM V TERMIO HANDLING */
00071 
00072 static struct termio t;
00073 
00074 #define ECHO_IS_ON(t) ((t).c_lflag & ECHO)
00075 #define TURN_ECHO_OFF(t) ((t).c_lflag &= ~ECHO)
00076 #define TURN_ECHO_ON(t) ((t).c_lflag |= ECHO)
00077 
00078 #ifndef TCSAFLUSH
00079 #define TCSAFLUSH 1
00080 #endif
00081 
00082 #ifndef TCSANOW
00083 #define TCSANOW 0
00084 #endif
00085 
00086 static int tcgetattr(int fd, struct termio *_t)
00087 {
00088         return ioctl(fd, TCGETA, _t);
00089 }
00090 
00091 static int tcsetattr(int fd, int flags, struct termio *_t)
00092 {
00093         if(flags & TCSAFLUSH)
00094                 ioctl(fd, TCFLSH, TCIOFLUSH);
00095         return ioctl(fd, TCSETS, _t);
00096 }
00097 
00098 #elif !defined(TCSAFLUSH)
00099 
00100 /* BSD TERMIO HANDLING */
00101 
00102 static struct sgttyb t;  
00103 
00104 #define ECHO_IS_ON(t) ((t).sg_flags & ECHO)
00105 #define TURN_ECHO_OFF(t) ((t).sg_flags &= ~ECHO)
00106 #define TURN_ECHO_ON(t) ((t).sg_flags |= ECHO)
00107 
00108 #define TCSAFLUSH 1
00109 #define TCSANOW 0
00110 
00111 static int tcgetattr(int fd, struct sgttyb *_t)
00112 {
00113         return ioctl(fd, TIOCGETP, (char *)_t);
00114 }
00115 
00116 static int tcsetattr(int fd, int flags, struct sgttyb *_t)
00117 {
00118         return ioctl(fd, TIOCSETP, (char *)_t);
00119 }
00120 
00121 #else /* POSIX TERMIO HANDLING */
00122 #define ECHO_IS_ON(t) ((t).c_lflag & ECHO)
00123 #define TURN_ECHO_OFF(t) ((t).c_lflag &= ~ECHO)
00124 #define TURN_ECHO_ON(t) ((t).c_lflag |= ECHO)
00125 
00126 static struct termios t;
00127 #endif /* SYSV_TERMIO */
00128 
00129 static void catch_signal(int signum,void (*handler)(int ))
00130 {
00131 #ifdef HAVE_SIGACTION
00132         struct sigaction act;
00133         struct sigaction oldact;
00134 
00135         memset(&act, 0, sizeof(act));
00136 
00137         act.sa_handler = handler;
00138 #ifdef SA_RESTART
00139         /*
00140          * We *want* SIGALRM to interrupt a system call.
00141          */
00142         if(signum != SIGALRM)
00143                 act.sa_flags = SA_RESTART;
00144 #endif
00145         sigemptyset(&act.sa_mask);
00146         sigaddset(&act.sa_mask,signum);
00147         sigaction(signum,&act,&oldact);
00148         return oldact.sa_handler;
00149 #else /* !HAVE_SIGACTION */
00150         /* FIXME: need to handle sigvec and systems with broken signal() */
00151         return signal(signum, handler);
00152 #endif
00153 }
00154 
00155 char *getsmbpass(const char *prompt)
00156 {
00157   FILE *in, *out;
00158   int echo_off;
00159   static char buf[256];
00160   static size_t bufsize = sizeof(buf);
00161   size_t nread;
00162 
00163   /* Catch problematic signals */
00164   catch_signal(SIGINT, SIGNAL_CAST SIG_IGN);
00165 
00166   /* Try to write to and read from the terminal if we can.
00167      If we can't open the terminal, use stderr and stdin.  */
00168 
00169   in = fopen ("/dev/tty", "w+");
00170   if (in == NULL)
00171     {
00172       in = stdin;
00173       out = stderr;
00174     }
00175   else
00176     out = in;
00177 
00178   setvbuf(in, NULL, _IONBF, 0);
00179 
00180   /* Turn echoing off if it is on now.  */
00181 
00182   if (tcgetattr (fileno (in), &t) == 0)
00183     {
00184           if (ECHO_IS_ON(t))
00185         {
00186                 TURN_ECHO_OFF(t);
00187                 echo_off = tcsetattr (fileno (in), TCSAFLUSH, &t) == 0;
00188                 TURN_ECHO_ON(t);
00189         }
00190       else
00191         echo_off = 0;
00192     }
00193   else
00194     echo_off = 0;
00195 
00196   /* Write the prompt.  */
00197   fputs (prompt, out);
00198   fflush (out);
00199 
00200   /* Read the password.  */
00201   buf[0] = 0;
00202   fgets(buf, bufsize, in);
00203   nread = strlen(buf);
00204   if (buf[nread - 1] == '\n')
00205     buf[nread - 1] = '\0';
00206 
00207   /* Restore echoing.  */
00208   if (echo_off)
00209     (void) tcsetattr (fileno (in), TCSANOW, &t);
00210 
00211   if (in != stdin)
00212     /* We opened the terminal; now close it.  */
00213     fclose (in);
00214 
00215   /* Catch problematic signals */
00216   catch_signal(SIGINT, SIGNAL_CAST SIG_DFL);
00217 
00218   printf("\n");
00219   return buf;
00220 }
00221 
00222 #else
00223  void getsmbpasswd_dummy(void);
00224  void getsmbpasswd_dummy(void) {;}
00225 #endif
00226 
00227 #endif /* REPLACE_GETPASS_BY_GETPASSPHRASE */

Sambaに対してSat Aug 29 21:22:58 2009に生成されました。  doxygen 1.4.7