source: libabac/compat/readpassphrase.c @ 3c30b59

abac0-leakabac0-mei
Last change on this file since 3c30b59 was 3c30b59, checked in by Mei <mei@…>, 11 years ago

1) add in new refactored regression testing directory
2) undo the abac.hh/ABAC.hh api changes
3) merged with Ted's changes to attribute format/nickname/issuer processing

  • Property mode set to 100755
File size: 5.6 KB
Line 
1/*      $OpenBSD: readpassphrase.c,v 1.23 2010/05/14 13:30:34 millert 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/* OPENBSD ORIGINAL: lib/libc/gen/readpassphrase.c */
25
26/**
27 * Tom Carroll <thomas.carroll@pnnl.gov>
28 *
29 * Properly handled tcsetattr and SIGTTOU
30 */
31
32#ifndef HAVE_READPASSPHRASE
33
34#include <ctype.h>
35#include <errno.h>
36#include <fcntl.h>
37#ifdef HAVE_PATHS_H
38# include <paths.h>
39#endif
40#include <signal.h>
41#include <string.h>
42#include <termios.h>
43#include <unistd.h>
44#include "compat/readpassphrase.h"
45
46#ifndef TCSASOFT
47# define TCSASOFT 0
48#endif
49
50#ifndef NSIG
51# ifdef _NSIG
52#  define NSIG _NSIG
53# else
54#  define NSIG 128
55# endif
56#endif
57
58static volatile sig_atomic_t signo[NSIG];
59
60static void handler(int);
61
62char *
63readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
64{
65        ssize_t tmp;
66        ssize_t nr;
67        int input, output, save_errno, i, need_restart;
68        char ch, *p, *end;
69        struct termios term, oterm;
70        struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
71        struct sigaction savetstp, savettin, savettou, savepipe;
72
73        /* I suppose we could alloc on demand in this case (XXX). */
74        if (bufsiz == 0) {
75                errno = EINVAL;
76                return(NULL);
77        }
78
79restart:
80        for (i = 0; i < NSIG; i++)
81                signo[i] = 0;
82        nr = -1;
83        save_errno = 0;
84        need_restart = 0;
85        /*
86         * Read and write to /dev/tty if available.  If not, read from
87         * stdin and write to stderr unless a tty is required.
88         */
89        if ((flags & RPP_STDIN) ||
90            (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
91                if (flags & RPP_REQUIRE_TTY) {
92                        errno = ENOTTY;
93                        return(NULL);
94                }
95                input = STDIN_FILENO;
96                output = STDERR_FILENO;
97        }
98
99        /*
100         * Turn off echo if possible.
101         * If we are using a tty but are not the foreground pgrp this will
102         * generate SIGTTOU, so do it *before* installing the signal handlers.
103         */
104        if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
105                memcpy(&term, &oterm, sizeof(term));
106                if (!(flags & RPP_ECHO_ON))
107                        term.c_lflag &= ~(ECHO | ECHONL);
108#ifdef VSTATUS
109                if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
110                        term.c_cc[VSTATUS] = _POSIX_VDISABLE;
111#endif
112                (void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
113        } else {
114                memset(&term, 0, sizeof(term));
115                term.c_lflag |= ECHO;
116                memset(&oterm, 0, sizeof(oterm));
117                oterm.c_lflag |= ECHO;
118        }
119
120        /*
121         * Catch signals that would otherwise cause the user to end
122         * up with echo turned off in the shell.  Don't worry about
123         * things like SIGXCPU and SIGVTALRM for now.
124         */
125        sigemptyset(&sa.sa_mask);
126        sa.sa_flags = 0;                /* don't restart system calls */
127        sa.sa_handler = handler;
128        (void)sigaction(SIGALRM, &sa, &savealrm);
129        (void)sigaction(SIGHUP, &sa, &savehup);
130        (void)sigaction(SIGINT, &sa, &saveint);
131        (void)sigaction(SIGPIPE, &sa, &savepipe);
132        (void)sigaction(SIGQUIT, &sa, &savequit);
133        (void)sigaction(SIGTERM, &sa, &saveterm);
134        (void)sigaction(SIGTSTP, &sa, &savetstp);
135        (void)sigaction(SIGTTIN, &sa, &savettin);
136        (void)sigaction(SIGTTOU, &sa, &savettou);
137
138        if (!(flags & RPP_STDIN))
139                tmp=write(output, prompt, strlen(prompt));
140        end = buf + bufsiz - 1;
141        p = buf;
142        while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
143                if (p < end) {
144                        if ((flags & RPP_SEVENBIT))
145                                ch &= 0x7f;
146                        if (isalpha(ch)) {
147                                if ((flags & RPP_FORCELOWER))
148                                        ch = (char)tolower(ch);
149                                if ((flags & RPP_FORCEUPPER))
150                                        ch = (char)toupper(ch);
151                        }
152                        *p++ = ch;
153                }
154        }
155        *p = '\0';
156        save_errno = errno;
157        if (!(term.c_lflag & ECHO))
158                tmp=write(output, "\n", 1);
159
160        /* Restore old terminal settings and signals. */
161        if (memcmp(&term, &oterm, sizeof(term)) != 0) {
162                sigset_t ttou_sigset, old_sigset;
163                sigemptyset(&ttou_sigset);
164                sigaddset(&ttou_sigset, SIGTTOU);
165                sigprocmask(SIG_BLOCK, &ttou_sigset, &old_sigset);
166                while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 &&
167                    errno == EINTR)
168                        continue;
169                sigprocmask(SIG_SETMASK, &old_sigset, NULL);
170        }
171        (void)sigaction(SIGALRM, &savealrm, NULL);
172        (void)sigaction(SIGHUP, &savehup, NULL);
173        (void)sigaction(SIGINT, &saveint, NULL);
174        (void)sigaction(SIGQUIT, &savequit, NULL);
175        (void)sigaction(SIGPIPE, &savepipe, NULL);
176        (void)sigaction(SIGTERM, &saveterm, NULL);
177        (void)sigaction(SIGTSTP, &savetstp, NULL);
178        (void)sigaction(SIGTTIN, &savettin, NULL);
179        (void)sigaction(SIGTTOU, &savettou, NULL);
180        if (input != STDIN_FILENO)
181                (void)close(input);
182
183        /*
184         * If we were interrupted by a signal, resend it to ourselves
185         * now that we have restored the signal handlers.
186         */
187        for (i = 0; i < NSIG; i++) {
188                if (signo[i]) {
189                        kill(getpid(), i);
190                        switch (i) {
191                        case SIGTSTP:
192                        case SIGTTIN:
193                        case SIGTTOU:
194                                need_restart = 1;
195                        }
196                }
197        }
198        if (need_restart)
199                goto restart;
200
201        return(nr == -1 ? errno = save_errno, NULL : buf);
202}
203
204static void handler(int s)
205{
206
207        signo[s] = 1;
208}
209
210#endif /* ! HAVE_READPASSPHRASE */
Note: See TracBrowser for help on using the repository browser.