1
use russh::ChannelId;
2
use russh::server::Session;
3
use server::command::user::Refusal;
4
use sqlx::types::Uuid;
5

            
6
use super::Session_;
7

            
8
impl Session_ {
9
    /// Refuses a channel whose tenant database is withheld by the fleet
10
    /// migrator, telling the user why rather than failing opaquely inside the
11
    /// session.
12
    ///
13
    /// Fails **closed**: a directory lookup that errors refuses too. Treating an
14
    /// unreadable directory as "not quarantined" would hand a session to
15
    /// exactly the tenant whose state could not be confirmed.
16
    ///
17
    /// Every channel that can reach the tenant's database goes through here.
18
    /// Guarding only the shell left `nomisync-eval` open, and that subsystem
19
    /// evaluates scripts against the same database.
20
37
    pub(super) async fn refuse_if_withheld(
21
37
        &self,
22
37
        user_id: Uuid,
23
37
        channel: ChannelId,
24
37
        session: &mut Session,
25
37
        kind: &str,
26
37
    ) -> Result<bool, russh::Error> {
27
37
        let refusal = match server::command::user::account_refusal(user_id).await {
28
37
            Ok(refusal) => refusal,
29
            Err(e) => {
30
                if !e.is_unavailable() {
31
                    tracing::error!("{kind}_request refusing user_id={user_id}: {e}");
32
                }
33
                Some(Refusal::Quarantined)
34
            }
35
        };
36

            
37
37
        let Some(refusal) = refusal else {
38
37
            return Ok(false);
39
        };
40

            
41
        let notice = match refusal {
42
            Refusal::Quarantined => {
43
                tracing::warn!("{kind}_request refused user_id={user_id} reason=quarantined");
44
                "\r\nThis account is undergoing maintenance and is temporarily \
45
                 unavailable. Please try again shortly.\r\n"
46
            }
47
            Refusal::Disabled => {
48
                tracing::warn!("{kind}_request refused user_id={user_id} reason=disabled");
49
                "\r\nThis account is disabled.\r\n"
50
            }
51
        }
52
        .to_owned();
53
        session.data(channel, notice.into_bytes())?;
54
        session.close(channel)?;
55
        Ok(true)
56
37
    }
57

            
58
    pub(super) fn refuse_request(&self, kind: &str) {
59
        tracing::warn!(
60
            "refused request kind={kind} peer={:?} user_id={:?}",
61
            self.peer,
62
            self.user_id
63
        );
64
    }
65
}