1 /* $OpenBSD: readlabel.c,v 1.15 2019/06/28 13:32:43 deraadt Exp $ */
2
3 /*
4 * Copyright (c) 1996, Jason Downs. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/types.h>
29 #include <sys/disk.h>
30 #include <sys/dkio.h>
31 #define DKTYPENAMES
32 #include <sys/disklabel.h>
33 #include <sys/ioctl.h>
34 #include <sys/stat.h>
35 #include <stdio.h>
36 #include <err.h>
37 #include <errno.h>
38 #include <limits.h>
39 #include <fcntl.h>
40 #include <paths.h>
41 #include <string.h>
42 #include <unistd.h>
43
44 #include "util.h"
45
46 /*
47 * Try to get a disklabel for the specified device, and return mount_xxx
48 * style filesystem type name for the specified partition.
49 */
50 char *
readlabelfs(char * device,int verbose)51 readlabelfs(char *device, int verbose)
52 {
53 char rpath[PATH_MAX];
54 struct dk_diskmap dm;
55 struct disklabel dk;
56 char part, *type;
57 struct stat sbuf;
58 int fd = -1;
59
60 /* Perform disk mapping if device is given as a DUID. */
61 if (isduid(device, 0)) {
62 if ((fd = open("/dev/diskmap", O_RDONLY|O_CLOEXEC)) != -1) {
63 bzero(&dm, sizeof(struct dk_diskmap));
64 strlcpy(rpath, device, sizeof(rpath));
65 part = rpath[strlen(rpath) - 1];
66 dm.device = rpath;
67 dm.fd = fd;
68 dm.flags = DM_OPENPART;
69 if (ioctl(fd, DIOCMAP, &dm) == -1)
70 close(fd);
71 else
72 goto disklabel;
73 }
74 }
75
76 /* Assuming device is of the form /dev/??p, build a raw partition. */
77 if (stat(device, &sbuf) == -1) {
78 if (verbose)
79 warn("%s", device);
80 return (NULL);
81 }
82 switch (sbuf.st_mode & S_IFMT) {
83 case S_IFCHR:
84 /* Ok... already a raw device. Hmm. */
85 strlcpy(rpath, device, sizeof(rpath));
86
87 /* Change partition name. */
88 part = rpath[strlen(rpath) - 1];
89 rpath[strlen(rpath) - 1] = 'a' + getrawpartition();
90 break;
91 case S_IFBLK:
92 if (strlen(device) > sizeof(_PATH_DEV) - 1) {
93 snprintf(rpath, sizeof(rpath), "%sr%s", _PATH_DEV,
94 &device[sizeof(_PATH_DEV) - 1]);
95 /* Change partition name. */
96 part = rpath[strlen(rpath) - 1];
97 rpath[strlen(rpath) - 1] = 'a' + getrawpartition();
98 break;
99 }
100 /* FALLTHROUGH */
101 default:
102 if (verbose)
103 warnx("%s: not a device node", device);
104 return (NULL);
105 }
106
107 /* If rpath doesn't exist, change that partition back. */
108 fd = open(rpath, O_RDONLY|O_CLOEXEC);
109 if (fd == -1) {
110 if (errno == ENOENT) {
111 rpath[strlen(rpath) - 1] = part;
112
113 fd = open(rpath, O_RDONLY|O_CLOEXEC);
114 if (fd == -1) {
115 if (verbose)
116 warn("%s", rpath);
117 return (NULL);
118 }
119 } else {
120 if (verbose)
121 warn("%s", rpath);
122 return (NULL);
123 }
124 }
125
126 disklabel:
127
128 if (ioctl(fd, DIOCGDINFO, &dk) == -1) {
129 if (verbose)
130 warn("%s: couldn't read disklabel", rpath);
131 close(fd);
132 return (NULL);
133 }
134 close(fd);
135
136 if (dk.d_partitions[part - 'a'].p_fstype >= FSMAXTYPES) {
137 if (verbose)
138 warnx("%s: bad filesystem type in label", rpath);
139 return (NULL);
140 }
141
142 type = fstypesnames[dk.d_partitions[part - 'a'].p_fstype];
143 return ((type[0] == '\0') ? NULL : type);
144 }
145