dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / hal / probing / network-printer / probe-snmp.c
blob15aa6ee8b5fe853c76b44933fecb43e46c7560ff
1 /*
2 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
5 * Licensed under the Academic Free License version 2.1
6 */
8 #include <sys/types.h>
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <arpa/inet.h>
12 #include <strings.h>
14 #undef PACKAGE_STRING
15 #undef PACKAGE_VERSION
17 #include <net-snmp/net-snmp-config.h>
18 #include <net-snmp/net-snmp-includes.h>
20 #include "logger.h"
21 #include "printer.h"
23 static int
24 hrDeviceDesc_to_info(char *string, char **manufacturer, char **model,
25 char **description)
27 int rc = -1;
28 char *s;
30 if (string == NULL)
31 return (-1);
33 /* if it has : and ; in it, it's probably a 1284 device id */
34 if ((strchr(string, ':') != NULL) && (strchr(string, ';') != NULL)) {
35 rc = ieee1284_devid_to_printer_info(string, manufacturer, model,
36 description, NULL, NULL, NULL);
37 } else {
38 rc = 0;
39 *description = strdup(string);
40 *manufacturer = strdup(string);
41 if ((s = strchr(*manufacturer, ' ')) != NULL) {
42 *s++ = '\0';
43 *model = strdup(s);
47 return (rc);
50 static struct snmp_pdu *
51 snmp_get_item(char *host, char *community, char *mib_item)
53 int status;
54 struct snmp_session session, *ss;
55 struct snmp_pdu *request = NULL, *result = NULL;
56 oid Oid[MAX_OID_LEN];
57 unsigned int oid_len = MAX_OID_LEN;
59 /* initialize the SNMP session */
60 snmp_sess_init(&session);
61 session.peername = host;
62 session.community = (uchar_t *)community;
63 session.community_len = strlen((const char *)session.community);
64 session.version = SNMP_VERSION_1;
65 session.retries = 0;
67 if ((ss = snmp_open(&session)) == NULL)
68 return (NULL);
70 /* add the requested data */
71 if (!read_objid(mib_item, Oid, &oid_len))
72 snmp_perror(mib_item);
74 /* initialize the request PDU */
75 request = snmp_pdu_create(SNMP_MSG_GET);
76 snmp_add_null_var(request, Oid, oid_len);
78 status = snmp_synch_response(ss, request, &result);
80 snmp_close(ss);
82 return (result);
85 static char *
86 snmp_get_string(char *host, char *community, char *mib_item)
88 char *result = NULL;
89 struct snmp_pdu *response = NULL;
91 response = snmp_get_item(host, community, mib_item);
93 if ((response != NULL) && (response->errstat == SNMP_ERR_NOERROR)) {
94 struct variable_list *v = response->variables;
96 if (v->type == ASN_OCTET_STR) {
97 result = calloc(1, v->val_len + 1);
98 memcpy(result, v->val.string, v->val_len);
102 HAL_DEBUG(("snmp_get_string(%s, %s, %s): %s", host, community, mib_item,
103 (result?result:"NULL")));
105 if (response != NULL)
106 snmp_free_pdu(response);
108 return (result);
111 static int
112 snmp_brother_printer_info(char *hostname, char *community, char **manufacturer,
113 char **model, char **description, char **serial_no,
114 char ***command_set)
116 int rc = -1;
117 char *tmp = NULL;
120 * Brother printers appear to store
121 * 1284 DevID SNMPv2-SMI::enterprises.2435.2.3.9.1.1.7.0
122 * Serial Number SNMPv2-SMI::enterprises.2435.2.3.9.4.2.1.5.5.1.0
124 tmp = snmp_get_string(hostname, community,
125 "SNMPv2-SMI::enterprises.2435.2.3.9.1.1.7.0");
126 if (tmp != NULL) {
127 rc = ieee1284_devid_to_printer_info(tmp, manufacturer, model,
128 description, NULL, serial_no, command_set);
129 free(tmp);
131 if (*serial_no == NULL)
132 *serial_no = snmp_get_string(hostname, community,
133 "SNMPv2-SMI::enterprises.2435.2.3.9.4.2.1.5.5.1.0");
136 return (rc);
139 static int
140 snmp_ricoh_printer_info(char *hostname, char *community, char **manufacturer,
141 char **model, char **description, char **serial_no,
142 char ***command_set)
144 int rc = -1;
145 char *tmp = NULL;
148 * OKI printers appear to store
149 * 1284 DevID SNMPv2-SMI::enterprises.367.3.2.1.1.1.11.0
150 * Serial Number SNMPv2-SMI::enterprises.367.3.2.1.2.1.4.0
152 tmp = snmp_get_string(hostname, community,
153 "SNMPv2-SMI::enterprises.367.3.2.1.1.1.11.0");
154 if (tmp != NULL) {
155 rc = ieee1284_devid_to_printer_info(tmp, manufacturer, model,
156 description, NULL, serial_no, command_set);
157 free(tmp);
159 if (*serial_no == NULL)
160 *serial_no = snmp_get_string(hostname, community,
161 "SNMPv2-SMI::enterprises.367.3.2.1.2.1.4.0");
164 return (rc);
167 static int
168 snmp_lexmark_printer_info(char *hostname, char *community, char **manufacturer,
169 char **model, char **description, char **serial_no,
170 char ***command_set)
172 int rc = -1;
173 char *tmp = NULL;
176 * Lexmark printers appear to store
177 * 1284 DevID SNMPv2-SMI::enterprises.641.2.1.2.1.3.1
178 * Serial Number SNMPv2-SMI::enterprises.641.2.1.2.1.6.1
180 tmp = snmp_get_string(hostname, community,
181 "SNMPv2-SMI::enterprises.641.2.1.2.1.3.1");
182 if (tmp != NULL) {
183 rc = ieee1284_devid_to_printer_info(tmp, manufacturer, model,
184 description, NULL, serial_no, command_set);
185 free(tmp);
187 if (*serial_no == NULL)
188 *serial_no = snmp_get_string(hostname, community,
189 "SNMPv2-SMI::enterprises.641.2.1.2.1.6.1");
192 return (rc);
195 static int
196 snmp_xerox_phaser_printer_info(char *hostname, char *community,
197 char **manufacturer, char **model, char **description,
198 char **serial_no, char ***command_set, char **uri)
200 int rc = -1;
201 char *tmp = NULL;
204 * Xerox Phaser XXXX printers store their
205 * 1284 DevID SNMPv2-SMI::enterprises.253.8.51.1.2.1.20.1
206 * Manufacturer:
207 * SNMPv2-SMI::enterprises.128.2.1.3.1.1.0
208 * SNMPv2-SMI::enterprises.23.2.32.3.2.1.10.1.16
209 * SNMPv2-SMI::enterprises.23.2.32.4.1.0
210 * Model:
211 * SNMPv2-SMI::enterprises.128.2.1.3.1.2.0
212 * SNMPv2-SMI::enterprises.23.2.32.3.2.1.10.1.17
213 * SNMPv2-SMI::enterprises.23.2.32.4.2.0
214 * Description SNMPv2-SMI::enterprises.253.8.53.3.2.1.2.1
215 * Serial Number SNMPv2-SMI::enterprises.253.8.53.3.2.1.3.1
216 * Uri SNMPv2-SMI::enterprises.128.2.1.3.6.23.1.5.1
219 tmp = snmp_get_string(hostname, community,
220 "SNMPv2-SMI::enterprises.253.8.51.1.2.1.20.1");
221 if (tmp != NULL) {
222 rc = ieee1284_devid_to_printer_info(tmp, manufacturer, model,
223 description, NULL, serial_no, command_set);
224 free(tmp);
227 if (*manufacturer == NULL)
228 *manufacturer = snmp_get_string(hostname, community,
229 "SNMPv2-SMI::enterprises.128.2.1.3.1.1.0");
230 if (*manufacturer == NULL)
231 *manufacturer = snmp_get_string(hostname, community,
232 "SNMPv2-SMI::enterprises.23.2.32.3.2.1.10.1.16");
233 if (*manufacturer == NULL)
234 *manufacturer = snmp_get_string(hostname, community,
235 "SNMPv2-SMI::enterprises.23.2.32.4.1.0");
237 if (*model == NULL)
238 *model = snmp_get_string(hostname, community,
239 "SNMPv2-SMI::enterprises.128.2.1.3.1.2.0");
240 if (*model == NULL)
241 *model = snmp_get_string(hostname, community,
242 "SNMPv2-SMI::enterprises.23.2.32.3.2.1.10.1.17");
243 if (*model == NULL)
244 *model = snmp_get_string(hostname, community,
245 "SNMPv2-SMI::enterprises.23.2.32.4.2.0");
247 if (*serial_no == NULL)
248 *serial_no = snmp_get_string(hostname, community,
249 "SNMPv2-SMI::enterprises.253.8.53.3.2.1.3.1");
251 if ((*manufacturer != NULL) && (*model != NULL))
252 rc = 0;
254 return (rc);
257 static int
258 snmp_qms_printer_info(char *hostname, char *community, char **manufacturer,
259 char **model, char **description, char **serial_no,
260 char ***command_set, char **uri)
262 int rc = -1;
263 char *tmp = NULL;
266 * MINOLTA-QMS printers appear to store
267 * Prouct Name SNMPv2-SMI::enterprises.2590.1.1.2.1.5.7.14.2.1.1.16.1
268 * Serial Number SNMPv2-SMI::enterprises.2590.1.1.1.5.5.1.1.3.2
269 * URI SNMPv2-SMI::enterprises.2590.1.1.2.1.5.7.14.2.2.1.3.1.1
270 * SNMPv2-SMI::enterprises.2590.1.1.2.1.5.7.14.2.2.1.3.1.2
272 tmp = snmp_get_string(hostname, community,
273 "SNMPv2-SMI::enterprises.2590.1.1.2.1.5.7.14.2.1.1.16.1");
274 if (tmp != NULL) {
275 rc = hrDeviceDesc_to_info(tmp, manufacturer, model,
276 description);
277 free(tmp);
279 if (*serial_no == NULL)
280 *serial_no = snmp_get_string(hostname, community,
281 "SNMPv2-SMI::enterprises.2590.1.1.1.5.5.1.1.3.2");
282 tmp = snmp_get_string(hostname, community,
283 "SNMPv2-SMI::enterprises.2590.1.1.2.1.5.7.14.2.2.1.3.1.2");
284 if (tmp == NULL)
285 tmp = snmp_get_string(hostname, community,
286 "SNMPv2-SMI::enterprises.2590.1.1.2.1.5.7.14.2.2.1.3.1.1");
287 if (tmp != NULL)
288 *uri = tmp;
291 return (rc);
294 static int
295 snmp_oki_printer_info(char *hostname, char *community, char **manufacturer,
296 char **model, char **description, char **serial_no,
297 char ***command_set)
299 int rc = -1;
300 char *tmp = NULL;
303 * OKI printers appear to store
304 * Prouct Name SNMPv2-SMI::enterprises.2001.1.2.683.1.3
305 * Serial Number SNMPv2-SMI::enterprises.2001.1.2.683.1.5
307 tmp = snmp_get_string(hostname, community,
308 "SNMPv2-SMI::enterprises.2001.1.2.683.1.3");
309 if (tmp != NULL) {
310 rc = ieee1284_devid_to_printer_info(tmp, manufacturer, model,
311 description, NULL, serial_no, command_set);
312 free(tmp);
314 if (*serial_no == NULL)
315 *serial_no = snmp_get_string(hostname, community,
316 "SNMPv2-SMI::enterprises.2001.1.2.683.1.5");
319 return (rc);
322 static int
323 snmp_hp_printer_info(char *hostname, char *community, char **manufacturer,
324 char **model, char **description, char **serial_no,
325 char ***command_set)
327 int rc = -1;
328 char *tmp = NULL;
331 * HP printers appear to store
332 * 1284 DevID SNMPv2-SMI::enterprises.11.2.3.9.1.1.7.0
333 * Serial Number SNMPv2-SMI::enterprises.2.3.9.4.2.2.5.1.1.17
335 tmp = snmp_get_string(hostname, community,
336 "SNMPv2-SMI::enterprises.11.2.3.9.1.1.7.0");
337 if (tmp != NULL) {
338 rc = ieee1284_devid_to_printer_info(tmp, manufacturer, model,
339 description, NULL, serial_no, command_set);
340 free(tmp);
342 if (*serial_no == NULL)
343 *serial_no = snmp_get_string(hostname, community,
344 "SNMPv2-SMI::enterprises.2.3.9.4.2.2.5.1.1.17");
347 return (rc);
350 static int
351 snmp_ppm_printer_info(char *hostname, char *community, char **manufacturer,
352 char **model, char **description, char **serial_no,
353 char ***command_set)
355 int rc = -1;
356 char *tmp = NULL;
359 * The PWG portMon MIB stores
360 * 1284 DevID SNMPv2-SMI::enterprises.2699.1.2.1.1.1.3`
362 tmp = snmp_get_string(hostname, community,
363 "SNMPv2-SMI::enterprises.2699.1.2.1.1.1.3");
364 if (tmp != NULL) {
365 rc = ieee1284_devid_to_printer_info(tmp, manufacturer, model,
366 description, NULL, serial_no, command_set);
367 free(tmp);
370 return (rc);
373 static int
374 snmp_prt_printer_info(char *hostname, char *community, char **manufacturer,
375 char **model, char **description, char **serial_no,
376 char ***command_set)
378 int rc = -1;
379 char *tmp = NULL;
382 * The Printer Printer MIB stores
383 * Vendor SNMPv2-SMI::mib-2.43.8.2.1.14.1.1
384 * Model SNMPv2-SMI::mib-2.43.8.2.1.15.1.1
385 * Serial SNMPv2-SMI::mib-2.43.8.2.1.17.1.1
388 if (*manufacturer == NULL)
389 *manufacturer = snmp_get_string(hostname, community,
390 "SNMPv2-SMI::mib-2.43.8.2.1.14.1.1");
391 if (*model == NULL)
392 *model = snmp_get_string(hostname, community,
393 "SNMPv2-SMI::mib-2.43.8.2.1.15.1.1");
394 if (*serial_no == NULL)
395 *serial_no = snmp_get_string(hostname, community,
396 "SNMPv2-SMI::mib-2.43.8.2.1.17.1.1");
398 if (*manufacturer != NULL)
399 rc = 0;
401 return (rc);
404 static int
405 snmp_host_resource_printer_info(char *hostname, char *community,
406 char **manufacturer, char **model, char **description,
407 char **serial_no, char ***command_set)
409 int rc = -1;
410 char *tmp = NULL;
412 tmp = snmp_get_string(hostname, community,
413 "HOST-RESOURCES-MIB::hrDeviceDescr.1");
414 if (tmp != NULL) {
415 rc = hrDeviceDesc_to_info(tmp, manufacturer, model,
416 description);
417 free(tmp);
420 return (rc);
424 snmp_printer_info(char *hostname, char *community, char **manufacturer,
425 char **model, char **description, char **serial_no,
426 char ***command_set, char **uri)
428 char *tmp = NULL;
430 init_snmp("network-printer-probe");
431 init_mib();
433 if (snmp_brother_printer_info(hostname, community, manufacturer, model,
434 description, serial_no, command_set) == 0) {
435 return (0);
436 } else if (snmp_ricoh_printer_info(hostname, community, manufacturer,
437 model, description, serial_no, command_set) == 0) {
438 return (0);
439 } else if (snmp_lexmark_printer_info(hostname, community, manufacturer,
440 model, description, serial_no, command_set) == 0) {
441 return (0);
442 } else if (snmp_xerox_phaser_printer_info(hostname, community,
443 manufacturer, model, description, serial_no,
444 command_set, uri) == 0) {
445 return (0);
446 } else if (snmp_qms_printer_info(hostname, community, manufacturer,
447 model, description, serial_no, command_set, uri) == 0) {
448 return (0);
449 } else if (snmp_oki_printer_info(hostname, community, manufacturer,
450 model, description, serial_no, command_set) == 0) {
451 return (0);
452 } else if (snmp_hp_printer_info(hostname, community, manufacturer,
453 model, description, serial_no, command_set) == 0) {
454 return (0);
455 } else if (snmp_ppm_printer_info(hostname, community, manufacturer,
456 model, description, serial_no, command_set) == 0) {
457 return (0);
458 } else if (snmp_prt_printer_info(hostname, community, manufacturer,
459 model, description, serial_no, command_set) == 0) {
460 return (0);
461 } else if (snmp_host_resource_printer_info(hostname, community,
462 manufacturer, model, description, serial_no,
463 command_set) == 0) {
464 return (0);
467 return (-1);
470 #ifdef NOTDEF
472 #define NP(x) (x?x:"")
475 main(int ac, char *av[])
477 int i;
479 for (i = 1; av[i] != NULL; i++) {
480 char *hostname = av[i], *manufacturer = NULL, *model = NULL,
481 *description = NULL, *serial_no = NULL,
482 **command_set = NULL, *uri = NULL;
483 int rc;
485 rc = snmp_printer_info(hostname, &manufacturer, &model,
486 &description, &serial_no, &command_set, &uri);
487 printf("SNMP data for %s...(%d)\n", hostname, rc);
488 printf("\tvendor = %s\n", NP(manufacturer));
489 printf("\tproduct = %s\n", NP(model));
490 printf("\tdescription = %s\n", NP(description));
491 printf("\tserial = %s\n", NP(serial_no));
492 printf("\tdevice = %s\n", NP(uri));
494 if (command_set != NULL) {
495 int j;
497 printf("\tcommand set = \n");
498 for (j = 0; command_set[j] != NULL; j++)
499 printf("\t\t%s\n", command_set[j]);
503 return (0);
505 #endif