Security: forbid unauthorized access to inventories using CSM

This commit is contained in:
Gabriel Pérez-Cerezo 2020-11-08 22:01:35 +01:00
parent b0667f1389
commit a77c1e4c50
2 changed files with 38 additions and 10 deletions

View File

@ -62,12 +62,12 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
if fields.unrent then if fields.unrent then
local pos = minetest.string_to_pos(formname:sub(17)) local pos = minetest.string_to_pos(formname:sub(17))
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
if inv:is_empty("mailbox") then if inv:is_empty("mailbox") then
mailbox.unrent(pos, player) mailbox.unrent(pos, player)
else else
minetest.chat_send_player(player:get_player_name(), "Your mailbox is not empty!") minetest.chat_send_player(player:get_player_name(), "Your mailbox is not empty!")
end end
end end
if fields.books_only then if fields.books_only then
local pos = minetest.string_to_pos(formname:sub(17)) local pos = minetest.string_to_pos(formname:sub(17))
@ -93,6 +93,7 @@ mailbox.after_place_node = function(pos, placer, _)
local inv = meta:get_inventory() local inv = meta:get_inventory()
inv:set_size("mailbox", 8*4) inv:set_size("mailbox", 8*4)
inv:set_size("drop", 1) inv:set_size("drop", 1)
end end
mailbox.on_rightclick_free = function(pos, _, clicker, _) mailbox.on_rightclick_free = function(pos, _, clicker, _)
@ -138,13 +139,14 @@ mailbox.can_dig = function(pos, player)
end end
mailbox.on_metadata_inventory_put = function(pos, listname, index, stack, player) mailbox.on_metadata_inventory_put = function(pos, listname, index, stack, player)
if listname == "drop" then local meta = minetest.get_meta(pos)
local inv = minetest.get_meta(pos):get_inventory() if listname == "drop" then
if inv:room_for_item("mailbox", stack) then local inv = meta:get_inventory()
if inv:room_for_item("mailbox", stack) then
inv:remove_item("drop", stack) inv:remove_item("drop", stack)
inv:add_item("mailbox", stack) inv:add_item("mailbox", stack)
end end
end end
end end
mailbox.allow_metadata_inventory_put = function(pos, listname, index, stack, player) mailbox.allow_metadata_inventory_put = function(pos, listname, index, stack, player)
@ -155,6 +157,7 @@ mailbox.allow_metadata_inventory_put = function(pos, listname, index, stack, pla
end end
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local owner = meta:get_string("owner")
local inv = meta:get_inventory() local inv = meta:get_inventory()
if inv:room_for_item("mailbox", stack) then if inv:room_for_item("mailbox", stack) then
return -1 return -1
@ -166,6 +169,25 @@ mailbox.allow_metadata_inventory_put = function(pos, listname, index, stack, pla
return 0 return 0
end end
mailbox.allow_metadata_inventory_take = function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos)
local name = player:get_player_name()
if not meta:get_string("owner") == name and not minetest.check_player_privs(name, {protection_bypass=true,}) then
return 0
end
return stack:get_count()
end
mailbox.allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
local meta = minetest.get_meta(pos)
local name = player:get_player_name()
if meta:get_string("owner") == name or minetest.check_player_privs(name, {protection_bypass=true,}) then
return count
end
return 0
end
minetest.register_node("mailbox:mailbox", { minetest.register_node("mailbox:mailbox", {
description = "Mailbox", description = "Mailbox",
@ -183,6 +205,8 @@ minetest.register_node("mailbox:mailbox", {
can_dig = mailbox.can_dig, can_dig = mailbox.can_dig,
on_metadata_inventory_put = mailbox.on_metadata_inventory_put, on_metadata_inventory_put = mailbox.on_metadata_inventory_put,
allow_metadata_inventory_put = mailbox.allow_metadata_inventory_put, allow_metadata_inventory_put = mailbox.allow_metadata_inventory_put,
allow_metadata_invenory_take = mailbox.allow_metadata_inventory_take,
allow_metadata_invenory_move = mailbox.allow_metadata_inventory_move,
}) })
minetest.register_node("mailbox:mailbox_free", { minetest.register_node("mailbox:mailbox_free", {
@ -202,7 +226,6 @@ minetest.register_node("mailbox:mailbox_free", {
can_dig = mailbox.can_dig, can_dig = mailbox.can_dig,
-- on_metadata_inventory_put = mailbox.on_metadata_inventory_put, -- on_metadata_inventory_put = mailbox.on_metadata_inventory_put,
-- allow_metadata_inventory_put = mailbox.allow_metadata_inventory_put, -- allow_metadata_inventory_put = mailbox.allow_metadata_inventory_put,
}) })
@ -224,6 +247,8 @@ minetest.register_node("mailbox:letterbox", {
can_dig = mailbox.can_dig, can_dig = mailbox.can_dig,
on_metadata_inventory_put = mailbox.on_metadata_inventory_put, on_metadata_inventory_put = mailbox.on_metadata_inventory_put,
allow_metadata_inventory_put = mailbox.allow_metadata_inventory_put, allow_metadata_inventory_put = mailbox.allow_metadata_inventory_put,
allow_metadata_invenory_take = mailbox.allow_metadata_inventory_take,
allow_metadata_invenory_move = mailbox.allow_metadata_inventory_move,
}) })
minetest.register_tool("mailbox:unrenter", { minetest.register_tool("mailbox:unrenter", {

3
mod.conf Normal file
View File

@ -0,0 +1,3 @@
name=mailbox
description=Mailbox mod
depends=default