]>
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 */ | |
316 | int msec) /* I - Timeout in milliseconds */ | |
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 | ||
336 | if (msec >= 0) | |
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 | ||
345 | while ((ready = poll(&pfd, 1, msec)) < 0 && errno == EINTR); | |
346 | ||
347 | #else | |
348 | fd_set input_set; /* select() input set */ | |
349 | struct timeval timeout; /* select() timeout */ | |
350 | ||
351 | do | |
352 | { | |
353 | FD_ZERO(&input_set); | |
354 | FD_SET(fd, &input_set); | |
355 | ||
356 | timeout.tv_sec = msec / 1000; | |
357 | timeout.tv_usec = (msec % 1000) * 1000; | |
358 | ||
359 | ready = select(fd + 1, &input_set, NULL, NULL, &timeout); | |
360 | } | |
361 | # ifdef WIN32 | |
362 | while (ready < 0 && WSAGetLastError() == WSAEINTR); | |
363 | # else | |
364 | while (ready < 0 && errno == EINTR); | |
365 | # endif /* WIN32 */ | |
366 | #endif /* HAVE_POLL */ | |
367 | ||
368 | /* | |
369 | * If we don't have any data ready, return right away... | |
370 | */ | |
371 | ||
372 | if (ready <= 0) | |
373 | return (NULL); | |
374 | } | |
375 | ||
376 | /* | |
377 | * Read the response data... | |
378 | */ | |
379 | ||
ac884b6a | 380 | addrlen = sizeof(address); |
91c84a35 | 381 | |
ac884b6a MS |
382 | if ((bytes = recvfrom(fd, buffer, sizeof(buffer), 0, (void *)&address, |
383 | &addrlen)) < 0) | |
91c84a35 MS |
384 | return (NULL); |
385 | ||
386 | /* | |
387 | * Look for the response status code in the SNMP message header... | |
388 | */ | |
389 | ||
390 | asn1_debug("DEBUG: IN ", buffer, bytes, 0); | |
391 | ||
392 | asn1_decode_snmp(buffer, bytes, packet); | |
393 | ||
ac884b6a MS |
394 | memcpy(&(packet->address), &address, sizeof(packet->address)); |
395 | ||
91c84a35 MS |
396 | /* |
397 | * Return decoded data packet... | |
398 | */ | |
399 | ||
400 | return (packet); | |
401 | } | |
402 | ||
403 | ||
404 | /* | |
405 | * 'cupsSNMPSetDebug()' - Enable/disable debug logging to stderr. | |
406 | * | |
407 | * @since CUPS 1.4@ | |
408 | */ | |
409 | ||
410 | void | |
411 | cupsSNMPSetDebug(int level) /* I - 1 to enable debug output, 0 otherwise */ | |
412 | { | |
413 | _cups_globals_t *cg = _cupsGlobals(); /* Global data */ | |
414 | ||
415 | ||
416 | cg->snmp_debug = level; | |
417 | } | |
418 | ||
419 | ||
ac884b6a MS |
420 | /* |
421 | * 'cupsSNMPWalk()' - Enumerate a group of OIDs. | |
422 | * | |
423 | * This function queries all of the OIDs with the specified OID prefix, | |
424 | * calling the "cb" function for every response that is received. | |
425 | * | |
426 | * The array pointed to by "prefix" is terminated by the value -1. | |
427 | * | |
428 | * @since CUPS 1.4@ | |
429 | */ | |
430 | ||
431 | int /* O - Number of OIDs found or -1 on error */ | |
432 | cupsSNMPWalk(int fd, /* I - SNMP socket */ | |
433 | http_addr_t *address, /* I - Address to query */ | |
434 | int version, /* I - SNMP version */ | |
435 | const char *community, /* I - Community name */ | |
436 | const int *prefix, /* I - OID prefix */ | |
437 | int msec, /* I - Timeout for each response in milliseconds */ | |
438 | cups_snmp_cb_t cb, /* I - Function to call for each response */ | |
439 | void *data) /* I - User data pointer that is passed to the callback function */ | |
440 | { | |
441 | int count = 0; /* Number of OIDs found */ | |
442 | int request_id = 0; /* Current request ID */ | |
443 | cups_snmp_t packet; /* Current response packet */ | |
444 | ||
445 | ||
446 | /* | |
447 | * Range check input... | |
448 | */ | |
449 | ||
450 | if (fd < 0 || !address || version != CUPS_SNMP_VERSION_1 || !community || | |
451 | !prefix || !cb) | |
452 | return (-1); | |
453 | ||
454 | /* | |
455 | * Copy the OID prefix and then loop until we have no more OIDs... | |
456 | */ | |
457 | ||
458 | cupsSNMPCopyOID(packet.object_name, prefix, CUPS_SNMP_MAX_OID); | |
459 | ||
460 | for (;;) | |
461 | { | |
462 | request_id ++; | |
463 | ||
464 | if (!cupsSNMPWrite(fd, address, version, community, | |
465 | CUPS_ASN1_GET_NEXT_REQUEST, request_id, | |
466 | packet.object_name)) | |
467 | return (-1); | |
468 | ||
469 | if (!cupsSNMPRead(fd, &packet, msec)) | |
470 | return (-1); | |
471 | ||
472 | if (!cupsSNMPIsOIDPrefixed(&packet, prefix)) | |
473 | return (count); | |
474 | ||
475 | if (packet.error || packet.error_status) | |
476 | return (count > 0 ? count : -1); | |
477 | ||
478 | count ++; | |
479 | ||
480 | (*cb)(&packet, data); | |
481 | } | |
482 | } | |
483 | ||
484 | ||
91c84a35 MS |
485 | /* |
486 | * 'cupsSNMPWrite()' - Send an SNMP query packet. | |
487 | * | |
ac884b6a | 488 | * The array pointed to by "oid" is terminated by the value -1. |
91c84a35 MS |
489 | * |
490 | * @since CUPS 1.4@ | |
491 | */ | |
492 | ||
493 | int /* O - 1 on success, 0 on error */ | |
494 | cupsSNMPWrite( | |
495 | int fd, /* I - SNMP socket */ | |
496 | http_addr_t *address, /* I - Address to send to */ | |
497 | int version, /* I - SNMP version */ | |
498 | const char *community, /* I - Community name */ | |
499 | cups_asn1_t request_type, /* I - Request type */ | |
500 | const unsigned request_id, /* I - Request ID */ | |
501 | const int *oid) /* I - OID */ | |
502 | { | |
503 | int i; /* Looping var */ | |
504 | cups_snmp_t packet; /* SNMP message packet */ | |
505 | unsigned char buffer[CUPS_SNMP_MAX_PACKET]; | |
506 | /* SNMP message buffer */ | |
507 | int bytes; /* Size of message */ | |
508 | ||
509 | ||
ac884b6a MS |
510 | /* |
511 | * Range check input... | |
512 | */ | |
513 | ||
514 | if (fd < 0 || !address || version != CUPS_SNMP_VERSION_1 || !community || | |
515 | (request_type != CUPS_ASN1_GET_REQUEST && | |
516 | request_type != CUPS_ASN1_GET_NEXT_REQUEST) || request_id < 1 || !oid) | |
517 | return (0); | |
518 | ||
91c84a35 MS |
519 | /* |
520 | * Create the SNMP message... | |
521 | */ | |
522 | ||
523 | memset(&packet, 0, sizeof(packet)); | |
524 | ||
525 | packet.version = version; | |
526 | packet.request_type = request_type; | |
527 | packet.request_id = request_id; | |
528 | packet.object_type = CUPS_ASN1_NULL_VALUE; | |
529 | ||
530 | strlcpy(packet.community, community, sizeof(packet.community)); | |
531 | ||
ac884b6a | 532 | for (i = 0; oid[i] >= 0 && i < (CUPS_SNMP_MAX_OID - 1); i ++) |
91c84a35 | 533 | packet.object_name[i] = oid[i]; |
ac884b6a MS |
534 | packet.object_name[i] = -1; |
535 | ||
536 | if (oid[i] >= 0) | |
537 | { | |
538 | errno = E2BIG; | |
539 | return (0); | |
540 | } | |
91c84a35 MS |
541 | |
542 | bytes = asn1_encode_snmp(buffer, sizeof(buffer), &packet); | |
543 | ||
544 | if (bytes < 0) | |
545 | { | |
546 | errno = E2BIG; | |
91c84a35 MS |
547 | return (0); |
548 | } | |
549 | ||
550 | asn1_debug("DEBUG: OUT ", buffer, bytes, 0); | |
551 | ||
552 | /* | |
553 | * Send the message... | |
554 | */ | |
555 | ||
556 | #ifdef AF_INET6 | |
557 | if (address->addr.sa_family == AF_INET6) | |
558 | address->ipv6.sin6_port = htons(CUPS_SNMP_PORT); | |
559 | else | |
560 | #endif /* AF_INET6 */ | |
561 | address->ipv4.sin_port = htons(CUPS_SNMP_PORT); | |
562 | ||
563 | return (sendto(fd, buffer, bytes, 0, (void *)address, | |
564 | httpAddrLength(address)) == bytes); | |
565 | } | |
566 | ||
567 | ||
ac884b6a MS |
568 | /* |
569 | * 'asn1_debug()' - Decode an ASN1-encoded message. | |
570 | */ | |
571 | ||
572 | static void | |
573 | asn1_debug(const char *prefix, /* I - Prefix string */ | |
574 | unsigned char *buffer, /* I - Buffer */ | |
575 | size_t len, /* I - Length of buffer */ | |
576 | int indent) /* I - Indentation */ | |
577 | { | |
578 | int i; /* Looping var */ | |
579 | unsigned char *bufend; /* End of buffer */ | |
580 | int integer; /* Number value */ | |
581 | int oid[CUPS_SNMP_MAX_OID]; /* OID value */ | |
582 | char string[CUPS_SNMP_MAX_STRING]; | |
583 | /* String value */ | |
584 | unsigned char value_type; /* Type of value */ | |
585 | int value_length; /* Length of value */ | |
586 | _cups_globals_t *cg = _cupsGlobals(); /* Global data */ | |
587 | ||
588 | ||
589 | if (cg->snmp_debug <= 0) | |
590 | return; | |
591 | ||
592 | if (cg->snmp_debug > 1 && indent == 0) | |
593 | { | |
594 | /* | |
595 | * Do a hex dump of the packet... | |
596 | */ | |
597 | ||
598 | int j; | |
599 | ||
600 | fprintf(stderr, "%sHex Dump (%d bytes):\n", prefix, (int)len); | |
601 | ||
602 | for (i = 0; i < len; i += 16) | |
603 | { | |
604 | fprintf(stderr, "%s%04x:", prefix, i); | |
605 | ||
606 | for (j = 0; j < 16 && (i + j) < len; j ++) | |
607 | { | |
608 | if (j && !(j & 3)) | |
609 | fprintf(stderr, " %02x", buffer[i + j]); | |
610 | else | |
611 | fprintf(stderr, " %02x", buffer[i + j]); | |
612 | } | |
613 | ||
614 | while (j < 16) | |
615 | { | |
616 | if (j && !(j & 3)) | |
617 | fputs(" ", stderr); | |
618 | else | |
619 | fputs(" ", stderr); | |
620 | ||
621 | j ++; | |
622 | } | |
623 | ||
624 | fputs(" ", stderr); | |
625 | ||
626 | for (j = 0; j < 16 && (i + j) < len; j ++) | |
627 | if (buffer[i + j] < ' ' || buffer[i + j] >= 0x7f) | |
628 | putc('.', stderr); | |
629 | else | |
630 | putc(buffer[i + j], stderr); | |
631 | ||
632 | putc('\n', stderr); | |
633 | } | |
634 | } | |
635 | ||
636 | if (indent == 0) | |
637 | fprintf(stderr, "%sMessage:\n", prefix); | |
638 | ||
639 | bufend = buffer + len; | |
640 | ||
641 | while (buffer < bufend) | |
642 | { | |
643 | /* | |
644 | * Get value type... | |
645 | */ | |
646 | ||
647 | value_type = asn1_get_type(&buffer, bufend); | |
648 | value_length = asn1_get_length(&buffer, bufend); | |
649 | ||
650 | switch (value_type) | |
651 | { | |
652 | case CUPS_ASN1_BOOLEAN : | |
653 | integer = asn1_get_integer(&buffer, bufend, value_length); | |
654 | ||
655 | fprintf(stderr, "%s%*sBOOLEAN %d bytes %d\n", prefix, indent, "", | |
656 | value_length, integer); | |
657 | break; | |
658 | ||
659 | case CUPS_ASN1_INTEGER : | |
660 | integer = asn1_get_integer(&buffer, bufend, value_length); | |
661 | ||
662 | fprintf(stderr, "%s%*sINTEGER %d bytes %d\n", prefix, indent, "", | |
663 | value_length, integer); | |
664 | break; | |
665 | ||
666 | case CUPS_ASN1_COUNTER : | |
667 | integer = asn1_get_integer(&buffer, bufend, value_length); | |
668 | ||
669 | fprintf(stderr, "%s%*sCOUNTER %d bytes %u\n", prefix, indent, "", | |
670 | value_length, (unsigned)integer); | |
671 | break; | |
672 | ||
673 | case CUPS_ASN1_GAUGE : | |
674 | integer = asn1_get_integer(&buffer, bufend, value_length); | |
675 | ||
676 | fprintf(stderr, "%s%*sGAUGE %d bytes %u\n", prefix, indent, "", | |
677 | value_length, (unsigned)integer); | |
678 | break; | |
679 | ||
680 | case CUPS_ASN1_TIMETICKS : | |
681 | integer = asn1_get_integer(&buffer, bufend, value_length); | |
682 | ||
683 | fprintf(stderr, "%s%*sTIMETICKS %d bytes %u\n", prefix, indent, "", | |
684 | value_length, (unsigned)integer); | |
685 | break; | |
686 | ||
687 | case CUPS_ASN1_OCTET_STRING : | |
688 | fprintf(stderr, "%s%*sOCTET STRING %d bytes \"%s\"\n", prefix, | |
689 | indent, "", value_length, | |
690 | asn1_get_string(&buffer, bufend, value_length, string, | |
691 | sizeof(string))); | |
692 | break; | |
693 | ||
694 | case CUPS_ASN1_HEX_STRING : | |
695 | asn1_get_string(&buffer, bufend, value_length, string, | |
696 | sizeof(string)); | |
697 | fprintf(stderr, "%s%*sHex-STRING %d bytes", prefix, | |
698 | indent, "", value_length); | |
699 | for (i = 0; i < value_length; i ++) | |
700 | fprintf(stderr, " %02X", string[i] & 255); | |
701 | putc('\n', stderr); | |
702 | break; | |
703 | ||
704 | case CUPS_ASN1_NULL_VALUE : | |
705 | fprintf(stderr, "%s%*sNULL VALUE %d bytes\n", prefix, indent, "", | |
706 | value_length); | |
707 | ||
708 | buffer += value_length; | |
709 | break; | |
710 | ||
711 | case CUPS_ASN1_OID : | |
712 | integer = asn1_get_oid(&buffer, bufend, value_length, oid, | |
713 | CUPS_SNMP_MAX_OID); | |
714 | ||
715 | fprintf(stderr, "%s%*sOID %d bytes ", prefix, indent, "", | |
716 | value_length); | |
717 | for (i = 0; i < integer; i ++) | |
718 | fprintf(stderr, ".%d", oid[i]); | |
719 | putc('\n', stderr); | |
720 | break; | |
721 | ||
722 | case CUPS_ASN1_SEQUENCE : | |
723 | fprintf(stderr, "%s%*sSEQUENCE %d bytes\n", prefix, indent, "", | |
724 | value_length); | |
725 | asn1_debug(prefix, buffer, value_length, indent + 4); | |
726 | ||
727 | buffer += value_length; | |
728 | break; | |
729 | ||
730 | case CUPS_ASN1_GET_NEXT_REQUEST : | |
731 | fprintf(stderr, "%s%*sGet-Next-Request-PDU %d bytes\n", prefix, | |
732 | indent, "", value_length); | |
733 | asn1_debug(prefix, buffer, value_length, indent + 4); | |
734 | ||
735 | buffer += value_length; | |
736 | break; | |
737 | ||
738 | case CUPS_ASN1_GET_REQUEST : | |
739 | fprintf(stderr, "%s%*sGet-Request-PDU %d bytes\n", prefix, indent, "", | |
740 | value_length); | |
741 | asn1_debug(prefix, buffer, value_length, indent + 4); | |
742 | ||
743 | buffer += value_length; | |
744 | break; | |
745 | ||
746 | case CUPS_ASN1_GET_RESPONSE : | |
747 | fprintf(stderr, "%s%*sGet-Response-PDU %d bytes\n", prefix, indent, | |
748 | "", value_length); | |
749 | asn1_debug(prefix, buffer, value_length, indent + 4); | |
750 | ||
751 | buffer += value_length; | |
752 | break; | |
753 | ||
754 | default : | |
755 | fprintf(stderr, "%s%*sUNKNOWN(%x) %d bytes\n", prefix, indent, "", | |
756 | value_type, value_length); | |
757 | ||
758 | buffer += value_length; | |
759 | break; | |
760 | } | |
761 | } | |
762 | } | |
763 | ||
764 | ||
91c84a35 MS |
765 | /* |
766 | * 'asn1_decode_snmp()' - Decode a SNMP packet. | |
767 | */ | |
768 | ||
769 | static int /* O - 0 on success, -1 on error */ | |
770 | asn1_decode_snmp(unsigned char *buffer, /* I - Buffer */ | |
771 | size_t len, /* I - Size of buffer */ | |
772 | cups_snmp_t *packet) /* I - SNMP packet */ | |
773 | { | |
774 | unsigned char *bufptr, /* Pointer into the data */ | |
775 | *bufend; /* End of data */ | |
776 | int length; /* Length of value */ | |
777 | ||
778 | ||
779 | /* | |
780 | * Initialize the decoding... | |
781 | */ | |
782 | ||
783 | memset(packet, 0, sizeof(cups_snmp_t)); | |
ac884b6a | 784 | packet->object_name[0] = -1; |
91c84a35 MS |
785 | |
786 | bufptr = buffer; | |
787 | bufend = buffer + len; | |
788 | ||
789 | if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_SEQUENCE) | |
790 | snmp_set_error(packet, _("Packet does not start with SEQUENCE")); | |
791 | else if (asn1_get_length(&bufptr, bufend) == 0) | |
792 | snmp_set_error(packet, _("SEQUENCE uses indefinite length")); | |
793 | else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_INTEGER) | |
794 | snmp_set_error(packet, _("No version number")); | |
795 | else if ((length = asn1_get_length(&bufptr, bufend)) == 0) | |
796 | snmp_set_error(packet, _("Version uses indefinite length")); | |
797 | else if ((packet->version = asn1_get_integer(&bufptr, bufend, length)) | |
798 | != CUPS_SNMP_VERSION_1) | |
799 | snmp_set_error(packet, _("Bad SNMP version number")); | |
800 | else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_OCTET_STRING) | |
801 | snmp_set_error(packet, _("No community name")); | |
802 | else if ((length = asn1_get_length(&bufptr, bufend)) == 0) | |
803 | snmp_set_error(packet, _("Community name uses indefinite length")); | |
804 | else | |
805 | { | |
806 | asn1_get_string(&bufptr, bufend, length, packet->community, | |
807 | sizeof(packet->community)); | |
808 | ||
809 | if ((packet->request_type = asn1_get_type(&bufptr, bufend)) | |
810 | != CUPS_ASN1_GET_RESPONSE) | |
811 | snmp_set_error(packet, _("Packet does not contain a Get-Response-PDU")); | |
812 | else if (asn1_get_length(&bufptr, bufend) == 0) | |
813 | snmp_set_error(packet, _("Get-Response-PDU uses indefinite length")); | |
814 | else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_INTEGER) | |
815 | snmp_set_error(packet, _("No request-id")); | |
816 | else if ((length = asn1_get_length(&bufptr, bufend)) == 0) | |
817 | snmp_set_error(packet, _("request-id uses indefinite length")); | |
818 | else | |
819 | { | |
820 | packet->request_id = asn1_get_integer(&bufptr, bufend, length); | |
821 | ||
822 | if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_INTEGER) | |
823 | snmp_set_error(packet, _("No error-status")); | |
824 | else if ((length = asn1_get_length(&bufptr, bufend)) == 0) | |
825 | snmp_set_error(packet, _("error-status uses indefinite length")); | |
826 | else | |
827 | { | |
828 | packet->error_status = asn1_get_integer(&bufptr, bufend, length); | |
829 | ||
830 | if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_INTEGER) | |
831 | snmp_set_error(packet, _("No error-index")); | |
832 | else if ((length = asn1_get_length(&bufptr, bufend)) == 0) | |
833 | snmp_set_error(packet, _("error-index uses indefinite length")); | |
834 | else | |
835 | { | |
836 | packet->error_index = asn1_get_integer(&bufptr, bufend, length); | |
837 | ||
838 | if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_SEQUENCE) | |
839 | snmp_set_error(packet, _("No variable-bindings SEQUENCE")); | |
840 | else if (asn1_get_length(&bufptr, bufend) == 0) | |
841 | snmp_set_error(packet, | |
842 | _("variable-bindings uses indefinite length")); | |
843 | else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_SEQUENCE) | |
844 | snmp_set_error(packet, _("No VarBind SEQUENCE")); | |
845 | else if (asn1_get_length(&bufptr, bufend) == 0) | |
846 | snmp_set_error(packet, _("VarBind uses indefinite length")); | |
847 | else if (asn1_get_type(&bufptr, bufend) != CUPS_ASN1_OID) | |
848 | snmp_set_error(packet, _("No name OID")); | |
849 | else if ((length = asn1_get_length(&bufptr, bufend)) == 0) | |
850 | snmp_set_error(packet, _("Name OID uses indefinite length")); | |
851 | else | |
852 | { | |
853 | asn1_get_oid(&bufptr, bufend, length, packet->object_name, | |
854 | CUPS_SNMP_MAX_OID); | |
855 | ||
856 | packet->object_type = asn1_get_type(&bufptr, bufend); | |
857 | ||
858 | if ((length = asn1_get_length(&bufptr, bufend)) == 0 && | |
859 | packet->object_type != CUPS_ASN1_NULL_VALUE && | |
860 | packet->object_type != CUPS_ASN1_OCTET_STRING) | |
861 | snmp_set_error(packet, _("Value uses indefinite length")); | |
862 | else | |
863 | { | |
864 | switch (packet->object_type) | |
865 | { | |
866 | case CUPS_ASN1_BOOLEAN : | |
867 | packet->object_value.boolean = | |
868 | asn1_get_integer(&bufptr, bufend, length); | |
869 | break; | |
870 | ||
871 | case CUPS_ASN1_INTEGER : | |
872 | packet->object_value.integer = | |
873 | asn1_get_integer(&bufptr, bufend, length); | |
874 | break; | |
875 | ||
876 | case CUPS_ASN1_NULL_VALUE : | |
877 | break; | |
878 | ||
879 | case CUPS_ASN1_OCTET_STRING : | |
880 | asn1_get_string(&bufptr, bufend, length, | |
881 | packet->object_value.string, | |
882 | CUPS_SNMP_MAX_STRING); | |
883 | break; | |
884 | ||
885 | case CUPS_ASN1_OID : | |
886 | asn1_get_oid(&bufptr, bufend, length, | |
887 | packet->object_value.oid, CUPS_SNMP_MAX_OID); | |
888 | break; | |
889 | ||
ac884b6a MS |
890 | case CUPS_ASN1_HEX_STRING : |
891 | packet->object_value.hex_string.num_bytes = length; | |
892 | ||
893 | asn1_get_string(&bufptr, bufend, length, | |
894 | (char *)packet->object_value.hex_string.bytes, | |
895 | CUPS_SNMP_MAX_STRING); | |
896 | break; | |
897 | ||
91c84a35 MS |
898 | case CUPS_ASN1_COUNTER : |
899 | packet->object_value.counter = | |
900 | asn1_get_integer(&bufptr, bufend, length); | |
901 | break; | |
902 | ||
903 | case CUPS_ASN1_GAUGE : | |
904 | packet->object_value.gauge = | |
905 | asn1_get_integer(&bufptr, bufend, length); | |
906 | break; | |
907 | ||
ac884b6a MS |
908 | case CUPS_ASN1_TIMETICKS : |
909 | packet->object_value.timeticks = | |
910 | asn1_get_integer(&bufptr, bufend, length); | |
911 | break; | |
912 | ||
91c84a35 MS |
913 | default : |
914 | snmp_set_error(packet, _("Unsupported value type")); | |
915 | break; | |
916 | } | |
917 | } | |
918 | } | |
919 | } | |
920 | } | |
921 | } | |
922 | } | |
923 | ||
924 | return (packet->error ? -1 : 0); | |
925 | } | |
926 | ||
927 | ||
91c84a35 MS |
928 | /* |
929 | * 'asn1_encode_snmp()' - Encode a SNMP packet. | |
930 | */ | |
931 | ||
932 | static int /* O - Length on success, -1 on error */ | |
933 | asn1_encode_snmp(unsigned char *buffer, /* I - Buffer */ | |
934 | size_t bufsize, /* I - Size of buffer */ | |
935 | cups_snmp_t *packet) /* I - SNMP packet */ | |
936 | { | |
937 | unsigned char *bufptr; /* Pointer into buffer */ | |
938 | int total, /* Total length */ | |
939 | msglen, /* Length of entire message */ | |
940 | commlen, /* Length of community string */ | |
941 | reqlen, /* Length of request */ | |
942 | listlen, /* Length of variable list */ | |
943 | varlen, /* Length of variable */ | |
944 | namelen, /* Length of object name OID */ | |
945 | valuelen; /* Length of object value */ | |
946 | ||
947 | ||
948 | /* | |
949 | * Get the lengths of the community string, OID, and message... | |
950 | */ | |
951 | ||
ac884b6a | 952 | |
91c84a35 MS |
953 | namelen = asn1_size_oid(packet->object_name); |
954 | ||
955 | switch (packet->object_type) | |
956 | { | |
957 | case CUPS_ASN1_NULL_VALUE : | |
958 | valuelen = 0; | |
959 | break; | |
960 | ||
961 | case CUPS_ASN1_BOOLEAN : | |
962 | valuelen = asn1_size_integer(packet->object_value.boolean); | |
963 | break; | |
964 | ||
965 | case CUPS_ASN1_INTEGER : | |
966 | valuelen = asn1_size_integer(packet->object_value.integer); | |
967 | break; | |
968 | ||
969 | case CUPS_ASN1_OCTET_STRING : | |
970 | valuelen = strlen(packet->object_value.string); | |
971 | break; | |
972 | ||
973 | case CUPS_ASN1_OID : | |
974 | valuelen = asn1_size_oid(packet->object_value.oid); | |
975 | break; | |
976 | ||
977 | default : | |
978 | packet->error = "Unknown object type"; | |
979 | return (-1); | |
980 | } | |
981 | ||
982 | varlen = 1 + asn1_size_length(namelen) + namelen + | |
983 | 1 + asn1_size_length(valuelen) + valuelen; | |
984 | listlen = 1 + asn1_size_length(varlen) + varlen; | |
985 | reqlen = 2 + asn1_size_integer(packet->request_id) + | |
986 | 2 + asn1_size_integer(packet->error_status) + | |
987 | 2 + asn1_size_integer(packet->error_index) + | |
988 | 1 + asn1_size_length(listlen) + listlen; | |
989 | commlen = strlen(packet->community); | |
990 | msglen = 2 + asn1_size_integer(packet->version) + | |
991 | 1 + asn1_size_length(commlen) + commlen + | |
992 | 1 + asn1_size_length(reqlen) + reqlen; | |
993 | total = 1 + asn1_size_length(msglen) + msglen; | |
994 | ||
995 | if (total > bufsize) | |
996 | { | |
997 | packet->error = "Message too large for buffer"; | |
998 | return (-1); | |
999 | } | |
1000 | ||
1001 | /* | |
1002 | * Then format the message... | |
1003 | */ | |
1004 | ||
1005 | bufptr = buffer; | |
1006 | ||
1007 | *bufptr++ = CUPS_ASN1_SEQUENCE; /* SNMPv1 message header */ | |
1008 | asn1_set_length(&bufptr, msglen); | |
1009 | ||
1010 | asn1_set_integer(&bufptr, packet->version); | |
1011 | /* version */ | |
1012 | ||
1013 | *bufptr++ = CUPS_ASN1_OCTET_STRING; /* community */ | |
1014 | asn1_set_length(&bufptr, commlen); | |
1015 | memcpy(bufptr, packet->community, commlen); | |
1016 | bufptr += commlen; | |
1017 | ||
ac884b6a | 1018 | *bufptr++ = packet->request_type; /* Get-Request-PDU/Get-Next-Request-PDU */ |
91c84a35 MS |
1019 | asn1_set_length(&bufptr, reqlen); |
1020 | ||
1021 | asn1_set_integer(&bufptr, packet->request_id); | |
1022 | ||
1023 | asn1_set_integer(&bufptr, packet->error_status); | |
1024 | ||
1025 | asn1_set_integer(&bufptr, packet->error_index); | |
1026 | ||
1027 | *bufptr++ = CUPS_ASN1_SEQUENCE; /* variable-bindings */ | |
1028 | asn1_set_length(&bufptr, listlen); | |
1029 | ||
1030 | *bufptr++ = CUPS_ASN1_SEQUENCE; /* variable */ | |
1031 | asn1_set_length(&bufptr, varlen); | |
1032 | ||
1033 | asn1_set_oid(&bufptr, packet->object_name); | |
1034 | /* ObjectName */ | |
1035 | ||
1036 | switch (packet->object_type) | |
1037 | { | |
1038 | case CUPS_ASN1_NULL_VALUE : | |
1039 | *bufptr++ = CUPS_ASN1_NULL_VALUE; | |
1040 | /* ObjectValue */ | |
1041 | *bufptr++ = 0; /* Length */ | |
1042 | break; | |
1043 | ||
1044 | case CUPS_ASN1_BOOLEAN : | |
1045 | asn1_set_integer(&bufptr, packet->object_value.boolean); | |
1046 | break; | |
1047 | ||
1048 | case CUPS_ASN1_INTEGER : | |
1049 | asn1_set_integer(&bufptr, packet->object_value.integer); | |
1050 | break; | |
1051 | ||
1052 | case CUPS_ASN1_OCTET_STRING : | |
1053 | *bufptr++ = CUPS_ASN1_OCTET_STRING; | |
1054 | asn1_set_length(&bufptr, valuelen); | |
1055 | memcpy(bufptr, packet->object_value.string, valuelen); | |
1056 | bufptr += valuelen; | |
1057 | break; | |
1058 | ||
1059 | case CUPS_ASN1_OID : | |
1060 | asn1_set_oid(&bufptr, packet->object_value.oid); | |
1061 | break; | |
1062 | ||
1063 | default : | |
1064 | break; | |
1065 | } | |
1066 | ||
1067 | return (bufptr - buffer); | |
1068 | } | |
1069 | ||
1070 | ||
1071 | /* | |
1072 | * 'asn1_get_integer()' - Get an integer value. | |
1073 | */ | |
1074 | ||
1075 | static int /* O - Integer value */ | |
1076 | asn1_get_integer( | |
1077 | unsigned char **buffer, /* IO - Pointer in buffer */ | |
1078 | unsigned char *bufend, /* I - End of buffer */ | |
1079 | int length) /* I - Length of value */ | |
1080 | { | |
1081 | int value; /* Integer value */ | |
1082 | ||
1083 | ||
1084 | for (value = 0; | |
1085 | length > 0 && *buffer < bufend; | |
1086 | length --, (*buffer) ++) | |
1087 | value = (value << 8) | **buffer; | |
1088 | ||
1089 | return (value); | |
1090 | } | |
1091 | ||
1092 | ||
1093 | /* | |
1094 | * 'asn1_get_length()' - Get a value length. | |
1095 | */ | |
1096 | ||
1097 | static int /* O - Length */ | |
1098 | asn1_get_length(unsigned char **buffer, /* IO - Pointer in buffer */ | |
1099 | unsigned char *bufend) /* I - End of buffer */ | |
1100 | { | |
1101 | int length; /* Length */ | |
1102 | ||
1103 | ||
1104 | length = **buffer; | |
1105 | (*buffer) ++; | |
1106 | ||
1107 | if (length & 128) | |
1108 | length = asn1_get_integer(buffer, bufend, length & 127); | |
1109 | ||
1110 | return (length); | |
1111 | } | |
1112 | ||
1113 | ||
1114 | /* | |
1115 | * 'asn1_get_oid()' - Get an OID value. | |
1116 | */ | |
1117 | ||
ac884b6a | 1118 | static int /* O - Number of OIDs */ |
91c84a35 MS |
1119 | asn1_get_oid( |
1120 | unsigned char **buffer, /* IO - Pointer in buffer */ | |
1121 | unsigned char *bufend, /* I - End of buffer */ | |
1122 | int length, /* I - Length of value */ | |
1123 | int *oid, /* I - OID buffer */ | |
1124 | int oidsize) /* I - Size of OID buffer */ | |
1125 | { | |
1126 | unsigned char *valend; /* End of value */ | |
ac884b6a MS |
1127 | int *oidptr, /* Current OID */ |
1128 | *oidend; /* End of OID buffer */ | |
91c84a35 MS |
1129 | int number; /* OID number */ |
1130 | ||
1131 | ||
1132 | valend = *buffer + length; | |
ac884b6a | 1133 | oidptr = oid; |
91c84a35 MS |
1134 | oidend = oid + oidsize - 1; |
1135 | ||
1136 | if (valend > bufend) | |
1137 | valend = bufend; | |
1138 | ||
1139 | number = asn1_get_packed(buffer, bufend); | |
1140 | ||
1141 | if (number < 80) | |
1142 | { | |
ac884b6a MS |
1143 | *oidptr++ = number / 40; |
1144 | number = number % 40; | |
1145 | *oidptr++ = number; | |
91c84a35 MS |
1146 | } |
1147 | else | |
1148 | { | |
ac884b6a MS |
1149 | *oidptr++ = 2; |
1150 | number -= 80; | |
1151 | *oidptr++ = number; | |
91c84a35 MS |
1152 | } |
1153 | ||
1154 | while (*buffer < valend) | |
1155 | { | |
1156 | number = asn1_get_packed(buffer, bufend); | |
1157 | ||
ac884b6a MS |
1158 | if (oidptr < oidend) |
1159 | *oidptr++ = number; | |
91c84a35 MS |
1160 | } |
1161 | ||
ac884b6a | 1162 | *oidptr = -1; |
91c84a35 | 1163 | |
ac884b6a | 1164 | return (oidptr - oid); |
91c84a35 MS |
1165 | } |
1166 | ||
1167 | ||
1168 | /* | |
1169 | * 'asn1_get_packed()' - Get a packed integer value. | |
1170 | */ | |
1171 | ||
1172 | static int /* O - Value */ | |
1173 | asn1_get_packed( | |
1174 | unsigned char **buffer, /* IO - Pointer in buffer */ | |
1175 | unsigned char *bufend) /* I - End of buffer */ | |
1176 | { | |
1177 | int value; /* Value */ | |
1178 | ||
1179 | ||
1180 | value = 0; | |
1181 | ||
1182 | while ((**buffer & 128) && *buffer < bufend) | |
1183 | { | |
1184 | value = (value << 7) | (**buffer & 127); | |
1185 | (*buffer) ++; | |
1186 | } | |
1187 | ||
1188 | if (*buffer < bufend) | |
1189 | { | |
1190 | value = (value << 7) | **buffer; | |
1191 | (*buffer) ++; | |
1192 | } | |
1193 | ||
1194 | return (value); | |
1195 | } | |
1196 | ||
1197 | ||
1198 | /* | |
1199 | * 'asn1_get_string()' - Get a string value. | |
1200 | */ | |
1201 | ||
1202 | static char * /* O - String */ | |
1203 | asn1_get_string( | |
1204 | unsigned char **buffer, /* IO - Pointer in buffer */ | |
1205 | unsigned char *bufend, /* I - End of buffer */ | |
1206 | int length, /* I - Value length */ | |
1207 | char *string, /* I - String buffer */ | |
1208 | int strsize) /* I - String buffer size */ | |
1209 | { | |
1210 | if (length < 0) | |
1211 | { | |
1212 | /* | |
1213 | * Disallow negative lengths! | |
1214 | */ | |
1215 | ||
1216 | *string = '\0'; | |
1217 | } | |
1218 | else if (length < strsize) | |
1219 | { | |
1220 | /* | |
1221 | * String is smaller than the buffer... | |
1222 | */ | |
1223 | ||
1224 | if (length > 0) | |
1225 | memcpy(string, *buffer, length); | |
1226 | ||
1227 | string[length] = '\0'; | |
1228 | } | |
1229 | else | |
1230 | { | |
1231 | /* | |
1232 | * String is larger than the buffer... | |
1233 | */ | |
1234 | ||
1235 | memcpy(string, *buffer, strsize - 1); | |
1236 | string[strsize - 1] = '\0'; | |
1237 | } | |
1238 | ||
1239 | if (length > 0) | |
1240 | (*buffer) += length; | |
1241 | ||
1242 | return (length < 0 ? NULL : string); | |
1243 | } | |
1244 | ||
1245 | ||
1246 | /* | |
1247 | * 'asn1_get_type()' - Get a value type. | |
1248 | */ | |
1249 | ||
1250 | static int /* O - Type */ | |
1251 | asn1_get_type(unsigned char **buffer, /* IO - Pointer in buffer */ | |
1252 | unsigned char *bufend) /* I - End of buffer */ | |
1253 | { | |
1254 | int type; /* Type */ | |
1255 | ||
1256 | ||
1257 | type = **buffer; | |
1258 | (*buffer) ++; | |
1259 | ||
1260 | if ((type & 31) == 31) | |
1261 | type = asn1_get_packed(buffer, bufend); | |
1262 | ||
1263 | return (type); | |
1264 | } | |
1265 | ||
1266 | ||
1267 | /* | |
1268 | * 'asn1_set_integer()' - Set an integer value. | |
1269 | */ | |
1270 | ||
1271 | static void | |
1272 | asn1_set_integer(unsigned char **buffer,/* IO - Pointer in buffer */ | |
1273 | int integer) /* I - Integer value */ | |
1274 | { | |
1275 | **buffer = CUPS_ASN1_INTEGER; | |
1276 | (*buffer) ++; | |
1277 | ||
1278 | if (integer > 0x7fffff || integer < -0x800000) | |
1279 | { | |
1280 | **buffer = 4; | |
1281 | (*buffer) ++; | |
1282 | **buffer = integer >> 24; | |
1283 | (*buffer) ++; | |
1284 | **buffer = integer >> 16; | |
1285 | (*buffer) ++; | |
1286 | **buffer = integer >> 8; | |
1287 | (*buffer) ++; | |
1288 | **buffer = integer; | |
1289 | (*buffer) ++; | |
1290 | } | |
1291 | else if (integer > 0x7fff || integer < -0x8000) | |
1292 | { | |
1293 | **buffer = 3; | |
1294 | (*buffer) ++; | |
1295 | **buffer = integer >> 16; | |
1296 | (*buffer) ++; | |
1297 | **buffer = integer >> 8; | |
1298 | (*buffer) ++; | |
1299 | **buffer = integer; | |
1300 | (*buffer) ++; | |
1301 | } | |
1302 | else if (integer > 0x7f || integer < -0x80) | |
1303 | { | |
1304 | **buffer = 2; | |
1305 | (*buffer) ++; | |
1306 | **buffer = integer >> 8; | |
1307 | (*buffer) ++; | |
1308 | **buffer = integer; | |
1309 | (*buffer) ++; | |
1310 | } | |
1311 | else | |
1312 | { | |
1313 | **buffer = 1; | |
1314 | (*buffer) ++; | |
1315 | **buffer = integer; | |
1316 | (*buffer) ++; | |
1317 | } | |
1318 | } | |
1319 | ||
1320 | ||
1321 | /* | |
1322 | * 'asn1_set_length()' - Set a value length. | |
1323 | */ | |
1324 | ||
1325 | static void | |
1326 | asn1_set_length(unsigned char **buffer, /* IO - Pointer in buffer */ | |
1327 | int length) /* I - Length value */ | |
1328 | { | |
1329 | if (length > 255) | |
1330 | { | |
1331 | **buffer = 0x82; /* 2-byte length */ | |
1332 | (*buffer) ++; | |
1333 | **buffer = length >> 8; | |
1334 | (*buffer) ++; | |
1335 | **buffer = length; | |
1336 | (*buffer) ++; | |
1337 | } | |
1338 | else if (length > 127) | |
1339 | { | |
1340 | **buffer = 0x81; /* 1-byte length */ | |
1341 | (*buffer) ++; | |
1342 | **buffer = length; | |
1343 | (*buffer) ++; | |
1344 | } | |
1345 | else | |
1346 | { | |
1347 | **buffer = length; /* Length */ | |
1348 | (*buffer) ++; | |
1349 | } | |
1350 | } | |
1351 | ||
1352 | ||
1353 | /* | |
1354 | * 'asn1_set_oid()' - Set an OID value. | |
1355 | */ | |
1356 | ||
1357 | static void | |
1358 | asn1_set_oid(unsigned char **buffer, /* IO - Pointer in buffer */ | |
1359 | const int *oid) /* I - OID value */ | |
1360 | { | |
1361 | **buffer = CUPS_ASN1_OID; | |
1362 | (*buffer) ++; | |
1363 | ||
1364 | asn1_set_length(buffer, asn1_size_oid(oid)); | |
1365 | ||
ac884b6a MS |
1366 | if (oid[1] < 0) |
1367 | { | |
1368 | asn1_set_packed(buffer, oid[0] * 40); | |
1369 | return; | |
1370 | } | |
1371 | ||
91c84a35 MS |
1372 | asn1_set_packed(buffer, oid[0] * 40 + oid[1]); |
1373 | ||
ac884b6a | 1374 | for (oid += 2; *oid >= 0; oid ++) |
91c84a35 MS |
1375 | asn1_set_packed(buffer, *oid); |
1376 | } | |
1377 | ||
1378 | ||
1379 | /* | |
1380 | * 'asn1_set_packed()' - Set a packed integer value. | |
1381 | */ | |
1382 | ||
1383 | static void | |
1384 | asn1_set_packed(unsigned char **buffer, /* IO - Pointer in buffer */ | |
1385 | int integer) /* I - Integer value */ | |
1386 | { | |
1387 | if (integer > 0xfffffff) | |
1388 | { | |
ac884b6a | 1389 | **buffer = ((integer >> 28) & 0x7f) | 0x80; |
91c84a35 MS |
1390 | (*buffer) ++; |
1391 | } | |
1392 | ||
1393 | if (integer > 0x1fffff) | |
1394 | { | |
ac884b6a | 1395 | **buffer = ((integer >> 21) & 0x7f) | 0x80; |
91c84a35 MS |
1396 | (*buffer) ++; |
1397 | } | |
1398 | ||
1399 | if (integer > 0x3fff) | |
1400 | { | |
ac884b6a | 1401 | **buffer = ((integer >> 14) & 0x7f) | 0x80; |
91c84a35 MS |
1402 | (*buffer) ++; |
1403 | } | |
1404 | ||
1405 | if (integer > 0x7f) | |
1406 | { | |
ac884b6a | 1407 | **buffer = ((integer >> 7) & 0x7f) | 0x80; |
91c84a35 MS |
1408 | (*buffer) ++; |
1409 | } | |
1410 | ||
1411 | **buffer = integer & 0x7f; | |
1412 | (*buffer) ++; | |
1413 | } | |
1414 | ||
1415 | ||
1416 | /* | |
1417 | * 'asn1_size_integer()' - Figure out the number of bytes needed for an | |
1418 | * integer value. | |
1419 | */ | |
1420 | ||
1421 | static int /* O - Size in bytes */ | |
1422 | asn1_size_integer(int integer) /* I - Integer value */ | |
1423 | { | |
1424 | if (integer > 0x7fffff || integer < -0x800000) | |
1425 | return (4); | |
1426 | else if (integer > 0x7fff || integer < -0x8000) | |
1427 | return (3); | |
1428 | else if (integer > 0x7f || integer < -0x80) | |
1429 | return (2); | |
1430 | else | |
1431 | return (1); | |
1432 | } | |
1433 | ||
1434 | ||
1435 | /* | |
1436 | * 'asn1_size_length()' - Figure out the number of bytes needed for a | |
1437 | * length value. | |
1438 | */ | |
1439 | ||
1440 | static int /* O - Size in bytes */ | |
1441 | asn1_size_length(int length) /* I - Length value */ | |
1442 | { | |
1443 | if (length > 0xff) | |
1444 | return (3); | |
1445 | else if (length > 0x7f) | |
1446 | return (2); | |
1447 | else | |
1448 | return (1); | |
1449 | } | |
1450 | ||
1451 | ||
1452 | /* | |
1453 | * 'asn1_size_oid()' - Figure out the numebr of bytes needed for an | |
1454 | * OID value. | |
1455 | */ | |
1456 | ||
1457 | static int /* O - Size in bytes */ | |
1458 | asn1_size_oid(const int *oid) /* I - OID value */ | |
1459 | { | |
1460 | int length; /* Length of value */ | |
1461 | ||
1462 | ||
ac884b6a MS |
1463 | if (oid[1] < 0) |
1464 | return (asn1_size_packed(oid[0] * 40)); | |
1465 | ||
1466 | for (length = asn1_size_packed(oid[0] * 40 + oid[1]), oid += 2; | |
1467 | *oid >= 0; | |
1468 | oid ++) | |
91c84a35 MS |
1469 | length += asn1_size_packed(*oid); |
1470 | ||
1471 | return (length); | |
1472 | } | |
1473 | ||
1474 | ||
1475 | /* | |
1476 | * 'asn1_size_packed()' - Figure out the number of bytes needed for a | |
1477 | * packed integer value. | |
1478 | */ | |
1479 | ||
1480 | static int /* O - Size in bytes */ | |
1481 | asn1_size_packed(int integer) /* I - Integer value */ | |
1482 | { | |
1483 | if (integer > 0xfffffff) | |
1484 | return (5); | |
1485 | else if (integer > 0x1fffff) | |
1486 | return (4); | |
1487 | else if (integer > 0x3fff) | |
1488 | return (3); | |
1489 | else if (integer > 0x7f) | |
1490 | return (2); | |
1491 | else | |
1492 | return (1); | |
1493 | } | |
1494 | ||
1495 | ||
1496 | /* | |
1497 | * 'snmp_set_error()' - Set the localized error for a packet. | |
1498 | */ | |
1499 | ||
1500 | static void | |
1501 | snmp_set_error(cups_snmp_t *packet, /* I - Packet */ | |
1502 | const char *message) /* I - Error message */ | |
1503 | { | |
1504 | _cups_globals_t *cg = _cupsGlobals(); /* Global data */ | |
1505 | ||
1506 | ||
1507 | if (!cg->lang_default) | |
1508 | cg->lang_default = cupsLangDefault(); | |
1509 | ||
1510 | packet->error = _cupsLangString(cg->lang_default, message); | |
1511 | } | |
1512 | ||
1513 | ||
1514 | /* | |
1515 | * End of "$Id$". | |
1516 | */ |