From 0e01e887f09fa3ebae4674945e581b79ee1c4a30 Mon Sep 17 00:00:00 2001 From: Ted Trask Date: Thu, 9 Jun 2016 15:52:19 +0000 Subject: Changes to connectedpeers since musl does not support utmp, making it impossible to get session data (cherry picked from commit 585dc0a805df564d954703a1354ef928d7f0156a) --- openssh-connectedpeers-html.lsp | 4 --- openssh-model.lua | 55 ++++++++++++++++++++--------------------- 2 files changed, 27 insertions(+), 32 deletions(-) diff --git a/openssh-connectedpeers-html.lsp b/openssh-connectedpeers-html.lsp index 363d726..7ee8fd9 100644 --- a/openssh-connectedpeers-html.lsp +++ b/openssh-connectedpeers-html.lsp @@ -23,8 +23,6 @@ for k,v in pairs(data.value) do io.write("") io.write("\n") io.write("\n") - io.write("\n") - io.write("\n") io.write("
Session user:".. html.html_escape(v.tty[i].user) .. "
Session TTY:".. html.html_escape(v.tty[i].tty) .. "
Session Started:".. html.html_escape(v.tty[i].time) .. "
Session Idle:".. html.html_escape(v.tty[i].idle) .. "
") io.write("\n") @@ -35,8 +33,6 @@ for k,v in pairs(data.value) do io.write("") io.write("\n") io.write("\n") - io.write("\n") - io.write("\n") io.write("\n") io.write("
Session user:No records
Session TTY:No records
Session Started:No records
Session Idle:No records
Other:This could be a sshfs session
") diff --git a/openssh-model.lua b/openssh-model.lua index 17b45be..5481b16 100644 --- a/openssh-model.lua +++ b/openssh-model.lua @@ -127,51 +127,50 @@ function mymodule.list_conn_peers() local output = cfe({ type="structure", value={}, label="Connected peers" }) local netstat = {} local ps = {} - local who = {} config = mymodule.read_config() - local f = modelfunctions.run_executable({"netstat", "-lnaW"}) + + local f = modelfunctions.run_executable({"ps"}) + for i,line in ipairs(format.search_for_lines(f, "sshd:")) do + local pid, user, tty = string.match(line, "^%s*(%d+)%s+%S+%s+%S+%s+%S+%s+([^@ ]+)@?(%S*)") + if pid then + ps[pid] = {user=user, tty=tty} + end + end + + f = modelfunctions.run_executable({"netstat", "-tnp"}) local flines = format.search_for_lines(f, "ESTABLISHED") - local g = modelfunctions.run_executable({"netstat", "-laW"}) + local g = modelfunctions.run_executable({"netstat", "-t"}) local glines = format.search_for_lines(g, "ESTABLISHED") for i,line in ipairs(flines) do - local loc, peer = string.match(line, "^%S+%s+%S+%s+%S+%s+(%S+)%s+(%S+)") + local loc, peer, pid = string.match(line, "^%S+%s+%S+%s+%S+%s+(%S+)%s+(%S+)%s+%S+%s+(%d+)") if loc then peer = string.match(peer, "%d+%.%d+%.%d+%.%d+") if string.find(loc, ":"..config.value.Port.value.."$") and peer then if not netstat[peer] then local name = string.match(glines[i], "^%S+%s+%S+%s+%S+%s+%S+%s+(%S+)") name = string.gsub(name, ":.*", "") - netstat[peer] = {cnt=0, name=name} + netstat[peer] = {cnt=0, name=name, tty={}} end netstat[peer].cnt = netstat[peer].cnt + 1 + if ps[pid] then + -- For root, the pid will match and contain the tty + -- For other users, we will find the tty in another process soon after + netstat[peer].tty[#netstat[peer].tty+1] = {user=ps[pid].user, tty=ps[pid].tty} + if ps[pid].tty == "" then + for j=tostring(pid)+1, tostring(pid)+5 do + local p = tostring(j) + if ps[p] and ps[p].user == ps[pid].user then + netstat[peer].tty[#netstat[peer].tty].tty = ps[p].tty + break + end + end + end + end end end end - f = modelfunctions.run_executable({"ps"}) - for i,line in ipairs(format.search_for_lines(f, "sshd:")) do - table.insert(ps, string.match(line,"@(%S+)")) - end - - local who = modelfunctions.run_executable({"who"}) for peer,v in pairs(netstat) do - if not (netstat[peer].tty) then netstat[peer].tty = {} end - for line in string.gmatch(who, "[^\n]+") do - if string.find(line, peer) or (v.name ~= "" and string.find(line, v.name)) then - for j,l in ipairs(ps) do - if string.find(line, l) then - local user,tty,idle,time = string.match(line, "^(%S*)%s*(%S*)%s*(%S*)%s*(%S*%s*%S*%s*%S*)") - table.insert(netstat[peer].tty, { - user=user, - tty=tty, - idle=idle, - time=time, - }) - break - end - end - end - end output.value[#output.value+1] = v output.value[#output.value].host = peer end -- cgit v1.2.3