aboutsummaryrefslogtreecommitdiff
path: root/consent.c
blob: cc94e4386f3442e052362d4360a1fcde6b07e760 (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
/*
 * Created by yuuta on 12/23/22.
 */

#include "common.h"
#include "log.h"

#include <stdio.h>
#include <errno.h>
#include <bsd/readpassphrase.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");
            char buf[10];
            if (!readpassphrase("Password: ", buf, sizeof(buf), 0)) {
                LOGF("Cannot read passphrase.");
                if (secure) {
                    fprintf(stderr, "Cannot read your response. Press any key to return.\n");
                    fgetc(stdin);
                    fprintf(stderr, "\033[r\033[H\033[J");
                }
                return 1;
            }
            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;
}