summaryrefslogtreecommitdiffstats
path: root/util/bbsrf.c
blob: c3bf44a9c0759740b0c052fc73371ca898726783 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* $Id$ */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/param.h>
#include <sys/types.h>
#include <pwd.h>
#include <syslog.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/uio.h>
#include "config.h"

#ifdef Solaris
    #include <utmpx.h>
    #define U_FILE UTMPX_FILE
#else
    #include <utmp.h>
    #define U_FILE UTMP_FILE
#endif

#ifdef FreeBSD
    #define UTMP_FILE _PATH_UTMP
#endif

#ifndef Solaris
    #if MAXHOSTNAMELEN < UT_HOSTSIZE
    #define MAX_HOMENAME_LEN MAXHOSTNAMELEN
    #else
    #define MAX_HOMENAME_LEN UT_HOSTSIZE
    #endif
#else
    /* according to /usr/include/utmpx.h ... */
    #define MAX_HOMENAME_LEN 256
#endif

/* fill the hid with from hostname */
void gethid(char *hid, char *tty)
{
    int fd;
    char *tp;
#ifdef Solaris
    struct utmpx data;
#else
    struct utmp data;
#endif

    gethostname(hid, MAX_HOMENAME_LEN);
    hid[MAX_HOMENAME_LEN] = '\0';
    tp = strrchr(tty, '/') + 1;
    if (tp && strlen(tp) == 5)
    {
    fd = open(U_FILE, O_RDONLY);
    if (fd < 0)
        syslog(LOG_ERR, "%s: %m", U_FILE);
    else
    {
        while (read(fd, &data, sizeof(data)) == sizeof(data))
        if (strcmp(data.ut_line, tp) == 0)
        {
            if (data.ut_host[0]) {
            strncpy(hid, data.ut_host, MAX_HOMENAME_LEN);
            hid[MAX_HOMENAME_LEN] = '\0';
            }
            break;
        }
        close(fd);
    }
    }
}

/*
   show ban file
   if filename exist, print it out, sleep 1 second, and return 0;
   otherwise, return -1.
 */
int showbanfile(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(1);
    }
    return fp ? 0 : -1;
}

int main(void)
{
    int uid, rtv = 0;
    char *tty, ttybuf[32], hid[MAX_HOMENAME_LEN + 1];

#ifndef Solaris
    openlog("bbsrf", LOG_PID | LOG_PERROR, LOG_USER);
#else
    openlog("bbsrf", LOG_PID, LOG_USER);
#endif
    chdir(BBSHOME);
    uid = getuid();

    while (1)
    {
    if (!showbanfile(BAN_FILE))
    {
        rtv = 1;
        break;
    }
    else if (uid != BBSUID)
    {
        syslog(LOG_ERR, "UID DOES NOT MATCH");
        rtv = -1;
        break;
    }
    else if (!getpwuid(uid))
    {
        syslog(LOG_ERR, "YOU DONT EXIST");
        rtv = -1;
        break;
    }
    else
    {
        tty = ttyname(0);
        if (tty)
        {
        strcpy(ttybuf, tty);
        gethid(hid, ttybuf);
        }
        else
        {
        strcpy(ttybuf, "notty");
        strcpy(hid, "unknown");
        }
        execl(BBSPROG, "mbbsd", hid, ttybuf, NULL);
        syslog(LOG_ERR, "execl(): %m");
        rtv = -1;
    }
    break;
    }
    return rtv;
}