]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/snmp.c
<rdar://problem/13655599> Seed: Print queue JOBS disappear after computer Wakes up...
[thirdparty/cups.git] / cups / snmp.c
index 2fbaf57819657d3d4f0ea7492fb2c2fd0fc725d2..034db89a9e6a022bf27b7c0f6f35213a6f8ee7c5 100644 (file)
@@ -1,9 +1,9 @@
 /*
  * "$Id$"
  *
- *   SNMP functions for the Common UNIX Printing System (CUPS).
+ *   SNMP functions for CUPS.
  *
- *   Copyright 2007-2008 by Apple Inc.
+ *   Copyright 2007-2011 by Apple Inc.
  *   Copyright 2006-2007 by Easy Software Products, all rights reserved.
  *
  *   These coded instructions, statements, and computer programs are the
  *                                 specified OID.
  *   _cupsSNMPIsOIDPrefixed()    - Test whether a SNMP response uses the
  *                                 specified OID prefix.
+ *   _cupsSNMPOIDToString()      - Convert an OID to a string.
  *   _cupsSNMPOpen()             - Open a SNMP socket.
  *   _cupsSNMPRead()             - Read and parse a SNMP response.
  *   _cupsSNMPSetDebug()         - Enable/disable debug logging to stderr.
+ *   _cupsSNMPStringToOID()      - Convert a numeric OID string to an OID array.
  *   _cupsSNMPWalk()             - Enumerate a group of OIDs.
  *   _cupsSNMPWrite()            - Send an SNMP query packet.
  *   asn1_debug()                - Decode an ASN1-encoded message.
  * Include necessary headers.
  */
 
-#include "globals.h"
+#include "cups-private.h"
 #include "snmp-private.h"
-#include <errno.h>
 #ifdef HAVE_POLL
-#  include <sys/poll.h>
+#  include <poll.h>
 #endif /* HAVE_POLL */
 
 
@@ -86,14 +87,14 @@ static char         *asn1_get_string(unsigned char **buffer,
                                         unsigned char *bufend,
                                         int length, char *string,
                                         int strsize);
-static int             asn1_get_length(unsigned char **buffer,
+static unsigned                asn1_get_length(unsigned char **buffer,
                                        unsigned char *bufend);
 static int             asn1_get_type(unsigned char **buffer,
                                      unsigned char *bufend);
 static void            asn1_set_integer(unsigned char **buffer,
                                         int integer);
 static void            asn1_set_length(unsigned char **buffer,
-                                       int length);
+                                       unsigned length);
 static void            asn1_set_oid(unsigned char **buffer,
                                     const int *oid);
 static void            asn1_set_packed(unsigned char **buffer,
@@ -108,13 +109,13 @@ static void               snmp_set_error(cups_snmp_t *packet,
 
 /*
  * '_cupsSNMPClose()' - Close a SNMP socket.
- *
- * @since CUPS 1.4@
  */
 
 void
 _cupsSNMPClose(int fd)                 /* I - SNMP socket file descriptor */
 {
+  DEBUG_printf(("4_cupsSNMPClose(fd=%d)", fd));
+
 #ifdef WIN32
   closesocket(fd);
 #else
@@ -127,8 +128,6 @@ _cupsSNMPClose(int fd)                      /* I - SNMP socket file descriptor */
  * '_cupsSNMPCopyOID()' - Copy an OID.
  *
  * The array pointed to by "src" is terminated by the value -1.
- *
- * @since CUPS 1.4@
  */
 
 int *                                  /* O - New OID */
@@ -139,6 +138,9 @@ _cupsSNMPCopyOID(int       *dst,    /* I - Destination OID */
   int  i;                              /* Looping var */
 
 
+  DEBUG_printf(("4_cupsSNMPCopyOID(dst=%p, src=%p, dstsize=%d)", dst, src,
+                dstsize));
+
   for (i = 0, dstsize --; src[i] >= 0 && i < dstsize; i ++)
     dst[i] = src[i];
 
@@ -153,8 +155,6 @@ _cupsSNMPCopyOID(int       *dst,    /* I - Destination OID */
  *
  * The default community name is the first community name found in the
  * snmp.conf file. If no community name is defined there, "public" is used.
- *
- * @since CUPS 1.4@
  */
 
 const char *                           /* O - Default community name */
@@ -167,6 +167,8 @@ _cupsSNMPDefaultCommunity(void)
   _cups_globals_t *cg = _cupsGlobals();        /* Global data */
 
 
+  DEBUG_puts("4_cupsSNMPDefaultCommunity()");
+
   if (!cg->snmp_community[0])
   {
     strlcpy(cg->snmp_community, "public", sizeof(cg->snmp_community));
@@ -176,7 +178,7 @@ _cupsSNMPDefaultCommunity(void)
     {
       linenum = 0;
       while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
-       if (!strcasecmp(line, "Community") && value)
+       if (!_cups_strcasecmp(line, "Community") && value)
        {
          strlcpy(cg->snmp_community, value, sizeof(cg->snmp_community));
          break;
@@ -186,6 +188,9 @@ _cupsSNMPDefaultCommunity(void)
     }
   }
 
+  DEBUG_printf(("5_cupsSNMPDefaultCommunity: Returning \"%s\"",
+                cg->snmp_community));
+
   return (cg->snmp_community);
 }
 
@@ -194,8 +199,6 @@ _cupsSNMPDefaultCommunity(void)
  * '_cupsSNMPIsOID()' - Test whether a SNMP response contains the specified OID.
  *
  * The array pointed to by "oid" is terminated by the value -1.
- *
- * @since CUPS 1.4@
  */
 
 int                                    /* O - 1 if equal, 0 if not equal */
@@ -209,8 +212,14 @@ _cupsSNMPIsOID(cups_snmp_t *packet,        /* I - Response packet */
   * Range check input...
   */
 
+  DEBUG_printf(("4_cupsSNMPIsOID(packet=%p, oid=%p)", packet, oid));
+
   if (!packet || !oid)
+  {
+    DEBUG_puts("5_cupsSNMPIsOID: Returning 0");
+
     return (0);
+  }
 
  /*
   * Compare OIDs...
@@ -220,7 +229,14 @@ _cupsSNMPIsOID(cups_snmp_t *packet,        /* I - Response packet */
        i < CUPS_SNMP_MAX_OID && oid[i] >= 0 && packet->object_name[i] >= 0;
        i ++)
     if (oid[i] != packet->object_name[i])
+    {
+      DEBUG_puts("5_cupsSNMPIsOID: Returning 0");
+
       return (0);
+    }
+
+  DEBUG_printf(("5_cupsSNMPIsOID: Returning %d",
+                i < CUPS_SNMP_MAX_OID && oid[i] == packet->object_name[i]));
 
   return (i < CUPS_SNMP_MAX_OID && oid[i] == packet->object_name[i]);
 }
@@ -231,8 +247,6 @@ _cupsSNMPIsOID(cups_snmp_t *packet, /* I - Response packet */
  *                              OID prefix.
  *
  * The array pointed to by "prefix" is terminated by the value -1.
- *
- * @since CUPS 1.4@
  */
 
 int                                    /* O - 1 if prefixed, 0 if not prefixed */
@@ -247,8 +261,15 @@ _cupsSNMPIsOIDPrefixed(
   * Range check input...
   */
 
+  DEBUG_printf(("4_cupsSNMPIsOIDPrefixed(packet=%p, prefix=%p)", packet,
+                prefix));
+
   if (!packet || !prefix)
+  {
+    DEBUG_puts("5_cupsSNMPIsOIDPrefixed: Returning 0");
+
     return (0);
+  }
 
  /*
   * Compare OIDs...
@@ -258,16 +279,61 @@ _cupsSNMPIsOIDPrefixed(
        i < CUPS_SNMP_MAX_OID && prefix[i] >= 0 && packet->object_name[i] >= 0;
        i ++)
     if (prefix[i] != packet->object_name[i])
+    {
+      DEBUG_puts("5_cupsSNMPIsOIDPrefixed: Returning 0");
+
       return (0);
+    }
+
+  DEBUG_printf(("5_cupsSNMPIsOIDPrefixed: Returning %d",
+                i < CUPS_SNMP_MAX_OID));
 
   return (i < CUPS_SNMP_MAX_OID);
 }
 
 
+/*
+ * '_cupsSNMPOIDToString()' - Convert an OID to a string.
+ */
+
+
+char *                                 /* O - New string or @code NULL@ on error */
+_cupsSNMPOIDToString(const int *src,   /* I - OID */
+                     char      *dst,   /* I - String buffer */
+                     size_t    dstsize)        /* I - Size of string buffer */
+{
+  char *dstptr,                        /* Pointer into string buffer */
+       *dstend;                        /* End of string buffer */
+
+
+  DEBUG_printf(("4_cupsSNMPOIDToString(src=%p, dst=%p, dstsize=" CUPS_LLFMT ")",
+                src, dst, CUPS_LLCAST dstsize));
+
+ /*
+  * Range check input...
+  */
+
+  if (!src || !dst || dstsize < 4)
+    return (NULL);
+
+ /*
+  * Loop through the OID array and build a string...
+  */
+
+  for (dstptr = dst, dstend = dstptr + dstsize - 1;
+       *src >= 0 && dstptr < dstend;
+       src ++, dstptr += strlen(dstptr))
+    snprintf(dstptr, dstend - dstptr + 1, ".%d", *src);
+
+  if (*src >= 0)
+    return (NULL);
+  else
+    return (dst);
+}
+
+
 /*
  * '_cupsSNMPOpen()' - Open a SNMP socket.
- *
- * @since CUPS 1.4@
  */
 
 int                                    /* O - SNMP socket file descriptor */
@@ -281,8 +347,14 @@ _cupsSNMPOpen(int family)          /* I - Address family - @code AF_INET@ or @code AF_IN
   * Create the SNMP socket...
   */
 
+  DEBUG_printf(("4_cupsSNMPOpen(family=%d)", family));
+
   if ((fd = socket(family, SOCK_DGRAM, 0)) < 0)
+  {
+    DEBUG_printf(("5_cupsSNMPOpen: Returning -1 (%s)", strerror(errno)));
+
     return (-1);
+  }
 
  /*
   * Set the "broadcast" flag...
@@ -290,13 +362,17 @@ _cupsSNMPOpen(int family)         /* I - Address family - @code AF_INET@ or @code AF_IN
 
   val = 1;
 
-  if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)))
+  if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, CUPS_SOCAST &val, sizeof(val)))
   {
+    DEBUG_printf(("5_cupsSNMPOpen: Returning -1 (%s)", strerror(errno)));
+
     close(fd);
 
     return (-1);
   }
 
+  DEBUG_printf(("5_cupsSNMPOpen: Returning %d", fd));
+
   return (fd);
 }
 
@@ -306,14 +382,12 @@ _cupsSNMPOpen(int family)         /* I - Address family - @code AF_INET@ or @code AF_IN
  *
  * If "timeout" is negative, @code _cupsSNMPRead@ will wait for a response
  * indefinitely.
- *
- * @since CUPS 1.4@
  */
 
 cups_snmp_t *                          /* O - SNMP packet or @code NULL@ if none */
 _cupsSNMPRead(int         fd,          /* I - SNMP socket file descriptor */
-             cups_snmp_t *packet,      /* I - SNMP packet buffer */
-            double      timeout)       /* I - Timeout in seconds */
+              cups_snmp_t *packet,     /* I - SNMP packet buffer */
+             double      timeout)      /* I - Timeout in seconds */
 {
   unsigned char        buffer[CUPS_SNMP_MAX_PACKET];
                                        /* Data packet */
@@ -326,8 +400,15 @@ _cupsSNMPRead(int         fd,              /* I - SNMP socket file descriptor */
   * Range check input...
   */
 
+  DEBUG_printf(("4_cupsSNMPRead(fd=%d, packet=%p, timeout=%.1f)", fd, packet,
+                timeout));
+
   if (fd < 0 || !packet)
+  {
+    DEBUG_puts("5_cupsSNMPRead: Returning NULL");
+
     return (NULL);
+  }
 
  /*
   * Optionally wait for a response...
@@ -343,7 +424,7 @@ _cupsSNMPRead(int         fd,               /* I - SNMP socket file descriptor */
     pfd.events = POLLIN;
 
     while ((ready = poll(&pfd, 1, (int)(timeout * 1000.0))) < 0 &&
-           errno == EINTR);
+           (errno == EINTR || errno == EAGAIN));
 
 #else
     fd_set             input_set;      /* select() input set */
@@ -355,14 +436,14 @@ _cupsSNMPRead(int         fd,             /* I - SNMP socket file descriptor */
       FD_SET(fd, &input_set);
 
       stimeout.tv_sec  = (int)timeout;
-      stimeout.tv_usec = (int)((timeout - stimeout.tv_usec) * 1000000);
+      stimeout.tv_usec = (int)((timeout - stimeout.tv_sec) * 1000000);
 
       ready = select(fd + 1, &input_set, NULL, NULL, &stimeout);
     }
 #  ifdef WIN32
     while (ready < 0 && WSAGetLastError() == WSAEINTR);
 #  else
-    while (ready < 0 && errno == EINTR);
+    while (ready < 0 && (errno == EINTR || errno == EAGAIN));
 #  endif /* WIN32 */
 #endif /* HAVE_POLL */
 
@@ -371,7 +452,11 @@ _cupsSNMPRead(int         fd,              /* I - SNMP socket file descriptor */
     */
 
     if (ready <= 0)
+    {
+      DEBUG_puts("5_cupsSNMPRead: Returning NULL (timeout)");
+
       return (NULL);
+    }
   }
 
  /*
@@ -382,7 +467,11 @@ _cupsSNMPRead(int         fd,              /* I - SNMP socket file descriptor */
 
   if ((bytes = recvfrom(fd, buffer, sizeof(buffer), 0, (void *)&address,
                         &addrlen)) < 0)
+  {
+    DEBUG_printf(("5_cupsSNMPRead: Returning NULL (%s)", strerror(errno)));
+
     return (NULL);
+  }
 
  /*
   * Look for the response status code in the SNMP message header...
@@ -398,14 +487,14 @@ _cupsSNMPRead(int         fd,             /* I - SNMP socket file descriptor */
   * Return decoded data packet...
   */
 
+  DEBUG_puts("5_cupsSNMPRead: Returning packet");
+
   return (packet);
 }
 
 
 /*
  * '_cupsSNMPSetDebug()' - Enable/disable debug logging to stderr.
- *
- * @since CUPS 1.4@
  */
 
 void
@@ -414,10 +503,80 @@ _cupsSNMPSetDebug(int level)              /* I - 1 to enable debug output, 0 otherwise */
   _cups_globals_t *cg = _cupsGlobals();        /* Global data */
 
 
+  DEBUG_printf(("4_cupsSNMPSetDebug(level=%d)", level));
+
   cg->snmp_debug = level;
 }
 
 
+/*
+ * '_cupsSNMPStringToOID()' - Convert a numeric OID string to an OID array.
+ *
+ * This function converts a string of the form ".N.N.N.N.N" to the
+ * corresponding OID array terminated by -1.
+ *
+ * @code NULL@ is returned if the array is not large enough or the string is
+ * not a valid OID number.
+ */
+
+int *                                  /* O - Pointer to OID array or @code NULL@ on error */
+_cupsSNMPStringToOID(const char *src,  /* I - OID string */
+                     int        *dst,  /* I - OID array */
+                    int        dstsize)/* I - Number of integers in OID array */
+{
+  int  *dstptr,                        /* Pointer into OID array */
+       *dstend;                        /* End of OID array */
+
+
+  DEBUG_printf(("4_cupsSNMPStringToOID(src=\"%s\", dst=%p, dstsize=%d)",
+                src, dst, dstsize));
+
+ /*
+  * Range check input...
+  */
+
+  if (!src || !dst || dstsize < 2)
+    return (NULL);
+
+ /*
+  * Skip leading "."...
+  */
+
+  if (*src == '.')
+    src ++;
+
+ /*
+  * Loop to the end of the string...
+  */
+
+  for (dstend = dst + dstsize - 1, dstptr = dst, *dstptr = 0;
+       *src && dstptr < dstend;
+       src ++)
+  {
+    if (*src == '.')
+    {
+      dstptr ++;
+      *dstptr = 0;
+    }
+    else if (isdigit(*src & 255))
+      *dstptr = *dstptr * 10 + *src - '0';
+    else
+      break;
+  }
+
+  if (*src)
+    return (NULL);
+
+ /*
+  * Terminate the end of the OID array and return...
+  */
+
+  dstptr[1] = -1;
+
+  return (dst);
+}
+
+
 /*
  * '_cupsSNMPWalk()' - Enumerate a group of OIDs.
  *
@@ -428,8 +587,6 @@ _cupsSNMPSetDebug(int level)                /* I - 1 to enable debug output, 0 otherwise */
  *
  * If "timeout" is negative, @code _cupsSNMPWalk@ will wait for a response
  * indefinitely.
- *
- * @since CUPS 1.4@
  */
 
 int                                    /* O - Number of OIDs found or -1 on error */
@@ -445,39 +602,69 @@ _cupsSNMPWalk(int            fd,  /* I - SNMP socket */
   int          count = 0;              /* Number of OIDs found */
   int          request_id = 0;         /* Current request ID */
   cups_snmp_t  packet;                 /* Current response packet */
+  int          lastoid[CUPS_SNMP_MAX_OID];
+                                       /* Last OID we got */
 
 
  /*
   * Range check input...
   */
 
+  DEBUG_printf(("4_cupsSNMPWalk(fd=%d, address=%p, version=%d, "
+                "community=\"%s\", prefix=%p, timeout=%.1f, cb=%p, data=%p)",
+               fd, address, version, community, prefix, timeout, cb, data));
+
   if (fd < 0 || !address || version != CUPS_SNMP_VERSION_1 || !community ||
       !prefix || !cb)
+  {
+    DEBUG_puts("5_cupsSNMPWalk: Returning -1");
+
     return (-1);
+  }
 
  /*
   * Copy the OID prefix and then loop until we have no more OIDs...
   */
 
   _cupsSNMPCopyOID(packet.object_name, prefix, CUPS_SNMP_MAX_OID);
+  lastoid[0] = -1;
 
   for (;;)
   {
     request_id ++;
 
     if (!_cupsSNMPWrite(fd, address, version, community,
-                       CUPS_ASN1_GET_NEXT_REQUEST, request_id,
-                      packet.object_name))
+                        CUPS_ASN1_GET_NEXT_REQUEST, request_id,
+                       packet.object_name))
+    {
+      DEBUG_puts("5_cupsSNMPWalk: Returning -1");
+
       return (-1);
+    }
 
     if (!_cupsSNMPRead(fd, &packet, timeout))
+    {
+      DEBUG_puts("5_cupsSNMPWalk: Returning -1");
+
       return (-1);
+    }
+
+    if (!_cupsSNMPIsOIDPrefixed(&packet, prefix) ||
+        _cupsSNMPIsOID(&packet, lastoid))
+    {
+      DEBUG_printf(("5_cupsSNMPWalk: Returning %d", count));
 
-    if (!_cupsSNMPIsOIDPrefixed(&packet, prefix))
       return (count);
+    }
 
     if (packet.error || packet.error_status)
+    {
+      DEBUG_printf(("5_cupsSNMPWalk: Returning %d", count > 0 ? count : -1));
+
       return (count > 0 ? count : -1);
+    }
+
+    _cupsSNMPCopyOID(lastoid, packet.object_name, CUPS_SNMP_MAX_OID);
 
     count ++;
 
@@ -490,8 +677,6 @@ _cupsSNMPWalk(int            fd,    /* I - SNMP socket */
  * '_cupsSNMPWrite()' - Send an SNMP query packet.
  *
  * The array pointed to by "oid" is terminated by the value -1.
- *
- * @since CUPS 1.4@
  */
 
 int                                    /* O - 1 on success, 0 on error */
@@ -509,16 +694,25 @@ _cupsSNMPWrite(
   unsigned char        buffer[CUPS_SNMP_MAX_PACKET];
                                        /* SNMP message buffer */
   int          bytes;                  /* Size of message */
+  http_addr_t  temp;                   /* Copy of address */
 
 
  /*
   * Range check input...
   */
 
+  DEBUG_printf(("4_cupsSNMPWrite(fd=%d, address=%p, version=%d, "
+                "community=\"%s\", request_type=%d, request_id=%u, oid=%p)",
+               fd, address, version, community, request_type, request_id, oid));
+
   if (fd < 0 || !address || version != CUPS_SNMP_VERSION_1 || !community ||
       (request_type != CUPS_ASN1_GET_REQUEST &&
        request_type != CUPS_ASN1_GET_NEXT_REQUEST) || request_id < 1 || !oid)
+  {
+    DEBUG_puts("5_cupsSNMPWrite: Returning 0 (bad arguments)");
+
     return (0);
+  }
 
  /*
   * Create the SNMP message...
@@ -530,7 +724,7 @@ _cupsSNMPWrite(
   packet.request_type = request_type;
   packet.request_id   = request_id;
   packet.object_type  = CUPS_ASN1_NULL_VALUE;
-  
+
   strlcpy(packet.community, community, sizeof(packet.community));
 
   for (i = 0; oid[i] >= 0 && i < (CUPS_SNMP_MAX_OID - 1); i ++)
@@ -539,6 +733,8 @@ _cupsSNMPWrite(
 
   if (oid[i] >= 0)
   {
+    DEBUG_puts("5_cupsSNMPWrite: Returning 0 (OID too big)");
+
     errno = E2BIG;
     return (0);
   }
@@ -547,6 +743,8 @@ _cupsSNMPWrite(
 
   if (bytes < 0)
   {
+    DEBUG_puts("5_cupsSNMPWrite: Returning 0 (request too big)");
+
     errno = E2BIG;
     return (0);
   }
@@ -557,15 +755,12 @@ _cupsSNMPWrite(
   * Send the message...
   */
 
-#ifdef AF_INET6
-  if (address->addr.sa_family == AF_INET6)
-    address->ipv6.sin6_port = htons(CUPS_SNMP_PORT);
-  else
-#endif /* AF_INET6 */
-  address->ipv4.sin_port = htons(CUPS_SNMP_PORT);
+  temp = *address;
+
+  _httpAddrSetPort(&temp, CUPS_SNMP_PORT);
 
-  return (sendto(fd, buffer, bytes, 0, (void *)address,
-                 httpAddrLength(address)) == bytes);
+  return (sendto(fd, buffer, bytes, 0, (void *)&temp,
+                 httpAddrLength(&temp)) == bytes);
 }
 
 
@@ -603,11 +798,11 @@ asn1_debug(const char    *prefix, /* I - Prefix string */
 
     fprintf(stderr, "%sHex Dump (%d bytes):\n", prefix, (int)len);
 
-    for (i = 0; i < len; i += 16)
+    for (i = 0; i < (int)len; i += 16)
     {
       fprintf(stderr, "%s%04x:", prefix, i);
 
-      for (j = 0; j < 16 && (i + j) < len; j ++)
+      for (j = 0; j < 16 && (i + j) < (int)len; j ++)
       {
         if (j && !(j & 3))
          fprintf(stderr, "  %02x", buffer[i + j]);
@@ -627,7 +822,7 @@ asn1_debug(const char    *prefix,   /* I - Prefix string */
 
       fputs("    ", stderr);
 
-      for (j = 0; j < 16 && (i + j) < len; j ++)
+      for (j = 0; j < 16 && (i + j) < (int)len; j ++)
         if (buffer[i + j] < ' ' || buffer[i + j] >= 0x7f)
          putc('.', stderr);
        else
@@ -764,7 +959,7 @@ asn1_debug(const char    *prefix,   /* I - Prefix string */
     }
   }
 }
-          
+
 
 /*
  * 'asn1_decode_snmp()' - Decode a SNMP packet.
@@ -881,9 +1076,12 @@ asn1_decode_snmp(unsigned char *buffer,   /* I - Buffer */
                    break;
 
                case CUPS_ASN1_OCTET_STRING :
+               case CUPS_ASN1_BIT_STRING :
+               case CUPS_ASN1_HEX_STRING :
+                   packet->object_value.string.num_bytes = length;
                    asn1_get_string(&bufptr, bufend, length,
-                                   packet->object_value.string,
-                                   CUPS_SNMP_MAX_STRING);
+                                   (char *)packet->object_value.string.bytes,
+                                   sizeof(packet->object_value.string.bytes));
                    break;
 
                case CUPS_ASN1_OID :
@@ -891,14 +1089,6 @@ asn1_decode_snmp(unsigned char *buffer,   /* I - Buffer */
                                 packet->object_value.oid, CUPS_SNMP_MAX_OID);
                    break;
 
-               case CUPS_ASN1_HEX_STRING :
-                   packet->object_value.hex_string.num_bytes = length;
-
-                   asn1_get_string(&bufptr, bufend, length,
-                                   (char *)packet->object_value.hex_string.bytes,
-                                   CUPS_SNMP_MAX_STRING);
-                   break;
-
                case CUPS_ASN1_COUNTER :
                    packet->object_value.counter =
                        asn1_get_integer(&bufptr, bufend, length);
@@ -971,7 +1161,7 @@ asn1_encode_snmp(unsigned char *buffer,    /* I - Buffer */
        break;
 
     case CUPS_ASN1_OCTET_STRING :
-        valuelen = strlen(packet->object_value.string);
+        valuelen = packet->object_value.string.num_bytes;
        break;
 
     case CUPS_ASN1_OID :
@@ -996,7 +1186,7 @@ asn1_encode_snmp(unsigned char *buffer,    /* I - Buffer */
            1 + asn1_size_length(reqlen) + reqlen;
   total   = 1 + asn1_size_length(msglen) + msglen;
 
-  if (total > bufsize)
+  if (total > (int)bufsize)
   {
     packet->error = "Message too large for buffer";
     return (-1);
@@ -1056,7 +1246,7 @@ asn1_encode_snmp(unsigned char *buffer,   /* I - Buffer */
     case CUPS_ASN1_OCTET_STRING :
         *bufptr++ = CUPS_ASN1_OCTET_STRING;
        asn1_set_length(&bufptr, valuelen);
-       memcpy(bufptr, packet->object_value.string, valuelen);
+       memcpy(bufptr, packet->object_value.string.bytes, valuelen);
        bufptr += valuelen;
        break;
 
@@ -1085,7 +1275,13 @@ asn1_get_integer(
   int  value;                          /* Integer value */
 
 
-  for (value = 0;
+  if (length > sizeof(int))
+  {
+    (*buffer) += length;
+    return (0);
+  }
+
+  for (value = (**buffer & 0x80) ? -1 : 0;
        length > 0 && *buffer < bufend;
        length --, (*buffer) ++)
     value = (value << 8) | **buffer;
@@ -1098,18 +1294,32 @@ asn1_get_integer(
  * 'asn1_get_length()' - Get a value length.
  */
 
-static int                             /* O  - Length */
+static unsigned                                /* O  - Length */
 asn1_get_length(unsigned char **buffer,        /* IO - Pointer in buffer */
                unsigned char *bufend)  /* I  - End of buffer */
 {
-  int  length;                         /* Length */
+  unsigned     length;                 /* Length */
 
 
   length = **buffer;
   (*buffer) ++;
 
   if (length & 128)
-    length = asn1_get_integer(buffer, bufend, length & 127);
+  {
+    int        count;                          /* Number of bytes for length */
+
+
+    if ((count = length & 127) > sizeof(unsigned))
+    {
+      (*buffer) += count;
+      return (0);
+    }
+
+    for (length = 0;
+        count > 0 && *buffer < bufend;
+        count --, (*buffer) ++)
+      length = (length << 8) | **buffer;
+  }
 
   return (length);
 }
@@ -1211,6 +1421,9 @@ asn1_get_string(
     char          *string,             /* I  - String buffer */
     int           strsize)             /* I  - String buffer size */
 {
+  if (length > (bufend - *buffer))
+    length = bufend - *buffer;
+
   if (length < 0)
   {
    /*
@@ -1328,7 +1541,7 @@ asn1_set_integer(unsigned char **buffer,/* IO - Pointer in buffer */
 
 static void
 asn1_set_length(unsigned char **buffer,        /* IO - Pointer in buffer */
-               int           length)   /* I  - Length value */
+               unsigned      length)   /* I  - Length value */
 {
   if (length > 255)
   {