net.c (11 calls), logs.c (9), disk_linux.c (13) and mount_linux.c (1) are messages 187-217 of mcdesys.cat now: no interfaces, no card of that name, the network-settings administrator sentence, the address checks (IPv4, subnet mask, router, IPv6 with prefix, DNS, domain), the DHCP client ID rule; the log sentences (no room, no name, inotify missing, cannot be watched/read, kqueue is a follow-up on this system, no buffer); the disk sentence set (lsblk/udisksctl missing, no such disk, not mounted, the system's, cannot be checked, erasing needs the administrator, the name rule, no drive, holds the system, mounted on, no permissions list); "No /proc/mounts." German copy with the same numbers in programs/localized/de_DE.UTF-8/msg/mcdesys.msg. Measured with a runner (setlocale + the public calls): LANG=C "Only the administrator can change the network settings: click the lock first." and "This log cannot be read."; LANG=de_DE.UTF-8 "Nur der Administrator kann die Netzwerkeinstellungen ändern: zuerst entsperren." and "Dieses Log kann nicht gelesen werden." make mcde-linux MCDE_DIRS="install lib/McdeSys" and "install programs/localized/C/msg" exit=0; "check lib/McdeSys": # FAIL: 0, exit=0; MCDE_DIRS=plist: ok.
346 lines
11 KiB
C
346 lines
11 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, mcde_sys_msg(MSG_NETIF_NONE, "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 checks of mcde_sys_wifi_join where the network is written into
|
|
* wpa_supplicant.conf (netbsd.c): the interface name is lower-case letters and
|
|
* digits (it goes into rc.conf's wpa_supplicant_flags); the network name has 1
|
|
* to 32 bytes, the passphrase, if there is one, 8 to 63; neither has a quote,
|
|
* a backslash or a newline, which would end the quoted string of the file.
|
|
* Kept here, apart from the NetBSD branch, so that make check can run them as
|
|
* an ordinary user (the join itself needs the administrator; tasksd t0081).
|
|
* 1 if all is well. An empty interface name passed the inline check this
|
|
* replaces. */
|
|
int
|
|
mcde_sys_wifi_config_ok(const char *ifname, const char *ssid,
|
|
const char *passphrase)
|
|
{
|
|
size_t n;
|
|
|
|
if (ifname == NULL || ifname[0] == '\0' ||
|
|
strspn(ifname, "abcdefghijklmnopqrstuvwxyz0123456789") !=
|
|
strlen(ifname))
|
|
return 0;
|
|
if (ssid == NULL || ssid[0] == '\0' || strlen(ssid) > 32 ||
|
|
strpbrk(ssid, "\"\n\\") != NULL)
|
|
return 0;
|
|
if (passphrase != NULL) {
|
|
n = strlen(passphrase);
|
|
if (n < 8 || n > 63 || strpbrk(passphrase, "\"\n\\") != NULL)
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
/* May this network name be a command-line argument of nmcli (the Linux
|
|
* mcde_sys_wifi_join)? 1 to 32 bytes, and not starting with "-": a name that
|
|
* does is an option to nmcli, and the name of a neighbour's access point is
|
|
* whatever it broadcasts. Measured with nmcli 1.56.1: "nmcli device wifi
|
|
* connect --ask password PW ifname IF" gives "invalid extra argument" -- the
|
|
* name was taken as the option --ask; and "--" in front is no remedy: nmcli
|
|
* takes the "--" for the name ("invalid extra argument 'Home'"). A passphrase
|
|
* is the argument of "password", whatever it starts with. */
|
|
int
|
|
mcde_sys_wifi_ssid_arg_ok(const char *ssid)
|
|
{
|
|
return ssid != NULL && ssid[0] != '\0' && ssid[0] != '-' &&
|
|
strlen(ssid) <= 32;
|
|
}
|
|
|
|
/* 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, mcde_sys_msg(MSG_LX_NO_CARD, "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, mcde_sys_msg(MSG_LX_NO_CARD, "There is no network card of that name."));
|
|
if (geteuid() != 0)
|
|
return mcde_sys_fail(MCDE_SYS_EPERM, mcde_sys_msg(MSG_NETIF_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, mcde_sys_msg(MSG_NETIF_BAD_V4, "\"%s\" is no IPv4 address."), c->address);
|
|
if (mcde_sys_net_prefix(c->netmask) < 1)
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_NETIF_BAD_MASK, "\"%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, mcde_sys_msg(MSG_NETIF_BAD_ROUTER, "\"%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, mcde_sys_msg(MSG_NETIF_BAD_V6, "\"%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, mcde_sys_msg(MSG_NETIF_BAD_DNS, "\"%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, mcde_sys_msg(MSG_NETIF_BAD_DOMAIN, "\"%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, mcde_sys_msg(MSG_NETIF_BAD_CLIENTID, "The DHCP client ID may hold letters, digits and signs, no blanks or quotes."));
|
|
return mcde_sys_net_configure_backend(ifname, c);
|
|
}
|