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