]> git.ipfire.org Git - thirdparty/cups.git/blame - backend/snmp.c
Full sweep of all Clang warnings, plus some bug fixes for incorrect memcpy usage.
[thirdparty/cups.git] / backend / snmp.c
CommitLineData
89d46774 1/*
f2d18633 2 * "$Id$"
89d46774 3 *
7e86f2f6 4 * SNMP discovery backend for CUPS.
89d46774 5 *
7e86f2f6
MS
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 2006-2007 by Easy Software Products, all rights reserved.
89d46774 8 *
7e86f2f6
MS
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/".
89d46774 14 *
7e86f2f6 15 * This file is subject to the Apple OS-Developed Software exception.
89d46774 16 */
17
18/*
19 * Include necessary headers.
20 */
21
ed486911 22#include "backend-private.h"
89d46774 23#include <cups/array.h>
24#include <cups/file.h>
91c84a35 25#include <cups/http-private.h>
b94498cf 26#include <regex.h>
89d46774 27
28
29/*
30 * This backend implements SNMP printer discovery. It uses a broadcast-
b94498cf 31 * based approach to get SNMP response packets from potential printers,
7a14d768
MS
32 * requesting OIDs from the Host and Port Monitor MIBs, does a URI
33 * lookup based on the device description string, and finally a probe of
34 * port 9100 (AppSocket) and 515 (LPD).
89d46774 35 *
36 * The current focus is on printers with internal network cards, although
7a14d768 37 * the code also works with many external print servers as well.
89d46774 38 *
39 * The backend reads the snmp.conf file from the CUPS_SERVERROOT directory
40 * which can contain comments, blank lines, or any number of the following
41 * directives:
42 *
43 * Address ip-address
44 * Address @LOCAL
45 * Address @IF(name)
46 * Community name
47 * DebugLevel N
b94498cf 48 * DeviceURI "regex pattern" uri
89d46774 49 * HostNameLookups on
50 * HostNameLookups off
b94498cf 51 * MaxRunTime N
89d46774 52 *
53 * The default is to use:
54 *
55 * Address @LOCAL
56 * Community public
57 * DebugLevel 0
58 * HostNameLookups off
ae71f5de 59 * MaxRunTime 120
89d46774 60 *
61 * This backend is known to work with the following network printers and
62 * print servers:
63 *
64 * Axis OfficeBasic, 5400, 5600
7a14d768 65 * Brother
89d46774 66 * EPSON
67 * Genicom
68 * HP JetDirect
69 * Lexmark
70 * Sharp
71 * Tektronix
72 * Xerox
73 *
74 * It does not currently work with:
75 *
76 * DLink
77 * Linksys
78 * Netgear
79 * Okidata
80 *
81 * (for all of these, they do not support the Host MIB)
82 */
83
89d46774 84/*
85 * Types...
86 */
87
749b1e90
MS
88enum /**** Request IDs for each field ****/
89{
90 DEVICE_TYPE = 1,
91 DEVICE_DESCRIPTION,
92 DEVICE_LOCATION,
93 DEVICE_ID,
94 DEVICE_URI,
95 DEVICE_PRODUCT
96};
97
b94498cf 98typedef struct device_uri_s /**** DeviceURI values ****/
99{
100 regex_t re; /* Regular expression to match */
101 cups_array_t *uris; /* URIs */
102} device_uri_t;
103
89d46774 104typedef struct snmp_cache_s /**** SNMP scan cache ****/
105{
106 http_addr_t address; /* Address of device */
107 char *addrname, /* Name of device */
108 *uri, /* device-uri */
109 *id, /* device-id */
7a14d768 110 *info, /* device-info */
749b1e90 111 *location, /* device-location */
89d46774 112 *make_and_model; /* device-make-and-model */
749b1e90 113 int sent; /* Has this device been listed? */
89d46774 114} snmp_cache_t;
115
89d46774 116
117/*
118 * Local functions...
119 */
120
121static char *add_array(cups_array_t *a, const char *s);
122static void add_cache(http_addr_t *addr, const char *addrname,
123 const char *uri, const char *id,
124 const char *make_and_model);
b94498cf 125static device_uri_t *add_device_uri(char *value);
89d46774 126static void alarm_handler(int sig);
89d46774 127static int compare_cache(snmp_cache_t *a, snmp_cache_t *b);
128static void debug_printf(const char *format, ...);
129static void fix_make_model(char *make_model,
130 const char *old_make_model,
131 int make_model_size);
132static void free_array(cups_array_t *a);
133static void free_cache(void);
134static http_addrlist_t *get_interface_addresses(const char *ifname);
d09495fa 135static void list_device(snmp_cache_t *cache);
89d46774 136static const char *password_cb(const char *prompt);
137static void probe_device(snmp_cache_t *device);
138static void read_snmp_conf(const char *address);
139static void read_snmp_response(int fd);
140static double run_time(void);
22c9029b 141static void scan_devices(int ipv4, int ipv6);
89d46774 142static int try_connect(http_addr_t *addr, const char *addrname,
143 int port);
144static void update_cache(snmp_cache_t *device, const char *uri,
145 const char *id, const char *make_model);
146
147
148/*
149 * Local globals...
150 */
151
152static cups_array_t *Addresses = NULL;
153static cups_array_t *Communities = NULL;
154static cups_array_t *Devices = NULL;
155static int DebugLevel = 0;
749b1e90
MS
156static const int DescriptionOID[] = { CUPS_OID_hrDeviceDescr, 1, -1 };
157static const int LocationOID[] = { CUPS_OID_sysLocation, 0, -1 };
7a14d768 158static const int DeviceTypeOID[] = { CUPS_OID_hrDeviceType, 1, -1 };
7a14d768 159static const int DeviceIdOID[] = { CUPS_OID_ppmPrinterIEEE1284DeviceId, 1, -1 };
749b1e90
MS
160static const int UriOID[] = { CUPS_OID_ppmPortServiceNameOrURI, 1, 1, -1 };
161static const int LexmarkProductOID[] = { 1,3,6,1,4,1,641,2,1,2,1,2,1,-1 };
162static const int LexmarkProductOID2[] = { 1,3,6,1,4,1,674,10898,100,2,1,2,1,2,1,-1 };
163static const int LexmarkDeviceIdOID[] = { 1,3,6,1,4,1,641,2,1,2,1,3,1,-1 };
164static const int XeroxProductOID[] = { 1,3,6,1,4,1,128,2,1,3,1,2,0,-1 };
b94498cf 165static cups_array_t *DeviceURIs = NULL;
89d46774 166static int HostNameLookups = 0;
ae71f5de 167static int MaxRunTime = 120;
89d46774 168static struct timeval StartTime;
169
170
171/*
172 * 'main()' - Discover printers via SNMP.
173 */
174
175int /* O - Exit status */
176main(int argc, /* I - Number of command-line arguments (6 or 7) */
177 char *argv[]) /* I - Command-line arguments */
178{
22c9029b
MS
179 int ipv4, /* SNMP IPv4 socket */
180 ipv6; /* SNMP IPv6 socket */
d09495fa 181#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
182 struct sigaction action; /* Actions for POSIX signals */
183#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
89d46774 184
185
186 /*
187 * Check command-line options...
188 */
189
190 if (argc > 2)
191 {
0837b7e8 192 _cupsLangPuts(stderr, _("Usage: snmp [host-or-ip-address]"));
89d46774 193 return (1);
194 }
195
196 /*
197 * Set the password callback for IPP operations...
198 */
199
200 cupsSetPasswordCB(password_cb);
201
d09495fa 202 /*
203 * Catch SIGALRM signals...
204 */
205
206#ifdef HAVE_SIGSET
207 sigset(SIGALRM, alarm_handler);
208#elif defined(HAVE_SIGACTION)
209 memset(&action, 0, sizeof(action));
210
211 sigemptyset(&action.sa_mask);
212 sigaddset(&action.sa_mask, SIGALRM);
213 action.sa_handler = alarm_handler;
214 sigaction(SIGALRM, &action, NULL);
215#else
216 signal(SIGALRM, alarm_handler);
217#endif /* HAVE_SIGSET */
218
89d46774 219 /*
220 * Open the SNMP socket...
221 */
222
22c9029b 223 if ((ipv4 = _cupsSNMPOpen(AF_INET)) < 0)
89d46774 224 return (1);
225
22c9029b
MS
226#ifdef AF_INET6
227 if ((ipv6 = _cupsSNMPOpen(AF_INET6)) < 0)
37e7e6e0 228 perror("DEBUG: Unable to create IPv6 socket");
22c9029b
MS
229#else
230 ipv6 = -1;
231#endif /* AF_INET6 */
232
89d46774 233 /*
234 * Read the configuration file and any cache data...
235 */
236
237 read_snmp_conf(argv[1]);
238
7a14d768 239 _cupsSNMPSetDebug(DebugLevel);
91c84a35 240
89d46774 241 Devices = cupsArrayNew((cups_array_func_t)compare_cache, NULL);
242
243 /*
244 * Scan for devices...
245 */
246
22c9029b 247 scan_devices(ipv4, ipv6);
89d46774 248
89d46774 249 /*
250 * Close, free, and return with no errors...
251 */
252
22c9029b
MS
253 _cupsSNMPClose(ipv4);
254 if (ipv6 >= 0)
255 _cupsSNMPClose(ipv6);
89d46774 256
257 free_array(Addresses);
258 free_array(Communities);
259 free_cache();
260
261 return (0);
262}
263
264
265/*
266 * 'add_array()' - Add a string to an array.
267 */
268
269static char * /* O - New string */
270add_array(cups_array_t *a, /* I - Array */
271 const char *s) /* I - String to add */
272{
273 char *dups; /* New string */
274
275
276 dups = strdup(s);
277
278 cupsArrayAdd(a, dups);
279
280 return (dups);
281}
282
283
284/*
285 * 'add_cache()' - Add a cached device...
286 */
287
288static void
289add_cache(http_addr_t *addr, /* I - Device IP address */
290 const char *addrname, /* I - IP address or name string */
291 const char *uri, /* I - Device URI */
292 const char *id, /* I - 1284 device ID */
293 const char *make_and_model) /* I - Make and model */
294{
295 snmp_cache_t *temp; /* New device entry */
296
297
298 debug_printf("DEBUG: add_cache(addr=%p, addrname=\"%s\", uri=\"%s\", "
299 "id=\"%s\", make_and_model=\"%s\")\n",
d09495fa 300 addr, addrname, uri ? uri : "(null)", id ? id : "(null)",
89d46774 301 make_and_model ? make_and_model : "(null)");
302
303 temp = calloc(1, sizeof(snmp_cache_t));
304 memcpy(&(temp->address), addr, sizeof(temp->address));
305
306 temp->addrname = strdup(addrname);
307
308 if (uri)
309 temp->uri = strdup(uri);
310
311 if (id)
312 temp->id = strdup(id);
313
314 if (make_and_model)
315 temp->make_and_model = strdup(make_and_model);
316
317 cupsArrayAdd(Devices, temp);
d09495fa 318
319 if (uri)
320 list_device(temp);
89d46774 321}
322
323
b94498cf 324/*
325 * 'add_device_uri()' - Add a device URI to the cache.
326 *
327 * The value string is modified (chopped up) as needed.
328 */
329
330static device_uri_t * /* O - Device URI */
331add_device_uri(char *value) /* I - Value from snmp.conf */
332{
333 device_uri_t *device_uri; /* Device URI */
334 char *start; /* Start of value */
335
336
337 /*
338 * Allocate memory as needed...
339 */
340
341 if (!DeviceURIs)
342 DeviceURIs = cupsArrayNew(NULL, NULL);
343
344 if (!DeviceURIs)
345 return (NULL);
346
347 if ((device_uri = calloc(1, sizeof(device_uri_t))) == NULL)
348 return (NULL);
349
350 if ((device_uri->uris = cupsArrayNew(NULL, NULL)) == NULL)
351 {
352 free(device_uri);
353 return (NULL);
354 }
355
356 /*
357 * Scan the value string for the regular expression and URI(s)...
358 */
359
360 value ++; /* Skip leading " */
361
362 for (start = value; *value && *value != '\"'; value ++)
363 if (*value == '\\' && value[1])
364 _cups_strcpy(value, value + 1);
365
366 if (!*value)
367 {
368 fputs("ERROR: Missing end quote for DeviceURI!\n", stderr);
369
370 cupsArrayDelete(device_uri->uris);
371 free(device_uri);
372
373 return (NULL);
374 }
375
376 *value++ = '\0';
377
378 if (regcomp(&(device_uri->re), start, REG_EXTENDED | REG_ICASE))
379 {
380 fputs("ERROR: Bad regular expression for DeviceURI!\n", stderr);
381
382 cupsArrayDelete(device_uri->uris);
383 free(device_uri);
384
385 return (NULL);
386 }
387
388 while (*value)
389 {
390 while (isspace(*value & 255))
391 value ++;
392
393 if (!*value)
394 break;
395
396 for (start = value; *value && !isspace(*value & 255); value ++);
397
398 if (*value)
399 *value++ = '\0';
400
401 cupsArrayAdd(device_uri->uris, strdup(start));
402 }
403
404 /*
405 * Add the device URI to the list and return it...
406 */
407
408 cupsArrayAdd(DeviceURIs, device_uri);
409
410 return (device_uri);
411}
412
413
89d46774 414/*
415 * 'alarm_handler()' - Handle alarm signals...
416 */
417
418static void
419alarm_handler(int sig) /* I - Signal number */
420{
421 /*
422 * Do nothing...
423 */
424
425 (void)sig;
426
d09495fa 427#if !defined(HAVE_SIGSET) && !defined(HAVE_SIGACTION)
428 signal(SIGALRM, alarm_handler);
429#endif /* !HAVE_SIGSET && !HAVE_SIGACTION */
430
89d46774 431 if (DebugLevel)
432 write(2, "DEBUG: ALARM!\n", 14);
433}
434
435
89d46774 436/*
437 * 'compare_cache()' - Compare two cache entries.
438 */
439
440static int /* O - Result of comparison */
441compare_cache(snmp_cache_t *a, /* I - First cache entry */
442 snmp_cache_t *b) /* I - Second cache entry */
443{
88f9aafc 444 return (_cups_strcasecmp(a->addrname, b->addrname));
89d46774 445}
446
447
448/*
449 * 'debug_printf()' - Display some debugging information.
450 */
451
452static void
453debug_printf(const char *format, /* I - Printf-style format string */
454 ...) /* I - Additional arguments as needed */
455{
456 va_list ap; /* Pointer to arguments */
457
458
459 if (!DebugLevel)
460 return;
461
462 va_start(ap, format);
463 vfprintf(stderr, format, ap);
464 va_end(ap);
465}
466
467
468/*
469 * 'fix_make_model()' - Fix common problems in the make-and-model string.
470 */
471
472static void
473fix_make_model(
474 char *make_model, /* I - New make-and-model string */
475 const char *old_make_model, /* I - Old make-and-model string */
476 int make_model_size) /* I - Size of new string buffer */
477{
a74454a7 478 char *mmptr; /* Pointer into make-and-model string */
89d46774 479
480
481 /*
482 * Fix some common problems with the make-and-model string so
483 * that printer driver detection works better...
484 */
485
88f9aafc 486 if (!_cups_strncasecmp(old_make_model, "Hewlett-Packard", 15))
89d46774 487 {
488 /*
489 * Strip leading Hewlett-Packard and hp prefixes and replace
490 * with a single HP manufacturer prefix...
491 */
492
a74454a7 493 mmptr = (char *)old_make_model + 15;
89d46774 494
495 while (isspace(*mmptr & 255))
496 mmptr ++;
497
88f9aafc 498 if (!_cups_strncasecmp(mmptr, "hp", 2))
89d46774 499 {
500 mmptr += 2;
501
502 while (isspace(*mmptr & 255))
503 mmptr ++;
504 }
505
506 make_model[0] = 'H';
507 make_model[1] = 'P';
508 make_model[2] = ' ';
509 strlcpy(make_model + 3, mmptr, make_model_size - 3);
510 }
88f9aafc 511 else if (!_cups_strncasecmp(old_make_model, "deskjet", 7))
89d46774 512 snprintf(make_model, make_model_size, "HP DeskJet%s", old_make_model + 7);
88f9aafc 513 else if (!_cups_strncasecmp(old_make_model, "officejet", 9))
b86bc4cf 514 snprintf(make_model, make_model_size, "HP OfficeJet%s", old_make_model + 9);
88f9aafc 515 else if (!_cups_strncasecmp(old_make_model, "stylus_pro_", 11))
89d46774 516 snprintf(make_model, make_model_size, "EPSON Stylus Pro %s",
517 old_make_model + 11);
518 else
519 strlcpy(make_model, old_make_model, make_model_size);
520
521 if ((mmptr = strstr(make_model, ", Inc.,")) != NULL)
522 {
523 /*
524 * Strip inc. from name, e.g. "Tektronix, Inc., Phaser 560"
525 * becomes "Tektronix Phaser 560"...
526 */
527
a74454a7 528 _cups_strcpy(mmptr, mmptr + 7);
529 }
530
d09495fa 531 if ((mmptr = strstr(make_model, " Network")) != NULL)
532 {
533 /*
534 * Drop unnecessary informational text, e.g. "Xerox DocuPrint N2025
535 * Network LaserJet - 2.12" becomes "Xerox DocuPrint N2025"...
536 */
537
538 *mmptr = '\0';
539 }
540
a74454a7 541 if ((mmptr = strchr(make_model, ',')) != NULL)
542 {
543 /*
544 * Drop anything after a trailing comma...
545 */
546
547 *mmptr = '\0';
89d46774 548 }
549}
550
551
552/*
553 * 'free_array()' - Free an array of strings.
554 */
555
556static void
557free_array(cups_array_t *a) /* I - Array */
558{
559 char *s; /* Current string */
560
561
562 for (s = (char *)cupsArrayFirst(a); s; s = (char *)cupsArrayNext(a))
563 free(s);
564
565 cupsArrayDelete(a);
566}
567
568
569/*
570 * 'free_cache()' - Free the array of cached devices.
571 */
572
573static void
574free_cache(void)
575{
576 snmp_cache_t *cache; /* Cached device */
577
578
579 for (cache = (snmp_cache_t *)cupsArrayFirst(Devices);
580 cache;
581 cache = (snmp_cache_t *)cupsArrayNext(Devices))
582 {
583 free(cache->addrname);
584
585 if (cache->uri)
586 free(cache->uri);
587
588 if (cache->id)
589 free(cache->id);
590
591 if (cache->make_and_model)
592 free(cache->make_and_model);
593
594 free(cache);
595 }
596
597 cupsArrayDelete(Devices);
598 Devices = NULL;
599}
600
601
602/*
603 * 'get_interface_addresses()' - Get the broadcast address(es) associated
604 * with an interface.
605 */
606
607static http_addrlist_t * /* O - List of addresses */
608get_interface_addresses(
609 const char *ifname) /* I - Interface name */
610{
611 struct ifaddrs *addrs, /* Interface address list */
612 *addr; /* Current interface address */
613 http_addrlist_t *first, /* First address in list */
614 *last, /* Last address in list */
615 *current; /* Current address */
616
617
618 if (getifaddrs(&addrs) < 0)
619 return (NULL);
620
621 for (addr = addrs, first = NULL, last = NULL; addr; addr = addr->ifa_next)
622 if ((addr->ifa_flags & IFF_BROADCAST) && addr->ifa_broadaddr &&
623 addr->ifa_broadaddr->sa_family == AF_INET &&
624 (!ifname || !strcmp(ifname, addr->ifa_name)))
625 {
626 current = calloc(1, sizeof(http_addrlist_t));
627
628 memcpy(&(current->addr), addr->ifa_broadaddr,
629 sizeof(struct sockaddr_in));
630
631 if (!last)
632 first = current;
633 else
634 last->next = current;
635
636 last = current;
637 }
638
639 freeifaddrs(addrs);
640
641 return (first);
642}
643
644
89d46774 645/*
d09495fa 646 * 'list_device()' - List a device we found...
89d46774 647 */
648
649static void
d09495fa 650list_device(snmp_cache_t *cache) /* I - Cached device */
89d46774 651{
d09495fa 652 if (cache->uri)
749b1e90
MS
653 cupsBackendReport("network", cache->uri, cache->make_and_model,
654 cache->info, cache->id, cache->location);
89d46774 655}
656
657
89d46774 658/*
659 * 'password_cb()' - Handle authentication requests.
660 *
661 * All we do right now is return NULL, indicating that no authentication
662 * is possible.
663 */
664
665static const char * /* O - Password (NULL) */
666password_cb(const char *prompt) /* I - Prompt message */
667{
668 (void)prompt; /* Anti-compiler-warning-code */
669
670 return (NULL);
671}
672
673
674/*
675 * 'probe_device()' - Probe a device to discover whether it is a printer.
676 *
677 * TODO: Try using the Port Monitor MIB to discover the correct protocol
678 * to use - first need a commercially-available printer that supports
679 * it, though...
680 */
681
682static void
683probe_device(snmp_cache_t *device) /* I - Device */
684{
b94498cf 685 char uri[1024], /* Full device URI */
686 *uriptr, /* Pointer into URI */
687 *format; /* Format string for device */
688 device_uri_t *device_uri; /* Current DeviceURI match */
89d46774 689
690
89d46774 691 debug_printf("DEBUG: %.3f Probing %s...\n", run_time(), device->addrname);
692
b94498cf 693#ifdef __APPLE__
694 /*
22c9029b 695 * If the printer supports Bonjour/mDNS, don't report it from the SNMP backend.
b94498cf 696 */
d09495fa 697
b94498cf 698 if (!try_connect(&(device->address), device->addrname, 5353))
d09495fa 699 {
b94498cf 700 debug_printf("DEBUG: %s supports mDNS, not reporting!\n", device->addrname);
701 return;
d09495fa 702 }
b94498cf 703#endif /* __APPLE__ */
d09495fa 704
b94498cf 705 /*
706 * Lookup the device in the match table...
707 */
89d46774 708
b94498cf 709 for (device_uri = (device_uri_t *)cupsArrayFirst(DeviceURIs);
710 device_uri;
711 device_uri = (device_uri_t *)cupsArrayNext(DeviceURIs))
ac884b6a
MS
712 if (device->make_and_model &&
713 !regexec(&(device_uri->re), device->make_and_model, 0, NULL, 0))
89d46774 714 {
d09495fa 715 /*
b94498cf 716 * Found a match, add the URIs...
89d46774 717 */
718
b94498cf 719 for (format = (char *)cupsArrayFirst(device_uri->uris);
720 format;
721 format = (char *)cupsArrayNext(device_uri->uris))
89d46774 722 {
b94498cf 723 for (uriptr = uri; *format && uriptr < (uri + sizeof(uri) - 1);)
724 if (*format == '%' && format[1] == 's')
89d46774 725 {
b94498cf 726 /*
727 * Insert hostname/address...
728 */
89d46774 729
7e86f2f6 730 strlcpy(uriptr, device->addrname, sizeof(uri) - (size_t)(uriptr - uri));
b94498cf 731 uriptr += strlen(uriptr);
732 format += 2;
733 }
734 else
735 *uriptr++ = *format++;
89d46774 736
b94498cf 737 *uriptr = '\0';
89d46774 738
b94498cf 739 update_cache(device, uri, NULL, NULL);
89d46774 740 }
741
89d46774 742 return;
b94498cf 743 }
89d46774 744
745 /*
b94498cf 746 * Then try the standard ports...
89d46774 747 */
748
749 if (!try_connect(&(device->address), device->addrname, 9100))
750 {
751 debug_printf("DEBUG: %s supports AppSocket!\n", device->addrname);
752
753 snprintf(uri, sizeof(uri), "socket://%s", device->addrname);
754 update_cache(device, uri, NULL, NULL);
755 }
756 else if (!try_connect(&(device->address), device->addrname, 515))
757 {
758 debug_printf("DEBUG: %s supports LPD!\n", device->addrname);
759
760 snprintf(uri, sizeof(uri), "lpd://%s/", device->addrname);
761 update_cache(device, uri, NULL, NULL);
762 }
763}
764
765
766/*
767 * 'read_snmp_conf()' - Read the snmp.conf file.
768 */
769
770static void
771read_snmp_conf(const char *address) /* I - Single address to probe */
772{
773 cups_file_t *fp; /* File pointer */
774 char filename[1024], /* Filename */
775 line[1024], /* Line from file */
776 *value; /* Value on line */
777 int linenum; /* Line number */
778 const char *cups_serverroot; /* CUPS_SERVERROOT env var */
779 const char *debug; /* CUPS_DEBUG_LEVEL env var */
d09495fa 780 const char *runtime; /* CUPS_MAX_RUN_TIME env var */
89d46774 781
782
783 /*
784 * Initialize the global address and community lists...
785 */
786
787 Addresses = cupsArrayNew(NULL, NULL);
788 Communities = cupsArrayNew(NULL, NULL);
789
790 if (address)
791 add_array(Addresses, address);
792
793 if ((debug = getenv("CUPS_DEBUG_LEVEL")) != NULL)
794 DebugLevel = atoi(debug);
795
d09495fa 796 if ((runtime = getenv("CUPS_MAX_RUN_TIME")) != NULL)
797 MaxRunTime = atoi(runtime);
798
89d46774 799 /*
800 * Find the snmp.conf file...
801 */
802
803 if ((cups_serverroot = getenv("CUPS_SERVERROOT")) == NULL)
804 cups_serverroot = CUPS_SERVERROOT;
805
806 snprintf(filename, sizeof(filename), "%s/snmp.conf", cups_serverroot);
807
808 if ((fp = cupsFileOpen(filename, "r")) != NULL)
809 {
810 /*
811 * Read the snmp.conf file...
812 */
813
814 linenum = 0;
815
816 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
817 {
818 if (!value)
819 fprintf(stderr, "ERROR: Missing value on line %d of %s!\n", linenum,
820 filename);
88f9aafc 821 else if (!_cups_strcasecmp(line, "Address"))
89d46774 822 {
823 if (!address)
824 add_array(Addresses, value);
825 }
88f9aafc 826 else if (!_cups_strcasecmp(line, "Community"))
89d46774 827 add_array(Communities, value);
88f9aafc 828 else if (!_cups_strcasecmp(line, "DebugLevel"))
89d46774 829 DebugLevel = atoi(value);
88f9aafc 830 else if (!_cups_strcasecmp(line, "DeviceURI"))
b94498cf 831 {
832 if (*value != '\"')
833 fprintf(stderr,
834 "ERROR: Missing double quote for regular expression on "
835 "line %d of %s!\n", linenum, filename);
836 else
837 add_device_uri(value);
838 }
88f9aafc
MS
839 else if (!_cups_strcasecmp(line, "HostNameLookups"))
840 HostNameLookups = !_cups_strcasecmp(value, "on") ||
841 !_cups_strcasecmp(value, "yes") ||
842 !_cups_strcasecmp(value, "true") ||
843 !_cups_strcasecmp(value, "double");
844 else if (!_cups_strcasecmp(line, "MaxRunTime"))
d09495fa 845 MaxRunTime = atoi(value);
89d46774 846 else
847 fprintf(stderr, "ERROR: Unknown directive %s on line %d of %s!\n",
848 line, linenum, filename);
849 }
850
851 cupsFileClose(fp);
852 }
853
854 /*
855 * Use defaults if parameters are undefined...
856 */
857
858 if (cupsArrayCount(Addresses) == 0)
859 {
bc44d920 860 /*
861 * If we have no addresses, exit immediately...
862 */
863
864 fprintf(stderr,
865 "DEBUG: No address specified and no Address line in %s...\n",
866 filename);
867 exit(0);
89d46774 868 }
869
870 if (cupsArrayCount(Communities) == 0)
871 {
872 fputs("INFO: Using default SNMP Community public\n", stderr);
873 add_array(Communities, "public");
874 }
875}
876
877
878/*
879 * 'read_snmp_response()' - Read and parse a SNMP response...
880 */
881
882static void
883read_snmp_response(int fd) /* I - SNMP socket file descriptor */
884{
89d46774 885 char addrname[256]; /* Source address name */
91c84a35 886 cups_snmp_t packet; /* Decoded packet */
89d46774 887 snmp_cache_t key, /* Search key */
888 *device; /* Matching device */
889
890
891 /*
892 * Read the response data...
893 */
894
7a14d768 895 if (!_cupsSNMPRead(fd, &packet, -1.0))
89d46774 896 {
897 fprintf(stderr, "ERROR: Unable to read data from socket: %s\n",
898 strerror(errno));
899 return;
900 }
901
902 if (HostNameLookups)
91c84a35 903 httpAddrLookup(&(packet.address), addrname, sizeof(addrname));
89d46774 904 else
91c84a35 905 httpAddrString(&(packet.address), addrname, sizeof(addrname));
89d46774 906
91c84a35 907 debug_printf("DEBUG: %.3f Received data from %s...\n", run_time(), addrname);
89d46774 908
909 /*
910 * Look for the response status code in the SNMP message header...
911 */
912
91c84a35 913 if (packet.error)
89d46774 914 {
91c84a35
MS
915 fprintf(stderr, "ERROR: Bad SNMP packet from %s: %s\n", addrname,
916 packet.error);
89d46774 917
918 return;
919 }
920
921 debug_printf("DEBUG: community=\"%s\"\n", packet.community);
922 debug_printf("DEBUG: request-id=%d\n", packet.request_id);
923 debug_printf("DEBUG: error-status=%d\n", packet.error_status);
924
771bd8cb 925 if (packet.error_status && packet.request_id != DEVICE_TYPE)
89d46774 926 return;
927
928 /*
929 * Find a matching device in the cache...
930 */
931
8ca02f3c 932 key.addrname = addrname;
933 device = (snmp_cache_t *)cupsArrayFind(Devices, &key);
89d46774 934
935 /*
936 * Process the message...
937 */
938
749b1e90 939 switch (packet.request_id)
89d46774 940 {
749b1e90
MS
941 case DEVICE_TYPE :
942 /*
943 * Got the device type response...
944 */
945
946 if (device)
947 {
948 debug_printf("DEBUG: Discarding duplicate device type for \"%s\"...\n",
949 addrname);
950 return;
951 }
952
953 /*
954 * Add the device and request the device data...
955 */
956
957 add_cache(&(packet.address), addrname, NULL, NULL, NULL);
958
959 _cupsSNMPWrite(fd, &(packet.address), CUPS_SNMP_VERSION_1,
960 packet.community, CUPS_ASN1_GET_REQUEST,
961 DEVICE_DESCRIPTION, DescriptionOID);
962 _cupsSNMPWrite(fd, &(packet.address), CUPS_SNMP_VERSION_1,
963 packet.community, CUPS_ASN1_GET_REQUEST,
964 DEVICE_ID, DeviceIdOID);
965 _cupsSNMPWrite(fd, &(packet.address), CUPS_SNMP_VERSION_1,
966 packet.community, CUPS_ASN1_GET_REQUEST,
967 DEVICE_URI, UriOID);
968 _cupsSNMPWrite(fd, &(packet.address), CUPS_SNMP_VERSION_1,
969 packet.community, CUPS_ASN1_GET_REQUEST,
970 DEVICE_LOCATION, LocationOID);
971 _cupsSNMPWrite(fd, &(packet.address), CUPS_SNMP_VERSION_1,
972 packet.community, CUPS_ASN1_GET_REQUEST,
973 DEVICE_PRODUCT, LexmarkProductOID);
974 _cupsSNMPWrite(fd, &(packet.address), CUPS_SNMP_VERSION_1,
975 packet.community, CUPS_ASN1_GET_REQUEST,
976 DEVICE_PRODUCT, LexmarkProductOID2);
977 _cupsSNMPWrite(fd, &(packet.address), CUPS_SNMP_VERSION_1,
978 packet.community, CUPS_ASN1_GET_REQUEST,
ef55b745 979 DEVICE_ID, LexmarkDeviceIdOID);
749b1e90
MS
980 _cupsSNMPWrite(fd, &(packet.address), CUPS_SNMP_VERSION_1,
981 packet.community, CUPS_ASN1_GET_REQUEST,
982 DEVICE_PRODUCT, XeroxProductOID);
983 break;
984
985 case DEVICE_DESCRIPTION :
986 if (device && packet.object_type == CUPS_ASN1_OCTET_STRING)
987 {
988 /*
989 * Update an existing cache entry...
990 */
991
992 char make_model[256]; /* Make and model */
993
994
d1c13e16
MS
995 if (strchr((char *)packet.object_value.string.bytes, ':') &&
996 strchr((char *)packet.object_value.string.bytes, ';'))
749b1e90
MS
997 {
998 /*
999 * Description is the IEEE-1284 device ID...
1000 */
89d46774 1001
3dd9c340
MS
1002 char *ptr; /* Pointer into device ID */
1003
1004 for (ptr = (char *)packet.object_value.string.bytes; *ptr; ptr ++)
1005 if (*ptr == '\n')
1006 *ptr = ';'; /* A lot of bad printers put a newline */
749b1e90 1007 if (!device->id)
d1c13e16 1008 device->id = strdup((char *)packet.object_value.string.bytes);
89d46774 1009
d1c13e16
MS
1010 backendGetMakeModel((char *)packet.object_value.string.bytes,
1011 make_model, sizeof(make_model));
89d46774 1012
749b1e90
MS
1013 if (device->info)
1014 free(device->info);
89d46774 1015
749b1e90
MS
1016 device->info = strdup(make_model);
1017 }
1018 else
1019 {
1020 /*
1021 * Description is plain text...
1022 */
89d46774 1023
d1c13e16 1024 fix_make_model(make_model, (char *)packet.object_value.string.bytes,
749b1e90 1025 sizeof(make_model));
89d46774 1026
749b1e90
MS
1027 if (device->info)
1028 free(device->info);
89d46774 1029
d1c13e16 1030 device->info = strdup((char *)packet.object_value.string.bytes);
749b1e90 1031 }
89d46774 1032
749b1e90
MS
1033 if (!device->make_and_model)
1034 device->make_and_model = strdup(make_model);
1035 }
1036 break;
89d46774 1037
749b1e90 1038 case DEVICE_ID :
ef55b745
MS
1039 if (device && packet.object_type == CUPS_ASN1_OCTET_STRING &&
1040 (!device->id ||
1041 strlen(device->id) < packet.object_value.string.num_bytes))
749b1e90
MS
1042 {
1043 /*
1044 * Update an existing cache entry...
1045 */
7a14d768 1046
749b1e90 1047 char make_model[256]; /* Make and model */
3dd9c340 1048 char *ptr; /* Pointer into device ID */
89d46774 1049
3dd9c340
MS
1050 for (ptr = (char *)packet.object_value.string.bytes; *ptr; ptr ++)
1051 if (*ptr == '\n')
1052 *ptr = ';'; /* A lot of bad printers put a newline */
749b1e90
MS
1053 if (device->id)
1054 free(device->id);
89d46774 1055
d1c13e16 1056 device->id = strdup((char *)packet.object_value.string.bytes);
7a14d768 1057
749b1e90
MS
1058 /*
1059 * Convert the ID to a make and model string...
1060 */
7a14d768 1061
d1c13e16
MS
1062 backendGetMakeModel((char *)packet.object_value.string.bytes,
1063 make_model, sizeof(make_model));
749b1e90
MS
1064 if (device->make_and_model)
1065 free(device->make_and_model);
7a14d768 1066
749b1e90
MS
1067 device->make_and_model = strdup(make_model);
1068 }
1069 break;
7a14d768 1070
749b1e90
MS
1071 case DEVICE_LOCATION :
1072 if (device && packet.object_type == CUPS_ASN1_OCTET_STRING &&
1073 !device->location)
d1c13e16 1074 device->location = strdup((char *)packet.object_value.string.bytes);
749b1e90 1075 break;
7a14d768 1076
749b1e90
MS
1077 case DEVICE_PRODUCT :
1078 if (device && packet.object_type == CUPS_ASN1_OCTET_STRING &&
1079 !device->id)
1080 {
1081 /*
1082 * Update an existing cache entry...
1083 */
7a14d768 1084
749b1e90 1085 if (!device->info)
d1c13e16 1086 device->info = strdup((char *)packet.object_value.string.bytes);
7a14d768 1087
749b1e90
MS
1088 if (device->make_and_model)
1089 free(device->make_and_model);
7a14d768 1090
d1c13e16 1091 device->make_and_model = strdup((char *)packet.object_value.string.bytes);
749b1e90
MS
1092 }
1093 break;
7a14d768 1094
749b1e90
MS
1095 case DEVICE_URI :
1096 if (device && packet.object_type == CUPS_ASN1_OCTET_STRING &&
12f89d24 1097 !device->uri && packet.object_value.string.num_bytes > 3)
749b1e90
MS
1098 {
1099 /*
1100 * Update an existing cache entry...
1101 */
7a14d768 1102
12f89d24
MS
1103 char scheme[32], /* URI scheme */
1104 userpass[256], /* Username:password in URI */
1105 hostname[256], /* Hostname in URI */
1106 resource[1024]; /* Resource path in URI */
1107 int port; /* Port number in URI */
1108
d1c13e16 1109 if (!strncmp((char *)packet.object_value.string.bytes, "lpr:", 4))
749b1e90
MS
1110 {
1111 /*
1112 * We want "lpd://..." for the URI...
1113 */
7a14d768 1114
d1c13e16 1115 packet.object_value.string.bytes[2] = 'd';
749b1e90 1116 }
7a14d768 1117
12f89d24
MS
1118 if (httpSeparateURI(HTTP_URI_CODING_ALL,
1119 (char *)packet.object_value.string.bytes,
1120 scheme, sizeof(scheme),
1121 userpass, sizeof(userpass),
1122 hostname, sizeof(hostname), &port,
1123 resource, sizeof(resource)) >= HTTP_URI_OK)
1124 device->uri = strdup((char *)packet.object_value.string.bytes);
749b1e90
MS
1125 }
1126 break;
89d46774 1127 }
1128}
1129
1130
1131/*
1132 * 'run_time()' - Return the total running time...
1133 */
1134
1135static double /* O - Number of seconds */
1136run_time(void)
1137{
1138 struct timeval curtime; /* Current time */
1139
1140
1141 gettimeofday(&curtime, NULL);
1142
1143 return (curtime.tv_sec - StartTime.tv_sec +
1144 0.000001 * (curtime.tv_usec - StartTime.tv_usec));
1145}
1146
1147
1148/*
1149 * 'scan_devices()' - Scan for devices using SNMP.
1150 */
1151
1152static void
22c9029b
MS
1153scan_devices(int ipv4, /* I - SNMP IPv4 socket */
1154 int ipv6) /* I - SNMP IPv6 socket */
89d46774 1155{
22c9029b
MS
1156 int fd, /* File descriptor for this address */
1157 busy; /* Are we busy processing something? */
89d46774 1158 char *address, /* Current address */
1159 *community; /* Current community */
1160 fd_set input; /* Input set for select() */
1161 struct timeval timeout; /* Timeout for select() */
1162 time_t endtime; /* End time for scan */
1163 http_addrlist_t *addrs, /* List of addresses */
1164 *addr; /* Current address */
1165 snmp_cache_t *device; /* Current device */
22c9029b 1166 char temp[1024]; /* Temporary address string */
89d46774 1167
1168
89d46774 1169 gettimeofday(&StartTime, NULL);
1170
89d46774 1171 /*
1172 * First send all of the broadcast queries...
1173 */
1174
1175 for (address = (char *)cupsArrayFirst(Addresses);
1176 address;
1177 address = (char *)cupsArrayNext(Addresses))
1178 {
1179 if (!strcmp(address, "@LOCAL"))
1180 addrs = get_interface_addresses(NULL);
1181 else if (!strncmp(address, "@IF(", 4))
1182 {
1183 char ifname[255]; /* Interface name */
1184
89d46774 1185 strlcpy(ifname, address + 4, sizeof(ifname));
1186 if (ifname[0])
1187 ifname[strlen(ifname) - 1] = '\0';
1188
1189 addrs = get_interface_addresses(ifname);
1190 }
1191 else
22c9029b 1192 addrs = httpAddrGetList(address, AF_UNSPEC, NULL);
89d46774 1193
1194 if (!addrs)
1195 {
1196 fprintf(stderr, "ERROR: Unable to scan \"%s\"!\n", address);
1197 continue;
1198 }
1199
1200 for (community = (char *)cupsArrayFirst(Communities);
1201 community;
1202 community = (char *)cupsArrayNext(Communities))
1203 {
1204 debug_printf("DEBUG: Scanning for devices in \"%s\" via \"%s\"...\n",
1205 community, address);
1206
1207 for (addr = addrs; addr; addr = addr->next)
22c9029b
MS
1208 {
1209#ifdef AF_INET6
5ec1fd3d 1210 if (httpAddrFamily(&(addr->addr)) == AF_INET6)
22c9029b
MS
1211 fd = ipv6;
1212 else
1213#endif /* AF_INET6 */
1214 fd = ipv4;
1215
1216 debug_printf("DEBUG: Sending get request to %s...\n",
1217 httpAddrString(&(addr->addr), temp, sizeof(temp)));
1218
7a14d768 1219 _cupsSNMPWrite(fd, &(addr->addr), CUPS_SNMP_VERSION_1, community,
749b1e90 1220 CUPS_ASN1_GET_REQUEST, DEVICE_TYPE, DeviceTypeOID);
22c9029b 1221 }
89d46774 1222 }
1223
1224 httpAddrFreeList(addrs);
1225 }
1226
1227 /*
1228 * Then read any responses that come in over the next 3 seconds...
1229 */
1230
ae71f5de 1231 endtime = time(NULL) + MaxRunTime;
89d46774 1232
1233 FD_ZERO(&input);
1234
1235 while (time(NULL) < endtime)
1236 {
ae71f5de 1237 timeout.tv_sec = 2;
89d46774 1238 timeout.tv_usec = 0;
1239
22c9029b
MS
1240 FD_SET(ipv4, &input);
1241 if (ipv6 >= 0)
1242 FD_SET(ipv6, &input);
1243
1244 fd = ipv4 > ipv6 ? ipv4 : ipv6;
89d46774 1245 if (select(fd + 1, &input, NULL, NULL, &timeout) < 0)
1246 {
22c9029b
MS
1247 fprintf(stderr, "ERROR: %.3f select() for %d/%d failed: %s\n", run_time(),
1248 ipv4, ipv6, strerror(errno));
89d46774 1249 break;
1250 }
1251
22c9029b
MS
1252 busy = 0;
1253
1254 if (FD_ISSET(ipv4, &input))
1255 {
1256 read_snmp_response(ipv4);
1257 busy = 1;
1258 }
1259
1260 if (ipv6 >= 0 && FD_ISSET(ipv6, &input))
1261 {
1262 read_snmp_response(ipv6);
1263 busy = 1;
1264 }
1265
1266 if (!busy)
749b1e90
MS
1267 {
1268 /*
1269 * List devices with complete information...
1270 */
89d46774 1271
749b1e90 1272 int sent_something = 0;
89d46774 1273
749b1e90
MS
1274 for (device = (snmp_cache_t *)cupsArrayFirst(Devices);
1275 device;
1276 device = (snmp_cache_t *)cupsArrayNext(Devices))
1277 if (!device->sent && device->info && device->make_and_model)
1278 {
1279 if (device->uri)
1280 list_device(device);
1281 else
1282 probe_device(device);
1283
1284 device->sent = sent_something = 1;
1285 }
1286
1287 if (!sent_something)
1288 break;
1289 }
1290 }
89d46774 1291
1292 debug_printf("DEBUG: %.3f Scan complete!\n", run_time());
1293}
1294
1295
89d46774 1296/*
1297 * 'try_connect()' - Try connecting on a port...
1298 */
1299
1300static int /* O - 0 on success or -1 on error */
1301try_connect(http_addr_t *addr, /* I - Socket address */
1302 const char *addrname, /* I - Hostname or IP address */
1303 int port) /* I - Port number */
1304{
1305 int fd; /* Socket */
1306 int status; /* Connection status */
1307
1308
1309 debug_printf("DEBUG: %.3f Trying %s://%s:%d...\n", run_time(),
1310 port == 515 ? "lpd" : "socket", addrname, port);
1311
5ec1fd3d 1312 if ((fd = socket(httpAddrFamily(addr), SOCK_STREAM, 0)) < 0)
89d46774 1313 {
c0e1af83 1314 fprintf(stderr, "ERROR: Unable to create socket: %s\n",
1315 strerror(errno));
89d46774 1316 return (-1);
1317 }
1318
22c9029b 1319 _httpAddrSetPort(addr, port);
89d46774 1320
89d46774 1321 alarm(1);
1322
7e86f2f6 1323 status = connect(fd, (void *)addr, (socklen_t)httpAddrLength(addr));
89d46774 1324
1325 close(fd);
1326 alarm(0);
1327
1328 return (status);
1329}
1330
1331
1332/*
1333 * 'update_cache()' - Update a cached device...
1334 */
1335
1336static void
1337update_cache(snmp_cache_t *device, /* I - Device */
1338 const char *uri, /* I - Device URI */
1339 const char *id, /* I - Device ID */
1340 const char *make_model) /* I - Device make and model */
1341{
1342 if (device->uri)
1343 free(device->uri);
1344
1345 device->uri = strdup(uri);
1346
1347 if (id)
1348 {
1349 if (device->id)
1350 free(device->id);
1351
1352 device->id = strdup(id);
1353 }
1354
1355 if (make_model)
1356 {
1357 if (device->make_and_model)
1358 free(device->make_and_model);
1359
1360 device->make_and_model = strdup(make_model);
1361 }
d09495fa 1362
1363 list_device(device);
89d46774 1364}
1365
1366
1367/*
f2d18633 1368 * End of "$Id$".
89d46774 1369 */