From 7acbb1f07c93d49cfc77173b2119aba02ee20166 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 29 Feb 2024 19:02:03 +0100 Subject: [PATCH 1/3] confd: import base64 encode/decode functions, 3-clause BSD licensed Signed-off-by: Joachim Wiberg --- src/confd/src/Makefile.am | 1 + src/confd/src/base64.c | 210 ++++++++++++++++++++++++++++++++++++++ src/confd/src/base64.h | 11 ++ 3 files changed, 222 insertions(+) create mode 100644 src/confd/src/base64.c create mode 100644 src/confd/src/base64.h diff --git a/src/confd/src/Makefile.am b/src/confd/src/Makefile.am index eacf6a053a..d605546669 100644 --- a/src/confd/src/Makefile.am +++ b/src/confd/src/Makefile.am @@ -23,6 +23,7 @@ confd_plugin_la_LIBADD = \ $(libsrx_LIBS) confd_plugin_la_SOURCES = \ + base64.c base64.h \ core.c core.h \ dagger.c dagger.h \ ietf-interfaces.c \ diff --git a/src/confd/src/base64.c b/src/confd/src/base64.c new file mode 100644 index 0000000000..f1a02ddf40 --- /dev/null +++ b/src/confd/src/base64.c @@ -0,0 +1,210 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ + +#include +#include +#include +#include "base64.h" + +/* + * Base64 encoding/decoding (RFC1341) + * Copyright (c) 2005-2011, Jouni Malinen + */ +static const unsigned char base64_table[65] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +/** + * base64_encode - Base64 encode + * @src: Data to be encoded + * @len: Length of the data to be encoded + * @out_len: Pointer to output length variable, or %NULL if not used + * + * Returns: + * Allocated buffer of out_len bytes of encoded data, %NULL on failure + * + * Caller is responsible for freeing the returned buffer. Returned + * buffer is nul terminated to make it easier to use as a C string. + * The nul terminator is not included in out_len. + */ +unsigned char *base64_encode(const unsigned char *src, size_t len, size_t *out_len) +{ + unsigned char *out, *pos; + const unsigned char *end, *in; + size_t olen; + int line_len; + + olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */ + olen += olen / 72; /* line feeds */ + olen++; /* nul termination */ + if (olen < len) + return NULL; /* integer overflow */ + out = malloc(olen); + if (out == NULL) + return NULL; + + end = src + len; + in = src; + pos = out; + line_len = 0; + while (end - in >= 3) { + *pos++ = base64_table[in[0] >> 2]; + *pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)]; + *pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)]; + *pos++ = base64_table[in[2] & 0x3f]; + in += 3; + line_len += 4; + if (line_len >= 72) { + *pos++ = '\n'; + line_len = 0; + } + } + + if (end - in) { + *pos++ = base64_table[in[0] >> 2]; + if (end - in == 1) { + *pos++ = base64_table[(in[0] & 0x03) << 4]; + *pos++ = '='; + } else { + *pos++ = base64_table[((in[0] & 0x03) << 4) | + (in[1] >> 4)]; + *pos++ = base64_table[(in[1] & 0x0f) << 2]; + } + *pos++ = '='; + line_len += 4; + } + + if (line_len) + *pos++ = '\n'; + + *pos = '\0'; + if (out_len) + *out_len = pos - out; + return out; +} + + +/** + * base64_decode - Base64 decode + * @src: Data to be decoded + * @len: Length of the data to be decoded + * @out_len: Pointer to output length variable + * + * Returns: + * Allocated buffer of out_len bytes of decoded data, or %NULL on failure + * Caller is responsible for freeing the returned buffer. + */ +unsigned char *base64_decode(const unsigned char *src, size_t len, size_t *out_len) +{ + unsigned char dtable[256], *out, *pos, block[4], tmp; + size_t i, count, olen; + int pad = 0; + + memset(dtable, 0x80, 256); + for (i = 0; i < sizeof(base64_table) - 1; i++) + dtable[base64_table[i]] = (unsigned char) i; + dtable['='] = 0; + + count = 0; + for (i = 0; i < len; i++) { + if (dtable[src[i]] != 0x80) + count++; + } + + if (count == 0 || count % 4) { + errno = EINVAL; + return NULL; + } + + olen = count / 4 * 3; + pos = out = malloc(olen); + if (out == NULL) + return NULL; + + count = 0; + for (i = 0; i < len; i++) { + tmp = dtable[src[i]]; + if (tmp == 0x80) + continue; + + if (src[i] == '=') + pad++; + block[count] = tmp; + count++; + if (count == 4) { + *pos++ = (block[0] << 2) | (block[1] >> 4); + *pos++ = (block[1] << 4) | (block[2] >> 2); + *pos++ = (block[2] << 6) | block[3]; + count = 0; + if (pad) { + if (pad == 1) + pos--; + else if (pad == 2) + pos -= 2; + else { + /* Invalid padding */ + free(out); + return NULL; + } + break; + } + } + } + + if (out_len) + *out_len = pos - out; + return out; +} + +#ifdef UNITTEST +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + int c, decode = 0, bytes; + char *buf, *ptr; + struct stat st; + size_t len; + FILE *fp; + + while ((c = getopt(argc, argv, "dh")) != EOF) { + switch (c) { + case 'd': + decode = 1; + break; + case 'h': + default: + printf("%s [-d] file", argv[0]); + return 0; + } + } + + if (argc <= optind) + errx(1, "Missing file arg."); + + fp = fopen(argv[optind], "r"); + if (!fp) + err(1, "failed opening %s", argv[optind]); + if (fstat(fileno(fp), &st)) + err(1, "failed fstat %s", argv[optind]); + len = st.st_size; + buf = malloc(len); + + bytes = fread(buf, 1, len, fp); + if (bytes != (int)len) + err(1, "failed reading entire file"); + + if (decode) + ptr = base64_decode(buf, bytes, NULL); + else + ptr = base64_encode(buf, bytes, NULL); + + puts(ptr); + free(ptr); + fclose(fp); + + return 0; +} +#endif diff --git a/src/confd/src/base64.h b/src/confd/src/base64.h new file mode 100644 index 0000000000..829091a63e --- /dev/null +++ b/src/confd/src/base64.h @@ -0,0 +1,11 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ + +#ifndef BASE64_H_ +#define BASE64_H_ + +#include + +unsigned char *base64_encode(const unsigned char *src, size_t len, size_t *out_len); +unsigned char *base64_decode(const unsigned char *src, size_t len, size_t *out_len); + +#endif /* BASE64_H_ */ From e5c1aa2b69400824b4b4070737924642152c209b Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 29 Feb 2024 19:03:10 +0100 Subject: [PATCH 2/3] 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 --- src/confd/bin/bootstrap | 2 +- src/confd/bin/gen-motd | 2 +- src/confd/src/ietf-system.c | 55 ++++++++++++++++++- ...0-19.yang => infix-system@2024-02-29.yang} | 30 +++++++++- 4 files changed, 85 insertions(+), 4 deletions(-) rename src/confd/yang/{infix-system@2023-10-19.yang => infix-system@2024-02-29.yang} (69%) diff --git a/src/confd/bin/bootstrap b/src/confd/bin/bootstrap index 68a1a57114..fac6de30fb 100755 --- a/src/confd/bin/bootstrap +++ b/src/confd/bin/bootstrap @@ -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 \ diff --git a/src/confd/bin/gen-motd b/src/confd/bin/gen-motd index e73f22f159..a6f0940f69 100644 --- a/src/confd/bin/gen-motd +++ b/src/confd/bin/gen-motd @@ -6,7 +6,7 @@ if [ -f /etc/motd ]; then cat < #include +#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); diff --git a/src/confd/yang/infix-system@2023-10-19.yang b/src/confd/yang/infix-system@2024-02-29.yang similarity index 69% rename from src/confd/yang/infix-system@2023-10-19.yang rename to src/confd/yang/infix-system@2024-02-29.yang index 9ec7a96c29..7db96eb79a 100644 --- a/src/confd/yang/infix-system@2023-10-19.yang +++ b/src/confd/yang/infix-system@2024-02-29.yang @@ -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; } } From a24d7a3ea2308bd4a8aade96a84caefdbf7f6d53 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 29 Feb 2024 20:09:25 +0100 Subject: [PATCH 3/3] package/klish: bump for motd-like Banner="" extension for klish.conf Signed-off-by: Joachim Wiberg --- package/klish/klish.conf | 2 ++ package/klish/klish.hash | 2 +- package/klish/klish.mk | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/package/klish/klish.conf b/package/klish/klish.conf index eef5397307..aa0e12fc07 100644 --- a/package/klish/klish.conf +++ b/package/klish/klish.conf @@ -6,6 +6,8 @@ UnixSocketPath=/run/klishd.sock Pager="/usr/bin/less -I -F -e -X -K -d -r -S" UsePager=y +Banner="\nSee the 'help' command for an introduction to the system\n\n" + # Set max size of history. By default 100 lines are saved, for every new # line after that, the oldest line is dropped. Set to 0 for unlimited. #HistorySize=100 diff --git a/package/klish/klish.hash b/package/klish/klish.hash index 58f1d351c8..63a9c1ce14 100644 --- a/package/klish/klish.hash +++ b/package/klish/klish.hash @@ -1,3 +1,3 @@ # Locally calculated sha256 9d9d33b873917ca5d0bdcc47a36d2fd385971ab0c045d1472fcadf95ee5bcf5b LICENCE -sha256 98183f02f1a1052fa8362761d549e13e4220949003813737bb88e3c7efd95aa7 klish-276c9ae11732c374fca32cc10ab1a174ece1c0a8-br1.tar.gz +sha256 edb7b9a53d57d87756a99fa25c4af3d6219bed9a863541e672580a2d1f6e7052 klish-b898fdd9a128adaad3a5c92d8dd7baac2085df1b-br1.tar.gz diff --git a/package/klish/klish.mk b/package/klish/klish.mk index 565a540631..7dfbb70c2b 100644 --- a/package/klish/klish.mk +++ b/package/klish/klish.mk @@ -4,7 +4,7 @@ # ################################################################################ -KLISH_VERSION = 276c9ae11732c374fca32cc10ab1a174ece1c0a8 +KLISH_VERSION = b898fdd9a128adaad3a5c92d8dd7baac2085df1b KLISH_SITE = https://github.com/kernelkit/klish.git #KLISH_VERSION = tags/3.0.0 #KLISH_SITE = https://src.libcode.org/pkun/klish.git