"""The packages dpkg has on a Debian-family host: the query and its parse.""" from __future__ import annotations from napalm_device_types.models import PackageDict #: One line per package dpkg knows, its state first (``ii``, ``rc``, ``hi``, …): #: dpkg also lists removed packages whose configuration it still keeps (#17). #: ``${Architecture}`` tells a multiarch host's ``libc6`` for amd64 from the one for #: i386, two packages of one ``${Package}`` (NetOrk/netork#2907). DPKG_QUERY_COMMAND = ( "dpkg-query -W -f='${db:Status-Abbrev}\\t${Package}\\t${Version}\\t${Installed-Size}" "\\t${source:Package}\\t${source:Version}\\t${Architecture}\\t${binary:Summary}\\n'" " 2>/dev/null" ) #: The second letter of ``db:Status-Abbrev`` for a package whose files are gone: #: ``n`` not installed, ``c`` only its configuration left (``rc``). Every other #: state -- half-installed, unpacked, half-configured, awaiting triggers -- has #: its files on disk. _NOT_ON_DISK = frozenset("nc") class DpkgPackageDict(PackageDict): """A dpkg package with the source package it was built from.""" source_package: str source_version: str def parse_dpkg_query(out: str) -> list[PackageDict]: """The packages in ``DPKG_QUERY_COMMAND``'s output whose files are on disk.""" packages: list[PackageDict] = [] for line in out.splitlines(): # Summary stays last and keeps whatever it contains: maxsplit must # equal the number of tabs the format writes, not the field count. status, _, line = line.partition("\t") if status[1:2] in _NOT_ON_DISK: continue package = _package(line.split("\t", 6)) if package is not None: packages.append(package) return packages def _field(parts: list[str], index: int) -> str: """Field *index* of a split line, stripped; empty where the line has fewer.""" return parts[index].strip() if len(parts) > index else "" def _package(parts: list[str]) -> DpkgPackageDict | None: if len(parts) < 2: return None name = _field(parts, 0) version = _field(parts, 1) size = _field(parts, 2) # Debian source package (e.g. openssh-server → openssh) for OSV matching. source_package = _field(parts, 3) or name # And its version, which is a different number from this package's. # # OSV states Debian ranges in *source* versions. A source package # that ships several binaries gives each its own upstream version: # libldb2 is 2:2.11.0+samba4.22.11+dfsg-… while its source, samba, # is 2:4.22.11+dfsg-…. A consumer matching on source_package and # comparing `version` compares two unrelated numbers — dpkg reads # ldb's 2.11.0 as older than the 2:4.17.4+dfsg-1 that fixed # CVE-2022-44640, and a host five releases past the fix was reported # vulnerable on four packages at once. # # dpkg leaves this empty when it equals `Version`; so does an older # dpkg that does not know the field at all. source_version = _field(parts, 4) or version package: DpkgPackageDict = { "name": name, "version": version, "installed": True, "description": _field(parts, 6), "size": int(size) * 1024 if size.isdigit() else 0, "source": "apt", "source_package": source_package, "source_version": source_version, } if architecture := _field(parts, 5): package["architecture"] = architecture return package