2023-11-16 11:30:02 +01:00
|
|
|
#include <libgen.h>
|
2023-11-15 14:50:02 +01:00
|
|
|
#include <string.h>
|
2023-11-16 11:30:02 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <curl/curl.h>
|
|
|
|
#include <magic.h>
|
2023-11-15 14:50:02 +01:00
|
|
|
|
|
|
|
#include "landerctl.h"
|
|
|
|
|
2023-11-16 11:30:02 +01:00
|
|
|
const char *landerctl_err_msg(landerctl_err err) {
|
|
|
|
switch (err) {
|
|
|
|
case landerctl_err_not_found:
|
|
|
|
return "File not found";
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void landerctl_set_common(landerctl_ctx *ctx) {
|
|
|
|
size_t url_len = strlen(ctx->cfg.server_url) + 4;
|
2023-11-15 14:50:02 +01:00
|
|
|
|
2023-11-16 11:30:02 +01:00
|
|
|
if (ctx->key != NULL) {
|
|
|
|
url_len += strlen(ctx->key);
|
2023-11-15 14:50:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
char mode_char;
|
|
|
|
|
2023-11-16 11:30:02 +01:00
|
|
|
switch (ctx->mode) {
|
2023-11-15 14:50:02 +01:00
|
|
|
case landerctl_mode_short:
|
|
|
|
mode_char = 's';
|
|
|
|
break;
|
|
|
|
case landerctl_mode_paste:
|
|
|
|
mode_char = 'p';
|
|
|
|
break;
|
|
|
|
case landerctl_mode_file:
|
|
|
|
mode_char = 'f';
|
|
|
|
break;
|
|
|
|
// Shouldn't be able to happen
|
|
|
|
default:
|
2023-11-16 11:30:02 +01:00
|
|
|
return;
|
2023-11-15 14:50:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
char url[url_len + 1];
|
|
|
|
|
2023-11-16 11:30:02 +01:00
|
|
|
if (ctx->key == NULL) {
|
|
|
|
sprintf(url, "%s/%c%s/", ctx->cfg.server_url, mode_char,
|
|
|
|
ctx->secure ? "l" : "");
|
2023-11-15 14:50:02 +01:00
|
|
|
} else {
|
2023-11-16 11:30:02 +01:00
|
|
|
sprintf(url, "%s/%c%s/%s", ctx->cfg.server_url, mode_char,
|
|
|
|
ctx->secure ? "l" : "", ctx->key);
|
2023-11-15 14:50:02 +01:00
|
|
|
}
|
|
|
|
|
2023-11-16 11:30:02 +01:00
|
|
|
curl_easy_setopt(ctx->curl, CURLOPT_URL, url);
|
2023-11-15 14:50:02 +01:00
|
|
|
|
|
|
|
// Add API key header
|
2023-11-16 11:30:02 +01:00
|
|
|
char api_key_header[strlen(ctx->cfg.api_key) + 12];
|
|
|
|
sprintf(api_key_header, "X-Api-Key: %s", ctx->cfg.api_key);
|
2023-11-15 14:50:02 +01:00
|
|
|
|
2023-11-16 11:30:02 +01:00
|
|
|
ctx->headers = curl_slist_append(NULL, api_key_header);
|
2023-11-15 14:50:02 +01:00
|
|
|
|
2023-11-16 11:30:02 +01:00
|
|
|
curl_easy_setopt(ctx->curl, CURLOPT_USERAGENT,
|
|
|
|
"landerctl/" LANDER_VERSION "");
|
|
|
|
}
|
2023-11-15 14:50:02 +01:00
|
|
|
|
2023-11-16 11:30:02 +01:00
|
|
|
landerctl_err landerctl_post_short(landerctl_ctx *ctx) {
|
|
|
|
curl_easy_setopt(ctx->curl, CURLOPT_POSTFIELDSIZE, strlen(ctx->arg));
|
|
|
|
curl_easy_setopt(ctx->curl, CURLOPT_POSTFIELDS, ctx->arg);
|
2023-11-15 14:50:02 +01:00
|
|
|
|
2023-11-16 11:30:02 +01:00
|
|
|
return landerctl_err_ok;
|
2023-11-15 14:50:02 +01:00
|
|
|
}
|
|
|
|
|
2023-11-16 11:30:02 +01:00
|
|
|
landerctl_err landerctl_post_paste(landerctl_ctx *ctx) {
|
|
|
|
ctx->data_file = fopen(ctx->arg, "rb");
|
|
|
|
|
|
|
|
if (ctx->data_file == NULL) {
|
|
|
|
return landerctl_err_not_found;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stat sb;
|
|
|
|
stat(ctx->arg, &sb);
|
|
|
|
|
|
|
|
curl_easy_setopt(ctx->curl, CURLOPT_POST, 1L);
|
|
|
|
curl_easy_setopt(ctx->curl, CURLOPT_READDATA, ctx->data_file);
|
|
|
|
curl_easy_setopt(ctx->curl, CURLOPT_POSTFIELDSIZE, sb.st_size);
|
|
|
|
|
|
|
|
return landerctl_err_ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
landerctl_err landerctl_post_file(landerctl_ctx *ctx) {
|
|
|
|
ctx->data_file = fopen(ctx->arg, "rb");
|
|
|
|
|
|
|
|
if (ctx->data_file == NULL) {
|
|
|
|
return landerctl_err_not_found;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stat sb;
|
|
|
|
stat(ctx->arg, &sb);
|
|
|
|
|
|
|
|
curl_easy_setopt(ctx->curl, CURLOPT_POST, 1L);
|
|
|
|
curl_easy_setopt(ctx->curl, CURLOPT_READDATA, ctx->data_file);
|
|
|
|
curl_easy_setopt(ctx->curl, CURLOPT_POSTFIELDSIZE_LARGE, sb.st_size);
|
2023-11-16 12:46:52 +01:00
|
|
|
curl_easy_setopt(ctx->curl, CURLOPT_NOPROGRESS, 0L);
|
2023-11-16 11:30:02 +01:00
|
|
|
|
|
|
|
magic_t cookie = magic_open(MAGIC_MIME_TYPE);
|
|
|
|
|
2023-11-17 09:15:55 +01:00
|
|
|
if (magic_load(cookie, NULL) == 0) {
|
|
|
|
const char *mime_type = magic_file(cookie, ctx->arg);
|
|
|
|
|
|
|
|
if (mime_type != NULL) {
|
|
|
|
char content_type_header[strlen(mime_type) + 24];
|
|
|
|
sprintf(content_type_header, "X-Lander-Content-Type: %s", mime_type);
|
|
|
|
|
|
|
|
ctx->headers = curl_slist_append(ctx->headers, content_type_header);
|
|
|
|
} else {
|
2023-11-17 09:31:20 +01:00
|
|
|
printf("Couldn't determine mime type; skipping Content-Type header\n");
|
2023-11-17 09:15:55 +01:00
|
|
|
}
|
|
|
|
} else {
|
2023-11-17 09:31:20 +01:00
|
|
|
printf("Couldn't load magic file; skipping Content-Type header\n");
|
2023-11-17 09:15:55 +01:00
|
|
|
}
|
2023-11-16 11:30:02 +01:00
|
|
|
|
|
|
|
char s[strlen(ctx->arg) + 1];
|
|
|
|
strcpy(s, ctx->arg);
|
|
|
|
const char *base_name = basename(s);
|
|
|
|
|
|
|
|
char filename_header[strlen(base_name) + 20];
|
|
|
|
sprintf(filename_header, "X-Lander-Filename: %s", base_name);
|
|
|
|
|
|
|
|
ctx->headers = curl_slist_append(ctx->headers, filename_header);
|
|
|
|
|
|
|
|
return landerctl_err_ok;
|
2023-11-15 14:50:02 +01:00
|
|
|
}
|