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.102 2017/08/28 13:39:18 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 public int
magic_check(struct magic_set * ms,const char * magicfile)145 magic_check(struct magic_set *ms, const char *magicfile)
146 {
147 	if (ms == NULL)
148 		return -1;
149 	return file_apprentice(ms, magicfile, FILE_CHECK);
150 }
151 
152 public int
magic_list(struct magic_set * ms,const char * magicfile)153 magic_list(struct magic_set *ms, const char *magicfile)
154 {
155 	if (ms == NULL)
156 		return -1;
157 	return file_apprentice(ms, magicfile, FILE_LIST);
158 }
159 
160 private void
close_and_restore(const struct magic_set * ms,const char * name,int fd,const zend_stat_t * sb)161 close_and_restore(const struct magic_set *ms, const char *name, int fd,
162     const zend_stat_t *sb)
163 {
164 	if (fd == STDIN_FILENO || name == NULL)
165 		return;
166 	(void) close(fd);
167 
168 	if ((ms->flags & MAGIC_PRESERVE_ATIME) != 0) {
169 		/*
170 		 * Try to restore access, modification times if read it.
171 		 * This is really *bad* because it will modify the status
172 		 * time of the file... And of course this will affect
173 		 * backup programs
174 		 */
175 #ifdef HAVE_UTIMES
176 		struct timeval  utsbuf[2];
177 		(void)memset(utsbuf, 0, sizeof(utsbuf));
178 		utsbuf[0].tv_sec = sb->st_atime;
179 		utsbuf[1].tv_sec = sb->st_mtime;
180 
181 		(void) utimes(name, utsbuf); /* don't care if loses */
182 #elif defined(HAVE_UTIME_H) || defined(HAVE_SYS_UTIME_H)
183 		struct utimbuf  utbuf;
184 
185 		(void)memset(&utbuf, 0, sizeof(utbuf));
186 		utbuf.actime = sb->st_atime;
187 		utbuf.modtime = sb->st_mtime;
188 		(void) utime(name, &utbuf); /* don't care if loses */
189 #endif
190 	}
191 }
192 
193 
194 /*
195  * find type of descriptor
196  */
197 public const char *
magic_descriptor(struct magic_set * ms,int fd)198 magic_descriptor(struct magic_set *ms, int fd)
199 {
200 	if (ms == NULL)
201 		return NULL;
202 	return file_or_stream(ms, NULL, NULL);
203 }
204 
205 /*
206  * find type of named file
207  */
208 public const char *
magic_file(struct magic_set * ms,const char * inname)209 magic_file(struct magic_set *ms, const char *inname)
210 {
211 	if (ms == NULL)
212 		return NULL;
213 	return file_or_stream(ms, inname, NULL);
214 }
215 
216 public const char *
magic_stream(struct magic_set * ms,php_stream * stream)217 magic_stream(struct magic_set *ms, php_stream *stream)
218 {
219 	if (ms == NULL)
220 		return NULL;
221 	return file_or_stream(ms, NULL, stream);
222 }
223 
224 private const char *
file_or_stream(struct magic_set * ms,const char * inname,php_stream * stream)225 file_or_stream(struct magic_set *ms, const char *inname, php_stream *stream)
226 {
227 	int	rv = -1;
228 	unsigned char *buf;
229 	zend_stat_t   sb;
230 	ssize_t nbytes = 0;	/* number of bytes read from a datafile */
231 	int no_in_stream = 0;
232 
233 	if (file_reset(ms, 1) == -1)
234 		goto out;
235 
236 	if (!inname && !stream) {
237 		return NULL;
238 	}
239 
240 	/*
241 	 * one extra for terminating '\0', and
242 	 * some overlapping space for matches near EOF
243 	 */
244 #define SLOP (1 + sizeof(union VALUETYPE))
245 	if ((buf = CAST(unsigned char *, emalloc(ms->bytes_max + SLOP))) == NULL)
246 		return NULL;
247 
248 	switch (file_fsmagic(ms, inname, &sb, stream)) {
249 	case -1:		/* error */
250 		goto done;
251 	case 0:			/* nothing found */
252 		break;
253 	default:		/* matched it and printed type */
254 		rv = 0;
255 		goto done;
256 	}
257 
258 	errno = 0;
259 
260 	if (!stream && inname) {
261 		no_in_stream = 1;
262 		stream = php_stream_open_wrapper((char *)inname, "rb", REPORT_ERRORS, NULL);
263 	}
264 
265 	if (!stream) {
266 		if (unreadable_info(ms, sb.st_mode, inname) == -1)
267 			goto done;
268 		rv = 0;
269 		goto done;
270 	}
271 
272 #ifdef O_NONBLOCK
273 /* we should be already be in non blocking mode for network socket */
274 #endif
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, inname, buf, (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, 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 	switch (param) {
364 	case MAGIC_PARAM_INDIR_MAX:
365 		ms->indir_max = (uint16_t)*(const size_t *)val;
366 		return 0;
367 	case MAGIC_PARAM_NAME_MAX:
368 		ms->name_max = (uint16_t)*(const size_t *)val;
369 		return 0;
370 	case MAGIC_PARAM_ELF_PHNUM_MAX:
371 		ms->elf_phnum_max = (uint16_t)*(const size_t *)val;
372 		return 0;
373 	case MAGIC_PARAM_ELF_SHNUM_MAX:
374 		ms->elf_shnum_max = (uint16_t)*(const size_t *)val;
375 		return 0;
376 	case MAGIC_PARAM_ELF_NOTES_MAX:
377 		ms->elf_notes_max = (uint16_t)*(const size_t *)val;
378 		return 0;
379 	case MAGIC_PARAM_REGEX_MAX:
380 		ms->elf_notes_max = (uint16_t)*(const size_t *)val;
381 		return 0;
382 	case MAGIC_PARAM_BYTES_MAX:
383 		ms->bytes_max = *(const size_t *)val;
384 		return 0;
385 	default:
386 		errno = EINVAL;
387 		return -1;
388 	}
389 }
390 
391 public int
magic_getparam(struct magic_set * ms,int param,void * val)392 magic_getparam(struct magic_set *ms, int param, void *val)
393 {
394 	switch (param) {
395 	case MAGIC_PARAM_INDIR_MAX:
396 		*(size_t *)val = ms->indir_max;
397 		return 0;
398 	case MAGIC_PARAM_NAME_MAX:
399 		*(size_t *)val = ms->name_max;
400 		return 0;
401 	case MAGIC_PARAM_ELF_PHNUM_MAX:
402 		*(size_t *)val = ms->elf_phnum_max;
403 		return 0;
404 	case MAGIC_PARAM_ELF_SHNUM_MAX:
405 		*(size_t *)val = ms->elf_shnum_max;
406 		return 0;
407 	case MAGIC_PARAM_ELF_NOTES_MAX:
408 		*(size_t *)val = ms->elf_notes_max;
409 		return 0;
410 	case MAGIC_PARAM_REGEX_MAX:
411 		*(size_t *)val = ms->regex_max;
412 		return 0;
413 	case MAGIC_PARAM_BYTES_MAX:
414 		*(size_t *)val = ms->bytes_max;
415 		return 0;
416 	default:
417 		errno = EINVAL;
418 		return -1;
419 	}
420 }
421