chiark / gitweb /
driverd: implement AddMatch/RemoveMatch logic
[elogind.git] / src / udev / scsi_id / scsi_id.c
1 /*
2  * Copyright (C) IBM Corp. 2003
3  * Copyright (C) SUSE Linux Products GmbH, 2006
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <signal.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <syslog.h>
27 #include <stdarg.h>
28 #include <ctype.h>
29 #include <getopt.h>
30 #include <sys/stat.h>
31
32 #include "libudev.h"
33 #include "libudev-private.h"
34 #include "scsi_id.h"
35
36 static const struct option options[] = {
37         { "device", required_argument, NULL, 'd' },
38         { "config", required_argument, NULL, 'f' },
39         { "page", required_argument, NULL, 'p' },
40         { "blacklisted", no_argument, NULL, 'b' },
41         { "whitelisted", no_argument, NULL, 'g' },
42         { "replace-whitespace", no_argument, NULL, 'u' },
43         { "sg-version", required_argument, NULL, 's' },
44         { "verbose", no_argument, NULL, 'v' },
45         { "version", no_argument, NULL, 'V' },
46         { "export", no_argument, NULL, 'x' },
47         { "help", no_argument, NULL, 'h' },
48         {}
49 };
50
51 static const char short_options[] = "d:f:ghip:uvVx";
52 static const char dev_short_options[] = "bgp:";
53
54 static int all_good;
55 static int dev_specified;
56 static char config_file[MAX_PATH_LEN] = "/etc/scsi_id.config";
57 static enum page_code default_page_code;
58 static int sg_version = 4;
59 static int use_stderr;
60 static int debug;
61 static int reformat_serial;
62 static int export;
63 static char vendor_str[64];
64 static char model_str[64];
65 static char vendor_enc_str[256];
66 static char model_enc_str[256];
67 static char revision_str[16];
68 static char type_str[16];
69
70 _printf_(6,0)
71 static void log_fn(struct udev *udev, int priority,
72                    const char *file, int line, const char *fn,
73                    const char *format, va_list args)
74 {
75         vsyslog(priority, format, args);
76 }
77
78 static void set_type(const char *from, char *to, size_t len)
79 {
80         int type_num;
81         char *eptr;
82         const char *type = "generic";
83
84         type_num = strtoul(from, &eptr, 0);
85         if (eptr != from) {
86                 switch (type_num) {
87                 case 0:
88                         type = "disk";
89                         break;
90                 case 1:
91                         type = "tape";
92                         break;
93                 case 4:
94                         type = "optical";
95                         break;
96                 case 5:
97                         type = "cd";
98                         break;
99                 case 7:
100                         type = "optical";
101                         break;
102                 case 0xe:
103                         type = "disk";
104                         break;
105                 case 0xf:
106                         type = "optical";
107                         break;
108                 default:
109                         break;
110                 }
111         }
112         strscpy(to, len, type);
113 }
114
115 /*
116  * get_value:
117  *
118  * buf points to an '=' followed by a quoted string ("foo") or a string ending
119  * with a space or ','.
120  *
121  * Return a pointer to the NUL terminated string, returns NULL if no
122  * matches.
123  */
124 static char *get_value(char **buffer)
125 {
126         static const char *quote_string = "\"\n";
127         static const char *comma_string = ",\n";
128         char *val;
129         const char *end;
130
131         if (**buffer == '"') {
132                 /*
133                  * skip leading quote, terminate when quote seen
134                  */
135                 (*buffer)++;
136                 end = quote_string;
137         } else {
138                 end = comma_string;
139         }
140         val = strsep(buffer, end);
141         if (val && end == quote_string)
142                 /*
143                  * skip trailing quote
144                  */
145                 (*buffer)++;
146
147         while (isspace(**buffer))
148                 (*buffer)++;
149
150         return val;
151 }
152
153 static int argc_count(char *opts)
154 {
155         int i = 0;
156         while (*opts != '\0')
157                 if (*opts++ == ' ')
158                         i++;
159         return i;
160 }
161
162 /*
163  * get_file_options:
164  *
165  * If vendor == NULL, find a line in the config file with only "OPTIONS=";
166  * if vendor and model are set find the first OPTIONS line in the config
167  * file that matches. Set argc and argv to match the OPTIONS string.
168  *
169  * vendor and model can end in '\n'.
170  */
171 static int get_file_options(struct udev *udev,
172                             const char *vendor, const char *model,
173                             int *argc, char ***newargv)
174 {
175         char *buffer;
176         FILE *fd;
177         char *buf;
178         char *str1;
179         char *vendor_in, *model_in, *options_in; /* read in from file */
180         int lineno;
181         int c;
182         int retval = 0;
183
184         fd = fopen(config_file, "re");
185         if (fd == NULL) {
186                 if (errno == ENOENT) {
187                         return 1;
188                 } else {
189                         log_error("can't open %s: %m\n", config_file);
190                         return -1;
191                 }
192         }
193
194         /*
195          * Allocate a buffer rather than put it on the stack so we can
196          * keep it around to parse any options (any allocated newargv
197          * points into this buffer for its strings).
198          */
199         buffer = malloc(MAX_BUFFER_LEN);
200         if (!buffer) {
201                 fclose(fd);
202                 return log_oom();
203         }
204
205         *newargv = NULL;
206         lineno = 0;
207         while (1) {
208                 vendor_in = model_in = options_in = NULL;
209
210                 buf = fgets(buffer, MAX_BUFFER_LEN, fd);
211                 if (buf == NULL)
212                         break;
213                 lineno++;
214                 if (buf[strlen(buffer) - 1] != '\n') {
215                         log_error("Config file line %d too long\n", lineno);
216                         break;
217                 }
218
219                 while (isspace(*buf))
220                         buf++;
221
222                 /* blank or all whitespace line */
223                 if (*buf == '\0')
224                         continue;
225
226                 /* comment line */
227                 if (*buf == '#')
228                         continue;
229
230                 str1 = strsep(&buf, "=");
231                 if (str1 && strcaseeq(str1, "VENDOR")) {
232                         str1 = get_value(&buf);
233                         if (!str1) {
234                                 retval = log_oom();
235                                 break;
236                         }
237                         vendor_in = str1;
238
239                         str1 = strsep(&buf, "=");
240                         if (str1 && strcaseeq(str1, "MODEL")) {
241                                 str1 = get_value(&buf);
242                                 if (!str1) {
243                                         retval = log_oom();
244                                         break;
245                                 }
246                                 model_in = str1;
247                                 str1 = strsep(&buf, "=");
248                         }
249                 }
250
251                 if (str1 && strcaseeq(str1, "OPTIONS")) {
252                         str1 = get_value(&buf);
253                         if (!str1) {
254                                 retval = log_oom();
255                                 break;
256                         }
257                         options_in = str1;
258                 }
259
260                 /*
261                  * Only allow: [vendor=foo[,model=bar]]options=stuff
262                  */
263                 if (!options_in || (!vendor_in && model_in)) {
264                         log_error("Error parsing config file line %d '%s'\n", lineno, buffer);
265                         retval = -1;
266                         break;
267                 }
268                 if (vendor == NULL) {
269                         if (vendor_in == NULL)
270                                 break;
271                 } else if ((vendor_in && strneq(vendor, vendor_in,
272                                                  strlen(vendor_in))) &&
273                            (!model_in || (strneq(model, model_in,
274                                                   strlen(model_in))))) {
275                                 /*
276                                  * Matched vendor and optionally model.
277                                  *
278                                  * Note: a short vendor_in or model_in can
279                                  * give a partial match (that is FOO
280                                  * matches FOOBAR).
281                                  */
282                                 break;
283                 }
284         }
285
286         if (retval == 0) {
287                 if (vendor_in != NULL || model_in != NULL ||
288                     options_in != NULL) {
289                         /*
290                          * Something matched. Allocate newargv, and store
291                          * values found in options_in.
292                          */
293                         strcpy(buffer, options_in);
294                         c = argc_count(buffer) + 2;
295                         *newargv = calloc(c, sizeof(**newargv));
296                         if (!*newargv) {
297                                 retval = log_oom();
298                         } else {
299                                 *argc = c;
300                                 c = 0;
301                                 /*
302                                  * argv[0] at 0 is skipped by getopt, but
303                                  * store the buffer address there for
304                                  * later freeing
305                                  */
306                                 (*newargv)[c] = buffer;
307                                 for (c = 1; c < *argc; c++)
308                                         (*newargv)[c] = strsep(&buffer, " \t");
309                         }
310                 } else {
311                         /* No matches  */
312                         retval = 1;
313                 }
314         }
315         if (retval != 0)
316                 free(buffer);
317         fclose(fd);
318         return retval;
319 }
320
321 static int set_options(struct udev *udev,
322                        int argc, char **argv, const char *short_opts,
323                        char *maj_min_dev)
324 {
325         int option;
326
327         /*
328          * optind is a global extern used by getopt. Since we can call
329          * set_options twice (once for command line, and once for config
330          * file) we have to reset this back to 1.
331          */
332         optind = 1;
333         while (1) {
334                 option = getopt_long(argc, argv, short_opts, options, NULL);
335                 if (option == -1)
336                         break;
337
338                 switch (option) {
339                 case 'b':
340                         all_good = 0;
341                         break;
342
343                 case 'd':
344                         dev_specified = 1;
345                         strscpy(maj_min_dev, MAX_PATH_LEN, optarg);
346                         break;
347
348                 case 'e':
349                         use_stderr = 1;
350                         break;
351
352                 case 'f':
353                         strscpy(config_file, MAX_PATH_LEN, optarg);
354                         break;
355
356                 case 'g':
357                         all_good = 1;
358                         break;
359
360                 case 'h':
361                         printf("Usage: scsi_id OPTIONS <device>\n"
362                                "  --device=                     device node for SG_IO commands\n"
363                                "  --config=                     location of config file\n"
364                                "  --page=0x80|0x83|pre-spc3-83  SCSI page (0x80, 0x83, pre-spc3-83)\n"
365                                "  --sg-version=3|4              use SGv3 or SGv4\n"
366                                "  --blacklisted                 threat device as blacklisted\n"
367                                "  --whitelisted                 threat device as whitelisted\n"
368                                "  --replace-whitespace          replace all whitespaces by underscores\n"
369                                "  --verbose                     verbose logging\n"
370                                "  --version                     print version\n"
371                                "  --export                      print values as environment keys\n"
372                                "  --help                        print this help text\n\n");
373                         exit(0);
374
375                 case 'p':
376                         if (streq(optarg, "0x80")) {
377                                 default_page_code = PAGE_80;
378                         } else if (streq(optarg, "0x83")) {
379                                 default_page_code = PAGE_83;
380                         } else if (streq(optarg, "pre-spc3-83")) {
381                                 default_page_code = PAGE_83_PRE_SPC3;
382                         } else {
383                                 log_error("Unknown page code '%s'\n", optarg);
384                                 return -1;
385                         }
386                         break;
387
388                 case 's':
389                         sg_version = atoi(optarg);
390                         if (sg_version < 3 || sg_version > 4) {
391                                 log_error("Unknown SG version '%s'\n", optarg);
392                                 return -1;
393                         }
394                         break;
395
396                 case 'u':
397                         reformat_serial = 1;
398                         break;
399
400                 case 'x':
401                         export = 1;
402                         break;
403
404                 case 'v':
405                         debug++;
406                         break;
407
408                 case 'V':
409                         printf("%s\n", VERSION);
410                         exit(0);
411                         break;
412
413                 default:
414                         exit(1);
415                 }
416         }
417         if (optind < argc && !dev_specified) {
418                 dev_specified = 1;
419                 strscpy(maj_min_dev, MAX_PATH_LEN, argv[optind]);
420         }
421         return 0;
422 }
423
424 static int per_dev_options(struct udev *udev,
425                            struct scsi_id_device *dev_scsi, int *good_bad, int *page_code)
426 {
427         int retval;
428         int newargc;
429         char **newargv = NULL;
430         int option;
431
432         *good_bad = all_good;
433         *page_code = default_page_code;
434
435         retval = get_file_options(udev, vendor_str, model_str, &newargc, &newargv);
436
437         optind = 1; /* reset this global extern */
438         while (retval == 0) {
439                 option = getopt_long(newargc, newargv, dev_short_options, options, NULL);
440                 if (option == -1)
441                         break;
442
443                 switch (option) {
444                 case 'b':
445                         *good_bad = 0;
446                         break;
447
448                 case 'g':
449                         *good_bad = 1;
450                         break;
451
452                 case 'p':
453                         if (streq(optarg, "0x80")) {
454                                 *page_code = PAGE_80;
455                         } else if (streq(optarg, "0x83")) {
456                                 *page_code = PAGE_83;
457                         } else if (streq(optarg, "pre-spc3-83")) {
458                                 *page_code = PAGE_83_PRE_SPC3;
459                         } else {
460                                 log_error("Unknown page code '%s'\n", optarg);
461                                 retval = -1;
462                         }
463                         break;
464
465                 default:
466                         log_error("Unknown or bad option '%c' (0x%x)\n", option, option);
467                         retval = -1;
468                         break;
469                 }
470         }
471
472         if (newargv) {
473                 free(newargv[0]);
474                 free(newargv);
475         }
476         return retval;
477 }
478
479 static int set_inq_values(struct udev *udev, struct scsi_id_device *dev_scsi, const char *path)
480 {
481         int retval;
482
483         dev_scsi->use_sg = sg_version;
484
485         retval = scsi_std_inquiry(udev, dev_scsi, path);
486         if (retval)
487                 return retval;
488
489         udev_util_encode_string(dev_scsi->vendor, vendor_enc_str, sizeof(vendor_enc_str));
490         udev_util_encode_string(dev_scsi->model, model_enc_str, sizeof(model_enc_str));
491
492         util_replace_whitespace(dev_scsi->vendor, vendor_str, sizeof(vendor_str));
493         util_replace_chars(vendor_str, NULL);
494         util_replace_whitespace(dev_scsi->model, model_str, sizeof(model_str));
495         util_replace_chars(model_str, NULL);
496         set_type(dev_scsi->type, type_str, sizeof(type_str));
497         util_replace_whitespace(dev_scsi->revision, revision_str, sizeof(revision_str));
498         util_replace_chars(revision_str, NULL);
499         return 0;
500 }
501
502 /*
503  * scsi_id: try to get an id, if one is found, printf it to stdout.
504  * returns a value passed to exit() - 0 if printed an id, else 1.
505  */
506 static int scsi_id(struct udev *udev, char *maj_min_dev)
507 {
508         struct scsi_id_device dev_scsi;
509         int good_dev;
510         int page_code;
511         int retval = 0;
512
513         memset(&dev_scsi, 0x00, sizeof(struct scsi_id_device));
514
515         if (set_inq_values(udev, &dev_scsi, maj_min_dev) < 0) {
516                 retval = 1;
517                 goto out;
518         }
519
520         /* get per device (vendor + model) options from the config file */
521         per_dev_options(udev, &dev_scsi, &good_dev, &page_code);
522         if (!good_dev) {
523                 retval = 1;
524                 goto out;
525         }
526
527         /* read serial number from mode pages (no values for optical drives) */
528         scsi_get_serial(udev, &dev_scsi, maj_min_dev, page_code, MAX_SERIAL_LEN);
529
530         if (export) {
531                 char serial_str[MAX_SERIAL_LEN];
532
533                 printf("ID_SCSI=1\n");
534                 printf("ID_VENDOR=%s\n", vendor_str);
535                 printf("ID_VENDOR_ENC=%s\n", vendor_enc_str);
536                 printf("ID_MODEL=%s\n", model_str);
537                 printf("ID_MODEL_ENC=%s\n", model_enc_str);
538                 printf("ID_REVISION=%s\n", revision_str);
539                 printf("ID_TYPE=%s\n", type_str);
540                 if (dev_scsi.serial[0] != '\0') {
541                         util_replace_whitespace(dev_scsi.serial, serial_str, sizeof(serial_str));
542                         util_replace_chars(serial_str, NULL);
543                         printf("ID_SERIAL=%s\n", serial_str);
544                         util_replace_whitespace(dev_scsi.serial_short, serial_str, sizeof(serial_str));
545                         util_replace_chars(serial_str, NULL);
546                         printf("ID_SERIAL_SHORT=%s\n", serial_str);
547                 }
548                 if (dev_scsi.wwn[0] != '\0') {
549                         printf("ID_WWN=0x%s\n", dev_scsi.wwn);
550                         if (dev_scsi.wwn_vendor_extension[0] != '\0') {
551                                 printf("ID_WWN_VENDOR_EXTENSION=0x%s\n", dev_scsi.wwn_vendor_extension);
552                                 printf("ID_WWN_WITH_EXTENSION=0x%s%s\n", dev_scsi.wwn, dev_scsi.wwn_vendor_extension);
553                         } else {
554                                 printf("ID_WWN_WITH_EXTENSION=0x%s\n", dev_scsi.wwn);
555                         }
556                 }
557                 if (dev_scsi.tgpt_group[0] != '\0') {
558                         printf("ID_TARGET_PORT=%s\n", dev_scsi.tgpt_group);
559                 }
560                 if (dev_scsi.unit_serial_number[0] != '\0') {
561                         printf("ID_SCSI_SERIAL=%s\n", dev_scsi.unit_serial_number);
562                 }
563                 goto out;
564         }
565
566         if (dev_scsi.serial[0] == '\0') {
567                 retval = 1;
568                 goto out;
569         }
570
571         if (reformat_serial) {
572                 char serial_str[MAX_SERIAL_LEN];
573
574                 util_replace_whitespace(dev_scsi.serial, serial_str, sizeof(serial_str));
575                 util_replace_chars(serial_str, NULL);
576                 printf("%s\n", serial_str);
577                 goto out;
578         }
579
580         printf("%s\n", dev_scsi.serial);
581 out:
582         return retval;
583 }
584
585 int main(int argc, char **argv)
586 {
587         struct udev *udev;
588         int retval = 0;
589         char maj_min_dev[MAX_PATH_LEN];
590         int newargc;
591         char **newargv;
592
593         udev = udev_new();
594         if (udev == NULL)
595                 goto exit;
596
597         log_open();
598         udev_set_log_fn(udev, log_fn);
599
600         /*
601          * Get config file options.
602          */
603         newargv = NULL;
604         retval = get_file_options(udev, NULL, NULL, &newargc, &newargv);
605         if (retval < 0) {
606                 retval = 1;
607                 goto exit;
608         }
609         if (newargv && (retval == 0)) {
610                 if (set_options(udev, newargc, newargv, short_options, maj_min_dev) < 0) {
611                         retval = 2;
612                         goto exit;
613                 }
614                 free(newargv);
615         }
616
617         /*
618          * Get command line options (overriding any config file settings).
619          */
620         if (set_options(udev, argc, argv, short_options, maj_min_dev) < 0)
621                 exit(1);
622
623         if (!dev_specified) {
624                 log_error("no device specified\n");
625                 retval = 1;
626                 goto exit;
627         }
628
629         retval = scsi_id(udev, maj_min_dev);
630
631 exit:
632         udev_unref(udev);
633         log_close();
634         return retval;
635 }