]>
Commit | Line | Data |
---|---|---|
91c84a35 MS |
1 | /* |
2 | * "$Id$" | |
3 | * | |
4 | * SNMP functions for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 2007-2008 by Apple Inc. | |
7 | * Copyright 2006-2007 by Easy Software Products, all rights reserved. | |
8 | * | |
9 | * These coded instructions, statements, and computer programs are the | |
10 | * property of Apple Inc. and are protected by Federal copyright | |
11 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
12 | * "LICENSE" which should have been included with this file. If this | |
13 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
14 | * | |
15 | * This file is subject to the Apple OS-Developed Software exception. | |
16 | * | |
17 | * Contents: | |
18 | * | |
ac884b6a MS |
19 | * cupsSNMPClose() - Close a SNMP socket. |
20 | * cupsSNMPCopyOID() - Copy an OID. | |
21 | * cupsSNMPDefaultCommunity() - Get the default SNMP community name. | |
22 | * cupsSNMPIsOID() - Test whether a SNMP response contains the | |
23 | * specified OID. | |
24 | * cupsSNMPIsOIDPrefixed() - Test whether a SNMP response uses the | |
25 | * specified OID prefix. | |
26 | * cupsSNMPOpen() - Open a SNMP socket. | |
27 | * cupsSNMPRead() - Read and parse a SNMP response. | |
28 | * cupsSNMPSetDebug() - Enable/disable debug logging to stderr. | |
29 | * cupsSNMPWalk() - Enumerate a group of OIDs. | |
30 | * cupsSNMPWrite() - Send an SNMP query packet. | |
31 | * asn1_debug() - Decode an ASN1-encoded message. | |
32 | * asn1_decode_snmp() - Decode a SNMP packet. | |
33 | * asn1_encode_snmp() - Encode a SNMP packet. | |
34 | * asn1_get_integer() - Get an integer value. | |
35 | * asn1_get_length() - Get a value length. | |
36 | * asn1_get_oid() - Get an OID value. | |
37 | * asn1_get_packed() - Get a packed integer value. | |
38 | * asn1_get_string() - Get a string value. | |
39 | * asn1_get_type() - Get a value type. | |
40 | * asn1_set_integer() - Set an integer value. | |
41 | * asn1_set_length() - Set a value length. | |
42 | * asn1_set_oid() - Set an OID value. | |
43 | * asn1_set_packed() - Set a packed integer value. | |
44 | * asn1_size_integer() - Figure out the number of bytes needed for an | |
45 | * integer value. | |
46 | * asn1_size_length() - Figure out the number of bytes needed for a | |
47 | * length value. | |
48 | * asn1_size_oid() - Figure out the numebr of bytes needed for an | |
49 | * OID value. | |
50 | * asn1_size_packed() - Figure out the number of bytes needed for a | |
51 | * packed integer value. | |
52 | * snmp_set_error() - Set the localized error for a packet. | |
91c84a35 MS |
53 | */ |
54 | ||
55 | /* | |
56 | * Include necessary headers. | |
57 | */ | |
58 | ||
59 | #include "globals.h" | |
60 | #include "snmp.h" | |
61 | #include <errno.h> | |
62 | #ifdef HAVE_POLL | |
63 | # include <sys/poll.h> | |
64 | #endif /* HAVE_POLL */ | |
65 | ||
66 | ||
67 | /* | |
68 | * Local functions... | |
69 | */ | |
70 | ||
91c84a35 MS |
71 | static void asn1_debug(const char *prefix, unsigned char *buffer, |
72 | size_t len, int indent); | |
ac884b6a MS |
73 | static int asn1_decode_snmp(unsigned char *buffer, size_t len, |
74 | cups_snmp_t *packet); | |
91c84a35 MS |
75 | static int asn1_encode_snmp(unsigned char *buffer, size_t len, |
76 | cups_snmp_t *packet); | |
77 | static int asn1_get_integer(unsigned char **buffer, | |
78 | unsigned char *bufend, | |
79 | int length); | |
80 | static int asn1_get_oid(unsigned char **buffer, | |
81 | unsigned char *bufend, | |
82 | int length, int *oid, int oidsize); | |
83 | static int asn1_get_packed(unsigned char **buffer, | |
84 | unsigned char *bufend); | |
85 | static char *asn1_get_string(unsigned char **buffer, | |
86 | unsigned char *bufend, | |
87 | int length, char *string, | |
88 | int strsize); | |
89 | static int asn1_get_length(unsigned char **buffer, | |
90 | unsigned char *bufend); | |
91 | static int asn1_get_type(unsigned char **buffer, | |
92 | unsigned char *bufend); | |
93 | static void asn1_set_integer(unsigned char **buffer, | |
94 | int integer); | |
95 | static void asn1_set_length(unsigned char **buffer, | |
96 | int length); | |
97 | static void asn1_set_oid(unsigned char **buffer, | |
98 | const int *oid); | |
99 | static void asn1_set_packed(unsigned char **buffer, | |
100 | int integer); | |
101 | static int asn1_size_integer(int integer); | |
102 | static int asn1_size_length(int length); | |
103 | static int asn1_size_oid(const int *oid); | |
104 | static int asn1_size_packed(int integer); | |
105 | static void snmp_set_error(cups_snmp_t *packet, | |
106 | const char *message); | |
107 | ||
108 | ||
109 | /* | |
110 | * 'cupsSNMPClose()' - Close a SNMP socket. | |
111 | * | |
112 | * @since CUPS 1.4@ | |
113 | */ | |
114 | ||
115 | void | |
116 | cupsSNMPClose(int fd) /* I - SNMP socket file descriptor */ | |
117 | { | |
118 | #ifdef WIN32 | |
119 | closesocket(fd); | |
120 | #else | |
121 | close(fd); | |
122 | #endif /* WIN32 */ | |
123 | } | |
124 | ||
125 | ||
126 | /* | |
127 | * 'cupsSNMPCopyOID()' - Copy an OID. | |
ac884b6a MS |
128 | * |
129 | * The array pointed to by "src" is terminated by the value -1. | |
130 | * | |
131 | * @since CUPS 1.4@ | |
91c84a35 MS |
132 | */ |
133 | ||
134 | int * /* O - New OID */ | |
135 | cupsSNMPCopyOID(int *dst, /* I - Destination OID */ | |
136 | const int *src, /* I - Source OID */ | |
137 | int dstsize) /* I - Number of integers in dst */ | |
138 | { | |
139 | int i; /* Looping var */ | |
140 | ||
141 | ||
ac884b6a | 142 | for (i = 0, dstsize --; src[i] >= 0 && i < dstsize; i ++) |
91c84a35 MS |
143 | dst[i] = src[i]; |
144 | ||
ac884b6a | 145 | dst[i] = -1; |
91c84a35 MS |
146 | |
147 | return (dst); | |
148 | } | |
149 | ||
150 | ||
ac884b6a MS |
151 | /* |
152 | * 'cupsSNMPDefaultCommunity()' - Get the default SNMP community name. | |
153 | * | |
154 | * The default community name is the first community name found in the | |
155 | * snmp.conf file. If no community name is defined there, "public" is used. | |
156 | * | |
157 | * @since CUPS 1.4@ | |
158 | */ | |
159 | ||
160 | const char * /* O - Default community name */ | |
161 | cupsSNMPDefaultCommunity(void) | |
162 | { | |
163 | cups_file_t *fp; /* snmp.conf file */ | |
164 | char line[1024], /* Line from file */ | |
165 | *value; /* Value from file */ | |
166 | int linenum; /* Line number in file */ | |
167 | _cups_globals_t *cg = _cupsGlobals(); /* Global data */ | |
168 | ||
169 | ||
170 | if (!cg->snmp_community[0]) | |
171 | { | |
172 | strlcpy(cg->snmp_community, "public", sizeof(cg->snmp_community)); | |
173 | ||
174 | snprintf(line, sizeof(line), "%s/snmp.conf", cg->cups_serverroot); | |
175 | if ((fp = cupsFileOpen(line, "r")) != NULL) | |
176 | { | |
177 | linenum = 0; | |
178 | while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum)) | |
179 | if (!strcasecmp(line, "Community") && value) | |
180 | { | |
181 | strlcpy(cg->snmp_community, value, sizeof(cg->snmp_community)); | |
182 | break; | |
183 | } | |
184 | ||
185 | cupsFileClose(fp); | |
186 | } | |
187 | } | |
188 | ||
189 | return (cg->snmp_community); | |
190 | } | |
191 | ||
192 | ||
91c84a35 MS |
193 | /* |
194 | * 'cupsSNMPIsOID()' - Test whether a SNMP response contains the specified OID. | |
195 | * | |
ac884b6a | 196 | * The array pointed to by "oid" is terminated by the value -1. |
91c84a35 MS |
197 | * |
198 | * @since CUPS 1.4@ | |
199 | */ | |
200 | ||
201 | int /* O - 1 if equal, 0 if not equal */ | |
202 | cupsSNMPIsOID(cups_snmp_t *packet, /* I - Response packet */ | |
203 | const int *oid) /* I - OID */ | |
204 | { | |
205 | int i; /* Looping var */ | |
206 | ||
207 | ||
208 | /* | |
209 | * Range check input... | |
210 | */ | |
211 | ||
212 | if (!packet || !oid) | |
213 | return (0); | |
214 | ||
215 | /* | |
216 | * Compare OIDs... | |
217 | */ | |
218 | ||
ac884b6a MS |
219 | for (i = 0; |
220 | i < CUPS_SNMP_MAX_OID && oid[i] >= 0 && packet->object_name[i] >= 0; | |
221 | i ++) | |
91c84a35 MS |
222 | if (oid[i] != packet->object_name[i]) |
223 | return (0); | |
224 | ||
225 | return (i < CUPS_SNMP_MAX_OID && oid[i] == packet->object_name[i]); | |
226 | } | |
227 | ||
228 | ||
229 | /* | |
230 | * 'cupsSNMPIsOIDPrefixed()' - Test whether a SNMP response uses the specified | |
231 | * OID prefix. | |
232 | * | |
ac884b6a | 233 | * The array pointed to by "prefix" is terminated by the value -1. |
91c84a35 MS |
234 | * |
235 | * @since CUPS 1.4@ | |
236 | */ | |
237 | ||
238 | int /* O - 1 if prefixed, 0 if not prefixed */ | |
239 | cupsSNMPIsOIDPrefixed( | |
240 | cups_snmp_t *packet, /* I - Response packet */ | |
241 | const int *prefix) /* I - OID prefix */ | |
242 | { | |
243 | int i; /* Looping var */ | |
244 | ||
245 | ||
246 | /* | |
247 | * Range check input... | |
248 | */ | |
249 | ||
250 | if (!packet || !prefix) | |
251 | return (0); | |
252 | ||
253 | /* | |
254 | * Compare OIDs... | |
255 | */ | |
256 | ||
257 | for (i = 0; | |
ac884b6a | 258 | i < CUPS_SNMP_MAX_OID && prefix[i] >= 0 && packet->object_name[i] >= 0; |
91c84a35 MS |
259 | i ++) |
260 | if (prefix[i] != packet->object_name[i]) | |
261 | return (0); | |
262 | ||
263 | return (i < CUPS_SNMP_MAX_OID); | |
264 | } | |
265 | ||
266 | ||
267 | /* | |
268 | * 'cupsSNMPOpen()' - Open a SNMP socket. | |
269 | * | |
270 | * @since CUPS 1.4@ | |
271 | */ | |
272 | ||
273 | int /* O - SNMP socket file descriptor */ | |
ac884b6a | 274 | cupsSNMPOpen(int family) /* I - Address family - @code AF_INET@ or @code AF_INET6@ */ |
91c84a35 MS |
275 | { |
276 | int fd; /* SNMP socket file descriptor */ | |
277 | int val; /* Socket option value */ | |
278 | ||
279 | ||
280 | /* | |
281 | * Create the SNMP socket... | |
282 | */ | |
283 | ||
ac884b6a | 284 | if ((fd = socket(family, SOCK_DGRAM, 0)) < 0) |
91c84a35 MS |
285 | return (-1); |
286 | ||
287 | /* | |
288 | * Set the "broadcast" flag... | |
289 | */ | |
290 | ||
291 | val = 1; | |
292 | ||
293 | if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val))) | |
294 | { | |
295 | close(fd); | |
296 | ||
297 | return (-1); | |
298 | } | |
299 | ||
300 | return (fd); | |
301 | } | |
302 | ||
303 | ||
304 | /* | |
ac884b6a | 305 | * 'cupsSNMPRead()' - Read and parse a SNMP response. |
91c84a35 | 306 | * |
ac884b6a | 307 | * If "timeout" is negative, @code cupsSNMPRead@ will wait for a response |
91c84a35 MS |
308 | * indefinitely. |
309 | * | |
310 | * @since CUPS 1.4@ | |
311 | */ | |
312 | ||
ac884b6a | 313 | cups_snmp_t * /* O - SNMP packet or @code NULL@ if none */ |
91c84a35 MS |
314 | cupsSNMPRead(int fd, /* I - SNMP socket file descriptor */ |
315 | cups_snmp_t *packet, /* I - SNMP packet buffer */ | |
568fa3fa | 316 | double timeout) /* I - Timeout in seconds */ |
91c84a35 MS |
317 | { |
318 | unsigned char buffer[CUPS_SNMP_MAX_PACKET]; | |
319 | /* Data packet */ | |
320 | int bytes; /* Number of bytes received */ | |
321 | socklen_t addrlen; /* Source address length */ | |
ac884b6a | 322 | http_addr_t address; /* Source address */ |
91c84a35 MS |
323 | |
324 | ||
325 | /* | |
326 | * Range check input... | |
327 | */ | |
328 | ||
329 | if (fd < 0 || !packet) | |
330 | return (NULL); | |
331 | ||
332 | /* | |
333 | * Optionally wait for a response... | |
334 | */ | |
335 | ||
568fa3fa | 336 | if (timeout >= 0.0) |
91c84a35 MS |
337 | { |
338 | int ready; /* Data ready on socket? */ | |
339 | #ifdef HAVE_POLL | |
340 | struct pollfd pfd; /* Polled file descriptor */ | |
341 | ||
342 | pfd.fd = fd; | |
343 | pfd.events = POLLIN; | |
344 | ||
568fa3fa MS |
345 | while ((ready = poll(&pfd, 1, (int)(timeout * 1000.0))) < 0 && |
346 | errno == EINTR); | |
91c84a35 MS |
347 | |
348 | #else | |
349 | fd_set input_set; /* select() input set */ | |
568fa3fa | 350 | struct timeval stimeout; /* select() timeout */ |
91c84a35 MS |
351 | |
352 | do | |
353 | { | |
354 | FD_ZERO(&input_set); | |
355 | FD_SET(fd, &input_set); | |
356 | ||
568fa3fa MS |
357 | stimeout.tv_sec = (int)timeout; |
358 | stimeout.tv_usec = (int)((timeout - stimeout.tv_usec) * 1000000); | |
91c84a35 | 359 | |
568fa3fa | 360 | ready = select(fd + 1, &input_set, NULL, NULL, &stimeout); |
91c84a35 MS |
361 | } |
362 | # ifdef WIN32 | |
363 | while (ready < 0 && WSAGetLastError() == WSAEINTR); | |
364 | # else | |
365 | while (ready < 0 && errno == EINTR); | |
366 | # endif /* WIN32 */ | |
367 | #endif /* HAVE_POLL */ | |
368 | ||
369 | /* | |
370 | * If we don't have any data ready, return right away... | |
371 | */ | |
372 | ||
373 | if (ready <= 0) | |
374 | return (NULL); | |
375 | } | |
376 | ||
377 | /* | |
378 | * Read the response data... | |
379 | */ | |
380 | ||
ac884b6a | 381 | addrlen = sizeof(address); |
91c84a35 | 382 | |
ac884b6a MS |
383 | if ((bytes = recvfrom(fd, buffer, sizeof(buffer), 0, (void *)&address, |
384 | &addrlen)) < 0) | |
91c84a35 MS |
385 | return (NULL); |
386 | ||
387 | /* | |
388 | * Look for the response status code in the SNMP message header... | |
389 | */ | |
390 | ||
391 | asn1_debug("DEBUG: IN ", buffer, bytes, 0); | |
392 | ||
393 | asn1_decode_snmp(buffer, bytes, packet); | |
394 | ||
ac884b6a MS |
395 | memcpy(&(packet->address), &address, sizeof(packet->address)); |
396 | ||
91c84a35 MS |
397 | /* |
398 | * Return decoded data packet... | |
399 | */ | |
400 | ||
401 | return (packet); | |
402 | } | |
403 | ||
404 | ||
405 | /* | |
406 | * 'cupsSNMPSetDebug()' - Enable/disable debug logging to stderr. | |
407 | * | |
408 | * @since CUPS 1.4@ | |
409 | */ | |
410 | ||
411 | void | |
412 | cupsSNMPSetDebug(int level) /* I - 1 to enable debug output, 0 otherwise */ | |
413 | { | |
414 | _cups_globals_t *cg = _cupsGlobals(); /* Global data */ | |
415 | ||
416 | ||
417 | cg->snmp_debug = level; | |
418 | } | |
419 | ||
420 | ||
ac884b6a MS |
421 | /* |
422 | * 'cupsSNMPWalk()' - Enumerate a group of OIDs. | |
423 | * | |
424 | * This function queries all of the OIDs with the specified OID prefix, | |
425 | * calling the "cb" function for every response that is received. | |
426 | * | |
427 | * The array pointed to by "prefix" is terminated by the value -1. | |
428 | * | |
568fa3fa MS |
429 | * If "timeout" is negative, @code cupsSNMPWalk@ will wait for a response |
430 | * indefinitely. | |
431 | * | |
ac884b6a MS |
432 | * @since CUPS 1.4@ |
433 | */ | |
434 | ||
435 | int /* O - Number of OIDs found or -1 on error */ | |
436 | cupsSNMPWalk(int fd, /* I - SNMP socket */ | |
437 | http_addr_t *address, /* I - Address to query */ | |
438 | int version, /* I - SNMP version */ | |
439 | const char *community, /* I - Community name */ | |
440 | const int *prefix, /* I - OID prefix */ | |
568fa3fa | 441 | double timeout, /* I - Timeout for each response in seconds */ |
ac884b6a MS |
442 | cups_snmp_cb_t cb, /* I - Function to call for each response */ |
443 | void *data) /* I - User data pointer that is passed to the callback function */ | |
444 | { | |
445 | int count = 0; /* Number of OIDs found */ | |
446 | int request_id = 0; /* Current request ID */ | |
447 | cups_snmp_t packet; /* Current response packet */ | |
448 | ||
449 | ||
450 | /* | |
451 | * Range check input... | |
452 | */ | |
453 | ||
454 | if (fd < 0 || !address || version != CUPS_SNMP_VERSION_1 || !community || | |
455 | !prefix || !cb) | |
456 | return (-1); | |
457 | ||
458 | /* | |
459 | * Copy the OID prefix and then loop until we have no more OIDs... | |
460 | */ | |
461 | ||
462 | cupsSNMPCopyOID(packet.object_name, prefix, CUPS_SNMP_MAX_OID); | |
463 | ||
464 | for (;;) | |
465 | { | |
466 | request_id ++; | |
467 | ||
468 | if (!cupsSNMPWrite(fd, address, version, community, | |
469 | CUPS_ASN1_GET_NEXT_REQUEST, request_id, | |
470 | packet.object_name)) | |
471 | return (-1); | |
472 | ||
568fa3fa | 473 | if (!cupsSNMPRead(fd, &packet, timeout)) |
ac884b6a MS |
474 | return (-1); |
475 | ||
476 | if (!cupsSNMPIsOIDPrefixed(&packet, prefix)) | |
477 | return (count); | |
478 | ||
479 | if (packet.error || packet.error_status) | |
480 | return (count > 0 ? count : -1); | |
481 | ||
482 | count ++; | |
483 | ||
484 | (*cb)(&packet, data); | |
485 | } | |
486 | } | |
487 | ||
488 | ||
91c84a35 MS |
489 | /* |
490 | * 'cupsSNMPWrite()' - Send an SNMP query packet. | |
491 | * | |
ac884b6a | 492 | * The array pointed to by "oid" is terminated by the value -1. |
91c84a35 MS |
493 | * |
494 | * @since CUPS 1.4@ | |
495 | */ | |
496 | ||
497 | int /* O - 1 on success, 0 on error */ | |
498 | cupsSNMPWrite( | |
499 | int fd, /* I - SNMP socket */ | |
500 | http_addr_t *address, /* I - Address to send to */ | |
501 | int version, /* I - SNMP version */ | |
502 | const char *community, /* I - Community name */ | |
503 | cups_asn1_t request_type, /* I - Request type */ | |
504 | const unsigned request_id, /* I - Request ID */ | |
505 | const int *oid) /* I - OID */ | |
506 | { | |
507 | int i; /* Looping var */ | |
508 | cups_snmp_t packet; /* SNMP message packet */ | |
509 | unsigned char buffer[CUPS_SNMP_MAX_PACKET]; | |
510 | /* SNMP message buffer */ | |
511 | int bytes; /* Size of message */ | |
512 | ||
513 | ||
ac884b6a MS |
514 | /* |
515 | * Range check input... | |
516 | */ | |
517 | ||
518 | if (fd < 0 || !address || version != CUPS_SNMP_VERSION_1 || !community || | |
519 | (request_type != CUPS_ASN1_GET_REQUEST && | |
520 | request_type != CUPS_ASN1_GET_NEXT_REQUEST) || request_id < 1 || !oid) | |
521 | return (0); | |
522 | ||
91c84a35 MS |
523 | /* |
524 | * Create the SNMP message... | |
525 | */ | |
526 | ||
527 | memset(&packet, 0, sizeof(packet)); | |
528 | ||
529 | packet.version = version; | |
530 | packet.request_type = request_type; | |
531 | packet.request_id = request_id; | |
532 | packet.object_type = CUPS_ASN1_NULL_VALUE; | |
533 | ||
534 | strlcpy(packet.community, community, sizeof(packet.community)); | |
535 | ||
ac884b6a | 536 | for (i = 0; oid[i] >= 0 && i < (CUPS_SNMP_MAX_OID - 1); i ++) |
91c84a35 | 537 | packet.object_name[i] = oid[i]; |
ac884b6a MS |
538 | packet.object_name[i] = -1; |
539 | ||
540 | if (oid[i] >= 0) | |
541 | { | |
542 | errno = E2BIG; | |
543 | return (0); | |
544 | } | |
91c84a35 MS |
545 | |
546 | bytes = asn1_encode_snmp(buffer, sizeof(buffer), &packet); | |
547 | ||
548 | if (bytes < 0) | |
549 | { | |
550 | errno = E2BIG; | |
91c84a35 MS |
551 | return (0); |
552 | } | |
553 | ||
554 | asn1_debug("DEBUG: OUT ", buffer, bytes, 0); | |
555 | ||
556 | /* | |
557 | * Send the message... | |
558 | */ | |
559 | ||
560 | #ifdef AF_INET6 | |
561 | if (address->addr.sa_family == AF_INET6) | |
562 | address->ipv6.sin6_port = htons(CUPS_SNMP_PORT); | |
563 | else | |
564 | #endif /* AF_INET6 */ | |
565 | address->ipv4.sin_port = htons(CUPS_SNMP_PORT); | |
566 | ||
567 | return (sendto(fd, buffer, bytes, 0, (void *)address, | |
568 | httpAddrLength(address)) == bytes); | |
569 | } | |
570 | ||
571 | ||
ac884b6a MS |
572 | /* |
573 | * 'asn1_debug()' - Decode an ASN1-encoded message. | |
574 | */ | |
575 | ||
576 | static void | |
577 | asn1_debug(const char *prefix, /* I - Prefix string */ | |
578 | unsigned char *buffer, /* I - Buffer */ | |
579 | size_t len, /* I - Length of buffer */ | |
580 | int indent) /* I - Indentation */ | |
581 | { | |
582 | int i; /* Looping var */ | |
583 | unsigned char *bufend; /* End of buffer */ | |
584 | int integer; /* Number value */ | |
585 | int oid[CUPS_SNMP_MAX_OID]; /* OID value */ | |
586 | char string[CUPS_SNMP_MAX_STRING]; | |
587 | /* String value */ | |
588 | unsigned char value_type; /* Type of value */ | |
589 | int value_length; /* Length of value */ | |
590 | _cups_globals_t *cg = _cupsGlobals(); /* Global data */ | |
591 | ||
592 | ||
593 | if (cg->snmp_debug <= 0) | |
594 | return; | |
595 | ||
596 | if (cg->snmp_debug > 1 && indent == 0) | |
597 | { | |
598 | /* | |
599 | * Do a hex dump of the packet... | |
600 | */ | |
601 | ||
602 | int j; | |
603 | ||
604 | fprintf(stderr, "%sHex Dump (%d bytes):\n", prefix, (int)len); | |
605 | ||
606 | for (i = 0; i < len; i += 16) | |
607 | { | |
608 | fprintf(stderr, "%s%04x:", prefix, i); | |
609 | ||
610 | for (j = 0; j < 16 && (i + j) < len; j ++) | |
611 | { | |
612 | if (j && !(j & 3)) | |
613 | fprintf(stderr, " %02x", buffer[i + j]); | |
614 | else | |
615 | fprintf(stderr, " %02x", buffer[i + j]); | |
616 | } | |
617 | ||
618 | while (j < 16) | |
619 | { | |
620 | if (j && !(j & 3)) | |
621 | fputs(" ", stderr); | |
622 | else | |
623 | fputs(" ", stderr); | |
624 | ||
625 | j ++; | |
626 | } | |
627 | ||
628 | fputs(" ", stderr); | |
629 | ||
630 | for (j = 0; j < 16 && (i + j) < len; j ++) | |
631 | if (buffer[i + j] < ' ' || buffer[i + j] >= 0x7f) | |
632 | putc('.', stderr); | |
633 | else | |
634 | putc(buffer[i + j], stderr); | |
635 | ||
636 | putc('\n', stderr); | |
637 | } | |
638 | } | |
639 | ||
640 | if (indent == 0) | |
641 | fprintf(stderr, "%sMessage:\n", prefix); | |
642 | ||
643 | bufend = buffer + len; | |
644 | ||
645 | while (buffer < bufend) | |
646 | { | |
647 | /* | |
648 | * Get value type... | |
649 | */ | |
650 | ||
651 | value_type = asn1_get_type(&buffer, bufend); | |
652 | value_length = asn1_get_length(&buffer, bufend); | |
653 | ||
654 | switch (value_type) | |
655 | { | |
656 | case CUPS_ASN1_BOOLEAN : | |
657 | integer = asn1_get_integer(&buffer, bufend, value_length); | |
658 | ||
659 | fprintf(stderr, "%s%*sBOOLEAN %d bytes %d\n", prefix, indent, "", | |
660 | value_length, integer); | |
661 | break; | |
662 | ||
663 | case CUPS_ASN1_INTEGER : | |
664 | integer = asn1_get_integer(&buffer, bufend, value_length); | |
665 | ||
666 | fprintf(stderr, "%s%*sINTEGER %d bytes %d\n", prefix, indent, "", | |
667 | value_length, integer); | |
668 | break; | |
669 | ||
670 | case CUPS_ASN1_COUNTER : | |
671 | integer = asn1_get_integer(&buffer, bufend, value_length); | |
672 | ||
673 | fprintf(stderr, "%s%*sCOUNTER %d bytes %u\n", prefix, indent, "", | |
674 | value_length, (unsigned)integer); | |
675 | break; | |
676 | ||
677 | case CUPS_ASN1_GAUGE : | |
678 | integer = asn1_get_integer(&buffer, bufend, value_length); | |
679 | ||
680 | fprintf(stderr, "%s%*sGAUGE %d bytes %u\n", prefix, indent, "", | |
681 | value_length, (unsigned)integer); | |
682 | break; | |
683 | ||
684 | case CUPS_ASN1_TIMETICKS : | |
685 | integer = asn1_get_integer(&buffer, bufend, value_length); | |
686 | ||
687 | fprintf(stderr, "%s%*sTIMETICKS %d bytes %u\n", prefix, indent, "", | |
688 | value_length, (unsigned)integer); | |
689 | break; | |
690 | ||
691 | case CUPS_ASN1_OCTET_STRING : | |
692 | fprintf(stderr, "%s%*sOCTET STRING %d bytes \"%s\"\n", prefix, | |
693 | indent, "", value_length, | |
694 | asn1_get_string(&buffer, bufend, value_length, string, | |
695 | sizeof(string))); | |
696 | break; | |
697 | ||
698 | case CUPS_ASN1_HEX_STRING : | |
699 | asn1_get_string(&buffer, bufend, value_length, string, | |
700 | sizeof(string)); | |
701 | fprintf(stderr, "%s%*sHex-STRING %d bytes", prefix, | |
702 | indent, "", value_length); | |
703 | for (i = 0; i < value_length; i ++) | |
704 | fprintf(stderr, " %02X", string[i] & 255); | |
705 | putc('\n', stderr); | |
706 | break; | |
707 | ||
708 | case CUPS_ASN1_NULL_VALUE : | |
709 | fprintf(stderr, "%s%*sNULL VALUE %d bytes\n", prefix, indent, "", | |
710 | value_length); | |
711 | ||
712 | buffer += value_length; | |
713 | break; | |
714 | ||
715 | case CUPS_ASN1_OID : | |
716 | integer = asn1_get_oid(&buffer, bufend, value_length, oid, | |
717 | CUPS_SNMP_MAX_OID); | |
718 | ||
719 | fprintf(stderr, "%s%*sOID %d bytes ", prefix, indent, "", | |
720 | value_length); | |
721 | for (i = 0; i < integer; i ++) | |
722 | fprintf(stderr, ".%d", oid[i]); | |
723 | putc('\n', stderr); | |
724 | break; | |
725 | ||
726 | case CUPS_ASN1_SEQUENCE : | |
727 | fprintf(stderr, "%s%*sSEQUENCE %d bytes\n", prefix, indent, "", | |
728 | value_length); | |
729 | asn1_debug(prefix, buffer, value_length, indent + 4); | |
730 | ||
731 | buffer += value_length; | |
732 | break; | |
733 | ||
734 | case CUPS_ASN1_GET_NEXT_REQUEST : | |
735 | fprintf(stderr, "%s%*sGet-Next-Request-PDU %d bytes\n", prefix, | |
736 | indent, "", value_length); | |
737 | asn1_debug(prefix, buffer, value_length, indent + 4); | |
738 | ||
739 | buffer += value_length; | |
740 | break; | |
741 | ||
742 | case CUPS_ASN1_GET_REQUEST : | |
743 | fprintf(stderr, "%s%*sGet-Request-PDU %d bytes\n", prefix, indent, "", | |
744 | value_length); | |
745 | asn1_debug(prefix, buffer, value_length, indent + 4); | |
746 | ||
747 | buffer += value_length; | |
748 | break; | |
749 | ||
750 | case CUPS_ASN1_GET_RESPONSE : | |
751 | fprintf(stderr, "%s%*sGet-Response-PDU %d bytes\n", prefix, indent, | |
752 | "", value_length); | |
753 | asn1_debug(prefix, buffer, value_length, indent + 4); | |
754 | ||
755 | buffer += value_length; | |
756 | break; | |
757 | ||
758 | default : | |
759 | fprintf(stderr, "%s%*sUNKNOWN(%x) %d bytes\n", prefix, indent, "", | |
760 | value_type, value_length); | |
761 | ||
762 | buffer += value_length; | |
763 | break; | |
764 | } | |
765 | } | |
766 | } | |
767 | ||
768 | ||
91c84a35 MS |
769 | /* |
770 | * 'asn1_decode_snmp()' - Decode a SNMP packet. | |
771 | */ | |
772 | ||
773 | static int /* O - 0 on success, -1 on error */ | |
774 | asn1_decode_snmp(unsigned char *buffer, /* I - Buffer */ | |
775 | size_t len, /* I - Size of buffer */ | |
776 | cups_snmp_t *packet) /* I - SNMP packet */ | |
777 | { | |
778 | unsigned char *bufptr, /* Pointer into the data */ | |
779 | *bufend; /* End of data */ | |
780 | int length; /* Length of value */ | |
781 | ||
782 | ||
783 | /* | |
784 | * Initialize the decoding... | |
785 | */ | |
786 | ||
787 | memset(packet, 0, sizeof(cups_snmp_t)); | |
ac884b6a | 788 | packet->object_name[0] = -1; |
91c84a35 MS |
789 | |
790 | bufptr = buffer; | |
791 | bufend = buffer + len; | |
792 | ||
793 | if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_SEQUENCE) | |
794 | snmp_set_error(packet, _("Packet does not start with SEQUENCE")); | |
795 | else if (asn1_get_length(&bufptr, bufend) == 0) | |
796 | snmp_set_error(packet, _("SEQUENCE uses indefinite length")); | |
797 | else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_INTEGER) | |
798 | snmp_set_error(packet, _("No version number")); | |
799 | else if ((length = asn1_get_length(&bufptr, bufend)) == 0) | |
800 | snmp_set_error(packet, _("Version uses indefinite length")); | |
801 | else if ((packet->version = asn1_get_integer(&bufptr, bufend, length)) | |
802 | != CUPS_SNMP_VERSION_1) | |
803 | snmp_set_error(packet, _("Bad SNMP version number")); | |
804 | else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_OCTET_STRING) | |
805 | snmp_set_error(packet, _("No community name")); | |
806 | else if ((length = asn1_get_length(&bufptr, bufend)) == 0) | |
807 | snmp_set_error(packet, _("Community name uses indefinite length")); | |
808 | else | |
809 | { | |
810 | asn1_get_string(&bufptr, bufend, length, packet->community, | |
811 | sizeof(packet->community)); | |
812 | ||
813 | if ((packet->request_type = asn1_get_type(&bufptr, bufend)) | |
814 | != CUPS_ASN1_GET_RESPONSE) | |
815 | snmp_set_error(packet, _("Packet does not contain a Get-Response-PDU")); | |
816 | else if (asn1_get_length(&bufptr, bufend) == 0) | |
817 | snmp_set_error(packet, _("Get-Response-PDU uses indefinite length")); | |
818 | else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_INTEGER) | |
819 | snmp_set_error(packet, _("No request-id")); | |
820 | else if ((length = asn1_get_length(&bufptr, bufend)) == 0) | |
821 | snmp_set_error(packet, _("request-id uses indefinite length")); | |
822 | else | |
823 | { | |
824 | packet->request_id = asn1_get_integer(&bufptr, bufend, length); | |
825 | ||
826 | if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_INTEGER) | |
827 | snmp_set_error(packet, _("No error-status")); | |
828 | else if ((length = asn1_get_length(&bufptr, bufend)) == 0) | |
829 | snmp_set_error(packet, _("error-status uses indefinite length")); | |
830 | else | |
831 | { | |
832 | packet->error_status = asn1_get_integer(&bufptr, bufend, length); | |
833 | ||
834 | if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_INTEGER) | |
835 | snmp_set_error(packet, _("No error-index")); | |
836 | else if ((length = asn1_get_length(&bufptr, bufend)) == 0) | |
837 | snmp_set_error(packet, _("error-index uses indefinite length")); | |
838 | else | |
839 | { | |
840 | packet->error_index = asn1_get_integer(&bufptr, bufend, length); | |
841 | ||
842 | if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_SEQUENCE) | |
843 | snmp_set_error(packet, _("No variable-bindings SEQUENCE")); | |
844 | else if (asn1_get_length(&bufptr, bufend) == 0) | |
845 | snmp_set_error(packet, | |
846 | _("variable-bindings uses indefinite length")); | |
847 | else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_SEQUENCE) | |
848 | snmp_set_error(packet, _("No VarBind SEQUENCE")); | |
849 | else if (asn1_get_length(&bufptr, bufend) == 0) | |
850 | snmp_set_error(packet, _("VarBind uses indefinite length")); | |
851 | else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_OID) | |
852 | snmp_set_error(packet, _("No name OID")); | |
853 | else if ((length = asn1_get_length(&bufptr, bufend)) == 0) | |
854 | snmp_set_error(packet, _("Name OID uses indefinite length")); | |
855 | else | |
856 | { | |
857 | asn1_get_oid(&bufptr, bufend, length, packet->object_name, | |
858 | CUPS_SNMP_MAX_OID); | |
859 | ||
860 | packet->object_type = asn1_get_type(&bufptr, bufend); | |
861 | ||
862 | if ((length = asn1_get_length(&bufptr, bufend)) == 0 && | |
863 | packet->object_type != CUPS_ASN1_NULL_VALUE && | |
864 | packet->object_type != CUPS_ASN1_OCTET_STRING) | |
865 | snmp_set_error(packet, _("Value uses indefinite length")); | |
866 | else | |
867 | { | |
868 | switch (packet->object_type) | |
869 | { | |
870 | case CUPS_ASN1_BOOLEAN : | |
871 | packet->object_value.boolean = | |
872 | asn1_get_integer(&bufptr, bufend, length); | |
873 | break; | |
874 | ||
875 | case CUPS_ASN1_INTEGER : | |
876 | packet->object_value.integer = | |
877 | asn1_get_integer(&bufptr, bufend, length); | |
878 | break; | |
879 | ||
880 | case CUPS_ASN1_NULL_VALUE : | |
881 | break; | |
882 | ||
883 | case CUPS_ASN1_OCTET_STRING : | |
884 | asn1_get_string(&bufptr, bufend, length, | |
885 | packet->object_value.string, | |
886 | CUPS_SNMP_MAX_STRING); | |
887 | break; | |
888 | ||
889 | case CUPS_ASN1_OID : | |
890 | asn1_get_oid(&bufptr, bufend, length, | |
891 | packet->object_value.oid, CUPS_SNMP_MAX_OID); | |
892 | break; | |
893 | ||
ac884b6a MS |
894 | case CUPS_ASN1_HEX_STRING : |
895 | packet->object_value.hex_string.num_bytes = length; | |
896 | ||
897 | asn1_get_string(&bufptr, bufend, length, | |
898 | (char *)packet->object_value.hex_string.bytes, | |
899 | CUPS_SNMP_MAX_STRING); | |
900 | break; | |
901 | ||
91c84a35 MS |
902 | case CUPS_ASN1_COUNTER : |
903 | packet->object_value.counter = | |
904 | asn1_get_integer(&bufptr, bufend, length); | |
905 | break; | |
906 | ||
907 | case CUPS_ASN1_GAUGE : | |
908 | packet->object_value.gauge = | |
909 | asn1_get_integer(&bufptr, bufend, length); | |
910 | break; | |
911 | ||
ac884b6a MS |
912 | case CUPS_ASN1_TIMETICKS : |
913 | packet->object_value.timeticks = | |
914 | asn1_get_integer(&bufptr, bufend, length); | |
915 | break; | |
916 | ||
91c84a35 MS |
917 | default : |
918 | snmp_set_error(packet, _("Unsupported value type")); | |
919 | break; | |
920 | } | |
921 | } | |
922 | } | |
923 | } | |
924 | } | |
925 | } | |
926 | } | |
927 | ||
928 | return (packet->error ? -1 : 0); | |
929 | } | |
930 | ||
931 | ||
91c84a35 MS |
932 | /* |
933 | * 'asn1_encode_snmp()' - Encode a SNMP packet. | |
934 | */ | |
935 | ||
936 | static int /* O - Length on success, -1 on error */ | |
937 | asn1_encode_snmp(unsigned char *buffer, /* I - Buffer */ | |
938 | size_t bufsize, /* I - Size of buffer */ | |
939 | cups_snmp_t *packet) /* I - SNMP packet */ | |
940 | { | |
941 | unsigned char *bufptr; /* Pointer into buffer */ | |
942 | int total, /* Total length */ | |
943 | msglen, /* Length of entire message */ | |
944 | commlen, /* Length of community string */ | |
945 | reqlen, /* Length of request */ | |
946 | listlen, /* Length of variable list */ | |
947 | varlen, /* Length of variable */ | |
948 | namelen, /* Length of object name OID */ | |
949 | valuelen; /* Length of object value */ | |
950 | ||
951 | ||
952 | /* | |
953 | * Get the lengths of the community string, OID, and message... | |
954 | */ | |
955 | ||
ac884b6a | 956 | |
91c84a35 MS |
957 | namelen = asn1_size_oid(packet->object_name); |
958 | ||
959 | switch (packet->object_type) | |
960 | { | |
961 | case CUPS_ASN1_NULL_VALUE : | |
962 | valuelen = 0; | |
963 | break; | |
964 | ||
965 | case CUPS_ASN1_BOOLEAN : | |
966 | valuelen = asn1_size_integer(packet->object_value.boolean); | |
967 | break; | |
968 | ||
969 | case CUPS_ASN1_INTEGER : | |
970 | valuelen = asn1_size_integer(packet->object_value.integer); | |
971 | break; | |
972 | ||
973 | case CUPS_ASN1_OCTET_STRING : | |
974 | valuelen = strlen(packet->object_value.string); | |
975 | break; | |
976 | ||
977 | case CUPS_ASN1_OID : | |
978 | valuelen = asn1_size_oid(packet->object_value.oid); | |
979 | break; | |
980 | ||
981 | default : | |
982 | packet->error = "Unknown object type"; | |
983 | return (-1); | |
984 | } | |
985 | ||
986 | varlen = 1 + asn1_size_length(namelen) + namelen + | |
987 | 1 + asn1_size_length(valuelen) + valuelen; | |
988 | listlen = 1 + asn1_size_length(varlen) + varlen; | |
989 | reqlen = 2 + asn1_size_integer(packet->request_id) + | |
990 | 2 + asn1_size_integer(packet->error_status) + | |
991 | 2 + asn1_size_integer(packet->error_index) + | |
992 | 1 + asn1_size_length(listlen) + listlen; | |
993 | commlen = strlen(packet->community); | |
994 | msglen = 2 + asn1_size_integer(packet->version) + | |
995 | 1 + asn1_size_length(commlen) + commlen + | |
996 | 1 + asn1_size_length(reqlen) + reqlen; | |
997 | total = 1 + asn1_size_length(msglen) + msglen; | |
998 | ||
999 | if (total > bufsize) | |
1000 | { | |
1001 | packet->error = "Message too large for buffer"; | |
1002 | return (-1); | |
1003 | } | |
1004 | ||
1005 | /* | |
1006 | * Then format the message... | |
1007 | */ | |
1008 | ||
1009 | bufptr = buffer; | |
1010 | ||
1011 | *bufptr++ = CUPS_ASN1_SEQUENCE; /* SNMPv1 message header */ | |
1012 | asn1_set_length(&bufptr, msglen); | |
1013 | ||
1014 | asn1_set_integer(&bufptr, packet->version); | |
1015 | /* version */ | |
1016 | ||
1017 | *bufptr++ = CUPS_ASN1_OCTET_STRING; /* community */ | |
1018 | asn1_set_length(&bufptr, commlen); | |
1019 | memcpy(bufptr, packet->community, commlen); | |
1020 | bufptr += commlen; | |
1021 | ||
ac884b6a | 1022 | *bufptr++ = packet->request_type; /* Get-Request-PDU/Get-Next-Request-PDU */ |
91c84a35 MS |
1023 | asn1_set_length(&bufptr, reqlen); |
1024 | ||
1025 | asn1_set_integer(&bufptr, packet->request_id); | |
1026 | ||
1027 | asn1_set_integer(&bufptr, packet->error_status); | |
1028 | ||
1029 | asn1_set_integer(&bufptr, packet->error_index); | |
1030 | ||
1031 | *bufptr++ = CUPS_ASN1_SEQUENCE; /* variable-bindings */ | |
1032 | asn1_set_length(&bufptr, listlen); | |
1033 | ||
1034 | *bufptr++ = CUPS_ASN1_SEQUENCE; /* variable */ | |
1035 | asn1_set_length(&bufptr, varlen); | |
1036 | ||
1037 | asn1_set_oid(&bufptr, packet->object_name); | |
1038 | /* ObjectName */ | |
1039 | ||
1040 | switch (packet->object_type) | |
1041 | { | |
1042 | case CUPS_ASN1_NULL_VALUE : | |
1043 | *bufptr++ = CUPS_ASN1_NULL_VALUE; | |
1044 | /* ObjectValue */ | |
1045 | *bufptr++ = 0; /* Length */ | |
1046 | break; | |
1047 | ||
1048 | case CUPS_ASN1_BOOLEAN : | |
1049 | asn1_set_integer(&bufptr, packet->object_value.boolean); | |
1050 | break; | |
1051 | ||
1052 | case CUPS_ASN1_INTEGER : | |
1053 | asn1_set_integer(&bufptr, packet->object_value.integer); | |
1054 | break; | |
1055 | ||
1056 | case CUPS_ASN1_OCTET_STRING : | |
1057 | *bufptr++ = CUPS_ASN1_OCTET_STRING; | |
1058 | asn1_set_length(&bufptr, valuelen); | |
1059 | memcpy(bufptr, packet->object_value.string, valuelen); | |
1060 | bufptr += valuelen; | |
1061 | break; | |
1062 | ||
1063 | case CUPS_ASN1_OID : | |
1064 | asn1_set_oid(&bufptr, packet->object_value.oid); | |
1065 | break; | |
1066 | ||
1067 | default : | |
1068 | break; | |
1069 | } | |
1070 | ||
1071 | return (bufptr - buffer); | |
1072 | } | |
1073 | ||
1074 | ||
1075 | /* | |
1076 | * 'asn1_get_integer()' - Get an integer value. | |
1077 | */ | |
1078 | ||
1079 | static int /* O - Integer value */ | |
1080 | asn1_get_integer( | |
1081 | unsigned char **buffer, /* IO - Pointer in buffer */ | |
1082 | unsigned char *bufend, /* I - End of buffer */ | |
1083 | int length) /* I - Length of value */ | |
1084 | { | |
1085 | int value; /* Integer value */ | |
1086 | ||
1087 | ||
1088 | for (value = 0; | |
1089 | length > 0 && *buffer < bufend; | |
1090 | length --, (*buffer) ++) | |
1091 | value = (value << 8) | **buffer; | |
1092 | ||
1093 | return (value); | |
1094 | } | |
1095 | ||
1096 | ||
1097 | /* | |
1098 | * 'asn1_get_length()' - Get a value length. | |
1099 | */ | |
1100 | ||
1101 | static int /* O - Length */ | |
1102 | asn1_get_length(unsigned char **buffer, /* IO - Pointer in buffer */ | |
1103 | unsigned char *bufend) /* I - End of buffer */ | |
1104 | { | |
1105 | int length; /* Length */ | |
1106 | ||
1107 | ||
1108 | length = **buffer; | |
1109 | (*buffer) ++; | |
1110 | ||
1111 | if (length & 128) | |
1112 | length = asn1_get_integer(buffer, bufend, length & 127); | |
1113 | ||
1114 | return (length); | |
1115 | } | |
1116 | ||
1117 | ||
1118 | /* | |
1119 | * 'asn1_get_oid()' - Get an OID value. | |
1120 | */ | |
1121 | ||
ac884b6a | 1122 | static int /* O - Number of OIDs */ |
91c84a35 MS |
1123 | asn1_get_oid( |
1124 | unsigned char **buffer, /* IO - Pointer in buffer */ | |
1125 | unsigned char *bufend, /* I - End of buffer */ | |
1126 | int length, /* I - Length of value */ | |
1127 | int *oid, /* I - OID buffer */ | |
1128 | int oidsize) /* I - Size of OID buffer */ | |
1129 | { | |
1130 | unsigned char *valend; /* End of value */ | |
ac884b6a MS |
1131 | int *oidptr, /* Current OID */ |
1132 | *oidend; /* End of OID buffer */ | |
91c84a35 MS |
1133 | int number; /* OID number */ |
1134 | ||
1135 | ||
1136 | valend = *buffer + length; | |
ac884b6a | 1137 | oidptr = oid; |
91c84a35 MS |
1138 | oidend = oid + oidsize - 1; |
1139 | ||
1140 | if (valend > bufend) | |
1141 | valend = bufend; | |
1142 | ||
1143 | number = asn1_get_packed(buffer, bufend); | |
1144 | ||
1145 | if (number < 80) | |
1146 | { | |
ac884b6a MS |
1147 | *oidptr++ = number / 40; |
1148 | number = number % 40; | |
1149 | *oidptr++ = number; | |
91c84a35 MS |
1150 | } |
1151 | else | |
1152 | { | |
ac884b6a MS |
1153 | *oidptr++ = 2; |
1154 | number -= 80; | |
1155 | *oidptr++ = number; | |
91c84a35 MS |
1156 | } |
1157 | ||
1158 | while (*buffer < valend) | |
1159 | { | |
1160 | number = asn1_get_packed(buffer, bufend); | |
1161 | ||
ac884b6a MS |
1162 | if (oidptr < oidend) |
1163 | *oidptr++ = number; | |
91c84a35 MS |
1164 | } |
1165 | ||
ac884b6a | 1166 | *oidptr = -1; |
91c84a35 | 1167 | |
ac884b6a | 1168 | return (oidptr - oid); |
91c84a35 MS |
1169 | } |
1170 | ||
1171 | ||
1172 | /* | |
1173 | * 'asn1_get_packed()' - Get a packed integer value. | |
1174 | */ | |
1175 | ||
1176 | static int /* O - Value */ | |
1177 | asn1_get_packed( | |
1178 | unsigned char **buffer, /* IO - Pointer in buffer */ | |
1179 | unsigned char *bufend) /* I - End of buffer */ | |
1180 | { | |
1181 | int value; /* Value */ | |
1182 | ||
1183 | ||
1184 | value = 0; | |
1185 | ||
1186 | while ((**buffer & 128) && *buffer < bufend) | |
1187 | { | |
1188 | value = (value << 7) | (**buffer & 127); | |
1189 | (*buffer) ++; | |
1190 | } | |
1191 | ||
1192 | if (*buffer < bufend) | |
1193 | { | |
1194 | value = (value << 7) | **buffer; | |
1195 | (*buffer) ++; | |
1196 | } | |
1197 | ||
1198 | return (value); | |
1199 | } | |
1200 | ||
1201 | ||
1202 | /* | |
1203 | * 'asn1_get_string()' - Get a string value. | |
1204 | */ | |
1205 | ||
1206 | static char * /* O - String */ | |
1207 | asn1_get_string( | |
1208 | unsigned char **buffer, /* IO - Pointer in buffer */ | |
1209 | unsigned char *bufend, /* I - End of buffer */ | |
1210 | int length, /* I - Value length */ | |
1211 | char *string, /* I - String buffer */ | |
1212 | int strsize) /* I - String buffer size */ | |
1213 | { | |
1214 | if (length < 0) | |
1215 | { | |
1216 | /* | |
1217 | * Disallow negative lengths! | |
1218 | */ | |
1219 | ||
1220 | *string = '\0'; | |
1221 | } | |
1222 | else if (length < strsize) | |
1223 | { | |
1224 | /* | |
1225 | * String is smaller than the buffer... | |
1226 | */ | |
1227 | ||
1228 | if (length > 0) | |
1229 | memcpy(string, *buffer, length); | |
1230 | ||
1231 | string[length] = '\0'; | |
1232 | } | |
1233 | else | |
1234 | { | |
1235 | /* | |
1236 | * String is larger than the buffer... | |
1237 | */ | |
1238 | ||
1239 | memcpy(string, *buffer, strsize - 1); | |
1240 | string[strsize - 1] = '\0'; | |
1241 | } | |
1242 | ||
1243 | if (length > 0) | |
1244 | (*buffer) += length; | |
1245 | ||
1246 | return (length < 0 ? NULL : string); | |
1247 | } | |
1248 | ||
1249 | ||
1250 | /* | |
1251 | * 'asn1_get_type()' - Get a value type. | |
1252 | */ | |
1253 | ||
1254 | static int /* O - Type */ | |
1255 | asn1_get_type(unsigned char **buffer, /* IO - Pointer in buffer */ | |
1256 | unsigned char *bufend) /* I - End of buffer */ | |
1257 | { | |
1258 | int type; /* Type */ | |
1259 | ||
1260 | ||
1261 | type = **buffer; | |
1262 | (*buffer) ++; | |
1263 | ||
1264 | if ((type & 31) == 31) | |
1265 | type = asn1_get_packed(buffer, bufend); | |
1266 | ||
1267 | return (type); | |
1268 | } | |
1269 | ||
1270 | ||
1271 | /* | |
1272 | * 'asn1_set_integer()' - Set an integer value. | |
1273 | */ | |
1274 | ||
1275 | static void | |
1276 | asn1_set_integer(unsigned char **buffer,/* IO - Pointer in buffer */ | |
1277 | int integer) /* I - Integer value */ | |
1278 | { | |
1279 | **buffer = CUPS_ASN1_INTEGER; | |
1280 | (*buffer) ++; | |
1281 | ||
1282 | if (integer > 0x7fffff || integer < -0x800000) | |
1283 | { | |
1284 | **buffer = 4; | |
1285 | (*buffer) ++; | |
1286 | **buffer = integer >> 24; | |
1287 | (*buffer) ++; | |
1288 | **buffer = integer >> 16; | |
1289 | (*buffer) ++; | |
1290 | **buffer = integer >> 8; | |
1291 | (*buffer) ++; | |
1292 | **buffer = integer; | |
1293 | (*buffer) ++; | |
1294 | } | |
1295 | else if (integer > 0x7fff || integer < -0x8000) | |
1296 | { | |
1297 | **buffer = 3; | |
1298 | (*buffer) ++; | |
1299 | **buffer = integer >> 16; | |
1300 | (*buffer) ++; | |
1301 | **buffer = integer >> 8; | |
1302 | (*buffer) ++; | |
1303 | **buffer = integer; | |
1304 | (*buffer) ++; | |
1305 | } | |
1306 | else if (integer > 0x7f || integer < -0x80) | |
1307 | { | |
1308 | **buffer = 2; | |
1309 | (*buffer) ++; | |
1310 | **buffer = integer >> 8; | |
1311 | (*buffer) ++; | |
1312 | **buffer = integer; | |
1313 | (*buffer) ++; | |
1314 | } | |
1315 | else | |
1316 | { | |
1317 | **buffer = 1; | |
1318 | (*buffer) ++; | |
1319 | **buffer = integer; | |
1320 | (*buffer) ++; | |
1321 | } | |
1322 | } | |
1323 | ||
1324 | ||
1325 | /* | |
1326 | * 'asn1_set_length()' - Set a value length. | |
1327 | */ | |
1328 | ||
1329 | static void | |
1330 | asn1_set_length(unsigned char **buffer, /* IO - Pointer in buffer */ | |
1331 | int length) /* I - Length value */ | |
1332 | { | |
1333 | if (length > 255) | |
1334 | { | |
1335 | **buffer = 0x82; /* 2-byte length */ | |
1336 | (*buffer) ++; | |
1337 | **buffer = length >> 8; | |
1338 | (*buffer) ++; | |
1339 | **buffer = length; | |
1340 | (*buffer) ++; | |
1341 | } | |
1342 | else if (length > 127) | |
1343 | { | |
1344 | **buffer = 0x81; /* 1-byte length */ | |
1345 | (*buffer) ++; | |
1346 | **buffer = length; | |
1347 | (*buffer) ++; | |
1348 | } | |
1349 | else | |
1350 | { | |
1351 | **buffer = length; /* Length */ | |
1352 | (*buffer) ++; | |
1353 | } | |
1354 | } | |
1355 | ||
1356 | ||
1357 | /* | |
1358 | * 'asn1_set_oid()' - Set an OID value. | |
1359 | */ | |
1360 | ||
1361 | static void | |
1362 | asn1_set_oid(unsigned char **buffer, /* IO - Pointer in buffer */ | |
1363 | const int *oid) /* I - OID value */ | |
1364 | { | |
1365 | **buffer = CUPS_ASN1_OID; | |
1366 | (*buffer) ++; | |
1367 | ||
1368 | asn1_set_length(buffer, asn1_size_oid(oid)); | |
1369 | ||
ac884b6a MS |
1370 | if (oid[1] < 0) |
1371 | { | |
1372 | asn1_set_packed(buffer, oid[0] * 40); | |
1373 | return; | |
1374 | } | |
1375 | ||
91c84a35 MS |
1376 | asn1_set_packed(buffer, oid[0] * 40 + oid[1]); |
1377 | ||
ac884b6a | 1378 | for (oid += 2; *oid >= 0; oid ++) |
91c84a35 MS |
1379 | asn1_set_packed(buffer, *oid); |
1380 | } | |
1381 | ||
1382 | ||
1383 | /* | |
1384 | * 'asn1_set_packed()' - Set a packed integer value. | |
1385 | */ | |
1386 | ||
1387 | static void | |
1388 | asn1_set_packed(unsigned char **buffer, /* IO - Pointer in buffer */ | |
1389 | int integer) /* I - Integer value */ | |
1390 | { | |
1391 | if (integer > 0xfffffff) | |
1392 | { | |
ac884b6a | 1393 | **buffer = ((integer >> 28) & 0x7f) | 0x80; |
91c84a35 MS |
1394 | (*buffer) ++; |
1395 | } | |
1396 | ||
1397 | if (integer > 0x1fffff) | |
1398 | { | |
ac884b6a | 1399 | **buffer = ((integer >> 21) & 0x7f) | 0x80; |
91c84a35 MS |
1400 | (*buffer) ++; |
1401 | } | |
1402 | ||
1403 | if (integer > 0x3fff) | |
1404 | { | |
ac884b6a | 1405 | **buffer = ((integer >> 14) & 0x7f) | 0x80; |
91c84a35 MS |
1406 | (*buffer) ++; |
1407 | } | |
1408 | ||
1409 | if (integer > 0x7f) | |
1410 | { | |
ac884b6a | 1411 | **buffer = ((integer >> 7) & 0x7f) | 0x80; |
91c84a35 MS |
1412 | (*buffer) ++; |
1413 | } | |
1414 | ||
1415 | **buffer = integer & 0x7f; | |
1416 | (*buffer) ++; | |
1417 | } | |
1418 | ||
1419 | ||
1420 | /* | |
1421 | * 'asn1_size_integer()' - Figure out the number of bytes needed for an | |
1422 | * integer value. | |
1423 | */ | |
1424 | ||
1425 | static int /* O - Size in bytes */ | |
1426 | asn1_size_integer(int integer) /* I - Integer value */ | |
1427 | { | |
1428 | if (integer > 0x7fffff || integer < -0x800000) | |
1429 | return (4); | |
1430 | else if (integer > 0x7fff || integer < -0x8000) | |
1431 | return (3); | |
1432 | else if (integer > 0x7f || integer < -0x80) | |
1433 | return (2); | |
1434 | else | |
1435 | return (1); | |
1436 | } | |
1437 | ||
1438 | ||
1439 | /* | |
1440 | * 'asn1_size_length()' - Figure out the number of bytes needed for a | |
1441 | * length value. | |
1442 | */ | |
1443 | ||
1444 | static int /* O - Size in bytes */ | |
1445 | asn1_size_length(int length) /* I - Length value */ | |
1446 | { | |
1447 | if (length > 0xff) | |
1448 | return (3); | |
1449 | else if (length > 0x7f) | |
1450 | return (2); | |
1451 | else | |
1452 | return (1); | |
1453 | } | |
1454 | ||
1455 | ||
1456 | /* | |
1457 | * 'asn1_size_oid()' - Figure out the numebr of bytes needed for an | |
1458 | * OID value. | |
1459 | */ | |
1460 | ||
1461 | static int /* O - Size in bytes */ | |
1462 | asn1_size_oid(const int *oid) /* I - OID value */ | |
1463 | { | |
1464 | int length; /* Length of value */ | |
1465 | ||
1466 | ||
ac884b6a MS |
1467 | if (oid[1] < 0) |
1468 | return (asn1_size_packed(oid[0] * 40)); | |
1469 | ||
1470 | for (length = asn1_size_packed(oid[0] * 40 + oid[1]), oid += 2; | |
1471 | *oid >= 0; | |
1472 | oid ++) | |
91c84a35 MS |
1473 | length += asn1_size_packed(*oid); |
1474 | ||
1475 | return (length); | |
1476 | } | |
1477 | ||
1478 | ||
1479 | /* | |
1480 | * 'asn1_size_packed()' - Figure out the number of bytes needed for a | |
1481 | * packed integer value. | |
1482 | */ | |
1483 | ||
1484 | static int /* O - Size in bytes */ | |
1485 | asn1_size_packed(int integer) /* I - Integer value */ | |
1486 | { | |
1487 | if (integer > 0xfffffff) | |
1488 | return (5); | |
1489 | else if (integer > 0x1fffff) | |
1490 | return (4); | |
1491 | else if (integer > 0x3fff) | |
1492 | return (3); | |
1493 | else if (integer > 0x7f) | |
1494 | return (2); | |
1495 | else | |
1496 | return (1); | |
1497 | } | |
1498 | ||
1499 | ||
1500 | /* | |
1501 | * 'snmp_set_error()' - Set the localized error for a packet. | |
1502 | */ | |
1503 | ||
1504 | static void | |
1505 | snmp_set_error(cups_snmp_t *packet, /* I - Packet */ | |
1506 | const char *message) /* I - Error message */ | |
1507 | { | |
1508 | _cups_globals_t *cg = _cupsGlobals(); /* Global data */ | |
1509 | ||
1510 | ||
1511 | if (!cg->lang_default) | |
1512 | cg->lang_default = cupsLangDefault(); | |
1513 | ||
1514 | packet->error = _cupsLangString(cg->lang_default, message); | |
1515 | } | |
1516 | ||
1517 | ||
1518 | /* | |
1519 | * End of "$Id$". | |
1520 | */ |