mirror of
https://github.com/kepler155c/opus
synced 2025-07-03 18:42:52 +00:00

Prevent labels from having .., /, *, control characters, or quotes (this generally messes things up) and limit them to a reasonable length. We might possibly also want to do this in snmp.lua, I'm not sure if that will break things though
93 lines
1.9 KiB
Lua
93 lines
1.9 KiB
Lua
local Event = require('opus.event')
|
|
local Socket = require('opus.socket')
|
|
|
|
local fs = _G.fs
|
|
|
|
local fileUid = 0
|
|
local fileHandles = { }
|
|
|
|
local function remoteOpen(fn, fl)
|
|
local fh = fs.open(fn, fl)
|
|
if fh then
|
|
local methods = { 'close', 'write', 'writeLine', 'flush', 'read', 'readLine', 'readAll', }
|
|
fileUid = fileUid + 1
|
|
fileHandles[fileUid] = fh
|
|
|
|
local vfh = {
|
|
methods = { },
|
|
fileUid = fileUid,
|
|
}
|
|
|
|
for _,m in ipairs(methods) do
|
|
if fh[m] then
|
|
table.insert(vfh.methods, m)
|
|
end
|
|
end
|
|
return vfh
|
|
end
|
|
end
|
|
|
|
local function remoteFileOperation(fileId, op, ...)
|
|
local fh = fileHandles[fileId]
|
|
if fh then
|
|
return fh[op](...)
|
|
end
|
|
end
|
|
|
|
local function sambaConnection(socket)
|
|
while true do
|
|
local msg = socket:read()
|
|
if not msg then
|
|
break
|
|
end
|
|
local fn = fs[msg.fn]
|
|
if msg.fn == 'open' then
|
|
fn = remoteOpen
|
|
elseif msg.fn == 'fileOp' then
|
|
fn = remoteFileOperation
|
|
end
|
|
local ret
|
|
local s, m = pcall(function()
|
|
ret = fn(table.unpack(msg.args))
|
|
end)
|
|
if not s and m then
|
|
_G.printError('samba: ' .. m)
|
|
end
|
|
socket:write({ response = ret })
|
|
end
|
|
|
|
print('samba: Connection closed')
|
|
end
|
|
|
|
local function sanitizeLabel(computer)
|
|
return (computer.id.."_"..computer.label:gsub("[%c%.\"'/%*]", "")):sub(64)
|
|
end
|
|
|
|
Event.addRoutine(function()
|
|
print('samba: listening on port 139')
|
|
|
|
while true do
|
|
local socket = Socket.server(139)
|
|
|
|
Event.addRoutine(function()
|
|
print('samba: connection from ' .. socket.dhost)
|
|
local s, m = pcall(sambaConnection, socket)
|
|
print('samba: closing connection to ' .. socket.dhost)
|
|
socket:close()
|
|
if not s and m then
|
|
print('Samba error')
|
|
_G.printError(m)
|
|
end
|
|
end)
|
|
end
|
|
end)
|
|
|
|
Event.on('network_attach', function(_, computer)
|
|
fs.mount(fs.combine('network', sanitizeLabel(computer)), 'netfs', computer.id)
|
|
end)
|
|
|
|
Event.on('network_detach', function(_, computer)
|
|
print('samba: detaching ' .. sanitizeLabel(computer))
|
|
fs.unmount(fs.combine('network', sanitizeLabel(computer)))
|
|
end)
|