xref: /PHP-7.4/ext/fileinfo/libmagic/magic.c (revision 622b10f0)
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.111 2019/05/07 02:27:11 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 #if 0
80 private const char* get_default_magic(void);
81 #endif
82 private const char *file_or_stream(struct magic_set *, const char *, php_stream *);
83 
84 #ifndef	STDIN_FILENO
85 #define	STDIN_FILENO	0
86 #endif
87 
88 public struct magic_set *
magic_open(int flags)89 magic_open(int flags)
90 {
91 	return file_ms_alloc(flags);
92 }
93 
94 private int
unreadable_info(struct magic_set * ms,mode_t md,const char * file)95 unreadable_info(struct magic_set *ms, mode_t md, const char *file)
96 {
97 	if (file) {
98 		/* We cannot open it, but we were able to stat it. */
99 		if (access(file, W_OK) == 0)
100 			if (file_printf(ms, "writable, ") == -1)
101 				return -1;
102 		if (access(file, X_OK) == 0)
103 			if (file_printf(ms, "executable, ") == -1)
104 				return -1;
105 	}
106 	if (S_ISREG(md))
107 		if (file_printf(ms, "regular file, ") == -1)
108 			return -1;
109 	if (file_printf(ms, "no read permission") == -1)
110 		return -1;
111 	return 0;
112 }
113 
114 public void
magic_close(struct magic_set * ms)115 magic_close(struct magic_set *ms)
116 {
117 	if (ms == NULL)
118 		return;
119 	file_ms_free(ms);
120 }
121 
122 /*
123  * load a magic file
124  */
125 public int
magic_load(struct magic_set * ms,const char * magicfile)126 magic_load(struct magic_set *ms, const char *magicfile)
127 {
128 	if (ms == NULL)
129 		return -1;
130 	return file_apprentice(ms, magicfile, FILE_LOAD);
131 }
132 
133 public int
magic_compile(struct magic_set * ms,const char * magicfile)134 magic_compile(struct magic_set *ms, const char *magicfile)
135 {
136 	if (ms == NULL)
137 		return -1;
138 	return file_apprentice(ms, magicfile, FILE_COMPILE);
139 }
140 
141 public int
magic_check(struct magic_set * ms,const char * magicfile)142 magic_check(struct magic_set *ms, const char *magicfile)
143 {
144 	if (ms == NULL)
145 		return -1;
146 	return file_apprentice(ms, magicfile, FILE_CHECK);
147 }
148 
149 public int
magic_list(struct magic_set * ms,const char * magicfile)150 magic_list(struct magic_set *ms, const char *magicfile)
151 {
152 	if (ms == NULL)
153 		return -1;
154 	return file_apprentice(ms, magicfile, FILE_LIST);
155 }
156 
157 #if 0
158 private void
159 close_and_restore(const struct magic_set *ms, const char *name, int fd,
160     const zend_stat_t *sb)
161 {
162 	if (fd == STDIN_FILENO || name == NULL)
163 		return;
164 	(void) close(fd);
165 
166 	if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
167 		/*
168 		 * Try to restore access, modification times if read it.
169 		 * This is really *bad* because it will modify the status
170 		 * time of the file... And of course this will affect
171 		 * backup programs
172 		 */
173 #ifdef HAVE_UTIMES
174 		struct timeval  utsbuf[2];
175 		(void)memset(utsbuf, 0, sizeof(utsbuf));
176 		utsbuf[0].tv_sec = sb->st_atime;
177 		utsbuf[1].tv_sec = sb->st_mtime;
178 
179 		(void) utimes(name, utsbuf); /* don't care if loses */
180 #elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
181 		struct utimbuf  utbuf;
182 
183 		(void)memset(&utbuf, 0, sizeof(utbuf));
184 		utbuf.actime = sb->st_atime;
185 		utbuf.modtime = sb->st_mtime;
186 		(void) utime(name, &utbuf); /* don't care if loses */
187 #endif
188 	}
189 }
190 #endif
191 
192 
193 /*
194  * find type of descriptor
195  */
196 public const char *
magic_descriptor(struct magic_set * ms,int fd)197 magic_descriptor(struct magic_set *ms, int fd)
198 {
199 	if (ms == NULL)
200 		return NULL;
201 	return file_or_stream(ms, NULL, NULL);
202 }
203 
204 /*
205  * find type of named file
206  */
207 public const char *
magic_file(struct magic_set * ms,const char * inname)208 magic_file(struct magic_set *ms, const char *inname)
209 {
210 	if (ms == NULL)
211 		return NULL;
212 	return file_or_stream(ms, inname, NULL);
213 }
214 
215 public const char *
magic_stream(struct magic_set * ms,php_stream * stream)216 magic_stream(struct magic_set *ms, php_stream *stream)
217 {
218 	if (ms == NULL)
219 		return NULL;
220 	return file_or_stream(ms, NULL, stream);
221 }
222 
223 private const char *
file_or_stream(struct magic_set * ms,const char * inname,php_stream * stream)224 file_or_stream(struct magic_set *ms, const char *inname, php_stream *stream)
225 {
226 	int	rv = -1;
227 	unsigned char *buf;
228 	zend_stat_t   sb;
229 	ssize_t nbytes = 0;	/* number of bytes read from a datafile */
230 	int no_in_stream = 0;
231 
232 	if (file_reset(ms, 1) == -1)
233 		goto out;
234 
235 	/*
236 	 * one extra for terminating '\0', and
237 	 * some overlapping space for matches near EOF
238 	 */
239 #define SLOP (1 + sizeof(union VALUETYPE))
240 	if ((buf = CAST(unsigned char *, emalloc(ms->bytes_max + SLOP))) == NULL)
241 		return NULL;
242 
243 	switch (file_fsmagic(ms, inname, &sb)) {
244 	case -1:		/* error */
245 		goto done;
246 	case 0:			/* nothing found */
247 		break;
248 	default:		/* matched it and printed type */
249 		rv = 0;
250 		goto done;
251 	}
252 
253 	errno = 0;
254 
255 	if (inname && !stream) {
256 		no_in_stream = 1;
257 		stream = php_stream_open_wrapper((char *)inname, "rb", REPORT_ERRORS, NULL);
258 		if (!stream) {
259 			if (unreadable_info(ms, sb.st_mode, inname) == -1)
260 				goto done;
261 			rv = -1;
262 			goto done;
263 		}
264 	}
265 
266 	php_stream_statbuf ssb;
267 	if (php_stream_stat(stream, &ssb) < 0) {
268 		if (ms->flags & MAGIC_ERROR) {
269 			file_error(ms, errno, "cannot stat `%s'", inname);
270 			rv = -1;
271 			goto done;
272 		}
273 	}
274 	memcpy(&sb, &ssb.sb, sizeof(zend_stat_t));
275 
276 	/*
277 	 * try looking at the first ms->bytes_max bytes
278 	 */
279 	if ((nbytes = php_stream_read(stream, (char *)buf, ms->bytes_max - nbytes)) < 0) {
280 		file_error(ms, errno, "cannot read `%s'", inname);
281 		goto done;
282 	}
283 
284 	(void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */
285 	if (file_buffer(ms, stream, &sb, inname, buf, CAST(size_t, nbytes)) == -1)
286 		goto done;
287 	rv = 0;
288 done:
289 	efree(buf);
290 
291 	if (no_in_stream && stream) {
292 		php_stream_close(stream);
293 	}
294 out:
295 	return rv == 0 ? file_getbuffer(ms) : NULL;
296 }
297 
298 
299 public const char *
magic_buffer(struct magic_set * ms,const void * buf,size_t nb)300 magic_buffer(struct magic_set *ms, const void *buf, size_t nb)
301 {
302 	if (ms == NULL)
303 		return NULL;
304 	if (file_reset(ms, 1) == -1)
305 		return NULL;
306 	/*
307 	 * The main work is done here!
308 	 * We have the file name and/or the data buffer to be identified.
309 	 */
310 	if (file_buffer(ms, NULL, NULL, NULL, buf, nb) == -1) {
311 		return NULL;
312 	}
313 	return file_getbuffer(ms);
314 }
315 
316 public const char *
magic_error(struct magic_set * ms)317 magic_error(struct magic_set *ms)
318 {
319 	if (ms == NULL)
320 		return "Magic database is not open";
321 	return (ms->event_flags & EVENT_HAD_ERR) ? ms->o.buf : NULL;
322 }
323 
324 public int
magic_errno(struct magic_set * ms)325 magic_errno(struct magic_set *ms)
326 {
327 	if (ms == NULL)
328 		return EINVAL;
329 	return (ms->event_flags & EVENT_HAD_ERR) ? ms->error : 0;
330 }
331 
332 public int
magic_getflags(struct magic_set * ms)333 magic_getflags(struct magic_set *ms)
334 {
335 	if (ms == NULL)
336 		return -1;
337 
338 	return ms->flags;
339 }
340 
341 public int
magic_setflags(struct magic_set * ms,int flags)342 magic_setflags(struct magic_set *ms, int flags)
343 {
344 	if (ms == NULL)
345 		return -1;
346 #if !defined(HAVE_UTIME) && !defined(HAVE_UTIMES)
347 	if (flags & MAGIC_PRESERVE_ATIME)
348 		return -1;
349 #endif
350 	ms->flags = flags;
351 	return 0;
352 }
353 
354 public int
magic_version(void)355 magic_version(void)
356 {
357 	return MAGIC_VERSION;
358 }
359 
360 public int
magic_setparam(struct magic_set * ms,int param,const void * val)361 magic_setparam(struct magic_set *ms, int param, const void *val)
362 {
363 	if (ms == NULL)
364 		return -1;
365 	switch (param) {
366 	case MAGIC_PARAM_INDIR_MAX:
367 		ms->indir_max = CAST(uint16_t, *CAST(const size_t *, val));
368 		return 0;
369 	case MAGIC_PARAM_NAME_MAX:
370 		ms->name_max = CAST(uint16_t, *CAST(const size_t *, val));
371 		return 0;
372 	case MAGIC_PARAM_ELF_PHNUM_MAX:
373 		ms->elf_phnum_max = CAST(uint16_t, *CAST(const size_t *, val));
374 		return 0;
375 	case MAGIC_PARAM_ELF_SHNUM_MAX:
376 		ms->elf_shnum_max = CAST(uint16_t, *CAST(const size_t *, val));
377 		return 0;
378 	case MAGIC_PARAM_ELF_NOTES_MAX:
379 		ms->elf_notes_max = CAST(uint16_t, *CAST(const size_t *, val));
380 		return 0;
381 	case MAGIC_PARAM_REGEX_MAX:
382 		ms->regex_max = CAST(uint16_t, *CAST(const size_t *, val));
383 		return 0;
384 	case MAGIC_PARAM_BYTES_MAX:
385 		ms->bytes_max = *CAST(const size_t *, val);
386 		return 0;
387 	default:
388 		errno = EINVAL;
389 		return -1;
390 	}
391 }
392 
393 public int
magic_getparam(struct magic_set * ms,int param,void * val)394 magic_getparam(struct magic_set *ms, int param, void *val)
395 {
396 	if (ms == NULL)
397 		return -1;
398 	switch (param) {
399 	case MAGIC_PARAM_INDIR_MAX:
400 		*CAST(size_t *, val) = ms->indir_max;
401 		return 0;
402 	case MAGIC_PARAM_NAME_MAX:
403 		*CAST(size_t *, val) = ms->name_max;
404 		return 0;
405 	case MAGIC_PARAM_ELF_PHNUM_MAX:
406 		*CAST(size_t *, val) = ms->elf_phnum_max;
407 		return 0;
408 	case MAGIC_PARAM_ELF_SHNUM_MAX:
409 		*CAST(size_t *, val) = ms->elf_shnum_max;
410 		return 0;
411 	case MAGIC_PARAM_ELF_NOTES_MAX:
412 		*CAST(size_t *, val) = ms->elf_notes_max;
413 		return 0;
414 	case MAGIC_PARAM_REGEX_MAX:
415 		*CAST(size_t *, val) = ms->regex_max;
416 		return 0;
417 	case MAGIC_PARAM_BYTES_MAX:
418 		*CAST(size_t *, val) = ms->bytes_max;
419 		return 0;
420 	default:
421 		errno = EINVAL;
422 		return -1;
423 	}
424 }
425