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
|
/*
* snmptrapd.c - receive and log snmp traps
*
*/
/***********************************************************
Copyright 1989 by Carnegie Mellon University
All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of CMU not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.
******************************************************************/
#include <sys/types.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/time.h>
#include <errno.h>
#include <syslog.h>
#ifdef linux
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#endif
#include "mib.h"
#include "snmp.h"
#include "snmp_impl.h"
#include "asn1.h"
#include "snmp_api.h"
#include "snmp_client.h"
#ifndef BSD4_3
#ifndef NFDBITS
typedef long fd_mask;
#define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */
#define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
#define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
#define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
#define FD_ZERO(p) bzero((char *)(p), sizeof(*(p)))
#endif
#endif
extern int errno;
int snmp_dump_packet = 0;
int Print = 0;
static void init_syslog();
char *
trap_description(trap)
int trap;
{
switch(trap){
case SNMP_TRAP_COLDSTART:
return "Cold Start";
case SNMP_TRAP_WARMSTART:
return "Warm Start";
case SNMP_TRAP_LINKDOWN:
return "Link Down";
case SNMP_TRAP_LINKUP:
return "Link Up";
case SNMP_TRAP_AUTHFAIL:
return "Authentication Failure";
case SNMP_TRAP_EGPNEIGHBORLOSS:
return "EGP Neighbor Loss";
case SNMP_TRAP_ENTERPRISESPECIFIC:
return "Enterprise Specific";
default:
return "Unknown Type";
}
}
char *
uptime_string(timeticks, buf)
u_long timeticks;
char *buf;
{
int seconds, minutes, hours, days;
timeticks /= 100;
days = timeticks / (60 * 60 * 24);
timeticks %= (60 * 60 * 24);
hours = timeticks / (60 * 60);
timeticks %= (60 * 60);
minutes = timeticks / 60;
seconds = timeticks % 60;
if (days == 0){
sprintf(buf, "%d:%02d:%02d", hours, minutes, seconds);
} else if (days == 1) {
sprintf(buf, "%d day, %d:%02d:%02d", days, hours, minutes, seconds);
} else {
sprintf(buf, "%d days, %d:%02d:%02d", days, hours, minutes, seconds);
}
return buf;
}
int snmp_input(op, session, reqid, pdu, magic)
int op;
struct snmp_session *session;
int reqid;
struct snmp_pdu *pdu;
void *magic;
{
struct variable_list *vars;
char buf[64], sbuf [10240];
if (op == RECEIVED_MESSAGE && pdu->command == TRP_REQ_MSG){
if (Print){
#ifdef HAVE_IPX
printf("%s: %s Trap (%d) Uptime: %s\n",
(pdu->agent_addr.sin_addr.s_addr == SNMP_DEFAULT_ADDRESS
? ipx_ntoa((ipxaddr *)&pdu->address)
: inet_ntoa(pdu->agent_addr.sin_addr)),
uptime_string(pdu->time, buf));
#else
printf("%s: %s Trap (%d) Uptime: %s\n",
inet_ntoa(pdu->agent_addr.sin_addr),
trap_description(pdu->trap_type), pdu->specific_type,
uptime_string(pdu->time, buf));
#endif
for(vars = pdu->variables; vars; vars = vars->next_variable)
print_variable(vars->name, vars->name_length, vars);
} else {
#ifdef HAVE_IPX
sprintf(sbuf, "%s: %s Trap (%d) Uptime: %s\n",
(pdu->agent_addr.sin_addr.s_addr == SNMP_DEFAULT_ADDRESS
? ipx_ntoa((ipxaddr *)&pdu->address)
: inet_ntoa(pdu->agent_addr.sin_addr)),
uptime_string(pdu->time, buf));
#else
sprintf (sbuf, "%s: %s Trap (%d) Uptime: %s",
inet_ntoa(pdu->agent_addr.sin_addr),
trap_description(pdu->trap_type), pdu->specific_type,
uptime_string(pdu->time, buf));
#endif
for (vars = pdu->variables; vars; vars = vars->next_variable) {
/* XXX: check buffer space avail */
strcat (sbuf, " ");
sprint_variable (sbuf + strlen (sbuf),
vars->name, vars->name_length, vars);
}
syslog(LOG_WARNING, sbuf);
}
} else if (op == TIMED_OUT){
printf("Timeout: This shouldn't happen!\n");
}
return 0;
}
int
main(argc, argv)
int argc;
char *argv[];
{
struct snmp_session session, *ss;
int arg;
int count, numfds, block;
fd_set fdset;
struct timeval timeout, *tvp;
int setup_ipx = 0;
init_syslog();
init_mib();
/*
* usage: snmptrapd [-p]
*/
for(arg = 1; arg < argc; arg++){
if (argv[arg][0] == '-'){
switch(argv[arg][1]){
case 'd':
snmp_dump_packet++;
break;
case 'p':
Print++;
break;
#if 1
case 's':
/* ignore */
break;
case 'v':
arg++;
break;
#endif
case 'i':
setup_ipx++;
break;
default:
printf("invalid option: -%c\n", argv[arg][1]);
printf("Usage: snmptrapd [-p] [-s] [-i] [-v 1]\n");
break;
}
continue;
}
}
bzero((char *)&session, sizeof(struct snmp_session));
session.peername = NULL;
session.community = NULL;
session.community_len = 0;
session.retries = SNMP_DEFAULT_RETRIES;
session.timeout = SNMP_DEFAULT_TIMEOUT;
session.authenticator = NULL;
session.callback = snmp_input;
session.callback_magic = NULL;
session.local_port = SNMP_TRAP_PORT;
if (setup_ipx > 0) {
#ifdef HAVE_IPX
session.local_port_ipx = SNMP_TRAP_PORT_IPX;
#else
fprintf (stderr, "snmptrapd: no IPX support compiled in.\n");
exit (1);
#endif
}
ss = snmp_open(&session);
if (ss == NULL){
printf("Couldn't open snmp\n");
exit(-1);
}
while(1){
numfds = 0;
FD_ZERO(&fdset);
block = 1;
tvp = &timeout;
timerclear(tvp);
snmp_select_info(&numfds, &fdset, tvp, &block);
if (block == 1)
tvp = NULL; /* block without timeout */
count = select(numfds, &fdset, 0, 0, tvp);
if (count > 0){
snmp_read(&fdset);
} else switch(count){
case 0:
snmp_timeout();
break;
case -1:
if (errno == EINTR){
continue;
} else {
perror("select");
}
return -1;
default:
printf("select returned %d\n", count);
return -1;
}
}
/* not reached */
}
static void
init_syslog(){
/*
* These definitions handle 4.2 systems without additional syslog facilities.
*/
#ifndef LOG_CONS
#define LOG_CONS 0 /* Don't bother if not defined... */
#endif
#ifndef LOG_LOCAL0
#define LOG_LOCAL0 0
#endif
/*
* All messages will be logged to the local0 facility and will be sent to
* the console if syslog doesn't work.
*/
openlog("snmptrapd", LOG_CONS, LOG_LOCAL0);
syslog(LOG_INFO, "Starting snmptrapd");
}
|