| 1 |
use std::collections::BTreeSet; |
| 2 |
use std::collections::HashSet; |
| 3 |
|
| 4 |
use typenum::Unsigned; |
| 5 |
|
| 6 |
use sequoia_openpgp as openpgp; |
| 7 |
use openpgp::cert::amalgamation::ValidAmalgamation; |
| 8 |
use openpgp::cert::ValidCert; |
| 9 |
use openpgp::packet::UserID; |
| 10 |
use openpgp::types::RevocationStatus; |
| 11 |
use openpgp::cert::amalgamation::ValidateAmalgamation; |
| 12 |
|
| 13 |
use crate::Result; |
| 14 |
use crate::cli::types::UserIDDesignators; |
| 15 |
use crate::cli::types::userid_designator::AddArgs; |
| 16 |
use crate::cli::types::userid_designator::PlainIsAdd; |
| 17 |
use crate::cli::types::userid_designator::ResolvedUserID; |
| 18 |
use crate::cli::types::userid_designator::UserIDDesignator; |
| 19 |
use crate::cli::types::userid_designator::UserIDDesignatorSemantics; |
| 20 |
use crate::common::ui; |
| 21 |
use crate::common::userid::lint_email; |
| 22 |
use crate::common::userid::lint_name; |
| 23 |
use crate::common::userid::lint_userid; |
| 24 |
use crate::sq::NULL_POLICY; |
| 25 |
|
| 26 |
const TRACE: bool = false; |
| 27 |
|
| 28 |
impl<Arguments, Options, Documentation> |
| 29 |
UserIDDesignators<Arguments, Options, Documentation> |
| 30 |
where |
| 31 |
Arguments: typenum::Unsigned, |
| 32 |
{ |
| 33 |
/// Resolve the user ID designators. |
| 34 |
/// |
| 35 |
/// We first match on self-signed user IDs. We return an error if |
| 36 |
/// a match is ambiguous (e.g., when matching on an email address, |
| 37 |
/// and there are multiple user IDs with the specified email |
| 38 |
/// address). |
| 39 |
/// |
| 40 |
/// If there are no matches, and we don't require a match (e.g., |
| 41 |
/// UserIDArg, EmailArg, NameArg), then we create a new user ID. |
| 42 |
/// |
| 43 |
/// This will match on revoked user IDs. |
| 44 |
/// |
| 45 |
/// When `--all` is provided, returns all non-revoked, self-signed |
| 46 |
/// user IDs. If there are none, an error is returned. |
| 47 |
/// |
| 48 |
/// The returned `ResolvedUserID` are deduped by user ID. That |
| 49 |
/// is, if multiple designators resolve to the same user ID, only |
| 50 |
/// one is kept. |
| 51 |
pub fn resolve(&self, vc: &ValidCert) -> Result<Vec<ResolvedUserID>> { |
| 52 |
tracer!(TRACE, "UserIDDesignators::resolve"); |
| 53 |
t!("{:?}", self.designators); |
| 54 |
|
| 55 |
let arguments = Arguments::to_usize(); |
| 56 |
let add_args = (arguments & AddArgs::to_usize()) > 0; |
| 57 |
let plain_is_add = (arguments & PlainIsAdd::to_usize()) > 0; |
| 58 |
|
| 59 |
// Find the matching User IDs. |
| 60 |
let mut userids = Vec::new(); |
| 61 |
|
| 62 |
// Don't stop at the first error. |
| 63 |
let mut missing = false; |
| 64 |
let mut ambiguous_email = false; |
| 65 |
let mut ambiguous_name = false; |
| 66 |
let mut bad = None; |
| 67 |
|
| 68 |
if let Some(true) = self.all() { |
| 69 |
let mut revoked_userids = BTreeSet::new(); |
| 70 |
let valid_userids = vc.userids() |
| 71 |
.filter_map(|ua| { |
| 72 |
if let RevocationStatus::Revoked(_) = ua.revocation_status() { |
| 73 |
revoked_userids.insert(ua.userid()); |
| 74 |
None |
| 75 |
} else { |
| 76 |
Some(ua.userid().clone()) |
| 77 |
} |
| 78 |
}) |
| 79 |
.collect::<BTreeSet<_>>(); |
| 80 |
|
| 81 |
let non_self_signed_userids = vc.cert().userids() |
| 82 |
.filter(|_| self.all_matches_non_self_signed()) |
| 83 |
.filter(|u| ! revoked_userids.contains(u.userid())) |
| 84 |
.filter(|u| ! valid_userids.contains(u.userid())) |
| 85 |
.map(|u| u.userid().clone()) |
| 86 |
.collect::<Vec<_>>(); |
| 87 |
|
| 88 |
let all_userids = valid_userids.into_iter() |
| 89 |
.chain(non_self_signed_userids.into_iter()) |
| 90 |
.map(|userid| ResolvedUserID::implicit(userid)) |
| 91 |
.collect::<Vec<_>>(); |
| 92 |
|
| 93 |
if all_userids.is_empty() { |
| 94 |
return Err(anyhow::anyhow!( |
| 95 |
"{} has no {}user IDs", |
| 96 |
vc.cert().fingerprint(), |
| 97 |
if self.all_matches_non_self_signed() { |
| 98 |
"" |
| 99 |
} else { |
| 100 |
"valid self-signed " |
| 101 |
})); |
| 102 |
} |
| 103 |
|
| 104 |
userids.extend(all_userids); |
| 105 |
} |
| 106 |
|
| 107 |
for designator in self.iter() { |
| 108 |
use UserIDDesignatorSemantics::*; |
| 109 |
match designator { |
| 110 |
UserIDDesignator::UserID(semantics, userid) => { |
| 111 |
let userid = UserID::from(&userid[..]); |
| 112 |
|
| 113 |
if let Some(_) = vc.userids() |
| 114 |
.find(|ua| { |
| 115 |
ua.userid() == &userid |
| 116 |
}) |
| 117 |
{ |
| 118 |
userids.push(designator.resolve_to(userid.clone())); |
| 119 |
} else if semantics == &Add { |
| 120 |
if ! self.allow_non_canonical_userids { |
| 121 |
// We're going to add a user ID. Lint it |
| 122 |
// first. |
| 123 |
lint_userid(&userid)?; |
| 124 |
} |
| 125 |
userids.push(designator.resolve_to(userid)); |
| 126 |
} else { |
| 127 |
weprintln!("{} is not a self-signed user ID.", |
| 128 |
ui::Safe(&userid)); |
| 129 |
missing = true; |
| 130 |
} |
| 131 |
} |
| 132 |
UserIDDesignator::Email(semantics, email) => { |
| 133 |
// Validate the email address. |
| 134 |
let email_userid = match UserID::from_address(None, None, email) { |
| 135 |
Ok(userid) => userid, |
| 136 |
Err(err) => { |
| 137 |
weprintln!("{:?} is not a valid email address: {}", |
| 138 |
email, err); |
| 139 |
bad = Some(err); |
| 140 |
continue; |
| 141 |
} |
| 142 |
}; |
| 143 |
|
| 144 |
// Extract a normalized version for comparison |
| 145 |
// purposes. |
| 146 |
let email_normalized = match email_userid.email_normalized() { |
| 147 |
Ok(Some(email)) => email, |
| 148 |
Ok(None) => { |
| 149 |
weprintln!("{:?} is not a valid email address", email); |
| 150 |
bad = Some(anyhow::anyhow!(format!( |
| 151 |
"{:?} is not a valid email address", email))); |
| 152 |
continue; |
| 153 |
} |
| 154 |
Err(err) => { |
| 155 |
weprintln!("{:?} is not a valid email address: {}", |
| 156 |
email, err); |
| 157 |
bad = Some(err); |
| 158 |
continue; |
| 159 |
} |
| 160 |
}; |
| 161 |
|
| 162 |
// Find any matching self-signed user IDs. |
| 163 |
let mut found = false; |
| 164 |
for ua in vc.userids() { |
| 165 |
if Some(&email_normalized) |
| 166 |
== ua.userid().email_normalized().unwrap_or(None).as_ref() |
| 167 |
{ |
| 168 |
if found { |
| 169 |
weprintln!("{} is ambiguous: it matches \ |
| 170 |
multiple self-signed user IDs.", |
| 171 |
email); |
| 172 |
ambiguous_email = true; |
| 173 |
} |
| 174 |
|
| 175 |
found = true; |
| 176 |
|
| 177 |
if semantics == &By { |
| 178 |
userids.push(designator.clone() |
| 179 |
.resolve_to(ua.userid().clone())); |
| 180 |
} else { |
| 181 |
userids.push(designator.clone() |
| 182 |
.resolve_to(email_userid.clone())); |
| 183 |
// Since we're not returning the |
| 184 |
// matching self-signed user ID, we |
| 185 |
// don't need to worry about ambiguous |
| 186 |
// matches. |
| 187 |
break; |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
if ! found { |
| 193 |
match semantics { |
| 194 |
Exact | By => { |
| 195 |
eprintln!("None of the self-signed user IDs \ |
| 196 |
are for the email address {:?}.", |
| 197 |
email); |
| 198 |
missing = true; |
| 199 |
} |
| 200 |
Add => { |
| 201 |
if ! self.allow_non_canonical_userids { |
| 202 |
// We're going to add a user ID. Lint it |
| 203 |
// first. |
| 204 |
lint_email(email)?; |
| 205 |
} |
| 206 |
userids.push( |
| 207 |
designator.clone().resolve_to(email_userid)); |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
UserIDDesignator::Name(semantics, name) => { |
| 213 |
let name_userid = UserID::from(&name[..]); |
| 214 |
if name_userid.name().ok() != Some(Some(&name[..])) { |
| 215 |
let err = format!("{:?} is not a valid display name", |
| 216 |
name); |
| 217 |
weprintln!("{}", err); |
| 218 |
bad = Some(anyhow::anyhow!(err)); |
| 219 |
continue; |
| 220 |
}; |
| 221 |
|
| 222 |
let mut found = false; |
| 223 |
for ua in vc.userids() { |
| 224 |
if let Ok(Some(n)) = ua.userid().name() { |
| 225 |
if n == name { |
| 226 |
if found { |
| 227 |
weprintln!("{:?} is ambiguous: it matches \ |
| 228 |
multiple self-signed user IDs.", |
| 229 |
name); |
| 230 |
ambiguous_name = true; |
| 231 |
} |
| 232 |
|
| 233 |
found = true; |
| 234 |
|
| 235 |
if semantics == &By { |
| 236 |
userids.push(designator.clone() |
| 237 |
.resolve_to(ua.userid().clone())); |
| 238 |
} else { |
| 239 |
userids.push(designator.clone() |
| 240 |
.resolve_to(name_userid.clone())); |
| 241 |
// Since we're not returning the |
| 242 |
// matching self-signed user ID, we |
| 243 |
// don't need to worry about ambiguous |
| 244 |
// matches. |
| 245 |
break; |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
if ! found { |
| 252 |
match semantics { |
| 253 |
Exact | By => { |
| 254 |
eprintln!("None of the self-signed user IDs \ |
| 255 |
are for the display name {:?}.", |
| 256 |
name); |
| 257 |
missing = true; |
| 258 |
} |
| 259 |
Add => { |
| 260 |
if ! self.allow_non_canonical_userids { |
| 261 |
// We're going to add a user ID. Lint it |
| 262 |
// first. |
| 263 |
lint_name(&name[..])?; |
| 264 |
} |
| 265 |
userids.push(designator.resolve_to( |
| 266 |
name_userid)); |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
if missing || ambiguous_email || ambiguous_name { |
| 275 |
weprintln!("{}'s self-signed user IDs:", vc.cert().fingerprint()); |
| 276 |
let mut have_valid = false; |
| 277 |
for ua in vc.userids() { |
| 278 |
if std::str::from_utf8(ua.userid().value()).is_ok() { |
| 279 |
have_valid = true; |
| 280 |
weprintln!(initial_indent=" - ", |
| 281 |
subsequent_indent=" ", |
| 282 |
"{}", ui::Safe(ua.userid())); |
| 283 |
} |
| 284 |
} |
| 285 |
if ! have_valid { |
| 286 |
weprintln!(" - Certificate has no valid self-signed user IDs."); |
| 287 |
} |
| 288 |
|
| 289 |
if let Ok(null) = vc.clone().with_policy(&NULL_POLICY, vc.time()) { |
| 290 |
if vc.userids().count() < null.userids().count() { |
| 291 |
weprintln!("Invalid self-signed user IDs:"); |
| 292 |
let valid: BTreeSet<_> |
| 293 |
= vc.userids().map(|ua| ua.userid().clone()).collect(); |
| 294 |
for ua in null.userids() { |
| 295 |
if valid.contains(ua.userid()) { |
| 296 |
continue; |
| 297 |
} |
| 298 |
|
| 299 |
if std::str::from_utf8(ua.userid().value()).is_ok() { |
| 300 |
let userid = ui::Safe(ua.userid()); |
| 301 |
if let Err(err) = ua.with_policy(vc.policy(), vc.time()) { |
| 302 |
weprintln!(initial_indent=" - ", |
| 303 |
subsequent_indent=" ", |
| 304 |
"{}: {}", |
| 305 |
userid, err); |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
if missing { |
| 314 |
if add_args && ! plain_is_add { |
| 315 |
weprintln!("Use `--add-userid` or `--add-email` to use \ |
| 316 |
a user ID even if it isn't self signed, or has \ |
| 317 |
an invalid self signature."); |
| 318 |
} |
| 319 |
return Err(anyhow::anyhow!("No matching self-signed user ID")); |
| 320 |
} |
| 321 |
if ambiguous_email { |
| 322 |
if add_args && ! plain_is_add { |
| 323 |
weprintln!("Use `--userid` with the full user ID, or \ |
| 324 |
`--add-userid` to add a new user ID."); |
| 325 |
} else { |
| 326 |
weprintln!("Use `--userid` with the full user ID."); |
| 327 |
} |
| 328 |
return Err(anyhow::anyhow!("\ |
| 329 |
An email address does not unambiguously designate a \ |
| 330 |
self-signed user ID")); |
| 331 |
} |
| 332 |
if ambiguous_name { |
| 333 |
if add_args && ! plain_is_add { |
| 334 |
weprintln!("Use `--userid` with the full user ID, or \ |
| 335 |
`--add-userid` to add a new user ID."); |
| 336 |
} else { |
| 337 |
weprintln!("Use `--userid` with the full user ID."); |
| 338 |
} |
| 339 |
return Err(anyhow::anyhow!("\ |
| 340 |
A name does not unambiguously designate a \ |
| 341 |
self-signed user ID")); |
| 342 |
} |
| 343 |
|
| 344 |
if let Some(err) = bad { |
| 345 |
return Err(err); |
| 346 |
} |
| 347 |
|
| 348 |
// Dedup while preserving order. |
| 349 |
let mut seen = HashSet::new(); |
| 350 |
userids.retain(|userid| seen.insert(userid.userid().clone())); |
| 351 |
|
| 352 |
t!(" => {:?}", |
| 353 |
userids.iter() |
| 354 |
.map(|u| { |
| 355 |
ui::Safe(u.userid()).to_string() |
| 356 |
}) |
| 357 |
.collect::<Vec<String>>() |
| 358 |
.join(", ")); |
| 359 |
Ok(userids) |
| 360 |
} |
| 361 |
} |