From 66a18cf1a5fc7159626b806d367695239fb9523f Mon Sep 17 00:00:00 2001 From: Hisham Date: Sun, 8 Jan 2017 13:31:27 -0200 Subject: Improve error checking in ownership check. See #664. --- src/luarocks/command_line.lua | 15 +++++++++++++-- src/luarocks/fs/lua.lua | 8 +++++--- src/luarocks/fs/unix/tools.lua | 5 ++++- src/luarocks/fs/win32.lua | 2 +- 4 files changed, 23 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/luarocks/command_line.lua b/src/luarocks/command_line.lua index 528ca068..6a1cc519 100644 --- a/src/luarocks/command_line.lua +++ b/src/luarocks/command_line.lua @@ -40,6 +40,18 @@ local function replace_tree(flags, tree) path.use_tree(tree) end +local function is_ownership_ok(directory) + local me = fs.current_user() + for _ = 1,3 do -- try up to grandparent + local owner = fs.attributes(directory, "owner") + if owner then + return owner == me + end + directory = dir.dir_name(directory) + end + return false +end + --- Main command-line processor. -- Parses input arguments and calls the appropriate driver function -- to execute the action requested on the command-line, forwarding @@ -182,8 +194,7 @@ function command_line.run_command(...) die("Current directory does not exist. Please run LuaRocks from an existing directory.") end - if fs.attributes(cfg.local_cache, "owner") ~= fs.current_user() or - fs.attributes(dir.dir_name(cfg.local_cache), "owner") ~= fs.current_user() then + if not is_ownership_ok(cfg.local_cache) then util.warning("The directory '" .. cfg.local_cache .. "' or its parent directory ".. "is not owned by the current user and the cache has been disabled. ".. "Please check the permissions and owner of that directory. ".. diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index a14303e1..54da4afd 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua @@ -760,11 +760,13 @@ end function fs_lua.attributes(file, attrtype) if attrtype == "permissions" then - return posix.stat(file, "mode") + return posix.stat(file, "mode") or nil elseif attrtype == "owner" then - return posix.getpwuid(posix.stat(file, "uid")).pw_name + local uid = posix.stat(file, "uid") + if not uid then return nil end + return posix.getpwuid(uid).pw_name or nil else - return "" + return nil end end diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index e1b28b19..b2ace3b1 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -204,9 +204,12 @@ function tools.attributes(filename, attrtype) local flag = ((attrtype == "permissions") and vars.STATPERMFLAG) or ((attrtype == "owner") and vars.STATOWNERFLAG) if not flag then return "" end - local pipe = io.popen(vars.STAT.." "..flag.." "..fs.Q(filename)) + local pipe = io.popen(fs.quiet_stderr(vars.STAT.." "..flag.." "..fs.Q(filename))) local ret = pipe:read("*l") pipe:close() + if ret == "" then + return nil + end return ret end diff --git a/src/luarocks/fs/win32.lua b/src/luarocks/fs/win32.lua index e2b37778..19c603dc 100644 --- a/src/luarocks/fs/win32.lua +++ b/src/luarocks/fs/win32.lua @@ -197,7 +197,7 @@ function win32.attributes(filename, attrtype) elseif attrtype == "owner" then return os.getenv("USERNAME") -- FIXME popen_read('powershell -Command "& {(get-acl '..filename..').owner}"'):gsub("^[^\\]*\\", "") end - return "" + return nil end --- Move a file on top of the other. -- cgit v1.2.3-55-g6feb