xref: /PHP-7.3/ext/fileinfo/libmagic/fsmagic.c (revision 6f24d75b)
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.77 2017/05/24 19:17:50 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 #ifdef WIN32
57 # define WIN32_LEAN_AND_MEAN
58 # include <windows.h>
59 #endif
60 
61 #ifndef HAVE_MAJOR
62 # define major(dev)  (((dev) >> 8) & 0xff)
63 # define minor(dev)  ((dev) & 0xff)
64 #endif
65 #undef HAVE_MAJOR
66 
67 #ifdef PHP_WIN32
68 
69 # undef S_IFIFO
70 #endif
71 private int
handle_mime(struct magic_set * ms,int mime,const char * str)72 handle_mime(struct magic_set *ms, int mime, const char *str)
73 {
74 	if ((mime & MAGIC_MIME_TYPE)) {
75 		if (file_printf(ms, "inode/%s", str) == -1)
76 			return -1;
77 		if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms,
78 		    "; charset=") == -1)
79 			return -1;
80 	}
81 	if ((mime & MAGIC_MIME_ENCODING) && file_printf(ms, "binary") == -1)
82 		return -1;
83 	return 0;
84 }
85 
86 protected int
file_fsmagic(struct magic_set * ms,const char * fn,zend_stat_t * sb,php_stream * stream)87 file_fsmagic(struct magic_set *ms, const char *fn, zend_stat_t *sb, php_stream *stream)
88 {
89 	int ret, did = 0;
90 	int mime = ms->flags & MAGIC_MIME;
91 	int silent = ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION);
92 
93 	if (ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))
94 		return 0;
95 
96 	if (fn == NULL && !stream) {
97 		return 0;
98 	}
99 
100 #define COMMA	(did++ ? ", " : "")
101 
102 	if (stream) {
103 		php_stream_statbuf ssb;
104 		if (php_stream_stat(stream, &ssb) < 0) {
105 			if (ms->flags & MAGIC_ERROR) {
106 				file_error(ms, errno, "cannot stat `%s'", fn);
107 				return -1;
108 			}
109 			return 0;
110 		}
111 		memcpy(sb, &ssb.sb, sizeof(struct stat));
112 	} else {
113 		if (php_sys_stat(fn, sb) != 0) {
114 			if (ms->flags & MAGIC_ERROR) {
115 				file_error(ms, errno, "cannot stat `%s'", fn);
116 				return -1;
117 			}
118 			return 0;
119 		}
120 	}
121 
122 	ret = 1;
123 	if (!mime && !silent) {
124 #ifdef S_ISUID
125 		if (sb->st_mode & S_ISUID)
126 			if (file_printf(ms, "%ssetuid", COMMA) == -1)
127 				return -1;
128 #endif
129 #ifdef S_ISGID
130 		if (sb->st_mode & S_ISGID)
131 			if (file_printf(ms, "%ssetgid", COMMA) == -1)
132 				return -1;
133 #endif
134 #ifdef S_ISVTX
135 		if (sb->st_mode & S_ISVTX)
136 			if (file_printf(ms, "%ssticky", COMMA) == -1)
137 				return -1;
138 #endif
139 	}
140 
141 	switch (sb->st_mode & S_IFMT) {
142 #ifndef PHP_WIN32
143 # ifdef S_IFCHR
144 		case S_IFCHR:
145 			/*
146 			 * If -s has been specified, treat character special files
147 			 * like ordinary files.  Otherwise, just report that they
148 			 * are block special files and go on to the next file.
149 			 */
150 			if ((ms->flags & MAGIC_DEVICES) != 0) {
151 				ret = 0;
152 				break;
153 			}
154 			if (mime) {
155 				if (handle_mime(ms, mime, "chardevice") == -1)
156 					return -1;
157 			} else {
158 #  ifdef HAVE_STAT_ST_RDEV
159 #   ifdef dv_unit
160 			if (file_printf(ms, "%scharacter special (%d/%d/%d)",
161 			    COMMA, major(sb->st_rdev), dv_unit(sb->st_rdev),
162 					dv_subunit(sb->st_rdev)) == -1)
163 				return -1;
164 # else
165 			if (file_printf(ms, "%scharacter special (%ld/%ld)",
166 			    COMMA, (long)major(sb->st_rdev),
167 			    (long)minor(sb->st_rdev)) == -1)
168 				return -1;
169 # endif
170 #else
171 			if (file_printf(ms, "%scharacter special", COMMA) == -1)
172 				return -1;
173 #endif
174 	}
175 			return 1;
176 # endif
177 #endif
178 
179 #ifdef	S_IFIFO
180 	case S_IFIFO:
181 		if((ms->flags & MAGIC_DEVICES) != 0)
182 			break;
183 		if (mime) {
184 			if (handle_mime(ms, mime, "fifo") == -1)
185 				return -1;
186 		} else if (file_printf(ms, "%sfifo (named pipe)", COMMA) == -1)
187 			return -1;
188 		break;
189 #endif
190 #ifdef	S_IFDOOR
191 	case S_IFDOOR:
192 		if (mime) {
193 			if (handle_mime(ms, mime, "door") == -1)
194 				return -1;
195 		} else if (file_printf(ms, "%sdoor", COMMA) == -1)
196 			return -1;
197 		break;
198 #endif
199 #ifdef	S_IFLNK
200 	case S_IFLNK:
201 		/* stat is used, if it made here then the link is broken */
202 			if (ms->flags & MAGIC_ERROR) {
203 			    file_error(ms, errno, "unreadable symlink `%s'", fn);
204 			    return -1;
205 			}
206 	return 1;
207 #endif
208 
209 #ifdef	S_IFSOCK
210 #ifndef __COHERENT__
211 	case S_IFSOCK:
212 		if (mime) {
213 			if (handle_mime(ms, mime, "socket") == -1)
214 				return -1;
215 		} else if (silent) {
216 		} else if (file_printf(ms, "%ssocket", COMMA) == -1)
217 			return -1;
218 		break;
219 #endif
220 #endif
221 	case S_IFREG:
222 		/*
223 		 * regular file, check next possibility
224 		 *
225 		 * If stat() tells us the file has zero length, report here that
226 		 * the file is empty, so we can skip all the work of opening and
227 		 * reading the file.
228 		 * But if the -s option has been given, we skip this
229 		 * optimization, since on some systems, stat() reports zero
230 		 * size for raw disk partitions. (If the block special device
231 		 * really has zero length, the fact that it is empty will be
232 		 * detected and reported correctly when we read the file.)
233 		 */
234 		if ((ms->flags & MAGIC_DEVICES) == 0 && sb->st_size == 0) {
235 			if (mime) {
236 				if (handle_mime(ms, mime, "x-empty") == -1)
237 					return -1;
238 			} else if (silent) {
239 			} else if (file_printf(ms, "%sempty", COMMA) == -1)
240 				return -1;
241 			break;
242 		}
243 		ret = 0;
244 		break;
245 
246 	default:
247 		file_error(ms, 0, "invalid mode 0%o", sb->st_mode);
248 		return -1;
249 		/*NOTREACHED*/
250 	}
251 
252 	if (!silent && !mime && did && ret == 0) {
253 	    if (file_printf(ms, " ") == -1)
254 		    return -1;
255 	}
256 	return ret;
257 }
258