hunting/utils/Servers/src/ftp_servers.c
2024-02-28 03:49:11 -08:00

218 lines
7.0 KiB
C

/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ftp_servers.h"
#include "ILog.h"
#include "curl_serve.h"
#include <curl/curl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
struct FtpFile
{
const char *filename;
FILE *stream;
};
static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
{
struct FtpFile *out = (struct FtpFile *)stream;
if (!out->stream) {
/* open file for writing */
out->stream = fopen(out->filename, "wb");
if (!out->stream)
return -1; /* failure, cannot open file to write */
}
return fwrite(buffer, size, nmemb, out->stream);
}
static CURL *ftp_curl_easy_make(ServerFtp *param)
{
CURL *curl = NULL;
curl = CurlEasyMake();
if (curl) {
/*
* You better replace the URL with one that works!
*/
curl_easy_setopt(curl, CURLOPT_URL, param->url);
/* User and password for the FTP login */
if (param->user_password) {
curl_easy_setopt(curl, CURLOPT_USERPWD, param->user_password);
}
/* We activate SSL and we require it for both control and data */
if (FTPS_FLAG_ENABLE == param->ftpsFlag) {
curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
}
if (SERVERS_NEVER_TIMEOUT < param->timeOutMs) {
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, param->timeOutMs);
}
}
return curl;
}
void FtpServersCheckConnect(ServerFtp *param)
{
if (!param) {
LogError("null pointer.\n");
return;
}
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = ftp_curl_easy_make(param);
if (curl) {
curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
res = curl_easy_perform(curl);
param->code = res;
/* always cleanup */
curl_easy_cleanup(curl);
if (CURLE_OK != res) {
/* we failed */
fprintf(stderr, "curl told us %d\n", res);
}
}
curl_global_cleanup();
}
void FtpServersDownload(ServerFtp *param)
{
if (!param) {
LogError("null pointer.\n");
return;
}
CURL *curl;
CURLcode res;
struct FtpFile ftpfile = {param->filePath, /* name to store the file as if successful */
NULL};
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = ftp_curl_easy_make(param);
if (curl) {
/* Define our callback to get called when there's data to be written */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
/* Set a pointer to our struct to pass to the callback */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);
res = curl_easy_perform(curl);
param->code = res;
/* always cleanup */
curl_easy_cleanup(curl);
if (CURLE_OK != res) {
/* we failed */
fprintf(stderr, "curl told us %d\n", res);
}
}
if (ftpfile.stream)
fclose(ftpfile.stream); /* close the local file */
curl_global_cleanup();
}
/* NOTE: if you want this example to work on Windows with libcurl as a
DLL, you MUST also provide a read callback with CURLOPT_READFUNCTION.
Failing to do so will give you a crash since a DLL may not use the
variable's memory when passed in to it from an app like this. */
static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *stream)
{
unsigned long nread;
/* in real-world cases, this would probably get this data differently
as this fread() stuff is exactly what the library already would do
by default internally */
size_t retcode = fread(ptr, size, nmemb, stream);
if (retcode > 0) {
nread = (unsigned long)retcode;
fprintf(stderr, "*** We read %lu bytes from file\n", nread);
}
return retcode;
}
void FtpServersUpload(ServerFtp *param)
{
if (!param) {
LogError("null pointer.\n");
return;
}
CURL *curl;
CURLcode res;
FILE *hd_src;
struct stat file_info;
unsigned long fsize;
struct curl_slist *headerlist = NULL;
// static const char buf_1[] = "RNFR " UPLOAD_FILE_AS; // TODO:
// static const char buf_2[] = "RNTO " RENAME_FILE_TO;
/* get the file size of the local file */
if (stat(param->filePath, &file_info)) {
LogInfo("Couldn't open '%s'\n", param->filePath);
return;
}
fsize = (unsigned long)file_info.st_size;
LogInfo("Local file size: %lu bytes.\n", fsize);
/* get a FILE * of the same file */
hd_src = fopen(param->filePath, "rb");
/* In windows, this will init the winsock stuff */
curl_global_init(CURL_GLOBAL_ALL);
/* get a curl handle */
curl = ftp_curl_easy_make(param);
if (curl) {
/* build a list of commands to pass to libcurl */
// headerlist = curl_slist_append(headerlist, buf_1); // TODO:
// headerlist = curl_slist_append(headerlist, buf_2);
/* we want to use our own read function */
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
/* enable uploading */
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
/* pass in that last of FTP commands to run after the transfer */
curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);
/* now specify which file to upload */
curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
/* Set the size of the file to upload (optional). If you give a *_LARGE
option you MUST make sure that the type of the passed-in argument is a
curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
make sure that to pass in a type 'long' argument. */
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)fsize);
/* Now run off and do what you have been told! */
res = curl_easy_perform(curl);
param->code = res;
/* Check for errors */
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
/* clean up the FTP commands list */
curl_slist_free_all(headerlist);
/* always cleanup */
curl_easy_cleanup(curl);
}
fclose(hd_src); /* close the local file */
curl_global_cleanup();
}