File: icmplog.c

package info (click to toggle)
jail 1:1.5-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 156 kB
  • ctags: 66
  • sloc: ansic: 481; makefile: 74; sh: 25
file content (193 lines) | stat: -rw-r--r-- 5,310 bytes parent folder | download | duplicates (2)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * $Id: icmplog.c,v 1.6 1998/08/03 14:09:36 ams Exp $
 * Copyright 1998 ams@wiw.org (Abhijit Menon-Sen)
 */

#include "icmplog.h"

#define ICMP_FILTER 1

/* return packet description */
char *icmps(int type)
{
    static char msg[15];

    if (types[type] == (char *)NULL) {
        snprintf(msg, 15, "ICMP type %d", type);
        msg[14] = '\0';
        return (msg);
    }
    else {
        return (types[type]);
    }
}

/* parse configuration file */
void configure(char *file)
{
    char buf[MAXLINE], *keyword, *name;
    FILE *rc;
    int type, level, i, j = 0;
    struct ex *curr = &root, *next = (struct ex *)NULL;

    /* use default configuration if none is specified */
    if (file == (char *)NULL) { file = cfgfile; }

    if ((rc = fopen(file, "r")) == (FILE *)NULL) {
        perror(file);
        return;
    }

    while (fgets(buf, MAXLINE-1, rc)) {
        /* ignore comments and blank lines */
        if (buf[0] == '#' || buf[0] == '\n') { continue; }

        keyword = strtok(buf, " \t");

        /* default log level */
        if (strcasecmp(keyword, "level") == 0) {
            lvl = loglevel(strtok((char *)NULL, " \t\n"));
            continue;
        }

        /* logging facility */
        if (strcasecmp(keyword, "facility") == 0) {
            fac = facility(strtok((char *)NULL, " \t\n"));
            continue;
        }

        /* list of hosts to ignore */
        if (strcasecmp(keyword, "except") == 0) {
            while ((name = strtok((char *)NULL, " \t\n,")) != (char *)NULL) {
                /* add to list */
                curr->host = (char *)malloc(strlen(name));
                if (curr->host == (char *)NULL) {
                    fprintf(stderr, "malloc() failed.\n");
                    exit(-1);
                }
                strcpy(curr->host, name);
                next = (struct ex *)malloc(sizeof(struct ex));
                if (next == (struct ex *)NULL) {
                    fprintf(stderr, "malloc() failed.\n");
                    exit(-1);
                }
                curr->link = next;
                curr = next;
                curr->link = (struct ex *)NULL;
            }
            continue;
        }

        /* numeric ICMP type */
        if (isdigit(keyword[0])) {
            type = strtol(keyword, (char **)NULL, 0);

            if (type < 0 || type > 255) {
                fprintf(stderr, "%d: invalid ICMP type.\n", type);
                continue;
            }

            level = loglevel(strtok((char *)NULL, " \t\n"));
            levels[type] = level;
        }
        /* type name */
        else {
            /* this is still just waiting for salvation */
            for (i = 0; i < 11; ++i) {
                if (strcasecmp(keyword, names[i].name) == 0) {
                    level = loglevel(strtok((char *)NULL, " \t\n"));
                    levels[names[i].type] = level;
                    j = 1;
                    break;
                }
            }
            if (j == 0) {
                fprintf(stderr, "%s: unknown packet type.\n", keyword);
            }
        }
    }
}

int main(int argc, char *argv[])
{
    char *rc = (char *)NULL, *host;
    int s = 0, c = 0, index = 0, type;
    icmppkt *pkt;
    unsigned int filter = 0;

    /* initialise logging level table */
    for (index = 0; index < 256; ++index) { levels[index] = lvl; }

    /* process command line parameters */
    while (1) {
        c = getopt_long(argc, argv, "f:nlv", par, &index);
        if (c == -1) { break; }
        switch (c) {
        case 0: switch (index) {
                case 0: rc = optarg;
                        break;
                case 1: resolve_names = 0;
                        break;
                case 2: printf("%s\n", version);
                        exit(0);
                        break;
                }
                break;
        case 'f': rc = optarg;
                  break;
        case 'n': resolve_names = 0;
                  break;
        case 'v': printf("%s\n", version);
                  exit(0);
                  break;
        default : printf ("options: --file, --no-resolve, --version\n");
                  exit(0);
        }
    }

    /* need root to bind raw socket */
    if (geteuid() != 0) {
        fprintf(stderr, "%s: root privileges required.\n", argv[0]);
        exit(-1);
    }

    /* parse configuration */
    configure(rc);

    background();

    if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) {
        perror("socket");
        exit(1);
    }

    /* 2.1.x kernels mask some ICMP types by default */
    setsockopt(s, SOL_RAW, ICMP_FILTER, &filter, sizeof (filter));

    if ((pkt = (icmppkt *)malloc(MTU)) == (icmppkt *)NULL) {
        fprintf(stderr, "malloc() failed\n");
        exit(-1);
    }

    openlog("icmplog", 0, fac);
    syslog(LOG_INFO, "started");

    while (1) {
        /* process packets */
        read(s, pkt, MTU);

        host = gethost(pkt->ip.saddr);
        type = pkt->icmp.type;

        if (excepted(pkt->ip.saddr, host)) { continue; }

        if (pkt->ip.ihl != 5) {
            /* pkt->ip.ihl < 5 should NEVER happen */
            continue;
        }

        if (levels[type]) {
            syslog(levels[type], "%s from %s", icmps(type), host);
        }
    }
}