Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * snmpgetnext.c - send snmp GETNEXT requests to a network entity. |
| 3 | * |
| 4 | */ |
| 5 | /*********************************************************************** |
| 6 | Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University |
| 7 | |
| 8 | All Rights Reserved |
| 9 | |
Wes Hardaker | 84bacb0 | 1997-11-07 23:28:59 +0000 | [diff] [blame] | 10 | Permission to use, copy, modify, and distribute this software and its |
| 11 | documentation for any purpose and without fee is hereby granted, |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 12 | provided that the above copyright notice appear in all copies and that |
Wes Hardaker | 84bacb0 | 1997-11-07 23:28:59 +0000 | [diff] [blame] | 13 | both that copyright notice and this permission notice appear in |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 14 | supporting documentation, and that the name of CMU not be |
| 15 | used in advertising or publicity pertaining to distribution of the |
Wes Hardaker | 84bacb0 | 1997-11-07 23:28:59 +0000 | [diff] [blame] | 16 | software without specific, written prior permission. |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 17 | |
| 18 | CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING |
| 19 | ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL |
| 20 | CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR |
| 21 | ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 22 | WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, |
| 23 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
| 24 | SOFTWARE. |
| 25 | ******************************************************************/ |
Wes Hardaker | f86050d | 1995-09-29 20:44:13 +0000 | [diff] [blame] | 26 | #include <config.h> |
| 27 | |
Wes Hardaker | 238b7ba | 1997-01-20 14:09:25 +0000 | [diff] [blame] | 28 | #if HAVE_STDLIB_H |
| 29 | #include <stdlib.h> |
| 30 | #endif |
| 31 | #if HAVE_UNISTD_H |
| 32 | #include <unistd.h> |
| 33 | #endif |
Wes Hardaker | a1a822e | 1998-03-26 15:22:30 +0000 | [diff] [blame] | 34 | #if HAVE_STRING_H |
Wes Hardaker | 238b7ba | 1997-01-20 14:09:25 +0000 | [diff] [blame] | 35 | #include <string.h> |
Wes Hardaker | a1a822e | 1998-03-26 15:22:30 +0000 | [diff] [blame] | 36 | #else |
| 37 | #include <strings.h> |
Wes Hardaker | 238b7ba | 1997-01-20 14:09:25 +0000 | [diff] [blame] | 38 | #endif |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 39 | #include <sys/types.h> |
Wes Hardaker | f86050d | 1995-09-29 20:44:13 +0000 | [diff] [blame] | 40 | #if HAVE_NETINET_IN_H |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 41 | #include <netinet/in.h> |
Wes Hardaker | f86050d | 1995-09-29 20:44:13 +0000 | [diff] [blame] | 42 | #endif |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 43 | #include <stdio.h> |
| 44 | #include <ctype.h> |
Wes Hardaker | f86050d | 1995-09-29 20:44:13 +0000 | [diff] [blame] | 45 | #if TIME_WITH_SYS_TIME |
Wes Hardaker | b5a5a3c | 1997-10-28 00:26:57 +0000 | [diff] [blame] | 46 | # ifdef WIN32 |
| 47 | # include <sys/timeb.h> |
| 48 | # else |
| 49 | # include <sys/time.h> |
| 50 | # endif |
Wes Hardaker | f86050d | 1995-09-29 20:44:13 +0000 | [diff] [blame] | 51 | # include <time.h> |
| 52 | #else |
| 53 | # if HAVE_SYS_TIME_H |
| 54 | # include <sys/time.h> |
| 55 | # else |
| 56 | # include <time.h> |
| 57 | # endif |
| 58 | #endif |
Wes Hardaker | 7a2cb58 | 1997-08-14 14:38:17 +0000 | [diff] [blame] | 59 | #if HAVE_SYS_SELECT_H |
| 60 | #include <sys/select.h> |
| 61 | #endif |
Wes Hardaker | b5a5a3c | 1997-10-28 00:26:57 +0000 | [diff] [blame] | 62 | #if HAVE_WINSOCK_H |
| 63 | #include <winsock.h> |
Dave Shield | 9a2b557 | 1999-05-18 16:23:50 +0000 | [diff] [blame] | 64 | #endif |
| 65 | #if HAVE_NETDB_H |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 66 | #include <netdb.h> |
Wes Hardaker | b5a5a3c | 1997-10-28 00:26:57 +0000 | [diff] [blame] | 67 | #endif |
Wes Hardaker | 238b7ba | 1997-01-20 14:09:25 +0000 | [diff] [blame] | 68 | #if HAVE_ARPA_INET_H |
| 69 | #include <arpa/inet.h> |
| 70 | #endif |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 71 | |
Michael Slifcak | 829bf95 | 2000-04-18 18:08:02 +0000 | [diff] [blame] | 72 | #include <getopt.h> |
| 73 | |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 74 | #include "asn1.h" |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 75 | #include "snmp_api.h" |
Wes Hardaker | 0310a93 | 1998-03-06 22:01:52 +0000 | [diff] [blame] | 76 | #include "snmp_impl.h" |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 77 | #include "snmp_client.h" |
Wes Hardaker | 522909f | 1997-03-05 14:06:41 +0000 | [diff] [blame] | 78 | #include "snmp.h" |
| 79 | #include "mib.h" |
Wes Hardaker | b5a5a3c | 1997-10-28 00:26:57 +0000 | [diff] [blame] | 80 | #include "system.h" |
Wes Hardaker | 191b2c8 | 1997-06-30 12:53:57 +0000 | [diff] [blame] | 81 | #include "snmp_parse_args.h" |
Wes Hardaker | 0918896 | 2000-04-12 16:13:08 +0000 | [diff] [blame] | 82 | #include "default_store.h" |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 83 | |
Wes Hardaker | 4bc17ac | 2000-04-14 22:08:48 +0000 | [diff] [blame] | 84 | #define DS_APP_DONT_FIX_PDUS 0 |
| 85 | |
| 86 | static void optProc(int argc, char *const *argv, int opt) |
| 87 | { |
| 88 | switch (opt) { |
| 89 | case 'C': |
| 90 | while (*optarg) { |
| 91 | switch (*optarg++) { |
| 92 | case 'f': |
| 93 | ds_toggle_boolean(DS_APPLICATION_ID, DS_APP_DONT_FIX_PDUS); |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | break; |
| 98 | } |
| 99 | } |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 100 | |
Wes Hardaker | 40b2bf1 | 1999-01-18 18:47:36 +0000 | [diff] [blame] | 101 | void usage(void) |
Wes Hardaker | 522909f | 1997-03-05 14:06:41 +0000 | [diff] [blame] | 102 | { |
Wes Hardaker | 4bc17ac | 2000-04-14 22:08:48 +0000 | [diff] [blame] | 103 | fprintf(stderr,"Usage: snmpgetnext [-Cf]"); |
Wes Hardaker | 4d0e99d | 1997-07-01 13:02:57 +0000 | [diff] [blame] | 104 | snmp_parse_args_usage(stderr); |
Wes Hardaker | b5a5a3c | 1997-10-28 00:26:57 +0000 | [diff] [blame] | 105 | fprintf(stderr," [<objectID> ...]\n\n"); |
Wes Hardaker | 4d0e99d | 1997-07-01 13:02:57 +0000 | [diff] [blame] | 106 | snmp_parse_args_descriptions(stderr); |
Wes Hardaker | 4bc17ac | 2000-04-14 22:08:48 +0000 | [diff] [blame] | 107 | fprintf(stderr, "snmpgetnext specific options\n"); |
| 108 | fprintf(stderr, " -Cf\t\tDon't fix errors and retry the request.\n"); |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Wes Hardaker | 40b2bf1 | 1999-01-18 18:47:36 +0000 | [diff] [blame] | 111 | int main(int argc, char *argv[]) |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 112 | { |
| 113 | struct snmp_session session, *ss; |
| 114 | struct snmp_pdu *pdu, *response; |
| 115 | struct variable_list *vars; |
Wes Hardaker | b5a5a3c | 1997-10-28 00:26:57 +0000 | [diff] [blame] | 116 | int arg; |
| 117 | int count; |
| 118 | int current_name = 0; |
| 119 | char *names[128]; |
Wes Hardaker | f95d33b | 1999-02-12 00:10:39 +0000 | [diff] [blame] | 120 | oid name[MAX_OID_LEN]; |
Niels Baggesen | 0045e2f | 1999-05-03 22:40:11 +0000 | [diff] [blame] | 121 | size_t name_length; |
Wes Hardaker | b5a5a3c | 1997-10-28 00:26:57 +0000 | [diff] [blame] | 122 | int status; |
| 123 | int failures = 0; |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 124 | |
Wes Hardaker | b5a5a3c | 1997-10-28 00:26:57 +0000 | [diff] [blame] | 125 | /* get the common command line arguments */ |
Wes Hardaker | f74f8d1 | 2000-05-22 23:21:37 +0000 | [diff] [blame] | 126 | if ((arg = snmp_parse_args(argc, argv, &session, "C:", &optProc)) < 0) { |
| 127 | usage(); |
| 128 | exit(1); |
| 129 | } |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 130 | |
Wes Hardaker | ecef027 | 1999-08-19 23:09:54 +0000 | [diff] [blame] | 131 | if (arg >= argc) { |
| 132 | fprintf(stderr, "Missing object name\n"); |
| 133 | usage(); |
| 134 | exit(1); |
| 135 | } |
| 136 | |
Wes Hardaker | b5a5a3c | 1997-10-28 00:26:57 +0000 | [diff] [blame] | 137 | /* get the object names */ |
Wes Hardaker | 191b2c8 | 1997-06-30 12:53:57 +0000 | [diff] [blame] | 138 | for(; arg < argc; arg++) |
| 139 | names[current_name++] = argv[arg]; |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 140 | |
Wes Hardaker | b5a5a3c | 1997-10-28 00:26:57 +0000 | [diff] [blame] | 141 | SOCK_STARTUP; |
| 142 | |
| 143 | /* open an SNMP session */ |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 144 | ss = snmp_open(&session); |
| 145 | if (ss == NULL){ |
Michael Slifcak | 16c2b16 | 1999-10-21 17:41:38 +0000 | [diff] [blame] | 146 | /* diagnose snmp_open errors with the input struct snmp_session pointer */ |
Michael Slifcak | c1a4cd7 | 1999-07-27 16:33:44 +0000 | [diff] [blame] | 147 | snmp_sess_perror("snmpgetnext", &session); |
Wes Hardaker | b5a5a3c | 1997-10-28 00:26:57 +0000 | [diff] [blame] | 148 | SOCK_CLEANUP; |
| 149 | exit(1); |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Wes Hardaker | b5a5a3c | 1997-10-28 00:26:57 +0000 | [diff] [blame] | 152 | /* create PDU for GET request and add object names to request */ |
Wes Hardaker | 3b0cab3 | 1998-03-11 16:06:17 +0000 | [diff] [blame] | 153 | pdu = snmp_pdu_create(SNMP_MSG_GETNEXT); |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 154 | |
| 155 | for(count = 0; count < current_name; count++){ |
Wes Hardaker | f95d33b | 1999-02-12 00:10:39 +0000 | [diff] [blame] | 156 | name_length = MAX_OID_LEN; |
Wes Hardaker | 7f4bb2f | 1997-12-05 19:58:52 +0000 | [diff] [blame] | 157 | if (snmp_parse_oid(names[count], name, &name_length) == NULL) { |
Michael Slifcak | c1a4cd7 | 1999-07-27 16:33:44 +0000 | [diff] [blame] | 158 | snmp_perror(names[count]); |
Wes Hardaker | b5a5a3c | 1997-10-28 00:26:57 +0000 | [diff] [blame] | 159 | failures++; |
| 160 | } else |
| 161 | snmp_add_null_var(pdu, name, name_length); |
| 162 | } |
| 163 | if (failures) { |
| 164 | SOCK_CLEANUP; |
| 165 | exit(1); |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Wes Hardaker | b5a5a3c | 1997-10-28 00:26:57 +0000 | [diff] [blame] | 168 | /* do the request */ |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 169 | retry: |
| 170 | status = snmp_synch_response(ss, pdu, &response); |
| 171 | if (status == STAT_SUCCESS){ |
Wes Hardaker | b5a5a3c | 1997-10-28 00:26:57 +0000 | [diff] [blame] | 172 | if (response->errstat == SNMP_ERR_NOERROR){ |
| 173 | for(vars = response->variables; vars; vars = vars->next_variable) |
| 174 | print_variable(vars->name, vars->name_length, vars); |
| 175 | } else { |
Wes Hardaker | 4e5278b | 1997-10-29 19:20:54 +0000 | [diff] [blame] | 176 | fprintf(stderr, "Error in packet.\nReason: %s\n", |
Wes Hardaker | b5a5a3c | 1997-10-28 00:26:57 +0000 | [diff] [blame] | 177 | snmp_errstring(response->errstat)); |
| 178 | if (response->errstat == SNMP_ERR_NOSUCHNAME){ |
Wes Hardaker | 4e5278b | 1997-10-29 19:20:54 +0000 | [diff] [blame] | 179 | fprintf(stderr, "This name doesn't exist: "); |
Wes Hardaker | b5a5a3c | 1997-10-28 00:26:57 +0000 | [diff] [blame] | 180 | for(count = 1, vars = response->variables; |
| 181 | vars && count != response->errindex; |
| 182 | vars = vars->next_variable, count++) |
| 183 | ; |
| 184 | if (vars) |
Wes Hardaker | 4e5278b | 1997-10-29 19:20:54 +0000 | [diff] [blame] | 185 | fprint_objid(stderr, vars->name, vars->name_length); |
| 186 | fprintf(stderr, "\n"); |
Wes Hardaker | b5a5a3c | 1997-10-28 00:26:57 +0000 | [diff] [blame] | 187 | } |
Michael Slifcak | 55211d2 | 1999-04-14 23:56:37 +0000 | [diff] [blame] | 188 | |
| 189 | /* retry if the errored variable was successfully removed */ |
Wes Hardaker | 4bc17ac | 2000-04-14 22:08:48 +0000 | [diff] [blame] | 190 | if (!ds_get_boolean(DS_APPLICATION_ID, DS_APP_DONT_FIX_PDUS)) { |
Wes Hardaker | 0918896 | 2000-04-12 16:13:08 +0000 | [diff] [blame] | 191 | pdu = snmp_fix_pdu(response, SNMP_MSG_GETNEXT); |
| 192 | snmp_free_pdu(response); |
| 193 | response = NULL; |
| 194 | if (pdu != NULL) |
| 195 | goto retry; |
| 196 | } |
Wes Hardaker | b5a5a3c | 1997-10-28 00:26:57 +0000 | [diff] [blame] | 197 | } |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 198 | } else if (status == STAT_TIMEOUT){ |
Wes Hardaker | d472802 | 1998-04-27 18:00:44 +0000 | [diff] [blame] | 199 | fprintf(stderr, "Timeout: No Response from %s.\n", session.peername); |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 200 | } else { /* status == STAT_ERROR */ |
Wes Hardaker | ca7eabd | 1999-07-23 22:22:02 +0000 | [diff] [blame] | 201 | snmp_sess_perror("snmpgetnext", ss); |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | if (response) |
Wes Hardaker | b5a5a3c | 1997-10-28 00:26:57 +0000 | [diff] [blame] | 205 | snmp_free_pdu(response); |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 206 | snmp_close(ss); |
Wes Hardaker | b5a5a3c | 1997-10-28 00:26:57 +0000 | [diff] [blame] | 207 | SOCK_CLEANUP; |
Niels Baggesen | df5cd02 | 1999-11-10 23:29:18 +0000 | [diff] [blame] | 208 | return 0; |
Wes Hardaker | fca311a | 1995-03-09 00:08:14 +0000 | [diff] [blame] | 209 | } |