~ubuntu-branches/ubuntu/trusty/net-snmp/trusty

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
/*
 * snmptest.c - send snmp requests to a network entity.
 *
 * Usage: snmptest -v 1 [-q] hostname community [objectID]
 *
 */
/***********************************************************************
	Copyright 1988, 1989, 1991, 1992 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 <net-snmp/net-snmp-config.h>

#if HAVE_STDLIB_H
#include <stdlib.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#include <sys/types.h>
#if HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#include <stdio.h>
#include <ctype.h>
#if TIME_WITH_SYS_TIME
# ifdef WIN32
#  include <sys/timeb.h>
# else
#  include <sys/time.h>
# endif
# include <time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif
#if HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#if HAVE_WINSOCK_H
#include <winsock.h>
#endif
#if HAVE_NETDB_H
#include <netdb.h>
#endif
#if HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif

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

int             command = SNMP_MSG_GET;

int             input_variable(netsnmp_variable_list *);

void
usage(void)
{
    fprintf(stderr, "USAGE: snmptest ");
    snmp_parse_args_usage(stderr);
    fprintf(stderr, "\n\n");
    snmp_parse_args_descriptions(stderr);
}

int
main(int argc, char *argv[])
{
    netsnmp_session session, *ss;
    netsnmp_pdu    *pdu = NULL, *response, *copy = NULL;
    netsnmp_variable_list *vars, *vp;
    netsnmp_transport *transport = NULL;
    int             ret;
    int             status, count;
    char            input[128];
    int             varcount, nonRepeaters = -1, maxRepetitions;

    /*
     * get the common command line arguments 
     */
    switch (snmp_parse_args(argc, argv, &session, NULL, NULL)) {
    case -2:
        exit(0);
    case -1:
        usage();
        exit(1);
    default:
        break;
    }

    SOCK_STARTUP;

    /*
     * open an SNMP session 
     */
    ss = snmp_open(&session);
    if (ss == NULL) {
        /*
         * diagnose snmp_open errors with the input netsnmp_session pointer 
         */
        snmp_sess_perror("snmptest", &session);
        SOCK_CLEANUP;
        exit(1);
    }

    varcount = 0;
    while (1) {
        vars = NULL;
        for (ret = 1; ret != 0;) {
            vp = (netsnmp_variable_list *)
                malloc(sizeof(netsnmp_variable_list));
            memset(vp, 0, sizeof(netsnmp_variable_list));

            while ((ret = input_variable(vp)) == -1);
            if (ret == 1) {
                varcount++;
                /*
                 * add it to the list 
                 */
                if (vars == NULL) {
                    /*
                     * if first variable 
                     */
                    pdu = snmp_pdu_create(command);
                    pdu->variables = vp;
                } else {
                    vars->next_variable = vp;
                }
                vars = vp;
            } else {
                /*
                 * free the last (unused) variable 
                 */
                if (vp->name)
                    free((char *) vp->name);
                if (vp->val.string)
                    free((char *) vp->val.string);
                free((char *) vp);

                if (command == SNMP_MSG_GETBULK) {
                    if (nonRepeaters == -1) {
                        nonRepeaters = varcount;
                        ret = -1;       /* so we collect more variables */
                        printf("Now input the repeating variables\n");
                    } else {
                        printf("What repeat count? ");
                        fflush(stdout);
                        fgets(input, sizeof(input), stdin);
                        maxRepetitions = atoi(input);
                        pdu->non_repeaters = nonRepeaters;
                        pdu->max_repetitions = maxRepetitions;
                    }
                }
            }
            if (varcount == 0 && ret == 0) {
                if (!copy) {
                    printf("No PDU to send.\n");
                    ret = -1;
                } else {
                    pdu = snmp_clone_pdu(copy);
                    printf("Resending last PDU.\n");
                }
            }
        }
        copy = snmp_clone_pdu(pdu);
        if (command == SNMP_MSG_TRAP2) {
            /*
             * No response needed 
             */
            if (!snmp_send(ss, pdu)) {
                snmp_free_pdu(pdu);
                snmp_sess_perror("snmptest", ss);
            }
        } else {
            status = snmp_synch_response(ss, pdu, &response);
            if (status == STAT_SUCCESS) {
                if (command == SNMP_MSG_INFORM &&
                    response->errstat == SNMP_ERR_NOERROR) {
                    printf("Inform Acknowledged\n");
                } else {
                    switch (response->command) {
                    case SNMP_MSG_GET:
                        printf("Received Get Request ");
                        break;
                    case SNMP_MSG_GETNEXT:
                        printf("Received Getnext Request ");
                        break;
                    case SNMP_MSG_RESPONSE:
                        printf("Received Get Response ");
                        break;
                    case SNMP_MSG_SET:
                        printf("Received Set Request ");
                        break;
                    case SNMP_MSG_TRAP:
                        printf("Received Trap Request ");
                        break;
                    case SNMP_MSG_GETBULK:
                        printf("Received Bulk Request ");
                        break;
                    case SNMP_MSG_INFORM:
                        printf("Received Inform Request ");
                        break;
                    case SNMP_MSG_TRAP2:
                        printf("Received SNMPv2 Trap Request ");
                        break;
                    }
                    transport = snmp_sess_transport(snmp_sess_pointer(ss));
                    if (transport != NULL && transport->f_fmtaddr != NULL) {
                        char *addr_string = transport->f_fmtaddr(transport,
                                                                 response->
                                                                 transport_data,
                                                                 response->
                                                                 transport_data_length);
                        if (addr_string != NULL) {
                            printf("from %s\n", addr_string);
                            free(addr_string);
                        }
                    } else {
                        printf("from <UNKNOWN>\n");
                    }
                    printf
                        ("requestid 0x%lX errstat 0x%lX errindex 0x%lX\n",
                         response->reqid, response->errstat,
                         response->errindex);
                    if (response->errstat == SNMP_ERR_NOERROR) {
                        for (vars = response->variables; vars;
                             vars = vars->next_variable)
                            print_variable(vars->name, vars->name_length,
                                           vars);
                    } else {
                        printf("Error in packet.\nReason: %s\n",
                               snmp_errstring(response->errstat));
                        if (response->errindex != 0) {
                            for (count = 1, vars = response->variables;
                                 vars && count != response->errindex;
                                 vars = vars->next_variable, count++);
                            if (vars) {
                                printf("Failed object: ");
                                print_objid(vars->name, vars->name_length);
                            }
                            printf("\n");
                        }
                    }
                }
            } else if (status == STAT_TIMEOUT) {
                printf("Timeout: No Response from %s\n", session.peername);
            } else {            /* status == STAT_ERROR */
                snmp_sess_perror("snmptest", ss);
            }

            if (response)
                snmp_free_pdu(response);
        }
        varcount = 0;
        nonRepeaters = -1;
    }
    SOCK_CLEANUP;
    return 0;
}

int
input_variable(netsnmp_variable_list * vp)
{
    char            buf[256];
    size_t          val_len;
    u_char          value[256], ch;
    oid             name[MAX_OID_LEN];

    printf("Variable: ");
    fflush(stdout);
    if (!fgets(buf, sizeof(buf), stdin)) {
        printf("Quitting,  Goobye\n");
        SOCK_CLEANUP;
        exit(0);
    }
    val_len = strlen(buf);

    if (val_len == 0 || *buf == '\n') {
        vp->name_length = 0;
        return 0;
    }
    if (buf[val_len - 1] == '\n')
        buf[--val_len] = 0;
    if (*buf == '$') {
        switch (toupper(buf[1])) {
        case 'G':
            command = SNMP_MSG_GET;
            printf("Request type is Get Request\n");
            break;
        case 'N':
            command = SNMP_MSG_GETNEXT;
            printf("Request type is Getnext Request\n");
            break;
        case 'S':
            command = SNMP_MSG_SET;
            printf("Request type is Set Request\n");
            break;
        case 'B':
            command = SNMP_MSG_GETBULK;
            printf("Request type is Bulk Request\n");
            printf
                ("Enter a blank line to terminate the list of non-repeaters\n");
            printf("and to begin the repeating variables\n");
            break;
        case 'I':
            command = SNMP_MSG_INFORM;
            printf("Request type is Inform Request\n");
            printf("(Are you sending to the right port?)\n");
            break;
        case 'T':
            command = SNMP_MSG_TRAP2;
            printf("Request type is SNMPv2 Trap Request\n");
            printf("(Are you sending to the right port?)\n");
            break;
        case 'D':
            if (snmp_get_dump_packet()) {
                snmp_set_dump_packet(0);
                printf("Turned packet dump off\n");
            } else {
                snmp_set_dump_packet(1);
                printf("Turned packet dump on\n");
            }
            break;
        case 'Q':
            switch ((toupper(buf[2]))) {
            case '\n':
            case 0:
                printf("Quitting,  Goodbye\n");
                SOCK_CLEANUP;
                exit(0);
                break;
            case 'P':
                if (snmp_get_quick_print()) {
                    snmp_set_quick_print(0);
                    printf("Turned quick printing off\n");
                } else {
                    snmp_set_quick_print(1);
                    printf("Turned quick printing on\n");
                }
                break;
            }
            break;
        default:
            printf("Bad command\n");
        }
        return -1;
    }
    vp->name_length = MAX_OID_LEN;
    if (!snmp_parse_oid(buf, name, &vp->name_length)) {
        snmp_perror(buf);
        return -1;
    }
    vp->name = (oid *) malloc(vp->name_length * sizeof(oid));
    memmove(vp->name, name, vp->name_length * sizeof(oid));

    if (command == SNMP_MSG_SET || command == SNMP_MSG_INFORM
        || command == SNMP_MSG_TRAP2) {
        printf("Type [i|u|s|x|d|n|o|t|a]: ");
        fflush(stdout);
        fgets(buf, sizeof(buf), stdin);
        ch = *buf;
        switch (ch) {
        case 'i':
            vp->type = ASN_INTEGER;
            break;
        case 'u':
            vp->type = ASN_UNSIGNED;
            break;
        case 's':
            vp->type = ASN_OCTET_STR;
            break;
        case 'x':
            vp->type = ASN_OCTET_STR;
            break;
        case 'd':
            vp->type = ASN_OCTET_STR;
            break;
        case 'n':
            vp->type = ASN_NULL;
            break;
        case 'o':
            vp->type = ASN_OBJECT_ID;
            break;
        case 't':
            vp->type = ASN_TIMETICKS;
            break;
        case 'a':
            vp->type = ASN_IPADDRESS;
            break;
        default:
            printf
                ("bad type \"%c\", use \"i\", \"u\", \"s\", \"x\", \"d\", \"n\", \"o\", \"t\", or \"a\".\n",
                 *buf);
            return -1;
        }
      getValue:
        printf("Value: ");
        fflush(stdout);
        fgets(buf, sizeof(buf), stdin);
        switch (vp->type) {
        case ASN_INTEGER:
            vp->val.integer = (long *) malloc(sizeof(long));
            *(vp->val.integer) = atoi(buf);
            vp->val_len = sizeof(long);
            break;
        case ASN_UNSIGNED:
            vp->val.integer = (long *) malloc(sizeof(long));
            *(vp->val.integer) = strtoul(buf, NULL, 0);
            vp->val_len = sizeof(long);
            break;
        case ASN_OCTET_STR:
            if (ch == 'd') {
                size_t          buf_len = 256;
                val_len = 0;
                if ((vp->val.string = (u_char *) malloc(buf_len)) == NULL) {
                    printf("malloc failure\n");
                    goto getValue;
                }
                if (!snmp_decimal_to_binary(&(vp->val.string), &buf_len,
                                            &val_len, 1, buf)) {
                    printf("Bad value or no sub-identifier > 255\n");
                    free(vp->val.string);
                    goto getValue;
                }
                vp->val_len = val_len;
            } else if (ch == 's') {
                /*
                 * -1 to omit trailing newline  
                 */
                vp->val.string = (u_char *) malloc(strlen(buf) - 1);
                if (vp->val.string == NULL) {
                    printf("malloc failure\n");
                    goto getValue;
                }
                memcpy(vp->val.string, buf, strlen(buf) - 1);
                vp->val.string[sizeof(vp->val.string)-1] = 0;
                vp->val_len = strlen(buf) - 1;
            } else if (ch == 'x') {
                size_t          buf_len = 256;
                val_len = 0;
                if ((vp->val.string = (u_char *) malloc(buf_len)) == NULL) {
                    printf("malloc failure\n");
                    goto getValue;
                }
                if (!snmp_hex_to_binary(&(vp->val.string), &buf_len,
                                        &val_len, 1, buf)) {
                    printf("Bad value (need pairs of hex digits)\n");
                    free(vp->val.string);
                    goto getValue;
                }
                vp->val_len = val_len;
            }
            break;
        case ASN_NULL:
            vp->val_len = 0;
            vp->val.string = NULL;
            break;
        case ASN_OBJECT_ID:
            if ('\n' == buf[strlen(buf) - 1])
                buf[strlen(buf) - 1] = '\0';
            vp->val_len = MAX_OID_LEN;;
            if (0 == read_objid(buf, (oid *) value, &vp->val_len)) {
                printf("Unrecognised OID value\n");
                goto getValue;
            }
            vp->val_len *= sizeof(oid);
            vp->val.objid = (oid *) malloc(vp->val_len);
            memmove(vp->val.objid, value, vp->val_len);
            break;
        case ASN_TIMETICKS:
            vp->val.integer = (long *) malloc(sizeof(long));
            *(vp->val.integer) = atoi(buf);
            vp->val_len = sizeof(long);
            break;
        case ASN_IPADDRESS:
            vp->val.integer = (long *) malloc(sizeof(long));
            *(vp->val.integer) = inet_addr(buf);
            vp->val_len = sizeof(long);
            break;
        default:
            printf("Internal error\n");
            break;
        }
    } else {                    /* some form of get message */
        vp->type = ASN_NULL;
        vp->val_len = 0;
    }
    return 1;
}