summaryrefslogtreecommitdiffstats
path: root/util/bbsrf.c
blob: 107923a4bac8016eebe1ea7d99cb8ac55361d1b9 (plain) (blame)
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
76
77
78
79
80
81
82
83
84
85
86
/* $Id$ */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pwd.h>
#include <syslog.h>
#include "config.h"

#define MAX_REMOTE_IP_LEN 32

static void get_remote_ip(int len, char *remote_ip)
{
    char frombuf[100];
    // note, SSH_CLIENT is deprecated since 2002
    char *ssh_client = getenv("SSH_CONNECTION");

    if (ssh_client) {
    // SSH_CONNECTION format: "client-ip client-port server-ip server-port"
    sscanf(ssh_client, "%s", frombuf);
    } else {
    strcpy(frombuf, "127.0.0.1");
    }

    strlcpy(remote_ip, frombuf, len);
}

/*
   show ban file
   if filename exist, print it out, sleep 10 second, and return 0;
   otherwise, return -1.
 */
static int showbanfile(const char *filename)
{
    FILE *fp;
    char buf[256];

    fp = fopen(filename, "r");
    if (fp)
    {
    while (fgets(buf, sizeof(buf), fp))
        fputs(buf, stdout);
    printf("\n============================="
           "=============================\n");
    fclose(fp);
    sleep(10);
    }
    return fp ? 0 : -1;
}

int main(void)
{
    int uid;
    char *tty, remote_ip[MAX_REMOTE_IP_LEN + 1];

    openlog("bbsrf", LOG_PID | LOG_PERROR, LOG_USER);
    chdir(BBSHOME);
    uid = getuid();

    if (uid != BBSUID) {
    syslog(LOG_ERR, "UID DOES NOT MATCH");
    return -1;
    }
    if (!getpwuid(uid)) {
    syslog(LOG_ERR, "YOU DONT EXIST");
    return -1;
    }


    if (!showbanfile(BAN_FILE)) {
    return 1;
    }

    get_remote_ip(sizeof(remote_ip), remote_ip);

    tty = ttyname(0);
    if (tty == NULL)
    tty = "notty";

    execl(BBSPROG, "mbbsd", remote_ip, tty, NULL);
    syslog(LOG_ERR, "execl(): %m");
    sleep(3); // prevent flooding

    return -1;
}