LICENSE at the root; the 103 files new in ext/src/mcde (none was in the CDE import) carry the CNCL line instead of the LGPL notice. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
312 lines
8.6 KiB
C
312 lines
8.6 KiB
C
/*
|
|
* net.c -- MCDE: the network interfaces (McdeSys.h, "Network")
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*
|
|
* getifaddrs(3) -- not POSIX, but the same on NetBSD (where it comes from)
|
|
* and on Linux -- gives the interfaces and their addresses; whether one is
|
|
* Wi-Fi, and which network it has joined, the backend tells
|
|
* (mcde_sys_if_kind). The loopback is left out.
|
|
*/
|
|
|
|
#define _NETBSD_SOURCE /* getifaddrs(3) on NetBSD */
|
|
#ifndef _DEFAULT_SOURCE
|
|
#define _DEFAULT_SOURCE /* and on Linux */
|
|
#endif
|
|
|
|
#include <ctype.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <sys/types.h>
|
|
#include <sys/ioctl.h>
|
|
#include <sys/socket.h>
|
|
#include <arpa/inet.h>
|
|
#include <ifaddrs.h>
|
|
#include <net/if.h>
|
|
#include <netdb.h>
|
|
#include <netinet/in.h>
|
|
|
|
#include <Mcde/McdeSys.h>
|
|
|
|
#include "private.h"
|
|
|
|
static McdeSysInterface *
|
|
Find(McdeSysInterface *ifs, size_t n, const char *name)
|
|
{
|
|
size_t i;
|
|
|
|
for (i = 0; i < n; i++)
|
|
if (strcmp(ifs[i].name, name) == 0)
|
|
return &ifs[i];
|
|
return NULL;
|
|
}
|
|
|
|
int
|
|
mcde_sys_interfaces(McdeSysInterface *ifs, size_t max)
|
|
{
|
|
struct ifaddrs *all, *a;
|
|
size_t n = 0;
|
|
|
|
if (getifaddrs(&all) != 0)
|
|
return mcde_sys_fail(MCDE_SYS_EFAIL, "No network interfaces.");
|
|
for (a = all; a != NULL; a = a->ifa_next) {
|
|
McdeSysInterface *it;
|
|
int fam;
|
|
|
|
if (a->ifa_flags & IFF_LOOPBACK)
|
|
continue;
|
|
if ((it = Find(ifs, n, a->ifa_name)) == NULL) {
|
|
if (n >= max)
|
|
continue;
|
|
it = &ifs[n++];
|
|
memset(it, 0, sizeof *it);
|
|
snprintf(it->name, sizeof it->name, "%s", a->ifa_name);
|
|
it->kind = mcde_sys_if_kind(it->name, it->ssid,
|
|
sizeof it->ssid);
|
|
}
|
|
if (a->ifa_addr == NULL || !(a->ifa_flags & IFF_UP))
|
|
continue;
|
|
fam = a->ifa_addr->sa_family;
|
|
/* an IPv4 address first; an IPv6 one only if there is none,
|
|
* and not the link-local one every interface has */
|
|
/* the first IPv4 address is the primary one (ip(8) lists it
|
|
* first; later ones are "secondary") */
|
|
if ((fam == AF_INET && (it->address[0] == '\0' ||
|
|
strchr(it->address, ':') != NULL)) ||
|
|
(fam == AF_INET6 && it->address[0] == '\0')) {
|
|
char host[NI_MAXHOST];
|
|
|
|
if (fam == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(
|
|
&((struct sockaddr_in6 *) a->ifa_addr)->sin6_addr))
|
|
continue;
|
|
if (getnameinfo(a->ifa_addr, fam == AF_INET ?
|
|
sizeof(struct sockaddr_in) :
|
|
sizeof(struct sockaddr_in6), host, sizeof host,
|
|
NULL, 0, NI_NUMERICHOST) == 0) {
|
|
snprintf(it->address, sizeof it->address, "%s",
|
|
host);
|
|
it->up = 1;
|
|
}
|
|
}
|
|
}
|
|
freeifaddrs(all);
|
|
return (int) n;
|
|
}
|
|
|
|
/* ---- the settings of one interface (System Settings > Network) ---------
|
|
* What every system has the same way is here: the addresses the interface
|
|
* has now (getifaddrs), its MTU (SIOCGIFMTU, ioctl(2) on a socket, as
|
|
* ifconfig(8) asks it on both), the DNS servers in use (resolv.conf(5),
|
|
* written by resolvconf on NetBSD and by NetworkManager or systemd on
|
|
* Linux), and the checks of what the user typed. Where the configuration
|
|
* lives -- dhcpcd.conf(5) on NetBSD, NetworkManager on Linux -- the
|
|
* backend knows (docs/reference/netconfig.txt in the Makronix tree). */
|
|
|
|
int
|
|
mcde_sys_net_ifname_ok(const char *name)
|
|
{
|
|
const char *p;
|
|
|
|
if (name == NULL || name[0] == '\0' || name[0] == '-' ||
|
|
strlen(name) >= IFNAMSIZ)
|
|
return 0;
|
|
for (p = name; *p != '\0'; p++)
|
|
if (!isalnum((unsigned char) *p) && *p != '_' && *p != '-' &&
|
|
*p != '.')
|
|
return 0;
|
|
return 1;
|
|
}
|
|
|
|
/* the prefix length of a dotted netmask, -1 when it is none */
|
|
int
|
|
mcde_sys_net_prefix(const char *netmask)
|
|
{
|
|
struct in_addr a;
|
|
unsigned long m;
|
|
int n = 0;
|
|
|
|
if (inet_pton(AF_INET, netmask, &a) != 1)
|
|
return -1;
|
|
m = ntohl(a.s_addr);
|
|
while (m & 0x80000000UL) {
|
|
n++;
|
|
m = (m << 1) & 0xffffffffUL;
|
|
}
|
|
return m == 0 ? n : -1;
|
|
}
|
|
|
|
void
|
|
mcde_sys_net_mask(int prefix, char *buf, size_t len)
|
|
{
|
|
unsigned long m = prefix <= 0 ? 0 :
|
|
(0xffffffffUL << (32 - (prefix > 32 ? 32 : prefix))) & 0xffffffffUL;
|
|
|
|
snprintf(buf, len, "%lu.%lu.%lu.%lu", (m >> 24) & 255, (m >> 16) & 255,
|
|
(m >> 8) & 255, m & 255);
|
|
}
|
|
|
|
static int
|
|
Ipv4(const char *s)
|
|
{
|
|
struct in_addr a;
|
|
|
|
return s != NULL && inet_pton(AF_INET, s, &a) == 1;
|
|
}
|
|
|
|
static int
|
|
Ipv6(const char *s)
|
|
{
|
|
struct in6_addr a;
|
|
|
|
return s != NULL && inet_pton(AF_INET6, s, &a) == 1;
|
|
}
|
|
|
|
/* a domain name: letters, digits, '-' and '.', not beginning with '-' */
|
|
static int
|
|
Domain(const char *s)
|
|
{
|
|
const char *p;
|
|
|
|
if (s[0] == '\0' || s[0] == '-' || s[0] == '.' || strlen(s) > 95)
|
|
return 0;
|
|
for (p = s; *p != '\0'; p++)
|
|
if (!isalnum((unsigned char) *p) && *p != '-' && *p != '.')
|
|
return 0;
|
|
return 1;
|
|
}
|
|
|
|
static void
|
|
Current(const char *ifname, McdeSysNetConfig *c)
|
|
{
|
|
struct ifaddrs *all, *a;
|
|
struct ifreq ifr;
|
|
char line[256];
|
|
FILE *fp;
|
|
int s;
|
|
|
|
if (getifaddrs(&all) == 0) {
|
|
for (a = all; a != NULL; a = a->ifa_next) {
|
|
if (a->ifa_addr == NULL ||
|
|
strcmp(a->ifa_name, ifname) != 0)
|
|
continue;
|
|
if (a->ifa_addr->sa_family == AF_INET &&
|
|
c->cur_address[0] == '\0') {
|
|
inet_ntop(AF_INET, &((struct sockaddr_in *)
|
|
a->ifa_addr)->sin_addr, c->cur_address,
|
|
sizeof c->cur_address);
|
|
if (a->ifa_netmask != NULL)
|
|
inet_ntop(AF_INET, &((struct sockaddr_in *)
|
|
a->ifa_netmask)->sin_addr,
|
|
c->cur_netmask,
|
|
sizeof c->cur_netmask);
|
|
} else if (a->ifa_addr->sa_family == AF_INET6 &&
|
|
c->cur_address6[0] == '\0') {
|
|
struct in6_addr *in6 = &((struct sockaddr_in6 *)
|
|
a->ifa_addr)->sin6_addr;
|
|
int i, bits = 0;
|
|
|
|
if (IN6_IS_ADDR_LINKLOCAL(in6))
|
|
continue;
|
|
inet_ntop(AF_INET6, in6, c->cur_address6,
|
|
sizeof c->cur_address6);
|
|
if (a->ifa_netmask != NULL) {
|
|
unsigned char *m = ((struct sockaddr_in6 *)
|
|
a->ifa_netmask)->sin6_addr.s6_addr;
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
unsigned char b = m[i];
|
|
|
|
while (b & 0x80) {
|
|
bits++;
|
|
b = (unsigned char) (b << 1);
|
|
}
|
|
}
|
|
}
|
|
c->cur_prefix6 = bits;
|
|
}
|
|
}
|
|
freeifaddrs(all);
|
|
}
|
|
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) >= 0) {
|
|
memset(&ifr, 0, sizeof ifr);
|
|
snprintf(ifr.ifr_name, sizeof ifr.ifr_name, "%s", ifname);
|
|
if (ioctl(s, SIOCGIFMTU, &ifr) == 0)
|
|
c->mtu = ifr.ifr_mtu;
|
|
close(s);
|
|
}
|
|
if ((fp = fopen("/etc/resolv.conf", "r")) != NULL) {
|
|
while (fgets(line, sizeof line, fp) != NULL &&
|
|
c->ncur_dns < MCDE_NET_DNS_MAX) {
|
|
char *last = NULL, *k = strtok_r(line, " \t\r\n", &last),
|
|
*v = strtok_r(NULL, " \t\r\n", &last);
|
|
|
|
if (k != NULL && v != NULL &&
|
|
strcmp(k, "nameserver") == 0)
|
|
snprintf(c->cur_dns[c->ncur_dns++],
|
|
sizeof c->cur_dns[0], "%s", v);
|
|
}
|
|
fclose(fp);
|
|
}
|
|
}
|
|
|
|
int
|
|
mcde_sys_net_config(const char *ifname, McdeSysNetConfig *c)
|
|
{
|
|
if (!mcde_sys_net_ifname_ok(ifname))
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "There is no network "
|
|
"card of that name.");
|
|
memset(c, 0, sizeof *c);
|
|
c->prefix6 = 64;
|
|
Current(ifname, c);
|
|
return mcde_sys_net_config_backend(ifname, c);
|
|
}
|
|
|
|
int
|
|
mcde_sys_net_configure(const char *ifname, const McdeSysNetConfig *c)
|
|
{
|
|
const char *p;
|
|
int i;
|
|
|
|
if (!mcde_sys_net_ifname_ok(ifname))
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "There is no network "
|
|
"card of that name.");
|
|
if (geteuid() != 0)
|
|
return mcde_sys_fail(MCDE_SYS_EPERM, "Only the administrator "
|
|
"can change the network settings: click the lock first.");
|
|
if (c->ipv4 == MCDE_IPV4_MANUAL || c->ipv4 == MCDE_IPV4_DHCP_MANUAL) {
|
|
if (!Ipv4(c->address))
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "\"%s\" is no "
|
|
"IPv4 address.", c->address);
|
|
if (mcde_sys_net_prefix(c->netmask) < 1)
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "\"%s\" is no "
|
|
"subnet mask (255.255.255.0, for example).",
|
|
c->netmask);
|
|
}
|
|
if (c->ipv4 == MCDE_IPV4_MANUAL && c->router[0] != '\0' &&
|
|
!Ipv4(c->router))
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "\"%s\" is no router "
|
|
"address.", c->router);
|
|
if (c->ipv6 == MCDE_IPV6_MANUAL && (!Ipv6(c->address6) ||
|
|
c->prefix6 < 1 || c->prefix6 > 128))
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "\"%s/%d\" is no IPv6 "
|
|
"address with its prefix length.", c->address6,
|
|
c->prefix6);
|
|
for (i = 0; i < c->ndns; i++)
|
|
if (!Ipv4(c->dns[i]) && !Ipv6(c->dns[i]))
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "\"%s\" is no "
|
|
"DNS server address.", c->dns[i]);
|
|
for (i = 0; i < c->nsearch; i++)
|
|
if (!Domain(c->search[i]))
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "\"%s\" is no "
|
|
"domain name.", c->search[i]);
|
|
for (p = c->clientid; *p != '\0'; p++)
|
|
if (!isgraph((unsigned char) *p) || *p == '"' || *p == '\\' ||
|
|
*p == '\'')
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "The DHCP client "
|
|
"ID may hold letters, digits and signs, no blanks "
|
|
"or quotes.");
|
|
return mcde_sys_net_configure_backend(ifname, c);
|
|
}
|