Skip to content

Commit 6022a4b

Browse files
A6GibKmbilelmoussaoui
authored andcommitted
server: Do not exit if the credential is incorrect
The credential loaded by the ImportCredential= property might be wrong, e.g. perhaps because the keyring's password changed and there is a trailing credential in the credstore. Loading credentials at the moment is a fallback and should not exit if the password therein is incorrect.
1 parent 8891a9c commit 6022a4b

1 file changed

Lines changed: 31 additions & 13 deletions

File tree

server/src/main.rs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,17 @@ struct Args {
4141
is_verbose: bool,
4242
}
4343

44+
/// Whether the daemon should exit if the password provided for unlocking the
45+
/// session keyring is incorrect.
46+
enum ShouldErrorOut {
47+
Yes,
48+
No,
49+
}
50+
4451
async fn inner_main(args: Args) -> Result<(), Error> {
4552
capability::drop_unnecessary_capabilities()?;
4653

47-
let secret = if args.login {
54+
let secret_info = if args.login {
4855
let mut stdin = std::io::stdin().lock();
4956
if stdin.is_terminal() {
5057
let password = rpassword::prompt_password("Enter the login password: ")?;
@@ -53,19 +60,15 @@ async fn inner_main(args: Args) -> Result<(), Error> {
5360
return Err(Error::EmptyPassword);
5461
}
5562

56-
Some(oo7::Secret::text(password))
63+
Some((oo7::Secret::text(password), ShouldErrorOut::Yes))
5764
} else {
5865
let mut buff = vec![];
5966
stdin.read_to_end(&mut buff)?;
6067

61-
Some(oo7::Secret::from(buff))
68+
Some((oo7::Secret::from(buff), ShouldErrorOut::No))
6269
}
6370
} else if let Ok(credential_dir) = std::env::var("CREDENTIALS_DIRECTORY") {
6471
// We try to unlock the login keyring with a system credential.
65-
//
66-
// FIXME We should not exit the service if the provided password is not
67-
// the correct one in this branch. The credential might run out of sync
68-
// and this is only a fallback.
6972
let mut contents = Vec::new();
7073
let cred_path = Path::new(&credential_dir).join("oo7.keyring-encryption-password");
7174

@@ -74,7 +77,7 @@ async fn inner_main(args: Args) -> Result<(), Error> {
7477
tracing::info!("Unlocking session keyring with user's systemd credentials");
7578
cred_file.read_to_end(&mut contents).await?;
7679
let secret = oo7::Secret::from(contents);
77-
Some(secret)
80+
Some((secret, ShouldErrorOut::No))
7881
}
7982
Err(err) if err.kind() == std::io::ErrorKind::NotFound => None,
8083
Err(err) => {
@@ -88,15 +91,30 @@ async fn inner_main(args: Args) -> Result<(), Error> {
8891

8992
tracing::info!("Starting {BINARY_NAME}");
9093

91-
Service::run(secret, args.replace)
92-
.await
93-
.inspect_err(|err| {
94-
if let Error::Zbus(zbus::Error::NameTaken) = err {
94+
if let Some((secret, should_error_out)) = secret_info {
95+
let res = Service::run(Some(secret), args.replace).await;
96+
match res {
97+
Ok(()) => (),
98+
// TODO Create a unit test that verifies that this is the correct
99+
// error type.
100+
Err(Error::File(oo7::file::Error::ChecksumMismatch))
101+
if matches!(should_error_out, ShouldErrorOut::No) =>
102+
{
103+
tracing::warn!(
104+
"Failed to unlock session keyring: credential contains wrong password"
105+
)
106+
}
107+
Err(Error::Zbus(zbus::Error::NameTaken)) if !args.replace => {
95108
tracing::error!(
96109
"There is an instance already running. Run with --replace to replace it."
97110
);
111+
Err(Error::Zbus(zbus::Error::NameTaken))?
98112
}
99-
})?;
113+
Err(err) => Err(err)?,
114+
}
115+
} else {
116+
Service::run(None, args.replace).await?;
117+
}
100118

101119
tracing::debug!("Starting loop");
102120

0 commit comments

Comments
 (0)