aboutsummaryrefslogtreecommitdiffstats
path: root/src/blogc-git-receiver/main.c
blob: 43a999311c52f8b4fcc2e2e9b6e91cf1b28d0e8e (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
/*
 * blogc: A blog compiler.
 * Copyright (C) 2014-2019 Rafael G. Martins <rafael@rafaelmartins.eng.br>
 *
 * This program can be distributed under the terms of the BSD License.
 * See the file LICENSE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include "shell.h"
#include "pre-receive.h"
#include "post-receive.h"


int
main(int argc, char *argv[])
{
    if (argc > 0) {
        if (0 == strcmp(basename(argv[0]), "pre-receive"))
            return bgr_pre_receive_hook(argc, argv);
        if (0 == strcmp(basename(argv[0]), "post-receive"))
            return bgr_post_receive_hook(argc, argv);
    }

    if (argc == 3 && (0 == strcmp(argv[1], "-c"))) {
        return bgr_shell(argc, argv);
    }

    // this is a hack to make blogc-git-receiver work out-of-the-box as a
    // `command=` in authorized_keys file. it will only work if the command
    // path is absolute.
    char *ssh_orig = getenv("SSH_ORIGINAL_COMMAND");
    if (argc == 1 && ssh_orig != NULL && argv[0][0] == '/') {
        setenv("SHELL", argv[0], 1);
        char* _argv[3] = {argv[0], "-c", ssh_orig};
        return bgr_shell(3, _argv);
    }

    fprintf(stderr, "error: this is a special shell, go away!\n");
    return 3;
}