Skip to content

Commit

Permalink
ffmuc-custom-banner: add new package
Browse files Browse the repository at this point in the history
  • Loading branch information
grische committed Jun 7, 2024
1 parent bd78c4c commit 8578f5d
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
15 changes: 15 additions & 0 deletions ffmuc-custom-banner/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
include $(TOPDIR)/rules.mk

PKG_NAME:=ffmuc-custom-banner
PKG_VERSION:=1
PKG_RELEASE:=1
PKG_LICENSE:=MIT

include $(TOPDIR)/../package/gluon.mk

define Package/$(PKG_NAME)
TITLE:=Creates a custom SSH login banner
DEPENDS:=+gluon-core
endef

$(eval $(call BuildPackageGluon,$(PKG_NAME)))
5 changes: 5 additions & 0 deletions ffmuc-custom-banner/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# ffmuc-custom banner

This package modifies the /etc/banner that is shown during SSH login.

⚠️ This package will consume an additional 2x the banner size, one for the template and one for the final banner. Make sure to have enough flash size available.
9 changes: 9 additions & 0 deletions ffmuc-custom-banner/files/etc/banner.gluon
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
________
/ ____/ /_ ______ ____
/ / __/ / / / / __ \/ __ \
/ /_/ / / /_/ / /_/ / / / /
\____/_/\__,_/\____/_/ /_/
-----------------------------
%SITE_NAME% %FIRMWARE_VERSION%
%MODEL%
-----------------------------
73 changes: 73 additions & 0 deletions ffmuc-custom-banner/luasrc/lib/gluon/upgrade/520-custom-banner
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/lua

local site = require "gluon.site"
local uci = require("simple-uci").cursor()
local info = require "gluon.info"

local banner_path = "/etc/banner"
local banner_backup_path = "/etc/banner.original"
local banner_template_path = "/etc/banner.gluon"

local function file_exists(path)
local file = io.open(path, "r")

if file ~= nil then
io.close(file)
return true
end

return false
end

local function read_file(filepath)
local fh = assert(io.open(filepath, "rb"))
local contents = assert(fh:read(_VERSION <= "Lua 5.2" and "*a" or "a"))
fh:close()
return contents
end

-- Write a string to a file.
local function write_file(filename, contents)
local fh = assert(io.open(filename, "wb"))
fh:write(contents)
fh:flush()
fh:close()
end

-- Check if banner is "original" by checking the first few lines for the OpenWRT ASCII art
-- https://github.com/openwrt/openwrt/blob/2ded629864de779df8ddd0224a875edf17f9fea5/package/base-files/files/etc/banner
local function is_banner_original(path)
if os.execute("head -n 6 " .. path .. " | md5sum | grep 0e85ae0983251ed04e91a85d9b7c5fe3") == 0 then
return true
end
return false
end

-- restore original banner
if uci:get("custom-banner", "settings", "enabled") == "0" then
if file_exists(banner_backup_path) then
os.rename(banner_backup_path, banner_path)
end
os.exit()
end


-- create a backup of the banner if the active banner is original (e.g. after an upgrade)
if is_banner_original(banner_path) then
-- rename (and overwrite) existing backup
os.rename(banner_path, banner_backup_path)
end

local model = string.gsub(read_file("/tmp/sysinfo/model"), "\n+", "")
local site_name = site.site_name() or "Freifunk"
local gluon_info = info.get_info()
local banner_template = read_file(banner_template_path)

local new_banner = banner_template
new_banner = string.gsub(new_banner, "%%SITE_NAME%%", site_name)
new_banner = string.gsub(new_banner, "%%FIRMWARE_VERSION%%", gluon_info.firmware_release)
new_banner = string.gsub(new_banner, "%%GLUON_VERSION%%", gluon_info.gluon_version)
new_banner = string.gsub(new_banner, "%%MODEL%%", model)

-- TODO: write this file to RAM (and regen on reboot) and link symlink it, to save space
write_file(banner_path, new_banner)

0 comments on commit 8578f5d

Please sign in to comment.