1N/A/*
1N/A * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
1N/A * Use is subject to license terms.
1N/A *
1N/A * Licensed under the Academic Free License version 2.1
1N/A */
1N/A
1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A#include <sys/types.h>
1N/A#include <sys/socket.h>
1N/A#include <netinet/in.h>
1N/A#include <arpa/inet.h>
1N/A#include <strings.h>
1N/A
1N/A#undef PACKAGE_STRING
1N/A#undef PACKAGE_VERSION
1N/A
1N/A#include <net-snmp/net-snmp-config.h>
1N/A#include <net-snmp/net-snmp-includes.h>
1N/A
1N/A#include "logger.h"
1N/A#include "printer.h"
1N/A
1N/Astatic int
1N/AhrDeviceDesc_to_info(char *string, char **manufacturer, char **model,
1N/A char **description)
1N/A{
1N/A int rc = -1;
1N/A char *s;
1N/A
1N/A if (string == NULL)
1N/A return (-1);
1N/A
1N/A /* if it has : and ; in it, it's probably a 1284 device id */
1N/A if ((strchr(string, ':') != NULL) && (strchr(string, ';') != NULL)) {
1N/A rc = ieee1284_devid_to_printer_info(string, manufacturer, model,
1N/A description, NULL, NULL, NULL);
1N/A } else {
1N/A rc = 0;
1N/A *description = strdup(string);
1N/A *manufacturer = strdup(string);
1N/A if ((s = strchr(*manufacturer, ' ')) != NULL) {
1N/A *s++ = NULL;
1N/A *model = strdup(s);
1N/A }
1N/A }
1N/A
1N/A return (rc);
1N/A}
1N/A
1N/Astatic struct snmp_pdu *
1N/Asnmp_get_item(char *host, char *community, char *mib_item)
1N/A{
1N/A int status;
1N/A struct snmp_session session, *ss;
1N/A struct snmp_pdu *request = NULL, *result = NULL;
1N/A oid Oid[MAX_OID_LEN];
1N/A unsigned int oid_len = MAX_OID_LEN;
1N/A
1N/A /* initialize the SNMP session */
1N/A snmp_sess_init(&session);
1N/A session.peername = host;
1N/A session.community = (uchar_t *)community;
1N/A session.community_len = strlen((const char *)session.community);
1N/A session.version = SNMP_VERSION_1;
1N/A session.retries = 0;
1N/A
1N/A if ((ss = snmp_open(&session)) == NULL)
1N/A return (NULL);
1N/A
1N/A /* add the requested data */
1N/A if (!read_objid(mib_item, Oid, &oid_len))
1N/A snmp_perror(mib_item);
1N/A
1N/A /* initialize the request PDU */
1N/A request = snmp_pdu_create(SNMP_MSG_GET);
1N/A snmp_add_null_var(request, Oid, oid_len);
1N/A
1N/A status = snmp_synch_response(ss, request, &result);
1N/A
1N/A snmp_close(ss);
1N/A
1N/A return (result);
1N/A}
1N/A
1N/Astatic char *
1N/Asnmp_get_string(char *host, char *community, char *mib_item)
1N/A{
1N/A char *result = NULL;
1N/A struct snmp_pdu *response = NULL;
1N/A
1N/A response = snmp_get_item(host, community, mib_item);
1N/A
1N/A if ((response != NULL) && (response->errstat == SNMP_ERR_NOERROR)) {
1N/A struct variable_list *v = response->variables;
1N/A
1N/A if (v->type == ASN_OCTET_STR) {
1N/A result = calloc(1, v->val_len + 1);
1N/A memcpy(result, v->val.string, v->val_len);
1N/A }
1N/A }
1N/A
1N/A HAL_DEBUG(("snmp_get_string(%s, %s, %s): %s", host, community, mib_item,
1N/A (result?result:"NULL")));
1N/A
1N/A if (response != NULL)
1N/A snmp_free_pdu(response);
1N/A
1N/A return (result);
1N/A}
1N/A
1N/Astatic int
1N/Asnmp_brother_printer_info(char *hostname, char *community, char **manufacturer,
1N/A char **model, char **description, char **serial_no,
1N/A char ***command_set)
1N/A{
1N/A int rc = -1;
1N/A char *tmp = NULL;
1N/A
1N/A /*
1N/A * Brother printers appear to store
1N/A * 1284 DevID SNMPv2-SMI::enterprises.2435.2.3.9.1.1.7.0
1N/A * Serial Number SNMPv2-SMI::enterprises.2435.2.3.9.4.2.1.5.5.1.0
1N/A */
1N/A tmp = snmp_get_string(hostname, community,
1N/A "SNMPv2-SMI::enterprises.2435.2.3.9.1.1.7.0");
1N/A if (tmp != NULL) {
1N/A rc = ieee1284_devid_to_printer_info(tmp, manufacturer, model,
1N/A description, NULL, serial_no, command_set);
1N/A free(tmp);
1N/A
1N/A if (*serial_no == NULL)
1N/A *serial_no = snmp_get_string(hostname, community,
1N/A "SNMPv2-SMI::enterprises.2435.2.3.9.4.2.1.5.5.1.0");
1N/A }
1N/A
1N/A return (rc);
1N/A}
1N/A
1N/Astatic int
1N/Asnmp_ricoh_printer_info(char *hostname, char *community, char **manufacturer,
1N/A char **model, char **description, char **serial_no,
1N/A char ***command_set)
1N/A{
1N/A int rc = -1;
1N/A char *tmp = NULL;
1N/A
1N/A /*
1N/A * OKI printers appear to store
1N/A * 1284 DevID SNMPv2-SMI::enterprises.367.3.2.1.1.1.11.0
1N/A * Serial Number SNMPv2-SMI::enterprises.367.3.2.1.2.1.4.0
1N/A */
1N/A tmp = snmp_get_string(hostname, community,
1N/A "SNMPv2-SMI::enterprises.367.3.2.1.1.1.11.0");
1N/A if (tmp != NULL) {
1N/A rc = ieee1284_devid_to_printer_info(tmp, manufacturer, model,
1N/A description, NULL, serial_no, command_set);
1N/A free(tmp);
1N/A
1N/A if (*serial_no == NULL)
1N/A *serial_no = snmp_get_string(hostname, community,
1N/A "SNMPv2-SMI::enterprises.367.3.2.1.2.1.4.0");
1N/A }
1N/A
1N/A return (rc);
1N/A}
1N/A
1N/Astatic int
1N/Asnmp_lexmark_printer_info(char *hostname, char *community, char **manufacturer,
1N/A char **model, char **description, char **serial_no,
1N/A char ***command_set)
1N/A{
1N/A int rc = -1;
1N/A char *tmp = NULL;
1N/A
1N/A /*
1N/A * Lexmark printers appear to store
1N/A * 1284 DevID SNMPv2-SMI::enterprises.641.2.1.2.1.3.1
1N/A * Serial Number SNMPv2-SMI::enterprises.641.2.1.2.1.6.1
1N/A */
1N/A tmp = snmp_get_string(hostname, community,
1N/A "SNMPv2-SMI::enterprises.641.2.1.2.1.3.1");
1N/A if (tmp != NULL) {
1N/A rc = ieee1284_devid_to_printer_info(tmp, manufacturer, model,
1N/A description, NULL, serial_no, command_set);
1N/A free(tmp);
1N/A
1N/A if (*serial_no == NULL)
1N/A *serial_no = snmp_get_string(hostname, community,
1N/A "SNMPv2-SMI::enterprises.641.2.1.2.1.6.1");
1N/A }
1N/A
1N/A return (rc);
1N/A}
1N/A
1N/Astatic int
1N/Asnmp_xerox_phaser_printer_info(char *hostname, char *community,
1N/A char **manufacturer, char **model, char **description,
1N/A char **serial_no, char ***command_set, char **uri)
1N/A{
1N/A int rc = -1;
1N/A char *tmp = NULL;
1N/A
1N/A /*
1N/A * Xerox Phaser XXXX printers store their
1N/A * 1284 DevID SNMPv2-SMI::enterprises.253.8.51.1.2.1.20.1
1N/A * Manufacturer:
1N/A * SNMPv2-SMI::enterprises.128.2.1.3.1.1.0
1N/A * SNMPv2-SMI::enterprises.23.2.32.3.2.1.10.1.16
1N/A * SNMPv2-SMI::enterprises.23.2.32.4.1.0
1N/A * Model:
1N/A * SNMPv2-SMI::enterprises.128.2.1.3.1.2.0
1N/A * SNMPv2-SMI::enterprises.23.2.32.3.2.1.10.1.17
1N/A * SNMPv2-SMI::enterprises.23.2.32.4.2.0
1N/A * Description SNMPv2-SMI::enterprises.253.8.53.3.2.1.2.1
1N/A * Serial Number SNMPv2-SMI::enterprises.253.8.53.3.2.1.3.1
1N/A * Uri SNMPv2-SMI::enterprises.128.2.1.3.6.23.1.5.1
1N/A */
1N/A
1N/A tmp = snmp_get_string(hostname, community,
1N/A "SNMPv2-SMI::enterprises.253.8.51.1.2.1.20.1");
1N/A if (tmp != NULL) {
1N/A rc = ieee1284_devid_to_printer_info(tmp, manufacturer, model,
1N/A description, NULL, serial_no, command_set);
1N/A free(tmp);
1N/A }
1N/A
1N/A if (*manufacturer == NULL)
1N/A *manufacturer = snmp_get_string(hostname, community,
1N/A "SNMPv2-SMI::enterprises.128.2.1.3.1.1.0");
1N/A if (*manufacturer == NULL)
1N/A *manufacturer = snmp_get_string(hostname, community,
1N/A "SNMPv2-SMI::enterprises.23.2.32.3.2.1.10.1.16");
1N/A if (*manufacturer == NULL)
1N/A *manufacturer = snmp_get_string(hostname, community,
1N/A "SNMPv2-SMI::enterprises.23.2.32.4.1.0");
1N/A
1N/A if (*model == NULL)
1N/A *model = snmp_get_string(hostname, community,
1N/A "SNMPv2-SMI::enterprises.128.2.1.3.1.2.0");
1N/A if (*model == NULL)
1N/A *model = snmp_get_string(hostname, community,
1N/A "SNMPv2-SMI::enterprises.23.2.32.3.2.1.10.1.17");
1N/A if (*model == NULL)
1N/A *model = snmp_get_string(hostname, community,
1N/A "SNMPv2-SMI::enterprises.23.2.32.4.2.0");
1N/A
1N/A if (*serial_no == NULL)
1N/A *serial_no = snmp_get_string(hostname, community,
1N/A "SNMPv2-SMI::enterprises.253.8.53.3.2.1.3.1");
1N/A
1N/A if ((*manufacturer != NULL) && (*model != NULL))
1N/A rc = 0;
1N/A
1N/A return (rc);
1N/A}
1N/A
1N/Astatic int
1N/Asnmp_qms_printer_info(char *hostname, char *community, char **manufacturer,
1N/A char **model, char **description, char **serial_no,
1N/A char ***command_set, char **uri)
1N/A{
1N/A int rc = -1;
1N/A char *tmp = NULL;
1N/A
1N/A /*
1N/A * MINOLTA-QMS printers appear to store
1N/A * Prouct Name SNMPv2-SMI::enterprises.2590.1.1.2.1.5.7.14.2.1.1.16.1
1N/A * Serial Number SNMPv2-SMI::enterprises.2590.1.1.1.5.5.1.1.3.2
1N/A * URI SNMPv2-SMI::enterprises.2590.1.1.2.1.5.7.14.2.2.1.3.1.1
1N/A * SNMPv2-SMI::enterprises.2590.1.1.2.1.5.7.14.2.2.1.3.1.2
1N/A */
1N/A tmp = snmp_get_string(hostname, community,
1N/A "SNMPv2-SMI::enterprises.2590.1.1.2.1.5.7.14.2.1.1.16.1");
1N/A if (tmp != NULL) {
1N/A rc = hrDeviceDesc_to_info(tmp, manufacturer, model,
1N/A description);
1N/A free(tmp);
1N/A
1N/A if (*serial_no == NULL)
1N/A *serial_no = snmp_get_string(hostname, community,
1N/A "SNMPv2-SMI::enterprises.2590.1.1.1.5.5.1.1.3.2");
1N/A tmp = snmp_get_string(hostname, community,
1N/A "SNMPv2-SMI::enterprises.2590.1.1.2.1.5.7.14.2.2.1.3.1.2");
1N/A if (tmp == NULL)
1N/A tmp = snmp_get_string(hostname, community,
1N/A "SNMPv2-SMI::enterprises.2590.1.1.2.1.5.7.14.2.2.1.3.1.1");
1N/A if (tmp != NULL)
1N/A *uri = tmp;
1N/A }
1N/A
1N/A return (rc);
1N/A}
1N/A
1N/Astatic int
1N/Asnmp_oki_printer_info(char *hostname, char *community, char **manufacturer,
1N/A char **model, char **description, char **serial_no,
1N/A char ***command_set)
1N/A{
1N/A int rc = -1;
1N/A char *tmp = NULL;
1N/A
1N/A /*
1N/A * OKI printers appear to store
1N/A * Prouct Name SNMPv2-SMI::enterprises.2001.1.2.683.1.3
1N/A * Serial Number SNMPv2-SMI::enterprises.2001.1.2.683.1.5
1N/A */
1N/A tmp = snmp_get_string(hostname, community,
1N/A "SNMPv2-SMI::enterprises.2001.1.2.683.1.3");
1N/A if (tmp != NULL) {
1N/A rc = ieee1284_devid_to_printer_info(tmp, manufacturer, model,
1N/A description, NULL, serial_no, command_set);
1N/A free(tmp);
1N/A
1N/A if (*serial_no == NULL)
1N/A *serial_no = snmp_get_string(hostname, community,
1N/A "SNMPv2-SMI::enterprises.2001.1.2.683.1.5");
1N/A }
1N/A
1N/A return (rc);
1N/A}
1N/A
1N/Astatic int
1N/Asnmp_hp_printer_info(char *hostname, char *community, char **manufacturer,
1N/A char **model, char **description, char **serial_no,
1N/A char ***command_set)
1N/A{
1N/A int rc = -1;
1N/A char *tmp = NULL;
1N/A
1N/A /*
1N/A * HP printers appear to store
1N/A * 1284 DevID SNMPv2-SMI::enterprises.11.2.3.9.1.1.7.0
1N/A * Serial Number SNMPv2-SMI::enterprises.2.3.9.4.2.2.5.1.1.17
1N/A */
1N/A tmp = snmp_get_string(hostname, community,
1N/A "SNMPv2-SMI::enterprises.11.2.3.9.1.1.7.0");
1N/A if (tmp != NULL) {
1N/A rc = ieee1284_devid_to_printer_info(tmp, manufacturer, model,
1N/A description, NULL, serial_no, command_set);
1N/A free(tmp);
1N/A
1N/A if (*serial_no == NULL)
1N/A *serial_no = snmp_get_string(hostname, community,
1N/A "SNMPv2-SMI::enterprises.2.3.9.4.2.2.5.1.1.17");
1N/A }
1N/A
1N/A return (rc);
1N/A}
1N/A
1N/Astatic int
1N/Asnmp_ppm_printer_info(char *hostname, char *community, char **manufacturer,
1N/A char **model, char **description, char **serial_no,
1N/A char ***command_set)
1N/A{
1N/A int rc = -1;
1N/A char *tmp = NULL;
1N/A
1N/A /*
1N/A * The PWG portMon MIB stores
1N/A * 1284 DevID SNMPv2-SMI::enterprises.2699.1.2.1.1.1.3`
1N/A */
1N/A tmp = snmp_get_string(hostname, community,
1N/A "SNMPv2-SMI::enterprises.2699.1.2.1.1.1.3");
1N/A if (tmp != NULL) {
1N/A rc = ieee1284_devid_to_printer_info(tmp, manufacturer, model,
1N/A description, NULL, serial_no, command_set);
1N/A free(tmp);
1N/A }
1N/A
1N/A return (rc);
1N/A}
1N/A
1N/Astatic int
1N/Asnmp_prt_printer_info(char *hostname, char *community, char **manufacturer,
1N/A char **model, char **description, char **serial_no,
1N/A char ***command_set)
1N/A{
1N/A int rc = -1;
1N/A char *tmp = NULL;
1N/A
1N/A /*
1N/A * The Printer Printer MIB stores
1N/A * Vendor SNMPv2-SMI::mib-2.43.8.2.1.14.1.1
1N/A * Model SNMPv2-SMI::mib-2.43.8.2.1.15.1.1
1N/A * Serial SNMPv2-SMI::mib-2.43.8.2.1.17.1.1
1N/A */
1N/A
1N/A if (*manufacturer == NULL)
1N/A *manufacturer = snmp_get_string(hostname, community,
1N/A "SNMPv2-SMI::mib-2.43.8.2.1.14.1.1");
1N/A if (*model == NULL)
1N/A *model = snmp_get_string(hostname, community,
1N/A "SNMPv2-SMI::mib-2.43.8.2.1.15.1.1");
1N/A if (*serial_no == NULL)
1N/A *serial_no = snmp_get_string(hostname, community,
1N/A "SNMPv2-SMI::mib-2.43.8.2.1.17.1.1");
1N/A
1N/A if (*manufacturer != NULL)
1N/A rc = 0;
1N/A
1N/A return (rc);
1N/A}
1N/A
1N/Astatic int
1N/Asnmp_host_resource_printer_info(char *hostname, char *community,
1N/A char **manufacturer, char **model, char **description,
1N/A char **serial_no, char ***command_set)
1N/A{
1N/A int rc = -1;
1N/A char *tmp = NULL;
1N/A
1N/A tmp = snmp_get_string(hostname, community,
1N/A "HOST-RESOURCES-MIB::hrDeviceDescr.1");
1N/A if (tmp != NULL) {
1N/A rc = hrDeviceDesc_to_info(tmp, manufacturer, model,
1N/A description);
1N/A free(tmp);
1N/A }
1N/A
1N/A return (rc);
1N/A}
1N/A
1N/Aint
1N/Asnmp_printer_info(char *hostname, char *community, char **manufacturer,
1N/A char **model, char **description, char **serial_no,
1N/A char ***command_set, char **uri)
1N/A{
1N/A char *tmp = NULL;
1N/A
1N/A init_snmp("network-printer-probe");
1N/A init_mib();
1N/A
1N/A if (snmp_brother_printer_info(hostname, community, manufacturer, model,
1N/A description, serial_no, command_set) == 0) {
1N/A return (0);
1N/A } else if (snmp_ricoh_printer_info(hostname, community, manufacturer,
1N/A model, description, serial_no, command_set) == 0) {
1N/A return (0);
1N/A } else if (snmp_lexmark_printer_info(hostname, community, manufacturer,
1N/A model, description, serial_no, command_set) == 0) {
1N/A return (0);
1N/A } else if (snmp_xerox_phaser_printer_info(hostname, community,
1N/A manufacturer, model, description, serial_no,
1N/A command_set, uri) == 0) {
1N/A return (0);
1N/A } else if (snmp_qms_printer_info(hostname, community, manufacturer,
1N/A model, description, serial_no, command_set, uri) == 0) {
1N/A return (0);
1N/A } else if (snmp_oki_printer_info(hostname, community, manufacturer,
1N/A model, description, serial_no, command_set) == 0) {
1N/A return (0);
1N/A } else if (snmp_hp_printer_info(hostname, community, manufacturer,
1N/A model, description, serial_no, command_set) == 0) {
1N/A return (0);
1N/A } else if (snmp_ppm_printer_info(hostname, community, manufacturer,
1N/A model, description, serial_no, command_set) == 0) {
1N/A return (0);
1N/A } else if (snmp_prt_printer_info(hostname, community, manufacturer,
1N/A model, description, serial_no, command_set) == 0) {
1N/A return (0);
1N/A } else if (snmp_host_resource_printer_info(hostname, community,
1N/A manufacturer, model, description, serial_no,
1N/A command_set) == 0) {
1N/A return (0);
1N/A }
1N/A
1N/A return (-1);
1N/A}
1N/A
1N/A#ifdef NOTDEF
1N/A
1N/A#define NP(x) (x?x:"")
1N/A
1N/Aint
1N/Amain(int ac, char *av[])
1N/A{
1N/A int i;
1N/A
1N/A for (i = 1; av[i] != NULL; i++) {
1N/A char *hostname = av[i], *manufacturer = NULL, *model = NULL,
1N/A *description = NULL, *serial_no = NULL,
1N/A **command_set = NULL, *uri = NULL;
1N/A int rc;
1N/A
1N/A rc = snmp_printer_info(hostname, &manufacturer, &model,
1N/A &description, &serial_no, &command_set, &uri);
1N/A printf("SNMP data for %s...(%d)\n", hostname, rc);
1N/A printf("\tvendor = %s\n", NP(manufacturer));
1N/A printf("\tproduct = %s\n", NP(model));
1N/A printf("\tdescription = %s\n", NP(description));
1N/A printf("\tserial = %s\n", NP(serial_no));
1N/A printf("\tdevice = %s\n", NP(uri));
1N/A
1N/A if (command_set != NULL) {
1N/A int j;
1N/A
1N/A printf("\tcommand set = \n");
1N/A for (j = 0; command_set[j] != NULL; j++)
1N/A printf("\t\t%s\n", command_set[j]);
1N/A }
1N/A }
1N/A
1N/A return (0);
1N/A}
1N/A#endif