Skip to main content

virtual_file_system/
home.rs

1use file_system::{Kind, PathOwned, Permissions};
2use task::TaskIdentifier;
3use users::UserIdentifier;
4
5use crate::{IoErrorKind, Result, ResultExt, VirtualFileSystem};
6
7fn is_valid_application_identifier(application_id: &str) -> bool {
8    !application_id.is_empty()
9        && !matches!(application_id, "." | "..")
10        && !application_id.contains(['/', '\0'])
11}
12
13impl VirtualFileSystem {
14    /// The home recorded in the user's account.
15    ///
16    /// Accounts record their home when they are created (`/users/<user name>`),
17    /// and accounts saved before homes were recorded keep `/users/<identifier>`.
18    /// The home is read from the account, never derived from the current user
19    /// name, so renaming a user does not move or lose their home. Recorded
20    /// homes are always a single entry directly below `/users`.
21    pub async fn get_user_home(&self, user: UserIdentifier) -> Result<PathOwned> {
22        let home = self
23            .users_manager()
24            .get_user_home(user)
25            .await
26            .map_io_error()?;
27        if !users::is_valid_user_home(&home) {
28            return Err(IoErrorKind::InvalidPath);
29        }
30        PathOwned::new(home).ok_or(IoErrorKind::InvalidPath)
31    }
32
33    /// The private directory of an application inside the user's recorded home.
34    pub async fn get_user_application_directory(
35        &self,
36        user: UserIdentifier,
37        application_id: &str,
38    ) -> Result<PathOwned> {
39        if !is_valid_application_identifier(application_id) {
40            return Err(IoErrorKind::InvalidPath);
41        }
42        self.get_user_home(user)
43            .await?
44            .append("applications")
45            .and_then(|path| path.append(application_id))
46            .ok_or(IoErrorKind::InvalidPath)
47    }
48
49    /// Lazily create the user's private application configuration, data and cache directories.
50    pub async fn ensure_user_application_directories(
51        &self,
52        task: TaskIdentifier,
53        user: UserIdentifier,
54        application_id: &str,
55    ) -> Result<PathOwned> {
56        if !is_valid_application_identifier(application_id) {
57            return Err(IoErrorKind::InvalidPath);
58        }
59        let current_user = self.task_manager().get_user(task).await.map_io_error()?;
60        if current_user != user && current_user != UserIdentifier::ROOT {
61            return Err(IoErrorKind::PermissionDenied);
62        }
63        let home = self.ensure_user_home(task, user).await?;
64        let group = self
65            .users_manager()
66            .get_user_primary_group(user)
67            .await
68            .map_io_error()?;
69        let application = self
70            .get_user_application_directory(user, application_id)
71            .await?;
72        let paths = [
73            home.append("applications")
74                .ok_or(IoErrorKind::InvalidPath)?,
75            application.clone(),
76            application
77                .clone()
78                .append("configuration")
79                .ok_or(IoErrorKind::InvalidPath)?,
80            application
81                .clone()
82                .append("data")
83                .ok_or(IoErrorKind::InvalidPath)?,
84            application
85                .clone()
86                .append("cache")
87                .ok_or(IoErrorKind::InvalidPath)?,
88        ];
89
90        for path in paths {
91            match self.get_statistics(&path).await {
92                Ok(statistics) => {
93                    if statistics.kind != Kind::Directory || statistics.user != user {
94                        return Err(IoErrorKind::PermissionDenied);
95                    }
96                }
97                Err(IoErrorKind::NotFound) => {
98                    self.create_directory(task, &path).await?;
99                    self.set_permissions(task, &path, Permissions::USER_FULL)
100                        .await?;
101                    if current_user == UserIdentifier::ROOT && user != UserIdentifier::ROOT {
102                        self.set_ownership(task, &path, Some(user), Some(group))
103                            .await?;
104                    }
105                }
106                Err(error) => return Err(error),
107            }
108        }
109
110        Ok(application)
111    }
112
113    /// Provision a private home before dropping login's privileges. Existing
114    /// homes must belong to the expected user; their contents are preserved.
115    pub async fn ensure_user_home(
116        &self,
117        task: TaskIdentifier,
118        user: UserIdentifier,
119    ) -> Result<PathOwned> {
120        let path = self.get_user_home(user).await?;
121        match self.get_statistics(&path).await {
122            Ok(statistics) => {
123                if statistics.kind != Kind::Directory {
124                    return Err(IoErrorKind::NotADirectory);
125                }
126                if statistics.user != user {
127                    return Err(IoErrorKind::PermissionDenied);
128                }
129                self.set_permissions(task, &path, Permissions::USER_FULL)
130                    .await?;
131                return Ok(path);
132            }
133            Err(IoErrorKind::NotFound) => {}
134            Err(error) => return Err(error),
135        }
136        let group = self
137            .users_manager()
138            .get_user_primary_group(user)
139            .await
140            .map_io_error()?;
141        self.create_directory(task, &path).await?;
142        self.set_permissions(task, &path, Permissions::USER_FULL)
143            .await?;
144        self.set_ownership(task, &path, Some(user), Some(group))
145            .await?;
146        Ok(path)
147    }
148}