Analyze the state of /etc/passwd vs systemd-sysusers.
(rootfs: &Dir)
| 285 | |
| 286 | /// Analyze the state of /etc/passwd vs systemd-sysusers. |
| 287 | pub fn analyze(rootfs: &Dir) -> Result<SysusersAnalysis> { |
| 288 | struct SysuserData { |
| 289 | #[allow(dead_code)] |
| 290 | uid: Option<IdSource>, |
| 291 | #[allow(dead_code)] |
| 292 | pgid: Option<GroupReference>, |
| 293 | } |
| 294 | |
| 295 | struct SysgroupData { |
| 296 | #[allow(dead_code)] |
| 297 | id: Option<IdSource>, |
| 298 | } |
| 299 | |
| 300 | let Some(passwd) = nameservice::passwd::load_etc_passwd(rootfs) |
| 301 | .map_err(|e| Error::PasswdLoadFailure(e.to_string()))? |
| 302 | else { |
| 303 | // If there's no /etc/passwd then we're done |
| 304 | return Ok(SysusersAnalysis::default()); |
| 305 | }; |
| 306 | |
| 307 | let mut passwd = passwd |
| 308 | .into_iter() |
| 309 | .map(|mut e| { |
| 310 | // Make the name be the map key, leaving the old value a stub |
| 311 | let mut name = String::new(); |
| 312 | std::mem::swap(&mut e.name, &mut name); |
| 313 | (name, e) |
| 314 | }) |
| 315 | .collect::<BTreeMap<_, _>>(); |
| 316 | let mut group = nameservice::group::load_etc_group(rootfs) |
| 317 | .map_err(|e| Error::GroupLoadFailure(e.to_string()))? |
| 318 | .into_iter() |
| 319 | .map(|mut e| { |
| 320 | // Make the name be the map key, leaving the old value a stub |
| 321 | let mut name = String::new(); |
| 322 | std::mem::swap(&mut e.name, &mut name); |
| 323 | (name, e) |
| 324 | }) |
| 325 | .collect::<BTreeMap<_, _>>(); |
| 326 | |
| 327 | let (sysusers_users, sysusers_groups) = { |
| 328 | let mut users = BTreeMap::new(); |
| 329 | let mut groups = BTreeMap::new(); |
| 330 | for ent in read_sysusers(rootfs)? { |
| 331 | match ent { |
| 332 | SysusersEntry::User { |
| 333 | name, uid, pgid, .. |
| 334 | } => { |
| 335 | users.insert(name, SysuserData { uid, pgid }); |
| 336 | } |
| 337 | SysusersEntry::Group { name, id } => { |
| 338 | groups.insert(name, SysgroupData { id }); |
| 339 | } |
| 340 | SysusersEntry::Range { .. } => { |
| 341 | // Nothing to do here |
| 342 | } |
| 343 | } |
| 344 | } |