• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * plausible.c --- Figure out if a pathname is ext* or something else.
3  *
4  * Copyright 2014, Oracle, Inc.
5  *
6  * Some parts are:
7  * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
8  *
9  * %Begin-Header%
10  * This file may be redistributed under the terms of the GNU Public
11  * License.
12  * %End-Header%
13  */
14 
15 #ifndef _LARGEFILE_SOURCE
16 #define _LARGEFILE_SOURCE
17 #endif
18 #ifndef _LARGEFILE64_SOURCE
19 #define _LARGEFILE64_SOURCE
20 #endif
21 
22 #include "config.h"
23 #include <fcntl.h>
24 #include <time.h>
25 #include <sys/types.h>
26 #ifdef HAVE_SYS_STAT_H
27 #include <sys/stat.h>
28 #endif
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 #ifdef HAVE_MAGIC_H
33 #include <magic.h>
34 #endif
35 #include "plausible.h"
36 #include "ext2fs/ext2fs.h"
37 #include "nls-enable.h"
38 #include "blkid/blkid.h"
39 
40 #ifdef HAVE_MAGIC_H
41 static magic_t (*dl_magic_open)(int);
42 static const char *(*dl_magic_file)(magic_t, const char *);
43 static int (*dl_magic_load)(magic_t, const char *);
44 static void (*dl_magic_close)(magic_t);
45 
46 /*
47  * NO_CHECK functionality was only added in file 4.20.
48  * Older systems like RHEL 5.x still have file 4.17
49  */
50 #ifndef MAGIC_NO_CHECK_COMPRESS
51 #define MAGIC_NO_CHECK_COMPRESS 0x0001000
52 #endif
53 #ifndef MAGIC_NO_CHECK_ELF
54 #define MAGIC_NO_CHECK_ELF 0x0010000
55 #endif
56 
57 #ifdef HAVE_DLOPEN
58 #include <dlfcn.h>
59 
60 static void *magic_handle;
61 
magic_library_available(void)62 static int magic_library_available(void)
63 {
64 	if (!magic_handle) {
65 		magic_handle = dlopen("libmagic.so.1", RTLD_NOW);
66 		if (!magic_handle)
67 			return 0;
68 
69 		dl_magic_open = (magic_t (*)(int))
70 			dlsym(magic_handle, "magic_open");
71 		dl_magic_file = (const char *(*)(magic_t, const char *))
72 			dlsym(magic_handle, "magic_file");
73 		dl_magic_load = (int (*)(magic_t, const char *))
74 			dlsym(magic_handle, "magic_load");
75 		dl_magic_close = (void (*)(magic_t))
76 			dlsym(magic_handle, "magic_close");
77 	}
78 
79 	if (!dl_magic_open || !dl_magic_file ||
80 	    !dl_magic_load || !dl_magic_close)
81 		return 0;
82 	return 1;
83 }
84 #else
magic_library_available(void)85 static int magic_library_available(void)
86 {
87 	dl_magic_open = magic_open;
88 	dl_magic_file = magic_file;
89 	dl_magic_load = magic_load;
90 	dl_magic_close = magic_close;
91 
92 	return 1;
93 }
94 #endif
95 #endif
96 
print_ext2_info(const char * device)97 static void print_ext2_info(const char *device)
98 
99 {
100 	struct ext2_super_block	*sb;
101 	ext2_filsys		fs;
102 	errcode_t		retval;
103 	time_t			tm;
104 
105 	retval = ext2fs_open2(device, 0, EXT2_FLAG_64BITS, 0, 0,
106 #ifdef _WIN64
107 			      windows_io_manager,
108 #else
109 			      unix_io_manager,
110 #endif
111                   &fs);
112 	if (retval)
113 		return;
114 	sb = fs->super;
115 
116 	if (sb->s_mtime) {
117 		tm = sb->s_mtime;
118 		if (sb->s_last_mounted[0])
119 			printf(_("\tlast mounted on %.*s on %s"),
120 			       EXT2_LEN_STR(sb->s_last_mounted), ctime(&tm));
121 		else
122 			printf(_("\tlast mounted on %s"), ctime(&tm));
123 	} else if (sb->s_mkfs_time) {
124 		tm = sb->s_mkfs_time;
125 		printf(_("\tcreated on %s"), ctime(&tm));
126 	} else if (sb->s_wtime) {
127 		tm = sb->s_wtime;
128 		printf(_("\tlast modified on %s"), ctime(&tm));
129 	}
130 	ext2fs_close_free(&fs);
131 }
132 
133 /*
134  * return 1 if there is no partition table, 0 if a partition table is
135  * detected, and -1 on an error.
136  */
137 #ifdef HAVE_BLKID_PROBE_ENABLE_PARTITIONS
check_partition_table(const char * device)138 static int check_partition_table(const char *device)
139 {
140 	blkid_probe pr;
141 	const char *value;
142 	int ret;
143 
144 	pr = blkid_new_probe_from_filename(device);
145 	if (!pr)
146 		return -1;
147 
148 	ret = blkid_probe_enable_partitions(pr, 1);
149 	if (ret < 0)
150 		goto errout;
151 
152 	ret = blkid_probe_enable_superblocks(pr, 0);
153 	if (ret < 0)
154 		goto errout;
155 
156 	ret = blkid_do_fullprobe(pr);
157 	if (ret < 0)
158 		goto errout;
159 
160 	ret = blkid_probe_lookup_value(pr, "PTTYPE", &value, NULL);
161 	if (ret == 0)
162 		fprintf(stderr, _("Found a %s partition table in %s\n"),
163 			value, device);
164 	else
165 		ret = 1;
166 
167 errout:
168 	blkid_free_probe(pr);
169 	return ret;
170 }
171 #else
check_partition_table(const char * device EXT2FS_ATTR ((unused)))172 static int check_partition_table(const char *device EXT2FS_ATTR((unused)))
173 {
174 	return -1;
175 }
176 #endif
177 
178 /*
179  * return 1 if the device looks plausible, creating the file if necessary
180  */
check_plausibility(const char * device,int flags,int * ret_is_dev)181 int check_plausibility(const char *device, int flags, int *ret_is_dev)
182 {
183 	int fd, ret, is_dev = 0;
184 	ext2fs_struct_stat s;
185 	int fl = O_RDONLY;
186 	blkid_cache cache = NULL;
187 	char *fs_type = NULL;
188 	char *fs_label = NULL;
189 
190 	fd = ext2fs_open_file(device, fl, 0666);
191 	if ((fd < 0) && (errno == ENOENT) && (flags & NO_SIZE)) {
192 		fprintf(stderr, _("The file %s does not exist and no "
193 				  "size was specified.\n"), device);
194 		exit(1);
195 	}
196 	if ((fd < 0) && (errno == ENOENT) && (flags & CREATE_FILE)) {
197 		fl |= O_CREAT;
198 		fd = ext2fs_open_file(device, fl, 0666);
199 		if (fd >= 0 && (flags & VERBOSE_CREATE))
200 			printf(_("Creating regular file %s\n"), device);
201 	}
202 	if (fd < 0) {
203 		fprintf(stderr, _("Could not open %s: %s\n"),
204 			device, error_message(errno));
205 		if (errno == ENOENT)
206 			fputs(_("\nThe device apparently does not exist; "
207 				"did you specify it correctly?\n"), stderr);
208 		exit(1);
209 	}
210 
211 	if (ext2fs_fstat(fd, &s) < 0) {
212 		perror("stat");
213 		exit(1);
214 	}
215 	close(fd);
216 
217 	if (S_ISBLK(s.st_mode))
218 		is_dev = 1;
219 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
220 	/* On FreeBSD, all disk devices are character specials */
221 	if (S_ISCHR(s.st_mode))
222 		is_dev = 1;
223 #endif
224 	if (ret_is_dev)
225 		*ret_is_dev = is_dev;
226 
227 	if ((flags & CHECK_BLOCK_DEV) && !is_dev) {
228 		printf(_("%s is not a block special device.\n"), device);
229 		return 0;
230 	}
231 
232 	/*
233 	 * Note: we use the older-style blkid API's here because we
234 	 * want as much functionality to be available when using the
235 	 * internal blkid library, when e2fsprogs is compiled for
236 	 * non-Linux systems that will probably not have the libraries
237 	 * from util-linux available.  We only use the newer
238 	 * blkid-probe interfaces to access functionality not
239 	 * available in the original blkid library.
240 	 */
241 	if ((flags & CHECK_FS_EXIST) && blkid_get_cache(&cache, NULL) >= 0) {
242 		fs_type = blkid_get_tag_value(cache, "TYPE", device);
243 		if (fs_type)
244 			fs_label = blkid_get_tag_value(cache, "LABEL", device);
245 		blkid_put_cache(cache);
246 	}
247 
248 	if (fs_type) {
249 		if (fs_label)
250 			printf(_("%s contains a %s file system labelled '%s'\n"),
251 			       device, fs_type, fs_label);
252 		else
253 			printf(_("%s contains a %s file system\n"), device,
254 			       fs_type);
255 		if (strncmp(fs_type, "ext", 3) == 0)
256 			print_ext2_info(device);
257 		free(fs_type);
258 		free(fs_label);
259 		return 0;
260 	}
261 
262 #ifdef HAVE_MAGIC_H
263 	if ((flags & CHECK_FS_EXIST) &&
264 	    !getenv("E2FSPROGS_LIBMAGIC_SUPPRESS") &&
265 	    magic_library_available()) {
266 		const char *msg;
267 		magic_t mag;
268 		int has_magic = 0;
269 
270 		mag = dl_magic_open(MAGIC_RAW | MAGIC_SYMLINK | MAGIC_DEVICES |
271 				    MAGIC_ERROR | MAGIC_NO_CHECK_ELF |
272 				    MAGIC_NO_CHECK_COMPRESS);
273 		dl_magic_load(mag, NULL);
274 
275 		msg = dl_magic_file(mag, device);
276 		if (msg && strcmp(msg, "data") && strcmp(msg, "empty")) {
277 			printf(_("%s contains `%s' data\n"), device, msg);
278 			has_magic = 1;
279 		}
280 
281 		dl_magic_close(mag);
282 		return !has_magic;
283 	}
284 #endif
285 	if (flags & CHECK_FS_EXIST) {
286 		ret = check_partition_table(device);
287 		if (ret >= 0)
288 			return ret;
289 	}
290 	return 1;
291 }
292 
293