summaryrefslogtreecommitdiffstats
path: root/src/lib/krb5/os/read_pwd.c
blob: f26896d5fa594c00a317d61816a77c84ce751885 (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
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* lib/krb5/os/read_pwd.c */
/*
 * Copyright 1990,1991 by the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 *
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 */

#include "k5-int.h"

#if !defined(_WIN32)
#define DEFINED_KRB5_READ_PASSWORD
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <setjmp.h>

#ifndef ECHO_PASSWORD
#include <termios.h>
#endif /* ECHO_PASSWORD */

krb5_error_code
krb5_read_password(krb5_context context,
                   const char *prompt, const char *prompt2,
                   char *return_pwd, unsigned int *size_return)
{
    krb5_data reply_data;
    krb5_prompt k5prompt;
    krb5_error_code retval;
    reply_data.length = *size_return; /* NB: size_return is also an input */
    reply_data.data = return_pwd;
    k5prompt.prompt = (char *)prompt;
    k5prompt.hidden = 1;
    k5prompt.reply = &reply_data;
    retval =  krb5_prompter_posix(NULL,
                                  NULL, NULL, NULL, 1, &k5prompt);

    if ((retval==0) && prompt2) {
        krb5_data verify_data;
        verify_data.data = malloc(*size_return);
        verify_data.length = *size_return;
        k5prompt.prompt = (char *)prompt2;
        k5prompt.reply = &verify_data;
        if (!verify_data.data)
            return ENOMEM;
        retval = krb5_prompter_posix(NULL,
                                     NULL,NULL, NULL, 1, &k5prompt);
        if (retval == 0) {
            /* compare */
            if (strncmp(return_pwd, (char *)verify_data.data, *size_return))
                retval = KRB5_LIBOS_BADPWDMATCH;
        }
        free(verify_data.data);
    }
    if (!retval)
        *size_return = k5prompt.reply->length;
    else
        memset(return_pwd, 0, *size_return);
    return retval;
}
#endif

#if defined(_WIN32)
#define DEFINED_KRB5_READ_PASSWORD

#include <io.h>

typedef struct {
    char *pwd_prompt;
    char *pwd_prompt2;
    char *pwd_return_pwd;
    int  *pwd_size_return;
} pwd_params;

void
center_dialog(HWND hwnd)
{
    int scrwidth, scrheight;
    int dlgwidth, dlgheight;
    RECT r;
    HDC hdc;

    if (hwnd == NULL)
        return;

    GetWindowRect(hwnd, &r);
    dlgwidth = r.right  - r.left;
    dlgheight = r.bottom - r.top ;
    hdc = GetDC(NULL);
    scrwidth = GetDeviceCaps(hdc, HORZRES);
    scrheight = GetDeviceCaps(hdc, VERTRES);
    ReleaseDC(NULL, hdc);
    r.left = (scrwidth - dlgwidth) / 2;
    r.top  = (scrheight - dlgheight) / 2;
    MoveWindow(hwnd, r.left, r.top, dlgwidth, dlgheight, TRUE);
}

#ifdef _WIN32
static krb5_error_code
read_console_password(krb5_context        context,
                      const char          * prompt,
                      const char          * prompt2,
                      char                * password,
                      int                 * pwsize)
{
    HANDLE              handle;
    DWORD               old_mode, new_mode;
    char                *tmpstr = 0;
    char                *ptr;
    int                 scratchchar;
    krb5_error_code     errcode = 0;

    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;

    (void) fputs(prompt, stdout);
    (void) fflush(stdout);
    (void) memset(password, 0, *pwsize);

    if (fgets(password, *pwsize, stdin) == NULL) {
        (void) putchar('\n');
        errcode = KRB5_LIBOS_CANTREADPWD;
        goto cleanup;
    }
    (void) putchar('\n');

    if ((ptr = strchr(password, '\n')))
        *ptr = '\0';
    else /* need to flush */
        do {
            scratchchar = getchar();
        } while (scratchchar != EOF && scratchchar != '\n');

    if (prompt2) {
        if (! (tmpstr = (char *)malloc(*pwsize))) {
            errcode = ENOMEM;
            goto cleanup;
        }
        (void) fputs(prompt2, stdout);
        (void) fflush(stdout);
        if (fgets(tmpstr, *pwsize, stdin) == NULL) {
            (void) putchar('\n');
            errcode = KRB5_LIBOS_CANTREADPWD;
            goto cleanup;
        }
        (void) putchar('\n');

        if ((ptr = strchr(tmpstr, '\n')))
            *ptr = '\0';
        else /* need to flush */
            do {
                scratchchar = getchar();
            } while (scratchchar != EOF && scratchchar != '\n');

        if (strncmp(password, tmpstr, *pwsize)) {
            errcode = KRB5_LIBOS_BADPWDMATCH;
            goto cleanup;
        }
    }

cleanup:
    (void) SetConsoleMode(handle, old_mode);
    if (tmpstr) {
        (void) memset(tmpstr, 0, *pwsize);
        (void) free(tmpstr);
    }
    if (errcode)
        (void) memset(password, 0, *pwsize);
    else
        *pwsize = strlen(password);
    return errcode;
}
#endif

static int CALLBACK
read_pwd_proc(HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
    pwd_params *dp;

    switch(msg) {
    case WM_INITDIALOG:
        dp = (pwd_params *) lParam;
        SetWindowLongPtr(hdlg, DWLP_USER, lParam);
        SetDlgItemText(hdlg, ID_READ_PWD_PROMPT, dp->pwd_prompt);
        SetDlgItemText(hdlg, ID_READ_PWD_PROMPT2, dp->pwd_prompt2);
        SetDlgItemText(hdlg, ID_READ_PWD_PWD, "");
        center_dialog(hdlg);
        return TRUE;

    case WM_COMMAND:
        dp = (pwd_params *) GetWindowLongPtr(hdlg, DWLP_USER);
        switch (wParam) {
        case IDOK:
            *(dp->pwd_size_return) =
                GetDlgItemText(hdlg, ID_READ_PWD_PWD,
                               dp->pwd_return_pwd, *(dp->pwd_size_return));
            EndDialog(hdlg, TRUE);
            break;

        case IDCANCEL:
            memset(dp->pwd_return_pwd, 0 , *(dp->pwd_size_return));
            *(dp->pwd_size_return) = 0;
            EndDialog(hdlg, FALSE);
            break;
        }
        return TRUE;

    default:
        return FALSE;
    }
}

krb5_error_code KRB5_CALLCONV
krb5_read_password(context, prompt, prompt2, return_pwd, size_return)
    krb5_context context;
    const char *prompt;
    const char *prompt2;
    char *return_pwd;
    int *size_return;
{
    DLGPROC dlgproc;
    HINSTANCE hinst;
    pwd_params dps;
    int rc;

#ifdef _WIN32
    if (_isatty(_fileno(stdin)))
        return(read_console_password
               (context, prompt, prompt2, return_pwd, size_return));
#endif

    dps.pwd_prompt = prompt;
    dps.pwd_prompt2 = prompt2;
    dps.pwd_return_pwd = return_pwd;
    dps.pwd_size_return = size_return;

    hinst = get_lib_instance();
#ifdef _WIN32
    dlgproc = read_pwd_proc;
#else
    dlgproc = (FARPROC) MakeProcInstance(read_pwd_proc, hinst);
#endif
    rc = DialogBoxParam(hinst, MAKEINTRESOURCE(ID_READ_PWD_DIALOG), 0,
                        dlgproc, (LPARAM) &dps);
#ifndef _WIN32
    FreeProcInstance ((FARPROC) dlgproc);
#endif
    return 0;
}
#endif

#ifndef DEFINED_KRB5_READ_PASSWORD
#define DEFINED_KRB5_READ_PASSWORD
/*
 * Don't expect to be called, just define it for sanity and the linker.
 */
krb5_error_code KRB5_CALLCONV
krb5_read_password(context, prompt, prompt2, return_pwd, size_return)
    krb5_context context;
    const char *prompt;
    const char *prompt2;
    char *return_pwd;
    int *size_return;
{
    *size_return = 0;
    return KRB5_LIBOS_CANTREADPWD;
}
#endif