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
|
/*
* Created by yuuta on 12/23/22.
*/
#include "common.h"
#include "log.h"
#include <stdio.h>
#include <errno.h>
int main_consent(int secure) {
if (!secure) {
fprintf(stderr, "WARNING: The terminal you are about to enter passwords is insecure.\n");
} else {
fprintf(stderr, "\033[r\033[H\033[J");
}
fprintf(stderr, "Program %s from user %d (process %d: '%s'), is asking for authorization.\n"
"It prompts that:\n\n%s\n\n",
a_env.exe,
a_env.usr,
a_env.pid,
"todo",
a_env.prompt);
switch (a_env.mode) {
case mode_consent: {
fprintf(stderr, "Consent? (Y / N) ");
char buf[5];
if (read(STDIN_FILENO, buf, sizeof(buf) - 1) < 0) {
int r = errno;
LOGFV("read: %m", r);
if (secure) {
fprintf(stderr, "Cannot read response: %m. Press any key to return.\n",
errno);
fgetc(stdin);
fprintf(stderr, "\033[r\033[H\033[J");
}
return r;
}
if (buf[0] == 'Y' || buf[0] == 'y') {
dprintf(p_env.out, "1");
} else {
dprintf(p_env.out, "0");
}
break;
}
case mode_password: {
fprintf(stderr, "Please enter password. "
"The program will know your password.\n"
"Password: ");
char buf[10];
if (!fgets(buf, sizeof(buf) - 1, stdin)) {
int r = errno;
LOGFV("read: %m", r);
if (secure) {
fprintf(stderr, "Cannot read your response: %m. Press any key to return.\n",
errno);
fgetc(stdin);
fprintf(stderr, "\033[r\033[H\033[J");
}
return r;
}
dprintf(p_env.out, "%s", buf);
break;
}
case mode_auth: {
// TODO
fgetc(stdin);
break;
}
}
if (secure) {
printf("\033[r\033[H\033[J");
}
return 0;
}
|