aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: a5f091b0c44351c849c6a0b3899dccc8ba4ca87c (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
#include "log.h"
#include "common.h"

#include <stdio.h>
#include <string.h>
#include <signal.h>

struct auth_env a_env;
struct proc_env p_env = {
        .in = STDIN_FILENO,
        .out = STDOUT_FILENO,
        .err = STDERR_FILENO
};

int main(int argc, char **argv) {
    if (argc != 2 && argc != 3) {
        fprintf(stderr, "Usage: %s consent|password|auth [prompt]\n",
                argv[0]);
        return 64;
    }
    if (!strcmp(argv[1], "consent")) {
        a_env.mode = mode_consent;
    } else if (!strcmp(argv[1], "password")) {
        a_env.mode = mode_password;
    } else if (!strcmp(argv[1], "auth")) {
        a_env.mode = mode_auth;
    } else {
        fprintf(stderr, "Unknown mode: %s\n", argv[1]);
        return 64;
    }

    if ((a_env.prompt = argv[2])) {
        char *p = argv[2];
        do {
            if ((*p) < 32 || (*p) > 126) {
                fprintf(stderr, "The given prompt is illegal.\n");
                return 64;
            }
        } while (*(++ p));
    }

    /* Because we need to frequently dup(2). */
    setbuf(stdout, NULL);
    a_env.pid = getppid();
    signal(SIGHUP, SIG_IGN);
    int r = sd_setup();
    if (r == -1) {
        sd_cleanup();
        return 13;
    }
    if (r) {
        sd_cleanup();
        return main_consent(0);
    } else {
        r = main_consent(1);
        sd_cleanup();
        return r;
    }
}