]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-builtin-usb_id.c
tty-ask-password: Split out password sending
[thirdparty/systemd.git] / src / udev / udev-builtin-usb_id.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /*
4 * USB device properties and persistent device path
5 *
6 * Copyright (c) 2005 SUSE Linux Products GmbH, Germany
7 * Author: Hannes Reinecke <hare@suse.de>
8 *
9 * Copyright (C) 2005-2011 Kay Sievers <kay@vrfy.org>
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include <ctype.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33
34 #include "alloc-util.h"
35 #include "fd-util.h"
36 #include "string-util.h"
37 #include "udev.h"
38
39 static void set_usb_iftype(char *to, int if_class_num, size_t len) {
40 const char *type = "generic";
41
42 switch (if_class_num) {
43 case 1:
44 type = "audio";
45 break;
46 case 2: /* CDC-Control */
47 break;
48 case 3:
49 type = "hid";
50 break;
51 case 5: /* Physical */
52 break;
53 case 6:
54 type = "media";
55 break;
56 case 7:
57 type = "printer";
58 break;
59 case 8:
60 type = "storage";
61 break;
62 case 9:
63 type = "hub";
64 break;
65 case 0x0a: /* CDC-Data */
66 break;
67 case 0x0b: /* Chip/Smart Card */
68 break;
69 case 0x0d: /* Content Security */
70 break;
71 case 0x0e:
72 type = "video";
73 break;
74 case 0xdc: /* Diagnostic Device */
75 break;
76 case 0xe0: /* Wireless Controller */
77 break;
78 case 0xfe: /* Application-specific */
79 break;
80 case 0xff: /* Vendor-specific */
81 break;
82 default:
83 break;
84 }
85 strncpy(to, type, len);
86 to[len-1] = '\0';
87 }
88
89 static int set_usb_mass_storage_ifsubtype(char *to, const char *from, size_t len) {
90 int type_num = 0;
91 char *eptr;
92 const char *type = "generic";
93
94 type_num = strtoul(from, &eptr, 0);
95 if (eptr != from) {
96 switch (type_num) {
97 case 1: /* RBC devices */
98 type = "rbc";
99 break;
100 case 2:
101 type = "atapi";
102 break;
103 case 3:
104 type = "tape";
105 break;
106 case 4: /* UFI */
107 type = "floppy";
108 break;
109 case 6: /* Transparent SPC-2 devices */
110 type = "scsi";
111 break;
112 default:
113 break;
114 }
115 }
116 strscpy(to, len, type);
117 return type_num;
118 }
119
120 static void set_scsi_type(char *to, const char *from, size_t len) {
121 int type_num;
122 char *eptr;
123 const char *type = "generic";
124
125 type_num = strtoul(from, &eptr, 0);
126 if (eptr != from) {
127 switch (type_num) {
128 case 0:
129 case 0xe:
130 type = "disk";
131 break;
132 case 1:
133 type = "tape";
134 break;
135 case 4:
136 case 7:
137 case 0xf:
138 type = "optical";
139 break;
140 case 5:
141 type = "cd";
142 break;
143 default:
144 break;
145 }
146 }
147 strscpy(to, len, type);
148 }
149
150 #define USB_DT_DEVICE 0x01
151 #define USB_DT_INTERFACE 0x04
152
153 static int dev_if_packed_info(struct udev_device *dev, char *ifs_str, size_t len) {
154 _cleanup_free_ char *filename = NULL;
155 _cleanup_close_ int fd = -1;
156 ssize_t size;
157 unsigned char buf[18 + 65535];
158 size_t pos = 0;
159 unsigned strpos = 0;
160 struct usb_interface_descriptor {
161 uint8_t bLength;
162 uint8_t bDescriptorType;
163 uint8_t bInterfaceNumber;
164 uint8_t bAlternateSetting;
165 uint8_t bNumEndpoints;
166 uint8_t bInterfaceClass;
167 uint8_t bInterfaceSubClass;
168 uint8_t bInterfaceProtocol;
169 uint8_t iInterface;
170 } _packed_;
171
172 if (asprintf(&filename, "%s/descriptors", udev_device_get_syspath(dev)) < 0)
173 return log_oom();
174
175 fd = open(filename, O_RDONLY|O_CLOEXEC);
176 if (fd < 0)
177 return log_debug_errno(errno, "Error opening USB device 'descriptors' file: %m");
178
179 size = read(fd, buf, sizeof(buf));
180 if (size < 18 || size == sizeof(buf))
181 return -EIO;
182
183 ifs_str[0] = '\0';
184 while (pos + sizeof(struct usb_interface_descriptor) < (size_t) size &&
185 strpos + 7 < len - 2) {
186
187 struct usb_interface_descriptor *desc;
188 char if_str[8];
189
190 desc = (struct usb_interface_descriptor *) &buf[pos];
191 if (desc->bLength < 3)
192 break;
193 pos += desc->bLength;
194
195 if (desc->bDescriptorType != USB_DT_INTERFACE)
196 continue;
197
198 if (snprintf(if_str, 8, ":%02x%02x%02x",
199 desc->bInterfaceClass,
200 desc->bInterfaceSubClass,
201 desc->bInterfaceProtocol) != 7)
202 continue;
203
204 if (strstr(ifs_str, if_str) != NULL)
205 continue;
206
207 memcpy(&ifs_str[strpos], if_str, 8),
208 strpos += 7;
209 }
210
211 if (strpos > 0) {
212 ifs_str[strpos++] = ':';
213 ifs_str[strpos++] = '\0';
214 }
215
216 return 0;
217 }
218
219 /*
220 * A unique USB identification is generated like this:
221 *
222 * 1.) Get the USB device type from InterfaceClass and InterfaceSubClass
223 * 2.) If the device type is 'Mass-Storage/SPC-2' or 'Mass-Storage/RBC',
224 * use the SCSI vendor and model as USB-Vendor and USB-model.
225 * 3.) Otherwise, use the USB manufacturer and product as
226 * USB-Vendor and USB-model. Any non-printable characters
227 * in those strings will be skipped; a slash '/' will be converted
228 * into a full stop '.'.
229 * 4.) If that fails, too, we will use idVendor and idProduct
230 * as USB-Vendor and USB-model.
231 * 5.) The USB identification is the USB-vendor and USB-model
232 * string concatenated with an underscore '_'.
233 * 6.) If the device supplies a serial number, this number
234 * is concatenated with the identification with an underscore '_'.
235 */
236 static int builtin_usb_id(struct udev_device *dev, int argc, char *argv[], bool test) {
237 char vendor_str[64] = "";
238 char vendor_str_enc[256];
239 const char *vendor_id;
240 char model_str[64] = "";
241 char model_str_enc[256];
242 const char *product_id;
243 char serial_str[UTIL_NAME_SIZE] = "";
244 char packed_if_str[UTIL_NAME_SIZE] = "";
245 char revision_str[64] = "";
246 char type_str[64] = "";
247 char instance_str[64] = "";
248 const char *ifnum = NULL;
249 const char *driver = NULL;
250 char serial[256];
251
252 struct udev_device *dev_interface = NULL;
253 struct udev_device *dev_usb = NULL;
254 const char *if_class, *if_subclass;
255 int if_class_num;
256 int protocol = 0;
257 size_t l;
258 char *s;
259
260 assert(dev);
261
262 /* shortcut, if we are called directly for a "usb_device" type */
263 if (udev_device_get_devtype(dev) != NULL && streq(udev_device_get_devtype(dev), "usb_device")) {
264 dev_if_packed_info(dev, packed_if_str, sizeof(packed_if_str));
265 dev_usb = dev;
266 goto fallback;
267 }
268
269 /* usb interface directory */
270 dev_interface = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_interface");
271 if (dev_interface == NULL) {
272 log_debug("unable to access usb_interface device of '%s'",
273 udev_device_get_syspath(dev));
274 return EXIT_FAILURE;
275 }
276
277 ifnum = udev_device_get_sysattr_value(dev_interface, "bInterfaceNumber");
278 driver = udev_device_get_sysattr_value(dev_interface, "driver");
279
280 if_class = udev_device_get_sysattr_value(dev_interface, "bInterfaceClass");
281 if (!if_class) {
282 log_debug("%s: cannot get bInterfaceClass attribute",
283 udev_device_get_sysname(dev));
284 return EXIT_FAILURE;
285 }
286
287 if_class_num = strtoul(if_class, NULL, 16);
288 if (if_class_num == 8) {
289 /* mass storage */
290 if_subclass = udev_device_get_sysattr_value(dev_interface, "bInterfaceSubClass");
291 if (if_subclass != NULL)
292 protocol = set_usb_mass_storage_ifsubtype(type_str, if_subclass, sizeof(type_str)-1);
293 } else {
294 set_usb_iftype(type_str, if_class_num, sizeof(type_str)-1);
295 }
296
297 log_debug("%s: if_class %d protocol %d",
298 udev_device_get_syspath(dev_interface), if_class_num, protocol);
299
300 /* usb device directory */
301 dev_usb = udev_device_get_parent_with_subsystem_devtype(dev_interface, "usb", "usb_device");
302 if (!dev_usb) {
303 log_debug("unable to find parent 'usb' device of '%s'",
304 udev_device_get_syspath(dev));
305 return EXIT_FAILURE;
306 }
307
308 /* all interfaces of the device in a single string */
309 dev_if_packed_info(dev_usb, packed_if_str, sizeof(packed_if_str));
310
311 /* mass storage : SCSI or ATAPI */
312 if (protocol == 6 || protocol == 2) {
313 struct udev_device *dev_scsi;
314 const char *scsi_model, *scsi_vendor, *scsi_type, *scsi_rev;
315 int host, bus, target, lun;
316
317 /* get scsi device */
318 dev_scsi = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
319 if (dev_scsi == NULL) {
320 log_debug("unable to find parent 'scsi' device of '%s'",
321 udev_device_get_syspath(dev));
322 goto fallback;
323 }
324 if (sscanf(udev_device_get_sysname(dev_scsi), "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4) {
325 log_debug("invalid scsi device '%s'", udev_device_get_sysname(dev_scsi));
326 goto fallback;
327 }
328
329 /* Generic SPC-2 device */
330 scsi_vendor = udev_device_get_sysattr_value(dev_scsi, "vendor");
331 if (!scsi_vendor) {
332 log_debug("%s: cannot get SCSI vendor attribute",
333 udev_device_get_sysname(dev_scsi));
334 goto fallback;
335 }
336 udev_util_encode_string(scsi_vendor, vendor_str_enc, sizeof(vendor_str_enc));
337 util_replace_whitespace(scsi_vendor, vendor_str, sizeof(vendor_str)-1);
338 util_replace_chars(vendor_str, NULL);
339
340 scsi_model = udev_device_get_sysattr_value(dev_scsi, "model");
341 if (!scsi_model) {
342 log_debug("%s: cannot get SCSI model attribute",
343 udev_device_get_sysname(dev_scsi));
344 goto fallback;
345 }
346 udev_util_encode_string(scsi_model, model_str_enc, sizeof(model_str_enc));
347 util_replace_whitespace(scsi_model, model_str, sizeof(model_str)-1);
348 util_replace_chars(model_str, NULL);
349
350 scsi_type = udev_device_get_sysattr_value(dev_scsi, "type");
351 if (!scsi_type) {
352 log_debug("%s: cannot get SCSI type attribute",
353 udev_device_get_sysname(dev_scsi));
354 goto fallback;
355 }
356 set_scsi_type(type_str, scsi_type, sizeof(type_str)-1);
357
358 scsi_rev = udev_device_get_sysattr_value(dev_scsi, "rev");
359 if (!scsi_rev) {
360 log_debug("%s: cannot get SCSI revision attribute",
361 udev_device_get_sysname(dev_scsi));
362 goto fallback;
363 }
364 util_replace_whitespace(scsi_rev, revision_str, sizeof(revision_str)-1);
365 util_replace_chars(revision_str, NULL);
366
367 /*
368 * some broken devices have the same identifiers
369 * for all luns, export the target:lun number
370 */
371 sprintf(instance_str, "%d:%d", target, lun);
372 }
373
374 fallback:
375 vendor_id = udev_device_get_sysattr_value(dev_usb, "idVendor");
376 product_id = udev_device_get_sysattr_value(dev_usb, "idProduct");
377
378 /* fallback to USB vendor & device */
379 if (vendor_str[0] == '\0') {
380 const char *usb_vendor = NULL;
381
382 usb_vendor = udev_device_get_sysattr_value(dev_usb, "manufacturer");
383 if (!usb_vendor)
384 usb_vendor = vendor_id;
385 if (!usb_vendor) {
386 log_debug("No USB vendor information available");
387 return EXIT_FAILURE;
388 }
389 udev_util_encode_string(usb_vendor, vendor_str_enc, sizeof(vendor_str_enc));
390 util_replace_whitespace(usb_vendor, vendor_str, sizeof(vendor_str)-1);
391 util_replace_chars(vendor_str, NULL);
392 }
393
394 if (model_str[0] == '\0') {
395 const char *usb_model = NULL;
396
397 usb_model = udev_device_get_sysattr_value(dev_usb, "product");
398 if (!usb_model)
399 usb_model = product_id;
400 if (!usb_model)
401 return EXIT_FAILURE;
402 udev_util_encode_string(usb_model, model_str_enc, sizeof(model_str_enc));
403 util_replace_whitespace(usb_model, model_str, sizeof(model_str)-1);
404 util_replace_chars(model_str, NULL);
405 }
406
407 if (revision_str[0] == '\0') {
408 const char *usb_rev;
409
410 usb_rev = udev_device_get_sysattr_value(dev_usb, "bcdDevice");
411 if (usb_rev) {
412 util_replace_whitespace(usb_rev, revision_str, sizeof(revision_str)-1);
413 util_replace_chars(revision_str, NULL);
414 }
415 }
416
417 if (serial_str[0] == '\0') {
418 const char *usb_serial;
419
420 usb_serial = udev_device_get_sysattr_value(dev_usb, "serial");
421 if (usb_serial) {
422 const unsigned char *p;
423
424 /* http://msdn.microsoft.com/en-us/library/windows/hardware/gg487321.aspx */
425 for (p = (unsigned char *)usb_serial; *p != '\0'; p++)
426 if (*p < 0x20 || *p > 0x7f || *p == ',') {
427 usb_serial = NULL;
428 break;
429 }
430 }
431
432 if (usb_serial) {
433 util_replace_whitespace(usb_serial, serial_str, sizeof(serial_str)-1);
434 util_replace_chars(serial_str, NULL);
435 }
436 }
437
438 s = serial;
439 l = strpcpyl(&s, sizeof(serial), vendor_str, "_", model_str, NULL);
440 if (!isempty(serial_str))
441 l = strpcpyl(&s, l, "_", serial_str, NULL);
442
443 if (!isempty(instance_str))
444 strpcpyl(&s, l, "-", instance_str, NULL);
445
446 udev_builtin_add_property(dev, test, "ID_VENDOR", vendor_str);
447 udev_builtin_add_property(dev, test, "ID_VENDOR_ENC", vendor_str_enc);
448 udev_builtin_add_property(dev, test, "ID_VENDOR_ID", vendor_id);
449 udev_builtin_add_property(dev, test, "ID_MODEL", model_str);
450 udev_builtin_add_property(dev, test, "ID_MODEL_ENC", model_str_enc);
451 udev_builtin_add_property(dev, test, "ID_MODEL_ID", product_id);
452 udev_builtin_add_property(dev, test, "ID_REVISION", revision_str);
453 udev_builtin_add_property(dev, test, "ID_SERIAL", serial);
454 if (!isempty(serial_str))
455 udev_builtin_add_property(dev, test, "ID_SERIAL_SHORT", serial_str);
456 if (!isempty(type_str))
457 udev_builtin_add_property(dev, test, "ID_TYPE", type_str);
458 if (!isempty(instance_str))
459 udev_builtin_add_property(dev, test, "ID_INSTANCE", instance_str);
460 udev_builtin_add_property(dev, test, "ID_BUS", "usb");
461 if (!isempty(packed_if_str))
462 udev_builtin_add_property(dev, test, "ID_USB_INTERFACES", packed_if_str);
463 if (ifnum != NULL)
464 udev_builtin_add_property(dev, test, "ID_USB_INTERFACE_NUM", ifnum);
465 if (driver != NULL)
466 udev_builtin_add_property(dev, test, "ID_USB_DRIVER", driver);
467 return EXIT_SUCCESS;
468 }
469
470 const struct udev_builtin udev_builtin_usb_id = {
471 .name = "usb_id",
472 .cmd = builtin_usb_id,
473 .help = "USB device properties",
474 .run_once = true,
475 };