Import file-5.18.
[dragonfly.git] / contrib / file / src / fsmagic.c
1 /*
2  * Copyright (c) Ian F. Darwin 1986-1995.
3  * Software written by Ian F. Darwin and others;
4  * maintained 1995-present by Christos Zoulas and others.
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 immediately at the beginning of the file, without modification,
11  *    this list of conditions, and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *  
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 /*
29  * fsmagic - magic based on filesystem info - directory, special files, etc.
30  */
31
32 #include "file.h"
33
34 #ifndef lint
35 FILE_RCSID("@(#)$File: fsmagic.c,v 1.71 2013/12/01 18:01:07 christos Exp $")
36 #endif  /* lint */
37
38 #include "magic.h"
39 #include <string.h>
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
43 #include <stdlib.h>
44 /* Since major is a function on SVR4, we cannot use `ifndef major'.  */
45 #ifdef MAJOR_IN_MKDEV
46 # include <sys/mkdev.h>
47 # define HAVE_MAJOR
48 #endif
49 #ifdef MAJOR_IN_SYSMACROS
50 # include <sys/sysmacros.h>
51 # define HAVE_MAJOR
52 #endif
53 #ifdef major                    /* Might be defined in sys/types.h.  */
54 # define HAVE_MAJOR
55 #endif
56   
57 #ifndef HAVE_MAJOR
58 # define major(dev)  (((dev) >> 8) & 0xff)
59 # define minor(dev)  ((dev) & 0xff)
60 #endif
61 #undef HAVE_MAJOR
62 #ifdef  S_IFLNK
63 private int
64 bad_link(struct magic_set *ms, int err, char *buf)
65 {
66         int mime = ms->flags & MAGIC_MIME;
67         if ((mime & MAGIC_MIME_TYPE) &&
68             file_printf(ms, "inode/symlink")
69             == -1)
70                 return -1;
71         else if (!mime) {
72                 if (ms->flags & MAGIC_ERROR) {
73                         file_error(ms, err,
74                                    "broken symbolic link to `%s'", buf);
75                         return -1;
76                 } 
77                 if (file_printf(ms, "broken symbolic link to `%s'", buf) == -1)
78                         return -1;
79         }
80         return 1;
81 }
82 #endif
83 private int
84 handle_mime(struct magic_set *ms, int mime, const char *str)
85 {
86         if ((mime & MAGIC_MIME_TYPE)) {
87                 if (file_printf(ms, "inode/%s", str) == -1)
88                         return -1;
89                 if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms,
90                     "; charset=") == -1)
91                         return -1;
92         }
93         if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms, "binary") == -1)
94                 return -1;
95         return 0;
96 }
97
98 protected int
99 file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
100 {
101         int ret, did = 0;
102         int mime = ms->flags & MAGIC_MIME;
103 #ifdef  S_IFLNK
104         char buf[BUFSIZ+4];
105         ssize_t nch;
106         struct stat tstatbuf;
107 #endif
108
109         if (ms->flags & MAGIC_APPLE)
110                 return 0;
111         if (fn == NULL)
112                 return 0;
113
114 #define COMMA   (did++ ? ", " : "")
115         /*
116          * Fstat is cheaper but fails for files you don't have read perms on.
117          * On 4.2BSD and similar systems, use lstat() to identify symlinks.
118          */
119 #ifdef  S_IFLNK
120         if ((ms->flags & MAGIC_SYMLINK) == 0)
121                 ret = lstat(fn, sb);
122         else
123 #endif
124         ret = stat(fn, sb);     /* don't merge into if; see "ret =" above */
125
126         if (ret) {
127                 if (ms->flags & MAGIC_ERROR) {
128                         file_error(ms, errno, "cannot stat `%s'", fn);
129                         return -1;
130                 }
131                 if (file_printf(ms, "cannot open `%s' (%s)",
132                     fn, strerror(errno)) == -1)
133                         return -1;
134                 return 0;
135         }
136
137         ret = 1;
138         if (!mime) {
139 #ifdef S_ISUID
140                 if (sb->st_mode & S_ISUID)
141                         if (file_printf(ms, "%ssetuid", COMMA) == -1)
142                                 return -1;
143 #endif
144 #ifdef S_ISGID
145                 if (sb->st_mode & S_ISGID) 
146                         if (file_printf(ms, "%ssetgid", COMMA) == -1)
147                                 return -1;
148 #endif
149 #ifdef S_ISVTX
150                 if (sb->st_mode & S_ISVTX) 
151                         if (file_printf(ms, "%ssticky", COMMA) == -1)
152                                 return -1;
153 #endif
154         }
155         
156         switch (sb->st_mode & S_IFMT) {
157         case S_IFDIR:
158                 if (mime) {
159                         if (handle_mime(ms, mime, "directory") == -1)
160                                 return -1;
161                 } else if (file_printf(ms, "%sdirectory", COMMA) == -1)
162                         return -1;
163                 break;
164 #ifdef S_IFCHR
165         case S_IFCHR:
166                 /* 
167                  * If -s has been specified, treat character special files
168                  * like ordinary files.  Otherwise, just report that they
169                  * are block special files and go on to the next file.
170                  */
171                 if ((ms->flags & MAGIC_DEVICES) != 0) {
172                         ret = 0;
173                         break;
174                 }
175                 if (mime) {
176                         if (handle_mime(ms, mime, "chardevice") == -1)
177                                 return -1;
178                 } else {
179 #ifdef HAVE_STAT_ST_RDEV
180 # ifdef dv_unit
181                         if (file_printf(ms, "%scharacter special (%d/%d/%d)",
182                             COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev),
183                                         dv_subunit(sb->st_rdev)) == -1)
184                                 return -1;
185 # else
186                         if (file_printf(ms, "%scharacter special (%ld/%ld)",
187                             COMMA, (long)major(sb->st_rdev),
188                             (long)minor(sb->st_rdev)) == -1)
189                                 return -1;
190 # endif
191 #else
192                         if (file_printf(ms, "%scharacter special", COMMA) == -1)
193                                 return -1;
194 #endif
195                 }
196                 break;
197 #endif
198 #ifdef S_IFBLK
199         case S_IFBLK:
200                 /* 
201                  * If -s has been specified, treat block special files
202                  * like ordinary files.  Otherwise, just report that they
203                  * are block special files and go on to the next file.
204                  */
205                 if ((ms->flags & MAGIC_DEVICES) != 0) {
206                         ret = 0;
207                         break;
208                 }
209                 if (mime) {
210                         if (handle_mime(ms, mime, "blockdevice") == -1)
211                                 return -1;
212                 } else {
213 #ifdef HAVE_STAT_ST_RDEV
214 # ifdef dv_unit
215                         if (file_printf(ms, "%sblock special (%d/%d/%d)",
216                             COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev),
217                             dv_subunit(sb->st_rdev)) == -1)
218                                 return -1;
219 # else
220                         if (file_printf(ms, "%sblock special (%ld/%ld)",
221                             COMMA, (long)major(sb->st_rdev),
222                             (long)minor(sb->st_rdev)) == -1)
223                                 return -1;
224 # endif
225 #else
226                         if (file_printf(ms, "%sblock special", COMMA) == -1)
227                                 return -1;
228 #endif
229                 }
230                 break;
231 #endif
232         /* TODO add code to handle V7 MUX and Blit MUX files */
233 #ifdef  S_IFIFO
234         case S_IFIFO:
235                 if((ms->flags & MAGIC_DEVICES) != 0)
236                         break;
237                 if (mime) {
238                         if (handle_mime(ms, mime, "fifo") == -1)
239                                 return -1;
240                 } else if (file_printf(ms, "%sfifo (named pipe)", COMMA) == -1)
241                         return -1;
242                 break;
243 #endif
244 #ifdef  S_IFDOOR
245         case S_IFDOOR:
246                 if (mime) {
247                         if (handle_mime(ms, mime, "door") == -1)
248                                 return -1;
249                 } else if (file_printf(ms, "%sdoor", COMMA) == -1)
250                         return -1;
251                 break;
252 #endif
253 #ifdef  S_IFLNK
254         case S_IFLNK:
255                 if ((nch = readlink(fn, buf, BUFSIZ-1)) <= 0) {
256                         if (ms->flags & MAGIC_ERROR) {
257                             file_error(ms, errno, "unreadable symlink `%s'",
258                                 fn);
259                             return -1;
260                         }
261                         if (mime) {
262                                 if (handle_mime(ms, mime, "symlink") == -1)
263                                         return -1;
264                         } else if (file_printf(ms,
265                             "%sunreadable symlink `%s' (%s)", COMMA, fn,
266                             strerror(errno)) == -1)
267                                 return -1;
268                         break;
269                 }
270                 buf[nch] = '\0';        /* readlink(2) does not do this */
271
272                 /* If broken symlink, say so and quit early. */
273                 if (*buf == '/') {
274                         if (stat(buf, &tstatbuf) < 0)
275                                 return bad_link(ms, errno, buf);
276                 } else {
277                         char *tmp;
278                         char buf2[BUFSIZ+BUFSIZ+4];
279
280                         if ((tmp = strrchr(fn,  '/')) == NULL) {
281                                 tmp = buf; /* in current directory anyway */
282                         } else {
283                                 if (tmp - fn + 1 > BUFSIZ) {
284                                         if (ms->flags & MAGIC_ERROR) {
285                                                 file_error(ms, 0, 
286                                                     "path too long: `%s'", buf);
287                                                 return -1;
288                                         }
289                                         if (mime) {
290                                                 if (handle_mime(ms, mime,
291                                                     "x-path-too-long") == -1)
292                                                         return -1;
293                                         } else if (file_printf(ms,
294                                             "%spath too long: `%s'", COMMA,
295                                             fn) == -1)
296                                                 return -1;
297                                         break;
298                                 }
299                                 /* take dir part */
300                                 (void)strlcpy(buf2, fn, sizeof buf2);
301                                 buf2[tmp - fn + 1] = '\0';
302                                 /* plus (rel) link */
303                                 (void)strlcat(buf2, buf, sizeof buf2);
304                                 tmp = buf2;
305                         }
306                         if (stat(tmp, &tstatbuf) < 0)
307                                 return bad_link(ms, errno, buf);
308                 }
309
310                 /* Otherwise, handle it. */
311                 if ((ms->flags & MAGIC_SYMLINK) != 0) {
312                         const char *p;
313                         ms->flags &= MAGIC_SYMLINK;
314                         p = magic_file(ms, buf);
315                         ms->flags |= MAGIC_SYMLINK;
316                         if (p == NULL)
317                                 return -1;
318                 } else { /* just print what it points to */
319                         if (mime) {
320                                 if (handle_mime(ms, mime, "symlink") == -1)
321                                         return -1;
322                         } else if (file_printf(ms, "%ssymbolic link to `%s'",
323                             COMMA, buf) == -1)
324                                 return -1;
325                 }
326                 break;
327 #endif
328 #ifdef  S_IFSOCK
329 #ifndef __COHERENT__
330         case S_IFSOCK:
331                 if (mime) {
332                         if (handle_mime(ms, mime, "socket") == -1)
333                                 return -1;
334                 } else if (file_printf(ms, "%ssocket", COMMA) == -1)
335                         return -1;
336                 break;
337 #endif
338 #endif
339         case S_IFREG:
340                 /*
341                  * regular file, check next possibility
342                  *
343                  * If stat() tells us the file has zero length, report here that
344                  * the file is empty, so we can skip all the work of opening and
345                  * reading the file.
346                  * But if the -s option has been given, we skip this
347                  * optimization, since on some systems, stat() reports zero
348                  * size for raw disk partitions. (If the block special device
349                  * really has zero length, the fact that it is empty will be
350                  * detected and reported correctly when we read the file.)
351                  */
352                 if ((ms->flags & MAGIC_DEVICES) == 0 && sb->st_size == 0) {
353                         if (mime) {
354                                 if (handle_mime(ms, mime, "x-empty") == -1)
355                                         return -1;
356                         } else if (file_printf(ms, "%sempty", COMMA) == -1)
357                                 return -1;
358                         break;
359                 }
360                 ret = 0;
361                 break;
362
363         default:
364                 file_error(ms, 0, "invalid mode 0%o", sb->st_mode);
365                 return -1;
366                 /*NOTREACHED*/
367         }
368
369         if (!mime && did && ret == 0) {
370             if (file_printf(ms, " ") == -1)
371                     return -1;
372         }
373         return ret;
374 }