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

« back to all changes in this revision

Viewing changes to apps/snmpset.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2004-09-13 12:06:21 UTC
  • Revision ID: james.westby@ubuntu.com-20040913120621-g952ntonlleihcvm
Tags: upstream-5.1.1
ImportĀ upstreamĀ versionĀ 5.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * snmpset.c - send snmp SET requests to a network entity.
 
3
 *
 
4
 */
 
5
/***********************************************************************
 
6
        Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University
 
7
 
 
8
                      All Rights Reserved
 
9
 
 
10
Permission to use, copy, modify, and distribute this software and its 
 
11
documentation for any purpose and without fee is hereby granted, 
 
12
provided that the above copyright notice appear in all copies and that
 
13
both that copyright notice and this permission notice appear in 
 
14
supporting documentation, and that the name of CMU not be
 
15
used in advertising or publicity pertaining to distribution of the
 
16
software without specific, written prior permission.  
 
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
******************************************************************/
 
26
#include <net-snmp/net-snmp-config.h>
 
27
 
 
28
#if HAVE_STDLIB_H
 
29
#include <stdlib.h>
 
30
#endif
 
31
#if HAVE_UNISTD_H
 
32
#include <unistd.h>
 
33
#endif
 
34
#if HAVE_STRING_H
 
35
#include <string.h>
 
36
#else
 
37
#include <strings.h>
 
38
#endif
 
39
#include <sys/types.h>
 
40
#if HAVE_NETINET_IN_H
 
41
#include <netinet/in.h>
 
42
#endif
 
43
#include <stdio.h>
 
44
#include <ctype.h>
 
45
#if TIME_WITH_SYS_TIME
 
46
# ifdef WIN32
 
47
#  include <sys/timeb.h>
 
48
# else
 
49
#  include <sys/time.h>
 
50
# endif
 
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
 
59
#if HAVE_SYS_SELECT_H
 
60
#include <sys/select.h>
 
61
#endif
 
62
#if HAVE_WINSOCK_H
 
63
#include <winsock.h>
 
64
#endif
 
65
#if HAVE_NETDB_H
 
66
#include <netdb.h>
 
67
#endif
 
68
#if HAVE_ARPA_INET_H
 
69
#include <arpa/inet.h>
 
70
#endif
 
71
 
 
72
#include <net-snmp/net-snmp-includes.h>
 
73
 
 
74
int             failures = 0;
 
75
 
 
76
void
 
77
usage(void)
 
78
{
 
79
    fprintf(stderr, "USAGE: snmpset ");
 
80
    snmp_parse_args_usage(stderr);
 
81
    fprintf(stderr, " OID TYPE VALUE [OID TYPE VALUE]...\n\n");
 
82
    snmp_parse_args_descriptions(stderr);
 
83
    fprintf(stderr,
 
84
            "  -C APPOPTS\t\tSet various application specific behaviours:\n");
 
85
    fprintf(stderr, "\t\t\t  q:  don't print results on success\n");
 
86
    fprintf(stderr, "\n  TYPE: one of i, u, t, a, o, s, x, d, b, n\n");
 
87
    fprintf(stderr,
 
88
            "\ti: INTEGER, u: unsigned INTEGER, t: TIMETICKS, a: IPADDRESS\n");
 
89
    fprintf(stderr,
 
90
            "\to: OBJID, s: STRING, x: HEX STRING, d: DECIMAL STRING, b: BITS\n");
 
91
#ifdef OPAQUE_SPECIAL_TYPES
 
92
    fprintf(stderr,
 
93
            "\tU: unsigned int64, I: signed int64, F: float, D: double\n");
 
94
#endif                          /* OPAQUE_SPECIAL_TYPES */
 
95
 
 
96
}
 
97
 
 
98
static int quiet = 0;
 
99
 
 
100
static
 
101
    void
 
102
optProc(int argc, char *const *argv, int opt)
 
103
{
 
104
    switch (opt) {
 
105
    case 'C':
 
106
        while (*optarg) {
 
107
            switch (*optarg++) {
 
108
            case 'q':
 
109
                quiet = 1;
 
110
                break;
 
111
 
 
112
            default:
 
113
                fprintf(stderr, "Unknown flag passed to -C: %c\n",
 
114
                        optarg[-1]);
 
115
                exit(1);
 
116
            }
 
117
        }
 
118
    }
 
119
}
 
120
 
 
121
int
 
122
main(int argc, char *argv[])
 
123
{
 
124
    netsnmp_session session, *ss;
 
125
    netsnmp_pdu    *pdu, *response = NULL;
 
126
    netsnmp_variable_list *vars;
 
127
    int             arg;
 
128
    int             count;
 
129
    int             current_name = 0;
 
130
    int             current_type = 0;
 
131
    int             current_value = 0;
 
132
    char           *names[SNMP_MAX_CMDLINE_OIDS];
 
133
    char            types[SNMP_MAX_CMDLINE_OIDS];
 
134
    char           *values[SNMP_MAX_CMDLINE_OIDS];
 
135
    oid             name[MAX_OID_LEN];
 
136
    size_t          name_length;
 
137
    int             status;
 
138
    int             exitval = 0;
 
139
 
 
140
    putenv(strdup("POSIXLY_CORRECT=1"));
 
141
 
 
142
    /*
 
143
     * get the common command line arguments 
 
144
     */
 
145
    switch (arg = snmp_parse_args(argc, argv, &session, "C:", optProc)) {
 
146
    case -2:
 
147
        exit(0);
 
148
    case -1:
 
149
        usage();
 
150
        exit(1);
 
151
    default:
 
152
        break;
 
153
    }
 
154
 
 
155
    if (arg >= argc) {
 
156
        fprintf(stderr, "Missing object name\n");
 
157
        usage();
 
158
        exit(1);
 
159
    }
 
160
    if ((argc - arg) > 3*SNMP_MAX_CMDLINE_OIDS) {
 
161
        fprintf(stderr, "Too many assignments specified. ");
 
162
        fprintf(stderr, "Only %d allowed in one request.\n", SNMP_MAX_CMDLINE_OIDS);
 
163
        usage();
 
164
        exit(1);
 
165
    }
 
166
 
 
167
    /*
 
168
     * get object names, types, and values 
 
169
     */
 
170
    for (; arg < argc; arg++) {
 
171
        DEBUGMSGTL(("snmp_parse_args", "handling (#%d): %s %s %s\n",
 
172
                    arg,argv[arg], arg+1 < argc ? argv[arg+1] : NULL,
 
173
                    arg+2 < argc ? argv[arg+2] : NULL));
 
174
        names[current_name++] = argv[arg++];
 
175
        if (arg < argc) {
 
176
            switch (*argv[arg]) {
 
177
            case '=':
 
178
            case 'i':
 
179
            case 'u':
 
180
            case 't':
 
181
            case 'a':
 
182
            case 'o':
 
183
            case 's':
 
184
            case 'x':
 
185
            case 'd':
 
186
            case 'b':
 
187
#ifdef OPAQUE_SPECIAL_TYPES
 
188
            case 'I':
 
189
            case 'U':
 
190
            case 'F':
 
191
            case 'D':
 
192
#endif                          /* OPAQUE_SPECIAL_TYPES */
 
193
                types[current_type++] = *argv[arg++];
 
194
                break;
 
195
            default:
 
196
                fprintf(stderr, "%s: Bad object type: %c\n", argv[arg - 1],
 
197
                        *argv[arg]);
 
198
                exit(1);
 
199
            }
 
200
        } else {
 
201
            fprintf(stderr, "%s: Needs type and value\n", argv[arg - 1]);
 
202
            exit(1);
 
203
        }
 
204
        if (arg < argc)
 
205
            values[current_value++] = argv[arg];
 
206
        else {
 
207
            fprintf(stderr, "%s: Needs value\n", argv[arg - 2]);
 
208
            exit(1);
 
209
        }
 
210
    }
 
211
 
 
212
    SOCK_STARTUP;
 
213
 
 
214
    /*
 
215
     * open an SNMP session 
 
216
     */
 
217
    ss = snmp_open(&session);
 
218
    if (ss == NULL) {
 
219
        /*
 
220
         * diagnose snmp_open errors with the input netsnmp_session pointer 
 
221
         */
 
222
        snmp_sess_perror("snmpset", &session);
 
223
        SOCK_CLEANUP;
 
224
        exit(1);
 
225
    }
 
226
 
 
227
    /*
 
228
     * create PDU for SET request and add object names and values to request 
 
229
     */
 
230
    pdu = snmp_pdu_create(SNMP_MSG_SET);
 
231
    for (count = 0; count < current_name; count++) {
 
232
        name_length = MAX_OID_LEN;
 
233
        if (snmp_parse_oid(names[count], name, &name_length) == NULL) {
 
234
            snmp_perror(names[count]);
 
235
            failures++;
 
236
        } else
 
237
            if (snmp_add_var
 
238
                (pdu, name, name_length, types[count], values[count])) {
 
239
            snmp_perror(names[count]);
 
240
            failures++;
 
241
        }
 
242
    }
 
243
 
 
244
    if (failures) {
 
245
        SOCK_CLEANUP;
 
246
        exit(1);
 
247
    }
 
248
 
 
249
    /*
 
250
     * do the request 
 
251
     */
 
252
    status = snmp_synch_response(ss, pdu, &response);
 
253
    if (status == STAT_SUCCESS) {
 
254
        if (response->errstat == SNMP_ERR_NOERROR) {
 
255
            if (!quiet) {
 
256
                for (vars = response->variables; vars;
 
257
                     vars = vars->next_variable)
 
258
                    print_variable(vars->name, vars->name_length, vars);
 
259
            }
 
260
        } else {
 
261
            fprintf(stderr, "Error in packet.\nReason: %s\n",
 
262
                    snmp_errstring(response->errstat));
 
263
            if (response->errindex != 0) {
 
264
                fprintf(stderr, "Failed object: ");
 
265
                for (count = 1, vars = response->variables;
 
266
                     vars && (count != response->errindex);
 
267
                     vars = vars->next_variable, count++);
 
268
                if (vars)
 
269
                    fprint_objid(stderr, vars->name, vars->name_length);
 
270
                fprintf(stderr, "\n");
 
271
            }
 
272
            exitval = 2;
 
273
        }
 
274
    } else if (status == STAT_TIMEOUT) {
 
275
        fprintf(stderr, "Timeout: No Response from %s\n",
 
276
                session.peername);
 
277
        exitval = 1;
 
278
    } else {                    /* status == STAT_ERROR */
 
279
        snmp_sess_perror("snmpset", ss);
 
280
        exitval = 1;
 
281
    }
 
282
 
 
283
    if (response)
 
284
        snmp_free_pdu(response);
 
285
    snmp_close(ss);
 
286
    SOCK_CLEANUP;
 
287
    return exitval;
 
288
}