package apply // A directory whose owner changes gives its contents the new owner too (design 47 H2, H-A-545 (2), H-A-550): a module // moved to an account of its own (runs-as) keeps reading what it wrote as the account it ran as before, and nobody // changes an owner by hand. // // Only what the directory's previous owner held, and only what no resource of the declaration places: a path the // declaration places (a directory or a file of its own, gitea's data below its state) keeps its declared owner, and // nothing below it is touched; an entry another account owns is left, and nothing below it is walked. No link is // followed (a link is owned as a link, Lchown), and the walk stays on the directory's file system. import ( "context" "errors" "fmt" "io/fs" "os" "path/filepath" "strings" "git.novox.be/novox/mesh-sdk/go/declaration" "github.com/novox/mesh-host/internal/store" "github.com/novox/mesh-host/internal/system" ) // giveContents gives every entry below dir whose user is from's (uid) to the new user to's, and the new group to's only // where the entry's group was from's (H-A-554 (2)); but for the paths placed names (cleaned paths the declaration places) // and what lies below them, an entry of another user and what lies below it, and what lies on another file system. An // entry with more than one hard link is left with its owner, said in the journal with its path and link count (H-A-579 (2)). An entry gone while the walk runs is nothing to give. Answers how many entries changed hands. Called before the directory's // own owner changes (H-A-554 (2)), so a walk that failed is walked again at the next apply. func giveContents(dir string, from, to [2]int, placed map[string]bool) (int, error) { root := filepath.Clean(dir) device, err := deviceOf(root) if err != nil { return 0, fmt.Errorf("the file system of %s cannot be read: %w", root, err) } given := 0 err = filepath.WalkDir(root, func(path string, e fs.DirEntry, err error) error { if err != nil { if errors.Is(err, fs.ErrNotExist) { return nil } return err } if path == root { return nil } skip := func() error { if e.IsDir() { return filepath.SkipDir } return nil } if placed[filepath.Clean(path)] { return skip() } info, err := os.Lstat(path) if errors.Is(err, fs.ErrNotExist) { return skip() } if err != nil { return err } uid, gid, ok := statOwner(info) if !ok || uid != from[0] { return skip() } if e.IsDir() { if d, derr := deviceOf(path); derr != nil || d != device { return filepath.SkipDir } } else if links := linkCount(info); links > 1 { // An entry with more than one name is left with its owner (H-A-579 (2)): its other names may lie outside the // directory, which the pass never crosses. journal(fmt.Sprintf("%s has %d hard links, and is left with its owner when the directory %s is given its new one", path, links, root)) return nil } group := gid if gid == from[1] { group = to[1] } if err := lchownContent(path, to[0], group); err != nil { if errors.Is(err, fs.ErrNotExist) { return skip() } return fmt.Errorf("cannot give %s to the directory's new owner: %w", path, err) } given++ return nil }) return given, err } // journal says one line in the node-engine's journal (its standard error); a variable so a test reads what was said. var journal = func(line string) { fmt.Fprintf(os.Stderr, "mesh-host: %s\n", line) } // lchownContent owns one entry as itself, a link as a link; a variable so a test sees what was given without being root. var lchownContent = os.Lchown // placedPaths is every path the resources place, cleaned: what giveContents leaves to its own resource. func placedPaths(resources ...[]declaration.Resource) map[string]bool { out := map[string]bool{} for _, list := range resources { for _, r := range list { if t := r.Target(); t != "" { out[filepath.Clean(t)] = true } } } return out } // giveContentsTo gives the contents of the directory r, found as before, to r's declared owner (giveContents), before the // directory's own owner changes. func giveContentsTo(r *declaration.Directory, before os.FileInfo, placed map[string]bool) error { fromUID, fromGID, ok := statOwner(before) if !ok || r.Owner == "" { return nil } uid, gid, err := idsOf(r.Owner) if err != nil { return fmt.Errorf("%s should belong to %q: %w", r.Path, r.Owner, err) } _, err = giveContents(r.Path, [2]int{fromUID, fromGID}, [2]int{uid, gid}, placed) return err } // ownDirectory gives a directory changed by path its owner (own); a variable so a test sees it without being root. var ownDirectory = own // ownerPassRuns is whether applying r runs the owner pass: the directory is found, a real directory the mesh made (or // handed to the node-engine), with another owner than the one declared (design 47 H2, H-A-641). The same conditions // applyDirectory judges before it gives the contents, read before any of them is applied so the module's units can be // stopped first. func ownerPassRuns(r *declaration.Directory, previous store.Applied, wasBefore bool) bool { if r.Owner == "" { return false } found, err := os.Lstat(r.Path) if err != nil || found.Mode()&os.ModeSymlink != 0 || !found.IsDir() { return false } if HandedOver()[handOverKey(r.ID, r.Path)] { return false } if wasBefore { ours := previous.Target != "" && filepath.Clean(previous.Target) == filepath.Clean(r.Path) && !previous.AsFound if !ours && !madeInTheMeshsTree(r.Path, found) { return false } } owned, err := ownedBy(r.Path, r.Owner) return err == nil && !owned } // stopForOwnerPass stops, once per module, the units of each module of d that declares a directory whose owner pass // will run (H-A-641): before the first pass, so no process of the earlier account writes beneath a directory while it is // given. The normal apply starts them again, as it starts any declared unit found stopped; a pass that fails gates the // module (applyScoped), so its units stay stopped until the next apply's pass completes (H-A-701). The tool runner is // never stopped (StopModule). A module whose units could not all be stopped fails at its directory, and is gated the same. func stopForOwnerPass(ctx context.Context, sys system.System, d *declaration.Declaration, known store.State, in inputs, run Runner, log func(string)) (failed map[string]error) { stopped := map[string]bool{} for _, res := range d.Resources { dir, ok := res.(*declaration.Directory) if !ok || in.refused[filepath.Clean(dir.Path)] != nil { continue } module, ours := d.ModuleOfID(dir.ID) if !ours || stopped[module] || failed[module] != nil { continue } previous, _ := known.Find(dir.ID) if !ownerPassRuns(dir, previous, in.dirsBefore[filepath.Clean(dir.Path)]) { continue } stopped[module] = true names, err := StopModule(ctx, sys, known, module, run) if err != nil { if failed == nil { failed = map[string]error{} } failed[module] = fmt.Errorf("the units of %s could not be stopped for the owner of %s: %w", module, dir.Path, err) continue } log(fmt.Sprintf(" stopped %s for the owner of %s: %s", module, dir.Path, strings.Join(names, ", "))) } return failed }