Coverage Report

Created: 2026-07-16 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/net-snmp/snmplib/snmp_client.c
Line
Count
Source
1
/*
2
 * snmp_client.c - a toolkit of common functions for an SNMP client.
3
 *
4
 */
5
/* Portions of this file are subject to the following copyright(s).  See
6
 * the Net-SNMP's COPYING file for more details and other copyrights
7
 * that may apply:
8
 */
9
/**********************************************************************
10
  Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University
11
12
                      All Rights Reserved
13
14
Permission to use, copy, modify, and distribute this software and its
15
documentation for any purpose and without fee is hereby granted,
16
provided that the above copyright notice appear in all copies and that
17
both that copyright notice and this permission notice appear in
18
supporting documentation, and that the name of CMU not be
19
used in advertising or publicity pertaining to distribution of the
20
software without specific, written prior permission.
21
22
CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
23
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
24
CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
25
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
26
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
27
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
28
SOFTWARE.
29
******************************************************************/
30
/*
31
 * Portions of this file are copyrighted by:
32
 * Copyright © 2003 Sun Microsystems, Inc. All rights reserved.
33
 * Use is subject to license terms specified in the COPYING file
34
 * distributed with the Net-SNMP package.
35
 *
36
 * Portions of this file are copyrighted by:
37
 * Copyright (c) 2016 VMware, Inc. All rights reserved.
38
 * Use is subject to license terms specified in the COPYING file
39
 * distributed with the Net-SNMP package.
40
 */
41
42
/** @defgroup snmp_client various PDU processing routines
43
 *  @ingroup library
44
 * 
45
 *  @{
46
 */
47
#include <net-snmp/net-snmp-config.h>
48
#include <net-snmp/net-snmp-features.h>
49
50
#include <stdio.h>
51
#include <errno.h>
52
#ifdef HAVE_INTTYPES_H
53
#include <inttypes.h>
54
#endif
55
#ifdef HAVE_STDLIB_H
56
#include <stdlib.h>
57
#endif
58
#ifdef HAVE_STRING_H
59
#include <string.h>
60
#else
61
#include <strings.h>
62
#endif
63
#ifdef HAVE_UNISTD_H
64
#include <unistd.h>
65
#endif
66
#include <sys/types.h>
67
#ifdef TIME_WITH_SYS_TIME
68
# include <sys/time.h>
69
# include <time.h>
70
#else
71
# ifdef HAVE_SYS_TIME_H
72
#  include <sys/time.h>
73
# else
74
#  include <time.h>
75
# endif
76
#endif
77
#ifdef HAVE_SYS_PARAM_H
78
#include <sys/param.h>
79
#endif
80
#ifdef HAVE_NETINET_IN_H
81
#include <netinet/in.h>
82
#endif
83
#ifdef HAVE_ARPA_INET_H
84
#include <arpa/inet.h>
85
#endif
86
#ifdef HAVE_SYS_SELECT_H
87
#include <sys/select.h>
88
#endif
89
#ifdef HAVE_SYSLOG_H
90
#include <syslog.h>
91
#endif
92
93
#include <net-snmp/types.h>
94
95
#include <net-snmp/agent/ds_agent.h>
96
#include <net-snmp/library/default_store.h>
97
#include <net-snmp/library/snmp.h>
98
#include <net-snmp/library/snmp-tc.h>
99
#include <net-snmp/library/snmp_api.h>
100
#include <net-snmp/library/snmp_client.h>
101
#include <net-snmp/library/snmp_impl.h>
102
#include <net-snmp/library/snmp_secmod.h>
103
#include <net-snmp/library/snmpusm.h>
104
#include <net-snmp/library/mib.h>
105
#include <net-snmp/library/snmp_logging.h>
106
#include <net-snmp/library/snmp_assert.h>
107
#include <net-snmp/library/large_fd_set.h>
108
#include <net-snmp/library/tools.h>
109
#include <net-snmp/pdu_api.h>
110
111
netsnmp_feature_child_of(snmp_client_all, libnetsnmp);
112
113
netsnmp_feature_child_of(snmp_split_pdu, snmp_client_all);
114
netsnmp_feature_child_of(snmp_reset_var_types, snmp_client_all);
115
netsnmp_feature_child_of(query_set_default_session, snmp_client_all);
116
netsnmp_feature_child_of(row_create, snmp_client_all);
117
118
#ifndef BSD4_3
119
#define BSD4_2
120
#endif
121
122
123
/*
124
 * Prototype definitions 
125
 */
126
static int      snmp_synch_input(int op, netsnmp_session * session,
127
                                 int reqid, netsnmp_pdu *pdu, void *magic);
128
129
netsnmp_pdu    *
130
snmp_pdu_create(int command)
131
0
{
132
0
    netsnmp_pdu    *pdu;
133
134
0
    pdu = calloc(1, sizeof(netsnmp_pdu));
135
0
    if (pdu) {
136
0
        pdu->version = SNMP_DEFAULT_VERSION;
137
0
        pdu->command = command;
138
0
        pdu->errstat = SNMP_DEFAULT_ERRSTAT;
139
0
        pdu->errindex = SNMP_DEFAULT_ERRINDEX;
140
0
        pdu->securityModel = SNMP_DEFAULT_SECMODEL;
141
0
        pdu->transport_data = NULL;
142
0
        pdu->transport_data_length = 0;
143
0
        pdu->securityNameLen = 0;
144
0
        pdu->contextNameLen = 0;
145
0
        pdu->time = 0;
146
0
        pdu->reqid = snmp_get_next_reqid();
147
0
        pdu->msgid = snmp_get_next_msgid();
148
0
    }
149
0
    return pdu;
150
151
0
}
152
153
154
/*
155
 * Add a null variable with the requested name to the end of the list of
156
 * variables for this pdu.
157
 */
158
netsnmp_variable_list *
159
snmp_add_null_var(netsnmp_pdu *pdu, const oid * name, size_t name_length)
160
0
{
161
0
    return snmp_pdu_add_variable(pdu, name, name_length, ASN_NULL, NULL, 0);
162
0
}
163
164
165
#include <net-snmp/library/snmp_debug.h>
166
static int
167
snmp_synch_input(int op,
168
                 netsnmp_session * session,
169
                 int reqid, netsnmp_pdu *pdu, void *magic)
170
0
{
171
0
    struct synch_state *state = (struct synch_state *) magic;
172
0
    int             rpt_type;
173
174
0
    if (reqid != state->reqid && pdu && pdu->command != SNMP_MSG_REPORT) {
175
0
        DEBUGMSGTL(("snmp_synch", "Unexpected response (ReqID: %d,%d - Cmd %d)\n",
176
0
                                   reqid, state->reqid, pdu->command ));
177
0
        return 0;
178
0
    }
179
180
0
    state->waiting = 0;
181
0
    DEBUGMSGTL(("snmp_synch", "Response (ReqID: %d - Cmd %d)\n",
182
0
                               reqid, (pdu ? pdu->command : -1)));
183
184
0
    if (op == NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE && pdu) {
185
0
        if (pdu->command == SNMP_MSG_REPORT) {
186
0
            rpt_type = snmpv3_get_report_type(pdu);
187
0
            if (SNMPV3_IGNORE_UNAUTH_REPORTS ||
188
0
                rpt_type == SNMPERR_NOT_IN_TIME_WINDOW) {
189
0
                state->waiting = 1;
190
0
            }
191
0
            state->pdu = NULL;
192
0
            state->status = STAT_ERROR;
193
0
            session->s_snmp_errno = rpt_type;
194
0
            SET_SNMP_ERROR(rpt_type);
195
0
        } else if (pdu->command == SNMP_MSG_RESPONSE) {
196
            /*
197
             * clone the pdu to return to snmp_synch_response 
198
             */
199
0
            state->pdu = snmp_clone_pdu(pdu);
200
0
            state->status = STAT_SUCCESS;
201
0
            session->s_snmp_errno = SNMPERR_SUCCESS;
202
0
        }
203
0
        else {
204
0
            char msg_buf[50];
205
0
            state->status = STAT_ERROR;
206
0
            session->s_snmp_errno = SNMPERR_PROTOCOL;
207
0
            SET_SNMP_ERROR(SNMPERR_PROTOCOL);
208
0
            snprintf(msg_buf, sizeof(msg_buf), "Expected RESPONSE-PDU but got %s-PDU",
209
0
                     snmp_pdu_type(pdu->command));
210
0
            snmp_set_detail(msg_buf);
211
0
            return 0;
212
0
        }
213
0
    } else if (op == NETSNMP_CALLBACK_OP_TIMED_OUT) {
214
0
        state->pdu = NULL;
215
0
        state->status = STAT_TIMEOUT;
216
0
        session->s_snmp_errno = SNMPERR_TIMEOUT;
217
0
        SET_SNMP_ERROR(SNMPERR_TIMEOUT);
218
0
    } else if (op == NETSNMP_CALLBACK_OP_SEC_ERROR) {
219
0
        state->pdu = NULL;
220
        /*
221
         * If we already have an error in status, then leave it alone.
222
         */
223
0
        if (state->status == STAT_SUCCESS) {
224
0
            state->status = STAT_ERROR;
225
0
            session->s_snmp_errno = SNMPERR_GENERR;
226
0
            SET_SNMP_ERROR(SNMPERR_GENERR);
227
0
        }
228
0
    } else if (op == NETSNMP_CALLBACK_OP_DISCONNECT) {
229
0
        state->pdu = NULL;
230
0
        state->status = STAT_ERROR;
231
0
        session->s_snmp_errno = SNMPERR_ABORT;
232
0
        SET_SNMP_ERROR(SNMPERR_ABORT);
233
0
    }
234
0
    DEBUGMSGTL(("snmp_synch", "status = %d errno = %d\n",
235
0
                               state->status, session->s_snmp_errno));
236
237
0
    return 1;
238
0
}
239
240
241
/*
242
 * Clone an SNMP variable data structure.
243
 * Sets pointers to structure private storage, or
244
 * allocates larger object identifiers and values as needed.
245
 *
246
 * Caller must make list association for cloned variable.
247
 *
248
 * Returns 0 if successful.
249
 */
250
int
251
snmp_clone_var(netsnmp_variable_list * var, netsnmp_variable_list * newvar)
252
0
{
253
0
    if (!newvar || !var)
254
0
        return 1;
255
256
0
    memmove(newvar, var, sizeof(netsnmp_variable_list));
257
0
    newvar->next_variable = NULL;
258
0
    newvar->name = NULL;
259
0
    newvar->val.string = NULL;
260
0
    newvar->data = NULL;
261
0
    newvar->dataFreeHook = NULL;
262
0
    newvar->index = 0;
263
264
    /*
265
     * Clone the object identifier and the value.
266
     * Allocate memory iff original will not fit into local storage.
267
     */
268
0
    if (snmp_set_var_objid(newvar, var->name, var->name_length))
269
0
        return 1;
270
271
    /*
272
     * need a pointer to copy a string value. 
273
     */
274
0
    if (var->val.string) {
275
0
        if (var->val.string != &var->buf[0]) {
276
0
            if (var->val_len <= sizeof(var->buf))
277
0
                newvar->val.string = newvar->buf;
278
0
            else {
279
0
                newvar->val.string = (u_char *) malloc(var->val_len);
280
0
                if (!newvar->val.string)
281
0
                    return 1;
282
0
            }
283
0
            memmove(newvar->val.string, var->val.string, var->val_len);
284
0
        } else {                /* fix the pointer to new local store */
285
0
            newvar->val.string = newvar->buf;
286
            /*
287
             * no need for a memmove, since we copied the whole var
288
             * struct (and thus var->buf) at the beginning of this function.
289
             */
290
0
        }
291
0
    } else {
292
0
        newvar->val.string = NULL;
293
0
        newvar->val_len = 0;
294
0
    }
295
296
0
    return 0;
297
0
}
298
299
300
/*
301
 * Possibly make a copy of source memory buffer.
302
 * Will reset destination pointer if source pointer is NULL.
303
 * Returns 0 if successful, 1 if memory allocation fails.
304
 */
305
int
306
snmp_clone_mem(void **dstPtr, const void *srcPtr, unsigned len)
307
0
{
308
0
    *dstPtr = NULL;
309
0
    if (srcPtr) {
310
0
        *dstPtr = malloc(len + 1);
311
0
        if (!*dstPtr) {
312
0
            return 1;
313
0
        }
314
0
        memmove(*dstPtr, srcPtr, len);
315
        /*
316
         * this is for those routines that expect 0-terminated strings!!!
317
         * someone should rather have called strdup
318
         */
319
0
        ((char *) *dstPtr)[len] = 0;
320
0
    }
321
0
    return 0;
322
0
}
323
324
325
/*
326
 * Walks through a list of varbinds and frees and allocated memory,
327
 * restoring pointers to local buffers
328
 */
329
void
330
snmp_reset_var_buffers(netsnmp_variable_list * var)
331
0
{
332
0
    while (var) {
333
0
        if (var->name != var->name_loc) {
334
0
            if(NULL != var->name)
335
0
                free(var->name);
336
0
            var->name = var->name_loc;
337
0
            var->name_length = 0;
338
0
        }
339
0
        if (var->val.string != var->buf) {
340
0
            if (NULL != var->val.string)
341
0
                free(var->val.string);
342
0
            var->val.string = var->buf;
343
0
            var->val_len = 0;
344
0
        }
345
0
        var = var->next_variable;
346
0
    }
347
0
}
348
349
/*
350
 * Creates and allocates a clone of the input PDU,
351
 * but does NOT copy the variables.
352
 * This function should be used with another function,
353
 * such as _copy_pdu_vars.
354
 *
355
 * Returns a pointer to the cloned PDU if successful.
356
 * Returns 0 if failure.
357
 */
358
static
359
netsnmp_pdu    *
360
_clone_pdu_header(netsnmp_pdu *pdu)
361
0
{
362
0
    netsnmp_pdu    *newpdu;
363
0
    struct snmp_secmod_def *sptr;
364
0
    int ret;
365
366
0
    if (!pdu)
367
0
        return NULL;
368
369
0
    newpdu = netsnmp_memdup(pdu, sizeof(netsnmp_pdu));
370
0
    if (!newpdu)
371
0
        return NULL;
372
373
    /*
374
     * reset copied pointers if copy fails 
375
     */
376
0
    newpdu->variables = NULL;
377
0
    newpdu->enterprise = NULL;
378
0
    newpdu->community = NULL;
379
0
    newpdu->securityEngineID = NULL;
380
0
    newpdu->securityName = NULL;
381
0
    newpdu->contextEngineID = NULL;
382
0
    newpdu->contextName = NULL;
383
0
    newpdu->transport_data = NULL;
384
0
    newpdu->securityStateRef = NULL;
385
386
    /*
387
     * copy buffers individually. If any copy fails, all are freed. 
388
     */
389
0
    if (snmp_clone_mem((void **) &newpdu->enterprise, pdu->enterprise,
390
0
                       sizeof(oid) * pdu->enterprise_length) ||
391
0
        snmp_clone_mem((void **) &newpdu->community, pdu->community,
392
0
                       pdu->community_len) ||
393
0
        snmp_clone_mem((void **) &newpdu->contextEngineID,
394
0
                       pdu->contextEngineID, pdu->contextEngineIDLen)
395
0
        || snmp_clone_mem((void **) &newpdu->securityEngineID,
396
0
                          pdu->securityEngineID, pdu->securityEngineIDLen)
397
0
        || snmp_clone_mem((void **) &newpdu->contextName, pdu->contextName,
398
0
                          pdu->contextNameLen)
399
0
        || snmp_clone_mem((void **) &newpdu->securityName,
400
0
                          pdu->securityName, pdu->securityNameLen)
401
0
        || snmp_clone_mem((void **) &newpdu->transport_data,
402
0
                          pdu->transport_data,
403
0
                          pdu->transport_data_length)) {
404
0
        snmp_free_pdu(newpdu);
405
0
        return NULL;
406
0
    }
407
408
0
    sptr = find_sec_mod(newpdu->securityModel);
409
0
    if (sptr && sptr->pdu_clone) {
410
        /* call security model if it needs to know about this */
411
0
        ret = sptr->pdu_clone(pdu, newpdu);
412
0
        if (ret) {
413
0
            snmp_free_pdu(newpdu);
414
0
            return NULL;
415
0
        }
416
0
    }
417
418
0
    return newpdu;
419
0
}
420
421
static
422
netsnmp_variable_list *
423
_copy_varlist(netsnmp_variable_list * var,      /* source varList */
424
              int errindex,     /* index of variable to drop (if any) */
425
              int copy_count)
426
0
{                               /* !=0 number variables to copy */
427
0
    netsnmp_variable_list *newhead, *newvar, *oldvar;
428
0
    int             ii = 0;
429
430
0
    newhead = NULL;
431
0
    oldvar = NULL;
432
433
0
    while (var && (copy_count-- > 0)) {
434
        /*
435
         * Drop the specified variable (if applicable) 
436
         * xxx hmm, is it intentional that dropping the errindex
437
         *     counts towards copy_count?
438
         */
439
0
        if (++ii == errindex) {
440
0
            var = var->next_variable;
441
0
            continue;
442
0
        }
443
444
        /*
445
         * clone the next variable. Cleanup if alloc fails 
446
         */
447
0
        newvar = (netsnmp_variable_list *)
448
0
            malloc(sizeof(netsnmp_variable_list));
449
0
        if (snmp_clone_var(var, newvar)) {
450
0
            if (newvar)
451
0
                free(newvar);
452
0
            snmp_free_varbind(newhead);
453
0
            return NULL;
454
0
        }
455
456
        /*
457
         * add cloned variable to new list  
458
         */
459
0
        if (NULL == newhead)
460
0
            newhead = newvar;
461
0
        if (oldvar)
462
0
            oldvar->next_variable = newvar;
463
0
        oldvar = newvar;
464
465
0
        var = var->next_variable;
466
0
    }
467
0
    return newhead;
468
0
}
469
470
471
/*
472
 * Copy some or all variables from source PDU to target PDU.
473
 * This function consolidates many of the needs of PDU variables:
474
 * Clone PDU : copy all the variables.
475
 * Split PDU : skip over some variables to copy other variables.
476
 * Fix PDU   : remove variable associated with error index.
477
 *
478
 * Designed to work with _clone_pdu_header.
479
 *
480
 * If drop_err is set, drop any variable associated with errindex.
481
 * If skip_count is set, skip the number of variable in pdu's list.
482
 * While copy_count is greater than zero, copy pdu variables to newpdu.
483
 *
484
 * If an error occurs, newpdu is freed and pointer is set to 0.
485
 *
486
 * Returns a pointer to the cloned PDU if successful.
487
 * Returns 0 if failure.
488
 */
489
static
490
netsnmp_pdu    *
491
_copy_pdu_vars(netsnmp_pdu *pdu,        /* source PDU */
492
               netsnmp_pdu *newpdu,     /* target PDU */
493
               int drop_err,    /* !=0 drop errored variable */
494
               int skip_count,  /* !=0 number of variables to skip */
495
               int copy_count)
496
0
{                               /* !=0 number of variables to copy */
497
0
    netsnmp_variable_list *var;
498
#ifdef TEMPORARILY_DISABLED
499
    int             copied;
500
#endif
501
0
    int             drop_idx;
502
503
0
    if (!newpdu)
504
0
        return NULL;            /* where is PDU to copy to ? */
505
506
0
    if (drop_err)
507
0
        drop_idx = pdu->errindex - skip_count;
508
0
    else
509
0
        drop_idx = 0;
510
511
0
    var = pdu->variables;
512
0
    while (var && (skip_count-- > 0))   /* skip over pdu variables */
513
0
        var = var->next_variable;
514
515
#ifdef TEMPORARILY_DISABLED
516
    copied = 0;
517
    if (pdu->flags & UCD_MSG_FLAG_FORCE_PDU_COPY)
518
        copied = 1;             /* We're interested in 'empty' responses too */
519
#endif
520
521
0
    newpdu->variables = _copy_varlist(var, drop_idx, copy_count);
522
#ifdef TEMPORARILY_DISABLED
523
    if (newpdu->variables)
524
        copied = 1;
525
#endif
526
527
#ifdef ALSO_TEMPORARILY_DISABLED
528
    /*
529
     * Error if bad errindex or if target PDU has no variables copied 
530
     */
531
    if ((drop_err && (ii < pdu->errindex))
532
#ifdef TEMPORARILY_DISABLED
533
        /*
534
         * SNMPv3 engineID probes are allowed to be empty.
535
         * See the comment in snmp_api.c for further details 
536
         */
537
        || copied == 0
538
#endif
539
        ) {
540
        snmp_free_pdu(newpdu);
541
        return 0;
542
    }
543
#endif
544
0
    return newpdu;
545
0
}
546
547
548
/*
549
 * Creates (allocates and copies) a clone of the input PDU.
550
 * If drop_err is set, don't copy any variable associated with errindex.
551
 * This function is called by snmp_clone_pdu and snmp_fix_pdu.
552
 *
553
 * Returns a pointer to the cloned PDU if successful.
554
 * Returns 0 if failure.
555
 */
556
static
557
netsnmp_pdu    *
558
_clone_pdu(netsnmp_pdu *pdu, int drop_err)
559
0
{
560
0
    netsnmp_pdu    *newpdu;
561
562
0
    newpdu = _clone_pdu_header(pdu);
563
0
    if (!newpdu)
564
0
        return newpdu;
565
0
    newpdu = _copy_pdu_vars(pdu, newpdu, drop_err, 0, 10000);   /* skip none, copy all */
566
567
0
    return newpdu;
568
0
}
569
570
571
/*
572
 * This function will clone a full varbind list
573
 *
574
 * Returns a pointer to the cloned varbind list if successful.
575
 * Returns 0 if failure
576
 */
577
netsnmp_variable_list *
578
snmp_clone_varbind(netsnmp_variable_list * varlist)
579
0
{
580
0
    return _copy_varlist(varlist, 0, 10000);    /* skip none, copy all */
581
0
}
582
583
/*
584
 * This function will clone a PDU including all of its variables.
585
 *
586
 * Returns a pointer to the cloned PDU if successful.
587
 * Returns 0 if failure
588
 */
589
netsnmp_pdu    *
590
snmp_clone_pdu(netsnmp_pdu *pdu)
591
0
{
592
0
    return _clone_pdu(pdu, 0);  /* copies all variables */
593
0
}
594
595
596
/*
597
 * This function will clone a PDU including some of its variables.
598
 *
599
 * If skip_count is not zero, it defines the number of variables to skip.
600
 * If copy_count is not zero, it defines the number of variables to copy.
601
 *
602
 * Returns a pointer to the cloned PDU if successful.
603
 * Returns 0 if failure.
604
 */
605
#ifndef NETSNMP_FEATURE_REMOVE_SNMP_SPLIT_PDU
606
netsnmp_pdu    *
607
snmp_split_pdu(netsnmp_pdu *pdu, int skip_count, int copy_count)
608
0
{
609
0
    netsnmp_pdu    *newpdu;
610
611
0
    newpdu = _clone_pdu_header(pdu);
612
0
    if (!newpdu)
613
0
        return newpdu;
614
0
    newpdu = _copy_pdu_vars(pdu, newpdu, 0,     /* don't drop any variables */
615
0
                            skip_count, copy_count);
616
617
0
    return newpdu;
618
0
}
619
#endif /* NETSNMP_FEATURE_REMOVE_SNMP_SPLIT_PDU */
620
621
622
/*
623
 * If there was an error in the input pdu, creates a clone of the pdu
624
 * that includes all the variables except the one marked by the errindex.
625
 * The command is set to the input command and the reqid, errstat, and
626
 * errindex are set to default values.
627
 * If the error status didn't indicate an error, the error index didn't
628
 * indicate a variable, the pdu wasn't a get response message, the
629
 * marked variable was not present in the initial request, or there
630
 * would be no remaining variables, this function will return 0.
631
 * If everything was successful, a pointer to the fixed cloned pdu will
632
 * be returned.
633
 */
634
netsnmp_pdu    *
635
snmp_fix_pdu(netsnmp_pdu *pdu, int command)
636
0
{
637
0
    netsnmp_pdu    *newpdu;
638
639
0
    if ((pdu->command != SNMP_MSG_RESPONSE)
640
0
        || (pdu->errstat == SNMP_ERR_NOERROR)
641
0
        || (NULL == pdu->variables)
642
0
        || (pdu->errindex > (int)snmp_varbind_len(pdu))
643
0
        || (pdu->errindex <= 0)) {
644
0
        return NULL;            /* pre-condition tests fail */
645
0
    }
646
647
0
    newpdu = _clone_pdu(pdu, 1);        /* copies all except errored variable */
648
0
    if (!newpdu)
649
0
        return NULL;
650
0
    if (!newpdu->variables) {
651
0
        snmp_free_pdu(newpdu);
652
0
        return NULL;            /* no variables. "should not happen" */
653
0
    }
654
0
    newpdu->command = command;
655
0
    newpdu->reqid = snmp_get_next_reqid();
656
0
    newpdu->msgid = snmp_get_next_msgid();
657
0
    newpdu->errstat = SNMP_DEFAULT_ERRSTAT;
658
0
    newpdu->errindex = SNMP_DEFAULT_ERRINDEX;
659
660
0
    return newpdu;
661
0
}
662
663
664
/*
665
 * Returns the number of variables bound to a PDU structure
666
 */
667
unsigned long
668
snmp_varbind_len(netsnmp_pdu *pdu)
669
0
{
670
0
    register netsnmp_variable_list *vars;
671
0
    unsigned long   retVal = 0;
672
0
    if (pdu)
673
0
        for (vars = pdu->variables; vars; vars = vars->next_variable) {
674
0
            retVal++;
675
0
        }
676
677
0
    return retVal;
678
0
}
679
680
/*
681
 * Add object identifier name to SNMP variable.
682
 * If the name is large, additional memory is allocated.
683
 * Returns 0 if successful.
684
 */
685
686
int
687
snmp_set_var_objid(netsnmp_variable_list * vp,
688
                   const oid * objid, size_t name_length)
689
0
{
690
0
    size_t          len = sizeof(oid) * name_length;
691
692
0
    if (vp->name != vp->name_loc && vp->name != NULL) {
693
        /*
694
         * Probably previously-allocated "big storage".  Better free it
695
         * else memory leaks possible.  
696
         */
697
0
        free(vp->name);
698
0
    }
699
700
    /*
701
     * use built-in storage for smaller values 
702
     */
703
0
    if (len <= sizeof(vp->name_loc)) {
704
0
        vp->name = vp->name_loc;
705
0
    } else {
706
0
        vp->name = (oid *) malloc(len);
707
0
        if (!vp->name)
708
0
            return 1;
709
0
    }
710
0
    if (objid)
711
0
        memmove(vp->name, objid, len);
712
0
    vp->name_length = name_length;
713
0
    return 0;
714
0
}
715
716
/**
717
 * snmp_set_var_typed_value is used to set data into the netsnmp_variable_list
718
 * structure.  Used to return data to the snmp request via the
719
 * netsnmp_request_info structure's requestvb pointer.
720
 *
721
 * @param newvar   the structure gets populated with the given data, type,
722
 *                 val_str, and val_len.
723
 * @param type     is the asn data type to be copied
724
 * @param val_str  is a buffer containing the value to be copied into the
725
 *                 newvar structure. 
726
 * @param val_len  the length of val_str
727
 * 
728
 * @return returns 0 on success and 1 on a malloc error
729
 */
730
731
int
732
snmp_set_var_typed_value(netsnmp_variable_list * newvar, u_char type,
733
                         const void * val_str, size_t val_len)
734
0
{
735
0
    newvar->type = type;
736
0
    return snmp_set_var_value(newvar, val_str, val_len);
737
0
}
738
739
int
740
snmp_set_var_typed_integer(netsnmp_variable_list * newvar,
741
                           u_char type, long val)
742
0
{
743
0
    newvar->type = type;
744
0
    return snmp_set_var_value(newvar, &val, sizeof(long));
745
0
}
746
747
int
748
count_varbinds(netsnmp_variable_list * var_ptr)
749
0
{
750
0
    int             count = 0;
751
752
0
    for (; var_ptr != NULL; var_ptr = var_ptr->next_variable)
753
0
        count++;
754
755
0
    return count;
756
0
}
757
758
netsnmp_feature_child_of(count_varbinds_of_type, netsnmp_unused);
759
#ifndef NETSNMP_FEATURE_REMOVE_COUNT_VARBINDS_OF_TYPE
760
int
761
count_varbinds_of_type(netsnmp_variable_list * var_ptr, u_char type)
762
0
{
763
0
    int             count = 0;
764
765
0
    for (; var_ptr != NULL; var_ptr = var_ptr->next_variable)
766
0
        if (var_ptr->type == type)
767
0
            count++;
768
769
0
    return count;
770
0
}
771
#endif /* NETSNMP_FEATURE_REMOVE_COUNT_VARBINDS_OF_TYPE */
772
773
netsnmp_feature_child_of(find_varind_of_type, netsnmp_unused);
774
#ifndef NETSNMP_FEATURE_REMOVE_FIND_VARIND_OF_TYPE
775
netsnmp_variable_list *
776
find_varbind_of_type(netsnmp_variable_list * var_ptr, u_char type)
777
0
{
778
0
    for (; var_ptr != NULL && var_ptr->type != type;
779
0
         var_ptr = var_ptr->next_variable);
780
781
0
    return var_ptr;
782
0
}
783
#endif /* NETSNMP_FEATURE_REMOVE_FIND_VARIND_OF_TYPE */
784
785
netsnmp_variable_list*
786
find_varbind_in_list( netsnmp_variable_list *vblist,
787
                      const oid *name, size_t len)
788
0
{
789
0
    for (; vblist != NULL; vblist = vblist->next_variable)
790
0
        if (!snmp_oid_compare(vblist->name, vblist->name_length, name, len))
791
0
            return vblist;
792
793
0
    return NULL;
794
0
}
795
796
/*
797
 * Add some value to SNMP variable.
798
 * If the value is large, additional memory is allocated.
799
 * Returns 0 if successful.
800
 */
801
802
int
803
snmp_set_var_value(netsnmp_variable_list * vars,
804
                   const void * value, size_t len)
805
0
{
806
0
    int             largeval = 1;
807
808
    /*
809
     * xxx-rks: why the unconditional free? why not use existing
810
     * memory, if len < vars->val_len ?
811
     */
812
0
    if (vars->val.string && vars->val.string != vars->buf) {
813
0
        free(vars->val.string);
814
0
    }
815
0
    vars->val.string = NULL;
816
0
    vars->val_len = 0;
817
818
0
    if (value == NULL && len > 0) {
819
0
        snmp_log(LOG_ERR, "bad size for NULL value\n");
820
0
        return 1;
821
0
    }
822
823
    /*
824
     * use built-in storage for smaller values 
825
     */
826
0
    if (len <= sizeof(vars->buf)) {
827
0
        vars->val.string = (u_char *) vars->buf;
828
0
        largeval = 0;
829
0
    }
830
831
0
    if ((0 == len) || (NULL == value)) {
832
0
        vars->val.string[0] = 0;
833
0
        return 0;
834
0
    }
835
836
0
    vars->val_len = len;
837
0
    switch (vars->type) {
838
0
    case ASN_INTEGER:
839
0
    case ASN_UNSIGNED:
840
0
    case ASN_TIMETICKS:
841
0
    case ASN_COUNTER:
842
0
    case ASN_UINTEGER:
843
0
        if (vars->val_len == sizeof(int)) {
844
0
            if (ASN_INTEGER == vars->type) {
845
0
                const int      *val_int 
846
0
                    = (const int *) value;
847
0
                *(vars->val.integer) = (long) *val_int;
848
0
            } else {
849
0
                const u_int    *val_uint
850
0
                    = (const u_int *) value;
851
0
                *(vars->val.integer) = (unsigned long) *val_uint;
852
0
            }
853
0
        }
854
0
        else if (vars->val_len == sizeof(long)){
855
0
            const u_long   *val_ulong
856
0
                = (const u_long *) value;
857
0
            *(vars->val.integer) = *val_ulong;
858
0
            if (*(vars->val.integer) > 0xffffffff) {
859
0
                NETSNMP_LOGONCE((LOG_INFO,
860
0
                                 "truncating integer value > 32 bits\n"));
861
0
                *(vars->val.integer) &= 0xffffffff;
862
0
            }
863
0
        }
864
0
        else if (vars->val_len == sizeof(long long)){
865
0
            const unsigned long long   *val_ullong
866
0
                = (const unsigned long long *) value;
867
0
            *(vars->val.integer) = (long) *val_ullong;
868
0
            if (*(vars->val.integer) > 0xffffffff) {
869
0
                NETSNMP_LOGONCE((LOG_INFO,
870
0
                                 "truncating integer value > 32 bits\n"));
871
0
                *(vars->val.integer) &= 0xffffffff;
872
0
            }
873
0
        }
874
0
        else if (vars->val_len == sizeof(intmax_t)){
875
0
            const uintmax_t *val_uintmax_t
876
0
                = (const uintmax_t *) value;
877
0
            *(vars->val.integer) = (long) *val_uintmax_t;
878
0
            if (*(vars->val.integer) > 0xffffffff) {
879
0
                NETSNMP_LOGONCE((LOG_INFO,
880
0
                                 "truncating integer value > 32 bits\n"));
881
0
                *(vars->val.integer) &= 0xffffffff;
882
0
            }
883
0
        }
884
0
        else if (vars->val_len == sizeof(short)) {
885
0
            if (ASN_INTEGER == vars->type) {
886
0
                const short      *val_short 
887
0
                    = (const short *) value;
888
0
                *(vars->val.integer) = (long) *val_short;
889
0
            } else {
890
0
                const u_short    *val_ushort
891
0
                    = (const u_short *) value;
892
0
                *(vars->val.integer) = (unsigned long) *val_ushort;
893
0
            }
894
0
        }
895
0
        else if (vars->val_len == sizeof(char)) {
896
0
            if (ASN_INTEGER == vars->type) {
897
0
                const signed char   *val_char
898
0
                    = (const signed char *) value;
899
0
                *(vars->val.integer) = (long) *val_char;
900
0
            } else {
901
0
                    const u_char    *val_uchar
902
0
                    = (const u_char *) value;
903
0
                *(vars->val.integer) = (unsigned long) *val_uchar;
904
0
            }
905
0
        }
906
0
        else {
907
0
            snmp_log(LOG_ERR,"bad size for integer-like type (%d)\n",
908
0
                     (int)vars->val_len);
909
0
            return (1);
910
0
        }
911
0
        vars->val_len = sizeof(long);
912
0
        break;
913
914
0
    case ASN_OBJECT_ID:
915
0
    case ASN_PRIV_IMPLIED_OBJECT_ID:
916
0
    case ASN_PRIV_INCL_RANGE:
917
0
    case ASN_PRIV_EXCL_RANGE:
918
0
        if (largeval) {
919
0
            vars->val.objid = (oid *) malloc(vars->val_len);
920
0
        }
921
0
        if (vars->val.objid == NULL) {
922
0
            snmp_log(LOG_ERR,"no storage for OID\n");
923
0
            return 1;
924
0
        }
925
0
        memmove(vars->val.objid, value, vars->val_len);
926
0
        break;
927
928
0
    case ASN_IPADDRESS: /* snmp_build_var_op treats IPADDR like a string */
929
0
        if (4 != vars->val_len) {
930
0
            netsnmp_assert("ipaddress length == 4");
931
0
        }
932
0
        NETSNMP_FALLTHROUGH;
933
0
    case ASN_PRIV_IMPLIED_OCTET_STR:
934
0
    case ASN_OCTET_STR:
935
0
    case ASN_BIT_STR:
936
0
    case ASN_OPAQUE:
937
0
    case ASN_NSAP:
938
0
        if (vars->val_len >= sizeof(vars->buf)) {
939
0
            vars->val.string = (u_char *) malloc(vars->val_len + 1);
940
0
        }
941
0
        if (vars->val.string == NULL) {
942
0
            snmp_log(LOG_ERR,"no storage for string\n");
943
0
            return 1;
944
0
        }
945
0
        memmove(vars->val.string, value, vars->val_len);
946
        /*
947
         * Make sure the string is zero-terminated; some bits of code make
948
         * this assumption.  Easier to do this here than fix all these wrong
949
         * assumptions.  
950
         */
951
0
        vars->val.string[vars->val_len] = '\0';
952
0
        break;
953
954
0
    case SNMP_NOSUCHOBJECT:
955
0
    case SNMP_NOSUCHINSTANCE:
956
0
    case SNMP_ENDOFMIBVIEW:
957
0
    case ASN_NULL:
958
0
        vars->val_len = 0;
959
0
        vars->val.string = NULL;
960
0
        break;
961
962
0
#ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES
963
0
    case ASN_OPAQUE_U64:
964
0
    case ASN_OPAQUE_I64:
965
0
#endif                          /* NETSNMP_WITH_OPAQUE_SPECIAL_TYPES */
966
0
    case ASN_COUNTER64:
967
0
        if (largeval || vars->val_len != sizeof(struct counter64)) {
968
0
            snmp_log(LOG_ERR,"bad size for counter 64 (%d)\n",
969
0
                     (int)vars->val_len);
970
0
            return (1);
971
0
        }
972
0
        vars->val_len = sizeof(struct counter64);
973
0
        memmove(vars->val.counter64, value, vars->val_len);
974
0
        break;
975
976
0
#ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES
977
0
    case ASN_OPAQUE_FLOAT:
978
0
        if (largeval) {
979
0
            snmp_log(LOG_ERR,"bad size for opaque float (%d)\n",
980
0
                     (int)vars->val_len);
981
0
            return (1);
982
0
        }
983
0
        vars->val_len = sizeof(float);
984
0
        memmove(vars->val.floatVal, value, vars->val_len);
985
0
        break;
986
987
0
    case ASN_OPAQUE_DOUBLE:
988
0
        if (largeval) {
989
0
            snmp_log(LOG_ERR,"bad size for opaque double (%d)\n",
990
0
                     (int)vars->val_len);
991
0
            return (1);
992
0
        }
993
0
        vars->val_len = sizeof(double);
994
0
        memmove(vars->val.doubleVal, value, vars->val_len);
995
0
        break;
996
997
0
#endif                          /* NETSNMP_WITH_OPAQUE_SPECIAL_TYPES */
998
999
0
    default:
1000
0
        snmp_log(LOG_ERR,"Internal error in type switching\n");
1001
0
        snmp_set_detail("Internal error in type switching\n");
1002
0
        return (1);
1003
0
    }
1004
1005
0
    return 0;
1006
0
}
1007
1008
void
1009
snmp_replace_var_types(netsnmp_variable_list * vbl, u_char old_type,
1010
                       u_char new_type)
1011
0
{
1012
0
    while (vbl) {
1013
0
        if (vbl->type == old_type) {
1014
0
            snmp_set_var_typed_value(vbl, new_type, NULL, 0);
1015
0
        }
1016
0
        vbl = vbl->next_variable;
1017
0
    }
1018
0
}
1019
1020
#ifndef NETSNMP_FEATURE_REMOVE_SNMP_RESET_VAR_TYPES
1021
void
1022
snmp_reset_var_types(netsnmp_variable_list * vbl, u_char new_type)
1023
0
{
1024
0
    while (vbl) {
1025
0
        snmp_set_var_typed_value(vbl, new_type, NULL, 0);
1026
0
        vbl = vbl->next_variable;
1027
0
    }
1028
0
}
1029
#endif /* NETSNMP_FEATURE_REMOVE_SNMP_RESET_VAR_TYPES */
1030
1031
int
1032
snmp_synch_response_cb(netsnmp_session * ss,
1033
                       netsnmp_pdu *pdu,
1034
                       netsnmp_pdu **response, snmp_callback pcb)
1035
0
{
1036
0
    struct synch_state    lstate, *state;
1037
0
    snmp_callback         cbsav;
1038
0
    void                 *cbmagsav;
1039
0
    int                   numfds, count;
1040
0
    netsnmp_large_fd_set  fdset;
1041
0
    struct timeval        timeout, *tvp;
1042
0
    int                   block;
1043
1044
0
    memset((void *) &lstate, 0, sizeof(lstate));
1045
0
    state = &lstate;
1046
0
    cbsav = ss->callback;
1047
0
    cbmagsav = ss->callback_magic;
1048
0
    ss->callback = pcb;
1049
0
    ss->callback_magic = (void *) state;
1050
0
    netsnmp_large_fd_set_init(&fdset, FD_SETSIZE);
1051
1052
0
    if (snmp_send(ss, pdu) == 0) {
1053
0
        snmp_free_pdu(pdu);
1054
0
        state->status = STAT_ERROR;
1055
0
    } else {
1056
0
        state->reqid = pdu->reqid;
1057
0
        state->waiting = 1;
1058
0
    }
1059
1060
0
    while (state->waiting) {
1061
0
        numfds = 0;
1062
0
        NETSNMP_LARGE_FD_ZERO(&fdset);
1063
0
        block = NETSNMP_SNMPBLOCK;
1064
0
        tvp = &timeout;
1065
0
        timerclear(tvp);
1066
0
        snmp_sess_select_info2_flags(NULL, &numfds, &fdset, tvp, &block,
1067
0
                                     NETSNMP_SELECT_NOALARMS);
1068
0
        if (block == 1)
1069
0
            tvp = NULL;         /* block without timeout */
1070
0
        count = netsnmp_large_fd_set_select(numfds, &fdset, NULL, NULL, tvp);
1071
0
        if (count > 0) {
1072
0
            snmp_read2(&fdset);
1073
0
        } else {
1074
0
            switch (count) {
1075
0
            case 0:
1076
0
                snmp_timeout();
1077
0
                break;
1078
0
            case -1:
1079
0
                if (errno == EINTR) {
1080
0
                    continue;
1081
0
                } else {
1082
0
                    snmp_errno = SNMPERR_GENERR;    /*MTCRITICAL_RESOURCE */
1083
                    /*
1084
                     * CAUTION! if another thread closed the socket(s)
1085
                     * waited on here, the session structure was freed.
1086
                     * It would be nice, but we can't rely on the pointer.
1087
                     * ss->s_snmp_errno = SNMPERR_GENERR;
1088
                     * ss->s_errno = errno;
1089
                     */
1090
0
                    snmp_set_detail(strerror(errno));
1091
0
                }
1092
0
                NETSNMP_FALLTHROUGH;
1093
0
            default:
1094
0
                state->status = STAT_ERROR;
1095
0
                state->waiting = 0;
1096
0
            }
1097
0
        }
1098
1099
0
        if ( ss->flags & SNMP_FLAGS_RESP_CALLBACK ) {
1100
0
            void (*cb)(void);
1101
0
            cb = (void (*)(void))(ss->myvoid);
1102
0
            cb();        /* Used to invoke 'netsnmp_check_outstanding_agent_requests();'
1103
                            on internal AgentX queries.  */
1104
0
        }
1105
0
    }
1106
0
    *response = state->pdu;
1107
0
    ss->callback = cbsav;
1108
0
    ss->callback_magic = cbmagsav;
1109
0
    netsnmp_large_fd_set_cleanup(&fdset);
1110
0
    return state->status;
1111
0
}
1112
1113
int
1114
snmp_synch_response(netsnmp_session * ss,
1115
                    netsnmp_pdu *pdu, netsnmp_pdu **response)
1116
0
{
1117
0
    return snmp_synch_response_cb(ss, pdu, response, snmp_synch_input);
1118
0
}
1119
1120
int
1121
snmp_sess_synch_response(struct session_list *slp,
1122
                         netsnmp_pdu *pdu, netsnmp_pdu **response)
1123
0
{
1124
0
    netsnmp_session      *ss;
1125
0
    struct synch_state    lstate, *state;
1126
0
    snmp_callback         cbsav;
1127
0
    void                 *cbmagsav;
1128
0
    int                   numfds, count;
1129
0
    netsnmp_large_fd_set  fdset;
1130
0
    struct timeval        timeout, *tvp;
1131
0
    int                   block;
1132
1133
0
    ss = snmp_sess_session(slp);
1134
0
    if (ss == NULL) {
1135
0
        return STAT_ERROR;
1136
0
    }
1137
1138
0
    memset((void *) &lstate, 0, sizeof(lstate));
1139
0
    state = &lstate;
1140
0
    cbsav = ss->callback;
1141
0
    cbmagsav = ss->callback_magic;
1142
0
    ss->callback = snmp_synch_input;
1143
0
    ss->callback_magic = (void *) state;
1144
0
    netsnmp_large_fd_set_init(&fdset, FD_SETSIZE);
1145
1146
0
    if (snmp_sess_send(slp, pdu) == 0) {
1147
0
        snmp_free_pdu(pdu);
1148
0
        state->status = STAT_ERROR;
1149
0
    } else {
1150
0
        state->waiting = 1;
1151
0
        state->reqid = pdu->reqid;
1152
0
    }
1153
1154
0
    while (state->waiting) {
1155
0
        numfds = 0;
1156
0
        NETSNMP_LARGE_FD_ZERO(&fdset);
1157
0
        block = NETSNMP_SNMPBLOCK;
1158
0
        tvp = &timeout;
1159
0
        timerclear(tvp);
1160
0
        snmp_sess_select_info2_flags(slp, &numfds, &fdset, tvp, &block,
1161
0
                                     NETSNMP_SELECT_NOALARMS);
1162
0
        if (block == 1)
1163
0
            tvp = NULL;         /* block without timeout */
1164
0
        count = netsnmp_large_fd_set_select(numfds, &fdset, NULL, NULL, tvp);
1165
0
        if (count > 0) {
1166
0
            snmp_sess_read2(slp, &fdset);
1167
0
        } else
1168
0
            switch (count) {
1169
0
            case 0:
1170
0
                snmp_sess_timeout(slp);
1171
0
                break;
1172
0
            case -1:
1173
0
                if (errno == EINTR) {
1174
0
                    continue;
1175
0
                } else {
1176
0
                    snmp_errno = SNMPERR_GENERR;    /*MTCRITICAL_RESOURCE */
1177
                    /*
1178
                     * CAUTION! if another thread closed the socket(s)
1179
                     * waited on here, the session structure was freed.
1180
                     * It would be nice, but we can't rely on the pointer.
1181
                     * ss->s_snmp_errno = SNMPERR_GENERR;
1182
                     * ss->s_errno = errno;
1183
                     */
1184
0
                    snmp_set_detail(strerror(errno));
1185
0
                }
1186
0
                NETSNMP_FALLTHROUGH;
1187
0
            default:
1188
0
                state->status = STAT_ERROR;
1189
0
                state->waiting = 0;
1190
0
            }
1191
0
    }
1192
0
    *response = state->pdu;
1193
0
    ss->callback = cbsav;
1194
0
    ss->callback_magic = cbmagsav;
1195
0
    netsnmp_large_fd_set_cleanup(&fdset);
1196
0
    return state->status;
1197
0
}
1198
1199
1200
const char     *
1201
snmp_errstring(int errstat)
1202
0
{
1203
0
    const char * const error_string[19] = {
1204
0
        "(noError) No Error",
1205
0
        "(tooBig) Response message would have been too large.",
1206
0
        "(noSuchName) There is no such variable name in this MIB.",
1207
0
        "(badValue) The value given has the wrong type or length.",
1208
0
        "(readOnly) The two parties used do not have access to use the specified SNMP PDU.",
1209
0
        "(genError) A general failure occurred",
1210
0
        "noAccess",
1211
0
        "wrongType (The set datatype does not match the data type the agent expects)",
1212
0
        "wrongLength (The set value has an illegal length from what the agent expects)",
1213
0
        "wrongEncoding",
1214
0
        "wrongValue (The set value is illegal or unsupported in some way)",
1215
0
        "noCreation (That table does not support row creation or that object can not ever be created)",
1216
0
        "inconsistentValue (The set value is illegal or unsupported in some way)",
1217
0
        "resourceUnavailable (This is likely a out-of-memory failure within the agent)",
1218
0
        "commitFailed",
1219
0
        "undoFailed",
1220
0
        "authorizationError (access denied to that object)",
1221
0
        "notWritable (That object does not support modification)",
1222
0
        "inconsistentName (That object can not currently be created)"
1223
0
    };
1224
1225
0
    if (errstat <= MAX_SNMP_ERR && errstat >= SNMP_ERR_NOERROR) {
1226
0
        return error_string[errstat];
1227
0
    } else {
1228
0
        return "Unknown Error";
1229
0
    }
1230
0
}
1231
1232
1233
1234
/*
1235
 *
1236
 *  Convenience routines to make various requests
1237
 *  over the specified SNMP session.
1238
 *
1239
 */
1240
#include <net-snmp/library/snmp_debug.h>
1241
1242
static netsnmp_session *_def_query_session = NULL;
1243
1244
#ifndef NETSNMP_FEATURE_REMOVE_QUERY_SET_DEFAULT_SESSION
1245
void
1246
0
netsnmp_query_set_default_session( netsnmp_session *sess) {
1247
0
    DEBUGMSGTL(("iquery", "set default session %p\n", sess));
1248
0
    _def_query_session = sess;
1249
0
}
1250
#endif /* NETSNMP_FEATURE_REMOVE_QUERY_SET_DEFAULT_SESSION */
1251
1252
void
1253
0
netsnmp_query_shutdown(void) {
1254
0
    if (_def_query_session) {
1255
0
        snmp_close(_def_query_session);
1256
0
        _def_query_session = NULL;
1257
0
    }
1258
0
}
1259
1260
/**
1261
 * Return a pointer to the default internal query session.
1262
 */
1263
netsnmp_session *
1264
0
netsnmp_query_get_default_session_unchecked( void ) {
1265
0
    DEBUGMSGTL(("iquery", "get default session %p\n", _def_query_session));
1266
0
    return _def_query_session;
1267
0
}
1268
1269
/**
1270
 * Return a pointer to the default internal query session and log a
1271
 * warning message once if this session does not exist.
1272
 */
1273
netsnmp_session *
1274
0
netsnmp_query_get_default_session( void ) {
1275
0
    static int warning_logged = 0;
1276
1277
0
    if (! _def_query_session && ! warning_logged) {
1278
0
        if (! netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID,
1279
0
                                    NETSNMP_DS_AGENT_INTERNAL_SECNAME)) {
1280
0
            snmp_log(LOG_WARNING,
1281
0
                     "iquerySecName has not been configured - internal queries will fail\n");
1282
0
        } else {
1283
0
            snmp_log(LOG_WARNING,
1284
0
                     "default session is not available - internal queries will fail\n");
1285
0
        }
1286
0
        warning_logged = 1;
1287
0
    }
1288
1289
0
    return netsnmp_query_get_default_session_unchecked();
1290
0
}
1291
1292
1293
/*
1294
 * Internal utility routine to actually send the query
1295
 */
1296
static int _query(netsnmp_variable_list *list,
1297
                  int                    request,
1298
0
                  netsnmp_session       *session) {
1299
1300
0
    netsnmp_pdu *pdu;
1301
0
    netsnmp_pdu *response = NULL;
1302
0
    netsnmp_variable_list *vb1, *vb2, *vtmp;
1303
0
    int ret, count;
1304
1305
0
    DEBUGMSGTL(("iquery", "query on session %p\n", session));
1306
1307
0
    if (NULL == list) {
1308
0
        snmp_log(LOG_ERR, "empty variable list in _query\n");
1309
0
        return SNMP_ERR_GENERR;
1310
0
    }
1311
1312
0
    pdu = snmp_pdu_create( request );
1313
0
    if (NULL == pdu) {
1314
0
        snmp_log(LOG_ERR, "could not allocate pdu\n");
1315
0
        return SNMP_ERR_GENERR;
1316
0
    }
1317
1318
    /*
1319
     * Clone the varbind list into the request PDU...
1320
     */
1321
0
    pdu->variables = snmp_clone_varbind( list );
1322
0
    if (NULL == pdu->variables) {
1323
0
        snmp_log(LOG_ERR, "could not clone variable list\n");
1324
0
        snmp_free_pdu(pdu);
1325
0
        return SNMP_ERR_GENERR;
1326
0
    }
1327
1328
0
#ifndef NETSNMP_NO_WRITE_SUPPORT
1329
0
retry:
1330
0
#endif
1331
0
    if ( session )
1332
0
        ret = snmp_synch_response(            session, pdu, &response );
1333
0
    else if (_def_query_session)
1334
0
        ret = snmp_synch_response( _def_query_session, pdu, &response );
1335
0
    else {
1336
        /* No session specified */
1337
0
        snmp_free_pdu(pdu);
1338
0
        return SNMP_ERR_GENERR;
1339
0
    }
1340
0
    DEBUGMSGTL(("iquery", "query returned %d\n", ret));
1341
1342
    /*
1343
     * ....then copy the results back into the
1344
     * list (assuming the request succeeded!).
1345
     * This avoids having to worry about how this
1346
     * list was originally allocated.
1347
     */
1348
0
    if ( ret == SNMP_ERR_NOERROR ) {
1349
0
        if ( response->errstat != SNMP_ERR_NOERROR ) {
1350
0
            DEBUGMSGT(("iquery", "Error in packet: %s\n",
1351
0
                       snmp_errstring(response->errstat)));
1352
            /*
1353
             * If the request failed, then remove the
1354
             *  offending varbind and try again.
1355
             *  (all except SET requests)
1356
             *
1357
             * XXX - implement a library version of
1358
             *       NETSNMP_DS_APP_DONT_FIX_PDUS ??
1359
             */
1360
0
            ret = response->errstat;
1361
0
            if (response->errindex != 0) {
1362
0
                DEBUGMSGT(("iquery:result", "Failed object:\n"));
1363
0
                for (count = 1, vtmp = response->variables;
1364
0
                     vtmp && count != response->errindex;
1365
0
                     vtmp = vtmp->next_variable, count++)
1366
0
                    /*EMPTY*/;
1367
0
                if (vtmp)
1368
0
                    DEBUGMSGVAR(("iquery:result", vtmp));
1369
0
                DEBUGMSG(("iquery:result", "\n"));
1370
0
            }
1371
0
#ifndef NETSNMP_NO_WRITE_SUPPORT
1372
0
            if (request != SNMP_MSG_SET &&
1373
0
                response->errindex != 0) {
1374
0
                DEBUGMSGTL(("iquery", "retrying query (%d, %ld)\n", ret, response->errindex));
1375
0
                pdu = snmp_fix_pdu( response, request );
1376
0
                snmp_free_pdu( response );
1377
0
                response = NULL;
1378
0
                if ( pdu != NULL )
1379
0
                    goto retry;
1380
0
            }
1381
0
#endif /* !NETSNMP_NO_WRITE_SUPPORT */
1382
0
        } else {
1383
0
            for (vb1 = response->variables, vb2 = list;
1384
0
                 vb1;
1385
0
                 vb1 = vb1->next_variable,  vb2 = vb2->next_variable) {
1386
0
                DEBUGMSGVAR(("iquery:result", vb1));
1387
0
                DEBUGMSG(("iquery:results", "\n"));
1388
0
                if ( !vb2 ) {
1389
0
                    ret = SNMP_ERR_GENERR;
1390
0
                    break;
1391
0
                }
1392
0
                vtmp = vb2->next_variable;
1393
0
                snmp_free_var_internals( vb2 );
1394
0
                snmp_clone_var( vb1, vb2 ); /* xxx: check return? */
1395
0
                vb2->next_variable = vtmp;
1396
0
            }
1397
0
        }
1398
0
    } else {
1399
        /* Distinguish snmp_send errors from SNMP errStat errors */
1400
0
        ret = -ret;
1401
0
    }
1402
0
    snmp_free_pdu( response );
1403
0
    return ret;
1404
0
}
1405
1406
/*
1407
 * These are simple wrappers round the internal utility routine
1408
 */
1409
int netsnmp_query_get(netsnmp_variable_list *list,
1410
0
                      netsnmp_session       *session){
1411
0
    return _query( list, SNMP_MSG_GET, session );
1412
0
}
1413
1414
1415
int netsnmp_query_getnext(netsnmp_variable_list *list,
1416
0
                          netsnmp_session       *session){
1417
0
    return _query( list, SNMP_MSG_GETNEXT, session );
1418
0
}
1419
1420
1421
#ifndef NETSNMP_NO_WRITE_SUPPORT
1422
int netsnmp_query_set(netsnmp_variable_list *list,
1423
0
                      netsnmp_session       *session){
1424
0
    return _query( list, SNMP_MSG_SET, session );
1425
0
}
1426
#endif /* !NETSNMP_NO_WRITE_SUPPORT */
1427
1428
/*
1429
 * A walk needs a bit more work.
1430
 */
1431
int netsnmp_query_walk(netsnmp_variable_list *list,
1432
0
                       netsnmp_session       *session) {
1433
    /*
1434
     * Create a working copy of the original (single)
1435
     * varbind, so we can use this varbind parameter
1436
     * to check when we've finished walking this subtree.
1437
     */
1438
0
    netsnmp_variable_list *vb = snmp_clone_varbind( list );
1439
0
    netsnmp_variable_list *res_list = NULL;
1440
0
    netsnmp_variable_list *res_last = NULL;
1441
0
    int ret;
1442
1443
    /*
1444
     * Now walk the tree as usual
1445
     */
1446
0
    ret = _query( vb, SNMP_MSG_GETNEXT, session );
1447
0
    while ( ret == SNMP_ERR_NOERROR &&
1448
0
        snmp_oidtree_compare( list->name, list->name_length,
1449
0
                                vb->name,   vb->name_length ) == 0) {
1450
1451
0
  if (vb->type == SNMP_ENDOFMIBVIEW ||
1452
0
      vb->type == SNMP_NOSUCHOBJECT ||
1453
0
      vb->type == SNMP_NOSUCHINSTANCE)
1454
0
      break;
1455
1456
        /*
1457
         * Copy each response varbind to the end of the result list
1458
         * and then re-use this to ask for the next entry.
1459
         */
1460
0
        if ( res_last ) {
1461
0
            res_last->next_variable = snmp_clone_varbind( vb );
1462
0
            res_last = res_last->next_variable;
1463
0
        } else {
1464
0
            res_list = snmp_clone_varbind( vb );
1465
0
            res_last = res_list;
1466
0
        }
1467
0
        ret = _query( vb, SNMP_MSG_GETNEXT, session );
1468
0
    }
1469
    /*
1470
     * Copy the first result back into the original varbind parameter,
1471
     * add the rest of the results (if any), and clean up.
1472
     */
1473
0
    if ( res_list ) {
1474
0
        snmp_clone_var( res_list, list );
1475
0
        list->next_variable = res_list->next_variable;
1476
0
        res_list->next_variable = NULL;
1477
0
        snmp_free_varbind( res_list );
1478
0
    }
1479
0
    snmp_free_varbind( vb );
1480
0
    return ret;
1481
0
}
1482
1483
/** **************************************************************************
1484
 *
1485
 * state machine
1486
 *
1487
 */
1488
int
1489
netsnmp_state_machine_run( netsnmp_state_machine_input *input)
1490
0
{
1491
0
    netsnmp_state_machine_step *current, *last;
1492
1493
0
    netsnmp_require_ptr_LRV( input, SNMPERR_GENERR );
1494
0
    netsnmp_require_ptr_LRV( input->steps, SNMPERR_GENERR );
1495
0
    last = current = input->steps;
1496
1497
0
    DEBUGMSGT(("state_machine:run", "starting step: %s\n", current->name));
1498
1499
0
    while (current) {
1500
1501
        /*
1502
         * log step and check for required data
1503
         */
1504
0
        DEBUGMSGT(("state_machine:run", "at step: %s\n", current->name));
1505
0
        if (NULL == current->run) {
1506
0
            DEBUGMSGT(("state_machine:run", "no run step\n"));
1507
0
            current->result = last->result;
1508
0
            break;
1509
0
        }
1510
1511
        /*
1512
         * run step
1513
         */
1514
0
        DEBUGMSGT(("state_machine:run", "running step: %s\n", current->name));
1515
0
        current->result = (*current->run)( input, current );
1516
0
        ++input->steps_so_far;
1517
        
1518
        /*
1519
         * log result and move to next step
1520
         */
1521
0
        input->last_run = current;
1522
0
        DEBUGMSGT(("state_machine:run:result", "step %s returned %d\n",
1523
0
                   current->name, current->result));
1524
0
        if (SNMPERR_SUCCESS == current->result)
1525
0
            current = current->on_success;
1526
0
        else if (SNMPERR_ABORT == current->result) {
1527
0
            DEBUGMSGT(("state_machine:run:result", "ABORT from %s\n",
1528
0
                       current->name));
1529
0
            break;
1530
0
        }
1531
0
        else
1532
0
            current = current->on_error;
1533
0
    }
1534
1535
    /*
1536
     * run cleanup
1537
     */
1538
0
    if ((input->cleanup) && (input->cleanup->run))
1539
0
        (*input->cleanup->run)( input, input->last_run );
1540
1541
0
    return input->last_run->result;
1542
0
}
1543
1544
#ifndef NETSNMP_NO_WRITE_SUPPORT
1545
#ifndef NETSNMP_FEATURE_REMOVE_ROW_CREATE
1546
/** **************************************************************************
1547
 *
1548
 * row create state machine steps
1549
 *
1550
 */
1551
typedef struct rowcreate_state_s {
1552
1553
    netsnmp_session        *session;
1554
    netsnmp_variable_list  *vars;
1555
    int                     row_status_index;
1556
} rowcreate_state;
1557
1558
static netsnmp_variable_list *
1559
_get_vb_num(netsnmp_variable_list *vars, int index)
1560
0
{
1561
0
    for (; vars && index > 0; --index)
1562
0
        vars = vars->next_variable;
1563
1564
0
    if (!vars || index > 0)
1565
0
        return NULL;
1566
    
1567
0
    return vars;
1568
0
}
1569
1570
1571
/*
1572
 * cleanup
1573
 */
1574
static int 
1575
_row_status_state_cleanup(netsnmp_state_machine_input *input,
1576
                 netsnmp_state_machine_step *step)
1577
0
{
1578
0
    rowcreate_state       *ctx;
1579
1580
0
    netsnmp_require_ptr_LRV( input, SNMPERR_ABORT );
1581
0
    netsnmp_require_ptr_LRV( step, SNMPERR_ABORT );
1582
1583
0
    DEBUGMSGT(("row_create:called", "_row_status_state_cleanup, last run step was %s rc %d\n",
1584
0
               step->name, step->result));
1585
1586
0
    ctx = (rowcreate_state *)input->input_context;
1587
0
    if (ctx && ctx->vars)
1588
0
        snmp_free_varbind( ctx->vars );
1589
1590
0
    return SNMPERR_SUCCESS;
1591
0
}
1592
1593
/*
1594
 * send a request to activate the row
1595
 */
1596
static int 
1597
_row_status_state_activate(netsnmp_state_machine_input *input,
1598
                  netsnmp_state_machine_step *step)
1599
0
{
1600
0
    rowcreate_state       *ctx;
1601
0
    netsnmp_variable_list *rs_var, *var = NULL;
1602
0
    int32_t                rc, val = RS_ACTIVE;
1603
1604
0
    netsnmp_require_ptr_LRV( input, SNMPERR_GENERR );
1605
0
    netsnmp_require_ptr_LRV( step, SNMPERR_GENERR );
1606
0
    netsnmp_require_ptr_LRV( input->input_context, SNMPERR_GENERR );
1607
1608
0
    ctx = (rowcreate_state *)input->input_context;
1609
1610
0
    DEBUGMSGT(("row_create:called", "called %s\n", step->name));
1611
1612
    /*
1613
     * just send the rowstatus varbind
1614
     */
1615
0
    rs_var = _get_vb_num(ctx->vars, ctx->row_status_index);
1616
0
    netsnmp_require_ptr_LRV(rs_var, SNMPERR_GENERR);
1617
1618
0
    var = snmp_varlist_add_variable(&var, rs_var->name, rs_var->name_length,
1619
0
                                    rs_var->type, &val, sizeof(val));
1620
0
    netsnmp_require_ptr_LRV( var, SNMPERR_GENERR );
1621
1622
    /*
1623
     * send set
1624
     */
1625
0
    rc = netsnmp_query_set( var, ctx->session );
1626
0
    if (-2 == rc)
1627
0
        rc = SNMPERR_ABORT;
1628
1629
0
    snmp_free_varbind(var);
1630
1631
0
    return rc;
1632
0
}
1633
1634
/*
1635
 * send each non-row status column, one at a time
1636
 */
1637
static int 
1638
_row_status_state_single_value_cols(netsnmp_state_machine_input *input,
1639
                                    netsnmp_state_machine_step *step)
1640
0
{
1641
0
    rowcreate_state       *ctx;
1642
0
    netsnmp_variable_list *var, *tmp_next, *row_status;
1643
0
    int                    rc = SNMPERR_GENERR;
1644
1645
0
    netsnmp_require_ptr_LRV( input, SNMPERR_GENERR );
1646
0
    netsnmp_require_ptr_LRV( step, SNMPERR_GENERR );
1647
0
    netsnmp_require_ptr_LRV( input->input_context, SNMPERR_GENERR );
1648
1649
0
    ctx = (rowcreate_state *)input->input_context;
1650
1651
0
    DEBUGMSGT(("row_create:called", "called %s\n", step->name));
1652
1653
0
    row_status = _get_vb_num(ctx->vars, ctx->row_status_index);
1654
0
    netsnmp_require_ptr_LRV(row_status, SNMPERR_GENERR);
1655
1656
    /*
1657
     * try one varbind at a time
1658
     */
1659
0
    for (var = ctx->vars; var; var = var->next_variable) {
1660
0
        if (var == row_status)
1661
0
            continue;
1662
1663
0
        tmp_next = var->next_variable;
1664
0
        var->next_variable = NULL;
1665
1666
        /*
1667
         * send set
1668
         */
1669
0
        rc = netsnmp_query_set( var, ctx->session );
1670
0
        var->next_variable = tmp_next;
1671
0
        if (-2 == rc)
1672
0
            rc = SNMPERR_ABORT;
1673
0
        if (rc != SNMPERR_SUCCESS)
1674
0
            break;
1675
0
    }
1676
1677
0
    return rc;
1678
0
}
1679
1680
/*
1681
 * send all values except row status
1682
 */
1683
static int 
1684
_row_status_state_multiple_values_cols(netsnmp_state_machine_input *input,
1685
                                       netsnmp_state_machine_step *step)
1686
0
{
1687
0
    rowcreate_state       *ctx;
1688
0
    netsnmp_variable_list *vars, *var, *last, *row_status;
1689
0
    int                    rc;
1690
1691
0
    netsnmp_require_ptr_LRV( input, SNMPERR_GENERR );
1692
0
    netsnmp_require_ptr_LRV( step, SNMPERR_GENERR );
1693
0
    netsnmp_require_ptr_LRV( input->input_context, SNMPERR_GENERR );
1694
1695
0
    ctx = (rowcreate_state *)input->input_context;
1696
1697
0
    DEBUGMSGT(("row_create:called", "called %s\n", step->name));
1698
1699
0
    vars = snmp_clone_varbind(ctx->vars);
1700
0
    netsnmp_require_ptr_LRV(vars, SNMPERR_GENERR);
1701
1702
0
    row_status = _get_vb_num(vars, ctx->row_status_index);
1703
0
    if (NULL == row_status) {
1704
0
        snmp_free_varbind(vars);
1705
0
        return SNMPERR_GENERR;
1706
0
    }
1707
1708
    /*
1709
     * remove row status varbind
1710
     */
1711
0
    if (row_status == vars) {
1712
0
        vars = row_status->next_variable;
1713
0
        row_status->next_variable = NULL;
1714
0
    }
1715
0
    else {
1716
0
        for (last=vars, var=last->next_variable;
1717
0
             var;
1718
0
             last=var, var = var->next_variable) {
1719
0
            if (var == row_status) {
1720
0
                last->next_variable = var->next_variable;
1721
0
                break;
1722
0
            }
1723
0
        }
1724
0
    }
1725
0
    snmp_free_var(row_status);
1726
1727
    /*
1728
     * send set
1729
     */
1730
0
    rc = netsnmp_query_set( vars, ctx->session );
1731
0
    if (-2 == rc)
1732
0
        rc = SNMPERR_ABORT;
1733
1734
0
    snmp_free_varbind(vars);
1735
1736
0
    return rc;
1737
0
}
1738
1739
/*
1740
 * send a createAndWait request with no other values
1741
 */
1742
static int 
1743
_row_status_state_single_value_createAndWait(netsnmp_state_machine_input *input,
1744
                                             netsnmp_state_machine_step *step)
1745
0
{
1746
0
    rowcreate_state       *ctx;
1747
0
    netsnmp_variable_list *var = NULL, *rs_var;
1748
0
    int32_t                rc, val = RS_CREATEANDWAIT;
1749
1750
0
    netsnmp_require_ptr_LRV( input, SNMPERR_GENERR );
1751
0
    netsnmp_require_ptr_LRV( step, SNMPERR_GENERR );
1752
0
    netsnmp_require_ptr_LRV( input->input_context, SNMPERR_GENERR );
1753
1754
0
    ctx = (rowcreate_state *)input->input_context;
1755
1756
0
    DEBUGMSGT(("row_create:called", "called %s\n", step->name));
1757
1758
0
    rs_var = _get_vb_num(ctx->vars, ctx->row_status_index);
1759
0
    netsnmp_require_ptr_LRV(rs_var, SNMPERR_GENERR);
1760
1761
0
    var = snmp_varlist_add_variable(&var, rs_var->name, rs_var->name_length,
1762
0
                                    rs_var->type, &val, sizeof(val));
1763
0
    netsnmp_require_ptr_LRV(var, SNMPERR_GENERR);
1764
1765
    /*
1766
     * send set
1767
     */
1768
0
    rc = netsnmp_query_set( var, ctx->session );
1769
0
    if (-2 == rc)
1770
0
        rc = SNMPERR_ABORT;
1771
1772
0
    snmp_free_varbind(var);
1773
1774
0
    return rc;
1775
0
}
1776
1777
/*
1778
 * send a creatAndWait request with all values
1779
 */
1780
static int 
1781
_row_status_state_all_values_createAndWait(netsnmp_state_machine_input *input,
1782
                                           netsnmp_state_machine_step *step)
1783
0
{
1784
0
    rowcreate_state       *ctx;
1785
0
    netsnmp_variable_list *vars, *rs_var;
1786
0
    int                    rc;
1787
1788
0
    netsnmp_require_ptr_LRV( input, SNMPERR_GENERR );
1789
0
    netsnmp_require_ptr_LRV( step, SNMPERR_GENERR );
1790
0
    netsnmp_require_ptr_LRV( input->input_context, SNMPERR_GENERR );
1791
1792
0
    ctx = (rowcreate_state *)input->input_context;
1793
1794
0
    DEBUGMSGT(("row_create:called", "called %s\n", step->name));
1795
1796
0
    vars = snmp_clone_varbind(ctx->vars);
1797
0
    netsnmp_require_ptr_LRV(vars, SNMPERR_GENERR);
1798
1799
    /*
1800
     * make sure row stats is createAndWait
1801
     */
1802
0
    rs_var = _get_vb_num(vars, ctx->row_status_index);
1803
0
    if (NULL == rs_var) {
1804
0
        snmp_free_varbind(vars);
1805
0
        return SNMPERR_GENERR;
1806
0
    }
1807
1808
0
    if (*rs_var->val.integer != RS_CREATEANDWAIT)
1809
0
        *rs_var->val.integer = RS_CREATEANDWAIT;
1810
1811
    /*
1812
     * send set
1813
     */
1814
0
    rc = netsnmp_query_set( vars, ctx->session );
1815
0
    if (-2 == rc)
1816
0
        rc = SNMPERR_ABORT;
1817
1818
0
    snmp_free_varbind(vars);
1819
1820
0
    return rc;
1821
0
}
1822
1823
1824
/**
1825
 * send createAndGo request with all values
1826
 */
1827
static int 
1828
_row_status_state_all_values_createAndGo(netsnmp_state_machine_input *input,
1829
                                         netsnmp_state_machine_step *step)
1830
0
{
1831
0
    rowcreate_state       *ctx;
1832
0
    netsnmp_variable_list *vars, *rs_var;
1833
0
    int                    rc;
1834
1835
0
    netsnmp_require_ptr_LRV( input, SNMPERR_GENERR );
1836
0
    netsnmp_require_ptr_LRV( step, SNMPERR_GENERR );
1837
0
    netsnmp_require_ptr_LRV( input->input_context, SNMPERR_GENERR );
1838
1839
0
    ctx = (rowcreate_state *)input->input_context;
1840
1841
0
    DEBUGMSGT(("row_create:called", "called %s\n", step->name));
1842
1843
0
    vars = snmp_clone_varbind(ctx->vars);
1844
0
    netsnmp_require_ptr_LRV(vars, SNMPERR_GENERR);
1845
1846
    /*
1847
     * make sure row stats is createAndGo
1848
     */
1849
0
    rs_var = _get_vb_num(vars, ctx->row_status_index + 1);
1850
0
    if (NULL == rs_var) {
1851
0
        snmp_free_varbind(vars);
1852
0
        return SNMPERR_GENERR;
1853
0
    }
1854
1855
0
    if (*rs_var->val.integer != RS_CREATEANDGO)
1856
0
        *rs_var->val.integer = RS_CREATEANDGO;
1857
1858
    /*
1859
     * send set
1860
     */
1861
0
    rc = netsnmp_query_set( vars, ctx->session );
1862
0
    if (-2 == rc)
1863
0
        rc = SNMPERR_ABORT;
1864
1865
0
    snmp_free_varbind(vars);
1866
1867
0
    return rc;
1868
0
}
1869
1870
/** **************************************************************************
1871
 *
1872
 * row api
1873
 *
1874
 */
1875
int
1876
netsnmp_row_create(netsnmp_session *sess, netsnmp_variable_list *vars,
1877
                   int row_status_index)
1878
0
{
1879
0
    netsnmp_state_machine_step rc_cleanup =
1880
0
        { "row_create_cleanup", 0, _row_status_state_cleanup,
1881
0
          0, NULL, NULL, 0, NULL };
1882
0
    netsnmp_state_machine_step rc_activate =
1883
0
        { "row_create_activate", 0, _row_status_state_activate,
1884
0
          0, NULL, NULL, 0, NULL };
1885
0
    netsnmp_state_machine_step rc_sv_cols =
1886
0
        { "row_create_single_value_cols", 0,
1887
0
          _row_status_state_single_value_cols, 0, &rc_activate,NULL, 0, NULL };
1888
0
    netsnmp_state_machine_step rc_mv_cols =
1889
0
        { "row_create_multiple_values_cols", 0,
1890
0
          _row_status_state_multiple_values_cols, 0, &rc_activate, &rc_sv_cols,
1891
0
          0, NULL };
1892
0
    netsnmp_state_machine_step rc_sv_caw =
1893
0
        { "row_create_single_value_createAndWait", 0,
1894
0
          _row_status_state_single_value_createAndWait, 0, &rc_mv_cols, NULL,
1895
0
          0, NULL };
1896
0
    netsnmp_state_machine_step rc_av_caw =
1897
0
        { "row_create_all_values_createAndWait", 0,
1898
0
          _row_status_state_all_values_createAndWait, 0, &rc_activate,
1899
0
          &rc_sv_caw, 0, NULL };
1900
0
    netsnmp_state_machine_step rc_av_cag =
1901
0
        { "row_create_all_values_createAndGo", 0,
1902
0
          _row_status_state_all_values_createAndGo, 0, NULL, &rc_av_caw, 0,
1903
0
          NULL };
1904
1905
0
    netsnmp_state_machine_input sm_input = { "row_create_machine", 0,
1906
0
                                             &rc_av_cag, &rc_cleanup };
1907
0
    rowcreate_state state;
1908
1909
0
    netsnmp_require_ptr_LRV( sess, SNMPERR_GENERR);
1910
0
    netsnmp_require_ptr_LRV( vars, SNMPERR_GENERR);
1911
1912
0
    state.session = sess;
1913
0
    state.vars = vars;
1914
1915
0
    state.row_status_index = row_status_index;
1916
0
    sm_input.input_context = &state;
1917
1918
0
    netsnmp_state_machine_run( &sm_input);
1919
1920
0
    return SNMPERR_SUCCESS;
1921
0
}
1922
#endif /* NETSNMP_FEATURE_REMOVE_ROW_CREATE */
1923
#endif /* NETSNMP_NO_WRITE_SUPPORT */
1924
1925
1926
/** @} */