maddy

Fork https://github.com/foxcpp/maddy

git clone git://git.lin.moe/go/maddy.git

 1#define _POSIX_C_SOURCE 200809L
 2#include <stdio.h>
 3#include <stdlib.h>
 4#include <security/pam_appl.h>
 5#include "pam.h"
 6
 7/*
 8I really doubt it is a good idea to bring Go to the binary whose primary task
 9is to call libpam using CGo anyway.
10*/
11
12int run(void) {
13    char *username = NULL, *password = NULL;
14    size_t username_buf_len = 0, password_buf_len = 0;
15
16    ssize_t username_len = getline(&username, &username_buf_len, stdin);
17    if (username_len < 0) {
18        perror("getline username");
19        return 2;
20    }
21
22    ssize_t password_len = getline(&password, &password_buf_len, stdin);
23    if (password_len < 0) {
24        perror("getline password");
25        return 2;
26    }
27
28    // Cut trailing \n.
29    if (username_len > 0) {
30        username[username_len - 1] = 0;
31    }
32    if (password_len > 0) {
33        password[password_len - 1] = 0;
34    }
35
36    struct error_obj err = run_pam_auth(username, password);
37    if (err.status != 0) {
38        if (err.status == 2) {
39            fprintf(stderr, "%s: %s\n", err.func_name, err.error_msg);
40        }
41        return err.status;
42    }
43
44    return 0;
45}
46
47#ifndef CGO
48int main() {
49    return run();
50}
51#endif