| 1 |
use std::ops::BitOr; |
| 2 |
use std::path::PathBuf; |
| 3 |
use std::str::FromStr; |
| 4 |
|
| 5 |
use anyhow::Context; |
| 6 |
use anyhow::Result; |
| 7 |
|
| 8 |
use typenum::Unsigned; |
| 9 |
|
| 10 |
use sequoia_openpgp as openpgp; |
| 11 |
use openpgp::KeyHandle; |
| 12 |
use openpgp::packet::UserID; |
| 13 |
|
| 14 |
use crate::cli::config; |
| 15 |
use crate::cli::encrypt::ENCRYPT_FOR_SELF; |
| 16 |
use crate::cli::escape_for_shell; |
| 17 |
use crate::cli::sign::SIGNER_SELF; |
| 18 |
use crate::cli::pki::vouch::CERTIFIER_SELF; |
| 19 |
use crate::cli::types::SpecialName; |
| 20 |
|
| 21 |
/// The prefix for the designators. |
| 22 |
/// |
| 23 |
/// See [`NoPrefix`], [`CertPrefix`], etc. |
| 24 |
pub trait ArgumentPrefix { |
| 25 |
fn prefix() -> &'static str; |
| 26 |
|
| 27 |
/// The argument group's name, e.g., "cert", "for". |
| 28 |
fn name() -> &'static str; |
| 29 |
} |
| 30 |
|
| 31 |
pub struct ConcreteArgumentPrefix<T>(std::marker::PhantomData<T>) |
| 32 |
where T: typenum::Unsigned; |
| 33 |
|
| 34 |
/// "--cert", "--userid", "--file", etc. |
| 35 |
pub type NoPrefix = ConcreteArgumentPrefix<typenum::U0>; |
| 36 |
/// "--cert", "--cert-userid", "--cert-file", etc. |
| 37 |
pub type CertPrefix = ConcreteArgumentPrefix<typenum::U1>; |
| 38 |
|
| 39 |
/// "--for", "--for-userid", "--for-file", etc. |
| 40 |
pub type RecipientPrefix = ConcreteArgumentPrefix<typenum::U2>; |
| 41 |
|
| 42 |
impl ArgumentPrefix for NoPrefix { |
| 43 |
fn prefix() -> &'static str { |
| 44 |
"" |
| 45 |
} |
| 46 |
|
| 47 |
fn name() -> &'static str { |
| 48 |
"cert" |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
impl ArgumentPrefix for CertPrefix { |
| 53 |
fn prefix() -> &'static str { |
| 54 |
"cert-" |
| 55 |
} |
| 56 |
|
| 57 |
fn name() -> &'static str { |
| 58 |
"cert" |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
impl ArgumentPrefix for RecipientPrefix { |
| 63 |
fn prefix() -> &'static str { |
| 64 |
"for-" |
| 65 |
} |
| 66 |
|
| 67 |
fn name() -> &'static str { |
| 68 |
"for" |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/// "--signer", "--signer-userid", "--signer-file", etc. |
| 73 |
pub type SignerPrefix = ConcreteArgumentPrefix<typenum::U3>; |
| 74 |
|
| 75 |
impl ArgumentPrefix for SignerPrefix { |
| 76 |
fn prefix() -> &'static str { |
| 77 |
"signer-" |
| 78 |
} |
| 79 |
|
| 80 |
fn name() -> &'static str { |
| 81 |
"signer" |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
/// "--revoker", "--revoker-userid", "--revoker-file", etc. |
| 86 |
pub type RevokerPrefix = ConcreteArgumentPrefix<typenum::U4>; |
| 87 |
|
| 88 |
impl ArgumentPrefix for RevokerPrefix { |
| 89 |
fn prefix() -> &'static str { |
| 90 |
"revoker-" |
| 91 |
} |
| 92 |
|
| 93 |
fn name() -> &'static str { |
| 94 |
"revoker" |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/// "--certifier", "--certifier-userid", "--certifier-file", etc. |
| 99 |
pub type CertifierPrefix = ConcreteArgumentPrefix<typenum::U5>; |
| 100 |
|
| 101 |
impl ArgumentPrefix for CertifierPrefix { |
| 102 |
fn prefix() -> &'static str { |
| 103 |
"certifier-" |
| 104 |
} |
| 105 |
|
| 106 |
fn name() -> &'static str { |
| 107 |
"certifier" |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
/// "--source", "--source-userid", "--source-file", etc. |
| 112 |
pub type SourcePrefix = ConcreteArgumentPrefix<typenum::U6>; |
| 113 |
|
| 114 |
impl ArgumentPrefix for SourcePrefix { |
| 115 |
fn prefix() -> &'static str { |
| 116 |
"source-" |
| 117 |
} |
| 118 |
|
| 119 |
fn name() -> &'static str { |
| 120 |
"source" |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
/// "--target", "--target-userid", "--target-file", etc. |
| 125 |
pub type TargetPrefix = ConcreteArgumentPrefix<typenum::U7>; |
| 126 |
|
| 127 |
impl ArgumentPrefix for TargetPrefix { |
| 128 |
fn prefix() -> &'static str { |
| 129 |
"target-" |
| 130 |
} |
| 131 |
|
| 132 |
fn name() -> &'static str { |
| 133 |
"target" |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/// Adds a `--file` argument. |
| 138 |
pub type FileArg = typenum::U1; |
| 139 |
|
| 140 |
/// Adds a `--cert` argument. |
| 141 |
pub type CertArg = typenum::U2; |
| 142 |
|
| 143 |
/// Adds a `--userid` argument. |
| 144 |
pub type UserIDArg = typenum::U8; |
| 145 |
|
| 146 |
/// Adds a `--email` argument. |
| 147 |
pub type EmailArg = typenum::U16; |
| 148 |
|
| 149 |
/// Adds a `--domain` argument. |
| 150 |
pub type DomainArg = typenum::U32; |
| 151 |
|
| 152 |
/// Adds a `--grep` argument. |
| 153 |
pub type GrepArg = typenum::U64; |
| 154 |
|
| 155 |
/// Adds `--with-password`, and `--with-password-file` arguments. |
| 156 |
/// |
| 157 |
/// This is only used for `sq encrypt`. |
| 158 |
pub type WithPasswordArgs = typenum::U128; |
| 159 |
|
| 160 |
/// Adds a `--special` argument. |
| 161 |
pub type SpecialArg = typenum::U256; |
| 162 |
|
| 163 |
/// Adds a `--self` argument. |
| 164 |
pub type SelfArg = typenum::U512; |
| 165 |
|
| 166 |
/// Enables --file, --cert, --userid, --email, --domain, and --grep |
| 167 |
/// (i.e., not --with-password, --with-password-file, --special). |
| 168 |
#[allow(dead_code)] |
| 169 |
pub type FileCertUserIDEmailDomainGrepArgs |
| 170 |
= <<<<<FileArg |
| 171 |
as std::ops::BitOr<CertArg>>::Output |
| 172 |
as std::ops::BitOr<UserIDArg>>::Output |
| 173 |
as std::ops::BitOr<EmailArg>>::Output |
| 174 |
as std::ops::BitOr<DomainArg>>::Output |
| 175 |
as std::ops::BitOr<GrepArg>>::Output; |
| 176 |
|
| 177 |
/// Enables --file, --cert, --userid, --email, and --domain, (i.e., |
| 178 |
/// not --grep, --with-password, --with-password-file, or --special). |
| 179 |
#[allow(dead_code)] |
| 180 |
pub type FileCertUserIDEmailDomainArgs |
| 181 |
= <<<<FileArg |
| 182 |
as std::ops::BitOr<CertArg>>::Output |
| 183 |
as std::ops::BitOr<UserIDArg>>::Output |
| 184 |
as std::ops::BitOr<EmailArg>>::Output |
| 185 |
as std::ops::BitOr<DomainArg>>::Output; |
| 186 |
|
| 187 |
/// Enables --cert, --userid, --email, --domain, and --grep (i.e., not |
| 188 |
/// --file, --with-password, --with-password-file, or --special). |
| 189 |
pub type CertUserIDEmailDomainGrepArgs |
| 190 |
= <<<<CertArg as std::ops::BitOr<UserIDArg>>::Output |
| 191 |
as std::ops::BitOr<EmailArg>>::Output |
| 192 |
as std::ops::BitOr<DomainArg>>::Output |
| 193 |
as std::ops::BitOr<GrepArg>>::Output; |
| 194 |
|
| 195 |
/// Enables --cert, --userid, --email, and --file (i.e., not --domain, |
| 196 |
/// --grep, --with-password, --with-password-file, or --special). |
| 197 |
pub type CertUserIDEmailFileArgs |
| 198 |
= <<<CertArg as std::ops::BitOr<UserIDArg>>::Output |
| 199 |
as std::ops::BitOr<EmailArg>>::Output |
| 200 |
as std::ops::BitOr<FileArg>>::Output; |
| 201 |
|
| 202 |
/// Enables --cert, --userid, --email, --file, and --self (i.e., not |
| 203 |
/// --domain, --grep, --with-password, --with-password-file, or |
| 204 |
/// --special). |
| 205 |
pub type CertUserIDEmailFileSelfArgs |
| 206 |
= <CertUserIDEmailFileArgs as std::ops::BitOr<SelfArg>>::Output; |
| 207 |
|
| 208 |
/// Enables --cert, --userid, --email, --file, --self, and --special. |
| 209 |
pub type CertUserIDEmailFileSelfSpecialArgs |
| 210 |
= <CertUserIDEmailFileSelfArgs as std::ops::BitOr<SpecialArg>>::Output; |
| 211 |
|
| 212 |
/// Enables --cert, --userid, and --email (i.e., not --domain, --grep, |
| 213 |
/// --file, --with-password, --with-password-file, or --special). |
| 214 |
pub type CertUserIDEmailArgs |
| 215 |
= <<CertArg as std::ops::BitOr<UserIDArg>>::Output |
| 216 |
as std::ops::BitOr<EmailArg>>::Output; |
| 217 |
|
| 218 |
/// Enables --cert, --userid, --email, --file, --with-password and |
| 219 |
/// --with-password-file (i.e., not --domain, --grep, or --special). |
| 220 |
pub type CertUserIDEmailFileWithPasswordArgs |
| 221 |
= <<<<CertArg as std::ops::BitOr<UserIDArg>>::Output |
| 222 |
as std::ops::BitOr<EmailArg>>::Output |
| 223 |
as std::ops::BitOr<FileArg>>::Output |
| 224 |
as std::ops::BitOr<WithPasswordArgs>>::Output; |
| 225 |
|
| 226 |
/// Enables --cert, --userid, --email, --file, --self, --with-password |
| 227 |
/// and --with-password-file (i.e., not --domain, --grep, or |
| 228 |
/// --special). |
| 229 |
pub type CertUserIDEmailFileSelfWithPasswordArgs = |
| 230 |
<CertUserIDEmailFileWithPasswordArgs as std::ops::BitOr<SelfArg>>::Output; |
| 231 |
|
| 232 |
/// Enables --cert, and --file (i.e., not --userid, --email, --domain, |
| 233 |
/// --grep, --with-password, --with-password-file, or --special). |
| 234 |
pub type CertFileArgs = <CertArg as std::ops::BitOr<FileArg>>::Output; |
| 235 |
|
| 236 |
/// Enables --cert, and --special (i.e., not --userid, --email, |
| 237 |
/// --domain, --grep, --with-password, or --with-password-file). |
| 238 |
pub type CertSpecialArgs = <CertArg as std::ops::BitOr<SpecialArg>>::Output; |
| 239 |
|
| 240 |
/// Argument parser options. |
| 241 |
|
| 242 |
/// Default options, no flag selected. |
| 243 |
pub type NoOptions = typenum::U0; |
| 244 |
|
| 245 |
/// Normally it is possible to designate multiple certificates. This |
| 246 |
/// errors out if there is more than one value. |
| 247 |
pub type OneValue = typenum::U1; |
| 248 |
|
| 249 |
/// Normally a certificate designator is required, and errors out if |
| 250 |
/// there isn't at least one value. This makes the cert designator |
| 251 |
/// completely optional. |
| 252 |
pub type OptionalValue = typenum::U2; |
| 253 |
|
| 254 |
/// Combines OneValue and OptionalValue. |
| 255 |
pub type OneOptionalValue |
| 256 |
= <OneValue as BitOr<OptionalValue>>::Output; |
| 257 |
|
| 258 |
/// Cause --file to require --output. |
| 259 |
pub type FileRequiresOutput = typenum::U4; |
| 260 |
|
| 261 |
/// Combines OneValue and FileRequiresOutput. |
| 262 |
/// |
| 263 |
/// Most useful for subcommands operating on keys. |
| 264 |
pub type OneValueAndFileRequiresOutput |
| 265 |
= <OneValue as BitOr<FileRequiresOutput>>::Output; |
| 266 |
|
| 267 |
/// Require either a cert designator, or the `all` parameter. |
| 268 |
/// |
| 269 |
/// Note: the `all` parameter is not part of the cert designators |
| 270 |
/// argument, but must be explicitly added. |
| 271 |
pub type CertOrAll = typenum::U8; |
| 272 |
|
| 273 |
/// Require either a cert designator, or the `without-signature` |
| 274 |
/// parameter. |
| 275 |
pub type SignerOrWithoutSignature = typenum::U16; |
| 276 |
|
| 277 |
// Additional documentation. |
| 278 |
|
| 279 |
/// The prefix for the designators. |
| 280 |
/// |
| 281 |
/// See [`NoPrefix`], [`CertPrefix`], etc. |
| 282 |
pub trait AdditionalDocs { |
| 283 |
/// The short help for clap. |
| 284 |
// XXX: This should return a Cow<'static, str>, but there is no |
| 285 |
// implementation of From<Cow<'static, str>> for StyledStr, |
| 286 |
// see https://github.com/clap-rs/clap/issues/5785 |
| 287 |
fn help(_arg: &'static str, help: &'static str) -> clap::builder::StyledStr { |
| 288 |
help.into() |
| 289 |
} |
| 290 |
|
| 291 |
/// The long help for clap, if any. |
| 292 |
// XXX: This should return a Cow<'static, str>, but there is no |
| 293 |
// implementation of From<Cow<'static, str>> for StyledStr, |
| 294 |
// see https://github.com/clap-rs/clap/issues/5785 |
| 295 |
fn long_help(_arg: &'static str, _help: &'static str) -> Option<clap::builder::StyledStr> { |
| 296 |
None |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
/// No additional documentation. |
| 301 |
pub struct NoDoc(()); |
| 302 |
impl AdditionalDocs for NoDoc {} |
| 303 |
|
| 304 |
|
| 305 |
/// Documentation for signer arguments. |
| 306 |
pub struct ToVerifyDoc {} |
| 307 |
impl AdditionalDocs for ToVerifyDoc { |
| 308 |
fn help(arg: &'static str, help: &'static str) -> clap::builder::StyledStr { |
| 309 |
match arg { |
| 310 |
"file" => |
| 311 |
"Require a signature from a certificate read from PATH" |
| 312 |
.into(), |
| 313 |
_ => { |
| 314 |
debug_assert!(help.starts_with("Use certificates")); |
| 315 |
help.replace("Use certificates", |
| 316 |
"Require a signature from a certificate") |
| 317 |
.into() |
| 318 |
}, |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
/// Documentation for certifier arguments. |
| 324 |
pub struct CertifierDoc {} |
| 325 |
impl AdditionalDocs for CertifierDoc { |
| 326 |
fn help(arg: &'static str, help: &'static str) -> clap::builder::StyledStr { |
| 327 |
match arg { |
| 328 |
"file" => |
| 329 |
"Create the certification using the key read from PATH" |
| 330 |
.into(), |
| 331 |
_ => { |
| 332 |
debug_assert!(help.starts_with("Use certificates")); |
| 333 |
help.replace("Use certificates", |
| 334 |
"Create the certification using the key") |
| 335 |
.into() |
| 336 |
}, |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
/// A certificate designator. |
| 342 |
#[derive(Debug)] |
| 343 |
pub enum CertDesignator { |
| 344 |
/// Reads certificates from stdin. |
| 345 |
/// |
| 346 |
/// This is translated from `--file -`. |
| 347 |
Stdin, |
| 348 |
|
| 349 |
/// Reads certificates from a file. |
| 350 |
/// |
| 351 |
/// `--file`. |
| 352 |
File(PathBuf), |
| 353 |
|
| 354 |
/// Looks up certificates on the cert store by key handle. |
| 355 |
/// |
| 356 |
/// By default, this matches on both the primary key, and the |
| 357 |
/// subkeys (whether they have a back sig or not). |
| 358 |
/// |
| 359 |
/// `--cert`. |
| 360 |
Cert(KeyHandle), |
| 361 |
|
| 362 |
/// Looks up certificates on the cert store by user ID. |
| 363 |
/// |
| 364 |
/// By default, this matches on fully authenticated user IDs |
| 365 |
/// (trust amount >= 120). The user IDs don't need to be |
| 366 |
/// self-signed. |
| 367 |
/// |
| 368 |
/// `--userid`. |
| 369 |
UserID(String), |
| 370 |
|
| 371 |
/// Looks up certificates on the cert store by email. |
| 372 |
/// |
| 373 |
/// By default, this matches on fully authenticated user IDs |
| 374 |
/// (trust amount >= 120) with the specified email address. The |
| 375 |
/// user IDs don't need to be self-signed. |
| 376 |
/// |
| 377 |
/// `--email`. |
| 378 |
Email(String), |
| 379 |
|
| 380 |
/// Looks up certificates on the cert store by email domain. |
| 381 |
/// |
| 382 |
/// By default, this matches on fully authenticated user IDs |
| 383 |
/// (trust amount >= 120) with an email address in the specified |
| 384 |
/// domain. The user IDs don't need to be self-signed. |
| 385 |
/// |
| 386 |
/// `--domain`. |
| 387 |
Domain(String), |
| 388 |
|
| 389 |
/// Looks up certificates on the cert store by substring. |
| 390 |
/// |
| 391 |
/// By default, this matches on fully authenticated user IDs |
| 392 |
/// (trust amount >= 120) that contain the specified string. The |
| 393 |
/// string is matched case insentively. The user IDs don't need |
| 394 |
/// to be self-signed. |
| 395 |
/// |
| 396 |
/// `--grep`. |
| 397 |
Grep(String), |
| 398 |
|
| 399 |
/// Looks up certificates special name. |
| 400 |
/// |
| 401 |
/// This maps special names like keys.openpgp.org to certificates. |
| 402 |
/// |
| 403 |
/// `--special`. |
| 404 |
Special(SpecialName), |
| 405 |
|
| 406 |
/// Use the configured set of certificates presumably belonging to |
| 407 |
/// oneself. |
| 408 |
/// |
| 409 |
/// This is used to add ones own certificates as encryption |
| 410 |
/// recipients. |
| 411 |
/// |
| 412 |
/// `--self`. |
| 413 |
Self_, |
| 414 |
} |
| 415 |
|
| 416 |
impl CertDesignator { |
| 417 |
/// Returns the argument's name, e.g., `--cert`. |
| 418 |
pub fn argument_name<Prefix>(&self) -> String |
| 419 |
where Prefix: ArgumentPrefix |
| 420 |
{ |
| 421 |
self.argument_name_with_prefix(Prefix::prefix()) |
| 422 |
} |
| 423 |
|
| 424 |
/// Returns the argument's name like |
| 425 |
/// [`CertDesignator::argument_name`], but with the prefix given |
| 426 |
/// as string, not as generic parameter. |
| 427 |
pub fn argument_name_with_prefix(&self, prefix: &str) -> String |
| 428 |
{ |
| 429 |
use CertDesignator::*; |
| 430 |
match self { |
| 431 |
Stdin => format!("--{}file", prefix), |
| 432 |
File(_path) => format!("--{}file", prefix), |
| 433 |
Cert(_kh) => { |
| 434 |
if ! prefix.is_empty() { |
| 435 |
// We want `--cert`, not `--cert-cert`, or |
| 436 |
// `--for` instead of `--for-cert`. |
| 437 |
format!("--{}", prefix.strip_suffix("-") |
| 438 |
.expect("prefix must end with -")) |
| 439 |
} else { |
| 440 |
format!("--{}cert", prefix) |
| 441 |
} |
| 442 |
}, |
| 443 |
UserID(_userid) => format!("--{}userid", prefix), |
| 444 |
Email(_email) => format!("--{}email", prefix), |
| 445 |
Domain(_domain) => format!("--{}domain", prefix), |
| 446 |
Grep(_pattern) => format!("--{}grep", prefix), |
| 447 |
Special(_special) => format!("--{}special", prefix), |
| 448 |
Self_ => format!("--{}self", prefix), |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
/// Returns the argument's name and value, e.g., `--cert-file |
| 453 |
/// file`. |
| 454 |
pub fn argument<Prefix>(&self) -> String |
| 455 |
where Prefix: ArgumentPrefix, |
| 456 |
{ |
| 457 |
self.argument_with_prefix(Prefix::prefix()) |
| 458 |
} |
| 459 |
|
| 460 |
/// Returns the argument's name and value like |
| 461 |
/// [`CertDesignator::argument`], but with the prefix given as |
| 462 |
/// string, not as generic parameter. |
| 463 |
pub fn argument_with_prefix(&self, prefix: &str) -> String |
| 464 |
{ |
| 465 |
|
| 466 |
let argument_name = self.argument_name_with_prefix(prefix); |
| 467 |
|
| 468 |
use CertDesignator::*; |
| 469 |
match self { |
| 470 |
Stdin => format!("{} -", argument_name), |
| 471 |
File(path) => format!("{} {}", argument_name, path.display()), |
| 472 |
Cert(kh) => format!("{} {}", argument_name, kh), |
| 473 |
UserID(userid) => format!("{} {:?}", argument_name, userid), |
| 474 |
Email(email) => format!("{} {:?}", argument_name, email), |
| 475 |
Domain(domain) => format!("{} {:?}", argument_name, domain), |
| 476 |
Grep(pattern) => format!("{} {:?}", argument_name, pattern), |
| 477 |
Special(special) => format!("{} {:?}", argument_name, special), |
| 478 |
Self_ => argument_name, |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
/// Returns the argument's value. |
| 483 |
pub fn argument_value(&self) -> Option<String> |
| 484 |
{ |
| 485 |
use CertDesignator::*; |
| 486 |
match self { |
| 487 |
Stdin => Some(format!("-")), |
| 488 |
File(path) => Some(format!("{}", path.display())), |
| 489 |
Cert(kh) => Some(format!("{}", kh)), |
| 490 |
UserID(userid) => Some(escape_for_shell(userid).to_string()), |
| 491 |
Email(email) => Some(escape_for_shell(email).to_string()), |
| 492 |
Domain(domain) => Some(escape_for_shell(domain).to_string()), |
| 493 |
Grep(pattern) => Some(escape_for_shell(pattern).to_string()), |
| 494 |
Special(special) => Some(special.to_string()), |
| 495 |
Self_ => None, |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
/// Whether the argument reads from a file. |
| 500 |
pub fn from_file(&self) -> bool { |
| 501 |
matches!(self, CertDesignator::File(_)) |
| 502 |
} |
| 503 |
|
| 504 |
/// Whether the argument reads from stdin. |
| 505 |
pub fn from_stdin(&self) -> bool { |
| 506 |
matches!(self, CertDesignator::Stdin) |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
/// A data structure that can be flattened into a clap `Command`, and |
| 511 |
/// adds arguments to address certificates. |
| 512 |
/// |
| 513 |
/// Depending on `Arguments`, it adds zero or more arguments to the |
| 514 |
/// subcommand. If `CertArg` is selected, for instance, then a |
| 515 |
/// `--cert` argument is added. |
| 516 |
/// |
| 517 |
/// `Prefix` is a prefix to use. Using `RecipientPrefix` will |
| 518 |
/// change, e.g., `--email` to `--for-email`. |
| 519 |
/// |
| 520 |
/// `Options` are the set of options to the argument parser. |
| 521 |
pub struct CertDesignators<Arguments, Prefix=NoPrefix, Options=NoOptions, |
| 522 |
Doc=NoDoc> |
| 523 |
{ |
| 524 |
/// The set of certificate designators. |
| 525 |
pub designators: Vec<CertDesignator>, |
| 526 |
|
| 527 |
/// --with-password |
| 528 |
with_passwords: usize, |
| 529 |
/// --with-password-file |
| 530 |
with_password_files: Vec<PathBuf>, |
| 531 |
|
| 532 |
arguments: std::marker::PhantomData<(Arguments, Prefix, Options, |
| 533 |
Doc)>, |
| 534 |
} |
| 535 |
|
| 536 |
impl<Arguments, Prefix, Options, Doc> std::fmt::Debug |
| 537 |
for CertDesignators<Arguments, Prefix, Options, Doc> |
| 538 |
{ |
| 539 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 540 |
f.debug_struct("CertDesignators") |
| 541 |
.field("designators", &self.designators) |
| 542 |
.finish() |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
impl From<KeyHandle> for CertDesignators<CertArg, NoPrefix, NoOptions, NoDoc> { |
| 547 |
/// Sometimes we need to convert a key handle into a cert |
| 548 |
/// designator. Voila. |
| 549 |
fn from(kh: KeyHandle) -> Self { |
| 550 |
Self { |
| 551 |
designators: vec![ CertDesignator::Cert(kh) ], |
| 552 |
with_passwords: 0, |
| 553 |
with_password_files: vec![], |
| 554 |
arguments: std::marker::PhantomData, |
| 555 |
} |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
impl From<&KeyHandle> for CertDesignators<CertArg, NoPrefix, NoOptions, NoDoc> { |
| 560 |
/// Sometimes we need to convert a key handle into a cert |
| 561 |
/// designator. Voila. |
| 562 |
fn from(kh: &KeyHandle) -> Self { |
| 563 |
kh.clone().into() |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
impl<Arguments, Prefix, Options, Doc> CertDesignators<Arguments, Prefix, Options, Doc> { |
| 568 |
/// Like `Vec::push`. |
| 569 |
pub fn push(&mut self, designator: CertDesignator) { |
| 570 |
self.designators.push(designator) |
| 571 |
} |
| 572 |
|
| 573 |
/// Like `Vec::is_empty`. |
| 574 |
pub fn is_empty(&self) -> bool { |
| 575 |
self.designators.is_empty() |
| 576 |
} |
| 577 |
|
| 578 |
/// Like `Vec::len`. |
| 579 |
pub fn len(&self) -> usize { |
| 580 |
self.designators.len() |
| 581 |
} |
| 582 |
|
| 583 |
/// Iterates over the certificate designators. |
| 584 |
pub fn iter(&self) -> impl Iterator<Item=&CertDesignator> { |
| 585 |
self.designators.iter() |
| 586 |
} |
| 587 |
|
| 588 |
/// Returns the number of times `--with-password` was given. |
| 589 |
pub fn with_passwords(&self) -> usize { |
| 590 |
self.with_passwords |
| 591 |
} |
| 592 |
|
| 593 |
/// Returns the `--with-password-file` arguments. |
| 594 |
pub fn with_password_files(&self) -> &[PathBuf] { |
| 595 |
&self.with_password_files[..] |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
impl<Arguments, Prefix, Options, Doc> clap::Args |
| 600 |
for CertDesignators<Arguments, Prefix, Options, Doc> |
| 601 |
where |
| 602 |
Arguments: typenum::Unsigned, |
| 603 |
Prefix: ArgumentPrefix, |
| 604 |
Options: typenum::Unsigned, |
| 605 |
Doc: AdditionalDocs, |
| 606 |
{ |
| 607 |
fn augment_args(mut cmd: clap::Command) -> clap::Command |
| 608 |
{ |
| 609 |
let arguments = Arguments::to_usize(); |
| 610 |
let file_arg = (arguments & FileArg::to_usize()) > 0; |
| 611 |
let cert_arg = (arguments & CertArg::to_usize()) > 0; |
| 612 |
let userid_arg = (arguments & UserIDArg::to_usize()) > 0; |
| 613 |
let email_arg = (arguments & EmailArg::to_usize()) > 0; |
| 614 |
let domain_arg = (arguments & DomainArg::to_usize()) > 0; |
| 615 |
let grep_arg = (arguments & GrepArg::to_usize()) > 0; |
| 616 |
let with_password_args = (arguments & WithPasswordArgs::to_usize()) > 0; |
| 617 |
let special_arg = (arguments & SpecialArg::to_usize()) > 0; |
| 618 |
let self_arg = (arguments & SelfArg::to_usize()) > 0; |
| 619 |
|
| 620 |
let options = Options::to_usize(); |
| 621 |
let one_value = (options & OneValue::to_usize()) > 0; |
| 622 |
let optional_value = (options & OptionalValue::to_usize()) > 0; |
| 623 |
let file_requires_output = |
| 624 |
(options & FileRequiresOutput::to_usize()) > 0; |
| 625 |
let cert_or_all = (options & CertOrAll::to_usize()) > 0; |
| 626 |
let signer_or_without_signature = |
| 627 |
(options & SignerOrWithoutSignature::to_usize()) > 0; |
| 628 |
|
| 629 |
let group = format!("cert-designator-{}-{:X}-{:X}", |
| 630 |
Prefix::name(), |
| 631 |
arguments, |
| 632 |
options); |
| 633 |
let mut arg_group = clap::ArgGroup::new(group); |
| 634 |
if one_value { |
| 635 |
arg_group = arg_group.multiple(false); |
| 636 |
} else { |
| 637 |
arg_group = arg_group.multiple(true); |
| 638 |
} |
| 639 |
|
| 640 |
if optional_value { |
| 641 |
arg_group = arg_group.required(false); |
| 642 |
} else { |
| 643 |
arg_group = arg_group.required(true); |
| 644 |
} |
| 645 |
|
| 646 |
if cert_or_all { |
| 647 |
arg_group = arg_group.arg("all"); |
| 648 |
} |
| 649 |
|
| 650 |
let action = if one_value { |
| 651 |
clap::ArgAction::Set |
| 652 |
} else { |
| 653 |
clap::ArgAction::Append |
| 654 |
}; |
| 655 |
|
| 656 |
// Converts a string to a valid `KeyHandle`. |
| 657 |
// |
| 658 |
// Note: `<KeyHandle as FromStr>::from_str` is not enough, as |
| 659 |
// we also want to bail if the fingerprint format is unknown. |
| 660 |
// That is, KeyHandle will happily parse a 24 character hex |
| 661 |
// string, but v4 fignerprints are 20 characters, and v6 |
| 662 |
// fingerprints are 32 characters. |
| 663 |
fn parse_as_key_handle(s: &str) -> Result<KeyHandle> { |
| 664 |
let kh = KeyHandle::from_str(s)?; |
| 665 |
if kh.is_invalid() { |
| 666 |
Err(anyhow::anyhow!( |
| 667 |
"{:?} is not a valid fingerprint or key ID \ |
| 668 |
(hint: v4 fingerprints are 20 hex characters, \ |
| 669 |
key IDs are 16 hex characters, you provided {} \ |
| 670 |
characters)", |
| 671 |
s, s.chars().count())) |
| 672 |
} else { |
| 673 |
Ok(kh) |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
fn parse_as_email(s: &str) -> Result<String> { |
| 678 |
let userid = UserID::from(format!("<{}>", s)); |
| 679 |
match userid.email_normalized() { |
| 680 |
Ok(Some(email)) => { |
| 681 |
Ok(email) |
| 682 |
} |
| 683 |
Ok(None) => { |
| 684 |
Err(anyhow::anyhow!( |
| 685 |
"{:?} is not a valid email address", s)) |
| 686 |
} |
| 687 |
Err(err) => { |
| 688 |
Err(err).context(format!( |
| 689 |
"{:?} is not a valid email address", s)) |
| 690 |
} |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
fn parse_as_domain(s: &str) -> Result<String> { |
| 695 |
let email = format!("localpart@{}", s); |
| 696 |
match parse_as_email(&email) { |
| 697 |
Ok(_) => Ok(s.to_string()), |
| 698 |
Err(err) => Err(err), |
| 699 |
} |
| 700 |
} |
| 701 |
|
| 702 |
let prefix = Prefix::prefix(); |
| 703 |
let mut full_argument_names = Vec::new(); |
| 704 |
let mut full_name = |name| { |
| 705 |
let name = if ! prefix.is_empty() && name == "cert" { |
| 706 |
// We want `--cert`, not `--cert-cert`, or |
| 707 |
// `--for` instead of `--for-cert`. |
| 708 |
prefix.strip_suffix("-").expect("prefix must end with -").into() |
| 709 |
} else { |
| 710 |
format!("{}{}", prefix, name) |
| 711 |
}; |
| 712 |
|
| 713 |
full_argument_names.push(name.clone()); |
| 714 |
name |
| 715 |
}; |
| 716 |
|
| 717 |
if cert_arg { |
| 718 |
let full_name = full_name("cert"); |
| 719 |
let help = "Use certificates with the specified \ |
| 720 |
fingerprint or key ID"; |
| 721 |
let mut arg = clap::Arg::new(&full_name) |
| 722 |
.long(&full_name) |
| 723 |
.value_name("FINGERPRINT|KEYID") |
| 724 |
.value_parser(parse_as_key_handle) |
| 725 |
.action(action.clone()) |
| 726 |
.help(Doc::help("cert", help)); |
| 727 |
if let Some(l) = Doc::long_help("cert", help) { |
| 728 |
arg = arg.long_help(l); |
| 729 |
} |
| 730 |
cmd = cmd.arg(arg); |
| 731 |
arg_group = arg_group.arg(full_name); |
| 732 |
} |
| 733 |
|
| 734 |
if special_arg { |
| 735 |
let full_name = full_name("special"); |
| 736 |
let help = "Use certificates identified by the special name"; |
| 737 |
let mut arg = clap::Arg::new(&full_name) |
| 738 |
.long(&full_name) |
| 739 |
.value_name("SPECIAL") |
| 740 |
.value_parser( |
| 741 |
clap::builder::EnumValueParser::<SpecialName>::new()) |
| 742 |
.action(action.clone()) |
| 743 |
.help(Doc::help("special", help)); |
| 744 |
if let Some(l) = Doc::long_help("special", help) { |
| 745 |
arg = arg.long_help(l); |
| 746 |
} |
| 747 |
cmd = cmd.arg(arg); |
| 748 |
arg_group = arg_group.arg(full_name); |
| 749 |
} |
| 750 |
|
| 751 |
if userid_arg { |
| 752 |
let full_name = full_name("userid"); |
| 753 |
let help = "Use certificates with the specified user ID"; |
| 754 |
let mut arg = clap::Arg::new(&full_name) |
| 755 |
.long(&full_name) |
| 756 |
.value_name("USERID") |
| 757 |
.action(action.clone()) |
| 758 |
.help(Doc::help("userid", help)); |
| 759 |
if let Some(l) = Doc::long_help("userid", help) { |
| 760 |
arg = arg.long_help(l); |
| 761 |
} |
| 762 |
cmd = cmd.arg(arg); |
| 763 |
arg_group = arg_group.arg(full_name); |
| 764 |
} |
| 765 |
|
| 766 |
if email_arg { |
| 767 |
let full_name = full_name("email"); |
| 768 |
let help = "Use certificates where a user ID includes \ |
| 769 |
the specified email address"; |
| 770 |
let mut arg = clap::Arg::new(&full_name) |
| 771 |
.long(&full_name) |
| 772 |
.value_name("EMAIL") |
| 773 |
.value_parser(parse_as_email) |
| 774 |
.action(action.clone()) |
| 775 |
.help(Doc::help("email", help)); |
| 776 |
if let Some(l) = Doc::long_help("email", help) { |
| 777 |
arg = arg.long_help(l); |
| 778 |
} |
| 779 |
cmd = cmd.arg(arg); |
| 780 |
arg_group = arg_group.arg(full_name); |
| 781 |
} |
| 782 |
|
| 783 |
if domain_arg { |
| 784 |
let full_name = full_name("domain"); |
| 785 |
let help = "Use certificates where a user ID includes \ |
| 786 |
an email address for the specified domain"; |
| 787 |
let mut arg = clap::Arg::new(&full_name) |
| 788 |
.long(&full_name) |
| 789 |
.value_name("DOMAIN") |
| 790 |
.value_parser(parse_as_domain) |
| 791 |
.action(action.clone()) |
| 792 |
.help(Doc::help("domain", help)); |
| 793 |
if let Some(l) = Doc::long_help("domain", help) { |
| 794 |
arg = arg.long_help(l); |
| 795 |
} |
| 796 |
cmd = cmd.arg(arg); |
| 797 |
arg_group = arg_group.arg(full_name); |
| 798 |
} |
| 799 |
|
| 800 |
if grep_arg { |
| 801 |
let full_name = full_name("grep"); |
| 802 |
let help = "Use certificates with a user ID that \ |
| 803 |
matches the pattern, case insensitively"; |
| 804 |
let mut arg = clap::Arg::new(&full_name) |
| 805 |
.long(&full_name) |
| 806 |
.value_name("PATTERN") |
| 807 |
.action(action.clone()) |
| 808 |
.help(Doc::help("grep", help)); |
| 809 |
if let Some(l) = Doc::long_help("grep", help) { |
| 810 |
arg = arg.long_help(l); |
| 811 |
} |
| 812 |
cmd = cmd.arg(arg); |
| 813 |
arg_group = arg_group.arg(full_name); |
| 814 |
} |
| 815 |
|
| 816 |
// Add all of the variants that are enabled. |
| 817 |
if file_arg { |
| 818 |
let full_name = full_name("file"); |
| 819 |
let help = "Read certificates from PATH"; |
| 820 |
let mut arg = clap::Arg::new(&full_name) |
| 821 |
.long(&full_name) |
| 822 |
.value_name("PATH") |
| 823 |
.value_parser(clap::value_parser!(PathBuf)) |
| 824 |
.action(action.clone()) |
| 825 |
.help(Doc::help("file", help)); |
| 826 |
|
| 827 |
if file_requires_output { |
| 828 |
arg = arg.requires("output"); |
| 829 |
} |
| 830 |
|
| 831 |
if let Some(l) = Doc::long_help("file", help) { |
| 832 |
arg = arg.long_help(l); |
| 833 |
} |
| 834 |
cmd = cmd.arg(arg); |
| 835 |
arg_group = arg_group.arg(full_name); |
| 836 |
} |
| 837 |
|
| 838 |
if self_arg { |
| 839 |
let full_name = full_name("self"); |
| 840 |
|
| 841 |
let (help, long_help) = match Prefix::name() { |
| 842 |
"for" => ( |
| 843 |
"Encrypt the message for yourself", |
| 844 |
format!( |
| 845 |
"Encrypt the message for yourself |
| 846 |
|
| 847 |
This adds the certificates listed in the configuration file under \ |
| 848 |
`{}` to the list of recipients. \ |
| 849 |
This can be used to make sure that you yourself can decrypt the message. |
| 850 |
|
| 851 |
{} |
| 852 |
", |
| 853 |
ENCRYPT_FOR_SELF, |
| 854 |
if let Some(certs) = config::get_augmentation(ENCRYPT_FOR_SELF) { |
| 855 |
format!("The following certs will be added: {}.", certs) |
| 856 |
} else { |
| 857 |
"Currently, the list of certificates to be added is empty." |
| 858 |
.into() |
| 859 |
}), |
| 860 |
), |
| 861 |
|
| 862 |
"signer" => ( |
| 863 |
"Sign using your default signer keys", |
| 864 |
format!( |
| 865 |
"Sign using your default signer keys |
| 866 |
|
| 867 |
This adds the certificates listed in the configuration file under \ |
| 868 |
`{}` to the list of signer keys. |
| 869 |
|
| 870 |
{} |
| 871 |
", |
| 872 |
SIGNER_SELF, |
| 873 |
if let Some(certs) = config::get_augmentation(SIGNER_SELF) { |
| 874 |
format!("The following keys will be added: {}.", certs) |
| 875 |
} else { |
| 876 |
"Currently, the list of keys to be added is empty." |
| 877 |
.into() |
| 878 |
}), |
| 879 |
), |
| 880 |
|
| 881 |
"certifier" => ( |
| 882 |
"Create the certification using your default certification \ |
| 883 |
key", |
| 884 |
format!( |
| 885 |
"Create the certification using your default certification key |
| 886 |
|
| 887 |
This uses the certificates set in the configuration file under \ |
| 888 |
`{}` as certification key. |
| 889 |
|
| 890 |
{} |
| 891 |
", |
| 892 |
CERTIFIER_SELF, |
| 893 |
if let Some(cert) |
| 894 |
= config::get_augmentation(CERTIFIER_SELF) |
| 895 |
{ |
| 896 |
format!("The following key will be used: {}.", |
| 897 |
cert) |
| 898 |
} else { |
| 899 |
"Currently, there is no default certification key." |
| 900 |
.into() |
| 901 |
}), |
| 902 |
), |
| 903 |
|
| 904 |
#[cfg(test)] |
| 905 |
"cert" => ( |
| 906 |
"dummy text for the test", |
| 907 |
"dummy text for the test".into(), |
| 908 |
), |
| 909 |
|
| 910 |
p => panic!("no help texts for --{}-self", p), |
| 911 |
}; |
| 912 |
|
| 913 |
cmd = cmd.arg( |
| 914 |
clap::Arg::new(&full_name) |
| 915 |
.long(&full_name) |
| 916 |
.action(clap::ArgAction::SetTrue) |
| 917 |
.help(help) |
| 918 |
.long_help(long_help)); |
| 919 |
arg_group = arg_group.arg(full_name); |
| 920 |
} |
| 921 |
|
| 922 |
if with_password_args { |
| 923 |
let full_name = "with-password"; |
| 924 |
let arg = clap::Arg::new(full_name) |
| 925 |
.long(full_name) |
| 926 |
.action(clap::ArgAction::Count) |
| 927 |
.help(Doc::help( |
| 928 |
"with-password-file", |
| 929 |
"Prompt to add a password to encrypt with")) |
| 930 |
.long_help("\ |
| 931 |
Prompt to add a password to encrypt with |
| 932 |
|
| 933 |
When using this option, the user is asked to provide a password, \ |
| 934 |
which is used to encrypt the message. \ |
| 935 |
This option can be provided more than once to provide more than \ |
| 936 |
one password. \ |
| 937 |
The encrypted data can afterwards be decrypted with either one of \ |
| 938 |
the recipient's keys, or one of the provided passwords."); |
| 939 |
|
| 940 |
cmd = cmd.arg(arg); |
| 941 |
arg_group = arg_group.arg(full_name); |
| 942 |
|
| 943 |
let full_name = "with-password-file"; |
| 944 |
let arg = clap::Arg::new(full_name) |
| 945 |
.long(full_name) |
| 946 |
.value_name("PATH") |
| 947 |
.value_parser(clap::value_parser!(PathBuf)) |
| 948 |
.action(action.clone()) |
| 949 |
.help(Doc::help( |
| 950 |
"with-password-file", |
| 951 |
"File containing password to encrypt the message")) |
| 952 |
.long_help("\ |
| 953 |
File containing password to encrypt the message |
| 954 |
|
| 955 |
Note that the entire key file will be used as the password including \ |
| 956 |
any surrounding whitespace like a trailing newline. |
| 957 |
|
| 958 |
This option can be provided more than once to provide more than \ |
| 959 |
one password. \ |
| 960 |
The encrypted data can afterwards be decrypted with either one of \ |
| 961 |
the recipient's keys, or one of the provided passwords."); |
| 962 |
|
| 963 |
cmd = cmd.arg(arg); |
| 964 |
arg_group = arg_group.arg(full_name); |
| 965 |
} |
| 966 |
|
| 967 |
if signer_or_without_signature { |
| 968 |
cmd = cmd.arg( |
| 969 |
clap::Arg::new("without-signature") |
| 970 |
.long("without-signature") |
| 971 |
.action(clap::ArgAction::SetTrue) |
| 972 |
.conflicts_with_all(&full_argument_names) |
| 973 |
.help("Do not sign the message")); |
| 974 |
arg_group = arg_group.arg("without-signature"); |
| 975 |
} |
| 976 |
|
| 977 |
cmd = cmd.group(arg_group); |
| 978 |
|
| 979 |
cmd |
| 980 |
} |
| 981 |
|
| 982 |
fn augment_args_for_update(cmd: clap::Command) -> clap::Command |
| 983 |
{ |
| 984 |
Self::augment_args(cmd) |
| 985 |
} |
| 986 |
} |
| 987 |
|
| 988 |
impl<Arguments, Prefix, Options, Doc> clap::FromArgMatches |
| 989 |
for CertDesignators<Arguments, Prefix, Options, Doc> |
| 990 |
where |
| 991 |
Arguments: typenum::Unsigned, |
| 992 |
Prefix: ArgumentPrefix, |
| 993 |
Options: typenum::Unsigned, |
| 994 |
Doc: AdditionalDocs, |
| 995 |
{ |
| 996 |
fn update_from_arg_matches(&mut self, matches: &clap::ArgMatches) |
| 997 |
-> Result<(), clap::Error> |
| 998 |
{ |
| 999 |
// eprintln!("matches: {:#?}", matches); |
| 1000 |
|
| 1001 |
let arguments = Arguments::to_usize(); |
| 1002 |
let file_arg = (arguments & FileArg::to_usize()) > 0; |
| 1003 |
let cert_arg = (arguments & CertArg::to_usize()) > 0; |
| 1004 |
let userid_arg = (arguments & UserIDArg::to_usize()) > 0; |
| 1005 |
let email_arg = (arguments & EmailArg::to_usize()) > 0; |
| 1006 |
let domain_arg = (arguments & DomainArg::to_usize()) > 0; |
| 1007 |
let grep_arg = (arguments & GrepArg::to_usize()) > 0; |
| 1008 |
let with_password_args = (arguments & WithPasswordArgs::to_usize()) > 0; |
| 1009 |
let special_arg = (arguments & SpecialArg::to_usize()) > 0; |
| 1010 |
let self_arg = (arguments & SelfArg::to_usize()) > 0; |
| 1011 |
|
| 1012 |
let mut designators = Vec::new(); |
| 1013 |
|
| 1014 |
let prefix = Prefix::prefix(); |
| 1015 |
|
| 1016 |
if let Some(Some(certs)) |
| 1017 |
= matches.try_get_many::<KeyHandle>( |
| 1018 |
if prefix.is_empty() { |
| 1019 |
"cert" |
| 1020 |
} else { |
| 1021 |
prefix.strip_suffix("-").expect("prefix must end with -") |
| 1022 |
}) |
| 1023 |
.ok().filter(|_| cert_arg) |
| 1024 |
{ |
| 1025 |
for cert in certs.cloned() { |
| 1026 |
designators.push(CertDesignator::Cert(cert)); |
| 1027 |
} |
| 1028 |
} |
| 1029 |
|
| 1030 |
if let Some(Some(userids)) |
| 1031 |
= matches.try_get_many::<String>(&format!("{}userid", prefix)) |
| 1032 |
.ok().filter(|_| userid_arg) |
| 1033 |
{ |
| 1034 |
for userid in userids.cloned() { |
| 1035 |
designators.push( |
| 1036 |
CertDesignator::UserID(userid)); |
| 1037 |
} |
| 1038 |
} |
| 1039 |
|
| 1040 |
if let Some(Some(emails)) |
| 1041 |
= matches.try_get_many::<String>(&format!("{}email", prefix)) |
| 1042 |
.ok().filter(|_| email_arg) |
| 1043 |
{ |
| 1044 |
for email in emails.cloned() { |
| 1045 |
designators.push(CertDesignator::Email(email)); |
| 1046 |
} |
| 1047 |
} |
| 1048 |
|
| 1049 |
if let Some(Some(domains)) |
| 1050 |
= matches.try_get_many::<String>(&format!("{}domain", prefix)) |
| 1051 |
.ok().filter(|_| domain_arg) |
| 1052 |
{ |
| 1053 |
for domain in domains.cloned() { |
| 1054 |
designators.push(CertDesignator::Domain(domain)); |
| 1055 |
} |
| 1056 |
} |
| 1057 |
|
| 1058 |
if let Some(Some(patterns)) |
| 1059 |
= matches.try_get_many::<String>(&format!("{}grep", prefix)) |
| 1060 |
.ok().filter(|_| grep_arg) |
| 1061 |
{ |
| 1062 |
for pattern in patterns.cloned() { |
| 1063 |
designators.push(CertDesignator::Grep(pattern)); |
| 1064 |
} |
| 1065 |
} |
| 1066 |
|
| 1067 |
if let Some(Some(paths)) |
| 1068 |
= matches.try_get_many::<PathBuf>(&format!("{}file", prefix)) |
| 1069 |
.ok().filter(|_| file_arg) |
| 1070 |
{ |
| 1071 |
for path in paths.cloned() { |
| 1072 |
if let Some("-") = path.to_str() { |
| 1073 |
designators.push(CertDesignator::Stdin); |
| 1074 |
} else { |
| 1075 |
designators.push(CertDesignator::File(path)); |
| 1076 |
} |
| 1077 |
} |
| 1078 |
} |
| 1079 |
|
| 1080 |
if with_password_args { |
| 1081 |
self.with_passwords = matches.get_count("with-password") as usize; |
| 1082 |
|
| 1083 |
if let Some(Some(paths)) |
| 1084 |
= matches.try_get_many::<PathBuf>("with-password-file").ok() |
| 1085 |
{ |
| 1086 |
self.with_password_files.extend(paths.cloned()); |
| 1087 |
} |
| 1088 |
} |
| 1089 |
|
| 1090 |
if let Some(Some(names)) |
| 1091 |
= matches.try_get_many::<SpecialName>(&format!("{}special", prefix)) |
| 1092 |
.ok().filter(|_| special_arg) |
| 1093 |
{ |
| 1094 |
for name in names.cloned() { |
| 1095 |
designators.push(CertDesignator::Special(name)); |
| 1096 |
} |
| 1097 |
} |
| 1098 |
|
| 1099 |
if self_arg && matches.get_flag(&format!("{}self", prefix)) { |
| 1100 |
designators.push(CertDesignator::Self_); |
| 1101 |
} |
| 1102 |
|
| 1103 |
// eprintln!("{:?}", designators); |
| 1104 |
|
| 1105 |
self.designators = designators; |
| 1106 |
Ok(()) |
| 1107 |
} |
| 1108 |
|
| 1109 |
fn from_arg_matches(matches: &clap::ArgMatches) |
| 1110 |
-> Result<Self, clap::Error> |
| 1111 |
{ |
| 1112 |
let mut designators = Self { |
| 1113 |
designators: Vec::new(), |
| 1114 |
with_passwords: 0, |
| 1115 |
with_password_files: Vec::new(), |
| 1116 |
arguments: std::marker::PhantomData, |
| 1117 |
}; |
| 1118 |
|
| 1119 |
// The way we use clap, this is never called. |
| 1120 |
designators.update_from_arg_matches(matches)?; |
| 1121 |
Ok(designators) |
| 1122 |
} |
| 1123 |
} |
| 1124 |
|
| 1125 |
#[cfg(test)] |
| 1126 |
mod test { |
| 1127 |
use super::*; |
| 1128 |
|
| 1129 |
// Check that flattening CertDesignators works as expected. |
| 1130 |
#[test] |
| 1131 |
fn cert_designators() { |
| 1132 |
use clap::Parser; |
| 1133 |
use clap::CommandFactory; |
| 1134 |
use clap::FromArgMatches; |
| 1135 |
|
| 1136 |
macro_rules! check { |
| 1137 |
($t:ty, |
| 1138 |
$cert:expr, $userid:expr, $email:expr, |
| 1139 |
$domain:expr, $grep:expr, $file:expr, |
| 1140 |
$self: expr, |
| 1141 |
$special:expr, |
| 1142 |
$with_password:expr) => |
| 1143 |
{{ |
| 1144 |
#[derive(Parser, Debug)] |
| 1145 |
#[clap(name = "prog")] |
| 1146 |
struct CLI { |
| 1147 |
#[command(flatten)] |
| 1148 |
pub certs: CertDesignators<$t>, |
| 1149 |
} |
| 1150 |
|
| 1151 |
let command = CLI::command(); |
| 1152 |
|
| 1153 |
// Check if --cert is recognized. |
| 1154 |
let m = command.clone().try_get_matches_from(vec![ |
| 1155 |
"prog", |
| 1156 |
"--cert", "C2B819056C652598", |
| 1157 |
"--cert", "C2B819056C652598", |
| 1158 |
]); |
| 1159 |
if $cert { |
| 1160 |
let m = m.expect("valid arguments"); |
| 1161 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1162 |
assert_eq!(c.certs.designators.len(), 2); |
| 1163 |
} else { |
| 1164 |
assert!(m.is_err()); |
| 1165 |
} |
| 1166 |
|
| 1167 |
// Either --cert is unknown, or the --cert's value |
| 1168 |
// is invalid. |
| 1169 |
let m = command.clone().try_get_matches_from(vec![ |
| 1170 |
"prog", |
| 1171 |
"--cert", "alice", |
| 1172 |
]); |
| 1173 |
assert!(m.is_err()); |
| 1174 |
|
| 1175 |
|
| 1176 |
// Check if --userid is recognized. |
| 1177 |
let m = command.clone().try_get_matches_from(vec![ |
| 1178 |
"prog", "--userid", "alice", "--userid", "bob", |
| 1179 |
]); |
| 1180 |
if $userid { |
| 1181 |
let m = m.expect("valid arguments"); |
| 1182 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1183 |
assert_eq!(c.certs.designators.len(), 2); |
| 1184 |
} else { |
| 1185 |
assert!(m.is_err()); |
| 1186 |
} |
| 1187 |
|
| 1188 |
|
| 1189 |
// Check if --email is recognized. |
| 1190 |
let m = command.clone().try_get_matches_from(vec![ |
| 1191 |
"prog", |
| 1192 |
"--email", "[email protected]", |
| 1193 |
"--email", "[email protected]", |
| 1194 |
]); |
| 1195 |
if $email { |
| 1196 |
let m = m.expect("valid arguments"); |
| 1197 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1198 |
assert_eq!(c.certs.designators.len(), 2); |
| 1199 |
} else { |
| 1200 |
assert!(m.is_err()); |
| 1201 |
} |
| 1202 |
|
| 1203 |
// Either --email is unknown, or the --email's value |
| 1204 |
// is invalid. |
| 1205 |
let m = command.clone().try_get_matches_from(vec![ |
| 1206 |
"prog", |
| 1207 |
"--email", "alice@[email protected]", |
| 1208 |
]); |
| 1209 |
assert!(m.is_err()); |
| 1210 |
|
| 1211 |
|
| 1212 |
// Check if --domain is recognized. |
| 1213 |
let m = command.clone().try_get_matches_from(vec![ |
| 1214 |
"prog", |
| 1215 |
"--domain", "example.org", |
| 1216 |
"--domain", "some.org", |
| 1217 |
]); |
| 1218 |
if $domain { |
| 1219 |
let m = m.expect("valid arguments"); |
| 1220 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1221 |
assert_eq!(c.certs.designators.len(), 2); |
| 1222 |
} else { |
| 1223 |
assert!(m.is_err()); |
| 1224 |
} |
| 1225 |
|
| 1226 |
// Either --domain is unknown, or the --domain's value |
| 1227 |
// is invalid. |
| 1228 |
let m = command.clone().try_get_matches_from(vec![ |
| 1229 |
"prog", |
| 1230 |
"--domain", "@example.org", |
| 1231 |
]); |
| 1232 |
assert!(m.is_err()); |
| 1233 |
|
| 1234 |
|
| 1235 |
// Check if --grep is recognized. |
| 1236 |
let m = command.clone().try_get_matches_from(vec![ |
| 1237 |
"prog", |
| 1238 |
"--grep", "a@b@c", |
| 1239 |
"--grep", "@some.org", |
| 1240 |
]); |
| 1241 |
if $grep { |
| 1242 |
let m = m.expect("valid arguments"); |
| 1243 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1244 |
assert_eq!(c.certs.designators.len(), 2); |
| 1245 |
} else { |
| 1246 |
assert!(m.is_err()); |
| 1247 |
} |
| 1248 |
|
| 1249 |
|
| 1250 |
// Check if --file is recognized. |
| 1251 |
let m = command.clone().try_get_matches_from(vec![ |
| 1252 |
"prog", |
| 1253 |
"--file", "filename", |
| 1254 |
"--file", "./foo/bar", |
| 1255 |
]); |
| 1256 |
if $file { |
| 1257 |
let m = m.expect("valid arguments"); |
| 1258 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1259 |
assert_eq!(c.certs.designators.len(), 2); |
| 1260 |
} else { |
| 1261 |
assert!(m.is_err()); |
| 1262 |
} |
| 1263 |
|
| 1264 |
|
| 1265 |
// Check that stdin is recognized. |
| 1266 |
let m = command.clone().try_get_matches_from(vec![ |
| 1267 |
"prog", |
| 1268 |
"--file", "-", |
| 1269 |
]); |
| 1270 |
if $file { |
| 1271 |
let m = m.expect("valid arguments"); |
| 1272 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1273 |
assert_eq!(c.certs.designators.len(), 1); |
| 1274 |
if let CertDesignator::Stdin |
| 1275 |
= c.certs.designators[0] |
| 1276 |
{ |
| 1277 |
} else { |
| 1278 |
panic!("Expected stdin, got {:?}", |
| 1279 |
c.certs.designators[0]); |
| 1280 |
} |
| 1281 |
} else { |
| 1282 |
assert!(m.is_err()); |
| 1283 |
} |
| 1284 |
|
| 1285 |
|
| 1286 |
// Check if --self is recognized. |
| 1287 |
let m = command.clone().try_get_matches_from(vec![ |
| 1288 |
"prog", |
| 1289 |
"--self", |
| 1290 |
]); |
| 1291 |
if $self { |
| 1292 |
let m = m.expect("valid arguments"); |
| 1293 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1294 |
assert_eq!(c.certs.designators.len(), 1); |
| 1295 |
} else { |
| 1296 |
assert!(m.is_err()); |
| 1297 |
} |
| 1298 |
|
| 1299 |
|
| 1300 |
// Check if --special is recognized. |
| 1301 |
let m = command.clone().try_get_matches_from(vec![ |
| 1302 |
"prog", |
| 1303 |
"--special", "keys.openpgp.org", |
| 1304 |
"--special", "keys.mailvelope.com", |
| 1305 |
]); |
| 1306 |
if $special { |
| 1307 |
let m = m.expect("valid arguments"); |
| 1308 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1309 |
assert_eq!(c.certs.designators.len(), 2); |
| 1310 |
} else { |
| 1311 |
assert!(m.is_err()); |
| 1312 |
} |
| 1313 |
|
| 1314 |
|
| 1315 |
// Check if --with-password is recognized. |
| 1316 |
let m = command.clone().try_get_matches_from(vec![ |
| 1317 |
"prog", |
| 1318 |
"--with-password", |
| 1319 |
"--with-password", |
| 1320 |
]); |
| 1321 |
if $with_password { |
| 1322 |
let m = m.expect("valid arguments"); |
| 1323 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1324 |
assert_eq!(c.certs.with_passwords(), 2); |
| 1325 |
} else { |
| 1326 |
assert!(m.is_err()); |
| 1327 |
} |
| 1328 |
|
| 1329 |
// Check if --with-password-file is recognized. |
| 1330 |
let m = command.clone().try_get_matches_from(vec![ |
| 1331 |
"prog", |
| 1332 |
"--with-password-file", "a", |
| 1333 |
"--with-password-file", "b", |
| 1334 |
]); |
| 1335 |
if $with_password { |
| 1336 |
let m = m.expect("valid arguments"); |
| 1337 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1338 |
assert_eq!(c.certs.with_password_files().len(), 2); |
| 1339 |
} else { |
| 1340 |
assert!(m.is_err()); |
| 1341 |
} |
| 1342 |
}} |
| 1343 |
} |
| 1344 |
|
| 1345 |
check!(CertUserIDEmailDomainGrepArgs, |
| 1346 |
true, true, true, true, true, false, false, false, false); |
| 1347 |
check!(CertUserIDEmailFileArgs, |
| 1348 |
true, true, true, false, false, true, false, false, false); |
| 1349 |
check!(CertUserIDEmailFileWithPasswordArgs, |
| 1350 |
true, true, true, false, false, true, false, false, true); |
| 1351 |
check!(CertUserIDEmailFileSelfWithPasswordArgs, |
| 1352 |
true, true, true, false, false, true, true, false, true); |
| 1353 |
check!(CertUserIDEmailFileSelfSpecialArgs, |
| 1354 |
true, true, true, false, false, true, true, true, false); |
| 1355 |
// No Args. |
| 1356 |
check!(typenum::U0,false, false, false, false, false, false, false, false, false); |
| 1357 |
check!(CertArg, true, false, false, false, false, false, false, false, false); |
| 1358 |
check!(UserIDArg, false, true, false, false, false, false, false, false, false); |
| 1359 |
check!(EmailArg, false, false, true, false, false, false, false, false, false); |
| 1360 |
check!(DomainArg, false, false, false, true, false, false, false, false, false); |
| 1361 |
check!(GrepArg, false, false, false, false, true, false, false, false, false); |
| 1362 |
check!(FileArg, false, false, false, false, false, true, false, false, false); |
| 1363 |
check!(SelfArg, false, false, false, false, false, false, true, false, false); |
| 1364 |
check!(SpecialArg, false, false, false, false, false, false, false, true, false); |
| 1365 |
check!(WithPasswordArgs, |
| 1366 |
false, false, false, false, false, false, false, false, true); |
| 1367 |
} |
| 1368 |
|
| 1369 |
#[test] |
| 1370 |
fn cert_designators_one() { |
| 1371 |
use clap::Parser; |
| 1372 |
use clap::CommandFactory; |
| 1373 |
use clap::FromArgMatches; |
| 1374 |
|
| 1375 |
#[derive(Parser, Debug)] |
| 1376 |
#[clap(name = "prog")] |
| 1377 |
struct CLI { |
| 1378 |
#[command(flatten)] |
| 1379 |
pub certs: CertDesignators<CertUserIDEmailFileArgs, |
| 1380 |
NoPrefix, |
| 1381 |
OneValue>, |
| 1382 |
} |
| 1383 |
|
| 1384 |
let command = CLI::command(); |
| 1385 |
|
| 1386 |
// Check if --cert is recognized. |
| 1387 |
let m = command.clone().try_get_matches_from(vec![ |
| 1388 |
"prog", |
| 1389 |
"--cert", "C2B819056C652598", |
| 1390 |
]); |
| 1391 |
let m = m.expect("valid arguments"); |
| 1392 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1393 |
assert_eq!(c.certs.designators.len(), 1); |
| 1394 |
|
| 1395 |
// Make sure that we can't give it twice. |
| 1396 |
let m = command.clone().try_get_matches_from(vec![ |
| 1397 |
"prog", |
| 1398 |
"--cert", "C2B819056C652598", |
| 1399 |
"--cert", "C2B819056C652598", |
| 1400 |
]); |
| 1401 |
assert!(m.is_err()); |
| 1402 |
|
| 1403 |
// Make sure that we can't give it zero times. |
| 1404 |
let m = command.clone().try_get_matches_from(vec![ |
| 1405 |
"prog", |
| 1406 |
]); |
| 1407 |
assert!(m.is_err()); |
| 1408 |
|
| 1409 |
// Mixing is also not allowed. |
| 1410 |
let m = command.clone().try_get_matches_from(vec![ |
| 1411 |
"prog", |
| 1412 |
"--cert", "C2B819056C652598", |
| 1413 |
"--email", "[email protected]", |
| 1414 |
]); |
| 1415 |
assert!(m.is_err()); |
| 1416 |
} |
| 1417 |
|
| 1418 |
#[test] |
| 1419 |
fn cert_designators_optional() { |
| 1420 |
use clap::Parser; |
| 1421 |
use clap::CommandFactory; |
| 1422 |
use clap::FromArgMatches; |
| 1423 |
|
| 1424 |
#[derive(Parser, Debug)] |
| 1425 |
#[clap(name = "prog")] |
| 1426 |
struct CLI { |
| 1427 |
#[command(flatten)] |
| 1428 |
pub certs: CertDesignators<CertUserIDEmailFileArgs, |
| 1429 |
NoPrefix, |
| 1430 |
OptionalValue>, |
| 1431 |
} |
| 1432 |
|
| 1433 |
let command = CLI::command(); |
| 1434 |
|
| 1435 |
// Check if --cert is recognized. |
| 1436 |
let m = command.clone().try_get_matches_from(vec![ |
| 1437 |
"prog", |
| 1438 |
"--cert", "C2B819056C652598", |
| 1439 |
]); |
| 1440 |
let m = m.expect("valid arguments"); |
| 1441 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1442 |
assert_eq!(c.certs.designators.len(), 1); |
| 1443 |
|
| 1444 |
// Make sure that we can give it twice. |
| 1445 |
let m = command.clone().try_get_matches_from(vec![ |
| 1446 |
"prog", |
| 1447 |
"--cert", "C2B819056C652598", |
| 1448 |
"--cert", "C2B819056C652598", |
| 1449 |
]); |
| 1450 |
let m = m.expect("valid arguments"); |
| 1451 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1452 |
assert_eq!(c.certs.designators.len(), 2); |
| 1453 |
|
| 1454 |
// Make sure that we can give it zero times. |
| 1455 |
let m = command.clone().try_get_matches_from(vec![ |
| 1456 |
"prog", |
| 1457 |
]); |
| 1458 |
let m = m.expect("valid arguments"); |
| 1459 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1460 |
assert_eq!(c.certs.designators.len(), 0); |
| 1461 |
|
| 1462 |
// Make sure mixing is allowed. |
| 1463 |
let m = command.clone().try_get_matches_from(vec![ |
| 1464 |
"prog", |
| 1465 |
"--cert", "C2B819056C652598", |
| 1466 |
"--email", "[email protected]", |
| 1467 |
]); |
| 1468 |
let m = m.expect("valid arguments"); |
| 1469 |
let c = CLI::from_arg_matches(&m).expect("ok"); |
| 1470 |
assert_eq!(c.certs.designators.len(), 2); |
| 1471 |
} |
| 1472 |
} |