okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
bcf88b738
main
1"""POSIX 'owner-only' checks that repair what is ours and explain what is not. 2 3Strata keeps its private state in folders (0700) and files (0600) of the user. The checks used to refuse as soon as a mode had drifted 4(a `chmod -R`, an rsync, a restored backup) or the owner was not the current user (one `sudo strata ...` leaves root-owned folders behind). 5Now: 6 7* the folder or file is ours and only the mode bits are wrong: the mode is put back (0700 / 0600) and the command goes on; 8* the filesystem cannot hold modes at all (a Windows drive seen from WSL, vfat, some network shares): one note, then it goes on; 9* another account owns it (root after sudo): the check still refuses, because that is the real boundary, but the message names the 10cause and the exact command that fixes it. 11 12Windows has no mode bits; its own check lives in `private_files` and is not touched here. 13""" 14 15from __future__import annotations 16 17import os 18import stat 19import sys 20from pathlib import Path 21 22POSIX = os .name == "posix" 23_NOTED :set [str ]= set () 24 25 26def owner_only_wrong (path :str | os .PathLike ,details :os .stat_result )-> bool : 27"""True when `path` (whose lstat or fstat is `details`) is not private to this user and could not be made so. 28 29Replaces `details.st_uid != os.getuid() or stat.S_IMODE(details.st_mode) & 0o077`. Never follows a link and never touches 30a file that another account owns. 31""" 32if not POSIX : 33return False 34if details .st_uid != os .getuid (): 35return True 36if not stat .S_IMODE (details .st_mode )& 0o077 : 37return False 38if stat .S_ISLNK (details .st_mode ): 39return True 40want = 0o700 if stat .S_ISDIR (details .st_mode )else 0o600 41try : 42os .chmod (path ,want )# not a link (checked above), so this is the file itself 43after = os .lstat (path ) 44except (OSError ,NotImplementedError ): 45after = None 46if after is not None and after .st_uid == os .getuid ()and not stat .S_IMODE (after .st_mode )& 0o077 : 47return False 48# The mode did not stick although the owner is right: the filesystem has no real modes. Say it once and carry on. 49folder = str (path if stat .S_ISDIR (details .st_mode )else Path (path ).parent ) 50if folder not in _NOTED : 51_NOTED .add (folder ) 52f"strata: { folder } is on a drive that cannot keep files private to your account (a Windows drive seen from Linux, " 53"a USB stick or a network share). Strata goes on; keep your data on the Linux disk to make it private." ,file = sys .stderr ) 54return False 55 56 57def _owner_name (uid :int )-> str : 58try : 59import pwd 60return pwd .getpwuid (uid ).pw_name 61except (ImportError ,KeyError ): 62return f"user { uid } " 63 64 65def fix_target (path :str | os .PathLike )-> Path : 66"""The highest folder above `path` (below the home folder) that another account owns: one `chown` there fixes the whole tree.""" 67target = Path (path ) 68home = Path .home () 69uid = os .getuid () 70best = target 71current = target 72for _ in range (6 ): 73parent = current .parent 74if parent == current or parent == home or parent == Path (parent .anchor ): 75break 76try : 77if parent .lstat ().st_uid == uid : 78break 79except OSError : 80break 81best = parent 82current = parent 83return best 84 85 86def owner_only_text (text :str ,path :str | os .PathLike )-> str : 87"""The message for a refused check. `text` is the old sentence ("recipe directory must be owner-only"); when another account owns 88`path` it becomes a sentence with the cause and the exact command, otherwise it is returned unchanged.""" 89if not POSIX : 90return text 91try : 92owner = os .lstat (path ).st_uid 93except OSError : 94return text 95if owner == os .getuid (): 96return text 97subject = text .split (" must " )[0 ].split (" is not " )[0 ].strip () 98target = fix_target (path ) 99return (f" { subject } belongs to { _owner_name ( owner ) } , not to you (usually from running Strata once with sudo). " 100f"Run this once, then try again: sudo chown -R \"$USER\" { target } " )