confd: migrate infix-system:motd -> infix-system:motd-banner

Inspired by openconfig-system[1], except the infix-system type is binary
to allow encoding an entire file, with optional control characters.

The openconfig-system model also carries login-banner, which is displayed
before login.  This could be added later to infix-system to let the user
set an OpenSSH login Banner.

[1]:  https://openconfig.net/projects/models/schemadocs/yangdoc/openconfig-system.html

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2024-03-01 10:04:19 +01:00
parent 87bffb3816
commit 9847a8fdd1
4 changed files with 85 additions and 4 deletions
+1 -1
View File
@@ -225,7 +225,7 @@ sysrepoctl -s $SEARCH \
-i infix-lldp@2023-08-23.yang -g wheel -p 0660 \
-i infix-dhcp-client@2024-01-30.yang -g wheel -p 0660 \
-i infix-shell-type@2023-08-21.yang -g wheel -p 0660 \
-i infix-system@2023-10-19.yang -g wheel -p 0660 \
-i infix-system@2024-02-29.yang -g wheel -p 0660 \
-i infix-services@2023-10-16.yang -g wheel -p 0660 \
-i ieee802-ethernet-interface@2019-06-21.yang -g wheel -p 0660 \
-i infix-ethernet-interface@2024-02-27.yang -g wheel -p 0660 \
+1 -1
View File
@@ -6,7 +6,7 @@ if [ -f /etc/motd ]; then
cat <<EOF
{
"ietf-system:system": {
"infix-system:motd": "$(sed ':a;N;$!ba;s/\n/\\n/g;s/\t/\\t/g' /etc/motd)"
"infix-system:motd-banner": "$(base64 -w0 /etc/motd)"
}
}
EOF
+54 -1
View File
@@ -13,6 +13,7 @@
#include <srx/srx_module.h>
#include <srx/srx_val.h>
#include "base64.h"
#include "core.h"
#define XPATH_BASE_ "/ietf-system:system"
@@ -1028,12 +1029,63 @@ static int change_motd(sr_session_ctx_t *session, uint32_t sub_id, const char *m
if (message) {
rc = writesf(message, "w", "%s", fn);
free(message);
}
if (rc) {
ERRNO("failed saving %s", fn);
return SR_ERR_SYS;
}
return SR_ERR_OK;
}
static int change_motd_banner(sr_session_ctx_t *session, uint32_t sub_id, const char *module,
const char *xpath, sr_event_t event, unsigned request_id, void *priv)
{
const char *fn = "/etc/motd";
unsigned char *raw, *txt;
size_t txt_len;
char *legacy;
int rc = 0;
/* Ignore all events except SR_EV_DONE */
if (event != SR_EV_DONE)
return SR_ERR_OK;
legacy = srx_get_str(session, "/ietf-system:system/infix-sys:motd");
if (legacy) {
NOTE("Legacy /system/motd exists, skipping %s", xpath);
free(legacy);
return SR_ERR_OK;
}
raw = (unsigned char *)srx_get_str(session, "%s", xpath);
if (raw) {
txt = base64_decode(raw, strlen((char *)raw), &txt_len);
if (!txt) {
ERRNO("failed base64 decoding of %s", xpath);
rc = -1;
} else {
FILE *fp = fopen(fn, "w");
if (!fp) {
rc = -1;
} else {
if (fwrite(txt, txt_len, sizeof(txt[0]), fp) != txt_len)
rc = -1;
fclose(fp);
}
free(txt);
}
free(raw);
} else {
(void)remove(fn);
}
if (rc) {
ERROR("failed writing /etc/motd: %s", strerror(errno));
ERRNO("failed saving %s", fn);
return SR_ERR_SYS;
}
@@ -1137,6 +1189,7 @@ int ietf_system_init(struct confd *confd)
REGISTER_CHANGE(confd->session, "ietf-system", XPATH_AUTH_, 0, change_auth, confd, &confd->sub);
REGISTER_CHANGE(confd->session, "ietf-system", XPATH_BASE_"/hostname", 0, change_hostname, confd, &confd->sub);
REGISTER_CHANGE(confd->session, "ietf-system", XPATH_BASE_"/infix-system:motd", 0, change_motd, confd, &confd->sub);
REGISTER_CHANGE(confd->session, "ietf-system", XPATH_BASE_"/infix-system:motd-banner", 0, change_motd_banner, confd, &confd->sub);
REGISTER_CHANGE(confd->session, "ietf-system", XPATH_BASE_"/clock", 0, change_clock, confd, &confd->sub);
REGISTER_CHANGE(confd->session, "ietf-system", XPATH_BASE_"/ntp", 0, change_ntp, confd, &confd->sub);
REGISTER_CHANGE(confd->session, "ietf-system", XPATH_BASE_"/dns-resolver", 0, change_dns, confd, &confd->sub);
@@ -19,6 +19,11 @@ module infix-system {
contact "kernelkit@googlegroups.com";
description "Infix augments and deviations to ietf-system.";
revision 2024-02-29 {
description "Mark infix-sys:motd as deprecated, to be replaced with type binary.";
reference "internal";
}
revision 2023-10-19 {
description "Change deviation for timezone-utc-offset from unsupported to Etc+/-HOUR
- Unit is set to hours (tzdata compatibility)
@@ -26,6 +31,7 @@ module infix-system {
reference "internal";
}
revision 2023-08-15 {
description "Add support for user login shell.
@@ -60,9 +66,31 @@ module infix-system {
augment "/sys:system" {
description "Augment of ietf-system for modifying /etc/motd.";
leaf motd {
description "Legacy MotD (Message of the Day), shown after login.
Please note, this is obsolete! When set it overrides the
'motd-banner' setting for backwards compatibility.
Instead, use 'motd-banner', which takes a base64 encoded
text file as an argument. For CLI users, edit with the
'text-editor motd-banner' command.";
status obsolete; // Replaced with motd-banner (binary)
type string;
description "Set the MotD (Message of the Day), shown after login";
}
// From openconfig-system, which also has login-banner (TODO)
leaf motd-banner {
description "Message of the Day (MotD), shown after SSH/console login.
Base64 encoded (binary) file contents for the system file
/etc/motd, displayed after SSH/console login.
They system may append additional standard information such
as the current system date and time, uptime, last login
timestamp, etc.";
type binary;
}
}