summaryrefslogtreecommitdiffstats
path: root/src/lib/krb5/os/prompter.c
blob: 26cdebc37879585ed7703877db1135b7d630b809 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#include "k5-int.h"
#include "os-proto.h"
#if !defined(_WIN32) || (defined(_WIN32) && defined(__CYGWIN32__))
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <limits.h>
/* Is vxworks broken w.r.t. termios? --tlyu */
#ifdef __vxworks
#define ECHO_PASSWORD
#endif

#include <termios.h>

#ifdef POSIX_SIGNALS
typedef struct sigaction osiginfo;
#else
typedef struct krb5_sigtype (*osiginfo)();
#endif

static void     catch_signals(osiginfo *);
static void     restore_signals(osiginfo *);
static krb5_sigtype     intrfunc(int sig);

static krb5_error_code  setup_tty(FILE*, int, struct termios *, osiginfo *);
static krb5_error_code  restore_tty(FILE*, struct termios *, osiginfo *);

static volatile int got_int;    /* should be sig_atomic_t */

krb5_error_code KRB5_CALLCONV
krb5_prompter_posix(
    krb5_context        context,
    void                *data,
    const char          *name,
    const char          *banner,
    int                 num_prompts,
    krb5_prompt         prompts[])
{
    int         fd, i, scratchchar;
    FILE        *fp;
    char        *retp;
    krb5_error_code     errcode;
    struct termios saveparm;
    osiginfo osigint;

    errcode = KRB5_LIBOS_CANTREADPWD;

    if (name) {
        fputs(name, stdout);
        fputs("\n", stdout);
    }
    if (banner) {
        fputs(banner, stdout);
        fputs("\n", stdout);
    }

    /*
     * Get a non-buffered stream on stdin.
     */
    fp = NULL;
    fd = dup(STDIN_FILENO);
    if (fd < 0)
        return KRB5_LIBOS_CANTREADPWD;
    set_cloexec_fd(fd);
    fp = fdopen(fd, "r");
    if (fp == NULL)
        goto cleanup;
    if (setvbuf(fp, NULL, _IONBF, 0))
        goto cleanup;

    for (i = 0; i < num_prompts; i++) {
        errcode = KRB5_LIBOS_CANTREADPWD;
        /* fgets() takes int, but krb5_data.length is unsigned. */
        if (prompts[i].reply->length > INT_MAX)
            goto cleanup;

        errcode = setup_tty(fp, prompts[i].hidden, &saveparm, &osigint);
        if (errcode)
            break;

        /* put out the prompt */
        (void)fputs(prompts[i].prompt, stdout);
        (void)fputs(": ", stdout);
        (void)fflush(stdout);
        (void)memset(prompts[i].reply->data, 0, prompts[i].reply->length);

        got_int = 0;
        retp = fgets(prompts[i].reply->data, (int)prompts[i].reply->length,
                     fp);
        if (prompts[i].hidden)
            putchar('\n');
        if (retp == NULL) {
            if (got_int)
                errcode = KRB5_LIBOS_PWDINTR;
            else
                errcode = KRB5_LIBOS_CANTREADPWD;
            restore_tty(fp, &saveparm, &osigint);
            break;
        }

        /* replace newline with null */
        retp = strchr(prompts[i].reply->data, '\n');
        if (retp != NULL)
            *retp = '\0';
        else {
            /* flush rest of input line */
            do {
                scratchchar = getc(fp);
            } while (scratchchar != EOF && scratchchar != '\n');
        }

        errcode = restore_tty(fp, &saveparm, &osigint);
        if (errcode)
            break;
        prompts[i].reply->length = strlen(prompts[i].reply->data);
    }
cleanup:
    if (fp != NULL)
        fclose(fp);
    else if (fd >= 0)
        close(fd);

    return errcode;
}

static krb5_sigtype
intrfunc(int sig)
{
    got_int = 1;
}

static void
catch_signals(osiginfo *osigint)
{
#ifdef POSIX_SIGNALS
    struct sigaction sa;

    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    sa.sa_handler = intrfunc;
    sigaction(SIGINT, &sa, osigint);
#else
    *osigint = signal(SIGINT, intrfunc);
#endif
}

static void
restore_signals(osiginfo *osigint)
{
#ifdef POSIX_SIGNALS
    sigaction(SIGINT, osigint, NULL);
#else
    signal(SIGINT, *osigint);
#endif
}

static krb5_error_code
setup_tty(FILE *fp, int hidden, struct termios *saveparm, osiginfo *osigint)
{
    krb5_error_code     ret;
    int                 fd;
    struct termios      tparm;

    ret = KRB5_LIBOS_CANTREADPWD;
    catch_signals(osigint);
    fd = fileno(fp);
    do {
        if (!isatty(fd)) {
            ret = 0;
            break;
        }
        if (tcgetattr(fd, &tparm) < 0)
            break;
        *saveparm = tparm;
#ifndef ECHO_PASSWORD
        if (hidden)
            tparm.c_lflag &= ~(ECHO|ECHONL);
#endif
        tparm.c_lflag |= ISIG|ICANON;
        if (tcsetattr(STDIN_FILENO, TCSANOW, &tparm) < 0)
            break;
        ret = 0;
    } while (0);
    /* If we're losing, restore signal handlers. */
    if (ret)
        restore_signals(osigint);
    return ret;
}

static krb5_error_code
restore_tty(FILE* fp, struct termios *saveparm, osiginfo *osigint)
{
    int ret, fd;

    ret = 0;
    fd = fileno(fp);
    if (isatty(fd)) {
        ret = tcsetattr(fd, TCSANOW, saveparm);
        if (ret < 0)
            ret = KRB5_LIBOS_CANTREADPWD;
        else
            ret = 0;
    }
    restore_signals(osigint);
    return ret;
}

#else /* non-Cygwin Windows, or Mac */

#if defined(_WIN32)

#include <io.h>

krb5_error_code KRB5_CALLCONV
krb5_prompter_posix(krb5_context context,
                    void *data,
                    const char *name,
                    const char *banner,
                    int num_prompts,
                    krb5_prompt prompts[])
{
    HANDLE              handle;
    DWORD               old_mode, new_mode;
    char                *ptr;
    int                 scratchchar;
    krb5_error_code     errcode = 0;
    int                 i;

    handle = GetStdHandle(STD_INPUT_HANDLE);
    if (handle == INVALID_HANDLE_VALUE)
        return ENOTTY;
    if (!GetConsoleMode(handle, &old_mode))
        return ENOTTY;

    new_mode = old_mode;
    new_mode |=  ( ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT );
    new_mode &= ~( ENABLE_ECHO_INPUT );

    if (!SetConsoleMode(handle, new_mode))
        return ENOTTY;

    if (!SetConsoleMode(handle, old_mode))
        return ENOTTY;

    if (name) {
        fputs(name, stdout);
        fputs("\n", stdout);
    }

    if (banner) {
        fputs(banner, stdout);
        fputs("\n", stdout);
    }

    for (i = 0; i < num_prompts; i++) {
        if (prompts[i].hidden) {
            if (!SetConsoleMode(handle, new_mode)) {
                errcode = ENOTTY;
                goto cleanup;
            }
        }

        fputs(prompts[i].prompt,stdout);
        fputs(": ", stdout);
        fflush(stdout);
        memset(prompts[i].reply->data, 0, prompts[i].reply->length);

        if (fgets(prompts[i].reply->data, prompts[i].reply->length, stdin)
            == NULL) {
            if (prompts[i].hidden)
                putchar('\n');
            errcode = KRB5_LIBOS_CANTREADPWD;
            goto cleanup;
        }
        if (prompts[i].hidden)
            putchar('\n');
        /* fgets always null-terminates the returned string */

        /* replace newline with null */
        if ((ptr = strchr(prompts[i].reply->data, '\n')))
            *ptr = '\0';
        else /* flush rest of input line */
            do {
                scratchchar = getchar();
            } while (scratchchar != EOF && scratchchar != '\n');

        prompts[i].reply->length = strlen(prompts[i].reply->data);

        if (!SetConsoleMode(handle, old_mode)) {
            errcode = ENOTTY;
            goto cleanup;
        }
    }

cleanup:
    if (errcode) {
        for (i = 0; i < num_prompts; i++) {
            memset(prompts[i].reply->data, 0, prompts[i].reply->length);
        }
    }
    return errcode;
}

#else /* !_WIN32 */

krb5_error_code KRB5_CALLCONV
krb5_prompter_posix(krb5_context context,
                    void *data,
                    const char *name,
                    const char *banner,
                    int num_prompts,
                    krb5_prompt prompts[])
{
    return(EINVAL);
}
#endif /* !_WIN32 */
#endif /* Windows or Mac */

void
k5_set_prompt_types(krb5_context context, krb5_prompt_type *types)
{
    context->prompt_types = types;
}

krb5_prompt_type*
KRB5_CALLCONV
krb5_get_prompt_types(krb5_context context)
{
    return context->prompt_types;
}