00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "tool_setup.h"
00023
00024 #ifndef HAVE_GETPASS_R
00025
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"
00061
00062 #ifdef __VMS
00063
00064 char *getpass_r(const char *prompt, char *buffer, size_t buflen)
00065 {
00066 long sts;
00067 short chan;
00068
00069
00070
00071
00072 struct _iosb
00073 {
00074 short int iosb$w_status;
00075 short int iosb$w_bcnt;
00076 int 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;
00095 }
00096 #define DONE
00097 #endif
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
00119
00120 i = i - (i >= 1 ? 2 : 1);
00121 }
00122 #ifndef __SYMBIAN32__
00123
00124 fputs("\n", stderr);
00125 #endif
00126
00127 if(i == buflen)
00128 buffer[buflen-1] = '\0';
00129
00130 return buffer;
00131 }
00132 #define DONE
00133 #endif
00134
00135 #ifdef NETWARE
00136
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
00152
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
00171 #define DONE
00172 #endif
00173
00174 #ifndef DONE
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
00192
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
00205 (void)fd;
00206 return FALSE;
00207 #endif
00208 return TRUE;
00209 }
00210 else {
00211
00212
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;
00219 #endif
00220 return TRUE;
00221 }
00222 }
00223
00224 char *getpass_r(const char *prompt,
00225 char *password,
00226 size_t buflen)
00227 {
00228 ssize_t nread;
00229 bool disabled;
00230 int fd = open("/dev/tty", O_RDONLY);
00231 if(-1 == fd)
00232 fd = STDIN_FILENO;
00233
00234 disabled = ttyecho(FALSE, fd);
00235
00236 fputs(prompt, stderr);
00237 nread = read(fd, password, buflen);
00238 if(nread > 0)
00239 password[--nread] = '\0';
00240 else
00241 password[0] = '\0';
00242
00243 if(disabled) {
00244
00245 fputs("\n", stderr);
00246 (void)ttyecho(TRUE, fd);
00247 }
00248
00249 if(STDIN_FILENO != fd)
00250 close(fd);
00251
00252 return password;
00253 }
00254
00255 #endif
00256 #endif