glacier-ng 4.0.0
This commit is contained in:
commit
0cf4829fa5
159 files changed
+33717
No files matched your search
Executable
+354
@@ -0,0 +1,354 @@
|
||||
#!/usr/bin/luajit -E
|
||||
|
||||
-- gstore: per-user glacier directory management (GL_SCOPE_USR only —
|
||||
-- this tool never touches system scope; that is syspkg's alone).
|
||||
--
|
||||
-- `gstore -n` is the one operation that needs root: it creates
|
||||
-- /glacier/usr/<uid>, which lives in a root-owned parent so no user can
|
||||
-- create (or squat) another uid's directory. Root runs it directly with
|
||||
-- an explicit UID; a user runs it for themselves through a doas rule
|
||||
-- that pins the arguments to exactly `-n`:
|
||||
--
|
||||
-- permit nopass :wheel as root cmd /path/to/gstore args -n
|
||||
--
|
||||
-- doas is the privilege boundary (SECURITY_DESIGN.txt section 6): that
|
||||
-- rule is what stops a user passing someone else's UID. With no UID,
|
||||
-- gstore acts for the invoking user, resolved the same way gpkg resolves
|
||||
-- whose store it uses, so `doas gstore -n` always creates the directory
|
||||
-- gpkg will look for.
|
||||
--
|
||||
-- Because it can run as root, gstore follows syspkg's loading rules, not
|
||||
-- the CWD-relative convention: -E in the shebang, package.path and every
|
||||
-- ffi.load() pinned to this script's own real directory, and when root,
|
||||
-- everything loaded must be root-owned and not group/world-writable all
|
||||
-- the way up to /. KEEP THIS PREAMBLE IN SYNC WITH syspkg's. It can't be
|
||||
-- shared through a lib/ file, since it has to run before anything from
|
||||
-- lib/ is trusted.
|
||||
|
||||
local ffi = require("ffi")
|
||||
local bit = require("bit")
|
||||
|
||||
-- asm() aliases so these don't collide with glacier_cdef's own
|
||||
-- declarations of the same libc symbols (ffi.cdef refuses redefinition).
|
||||
ffi.cdef[[
|
||||
unsigned int gstore_geteuid(void) asm("geteuid");
|
||||
char *gstore_realpath(const char *path, char *resolved) asm("realpath");
|
||||
struct gstore_passwd {
|
||||
char *pw_name;
|
||||
char *pw_passwd;
|
||||
unsigned int pw_uid;
|
||||
unsigned int pw_gid;
|
||||
char *pw_gecos;
|
||||
char *pw_dir;
|
||||
char *pw_shell;
|
||||
};
|
||||
struct gstore_passwd *gstore_getpwnam(const char *name) asm("getpwnam");
|
||||
]]
|
||||
|
||||
-- Errors raised before the trust checks below have passed. Same as
|
||||
-- syspkg: reproduces lg_printf(LG_ERR, ...) byte for byte, because
|
||||
-- loading libglacier_log as root before verifying it is the hole the
|
||||
-- checks close.
|
||||
local function die(fmt, ...)
|
||||
io.stdout:write("\27[31m\27[1m[x]\27[0m\27[m " .. string.format(fmt, ...) .. "\n")
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
local is_root = ffi.C.gstore_geteuid() == 0
|
||||
|
||||
-- This script's own directory, symlinks resolved.
|
||||
local self_dir
|
||||
do
|
||||
local buf = ffi.new("char[4096]")
|
||||
if ffi.C.gstore_realpath(arg[0], buf) == nil then
|
||||
die("Cannot resolve own location from '%s'.", tostring(arg[0]))
|
||||
end
|
||||
self_dir = ffi.string(buf):match("^(.*)/[^/]*$")
|
||||
if not self_dir or self_dir == "" then self_dir = "/" end
|
||||
end
|
||||
|
||||
local function shell_quote(s)
|
||||
return "'" .. s:gsub("'", "'\\''") .. "'"
|
||||
end
|
||||
|
||||
local function require_root_owned(path)
|
||||
local p = io.popen("stat -L -c '%u %a' -- " .. shell_quote(path) .. " 2>/dev/null")
|
||||
local line = p and p:read("*l")
|
||||
if p then p:close() end
|
||||
|
||||
local uid, mode = (line or ""):match("^(%d+) ([0-7]+)$")
|
||||
if not uid then
|
||||
die("Cannot stat %s.", path)
|
||||
end
|
||||
if tonumber(uid) ~= 0 then
|
||||
die("Refusing to run: %s is not owned by root.", path)
|
||||
end
|
||||
if bit.band(tonumber(mode, 8), tonumber("022", 8)) ~= 0 then
|
||||
die("Refusing to run: %s is group- or world-writable.", path)
|
||||
end
|
||||
end
|
||||
|
||||
local trusted = {}
|
||||
do
|
||||
local d = self_dir
|
||||
while true do
|
||||
table.insert(trusted, d)
|
||||
if d == "/" then break end
|
||||
d = d:match("^(.*)/[^/]*$")
|
||||
if d == "" then d = "/" end
|
||||
end
|
||||
end
|
||||
|
||||
local lib_dir = self_dir .. "/lib"
|
||||
local so_dir = lib_dir .. "/glacier"
|
||||
local so = {
|
||||
log = so_dir .. "/libglacier_log.so",
|
||||
istore = so_dir .. "/libglacier_istoreutils.so",
|
||||
}
|
||||
|
||||
-- libglacier_pkg.so and libglacier_transaction.so are never ffi.load()ed
|
||||
-- directly, but istoreutils pulls them in through its $ORIGIN RUNPATH,
|
||||
-- so they run as root too.
|
||||
for _, p in ipairs({
|
||||
lib_dir, so_dir,
|
||||
lib_dir .. "/cargs.lua",
|
||||
lib_dir .. "/glacier_cdef.lua",
|
||||
so.log, so.istore,
|
||||
so_dir .. "/libglacier_pkg.so",
|
||||
so_dir .. "/libglacier_transaction.so",
|
||||
}) do
|
||||
table.insert(trusted, p)
|
||||
end
|
||||
|
||||
if is_root then
|
||||
-- Installed, lib/glacier and each .so are symlinks out of this
|
||||
-- package's store dir (lib/glacier -> /usr/lib/glacier -> links
|
||||
-- into libglacier-ng's), so the directories the loader really
|
||||
-- walks are the REAL path's ancestors. Check those too.
|
||||
local checked = {}
|
||||
local buf = ffi.new("char[4096]")
|
||||
for _, p in ipairs(trusted) do
|
||||
require_root_owned(p)
|
||||
if ffi.C.gstore_realpath(p, buf) == nil then
|
||||
die("Cannot stat %s.", p)
|
||||
end
|
||||
local d = ffi.string(buf)
|
||||
while not checked[d] do
|
||||
checked[d] = true
|
||||
require_root_owned(d)
|
||||
if d == "/" then break end
|
||||
d = d:match("^(.*)/[^/]*$")
|
||||
if d == "" then d = "/" end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
package.path = self_dir .. "/?.lua"
|
||||
package.cpath = ""
|
||||
|
||||
local cargs = require("lib/cargs")
|
||||
require("lib/glacier_cdef")
|
||||
|
||||
local log = ffi.load(so.log)
|
||||
local istore = ffi.load(so.istore)
|
||||
|
||||
local USR_ROOT = "/glacier/usr"
|
||||
|
||||
local argc, argv = cargs.get_argv()
|
||||
local args = {}
|
||||
|
||||
local usage_line = "usage: %s [-hv] [-n [UID]] [-sbx UID] [-V]"
|
||||
|
||||
local longopts = {
|
||||
help = "-h",
|
||||
version = "-v",
|
||||
sync = "-s",
|
||||
new = "-n",
|
||||
delete = "-x",
|
||||
backup = "-b",
|
||||
verbose = "-V",
|
||||
}
|
||||
|
||||
for i = 1, argc - 1 do
|
||||
table.insert(args, ffi.string(argv[i]))
|
||||
end
|
||||
|
||||
if #args == 0 then
|
||||
log.lg_printf(2, usage_line, argv[0])
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
local isVerbose = false
|
||||
|
||||
-- Whose directory `gstore -n` with no UID is for. Same order as gpkg's
|
||||
-- get_loginuid (so the two always agree), plus DOAS_USER for when doas
|
||||
-- is the reason we're uid 0: the kernel's loginuid if one was set at
|
||||
-- login, else the user doas says invoked it, else our real uid.
|
||||
local function invoking_uid()
|
||||
local f = io.open("/proc/self/loginuid", "r")
|
||||
if f then
|
||||
local n = tonumber(f:read("*l"))
|
||||
f:close()
|
||||
if n and n ~= 4294967295 then return n end
|
||||
end
|
||||
|
||||
local doas_user = os.getenv("DOAS_USER")
|
||||
if is_root and doas_user then
|
||||
local pw = ffi.C.gstore_getpwnam(doas_user)
|
||||
if pw ~= nil then return tonumber(pw.pw_uid) end
|
||||
end
|
||||
|
||||
return tonumber(ffi.C.getuid())
|
||||
end
|
||||
|
||||
-- `-n`'s UID is optional; everything else's isn't.
|
||||
local function uid_arg(i, optional)
|
||||
local uid = args[i + 1]
|
||||
if uid == nil or uid:sub(1, 1) == "-" then
|
||||
if optional then return nil end
|
||||
log.lg_printf(2, "No UID was supplied.")
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
-- A uid_t: anything else would wrap around in C to some other UID.
|
||||
local uidn = tonumber(uid)
|
||||
if not uidn or uidn ~= math.floor(uidn) or uidn < 0 or uidn > 4294967294 then
|
||||
log.lg_printf(2, "Invalid UID: %s", uid)
|
||||
os.exit(1)
|
||||
end
|
||||
return uidn
|
||||
end
|
||||
|
||||
local function handle_flag(flag, i)
|
||||
if flag == "-h" then
|
||||
log.lg_gstore_help(argc, argv)
|
||||
os.exit(0)
|
||||
elseif flag == "-v" then
|
||||
print("v4.0.0")
|
||||
os.exit(0)
|
||||
elseif flag == "-V" then
|
||||
isVerbose = true
|
||||
elseif flag == "-s" then
|
||||
log.lg_printf(2, "-s is not implemented yet.")
|
||||
os.exit(1)
|
||||
elseif flag == "-n" then
|
||||
if isVerbose then
|
||||
print("Glacier v4.0.0")
|
||||
end
|
||||
|
||||
if not is_root then
|
||||
log.lg_printf(2, "Cannot create Glacier directories: Permission denied")
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
local uidn = uid_arg(i, true) or invoking_uid()
|
||||
if uidn == 0 then
|
||||
log.lg_printf(2, "No UID was supplied.")
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
if uidn < 1000 then
|
||||
log.lg_printf(2, "Cannot create Glacier directories for system UIDs.")
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
if istore.gl_init_user(uidn, isVerbose) ~= 0 then
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
local base = string.format("%s/%d", USR_ROOT, uidn)
|
||||
log.lg_printf(3, "Successfully created Glacier directories for UID %d", ffi.new("int", uidn))
|
||||
log.lg_printf(3, "The new directories are located at:")
|
||||
print("\t* " .. base .. "/index")
|
||||
print("\t* " .. base .. "/store")
|
||||
print("\t* " .. base .. "/links")
|
||||
os.exit(0)
|
||||
elseif flag == "-b" then
|
||||
local uidn = uid_arg(i, false)
|
||||
local uids = tostring(uidn)
|
||||
|
||||
local tar_path = args[i + 2]
|
||||
if not tar_path then
|
||||
log.lg_printf(2, "No OUTFILE was supplied.")
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
local base = USR_ROOT .. "/" .. uids
|
||||
|
||||
local uid_index_exists = istore.gl_usr_istore_exists(ffi.C.INDEX, uidn)
|
||||
local uid_store_exists = istore.gl_usr_istore_exists(ffi.C.STORE, uidn)
|
||||
|
||||
if not uid_index_exists and not uid_store_exists then
|
||||
log.lg_printf(2, "Index and store for UID %s do not exist.", uids)
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
if not uid_index_exists and uid_store_exists then
|
||||
log.lg_printf(2, "UID %s is missing its index directory.", uids)
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
if uid_index_exists and not uid_store_exists then
|
||||
log.lg_printf(2, "UID %s is missing its store directory.", uids)
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
local status = istore.gl_backup_istore(tar_path, USR_ROOT, uids)
|
||||
|
||||
if status ~= 0 then
|
||||
log.lg_printf(2, "Failed to create backup for UID %s.", uids)
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
log.lg_printf(3, "Successfully created backup for UID %s.", uids)
|
||||
log.lg_printf(3, "Archived the following directories:")
|
||||
print("\t* " .. base .. "/index")
|
||||
print("\t* " .. base .. "/store")
|
||||
print("\t* " .. base .. "/links")
|
||||
os.exit(0)
|
||||
elseif flag == "-x" then
|
||||
local uidn = uid_arg(i, false)
|
||||
|
||||
if uidn < 1000 then
|
||||
log.lg_printf(2, "Cannot delete Glacier directories for system UIDs.")
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
if istore.gl_delete_user(uidn, isVerbose) == 0 then
|
||||
os.exit(0)
|
||||
end
|
||||
os.exit(1)
|
||||
else
|
||||
log.lg_printf(2, "Unknown option: %s", flag)
|
||||
log.lg_printf(2, usage_line, argv[0])
|
||||
os.exit(1)
|
||||
end
|
||||
end
|
||||
|
||||
for i = 1, #args do
|
||||
local a = args[i]
|
||||
|
||||
if a:sub(1, 2) == "--" then
|
||||
local name = a:sub(3)
|
||||
local short = longopts[name]
|
||||
if not short then
|
||||
log.lg_printf(2, "Unknown option: %s", a)
|
||||
log.lg_printf(2, usage_line, argv[0])
|
||||
os.exit(1)
|
||||
end
|
||||
a = short
|
||||
end
|
||||
|
||||
if a:sub(1, 1) == "-" and #a > 2 and a:sub(2, 2) ~= "-" then
|
||||
for c in a:sub(2):gmatch(".") do
|
||||
handle_flag("-" .. c, i)
|
||||
end
|
||||
elseif a:sub(1, 1) == "-" then
|
||||
handle_flag(a, i)
|
||||
elseif i > 1 and args[i - 1]:sub(1, 1) == "-" then
|
||||
-- operand of the preceding flag (UID / OUTFILE); handled there
|
||||
else
|
||||
log.lg_printf(2, "Unexpected argument: %s", a)
|
||||
os.exit(1)
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user