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