summaryrefslogtreecommitdiff
path: root/apps/agentxtrap.c
blob: f366f61853bb7ea970f710438e22f67726d21d0e (plain)
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-features.h>

#include <errno.h>
#include <signal.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h> /* optind, optarg and optopt */
#endif

#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/ds_agent.h>

#include "../agent/mibgroup/agentx/agentx_config.h"
#include "../agent/mibgroup/agentx/client.h"
#include "../agent/mibgroup/agentx/protocol.h"

netsnmp_feature_require(snmp_split_pdu)
netsnmp_feature_require(snmp_reset_var_types)


#ifdef __GNUC__
#define UNUSED __attribute__((unused))
#else
#define UNUSED
#endif

extern const oid sysuptime_oid[];
extern const size_t sysuptime_oid_len;
extern const oid snmptrap_oid[];
extern const size_t snmptrap_oid_len;

static void
usage(const char* progname)
{
    fprintf(stderr,
            "USAGE: %s [OPTIONS] TRAP-PARAMETERS\n"
            "\n"
            "  Version:  %s\n"
            "  Web:      http://www.net-snmp.org/\n"
            "  Email:    net-snmp-coders@lists.sourceforge.net\n"
            "\n"
            "OPTIONS:\n", progname, netsnmp_get_version());

    fprintf(stderr,
            "  -h\t\t\tdisplay this help message\n"
            "  -V\t\t\tdisplay package version number\n"
            "  -m MIB[:...]\t\tload given list of MIBs (ALL loads "
            "everything)\n"
            "  -M DIR[:...]\t\tlook in given list of directories for MIBs\n"
            "  -D[TOKEN[,...]]\tturn on debugging output for the specified "
            "TOKENs\n"
            "\t\t\t   (ALL gives extremely verbose debugging output)\n"
            "  -d\t\t\tdump all traffic\n");
#ifndef NETSNMP_DISABLE_MIB_LOADING
    fprintf(stderr,
            "  -P MIBOPTS\t\tToggle various defaults controlling mib "
            "parsing:\n");
    snmp_mib_toggle_options_usage("\t\t\t  ", stderr);
#endif /* NETSNMP_DISABLE_MIB_LOADING */
    fprintf(stderr,
            "  -L LOGOPTS\t\tToggle various defaults controlling logging:\n");
    snmp_log_options_usage("\t\t\t  ", stderr);

    fprintf(stderr,
            "  -c context\n"
            "  -U uptime\n"
            "  -x ADDRESS\t\tuse ADDRESS as AgentX address\n"
            "\n"
            "TRAP-PARAMETERS:\n"
            "  trapoid [OID TYPE VALUE] ...\n");
}

struct tState_s;
typedef const struct tState_s* tState;
struct tState_s {
    void (*entry)(tState self); /**<< State entry action */
    void (*exit)(tState self); /**<< State exit action */
    /** Handler for AgentX-Response-PDU's */
    void (*response)(tState self, netsnmp_pdu *res);
    /** State to change to if an AgentX timeout occurs or the timer runs out */
    tState timeout;
    void (*disconnect)(tState self); /**<< Handler for disconnect indications */
    /** Handler for Close-PDU indications */
    void (*close)(tState self, netsnmp_pdu *res);
    const char* name; /**<< Name of the current state */
    int is_open; /**<< If the connection is open in this state */
};

static tState state; /**<< Current state of the state machine */
static tState next_state; /**<< Next state of the state machine */

static const char  *context = NULL; /**<< Context that delivers the trap */
static size_t       contextLen; /**<< Length of eventual context */
static int          result = 1; /**<< Program return value */
static netsnmp_pdu *pdu = NULL; /**<< The trap pdu that is to be sent */
/** The reference number of the next packet */
static long         packetid = 0;
/** The session id of the session to the master */
static long         session;
static void        *sessp = NULL; /**<< The current communication session */

#define STATE_CALL(method)                                              \
    if(!state->method) {                                                \
        snmp_log(LOG_ERR, "No " #method " method in %s, terminating\n", \
                 state->name);                                          \
        abort();                                                        \
    } else                                                              \
        state->method

static void
change_state(tState new_state)
{
    if (next_state && next_state != new_state)
        DEBUGMSGTL(("process", "Ignore transition to %s\n", next_state->name));
    next_state = new_state;
}

static int
handle_agentx_response(int operation, netsnmp_session *sp, UNUSED int reqid,
                       netsnmp_pdu *act, UNUSED void *magic)
{
    switch(operation) {
    case NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE:
        if(act->command == AGENTX_MSG_CLEANUPSET) {
            /* Do nothing - no response and no action as nothing get
             * allocated in any handler here
             */
        } else if(act->command != AGENTX_MSG_RESPONSE) {
            /* Copy the head to a response */
            netsnmp_pdu* res = snmp_split_pdu(act, 0, 0);
            res->command = AGENTX_MSG_RESPONSE;
            if (act->sessid != session || !state->is_open)
                res->errstat = AGENTX_ERR_NOT_OPEN;
            if(res->errstat == AGENTX_ERR_NOERROR)
                switch(act->command) {
                case AGENTX_MSG_GET:
                    res->variables = snmp_clone_varbind(act->variables);
                    snmp_reset_var_types(res->variables, SNMP_NOSUCHOBJECT);
                    break;
                case AGENTX_MSG_GETNEXT:
                case AGENTX_MSG_GETBULK:
                    res->variables = snmp_clone_varbind(act->variables);
                    snmp_reset_var_types(res->variables, SNMP_ENDOFMIBVIEW);
                    break;
                case AGENTX_MSG_TESTSET:
                    res->errstat = SNMP_ERR_NOTWRITABLE;
                    res->errindex = 1;
                    break;
                case AGENTX_MSG_COMMITSET:
                    res->errstat = SNMP_ERR_COMMITFAILED;
                    res->errindex = 1;
                    break;
                case AGENTX_MSG_UNDOSET:
                    /* Success - could undo not setting any value :-) */
                    break;
                case AGENTX_MSG_CLOSE:
                    /* Always let the master succeed! */
                    break;
                default:
                    /* Unknown command */
                    res->errstat = AGENTX_ERR_PARSE_FAILED;
                    break;
                }
            if(snmp_send(sp, res) == 0)
                snmp_free_pdu(res);
            switch(act->command) {
            case AGENTX_MSG_CLOSE:
                /* Take action once the answer is sent! */
                STATE_CALL(close)(state, act);
                break;
            default:
                /* Do nothing */
                break;
            }
        } else
            /* RESPONSE act->time, act->errstat, act->errindex, varlist */
            STATE_CALL(response)(state, act);
        break;
    case NETSNMP_CALLBACK_OP_TIMED_OUT:
        change_state(state->timeout);
        break;
    case NETSNMP_CALLBACK_OP_DISCONNECT:
        STATE_CALL(disconnect)(state);
        break;
    }
    return 0;
}

extern const struct tState_s Connecting;
extern const struct tState_s Opening;
extern const struct tState_s Notifying;
extern const struct tState_s Closing;
extern const struct tState_s Disconnecting;
extern const struct tState_s Exit;

static void
StateDisconnect(UNUSED tState self)
{
    snmp_log(LOG_ERR, "Unexpected disconnect in state %s\n", self->name);
    change_state(&Disconnecting);
}

static void
StateClose(UNUSED tState self, netsnmp_pdu *act)
{
    snmp_log(LOG_ERR, "Unexpected close with reason code %ld in state %s\n",
             act->errstat, self->name);
    change_state(&Disconnecting);
}

static void
ConnectingEntry(UNUSED tState self)
{
    netsnmp_session init;
    netsnmp_transport* t;
    void* sess;

    if(sessp) {
        snmp_sess_close(sessp);
        sessp = NULL;
    }

    snmp_sess_init(&init);
    init.version = AGENTX_VERSION_1;
    init.retries = 0; /* Retries are handled by the state machine */
    init.timeout = SNMP_DEFAULT_TIMEOUT;
    init.flags |= SNMP_FLAGS_STREAM_SOCKET;
    init.callback = handle_agentx_response;
    init.authenticator = NULL;

    if(!(t = netsnmp_transport_open_client(
             "agentx", netsnmp_ds_get_string(
                 NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_X_SOCKET)))) {
        snmp_perror("Failed to connect to AgentX server");
        change_state(&Exit);
    } else if(!(sess = snmp_sess_add_ex(
                    &init, t, NULL, agentx_parse, NULL, NULL,
                    agentx_realloc_build, agentx_check_packet, NULL))) {
        snmp_perror("Failed to create session");
        change_state(&Exit);
    } else {
        sessp = sess;
        change_state(&Opening);
    }
}

const struct tState_s Connecting = {
    ConnectingEntry,
    NULL,
    NULL,
    NULL,
    StateDisconnect,
    NULL,
    "Connnecting",
    0
};

static netsnmp_pdu*
pdu_create_opt_context(int command, const char* context, size_t len)
{
    netsnmp_pdu* res = snmp_pdu_create(command);
    if (res)
        if (context) {
            if (snmp_clone_mem((void**)&res->contextName, context, len)) {
                snmp_free_pdu(res);
                res = NULL;
            } else
                res->contextNameLen = len;
        }
    return res;
}

static void
OpeningEntry(UNUSED tState self)
{
    netsnmp_pdu* act =
        pdu_create_opt_context(AGENTX_MSG_OPEN, context, contextLen);
    if(act) {
        act->sessid = 0;
        act->transid = 0;
        act->reqid = ++packetid;
        act->time = 0;
        snmp_pdu_add_variable(act, NULL, 0, ASN_OCTET_STR, NULL, 0);
        if(snmp_sess_send(sessp, act) == 0)
            snmp_free_pdu(act);
    }
}

static void
OpeningRes(UNUSED tState self, netsnmp_pdu *act)
{
    if(act->errstat == AGENTX_ERR_NOERROR) {
        session = act->sessid;
        change_state(&Notifying);
    } else {
        snmp_perror("Failed to open session");
        change_state(&Exit);
    }
}

const struct tState_s Opening = {
    OpeningEntry,
    NULL,
    OpeningRes,
    &Disconnecting,
    StateDisconnect,
    StateClose,
    "Opening",
    0
};

static void
NotifyingEntry(UNUSED tState self)
{
    netsnmp_pdu* act = snmp_clone_pdu(pdu);
    if(act) {
        act->sessid = session;
        act->transid = 0;
        act->reqid = ++packetid;
        if(snmp_sess_send(sessp, act) == 0)
            snmp_free_pdu(act);
    }
}

static void
NotifyingRes(UNUSED tState self, netsnmp_pdu *act)
{
    if(act->errstat == AGENTX_ERR_NOERROR)
        result = 0;
    else
        snmp_perror("Failed to send notification");
    /** \todo: Retry handling --- ClosingReconnect??? */
    change_state(&Closing);
}

const struct tState_s Notifying = {
    NotifyingEntry,
    NULL,
    NotifyingRes,
    NULL,            /** \todo: Retry handling? */
    StateDisconnect, /** \todo: Retry handling? */
    StateClose,
    "Notifying",
    1
};

static void
ClosingEntry(UNUSED tState self)
{
    /* CLOSE pdu->errstat */
    netsnmp_pdu* act =
        pdu_create_opt_context(AGENTX_MSG_CLOSE, context, contextLen);
    if(act) {
        act->sessid = session;
        act->transid = 0;
        act->reqid = ++packetid;
        act->errstat = AGENTX_CLOSE_SHUTDOWN;
        if(snmp_sess_send(sessp, act) == 0)
            snmp_free_pdu(act);
    }
}

static void
ClosingRes(UNUSED tState self, netsnmp_pdu *act)
{
    if(act->errstat != AGENTX_ERR_NOERROR) {
        snmp_log(LOG_ERR, "AgentX error status of %ld\n", act->errstat);
    }
    change_state(&Disconnecting);
}

static void
ClosingDisconnect(UNUSED tState self)
{
    change_state(&Disconnecting);
}

static void
ClosingClose(UNUSED tState self, UNUSED netsnmp_pdu *act)
{
    change_state(&Disconnecting);
}

const struct tState_s Closing = {
    ClosingEntry,
    NULL,
    ClosingRes,
    &Disconnecting,
    ClosingDisconnect,
    ClosingClose,
    "Closing",
    1
};

static void
DisconnectingEntry(UNUSED tState self)
{
    snmp_sess_close(sessp);
    sessp = NULL;
    change_state(&Exit);
}

const struct tState_s Disconnecting = {
    DisconnectingEntry,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    "Disconnecting",
    0
};

const struct tState_s Exit = {
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    "Exit",
    0
};

int
main(int argc, char *argv[])
{
    int             arg;
    char           *prognam;
    char           *cp = NULL;

    const char*     sysUpTime = NULL;

    prognam = strrchr(argv[0], '/');
    if (prognam)
        ++prognam;
    else
        prognam = argv[0];

    putenv(strdup("POSIXLY_CORRECT=1"));

    while ((arg = getopt(argc, argv, ":Vhm:M:D:dP:L:U:c:x:")) != -1) {
        switch (arg) {
        case 'h':
            usage(prognam);
            exit(0);
        case 'm':
            setenv("MIBS", optarg, 1);
            break;
        case 'M':
            setenv("MIBDIRS", optarg, 1);
            break;
        case 'c':
            context = optarg;
            contextLen = strlen(context);
            break;
        case 'D':
            debug_register_tokens(optarg);
            snmp_set_do_debugging(1);
            break;
        case 'd':
            netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
                                   NETSNMP_DS_LIB_DUMP_PACKET, 1);
            break;
        case 'U':
            sysUpTime = optarg;
            break;
        case 'V':
            fprintf(stderr, "NET-SNMP version: %s\n", netsnmp_get_version());
            exit(0);
            break;
#ifndef DISABLE_MIB_LOADING
        case 'P':
            cp = snmp_mib_toggle_options(optarg);
            if (cp != NULL) {
                fprintf(stderr, "Unknown parser option to -P: %c.\n", *cp);
                usage(prognam);
                exit(1);
            }
            break;
#endif /* DISABLE_MIB_LOADING */
        case 'L':
            if (snmp_log_options(optarg, argc, argv) < 0) {
                exit(1);
            }
            break;
        case 'x':
            if (optarg != NULL) {
                netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID,
                                      NETSNMP_DS_AGENT_X_SOCKET, optarg);
            } else
                usage(argv[0]);
            break;

        case ':':
            fprintf(stderr, "Option -%c requires an operand\n", optopt);
            usage(prognam);
            exit(1);
            break;
        case '?':
            fprintf(stderr, "Unrecognized option: -%c\n", optopt);
            usage(prognam);
            exit(1);
            break;
        }
    }

    arg = optind;

    /* initialize tcpip, if necessary */
    SOCK_STARTUP;

    init_snmp("snmpapp");
    agentx_config_init();

    /* NOTIFY varlist */
    pdu = pdu_create_opt_context(AGENTX_MSG_NOTIFY, context, contextLen);

    if (sysUpTime)
        snmp_add_var(pdu, sysuptime_oid, sysuptime_oid_len, 't', sysUpTime);

    if (arg == argc) {
        fprintf(stderr, "Missing trap-oid parameter\n");
        usage(prognam);
        SOCK_CLEANUP;
        exit(1);
    }

    if (snmp_add_var(pdu, snmptrap_oid, snmptrap_oid_len, 'o', argv[arg])) {
        snmp_perror(argv[arg]);
        SOCK_CLEANUP;
        exit(1);
    }
    ++arg;

    while (arg < argc) {
        oid    name[MAX_OID_LEN];
        size_t name_length = MAX_OID_LEN;
        arg += 3;
        if (arg > argc) {
            fprintf(stderr, "%s: Missing type/value for variable\n",
                    argv[arg - 3]);
            SOCK_CLEANUP;
            exit(1);
        }
        if (!snmp_parse_oid(argv[arg - 3], name, &name_length)) {
            snmp_perror(argv[arg - 3]);
            SOCK_CLEANUP;
            exit(1);
        }
        if (snmp_add_var(pdu, name, name_length, argv[arg - 2][0],
                         argv[arg - 1]) != 0) {
            snmp_perror(argv[arg - 3]);
            SOCK_CLEANUP;
            exit(1);
        }
    }

    packetid = 0;

    state = &Connecting;
    next_state = NULL;
    if(state->entry) state->entry(state);

    /* main loop here... */
    for(;;) {
        int block = 1;
        int numfds = 0;
        int count;
        fd_set fdset;
        struct timeval timeout;

        while(next_state) {
            if(state->exit) state->exit(state);
            DEBUGMSGTL(("process", "State transition: %s -> %s\n",
                        state->name, next_state->name));
            state = next_state;
            next_state = NULL;
            if(state->entry) state->entry(state);
        }

        if(state == &Exit)
            break;

        FD_ZERO(&fdset);
        snmp_sess_select_info(sessp, &numfds, &fdset, &timeout, &block);
        count = select(numfds, &fdset, NULL, NULL, !block ? &timeout : NULL);
        if (count > 0)
            snmp_sess_read(sessp, &fdset);
        else if (count == 0)
            snmp_sess_timeout(sessp);
        else if (errno != EINTR) {
            snmp_log(LOG_ERR, "select error [%s]\n", strerror(errno));
            change_state(&Exit);
        }
    }

    /* at shutdown time */
    snmp_free_pdu(pdu);
    pdu = NULL;

    snmp_shutdown("snmpapp");

    SOCK_CLEANUP;
    exit(result);
}