ClickHouse

Форк
0
/
readpassphrase.c 
201 строка · 6.3 Кб
1
/*    $OpenBSD: readpassphrase.c,v 1.26 2016/10/18 12:47:18 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
#ifndef _PATH_TTY
27
#define _PATH_TTY "/dev/tty"
28
#endif
29

30
#pragma clang diagnostic ignored "-Wreserved-identifier"
31

32
#include <termios.h>
33
#include <signal.h>
34
#include <ctype.h>
35
#include <fcntl.h>
36
#include "readpassphrase.h"
37
#include <errno.h>
38
#include <string.h>
39
#include <unistd.h>
40

41
#ifndef TCSASOFT
42
/* If we don't have TCSASOFT define it so that ORing it it below is a no-op. */
43
# define TCSASOFT 0
44
#endif
45

46
/* SunOS 4.x which lacks _POSIX_VDISABLE, but has VDISABLE */
47
#if !defined(_POSIX_VDISABLE) && defined(VDISABLE)
48
#  define _POSIX_VDISABLE       VDISABLE
49
#endif
50

51
static volatile sig_atomic_t signo[NSIG];
52

53
static void handler(int);
54

55
char *
56
readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
57
{
58
    ssize_t nr;
59
    int input, output, save_errno, i, need_restart;
60
    char ch, *p, *end;
61
    struct termios term, oterm;
62
    struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
63
    struct sigaction savetstp, savettin, savettou, savepipe;
64

65
    /* I suppose we could alloc on demand in this case (XXX). */
66
    if (bufsiz == 0) {
67
        errno = EINVAL;
68
        return(NULL);
69
    }
70

71
restart:
72
    for (i = 0; i < NSIG; i++)
73
        signo[i] = 0;
74
    nr = -1;
75
    save_errno = 0;
76
    need_restart = 0;
77
    /*
78
     * Read and write to /dev/tty if available.  If not, read from
79
     * stdin and write to stderr unless a tty is required.
80
     */
81
    if ((flags & RPP_STDIN) ||
82
        (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
83
        if (flags & RPP_REQUIRE_TTY) {
84
            errno = ENOTTY;
85
            return(NULL);
86
        }
87
        input = STDIN_FILENO;
88
        output = STDERR_FILENO;
89
    }
90

91
    /*
92
     * Turn off echo if possible.
93
     * If we are using a tty but are not the foreground pgrp this will
94
     * generate SIGTTOU, so do it *before* installing the signal handlers.
95
     */
96
    if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
97
        memcpy(&term, &oterm, sizeof(term));
98
        if (!(flags & RPP_ECHO_ON))
99
            term.c_lflag &= ~((unsigned int) (ECHO | ECHONL));
100
#ifdef VSTATUS
101
        if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
102
            term.c_cc[VSTATUS] = _POSIX_VDISABLE;
103
#endif
104
        (void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
105
    } else {
106
        memset(&term, 0, sizeof(term));
107
        term.c_lflag |= ECHO;
108
        memset(&oterm, 0, sizeof(oterm));
109
        oterm.c_lflag |= ECHO;
110
    }
111

112
    /*
113
     * Catch signals that would otherwise cause the user to end
114
     * up with echo turned off in the shell.  Don't worry about
115
     * things like SIGXCPU and SIGVTALRM for now.
116
     */
117
    sigemptyset(&sa.sa_mask);
118
    sa.sa_flags = 0;        /* don't restart system calls */
119
    sa.sa_handler = handler;
120
    (void)sigaction(SIGALRM, &sa, &savealrm);
121
    (void)sigaction(SIGHUP, &sa, &savehup);
122
    (void)sigaction(SIGINT, &sa, &saveint);
123
    (void)sigaction(SIGPIPE, &sa, &savepipe);
124
    (void)sigaction(SIGQUIT, &sa, &savequit);
125
    (void)sigaction(SIGTERM, &sa, &saveterm);
126
    (void)sigaction(SIGTSTP, &sa, &savetstp);
127
    (void)sigaction(SIGTTIN, &sa, &savettin);
128
    (void)sigaction(SIGTTOU, &sa, &savettou);
129

130
    if (!(flags & RPP_STDIN))
131
        (void)write(output, prompt, strlen(prompt));
132
    end = buf + bufsiz - 1;
133
    p = buf;
134
    while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
135
        if (p < end) {
136
            if ((flags & RPP_SEVENBIT))
137
                ch &= 0x7f;
138
            if (isalpha((unsigned char)ch)) {
139
                if ((flags & RPP_FORCELOWER))
140
                    ch = (char)tolower((unsigned char)ch);
141
                if ((flags & RPP_FORCEUPPER))
142
                    ch = (char)toupper((unsigned char)ch);
143
            }
144
            *p++ = ch;
145
        }
146
    }
147
    *p = '\0';
148
    save_errno = errno;
149
    if (!(term.c_lflag & ECHO))
150
        (void)write(output, "\n", 1);
151

152
    /* Restore old terminal settings and signals. */
153
    if (memcmp(&term, &oterm, sizeof(term)) != 0) {
154
        const int sigttou = (int)signo[SIGTTOU];
155

156
        /* Ignore SIGTTOU generated when we are not the fg pgrp. */
157
        while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 &&
158
            errno == EINTR && !signo[SIGTTOU])
159
            continue;
160
        signo[SIGTTOU] = sigttou;
161
    }
162
    (void)sigaction(SIGALRM, &savealrm, NULL);
163
    (void)sigaction(SIGHUP, &savehup, NULL);
164
    (void)sigaction(SIGINT, &saveint, NULL);
165
    (void)sigaction(SIGQUIT, &savequit, NULL);
166
    (void)sigaction(SIGPIPE, &savepipe, NULL);
167
    (void)sigaction(SIGTERM, &saveterm, NULL);
168
    (void)sigaction(SIGTSTP, &savetstp, NULL);
169
    (void)sigaction(SIGTTIN, &savettin, NULL);
170
    (void)sigaction(SIGTTOU, &savettou, NULL);
171
    if (input != STDIN_FILENO)
172
        (void)close(input);
173

174
    /*
175
     * If we were interrupted by a signal, resend it to ourselves
176
     * now that we have restored the signal handlers.
177
     */
178
    for (i = 0; i < NSIG; i++) {
179
        if (signo[i]) {
180
            kill(getpid(), i);
181
            switch (i) {
182
            case SIGTSTP:
183
            case SIGTTIN:
184
            case SIGTTOU:
185
                need_restart = 1;
186
            }
187
        }
188
    }
189
    if (need_restart)
190
        goto restart;
191

192
    if (save_errno)
193
        errno = save_errno;
194
    return(nr == -1 ? NULL : buf);
195
}
196
//DEF_WEAK(readpassphrase);
197

198
static void handler(int s)
199
{
200
    signo[s] = 1;
201
}
202

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.