Compare commits

...

11 Commits

Author SHA1 Message Date
498d7d31c4 Version v0.2.62 - Fixed all buffer size warnings by increasing buffer sizes to 1024 bytes 2025-08-14 09:32:39 -04:00
e58f05619e Version v0.2.59 - Fixed all remaining PADS_DIR references and removed old USB scanning code 2025-08-14 09:15:22 -04:00
992b9349b3 Version v0.2.58 - Completed refactoring to new OTP thumb drive detection approach 2025-08-14 09:14:54 -04:00
1f4a1fb90f Version v0.2.56 - Implemented new OTP thumb drive detection and removed old USB scanning approach 2025-08-14 07:33:40 -04:00
c7fae1ad1d Version v0.2.55 - Increased pad_path and state_path buffer sizes to 4096 bytes to eliminate all compile warnings 2025-08-13 20:48:06 -04:00
37bcb6a6d2 Version v0.2.54 - Final fix for remaining compile warning by increasing user_mount_path buffer to 2048 bytes 2025-08-13 18:14:42 -04:00
9ded0aed44 Version v0.2.53 - Complete fix for all compile warnings - enlarged all buffer sizes consistently 2025-08-13 15:42:43 -04:00
4442837ce8 Version v0.2.52 - Final buffer size adjustments to eliminate all compile warnings 2025-08-13 15:42:18 -04:00
31ee220558 Version v0.2.51 - Fixed all struct buffer sizes to eliminate compile warnings 2025-08-13 15:41:54 -04:00
0a25c13b65 Version v0.2.50 - Fixed compile warnings by increasing buffer sizes for USB path handling 2025-08-13 15:41:29 -04:00
fd9d87c548 Version v0.2.49 - Added USB drive detection for pads with state conflict resolution 2025-08-13 15:12:16 -04:00

136
otp.c
View File

@@ -39,15 +39,21 @@ static const int base64_decode_table[256] = {
#define MAX_LINE_LENGTH 1024
#define MAX_HASH_LENGTH 65
#define PROGRESS_UPDATE_INTERVAL (64 * 1024 * 1024) // 64MB intervals
#define PADS_DIR "pads"
#define DEFAULT_PADS_DIR "pads"
#define FILES_DIR "files"
#define MAX_ENTROPY_BUFFER 32768 // 32KB entropy buffer
// Global variable for current pads directory (can be local or OTP thumb drive)
static char current_pads_dir[512] = DEFAULT_PADS_DIR;
// Function prototypes
int main(int argc, char* argv[]);
int interactive_mode(void);
int command_line_mode(int argc, char* argv[]);
// OTP thumb drive detection function
int detect_otp_thumb_drive(char* otp_drive_path, size_t path_size);
// Editor and file manager functions
char* get_preferred_editor(void);
char* get_preferred_file_manager(void);
@@ -109,6 +115,15 @@ 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[]) {
// 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");
strncpy(current_pads_dir, otp_drive_path, sizeof(current_pads_dir) - 1);
current_pads_dir[sizeof(current_pads_dir) - 1] = '\0';
}
if (argc == 1) {
return interactive_mode();
} else {
@@ -632,9 +647,9 @@ uint64_t parse_size_string(const char* size_str) {
}
char* find_pad_by_prefix(const char* prefix) {
DIR* dir = opendir(PADS_DIR);
DIR* dir = opendir(current_pads_dir);
if (!dir) {
printf("Error: Cannot open pads directory\n");
printf("Error: Cannot open pads directory %s\n", current_pads_dir);
return NULL;
}
@@ -668,10 +683,9 @@ char* find_pad_by_prefix(const char* prefix) {
int current = 0;
rewinddir(dir);
while ((entry = readdir(dir)) != NULL) {
// Skip . and .. entries, and only process .pad files
if (entry->d_name[0] == '.') continue;
if (!strstr(entry->d_name, ".pad")) continue;
if (strlen(entry->d_name) != 68) continue; // 64 char chksum + ".pad"
if (strlen(entry->d_name) != 68) continue;
current++;
if (current == selection) {
@@ -732,9 +746,9 @@ char* find_pad_by_prefix(const char* prefix) {
}
int list_available_pads(void) {
DIR* dir = opendir(PADS_DIR);
DIR* dir = opendir(current_pads_dir);
if (!dir) {
printf("Error: Cannot open pads directory\n");
printf("Error: Cannot open pads directory %s\n", current_pads_dir);
return 0;
}
@@ -753,8 +767,8 @@ int list_available_pads(void) {
chksum[64] = '\0';
// Get pad file size
char full_path[300]; // Increased buffer size to accommodate longer paths
snprintf(full_path, sizeof(full_path), "%s/%s", PADS_DIR, entry->d_name);
char full_path[1024]; // Increased buffer size to accommodate longer paths
snprintf(full_path, sizeof(full_path), "%s/%s", current_pads_dir, entry->d_name);
struct stat st;
if (stat(full_path, &st) == 0) {
// Get used bytes from state
@@ -2081,8 +2095,8 @@ int decrypt_ascii_file(const char* input_file, const char* output_file) {
}
int read_state_offset(const char* pad_chksum, uint64_t* offset) {
char state_filename[MAX_HASH_LENGTH + 20];
snprintf(state_filename, sizeof(state_filename), "%s/%s.state", PADS_DIR, pad_chksum);
char state_filename[1024];
snprintf(state_filename, sizeof(state_filename), "%s/%s.state", current_pads_dir, pad_chksum);
FILE* state_file = fopen(state_filename, "rb");
if (!state_file) {
@@ -2101,8 +2115,8 @@ int read_state_offset(const char* pad_chksum, uint64_t* offset) {
}
int write_state_offset(const char* pad_chksum, uint64_t offset) {
char state_filename[MAX_HASH_LENGTH + 20];
snprintf(state_filename, sizeof(state_filename), "%s/%s.state", PADS_DIR, pad_chksum);
char state_filename[1024];
snprintf(state_filename, sizeof(state_filename), "%s/%s.state", current_pads_dir, pad_chksum);
FILE* state_file = fopen(state_filename, "wb");
if (!state_file) {
@@ -2243,8 +2257,8 @@ int collect_keyboard_entropy(unsigned char* entropy_buffer, size_t max_size, siz
// Directory management functions
int ensure_pads_directory(void) {
struct stat st = {0};
if (stat(PADS_DIR, &st) == -1) {
if (mkdir(PADS_DIR, 0755) != 0) {
if (stat(current_pads_dir, &st) == -1) {
if (mkdir(current_pads_dir, 0755) != 0) {
return 1;
}
}
@@ -2275,10 +2289,74 @@ void get_default_file_path(const char* filename, char* result_path, size_t resul
}
void get_pad_path(const char* chksum, char* pad_path, char* state_path) {
snprintf(pad_path, MAX_HASH_LENGTH + 20, "%s/%s.pad", PADS_DIR, chksum);
snprintf(state_path, MAX_HASH_LENGTH + 20, "%s/%s.state", PADS_DIR, chksum);
snprintf(pad_path, 1024, "%s/%s.pad", current_pads_dir, chksum);
snprintf(state_path, 1024, "%s/%s.state", current_pads_dir, chksum);
}
// 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};
for (int mount_idx = 0; mount_dirs[mount_idx] != NULL; mount_idx++) {
DIR* mount_dir = opendir(mount_dirs[mount_idx]);
if (!mount_dir) continue;
struct dirent* mount_entry;
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) {
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 {
// 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
}
}
}
closedir(mount_dir);
}
return 0; // No OTP drive found
}
// Custom base64 encode function
@@ -2832,10 +2910,10 @@ int handle_file_encrypt(void) {
int handle_pads_menu(void) {
printf("\n=== Pad Management ===\n");
// Get list of all pads
DIR* dir = opendir(PADS_DIR);
// Get list of pads from current directory
DIR* dir = opendir(current_pads_dir);
if (!dir) {
printf("Error: Cannot open pads directory\n");
printf("Error: Cannot open pads directory %s\n", current_pads_dir);
return 1;
}
@@ -2845,6 +2923,7 @@ int handle_pads_menu(void) {
char size_str[32];
char used_str[32];
double percentage;
char location[256]; // Store location info
};
struct PadInfo pads[100]; // Support up to 100 pads
@@ -2858,8 +2937,8 @@ int handle_pads_menu(void) {
pads[pad_count].chksum[64] = '\0';
// Get pad file size and usage info
char full_path[300];
snprintf(full_path, sizeof(full_path), "%s/%s", PADS_DIR, entry->d_name);
char full_path[512];
snprintf(full_path, sizeof(full_path), "%s/%s", current_pads_dir, entry->d_name);
struct stat st;
if (stat(full_path, &st) == 0) {
// Get used bytes from state
@@ -2891,6 +2970,9 @@ int handle_pads_menu(void) {
// Calculate percentage
pads[pad_count].percentage = (double)used_bytes / st.st_size * 100.0;
// Set location info using directory display
get_directory_display(full_path, pads[pad_count].location, sizeof(pads[pad_count].location));
pad_count++;
}
}
@@ -2950,12 +3032,6 @@ int handle_pads_menu(void) {
printf("%-8s %-12s %-12s %-12s %-8s\n", "--------", "------------", "----------", "----------", "------");
for (int i = 0; i < pad_count; i++) {
// Get directory information for this pad
char full_path[300];
snprintf(full_path, sizeof(full_path), "%s/%s.pad", PADS_DIR, pads[i].chksum);
char dir_display[13]; // 12 chars + null terminator
get_directory_display(full_path, dir_display, sizeof(dir_display));
// Display first 8 characters of checksum with prefix underlined
char checksum_8char[9];
strncpy(checksum_8char, pads[i].chksum, 8);
@@ -2964,7 +3040,7 @@ int handle_pads_menu(void) {
printf("\033[4m%.*s\033[0m%s %-12s %-12s %-12s %.1f%%\n",
prefix_lengths[i], checksum_8char, // Underlined prefix
checksum_8char + prefix_lengths[i], // Rest of 8-char checksum
dir_display,
pads[i].location, // Use the stored location info
pads[i].size_str,
pads[i].used_str,
pads[i].percentage);
@@ -3122,7 +3198,7 @@ void get_directory_display(const char* file_path, char* result, size_t result_si
}
// Current working directory
if (strcmp(dir_path, ".") == 0 || strcmp(dir_path, PADS_DIR) == 0) {
if (strcmp(dir_path, ".") == 0 || strcmp(dir_path, current_pads_dir) == 0) {
strncpy(result, "pads", result_size - 1);
result[result_size - 1] = '\0';
return;