libclamav/untar.c
6d91b5f0
 /*
  *  Copyright (C) 2004 Nigel Horne <njh@bandsman.co.uk>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation; either version 2 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
30738099
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
6d91b5f0
  *
f92f5b94
  * Much of this code is based on minitar.c which is in the public domain.
a0dfa4f3
  * Author: Charles G. Waldman (cgw@pgt.com),  Aug 4 1998
f49103d6
  * There are many tar files that this code cannot decode.
6d91b5f0
  */
257a649a
 static	char	const	rcsid[] = "$Id: untar.c,v 1.32 2006/10/15 11:10:42 njh Exp $";
 
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
a0dfa4f3
 
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
c522ec92
 #ifdef	HAVE_UNISTD_H
a0dfa4f3
 #include <unistd.h>
c522ec92
 #endif
a0dfa4f3
 #include <sys/stat.h>
f92f5b94
 #include <fcntl.h>
c522ec92
 #ifdef	HAVE_SYS_PARAM_H
5eeffbb9
 #include <sys/param.h>	/* for NAME_MAX */
c522ec92
 #endif
a0dfa4f3
 
 #include "clamav.h"
 #include "others.h"
f92f5b94
 #include "untar.h"
b96838ed
 #include "mbox.h"
f92f5b94
 #include "blob.h"
a0dfa4f3
 
 #define BLOCKSIZE 512
 
7d80d0c1
 #ifndef	O_BINARY
 #define	O_BINARY	0
 #endif
 
f92f5b94
 static int
 octal(const char *str)
a0dfa4f3
 {
fc2d81bd
 	int ret;
a0dfa4f3
 
fc2d81bd
 	if(sscanf(str, "%o", (unsigned int *)&ret) != 1)
 		return -1;
a0dfa4f3
 	return ret;
 }
 
 int
255e314c
 cli_untar(const char *dir, int desc, unsigned int posix)
a0dfa4f3
 {
 	int size = 0;
 	int in_block = 0;
f92f5b94
 	char fullname[NAME_MAX + 1];
7e91785c
 	FILE *outfile = NULL;
a0dfa4f3
 
 	cli_dbgmsg("In untar(%s, %d)\n", dir ? dir : "", desc);
 
 	for(;;) {
 		char block[BLOCKSIZE];
f4ff13a5
 		const int nread = cli_readn(desc, block, (unsigned int)sizeof(block));
a0dfa4f3
 
 		if(!in_block && nread == 0)
 			break;
 
7e91785c
 		if(nread < 0) {
03eaed11
 			if(outfile)
 				fclose(outfile);
7e91785c
 			cli_errmsg("cli_untar: block read error\n");
a0dfa4f3
 			return CL_EIO;
 		}
 
 		if(!in_block) {
 			char type;
4ff385dd
 			const char *suffix;
f92f5b94
 			size_t suffixLen = 0;
ff6911d3
 			int fd, directory, skipEntry = 0;
f92f5b94
 			char magic[7], name[101], osize[13];
 
 			if(outfile) {
 				if(fclose(outfile)) {
 					cli_errmsg("cli_untar: cannot close file %s\n",
c522ec92
 						fullname);
f92f5b94
 					return CL_EIO;
 				}
 				outfile = (FILE*)0;
 			}
a0dfa4f3
 
5eeffbb9
 			if(block[0] == '\0')	/* We're done */
a0dfa4f3
 				break;
 
b96838ed
 			/* Notice assumption that BLOCKSIZE > 262 */
255e314c
 			if(posix) {
 				strncpy(magic, block+257, 5);
 				magic[5] = '\0';
 				if(strcmp(magic, "ustar") != 0) {
 					cli_dbgmsg("Incorrect magic string '%s' in tar header\n", magic);
 					return CL_EFORMAT;
 				}
a0dfa4f3
 			}
 
 			type = block[156];
 
f49103d6
 			/*
 			 * Extra types from djgardner@users.sourceforge.net
 			 */
a0dfa4f3
 			switch(type) {
f49103d6
 				case '0':	/* plain file */
 				case '\0':	/* plain file */
 				case '7':	/* contiguous file */
a0dfa4f3
 					directory = 0;
 					break;
0f750119
 				case '1':	/* Link to already archived file */
f49103d6
 				case '5':	/* directory */
 				case '2':	/* sym link */
 				case '3':	/* char device */
 				case '4':	/* block device */
 				case '6':	/* fifo special */
ff6911d3
 				case 'V':	/* Volume header */
a0dfa4f3
 					directory = 1;
 					break;
ff6911d3
 				case 'K':
 				case 'L':
 					/* GNU extension - ././@LongLink
 					 * Discard the blocks with the extended filename,
 					 * the last header will contain parts of it anyway
 					 */
 					directory = 0;
 					skipEntry = 1;
 					break;
a0dfa4f3
 				default:
ab537286
 					/*cli_errmsg("cli_untar: unknown type flag %c\n", type);
 					return CL_EFORMAT;*/
 					/*
 					 * It isn't really a tar file
 					 */
 					cli_dbgmsg("cli_untar: unknown type flag %c\n", type);
 					/*
 					 * We don't know that it's clean at all,
 					 * it would be better to have a
 					 * CL_CONTINUE return value since it
 					 * may be a different format
 					 */
 					return CL_CLEAN;
a0dfa4f3
 			}
 
f49103d6
 			if(directory) {
 				in_block = 0;
a0dfa4f3
 				continue;
f49103d6
 			}
a0dfa4f3
 
ff6911d3
 			strncpy(osize, block+124, 12);
 			osize[12] = '\0';
 			size = octal(osize);
 			if(size < 0) {
 				cli_errmsg("Invalid size in tar header\n");
0a7022c1
 				if(outfile)
 					fclose(outfile);
ff6911d3
 				return CL_EFORMAT;
 			}
 			cli_dbgmsg("cli_untar: size = %d\n", size);
 
 			if(skipEntry) {
 				const int nskip = (size % BLOCKSIZE || !size) ? size + BLOCKSIZE - (size % BLOCKSIZE) : size;
 				cli_dbgmsg("cli_untar: GNU extension, skipping entry\n");
 				lseek(desc, nskip, SEEK_CUR);
 				continue;
 			}
 
f92f5b94
 			strncpy(name, block, 100);
 			name[100] = '\0';
a0dfa4f3
 
f92f5b94
 			/*
 			 * see also fileblobSetFilename()
 			 * TODO: check if the suffix needs to be put back
 			 */
 			sanitiseName(name);
 			suffix = strrchr(name, '.');
 			if(suffix == NULL)
 				suffix = "";
 			else {
 				suffixLen = strlen(suffix);
 				if(suffixLen > 4) {
 					/* Found a full stop which isn't a suffix */
 					suffix = "";
 					suffixLen = 0;
a0dfa4f3
 				}
 			}
f92f5b94
 			snprintf(fullname, sizeof(fullname) - 1 - suffixLen, "%s/%.*sXXXXXX", dir,
 				(int)(sizeof(fullname) - 9 - suffixLen - strlen(dir)), name);
 #if	defined(C_LINUX) || defined(C_BSD) || defined(HAVE_MKSTEMP) || defined(C_SOLARIS) || defined(C_CYGWIN)
 			fd = mkstemp(fullname);
 #else
 			(void)mktemp(fullname);
 			fd = open(fullname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC|O_BINARY, 0600);
 #endif
 
 			if(fd < 0) {
 				cli_errmsg("Can't create temporary file %s: %s\n", fullname, strerror(errno));
 				cli_dbgmsg("%lu %d %d\n", suffixLen, sizeof(fullname), strlen(fullname));
 				return CL_ETMPFILE;
 			}
 
 			cli_dbgmsg("cli_untar: extracting %s\n", fullname);
 
 			in_block = 1;
 			if((outfile = fdopen(fd, "wb")) == NULL) {
 				cli_errmsg("cli_untar: cannot create file %s\n",
c522ec92
 					fullname);
f92f5b94
 				close(fd);
 				return CL_ETMPFILE;
 			}
a0dfa4f3
 		} else { /* write or continue writing file contents */
 			const int nbytes = size>512? 512:size;
c522ec92
 			const int nwritten = (int)fwrite(block, 1, (size_t)nbytes, outfile);
a0dfa4f3
 
 			if(nwritten != nbytes) {
b7c43caa
 				cli_errmsg("cli_untar: only wrote %d bytes to file %s (out of disc space?)\n",
a0dfa4f3
 					nwritten, fullname);
0a7022c1
 				if(outfile)
 					fclose(outfile);
e1dce709
 				return CL_EIO;
a0dfa4f3
 			}
 			size -= nbytes;
 		}
77880aed
 		if (size == 0)
 			in_block = 0;
a0dfa4f3
 	}
f92f5b94
 	if(outfile)
ad6b0de4
 		return fclose(outfile);
 
ea54f8ca
 	return 0;
a0dfa4f3
 }