tool_getpass.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 #include "tool_setup.h"
00023 
00024 #ifndef HAVE_GETPASS_R
00025 /* this file is only for systems without getpass_r() */
00026 
00027 #ifdef HAVE_FCNTL_H
00028 #  include <fcntl.h>
00029 #endif
00030 
00031 #ifdef HAVE_TERMIOS_H
00032 #  include <termios.h>
00033 #elif defined(HAVE_TERMIO_H)
00034 #  include <termio.h>
00035 #endif
00036 
00037 #ifdef __VMS
00038 #  include descrip
00039 #  include starlet
00040 #  include iodef
00041 #endif
00042 
00043 #ifdef WIN32
00044 #  include <conio.h>
00045 #endif
00046 
00047 #ifdef NETWARE
00048 #  ifdef __NOVELL_LIBC__
00049 #    include <screen.h>
00050 #  else
00051 #    include <nwconio.h>
00052 #  endif
00053 #endif
00054 
00055 #ifdef HAVE_UNISTD_H
00056 #include <unistd.h>
00057 #endif
00058 #include "tool_getpass.h"
00059 
00060 #include "memdebug.h" /* keep this as LAST include */
00061 
00062 #ifdef __VMS
00063 /* VMS implementation */
00064 char *getpass_r(const char *prompt, char *buffer, size_t buflen)
00065 {
00066   long sts;
00067   short chan;
00068 
00069   /* MSK, 23-JAN-2004, iosbdef.h wasn't in VAX V7.2 or CC 6.4  */
00070   /* distribution so I created this.  May revert back later to */
00071   /* struct _iosb iosb;                                        */
00072   struct _iosb
00073      {
00074      short int iosb$w_status; /* status     */
00075      short int iosb$w_bcnt;   /* byte count */
00076      int       unused;        /* unused     */
00077      } iosb;
00078 
00079   $DESCRIPTOR(ttdesc, "TT");
00080 
00081   buffer[0] = '\0';
00082   sts = sys$assign(&ttdesc, &chan, 0, 0);
00083   if(sts & 1) {
00084     sts = sys$qiow(0, chan,
00085                    IO$_READPROMPT | IO$M_NOECHO,
00086                    &iosb, 0, 0, buffer, buflen, 0, 0,
00087                    prompt, strlen(prompt));
00088 
00089     if((sts & 1) && (iosb.iosb$w_status & 1))
00090       buffer[iosb.iosb$w_bcnt] = '\0';
00091 
00092     sts = sys$dassgn(chan);
00093   }
00094   return buffer; /* we always return success */
00095 }
00096 #define DONE
00097 #endif /* __VMS */
00098 
00099 #ifdef __SYMBIAN32__
00100 #  define getch() getchar()
00101 #endif
00102 
00103 #if defined(WIN32) || defined(__SYMBIAN32__)
00104 
00105 char *getpass_r(const char *prompt, char *buffer, size_t buflen)
00106 {
00107   size_t i;
00108   fputs(prompt, stderr);
00109 
00110   for(i = 0; i < buflen; i++) {
00111     buffer[i] = (char)getch();
00112     if(buffer[i] == '\r' || buffer[i] == '\n') {
00113       buffer[i] = '\0';
00114       break;
00115     }
00116     else
00117       if(buffer[i] == '\b')
00118         /* remove this letter and if this is not the first key, remove the
00119            previous one as well */
00120         i = i - (i >= 1 ? 2 : 1);
00121   }
00122 #ifndef __SYMBIAN32__
00123   /* since echo is disabled, print a newline */
00124   fputs("\n", stderr);
00125 #endif
00126   /* if user didn't hit ENTER, terminate buffer */
00127   if(i == buflen)
00128     buffer[buflen-1] = '\0';
00129 
00130   return buffer; /* we always return success */
00131 }
00132 #define DONE
00133 #endif /* WIN32 || __SYMBIAN32__ */
00134 
00135 #ifdef NETWARE
00136 /* NetWare implementation */
00137 #ifdef __NOVELL_LIBC__
00138 char *getpass_r(const char *prompt, char *buffer, size_t buflen)
00139 {
00140   return getpassword(prompt, buffer, buflen);
00141 }
00142 #else
00143 char *getpass_r(const char *prompt, char *buffer, size_t buflen)
00144 {
00145   size_t i = 0;
00146 
00147   printf("%s", prompt);
00148   do {
00149     buffer[i++] = getch();
00150     if(buffer[i-1] == '\b') {
00151       /* remove this letter and if this is not the first key,
00152          remove the previous one as well */
00153       if(i > 1) {
00154         printf("\b \b");
00155         i = i - 2;
00156       }
00157       else {
00158         RingTheBell();
00159         i = i - 1;
00160       }
00161     }
00162     else if(buffer[i-1] != 13)
00163       putchar('*');
00164 
00165   } while((buffer[i-1] != 13) && (i < buflen));
00166   buffer[i-1] = '\0';
00167   printf("\r\n");
00168   return buffer;
00169 }
00170 #endif /* __NOVELL_LIBC__ */
00171 #define DONE
00172 #endif /* NETWARE */
00173 
00174 #ifndef DONE /* not previously provided */
00175 
00176 #ifdef HAVE_TERMIOS_H
00177 #  define struct_term  struct termios
00178 #elif defined(HAVE_TERMIO_H)
00179 #  define struct_term  struct termio
00180 #else
00181 #  undef  struct_term
00182 #endif
00183 
00184 static bool ttyecho(bool enable, int fd)
00185 {
00186 #ifdef struct_term
00187   static struct_term withecho;
00188   static struct_term noecho;
00189 #endif
00190   if(!enable) {
00191     /* disable echo by extracting the current 'withecho' mode and remove the
00192        ECHO bit and set back the struct */
00193 #ifdef HAVE_TERMIOS_H
00194     tcgetattr(fd, &withecho);
00195     noecho = withecho;
00196     noecho.c_lflag &= ~ECHO;
00197     tcsetattr(fd, TCSANOW, &noecho);
00198 #elif defined(HAVE_TERMIO_H)
00199     ioctl(fd, TCGETA, &withecho);
00200     noecho = withecho;
00201     noecho.c_lflag &= ~ECHO;
00202     ioctl(fd, TCSETA, &noecho);
00203 #else
00204     /* neither HAVE_TERMIO_H nor HAVE_TERMIOS_H, we can't disable echo! */
00205     (void)fd;
00206     return FALSE; /* not disabled */
00207 #endif
00208     return TRUE; /* disabled */
00209   }
00210   else {
00211     /* re-enable echo, assumes we disabled it before (and set the structs we
00212        now use to reset the terminal status) */
00213 #ifdef HAVE_TERMIOS_H
00214     tcsetattr(fd, TCSAFLUSH, &withecho);
00215 #elif defined(HAVE_TERMIO_H)
00216     ioctl(fd, TCSETA, &withecho);
00217 #else
00218     return FALSE; /* not enabled */
00219 #endif
00220     return TRUE; /* enabled */
00221   }
00222 }
00223 
00224 char *getpass_r(const char *prompt, /* prompt to display */
00225                 char *password,     /* buffer to store password in */
00226                 size_t buflen)      /* size of buffer to store password in */
00227 {
00228   ssize_t nread;
00229   bool disabled;
00230   int fd = open("/dev/tty", O_RDONLY);
00231   if(-1 == fd)
00232     fd = STDIN_FILENO; /* use stdin if the tty couldn't be used */
00233 
00234   disabled = ttyecho(FALSE, fd); /* disable terminal echo */
00235 
00236   fputs(prompt, stderr);
00237   nread = read(fd, password, buflen);
00238   if(nread > 0)
00239     password[--nread] = '\0'; /* zero terminate where enter is stored */
00240   else
00241     password[0] = '\0'; /* got nothing */
00242 
00243   if(disabled) {
00244     /* if echo actually was disabled, add a newline */
00245     fputs("\n", stderr);
00246     (void)ttyecho(TRUE, fd); /* enable echo */
00247   }
00248 
00249   if(STDIN_FILENO != fd)
00250     close(fd);
00251 
00252   return password; /* return pointer to buffer */
00253 }
00254 
00255 #endif /* DONE */
00256 #endif /* HAVE_GETPASS_R */


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:07