-
Notifications
You must be signed in to change notification settings - Fork 0
/
arpsnmp.c
214 lines (185 loc) · 4.61 KB
/
arpsnmp.c
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* Copyright (c) 1996, 1997, 1999, 2004
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that: (1) source code distributions
* retain the above copyright notice and this paragraph in its entirety, (2)
* distributions including binary code include the above copyright notice and
* this paragraph in its entirety in the documentation or other materials
* provided with the distribution, and (3) all advertising materials mentioning
* features or use of this software display the following acknowledgement:
* ``This product includes software developed by the University of California,
* Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
* the University nor the names of its contributors may be used to endorse
* or promote products derived from this software without specific prior
* written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#ifndef lint
static const char copyright[] =
"Copyright (c) 1996, 1997, 1999, 2004\n\
The Regents of the University of California. All rights reserved.\n";
static const char rcsid[] =
"@(#) $Header: /usr/src/local/sbin/arpwatch/RCS/arpsnmp.c,v 1.9 2004/01/22 22:25:11 leres Exp $ (LBL)";
#endif
/*
* arpsnmp - keep track of ethernet/ip address pairings, report changes
*/
#include <sys/param.h>
#include <sys/types.h> /* concession to AIX */
#include <sys/file.h>
#include <ctype.h>
#ifdef HAVE_MEMORY_H
#include <memory.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
#include "gnuc.h"
#ifdef HAVE_OS_PROTO_H
#include "os-proto.h"
#endif
#include "arpwatch.h"
#include "db.h"
#include "ec.h"
#include "file.h"
#include "machdep.h"
#include "util.h"
/* Forwards */
int main(int, char **);
int readsnmp(char *);
int snmp_add(u_int32_t, u_char *, time_t, char *, char *);
__dead void usage(void) __attribute__((volatile));
char *prog;
char *path_sendmail = PATH_SENDMAIL;
extern int optind;
extern int opterr;
extern char *optarg;
int
main(int argc, char **argv)
{
register char *cp;
register int op, i;
char errbuf[256];
char options[] =
"d"
"m:"
"f:"
"s:"
;
if ((cp = strrchr(argv[0], '/')) != NULL)
prog = cp + 1;
else
prog = argv[0];
if (abort_on_misalignment(errbuf) < 0) {
(void)fprintf(stderr, "%s: %s\n", prog, errbuf);
exit(1);
}
opterr = 0;
while ((op = getopt(argc, argv, options)) != EOF)
switch (op) {
case 'd':
++debug;
#ifndef DEBUG
(void)fprintf(stderr,
"%s: Warning: Not compiled with -DDEBUG\n", prog);
#endif
break;
case 'm':
mailaddress = optarg;
break;
case 'f':
arpfile = optarg;
break;
case 's':
path_sendmail = optarg;
break;
default:
usage();
}
if (optind == argc)
usage();
openlog(prog, 0, LOG_DAEMON);
/* Read in database */
initializing = 1;
/* XXX todo: file locking */
if (!readdata())
exit(1);
sorteinfo();
#ifdef DEBUG
if (debug > 2) {
debugdump();
exit(0);
}
#endif
initializing = 0;
/* Suck files in then exit */
for (i = optind; i < argc; ++i)
(void)readsnmp(argv[i]);
if (!dump())
exit(1);
exit(0);
}
static time_t now;
int
snmp_add(register u_int32_t a, register u_char *e, time_t t, register char *h,
char *interface)
{
/* Watch for ethernet broadcast */
if (MEMCMP(e, zero, 6) == 0 || MEMCMP(e, allones, 6) == 0) {
dosyslog(LOG_INFO, "ethernet broadcast", a, e, NULL,
interface);
return (1);
}
/* Watch for some ip broadcast addresses */
if (a == 0 || a == 1) {
dosyslog(LOG_INFO, "ip broadcast", a, e, NULL, interface);
return (1);
}
/* Use current time (although it would be nice to subtract idle time) */
return (ent_add(a, e, now, h, interface));
}
/* Process an snmp file */
int
readsnmp(register char *file)
{
register FILE *f;
if (debug > 2)
(void)fprintf(stderr, "%s: reading %s\n", prog, file);
if ((f = fopen(file, "r")) == NULL) {
syslog(LOG_ERR, "fopen(%s): %m", file);
return(0);
}
now = time(NULL);
if (!file_loop(f, snmp_add, file)) {
(void)fclose(f);
return(0);
}
(void)fclose(f);
return(1);
}
__dead void
usage(void)
{
extern char version[];
char usage[] =
"[-d] "
"[-m addr ] "
"[-f datafile] "
"[-s sendmail_path] "
"file [...]\n"
;
(void)fprintf(stderr, "Version %s\n", version);
(void)fprintf(stderr,
"usage: %s %s", prog, usage);
exit(1);
}