source: libabac/compat/readpassphrase.c @ 461541a

abac0-leakabac0-meimei-idmei-rt0-nmei_rt0tvf-new-xml
Last change on this file since 461541a was 461541a, checked in by Mei <mei@…>, 11 years ago

1) updated original rt0 to remove libstrongswan dependency

a) identity credential being made/accessed with openssl api calls

(X509/EVP_PKEY pem)

b) attribute credential being made/access via xmlsec1 (custom XML

structure)

2) refactored libcreddy into libabac and now one ABAC namespace for

libabac

3) added attribute_rule suboption to creddy's attribute as another way

to insert access rule

4) added some regression tests into example directory
5) updated some docs.

  • 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 nr;
66        int input, output, save_errno, i, need_restart;
67        char ch, *p, *end;
68        struct termios term, oterm;
69        struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
70        struct sigaction savetstp, savettin, savettou, savepipe;
71
72        /* I suppose we could alloc on demand in this case (XXX). */
73        if (bufsiz == 0) {
74                errno = EINVAL;
75                return(NULL);
76        }
77
78restart:
79        for (i = 0; i < NSIG; i++)
80                signo[i] = 0;
81        nr = -1;
82        save_errno = 0;
83        need_restart = 0;
84        /*
85         * Read and write to /dev/tty if available.  If not, read from
86         * stdin and write to stderr unless a tty is required.
87         */
88        if ((flags & RPP_STDIN) ||
89            (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
90                if (flags & RPP_REQUIRE_TTY) {
91                        errno = ENOTTY;
92                        return(NULL);
93                }
94                input = STDIN_FILENO;
95                output = STDERR_FILENO;
96        }
97
98        /*
99         * Turn off echo if possible.
100         * If we are using a tty but are not the foreground pgrp this will
101         * generate SIGTTOU, so do it *before* installing the signal handlers.
102         */
103        if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
104                memcpy(&term, &oterm, sizeof(term));
105                if (!(flags & RPP_ECHO_ON))
106                        term.c_lflag &= ~(ECHO | ECHONL);
107#ifdef VSTATUS
108                if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
109                        term.c_cc[VSTATUS] = _POSIX_VDISABLE;
110#endif
111                (void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
112        } else {
113                memset(&term, 0, sizeof(term));
114                term.c_lflag |= ECHO;
115                memset(&oterm, 0, sizeof(oterm));
116                oterm.c_lflag |= ECHO;
117        }
118
119        /*
120         * Catch signals that would otherwise cause the user to end
121         * up with echo turned off in the shell.  Don't worry about
122         * things like SIGXCPU and SIGVTALRM for now.
123         */
124        sigemptyset(&sa.sa_mask);
125        sa.sa_flags = 0;                /* don't restart system calls */
126        sa.sa_handler = handler;
127        (void)sigaction(SIGALRM, &sa, &savealrm);
128        (void)sigaction(SIGHUP, &sa, &savehup);
129        (void)sigaction(SIGINT, &sa, &saveint);
130        (void)sigaction(SIGPIPE, &sa, &savepipe);
131        (void)sigaction(SIGQUIT, &sa, &savequit);
132        (void)sigaction(SIGTERM, &sa, &saveterm);
133        (void)sigaction(SIGTSTP, &sa, &savetstp);
134        (void)sigaction(SIGTTIN, &sa, &savettin);
135        (void)sigaction(SIGTTOU, &sa, &savettou);
136
137        if (!(flags & RPP_STDIN))
138                (void)write(output, prompt, strlen(prompt));
139        end = buf + bufsiz - 1;
140        p = buf;
141        while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
142                if (p < end) {
143                        if ((flags & RPP_SEVENBIT))
144                                ch &= 0x7f;
145                        if (isalpha(ch)) {
146                                if ((flags & RPP_FORCELOWER))
147                                        ch = (char)tolower(ch);
148                                if ((flags & RPP_FORCEUPPER))
149                                        ch = (char)toupper(ch);
150                        }
151                        *p++ = ch;
152                }
153        }
154        *p = '\0';
155        save_errno = errno;
156        if (!(term.c_lflag & ECHO))
157                (void)write(output, "\n", 1);
158
159        /* Restore old terminal settings and signals. */
160        if (memcmp(&term, &oterm, sizeof(term)) != 0) {
161                sigset_t ttou_sigset, old_sigset;
162                sigemptyset(&ttou_sigset);
163                sigaddset(&ttou_sigset, SIGTTOU);
164                sigprocmask(SIG_BLOCK, &ttou_sigset, &old_sigset);
165                while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 &&
166                    errno == EINTR)
167                        continue;
168                sigprocmask(SIG_SETMASK, &old_sigset, NULL);
169        }
170        (void)sigaction(SIGALRM, &savealrm, NULL);
171        (void)sigaction(SIGHUP, &savehup, NULL);
172        (void)sigaction(SIGINT, &saveint, NULL);
173        (void)sigaction(SIGQUIT, &savequit, NULL);
174        (void)sigaction(SIGPIPE, &savepipe, NULL);
175        (void)sigaction(SIGTERM, &saveterm, NULL);
176        (void)sigaction(SIGTSTP, &savetstp, NULL);
177        (void)sigaction(SIGTTIN, &savettin, NULL);
178        (void)sigaction(SIGTTOU, &savettou, NULL);
179        if (input != STDIN_FILENO)
180                (void)close(input);
181
182        /*
183         * If we were interrupted by a signal, resend it to ourselves
184         * now that we have restored the signal handlers.
185         */
186        for (i = 0; i < NSIG; i++) {
187                if (signo[i]) {
188                        kill(getpid(), i);
189                        switch (i) {
190                        case SIGTSTP:
191                        case SIGTTIN:
192                        case SIGTTOU:
193                                need_restart = 1;
194                        }
195                }
196        }
197        if (need_restart)
198                goto restart;
199
200        return(nr == -1 ? errno = save_errno, NULL : buf);
201}
202
203static void handler(int s)
204{
205
206        signo[s] = 1;
207}
208
209#endif /* ! HAVE_READPASSPHRASE */
Note: See TracBrowser for help on using the repository browser.