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
use chrono::{DateTime, FixedOffset, ParseResult, Utc};
use small_map::SmallMap;
use smallvec::{smallvec, SmallVec};
use std::{borrow::Cow, num::ParseIntError, path::Path};
use tracing::debug;

use crate::verify;

pub struct InReleaseParser {
    _source: Vec<SmallMap<16, String, String>>,
    pub checksums: SmallVec<[ChecksumItem; 32]>,
    pub acquire_by_hash: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ChecksumItem {
    pub name: String,
    pub size: u64,
    pub checksum: String,
    pub file_type: DistFileType,
}

#[derive(Debug, PartialEq, Clone, Eq)]
pub enum DistFileType {
    BinaryContents,
    Contents,
    CompressContents(String, String),
    PackageList,
    CompressPackageList(String, String),
    Release,
}

#[derive(Debug, thiserror::Error)]
pub enum InReleaseParserError {
    #[error(transparent)]
    VerifyError(#[from] crate::verify::VerifyError),
    #[error("Bad InRelease Data")]
    BadInReleaseData,
    #[error("Bad vaild until")]
    BadInReleaseVaildUntil,
    #[error("Earlier signature: {0}")]
    EarlierSignature(String),
    #[error("Expired signature: {0}")]
    ExpiredSignature(String),
    #[error("Bad SHA256 value: {0}")]
    BadSha256Value(String),
    #[error("Bad checksum entry: {0}")]
    BadChecksumEntry(String),
    #[error("Bad InRelease")]
    InReleaseSyntaxError,
    #[error("Unsupport file type in path")]
    UnsupportFileType,
    #[error(transparent)]
    ParseIntError(ParseIntError),
}

pub type InReleaseParserResult<T> = Result<T, InReleaseParserError>;

pub struct InRelease<'a> {
    pub inrelease: &'a str,
    pub signed_by: Option<&'a str>,
    pub mirror: &'a str,
    pub arch: &'a str,
    pub is_flat: bool,
    pub p: &'a Path,
    pub rootfs: &'a Path,
    pub components: &'a [String],
}

impl InReleaseParser {
    pub fn new(in_release: InRelease<'_>) -> InReleaseParserResult<Self> {
        let InRelease {
            inrelease: s,
            signed_by,
            mirror: _,
            arch,
            is_flat,
            p,
            rootfs,
            components,
        } = in_release;

        let s = if s.starts_with("-----BEGIN PGP SIGNED MESSAGE-----") {
            Cow::Owned(verify::verify(s, signed_by, rootfs)?)
        } else {
            Cow::Borrowed(s)
        };

        let source = debcontrol_from_str(&s)?;

        let source_first = source.first();

        debug!("InRelease is: {source:#?}");

        if !is_flat {
            let date = source_first
                .and_then(|x| x.get("Date"))
                .take()
                .ok_or_else(|| InReleaseParserError::BadInReleaseData)?;

            let date = parse_date(date).map_err(|e| {
                debug!("Parse data failed: {}", e);
                InReleaseParserError::BadInReleaseData
            })?;

            let now = Utc::now();

            // Make `Valid-Until` field optional.
            // Some third-party repos do not have such field in their InRelease files.
            let valid_until = source_first.and_then(|x| x.get("Valid-Until")).take();
            if now < date {
                return Err(InReleaseParserError::EarlierSignature(
                    p.display().to_string(),
                ));
            }

            // Check if the `Valid-Until` field is valid only when it is defined.
            if let Some(valid_until_date) = valid_until {
                let valid_until = parse_date(valid_until_date).map_err(|e| {
                    debug!("Parse valid_until failed: {}", e);
                    InReleaseParserError::BadInReleaseVaildUntil
                })?;

                if now > valid_until {
                    return Err(InReleaseParserError::ExpiredSignature(
                        p.display().to_string(),
                    ));
                }
            }
        }

        let acquire_by_hash = source_first
            .and_then(|x| x.get("Acquire-By-Hash"))
            .map(|x| x.to_lowercase() == "yes")
            .unwrap_or(false);

        let sha256 = source_first
            .and_then(|x| x.get("SHA256"))
            .take()
            .ok_or_else(|| InReleaseParserError::BadSha256Value(p.display().to_string()))?;

        let mut checksums = sha256.split('\n');

        // remove first item, It's empty
        checksums.next();

        let mut checksums_res = vec![];

        for i in checksums {
            let mut checksum_entry = i.split_whitespace();
            let checksum = checksum_entry
                .next()
                .ok_or_else(|| InReleaseParserError::BadChecksumEntry(i.to_owned()))?;
            let size = checksum_entry
                .next()
                .ok_or_else(|| InReleaseParserError::BadChecksumEntry(i.to_owned()))?;
            let name = checksum_entry
                .next()
                .ok_or_else(|| InReleaseParserError::BadChecksumEntry(i.to_owned()))?;
            checksums_res.push((name, size, checksum));
        }

        let mut res: SmallVec<[_; 32]> = smallvec![];

        let c_res_clone = checksums_res.clone();

        let c = checksums_res
            .into_iter()
            .filter(|(name, _, _)| {
                let mut name_split = name.split('/');
                let component = name_split.next();
                let component_type = name_split.next();

                // debian-installer 是为 Debian 安装器专门准备的源,应该没有人把 oma 用在这种场景上面
                let is_debian_installer = component_type
                    .map(|x| x == "debian-installer")
                    .unwrap_or(false);

                if let Some(c) = component {
                    if c != *name {
                        components.contains(&c.to_string())
                            && ((name.contains("all") || name.contains(arch))
                                && !is_debian_installer)
                    } else {
                        name.contains("all") || name.contains(arch)
                    }
                } else {
                    name.contains("all") || name.contains(arch)
                }
            })
            .collect::<Vec<_>>();

        let c = if c.is_empty() { c_res_clone } else { c };

        for i in c {
            let t = match i.0 {
                x if x.contains("BinContents") => DistFileType::BinaryContents,
                x if x.contains("Contents-") && file_is_compress(x) && !x.contains("udeb") => {
                    let s = x.split_once('.').unwrap();
                    DistFileType::CompressContents(s.0.to_string(), s.1.to_string())
                }
                x if x.contains("Contents-") && !x.contains('.') && !x.contains("udeb") => {
                    DistFileType::Contents
                }
                x if x.contains("Packages") && !x.contains('.') => DistFileType::PackageList,
                x if x.contains("Packages") && file_is_compress(x) => {
                    let s = x.split_once('.').unwrap();
                    DistFileType::CompressPackageList(s.0.to_string(), s.1.to_string())
                }
                x if x.contains("Release") => DistFileType::Release,
                x => {
                    debug!("Unknown file type: {x:?}");
                    continue;
                }
            };

            res.push(ChecksumItem {
                name: i.0.to_owned(),
                size: i
                    .1
                    .parse::<u64>()
                    .map_err(InReleaseParserError::ParseIntError)?,
                checksum: i.2.to_owned(),
                file_type: t,
            })
        }

        Ok(Self {
            _source: source,
            checksums: res,
            acquire_by_hash,
        })
    }
}

fn file_is_compress(name: &str) -> bool {
    name.ends_with(".gz") || name.ends_with(".bz2") || name.ends_with(".xz")
}

fn parse_date(date: &str) -> ParseResult<DateTime<FixedOffset>> {
    match DateTime::parse_from_rfc2822(date) {
        Ok(res) => Ok(res),
        Err(e) => {
            debug!("Parse {} failed: {e}, try to use date hack.", date);
            let hack_date = date_hack(date);
            DateTime::parse_from_rfc2822(&hack_date)
        }
    }
}

/// Replace RFC 1123/822/2822 non-compliant "UTC" marker with RFC 2822-compliant "+0000" whilst parsing InRelease.
/// and for non-standard X:YY:ZZ conversion to XX:YY:ZZ.
///
/// - Some third-party repositories (such as those generated with Aptly) uses "UTC" to denote the Coordinated Universal
/// Time, which is not allowed in RFC 1123 or 822/2822 (all calls for "GMT" or "UT", 822 allows "Z", and 2822 allows
/// "+0000").
/// - This is used by many commercial software vendors, such as Google, Microsoft, and Spotify.
/// - This is allowed in APT's RFC 1123 parser. However, as chrono requires full compliance with the
/// aforementioned RFC documents, "UTC" is considered illegal.
///
/// Replace the "UTC" marker at the end of date strings to make it palatable to chronos.
///
/// and for non-standard X:YY:ZZ conversion to XX:YY:ZZ to make it palatable to chronos.
fn date_hack(date: &str) -> String {
    let mut split_time = date
        .split_ascii_whitespace()
        .map(|x| x.to_string())
        .collect::<Vec<_>>();

    for c in split_time.iter_mut() {
        if c.is_empty() || !c.contains(':') {
            continue;
        }

        let mut time_split = c.splitn(2, ':').map(|x| x.to_string()).collect::<Vec<_>>();

        // X:YY:ZZ conversion to XX:YY:ZZ to make it palatable to chronos
        for k in time_split.iter_mut() {
            match k.parse::<u64>() {
                Ok(n) => match n {
                    0..=9 if k.len() == 1 => {
                        *k = "0".to_string() + k;
                    }
                    _ => continue,
                },
                Err(_) => break,
            }
        }

        *c = time_split.join(":");
    }

    let date = split_time.join(" ");

    date.replace("UTC", "+0000")
}

fn debcontrol_from_str(s: &str) -> InReleaseParserResult<Vec<SmallMap<16, String, String>>> {
    let mut res = vec![];

    let debcontrol =
        oma_debcontrol::parse_str(s).map_err(|_| InReleaseParserError::InReleaseSyntaxError)?;

    for i in debcontrol {
        let mut item = SmallMap::<16, _, _>::new();
        let field = i.fields;

        for j in field {
            item.insert(j.name.to_string(), j.value.to_string());
        }

        res.push(item);
    }

    Ok(res)
}

#[test]
fn test_date_hack() {
    let a = "Thu, 02 May 2024  9:58:03 UTC";
    let hack = date_hack(&a);
    assert_eq!(hack, "Thu, 02 May 2024 09:58:03 +0000");
    let b = DateTime::parse_from_rfc2822(&hack);
    assert!(b.is_ok());

    let a = "Thu, 02 May 2024 09:58:03 +0000";
    let hack = date_hack(&a);
    assert_eq!(hack, "Thu, 02 May 2024 09:58:03 +0000");
    let b = DateTime::parse_from_rfc2822(&hack);
    assert!(b.is_ok());

    let a = "Thu, 02 May 2024  0:58:03 +0000";
    let hack = date_hack(&a);
    assert_eq!(hack, "Thu, 02 May 2024 00:58:03 +0000");
    let b = DateTime::parse_from_rfc2822(&hack);
    assert!(b.is_ok());
}