|
|
|
@@ -134,18 +134,23 @@ int main(int argc, char* argv[]) {
|
|
|
|
|
// Load preferences first
|
|
|
|
|
load_preferences();
|
|
|
|
|
|
|
|
|
|
// Check for piped input first (before any output)
|
|
|
|
|
int is_pipe_mode = (argc == 1 && has_stdin_data());
|
|
|
|
|
|
|
|
|
|
// Check for OTP thumb drive on startup
|
|
|
|
|
char otp_drive_path[512];
|
|
|
|
|
if (detect_otp_thumb_drive(otp_drive_path, sizeof(otp_drive_path))) {
|
|
|
|
|
printf("Detected OTP thumb drive: %s\n", otp_drive_path);
|
|
|
|
|
printf("Using as default pads directory for this session.\n\n");
|
|
|
|
|
// Only show messages in interactive/command mode, not pipe mode
|
|
|
|
|
if (!is_pipe_mode) {
|
|
|
|
|
printf("Detected OTP thumb drive: %s\n", otp_drive_path);
|
|
|
|
|
printf("Using as default pads directory for this session.\n\n");
|
|
|
|
|
}
|
|
|
|
|
strncpy(current_pads_dir, otp_drive_path, sizeof(current_pads_dir) - 1);
|
|
|
|
|
current_pads_dir[sizeof(current_pads_dir) - 1] = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for piped input
|
|
|
|
|
if (argc == 1 && has_stdin_data()) {
|
|
|
|
|
// No arguments but has piped data - enter pipe mode for interactive pad selection
|
|
|
|
|
if (is_pipe_mode) {
|
|
|
|
|
// No arguments but has piped data - enter pipe mode
|
|
|
|
|
char* piped_text = read_stdin_text();
|
|
|
|
|
if (piped_text) {
|
|
|
|
|
int result = pipe_mode(argc, argv, piped_text);
|
|
|
|
@@ -1385,8 +1390,15 @@ int encrypt_text(const char* pad_identifier, const char* input_text) {
|
|
|
|
|
printf("Warning: Failed to update state file\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Output in ASCII armor format
|
|
|
|
|
printf("\n\n-----BEGIN OTP MESSAGE-----\n");
|
|
|
|
|
// Output in ASCII armor format - clean format for piping, spaced format for interactive
|
|
|
|
|
int is_interactive = (input_text == NULL); // Interactive if no input_text provided
|
|
|
|
|
|
|
|
|
|
if (is_interactive) {
|
|
|
|
|
printf("\n\n-----BEGIN OTP MESSAGE-----\n");
|
|
|
|
|
} else {
|
|
|
|
|
printf("-----BEGIN OTP MESSAGE-----\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("Version: %s\n", get_version());
|
|
|
|
|
printf("Pad-ChkSum: %s\n", chksum_hex);
|
|
|
|
|
printf("Pad-Offset: %lu\n", current_offset);
|
|
|
|
@@ -1398,7 +1410,11 @@ int encrypt_text(const char* pad_identifier, const char* input_text) {
|
|
|
|
|
printf("%.64s\n", base64_cipher + i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("-----END OTP MESSAGE-----\n\n\n");
|
|
|
|
|
if (is_interactive) {
|
|
|
|
|
printf("-----END OTP MESSAGE-----\n\n\n");
|
|
|
|
|
} else {
|
|
|
|
|
printf("-----END OTP MESSAGE-----\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
|
free(pad_data);
|
|
|
|
@@ -2409,8 +2425,6 @@ int pipe_mode(int argc, char* argv[], const char* piped_text) {
|
|
|
|
|
(void)argc; // Suppress unused parameter warning
|
|
|
|
|
(void)argv; // Suppress unused parameter warning
|
|
|
|
|
|
|
|
|
|
printf("Piped text received: \"%s\"\n", piped_text);
|
|
|
|
|
|
|
|
|
|
// Check if we have a default pad configured
|
|
|
|
|
char* default_pad = get_default_pad_path();
|
|
|
|
|
if (default_pad) {
|
|
|
|
@@ -2427,49 +2441,21 @@ int pipe_mode(int argc, char* argv[], const char* piped_text) {
|
|
|
|
|
strncpy(pad_checksum, filename, 64);
|
|
|
|
|
pad_checksum[64] = '\0';
|
|
|
|
|
|
|
|
|
|
printf("Using default pad: %.16s...\n\n", pad_checksum);
|
|
|
|
|
free(default_pad);
|
|
|
|
|
|
|
|
|
|
// Encrypt using the default pad
|
|
|
|
|
// Encrypt using the default pad (silent mode)
|
|
|
|
|
return encrypt_text(pad_checksum, piped_text);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("Warning: Default pad not found or invalid: %s\n", default_pad);
|
|
|
|
|
fprintf(stderr, "Error: Default pad not found or invalid: %s\n", default_pad);
|
|
|
|
|
free(default_pad);
|
|
|
|
|
// Fall through to interactive selection
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("No default pad configured.\n\n");
|
|
|
|
|
|
|
|
|
|
// List available pads for interactive selection
|
|
|
|
|
int pad_count = list_available_pads();
|
|
|
|
|
if (pad_count == 0) {
|
|
|
|
|
printf("No pads available. Generate a pad first.\n");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reopen stdin from the controlling terminal for interactive input
|
|
|
|
|
FILE* tty = fopen("/dev/tty", "r");
|
|
|
|
|
if (!tty) {
|
|
|
|
|
printf("Error: Cannot open terminal for input\n");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("\nEnter pad selection (number, checksum, or prefix): ");
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
|
|
char pad_input[MAX_HASH_LENGTH];
|
|
|
|
|
if (!fgets(pad_input, sizeof(pad_input), tty)) {
|
|
|
|
|
printf("Error: Failed to read pad selection\n");
|
|
|
|
|
fclose(tty);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
pad_input[strcspn(pad_input, "\n")] = 0;
|
|
|
|
|
fclose(tty);
|
|
|
|
|
|
|
|
|
|
// Encrypt the piped text
|
|
|
|
|
return encrypt_text(pad_input, piped_text);
|
|
|
|
|
fprintf(stderr, "Error: No default pad configured for pipe mode\n");
|
|
|
|
|
fprintf(stderr, "Configure a default pad in ~/.otp/otp.conf\n");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Preferences management functions implementation
|
|
|
|
|