feat(landerctl): re-implement paste & file functionality

feature/50-one-time-keys
Jef Roosens 2024-10-03 09:44:00 +02:00
parent 086ee8900b
commit 8b7ce0085e
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 120 additions and 0 deletions

View File

@ -75,6 +75,8 @@ landerctl_err landerctl_post_paste(landerctl_ctx *ctx);
landerctl_err landerctl_post_file(landerctl_ctx *ctx);
int landerctl_cmd_short(landerctl_args *args);
int landerctl_cmd_paste(landerctl_args *args);
int landerctl_cmd_file(landerctl_args *args);
int landerctl_parse_args(landerctl_args *out, int argc, char **argv);
#endif

View File

@ -1,6 +1,9 @@
#include <libgen.h>
#include <string.h>
#include <sys/stat.h>
#include <curl/curl.h>
#include <magic.h>
#include "landerctl.h"
@ -147,3 +150,112 @@ int landerctl_cmd_short(landerctl_args *args) {
return res;
}
int landerctl_cmd_paste(landerctl_args *args) {
// TODO argument count check
int res;
landerctl_curl curl;
if ((res = landerctl_curl_init(&curl))) {
return res;
}
const char *key = args->args.len == 2 ? args->args.arr[1] : NULL;
if ((res = landerctl_curl_set_common(&curl, args, key))) {
return res;
}
const char *data_path = args->args.arr[0];
FILE *f = fopen(data_path, "rb");
if (f == NULL) {
fprintf(stderr, "Failed to open data file %s\n", args->args.arr[0]);
return 1;
}
struct stat sb;
stat(data_path, &sb);
curl_easy_setopt(curl.curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl.curl, CURLOPT_READDATA, f);
curl_easy_setopt(curl.curl, CURLOPT_POSTFIELDSIZE, sb.st_size);
res = landerctl_curl_perform(&curl);
if (res == 0) {
landerctl_curl_inspect(&curl, args);
}
landerctl_curl_cleanup(&curl);
return res;
}
int landerctl_cmd_file(landerctl_args *args) {
// TODO argument count check
int res;
landerctl_curl curl;
if ((res = landerctl_curl_init(&curl))) {
return res;
}
const char *key = args->args.len == 2 ? args->args.arr[1] : NULL;
if ((res = landerctl_curl_set_common(&curl, args, key))) {
return res;
}
const char *data_path = args->args.arr[0];
FILE *f = fopen(data_path, "rb");
if (f == NULL) {
fprintf(stderr, "Failed to open data file %s\n", args->args.arr[0]);
return 1;
}
struct stat sb;
stat(data_path, &sb);
curl_easy_setopt(curl.curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl.curl, CURLOPT_READDATA, f);
curl_easy_setopt(curl.curl, CURLOPT_POSTFIELDSIZE_LARGE, sb.st_size);
curl_easy_setopt(curl.curl, CURLOPT_NOPROGRESS, 0L);
magic_t cookie = magic_open(MAGIC_MIME_TYPE);
if (magic_load(cookie, NULL) == 0) {
const char *mime_type = magic_file(cookie, data_path);
if (mime_type != NULL) {
char content_type_header[strlen(mime_type) + 24];
sprintf(content_type_header, "X-Lander-Content-Type: %s", mime_type);
curl.headers = curl_slist_append(curl.headers, content_type_header);
} else {
printf("Couldn't determine mime type; skipping Content-Type header\n");
}
} else {
printf("Couldn't load magic file; skipping Content-Type header\n");
}
char s[strlen(data_path) + 1];
strcpy(s, data_path);
const char *base_name = basename(s);
char filename_header[strlen(base_name) + 20];
sprintf(filename_header, "X-Lander-Filename: %s", base_name);
curl.headers = curl_slist_append(curl.headers, filename_header);
res = landerctl_curl_perform(&curl);
if (res == 0) {
landerctl_curl_inspect(&curl, args);
}
landerctl_curl_cleanup(&curl);
return res;
}

View File

@ -21,6 +21,12 @@ int main(int argc, char **argv) {
case landerctl_mode_short:
res = landerctl_cmd_short(&args);
break;
case landerctl_mode_paste:
res = landerctl_cmd_paste(&args);
break;
case landerctl_mode_file:
res = landerctl_cmd_file(&args);
break;
/* case landerctl_mode_paste: */
/* res = landerctl_post_paste(&ctx); */
/* break; */