confd: add configurable mDNS hostname (avahi host-name)

Add a `hostname` leaf to the `mdns` YANG container with default `"%h"`,
allowing operators to override the avahi host-name used for mDNS A/AAAA
records without reflashing. The default expands to DEFAULT_HOSTNAME from
os-release, preserving existing behaviour for unconfigured deployments.

Format specifiers %h/%i/%m are supported via the existing hostnamefmt()
infrastructure, which is also fixed to copy the const fmt argument to a
local buffer before modification (UB when called with a libyang-owned
string).

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2026-03-09 19:25:18 +01:00
parent 1282352359
commit 571edefa9d
5 changed files with 43 additions and 10 deletions
+11 -3
View File
@@ -340,12 +340,20 @@ static void fput_list(FILE *fp, struct lyd_node *cfg, const char *list, const ch
#define AVAHI_CONF "/etc/avahi/avahi-daemon.conf"
static void mdns_conf(struct lyd_node *cfg)
static void mdns_conf(struct confd *confd, struct lyd_node *cfg)
{
const char *hostname = fgetkey("/etc/os-release", "DEFAULT_HOSTNAME");
char hname[HOST_NAME_MAX + 1];
const char *hostname;
const char *fmt;
struct lyd_node *ctx;
FILE *fp;
fmt = lydx_get_cattr(cfg, "hostname"); /* "%h" when unset (YANG default) */
if (!hostnamefmt(confd, fmt, hname, sizeof(hname), NULL, 0))
hostname = hname;
else
hostname = fgetkey("/etc/os-release", "DEFAULT_HOSTNAME");
fp = fopen(AVAHI_CONF, "w");
if (!fp) {
ERRNO("failed creating %s", AVAHI_CONF);
@@ -424,7 +432,7 @@ static int mdns_change(sr_session_ctx_t *session, struct lyd_node *config, struc
ena = lydx_is_enabled(srv, "enabled");
if (ena) {
/* Generate/update avahi-daemon.conf */
mdns_conf(srv);
mdns_conf(confd, srv);
/* Generate/update basic mDNS service records */
mdns_records(MDNS_UPDATE, all);
+12 -6
View File
@@ -1540,27 +1540,33 @@ static char *get_mac(struct confd *confd, char *mac, size_t len)
int hostnamefmt(struct confd *confd, const char *fmt, char *hostnm, size_t hostlen,
char *domain, size_t domlen)
{
char buf[HOST_NAME_MAX + 1];
char mac[10];
char *ptr;
char *f, *ptr;
size_t i;
if (!fmt || !*fmt)
return -1;
strlcpy(buf, fmt, sizeof(buf));
f = buf;
memset(hostnm, 0, hostlen);
if (domain)
memset(domain, 0, domlen);
ptr = strchr(fmt, '.');
ptr = strchr(f, '.');
if (ptr) {
*ptr++ = 0;
if (domain)
strlcpy(domain, ptr, domlen);
}
for (i = 0; i < strlen(fmt); i++) {
if (fmt[i] == '%') {
switch (fmt[++i]) {
for (i = 0; i < strlen(f); i++) {
if (f[i] == '%') {
if (f[++i] == '\0')
break;
switch (f[i]) {
case 'i':
strlcat(hostnm, id, hostlen);
break;
@@ -1577,7 +1583,7 @@ int hostnamefmt(struct confd *confd, const char *fmt, char *hostnm, size_t hostl
break; /* Unknown, skip */
}
} else {
char ch[2] = { fmt[i], 0 };
char ch[2] = { f[i], 0 };
strlcat(hostnm, ch, hostlen);
}
}
+1 -1
View File
@@ -43,7 +43,7 @@ MODULES=(
"infix-firewall-icmp-types@2025-04-26.yang"
"infix-meta@2025-12-10.yang"
"infix-system@2026-03-09.yang"
"infix-services@2025-12-10.yang"
"infix-services@2026-03-03.yang"
"ieee802-ethernet-interface@2019-06-21.yang"
"infix-ethernet-interface@2024-02-27.yang"
"infix-factory-default@2023-06-28.yang"
+19
View File
@@ -16,6 +16,9 @@ module infix-services {
reference
"RFC 9640: YANG Data Types and Groupings for Cryptography";
}
import infix-system {
prefix infix-sys;
}
import ietf-keystore {
prefix ks;
@@ -25,6 +28,10 @@ module infix-services {
contact "kernelkit@googlegroups.com";
description "Infix services, generic.";
revision 2026-03-03 {
description "Add hostname leaf to mdns container for avahi host-name override.";
reference "internal";
}
revision 2025-12-10 {
description "Adapt to changes in final version of ietf-keystore";
reference "internal";
@@ -80,6 +87,18 @@ module infix-services {
type inet:domain-name;
}
leaf hostname {
description "Hostname for mDNS A/AAAA records (avahi host-name setting).
When not set the default is the build-time branding hostname.
Supports the same format specifiers as /system/hostname:
%h Default hostname (DEFAULT_HOSTNAME from os-release)
%i OS ID from os-release
%m Last three octets of base MAC (e.g. c0-ff-ee)";
type infix-sys:hostname;
default "%h";
}
container interfaces {
description "Filter interfaces to act on.";