From 70733ab4f2d6171925ad2a9725dab09160966072 Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Mon, 8 Apr 2019 09:30:47 -0400 Subject: [PATCH] network group wip + virtual dirs + better trust --- sys/apis/security.lua | 16 ++++++---------- sys/apis/socket.lua | 35 ++++++++++++++++++++++++----------- sys/apps/Welcome.lua | 24 +++++++++++++++++++----- sys/apps/network/snmp.lua | 1 + sys/apps/telnet.lua | 17 ++++++++++++++--- sys/apps/vnc.lua | 11 ++++++++++- sys/init/2.vfs.lua | 9 +++++++++ sys/init/5.network.lua | 10 +++++++++- 8 files changed, 92 insertions(+), 31 deletions(-) diff --git a/sys/apis/security.lua b/sys/apis/security.lua index 24d24d4..ab0fea8 100644 --- a/sys/apis/security.lua +++ b/sys/apis/security.lua @@ -1,20 +1,18 @@ local Config = require('config') -local config = { } - local Security = { } function Security.verifyPassword(password) - Config.load('os', config) - return config.password and password == config.password + local current = Security.getPassword() + return current and password == current end function Security.hasPassword() - return not not config.password + return not not Security.getPassword() end function Security.getSecretKey() - Config.load('os', config) + local config = Config.load('os') if not config.secretKey then config.secretKey = math.random(100000, 999999) Config.update('os', config) @@ -23,7 +21,6 @@ function Security.getSecretKey() end function Security.getPublicKey() - local exchange = { base = 11, primeMod = 625210769 @@ -47,14 +44,13 @@ function Security.getPublicKey() end function Security.updatePassword(password) - Config.load('os', config) + local config = Config.load('os') config.password = password Config.update('os', config) end function Security.getPassword() - Config.load('os', config) - return config.password + return Config.load('os').password end return Security diff --git a/sys/apis/socket.lua b/sys/apis/socket.lua index 75ae3a3..fd73abd 100644 --- a/sys/apis/socket.lua +++ b/sys/apis/socket.lua @@ -105,7 +105,7 @@ end function Socket.connect(host, port) if not device.wireless_modem then - return false, 'Wireless modem not found' + return false, 'Wireless modem not found', 'NOMODEM' end local socket = newSocket(host == os.getComputerID()) @@ -138,15 +138,19 @@ function Socket.connect(host, port) _G.transport.open(socket) return socket + elseif msg.type == 'NOPASS' then + socket:close() + return false, 'Password not set on target', 'NOPASS' + elseif msg.type == 'REJE' then socket:close() - return false, 'Password not set on target or not trusted' + return false, 'Trust not established', 'NOTRUST' end end until e == 'timer' and id == timerId socket:close() - return false, 'Connection timed out' + return false, 'Connection timed out', 'TIMEOUT' end local function trusted(msg, port) @@ -190,7 +194,15 @@ function Socket.server(port) socket.wseq = msg.wseq socket.rseq = msg.rseq - if trusted(msg, port) then + if not Security.hasPassword() then + socket.transmit(socket.dport, socket.sport, { + type = 'NOPASS', + dhost = socket.dhost, + shost = socket.shost, + }) + socket:close() + + elseif trusted(msg, port) then socket.connected = true socket.transmit(socket.dport, socket.sport, { type = 'CONN', @@ -201,14 +213,15 @@ function Socket.server(port) _G.transport.open(socket) return socket - end - socket.transmit(socket.dport, socket.sport, { - type = 'REJE', - dhost = socket.dhost, - shost = socket.shost, - }) - socket:close() + else + socket.transmit(socket.dport, socket.sport, { + type = 'REJE', + dhost = socket.dhost, + shost = socket.shost, + }) + socket:close() + end end end end diff --git a/sys/apps/Welcome.lua b/sys/apps/Welcome.lua index 0d190b9..c5d5e41 100644 --- a/sys/apps/Welcome.lua +++ b/sys/apps/Welcome.lua @@ -1,4 +1,5 @@ local Ansi = require('ansi') +local Config = require('config') local Security = require('security') local SHA1 = require('sha1') local UI = require('ui') @@ -53,7 +54,7 @@ local page = UI.Page { }, password = UI.WizardPage { index = 3, - labelText = UI.Text { + passwordLabel = UI.Text { x = 3, y = 2, value = 'Password' }, @@ -62,14 +63,22 @@ local page = UI.Page { limit = 32, mask = true, shadowText = 'password', - accelerators = { - enter = 'new_password', - }, }, +--[[ + groupLabel = UI.Text { + x = 3, y = 3, + value = 'Group' + }, + group = UI.TextEntry { + x = 12, ex = -3, y = 3, + limit = 32, + shadowText = 'network group', + }, +]] intro = UI.TextArea { textColor = colors.yellow, inactive = true, - x = 3, ex = -3, y = 4, ey = -3, + x = 3, ex = -3, y = 5, ey = -3, value = string.format(passwordIntro, Ansi.white), }, }, @@ -101,6 +110,11 @@ function page.wizard.pages.password:validate() if #self.newPass.value > 0 then Security.updatePassword(SHA1.sha1(self.newPass.value)) end + if #self.group.value > 0 then + local config = Config.load('os') + config.group = self.group.value + Config.update('os', config) + end return true end diff --git a/sys/apps/network/snmp.lua b/sys/apps/network/snmp.lua index 07ccc0a..aa25151 100644 --- a/sys/apps/network/snmp.lua +++ b/sys/apps/network/snmp.lua @@ -141,6 +141,7 @@ local function sendInfo() infoTimer = os.clock() info.label = os.getComputerLabel() info.uptime = math.floor(os.clock()) + info.group = network.getGroup() if turtle then info.fuel = turtle.getFuelLevel() info.status = turtle.getStatus() diff --git a/sys/apps/telnet.lua b/sys/apps/telnet.lua index 8a736a3..3f5dd84 100644 --- a/sys/apps/telnet.lua +++ b/sys/apps/telnet.lua @@ -24,10 +24,21 @@ if multishell then multishell.setTitle(multishell.getCurrent(), 'Telnet ' .. remoteId) end -local socket, msg = Socket.connect(remoteId, 23) +local socket, msg, reason -if not socket then - error(msg) +while true do + socket, msg, reason = Socket.connect(remoteId, 23) + + if socket then + break + elseif reason ~= 'NOTRUST' then + error(msg) + end + + local s, m = shell.run('trust ' .. remoteId) + if not s then + error(m) + end end local ct = Util.shallowCopy(term.current()) diff --git a/sys/apps/vnc.lua b/sys/apps/vnc.lua index 0540125..b57df3b 100644 --- a/sys/apps/vnc.lua +++ b/sys/apps/vnc.lua @@ -6,6 +6,7 @@ local Util = require('util') local colors = _G.colors local multishell = _ENV.multishell local os = _G.os +local shell = _ENV.shell local term = _G.term local remoteId @@ -26,7 +27,15 @@ if multishell then end local function connect() - local socket, msg = Socket.connect(remoteId, 5900) + local socket, msg, reason = Socket.connect(remoteId, 5900) + + if reason == 'NOTRUST' then + local s, m = shell.run('trust ' .. remoteId) + if not s then + return s, m + end + socket, msg = Socket.connect(remoteId, 5900) + end if not socket then return false, msg diff --git a/sys/init/2.vfs.lua b/sys/init/2.vfs.lua index d80474c..b32f6b4 100644 --- a/sys/init/2.vfs.lua +++ b/sys/init/2.vfs.lua @@ -5,6 +5,8 @@ end _G.requireInjector(_ENV) local Util = require('util') +-- TODO: support getDrive for virtual nodes + local fs = _G.fs fs.native = Util.shallowCopy(fs) @@ -88,6 +90,13 @@ function nativefs.exists(node, dir) return fs.native.exists(dir) end +function nativefs.getDrive(node, dir) + if node.mountPoint == dir then + return fs.native.getDrive(dir) or 'virt' + end + return fs.native.getDrive(dir) +end + function nativefs.delete(node, dir) if node.mountPoint == dir then fs.unmount(dir) diff --git a/sys/init/5.network.lua b/sys/init/5.network.lua index 417126e..bd97122 100644 --- a/sys/init/5.network.lua +++ b/sys/init/5.network.lua @@ -6,7 +6,15 @@ local device = _G.device local kernel = _G.kernel local os = _G.os -_G.network = { } +do + local config = Config.load('os') + _G.network = setmetatable({ }, { __index = { + getGroup = function() return config.group end, + setGroup = function(name) + config.group = name + end + }}) +end local function startNetwork() kernel.run({