sequoia-sq 1.4.0

Command-line frontends for Sequoia
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
use std::collections::BTreeMap;
use std::fs::File;
use std::io;
use std::io::IsTerminal;
use std::io::Seek;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;

use anyhow::Context;

use futures_util::StreamExt;

use tokio::sync::oneshot;
use tokio::task::JoinSet;
use tokio::task::LocalSet;

use indicatif::ProgressBar;
use indicatif::ProgressStyle;
use indicatif::WeakProgressBar;

use sequoia_net as net;
use net::reqwest;

use tempfile::NamedTempFile;

use sequoia_openpgp as openpgp;
use openpgp::Cert;
use openpgp::Fingerprint;
use openpgp::KeyHandle;
use openpgp::Packet;
use openpgp::parse::PacketParser;
use openpgp::parse::PacketParserResult;
use openpgp::parse::Parse;
use openpgp::parse::buffered_reader::{self, BufferedReader};
use openpgp::types::KeyFlags;

use crate::Result;
use crate::Sq;
use crate::cli::download;
use crate::cli::types::TrustAmount;
use crate::commands::network::CONNECT_TIMEOUT;
use crate::commands::network::USER_AGENT;
use crate::commands::verify::verify;
use crate::common::key_handle_dealias;
use crate::common::pki::authenticate::AuthenticateContext;
use crate::common::pki::authenticate::Query;
use crate::common::pki::authenticate;
use crate::common::ui;
use crate::sq::TrustThreshold;

// So we can deal with either named temp files or files.
enum SomeFile {
    Temp(NamedTempFile),
    File((File, PathBuf)),
}

impl SomeFile {
    fn as_ref(&self) -> &File {
        match self {
            SomeFile::Temp(t) => t.as_file(),
            SomeFile::File((f, _)) => &f,
        }
    }

    fn as_mut(&mut self) -> &mut File {
        match self {
            SomeFile::Temp(t) => t.as_file_mut(),
            SomeFile::File((ref mut f, _)) => f,
        }
    }

    fn path(&self) -> &Path {
        match self {
            SomeFile::Temp(t) => t.path(),
            SomeFile::File((_, p)) => p.as_path(),
        }
    }

    /// Writes a copy of the file to `new_path`.
    ///
    /// We optimize the case where this file is a temporary file, in
    /// which case we simply rename it.
    fn persist<P: AsRef<Path>>(self, new_path: P) -> Result<()> {
        match self {
            SomeFile::Temp(t) => {
                t.persist(new_path)?;
            },

            SomeFile::File((_, p)) => {
                // This was sourced from a local file, we cannot
                // rename that, but we can copy it.
                std::fs::copy(p, new_path)?;
            },
        }

        Ok(())
    }
}

// Spawn a task to download the `$url` to `$output` using `$http_client`.
//
// This is a macro rather than a function due to lifetimes.
//
// `$rt` is uninterpreted, and is returned as is.
//
// `$limit` causes the download to abort after that many bytes.
//
// `$file_name` is `$output`'s file name.  Its purely used for
// decorative purposes.
//
// `$pb` is a weak reference to a progress bar.
macro_rules! get {
    ($http_client:expr, $rt:expr, $url:expr, $limit:expr, $file_name:expr,
     $output: expr, $pb:expr) => {{
         let url: String = $url.into();
         let http_client: reqwest::Client = $http_client.clone();
         let limit: Option<usize> = $limit;
         let file_name: String = $file_name.into();
         let mut output = $output;
         let pb: WeakProgressBar = $pb;

         async move {
             if let Some(local_file_name) = url.strip_prefix("file://") {
                 let local_file_name = PathBuf::from(local_file_name);
                 match File::open(&local_file_name) {
                     Ok(file) => {
                         Ok(($rt, SomeFile::File((file, local_file_name))))
                     }
                     Err(err) => Err(err.into()),
                 }
             } else {
                 let mut bytes = 0;
                 let response = http_client.get(&url).send()
                     .await
                     .and_then(|r| r.error_for_status())
                     .with_context(|| format!("Fetching {}", url))?;

                 let len = response.content_length();
                 if let Some(pb) = pb.upgrade() {
                     if let Some(len) = len {
                         pb.inc_length(len);
                     } else {
                         // We don't know how much we need to download.  Switch
                         // to a spinner.
                         if ! pb.is_hidden() {
                             pb.set_style(ProgressStyle::default_spinner());
                         }
                     }
                 }

                 let mut stream = response.bytes_stream();
                 while let Some(item) = stream.next().await {
                     let item = item.with_context(|| {
                         format!("Fetching {}", url)
                     })?;
                     output.write_all(item.as_ref()).with_context(|| {
                         format!("Writing to {}", file_name)
                     })?;
                     bytes += item.len();
                     pb.upgrade().map(|pb| pb.inc(item.len() as u64));

                     if let Some(limit) = limit {
                         if bytes > limit {
                             return Err(anyhow::anyhow!(
                                 "{} exceeded download limit size ({} bytes)",
                                 url, limit));
                         }
                     }
                 }

                 output.flush()?;

                 Ok::<_, anyhow::Error>(($rt, SomeFile::Temp(output)))
             }
         }
    }}
}

pub fn dispatch(sq: Sq, c: download::Command)
    -> Result<()>
{
    let url = c.url;
    let signature_url = c.detached;
    let signatures = c.signatures;
    let signers =
        sq.resolve_certs_or_fail(&c.signers, TrustThreshold::Full)?;
    let output = c.output;

    if ! sq.quiet() && ! sq.batch {
        let output_is_terminal
            = output.path().is_none() && std::io::stdout().is_terminal();
        if output_is_terminal {
            weprintln!("Warning: will write the data to stdout, \
                        which appears to be a terminal.  Use --output \
                        to write to a file instead.");
        }
    }


    // Create the output file early.  Otherwise we may download a lot
    // of data and then fail to copy it.
    let mut output_file_;
    let mut stdout_;
    let mut output_file: &mut dyn Write = if let Some(file) = output.path() {
        output_file_ = if sq.overwrite {
            File::create(file)
                .with_context(|| format!("Opening {}", file.display()))?
        } else {
            File::options().write(true).create_new(true).open(file)
                .map_err(|err| {
                    if err.kind() == std::io::ErrorKind::AlreadyExists {
                        return anyhow::anyhow!(
                            "File {} exists, use \"sq --overwrite ...\" to overwrite",
                            file.display(),
                        );
                    }
                    err.into()
                })
                .with_context(|| format!("Opening {}", file.display()))?
        };
        &mut output_file_
    } else {
        stdout_ = std::io::stdout();
        &mut stdout_
    };


    // Create the progress bar.
    let progress_bar = if sq.verbose() || sq.batch {
        ProgressBar::hidden()
    } else {
        ProgressBar::new(0)
            .with_style(ProgressStyle::with_template(
                "{wide_bar} {decimal_bytes}/{decimal_total_bytes} ({eta} left)")
                        .expect("valid format"))
    };

    // A temporary file for the main data.  If output is not stdout,
    // we try and put it in the same directory as where it should end
    // up.
    let data_file = {
        let mut data_file = tempfile::Builder::new();
        data_file.prefix("sq-download");

        let partial;
        if let Some(path) = output.path() {
            if let Some(file_name) = path.file_name() {
                partial = format!(
                    "{}-partial",
                    String::from_utf8_lossy(file_name.as_encoded_bytes()));
                data_file.prefix(&partial);
            }

            if let Some(directory) = path.parent() {
                data_file.tempfile_in(directory)
            } else {
                let cwd = std::env::current_dir()?;
                data_file.tempfile_in(cwd)
            }
        } else {
            data_file.tempfile()
        }.context("Creating temporary file")?
    };

    let http_client = reqwest::Client::builder()
        .user_agent(USER_AGENT)
        .connect_timeout(CONNECT_TIMEOUT)
        .build()?;

    let requests = LocalSet::new();
    let mut task_set = JoinSet::new();

    // Since JoinSet::join_next has to return the same type, we use
    // the following to discriminate the tasks.
    enum Task {
        Url,
        Signature,
    }

    // Schedule the download of the file.
    task_set.spawn_local_on(
        get!(http_client.clone(), Task::Url, url, None,
             data_file.path().display().to_string(), data_file,
             progress_bar.downgrade()),
        &requests);

    // We need to do some acrobatics!!!  After we download the
    // signature file, we want to make sure that we can authenticate a
    // signer.  This means we need to use sq.  But, we can't move sq
    // to an async task, because sq has a lifetime that is shorter
    // than 'static.  Instead, we set up a scoped thread, which can
    // use variables with lifetimes less than static, and then do the
    // processing there.
    let signature_url_ = signature_url.clone();
    let (mut data_file, signature_file) = std::thread::scope(|scope| {
        // Schedule the download of the signature.
        if let Some(ref url) = signature_url_ {
            let sig_file = tempfile::NamedTempFile::new()?;

            let getter = get!(
                http_client.clone(), Task::Signature, url, None,
                sig_file.path().display().to_string(), sig_file,
                progress_bar.downgrade());

            let (request_tx, request_rx) = oneshot::channel();
            let (response_tx, response_rx) = oneshot::channel();

            task_set.spawn_local_on(
                async move {
                    let (task, sig_file) = getter.await?;

                    // The processing is handled by the thread below.
                    if request_tx.send(sig_file).is_err() {
                        return Err(anyhow::anyhow!(
                            "internal error: protocol violation"));
                    }
                    let sig_file = response_rx.await??;
                    Ok((task, sig_file))
                },
                &requests);

            let progress_bar_ = progress_bar.downgrade();
            let sq_ = &sq;
            let signers_ = &signers;
            scope.spawn(move || {
                let result = (|| {
                    let progress_bar = progress_bar_;
                    let sq = sq_;
                    let signers = signers_;

                    let mut sig_file = if let Ok(sig_file)
                        = request_rx.blocking_recv()
                    {
                        sig_file
                    } else {
                        return Err(anyhow::anyhow!(
                            "internal error: protocol violation"));
                    };

                    // Read the signature data and make sure we can
                    // authenticate at least one issuer's certificate.
                    sig_file.as_mut().rewind()?;
                    let mut ppr = PacketParser::from_reader(sig_file.as_ref())
                        .context("Parsing detached signature: either the signature \
                                  file does not actually contain an OpenPGP \
                                  signature, or it is corrupted.")?;
                    let mut signatures = Vec::new();
                    while let PacketParserResult::Some(pp) = ppr {
                        let (packet, next_ppr) = pp.next()?;
                        ppr = next_ppr;

                        match packet {
                            Packet::Signature(sig) => {
                                signatures.push(sig);
                            }
                            Packet::Marker(_) => (),
                            _ => {
                                return Err(anyhow::anyhow!(
                                    "Signature file does not contain a detached \
                                     signature.  It includes a {}.",
                                    packet.tag()));
                            }
                        }
                    }

                    if signatures.is_empty() {
                        return Err(anyhow::anyhow!(
                            "Signature file does not contain any signatures."));
                    }

                    let mut seen: BTreeMap<Fingerprint, Cert> = BTreeMap::new();
                    let mut authenticated = false;
                    for sig in signatures.iter() {
                        for issuer in sig.get_issuers() {
                            if let Some(cert)
                                = signers.iter().find(|c| c.key_handle().aliases(&issuer))
                            {
                                if seen.contains_key(&cert.fingerprint()) {
                                    // Already saw that certificate.
                                    continue;
                                }

                                authenticated = true;

                                if let Some(pb) = progress_bar.upgrade() {
                                    pb.suspend(|| {
                                        weprintln!(
                                            "Alleged signer {} is good listed.",
                                            cert.fingerprint());
                                    })
                                }
                            } else if let Ok(cert)
                                = sq.lookup_one(issuer,
                                                Some(KeyFlags::signing()),
                                                false)
                            {
                                if seen.contains_key(&cert.fingerprint()) {
                                    // Already saw that certificate.
                                    continue;
                                }

                                let mut auth = || {
                                    let result = authenticate(
                                        &mut std::io::stderr(),
                                        &sq,
                                        AuthenticateContext::Download,
                                        vec![
                                            Query::for_key_handle(
                                                None, cert.key_handle())
                                        ],
                                        false, // gossip
                                        false, // show unusable
                                        false, // certification network
                                        Some(TrustAmount::Full), // trust amount
                                        true, // show paths
                                    );

                                    if let Err(err) = result {
                                        weprintln!("Can't authenticate the \
                                                    alleged signer:");
                                        let _ = ui::emit_cert(
                                            &mut io::stderr(),
                                            sq, &cert);

                                        weprintln!(
                                            initial_indent = " - ",
                                            "{}",
                                            crate::one_line_error_chain(err));
                                        weprintln!();
                                    } else {
                                        authenticated = true;
                                    }
                                };

                                if let Some(pb) = progress_bar.upgrade() {
                                    pb.suspend(auth);
                                } else {
                                    auth();
                                }

                                seen.insert(cert.fingerprint(),
                                            cert);
                            }
                        }
                    }

                    if ! authenticated {
                        if let Some(pb) = progress_bar.upgrade() {
                            pb.finish_and_clear();
                        }

                        if seen.is_empty() {
                            weprintln!("We can't verify the signature, because \
                                        we don't have certificates for any of \
                                        the alleged signers:");
                        } else {
                            weprintln!("We can't verify the signature, because \
                                        we can't authenticate any of the \
                                        alleged signers:");
                        }

                        weprintln!();
                        let issuers = signatures.iter()
                            .flat_map(|sig| sig.get_issuers().into_iter())
                            .collect::<Vec<_>>();
                        let issuers
                            = key_handle_dealias(&issuers).collect::<Vec<_>>();

                        if issuers.is_empty() {
                            weprintln!(initial_indent = " - ",
                                       "No issuers (the signature may be \
                                       malformed)");
                        } else {
                            let mut missing = Vec::new();
                            for issuer in issuers.iter() {
                                if let KeyHandle::Fingerprint(fpr) = issuer {
                                    if let Some(cert) = seen.get(fpr) {
                                        weprintln!(initial_indent = " - ",
                                                   "{} {}",
                                                  issuer,
                                                   sq.best_userid(cert, true)
                                                   .display());
                                    } else {
                                        weprintln!(initial_indent = " - ",
                                                   "{} (missing certificate)",
                                                   issuer);
                                        missing.push(issuer);
                                    }
                                } else {
                                    weprintln!(initial_indent = " - ",
                                               "{} (missing certificate)",
                                               issuer);
                                    missing.push(issuer);
                                }
                            }

                            if ! missing.is_empty() {
                                let mut hint = sq.hint(format_args!(
                                    "Try searching public directories:"))
                                    .sq().arg("network").arg("search");
                                for issuer in issuers.into_iter() {
                                    hint = hint.arg(issuer.to_string());
                                }
                                hint.done();
                            }
                            if let Some(issuer) = seen.keys().next() {
                                sq.hint(format_args!(
                                    "Verify that one of the certificates is \
                                     authentic, and then link it:"))
                                .sq().arg("pki").arg("link").arg("add")
                                    .arg_value_hidden("--cert",
                                                      issuer,
                                                      "FINGERPRINT")
                                    .done();
                            }
                        }

                        return Err(anyhow::anyhow!("\
                            Can't authenticate any of the alleged signers"));
                    }

                    drop(ppr);

                    Ok(sig_file)
                })();

                if let Err(result) = response_tx.send(result) {
                    // (send returns result on failure.)  We failed to
                    // return the result.  Don't make things worse by
                    // swallowing any error.
                    if let Err(err) = result.as_ref() {
                        crate::print_error_chain(&err);
                    }

                    Err(anyhow::anyhow!("Internal error: failed to return \
                                         result to caller"))
                } else {
                    Ok(())
                }
            });
        }

        // And GO!!!
        let rt = tokio::runtime::Runtime::new()?;
        let (data_file, signature_file) = requests.block_on(&rt, async move {
            let mut data_file = None;
            let mut signature_file = None;
            let mut errors: Vec<anyhow::Error> = Vec::new();

            while let Some(result) = task_set.join_next().await {
                match result {
                    Ok(Ok((Task::Signature, file))) => signature_file = Some(file),
                    Ok(Ok((Task::Url, file))) => data_file = Some(file),
                    Ok(Err(err)) => {
                        // In case of an error abort all other (probably long
                        // running) tasks.
                        task_set.abort_all();
                        errors.push(err)
                    },
                    Err(err) => {
                        // In case of an error abort all other (probably long
                        // running) tasks.
                        task_set.abort_all();
                        // Ignore errors resulting from aborting tasks.
                        if ! err.is_cancelled() {
                            errors.push(err.into());
                        }
                    },
                }
            }

            let mut errors = errors.into_iter();
            if let Some(err) = errors.next() {
                eprintln!("Aborting download.");
                std::iter::once(&err)
                    .chain(errors.as_ref())
                    .for_each(|e| eprintln!("- {:?}", e));

                return Err(err);
            }

            let data_file = if let Some(data_file) = data_file {
                data_file
            } else {
                return Err(anyhow::anyhow!(
                    "Internal error while downloading data file"));
            };

            if signature_url_.is_some() && signature_file.is_none() {
                return Err(anyhow::anyhow!(
                    "Internal error while downloading signature file"));
            }

            Ok::<_, anyhow::Error>((data_file, signature_file))
        })?;

        Ok::<_, anyhow::Error>((data_file, signature_file))
    })?;

    drop(progress_bar);

    weprintln!();
    weprintln!("Finished downloading data.  Authenticating data.");
    weprintln!();

    data_file.as_mut().rewind()?;

    let result = verify(
        sq,
        buffered_reader::File::new_with_cookie(
            data_file.as_ref().try_clone()?, data_file.path(),
            Default::default())?.into_boxed(),
        signature_file.as_ref().map(|f| f.path().to_path_buf()),
        "--signature-url",
        signature_url.map(PathBuf::from),
        &mut output_file,
        signatures,
        signers);

    if let Err(err) = result {
        if let Some(path) = output.path() {
            if let Err(err) = std::fs::remove_file(path) {
                weprintln!("Verification failed, failed to remove \
                            unverified output saved to {}: {}",
                           path.display(), err);
            }
        }

        return Err(err);
    }

    if signature_file.is_some() {
        // Verify doesn't copy the data when checking detached
        // signatures.  Do it now.
        if let Some(p) = output.path() {
            data_file.persist(p)?;
        } else {
            // Copy the data to stdout.
            data_file.as_mut().rewind()?;
            std::io::copy(&mut data_file.as_ref(), output_file)?;
        }
    }

    result
}