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
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  *
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
  *
6d91b5f0
  * Change History:
  * $Log: untar.c,v $
16b89181
  * Revision 1.10  2004/09/20 13:37:44  kojm
  * 0.80rc
  *
7d80d0c1
  * Revision 1.9  2004/09/14 10:29:31  nigelhorne
  * Fix compilation error on AIX and OSF
  *
ea54f8ca
  * Revision 1.8  2004/09/12 23:43:45  kojm
  * return with CL_EFORMAT instead of CL_EDSIG
  *
b96838ed
  * Revision 1.7  2004/09/12 19:51:59  nigelhorne
  * Now builds with --enable-debug
  *
03eaed11
  * Revision 1.6  2004/09/08 16:02:34  nigelhorne
  * fclose on error
  *
9f9ac4ec
  * Revision 1.5  2004/09/06 14:16:48  nigelhorne
  * Added CYGWIN support
  *
4ff385dd
  * Revision 1.4  2004/09/06 08:45:44  nigelhorne
  * Code Tidy
  *
f92f5b94
  * Revision 1.3  2004/09/06 08:34:47  nigelhorne
  * Randomise extracted file names from tar file
  *
a0dfa4f3
  * Revision 1.2  2004/09/05 18:58:21  nigelhorne
  * Extract files completed
  *
6d91b5f0
  * Revision 1.1  2004/09/05 15:28:10  nigelhorne
  * First draft
  *
  */
16b89181
 static	char	const	rcsid[] = "$Id: untar.c,v 1.10 2004/09/20 13:37:44 kojm Exp $";
a0dfa4f3
 
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
 #include <unistd.h>
 #include <sys/stat.h>
f92f5b94
 #include <fcntl.h>
 #include <sys/param.h>        /* for NAME_MAX */
a0dfa4f3
 
 #include "clamav.h"
 #include "others.h"
f92f5b94
 #include "untar.h"
b96838ed
 #include "mbox.h"
f92f5b94
 #include "blob.h"
a0dfa4f3
 
 #define BLOCKSIZE 512
 
9f9ac4ec
 /* Maximum filenames under various systems */
 #ifndef	NAME_MAX	/* e.g. Linux */
 
 #ifdef	MAXNAMELEN	/* e.g. Solaris */
 #define	NAME_MAX	MAXNAMELEN
 #else
 
 #ifdef	FILENAME_MAX	/* e.g. SCO */
 #define	NAME_MAX	FILENAME_MAX
 #endif
 
 #endif
 
 #endif
b96838ed
 
7d80d0c1
 #ifndef	O_BINARY
 #define	O_BINARY	0
 #endif
 
f92f5b94
 static int
 octal(const char *str)
a0dfa4f3
 {
 	int ret = -1;
 
 	sscanf(str, "%o", &ret);
 	return ret;
 }
 
 int
 cli_untar(const char *dir, int desc)
 {
 	int size = 0;
 	int in_block = 0;
f92f5b94
 	char fullname[NAME_MAX + 1];
a0dfa4f3
 	FILE *outfile = (FILE*)0;
 
 	cli_dbgmsg("In untar(%s, %d)\n", dir ? dir : "", desc);
 
 	for(;;) {
 		char block[BLOCKSIZE];
9f9ac4ec
 		const int nread = cli_readn(desc, block, sizeof(block));
a0dfa4f3
 
 		if(!in_block && nread == 0)
 			break;
 
 		if(nread != BLOCKSIZE) {
03eaed11
 			if(outfile)
 				fclose(outfile);
a0dfa4f3
 			cli_errmsg("cli_untar: incomplete block read\n");
 			return CL_EIO;
 		}
 
 		if(!in_block) {
 			char type;
4ff385dd
 			const char *suffix;
f92f5b94
 			size_t suffixLen = 0;
 			int fd, directory;
 			char magic[7], name[101], osize[13];
 
 			if(outfile) {
 				if(fclose(outfile)) {
 					cli_errmsg("cli_untar: cannot close file %s\n",
 					    fullname);
 					return CL_EIO;
 				}
 				outfile = (FILE*)0;
 			}
a0dfa4f3
 
 			if(block[0] == '\0')  /* We're done */
 				break;
 
b96838ed
 			/* Notice assumption that BLOCKSIZE > 262 */
a0dfa4f3
 			strncpy(magic, block+257, 6);
 			magic[6] = '\0';
 			if(strcmp(magic, "ustar ") != 0) {
16b89181
 				cli_dbgmsg("Incorrect magic number in tar header\n");
ea54f8ca
 				return CL_EFORMAT;
a0dfa4f3
 			}
 
 			type = block[156];
 
 			switch(type) {
 				case '0':
 				case '\0':
 					directory = 0;
 					break;
 				case '5':
 					directory = 1;
 					break;
 				default:
 					cli_errmsg("cli_untar: unknown type flag %c\n", type);
4ff385dd
 					return CL_EIO;
a0dfa4f3
 			}
 
 			if(directory)
 				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",
 				    fullname);
 				close(fd);
 				return CL_ETMPFILE;
 			}
 
 			strncpy(osize, block+124, 12);
 			osize[12] = '\0';
 			size = octal(osize);
03eaed11
 			if(size < 0) {
f92f5b94
 				cli_errmsg("Invalid size in tar header\n");
 				fclose(outfile);
ea54f8ca
 				return CL_EFORMAT;
f92f5b94
 			}
a0dfa4f3
 		} else { /* write or continue writing file contents */
 			const int nbytes = size>512? 512:size;
 			const int nwritten = fwrite(block, 1, nbytes, outfile);
 
 			if(nwritten != nbytes) {
 				cli_errmsg("cli_untar: only wrote %d bytes to file %s\n",
 					nwritten, fullname);
 			}
 			size -= nbytes;
 			if (size == 0)
 				in_block = 0;
 		}
 	}
f92f5b94
 	if(outfile)
 		fclose(outfile);
ea54f8ca
 	return 0;
a0dfa4f3
 }