Compare commits

...

8 Commits

430
otp.c
View File

@@ -46,10 +46,26 @@ static const int base64_decode_table[256] = {
// Global variable for current pads directory (can be local or OTP thumb drive)
static char current_pads_dir[512] = DEFAULT_PADS_DIR;
// Global variable for default pad path from preferences
static char default_pad_path[1024] = "";
// Function prototypes
int main(int argc, char* argv[]);
int interactive_mode(void);
int command_line_mode(int argc, char* argv[]);
int pipe_mode(int argc, char* argv[], const char* piped_text);
// Stdin detection functions
int has_stdin_data(void);
char* read_stdin_text(void);
// Preferences management functions
int load_preferences(void);
int save_preferences(void);
char* get_preference(const char* key);
int set_preference(const char* key, const char* value);
char* get_default_pad_path(void);
int set_default_pad_path(const char* pad_path);
// OTP thumb drive detection function
int detect_otp_thumb_drive(char* otp_drive_path, size_t path_size);
@@ -115,6 +131,9 @@ void get_directory_display(const char* file_path, char* result, size_t result_si
void print_usage(const char* program_name);
int main(int argc, char* argv[]) {
// Load preferences first
load_preferences();
// Check for OTP thumb drive on startup
char otp_drive_path[512];
if (detect_otp_thumb_drive(otp_drive_path, sizeof(otp_drive_path))) {
@@ -124,7 +143,18 @@ int main(int argc, char* argv[]) {
current_pads_dir[sizeof(current_pads_dir) - 1] = '\0';
}
if (argc == 1) {
// Check for piped input
if (argc == 1 && has_stdin_data()) {
// No arguments but has piped data - enter pipe mode for interactive pad selection
char* piped_text = read_stdin_text();
if (piped_text) {
int result = pipe_mode(argc, argv, piped_text);
free(piped_text);
return result;
}
// If reading stdin failed, fall back to interactive mode
return interactive_mode();
} else if (argc == 1) {
return interactive_mode();
} else {
return command_line_mode(argc, argv);
@@ -132,41 +162,42 @@ int main(int argc, char* argv[]) {
}
int interactive_mode(void) {
// printf("\n\n\n\n=== OTP Cipher %s ===\n\n", get_version());
char input[10];
while (1) {
show_main_menu();
char input[10];
if (fgets(input, sizeof(input), stdin)) {
char choice = toupper(input[0]);
switch (choice) {
case 'T':
handle_text_encrypt();
break;
case 'F':
handle_file_encrypt();
break;
case 'D':
handle_decrypt_menu();
break;
case 'P':
handle_pads_menu();
break;
case 'X':
case 'Q':
printf("Goodbye!\n");
return 0;
default:
printf("Invalid option. Please select T, F, D, P, or X.\n");
continue;
}
} else {
printf("Error reading input. Please try again.\n");
continue;
if (!fgets(input, sizeof(input), stdin)) {
printf("Goodbye!\n");
break;
}
char choice = toupper(input[0]);
switch (choice) {
case 'T':
handle_text_encrypt();
break;
case 'F':
handle_file_encrypt();
break;
case 'D':
handle_decrypt_menu();
break;
case 'P':
handle_pads_menu();
break;
case 'X':
case 'Q':
printf("Goodbye!\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
break;
}
printf("\n");
}
return 0;
}
int command_line_mode(int argc, char* argv[]) {
@@ -1127,10 +1158,39 @@ int generate_pad_with_entropy(uint64_t size_bytes, int display_progress, int use
// Get final paths in pads directory
get_pad_path(chksum_hex, pad_path, state_path);
// Try rename first (works for same filesystem)
if (rename(temp_filename, pad_path) != 0) {
printf("Error: Cannot move pad file to pads directory\n");
// If rename fails, try copy and delete (works across filesystems)
FILE* temp_file = fopen(temp_filename, "rb");
FILE* dest_file = fopen(pad_path, "wb");
if (!temp_file || !dest_file) {
printf("Error: Cannot copy pad file to pads directory\n");
if (temp_file) fclose(temp_file);
if (dest_file) fclose(dest_file);
unlink(temp_filename);
return 1;
}
// Copy file in chunks
unsigned char copy_buffer[64 * 1024];
size_t bytes_read;
while ((bytes_read = fread(copy_buffer, 1, sizeof(copy_buffer), temp_file)) > 0) {
if (fwrite(copy_buffer, 1, bytes_read, dest_file) != bytes_read) {
printf("Error: Failed to copy pad file to pads directory\n");
fclose(temp_file);
fclose(dest_file);
unlink(temp_filename);
unlink(pad_path);
return 1;
}
}
fclose(temp_file);
fclose(dest_file);
// Remove temporary file after successful copy
unlink(temp_filename);
return 1;
}
// Set pad file to read-only
@@ -2293,6 +2353,211 @@ void get_pad_path(const char* chksum, char* pad_path, char* state_path) {
snprintf(state_path, 1024, "%s/%s.state", current_pads_dir, chksum);
}
// Stdin detection functions implementation
int has_stdin_data(void) {
// Check if stdin is a pipe/redirect (not a terminal)
if (!isatty(STDIN_FILENO)) {
return 1;
}
return 0;
}
char* read_stdin_text(void) {
size_t capacity = 4096;
size_t length = 0;
char* buffer = malloc(capacity);
if (!buffer) {
return NULL;
}
char chunk[1024];
while (fgets(chunk, sizeof(chunk), stdin)) {
size_t chunk_len = strlen(chunk);
// Ensure we have enough capacity
while (length + chunk_len >= capacity) {
capacity *= 2;
char* new_buffer = realloc(buffer, capacity);
if (!new_buffer) {
free(buffer);
return NULL;
}
buffer = new_buffer;
}
strcpy(buffer + length, chunk);
length += chunk_len;
}
// Remove trailing newline if present
if (length > 0 && buffer[length - 1] == '\n') {
buffer[length - 1] = '\0';
length--;
}
// If empty, free and return NULL
if (length == 0) {
free(buffer);
return NULL;
}
return buffer;
}
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\n", piped_text);
// List available pads for 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);
}
// Preferences management functions implementation
int load_preferences(void) {
char* home_dir = getenv("HOME");
if (!home_dir) {
return 1; // No home directory
}
char preferences_dir[1024];
char preferences_file[1024];
snprintf(preferences_dir, sizeof(preferences_dir), "%s/.otp", home_dir);
snprintf(preferences_file, sizeof(preferences_file), "%s/otp.conf", preferences_dir);
FILE* file = fopen(preferences_file, "r");
if (!file) {
return 0; // No preferences file, use defaults
}
char line[1024];
while (fgets(line, sizeof(line), file)) {
// Remove newline
line[strcspn(line, "\n")] = 0;
// Skip empty lines and comments
if (strlen(line) == 0 || line[0] == '#') {
continue;
}
// Parse key=value pairs
char* equals = strchr(line, '=');
if (equals) {
*equals = '\0';
char* key = line;
char* value = equals + 1;
// Trim whitespace
while (*key == ' ' || *key == '\t') key++;
while (*value == ' ' || *value == '\t') value++;
if (strcmp(key, "default_pad") == 0) {
strncpy(default_pad_path, value, sizeof(default_pad_path) - 1);
default_pad_path[sizeof(default_pad_path) - 1] = '\0';
}
}
}
fclose(file);
return 0;
}
int save_preferences(void) {
char* home_dir = getenv("HOME");
if (!home_dir) {
return 1;
}
char preferences_dir[1024];
char preferences_file[1024];
snprintf(preferences_dir, sizeof(preferences_dir), "%s/.otp", home_dir);
snprintf(preferences_file, sizeof(preferences_file), "%s/otp.conf", preferences_dir);
// Create .otp directory if it doesn't exist
struct stat st = {0};
if (stat(preferences_dir, &st) == -1) {
if (mkdir(preferences_dir, 0755) != 0) {
return 1;
}
}
FILE* file = fopen(preferences_file, "w");
if (!file) {
return 1;
}
fprintf(file, "# OTP Preferences File\n");
fprintf(file, "# This file is automatically generated and updated by the OTP program\n\n");
if (strlen(default_pad_path) > 0) {
fprintf(file, "default_pad=%s\n", default_pad_path);
}
fclose(file);
return 0;
}
char* get_preference(const char* key) {
if (strcmp(key, "default_pad") == 0) {
if (strlen(default_pad_path) > 0) {
return strdup(default_pad_path);
}
}
return NULL;
}
int set_preference(const char* key, const char* value) {
if (strcmp(key, "default_pad") == 0) {
if (value) {
strncpy(default_pad_path, value, sizeof(default_pad_path) - 1);
default_pad_path[sizeof(default_pad_path) - 1] = '\0';
} else {
default_pad_path[0] = '\0';
}
return save_preferences();
}
return 1;
}
char* get_default_pad_path(void) {
if (strlen(default_pad_path) > 0) {
return strdup(default_pad_path);
}
return NULL;
}
int set_default_pad_path(const char* pad_path) {
return set_preference("default_pad", pad_path);
}
// OTP thumb drive detection function implementation
int detect_otp_thumb_drive(char* otp_drive_path, size_t path_size) {
const char* mount_dirs[] = {"/media", "/run/media", "/mnt", NULL};
@@ -2305,14 +2570,39 @@ int detect_otp_thumb_drive(char* otp_drive_path, size_t path_size) {
while ((mount_entry = readdir(mount_dir)) != NULL) {
if (mount_entry->d_name[0] == '.') continue;
// Check if drive name starts with "OTP"
if (strncmp(mount_entry->d_name, "OTP", 3) != 0) continue;
char mount_path[512];
snprintf(mount_path, sizeof(mount_path), "%s/%s", mount_dirs[mount_idx], mount_entry->d_name);
// For /run/media, we need to go one level deeper (skip username)
if (strcmp(mount_dirs[mount_idx], "/run/media") == 0) {
// For /media, we need to go one level deeper (user directories)
if (strcmp(mount_dirs[mount_idx], "/media") == 0) {
// This is /media/[username] - look inside for drives
DIR* user_dir = opendir(mount_path);
if (!user_dir) continue;
struct dirent* user_entry;
while ((user_entry = readdir(user_dir)) != NULL) {
if (user_entry->d_name[0] == '.') continue;
// Check if drive name starts with "OTP"
if (strncmp(user_entry->d_name, "OTP", 3) != 0) continue;
char user_mount_path[512];
snprintf(user_mount_path, sizeof(user_mount_path), "%s/%s", mount_path, user_entry->d_name);
// Check if this is a readable directory
DIR* drive_dir = opendir(user_mount_path);
if (drive_dir) {
closedir(drive_dir);
strncpy(otp_drive_path, user_mount_path, path_size - 1);
otp_drive_path[path_size - 1] = '\0';
closedir(user_dir);
closedir(mount_dir);
return 1; // Found OTP drive
}
}
closedir(user_dir);
} else if (strcmp(mount_dirs[mount_idx], "/run/media") == 0) {
// For /run/media, we need to go one level deeper (skip username)
DIR* user_dir = opendir(mount_path);
if (!user_dir) continue;
@@ -2339,14 +2629,17 @@ int detect_otp_thumb_drive(char* otp_drive_path, size_t path_size) {
}
closedir(user_dir);
} else {
// Direct mount point (like /media/OTP_DRIVE or /mnt/OTP_DRIVE)
DIR* drive_dir = opendir(mount_path);
if (drive_dir) {
closedir(drive_dir);
strncpy(otp_drive_path, mount_path, path_size - 1);
otp_drive_path[path_size - 1] = '\0';
closedir(mount_dir);
return 1; // Found OTP drive
// Direct mount point (like /mnt/OTP_DRIVE)
// Check if drive name starts with "OTP"
if (strncmp(mount_entry->d_name, "OTP", 3) == 0) {
DIR* drive_dir = opendir(mount_path);
if (drive_dir) {
closedir(drive_dir);
strncpy(otp_drive_path, mount_path, path_size - 1);
otp_drive_path[path_size - 1] = '\0';
closedir(mount_dir);
return 1; // Found OTP drive
}
}
}
}
@@ -2988,10 +3281,15 @@ int handle_pads_menu(void) {
char input[10];
if (fgets(input, sizeof(input), stdin)) {
char choice = toupper(input[0]);
if (choice == 'G') {
return handle_generate_menu();
char choice = toupper(input[0]);
if (choice == 'G') {
int result = handle_generate_menu();
if (result == 0) {
// After successful pad generation, return to pads menu
return handle_pads_menu();
}
return result;
}
}
return 0;
}
@@ -3060,7 +3358,12 @@ int handle_pads_menu(void) {
// Handle actions first
if (toupper(input[0]) == 'G') {
return handle_generate_menu();
int result = handle_generate_menu();
if (result == 0) {
// After successful pad generation, return to pads menu
return handle_pads_menu();
}
return result;
} else if (toupper(input[0]) == 'B') {
return 0; // Back to main menu
}
@@ -3147,8 +3450,15 @@ void get_directory_display(const char* file_path, char* result, size_t result_si
if (path_after_media) {
path_after_media++; // Skip the slash
// For /media/user/LABEL pattern, skip the username to get to the drive label
if (strstr(media_start, "/media/")) {
char* next_slash = strchr(path_after_media, '/');
if (next_slash) {
path_after_media = next_slash + 1;
}
}
// For /run/media/user/LABEL pattern, skip the username
if (strstr(media_start, "/run/media/")) {
else if (strstr(media_start, "/run/media/")) {
char* next_slash = strchr(path_after_media, '/');
if (next_slash) {
path_after_media = next_slash + 1;
@@ -3157,15 +3467,23 @@ void get_directory_display(const char* file_path, char* result, size_t result_si
// Extract just the USB label (up to next slash or end)
char* label_end = strchr(path_after_media, '/');
char usb_label[32];
if (label_end) {
size_t label_len = label_end - path_after_media;
if (label_len > 11) label_len = 11; // Max 11 chars for display
strncpy(result, path_after_media, label_len);
result[label_len] = '\0';
if (label_len > sizeof(usb_label) - 1) label_len = sizeof(usb_label) - 1;
strncpy(usb_label, path_after_media, label_len);
usb_label[label_len] = '\0';
} else {
// USB label is the last part
strncpy(result, path_after_media, result_size - 1);
result[result_size - 1] = '\0';
strncpy(usb_label, path_after_media, sizeof(usb_label) - 1);
usb_label[sizeof(usb_label) - 1] = '\0';
}
// Format with USB: prefix, limiting total length to fit in result
snprintf(result, result_size, "USB:%s", usb_label);
// Truncate if too long
if (strlen(result) > 11) {
result[11] = '\0';
}
return;
}