Compare commits

...

3 Commits

2 changed files with 63 additions and 35 deletions

11
encrypted.txt Normal file
View File

@@ -0,0 +1,11 @@
-----BEGIN OTP MESSAGE-----
Version: v0.2.72
Pad-ChkSum: 97d9d82b5414a9439102f3811fb90ab1d6368a00d33229a18b306476f9d04f82
Pad-Offset: 2873419
iR6J7HHK1Oc6
-----END OTP MESSAGE-----

75
otp.c
View File

@@ -134,18 +134,23 @@ int main(int argc, char* argv[]) {
// Load preferences first // Load preferences first
load_preferences(); 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 // Check for OTP thumb drive on startup
char otp_drive_path[512]; char otp_drive_path[512];
if (detect_otp_thumb_drive(otp_drive_path, sizeof(otp_drive_path))) { if (detect_otp_thumb_drive(otp_drive_path, sizeof(otp_drive_path))) {
// 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("Detected OTP thumb drive: %s\n", otp_drive_path);
printf("Using as default pads directory for this session.\n\n"); printf("Using as default pads directory for this session.\n\n");
}
strncpy(current_pads_dir, otp_drive_path, sizeof(current_pads_dir) - 1); strncpy(current_pads_dir, otp_drive_path, sizeof(current_pads_dir) - 1);
current_pads_dir[sizeof(current_pads_dir) - 1] = '\0'; current_pads_dir[sizeof(current_pads_dir) - 1] = '\0';
} }
// Check for piped input if (is_pipe_mode) {
if (argc == 1 && has_stdin_data()) { // No arguments but has piped data - enter pipe mode
// No arguments but has piped data - enter pipe mode for interactive pad selection
char* piped_text = read_stdin_text(); char* piped_text = read_stdin_text();
if (piped_text) { if (piped_text) {
int result = pipe_mode(argc, argv, 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"); printf("Warning: Failed to update state file\n");
} }
// Output in ASCII armor format // 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"); printf("\n\n-----BEGIN OTP MESSAGE-----\n");
} else {
printf("-----BEGIN OTP MESSAGE-----\n");
}
printf("Version: %s\n", get_version()); printf("Version: %s\n", get_version());
printf("Pad-ChkSum: %s\n", chksum_hex); printf("Pad-ChkSum: %s\n", chksum_hex);
printf("Pad-Offset: %lu\n", current_offset); 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("%.64s\n", base64_cipher + i);
} }
if (is_interactive) {
printf("-----END OTP MESSAGE-----\n\n\n"); printf("-----END OTP MESSAGE-----\n\n\n");
} else {
printf("-----END OTP MESSAGE-----\n");
}
// Cleanup // Cleanup
free(pad_data); free(pad_data);
@@ -2409,38 +2425,39 @@ int pipe_mode(int argc, char* argv[], const char* piped_text) {
(void)argc; // Suppress unused parameter warning (void)argc; // Suppress unused parameter warning
(void)argv; // Suppress unused parameter warning (void)argv; // Suppress unused parameter warning
printf("Piped text received: \"%s\"\n\n", piped_text); // Check if we have a default pad configured
char* default_pad = get_default_pad_path();
if (default_pad) {
// Verify the default pad exists and extract checksum
if (access(default_pad, R_OK) == 0) {
// Extract checksum from pad filename
char* filename = strrchr(default_pad, '/');
if (!filename) filename = default_pad;
else filename++; // Skip the '/'
// List available pads for selection // Extract checksum (remove .pad extension)
int pad_count = list_available_pads(); if (strlen(filename) >= 68 && strstr(filename, ".pad")) {
if (pad_count == 0) { char pad_checksum[65];
printf("No pads available. Generate a pad first.\n"); strncpy(pad_checksum, filename, 64);
pad_checksum[64] = '\0';
free(default_pad);
// Encrypt using the default pad (silent mode)
return encrypt_text(pad_checksum, piped_text);
}
}
fprintf(stderr, "Error: Default pad not found or invalid: %s\n", default_pad);
free(default_pad);
return 1; return 1;
} }
// Reopen stdin from the controlling terminal for interactive input fprintf(stderr, "Error: No default pad configured for pipe mode\n");
FILE* tty = fopen("/dev/tty", "r"); fprintf(stderr, "Configure a default pad in ~/.otp/otp.conf\n");
if (!tty) {
printf("Error: Cannot open terminal for input\n");
return 1; 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);
}
// Preferences management functions implementation // Preferences management functions implementation
int load_preferences(void) { int load_preferences(void) {
char* home_dir = getenv("HOME"); char* home_dir = getenv("HOME");