|
Packit Service |
569379 |
/*
|
|
Packit Service |
569379 |
* Copyright 2012--2014 Red Hat Inc., Durham, North Carolina.
|
|
Packit Service |
569379 |
* All Rights Reserved.
|
|
Packit Service |
569379 |
*
|
|
Packit Service |
569379 |
* This library is free software; you can redistribute it and/or
|
|
Packit Service |
569379 |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
569379 |
* License as published by the Free Software Foundation; either
|
|
Packit Service |
569379 |
* version 2.1 of the License, or (at your option) any later version.
|
|
Packit Service |
569379 |
*
|
|
Packit Service |
569379 |
* This library is distributed in the hope that it will be useful,
|
|
Packit Service |
569379 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
569379 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
569379 |
* Lesser General Public License for more details.
|
|
Packit Service |
569379 |
*
|
|
Packit Service |
569379 |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
569379 |
* License along with this library; if not, write to the Free Software
|
|
Packit Service |
569379 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
Packit Service |
569379 |
*
|
|
Packit Service |
569379 |
*/
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
#ifdef HAVE_CONFIG_H
|
|
Packit Service |
569379 |
#include <config.h>
|
|
Packit Service |
569379 |
#endif
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
#include <string.h>
|
|
Packit Service |
569379 |
#include <errno.h>
|
|
Packit Service |
569379 |
#ifdef OS_WINDOWS
|
|
Packit Service |
569379 |
#include <io.h>
|
|
Packit Service |
569379 |
#include <direct.h>
|
|
Packit Service |
569379 |
/* The rand_s function requires constant _CRT_RAND_S to be defined before including <stdlib.h>.
|
|
Packit Service |
569379 |
* See https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/rand-s
|
|
Packit Service |
569379 |
*/
|
|
Packit Service |
569379 |
#define _CRT_RAND_S
|
|
Packit Service |
569379 |
#else
|
|
Packit Service |
569379 |
#include <unistd.h>
|
|
Packit Service |
569379 |
#include <ftw.h>
|
|
Packit Service |
569379 |
#endif
|
|
Packit Service |
569379 |
#include <stdlib.h>
|
|
Packit Service |
569379 |
#include <limits.h>
|
|
Packit Service |
569379 |
#include <sys/stat.h>
|
|
Packit Service |
569379 |
#include <fcntl.h>
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
#include <curl/curl.h>
|
|
Packit Service |
569379 |
#include <curl/easy.h>
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
#include "oscap_acquire.h"
|
|
Packit Service |
569379 |
#include "common/util.h"
|
|
Packit Service |
569379 |
#include "common/oscap_buffer.h"
|
|
Packit Service |
569379 |
#include "common/_error.h"
|
|
Packit Service |
569379 |
#include "oscap_string.h"
|
|
Packit Service |
569379 |
#include "oscap_helpers.h"
|
|
Packit Service |
7dc6a6 |
#include "debug_priv.h"
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
#ifndef OSCAP_TEMP_DIR
|
|
Packit Service |
569379 |
#define OSCAP_TEMP_DIR "/tmp"
|
|
Packit Service |
569379 |
#endif
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
#define TEMP_DIR_TEMPLATE OSCAP_TEMP_DIR "/oscap.XXXXXX"
|
|
Packit Service |
569379 |
#define TEMP_URL_TEMPLATE "downloaded.XXXXXX"
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
#ifdef OS_WINDOWS
|
|
Packit Service |
569379 |
char *oscap_acquire_temp_dir()
|
|
Packit Service |
569379 |
{
|
|
Packit Service |
569379 |
WCHAR temp_path[PATH_MAX];
|
|
Packit Service |
569379 |
WCHAR temp_dir[PATH_MAX];
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
DWORD ret = GetTempPathW(PATH_MAX, temp_path);
|
|
Packit Service |
569379 |
if (ret > PATH_MAX || ret == 0) {
|
|
Packit Service |
569379 |
oscap_seterr(OSCAP_EFAMILY_WINDOWS, "Could not retrieve the path of the directory for temporary files.");
|
|
Packit Service |
569379 |
return NULL;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
unsigned int unique;
|
|
Packit Service |
569379 |
rand_s(&unique);
|
|
Packit Service |
569379 |
ret = GetTempFileNameW(temp_path, L"oscap", unique, temp_dir);
|
|
Packit Service |
569379 |
if (ret == 0) {
|
|
Packit Service |
569379 |
oscap_seterr(OSCAP_EFAMILY_WINDOWS, "Could not get a name for new temporary directory.");
|
|
Packit Service |
569379 |
return NULL;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
char *temp_dir_str = oscap_windows_wstr_to_str(temp_dir);
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
WCHAR *path_prefix = temp_dir;
|
|
Packit Service |
569379 |
do {
|
|
Packit Service |
569379 |
/* Use wide characters with L modifier because we work with a wide string. */
|
|
Packit Service |
569379 |
WCHAR *delimiter = wcschr(path_prefix, L'\\');
|
|
Packit Service |
569379 |
if (delimiter == NULL) {
|
|
Packit Service |
569379 |
break;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
*delimiter = L'\0';
|
|
Packit Service |
569379 |
if (!CreateDirectoryW(temp_dir, NULL)) {
|
|
Packit Service |
569379 |
ret = GetLastError();
|
|
Packit Service |
569379 |
if (ret != ERROR_ALREADY_EXISTS) {
|
|
Packit Service |
569379 |
char *error_message = oscap_windows_error_message(ret);
|
|
Packit Service |
569379 |
oscap_seterr(OSCAP_EFAMILY_WINDOWS, "Could not create temp directory '%s': %s.", temp_dir_str, error_message);
|
|
Packit Service |
569379 |
free(error_message);
|
|
Packit Service |
569379 |
free(temp_dir_str);
|
|
Packit Service |
569379 |
return NULL;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
*delimiter = L'\\';
|
|
Packit Service |
569379 |
path_prefix = ++delimiter;
|
|
Packit Service |
569379 |
} while (*path_prefix != L'\0');
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
if (!CreateDirectoryW(temp_dir, NULL)) {
|
|
Packit Service |
569379 |
ret = GetLastError();
|
|
Packit Service |
569379 |
char *error_message = oscap_windows_error_message(ret);
|
|
Packit Service |
569379 |
oscap_seterr(OSCAP_EFAMILY_WINDOWS, "Could not create temp directory '%s': %s.", temp_dir_str, error_message);
|
|
Packit Service |
569379 |
free(error_message);
|
|
Packit Service |
569379 |
free(temp_dir_str);
|
|
Packit Service |
569379 |
return NULL;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
return temp_dir_str;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
#else
|
|
Packit Service |
569379 |
char *oscap_acquire_temp_dir()
|
|
Packit Service |
569379 |
{
|
|
Packit Service |
569379 |
char *temp_dir = oscap_strdup(TEMP_DIR_TEMPLATE);
|
|
Packit Service |
569379 |
if (mkdtemp(temp_dir) == NULL) {
|
|
Packit Service |
569379 |
free(temp_dir);
|
|
Packit Service |
569379 |
oscap_seterr(OSCAP_EFAMILY_GLIBC, "Could not create temp directory " TEMP_DIR_TEMPLATE ". %s", strerror(errno));
|
|
Packit Service |
569379 |
return NULL;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
return temp_dir;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
#endif
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
#ifdef OS_WINDOWS
|
|
Packit Service |
569379 |
static bool _recursive_delete_directory(WCHAR *directory)
|
|
Packit Service |
569379 |
{
|
|
Packit Service |
569379 |
if (directory == NULL) {
|
|
Packit Service |
569379 |
return false;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
WCHAR find_pattern[MAX_PATH];
|
|
Packit Service |
569379 |
wcsncpy(find_pattern, directory, MAX_PATH);
|
|
Packit Service |
569379 |
wcsncat(find_pattern, L"\\*", MAX_PATH);
|
|
Packit Service |
569379 |
WCHAR dir_path[MAX_PATH];
|
|
Packit Service |
569379 |
wcsncpy(dir_path, directory, MAX_PATH);
|
|
Packit Service |
569379 |
wcsncat(dir_path, L"\\", MAX_PATH);
|
|
Packit Service |
569379 |
WCHAR file_path[MAX_PATH];
|
|
Packit Service |
569379 |
wcsncpy(file_path, dir_path, MAX_PATH);
|
|
Packit Service |
569379 |
DWORD err;
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
WIN32_FIND_DATAW find_data;
|
|
Packit Service |
569379 |
HANDLE find_handle = FindFirstFileW(find_pattern, &find_data);
|
|
Packit Service |
569379 |
if (find_handle == INVALID_HANDLE_VALUE) {
|
|
Packit Service |
569379 |
err = GetLastError();
|
|
Packit Service |
569379 |
char *error_message = oscap_windows_error_message(err);
|
|
Packit Service |
569379 |
oscap_seterr(OSCAP_EFAMILY_WINDOWS, "FindFirstFileW error: %s", error_message);
|
|
Packit Service |
569379 |
free(error_message);
|
|
Packit Service |
569379 |
return false;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
do {
|
|
Packit Service |
569379 |
if (wcscmp(find_data.cFileName, L".") != 0 && wcscmp(find_data.cFileName, L"..") != 0) {
|
|
Packit Service |
569379 |
wcsncat(file_path, find_data.cFileName, MAX_PATH);
|
|
Packit Service |
569379 |
if (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
|
Packit Service |
569379 |
if (!_recursive_delete_directory(file_path)) {
|
|
Packit Service |
569379 |
FindClose(find_handle);
|
|
Packit Service |
569379 |
return false;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
} else {
|
|
Packit Service |
569379 |
if (!DeleteFileW(file_path)) {
|
|
Packit Service |
569379 |
err = GetLastError();
|
|
Packit Service |
569379 |
char *error_message = oscap_windows_error_message(err);
|
|
Packit Service |
569379 |
oscap_seterr(OSCAP_EFAMILY_WINDOWS, "DeleteFileW Error: %s", error_message);
|
|
Packit Service |
569379 |
free(error_message);
|
|
Packit Service |
569379 |
FindClose(find_handle);
|
|
Packit Service |
569379 |
return false;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
wcsncpy(file_path, dir_path, MAX_PATH);
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
} while (FindNextFileW(find_handle, &find_data) != 0);
|
|
Packit Service |
569379 |
FindClose(find_handle);
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
if (!RemoveDirectoryW(dir_path)) {
|
|
Packit Service |
569379 |
err = GetLastError();
|
|
Packit Service |
569379 |
char *error_message = oscap_windows_error_message(err);
|
|
Packit Service |
569379 |
oscap_seterr(OSCAP_EFAMILY_WINDOWS, "RemoveDirectoryW error: %s", error_message);
|
|
Packit Service |
569379 |
free(error_message);
|
|
Packit Service |
569379 |
return false;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
return true;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
void oscap_acquire_cleanup_dir(char **dir_path)
|
|
Packit Service |
569379 |
{
|
|
Packit Service |
569379 |
if (*dir_path != NULL) {
|
|
Packit Service |
569379 |
WCHAR *dir_path_wstr = oscap_windows_str_to_wstr(*dir_path);
|
|
Packit Service |
569379 |
_recursive_delete_directory(dir_path_wstr);
|
|
Packit Service |
569379 |
free(dir_path_wstr);
|
|
Packit Service |
569379 |
free(*dir_path);
|
|
Packit Service |
569379 |
*dir_path = NULL;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
#else
|
|
Packit Service |
569379 |
static int __unlink_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
|
|
Packit Service |
569379 |
{
|
|
Packit Service |
569379 |
int rv = remove(fpath);
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
if (rv)
|
|
Packit Service |
569379 |
oscap_seterr(OSCAP_EFAMILY_GLIBC, "Could not remove %s. %s", fpath, strerror(errno));
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
return rv;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
void oscap_acquire_cleanup_dir(char **dir_path)
|
|
Packit Service |
569379 |
{
|
|
Packit Service |
569379 |
if (*dir_path != NULL)
|
|
Packit Service |
569379 |
{
|
|
Packit Service |
569379 |
nftw(*dir_path, __unlink_cb, 64, FTW_DEPTH | FTW_PHYS | FTW_MOUNT);
|
|
Packit Service |
569379 |
free(*dir_path);
|
|
Packit Service |
569379 |
*dir_path = NULL;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
#endif
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
int
|
|
Packit Service |
569379 |
oscap_acquire_temp_file(const char *dir, const char *template, char **filename)
|
|
Packit Service |
569379 |
{
|
|
Packit Service |
569379 |
#ifdef OS_WINDOWS
|
|
Packit Service |
569379 |
int old_mode;
|
|
Packit Service |
569379 |
#else
|
|
Packit Service |
569379 |
mode_t old_mode;
|
|
Packit Service |
569379 |
#endif
|
|
Packit Service |
569379 |
int fd;
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
if (dir == NULL || template == NULL || filename == NULL)
|
|
Packit Service |
569379 |
return -1;
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
*filename = malloc(PATH_MAX * sizeof(char));
|
|
Packit Service |
569379 |
old_mode = umask(077); /* Override unusual umask. Ensure 0700 permissions. */
|
|
Packit Service |
569379 |
#ifdef OS_WINDOWS
|
|
Packit Service |
569379 |
char *base_name = oscap_strdup(template);
|
|
Packit Service |
569379 |
_mktemp_s(base_name, strlen(base_name) + 1); // +1 for terminator
|
|
Packit Service |
569379 |
snprintf(*filename, PATH_MAX, "%s/%s", dir, base_name);
|
|
Packit Service |
569379 |
free(base_name);
|
|
Packit Service |
569379 |
fd = open(*filename, _O_RDWR | _O_CREAT, _S_IREAD | _S_IWRITE);
|
|
Packit Service |
569379 |
#else
|
|
Packit Service |
569379 |
snprintf(*filename, PATH_MAX, "%s/%s", dir, template);
|
|
Packit Service |
569379 |
fd = mkstemp(*filename);
|
|
Packit Service |
569379 |
#endif
|
|
Packit Service |
569379 |
(void) umask(old_mode);
|
|
Packit Service |
569379 |
if (fd < 1) {
|
|
Packit Service |
569379 |
oscap_seterr(OSCAP_EFAMILY_GLIBC, "mkstemp for %s failed: %s", *filename, strerror(errno));
|
|
Packit Service |
569379 |
free(*filename);
|
|
Packit Service |
569379 |
*filename = NULL;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
return fd;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
bool
|
|
Packit Service |
569379 |
oscap_acquire_url_is_supported(const char *url)
|
|
Packit Service |
569379 |
{
|
|
Packit Service |
569379 |
return oscap_str_startswith(url, "http://") || oscap_str_startswith(url, "https://");
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
char *
|
|
Packit Service |
569379 |
oscap_acquire_url_to_filename(const char *url)
|
|
Packit Service |
569379 |
{
|
|
Packit Service |
569379 |
/* RFC 3986: 2.1. Percent-Encoding */
|
|
Packit Service |
569379 |
char *curl_filename = NULL;
|
|
Packit Service |
569379 |
char *filename = NULL;
|
|
Packit Service |
569379 |
CURL *curl;
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
if (curl_global_init(CURL_GLOBAL_ALL) != 0) {
|
|
Packit Service |
569379 |
oscap_seterr(OSCAP_EFAMILY_NET, "Failed to initialize libcurl.");
|
|
Packit Service |
569379 |
return NULL;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
curl = curl_easy_init();
|
|
Packit Service |
569379 |
if (curl == NULL) {
|
|
Packit Service |
569379 |
oscap_seterr(OSCAP_EFAMILY_NET, "Failed to initialize libcurl.");
|
|
Packit Service |
569379 |
return NULL;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
curl_filename = curl_easy_escape(curl , url , 0);
|
|
Packit Service |
569379 |
if (curl_filename == NULL) {
|
|
Packit Service |
569379 |
curl_easy_cleanup(curl);
|
|
Packit Service |
569379 |
curl_global_cleanup();
|
|
Packit Service |
569379 |
oscap_seterr(OSCAP_EFAMILY_NET, "Failed to escape the given url %s", url);
|
|
Packit Service |
569379 |
return NULL;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
filename = oscap_strdup(curl_filename);
|
|
Packit Service |
569379 |
curl_free(curl_filename);
|
|
Packit Service |
569379 |
curl_easy_cleanup(curl);
|
|
Packit Service |
569379 |
curl_global_cleanup();
|
|
Packit Service |
569379 |
return filename;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
|
|
Packit Service |
7dc6a6 |
static int _curl_trace(CURL *handle, curl_infotype type, char *data, size_t size, void *userp)
|
|
Packit Service |
7dc6a6 |
{
|
|
Packit Service |
7dc6a6 |
const char *title;
|
|
Packit Service |
7dc6a6 |
|
|
Packit Service |
7dc6a6 |
switch (type) {
|
|
Packit Service |
7dc6a6 |
case CURLINFO_TEXT:
|
|
Packit Service |
7dc6a6 |
title = "== cURL info";
|
|
Packit Service |
7dc6a6 |
break;
|
|
Packit Service |
7dc6a6 |
case CURLINFO_HEADER_OUT:
|
|
Packit Service |
7dc6a6 |
title = "=> cURL header (out)";
|
|
Packit Service |
7dc6a6 |
break;
|
|
Packit Service |
7dc6a6 |
case CURLINFO_HEADER_IN:
|
|
Packit Service |
7dc6a6 |
title = "<= cURL header (in)";
|
|
Packit Service |
7dc6a6 |
break;
|
|
Packit Service |
7dc6a6 |
case CURLINFO_DATA_OUT:
|
|
Packit Service |
7dc6a6 |
case CURLINFO_SSL_DATA_OUT:
|
|
Packit Service |
7dc6a6 |
case CURLINFO_DATA_IN:
|
|
Packit Service |
7dc6a6 |
case CURLINFO_SSL_DATA_IN:
|
|
Packit Service |
7dc6a6 |
default:
|
|
Packit Service |
7dc6a6 |
return 0;
|
|
Packit Service |
7dc6a6 |
break;
|
|
Packit Service |
7dc6a6 |
}
|
|
Packit Service |
7dc6a6 |
|
|
Packit Service |
7dc6a6 |
dD("%s: %s", title, data);
|
|
Packit Service |
7dc6a6 |
|
|
Packit Service |
7dc6a6 |
return 0;
|
|
Packit Service |
7dc6a6 |
}
|
|
Packit Service |
7dc6a6 |
|
|
Packit Service |
569379 |
char* oscap_acquire_url_download(const char *url, size_t* memory_size)
|
|
Packit Service |
569379 |
{
|
|
Packit Service |
569379 |
CURL *curl;
|
|
Packit Service |
569379 |
curl = curl_easy_init();
|
|
Packit Service |
569379 |
if (curl == NULL) {
|
|
Packit Service |
569379 |
oscap_seterr(OSCAP_EFAMILY_NET, "Failed to initialize libcurl.");
|
|
Packit Service |
569379 |
return NULL;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
struct oscap_buffer* buffer = oscap_buffer_new();
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
curl_easy_setopt(curl, CURLOPT_URL, url);
|
|
Packit Service |
569379 |
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_memory_callback);
|
|
Packit Service |
569379 |
curl_easy_setopt(curl, CURLOPT_WRITEDATA, buffer);
|
|
Packit Service |
b55627 |
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "");
|
|
Packit Service |
7dc6a6 |
curl_easy_setopt(curl, CURLOPT_TRANSFER_ENCODING, true);
|
|
Packit Service |
569379 |
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true);
|
|
Packit Service |
7dc6a6 |
curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
|
|
Packit Service |
7dc6a6 |
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, _curl_trace);
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
CURLcode res = curl_easy_perform(curl);
|
|
Packit Service |
569379 |
curl_easy_cleanup(curl);
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
if (res != 0) {
|
|
Packit Service |
569379 |
oscap_seterr(OSCAP_EFAMILY_NET, "Download failed: %s", curl_easy_strerror(res));
|
|
Packit Service |
569379 |
oscap_buffer_free(buffer);
|
|
Packit Service |
569379 |
return NULL;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
*memory_size = oscap_buffer_get_length(buffer);
|
|
Packit Service |
569379 |
char* data = oscap_buffer_bequeath(buffer); // get data and free buffer struct
|
|
Packit Service |
569379 |
return data;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
size_t write_to_memory_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
|
|
Packit Service |
569379 |
{
|
|
Packit Service |
569379 |
size_t new_received_size = size * nmemb; // total size of newly received data
|
|
Packit Service |
569379 |
oscap_buffer_append_binary_data((struct oscap_buffer*)userdata, ptr, new_received_size);
|
|
Packit Service |
569379 |
return new_received_size;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
char *
|
|
Packit Service |
569379 |
oscap_acquire_pipe_to_string(int fd)
|
|
Packit Service |
569379 |
{
|
|
Packit Service |
569379 |
struct oscap_string *pipe_string = oscap_string_new();
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
char readbuf;
|
|
Packit Service |
569379 |
// FIXME: Read by larger chunks in the future
|
|
Packit Service |
569379 |
while (read(fd, &readbuf, 1) > 0) {
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
if (readbuf == '&') {
|
|
Packit Service |
569379 |
// & is a special case, we have to "escape" it manually
|
|
Packit Service |
569379 |
// (all else will eventually get handled by libxml)
|
|
Packit Service |
569379 |
oscap_string_append_string(pipe_string, "&");
|
|
Packit Service |
569379 |
} else {
|
|
Packit Service |
569379 |
oscap_string_append_char(pipe_string, readbuf);
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
close(fd);
|
|
Packit Service |
569379 |
return oscap_string_bequeath(pipe_string);
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
char *oscap_acquire_guess_realpath(const char *filepath)
|
|
Packit Service |
569379 |
{
|
|
Packit Service |
569379 |
char resolved_name[PATH_MAX];
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
char *rpath = oscap_realpath(filepath, resolved_name);
|
|
Packit Service |
569379 |
if (rpath != NULL)
|
|
Packit Service |
569379 |
rpath = oscap_strdup(rpath);
|
|
Packit Service |
569379 |
else {
|
|
Packit Service |
569379 |
// file does not exists, let's try to guess realpath
|
|
Packit Service |
569379 |
// this is not 100% correct, but it is good enough
|
|
Packit Service |
569379 |
char *copy = oscap_strdup(filepath);
|
|
Packit Service |
569379 |
if (copy == NULL) {
|
|
Packit Service |
569379 |
oscap_seterr(OSCAP_EFAMILY_OSCAP, "Cannot guess realpath for %s, directory: cannot allocate memory!", filepath);
|
|
Packit Service |
569379 |
return NULL;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
char *dir_name = oscap_dirname(copy);
|
|
Packit Service |
569379 |
char *real_dir = oscap_realpath(dir_name, resolved_name);
|
|
Packit Service |
569379 |
if (real_dir == NULL) {
|
|
Packit Service |
569379 |
oscap_seterr(OSCAP_EFAMILY_OSCAP, "Cannot guess realpath for %s, directory: %s does not exists!", filepath, dir_name);
|
|
Packit Service |
569379 |
free(copy);
|
|
Packit Service |
569379 |
free(dir_name);
|
|
Packit Service |
569379 |
return NULL;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
free(dir_name);
|
|
Packit Service |
569379 |
char *base_name = oscap_basename((char *)filepath);
|
|
Packit Service |
569379 |
rpath = oscap_sprintf("%s/%s", real_dir, base_name);
|
|
Packit Service |
569379 |
free(base_name);
|
|
Packit Service |
569379 |
free(copy);
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
return rpath;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
int oscap_acquire_mkdir_p(const char *path)
|
|
Packit Service |
569379 |
{
|
|
Packit Service |
569379 |
// NOTE: This assumes a UNIX VFS path, C:\\folder\\folder would break it!
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
if (strlen(path) > PATH_MAX) {
|
|
Packit Service |
569379 |
return -1;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
else {
|
|
Packit Service |
569379 |
char temp[PATH_MAX + 1]; // +1 for \0
|
|
Packit Service |
569379 |
unsigned int i;
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
for (i = 0; i <= strlen(path); i++) {
|
|
Packit Service |
569379 |
if (path[i] == '/' || path[i] == '\0') {
|
|
Packit Service |
569379 |
strncpy(temp, path, i);
|
|
Packit Service |
569379 |
temp[i] = '\0';
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
// skip leading '/', we will never be creating the root anyway
|
|
Packit Service |
569379 |
if (strlen(temp) == 0)
|
|
Packit Service |
569379 |
continue;
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
#ifndef OS_WINDOWS
|
|
Packit Service |
569379 |
if (mkdir(temp, S_IRWXU) != 0 && errno != EEXIST) {
|
|
Packit Service |
569379 |
#else
|
|
Packit Service |
569379 |
if (mkdir(temp) != 0 && errno != EEXIST) {
|
|
Packit Service |
569379 |
#endif
|
|
Packit Service |
569379 |
oscap_seterr(OSCAP_EFAMILY_GLIBC,
|
|
Packit Service |
569379 |
"Error making directory '%s', while doing recursive mkdir for '%s', error was '%s'.",
|
|
Packit Service |
569379 |
temp, path, strerror(errno));
|
|
Packit Service |
569379 |
return -1;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
return 0;
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
|
|
Packit Service |
569379 |
int oscap_acquire_ensure_parent_dir(const char *filepath)
|
|
Packit Service |
569379 |
{
|
|
Packit Service |
569379 |
char *filepath_cpy = oscap_strdup(filepath);
|
|
Packit Service |
569379 |
char *dirpath = oscap_dirname(filepath_cpy);
|
|
Packit Service |
569379 |
int ret = oscap_acquire_mkdir_p(dirpath);
|
|
Packit Service |
569379 |
if (ret != 0) {
|
|
Packit Service |
569379 |
oscap_seterr(OSCAP_EFAMILY_GLIBC, "Error making directory '%s' to ensure correct path of '%s'.", dirpath, filepath);
|
|
Packit Service |
569379 |
}
|
|
Packit Service |
569379 |
free(dirpath);
|
|
Packit Service |
569379 |
free(filepath_cpy);
|
|
Packit Service |
569379 |
return ret;
|
|
Packit Service |
569379 |
}
|