|
| 1 | +echo "Temporarily remove automatic printer discovery" |
| 2 | + |
| 3 | +# cups-browsed is the daemon that watches the network and creates print queues |
| 4 | +# by itself. Hardening it (1787815267) took a root daemon with a predictable |
| 5 | +# cache down to a confined service account, but a daemon that turns anything |
| 6 | +# advertising itself on the network into a print queue is a lot of exposure for |
| 7 | +# a convenience, so it comes out of the default install while that is reworked. |
| 8 | +# Temporarily, and only the discovery half: CUPS itself stays, printing keeps |
| 9 | +# working, and a printer is added by hand in Print Settings instead of |
| 10 | +# appearing on its own. |
| 11 | +machine_marker="${OMARCHY_CUPS_BROWSED_REMOVAL_MARKER:-/var/lib/omarchy/migrations/1788009111}" |
| 12 | + |
| 13 | +[[ ! -e $machine_marker ]] || exit 0 |
| 14 | + |
| 15 | +# Nothing to do on a machine that never had it. Checked before any sudo so those |
| 16 | +# runs never prompt for a password, and before the marker so no machine pays a |
| 17 | +# password prompt for a removal it does not need. |
| 18 | +omarchy-pkg-present cups-browsed || exit 0 |
| 19 | + |
| 20 | +# Ask pacman whether the removal is possible before touching the service. If |
| 21 | +# something here depends on cups-browsed, the alternative is a machine whose |
| 22 | +# discovery daemon has been stopped and whose package removal then failed -- |
| 23 | +# broken rather than removed. |
| 24 | +if ! pacman -R --print cups-browsed >/dev/null 2>&1; then |
| 25 | + echo " Something else on this machine still depends on cups-browsed, so it is staying installed." |
| 26 | + exit 0 |
| 27 | +fi |
| 28 | + |
| 29 | +# Disable before removing, and this is the only window in which it works. |
| 30 | +# pacman deletes the unit file but not the enable symlink systemd wrote under |
| 31 | +# /etc, and once the unit is gone `systemctl disable` refuses it by name and |
| 32 | +# leaves the symlink dangling with nothing left that can clean it. Stopping is |
| 33 | +# part of the same step: a daemon whose executable has been unlinked keeps |
| 34 | +# running until it is told not to. A masked or already-disabled unit reports |
| 35 | +# not-enabled and is left alone. Doing it before the queue list is read also |
| 36 | +# means the list cannot grow a new entry while it is being acted on. |
| 37 | +if systemctl is-enabled --quiet cups-browsed.service 2>/dev/null; then |
| 38 | + sudo systemctl disable --now cups-browsed.service |
| 39 | +elif systemctl is-active --quiet cups-browsed.service 2>/dev/null; then |
| 40 | + sudo systemctl stop cups-browsed.service |
| 41 | +fi |
| 42 | + |
| 43 | +# cups-browsed keeps the queues it generated when it stops -- |
| 44 | +# KeepGeneratedQueuesOnShutdown defaults to Yes and nothing here overrides it -- |
| 45 | +# and those queues route through its own implicitclass backend, which goes with |
| 46 | +# the package, so none of them can print again. The idle ones are removed rather |
| 47 | +# than left in Print Settings looking like printers; the ones with jobs on them |
| 48 | +# are handled below. Only queues on that backend: a printer added by hand has an |
| 49 | +# ipp:// or usb:// device and is left alone. |
| 50 | +# |
| 51 | +# LC_ALL=C because lpstat translates "device for", and on a German or French |
| 52 | +# machine the untranslated pattern would match nothing and read exactly like a |
| 53 | +# machine that had no queues to clean up. Captured rather than piped so that |
| 54 | +# failing to reach cupsd is distinguishable from finding nothing. The name is |
| 55 | +# matched greedily because CUPS allows a colon in a queue name but never a |
| 56 | +# space, so the last ": implicitclass://" is the separator and an earlier colon |
| 57 | +# belongs to the name. |
| 58 | +if ! queue_report=$(LC_ALL=C lpstat -v 2>/dev/null); then |
| 59 | + echo " Could not ask CUPS which queues discovery had created." |
| 60 | + echo " Discovery is off, but cups-browsed stays installed; remove it by hand once CUPS answers." |
| 61 | + exit 0 |
| 62 | +fi |
| 63 | + |
| 64 | +generated_queues=$(printf '%s\n' "$queue_report" | |
| 65 | + sed -n 's|^device for \(.*\): implicitclass://.*|\1|p') |
| 66 | + |
| 67 | +unremoved=0 |
| 68 | + |
| 69 | +while IFS= read -r queue; do |
| 70 | + [[ -n $queue ]] || continue |
| 71 | + |
| 72 | + # A queue name is whatever the printer advertised, put through cups-browsed's |
| 73 | + # own sanitizer. `lpstat -o` takes its destination as an optional argument, so |
| 74 | + # a leading dash reads as the next option and a comma as a list separator, and |
| 75 | + # "all" is its word for every destination. The jobs on such a queue cannot be |
| 76 | + # asked about, so it is left alone and named. Never a reason to keep the |
| 77 | + # package, though: that would hand a printer that picked its own name a veto |
| 78 | + # over the removal. |
| 79 | + if [[ $queue == -* || $queue == *,* || $queue == "all" ]]; then |
| 80 | + echo " Cannot safely ask about jobs on the queue named '$queue'; remove it in Print Settings." |
| 81 | + continue |
| 82 | + fi |
| 83 | + |
| 84 | + # Removing a queue aborts what is printing on it. The implicitclass backend |
| 85 | + # only needs cups-browsed to pick a destination, so a job already past that |
| 86 | + # point finishes on its own even though the daemon has stopped -- and a job |
| 87 | + # that has not is one this cannot route anyway. Either way the queue is left |
| 88 | + # for the person whose job it is, and named so they know to remove it. |
| 89 | + if job_report=$(LC_ALL=C lpstat -o "$queue" 2>/dev/null); then |
| 90 | + if [[ -n $job_report ]]; then |
| 91 | + echo " $queue still has jobs, so it is being left alone." |
| 92 | + echo " Once they finish or are cancelled, remove it in Print Settings; it cannot print again." |
| 93 | + continue |
| 94 | + fi |
| 95 | + else |
| 96 | + echo " Could not check for jobs on $queue, so it is being left alone." |
| 97 | + continue |
| 98 | + fi |
| 99 | + |
| 100 | + if ! sudo lpadmin -x "$queue"; then |
| 101 | + echo " Could not remove the queue $queue." |
| 102 | + unremoved=1 |
| 103 | + fi |
| 104 | +done <<<"$generated_queues" |
| 105 | + |
| 106 | +# A queue that would not delete is a CUPS that is not answering as expected, so |
| 107 | +# the package stays rather than deleting the backend out from under it. Discovery |
| 108 | +# is stopped either way, which is the half that mattered. omarchy-migrate records |
| 109 | +# this migration for the user as soon as it exits zero, so this is where the |
| 110 | +# machine stays until someone removes the package by hand -- said plainly rather |
| 111 | +# than dressed up as a retry. |
| 112 | +if ((unremoved)); then |
| 113 | + echo " Leaving cups-browsed installed. Discovery is off; remove the package by hand once those queues are gone." |
| 114 | + exit 0 |
| 115 | +fi |
| 116 | + |
| 117 | +# Raw pacman rather than omarchy-pkg-drop, which passes -n: that discards the |
| 118 | +# files pacman has marked as backups instead of renaming them .pacsave, and |
| 119 | +# /etc/cups/cups-browsed.conf is one of them. A removal meant to be temporary |
| 120 | +# should leave the machine's copy of its own configuration behind. Plain -R |
| 121 | +# rather than -Rs, so this only ever removes the one package it names: -s also |
| 122 | +# sweeps dependencies that have become unneeded, which is nothing today but is |
| 123 | +# a promise the dependency graph of a rolling distribution cannot keep. |
| 124 | +# pacman's systemd hook reloads the system manager once the unit file goes. |
| 125 | +sudo pacman -R --noconfirm cups-browsed |
| 126 | + |
| 127 | +# Migration state is per user, so every account on this machine runs every |
| 128 | +# migration. Without machine-wide state, an account whose first run comes after |
| 129 | +# someone deliberately reinstalled cups-browsed would quietly take it back out |
| 130 | +# again. This records that the machine has had its one removal. |
| 131 | +sudo install -Dm644 /dev/null "$machine_marker" |
0 commit comments