/src/FreeRDP/libfreerdp/utils/passphrase.c
Line | Count | Source |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * Passphrase Handling Utils |
4 | | * |
5 | | * Copyright 2011 Shea Levy <shea@shealevy.com> |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | #include <winpr/environment.h> |
21 | | |
22 | | #include <freerdp/config.h> |
23 | | #include <freerdp/freerdp.h> |
24 | | |
25 | | #include <errno.h> |
26 | | #include <freerdp/utils/passphrase.h> |
27 | | |
28 | | #ifdef _WIN32 |
29 | | |
30 | | #include <stdio.h> |
31 | | #include <io.h> |
32 | | #include <conio.h> |
33 | | #include <wincred.h> |
34 | | |
35 | | static char read_chr(FILE* f) |
36 | | { |
37 | | char chr; |
38 | | const BOOL isTty = _isatty(_fileno(f)); |
39 | | if (isTty) |
40 | | return fgetc(f); |
41 | | if (fscanf_s(f, "%c", &chr, (UINT32)sizeof(char)) && !feof(f)) |
42 | | return chr; |
43 | | return 0; |
44 | | } |
45 | | |
46 | | int freerdp_interruptible_getc(rdpContext* context, FILE* f) |
47 | | { |
48 | | return read_chr(f); |
49 | | } |
50 | | |
51 | | const char* freerdp_passphrase_read(rdpContext* context, const char* prompt, char* buf, |
52 | | size_t bufsiz, int from_stdin) |
53 | | { |
54 | | WCHAR UserNameW[CREDUI_MAX_USERNAME_LENGTH + 1] = { 'p', 'r', 'e', 'f', 'i', |
55 | | 'l', 'l', 'e', 'd', '\0' }; |
56 | | WCHAR PasswordW[CREDUI_MAX_PASSWORD_LENGTH + 1] = { 0 }; |
57 | | BOOL fSave = FALSE; |
58 | | DWORD dwFlags = 0; |
59 | | WCHAR* promptW = ConvertUtf8ToWCharAlloc(prompt, NULL); |
60 | | const DWORD status = |
61 | | CredUICmdLinePromptForCredentialsW(promptW, NULL, 0, UserNameW, ARRAYSIZE(UserNameW), |
62 | | PasswordW, ARRAYSIZE(PasswordW), &fSave, dwFlags); |
63 | | free(promptW); |
64 | | if (ConvertWCharNToUtf8(PasswordW, ARRAYSIZE(PasswordW), buf, bufsiz) < 0) |
65 | | return NULL; |
66 | | return buf; |
67 | | } |
68 | | |
69 | | #elif !defined(ANDROID) |
70 | | |
71 | | #include <fcntl.h> |
72 | | #include <stdio.h> |
73 | | #include <string.h> |
74 | | #include <sys/stat.h> |
75 | | #include <sys/wait.h> |
76 | | #include <termios.h> |
77 | | #include <unistd.h> |
78 | | #include <freerdp/utils/signal.h> |
79 | | #include <freerdp/log.h> |
80 | | #if defined(WINPR_HAVE_POLL_H) && !defined(__APPLE__) |
81 | | #include <poll.h> |
82 | | #else |
83 | | #include <time.h> |
84 | | #include <sys/select.h> |
85 | | #endif |
86 | | |
87 | | #define TAG FREERDP_TAG("utils.passphrase") |
88 | | |
89 | | static int wait_for_fd(int fd, int timeout) |
90 | 0 | { |
91 | 0 | int status = 0; |
92 | 0 | #if defined(WINPR_HAVE_POLL_H) && !defined(__APPLE__) |
93 | 0 | struct pollfd pollset = { 0 }; |
94 | 0 | pollset.fd = fd; |
95 | 0 | pollset.events = POLLIN; |
96 | 0 | pollset.revents = 0; |
97 | |
|
98 | 0 | do |
99 | 0 | { |
100 | 0 | status = poll(&pollset, 1, timeout); |
101 | 0 | } while ((status < 0) && (errno == EINTR)); |
102 | |
|
103 | | #else |
104 | | fd_set rset = { 0 }; |
105 | | struct timeval tv = { 0 }; |
106 | | FD_ZERO(&rset); |
107 | | FD_SET(fd, &rset); |
108 | | |
109 | | if (timeout) |
110 | | { |
111 | | tv.tv_sec = timeout / 1000; |
112 | | tv.tv_usec = (timeout % 1000) * 1000; |
113 | | } |
114 | | |
115 | | do |
116 | | { |
117 | | status = select(fd + 1, &rset, NULL, NULL, timeout ? &tv : NULL); |
118 | | } while ((status < 0) && (errno == EINTR)); |
119 | | |
120 | | #endif |
121 | 0 | return status; |
122 | 0 | } |
123 | | |
124 | | static void replace_char(char* buffer, WINPR_ATTR_UNUSED size_t buffer_len, const char* toreplace) |
125 | 0 | { |
126 | 0 | while (*toreplace != '\0') |
127 | 0 | { |
128 | 0 | char* ptr = NULL; |
129 | 0 | while ((ptr = strrchr(buffer, *toreplace)) != NULL) |
130 | 0 | *ptr = '\0'; |
131 | 0 | toreplace++; |
132 | 0 | } |
133 | 0 | } |
134 | | |
135 | | static const char* freerdp_passphrase_read_tty(rdpContext* context, const char* prompt, char* buf, |
136 | | size_t bufsiz, int from_stdin) |
137 | 0 | { |
138 | 0 | BOOL terminal_needs_reset = FALSE; |
139 | 0 | char term_name[L_ctermid] = { 0 }; |
140 | |
|
141 | 0 | FILE* fout = NULL; |
142 | |
|
143 | 0 | if (bufsiz == 0) |
144 | 0 | { |
145 | 0 | errno = EINVAL; |
146 | 0 | return NULL; |
147 | 0 | } |
148 | | |
149 | 0 | ctermid(term_name); |
150 | 0 | int terminal_fildes = 0; |
151 | 0 | if (from_stdin || (strcmp(term_name, "") == 0)) |
152 | 0 | { |
153 | 0 | fout = stdout; |
154 | 0 | terminal_fildes = STDIN_FILENO; |
155 | 0 | } |
156 | 0 | else |
157 | 0 | { |
158 | 0 | const int term_file = open(term_name, O_RDWR); |
159 | 0 | if (term_file < 0) |
160 | 0 | { |
161 | 0 | fout = stdout; |
162 | 0 | terminal_fildes = STDIN_FILENO; |
163 | 0 | } |
164 | 0 | else |
165 | 0 | { |
166 | 0 | fout = fdopen(term_file, "w"); |
167 | 0 | if (!fout) |
168 | 0 | { |
169 | 0 | close(term_file); |
170 | 0 | return NULL; |
171 | 0 | } |
172 | 0 | terminal_fildes = term_file; |
173 | 0 | } |
174 | 0 | } |
175 | | |
176 | 0 | struct termios orig_flags = { 0 }; |
177 | 0 | if (tcgetattr(terminal_fildes, &orig_flags) != -1) |
178 | 0 | { |
179 | 0 | struct termios new_flags = { 0 }; |
180 | 0 | new_flags = orig_flags; |
181 | 0 | new_flags.c_lflag &= (uint32_t)~ECHO; |
182 | 0 | new_flags.c_lflag |= ECHONL; |
183 | 0 | terminal_needs_reset = TRUE; |
184 | 0 | if (tcsetattr(terminal_fildes, TCSAFLUSH, &new_flags) == -1) |
185 | 0 | terminal_needs_reset = FALSE; |
186 | 0 | } |
187 | |
|
188 | 0 | FILE* fp = fdopen(terminal_fildes, "r"); |
189 | 0 | if (!fp) |
190 | 0 | goto error; |
191 | | |
192 | 0 | (void)fprintf(fout, "%s", prompt); |
193 | 0 | (void)fflush(fout); |
194 | |
|
195 | 0 | { |
196 | 0 | char* ptr = NULL; |
197 | 0 | size_t ptr_len = 0; |
198 | 0 | const SSIZE_T res = freerdp_interruptible_get_line(context, &ptr, &ptr_len, fp); |
199 | 0 | if (res < 0) |
200 | 0 | goto error; |
201 | | |
202 | 0 | replace_char(ptr, ptr_len, "\r\n"); |
203 | |
|
204 | 0 | strncpy(buf, ptr, MIN(bufsiz, ptr_len)); |
205 | 0 | free(ptr); |
206 | 0 | } |
207 | | |
208 | 0 | if (terminal_needs_reset) |
209 | 0 | { |
210 | 0 | if (tcsetattr(terminal_fildes, TCSAFLUSH, &orig_flags) == -1) |
211 | 0 | goto error; |
212 | 0 | } |
213 | | |
214 | 0 | if (terminal_fildes != STDIN_FILENO) |
215 | 0 | (void)fclose(fp); |
216 | |
|
217 | 0 | return buf; |
218 | | |
219 | 0 | error: |
220 | 0 | { |
221 | | // NOLINTNEXTLINE(clang-analyzer-unix.Stream) |
222 | 0 | int saved_errno = errno; |
223 | 0 | if (terminal_needs_reset) |
224 | 0 | (void)tcsetattr(terminal_fildes, TCSAFLUSH, &orig_flags); |
225 | |
|
226 | 0 | if (terminal_fildes != STDIN_FILENO) |
227 | 0 | { |
228 | 0 | if (fp) |
229 | 0 | (void)fclose(fp); |
230 | 0 | } |
231 | | // NOLINTNEXTLINE(clang-analyzer-unix.Stream) |
232 | 0 | errno = saved_errno; |
233 | 0 | } |
234 | |
|
235 | 0 | return NULL; |
236 | 0 | } |
237 | | |
238 | | static const char* freerdp_passphrase_read_askpass(const char* prompt, char* buf, size_t bufsiz, |
239 | | char const* askpass_env) |
240 | 0 | { |
241 | 0 | char command[4096] = { 0 }; |
242 | |
|
243 | 0 | (void)sprintf_s(command, sizeof(command), "%s 'FreeRDP authentication\n%s'", askpass_env, |
244 | 0 | prompt); |
245 | | // NOLINTNEXTLINE(clang-analyzer-optin.taint.GenericTaint) |
246 | 0 | FILE* askproc = popen(command, "r"); |
247 | 0 | if (!askproc) |
248 | 0 | return NULL; |
249 | 0 | WINPR_ASSERT(bufsiz <= INT32_MAX); |
250 | 0 | if (fgets(buf, (int)bufsiz, askproc) != NULL) |
251 | 0 | buf[strcspn(buf, "\r\n")] = '\0'; |
252 | 0 | else |
253 | 0 | buf = NULL; |
254 | 0 | const int status = pclose(askproc); |
255 | 0 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) |
256 | 0 | buf = NULL; |
257 | |
|
258 | 0 | return buf; |
259 | 0 | } |
260 | | |
261 | | const char* freerdp_passphrase_read(rdpContext* context, const char* prompt, char* buf, |
262 | | size_t bufsiz, int from_stdin) |
263 | 0 | { |
264 | | // NOLINTNEXTLINE(concurrency-mt-unsafe) |
265 | 0 | const char* askpass_env = getenv("FREERDP_ASKPASS"); |
266 | |
|
267 | 0 | if (askpass_env) |
268 | 0 | return freerdp_passphrase_read_askpass(prompt, buf, bufsiz, askpass_env); |
269 | 0 | else |
270 | 0 | return freerdp_passphrase_read_tty(context, prompt, buf, bufsiz, from_stdin); |
271 | 0 | } |
272 | | |
273 | | static BOOL set_termianl_nonblock(int ifd, BOOL nonblock); |
274 | | |
275 | | static void restore_terminal(void) |
276 | 0 | { |
277 | 0 | (void)set_termianl_nonblock(-1, FALSE); |
278 | 0 | } |
279 | | |
280 | | BOOL set_termianl_nonblock(int ifd, BOOL nonblock) |
281 | 0 | { |
282 | 0 | static int fd = -1; |
283 | 0 | static bool registered = false; |
284 | 0 | static int orig = 0; |
285 | 0 | static struct termios termios = { 0 }; |
286 | |
|
287 | 0 | if (ifd >= 0) |
288 | 0 | fd = ifd; |
289 | |
|
290 | 0 | if (fd < 0) |
291 | 0 | return FALSE; |
292 | | |
293 | 0 | if (nonblock) |
294 | 0 | { |
295 | 0 | if (!registered) |
296 | 0 | { |
297 | 0 | (void)atexit(restore_terminal); |
298 | 0 | registered = true; |
299 | 0 | } |
300 | |
|
301 | 0 | const int rc1 = fcntl(fd, F_SETFL, orig | O_NONBLOCK); |
302 | 0 | if (rc1 != 0) |
303 | 0 | { |
304 | 0 | char buffer[128] = { 0 }; |
305 | 0 | WLog_ERR(TAG, "fcntl(F_SETFL) failed with %s", |
306 | 0 | winpr_strerror(errno, buffer, sizeof(buffer))); |
307 | 0 | return FALSE; |
308 | 0 | } |
309 | 0 | const int rc2 = tcgetattr(fd, &termios); |
310 | 0 | if (rc2 != 0) |
311 | 0 | { |
312 | 0 | char buffer[128] = { 0 }; |
313 | 0 | WLog_ERR(TAG, "tcgetattr() failed with %s", |
314 | 0 | winpr_strerror(errno, buffer, sizeof(buffer))); |
315 | 0 | return FALSE; |
316 | 0 | } |
317 | | |
318 | 0 | struct termios now = termios; |
319 | 0 | cfmakeraw(&now); |
320 | 0 | const int rc3 = tcsetattr(fd, TCSANOW, &now); |
321 | 0 | if (rc3 != 0) |
322 | 0 | { |
323 | 0 | char buffer[128] = { 0 }; |
324 | 0 | WLog_ERR(TAG, "tcsetattr(TCSANOW) failed with %s", |
325 | 0 | winpr_strerror(errno, buffer, sizeof(buffer))); |
326 | 0 | return FALSE; |
327 | 0 | } |
328 | 0 | } |
329 | 0 | else |
330 | 0 | { |
331 | 0 | const int rc1 = tcsetattr(fd, TCSANOW, &termios); |
332 | 0 | if (rc1 != 0) |
333 | 0 | { |
334 | 0 | char buffer[128] = { 0 }; |
335 | 0 | WLog_ERR(TAG, "tcsetattr(TCSANOW) failed with %s", |
336 | 0 | winpr_strerror(errno, buffer, sizeof(buffer))); |
337 | 0 | return FALSE; |
338 | 0 | } |
339 | 0 | const int rc2 = fcntl(fd, F_SETFL, orig); |
340 | 0 | if (rc2 != 0) |
341 | 0 | { |
342 | 0 | char buffer[128] = { 0 }; |
343 | 0 | WLog_ERR(TAG, "fcntl(F_SETFL) failed with %s", |
344 | 0 | winpr_strerror(errno, buffer, sizeof(buffer))); |
345 | 0 | return FALSE; |
346 | 0 | } |
347 | 0 | fd = -1; |
348 | 0 | } |
349 | 0 | return TRUE; |
350 | 0 | } |
351 | | |
352 | | int freerdp_interruptible_getc(rdpContext* context, FILE* stream) |
353 | 0 | { |
354 | 0 | int rc = EOF; |
355 | 0 | const int fd = fileno(stream); |
356 | |
|
357 | 0 | (void)set_termianl_nonblock(fd, TRUE); |
358 | |
|
359 | 0 | do |
360 | 0 | { |
361 | 0 | const int res = wait_for_fd(fd, 10); |
362 | 0 | if (res != 0) |
363 | 0 | { |
364 | 0 | char c = 0; |
365 | 0 | const ssize_t rd = read(fd, &c, 1); |
366 | 0 | if (rd == 1) |
367 | 0 | { |
368 | 0 | if (c == 3) /* ctrl + c */ |
369 | 0 | return EOF; |
370 | 0 | if (c == 4) /* ctrl + d */ |
371 | 0 | return EOF; |
372 | 0 | if (c == 26) /* ctrl + z */ |
373 | 0 | return EOF; |
374 | 0 | rc = (int)c; |
375 | 0 | } |
376 | 0 | break; |
377 | 0 | } |
378 | 0 | } while (!freerdp_shall_disconnect_context(context)); |
379 | | |
380 | 0 | (void)set_termianl_nonblock(fd, FALSE); |
381 | |
|
382 | 0 | return rc; |
383 | 0 | } |
384 | | |
385 | | #else |
386 | | |
387 | | const char* freerdp_passphrase_read(rdpContext* context, const char* prompt, char* buf, |
388 | | size_t bufsiz, int from_stdin) |
389 | | { |
390 | | return NULL; |
391 | | } |
392 | | |
393 | | int freerdp_interruptible_getc(rdpContext* context, FILE* f) |
394 | | { |
395 | | return EOF; |
396 | | } |
397 | | #endif |
398 | | |
399 | | SSIZE_T freerdp_interruptible_get_line(rdpContext* context, char** plineptr, size_t* psize, |
400 | | FILE* stream) |
401 | 0 | { |
402 | 0 | int c = 0; |
403 | 0 | char* n = NULL; |
404 | 0 | size_t step = 32; |
405 | 0 | size_t used = 0; |
406 | 0 | char* ptr = NULL; |
407 | 0 | size_t len = 0; |
408 | |
|
409 | 0 | if (!plineptr || !psize) |
410 | 0 | { |
411 | 0 | errno = EINVAL; |
412 | 0 | return -1; |
413 | 0 | } |
414 | | |
415 | 0 | bool echo = true; |
416 | 0 | #if !defined(_WIN32) && !defined(ANDROID) |
417 | 0 | { |
418 | 0 | const int fd = fileno(stream); |
419 | |
|
420 | 0 | struct termios termios = { 0 }; |
421 | | /* This might fail if /from-stdin is used. */ |
422 | 0 | if (tcgetattr(fd, &termios) == 0) |
423 | 0 | echo = (termios.c_lflag & ECHO) != 0; |
424 | 0 | else |
425 | 0 | echo = false; |
426 | 0 | } |
427 | 0 | #endif |
428 | |
|
429 | 0 | if (*plineptr && (*psize > 0)) |
430 | 0 | { |
431 | 0 | ptr = *plineptr; |
432 | 0 | used = *psize; |
433 | 0 | if (echo) |
434 | 0 | { |
435 | 0 | printf("%s", ptr); |
436 | 0 | (void)fflush(stdout); |
437 | 0 | } |
438 | 0 | } |
439 | |
|
440 | 0 | do |
441 | 0 | { |
442 | 0 | if (used + 2 >= len) |
443 | 0 | { |
444 | 0 | len += step; |
445 | 0 | n = realloc(ptr, len); |
446 | |
|
447 | 0 | if (!n) |
448 | 0 | { |
449 | 0 | free(ptr); |
450 | 0 | *plineptr = NULL; |
451 | 0 | return -1; |
452 | 0 | } |
453 | | |
454 | 0 | ptr = n; |
455 | 0 | } |
456 | | |
457 | 0 | c = freerdp_interruptible_getc(context, stream); |
458 | 0 | if (c == 127) |
459 | 0 | { |
460 | 0 | if (used > 0) |
461 | 0 | { |
462 | 0 | ptr[used--] = '\0'; |
463 | 0 | if (echo) |
464 | 0 | { |
465 | 0 | printf("\b"); |
466 | 0 | printf(" "); |
467 | 0 | printf("\b"); |
468 | 0 | (void)fflush(stdout); |
469 | 0 | } |
470 | 0 | } |
471 | 0 | continue; |
472 | 0 | } |
473 | 0 | if (echo) |
474 | 0 | { |
475 | 0 | printf("%c", c); |
476 | 0 | (void)fflush(stdout); |
477 | 0 | } |
478 | 0 | if (c != EOF) |
479 | 0 | ptr[used++] = (char)c; |
480 | 0 | } while ((c != '\n') && (c != '\r') && (c != EOF)); |
481 | | |
482 | 0 | printf("\n"); |
483 | 0 | ptr[used] = '\0'; |
484 | 0 | if (c == EOF) |
485 | 0 | { |
486 | 0 | free(ptr); |
487 | 0 | *plineptr = NULL; |
488 | 0 | return EOF; |
489 | 0 | } |
490 | 0 | *plineptr = ptr; |
491 | 0 | *psize = used; |
492 | 0 | return WINPR_ASSERTING_INT_CAST(SSIZE_T, used); |
493 | 0 | } |