Debian Codegraph @

agent/mibgroup/snmpv3/usmUser.c

raw
1 /*
2 * usmUser.c
3 *
4 * Portions of this file are subject to the following copyright(s). See
5 * the Net-SNMP's COPYING file for more details and other copyrights
6 * that may apply:
7 *
8 * Portions of this file are copyrighted by:
9 * Copyright (c) 2016 VMware, Inc. All rights reserved.
10 * Use is subject to license terms specified in the COPYING file
11 * distributed with the Net-SNMP package.
12 */
13
14 #include <net-snmp/net-snmp-config.h>
15 #include <net-snmp/net-snmp-features.h>
16 #include <stdlib.h>
17
18 #ifdef HAVE_STRING_H
19 #include <string.h>
20 #else
21 #include <strings.h>
22 #endif
23
24 #include <net-snmp/net-snmp-includes.h>
25 #include <net-snmp/agent/net-snmp-agent-includes.h>
26
27 #include "util_funcs/header_generic.h"
28 #include "usmUser.h"
29
30 #ifndef NETSNMP_NO_WRITE_SUPPORT
31 int usmStatusCheck(struct usmUser *uptr);
32 #endif /* !NETSNMP_NO_WRITE_SUPPORT */
33
34 netsnmp_feature_child_of(usmuser_all, libnetsnmpmibs);
35 netsnmp_feature_child_of(init_register_usmuser_context, usmuser_all);
36
37 netsnmp_feature_require(scapi_get_proper_priv_length);
38
39 struct variable4 usmUser_variables[] = {
40 {USMUSERSPINLOCK, ASN_INTEGER, NETSNMP_OLDAPI_RWRITE,
41 var_usmUser, 1, {1}},
42 {USMUSERSECURITYNAME, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
43 var_usmUser, 3, {2, 1, 3}},
44 {USMUSERCLONEFROM, ASN_OBJECT_ID, NETSNMP_OLDAPI_RWRITE,
45 var_usmUser, 3, {2, 1, 4}},
46 {USMUSERAUTHPROTOCOL, ASN_OBJECT_ID, NETSNMP_OLDAPI_RWRITE,
47 var_usmUser, 3, {2, 1, 5}},
48 {USMUSERAUTHKEYCHANGE, ASN_OCTET_STR, NETSNMP_OLDAPI_RWRITE,
49 var_usmUser, 3, {2, 1, 6}},
50 {USMUSEROWNAUTHKEYCHANGE, ASN_OCTET_STR, NETSNMP_OLDAPI_RWRITE,
51 var_usmUser, 3, {2, 1, 7}},
52 {USMUSERPRIVPROTOCOL, ASN_OBJECT_ID, NETSNMP_OLDAPI_RWRITE,
53 var_usmUser, 3, {2, 1, 8}},
54 {USMUSERPRIVKEYCHANGE, ASN_OCTET_STR, NETSNMP_OLDAPI_RWRITE,
55 var_usmUser, 3, {2, 1, 9}},
56 {USMUSEROWNPRIVKEYCHANGE, ASN_OCTET_STR, NETSNMP_OLDAPI_RWRITE,
57 var_usmUser, 3, {2, 1, 10}},
58 {USMUSERPUBLIC, ASN_OCTET_STR, NETSNMP_OLDAPI_RWRITE,
59 var_usmUser, 3, {2, 1, 11}},
60 {USMUSERSTORAGETYPE, ASN_INTEGER, NETSNMP_OLDAPI_RWRITE,
61 var_usmUser, 3, {2, 1, 12}},
62 {USMUSERSTATUS, ASN_INTEGER, NETSNMP_OLDAPI_RWRITE,
63 var_usmUser, 3, {2, 1, 13}},
64
65 };
66
67 oid usmUser_variables_oid[] = { 1, 3, 6, 1, 6, 3, 15, 1, 2 };
68
69
70 /*
71 * needed for the write_ functions to find the start of the index
72 */
73 #define USM_MIB_LENGTH 12
74
75 #ifndef NETSNMP_NO_WRITE_SUPPORT
76 static unsigned int usmUserSpinLock = 0;
77 #endif
78
79 void
80 init_usmUser(void)
81 {
82 REGISTER_MIB("snmpv3/usmUser", usmUser_variables, variable4,
83 usmUser_variables_oid);
84 }
85
86 #ifndef NETSNMP_FEATURE_REMOVE_INIT_REGISTER_USMUSER_CONTEXT
87 void
88 init_register_usmUser_context(const char *contextName) {
89 register_mib_context("snmpv3/usmUser",
90 (struct variable *) usmUser_variables,
91 sizeof(struct variable4),
92 sizeof(usmUser_variables)/sizeof(struct variable4),
93 usmUser_variables_oid,
94 sizeof(usmUser_variables_oid)/sizeof(oid),
95 DEFAULT_MIB_PRIORITY, 0, 0, NULL,
96 contextName, -1, 0);
97 }
98 #endif /* NETSNMP_FEATURE_REMOVE_INIT_REGISTER_USMUSER_CONTEXT */
99
100 /*******************************************************************-o-******
101 * usm_generate_OID
102 *
103 * Parameters:
104 * *prefix (I) OID prefix to the usmUser table entry.
105 * prefixLen (I)
106 * *uptr (I) Pointer to a user in the user list.
107 * *length (O) Length of generated index OID.
108 *
109 * Returns:
110 * Pointer to the OID index for the user (uptr) -OR-
111 * NULL on failure.
112 *
113 *
114 * Generate the index OID for a given usmUser name. 'length' is set to
115 * the length of the index OID.
116 *
117 * Index OID format is:
118 *
119 * <...prefix>.<engineID_length>.<engineID>.<user_name_length>.<user_name>
120 */
121 oid *
122 usm_generate_OID(oid * prefix, size_t prefixLen, struct usmUser *uptr,
123 size_t * length)
124 {
125 oid *indexOid;
126 int i;
127
128 *length = 2 + uptr->engineIDLen + strlen(uptr->name) + prefixLen;
129 indexOid = (oid *) malloc(*length * sizeof(oid));
130 if (indexOid) {
131 memmove(indexOid, prefix, prefixLen * sizeof(oid));
132
133 indexOid[prefixLen] = uptr->engineIDLen;
134 for (i = 0; i < (int) uptr->engineIDLen; i++)
135 indexOid[prefixLen + 1 + i] = (oid) uptr->engineID[i];
136
137 indexOid[prefixLen + uptr->engineIDLen + 1] = strlen(uptr->name);
138 for (i = 0; i < (int) strlen(uptr->name); i++)
139 indexOid[prefixLen + uptr->engineIDLen + 2 + i] =
140 (oid) uptr->name[i];
141 }
142 return indexOid;
143
144 } /* end usm_generate_OID() */
145
146 /*
147 * usm_parse_oid(): parses an index to the usmTable to break it down into
148 * a engineID component and a name component. The results are stored in:
149 *
150 * **engineID: a newly malloced string.
151 * *engineIDLen: The length of the malloced engineID string above.
152 * **name: a newly malloced string.
153 * *nameLen: The length of the malloced name string above.
154 *
155 * returns 1 if an error is encountered, or 0 if successful.
156 */
157 int
158 usm_parse_oid(oid * oidIndex, size_t oidLen,
159 unsigned char **engineID, size_t * engineIDLen,
160 unsigned char **name, size_t * nameLen)
161 {
162 int nameL;
163 int engineIDL;
164 int i;
165
166 /*
167 * first check the validity of the oid
168 */
169 if ((oidLen <= 0) || (!oidIndex)) {
170 DEBUGMSGTL(("usmUser",
171 "parse_oid: null oid or zero length oid passed in\n"));
172 return 1;
173 }
174 engineIDL = *oidIndex; /* initial engineID length */
175 if ((int) oidLen < engineIDL + 2) {
176 DEBUGMSGTL(("usmUser",
177 "parse_oid: invalid oid length: less than the engineIDLen\n"));
178 return 1;
179 }
180 nameL = oidIndex[engineIDL + 1]; /* the initial name length */
181 if ((int) oidLen != engineIDL + nameL + 2) {
182 DEBUGMSGTL(("usmUser",
183 "parse_oid: invalid oid length: length is not exact\n"));
184 return 1;
185 }
186
187 /*
188 * its valid, malloc the space and store the results
189 */
190 if (engineID == NULL || name == NULL) {
191 DEBUGMSGTL(("usmUser",
192 "parse_oid: null storage pointer passed in.\n"));
193 return 1;
194 }
195
196 *engineID = (unsigned char *) malloc(engineIDL);
197 if (*engineID == NULL) {
198 DEBUGMSGTL(("usmUser",
199 "parse_oid: malloc of the engineID failed\n"));
200 return 1;
201 }
202 *engineIDLen = engineIDL;
203
204 *name = (unsigned char *) malloc(nameL + 1);
205 if (*name == NULL) {
206 DEBUGMSGTL(("usmUser", "parse_oid: malloc of the name failed\n"));
207 free(*engineID);
208 return 1;
209 }
210 *nameLen = nameL;
211
212 for (i = 0; i < engineIDL; i++) {
213 if (oidIndex[i + 1] > 255) {
214 goto UPO_parse_error;
215 }
216 engineID[0][i] = (unsigned char) oidIndex[i + 1];
217 }
218
219 for (i = 0; i < nameL; i++) {
220 if (oidIndex[i + 2 + engineIDL] > 255) {
221 UPO_parse_error:
222 free(*engineID);
223 free(*name);
224 return 1;
225 }
226 name[0][i] = (unsigned char) oidIndex[i + 2 + engineIDL];
227 }
228 name[0][nameL] = 0;
229
230 return 0;
231
232 } /* end usm_parse_oid() */
233
234 /*******************************************************************-o-******
235 * usm_parse_user
236 *
237 * Parameters:
238 * *name Complete OID indexing a given usmUser entry.
239 * name_length
240 *
241 * Returns:
242 * Pointer to a usmUser -OR-
243 * NULL if name does not convert to a usmUser.
244 *
245 * Convert an (full) OID and return a pointer to a matching user in the
246 * user list if one exists.
247 */
248 struct usmUser *
249 usm_parse_user(oid * name, size_t name_len)
250 {
251 struct usmUser *uptr;
252
253 char *newName;
254 u_char *engineID;
255 size_t nameLen, engineIDLen;
256
257 /*
258 * get the name and engineID out of the incoming oid
259 */
260 if (usm_parse_oid(&name[USM_MIB_LENGTH], name_len - USM_MIB_LENGTH,
261 &engineID, &engineIDLen, (u_char **) & newName,
262 &nameLen))
263 return NULL;
264
265 /*
266 * Now see if a user exists with these index values
267 */
268 uptr = usm_get_user(engineID, engineIDLen, newName);
269 free(engineID);
270 free(newName);
271
272 return uptr;
273
274 } /* end usm_parse_user() */
275
276 /*******************************************************************-o-******
277 * var_usmUser
278 *
279 * Parameters:
280 * *vp (I) Variable-binding associated with this action.
281 * *name (I/O) Input name requested, output name found.
282 * *length (I/O) Length of input and output oid's.
283 * exact (I) TRUE if an exact match was requested.
284 * *var_len (O) Length of variable or 0 if function returned.
285 * (**write_method) Hook to name a write method (UNUSED).
286 *
287 * Returns:
288 * Pointer to (char *) containing related data of length 'length'
289 * (May be NULL.)
290 *
291 *
292 * Call-back function passed to the agent in order to return information
293 * for the USM MIB tree.
294 *
295 *
296 * If this invocation is not for USMUSERSPINLOCK, lookup user name
297 * in the usmUser list.
298 *
299 * If the name does not match any user and the request
300 * is for an exact match, -or- if the usmUser list is empty, create a
301 * new list entry.
302 *
303 * Finally, service the given USMUSER* var-bind. A NULL user generally
304 * results in a NULL return value.
305 */
306 u_char *
307 var_usmUser(struct variable * vp,
308 oid * name,
309 size_t * length,
310 int exact, size_t * var_len, WriteMethod ** write_method)
311 {
312 struct usmUser *uptr = NULL, *nptr;
313 int i, rtest, result;
314 oid *indexOid;
315 size_t len;
316
317 /*
318 * variables we may use later
319 */
320 static long long_ret;
321 #ifndef NETSNMP_NO_WRITE_SUPPORT
322 static u_char string[1];
323 static oid objid[2]; /* for .0.0 */
324 #endif
325
326 if (!vp || !name || !length || !var_len)
327 return NULL;
328
329 /* assume it isnt writable for the time being */
330 *write_method = (WriteMethod*)0;
331
332 /* assume an integer and change later if not */
333 *var_len = sizeof(long_ret);
334
335 if (vp->magic != USMUSERSPINLOCK) {
336 oid newname[MAX_OID_LEN];
337 len = (*length < vp->namelen) ? *length : vp->namelen;
338 rtest = snmp_oid_compare(name, len, vp->name, len);
339 if (rtest > 0 ||
340 /*
341 * (rtest == 0 && !exact && (int) vp->namelen+1 < (int) *length) ||
342 */
343 (exact == 1 && rtest != 0)) {
344 if (var_len)
345 *var_len = 0;
346 return NULL;
347 }
348 memset(newname, 0, sizeof(newname));
349 if (((int) *length) <= (int) vp->namelen || rtest == -1) {
350 /*
351 * oid is not within our range yet
352 */
353 /*
354 * need to fail if not exact
355 */
356 uptr = usm_get_userList();
357
358 } else {
359 for (nptr = usm_get_userList(), uptr = NULL;
360 nptr != NULL; nptr = nptr->next) {
361 indexOid =
362 usm_generate_OID(vp->name, vp->namelen, nptr, &len);
363 result = snmp_oid_compare(name, *length, indexOid, len);
364 DEBUGMSGTL(("9:usmUser", "Checking user: %s - ",
365 nptr->name));
366 for (i = 0; i < (int) nptr->engineIDLen; i++) {
367 DEBUGMSG(("9:usmUser", " %x", nptr->engineID[i]));
368 }
369 DEBUGMSG(("9:usmUser", " - %d \n -> OID: ", result));
370 DEBUGMSGOID(("9:usmUser", indexOid, len));
371 DEBUGMSG(("9:usmUser", "\n"));
372
373 free(indexOid);
374
375 if (exact) {
376 if (result == 0) {
377 uptr = nptr;
378 }
379 } else {
380 if (result == 0) {
381 /*
382 * found an exact match. Need the next one for !exact
383 */
384 uptr = nptr->next;
385 } else if (result == -1) {
386 uptr = nptr;
387 break;
388 }
389 }
390 }
391 } /* endif -- name <= vp->name */
392
393 /*
394 * if uptr is NULL and exact we need to continue for creates
395 */
396 if (uptr == NULL && !exact)
397 return (NULL);
398
399 if (uptr) {
400 indexOid = usm_generate_OID(vp->name, vp->namelen, uptr, &len);
401 *length = len;
402 memmove(name, indexOid, len * sizeof(oid));
403
404 DEBUGMSGTL(("usmUser", "Found user: %s - ", uptr->name));
405 for (i = 0; i < (int) uptr->engineIDLen; i++) {
406 DEBUGMSG(("usmUser", " %x", uptr->engineID[i]));
407 }
408 DEBUGMSG(("usmUser", "\n -> OID: "));
409 DEBUGMSGOID(("usmUser", indexOid, len));
410 DEBUGMSG(("usmUser", "\n"));
411
412 free(indexOid);
413 }
414 } else {
415 if (header_generic(vp, name, length, exact, var_len, write_method))
416 return NULL;
417 } /* endif -- vp->magic != USMUSERSPINLOCK */
418
419 switch (vp->magic) {
420 #ifndef NETSNMP_NO_WRITE_SUPPORT
421 case USMUSERSPINLOCK:
422 *write_method = write_usmUserSpinLock;
423 long_ret = usmUserSpinLock;
424 return (unsigned char *) &long_ret;
425
426 case USMUSERSECURITYNAME:
427 if (uptr) {
428 *var_len = strlen(uptr->secName);
429 return (unsigned char *) uptr->secName;
430 }
431 return NULL;
432
433 case USMUSERCLONEFROM:
434 *write_method = write_usmUserCloneFrom;
435 if (uptr) {
436 objid[0] = 0; /* "When this object is read, the ZeroDotZero OID */
437 objid[1] = 0; /* is returned." */
438 *var_len = sizeof(oid) * 2;
439 return (unsigned char *) objid;
440 }
441 return NULL;
442
443 case USMUSERAUTHPROTOCOL:
444 *write_method = write_usmUserAuthProtocol;
445 if (uptr) {
446 *var_len = uptr->authProtocolLen * sizeof(oid);
447 return (u_char *) uptr->authProtocol;
448 }
449 return NULL;
450
451 case USMUSERAUTHKEYCHANGE:
452 case USMUSEROWNAUTHKEYCHANGE:
453 /*
454 * we treat these the same, and let the calling module
455 * distinguish between them
456 */
457 *write_method = write_usmUserAuthKeyChange;
458 if (uptr) {
459 *string = 0; /* always return a NULL string */
460 *var_len = 0;
461 return string;
462 }
463 return NULL;
464
465 case USMUSERPRIVPROTOCOL:
466 *write_method = write_usmUserPrivProtocol;
467 if (uptr) {
468 *var_len = uptr->privProtocolLen * sizeof(oid);
469 return (u_char *) uptr->privProtocol;
470 }
471 return NULL;
472
473 case USMUSERPRIVKEYCHANGE:
474 case USMUSEROWNPRIVKEYCHANGE:
475 /*
476 * we treat these the same, and let the calling module
477 * distinguish between them
478 */
479 *write_method = write_usmUserPrivKeyChange;
480 if (uptr) {
481 *string = 0; /* always return a NULL string */
482 *var_len = 0;
483 return string;
484 }
485 return NULL;
486
487 case USMUSERPUBLIC:
488 *write_method = write_usmUserPublic;
489 if (uptr) {
490 if (uptr->userPublicString) {
491 *var_len = uptr->userPublicStringLen;
492 return uptr->userPublicString;
493 }
494 *string = 0;
495 *var_len = 0; /* return an empty string if the public
496 * string hasn't been defined yet */
497 return string;
498 }
499 return NULL;
500
501 case USMUSERSTORAGETYPE:
502 *write_method = write_usmUserStorageType;
503 if (uptr) {
504 long_ret = uptr->userStorageType;
505 return (unsigned char *) &long_ret;
506 }
507 return NULL;
508
509 case USMUSERSTATUS:
510 *write_method = write_usmUserStatus;
511 if (uptr) {
512 long_ret = uptr->userStatus;
513 return (unsigned char *) &long_ret;
514 }
515 return NULL;
516 default:
517 DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_usmUser\n",
518 vp->magic));
519 #else /* !NETSNMP_NO_WRITE_SUPPORT */
520 default:
521 DEBUGMSGTL(("snmpd", "no write support for var_usmUser\n"));
522 #endif /* !NETSNMP_NO_WRITE_SUPPORT */
523 }
524 return NULL;
525
526 } /* end var_usmUser() */
527
528
529 #ifndef NETSNMP_NO_WRITE_SUPPORT
530
531 /*
532 * write_usmUserSpinLock(): called when a set is performed on the
533 * usmUserSpinLock object
534 */
535 int
536 write_usmUserSpinLock(int action,
537 u_char * var_val,
538 u_char var_val_type,
539 size_t var_val_len,
540 u_char * statP, oid * name, size_t name_len)
541 {
542 /*
543 * variables we may use later
544 */
545 static long long_ret;
546
547 if (var_val_type != ASN_INTEGER) {
548 DEBUGMSGTL(("usmUser",
549 "write to usmUserSpinLock not ASN_INTEGER\n"));
550 return SNMP_ERR_WRONGTYPE;
551 }
552 if (var_val_len > sizeof(long_ret)) {
553 DEBUGMSGTL(("usmUser", "write to usmUserSpinLock: bad length\n"));
554 return SNMP_ERR_WRONGLENGTH;
555 }
556 long_ret = *((long *) var_val);
557 if (long_ret != (long) usmUserSpinLock)
558 return SNMP_ERR_INCONSISTENTVALUE;
559 if (action == COMMIT) {
560 if (usmUserSpinLock == 2147483647)
561 usmUserSpinLock = 0;
562 else
563 usmUserSpinLock++;
564 }
565 return SNMP_ERR_NOERROR;
566 } /* end write_usmUserSpinLock() */
567
568 /*******************************************************************-o-******
569 * write_usmUserCloneFrom
570 *
571 * Parameters:
572 * action
573 * *var_val
574 * var_val_type
575 * var_val_len
576 * *statP (UNUSED)
577 * *name OID of user to clone from.
578 * name_len
579 *
580 * Returns:
581 * SNMP_ERR_NOERROR On success -OR- If user exists
582 * and has already been cloned.
583 * SNMP_ERR_GENERR Local function call failures.
584 * SNMP_ERR_INCONSISTENTNAME 'name' does not exist in user list
585 * -OR- user to clone from != RS_ACTIVE.
586 * SNMP_ERR_WRONGLENGTH OID length > than local buffer size.
587 * SNMP_ERR_WRONGTYPE ASN_OBJECT_ID is wrong.
588 *
589 *
590 * XXX: should handle action=UNDO's.
591 */
592 int
593 write_usmUserCloneFrom(int action,
594 u_char * var_val,
595 u_char var_val_type,
596 size_t var_val_len,
597 u_char * statP, oid * name, size_t name_len)
598 {
599 struct usmUser *uptr, *cloneFrom;
600
601 if (action == RESERVE1) {
602 if (var_val_type != ASN_OBJECT_ID) {
603 DEBUGMSGTL(("usmUser",
604 "write to usmUserCloneFrom not ASN_OBJECT_ID\n"));
605 return SNMP_ERR_WRONGTYPE;
606 }
607 if (var_val_len > USM_LENGTH_OID_MAX * sizeof(oid) ||
608 var_val_len % sizeof(oid) != 0) {
609 DEBUGMSGTL(("usmUser",
610 "write to usmUserCloneFrom: bad length\n"));
611 return SNMP_ERR_WRONGLENGTH;
612 }
613 } else if (action == RESERVE2) {
614 if ((uptr = usm_parse_user(name, name_len)) == NULL) {
615 /*
616 * We don't allow creations here.
617 */
618 return SNMP_ERR_INCONSISTENTNAME;
619 }
620
621 /*
622 * Has the user already been cloned? If so, writes to this variable
623 * are defined to have no effect and to produce no error.
624 */
625 if (uptr->cloneFrom != NULL) {
626 return SNMP_ERR_NOERROR;
627 }
628
629 cloneFrom =
630 usm_parse_user((oid *) var_val, var_val_len / sizeof(oid));
631 if (cloneFrom == NULL || cloneFrom->userStatus != SNMP_ROW_ACTIVE) {
632 return SNMP_ERR_INCONSISTENTNAME;
633 }
634 uptr->cloneFrom = snmp_duplicate_objid((oid *) var_val,
635 var_val_len / sizeof(oid));
636 usm_cloneFrom_user(cloneFrom, uptr);
637
638 if (usmStatusCheck(uptr) && uptr->userStatus == SNMP_ROW_NOTREADY) {
639 uptr->userStatus = SNMP_ROW_NOTINSERVICE;
640 }
641 }
642
643 return SNMP_ERR_NOERROR;
644 }
645
646 /*******************************************************************-o-******
647 * write_usmUserAuthProtocol
648 *
649 * Parameters:
650 * action
651 * *var_val OID of auth transform to set.
652 * var_val_type
653 * var_val_len
654 * *statP
655 * *name OID of user upon which to perform set operation.
656 * name_len
657 *
658 * Returns:
659 * SNMP_ERR_NOERROR On success.
660 * SNMP_ERR_GENERR
661 * SNMP_ERR_INCONSISTENTVALUE
662 * SNMP_ERR_NOSUCHNAME
663 * SNMP_ERR_WRONGLENGTH
664 * SNMP_ERR_WRONGTYPE
665 */
666 int
667 write_usmUserAuthProtocol(int action,
668 u_char * var_val,
669 u_char var_val_type,
670 size_t var_val_len,
671 u_char * statP, oid * name, size_t name_len)
672 {
673 static oid *optr;
674 static size_t olen;
675 static int resetOnFail;
676 struct usmUser *uptr;
677
678 if (action == RESERVE1) {
679 resetOnFail = 0;
680 if (var_val_type != ASN_OBJECT_ID) {
681 DEBUGMSGTL(("usmUser",
682 "write to usmUserAuthProtocol not ASN_OBJECT_ID\n"));
683 return SNMP_ERR_WRONGTYPE;
684 }
685 if (var_val_len > USM_LENGTH_OID_MAX * sizeof(oid) ||
686 var_val_len % sizeof(oid) != 0) {
687 DEBUGMSGTL(("usmUser",
688 "write to usmUserAuthProtocol: bad length\n"));
689 return SNMP_ERR_WRONGLENGTH;
690 }
691 } else if (action == RESERVE2) {
692 if ((uptr = usm_parse_user(name, name_len)) == NULL) {
693 return SNMP_ERR_INCONSISTENTNAME;
694 }
695
696 if (uptr->userStatus == RS_ACTIVE
697 || uptr->userStatus == RS_NOTREADY
698 || uptr->userStatus == RS_NOTINSERVICE) {
699 /*
700 * The authProtocol is already set. It is only legal to CHANGE it
701 * to usmNoAuthProtocol...
702 */
703 if (snmp_oid_compare
704 ((oid *) var_val, var_val_len / sizeof(oid),
705 usmNoAuthProtocol,
706 sizeof(usmNoAuthProtocol) / sizeof(oid)) == 0) {
707 /*
708 * ... and then only if the privProtocol is equal to
709 * usmNoPrivProtocol.
710 */
711 if (snmp_oid_compare
712 (uptr->privProtocol, uptr->privProtocolLen,
713 usmNoPrivProtocol,
714 sizeof(usmNoPrivProtocol) / sizeof(oid)) != 0) {
715 return SNMP_ERR_INCONSISTENTVALUE;
716 }
717 optr = uptr->authProtocol;
718 olen = uptr->authProtocolLen;
719 resetOnFail = 1;
720 uptr->authProtocol = snmp_duplicate_objid((oid *) var_val,
721 var_val_len /
722 sizeof(oid));
723 if (uptr->authProtocol == NULL) {
724 return SNMP_ERR_RESOURCEUNAVAILABLE;
725 }
726 uptr->authProtocolLen = var_val_len / sizeof(oid);
727 } else
728 if (snmp_oid_compare
729 ((oid *) var_val, var_val_len / sizeof(oid),
730 uptr->authProtocol, uptr->authProtocolLen) == 0) {
731 /*
732 * But it's also okay to set it to the same thing as it
733 * currently is.
734 */
735 return SNMP_ERR_NOERROR;
736 } else {
737 return SNMP_ERR_INCONSISTENTVALUE;
738 }
739 } else {
740 /*
741 * This row is under creation. It's okay to set
742 * usmUserAuthProtocol to any valid authProtocol but it will be
743 * overwritten when usmUserCloneFrom is set (so don't write it if
744 * that has already been set).
745 */
746
747 if (snmp_oid_compare
748 ((oid *) var_val, var_val_len / sizeof(oid),
749 usmNoAuthProtocol,
750 sizeof(usmNoAuthProtocol) / sizeof(oid)) == 0
751 #ifndef NETSNMP_DISABLE_MD5
752 || snmp_oid_compare((oid *) var_val,
753 var_val_len / sizeof(oid),
754 usmHMACMD5AuthProtocol,
755 sizeof(usmHMACMD5AuthProtocol) /
756 sizeof(oid)) == 0
757 #endif
758 || snmp_oid_compare((oid *) var_val,
759 var_val_len / sizeof(oid),
760 usmHMACSHA1AuthProtocol,
761 sizeof(usmHMACSHA1AuthProtocol) /
762 sizeof(oid)) == 0) {
763 if (uptr->cloneFrom != NULL) {
764 optr = uptr->authProtocol;
765 olen = uptr->authProtocolLen;
766 resetOnFail = 1;
767 uptr->authProtocol =
768 snmp_duplicate_objid((oid *) var_val,
769 var_val_len / sizeof(oid));
770 if (uptr->authProtocol == NULL) {
771 return SNMP_ERR_RESOURCEUNAVAILABLE;
772 }
773 uptr->authProtocolLen = var_val_len / sizeof(oid);
774 }
775 } else {
776 /*
777 * Unknown authentication protocol.
778 */
779 return SNMP_ERR_WRONGVALUE;
780 }
781 }
782 } else if (action == COMMIT) {
783 SNMP_FREE(optr);
784 } else if (action == FREE || action == UNDO) {
785 if ((uptr = usm_parse_user(name, name_len)) != NULL) {
786 if (resetOnFail) {
787 SNMP_FREE(uptr->authProtocol);
788 uptr->authProtocol = optr;
789 uptr->authProtocolLen = olen;
790 }
791 }
792 }
793 return SNMP_ERR_NOERROR;
794 } /* end write_usmUserAuthProtocol() */
795
796 /*******************************************************************-o-******
797 * write_usmUserAuthKeyChange
798 *
799 * Parameters:
800 * action
801 * *var_val Octet string representing new KeyChange value.
802 * var_val_type
803 * var_val_len
804 * *statP (UNUSED)
805 * *name OID of user upon which to perform set operation.
806 * name_len
807 *
808 * Returns:
809 * SNMP_ERR_NOERR Success.
810 * SNMP_ERR_WRONGTYPE
811 * SNMP_ERR_WRONGLENGTH
812 * SNMP_ERR_NOSUCHNAME
813 * SNMP_ERR_GENERR
814 *
815 * Note: This function handles both the usmUserAuthKeyChange and
816 * usmUserOwnAuthKeyChange objects. We are not passed the name
817 * of the user requseting the keychange, so we leave this to the
818 * calling module to verify when and if we should be called. To
819 * change this would require a change in the mib module API to
820 * pass in the securityName requesting the change.
821 *
822 * XXX: should handle action=UNDO's.
823 */
824 int
825 write_usmUserAuthKeyChange(int action,
826 u_char * var_val,
827 u_char var_val_type,
828 size_t var_val_len,
829 u_char * statP, oid * name, size_t name_len)
830 {
831 struct usmUser *uptr;
832 unsigned char buf[SNMP_MAXBUF_SMALL];
833 size_t buflen = SNMP_MAXBUF_SMALL;
834 const char fnAuthKey[] = "write_usmUserAuthKeyChange";
835 const char fnOwnAuthKey[] = "write_usmUserOwnAuthKeyChange";
836 const char *fname;
837 static unsigned char *oldkey;
838 static size_t oldkeylen;
839 static int resetOnFail;
840
841 if (name[USM_MIB_LENGTH - 1] == 6) {
842 fname = fnAuthKey;
843 } else {
844 fname = fnOwnAuthKey;
845 }
846
847 if (action == RESERVE1) {
848 resetOnFail = 0;
849 if (var_val_type != ASN_OCTET_STR) {
850 DEBUGMSGTL(("usmUser", "write to %s not ASN_OCTET_STR\n",
851 fname));
852 return SNMP_ERR_WRONGTYPE;
853 }
854 if (var_val_len == 0) {
855 return SNMP_ERR_WRONGLENGTH;
856 }
857 } else if (action == RESERVE2) {
858 if ((uptr = usm_parse_user(name, name_len)) == NULL) {
859 return SNMP_ERR_INCONSISTENTNAME;
860 } else {
861 #ifndef NETSNMP_DISABLE_MD5
862 if (snmp_oid_compare(uptr->authProtocol, uptr->authProtocolLen,
863 usmHMACMD5AuthProtocol,
864 sizeof(usmHMACMD5AuthProtocol) /
865 sizeof(oid)) == 0) {
866 if (var_val_len != 0 && var_val_len != 32) {
867 return SNMP_ERR_WRONGLENGTH;
868 }
869 } else
870 #endif
871 if (snmp_oid_compare
872 (uptr->authProtocol, uptr->authProtocolLen,
873 usmHMACSHA1AuthProtocol,
874 sizeof(usmHMACSHA1AuthProtocol) / sizeof(oid)) == 0) {
875 if (var_val_len != 0 && var_val_len != 40) {
876 return SNMP_ERR_WRONGLENGTH;
877 }
878 }
879 }
880 } else if (action == ACTION) {
881 if ((uptr = usm_parse_user(name, name_len)) == NULL) {
882 return SNMP_ERR_INCONSISTENTNAME;
883 }
884 if (uptr->cloneFrom == NULL) {
885 return SNMP_ERR_INCONSISTENTNAME;
886 }
887 if (snmp_oid_compare(uptr->authProtocol, uptr->authProtocolLen,
888 usmNoAuthProtocol,
889 sizeof(usmNoAuthProtocol) / sizeof(oid)) ==
890 0) {
891 /*
892 * "When the value of the corresponding usmUserAuthProtocol is
893 * usmNoAuthProtocol, then a set is successful, but effectively
894 * is a no-op."
895 */
896 DEBUGMSGTL(("usmUser",
897 "%s: noAuthProtocol keyChange... success!\n",
898 fname));
899 return SNMP_ERR_NOERROR;
900 }
901
902 /*
903 * Change the key.
904 */
905 DEBUGMSGTL(("usmUser", "%s: changing auth key for user %s\n",
906 fname, uptr->secName));
907
908 if (decode_keychange(uptr->authProtocol, uptr->authProtocolLen,
909 uptr->authKey, uptr->authKeyLen,
910 var_val, var_val_len,
911 buf, &buflen) != SNMPERR_SUCCESS) {
912 DEBUGMSGTL(("usmUser", "%s: ... failed\n", fname));
913 return SNMP_ERR_GENERR;
914 }
915 DEBUGMSGTL(("usmUser", "%s: ... succeeded\n", fname));
916 resetOnFail = 1;
917 oldkey = uptr->authKey;
918 oldkeylen = uptr->authKeyLen;
919 uptr->authKey = netsnmp_memdup(buf, buflen);
920 if (uptr->authKey == NULL) {
921 return SNMP_ERR_RESOURCEUNAVAILABLE;
922 }
923 uptr->authKeyLen = buflen;
924 } else if (action == COMMIT) {
925 SNMP_FREE(oldkey);
926 } else if (action == UNDO) {
927 if ((uptr = usm_parse_user(name, name_len)) != NULL && resetOnFail) {
928 SNMP_FREE(uptr->authKey);
929 uptr->authKey = oldkey;
930 uptr->authKeyLen = oldkeylen;
931 }
932 }
933
934 return SNMP_ERR_NOERROR;
935 } /* end write_usmUserAuthKeyChange() */
936
937 int
938 write_usmUserPrivProtocol(int action,
939 u_char * var_val,
940 u_char var_val_type,
941 size_t var_val_len,
942 u_char * statP, oid * name, size_t name_len)
943 {
944 static oid *optr;
945 static size_t olen;
946 static int resetOnFail;
947 struct usmUser *uptr;
948
949 if (action == RESERVE1) {
950 resetOnFail = 0;
951 if (var_val_type != ASN_OBJECT_ID) {
952 DEBUGMSGTL(("usmUser",
953 "write to usmUserPrivProtocol not ASN_OBJECT_ID\n"));
954 return SNMP_ERR_WRONGTYPE;
955 }
956 if (var_val_len > USM_LENGTH_OID_MAX * sizeof(oid) ||
957 var_val_len % sizeof(oid) != 0) {
958 DEBUGMSGTL(("usmUser",
959 "write to usmUserPrivProtocol: bad length\n"));
960 return SNMP_ERR_WRONGLENGTH;
961 }
962 } else if (action == RESERVE2) {
963 if ((uptr = usm_parse_user(name, name_len)) == NULL) {
964 DEBUGMSGTL(("usmUser", "usm_parse_user() error\n"));
965 return SNMP_ERR_INCONSISTENTNAME;
966 }
967
968 if (uptr->userStatus == RS_ACTIVE
969 || uptr->userStatus == RS_NOTREADY
970 || uptr->userStatus == RS_NOTINSERVICE) {
971 /*
972 * The privProtocol is already set. It is only legal to CHANGE it
973 * to usmNoPrivProtocol.
974 */
975 if (snmp_oid_compare
976 ((oid *) var_val, var_val_len / sizeof(oid),
977 usmNoPrivProtocol,
978 sizeof(usmNoPrivProtocol) / sizeof(oid)) == 0) {
979 resetOnFail = 1;
980 optr = uptr->privProtocol;
981 olen = uptr->privProtocolLen;
982 uptr->privProtocol = snmp_duplicate_objid((oid *) var_val,
983 var_val_len /
984 sizeof(oid));
985 if (uptr->privProtocol == NULL) {
986 DEBUGMSGTL(("usmUser", "snmp_duplicate_objid() error\n"));
987 return SNMP_ERR_RESOURCEUNAVAILABLE;
988 }
989 uptr->privProtocolLen = var_val_len / sizeof(oid);
990 } else
991 if (snmp_oid_compare
992 ((oid *) var_val, var_val_len / sizeof(oid),
993 uptr->privProtocol, uptr->privProtocolLen) == 0) {
994 /*
995 * But it's also okay to set it to the same thing as it
996 * currently is.
997 */
998 return SNMP_ERR_NOERROR;
999 } else {
1000 DEBUGMSGTL(("usmUser", "inconsistent value error\n"));
1001 return SNMP_ERR_INCONSISTENTVALUE;
1002 }
1003 } else {
1004 /*
1005 * This row is under creation. It's okay to set
1006 * usmUserPrivProtocol to any valid privProtocol with the proviso
1007 * that if usmUserAuthProtocol is set to usmNoAuthProtocol, it may
1008 * only be set to usmNoPrivProtocol. The value will be overwritten
1009 * when usmUserCloneFrom is set (so don't write it if that has
1010 * already been set).
1011 */
1012 if (snmp_oid_compare(uptr->authProtocol, uptr->authProtocolLen,
1013 usmNoAuthProtocol,
1014 sizeof(usmNoAuthProtocol) /
1015 sizeof(oid)) == 0) {
1016 if (snmp_oid_compare
1017 ((oid *) var_val, var_val_len / sizeof(oid),
1018 usmNoPrivProtocol,
1019 sizeof(usmNoPrivProtocol) / sizeof(oid)) != 0) {
1020 DEBUGMSGTL(("usmUser", "inconsistent value error\n"));
1021 return SNMP_ERR_INCONSISTENTVALUE;
1022 }
1023 } else {
1024 if (snmp_oid_compare
1025 ((oid *) var_val, var_val_len / sizeof(oid),
1026 usmNoPrivProtocol,
1027 sizeof(usmNoPrivProtocol) / sizeof(oid)) != 0
1028 #ifndef NETSNMP_DISABLE_DES
1029 && snmp_oid_compare
1030 ((oid *) var_val, var_val_len / sizeof(oid),
1031 usmDESPrivProtocol,
1032 sizeof(usmDESPrivProtocol) / sizeof(oid)) != 0
1033 #endif
1034 && snmp_oid_compare
1035 ((oid *) var_val, var_val_len / sizeof(oid),
1036 usmAESPrivProtocol,
1037 sizeof(usmAESPrivProtocol) / sizeof(oid)) != 0) {
1038 DEBUGMSGTL(("usmUser", "wrong value error\n"));
1039 return SNMP_ERR_WRONGVALUE;
1040 }
1041 }
1042 resetOnFail = 1;
1043 optr = uptr->privProtocol;
1044 olen = uptr->privProtocolLen;
1045 uptr->privProtocol = snmp_duplicate_objid((oid *) var_val,
1046 var_val_len /
1047 sizeof(oid));
1048 if (uptr->privProtocol == NULL) {
1049 DEBUGMSGTL(("usmUser", "resource unavailable error\n"));
1050 return SNMP_ERR_RESOURCEUNAVAILABLE;
1051 }
1052 uptr->privProtocolLen = var_val_len / sizeof(oid);
1053 }
1054 } else if (action == COMMIT) {
1055 SNMP_FREE(optr);
1056 } else if (action == FREE || action == UNDO) {
1057 if ((uptr = usm_parse_user(name, name_len)) != NULL) {
1058 if (resetOnFail) {
1059 SNMP_FREE(uptr->privProtocol);
1060 uptr->privProtocol = optr;
1061 uptr->privProtocolLen = olen;
1062 }
1063 }
1064 }
1065
1066 return SNMP_ERR_NOERROR;
1067 } /* end write_usmUserPrivProtocol() */
1068
1069 /*
1070 * Note: This function handles both the usmUserPrivKeyChange and
1071 * usmUserOwnPrivKeyChange objects. We are not passed the name
1072 * of the user requseting the keychange, so we leave this to the
1073 * calling module to verify when and if we should be called. To
1074 * change this would require a change in the mib module API to
1075 * pass in the securityName requesting the change.
1076 *
1077 */
1078 int
1079 write_usmUserPrivKeyChange(int action,
1080 u_char * var_val,
1081 u_char var_val_type,
1082 size_t var_val_len,
1083 u_char * statP, oid * name, size_t name_len)
1084 {
1085 struct usmUser *uptr;
1086 unsigned char buf[SNMP_MAXBUF_SMALL], buf2[SNMP_MAXBUF_SMALL];
1087 size_t buflen = sizeof(buf);
1088 const char fnPrivKey[] = "write_usmUserPrivKeyChange";
1089 const char fnOwnPrivKey[] = "write_usmUserOwnPrivKeyChange";
1090 const char *fname;
1091 static unsigned char *oldkey;
1092 static size_t oldkeylen;
1093 static int resetOnFail;
1094 int plen;
1095
1096 if (name[USM_MIB_LENGTH - 1] == 9) {
1097 fname = fnPrivKey;
1098 } else {
1099 fname = fnOwnPrivKey;
1100 }
1101
1102 if (action == RESERVE1) {
1103 resetOnFail = 0;
1104 if (var_val_type != ASN_OCTET_STR) {
1105 DEBUGMSGTL(("usmUser", "write to %s not ASN_OCTET_STR\n",
1106 fname));
1107 return SNMP_ERR_WRONGTYPE;
1108 }
1109 if (var_val_len == 0) {
1110 return SNMP_ERR_WRONGLENGTH;
1111 }
1112 } else if (action == RESERVE2) {
1113 if ((uptr = usm_parse_user(name, name_len)) == NULL) {
1114 return SNMP_ERR_INCONSISTENTNAME;
1115 } else {
1116 const netsnmp_priv_alg_info *pai =
1117 sc_get_priv_alg_byoid(uptr->privProtocol,
1118 uptr->privProtocolLen);
1119 if (NULL == pai) {
1120 DEBUGMSGTL(("usmUser", "%s: unknown privProtocol\n",
1121 fname));
1122 return SNMP_ERR_GENERR;
1123 }
1124 plen = pai->proper_length;
1125 DEBUGMSGTL(("usmUser", "plen %d\n", plen));
1126 /*
1127 * ?? we store salt with key. See also the corresponding statement
1128 * in apps/snmpusm.c.
1129 */
1130 if (USM_CREATE_USER_PRIV_DES == pai->type)
1131 plen *= 2;
1132 if (var_val_len != 0 && var_val_len != (2 * plen)) {
1133 DEBUGMSGTL(("usmUser", "%s: bad len. %" NETSNMP_PRIz "d != %d\n",
1134 fname, var_val_len, 2 * plen));
1135 return SNMP_ERR_WRONGLENGTH;
1136 }
1137 }
1138 } else if (action == ACTION) {
1139
1140 if ((uptr = usm_parse_user(name, name_len)) == NULL) {
1141 return SNMP_ERR_INCONSISTENTNAME;
1142 }
1143 if (uptr->cloneFrom == NULL) {
1144 return SNMP_ERR_INCONSISTENTNAME;
1145 }
1146 plen = sc_get_proper_priv_length(uptr->privProtocol,
1147 uptr->privProtocolLen);
1148 DEBUGMSGTL(("usmUser", "plen %d\n", plen));
1149 if (snmp_oid_compare(uptr->privProtocol, uptr->privProtocolLen,
1150 usmNoPrivProtocol,
1151 sizeof(usmNoPrivProtocol) / sizeof(oid)) ==
1152 0) {
1153 /*
1154 * "When the value of the corresponding usmUserPrivProtocol is
1155 * usmNoPrivProtocol, then a set is successful, but effectively
1156 * is a no-op."
1157 */
1158 DEBUGMSGTL(("usmUser",
1159 "%s: noPrivProtocol keyChange... success!\n",
1160 fname));
1161 return SNMP_ERR_NOERROR;
1162 }
1163
1164 /*
1165 * extend key as needed
1166 */
1167 DEBUGMSGTL(("9:usmUser", "%s: var_val_len %" NETSNMP_PRIz "d\n", fname, var_val_len));
1168 if (var_val_len < ( 2 * plen )) {
1169 struct usmUser dummy;
1170 memset(&dummy, 0x0, sizeof(dummy));
1171 dummy.engineID = uptr->engineID;
1172 dummy.engineIDLen = uptr->engineIDLen;
1173 dummy.authProtocol = uptr->authProtocol;
1174 dummy.authProtocolLen = uptr->authProtocolLen;
1175 dummy.privProtocol = uptr->privProtocol;
1176 dummy.privProtocolLen = uptr->privProtocolLen;
1177 memcpy(buf2, var_val, var_val_len);
1178 dummy.privKey = buf2;
1179 dummy.privKeyLen = var_val_len;
1180 if (SNMP_ERR_NOERROR != usm_extend_user_kul(&dummy, sizeof(buf2))) {
1181 DEBUGMSGTL(("usmUser", "%s: extend kul failed\n", fname));
1182 return SNMP_ERR_GENERR;
1183 }
1184 DEBUGMSGTL(("9:usmUser", "%s: extend kul OK\n", fname));
1185 var_val = dummy.privKey;
1186 var_val_len = dummy.privKeyLen;
1187 /*
1188 * make sure no reallocation happened; buf2 must be large enoungh
1189 */
1190 netsnmp_assert( dummy.privKey == buf2 );
1191 }
1192
1193 /*
1194 * Change the key.
1195 */
1196 DEBUGMSGTL(("usmUser", "%s: changing priv key for user %s\n",
1197 fname, uptr->secName));
1198
1199 if (decode_keychange(uptr->authProtocol, uptr->authProtocolLen,
1200 uptr->privKey, uptr->privKeyLen,
1201 var_val, var_val_len,
1202 buf, &buflen) != SNMPERR_SUCCESS) {
1203 DEBUGMSGTL(("usmUser", "%s: ... failed\n", fname));
1204 return SNMP_ERR_GENERR;
1205 }
1206 DEBUGMSGTL(("usmUser", "%s: ... succeeded\n", fname));
1207 resetOnFail = 1;
1208 oldkey = uptr->privKey;
1209 oldkeylen = uptr->privKeyLen;
1210 uptr->privKey = netsnmp_memdup(buf, buflen);
1211 if (uptr->privKey == NULL) {
1212 return SNMP_ERR_RESOURCEUNAVAILABLE;
1213 }
1214 uptr->privKeyLen = buflen;
1215 } else if (action == COMMIT) {
1216 SNMP_FREE(oldkey);
1217 } else if (action == UNDO) {
1218 if ((uptr = usm_parse_user(name, name_len)) != NULL && resetOnFail) {
1219 SNMP_FREE(uptr->privKey);
1220 uptr->privKey = oldkey;
1221 uptr->privKeyLen = oldkeylen;
1222 }
1223 }
1224
1225 return SNMP_ERR_NOERROR;
1226 } /* end write_usmUserPrivKeyChange() */
1227
1228 int
1229 write_usmUserPublic(int action,
1230 u_char * var_val,
1231 u_char var_val_type,
1232 size_t var_val_len,
1233 u_char * statP, oid * name, size_t name_len)
1234 {
1235 struct usmUser *uptr = NULL;
1236
1237 if (var_val_type != ASN_OCTET_STR) {
1238 DEBUGMSGTL(("usmUser",
1239 "write to usmUserPublic not ASN_OCTET_STR\n"));
1240 return SNMP_ERR_WRONGTYPE;
1241 }
1242 if (var_val_len > 32) {
1243 DEBUGMSGTL(("usmUser", "write to usmUserPublic: bad length\n"));
1244 return SNMP_ERR_WRONGLENGTH;
1245 }
1246 if (action == COMMIT) {
1247 /*
1248 * don't allow creations here
1249 */
1250 if ((uptr = usm_parse_user(name, name_len)) == NULL) {
1251 return SNMP_ERR_NOSUCHNAME;
1252 }
1253 if (uptr->userPublicString)
1254 free(uptr->userPublicString);
1255 uptr->userPublicString = (u_char *) malloc(var_val_len);
1256 if (uptr->userPublicString == NULL) {
1257 return SNMP_ERR_GENERR;
1258 }
1259 memcpy(uptr->userPublicString, var_val, var_val_len);
1260 uptr->userPublicStringLen = var_val_len;
1261 DEBUGMSG(("usmUser", "setting public string: %d - ", (int)var_val_len));
1262 DEBUGMSGHEX(("usmUser", uptr->userPublicString, var_val_len));
1263 DEBUGMSG(("usmUser", "\n"));
1264 }
1265 return SNMP_ERR_NOERROR;
1266 } /* end write_usmUserPublic() */
1267
1268 int
1269 write_usmUserStorageType(int action,
1270 u_char * var_val,
1271 u_char var_val_type,
1272 size_t var_val_len,
1273 u_char * statP, oid * name, size_t name_len)
1274 {
1275 long long_ret = *((long *) var_val);
1276 static long oldValue;
1277 struct usmUser *uptr;
1278 static int resetOnFail;
1279
1280 if (action == RESERVE1) {
1281 resetOnFail = 0;
1282 if (var_val_type != ASN_INTEGER) {
1283 DEBUGMSGTL(("usmUser",
1284 "write to usmUserStorageType not ASN_INTEGER\n"));
1285 return SNMP_ERR_WRONGTYPE;
1286 }
1287 if (var_val_len != sizeof(long)) {
1288 DEBUGMSGTL(("usmUser",
1289 "write to usmUserStorageType: bad length\n"));
1290 return SNMP_ERR_WRONGLENGTH;
1291 }
1292 if (long_ret < 1 || long_ret > 5) {
1293 return SNMP_ERR_WRONGVALUE;
1294 }
1295 } else if (action == RESERVE2) {
1296 if ((uptr = usm_parse_user(name, name_len)) == NULL) {
1297 return SNMP_ERR_INCONSISTENTNAME;
1298 }
1299 if ((long_ret == ST_VOLATILE || long_ret == ST_NONVOLATILE) &&
1300 (uptr->userStorageType == ST_VOLATILE ||
1301 uptr->userStorageType == ST_NONVOLATILE)) {
1302 oldValue = uptr->userStorageType;
1303 uptr->userStorageType = long_ret;
1304 resetOnFail = 1;
1305 } else {
1306 /*
1307 * From RFC2574:
1308 *
1309 * "Note that any user who employs authentication or privacy must
1310 * allow its secret(s) to be updated and thus cannot be 'readOnly'.
1311 *
1312 * If an initial set operation tries to set the value to 'readOnly'
1313 * for a user who employs authentication or privacy, then an
1314 * 'inconsistentValue' error must be returned. Note that if the
1315 * value has been previously set (implicit or explicit) to any
1316 * value, then the rules as defined in the StorageType Textual
1317 * Convention apply.
1318 */
1319 DEBUGMSGTL(("usmUser",
1320 "long_ret %ld uptr->st %d uptr->status %d\n",
1321 long_ret, uptr->userStorageType,
1322 uptr->userStatus));
1323
1324 if (long_ret == ST_READONLY &&
1325 uptr->userStorageType != ST_READONLY &&
1326 (uptr->userStatus == RS_ACTIVE ||
1327 uptr->userStatus == RS_NOTINSERVICE)) {
1328 return SNMP_ERR_WRONGVALUE;
1329 } else if (long_ret == ST_READONLY &&
1330 (snmp_oid_compare
1331 (uptr->privProtocol, uptr->privProtocolLen,
1332 usmNoPrivProtocol,
1333 sizeof(usmNoPrivProtocol) / sizeof(oid)) != 0
1334 || snmp_oid_compare(uptr->authProtocol,
1335 uptr->authProtocolLen,
1336 usmNoAuthProtocol,
1337 sizeof(usmNoAuthProtocol) /
1338 sizeof(oid)) != 0)) {
1339 return SNMP_ERR_INCONSISTENTVALUE;
1340 } else {
1341 return SNMP_ERR_WRONGVALUE;
1342 }
1343 }
1344 } else if (action == UNDO || action == FREE) {
1345 if ((uptr = usm_parse_user(name, name_len)) != NULL && resetOnFail) {
1346 uptr->userStorageType = oldValue;
1347 }
1348 }
1349 return SNMP_ERR_NOERROR;
1350 } /* end write_usmUserStorageType() */
1351
1352
1353 /*
1354 * Return 1 if enough objects have been set up to transition rowStatus to
1355 * notInService(2) or active(1).
1356 */
1357
1358 int
1359 usmStatusCheck(struct usmUser *uptr)
1360 {
1361 if (uptr == NULL) {
1362 return 0;
1363 } else {
1364 if (uptr->cloneFrom == NULL) {
1365 return 0;
1366 }
1367 }
1368 return 1;
1369 }
1370
1371 /*******************************************************************-o-******
1372 * write_usmUserStatus
1373 *
1374 * Parameters:
1375 * action
1376 * *var_val
1377 * var_val_type
1378 * var_val_len
1379 * *statP
1380 * *name
1381 * name_len
1382 *
1383 * Returns:
1384 * SNMP_ERR_NOERROR On success.
1385 * SNMP_ERR_GENERR
1386 * SNMP_ERR_INCONSISTENTNAME
1387 * SNMP_ERR_INCONSISTENTVALUE
1388 * SNMP_ERR_WRONGLENGTH
1389 * SNMP_ERR_WRONGTYPE
1390 */
1391 int
1392 write_usmUserStatus(int action,
1393 u_char * var_val,
1394 u_char var_val_type,
1395 size_t var_val_len,
1396 u_char * statP, oid * name, size_t name_len)
1397 {
1398 /*
1399 * variables we may use later
1400 */
1401 static long long_ret;
1402 unsigned char *engineID;
1403 size_t engineIDLen;
1404 char *newName;
1405 size_t nameLen;
1406 struct usmUser *uptr = NULL;
1407
1408 if (action == RESERVE1) {
1409 if (var_val_type != ASN_INTEGER) {
1410 DEBUGMSGTL(("usmUser",
1411 "write to usmUserStatus not ASN_INTEGER\n"));
1412 return SNMP_ERR_WRONGTYPE;
1413 }
1414 if (var_val_len != sizeof(long_ret)) {
1415 DEBUGMSGTL(("usmUser",
1416 "write to usmUserStatus: bad length\n"));
1417 return SNMP_ERR_WRONGLENGTH;
1418 }
1419 long_ret = *((long *) var_val);
1420 if (long_ret == RS_NOTREADY || long_ret < 1 || long_ret > 6) {
1421 return SNMP_ERR_WRONGVALUE;
1422 }
1423
1424 /*
1425 * See if we can parse the oid for engineID/name first.
1426 */
1427 if (usm_parse_oid(&name[USM_MIB_LENGTH], name_len - USM_MIB_LENGTH,
1428 &engineID, &engineIDLen, (u_char **) & newName,
1429 &nameLen)) {
1430 DEBUGMSGTL(("usmUser",
1431 "can't parse the OID for engineID or name\n"));
1432 return SNMP_ERR_INCONSISTENTNAME;
1433 }
1434
1435 if (engineIDLen < 5 || engineIDLen > 32 || nameLen < 1
1436 || nameLen > 32) {
1437 SNMP_FREE(engineID);
1438 SNMP_FREE(newName);
1439 return SNMP_ERR_NOCREATION;
1440 }
1441
1442 /*
1443 * Now see if a user already exists with these index values.
1444 */
1445 uptr = usm_get_user(engineID, engineIDLen, newName);
1446
1447 if (uptr != NULL) {
1448 if (long_ret == RS_CREATEANDGO || long_ret == RS_CREATEANDWAIT) {
1449 SNMP_FREE(engineID);
1450 SNMP_FREE(newName);
1451 long_ret = RS_NOTREADY;
1452 return SNMP_ERR_INCONSISTENTVALUE;
1453 }
1454 SNMP_FREE(engineID);
1455 SNMP_FREE(newName);
1456 } else {
1457 if (long_ret == RS_ACTIVE || long_ret == RS_NOTINSERVICE) {
1458 SNMP_FREE(engineID);
1459 SNMP_FREE(newName);
1460 return SNMP_ERR_INCONSISTENTVALUE;
1461 }
1462 if (long_ret == RS_CREATEANDGO || long_ret == RS_CREATEANDWAIT) {
1463 if ((uptr = usm_create_user()) == NULL) {
1464 SNMP_FREE(engineID);
1465 SNMP_FREE(newName);
1466 return SNMP_ERR_RESOURCEUNAVAILABLE;
1467 }
1468 uptr->engineID = engineID;
1469 uptr->name = newName;
1470 uptr->secName = strdup(uptr->name);
1471 if (uptr->secName == NULL) {
1472 usm_free_user(uptr);
1473 return SNMP_ERR_RESOURCEUNAVAILABLE;
1474 }
1475 uptr->engineIDLen = engineIDLen;
1476
1477 /*
1478 * Set status to createAndGo or createAndWait so we can tell
1479 * that this row is under creation.
1480 */
1481
1482 uptr->userStatus = long_ret;
1483
1484 /*
1485 * Add to the list of users (we will take it off again
1486 * later if something goes wrong).
1487 */
1488
1489 usm_add_user(uptr);
1490 } else {
1491 SNMP_FREE(engineID);
1492 SNMP_FREE(newName);
1493 }
1494 }
1495 } else if (action == ACTION) {
1496 usm_parse_oid(&name[USM_MIB_LENGTH], name_len - USM_MIB_LENGTH,
1497 &engineID, &engineIDLen, (u_char **) & newName,
1498 &nameLen);
1499 uptr = usm_get_user(engineID, engineIDLen, newName);
1500 SNMP_FREE(engineID);
1501 SNMP_FREE(newName);
1502
1503 if (uptr != NULL) {
1504 if (long_ret == RS_CREATEANDGO || long_ret == RS_ACTIVE) {
1505 if (usmStatusCheck(uptr)) {
1506 uptr->userStatus = RS_ACTIVE;
1507 } else {
1508 return SNMP_ERR_INCONSISTENTVALUE;
1509 }
1510 } else if (long_ret == RS_CREATEANDWAIT) {
1511 if (usmStatusCheck(uptr)) {
1512 uptr->userStatus = RS_NOTINSERVICE;
1513 } else {
1514 uptr->userStatus = RS_NOTREADY;
1515 }
1516 } else if (long_ret == RS_NOTINSERVICE) {
1517 if (uptr->userStatus == RS_ACTIVE ||
1518 uptr->userStatus == RS_NOTINSERVICE) {
1519 uptr->userStatus = RS_NOTINSERVICE;
1520 } else {
1521 return SNMP_ERR_INCONSISTENTVALUE;
1522 }
1523 }
1524 }
1525 } else if (action == COMMIT) {
1526 usm_parse_oid(&name[USM_MIB_LENGTH], name_len - USM_MIB_LENGTH,
1527 &engineID, &engineIDLen, (u_char **) & newName,
1528 &nameLen);
1529 uptr = usm_get_user(engineID, engineIDLen, newName);
1530 SNMP_FREE(engineID);
1531 SNMP_FREE(newName);
1532
1533 if (uptr != NULL) {
1534 if (long_ret == RS_DESTROY) {
1535 usm_remove_user(uptr);
1536 usm_free_user(uptr);
1537 }
1538 }
1539 } else if (action == UNDO || action == FREE) {
1540 if (usm_parse_oid(&name[USM_MIB_LENGTH], name_len - USM_MIB_LENGTH,
1541 &engineID, &engineIDLen, (u_char **) & newName,
1542 &nameLen)) {
1543 /* Can't extract engine info from the OID - nothing to undo */
1544 return SNMP_ERR_NOERROR;
1545 }
1546 uptr = usm_get_user(engineID, engineIDLen, newName);
1547 SNMP_FREE(engineID);
1548 SNMP_FREE(newName);
1549
1550 if (long_ret == RS_CREATEANDGO || long_ret == RS_CREATEANDWAIT) {
1551 usm_remove_user(uptr);
1552 usm_free_user(uptr);
1553 }
1554 }
1555
1556 return SNMP_ERR_NOERROR;
1557 } /* write_usmUserStatus */
1558
1559 #endif /* !NETSNMP_NO_WRITE_SUPPORT */
1560
1561 #if 0
1562
1563 /*
1564 * see if we can parse the oid for engineID/name first
1565 */
1566 if (usm_parse_oid(&name[USM_MIB_LENGTH], name_len - USM_MIB_LENGTH,
1567 &engineID, &engineIDLen, (u_char **) & newName,
1568 &nameLen))
1569 return SNMP_ERR_INCONSISTENTNAME;
1570
1571 /*
1572 * Now see if a user already exists with these index values
1573 */
1574 uptr = usm_get_user(engineID, engineIDLen, newName);
1575
1576
1577 if (uptr) { /* If so, we set the appropriate value... */
1578 free(engineID);
1579 free(newName);
1580 if (long_ret == RS_CREATEANDGO || long_ret == RS_CREATEANDWAIT) {
1581 return SNMP_ERR_INCONSISTENTVALUE;
1582 }
1583 if (long_ret == RS_DESTROY) {
1584 usm_remove_user(uptr);
1585 usm_free_user(uptr);
1586 } else {
1587 uptr->userStatus = long_ret;
1588 }
1589
1590 } else { /* ...else we create a new user */
1591 /*
1592 * check for a valid status column set
1593 */
1594 if (long_ret == RS_ACTIVE || long_ret == RS_NOTINSERVICE) {
1595 free(engineID);
1596 free(newName);
1597 return SNMP_ERR_INCONSISTENTVALUE;
1598 }
1599 if (long_ret == RS_DESTROY) {
1600 /*
1601 * destroying a non-existent row is actually legal
1602 */
1603 free(engineID);
1604 free(newName);
1605 return SNMP_ERR_NOERROR;
1606 }
1607
1608 /*
1609 * generate a new user
1610 */
1611 if ((uptr = usm_create_user()) == NULL) {
1612 free(engineID);
1613 free(newName);
1614 return SNMP_ERR_GENERR;
1615 }
1616
1617 /*
1618 * copy in the engineID
1619 */
1620 uptr->engineID = (unsigned char *) malloc(engineIDLen);
1621 if (uptr->engineID == NULL) {
1622 free(engineID);
1623 free(newName);
1624 usm_free_user(uptr);
1625 return SNMP_ERR_GENERR;
1626 }
1627 uptr->engineIDLen = engineIDLen;
1628 memcpy(uptr->engineID, engineID, engineIDLen);
1629 free(engineID);
1630
1631 /*
1632 * copy in the name and secname
1633 */
1634 if ((uptr->name = strdup(newName)) == NULL) {
1635 free(newName);
1636 usm_free_user(uptr);
1637 return SNMP_ERR_GENERR;
1638 }
1639 free(newName);
1640 if ((uptr->secName = strdup(uptr->name)) == NULL) {
1641 usm_free_user(uptr);
1642 return SNMP_ERR_GENERR;
1643 }
1644
1645 /*
1646 * set the status of the row based on the request
1647 */
1648 if (long_ret == RS_CREATEANDGO)
1649 uptr->userStatus = RS_ACTIVE;
1650 else if (long_ret == RS_CREATEANDWAIT)
1651 uptr->userStatus = RS_NOTINSERVICE;
1652
1653 /*
1654 * finally, add it to our list of users
1655 */
1656 usm_add_user(uptr);
1657
1658 } /* endif -- uptr */
1659 } /* endif -- action==COMMIT */
1660
1661 return SNMP_ERR_NOERROR;
1662
1663 } /* end write_usmUserStatus() */
1664 #endif