module main import nspkg import crypto.sha256 import os #flag -larchive #include #include // libalpm reads a package's mtree for us; these are the libarchive accessors // pacman's -Qkk reads each entry with fn C.archive_entry_pathname(entry &C.Archive_entry) &char fn C.archive_entry_filetype(entry &C.Archive_entry) u32 fn C.archive_entry_mode(entry &C.Archive_entry) u32 fn C.archive_entry_uid(entry &C.Archive_entry) i64 fn C.archive_entry_gid(entry &C.Archive_entry) i64 fn C.archive_entry_mtime(entry &C.Archive_entry) i64 fn C.archive_entry_size(entry &C.Archive_entry) i64 fn C.archive_entry_symlink(entry &C.Archive_entry) &char fn C.archive_entry_digest(entry &C.Archive_entry, kind int) &u8 const ae_ifmt = u32(0o170000) const ae_ifreg = u32(0o100000) const ae_ifdir = u32(0o040000) const ae_iflnk = u32(0o120000) // the mode bits pacman compares: permissions, setuid, setgid and sticky const mode_bits = u32(0o7777) // MtreeEntry is what a package's mtree records of one of its files struct MtreeEntry { path string // as in the mtree, './' stripped kind u32 // ae_ifreg, ae_ifdir or ae_iflnk mode u32 uid i64 gid i64 mtime i64 size i64 symlink string sha256 string // hex, empty when the mtree has none } // mtree_entries reads a package's mtree. an error is a broken one: libalpm's // mtree reader hands an entry over at 0, answers 1 at the mtree's end and // anything else for entries beyond reading, so the loop tells the end and a // read failure apart — pacman's own loop stops at anything that is not 0, // which reports a partial mtree as a complete one. the second value is // whether the package carries an mtree file at all (packages built before // mtrees existed do not; that is not an error) fn mtree_entries(pkg &nspkg.Package) !([]MtreeEntry, bool) { archive := pkg.mtree_open() if archive == unsafe { nil } { // libalpm answers nil both for no mtree file and for one libarchive // cannot open; it tells them apart by the file being there, and so // does this: a corrupt mtree is the package's problem, not a package // built before mtrees (pacman's check.c leaves this as a TODO) handle := pkg.get_handle() // the reason is read first: libalpm's package getters clear it reason := handle.error().msg() mtfile := os.join_path(handle.option_get_dbpath(), 'local', '${pkg.get_name()}-${pkg.get_version()}', 'mtree') if os.exists(mtfile) { return error('the mtree file cannot be read (${reason}): ${mtfile}') } return []MtreeEntry{}, false } mut out := []MtreeEntry{} mut entry := &C.Archive_entry(unsafe { nil }) for { more := pkg.mtree_step(archive, &entry) or { read_err := error('reading the mtree failed: ${err.msg()}') pkg.mtree_close(archive) or { return error('${read_err.msg()} (closing it failed too: ${err.msg()})') } return read_err } if !more { break } mut path := unsafe { cstring_to_vstring(C.archive_entry_pathname(entry)) } if path.starts_with('./') { path = path[2..] } link := C.archive_entry_symlink(entry) out << MtreeEntry{ path: path kind: C.archive_entry_filetype(entry) mode: C.archive_entry_mode(entry) & mode_bits uid: C.archive_entry_uid(entry) gid: C.archive_entry_gid(entry) mtime: C.archive_entry_mtime(entry) size: C.archive_entry_size(entry) symlink: if link == unsafe { nil } { '' } else { unsafe { cstring_to_vstring(link) } } sha256: digest_hex(C.archive_entry_digest(entry, C.ARCHIVE_ENTRY_DIGEST_SHA256)) } } pkg.mtree_close(archive) or { // the close is the read's last word; the entries fail with it return error('opening the mtree of ${pkg.get_name()} failed to close it: ${err.msg()}') } return out, true } // digest_hex is a 32 byte sha256 digest as hex, empty when it is all zero: // libarchive leaves it so when the mtree records none fn digest_hex(d &u8) string { if d == unsafe { nil } { return '' } bytes := unsafe { d.vbytes(32) }.clone() return if bytes.all(it == 0) { '' } else { bytes.hex() } } // file_sha256 hashes a file in chunks, so a large one is never read whole. // the read names end of input as its own error (os.Eof); a failure to read // anything else is the caller's news — a digest of the part that came out // would pass itself off as the whole file's fn file_sha256(path string) !string { mut f := os.open(path)! defer { f.close() } mut d := sha256.new() mut buf := []u8{len: 65536} for { n := f.read(mut buf) or { // end of input is a chunking loop's end, not a failure if err is os.Eof { break } return err } if n == 0 { break } d.write(buf[..n])! } return d.sum([]).hex() } // check_file_full compares one file on disk with its mtree entry, the way // pacman's -Qkk does: the type first, then owner, group and mode, the target // of a link, and for anything that is not a directory the mtime, and for a // regular file the size and the sha256. a backup file is expected to change // in time, size and content, so those only make a note for it fn check_file_full(path string, e MtreeEntry, backup bool) []VerifyLine { st := os.lstat(path) or { // a stat denied by permissions is not a missing file, as in the // fast check. the errno comes along on the error, so the reading // does not lean on strerror's wording if stat_denied(err) { return [VerifyLine{false, '${path} (${err.msg()})'}] } return [VerifyLine{true, '${path} (missing)'}] } kind := st.mode & ae_ifmt if kind != e.kind { return [VerifyLine{true, '${path} (file type mismatch)'}] } mut out := []VerifyLine{} if i64(st.uid) != e.uid { out << VerifyLine{true, '${path} (UID mismatch)'} } if i64(st.gid) != e.gid { out << VerifyLine{true, '${path} (GID mismatch)'} } if st.mode & mode_bits != e.mode { out << VerifyLine{true, '${path} (permissions mismatch)'} } if kind == ae_iflnk { // a link that cannot be read is its own finding, as pacman's // check_symlink says it, not a target that differs if target := os.readlink(path) { if target != e.symlink { out << VerifyLine{true, '${path} (symlink path mismatch)'} } } else { out << VerifyLine{true, '${path} (unable to read symlink contents: ${err.msg()})'} } } // a backup file's changes are the user's: they are noted, not counted note := fn [backup, path] (what string) VerifyLine { return if backup { VerifyLine{false, 'backup file ${path} (${what})'} } else { VerifyLine{true, '${path} (${what})'} } } if kind != ae_ifdir && st.mtime != e.mtime { out << note('modification time mismatch') } if kind == ae_ifreg { if i64(st.size) != e.size { out << note('size mismatch') } if e.sha256 == '' { out << VerifyLine{true, '${path} (SHA256 checksum information not available)'} } else if sum := file_sha256(path) { if sum != e.sha256 { out << note('SHA256 checksum mismatch') } } else { // unverified counts as altered, as pacman's "failed to calculate" // does: run as root to read the root-only files out << VerifyLine{true, '${path} (cannot read: ${err.msg()})'} } } return out } // verify_pkg_full is pacman's -Qkk for one package: every entry of its // mtree against the disk. the .INSTALL and .CHANGELOG entries are the db's // own copies; the other dot entries describe the package, not its files. // the report is held and handed back with the number of altered files: the // caller prints it after its spinner stops, since an inline println would // draw under the running frame fn verify_pkg_full(handle &nspkg.Handle, pkg &nspkg.Package) (int, []VerifyLine) { name := pkg.get_name() mut lines := []VerifyLine{} // a broken mtree is that package's verdict; the check goes on entries, has_mtree := mtree_entries(pkg) or { lines << VerifyLine{true, '${name}: ${err.msg()}'} return 1, lines } if !has_mtree { lines << VerifyLine{false, '${name}: no mtree file'} return 0, lines } backups := backups_of(pkg).map(it.get_name()) mut altered := 0 for e in entries { path := if e.path.starts_with('.') { dbfile := match e.path { '.INSTALL' { 'install' } '.CHANGELOG' { 'changelog' } else { '' } } if dbfile == '' { continue } // the db the mtree was read from: the handle's, not the global os.join_path(handle.option_get_dbpath(), 'local', '${name}-${pkg.get_version()}', dbfile) } else { '${root_path}${e.path}' } if e.kind !in [ae_ifreg, ae_ifdir, ae_iflnk] { lines << VerifyLine{false, '${name}: warning: ${path} (file type not recognized)'} continue } // a file NoExtract kept off the disk is not missing if !os.exists(path) && !os.is_link(path) && handle.option_match_no_extract(e.path) { continue } findings := check_file_full(path, e, e.path in backups) for f in findings { lines << VerifyLine{f.problem, if f.problem { '${name}: ${f.text}' } else { '${name}: note: ${f.text}' }} } if findings.any(it.problem) { altered++ } } return altered, lines }