libapt 1.3.0

Rust library for interfacing with Debian apt repositories.
Documentation
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//! Implementation of the InRelease file parsing.

#[cfg(not(test))]
use log::{info, warn};

#[cfg(test)]
use std::{println as warn, println as info};

use chrono::DateTime;
use chrono::FixedOffset;
use std::collections::HashMap;

use serde::{Deserialize, Serialize};

use crate::signature::verify_in_release;
use crate::util::{download, get_etag};
use crate::Architecture;
use crate::Distro;
use crate::Link;
use crate::LinkHash;
use crate::{Error, ErrorType, Result};

/// The Release struct groups all data from the InRelease file.
///
/// When the InRelease file is parsed, all specified values from
/// [Debian Wiki InRelease specification](https://wiki.debian.org/DebianRepository/Format#A.22Release.22_files)
/// are considered.
#[derive(Debug, Deserialize, Serialize)]
pub struct Release {
    // fields from apt release file
    hash: Option<String>,
    pub origin: Option<String>,
    pub label: Option<String>,
    pub suite: Option<String>,
    pub version: Option<String>,
    pub codename: Option<String>,
    pub date: Option<DateTime<FixedOffset>>,
    pub valid_until: Option<DateTime<FixedOffset>>,
    pub architectures: Vec<Architecture>,
    pub components: Vec<String>,
    pub description: Option<String>,
    pub links: HashMap<String, Link>,
    pub acquire_by_hash: bool,
    pub signed_by: Vec<String>,
    pub changelogs: Option<String>,
    pub snapshots: Option<String>,
    // internal data
    pub distro: Distro,
    pub issues: Vec<Error>,
}

impl Release {
    /// Create a new Release struct with default values.
    fn new(distro: &Distro) -> Release {
        Release {
            hash: None,
            origin: None,
            label: None,
            suite: None,
            version: None,
            codename: None,
            date: None,
            valid_until: None,
            architectures: Vec::new(),
            components: Vec::new(),
            description: None,
            links: HashMap::new(),
            acquire_by_hash: false, // default is false
            signed_by: Vec::new(),
            changelogs: None,
            snapshots: None,
            distro: distro.clone(),
            issues: Vec::new(),
        }
    }

    /// Download and parse the InRelease file of the given Distro.
    pub async fn from_distro(distro: &Distro) -> Result<Release> {
        // Get URL content.
        let url = distro.in_release_url()?;
        let content = download(&url).await?;

        // Verify signature.
        let content = verify_in_release(content, distro).await?;

        // Parse content.
        let mut section = ReleaseSection::Keywords;
        let mut release = Release::new(distro);

        for line in content.lines() {
            if line.trim().is_empty() {
                continue;
            }

            if line.starts_with("---") {
                if line.starts_with("-----BEGIN PGP SIGNATURE") {
                    // Signature check not implemented. Stop parsing.
                    break;
                } else {
                    // Skip PGP headers.
                    continue;
                }
            } else if !line.starts_with(" ") {
                section = ReleaseSection::Keywords;
            }

            match &section {
                ReleaseSection::Keywords => {
                    if !line.contains(":") {
                        return Err(Error::new(
                            &format!("Invalid line! {line}"),
                            ErrorType::InReleaseFormat,
                        ));
                    }

                    let mut parts = line.splitn(2, ":");
                    let keyword = parts.next().unwrap();
                    let value = parts.next().unwrap();

                    let keyword = keyword.to_lowercase();
                    let plain_value = value.trim();
                    let value = Some(plain_value.to_string());

                    if keyword == "hash" {
                        release.hash = value;
                    } else if keyword == "origin" {
                        release.origin = value;
                    } else if keyword == "label" {
                        release.label = value;
                    } else if keyword == "suite" {
                        release.suite = value;
                    } else if keyword == "version" {
                        release.version = value;
                    } else if keyword == "codename" {
                        release.codename = value;
                    } else if keyword == "description" {
                        release.description = value;
                    } else if keyword == "changelogs" {
                        release.changelogs = value;
                    } else if keyword == "snapshots" {
                        release.snapshots = value;
                    } else if keyword == "date" {
                        let plain_value = plain_value.replace("UTC", "+0000");
                        release.date = match DateTime::parse_from_rfc2822(&plain_value) {
                            Ok(date) => Some(date),
                            Err(e) => {
                                warn!("Parsing Release date \"{plain_value}\" failed! {e}");
                                None
                            }
                        }
                    } else if keyword == "valid-until" {
                        let plain_value = plain_value.replace("UTC", "+0000");
                        release.valid_until = match DateTime::parse_from_rfc2822(&plain_value) {
                            Ok(date) => Some(date),
                            Err(e) => {
                                warn!("Parsing Release valid until failed! {e}");
                                None
                            }
                        }
                    } else if keyword == "architectures" {
                        release.architectures = plain_value
                            .split(" ")
                            .filter_map(|e| match Architecture::from_str(e) {
                                Ok(arch) => Some(arch),
                                Err(e) => {
                                    warn!("Parsing architecture {e} failed!");
                                    None
                                }
                            })
                            .collect();
                    } else if keyword == "components" {
                        release.components = plain_value
                            .split(" ")
                            .filter(|e| !e.trim().is_empty())
                            .map(|e| e.to_string())
                            .collect();
                    } else if keyword == "acquire-by-hash" {
                        release.acquire_by_hash = match plain_value.to_lowercase().as_str() {
                            "yes" => true,
                            _ => false,
                        }
                    } else if keyword == "signed-by" {
                        release.signed_by = plain_value
                            .split(",")
                            .map(|e| e.trim().to_string())
                            .collect();
                    } else if keyword == "md5sum" {
                        section = ReleaseSection::HashMD5;
                    } else if keyword == "sha1" {
                        section = ReleaseSection::HashSHA1;
                    } else if keyword == "sha256" {
                        section = ReleaseSection::HashSHA256;
                    } else if keyword == "sha512" {
                        section = ReleaseSection::HashSHA512;
                    } else {
                        warn!("Unknown keyword: {keyword} of line {line}!");
                    }
                }
                section => {
                    let link = match Link::form_release(line, distro) {
                        Ok(link) => link,
                        Err(e) => {
                            release.issues.push(e);
                            continue;
                        }
                    };

                    let url = link.url.clone();

                    if !release.links.contains_key(&url) {
                        release.links.insert(url.clone(), link);
                    }

                    let link = release.links.get_mut(&url).unwrap();

                    match section {
                        ReleaseSection::HashMD5 => match link.add_hash(line, LinkHash::Md5) {
                            Ok(_) => {}
                            Err(e) => {
                                release.issues.push(e);
                            }
                        },
                        ReleaseSection::HashSHA1 => match link.add_hash(line, LinkHash::Sha1) {
                            Ok(_) => {}
                            Err(e) => {
                                release.issues.push(e);
                            }
                        },
                        ReleaseSection::HashSHA256 => match link.add_hash(line, LinkHash::Sha256) {
                            Ok(_) => {}
                            Err(e) => {
                                release.issues.push(e);
                            }
                        },
                        ReleaseSection::HashSHA512 => match link.add_hash(line, LinkHash::Sha512) {
                            Ok(_) => {}
                            Err(e) => {
                                release.issues.push(e);
                            }
                        },
                        _ => {}
                    };
                }
            }
        }

        Ok(release)
    }

    pub fn check_compliance(&self) -> Result<()> {
        if self.components.is_empty() {
            return Err(Error::new(
                "No components provided.",
                ErrorType::InReleaseStandard,
            ));
        }

        if self.architectures.is_empty() {
            return Err(Error::new(
                "No architectures provided.",
                ErrorType::InReleaseStandard,
            ));
        }

        if self.suite == None && self.codename == None {
            return Err(Error::new(
                "Neither suite nor codename provided.",
                ErrorType::InReleaseStandard,
            ));
        }

        if self.date == None {
            return Err(Error::new(
                "No date provided.",
                ErrorType::InReleaseStandard,
            ));
        }

        for key in self.links.keys() {
            let link = self.links.get(key).unwrap();
            if !link.hashes.contains_key(&LinkHash::Sha256) {
                return Err(Error::new(
                    &format!("No SHA256 hash provided for URL {key}."),
                    ErrorType::InReleaseStandard,
                ));
            }
        }

        Ok(())
    }

    pub async fn get_package_links(&self) -> Vec<(String, Architecture, Link)> {
        let mut components = Vec::new();

        for architecture in &self.architectures {
            for component in &self.components {
                let link = match self.get_package_index_link(component, architecture).await {
                    Ok(link) => link,
                    Err(_) => {
                        info!("No link for component {component} and architecture {architecture}. Skipping.");
                        continue;
                    }
                };
                components.push((component.to_string(), architecture.clone(), link));
            }
        }

        components
    }

    pub async fn get_package_index_link(
        &self,
        component: &str,
        architecture: &Architecture,
    ) -> Result<Link> {
        let index_url = if architecture == &Architecture::Source {
            format!("{component}/source/Sources")
        } else {
            let arch_str = architecture.to_string();
            format!("{component}/binary-{arch_str}/Packages")
        };

        let index_url = self.distro.url(&index_url, false);

        // Supported compression extensions, try form best to no compression
        let extensions = vec![".xz", ".gz", ""];

        for ext in extensions {
            // Build URL for compressed index.
            let package_index = index_url.clone() + ext;

            // Find link in release.
            // The link is mandatory to get the hash sums for verification.
            match self.links.get(&package_index) {
                Some(link) => {
                    match get_etag(&link.url).await {
                        Ok(_) => return Ok(link.clone()), // Index file exists.
                        Err(_) => {
                            info!("No etag for {package_index}, trying next link.");
                            continue;
                        }
                    }
                }
                None => {
                    info!("Index {package_index} not found.");
                }
            }
        }

        // No link found.
        Err(Error::new(
            &format!("No matching package index found for component {component} and architecture {architecture}!"),
            ErrorType::ApiUsage,
        ))
    }
}

/// Internal helper as marker for the sections of the InRelease file.
enum ReleaseSection {
    Keywords,
    HashMD5,
    HashSHA1,
    HashSHA256,
    HashSHA512,
}

#[cfg(test)]
mod tests {
    use crate::{Distro, Key, Release};

    #[tokio::test]
    async fn parse_ubuntu_jammy_release_file() {
        // Ubuntu Jammy signing key.
        let key = Key::key("/etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg");

        // Ubuntu Jammy distribution.
        let distro = Distro::repo("http://archive.ubuntu.com/ubuntu", "jammy", key);

        let release = Release::from_distro(&distro).await.unwrap();

        assert_eq!(release.origin, Some("Ubuntu".to_string()), "Origin");
        assert_eq!(release.label, Some("Ubuntu".to_string()), "Label");
        assert_eq!(release.suite, Some("jammy".to_string()), "Suite");
        assert_eq!(release.codename, Some("jammy".to_string()), "Codename");
        assert_eq!(release.version, Some("22.04".to_string()), "Version");
        assert_eq!(release.acquire_by_hash, true, "Acquire-By-Hash");

        // Parse the InRelease file.
        let release = Release::from_distro(&distro).await.unwrap();

        // Check for compliance with https://wiki.debian.org/DebianRepository/Format#A.22Release.22_files.
        release.check_compliance().unwrap();
    }

    #[tokio::test]
    async fn parse_ebcl_release_file() {
        // EBcL signing key.
        let key = Key::armored_key("https://linux.elektrobit.com/eb-corbos-linux/ebcl_1.0_key.pub");

        // EBcL 1.3 distribution.
        let distro = Distro::repo(
            "http://linux.elektrobit.com/eb-corbos-linux/1.3",
            "ebcl",
            key,
        );

        let release = Release::from_distro(&distro).await.unwrap();

        assert_eq!(release.origin, Some("Elektrobit".to_string()), "Origin");
        assert_eq!(release.suite, Some("ebcl".to_string()), "Suite");
        assert_eq!(release.codename, Some("ebcl".to_string()), "Codename");

        // Parse the InRelease file.
        let release = Release::from_distro(&distro).await.unwrap();

        // Check for compliance with https://wiki.debian.org/DebianRepository/Format#A.22Release.22_files.
        release.check_compliance().unwrap();
    }

    #[tokio::test]
    async fn test_wrong_key() {
        // Ubuntu Jammy signing key.
        let key = Key::key("/etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg");

        // Ubuntu Jammy distribution.
        let distro = Distro::repo(
            "http://linux.elektrobit.com/eb-corbos-linux/1.3",
            "ebcl",
            key,
        );

        match Release::from_distro(&distro).await {
            Ok(_) => assert!(false), // Key verification shall fail!
            Err(_) => {}
        };
    }

    #[tokio::test]
    async fn test_package_index_link() {
        // Ubuntu Jammy signing key.
        let key = Key::key("/etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg");

        // Ubuntu Jammy distribution.
        let distro = Distro::repo("http://archive.ubuntu.com/ubuntu", "jammy", key);

        let release = Release::from_distro(&distro).await.unwrap();

        let link = release
            .get_package_index_link("main", &crate::Architecture::Amd64)
            .await
            .unwrap();
        assert_eq!(
            link.url,
            "http://archive.ubuntu.com/ubuntu/dists/jammy/main/binary-amd64/Packages.xz"
                .to_string()
        );

        match release
            .get_package_index_link("main", &crate::Architecture::Arm64)
            .await
        {
            Ok(_) => assert!(false), // Should not exist!
            Err(_) => {}             // Ok, expected.
        };
    }

    #[tokio::test]
    async fn test_get_package_links() {
        // Ubuntu Jammy signing key.
        let key = Key::key("/etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg");

        // Ubuntu Jammy distribution.
        let distro = Distro::repo("http://archive.ubuntu.com/ubuntu", "jammy", key);

        let release = Release::from_distro(&distro).await.unwrap();

        let components = release.get_package_links().await;
        println!("Components: {:?}", components);
        println!("Found {} package indices.", components.len());
        assert_eq!(components.len(), 8);
    }
}