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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
/*
* Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000
* 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: report.c,v 1.46 2000/09/30 23:41:04 leres Exp $ (LBL)";
#endif
/*
* report - arpwatch report generating routines
*/
#include <sys/param.h>
#include <sys/types.h> /* concession to AIX */
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/wait.h>
#if __STDC__
struct mbuf;
struct rtentry;
#endif
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#ifdef TIME_WITH_SYS_TIME
#include <time.h>
#endif
#include <unistd.h>
#include "gnuc.h"
#ifdef HAVE_OS_PROTO_H
#include "os-proto.h"
#endif
#include "addresses.h"
#include "arpwatch.h"
#include "dns.h"
#include "ec.h"
#include "report.h"
#include "setsignal.h"
#include "util.h"
#include "localhost.h"
#define PLURAL(n) ((n) == 1 || (n) == -1 ? "" : "s")
static int cdepth; /* number of outstanding children */
static char *fmtdate(time_t);
static char *fmtdelta(time_t);
RETSIGTYPE reaper(int);
static int32_t gmt2local(void);
static char *
fmtdelta(register time_t t)
{
register char *cp;
register int minus;
static char buf[132];
minus = 0;
if (t < 0) {
t = -t;
++minus;
}
if (t < 60) {
cp = "second";
} else if (t < 60 * 60) {
t /= 60;
cp = "minute";
} else if (t < 24 * 60 * 60) {
t /= (60 * 60);
cp = "hour";
} else {
t /= (24 * 60 * 60);
cp = "day";
}
if (minus)
t = -t;
(void)sprintf(buf, "%u %s%s", (u_int32_t)t, cp, PLURAL(t));
return(buf);
}
static char *dow[7] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};
static char *moy[12] = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
#define DOW(d) ((d) < 0 || (d) >= 7 ? "?" : dow[d])
#define MOY(m) ((m) < 0 || (m) >= 12 ? "?" : moy[(m)])
static char *
fmtdate(time_t t)
{
register struct tm *tm;
register int32_t mw;
register char ch;
static int init = 0;
static char zone[32], buf[132];
if (t == 0)
return("<no date>");
if (!init) {
mw = gmt2local() / 60;
if (mw < 0) {
ch = '-';
mw = -mw;
} else {
ch = '+';
}
(void)sprintf(zone, "%c%02d%02d", ch, mw / 60, mw % 60);
}
tm = localtime(&t);
(void)sprintf(buf, "%s, %s %d, %d %d:%02d:%02d %s",
DOW(tm->tm_wday),
MOY(tm->tm_mon),
tm->tm_mday,
tm->tm_year + 1900,
tm->tm_hour,
tm->tm_min,
tm->tm_sec,
zone);
return(buf);
}
/*
* Returns the difference between gmt and local time in seconds.
* Use gmtime() and localtime() to keep things simple.
*/
static int32_t
gmt2local(void)
{
register int dt, dir;
register struct tm *gmt, *loc;
time_t t;
struct tm sgmt;
t = time(NULL);
gmt = &sgmt;
*gmt = *gmtime(&t);
loc = localtime(&t);
dt = (loc->tm_hour - gmt->tm_hour) * 60 * 60 +
(loc->tm_min - gmt->tm_min) * 60;
/*
* If the year or julian day is different, we span 00:00 GMT
* and must add or subtract a day. Check the year first to
* avoid problems when the julian day wraps.
*/
dir = loc->tm_year - gmt->tm_year;
if (dir == 0)
dir = loc->tm_yday - gmt->tm_yday;
dt += dir * 24 * 60 * 60;
return (dt);
}
RETSIGTYPE
reaper(int signo)
{
register pid_t pid;
DECLWAITSTATUS status;
for (;;) {
pid = waitpid((pid_t)0, &status, WNOHANG);
if ((int)pid < 0) {
/* ptrace foo */
if (errno == EINTR)
continue;
/* ECHILD means no one left */
if (errno != ECHILD)
syslog(LOG_ERR, "reaper: %m");
break;
}
/* Already got everyone who was done */
if (pid == 0)
break;
--cdepth;
if (WEXITSTATUS(status))
syslog(LOG_DEBUG, "reaper: pid %d, exit status %d",
pid, WEXITSTATUS(status));
}
return RETSIGVAL;
}
void
report(register char *title, register u_int32_t a, register u_char *e1,
register u_char *e2, register time_t *t1p, register time_t *t2p,
char *interface)
{
extern char *path_sendmail;
register char *cp, *hn;
register int fd, pid;
register FILE *f;
char tempfile[64], cpu[64], os[64];
char *fmt = "%20s: %s\n";
char *watcher = mailaddress;
char *watchee = WATCHEE;
char *sendmail = path_sendmail;
char *unknown = "<unknown>";
char buf[132];
static int init = 0;
/* No report until we're initialized */
if (initializing)
return;
if (debug) {
if (debug > 1) {
dosyslog(LOG_NOTICE, title, a, e1, e2, interface);
return;
}
f = stdout;
(void)putc('\n', f);
} else {
/* Setup child reaper if we haven't already */
if (!init) {
(void)setsignal(SIGCHLD, reaper);
++init;
}
while (cdepth >= 3) {
syslog(LOG_ERR, "report: pausing (cdepth %d)", cdepth);
pause();
}
/* Syslog this event too */
dosyslog(LOG_NOTICE, title, a, e1, e2, interface);
/* return if watcher is an empty string */
if ( quiet )
return;
/* Update child depth */
++cdepth;
/* Fork off child to send mail */
pid = fork();
if (pid) {
/* Parent */
if (pid < 0)
syslog(LOG_ERR, "report: fork() 1: %m");
return;
}
/* Child */
closelog();
(void)strcpy(tempfile, "/tmp/arpwatch.XXXXXX");
if ((fd = mkstemp(tempfile)) < 0) {
syslog(LOG_ERR, "mkstemp(%s) %m", tempfile);
exit(1);
}
if ((f = fdopen(fd, "w+")) == NULL) {
syslog(LOG_ERR, "child fdopen(%s): %m", tempfile);
exit(1);
}
/* Cheap delete-on-close */
if (unlink(tempfile) < 0)
syslog(LOG_ERR, "unlink(%s): %m", tempfile);
}
(void)fprintf(f, "From: arpwatch (Arpwatch %s)\n", localhost());
(void)fprintf(f, "To: %s\n", watcher);
if (interface == NULL) interface = ""; /* shouldn't happen */
hn = gethname(a);
(void)fprintf(f, "Subject: %s (%s) %s\n", title, hn,
interface);
if (isdigit(*hn))
hn = unknown;
(void)putc('\n', f);
(void)fprintf(f, fmt, "hostname", hn);
(void)fprintf(f, fmt, "ip address", intoa(a));
(void)fprintf(f, fmt, "interface", interface);
(void)fprintf(f, fmt, "ethernet address", e2str(e1));
if ((cp = ec_find(e1)) == NULL)
cp = unknown;
(void)fprintf(f, fmt, "ethernet vendor", cp);
if (hn != unknown && gethinfo(hn, cpu, sizeof(cpu), os, sizeof(os))) {
(void)sprintf(buf, "%s %s", cpu, os);
(void)fprintf(f, fmt, "dns cpu & os", buf);
}
if (e2) {
(void)fprintf(f, fmt, "old ethernet address", e2str(e2));
if ((cp = ec_find(e2)) == NULL)
cp = unknown;
(void)fprintf(f, fmt, "old ethernet vendor", cp);
}
if (t1p)
(void)fprintf(f, fmt, "timestamp", fmtdate(*t1p));
if (t2p)
(void)fprintf(f, fmt, "previous timestamp", fmtdate(*t2p));
if (t1p && t2p && *t1p && *t2p)
(void)fprintf(f, fmt, "delta", fmtdelta(*t1p - *t2p));
if (debug) {
fflush(f);
return;
}
(void)rewind(f);
if (dup2(fileno(f), fileno(stdin)) < 0) {
syslog(LOG_ERR, "dup2: %m");
exit(1);
}
/* XXX Need to freopen()? */
/* Always Deliver interactively (pause when child depth gets large) */
execl(sendmail, "sendmail", "-odi", watcher, NULL);
syslog(LOG_ERR, "execl: %s: %m", sendmail);
exit(1);
}
|