-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
201 lines (176 loc) · 4.73 KB
/
util.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
/*
* Copyright (c) 1996, 1997, 1999, 2000, 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 rcsid[] =
"@(#) $Id: util.c,v 1.10 2004/01/22 22:25:27 leres Exp $ (LBL)";
#endif
/*
* util - arpwatch utility routines
*/
#include <sys/types.h>
#include <sys/file.h>
#include <fcntl.h>
#ifdef HAVE_MEMORY_H
#include <memory.h>
#endif
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.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 "util.h"
#include "addresses.h"
char *arpdir = ARPDIR;
char *arpfile = ARPFILE;
char *ethercodes = ETHERCODES;
/* Broadcast ethernet addresses */
u_char zero[6] = { 0, 0, 0, 0, 0, 0 };
u_char allones[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
int debug = 0;
int initializing = 1; /* true if initializing */
/**/
/**/
int nopromisc = 0; /* don't activate promisc mode */
/**/
/**/
int allsubnets = 0; /* watch all attached subnets */
/**/
/**/
char *mailaddress = WATCHER;
/**/
/**/
int quiet = 0; /* send mail by default */
/**/
/**/
/* syslog() helper routine */
void
dosyslog(register int p, register char *s, register u_int32_t a,
register u_char *ea, register u_char *ha, char *interface)
{
char xbuf[64];
/* No report until we're initialized */
if (initializing)
return;
/* Display both ethernet addresses if they don't match */
(void)strcpy(xbuf, e2str(ea));
if (ha != NULL && MEMCMP(ea, ha, 6) != 0) {
(void)strcat(xbuf, " (");
(void)strcat(xbuf, e2str(ha));
(void)strcat(xbuf, ")");
}
if (debug)
fprintf(stderr, "%s: %s %s %s %s\n", prog, s, intoa(a),
xbuf, interface);
else
syslog(p, "%s %s %s %s", s, intoa(a), xbuf, interface);
}
static FILE *dumpf;
void
dumpone(register u_int32_t a, register u_char *e, register time_t t,
register char *h, char *interface)
{
(void)fprintf(dumpf, "%s\t%s\t%u\t%s\t%s\n", e2str(e), intoa(a),
(u_int32_t)t, ((h != NULL)?h:""),
((interface != NULL)?interface:""));
}
int
dump(void)
{
register int fd;
char oldarpfile[256], newarpfile[256];
(void)sprintf(oldarpfile, "%s-", arpfile);
(void)sprintf(newarpfile, "%s.new", arpfile);
if ((fd = creat(newarpfile, 0644)) < 0) {
syslog(LOG_ERR, "creat(%s): %m", newarpfile);
return(0);
}
if ((dumpf = fdopen(fd, "w")) == NULL) {
syslog(LOG_ERR, "fdopen(%s): %m", newarpfile);
return(0);
}
(void)ent_loop(dumpone);
if (ferror(dumpf)) {
syslog(LOG_ERR, "ferror %s: %m", newarpfile);
return(0);
}
(void)fclose(dumpf);
if (rename(arpfile, oldarpfile) < 0) {
syslog(LOG_ERR, "rename %s -> %s: %m", arpfile, oldarpfile);
return(0);
}
if (rename(newarpfile, arpfile) < 0) {
syslog(LOG_ERR, "rename %s -> %s: %m", newarpfile, arpfile);
return(0);
}
return(1);
}
/* Initialize the databases */
int
readdata(void)
{
register FILE *f;
if ((f = fopen(arpfile, "r")) == NULL) {
syslog(LOG_ERR, "fopen(%s): %m", arpfile);
return(0);
}
if (!file_loop(f, ent_add, arpfile)) {
(void)fclose(f);
return(0);
}
(void)fclose(f);
/* It's not fatal if we can't open the ethercodes file */
if ((f = fopen(ethercodes, "r")) != NULL) {
(void)ec_loop(f, ec_add, ethercodes);
(void)fclose(f);
}
return(1);
}
char *
savestr(register const char *str)
{
register int i;
register char *cp;
static char *strptr = NULL;
static int strsize = 0;
i = strlen(str) + 1;
if (i > strsize) {
strsize = 512;
strptr = malloc(strsize);
if (strptr == NULL) {
syslog(LOG_ERR, "savestr(): malloc: %m");
exit(1);
}
memset(strptr, 0, strsize);
}
(void)strcpy(strptr, str);
cp = strptr;
strptr += i;
strsize -= i;
return (cp);
}