-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword.c
More file actions
163 lines (140 loc) · 3.94 KB
/
password.c
File metadata and controls
163 lines (140 loc) · 3.94 KB
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
#ifndef _WIN32
#define _POSIX_C_SOURCE 200809L
#endif
#include "password.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
uint8_t *read_password(const char *prompt, size_t *out_len) {
/* Open the console directly so piped stdin doesn't interfere. */
HANDLE h = CreateFileA("CONIN$",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (h == INVALID_HANDLE_VALUE) {
fprintf(stderr, "error: cannot open console\n");
return NULL;
}
DWORD old_mode;
GetConsoleMode(h, &old_mode);
SetConsoleMode(h, old_mode & ~ENABLE_ECHO_INPUT);
fprintf(stderr, "%s", prompt);
fflush(stderr);
size_t cap = 256;
char *buf = malloc(cap);
if (!buf) {
SetConsoleMode(h, old_mode);
CloseHandle(h);
fprintf(stderr, "\nerror: out of memory\n");
return NULL;
}
size_t pos = 0;
char c;
DWORD n_read;
while (ReadFile(h, &c, 1, &n_read, NULL) && n_read == 1) {
if (c == '\r' || c == '\n') break;
if (pos + 1 >= cap) {
size_t new_cap = cap * 2;
char *new_buf = realloc(buf, new_cap);
if (!new_buf) {
memset(buf, 0, cap);
free(buf);
SetConsoleMode(h, old_mode);
CloseHandle(h);
fprintf(stderr, "\nerror: out of memory\n");
return NULL;
}
buf = new_buf;
cap = new_cap;
}
buf[pos++] = c;
}
fprintf(stderr, "\n");
SetConsoleMode(h, old_mode);
CloseHandle(h);
if (pos == 0) {
memset(buf, 0, cap);
free(buf);
fprintf(stderr, "error: password cannot be empty\n");
return NULL;
}
*out_len = pos;
return (uint8_t *)buf;
}
#else
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
uint8_t *read_password(const char *prompt, size_t *out_len) {
int fd;
int need_close = 0;
if (isatty(STDIN_FILENO)) {
fd = STDIN_FILENO;
} else {
fd = open("/dev/tty", O_RDWR);
if (fd < 0) {
fprintf(stderr, "error: cannot open /dev/tty\n");
return NULL;
}
need_close = 1;
}
struct termios old_tc, new_tc;
if (tcgetattr(fd, &old_tc) != 0) {
fprintf(stderr, "error: tcgetattr failed\n");
if (need_close) close(fd);
return NULL;
}
new_tc = old_tc;
new_tc.c_lflag &= ~(tcflag_t)ECHO;
if (tcsetattr(fd, TCSAFLUSH, &new_tc) != 0) {
fprintf(stderr, "error: cannot disable echo\n");
if (need_close) close(fd);
return NULL;
}
fprintf(stderr, "%s", prompt);
size_t cap = 256;
char *buf = malloc(cap);
if (!buf) {
tcsetattr(fd, TCSAFLUSH, &old_tc);
if (need_close) close(fd);
fprintf(stderr, "\nerror: out of memory\n");
return NULL;
}
size_t pos = 0;
char c;
while (1) {
ssize_t n = read(fd, &c, 1);
if (n <= 0 || c == '\n' || c == '\r')
break;
if (pos + 1 >= cap) {
size_t new_cap = cap * 2;
char *new_buf = realloc(buf, new_cap);
if (!new_buf) {
memset(buf, 0, cap);
free(buf);
tcsetattr(fd, TCSAFLUSH, &old_tc);
if (need_close) close(fd);
fprintf(stderr, "\nerror: out of memory\n");
return NULL;
}
buf = new_buf;
cap = new_cap;
}
buf[pos++] = c;
}
fprintf(stderr, "\n");
tcsetattr(fd, TCSAFLUSH, &old_tc);
if (need_close)
close(fd);
if (pos == 0) {
memset(buf, 0, cap);
free(buf);
fprintf(stderr, "error: password cannot be empty\n");
return NULL;
}
*out_len = pos;
return (uint8_t *)buf;
}
#endif