xref: /PHP-8.1/ext/fileinfo/libmagic/magic.c (revision 3b9173dc)
1 /*
2  * Copyright (c) Christos Zoulas 2003.
3  * All Rights Reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice immediately at the beginning of the file, without modification,
10  *    this list of conditions, and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include "file.h"
29 
30 #ifndef	lint
31 FILE_RCSID("@(#)$File: magic.c,v 1.114 2021/02/05 21:33:49 christos Exp $")
32 #endif	/* lint */
33 
34 #include "magic.h"
35 
36 #include <stdlib.h>
37 #ifdef PHP_WIN32
38 #include "win32/unistd.h"
39 #else
40 #include <unistd.h>
41 #endif
42 #include <string.h>
43 #include "config.h"
44 
45 #ifdef PHP_WIN32
46 #include <shlwapi.h>
47 #endif
48 #include <limits.h>	/* for PIPE_BUF */
49 
50 #if defined(HAVE_UTIMES)
51 # include <sys/time.h>
52 #elif defined(HAVE_UTIME)
53 # if defined(HAVE_SYS_UTIME_H)
54 #  include <sys/utime.h>
55 # elif defined(HAVE_UTIME_H)
56 #  include <utime.h>
57 # endif
58 #endif
59 
60 #ifdef HAVE_UNISTD_H
61 #include <unistd.h>	/* for read() */
62 #endif
63 
64 #ifndef PIPE_BUF
65 /* Get the PIPE_BUF from pathconf */
66 #ifdef _PC_PIPE_BUF
67 #define PIPE_BUF pathconf(".", _PC_PIPE_BUF)
68 #else
69 #define PIPE_BUF 512
70 #endif
71 #endif
72 
73 #ifdef PHP_WIN32
74 # undef S_IFLNK
75 # undef S_IFIFO
76 #endif
77 
78 private int unreadable_info(struct magic_set *, mode_t, const char *);
79 private const char *file_or_stream(struct magic_set *, const char *, php_stream *);
80 
81 #ifndef	STDIN_FILENO
82 #define	STDIN_FILENO	0
83 #endif
84 
85 public struct magic_set *
magic_open(int flags)86 magic_open(int flags)
87 {
88 	return file_ms_alloc(flags);
89 }
90 
91 private int
unreadable_info(struct magic_set * ms,mode_t md,const char * file)92 unreadable_info(struct magic_set *ms, mode_t md, const char *file)
93 {
94 	if (file) {
95 		/* We cannot open it, but we were able to stat it. */
96 		if (access(file, W_OK) == 0)
97 			if (file_printf(ms, "writable, ") == -1)
98 				return -1;
99 		if (access(file, X_OK) == 0)
100 			if (file_printf(ms, "executable, ") == -1)
101 				return -1;
102 	}
103 	if (S_ISREG(md))
104 		if (file_printf(ms, "regular file, ") == -1)
105 			return -1;
106 	if (file_printf(ms, "no read permission") == -1)
107 		return -1;
108 	return 0;
109 }
110 
111 public void
magic_close(struct magic_set * ms)112 magic_close(struct magic_set *ms)
113 {
114 	if (ms == NULL)
115 		return;
116 	file_ms_free(ms);
117 }
118 
119 /*
120  * load a magic file
121  */
122 public int
magic_load(struct magic_set * ms,const char * magicfile)123 magic_load(struct magic_set *ms, const char *magicfile)
124 {
125 	if (ms == NULL)
126 		return -1;
127 	return file_apprentice(ms, magicfile, FILE_LOAD);
128 }
129 
130 public int
magic_compile(struct magic_set * ms,const char * magicfile)131 magic_compile(struct magic_set *ms, const char *magicfile)
132 {
133 	if (ms == NULL)
134 		return -1;
135 	return file_apprentice(ms, magicfile, FILE_COMPILE);
136 }
137 
138 public int
magic_check(struct magic_set * ms,const char * magicfile)139 magic_check(struct magic_set *ms, const char *magicfile)
140 {
141 	if (ms == NULL)
142 		return -1;
143 	return file_apprentice(ms, magicfile, FILE_CHECK);
144 }
145 
146 public int
magic_list(struct magic_set * ms,const char * magicfile)147 magic_list(struct magic_set *ms, const char *magicfile)
148 {
149 	if (ms == NULL)
150 		return -1;
151 	return file_apprentice(ms, magicfile, FILE_LIST);
152 }
153 
154 #ifndef COMPILE_ONLY
155 
156 /*
157  * find type of descriptor
158  */
159 public const char *
magic_descriptor(struct magic_set * ms,int fd)160 magic_descriptor(struct magic_set *ms, int fd)
161 {
162 	if (ms == NULL)
163 		return NULL;
164 	return file_or_stream(ms, NULL, NULL);
165 }
166 
167 /*
168  * find type of named file
169  */
170 public const char *
magic_file(struct magic_set * ms,const char * inname)171 magic_file(struct magic_set *ms, const char *inname)
172 {
173 	if (ms == NULL)
174 		return NULL;
175 	return file_or_stream(ms, inname, NULL);
176 }
177 
178 public const char *
magic_stream(struct magic_set * ms,php_stream * stream)179 magic_stream(struct magic_set *ms, php_stream *stream)
180 {
181 	if (ms == NULL)
182 		return NULL;
183 	return file_or_stream(ms, NULL, stream);
184 }
185 
186 private const char *
file_or_stream(struct magic_set * ms,const char * inname,php_stream * stream)187 file_or_stream(struct magic_set *ms, const char *inname, php_stream *stream)
188 {
189 	int	rv = -1;
190 	unsigned char *buf;
191 	zend_stat_t   sb;
192 	ssize_t nbytes = 0;	/* number of bytes read from a datafile */
193 	int no_in_stream = 0;
194 
195 	if (file_reset(ms, 1) == -1)
196 		goto out;
197 
198 	/*
199 	 * one extra for terminating '\0', and
200 	 * some overlapping space for matches near EOF
201 	 */
202 #define SLOP (1 + sizeof(union VALUETYPE))
203 	if ((buf = CAST(unsigned char *, emalloc(ms->bytes_max + SLOP))) == NULL)
204 		return NULL;
205 
206 	switch (file_fsmagic(ms, inname, &sb)) {
207 	case -1:		/* error */
208 		goto done;
209 	case 0:			/* nothing found */
210 		break;
211 	default:		/* matched it and printed type */
212 		rv = 0;
213 		goto done;
214 	}
215 
216 	errno = 0;
217 
218 	if (inname && !stream) {
219 		no_in_stream = 1;
220 		stream = php_stream_open_wrapper((char *)inname, "rb", REPORT_ERRORS, NULL);
221 		if (!stream) {
222 			if (unreadable_info(ms, sb.st_mode, inname) == -1)
223 				goto done;
224 			rv = -1;
225 			goto done;
226 		}
227 	}
228 
229 	php_stream_statbuf ssb;
230 	if (php_stream_stat(stream, &ssb) < 0) {
231 		if (ms->flags & MAGIC_ERROR) {
232 			file_error(ms, errno, "cannot stat `%s'", inname);
233 			rv = -1;
234 			goto done;
235 		}
236 	}
237 	memcpy(&sb, &ssb.sb, sizeof(zend_stat_t));
238 
239 	/*
240 	 * try looking at the first ms->bytes_max bytes
241 	 */
242 	if ((nbytes = php_stream_read(stream, (char *)buf, ms->bytes_max - nbytes)) < 0) {
243 		file_error(ms, errno, "cannot read `%s'", inname);
244 		goto done;
245 	}
246 
247 	(void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
248 	if (file_buffer(ms, stream, &sb, inname, buf, CAST(size_t, nbytes)) == -1)
249 		goto done;
250 	rv = 0;
251 done:
252 	efree(buf);
253 
254 	if (no_in_stream && stream) {
255 		php_stream_close(stream);
256 	}
257 out:
258 	return rv == 0 ? file_getbuffer(ms) : NULL;
259 }
260 
261 
262 public const char *
magic_buffer(struct magic_set * ms,const void * buf,size_t nb)263 magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
264 {
265 	if (ms == NULL)
266 		return NULL;
267 	if (file_reset(ms, 1) == -1)
268 		return NULL;
269 	/*
270 	 * The main work is done here!
271 	 * We have the file name and/or the data buffer to be identified.
272 	 */
273 	if (file_buffer(ms, NULL, NULL, NULL, buf, nb) == -1) {
274 		return NULL;
275 	}
276 	return file_getbuffer(ms);
277 }
278 #endif
279 
280 public const char *
magic_error(struct magic_set * ms)281 magic_error(struct magic_set *ms)
282 {
283 	if (ms == NULL)
284 		return "Magic database is not open";
285 	return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
286 }
287 
288 public int
magic_errno(struct magic_set * ms)289 magic_errno(struct magic_set *ms)
290 {
291 	if (ms == NULL)
292 		return EINVAL;
293 	return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
294 }
295 
296 public int
magic_getflags(struct magic_set * ms)297 magic_getflags(struct magic_set *ms)
298 {
299 	if (ms == NULL)
300 		return -1;
301 
302 	return ms->flags;
303 }
304 
305 public int
magic_setflags(struct magic_set * ms,int flags)306 magic_setflags(struct magic_set *ms, int flags)
307 {
308 	if (ms == NULL)
309 		return -1;
310 #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
311 	if (flags & MAGIC_PRESERVE_ATIME)
312 		return -1;
313 #endif
314 	ms->flags = flags;
315 	return 0;
316 }
317 
318 public int
magic_version(void)319 magic_version(void)
320 {
321 	return MAGIC_VERSION;
322 }
323 
324 public int
magic_setparam(struct magic_set * ms,int param,const void * val)325 magic_setparam(struct magic_set *ms, int param, const void *val)
326 {
327 	if (ms == NULL)
328 		return -1;
329 	switch (param) {
330 	case MAGIC_PARAM_INDIR_MAX:
331 		ms->indir_max = CAST(uint16_t, *CAST(const size_t *, val));
332 		return 0;
333 	case MAGIC_PARAM_NAME_MAX:
334 		ms->name_max = CAST(uint16_t, *CAST(const size_t *, val));
335 		return 0;
336 	case MAGIC_PARAM_ELF_PHNUM_MAX:
337 		ms->elf_phnum_max = CAST(uint16_t, *CAST(const size_t *, val));
338 		return 0;
339 	case MAGIC_PARAM_ELF_SHNUM_MAX:
340 		ms->elf_shnum_max = CAST(uint16_t, *CAST(const size_t *, val));
341 		return 0;
342 	case MAGIC_PARAM_ELF_NOTES_MAX:
343 		ms->elf_notes_max = CAST(uint16_t, *CAST(const size_t *, val));
344 		return 0;
345 	case MAGIC_PARAM_REGEX_MAX:
346 		ms->regex_max = CAST(uint16_t, *CAST(const size_t *, val));
347 		return 0;
348 	case MAGIC_PARAM_BYTES_MAX:
349 		ms->bytes_max = *CAST(const size_t *, val);
350 		return 0;
351 	case MAGIC_PARAM_ENCODING_MAX:
352 		ms->encoding_max = *CAST(const size_t *, val);
353 		return 0;
354 	default:
355 		errno = EINVAL;
356 		return -1;
357 	}
358 }
359 
360 public int
magic_getparam(struct magic_set * ms,int param,void * val)361 magic_getparam(struct magic_set *ms, int param, void *val)
362 {
363 	if (ms == NULL)
364 		return -1;
365 	switch (param) {
366 	case MAGIC_PARAM_INDIR_MAX:
367 		*CAST(size_t *, val) = ms->indir_max;
368 		return 0;
369 	case MAGIC_PARAM_NAME_MAX:
370 		*CAST(size_t *, val) = ms->name_max;
371 		return 0;
372 	case MAGIC_PARAM_ELF_PHNUM_MAX:
373 		*CAST(size_t *, val) = ms->elf_phnum_max;
374 		return 0;
375 	case MAGIC_PARAM_ELF_SHNUM_MAX:
376 		*CAST(size_t *, val) = ms->elf_shnum_max;
377 		return 0;
378 	case MAGIC_PARAM_ELF_NOTES_MAX:
379 		*CAST(size_t *, val) = ms->elf_notes_max;
380 		return 0;
381 	case MAGIC_PARAM_REGEX_MAX:
382 		*CAST(size_t *, val) = ms->regex_max;
383 		return 0;
384 	case MAGIC_PARAM_BYTES_MAX:
385 		*CAST(size_t *, val) = ms->bytes_max;
386 		return 0;
387 	case MAGIC_PARAM_ENCODING_MAX:
388 		*CAST(size_t *, val) = ms->encoding_max;
389 		return 0;
390 	default:
391 		errno = EINVAL;
392 		return -1;
393 	}
394 }
395