Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions projects/start-wrt/API_CONTRACT.md
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,9 @@ struct Device {
/// (`devices.set-dns-injection`; `vpn-server.peer-set-dns-injection` for a
/// VPN peer).
allow_dns_injection: bool,
/// Whether the router refused this device's DNS records for lack of that
/// permission in the last half hour.
dns_injection_requested: bool,
/// Whether the router forwards this device's IPv6 router advertisements
/// to the rest of the LAN (`devices.set-router-advertisements`). Always
/// false for VPN peers.
Expand Down
62 changes: 54 additions & 8 deletions projects/start-wrt/backend/ctrl/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use patch_db::{HasModel, ModelExt, PatchDb};
use rpc_toolkit::yajrc::RpcError;
use rpc_toolkit::{from_fn_async, CallRemote, Empty, HandlerArgs, HandlerExt, ParentHandler};
use serde::{Deserialize, Serialize};
use startos::auth::{AuthKeys, LoginContext, Session};
use startos::auth::{AuthKeys, LoginAttempt, LoginContext, Session};
use startos::middleware::auth::local::LocalAuthContext;
use startos::middleware::auth::signature::{
check_enrolled, url_host_str, HasUnenrolledKeys, LoginMetadata, NonceCache,
Expand Down Expand Up @@ -239,17 +239,47 @@ impl LoginContext for ServerContext {
db.as_session_pubkeys_mut()
}
fn check_password(_: &Model<Self::Database>, password: &str) -> Result<(), startos::Error> {
check_password_sync(Path::new(SHADOW_PATH), password).map_err(|e| {
crate::activity::log("auth", "login", false, "Login failed", Some(&e.to_string()));
e.into()
})
Ok(check_password_sync(Path::new(SHADOW_PATH), password)?)
}
fn sign_in_expired(session: &Session, now: DateTime<Utc>) -> bool {
sign_in_ended(session.logged_in, now, true)
}
async fn post_login_hook(&self, _: &str) -> Result<(), startos::Error> {
crate::activity::log("auth", "login", true, "Logged in", None);
Ok(())
async fn login_attempt_hook(
&self,
attempt: &LoginAttempt,
result: Result<(), &startos::Error>,
) {
let mut source = String::new();
if let Some(peer) = attempt.peer {
source = format!(" from {}", crate::peer::describe(peer).await);
}
let client = describe_client(attempt.user_agent.as_deref());
match result {
Ok(()) => crate::activity::log(
"auth",
"login",
true,
&format!("Logged in{source} {client}"),
None,
),
Err(e) => crate::activity::log(
"auth",
"login",
false,
&format!("Login failed{source} {client}"),
Some(&e.to_string()),
),
}
}
}

/// Every mainstream browser's user agent starts `Mozilla/`; other clients are
/// named by their first product token.
fn describe_client(user_agent: Option<&str>) -> String {
match user_agent.and_then(|ua| ua.split_whitespace().next()) {
Some(product) if product.starts_with("Mozilla/") => "using a browser".into(),
Some(product) => format!("using {}", product.chars().take(64).collect::<String>()),
None => "with no user agent".into(),
}
}

Expand Down Expand Up @@ -701,6 +731,22 @@ mod tests {
const LAST_ACTIVE_RESOLUTION: TimeDelta =
<ServerContext as SignatureAuthContext>::LAST_ACTIVE_RESOLUTION;

#[test]
fn login_clients_are_named_by_user_agent() {
for (user_agent, expected) in [
(
Some("Mozilla/5.0 (X11; Linux x86_64; rv:144.0) Gecko/20100101 Firefox/144.0"),
"using a browser",
),
(Some("curl/8.5.0"), "using curl/8.5.0"),
(Some("node"), "using node"),
(Some(" "), "with no user agent"),
(None, "with no user agent"),
] {
assert_eq!(describe_client(user_agent), expected, "{user_agent:?}");
}
}

#[test]
fn validate_password_length_rejects_short() {
assert!(validate_password_length("shortpass12").is_err());
Expand Down
2 changes: 2 additions & 0 deletions projects/start-wrt/backend/ctrl/src/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,14 @@ async fn restore(
None,
);

crate::boot::record_reboot(crate::boot::RunState::Restoring);
tokio::spawn(async {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
if let Err(e) = Command::new("reboot")
.invoke(ErrorKind::Filesystem.into())
.await
{
crate::boot::record_running();
tracing::error!("failed to reboot after restore: {e}");
}
});
Expand Down
8 changes: 8 additions & 0 deletions projects/start-wrt/backend/ctrl/src/bins/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ async fn inner_main() -> Result<(), Error> {
} else {
// Normal mode: restore WiFi + manage captive portal state

crate::boot::record_start(crate::update::update_pending());
// Record the outcome of a pending OTA update (if any) in the Activity
// log now that the new firmware is up.
crate::update::check_pending_update_marker();
Expand Down Expand Up @@ -387,6 +388,7 @@ async fn inner_main() -> Result<(), Error> {

if !setup_mode {
tokio::spawn(crate::auth::reap_idle_keys(ctx.clone()));
tokio::spawn(crate::ssh_sessions::run());
}
let handler = Server::new(move || ready(Ok(ctx.clone())), main_api())
.middleware(Auth::new().with_local_auth().with_signature_auth());
Expand Down Expand Up @@ -508,10 +510,16 @@ async fn inner_main() -> Result<(), Error> {
tokio::signal::unix::signal(SignalKind::terminate()).with_kind(ErrorKind::Filesystem)?;
let mut sigint =
tokio::signal::unix::signal(SignalKind::interrupt()).with_kind(ErrorKind::Filesystem)?;
if !setup_mode {
crate::boot::record_running();
}
tokio::select! {
_ = sigterm.recv() => tracing::info!("received SIGTERM, shutting down"),
_ = sigint.recv() => tracing::info!("received SIGINT, shutting down"),
}
if !setup_mode {
crate::boot::record_stop();
}
server.shutdown().await;

Ok(())
Expand Down
Loading
Loading