Coverage Report

Created: 2025-12-31 07:31

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