feat(landerctl): implement -S flag using restructured codebase

feature/50-one-time-keys
Jef Roosens 2024-10-03 09:30:35 +02:00
parent 4b469623dd
commit 086ee8900b
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
5 changed files with 423 additions and 137 deletions

View File

@ -141,6 +141,7 @@ clean:
bear: clean bear: clean
bear -- make bear -- make
bear --append -- make build-test bear --append -- make build-test
bear --append -- make -C landerctl
# Make make aware of the .d files # Make make aware of the .d files

View File

@ -50,6 +50,23 @@ typedef struct landerctl_ctx {
FILE *data_file; FILE *data_file;
} landerctl_ctx; } landerctl_ctx;
typedef struct landerctl_args {
bool secure;
bool verbose;
landerctl_cfg cfg;
struct {
char **arr;
int len;
} args;
landerctl_mode mode;
} landerctl_args;
typedef struct landerctl_curl {
CURL *curl;
struct curl_slist *headers;
char err_msg[CURL_ERROR_SIZE];
} landerctl_curl;
const char *landerctl_err_msg(landerctl_err err); const char *landerctl_err_msg(landerctl_err err);
void landerctl_set_common(landerctl_ctx *ctx); void landerctl_set_common(landerctl_ctx *ctx);
@ -57,4 +74,7 @@ landerctl_err landerctl_post_short(landerctl_ctx *ctx);
landerctl_err landerctl_post_paste(landerctl_ctx *ctx); landerctl_err landerctl_post_paste(landerctl_ctx *ctx);
landerctl_err landerctl_post_file(landerctl_ctx *ctx); landerctl_err landerctl_post_file(landerctl_ctx *ctx);
int landerctl_cmd_short(landerctl_args *args);
int landerctl_parse_args(landerctl_args *out, int argc, char **argv);
#endif #endif

View File

@ -0,0 +1,93 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "landerctl.h"
const char *cfg_file_name = ".landerrc";
const char *usage = "%s [-SPFsv] [-c CONFIG_FILE] [ARGS]\n";
int landerctl_parse_config(landerctl_cfg *cfg, const char *path) {
// Parse default file location instead
if (path == NULL) {
const char *home_dir = getenv("HOME");
if (home_dir == NULL) {
path = cfg_file_name;
} else {
// This is a blatant memleak if a custom config file is set, but it really
// doesn't matter for a short-lived CLI tool
char *buf = malloc(strlen(home_dir) + strlen(cfg_file_name) + 2);
sprintf(buf, "%s/%s", home_dir, cfg_file_name);
path = buf;
}
}
char *err_msg = NULL;
landerctl_cfg_err parse_res = landerctl_cfg_parse(cfg, path);
switch (parse_res) {
case landerctl_cfg_err_ok:
break;
case landerctl_cfg_err_not_found:
err_msg = "Config file not found";
break;
case landerctl_cfg_err_invalid:
err_msg = "Invalid config file";
break;
case landerctl_cfg_err_incomplete:
err_msg = "Incomplete config file";
break;
}
if (err_msg != NULL) {
fprintf(stderr, "%s\n", err_msg);
return 1;
}
return 0;
}
int landerctl_parse_args(landerctl_args *args, int argc, char **argv) {
optind = 1;
int c;
const char *cfg_path = NULL;
while ((c = getopt(argc, argv, "SPFsvc:")) != -1) {
switch (c) {
case 'S':
args->mode = landerctl_mode_short;
break;
case 'P':
args->mode = landerctl_mode_paste;
break;
case 'F':
args->mode = landerctl_mode_file;
break;
case 's':
args->secure = true;
break;
case 'v':
args->verbose = true;
break;
case 'c':
cfg_path = optarg;
break;
case '?':
fprintf(stderr, usage, argv[0]);
return 2;
}
}
int res;
if ((res = landerctl_parse_config(&args->cfg, cfg_path))) {
return res;
}
args->args.arr = &argv[optind];
args->args.len = argc - optind;
return 0;
}

View File

@ -0,0 +1,149 @@
#include <string.h>
#include <curl/curl.h>
#include "landerctl.h"
int landerctl_curl_init(landerctl_curl *out) {
curl_global_init(CURL_GLOBAL_ALL);
CURL *curl = curl_easy_init();
if (curl == NULL) {
fprintf(stderr, "Failed to initialize cURL client.\n");
return 8;
}
out->curl = curl;
out->headers = NULL;
return 0;
}
int landerctl_curl_set_common(landerctl_curl *curl, landerctl_args *args,
const char *key) {
size_t url_len = strlen(args->cfg.server_url) + 4;
if (key != NULL) {
url_len += strlen(key);
}
char mode_char;
switch (args->mode) {
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:
return 10;
}
char url[url_len + 1];
if (key == NULL) {
sprintf(url, "%s/%c%s/", args->cfg.server_url, mode_char,
args->secure ? "l" : "");
} else {
sprintf(url, "%s/%c%s/%s", args->cfg.server_url, mode_char,
args->secure ? "l" : "", key);
}
curl_easy_setopt(curl->curl, CURLOPT_URL, url);
// Add API key header
char api_key_header[strlen(args->cfg.api_key) + 12];
sprintf(api_key_header, "X-Api-Key: %s", args->cfg.api_key);
curl->headers = curl_slist_append(curl->headers, api_key_header);
curl_easy_setopt(curl->curl, CURLOPT_USERAGENT,
"landerctl/" LANDER_VERSION "");
if (args->verbose) {
curl_easy_setopt(curl->curl, CURLOPT_VERBOSE, 1L);
}
curl_easy_setopt(curl->curl, CURLOPT_CAINFO, args->cfg.ca_certs_bundle);
curl_easy_setopt(curl->curl, CURLOPT_ERRORBUFFER, curl->err_msg);
return 0;
}
int landerctl_curl_perform(landerctl_curl *curl) {
curl_easy_setopt(curl->curl, CURLOPT_HTTPHEADER, curl->headers);
int res = curl_easy_perform(curl->curl);
if (res != CURLE_OK) {
fprintf(stderr, "Libcurl encountered an error (code %i): %s\n", res,
curl->err_msg);
}
return res;
}
int landerctl_curl_inspect(landerctl_curl *curl, landerctl_args *args) {
long response_code;
curl_easy_getinfo(curl->curl, CURLINFO_RESPONSE_CODE, &response_code);
if (response_code < 200 || response_code > 299) {
fprintf(stderr, "HTTP status code %li\n", response_code);
return 3;
} else {
struct curl_header *location_header;
if (curl_easy_header(curl->curl, "Location", 0, CURLH_HEADER, -1,
&location_header) == CURLHE_OK) {
printf("%s%s\n", args->cfg.server_url, location_header->value);
} else {
fprintf(stderr, "Server returned a 2xx without a Location header.\n");
return 5;
}
}
return 0;
}
void landerctl_curl_cleanup(landerctl_curl *curl) {
curl_easy_cleanup(curl->curl);
curl_slist_free_all(curl->headers);
}
int landerctl_cmd_short(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 *url = args->args.arr[0];
curl_easy_setopt(curl.curl, CURLOPT_POSTFIELDSIZE, strlen(url));
curl_easy_setopt(curl.curl, CURLOPT_POSTFIELDS, url);
res = landerctl_curl_perform(&curl);
if (res == 0) {
landerctl_curl_inspect(&curl, args);
}
landerctl_curl_cleanup(&curl);
return res;
}

View File

@ -9,162 +9,185 @@
#include "landerctl.h" #include "landerctl.h"
const char *cfg_file_name = ".landerrc";
const char *usage = "%s [-SPFsv] [-c CONFIG_FILE] arg [key]\n";
int main(int argc, char **argv) { int main(int argc, char **argv) {
landerctl_ctx ctx = {0}; landerctl_args args = {0};
const char *home_dir = getenv("HOME"); int res;
const char *cfg_path; if ((res = landerctl_parse_args(&args, argc, argv))) {
return res;
if (home_dir == NULL) {
cfg_path = cfg_file_name;
} else {
// This is a blatant memleak if a custom config file is set, but it really
// doesn't matter for a short-lived CLI tool
char *buf = malloc(strlen(home_dir) + strlen(cfg_file_name) + 2);
sprintf(buf, "%s/%s", home_dir, cfg_file_name);
cfg_path = buf;
} }
opterr = 0; switch (args.mode) {
int c;
while ((c = getopt(argc, argv, "SPFsvc:")) != -1) {
switch (c) {
case 'S':
ctx.mode = landerctl_mode_short;
break;
case 'P':
ctx.mode = landerctl_mode_paste;
break;
case 'F':
ctx.mode = landerctl_mode_file;
break;
case 's':
ctx.secure = true;
break;
case 'v':
ctx.verbose = true;
break;
case 'c':
cfg_path = optarg;
break;
case '?':
printf(usage, argv[0]);
exit(2);
}
}
char *err_msg = NULL;
landerctl_cfg_err parse_res = landerctl_cfg_parse(&ctx.cfg, cfg_path);
switch (parse_res) {
case landerctl_cfg_err_ok:
break;
case landerctl_cfg_err_not_found:
err_msg = "Config file not found";
break;
case landerctl_cfg_err_invalid:
err_msg = "Invalid config file";
break;
case landerctl_cfg_err_incomplete:
err_msg = "Incomplete config file";
break;
}
if (err_msg != NULL) {
fprintf(stderr, "%s\n", err_msg);
exit(1);
}
if (ctx.mode == landerctl_mode_none) {
printf("No mode specified.\n\n");
printf(usage, argv[0]);
exit(2);
}
if (optind == argc || (argc - optind > 2)) {
printf(usage, argv[0]);
exit(2);
}
ctx.arg = argv[optind];
ctx.key = argc - optind == 2 ? argv[optind + 1] : NULL;
curl_global_init(CURL_GLOBAL_ALL);
ctx.curl = curl_easy_init();
if (ctx.curl == NULL) {
exit(255);
}
landerctl_set_common(&ctx);
landerctl_err res;
switch (ctx.mode) {
case landerctl_mode_short: case landerctl_mode_short:
res = landerctl_post_short(&ctx); res = landerctl_cmd_short(&args);
break;
case landerctl_mode_paste:
res = landerctl_post_paste(&ctx);
break;
case landerctl_mode_file:
res = landerctl_post_file(&ctx);
break; break;
/* case landerctl_mode_paste: */
/* res = landerctl_post_paste(&ctx); */
/* break; */
/* case landerctl_mode_file: */
/* res = landerctl_post_file(&ctx); */
/* break; */
default: default:
return 7; res = 7;
break;
} }
if (res != landerctl_err_ok) { return res;
printf("%s\n", landerctl_err_msg(res));
exit(6);
}
if (ctx.verbose) { /* landerctl_ctx ctx = {0}; */
curl_easy_setopt(ctx.curl, CURLOPT_VERBOSE, 1L);
}
curl_easy_setopt(ctx.curl, CURLOPT_HTTPHEADER, ctx.headers); /* const char *home_dir = getenv("HOME"); */
curl_easy_setopt(ctx.curl, CURLOPT_CAINFO, ctx.cfg.ca_certs_bundle); /* const char *cfg_path; */
char curl_err_msg[CURL_ERROR_SIZE]; /* if (home_dir == NULL) { */
curl_easy_setopt(ctx.curl, CURLOPT_ERRORBUFFER, curl_err_msg); /* cfg_path = cfg_file_name; */
/* } else { */
/* // This is a blatant memleak if a custom config file is set, but it
* really */
/* // doesn't matter for a short-lived CLI tool */
/* char *buf = malloc(strlen(home_dir) + strlen(cfg_file_name) + 2); */
/* sprintf(buf, "%s/%s", home_dir, cfg_file_name); */
/* cfg_path = buf; */
/* } */
int exit_code = 0; /* opterr = 0; */
/* int c; */
if (curl_easy_perform(ctx.curl) == CURLE_OK) { /* while ((c = getopt(argc, argv, "SPFsvc:")) != -1) { */
long response_code; /* switch (c) { */
curl_easy_getinfo(ctx.curl, CURLINFO_RESPONSE_CODE, &response_code); /* case 'S': */
/* ctx.mode = landerctl_mode_short; */
/* break; */
/* case 'P': */
/* ctx.mode = landerctl_mode_paste; */
/* break; */
/* case 'F': */
/* ctx.mode = landerctl_mode_file; */
/* break; */
/* case 's': */
/* ctx.secure = true; */
/* break; */
/* case 'v': */
/* ctx.verbose = true; */
/* break; */
/* case 'c': */
/* cfg_path = optarg; */
/* break; */
/* case '?': */
/* printf(usage, argv[0]); */
/* exit(2); */
/* } */
/* } */
if (response_code < 200 || response_code > 299) { /* char *err_msg = NULL; */
fprintf(stderr, "HTTP status code %li\n", response_code); /* landerctl_cfg_err parse_res = landerctl_cfg_parse(&ctx.cfg, cfg_path); */
exit_code = 3;
} else {
struct curl_header *location_header;
if (curl_easy_header(ctx.curl, "Location", 0, CURLH_HEADER, -1, /* switch (parse_res) { */
&location_header) == CURLHE_OK) { /* case landerctl_cfg_err_ok: */
printf("%s%s\n", ctx.cfg.server_url, location_header->value); /* break; */
} else { /* case landerctl_cfg_err_not_found: */
fprintf(stderr, "Server returned a 2xx without a Location header.\n"); /* err_msg = "Config file not found"; */
exit_code = 5; /* break; */
} /* case landerctl_cfg_err_invalid: */
} /* err_msg = "Invalid config file"; */
} else { /* break; */
fprintf(stderr, "Libcurl encountered an error: %s\n", curl_err_msg); /* case landerctl_cfg_err_incomplete: */
exit_code = 4; /* err_msg = "Incomplete config file"; */
} /* break; */
/* } */
curl_easy_cleanup(ctx.curl); /* if (err_msg != NULL) { */
curl_slist_free_all(ctx.headers); /* fprintf(stderr, "%s\n", err_msg); */
/* exit(1); */
/* } */
if (ctx.data_file != NULL) { /* if (ctx.mode == landerctl_mode_none) { */
fclose(ctx.data_file); /* printf("No mode specified.\n\n"); */
} /* printf(usage, argv[0]); */
/* exit(2); */
/* } */
return exit_code; /* if (optind == argc || (argc - optind > 2)) { */
/* printf(usage, argv[0]); */
/* exit(2); */
/* } */
/* ctx.arg = argv[optind]; */
/* ctx.key = argc - optind == 2 ? argv[optind + 1] : NULL; */
/* curl_global_init(CURL_GLOBAL_ALL); */
/* ctx.curl = curl_easy_init(); */
/* if (ctx.curl == NULL) { */
/* exit(255); */
/* } */
/* landerctl_set_common(&ctx); */
/* landerctl_err res; */
/* switch (ctx.mode) { */
/* case landerctl_mode_short: */
/* res = landerctl_post_short(&ctx); */
/* break; */
/* case landerctl_mode_paste: */
/* res = landerctl_post_paste(&ctx); */
/* break; */
/* case landerctl_mode_file: */
/* res = landerctl_post_file(&ctx); */
/* break; */
/* default: */
/* return 7; */
/* } */
/* if (res != landerctl_err_ok) { */
/* printf("%s\n", landerctl_err_msg(res)); */
/* exit(6); */
/* } */
/* if (ctx.verbose) { */
/* curl_easy_setopt(ctx.curl, CURLOPT_VERBOSE, 1L); */
/* } */
/* curl_easy_setopt(ctx.curl, CURLOPT_HTTPHEADER, ctx.headers); */
/* curl_easy_setopt(ctx.curl, CURLOPT_CAINFO, ctx.cfg.ca_certs_bundle); */
/* char curl_err_msg[CURL_ERROR_SIZE]; */
/* curl_easy_setopt(ctx.curl, CURLOPT_ERRORBUFFER, curl_err_msg); */
/* int exit_code = 0; */
/* if (curl_easy_perform(ctx.curl) == CURLE_OK) { */
/* long response_code; */
/* curl_easy_getinfo(ctx.curl, CURLINFO_RESPONSE_CODE, &response_code); */
/* if (response_code < 200 || response_code > 299) { */
/* fprintf(stderr, "HTTP status code %li\n", response_code); */
/* exit_code = 3; */
/* } else { */
/* struct curl_header *location_header; */
/* if (curl_easy_header(ctx.curl, "Location", 0, CURLH_HEADER, -1, */
/* &location_header) == CURLHE_OK) { */
/* printf("%s%s\n", ctx.cfg.server_url, location_header->value); */
/* } else { */
/* fprintf(stderr, "Server returned a 2xx without a Location
* header.\n"); */
/* exit_code = 5; */
/* } */
/* } */
/* } else { */
/* fprintf(stderr, "Libcurl encountered an error: %s\n", curl_err_msg); */
/* exit_code = 4; */
/* } */
/* curl_easy_cleanup(ctx.curl); */
/* curl_slist_free_all(ctx.headers); */
/* if (ctx.data_file != NULL) { */
/* fclose(ctx.data_file); */
/* } */
/* return exit_code; */
/* struct stat sb; */ /* struct stat sb; */