Skip to content

lemano revert

lemano revert puts the files and kernel parameters of a target back to how generation SEQ recorded them. It is the only command that writes on a server; every other command only reads.

lemano revert NAME --to SEQ [PATH...] [--sysctl KEY]... [--delete-added]
              [--dry-run] [--expect-plan DIGEST | --auto-approve]
              [--trial[=DURATION]] [--no-trial] [--auto-confirm]
              [--reload NAME]... [-f PATH]

The flags and examples of the command are also on its generated page.

What is reverted

GivenReverted
Neither paths nor --sysctlevery File and Sysctl object that differs between generation SEQ and the live server
Pathsthose files only
--sysctl KEYthat parameter’s live value, and the files that set it at boot

Paths and --sysctl keys are a filter: only those are considered, and nothing else is looked at. --delete-added then deletes only the added files among the paths you named. The plan says so in its header (“Only the paths and keys you named are considered”).

This command reverts regular files and kernel parameters only. A name that is not one of them (a package, a user, a service) is refused with its kind named. The preview lists the other kinds that differ (packages, users, groups, services, firewall rules, mounts) under “Other kinds that differ, not reverted by this command”, and directories and symbolic links as not reverted; nothing is ignored without a word.

For each file:

  • Changed since SEQ: written back with generation SEQ’s content, owner, group and mode.
  • Deleted since SEQ: created again, with its owner, group and mode.
  • Added since SEQ: listed, and deleted only with --delete-added.
  • Content not stored in SEQ (larger than the content limit, excluded with contentExcludePaths, or not read): listed with the reason. The other files are reverted if you approve.
  • Not read by the fresh pass (no permission): listed; its live state is not known, so it is not touched.
  • Named by the target’s excludePaths: listed and not touched.

For a kernel parameter, the live value is set with sysctl -w and the value at boot is restored by reverting the files that set it (its sources in either generation). Both show as separate lines, because removing a line from a file does not change the live value. A file outside the places the File kind reads (for example under /usr/lib/sysctl.d) is named as not kept by Lemano, and not reverted.

The preview and the question

The revert takes the target’s lock, then runs a fresh pass, recorded like any discover. The preview is the difference between that live state and generation SEQ, in the direction of the revert: - is what the server has now, + what it will have. Content is shown with the rules of lemano diff: a file that only some users can read, or one that holds sealed text (a crontab, sudoers, sshd’s configuration, /etc/fstab, an authorized_keys file), shows only that its content changes, unless you add --show-secrets. --no-content shows no content. With -o json or -o yaml the plan carries the same content diffs.

Then the command asks:

Do you want to revert these changes? Only 'yes' will be accepted.
  Enter a value:

Any other answer cancels, with exit code 1, and nothing is written.

  • --dry-run shows the preview and stops.
  • --expect-plan DIGEST approves the plan with that digest, as a preview printed it, and asks nothing. See below.
  • --auto-approve writes without asking, whatever plan the run builds. It cannot be combined with --expect-plan.
  • Without a terminal to ask on (cron, a pipeline, a script or an agent), or with -o json or -o yaml, and without --expect-plan or --auto-approve, the command shows the same preview as --dry-run, writes nothing on the server, records no revert and fails with exit code 9 (ConfirmationRequired). Its error holds the command that approves the plan shown: yours plus --expect-plan DIGEST. A plan with nothing to revert needs no approval and exits 0. See scripting.

The lock is held from the fresh pass to the end, so no other Lemano command reads or writes the target in between. The revert’s generation records how it was approved: typed (yes at a terminal), expect-plan or auto-approve.

The plan digest

Every plan has a planDigest (a Plan digest: line in text): hmac-sha256: and 64 hexadecimal digits, a keyed hash, under the store’s hash key, of what this revert would do on the host. It holds the target, the generation, the privilege the writes run with, --delete-added, the trial and its window, and, for each change the revert sends to the host, its path or key, its action, the validator, the keyed hashes of the file’s content before and after with its owner, group and mode, and for a kernel parameter its live values and the files that set it at boot. It does not hold times, pass IDs, the generation number of the reading, the kinds that are not reverted, the services that read a file, the text of a diff, or a change that is listed and not attempted. The digest is one keyed hash, so it reveals nothing about the content of a file, and it moves when the content to be written changes. It is the same for two previews of an unchanged host whatever --no-content or --show-secrets say, and something else changing on the host does not move it. Something the revert would do differently does: a file in the plan changed, another file now differs, a different option.

With --expect-plan DIGEST the command reads the server, builds its plan and compares:

  • same digest: it applies that plan. The check just before each write still refuses a file that changed during the run (exit 7);
  • another digest: it writes nothing, prints the new plan, and fails with exit code 9 and the reason ConfirmationRequired, saying that the plan changed since it was approved. The hint has the command that approves the new plan;
  • nothing left to revert: it says so and exits 0.

A trial without a terminal still needs --auto-confirm or --no-trial.

How a file is written

The changes travel on the session’s standard input, never on a command line. For each file, the server:

  1. creates a temporary file in the same directory, with its final mode from the start, so it is never readable by others meanwhile, and sets its owner and group;
  2. writes the content and flushes it to disk;
  3. checks it, when a check applies: sshd -t for sshd’s configuration, visudo -c for sudoers; a file that fails is not written, and the live one stays as it was; then checks that the file it wrote has the SHA-256 of the content Lemano sent, which travels with it; a content damaged on the way is refused;
  4. checks that the live file still has the content the preview showed (its SHA-256, which the fresh pass read); otherwise the file is refused and reported as “changed since the preview”;
  5. renames the temporary file over the live one, and flushes the directory;
  6. restores the SELinux label on servers that use SELinux.

The check in step 4 comes from the recorded state: the preview was made from a pass that Lemano keeps, so the server can tell, file by file, whether someone changed the file after you approved. Writing over a newer version without knowing it is what the check exists to prevent.

No backup is left on the server: the version being replaced is in the store, from the fresh pass, and the next revert can put it back.

Writing needs root through sudo -n (or the sudo password the target names). On a target with privilege: user, only files the login owns are reverted, kernel parameters are not, and the rest is listed with the reason.

Services

Nothing is restarted or reloaded unless you ask. The preview names the services that probably read a file: a unit whose file or drop-in it is, and the SSH service for sshd’s configuration. --reload NAME (repeatable) reloads a service after a successful write. The name is resolved to the unit the server has, from the fresh pass: --reload sshd reloads ssh.service on Debian and Ubuntu and sshd.service on the Red Hat family. A name the server does not have stops the revert before anything is written.

ufw is the exception. It reads its files only when it loads them, and its unit has no reload action, so a revert that writes one of its files (/etc/ufw/*.rules, ufw.conf, sysctl.conf, before.init, after.init, /etc/default/ufw) while ufw is enabled runs ufw reload after the writes; the preview lists it as ufw (ufw reload). The undo of a trial does the same after it puts the files back. While ufw is not enabled nothing is loaded: the plan says the rules apply when it is enabled. Reverting ufw.conf to a disabled state does not unload rules that are already loaded.

Trials

A revert that writes a file controlling access to the server runs as a trial: sshd’s configuration, sudoers, /etc/pam.d, /etc/security and below, firewall rule files (/etc/nftables.conf, /etc/ufw, /etc/firewalld, /etc/iptables, /etc/sysconfig/iptables), authorized_keys files, /etc/passwd, /etc/shadow, /etc/group, /etc/gshadow, /etc/nsswitch.conf, /etc/hosts.allow and /etc/hosts.deny. --trial[=DURATION] runs any revert as a trial; --no-trial turns it off.

  1. Before writing, the server keeps a copy of each file (content, owner, group, mode) and an undo script in a root-only folder under /var/lib/lemano/trial/, and starts a transient timer (systemd-run, or at without systemd) that runs the script when the window ends: 5 minutes by default, from 1 to 60 minutes. With neither, the trial is refused and nothing is written.
  2. When the revert restarts the SSH server (a file that decides where it listens), the server checks, on the host, that it listens on the port (and the IP address) the target connects to, provided it listened there before. If not, the host undoes the trial at once, restoring the files and restarting the server as the write did, without waiting for a login that cannot happen. A target reached through a jump host or a proxy command is not checked this way.
  3. After writing, Lemano opens a new connection, a fresh login through the same route (jump hosts, proxy command), and runs a command with the same privilege. A change that cut access is caught here. A connection refused or cut (sshd refuses connections for a moment while it reloads) is tried again for up to 20 seconds; a refused login is not.
  4. If the login works, it asks: Keep these changes? Only 'yes' will be accepted. (--auto-confirm answers yes once the login worked). On yes, the server stops the timer and removes the folder, and the generation records how it was kept (typed or auto-confirm).
  5. On any other answer, the change is undone at once through the fresh connection. When the fresh login failed, it is undone at once through the connection that wrote, which sshd keeps open. The undo checks sshd’s configuration and sudoers before any reload, and removes the folder; the outcome undone is recorded.
  6. When neither connection works, the server’s timer undoes the change when the window ends, and the next pass after it records the outcome.

A write that changed nothing (every file changed since the preview, for example) cancels its trial at once.

Note

The timer is transient: it does not survive a reboot. On a host with systemd the trial also leaves a unit that undoes it at boot, before the SSH server starts, so a server that restarts during the window comes back with the old files and nothing of the trial left. A host without systemd holds the undo in an at job: atd runs a job whose time passed while the machine was off as soon as it starts, which is not ordered before the SSH server.

A trial whose folder is still on the host after a restart (the undo at boot failed, or the host has no systemd and no atd), or whose undo began and did not finish (a file could not be put back), is reported on the target as the condition UnfinishedTrial (see it with lemano target show NAME), with the command that finishes it; --undo takes up an undo that did not finish:

lemano target end-trial NAME --undo    # put the saved files back
lemano target end-trial NAME --keep    # keep the current files

Either removes the folder, and a pass follows, which clears the condition. A revert is refused while an unfinished trial is on the server, and while a trial waits for its window to end. The refusals are reasons of their own, with their facts in the error’s details (scripting): UnfinishedTrial lists the trial IDs, TrialPending gives the trial’s generation and when the host undoes it, and NoUnfinishedTrial is end-trial finding nothing to finish. All three exit with code 2, as the same refusals did before.

History

A revert writes a new generation; no generation is removed or replaced.

GenerationTriggerRecords
the fresh pass, when something changeddiscoverthe live state the preview was made from
the revertrevert (or trial)the source generation, how it was approved, and for each change its operation and result: applied, skipped or failed, with the reason
a confirmed trialtrialthe outcome confirmed, and how it was kept
a trial undone at oncetrialthe outcome undone
the pass after the revertdiscoverthe state the revert left
a trial left to the timer, at the next pass after its windowtrialthe outcome: undone by the server, partly, or not undone

lemano history shows the revert as “reverted to SEQ: 2 applied, 1 skipped; approved: –auto-approve” (or “–expect-plan (the plan shown earlier, unchanged)”), and -o json gives source, changes and approval. The changes Lemano made are not attributed to anyone in the pass that follows.

Exit codes

CodeWhen
0every change was applied; or --dry-run; or nothing differs
1the revert was cancelled, or a trial was not kept (undone at once)
2a wrong command line
3the target could not be reached, or the fresh login of a trial failed (the change is undone)
5another command holds the target’s lock
7at least one change was not applied: a file changed since the preview, a failed check, no answer from the server
9nobody to approve it: no terminal, or -o json/-o yaml, and no --expect-plan or --auto-approve; or the plan is not the one --expect-plan named. The plan was shown and nothing was written

See exit codes for the others.