read_pwd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. #include <openssl/e_os2.h>
  59. #if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_WIN32)
  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. #ifndef OPENSSL_SYS_WINCE
  97. #include <wincon.h>
  98. #endif
  99. #endif
  100. /* There are 5 types of terminal interface supported,
  101. * TERMIO, TERMIOS, VMS, MSDOS and SGTTY
  102. */
  103. #if defined(__sgi) && !defined(TERMIOS)
  104. #define TERMIOS
  105. #undef TERMIO
  106. #undef SGTTY
  107. #endif
  108. #if defined(linux) && !defined(TERMIO)
  109. #undef TERMIOS
  110. #define TERMIO
  111. #undef SGTTY
  112. #endif
  113. #ifdef _LIBC
  114. #undef TERMIOS
  115. #define TERMIO
  116. #undef SGTTY
  117. #endif
  118. #if !defined(TERMIO) && !defined(TERMIOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MSDOS) && !defined(MAC_OS_pre_X) && !defined(MAC_OS_GUSI_SOURCE)
  119. #undef TERMIOS
  120. #undef TERMIO
  121. #define SGTTY
  122. #endif
  123. #if defined(OPENSSL_SYS_VXWORKS)
  124. #undef TERMIOS
  125. #undef TERMIO
  126. #undef SGTTY
  127. #endif
  128. #ifdef TERMIOS
  129. #include <termios.h>
  130. #define TTY_STRUCT struct termios
  131. #define TTY_FLAGS c_lflag
  132. #define TTY_get(tty,data) tcgetattr(tty,data)
  133. #define TTY_set(tty,data) tcsetattr(tty,TCSANOW,data)
  134. #endif
  135. #ifdef TERMIO
  136. #include <termio.h>
  137. #define TTY_STRUCT struct termio
  138. #define TTY_FLAGS c_lflag
  139. #define TTY_get(tty,data) ioctl(tty,TCGETA,data)
  140. #define TTY_set(tty,data) ioctl(tty,TCSETA,data)
  141. #endif
  142. #ifdef SGTTY
  143. #include <sgtty.h>
  144. #define TTY_STRUCT struct sgttyb
  145. #define TTY_FLAGS sg_flags
  146. #define TTY_get(tty,data) ioctl(tty,TIOCGETP,data)
  147. #define TTY_set(tty,data) ioctl(tty,TIOCSETP,data)
  148. #endif
  149. #if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) && !defined(MAC_OS_pre_X)
  150. #include <sys/ioctl.h>
  151. #endif
  152. #if defined(OPENSSL_SYS_MSDOS) && !defined(__CYGWIN32__) && !defined(OPENSSL_SYS_WINCE)
  153. #include <conio.h>
  154. #define fgets(a,b,c) noecho_fgets(a,b,c)
  155. #endif
  156. #ifdef OPENSSL_SYS_VMS
  157. #include <ssdef.h>
  158. #include <iodef.h>
  159. #include <ttdef.h>
  160. #include <descrip.h>
  161. struct IOSB {
  162. short iosb$w_value;
  163. short iosb$w_count;
  164. long iosb$l_info;
  165. };
  166. #endif
  167. #if defined(MAC_OS_pre_X) || defined(MAC_OS_GUSI_SOURCE)
  168. /*
  169. * This one needs work. As a matter of fact the code is unoperational
  170. * and this is only a trick to get it compiled.
  171. * <appro@fy.chalmers.se>
  172. */
  173. #define TTY_STRUCT int
  174. #endif
  175. #ifndef NX509_SIG
  176. #define NX509_SIG 32
  177. #endif
  178. static void read_till_nl(FILE *);
  179. static void recsig(int);
  180. static void pushsig(void);
  181. static void popsig(void);
  182. #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16)
  183. static int noecho_fgets(char *buf, int size, FILE *tty);
  184. #endif
  185. #ifdef SIGACTION
  186. static struct sigaction savsig[NX509_SIG];
  187. #else
  188. static void (*savsig[NX509_SIG])(int );
  189. #endif
  190. static jmp_buf save;
  191. int des_read_pw_string(char *buf, int length, const char *prompt,
  192. int verify)
  193. {
  194. char buff[BUFSIZ];
  195. int ret;
  196. ret=des_read_pw(buf,buff,(length>BUFSIZ)?BUFSIZ:length,prompt,verify);
  197. OPENSSL_cleanse(buff,BUFSIZ);
  198. return(ret);
  199. }
  200. #ifdef OPENSSL_SYS_WINCE
  201. int des_read_pw(char *buf, char *buff, int size, const char *prompt, int verify)
  202. {
  203. memset(buf,0,size);
  204. memset(buff,0,size);
  205. return(0);
  206. }
  207. #elif defined(OPENSSL_SYS_WIN16)
  208. int des_read_pw(char *buf, char *buff, int size, char *prompt, int verify)
  209. {
  210. memset(buf,0,size);
  211. memset(buff,0,size);
  212. return(0);
  213. }
  214. #else /* !OPENSSL_SYS_WINCE && !OPENSSL_SYS_WIN16 */
  215. static void read_till_nl(FILE *in)
  216. {
  217. #define SIZE 4
  218. char buf[SIZE+1];
  219. do {
  220. fgets(buf,SIZE,in);
  221. } while (strchr(buf,'\n') == NULL);
  222. }
  223. /* return 0 if ok, 1 (or -1) otherwise */
  224. int des_read_pw(char *buf, char *buff, int size, const char *prompt,
  225. int verify)
  226. {
  227. #ifdef OPENSSL_SYS_VMS
  228. struct IOSB iosb;
  229. $DESCRIPTOR(terminal,"TT");
  230. long tty_orig[3], tty_new[3];
  231. long status;
  232. unsigned short channel = 0;
  233. #else
  234. #if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
  235. TTY_STRUCT tty_orig,tty_new;
  236. #endif
  237. #endif
  238. int number;
  239. int ok;
  240. /* statics are simply to avoid warnings about longjmp clobbering
  241. things */
  242. static int ps;
  243. int is_a_tty;
  244. static FILE *tty;
  245. char *p;
  246. if (setjmp(save))
  247. {
  248. ok=0;
  249. goto error;
  250. }
  251. number=5;
  252. ok=0;
  253. ps=0;
  254. is_a_tty=1;
  255. tty=NULL;
  256. #ifdef OPENSSL_SYS_MSDOS
  257. if ((tty=fopen("con","r")) == NULL)
  258. tty=stdin;
  259. #elif defined(MAC_OS_pre_X) || defined(OPENSSL_SYS_VXWORKS)
  260. tty=stdin;
  261. #else
  262. #ifndef OPENSSL_SYS_MPE
  263. if ((tty=fopen("/dev/tty","r")) == NULL)
  264. #endif
  265. tty=stdin;
  266. #endif
  267. #if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
  268. if (TTY_get(fileno(tty),&tty_orig) == -1)
  269. {
  270. #ifdef ENOTTY
  271. if (errno == ENOTTY)
  272. is_a_tty=0;
  273. else
  274. #endif
  275. #ifdef EINVAL
  276. /* Ariel Glenn ariel@columbia.edu reports that solaris
  277. * can return EINVAL instead. This should be ok */
  278. if (errno == EINVAL)
  279. is_a_tty=0;
  280. else
  281. #endif
  282. return(-1);
  283. }
  284. memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
  285. #endif
  286. #ifdef OPENSSL_SYS_VMS
  287. status = sys$assign(&terminal,&channel,0,0);
  288. if (status != SS$_NORMAL)
  289. return(-1);
  290. status=sys$qiow(0,channel,IO$_SENSEMODE,&iosb,0,0,tty_orig,12,0,0,0,0);
  291. if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
  292. return(-1);
  293. #endif
  294. pushsig();
  295. ps=1;
  296. #ifdef TTY_FLAGS
  297. tty_new.TTY_FLAGS &= ~ECHO;
  298. #endif
  299. #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
  300. if (is_a_tty && (TTY_set(fileno(tty),&tty_new) == -1))
  301. #ifdef OPENSSL_SYS_MPE
  302. ; /* MPE lies -- echo really has been disabled */
  303. #else
  304. return(-1);
  305. #endif
  306. #endif
  307. #ifdef OPENSSL_SYS_VMS
  308. tty_new[0] = tty_orig[0];
  309. tty_new[1] = tty_orig[1] | TT$M_NOECHO;
  310. tty_new[2] = tty_orig[2];
  311. status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
  312. if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
  313. return(-1);
  314. #endif
  315. ps=2;
  316. while ((!ok) && (number--))
  317. {
  318. fputs(prompt,stderr);
  319. fflush(stderr);
  320. buf[0]='\0';
  321. fgets(buf,size,tty);
  322. if (feof(tty)) goto error;
  323. if (ferror(tty)) goto error;
  324. if ((p=(char *)strchr(buf,'\n')) != NULL)
  325. *p='\0';
  326. else read_till_nl(tty);
  327. if (verify)
  328. {
  329. fprintf(stderr,"\nVerifying password - %s",prompt);
  330. fflush(stderr);
  331. buff[0]='\0';
  332. fgets(buff,size,tty);
  333. if (feof(tty)) goto error;
  334. if ((p=(char *)strchr(buff,'\n')) != NULL)
  335. *p='\0';
  336. else read_till_nl(tty);
  337. if (strcmp(buf,buff) != 0)
  338. {
  339. fprintf(stderr,"\nVerify failure");
  340. fflush(stderr);
  341. break;
  342. /* continue; */
  343. }
  344. }
  345. ok=1;
  346. }
  347. error:
  348. fprintf(stderr,"\n");
  349. #if 0
  350. perror("fgets(tty)");
  351. #endif
  352. /* What can we do if there is an error? */
  353. #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
  354. if (ps >= 2) TTY_set(fileno(tty),&tty_orig);
  355. #endif
  356. #ifdef OPENSSL_SYS_VMS
  357. if (ps >= 2)
  358. status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0
  359. ,tty_orig,12,0,0,0,0);
  360. #endif
  361. if (ps >= 1) popsig();
  362. if (stdin != tty) fclose(tty);
  363. #ifdef OPENSSL_SYS_VMS
  364. status = sys$dassgn(channel);
  365. #endif
  366. return(!ok);
  367. }
  368. static void pushsig(void)
  369. {
  370. int i;
  371. #ifdef SIGACTION
  372. struct sigaction sa;
  373. memset(&sa,0,sizeof sa);
  374. sa.sa_handler=recsig;
  375. #endif
  376. for (i=1; i<NX509_SIG; i++)
  377. {
  378. #ifdef SIGUSR1
  379. if (i == SIGUSR1)
  380. continue;
  381. #endif
  382. #ifdef SIGUSR2
  383. if (i == SIGUSR2)
  384. continue;
  385. #endif
  386. #ifdef SIGACTION
  387. sigaction(i,&sa,&savsig[i]);
  388. #else
  389. savsig[i]=signal(i,recsig);
  390. #endif
  391. }
  392. #ifdef SIGWINCH
  393. signal(SIGWINCH,SIG_DFL);
  394. #endif
  395. }
  396. static void popsig(void)
  397. {
  398. int i;
  399. for (i=1; i<NX509_SIG; i++)
  400. {
  401. #ifdef SIGUSR1
  402. if (i == SIGUSR1)
  403. continue;
  404. #endif
  405. #ifdef SIGUSR2
  406. if (i == SIGUSR2)
  407. continue;
  408. #endif
  409. #ifdef SIGACTION
  410. sigaction(i,&savsig[i],NULL);
  411. #else
  412. signal(i,savsig[i]);
  413. #endif
  414. }
  415. }
  416. static void recsig(int i)
  417. {
  418. longjmp(save,1);
  419. #ifdef LINT
  420. i=i;
  421. #endif
  422. }
  423. #ifdef OPENSSL_SYS_MSDOS
  424. static int noecho_fgets(char *buf, int size, FILE *tty)
  425. {
  426. int i;
  427. char *p;
  428. p=buf;
  429. for (;;)
  430. {
  431. if (size == 0)
  432. {
  433. *p='\0';
  434. break;
  435. }
  436. size--;
  437. #ifdef WIN16TTY
  438. i=_inchar();
  439. #else
  440. i=getch();
  441. #endif
  442. if (i == '\r') i='\n';
  443. *(p++)=i;
  444. if (i == '\n')
  445. {
  446. *p='\0';
  447. break;
  448. }
  449. }
  450. #ifdef WIN_CONSOLE_BUG
  451. /* Win95 has several evil console bugs: one of these is that the
  452. * last character read using getch() is passed to the next read: this is
  453. * usually a CR so this can be trouble. No STDIO fix seems to work but
  454. * flushing the console appears to do the trick.
  455. */
  456. {
  457. HANDLE inh;
  458. inh = GetStdHandle(STD_INPUT_HANDLE);
  459. FlushConsoleInputBuffer(inh);
  460. }
  461. #endif
  462. return(strlen(buf));
  463. }
  464. #endif
  465. #endif /* !OPENSSL_SYS_WINCE && !WIN16 */