read_pwd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /* crypto/des/read_pwd.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_WIN32)
  59. #include <openssl/opensslconf.h>
  60. #ifdef OPENSSL_UNISTD
  61. # include OPENSSL_UNISTD
  62. #else
  63. # include <unistd.h>
  64. #endif
  65. /* If unistd.h defines _POSIX_VERSION, we conclude that we
  66. * are on a POSIX system and have sigaction and termios. */
  67. #if defined(_POSIX_VERSION)
  68. # define SIGACTION
  69. # if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
  70. # define TERMIOS
  71. # endif
  72. #endif
  73. #endif
  74. /* #define SIGACTION */ /* Define this if you have sigaction() */
  75. #ifdef WIN16TTY
  76. #undef OPENSSL_SYS_WIN16
  77. #undef _WINDOWS
  78. #include <graph.h>
  79. #endif
  80. /* 06-Apr-92 Luke Brennan Support for VMS */
  81. #include "des_locl.h"
  82. #include "cryptlib.h"
  83. #include <signal.h>
  84. #include <stdio.h>
  85. #include <string.h>
  86. #include <setjmp.h>
  87. #include <errno.h>
  88. #ifdef OPENSSL_SYS_VMS /* prototypes for sys$whatever */
  89. #include <starlet.h>
  90. #ifdef __DECC
  91. #pragma message disable DOLLARID
  92. #endif
  93. #endif
  94. #ifdef WIN_CONSOLE_BUG
  95. #include <windows.h>
  96. #include <wincon.h>
  97. #endif
  98. /* There are 5 types of terminal interface supported,
  99. * TERMIO, TERMIOS, VMS, MSDOS and SGTTY
  100. */
  101. #if defined(__sgi) && !defined(TERMIOS)
  102. #define TERMIOS
  103. #undef TERMIO
  104. #undef SGTTY
  105. #endif
  106. #if defined(linux) && !defined(TERMIO)
  107. #undef TERMIOS
  108. #define TERMIO
  109. #undef SGTTY
  110. #endif
  111. #ifdef _LIBC
  112. #undef TERMIOS
  113. #define TERMIO
  114. #undef SGTTY
  115. #endif
  116. #if !defined(TERMIO) && !defined(TERMIOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MSDOS) && !defined(MAC_OS_pre_X) && !defined(MAC_OS_GUSI_SOURCE)
  117. #undef TERMIOS
  118. #undef TERMIO
  119. #define SGTTY
  120. #endif
  121. #ifdef TERMIOS
  122. #include <termios.h>
  123. #define TTY_STRUCT struct termios
  124. #define TTY_FLAGS c_lflag
  125. #define TTY_get(tty,data) tcgetattr(tty,data)
  126. #define TTY_set(tty,data) tcsetattr(tty,TCSANOW,data)
  127. #endif
  128. #ifdef TERMIO
  129. #include <termio.h>
  130. #define TTY_STRUCT struct termio
  131. #define TTY_FLAGS c_lflag
  132. #define TTY_get(tty,data) ioctl(tty,TCGETA,data)
  133. #define TTY_set(tty,data) ioctl(tty,TCSETA,data)
  134. #endif
  135. #ifdef SGTTY
  136. #include <sgtty.h>
  137. #define TTY_STRUCT struct sgttyb
  138. #define TTY_FLAGS sg_flags
  139. #define TTY_get(tty,data) ioctl(tty,TIOCGETP,data)
  140. #define TTY_set(tty,data) ioctl(tty,TIOCSETP,data)
  141. #endif
  142. #if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) && !defined(MAC_OS_pre_X)
  143. #include <sys/ioctl.h>
  144. #endif
  145. #if defined(OPENSSL_SYS_MSDOS) && !defined(__CYGWIN32__)
  146. #include <conio.h>
  147. #define fgets(a,b,c) noecho_fgets(a,b,c)
  148. #endif
  149. #ifdef OPENSSL_SYS_VMS
  150. #include <ssdef.h>
  151. #include <iodef.h>
  152. #include <ttdef.h>
  153. #include <descrip.h>
  154. struct IOSB {
  155. short iosb$w_value;
  156. short iosb$w_count;
  157. long iosb$l_info;
  158. };
  159. #endif
  160. #if defined(MAC_OS_pre_X) || defined(MAC_OS_GUSI_SOURCE)
  161. /*
  162. * This one needs work. As a matter of fact the code is unoperational
  163. * and this is only a trick to get it compiled.
  164. * <appro@fy.chalmers.se>
  165. */
  166. #define TTY_STRUCT int
  167. #endif
  168. #ifndef NX509_SIG
  169. #define NX509_SIG 32
  170. #endif
  171. static void read_till_nl(FILE *);
  172. static void recsig(int);
  173. static void pushsig(void);
  174. static void popsig(void);
  175. #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16)
  176. static int noecho_fgets(char *buf, int size, FILE *tty);
  177. #endif
  178. #ifdef SIGACTION
  179. static struct sigaction savsig[NX509_SIG];
  180. #else
  181. static void (*savsig[NX509_SIG])(int );
  182. #endif
  183. static jmp_buf save;
  184. int des_read_pw_string(char *buf, int length, const char *prompt,
  185. int verify)
  186. {
  187. char buff[BUFSIZ];
  188. int ret;
  189. ret=des_read_pw(buf,buff,(length>BUFSIZ)?BUFSIZ:length,prompt,verify);
  190. memset(buff,0,BUFSIZ);
  191. return(ret);
  192. }
  193. #ifndef OPENSSL_SYS_WIN16
  194. static void read_till_nl(FILE *in)
  195. {
  196. #define SIZE 4
  197. char buf[SIZE+1];
  198. do {
  199. fgets(buf,SIZE,in);
  200. } while (strchr(buf,'\n') == NULL);
  201. }
  202. /* return 0 if ok, 1 (or -1) otherwise */
  203. int des_read_pw(char *buf, char *buff, int size, const char *prompt,
  204. int verify)
  205. {
  206. #ifdef OPENSSL_SYS_VMS
  207. struct IOSB iosb;
  208. $DESCRIPTOR(terminal,"TT");
  209. long tty_orig[3], tty_new[3];
  210. long status;
  211. unsigned short channel = 0;
  212. #else
  213. #ifndef OPENSSL_SYS_MSDOS
  214. TTY_STRUCT tty_orig,tty_new;
  215. #endif
  216. #endif
  217. int number;
  218. int ok;
  219. /* statics are simply to avoid warnings about longjmp clobbering
  220. things */
  221. static int ps;
  222. int is_a_tty;
  223. static FILE *tty;
  224. char *p;
  225. if (setjmp(save))
  226. {
  227. ok=0;
  228. goto error;
  229. }
  230. number=5;
  231. ok=0;
  232. ps=0;
  233. is_a_tty=1;
  234. tty=NULL;
  235. #ifdef OPENSSL_SYS_MSDOS
  236. if ((tty=fopen("con","r")) == NULL)
  237. tty=stdin;
  238. #elif defined(MAC_OS_pre_X)
  239. tty=stdin;
  240. #else
  241. #ifndef OPENSSL_SYS_MPE
  242. if ((tty=fopen("/dev/tty","r")) == NULL)
  243. #endif
  244. tty=stdin;
  245. #endif
  246. #if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
  247. if (TTY_get(fileno(tty),&tty_orig) == -1)
  248. {
  249. #ifdef ENOTTY
  250. if (errno == ENOTTY)
  251. is_a_tty=0;
  252. else
  253. #endif
  254. #ifdef EINVAL
  255. /* Ariel Glenn ariel@columbia.edu reports that solaris
  256. * can return EINVAL instead. This should be ok */
  257. if (errno == EINVAL)
  258. is_a_tty=0;
  259. else
  260. #endif
  261. return(-1);
  262. }
  263. memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
  264. #endif
  265. #ifdef OPENSSL_SYS_VMS
  266. status = sys$assign(&terminal,&channel,0,0);
  267. if (status != SS$_NORMAL)
  268. return(-1);
  269. status=sys$qiow(0,channel,IO$_SENSEMODE,&iosb,0,0,tty_orig,12,0,0,0,0);
  270. if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
  271. return(-1);
  272. #endif
  273. pushsig();
  274. ps=1;
  275. #ifdef TTY_FLAGS
  276. tty_new.TTY_FLAGS &= ~ECHO;
  277. #endif
  278. #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
  279. if (is_a_tty && (TTY_set(fileno(tty),&tty_new) == -1))
  280. #ifdef OPENSSL_SYS_MPE
  281. ; /* MPE lies -- echo really has been disabled */
  282. #else
  283. return(-1);
  284. #endif
  285. #endif
  286. #ifdef OPENSSL_SYS_VMS
  287. tty_new[0] = tty_orig[0];
  288. tty_new[1] = tty_orig[1] | TT$M_NOECHO;
  289. tty_new[2] = tty_orig[2];
  290. status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
  291. if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
  292. return(-1);
  293. #endif
  294. ps=2;
  295. while ((!ok) && (number--))
  296. {
  297. fputs(prompt,stderr);
  298. fflush(stderr);
  299. buf[0]='\0';
  300. fgets(buf,size,tty);
  301. if (feof(tty)) goto error;
  302. if (ferror(tty)) goto error;
  303. if ((p=(char *)strchr(buf,'\n')) != NULL)
  304. *p='\0';
  305. else read_till_nl(tty);
  306. if (verify)
  307. {
  308. fprintf(stderr,"\nVerifying password - %s",prompt);
  309. fflush(stderr);
  310. buff[0]='\0';
  311. fgets(buff,size,tty);
  312. if (feof(tty)) goto error;
  313. if ((p=(char *)strchr(buff,'\n')) != NULL)
  314. *p='\0';
  315. else read_till_nl(tty);
  316. if (strcmp(buf,buff) != 0)
  317. {
  318. fprintf(stderr,"\nVerify failure");
  319. fflush(stderr);
  320. break;
  321. /* continue; */
  322. }
  323. }
  324. ok=1;
  325. }
  326. error:
  327. fprintf(stderr,"\n");
  328. #ifdef DEBUG
  329. perror("fgets(tty)");
  330. #endif
  331. /* What can we do if there is an error? */
  332. #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
  333. if (ps >= 2) TTY_set(fileno(tty),&tty_orig);
  334. #endif
  335. #ifdef OPENSSL_SYS_VMS
  336. if (ps >= 2)
  337. status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0
  338. ,tty_orig,12,0,0,0,0);
  339. #endif
  340. if (ps >= 1) popsig();
  341. if (stdin != tty) fclose(tty);
  342. #ifdef OPENSSL_SYS_VMS
  343. status = sys$dassgn(channel);
  344. #endif
  345. return(!ok);
  346. }
  347. #else /* OPENSSL_SYS_WIN16 */
  348. int des_read_pw(char *buf, char *buff, int size, char *prompt, int verify)
  349. {
  350. memset(buf,0,size);
  351. memset(buff,0,size);
  352. return(0);
  353. }
  354. #endif
  355. static void pushsig(void)
  356. {
  357. int i;
  358. #ifdef SIGACTION
  359. struct sigaction sa;
  360. memset(&sa,0,sizeof sa);
  361. sa.sa_handler=recsig;
  362. #endif
  363. for (i=1; i<NX509_SIG; i++)
  364. {
  365. #ifdef SIGUSR1
  366. if (i == SIGUSR1)
  367. continue;
  368. #endif
  369. #ifdef SIGUSR2
  370. if (i == SIGUSR2)
  371. continue;
  372. #endif
  373. #ifdef SIGACTION
  374. sigaction(i,&sa,&savsig[i]);
  375. #else
  376. savsig[i]=signal(i,recsig);
  377. #endif
  378. }
  379. #ifdef SIGWINCH
  380. signal(SIGWINCH,SIG_DFL);
  381. #endif
  382. }
  383. static void popsig(void)
  384. {
  385. int i;
  386. for (i=1; i<NX509_SIG; i++)
  387. {
  388. #ifdef SIGUSR1
  389. if (i == SIGUSR1)
  390. continue;
  391. #endif
  392. #ifdef SIGUSR2
  393. if (i == SIGUSR2)
  394. continue;
  395. #endif
  396. #ifdef SIGACTION
  397. sigaction(i,&savsig[i],NULL);
  398. #else
  399. signal(i,savsig[i]);
  400. #endif
  401. }
  402. }
  403. static void recsig(int i)
  404. {
  405. longjmp(save,1);
  406. #ifdef LINT
  407. i=i;
  408. #endif
  409. }
  410. #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16)
  411. static int noecho_fgets(char *buf, int size, FILE *tty)
  412. {
  413. int i;
  414. char *p;
  415. p=buf;
  416. for (;;)
  417. {
  418. if (size == 0)
  419. {
  420. *p='\0';
  421. break;
  422. }
  423. size--;
  424. #ifdef WIN16TTY
  425. i=_inchar();
  426. #else
  427. i=getch();
  428. #endif
  429. if (i == '\r') i='\n';
  430. *(p++)=i;
  431. if (i == '\n')
  432. {
  433. *p='\0';
  434. break;
  435. }
  436. }
  437. #ifdef WIN_CONSOLE_BUG
  438. /* Win95 has several evil console bugs: one of these is that the
  439. * last character read using getch() is passed to the next read: this is
  440. * usually a CR so this can be trouble. No STDIO fix seems to work but
  441. * flushing the console appears to do the trick.
  442. */
  443. {
  444. HANDLE inh;
  445. inh = GetStdHandle(STD_INPUT_HANDLE);
  446. FlushConsoleInputBuffer(inh);
  447. }
  448. #endif
  449. return(strlen(buf));
  450. }
  451. #endif