481 {
482 session = {};
483
484 if (result.
type.find(
"LUKS") != std::string::npos) {
485 std::string err_keyfile;
488 return true;
489 }
490 const std::string passphrase =
getenv_str(
"RECOVERY_LUKS_PASSPHRASE");
492 if (!err_keyfile.empty()) error = err_keyfile + "; " + error;
493 return false;
494 }
495
496 if (result.
type.find(
"BitLocker") != std::string::npos) {
497 std::string err_password;
498 if (
const std::string password =
getenv_str(
"RECOVERY_BITLOCKER_PASSWORD");
500 return true;
501 }
502 std::string err_recovery;
503 if (
const std::string recovery_key =
getenv_str(
"RECOVERY_BITLOCKER_RECOVERY_KEY");
505 return true;
506 }
507 std::string err_bek;
508 if (
const std::string bek_file =
getenv_existing_file(
"RECOVERY_BITLOCKER_BEK_FILE"); !bek_file.empty()) {
509
510 std::string open_target = source_path;
513 error = err_bek;
514 return false;
515 }
517 }
519 error = "dislocker or losetup not available";
520 return false;
521 }
522 const std::string mapper =
"recovery_bitlocker_" +
sanitize_name(std::to_string(::getpid()) +
"_" + std::to_string(result.
offset_bytes));
523 fs::path mount_dir = fs::temp_directory_path() / (mapper + "_dislocker");
524 fs::create_directories(mount_dir);
525 std::ostringstream cmd;
526 cmd <<
"dislocker -r -V " <<
shell_quote(open_target) <<
' '
529 std::string out;
530 int rc = 0;
532 const fs::path dislocker_file = mount_dir / "dislocker-file";
533 std::string loop;
534 if (fs::exists(dislocker_file) &&
create_loop_for_offset(dislocker_file.string(), 0, loop, logs, err_bek)) {
543 logs.push_back(
trs(
"Unlocked BitLocker container through BEK file:") + std::string(
" ") + session.
mapped_path);
544 return true;
545 }
546 } else {
547 err_bek =
trim_copy(out.empty() ?
"dislocker failed" : out);
548 }
549 }
550 error = !err_password.empty() ? err_password : (!err_recovery.empty() ? err_recovery : err_bek);
551 if (error.empty()) error = "no BitLocker credential environment variable provided";
552 return false;
553 }
554
555 error = "unsupported encrypted container type";
556 return false;
557}
std::string shell_quote(const std::string &argument)
std::string trim_copy(std::string value)
bool dislocker_open(const std::string &source_path, uint64_t offset, const std::string &credential, bool use_recovery_key, UnlockSession &session, std::vector< std::string > &logs, std::string &error)
bool cryptsetup_open(const std::string &source_path, uint64_t offset, const std::string &passphrase, UnlockSession &session, std::vector< std::string > &logs, std::string &error)
bool cryptsetup_open_with_keyfile(const std::string &source_path, uint64_t offset, const std::string &keyfile, UnlockSession &session, std::vector< std::string > &logs, std::string &error)
bool create_loop_for_offset(const std::string &source_path, uint64_t offset, std::string &loop_path, std::vector< std::string > &logs, std::string &error)
bool run_command_capture(const std::string &cmd, std::string &output, int &rc)
std::string getenv_str(const char *name)
std::string getenv_existing_file(const char *name)
bool command_exists(const std::string &name)
std::string helper_loop_path
std::string container_type
std::string helper_mount_dir