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