chiark / gitweb /
scsi_id: make sure, we do not have slashes in values
[elogind.git] / extras / volume_id / vol_id.c
1 /*
2  * vol_id - read filesystem label and uuid
3  *
4  * Copyright (C) 2005-2008 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef _GNU_SOURCE
21 #define _GNU_SOURCE 1
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <errno.h>
30 #include <pwd.h>
31 #include <grp.h>
32 #include <getopt.h>
33 #include <fcntl.h>
34 #include <inttypes.h>
35 #include <sys/ioctl.h>
36
37 #include "../../udev/udev.h"
38 #include "lib/libvolume_id.h"
39
40 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
41
42 static int debug;
43 struct udev *udev_ctx;
44
45 static void log_fn(struct udev *udev, int priority,
46                    const char *file, int line, const char *fn,
47                    const char *format, va_list args)
48 {
49         if (debug) {
50                 fprintf(stderr, "%s: ", fn != NULL ? fn : file);
51                 vfprintf(stderr, format, args);
52         } else {
53                 vsyslog(priority, format, args);
54         }
55 }
56
57 static void vid_log(int priority, const char *file, int line, const char *format, ...)
58 {
59         va_list args;
60
61         if (priority > udev_get_log_priority(udev_ctx))
62                 return;
63         va_start(args, format);
64         log_fn(udev_ctx, priority, file, line, NULL, format, args);
65         va_end(args);
66         return;
67 }
68
69 static int all_probers(volume_id_probe_fn_t probe_fn,
70                        struct volume_id *id, uint64_t off, uint64_t size,
71                        void *data)
72 {
73         const char *type;
74
75         if (probe_fn(id, off, size) == 0)
76                 if (volume_id_get_type(id, &type))
77                         printf("%s\n", type);
78
79         return 0;
80 }
81
82 int main(int argc, char *argv[])
83 {
84         static const struct option options[] = {
85                 { "label", no_argument, NULL, 'l' },
86                 { "label-raw", no_argument, NULL, 'L' },
87                 { "uuid", no_argument, NULL, 'u' },
88                 { "type", no_argument, NULL, 't' },
89                 { "export", no_argument, NULL, 'x' },
90                 { "skip-raid", no_argument, NULL, 's' },
91                 { "size", required_argument, NULL, 'S' },
92                 { "probe-all", no_argument, NULL, 'a' },
93                 { "offset", optional_argument, NULL, 'o' },
94                 { "debug", no_argument, NULL, 'd' },
95                 { "help", no_argument, NULL, 'h' },
96                 {}
97         };
98
99         enum print_type {
100                 PRINT_EXPORT,
101                 PRINT_TYPE,
102                 PRINT_LABEL,
103                 PRINT_UUID,
104                 PRINT_LABEL_RAW,
105         } print = PRINT_EXPORT;
106
107         struct volume_id *vid = NULL;
108         char label_safe[256];
109         char label_enc[256];
110         char uuid_safe[256];
111         char uuid_enc[256];
112         char type_enc[256];
113         char type_version_enc[256];
114         uint64_t size = 0;
115         int skip_raid = 0;
116         int probe_all = 0;
117         uint64_t offset = 0;
118         const char *node;
119         int fd;
120         const char *label, *uuid, *type, *type_version, *usage;
121         int retval;
122         int rc = 0;
123
124         udev_ctx = udev_new();
125         if (udev_ctx == NULL)
126                 goto exit;
127         logging_init("vol_id");
128         udev_set_log_fn(udev_ctx, log_fn);
129
130         /* hook in our debug into libvolume_id */
131         volume_id_log_fn = vid_log;
132
133         while (1) {
134                 int option;
135
136                 option = getopt_long(argc, argv, "lLutxsS:aodh", options, NULL);
137                 if (option == -1)
138                         break;
139
140                 switch (option) {
141                 case 'd':
142                         debug = 1;
143                         if (udev_get_log_priority(udev_ctx) < LOG_INFO)
144                                 udev_set_log_priority(udev_ctx, LOG_INFO);
145                         break;
146                 case 'l':
147                         print = PRINT_LABEL;
148                         break;
149                 case 'L':
150                         print = PRINT_LABEL_RAW;
151                         break;
152                 case 'u':
153                         print = PRINT_UUID;
154                         break;
155                 case 't':
156                         print = PRINT_TYPE;
157                         break;
158                 case 'x':
159                         print = PRINT_EXPORT;
160                         break;
161                 case 's':
162                         skip_raid = 1;
163                         break;
164                 case 'a':
165                         probe_all = 1;
166                         break;
167                 case 'S':
168                         if (optarg[0] != '\0')
169                                 size = strtoull(optarg, NULL, 0);
170                         break;
171                 case 'o':
172                         if (optarg[0] != '\0')
173                                 offset = strtoull(optarg, NULL, 0);
174                         break;
175                 case 'h':
176                         printf("Usage: vol_id [options] <device>\n"
177                             " --export         export key/value pairs\n"
178                             " --type           filesystem type\n"
179                             " --label          filesystem label\n"
180                             " --label-raw      raw label\n"
181                             " --uuid           filesystem uuid\n"
182                             " --skip-raid      don't probe for raid\n"
183                             " --probe-all      find possibly conflicting signatures\n"
184                             " --offset=<bytes> probe at the given offset\n"
185                             " --size=<bytes>   overwrite device size\n"
186                             " --debug          print debug output to stderr\n"
187                             " --help\n\n");
188                         goto exit;
189                 default:
190                         retval = 1;
191                         goto exit;
192                 }
193         }
194
195         node = argv[optind];
196         if (!node) {
197                 err(udev_ctx, "no device\n");
198                 fprintf(stderr, "no device\n");
199                 rc = 1;
200                 goto exit;
201         }
202
203         fd = open(node, O_RDONLY);
204         if (fd < 0) {
205                 fprintf(stderr, "%s: error opening volume\n", node);
206                 rc = 2;
207                 goto exit;
208         }
209
210         vid = volume_id_open_fd(fd);
211         if (vid == NULL) {
212                 rc = 2;
213                 goto exit;
214         }
215
216         if (size == 0) {
217                 if (ioctl(fd, BLKGETSIZE64, &size) != 0)
218                         size = 0;
219                 info(udev_ctx, "BLKGETSIZE64=%" PRIu64 " (%" PRIu64 "GB)\n", size, size >> 30);
220         }
221
222         /* try to drop all privileges before reading disk content */
223         if (getuid() == 0) {
224                 struct passwd *pw;
225
226                 pw = getpwnam("nobody");
227                 if (pw != NULL && pw->pw_uid > 0 && pw->pw_gid > 0) {
228                         if (setgroups(0, NULL) != 0 ||
229                             setgid(pw->pw_gid) != 0 ||
230                             setuid(pw->pw_uid) != 0)
231                                 info(udev_ctx, "unable to drop privileges: %s\n\n", strerror(errno));
232                 }
233         }
234
235         if (probe_all) {
236                 volume_id_all_probers(all_probers, vid, offset, size, NULL);
237                 goto exit;
238         }
239
240         if (skip_raid)
241                 retval = volume_id_probe_filesystem(vid, offset, size);
242         else
243                 retval = volume_id_probe_all(vid, offset, size);
244         if (retval != 0) {
245                 fprintf(stderr, "unknown or non-unique volume type "
246                                 "(--probe-all lists possibly conflicting types)\n");
247                 rc = 4;
248                 goto exit;
249         }
250
251         if (!volume_id_get_label(vid, &label) ||
252             !volume_id_get_usage(vid, &usage) ||
253             !volume_id_get_type(vid, &type) ||
254             !volume_id_get_type_version(vid, &type_version) ||
255             !volume_id_get_uuid(vid, &uuid)) {
256                 rc = 4;
257                 goto exit;
258         }
259
260         udev_util_replace_whitespace(label, label_safe, sizeof(label_safe));
261         udev_util_replace_chars(label_safe, UDEV_ALLOWED_CHARS_INPUT);
262         volume_id_encode_string(label, label_enc, sizeof(label_enc));
263
264         udev_util_replace_whitespace(uuid, uuid_safe, sizeof(uuid_safe));
265         udev_util_replace_chars(uuid_safe, UDEV_ALLOWED_CHARS_INPUT);
266         volume_id_encode_string(uuid, uuid_enc, sizeof(uuid_enc));
267
268         volume_id_encode_string(type, type_enc, sizeof(type_enc));
269         volume_id_encode_string(type_version, type_version_enc, sizeof(type_version_enc));
270
271         switch (print) {
272         case PRINT_EXPORT:
273                 printf("ID_FS_USAGE=%s\n", usage);
274                 printf("ID_FS_TYPE=%s\n", type_enc);
275                 printf("ID_FS_VERSION=%s\n", type_version_enc);
276                 printf("ID_FS_UUID=%s\n", uuid_safe);
277                 printf("ID_FS_UUID_ENC=%s\n", uuid_enc);
278                 printf("ID_FS_LABEL=%s\n", label_safe);
279                 printf("ID_FS_LABEL_ENC=%s\n", label_enc);
280                 break;
281         case PRINT_TYPE:
282                 printf("%s\n", type);
283                 break;
284         case PRINT_LABEL:
285                 if (label_safe[0] == '\0' || strcmp(usage, "raid") == 0) {
286                         rc = 3;
287                         goto exit;
288                 }
289                 printf("%s\n", label_safe);
290                 break;
291         case PRINT_UUID:
292                 if (uuid_enc[0] == '\0' || strcmp(usage, "raid") == 0) {
293                         rc = 4;
294                         goto exit;
295                 }
296                 printf("%s\n", uuid_enc);
297                 break;
298         case PRINT_LABEL_RAW:
299                 if (label[0] == '\0' || strcmp(usage, "raid") == 0) {
300                         rc = 3;
301                         goto exit;
302                 }
303                 printf("%s\n", label);
304                 break;
305         }
306
307 exit:
308         if (vid != NULL)
309                 volume_id_close(vid);
310         udev_unref(udev_ctx);
311         logging_close();
312         return rc;
313 }