1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use crate::{common::*, dbus_helpers::*, Client, DBusEntry};
use dbus::arg::RefArg;
use std::{
    borrow::Cow,
    fs::{metadata, File, OpenOptions},
    io::{self, Seek, SeekFrom},
    iter::FromIterator,
    path::{Path, PathBuf},
    time::{Duration, SystemTime},
};
use url::Url;

/// Describes the type of keyring to use with a remote.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum KeyringKind {
    Unknown,
    None,
    GPG,
    PKCS7,
    JCAT,
}

impl From<u8> for KeyringKind {
    fn from(value: u8) -> KeyringKind {
        use self::KeyringKind::*;
        match value {
            0 => Unknown,
            1 => None,
            2 => GPG,
            3 => PKCS7,
            4 => JCAT,
            _ => Unknown,
        }
    }
}

impl Default for KeyringKind {
    fn default() -> Self { KeyringKind::None }
}

/// Describes the kind of remote.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RemoteKind {
    Unknown,
    Download,
    Local,
    Directory,
}

impl From<u8> for RemoteKind {
    fn from(value: u8) -> RemoteKind {
        use self::RemoteKind::*;
        match value {
            1 => Download,
            2 => Local,
            3 => Directory,
            _ => Unknown,
        }
    }
}

impl Default for RemoteKind {
    fn default() -> Self { RemoteKind::Unknown }
}

/// An error that may occur when updating the metadata for a remote.
#[derive(Debug, Error)]
pub enum UpdateError {
    #[error("fwupd client errored when updating metadata for remote")]
    Client(#[source] crate::Error),
    #[error("failed to write firmware metadata to disk")]
    Copy(#[source] io::Error),
    #[error("failed to create parent directories for the remote's metadata cache")]
    CreateParent(#[source] io::Error),
    #[error("remote returned error when fetching firmware metadata")]
    Get(#[source] ureq::Error),
    #[error("attempted to update a remote without a URI")]
    NoUri,
    #[error("unable to open cached firmware metadata ({:?}) for remote", _1)]
    Open(#[source] io::Error, PathBuf),
    #[error("failed to read the cached firmware metadata ({:?}) for remote", _1)]
    Read(#[source] io::Error, PathBuf),
    #[error("failed to seek to beginning of firmware file")]
    Seek(#[source] io::Error),
    #[error("failed to truncate firmware metadata file")]
    Truncate(#[source] io::Error),
    #[error("failed to get fwupd user agent")]
    UserAgent(#[source] crate::Error),
}

/// The remote ID of a remote.
#[derive(Clone, Debug, Default, Eq, PartialEq, Shrinkwrap)]
pub struct RemoteId(pub(crate) Box<str>);

/// Information about an available fwupd remote.
#[derive(Clone, Debug, Default)]
pub struct Remote {
    pub agreement:         Option<Box<str>>,
    pub approval_required: bool,
    pub checksum:          Option<Box<str>>,
    pub enabled:           bool,
    pub filename_cache:    Box<str>,
    pub filename_source:   Box<str>,
    pub firmware_base_uri: Option<Box<str>>,
    pub keyring:           KeyringKind,
    pub kind:              RemoteKind,
    pub modification_time: u64,
    pub password:          Option<Box<str>>,
    pub priority:          i16,
    pub remote_id:         RemoteId,
    pub report_uri:        Option<Box<str>>,
    pub title:             Box<str>,
    pub uri:               Option<Box<str>>,
    pub username:          Option<Box<str>>,
}

impl Remote {
    /// Updates the metadata for this remote.
    pub fn update_metadata(&self, client: &Client) -> Result<(), UpdateError> {
        if !self.enabled {
            return Ok(());
        }

        if let Some(ref uri) = self.uri {
            if let Some(file) = self.update_file(&client.http, uri)? {
                let sig = self.update_signature(&client.http, uri)?;
                client.update_metadata(&self, file, sig).map_err(UpdateError::Client)?;
            }
        }

        Ok(())
    }

    pub(crate) fn firmware_uri(&self, url: &str) -> Url {
        let uri = if let Some(ref firmware_base_uri) = self.firmware_base_uri {
            let mut firmware_base_uri: &str = firmware_base_uri;
            if firmware_base_uri.ends_with('/') {
                firmware_base_uri = &firmware_base_uri[..firmware_base_uri.len() - 1];
            }

            let basename = Path::new(url)
                .file_name()
                .expect("release URI without basename")
                .to_str()
                .expect("basename of release URI is not UTF-8");

            Cow::Owned([firmware_base_uri, "/", basename].concat())
        // Use the base URI of the metadata to build the full path.
        } else if !url.contains('/') {
            let remote_uri: &str = self.uri.as_ref().expect("remote URI without URI");
            let mut dirname = Path::new(remote_uri)
                .parent()
                .expect("metadata URI without parent")
                .as_os_str()
                .to_str()
                .expect("metadata URI is not UTF-8");

            if dirname.ends_with('/') {
                dirname = &dirname[..dirname.len() - 1];
            }

            Cow::Owned([dirname, "/", url].concat())
        // A normal URI
        } else {
            Cow::Borrowed(url)
        };

        uri.parse::<Url>().expect("firmware uri is not a valid uri")
    }

    /// Fetch the time since the last update, if such a time can be fetched.
    pub fn time_since_last_update(&self) -> Option<Duration> {
        metadata(&self.local_cache(self.filename_cache.as_ref()))
            .and_then(|md| md.modified())
            .ok()
            .and_then(|modified| SystemTime::now().duration_since(modified).ok())
    }

    fn local_cache(&self, file: &str) -> PathBuf {
        let file_name =
            Path::new(file).file_name().expect("remote filename cache does not have a file name");

        let id: &str = &*self.remote_id;
        cache_path(&Path::new(id).join(file_name))
    }

    /// Fetch the latest firmware from the remote
    fn update_file(&self, http: &ureq::Agent, uri: &str) -> Result<Option<File>, UpdateError> {
        let local_cache = &self.local_cache(self.filename_cache.as_ref());
        let checksum = self.checksum.as_ref().unwrap();

        if local_cache.exists() && self.checksum.is_some() {
            let checksum_matched = (|| {
                let mut file = OpenOptions::new().read(true).open(local_cache)?;

                validate_checksum(&mut file, checksum, checksum_guess_kind(checksum))
            })();

            if checksum_matched.is_ok() {
                return Ok(None);
            }
        };

        let file = Remote::fetch(http, uri, local_cache)?;

        Ok(Some(file))
    }

    /// Fetch the latest signature for the remote
    fn update_signature(&self, http: &ureq::Agent, uri: &str) -> Result<File, UpdateError> {
        let extension = match self.keyring {
            KeyringKind::JCAT => ".jcat",
            KeyringKind::PKCS7 => ".p7b",
            _ => ".asc",
        };

        let cache = &self.local_cache(&[self.filename_cache.as_ref(), extension].concat());
        let uri = [uri, extension].concat();

        Remote::fetch(http, &uri, cache)
    }

    /// Fetch a file from a remote URI to disk
    fn fetch(http: &ureq::Agent, uri: &str, file: &Path) -> Result<File, UpdateError> {
        info!("fetching {} to {:?}", uri, file);

        if file.exists() {
            let _ = std::fs::remove_file(file);
        }

        // Open the file that we're going to write to
        let mut file = OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .open(file)
            .map_err(|why| UpdateError::Open(why, file.to_path_buf()))?;

        // Initiate connection to fetch firmware from remote
        let mut resp = http.get(uri).call().map_err(UpdateError::Get)?.into_reader();

        std::io::copy(&mut resp, &mut file).map_err(UpdateError::Copy)?;

        file.seek(SeekFrom::Start(0)).map_err(UpdateError::Seek)?;

        Ok(file)
    }
}

impl AsRef<RemoteId> for Remote {
    fn as_ref(&self) -> &RemoteId { &self.remote_id }
}

impl FromIterator<DBusEntry> for Remote {
    fn from_iter<T>(iter: T) -> Self
    where
        T: IntoIterator<Item = DBusEntry>,
    {
        let mut remote = Remote::default();

        for (key, value) in iter {
            let key = key.as_str();
            match key {
                "Agreement" => remote.agreement = Some(dbus_str(&value, key).into()),
                "ApprovalRequired" => remote.approval_required = dbus_u64(&value, key) != 0,
                KEY_CHECKSUM => remote.checksum = Some(dbus_str(&value, key).into()),
                "Enabled" => remote.enabled = dbus_u64(&value, key) != 0,
                "FilenameCache" => remote.filename_cache = dbus_str(&value, key).into(),
                "FilenameSource" => remote.filename_source = dbus_str(&value, key).into(),
                "FirmwareBaseUri" => remote.firmware_base_uri = Some(dbus_str(&value, key).into()),
                "Keyring" => remote.keyring = KeyringKind::from(dbus_u64(&value, key) as u8),
                "ModificationTime" => remote.modification_time = dbus_u64(&value, key),
                "Password" => remote.password = Some(dbus_str(&value, key).into()),
                "Priority" => {
                    let value = value
                        .as_iter()
                        .expect("Priority is not a variant")
                        .next()
                        .expect("Priority does not contain a value");

                    remote.priority = dbus_i64(&value, key) as i16;
                }
                KEY_REMOTE_ID => remote.remote_id = RemoteId(dbus_str(&value, key).into()),
                "ReportUri" => remote.report_uri = Some(dbus_str(&value, key).into()),
                "Title" => remote.title = dbus_str(&value, key).into(),
                "Type" => remote.kind = RemoteKind::from(dbus_u64(&value, key) as u8),
                "Username" => remote.username = Some(dbus_str(&value, key).into()),
                KEY_URI => remote.uri = Some(dbus_str(&value, key).into()),
                other => {
                    eprintln!("unknown remote key: {} ({}): {:?}", other, value.signature(), value);
                }
            }
        }

        remote
    }
}