[BACK]Return to readpass.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/readpass.c, Revision 1.1

1.1     ! deraadt     1: /*
        !             2:
        !             3: readpass.c
        !             4:
        !             5: Author: Tatu Ylonen <ylo@cs.hut.fi>
        !             6:
        !             7: Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
        !             8:                    All rights reserved
        !             9:
        !            10: Created: Mon Jul 10 22:08:59 1995 ylo
        !            11:
        !            12: Functions for reading passphrases and passwords.
        !            13:
        !            14: */
        !            15:
        !            16: #include "includes.h"
        !            17: RCSID("$Id: readpass.c,v 1.2 1999/05/04 11:59:03 bg Exp $");
        !            18:
        !            19: #include "xmalloc.h"
        !            20: #include "ssh.h"
        !            21:
        !            22: /* Saved old terminal mode for read_passphrase. */
        !            23: #ifdef USING_TERMIOS
        !            24: static struct termios saved_tio;
        !            25: #endif
        !            26: #ifdef USING_SGTTY
        !            27: static struct sgttyb saved_tio;
        !            28: #endif
        !            29:
        !            30: /* Old interrupt signal handler for read_passphrase. */
        !            31: static RETSIGTYPE (*old_handler)(int sig) = NULL;
        !            32:
        !            33: /* Interrupt signal handler for read_passphrase. */
        !            34:
        !            35: RETSIGTYPE intr_handler(int sig)
        !            36: {
        !            37:   /* Restore terminal modes. */
        !            38: #ifdef USING_TERMIOS
        !            39:   tcsetattr(fileno(stdin), TCSANOW, &saved_tio);
        !            40: #endif
        !            41: #ifdef USING_SGTTY
        !            42:   ioctl(fileno(stdin), TIOCSETP, &saved_tio);
        !            43: #endif
        !            44:   /* Restore the old signal handler. */
        !            45:   signal(sig, old_handler);
        !            46:   /* Resend the signal, with the old handler. */
        !            47:   kill(getpid(), sig);
        !            48: }
        !            49:
        !            50: /* Reads a passphrase from /dev/tty with echo turned off.  Returns the
        !            51:    passphrase (allocated with xmalloc).  Exits if EOF is encountered.
        !            52:    The passphrase if read from stdin if from_stdin is true (as is the
        !            53:    case with ssh-keygen).  */
        !            54:
        !            55: char *read_passphrase(const char *prompt, int from_stdin)
        !            56: {
        !            57:   char buf[1024], *cp;
        !            58: #ifdef USING_TERMIOS
        !            59:   struct termios tio;
        !            60: #endif
        !            61: #ifdef USING_SGTTY
        !            62:   struct sgttyb tio;
        !            63: #endif
        !            64:   FILE *f;
        !            65:
        !            66:   if (from_stdin)
        !            67:     f = stdin;
        !            68:   else
        !            69:     {
        !            70:       /* Read the passphrase from /dev/tty to make it possible to ask it even
        !            71:         when stdin has been redirected. */
        !            72:       f = fopen("/dev/tty", "r");
        !            73:       if (!f)
        !            74:        {
        !            75:          if (getenv("DISPLAY"))
        !            76:            {
        !            77:              char command[512];
        !            78:              fprintf(stderr,
        !            79:                      "Executing ssh-askpass to query the password...\n");
        !            80:              fflush(stdout);
        !            81:              fflush(stderr);
        !            82:              sprintf(command, "ssh-askpass '%.400s'", prompt);
        !            83:              f = popen(command, "r");
        !            84:              if (!fgets(buf, sizeof(buf), f))
        !            85:                {
        !            86:                  pclose(f);
        !            87:                  fprintf(stderr, "No passphrase supplied.  Exiting.\n");
        !            88:                  exit(1);
        !            89:                }
        !            90:              pclose(f);
        !            91:              if (strchr(buf, '\n'))
        !            92:                *strchr(buf, '\n') = 0;
        !            93:              return xstrdup(buf);
        !            94:            }
        !            95:
        !            96:          /* No controlling terminal and no DISPLAY.  Nowhere to read. */
        !            97:          fprintf(stderr, "You have no controlling tty and no DISPLAY.  Cannot read passphrase.\n");
        !            98:          exit(1);
        !            99:        }
        !           100:     }
        !           101:
        !           102:   /* Display the prompt (on stderr because stdout might be redirected). */
        !           103:   fflush(stdout);
        !           104:   fprintf(stderr, "%s", prompt);
        !           105:   fflush(stderr);
        !           106:
        !           107:   /* Get terminal modes. */
        !           108: #ifdef USING_TERMIOS
        !           109:   tcgetattr(fileno(f), &tio);
        !           110: #endif
        !           111: #ifdef USING_SGTTY
        !           112:   ioctl(fileno(f), TIOCGETP, &tio);
        !           113: #endif
        !           114:   saved_tio = tio;
        !           115:   /* Save signal handler and set the new handler. */
        !           116:   old_handler = signal(SIGINT, intr_handler);
        !           117:
        !           118:   /* Set new terminal modes disabling all echo. */
        !           119: #ifdef USING_TERMIOS
        !           120:   tio.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
        !           121:   tcsetattr(fileno(f), TCSANOW, &tio);
        !           122: #endif
        !           123: #ifdef USING_SGTTY
        !           124:   tio.sg_flags &= ~(ECHO);
        !           125:   ioctl(fileno(f), TIOCSETP, &tio);
        !           126: #endif
        !           127:
        !           128:   /* Read the passphrase from the terminal. */
        !           129:   if (fgets(buf, sizeof(buf), f) == NULL)
        !           130:     {
        !           131:       /* Got EOF.  Just exit. */
        !           132:       /* Restore terminal modes. */
        !           133: #ifdef USING_TERMIOS
        !           134:       tcsetattr(fileno(f), TCSANOW, &saved_tio);
        !           135: #endif
        !           136: #ifdef USING_SGTTY
        !           137:       ioctl(fileno(f), TIOCSETP, &saved_tio);
        !           138: #endif
        !           139:       /* Restore the signal handler. */
        !           140:       signal(SIGINT, old_handler);
        !           141:       /* Print a newline (the prompt probably didn\'t have one). */
        !           142:       fprintf(stderr, "\n");
        !           143:       /* Close the file. */
        !           144:       if (f != stdin)
        !           145:        fclose(f);
        !           146:       exit(1);
        !           147:     }
        !           148:   /* Restore terminal modes. */
        !           149: #ifdef USING_TERMIOS
        !           150:   tcsetattr(fileno(f), TCSANOW, &saved_tio);
        !           151: #endif
        !           152: #ifdef USING_SGTTY
        !           153:   ioctl(fileno(f), TIOCSETP, &saved_tio);
        !           154: #endif
        !           155:   /* Restore the signal handler. */
        !           156:   (void)signal(SIGINT, old_handler);
        !           157:   /* Remove newline from the passphrase. */
        !           158:   if (strchr(buf, '\n'))
        !           159:     *strchr(buf, '\n') = 0;
        !           160:   /* Allocate a copy of the passphrase. */
        !           161:   cp = xstrdup(buf);
        !           162:   /* Clear the buffer so we don\'t leave copies of the passphrase laying
        !           163:      around. */
        !           164:   memset(buf, 0, sizeof(buf));
        !           165:   /* Print a newline since the prompt probably didn\'t have one. */
        !           166:   fprintf(stderr, "\n");
        !           167:   /* Close the file. */
        !           168:   if (f != stdin)
        !           169:     fclose(f);
        !           170:   return cp;
        !           171: }