diff --git a/ffmuc-custom-banner/Makefile b/ffmuc-custom-banner/Makefile new file mode 100644 index 00000000..1a1b2c26 --- /dev/null +++ b/ffmuc-custom-banner/Makefile @@ -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))) diff --git a/ffmuc-custom-banner/README.md b/ffmuc-custom-banner/README.md new file mode 100644 index 00000000..f2f9ae6a --- /dev/null +++ b/ffmuc-custom-banner/README.md @@ -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. diff --git a/ffmuc-custom-banner/files/etc/banner.gluon b/ffmuc-custom-banner/files/etc/banner.gluon new file mode 100644 index 00000000..952fc32d --- /dev/null +++ b/ffmuc-custom-banner/files/etc/banner.gluon @@ -0,0 +1,9 @@ + ________ + / ____/ /_ ______ ____ + / / __/ / / / / __ \/ __ \ + / /_/ / / /_/ / /_/ / / / / + \____/_/\__,_/\____/_/ /_/ +----------------------------- + %SITE_NAME% %FIRMWARE_VERSION% + %MODEL% +----------------------------- diff --git a/ffmuc-custom-banner/luasrc/lib/gluon/upgrade/520-custom-banner b/ffmuc-custom-banner/luasrc/lib/gluon/upgrade/520-custom-banner new file mode 100755 index 00000000..ff4ca667 --- /dev/null +++ b/ffmuc-custom-banner/luasrc/lib/gluon/upgrade/520-custom-banner @@ -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)