2021-01-12 23:11:00 +11:00
-- Copyright (C) 2018-2021 by KittenOS NEO contributors
--
-- Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.
--
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
-- THIS SOFTWARE.
2018-03-19 10:10:54 +11:00
-- CRITICAL FILE!
-- This file defines how your KittenOS NEO system responds to access requests.
-- Modification, renaming or deletion can disable security features.
-- Usually, a change that breaks the ability for the file to do it's job will cause the "failsafe" to activate,
-- and for the system to become unable to run user applications.
-- However - I would not like to test this in a situation where said user applications were in any way untrusted,
-- for example, if you downloaded them from the Internet, or in particular if someone forwarded them over Discord.
-- IRC is usually pretty safe, but no guarantees.
-- Returns "allow", "deny", or "ask".
2020-04-02 09:21:36 +11:00
local function actualPolicy ( pkg , pid , perm , pkgSvcPfx )
2018-03-19 10:10:54 +11:00
-- System stuff is allowed.
if pkg : sub ( 1 , 4 ) == " sys- " then
return " allow "
end
2020-04-02 09:21:36 +11:00
-- svc-t's job is solely to emulate terminals
-- TO INSTALL YOUR OWN TERMINAL EMULATOR:
-- perm|app-yourterm|r.neo.t
if pkg == " svc-t " and perm == " r.neo.pub.t " then
return " allow "
end
2018-03-19 10:10:54 +11:00
-- <The following is for apps & services>
2020-04-02 09:21:36 +11:00
-- x.neo.pub.* is open to all
2018-03-19 10:10:54 +11:00
if perm : sub ( 1 , 10 ) == " x.neo.pub. " then
return " allow "
end
2018-04-07 06:05:47 +10:00
-- These signals are harmless, though they identify HW (as does everything in OC...)
2019-01-03 02:27:23 +11:00
if perm == " s.h.component_added " or perm == " s.h.component_removed " or perm == " s.h.tablet_use " or perm == " c.tablet " then
2018-04-07 06:05:47 +10:00
return " allow "
end
2020-04-02 09:21:36 +11:00
-- Userlevel can register for itself
if perm == " r. " .. pkgSvcPfx then
2018-04-09 09:04:40 +10:00
return " allow "
2018-03-19 10:10:54 +11:00
end
-- Userlevel has no other registration rights
if perm : sub ( 1 , 2 ) == " r. " then
return " deny "
end
2018-04-09 09:04:40 +10:00
-- app/svc stuff is world-accessible,
-- but note perm|*| overrides this
2018-03-19 10:10:54 +11:00
if perm : sub ( 1 , 6 ) == " x.app. " then
return " allow "
end
if perm : sub ( 1 , 6 ) == " x.svc. " then
return " allow "
end
-- For hardware access, ASK!
return " ask "
end
2020-04-02 09:21:36 +11:00
return function ( nexus , settings , pkg , pid , perm , rsp , pkgSvcPfx )
local res = actualPolicy ( pkg , pid , perm , pkgSvcPfx )
2018-04-09 09:04:40 +10:00
if settings then
res = settings.getSetting ( " perm| " .. pkg .. " | " .. perm ) or
2018-04-24 07:18:18 +10:00
settings.getSetting ( " perm|*| " .. perm ) or res
2018-03-19 10:10:54 +11:00
end
2018-03-30 22:36:48 +11:00
if res == " ask " and nexus then
local totalW = 3 + 6 + 2 + 8
local fmt = require ( " fmttext " ) . fmtText ( unicode.safeTextFormat ( string.format ( " %s/%i wants: \n %s \n Allow this? \n \n " , pkg , pid , perm ) ) , totalW )
local buttons = {
{ " <No> " , function ( w )
2018-03-19 10:10:54 +11:00
rsp ( false )
2018-04-26 07:57:25 +10:00
nexus.windows [ w.id ] = nil
w.close ( )
2018-03-30 22:36:48 +11:00
end } ,
{ " <Always> " , function ( w )
2018-03-19 14:08:09 +11:00
if settings then
settings.setSetting ( " perm| " .. pkg .. " | " .. perm , " allow " )
end
2018-03-19 10:10:54 +11:00
rsp ( true )
2018-04-26 07:57:25 +10:00
nexus.windows [ w.id ] = nil
w.close ( )
2018-03-30 22:36:48 +11:00
end } ,
{ " <Yes> " , function ( w )
rsp ( true )
2018-04-26 07:57:25 +10:00
nexus.windows [ w.id ] = nil
w.close ( )
2018-03-30 22:36:48 +11:00
end }
}
2018-04-26 07:57:25 +10:00
local cButton = 0
nexus.create ( totalW , # fmt , " security " , function ( window , ev , a , b , c )
while ev do
2018-03-30 22:36:48 +11:00
if ev == " line " or ev == " touch " then
local cor = b
2018-04-26 07:57:25 +10:00
local iev = ev
ev = nil
if iev == " line " then
2018-03-30 22:36:48 +11:00
cor = a
if fmt [ a ] then
window.span ( 1 , a , fmt [ a ] , 0xFFFFFF , 0 )
end
end
if cor == # fmt then
local x = 1
for k , v in ipairs ( buttons ) do
2018-04-26 07:57:25 +10:00
if iev == " line " then
2018-03-30 22:36:48 +11:00
if k ~= cButton + 1 then
window.span ( x , a , v [ 1 ] , 0xFFFFFF , 0 )
else
window.span ( x , a , v [ 1 ] , 0 , 0xFFFFFF )
end
elseif a >= x and a < ( x + # v [ 1 ] ) then
cButton = k - 1
ev = " key "
a = 32
b = 0
c = true
break
end
x = x + # v [ 1 ] + 1
end
end
elseif ev == " close " then
rsp ( false )
2018-04-27 01:13:42 +10:00
nexus.windows [ window.id ] = nil
window.close ( )
2018-04-26 07:57:25 +10:00
ev = nil
elseif ev == " key " then
2018-03-30 22:36:48 +11:00
if c and ( a == 9 or b == 205 ) then
cButton = ( cButton + 1 ) % # buttons
ev = " line "
a = # fmt
elseif c and b == 203 then
cButton = ( cButton - 1 ) % # buttons
ev = " line "
a = # fmt
elseif c and ( a == 13 or a == 32 ) then
buttons [ cButton + 1 ] [ 2 ] ( window )
ev = nil
else
ev = nil
end
else
ev = nil
end
end
end )
2018-03-19 10:10:54 +11:00
else
rsp ( res == " allow " )
end
end