ClamAV is an open source (GPLv2) anti-virus toolkit.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
clamav/libclamunrar_iface/unrar_iface.cpp

491 lines
16 KiB

/*
* Interface to libclamunrar
*
* Copyright (C) 2013-2022 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
* Copyright (C) 2007-2013 Sourcefire, Inc.
*
* Authors: Trog, Torok Edvin, Tomasz Kojm, Micah Snyder
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
Add CMake build tooling This patch adds experimental-quality CMake build tooling. The libmspack build required a modification to use "" instead of <> for header #includes. This will hopefully be included in the libmspack upstream project when adding CMake build tooling to libmspack. Removed use of libltdl when using CMake. Flex & Bison are now required to build. If -DMAINTAINER_MODE, then GPERF is also required, though it currently doesn't actually do anything. TODO! I found that the autotools build system was generating the lexer output but not actually compiling it, instead using previously generated (and manually renamed) lexer c source. As a consequence, changes to the .l and .y files weren't making it into the build. To resolve this, I removed generated flex/bison files and fixed the tooling to use the freshly generated files. Flex and bison are now required build tools. On Windows, this adds a dependency on the winflexbison package, which can be obtained using Chocolatey or may be manually installed. CMake tooling only has partial support for building with external LLVM library, and no support for the internal LLVM (to be removed in the future). I.e. The CMake build currently only supports the bytecode interpreter. Many files used include paths relative to the top source directory or relative to the current project, rather than relative to each build target. Modern CMake support requires including internal dependency headers the same way you would external dependency headers (albeit with "" instead of <>). This meant correcting all header includes to be relative to the build targets and not relative to the workspace. For example, ... ```c include "../libclamav/clamav.h" include "clamd/clamd_others.h" ``` ... becomes: ```c // libclamav include "clamav.h" // clamd include "clamd_others.h" ``` Fixes header name conflicts by renaming a few of the files. Converted the "shared" code into a static library, which depends on libclamav. The ironically named "shared" static library provides features common to the ClamAV apps which are not required in libclamav itself and are not intended for use by downstream projects. This change was required for correct modern CMake practices but was also required to use the automake "subdir-objects" option. This eliminates warnings when running autoreconf which, in the next version of autoconf & automake are likely to break the build. libclamav used to build in multiple stages where an earlier stage is a static library containing utils required by the "shared" code. Linking clamdscan and clamdtop with this libclamav utils static lib allowed these two apps to function without libclamav. While this is nice in theory, the practical gains are minimal and it complicates the build system. As such, the autotools and CMake tooling was simplified for improved maintainability and this feature was thrown out. clamdtop and clamdscan now require libclamav to function. Removed the nopthreads version of the autotools libclamav_internal_utils static library and added pthread linking to a couple apps that may have issues building on some platforms without it, with the intention of removing needless complexity from the source. Kept the regular version of libclamav_internal_utils.la though it is no longer used anywhere but in libclamav. Added an experimental doxygen build option which attempts to build clamav.h and libfreshclam doxygen html docs. The CMake build tooling also may build the example program(s), which isn't a feature in the Autotools build system. Changed C standard to C90+ due to inline linking issues with socket.h when linking libfreshclam.so on Linux. Generate common.rc for win32. Fix tabs/spaces in shared Makefile.am, and remove vestigial ifndef from misc.c. Add CMake files to the automake dist, so users can try the new CMake tooling w/out having to build from a git clone. clamonacc changes: - Renamed FANOTIFY macro to HAVE_SYS_FANOTIFY_H to better match other similar macros. - Added a new clamav-clamonacc.service systemd unit file, based on the work of ChadDevOps & Aaron Brighton. - Added missing clamonacc man page. Updates to clamdscan man page, add missing options. Remove vestigial CL_NOLIBCLAMAV definitions (all apps now use libclamav). Rename Windows mspack.dll to libmspack.dll so all ClamAV-built libraries have the lib-prefix with Visual Studio as with CMake.
5 years ago
#include "rar.hpp"
#include "dll.hpp"
extern "C" {
#include <fcntl.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <wchar.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "unrar_iface.h"
#include "clamav-types.h"
#ifndef MIN
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#endif
#ifndef MAX
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#endif
/* tell compiler about branches that are very rarely taken,
* such as debug paths, and error paths */
#if (__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 2)
#define UNLIKELY(cond) __builtin_expect(!!(cond), 0)
#define LIKELY(cond) __builtin_expect(!!(cond), 1)
#else
#define UNLIKELY(cond) (cond)
#define LIKELY(cond) (cond)
#endif
#define unrar_dbgmsg (!UNLIKELY(unrar_debug)) ? (void)0 : unrar_dbgmsg_internal
#define CMTBUFSIZE (64 * 1024)
int CALLBACK CallbackProc(UINT msg, LPARAM UserData, LPARAM P1, LPARAM P2);
static void unrar_dbgmsg_internal(const char* str, ...)
{
va_list ap;
va_start(ap, str);
vfprintf(stderr, str, ap);
va_end(ap);
}
uint8_t unrar_debug = 0;
/**
* @brief Translate an ERAR_<code> to the appropriate UNRAR_<code>
*
* @param errorCode ERAR_<code>
* @return cl_unrar_error_t UNRAR_OK, UNRAR_ENCRYPTED, or UNRAR_ERR.
*/
static cl_unrar_error_t unrar_retcode(int retcode)
{
cl_unrar_error_t status = UNRAR_ERR;
switch (retcode) {
case ERAR_SUCCESS: {
unrar_dbgmsg("unrar_retcode: Success!\n");
status = UNRAR_OK;
break;
}
case ERAR_END_ARCHIVE: {
unrar_dbgmsg("unrar_retcode: No more files in archive.\n");
status = UNRAR_BREAK;
break;
}
case ERAR_NO_MEMORY: {
unrar_dbgmsg("unrar_retcode: Not enough memory!\n");
status = UNRAR_EMEM;
break;
}
case ERAR_MISSING_PASSWORD: {
unrar_dbgmsg("unrar_retcode: Encrypted file header found in archive.\n");
status = UNRAR_ENCRYPTED;
break;
}
case ERAR_BAD_PASSWORD: {
unrar_dbgmsg("unrar_retcode: Encrypted archive or encrypted file in archive.\n");
status = UNRAR_ENCRYPTED;
break;
}
case ERAR_BAD_DATA: {
unrar_dbgmsg("unrar_retcode: Bad data / File CRC error.\n");
break;
}
case ERAR_UNKNOWN_FORMAT: {
unrar_dbgmsg("unrar_retcode: Unknown archive format.\n");
break;
}
case ERAR_EOPEN: {
unrar_dbgmsg("unrar_retcode: Volume open error.\n");
status = UNRAR_EOPEN;
break;
}
case ERAR_ECREATE: {
unrar_dbgmsg("unrar_retcode: File create error.\n");
break;
}
case ERAR_ECLOSE: {
unrar_dbgmsg("unrar_retcode: File close error.\n");
break;
}
case ERAR_EREAD: {
unrar_dbgmsg("unrar_retcode: Read error.\n");
break;
}
case ERAR_EWRITE: {
unrar_dbgmsg("unrar_retcode: Write error.\n");
break;
}
case ERAR_EREFERENCE: {
unrar_dbgmsg("unrar_retcode: Error attempting to unpack the reference record without its source file.\n");
break;
}
default: {
unrar_dbgmsg("unrar_retcode: Unexpected error code: %d\n", retcode);
}
}
return status;
}
static size_t unrar_strnlen(const char* s, size_t n)
{
size_t i = 0;
for (; (i < n) && s[i] != '\0'; ++i)
;
return i;
}
static char* unrar_strndup(const char* s, size_t n)
{
char* alloc;
size_t len;
if (!s) {
return NULL;
}
len = unrar_strnlen(s, n);
alloc = (char*)malloc(len + 1);
if (!alloc) {
return NULL;
} else
memcpy(alloc, s, len);
alloc[len] = '\0';
return alloc;
}
cl_unrar_error_t unrar_open(const char* filename, void** hArchive, char** comment, uint32_t* comment_size, uint8_t debug_flag)
{
struct RAROpenArchiveDataEx* archiveData = NULL;
HANDLE archiveHandle = NULL;
cl_unrar_error_t status = UNRAR_ERR;
if (NULL == filename || NULL == hArchive || NULL == comment || NULL == comment_size) {
unrar_dbgmsg("unrar_open: Invalid arguments.\n");
goto done;
}
/* Enable debug messages in unrar_iface.cpp */
unrar_debug = debug_flag;
archiveData = (struct RAROpenArchiveDataEx*)calloc(sizeof(struct RAROpenArchiveDataEx), 1);
if (archiveData == NULL) {
unrar_dbgmsg("unrar_open: Not enough memory to allocate main archive header data structure.\n");
status = UNRAR_EMEM;
}
archiveData->ArcName = (char*)filename;
archiveData->OpenMode = RAR_OM_EXTRACT;
archiveData->OpFlags |= ROADOF_KEEPBROKEN;
archiveData->CmtBuf = (char*)calloc(1, CMTBUFSIZE);
if (archiveData->CmtBuf == NULL) {
unrar_dbgmsg("unrar_open: Not enough memory to allocate main archive header comment buffer.\n");
status = UNRAR_EMEM;
}
archiveData->CmtBufSize = CMTBUFSIZE;
if (NULL == (archiveHandle = RAROpenArchiveEx(archiveData))) {
/* Failed to open archive */
unrar_dbgmsg("unrar_open: Failed to open archive: %s\n", filename);
status = unrar_retcode(archiveData->OpenResult);
goto done;
}
switch (archiveData->CmtState) {
case 0: {
unrar_dbgmsg("unrar_open: Comments are not present in this archive.\n");
break;
}
case ERAR_BAD_DATA: {
unrar_dbgmsg("unrar_open: Archive Comments may be broken.\n");
}
case ERAR_SMALL_BUF: {
unrar_dbgmsg("unrar_open: Archive Comments are not present in this file.\n");
}
case 1: {
unrar_dbgmsg("unrar_open: Archive Comments:\n\t %s\n", archiveData->CmtBuf);
break;
}
case ERAR_NO_MEMORY: {
unrar_dbgmsg("unrar_open: Memory error when reading archive comments!\n");
status = UNRAR_EMEM;
break;
}
default: {
unrar_dbgmsg("unrar_open: Unknown archive comment state %u!\n", archiveData->CmtState);
}
}
if (archiveData->CmtSize > 0) {
*comment_size = MIN(archiveData->CmtSize, archiveData->CmtBufSize);
*comment = unrar_strndup(archiveData->CmtBuf, *comment_size);
if (NULL == *comment) {
unrar_dbgmsg("unrar_open: Error duplicating comment buffer.\n");
*comment_size = 0;
status = UNRAR_EMEM;
}
}
unrar_dbgmsg("unrar_open: Volume attribute (archive volume): %s\n", (archiveData->Flags & ROADF_VOLUME) ? "yes" : "no");
unrar_dbgmsg("unrar_open: Archive comment present: %s\n", (archiveData->Flags & ROADF_COMMENT) ? "yes" : "no");
unrar_dbgmsg("unrar_open: Archive lock attribute: %s\n", (archiveData->Flags & ROADF_LOCK) ? "yes" : "no");
unrar_dbgmsg("unrar_open: Solid attribute (solid archive): %s\n", (archiveData->Flags & ROADF_SOLID) ? "yes" : "no");
unrar_dbgmsg("unrar_open: New volume naming scheme ('volname.partN.rar'): %s\n", (archiveData->Flags & ROADF_NEWNUMBERING) ? "yes" : "no");
unrar_dbgmsg("unrar_open: Authenticity information present (obsolete): %s\n", (archiveData->Flags & ROADF_SIGNED) ? "yes" : "no");
unrar_dbgmsg("unrar_open: Recovery record present: %s\n", (archiveData->Flags & ROADF_RECOVERY) ? "yes" : "no");
unrar_dbgmsg("unrar_open: Block headers are encrypted: %s\n", (archiveData->Flags & ROADF_ENCHEADERS) ? "yes" : "no");
unrar_dbgmsg("unrar_open: First volume (set only by RAR 3.0 and later): %s\n", (archiveData->Flags & ROADF_FIRSTVOLUME) ? "yes" : "no");
unrar_dbgmsg("unrar_open: Opened archive: %s\n", filename);
*hArchive = (void*)archiveHandle;
status = UNRAR_OK;
done:
if (NULL != archiveData) {
if (NULL != archiveData->CmtBuf) {
free(archiveData->CmtBuf);
archiveData->CmtBuf = NULL;
}
free(archiveData);
}
return status;
}
/**
* @brief Get file metadata from the next file header.
*
* @param hArchive Handle to the archive we're extracting.
* @param[in,out] file_metadata Pointer to a pre-allocated metadata structure.
* @return cl_unrar_error_t UNRAR_OK if metadata retrieved, UNRAR_BREAK if no more files, UNRAR_ENCRYPTED if header was encrypted, else maybe UNRAR_EMEM or UNRAR_ERR.
*/
cl_unrar_error_t unrar_peek_file_header(void* hArchive, unrar_metadata_t* file_metadata)
{
cl_unrar_error_t status = UNRAR_ERR;
struct RARHeaderDataEx headerData;
int read_header_ret = 0;
wchar_t RedirName[1024];
memset(&headerData, 0, sizeof(struct RARHeaderDataEx));
if (NULL == hArchive || NULL == file_metadata) {
unrar_dbgmsg("unrar_peek_file_header: Invalid arguments.\n");
goto done;
}
memset(file_metadata, 0, sizeof(unrar_metadata_t));
/*
* File header comments are not functional in unrar 5.6.5 and the struct member only exists for backwards compatibility.
* The unrar user manual says to set headerData.CmtBuff = NULL, and headerData.CmtBufSize = 0.
*/
headerData.CmtBuf = NULL;
headerData.CmtBufSize = 0;
headerData.RedirNameSize = 1024 * sizeof(wchar_t);
headerData.RedirName = (wchar_t*)&RedirName;
memset(headerData.RedirName, 0, headerData.RedirNameSize);
read_header_ret = RARReadHeaderEx(hArchive, &headerData);
if (ERAR_SUCCESS != read_header_ret) {
status = unrar_retcode(read_header_ret);
goto done;
}
file_metadata->unpack_size = headerData.UnpSize + ((int64_t)headerData.UnpSizeHigh << 32);
file_metadata->pack_size = headerData.PackSize + ((int64_t)headerData.PackSizeHigh << 32);
file_metadata->filename = unrar_strndup(headerData.FileName, 1024);
file_metadata->crc = headerData.FileCRC;
file_metadata->encrypted = (headerData.Flags & RHDF_ENCRYPTED) ? 1 : 0;
file_metadata->is_dir = (headerData.Flags & RHDF_DIRECTORY) ? 1 : 0;
file_metadata->method = headerData.Method;
unrar_dbgmsg("unrar_peek_file_header: Name: %s\n", headerData.FileName);
unrar_dbgmsg("unrar_peek_file_header: Directory?: %u\n", file_metadata->is_dir);
unrar_dbgmsg("unrar_peek_file_header: Target Dir: %u\n", headerData.DirTarget);
unrar_dbgmsg("unrar_peek_file_header: RAR Version: %u\n", headerData.UnpVer);
unrar_dbgmsg("unrar_peek_file_header: Packed Size: %" PRIu64 "\n", file_metadata->pack_size);
unrar_dbgmsg("unrar_peek_file_header: Unpacked Size: %" PRIu64 "\n", file_metadata->unpack_size);
// PrintTime("mtime",HeaderData.MtimeLow,HeaderData.MtimeHigh);
// PrintTime("ctime",HeaderData.CtimeLow,HeaderData.CtimeHigh);
// PrintTime("atime",HeaderData.AtimeLow,HeaderData.AtimeHigh);
if (headerData.RedirType != 0) {
unrar_dbgmsg("unrar_peek_file_header: link type %d, target %ls\n", headerData.RedirType, headerData.RedirName);
}
status = UNRAR_OK;
done:
if (NULL != headerData.CmtBuf) {
free(headerData.CmtBuf);
headerData.CmtBuf = NULL;
}
return status;
}
cl_unrar_error_t unrar_extract_file(void* hArchive, const char* destPath, char* outputBuffer)
{
cl_unrar_error_t status = UNRAR_ERR;
int process_file_ret = 0;
if (NULL == hArchive || NULL == destPath) {
unrar_dbgmsg("unrar_extract_file: Invalid arguments.\n");
goto done;
}
if (NULL != outputBuffer) {
LPARAM UserData = (LPARAM)outputBuffer;
RARSetCallback(hArchive, CallbackProc, UserData);
}
process_file_ret = RARProcessFile(hArchive, RAR_EXTRACT, NULL, (char*)destPath);
if (ERAR_BAD_DATA == process_file_ret) {
unrar_dbgmsg("unrar_extract_file: Warning: Bad data/Invalid CRC. Attempting to scan anyways...\n");
} else if (ERAR_SUCCESS != process_file_ret) {
status = unrar_retcode(process_file_ret);
goto done;
}
#ifdef _WIN32
unrar_dbgmsg("unrar_extract_file: Extracted file to: %s\n", destPath);
#else
unrar_dbgmsg("unrar_extract_file: Extracted file to: %s\n", destPath);
#endif
status = UNRAR_OK;
done:
return status;
}
cl_unrar_error_t unrar_skip_file(void* hArchive)
{
cl_unrar_error_t status = UNRAR_ERR;
int process_file_ret = 0;
if (NULL == hArchive) {
unrar_dbgmsg("unrar_skip_file: Invalid arguments.\n");
goto done;
}
process_file_ret = RARProcessFile(hArchive, RAR_SKIP, NULL, NULL);
if (ERAR_SUCCESS != process_file_ret) {
status = unrar_retcode(process_file_ret);
goto done;
}
unrar_dbgmsg("unrar_skip_file: File skipped.\n");
status = UNRAR_OK;
done:
return status;
}
void unrar_close(void* hArchive)
{
RARCloseArchive(hArchive);
}
int CALLBACK CallbackProc(UINT msg, LPARAM UserData, LPARAM P1, LPARAM P2)
{
int status = 1; /* -1 to cancel, 1 to continue */
switch (msg) {
case UCM_CHANGEVOLUMEW: {
/* We don't support RAR's split into multiple volumes
* ClamAV is not aware of more than 1 file at a time */
status = -1;
unrar_dbgmsg("CallbackProc: Archive has multiple volumes, but we don't support multiple volumes.\n");
break;
}
case UCM_PROCESSDATA: {
char* UserBuffer = (char*)UserData;
if (UserBuffer == NULL) {
/* No buffer provided, continue with extraction to a temp file. */
status = 1;
unrar_dbgmsg("CallbackProc: Extracting to a new tempfile!\n");
} else {
/* Buffer provided, write to it and cancel extraction to the temp file. */
memcpy(UserBuffer, (char*)P1, P2);
status = -1;
unrar_dbgmsg("CallbackProc: Extracting %lu bytes of data to a provided buffer.\n", P2);
}
break;
}
case UCM_NEEDPASSWORDW: {
/* Let's try an empty password. Probably won't work. */
wchar_t* password_buffer = (wchar_t*)P1;
if (NULL == password_buffer || P2 == 0) {
status = -1;
unrar_dbgmsg("CallbackProc: P1 callback argument is invalid.\n");
break;
}
memset(password_buffer, 0, P2 * sizeof(wchar_t));
status = 1;
unrar_dbgmsg("CallbackProc: Password required, attempting empty password.\n");
break;
}
default: {
/* ... */
unrar_dbgmsg("CallbackProc: Unexpected callback type!\n");
}
}
return status;
}
} /* extern "C" */