treadpassphrase.c - ratox - FIFO based tox client
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       treadpassphrase.c (5310B)
       ---
            1 /*        $OpenBSD: readpassphrase.c,v 1.25 2015/09/14 10:45:27 guenther Exp $        */
            2 
            3 /*
            4  * Copyright (c) 2000-2002, 2007, 2010
            5  *        Todd C. Miller <Todd.Miller@courtesan.com>
            6  *
            7  * Permission to use, copy, modify, and distribute this software for any
            8  * purpose with or without fee is hereby granted, provided that the above
            9  * copyright notice and this permission notice appear in all copies.
           10  *
           11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
           12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
           13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
           14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
           15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
           16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
           17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
           18  *
           19  * Sponsored in part by the Defense Advanced Research Projects
           20  * Agency (DARPA) and Air Force Research Laboratory, Air Force
           21  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
           22  */
           23 
           24 #include <ctype.h>
           25 #include <errno.h>
           26 #include <fcntl.h>
           27 #include <paths.h>
           28 #include <pwd.h>
           29 #include <signal.h>
           30 #include <string.h>
           31 #include <termios.h>
           32 #include <unistd.h>
           33 
           34 #include "readpassphrase.h"
           35 
           36 #ifdef TCSASOFT
           37 #define _T_FLUSH        (TCSAFLUSH|TCSASOFT)
           38 #else
           39 #define _T_FLUSH        (TCSAFLUSH)
           40 #endif
           41 
           42 static volatile sig_atomic_t signo[_NSIG];
           43 
           44 static void handler(int);
           45 
           46 char *
           47 readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
           48 {
           49         ssize_t nr;
           50         int input, output, save_errno, i, need_restart;
           51         char ch, *p, *end;
           52         struct termios term, oterm;
           53         struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
           54         struct sigaction savetstp, savettin, savettou, savepipe;
           55 
           56         /* I suppose we could alloc on demand in this case (XXX). */
           57         if (bufsiz == 0) {
           58                 errno = EINVAL;
           59                 return(NULL);
           60         }
           61 
           62 restart:
           63         for (i = 0; i < _NSIG; i++)
           64                 signo[i] = 0;
           65         nr = -1;
           66         save_errno = 0;
           67         need_restart = 0;
           68         /*
           69          * Read and write to /dev/tty if available.  If not, read from
           70          * stdin and write to stderr unless a tty is required.
           71          */
           72         if ((flags & RPP_STDIN) ||
           73             (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
           74                 if (flags & RPP_REQUIRE_TTY) {
           75                         errno = ENOTTY;
           76                         return(NULL);
           77                 }
           78                 input = STDIN_FILENO;
           79                 output = STDERR_FILENO;
           80         }
           81 
           82         /*
           83          * Turn off echo if possible.
           84          * If we are using a tty but are not the foreground pgrp this will
           85          * generate SIGTTOU, so do it *before* installing the signal handlers.
           86          */
           87         if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
           88                 memcpy(&term, &oterm, sizeof(term));
           89                 if (!(flags & RPP_ECHO_ON))
           90                         term.c_lflag &= ~(ECHO | ECHONL);
           91 #ifdef VSTATUS
           92                 if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
           93                         term.c_cc[VSTATUS] = _POSIX_VDISABLE;
           94 #endif
           95                 (void)tcsetattr(input, _T_FLUSH, &term);
           96         } else {
           97                 memset(&term, 0, sizeof(term));
           98                 term.c_lflag |= ECHO;
           99                 memset(&oterm, 0, sizeof(oterm));
          100                 oterm.c_lflag |= ECHO;
          101         }
          102 
          103         /*
          104          * Catch signals that would otherwise cause the user to end
          105          * up with echo turned off in the shell.  Don't worry about
          106          * things like SIGXCPU and SIGVTALRM for now.
          107          */
          108         sigemptyset(&sa.sa_mask);
          109         sa.sa_flags = 0;                /* don't restart system calls */
          110         sa.sa_handler = handler;
          111         (void)sigaction(SIGALRM, &sa, &savealrm);
          112         (void)sigaction(SIGHUP, &sa, &savehup);
          113         (void)sigaction(SIGINT, &sa, &saveint);
          114         (void)sigaction(SIGPIPE, &sa, &savepipe);
          115         (void)sigaction(SIGQUIT, &sa, &savequit);
          116         (void)sigaction(SIGTERM, &sa, &saveterm);
          117         (void)sigaction(SIGTSTP, &sa, &savetstp);
          118         (void)sigaction(SIGTTIN, &sa, &savettin);
          119         (void)sigaction(SIGTTOU, &sa, &savettou);
          120 
          121         if (!(flags & RPP_STDIN))
          122                 (void)write(output, prompt, strlen(prompt));
          123         end = buf + bufsiz - 1;
          124         p = buf;
          125         while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
          126                 if (p < end) {
          127                         if ((flags & RPP_SEVENBIT))
          128                                 ch &= 0x7f;
          129                         if (isalpha((unsigned char)ch)) {
          130                                 if ((flags & RPP_FORCELOWER))
          131                                         ch = (char)tolower((unsigned char)ch);
          132                                 if ((flags & RPP_FORCEUPPER))
          133                                         ch = (char)toupper((unsigned char)ch);
          134                         }
          135                         *p++ = ch;
          136                 }
          137         }
          138         *p = '\0';
          139         save_errno = errno;
          140         if (!(term.c_lflag & ECHO))
          141                 (void)write(output, "\n", 1);
          142 
          143         /* Restore old terminal settings and signals. */
          144         if (memcmp(&term, &oterm, sizeof(term)) != 0) {
          145                 while (tcsetattr(input, _T_FLUSH, &oterm) == -1 &&
          146                     errno == EINTR && !signo[SIGTTOU])
          147                         continue;
          148         }
          149         (void)sigaction(SIGALRM, &savealrm, NULL);
          150         (void)sigaction(SIGHUP, &savehup, NULL);
          151         (void)sigaction(SIGINT, &saveint, NULL);
          152         (void)sigaction(SIGQUIT, &savequit, NULL);
          153         (void)sigaction(SIGPIPE, &savepipe, NULL);
          154         (void)sigaction(SIGTERM, &saveterm, NULL);
          155         (void)sigaction(SIGTSTP, &savetstp, NULL);
          156         (void)sigaction(SIGTTIN, &savettin, NULL);
          157         (void)sigaction(SIGTTOU, &savettou, NULL);
          158         if (input != STDIN_FILENO)
          159                 (void)close(input);
          160 
          161         /*
          162          * If we were interrupted by a signal, resend it to ourselves
          163          * now that we have restored the signal handlers.
          164          */
          165         for (i = 0; i < _NSIG; i++) {
          166                 if (signo[i]) {
          167                         kill(getpid(), i);
          168                         switch (i) {
          169                         case SIGTSTP:
          170                         case SIGTTIN:
          171                         case SIGTTOU:
          172                                 need_restart = 1;
          173                         }
          174                 }
          175         }
          176         if (need_restart)
          177                 goto restart;
          178 
          179         if (save_errno)
          180                 errno = save_errno;
          181         return(nr == -1 ? NULL : buf);
          182 }
          183 
          184 static void handler(int s)
          185 {
          186         signo[s] = 1;
          187 }