Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package/klish/klish.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package/klish/klish.hash
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Locally calculated
sha256 9d9d33b873917ca5d0bdcc47a36d2fd385971ab0c045d1472fcadf95ee5bcf5b LICENCE
sha256 98183f02f1a1052fa8362761d549e13e4220949003813737bb88e3c7efd95aa7 klish-276c9ae11732c374fca32cc10ab1a174ece1c0a8-br1.tar.gz
sha256 edb7b9a53d57d87756a99fa25c4af3d6219bed9a863541e672580a2d1f6e7052 klish-b898fdd9a128adaad3a5c92d8dd7baac2085df1b-br1.tar.gz
2 changes: 1 addition & 1 deletion package/klish/klish.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/confd/bin/bootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
2 changes: 1 addition & 1 deletion src/confd/bin/gen-motd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/confd/src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
210 changes: 210 additions & 0 deletions src/confd/src/base64.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
/* SPDX-License-Identifier: BSD-3-Clause */

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "base64.h"

/*
* Base64 encoding/decoding (RFC1341)
* Copyright (c) 2005-2011, Jouni Malinen <j@w1.fi>
*/
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 <err.h>
#include <getopt.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>

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
11 changes: 11 additions & 0 deletions src/confd/src/base64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* SPDX-License-Identifier: BSD-3-Clause */

#ifndef BASE64_H_
#define BASE64_H_

#include <stdlib.h>

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_ */
55 changes: 54 additions & 1 deletion src/confd/src/ietf-system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down
Loading