subcomponent 0.1.0

A components orchestrator
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
/*
 * Copyright (c) 2017 Jean Guyomarc'h
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

extern crate std;
extern crate tempdir;
extern crate crypto;
use config;
use compiler::parser;
use compiler::parser::PropertyValue;
use fetcher;
use subprocess;
use unpacker;

use crypto::digest::Digest;
use std::io::Read;

const METHOD_NAME: &'static str = "artifact";

pub struct Method {
    urls: Vec<String>,
    md5: Option<String>,
    sha1: Option<String>,
    sha256: Option<String>,
    sha512: Option<String>,
    pgp_pubkey: Option<String>,
    pgp_signature: Option<String>,
    unpackers: Vec<Box<unpacker::Unpacker>>,
    need_integrity_checking: bool,
}


impl Method {
   fn check_pgp_signature(&self, download: &std::path::Path) -> Result<bool, subprocess::Error> {
      if self.pgp_pubkey.is_some() && self.pgp_signature.is_some() {
         let pubkey = self.pgp_pubkey.as_ref().unwrap();
         let signature = self.pgp_signature.as_ref().unwrap();

         /*
          * We first need to make sure that the public key has been registered
          * by gpg, so we can check the downloaded archive against the pgp
          * signature.
          */
         let mut check = subprocess::new("gpg");
         check.stdout(std::process::Stdio::null());
         check.arg("--recv-key");
         check.arg(pubkey);

         subprocess::run(&mut check)?;

         /*
          * Download the signature file using cURL.
          */
         let tmp_dir = tempdir::TempDir::new("pgp-dl")?;
         let signature_file = tmp_dir.path().join("signature.sig");
         let mut dl = subprocess::new("curl");
         dl.stdout(std::process::Stdio::null());
         dl.arg("--silent");
         dl.arg("--show-error");
         dl.arg("--output");
         dl.arg(&signature_file);
         dl.arg(signature);
         subprocess::run(&mut dl)?;

         /*
          * Now that gpg has got the public key, download the signature file so
          * we can check it against the downloaded archive.
          */
         let mut cmd = subprocess::new("gpg");
         cmd.stdout(std::process::Stdio::null());
         cmd.arg("--verify");
         cmd.arg(&signature_file);
         cmd.arg(download);
         subprocess::run(&mut cmd)?;

         /* If at this point nothing failed, we will naturally return true */
      }
      Ok(true)
   }

   fn check_md5(&self, file: &[u8]) -> bool {
      if let Some(ref expected) = self.md5 {
         debug!("Checking that md5 is {}", expected);

         let mut digest = crypto::md5::Md5::new();
         digest.input(file);

         let mut hash = vec![0u8; digest.output_bits() / 8];
         digest.result(hash.as_mut_slice());

         /*
          * We have received the hash as an array of bytes. We have to compare
          * it with a string that has every byte of the hash formatted as
          * hexadecimal. So we format the byte array into a formatted string.
          *
          * It is a bit ugly, as the conversion is done in one go, without
          * a clever loop. Let's say it's for performance reasons...
          */
         let hash_str = format!("{:02x}{:02x}{:02x}{:02x}\
      {:02x}{:02x}{:02x}{:02x}\
      {:02x}{:02x}{:02x}{:02x}\
      {:02x}{:02x}{:02x}{:02x}",
      hash[0], hash[1], hash[2], hash[3],
      hash[4], hash[5], hash[6], hash[7],
      hash[8], hash[9], hash[10], hash[11],
      hash[12], hash[13], hash[14], hash[15]);

         if *expected != hash_str {
            return false;
         } else {
            trace!("MD5 {} matches {}", expected, hash_str);
         }
      }
      /* If we have no md5 to check against, we pass. */
      true
   }

   fn check_sha1(&self, file: &[u8]) -> bool {
      if let Some(ref expected) = self.sha1 {
         debug!("Checking that sha1 is {}", expected);

         let mut digest = crypto::sha1::Sha1::new();
         digest.input(file);

         let mut hash = vec![0u8; digest.output_bits() / 8];
         digest.result(hash.as_mut_slice());

         let hash_str = format!("{:02x}{:02x}{:02x}{:02x}{:02x}\
      {:02x}{:02x}{:02x}{:02x}{:02x}\
      {:02x}{:02x}{:02x}{:02x}{:02x}\
      {:02x}{:02x}{:02x}{:02x}{:02x}",
      hash[0], hash[1], hash[2], hash[3], hash[4],
      hash[5], hash[6], hash[7], hash[8], hash[9],
      hash[10], hash[11], hash[12], hash[13], hash[14],
      hash[15], hash[16], hash[17], hash[18], hash[19]);

         if *expected != hash_str {
            error!("SHA1 mismatch: {}. Expected: {}.", hash_str, expected);
            return false
         } else {
            trace!("SHA1 {} matches {}", expected, hash_str);
         }
      }
      /* If we have no sha1 to check against, we pass. */
      true
   }

   fn check_sha256(&self, file: &[u8]) -> bool {
      if let Some(ref expected) = self.sha256 {
         debug!("Checking that sha256 is {}", expected);

         let mut digest = crypto::sha2::Sha256::new();
         digest.input(file);

         let mut hash = vec![0u8; digest.output_bits() / 8];
         digest.result(hash.as_mut_slice());

         let hash_str = format!("{:02x}{:02x}{:02x}{:02x}{:02x}\
      {:02x}{:02x}{:02x}{:02x}{:02x}\
      {:02x}{:02x}{:02x}{:02x}{:02x}\
      {:02x}{:02x}{:02x}{:02x}{:02x}\
      {:02x}{:02x}{:02x}{:02x}{:02x}\
      {:02x}{:02x}{:02x}{:02x}{:02x}\
      {:02x}{:02x}",
      hash[0], hash[1], hash[2], hash[3], hash[4],
      hash[5], hash[6], hash[7], hash[8], hash[9],
      hash[10], hash[11], hash[12], hash[13], hash[14],
      hash[15], hash[16], hash[17], hash[18], hash[19],
      hash[20], hash[21], hash[22], hash[23], hash[24],
      hash[25], hash[26], hash[27], hash[28], hash[29],
      hash[30], hash[31]
      );

         if *expected != hash_str {
            error!("SHA256 mismatch: {}. Expected: {}.", hash_str, expected);
            return false
         } else {
            trace!("SHA256 {} matches {}", expected, hash_str);
         }
      }
      /* If we have no sha1 to check against, we pass. */
      true
   }

   fn check_sha512(&self, file: &[u8]) -> bool {
      if let Some(ref expected) = self.sha512 {
         debug!("Checking that sha512 is {}", expected);

         let mut digest = crypto::sha2::Sha512::new();
         digest.input(file);

         let mut hash = vec![0u8; digest.output_bits() / 8];
         digest.result(hash.as_mut_slice());

         let hash_str = format!("{:02x}{:02x}{:02x}{:02x}{:02x}\
      {:02x}{:02x}{:02x}{:02x}{:02x}\
      {:02x}{:02x}{:02x}{:02x}{:02x}\
      {:02x}{:02x}{:02x}{:02x}{:02x}\
      {:02x}{:02x}{:02x}{:02x}{:02x}\
      {:02x}{:02x}{:02x}{:02x}{:02x}\
      {:02x}{:02x}{:02x}{:02x}{:02x}\
      {:02x}{:02x}{:02x}{:02x}{:02x}\
      {:02x}{:02x}{:02x}{:02x}{:02x}\
      {:02x}{:02x}{:02x}{:02x}{:02x}\
      {:02x}{:02x}{:02x}{:02x}{:02x}\
      {:02x}{:02x}{:02x}{:02x}{:02x}\
      {:02x}{:02x}{:02x}{:02x}",
      hash[0], hash[1], hash[2], hash[3], hash[4],
      hash[5], hash[6], hash[7], hash[8], hash[9],
      hash[10], hash[11], hash[12], hash[13], hash[14],
      hash[15], hash[16], hash[17], hash[18], hash[19],
      hash[20], hash[21], hash[22], hash[23], hash[24],
      hash[25], hash[26], hash[27], hash[28], hash[29],
      hash[30], hash[31], hash[32], hash[33], hash[34],
      hash[35], hash[36], hash[37], hash[38], hash[39],
      hash[40], hash[41], hash[42], hash[43], hash[44],
      hash[45], hash[46], hash[47], hash[48], hash[49],
      hash[50], hash[51], hash[52], hash[53], hash[54],
      hash[55], hash[56], hash[57], hash[58], hash[59],
      hash[60], hash[61], hash[62], hash[63]
      );

         if *expected != hash_str {
            error!("SHA512 mismatch: {}. Expected: {}.", hash_str, expected);
            return false
         } else {
            trace!("SHA512 {} matches {}", expected, hash_str);
         }
      }
      /* If we have no sha1 to check against, we pass. */
      true
   }

   fn download(&self, component: &config::Component, out_path: &std::path::Path) -> Result<std::path::PathBuf, fetcher::Error> {
      /*
       * Sanitize the output directory where the component will be downloaded.
       */
      for url in &self.urls {
         trace!("Downloading from {}", url);

         /* Find out what is the name of the file to be downloaded */
         let file = match url.rfind('/') {
            Some(index) => { &url[index+1..] },
            None => {
               error!("Failed to determine the file to be downloaded");
               continue;
            }
         };

         /* Final download path */
         let mut output_path = std::path::PathBuf::new();
         output_path.push(out_path);
         output_path.push(file);

         /* Run cURL */
         let mut cmd = subprocess::new("curl");
         cmd.stdout(std::process::Stdio::null());
         cmd.arg("--silent");
         cmd.arg("--show-error");
         cmd.arg("--output");
         cmd.arg(&output_path);
         cmd.arg(url);

         if subprocess::run(&mut cmd).is_ok() {

            let mut hash_failed = 0;
            if self.need_integrity_checking {
               let mut file = std::fs::File::open(&output_path)?;
               let mut file_data: Vec<u8> = Vec::new();

               file.read_to_end(&mut file_data)?;

               if ! self.check_md5(&file_data) {
                  hash_failed += 1;
               }
               if ! self.check_sha1(&file_data) {
                  hash_failed += 1;
               }
               if ! self.check_sha256(&file_data) {
                  hash_failed += 1;
               }
               if ! self.check_sha512(&file_data) {
                  hash_failed += 1;
               }
               if ! self.check_pgp_signature(&output_path)? {
                  hash_failed += 1;
               }
            }

            if hash_failed == 0 {
               return Ok(output_path);
            } else {
               error!("{} hashing check(s) failed", hash_failed);
            }
         }


         error!("Failed to fetch coherent artifact \"{}\" for url \"{}\"",
                component.id_get(), url);
      }

      /*
       * At this point, everything failed!
       */
      Err(fetcher::Error::EverythingFailed)
   }
}

impl fetcher::Method for Method {
   fn fetch_new(&self, component: &config::Component) -> Result<(), fetcher::Error> {

      let mut dl_path = std::path::PathBuf::new();
      let component_path = std::path::Path::new(component.path_get());

      /*
       * We will want to download the file in the directory that will contain
       * the final file.
       */
      if ! component_path.is_absolute() {
         dl_path.push(std::env::current_dir()?);
      }
      dl_path.push(component_path);
      dl_path.pop();

      let mut file = self.download(component, dl_path.as_path())?;

      for unpacker in &self.unpackers {
         let canon_file = std::fs::canonicalize(file)?;
         file = unpacker.unpack(canon_file.as_path(), dl_path.as_path())?;
      }

      if ! component_path.exists() {
         error!("The fetch operation did not create the specified component \
         path {:?}", component_path);
         error!("This may happen because unpacking resulted in a \
                different path than the one expected.");
         Err(fetcher::Error::ComponentPathNotCreated)
      } else {
         Ok(())
      }
   }
   fn fetch_update(&self, component: &config::Component, force: bool) -> Result<(), fetcher::Error> {
      /*
       * Unless we forced the fetch, we do nothing at all. We have no way to
       * check that the directory is consistent with the source archive. We
       * could try to keep the hash of the final output around, but it will
       * be slow (imagine hashing the kernel), and tedious to manage.
       */
      if force {
         self.fetch_new(component)
      } else {
         Ok(())
      }
   }

   #[allow(unused_variables)]
   fn is_fetchable(&self, component: &config::Component) -> bool {
      /*
       * Artifacts are always fetchable. We don't have to bother with overriding
       * the existing artifact.
       */
      true
   }

   fn name_get(&self) -> &str {
      METHOD_NAME
   }
}


fn parse_hash_algorithm(component: &str, cfg: &config::Config, algo: &str) -> Result<Option<String>, parser::Error> {
   if let Some(prop) = cfg.get_fetch_property(component, METHOD_NAME, algo) {
      match prop {
         PropertyValue::StringValue(val) => { return Ok(Some(val.clone())); },
         _ => { return Err(parser::Error::InvalidPropertyType); }
      }
   }
   Ok(None)
}

fn parse_pgp_pubkey(component: &str, cfg: &config::Config) -> Result<Option<String>, parser::Error> {
   if let Some(prop) = cfg.get_fetch_property(component, METHOD_NAME, "pgp-pubkey") {
      match prop {
         PropertyValue::StringValue(val) => { return Ok(Some(val.clone())); },
         _ => { return Err(parser::Error::InvalidPropertyType); }
      }
   }
   Ok(None)
}

fn parse_pgp_signature(component: &str, cfg: &config::Config) -> Result<Option<String>, parser::Error> {
   if let Some(prop) = cfg.get_fetch_property(component, METHOD_NAME, "pgp-signature") {
      match prop {
         PropertyValue::StringValue(val) => { return Ok(Some(val.clone())); },
         _ => { return Err(parser::Error::InvalidPropertyType); }
      }
   }
   Ok(None)
}

fn parse_compression(component: &str, cfg: &config::Config) -> Result<Vec<Box<unpacker::Unpacker>>, parser::Error> {
   if let Some(prop) = cfg.get_fetch_property(component, METHOD_NAME, "compression") {
      match prop {
         PropertyValue::StringListValue(val) => {
            match unpacker::get_for_archive(val) {
               Ok(unpackers) => { return Ok(unpackers); },
               Err(_) => { return Err(parser::Error::InvalidUnpackMethod); }
            }
         },
         _ => { return Err(parser::Error::InvalidPropertyType); },
      }
   }
   Ok(Vec::new())
}

pub fn parse(component: &str, cfg: &config::Config) -> Result<Box<fetcher::Method>, parser::Error> {

   let urls = try!(fetcher::parse_url(component, METHOD_NAME, cfg));
   let md5 = try!(parse_hash_algorithm(component, cfg, "md5"));
   let sha1 = try!(parse_hash_algorithm(component, cfg, "sha1"));
   let sha256 = try!(parse_hash_algorithm(component, cfg, "sha256"));
   let sha512 = try!(parse_hash_algorithm(component, cfg, "sha512"));
   let pgp_pubkey = try!(parse_pgp_pubkey(component, cfg));
   let pgp_signature = try!(parse_pgp_signature(component, cfg));
   let unpackers = try!(parse_compression(component, cfg));
   let mut need_integrity_checking = false;

   if (pgp_pubkey.is_some() || pgp_signature.is_some()) &&
      (pgp_pubkey.is_none() || pgp_signature.is_none()) {
      error!("pgp-pubkey and pgp-signature must be used together");
      return Err(parser::Error::MissingRequiredProperty);
   }

   /*
    * After downloading a file, we will have to check one or several hashes,
    * when specified. Hashing requires to load the whole downloaded file in
    * RAM, and it may take some time. So, instead of wasting time in loading
    * the file when to hash check is required, we will first determine whether
    * we need or not to check hashes. So, we will only read the whole file
    * when it is reaaly needed.
    */
   if md5.is_some() || sha1.is_some() || sha256.is_some() || sha512.is_some() ||
      (pgp_pubkey.is_some() && pgp_signature.is_some()) {
      need_integrity_checking = true;
   }


   Ok(Box::new(Method {
      urls: urls,
      md5: md5,
      sha1: sha1,
      sha256: sha256,
      sha512: sha512,
      pgp_pubkey: pgp_pubkey,
      pgp_signature: pgp_signature,

      unpackers: unpackers,
      need_integrity_checking: need_integrity_checking,
   }))
}