mcde_sys_net_browse lists the computers that announce SMB, AFP, NFS, SSH or just themselves by DNS-SD: on NetBSD through mdnsd with dns_sd.h from base (DNSServiceBrowse per type, DNSServiceResolve per service, select until the time is up; docs/reference/dns-sd.txt), on Linux with avahi-browse -atrpk (its \DDD escapes decoded). mcde_sys_net_shares lists NFS exports (showmount -e) and SMB shares (smbclient -g -N -L); mcde_sys_net_mount connects sftp:// (sshfs, a password on its standard input), nfs:// (mount_nfs / mount.nfs) and afp:// (afpfs-ng's mount_afp) to a folder; mcde_sys_net_unmount (fusermount -u, else umount). Host and user names are checked before they reach a command line. mcde_sys_capture_limited ends a child after a time: showmount waited minutes for an rpcbind that was not there (measured in the build VM); the share lists wait 8 seconds. Measured: on the Linux host 4 computers with their services (KITT, N95 with SMB; two laptops with SMB and SSH), the local SMB share and NFS export listed, a host name with ';' refused. In the build VM: it builds with -ldns_sd; without mdnsd a clear message; with mdnsd started the browse ran and found none (qemu's user network carries no multicast); showmount gave up after 8 s. Mounting is not measured yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1118 lines
32 KiB
C
1118 lines
32 KiB
C
/*
|
|
* netbsd.c -- MCDE: the NetBSD backend of McdeSys (and so Makronix's)
|
|
*
|
|
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
|
|
*
|
|
* The system's own ways: sysctl(3) for the processor and the machine,
|
|
* /etc/release (src/etc/Makefile, etc-release: "Makronix VERSION NAME/MACHINE
|
|
* (based on NetBSD ...)") for the system, statvfs(2)'s f_mntfromname for the
|
|
* startup disk, shutdown(8) and zzz(8) for power, the SIOCG80211NWID ioctl
|
|
* for Wi-Fi, mixerctl(1) when no sound server runs, btconfig(8) for
|
|
* Bluetooth, the envsys(4) dictionary for the battery, wpa_supplicant(8) as
|
|
* sysinst sets it up for Wi-Fi.
|
|
*/
|
|
|
|
#define _NETBSD_SOURCE
|
|
|
|
#include <fcntl.h>
|
|
#include <grp.h>
|
|
#include <paths.h>
|
|
#include <pwd.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <sys/param.h>
|
|
#include <sys/ioctl.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/statvfs.h>
|
|
#include <sys/sysctl.h>
|
|
#include <sys/envsys.h>
|
|
#include <net/if.h>
|
|
#include <net80211/ieee80211_ioctl.h>
|
|
#include <prop/proplib.h>
|
|
#include <sys/select.h>
|
|
#include <sys/time.h>
|
|
#include <dns_sd.h>
|
|
|
|
#include <Mcde/McdeSys.h>
|
|
|
|
#include "private.h"
|
|
|
|
const char *
|
|
mcde_sys_backend(void)
|
|
{
|
|
return "netbsd";
|
|
}
|
|
|
|
static int
|
|
Sysctl(const char *name, char *buf, size_t len)
|
|
{
|
|
size_t n = len;
|
|
|
|
if (sysctlbyname(name, buf, &n, NULL, 0) != 0 || n == 0)
|
|
return 0;
|
|
buf[n < len ? n : len - 1] = '\0';
|
|
return buf[0] != '\0';
|
|
}
|
|
|
|
void
|
|
mcde_sys_info_backend(McdeSysInfo *info)
|
|
{
|
|
char line[256], vendor[64], product[64];
|
|
struct statvfs vfs;
|
|
FILE *fp;
|
|
|
|
/* the first line of /etc/release names the system */
|
|
if ((fp = fopen("/etc/release", "r")) != NULL) {
|
|
if (fgets(line, sizeof line, fp) != NULL) {
|
|
char sys[64], ver[64], name[64];
|
|
|
|
if (sscanf(line, "%63s %63s %63[^/ ]", sys, ver, name) == 3) {
|
|
snprintf(info->system, sizeof info->system, "%s", sys);
|
|
snprintf(info->version, sizeof info->version,
|
|
"%s (%s)", ver, name);
|
|
}
|
|
}
|
|
fclose(fp);
|
|
}
|
|
if (!Sysctl("machdep.cpu_brand", info->cpu, sizeof info->cpu))
|
|
(void) Sysctl("hw.model", info->cpu, sizeof info->cpu);
|
|
if (Sysctl("machdep.dmi.system-vendor", vendor, sizeof vendor) &&
|
|
Sysctl("machdep.dmi.system-product", product, sizeof product))
|
|
snprintf(info->model, sizeof info->model, "%s %s", vendor,
|
|
product);
|
|
if (statvfs("/", &vfs) == 0)
|
|
snprintf(info->disk, sizeof info->disk, "%s", vfs.f_mntfromname);
|
|
/* sysconf(3) has no processor count under _POSIX_C_SOURCE here */
|
|
if (info->cpus < 1) {
|
|
int n = 0;
|
|
size_t len = sizeof n;
|
|
|
|
if (sysctlbyname("hw.ncpuonline", &n, &len, NULL, 0) == 0)
|
|
info->cpus = n;
|
|
}
|
|
}
|
|
|
|
int
|
|
mcde_sys_power(McdeSysPower what)
|
|
{
|
|
static char *sleep_[] = { "/usr/sbin/zzz", NULL };
|
|
static char *restart[] = { "/sbin/shutdown", "-r", "now", NULL };
|
|
static char *halt[] = { "/sbin/shutdown", "-p", "now", NULL };
|
|
|
|
switch (what) {
|
|
case MCDE_POWER_SLEEP:
|
|
return mcde_sys_run(sleep_);
|
|
case MCDE_POWER_RESTART:
|
|
return mcde_sys_run(restart);
|
|
case MCDE_POWER_SHUTDOWN:
|
|
return mcde_sys_run(halt);
|
|
}
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "Unknown power action.");
|
|
}
|
|
|
|
/* ---- network: an interface that answers SIOCG80211NWID is Wi-Fi, and the
|
|
* answer is the network joined (ifconfig(8) asks the same way) */
|
|
McdeSysNetKind
|
|
mcde_sys_if_kind(const char *name, char *ssid, size_t len)
|
|
{
|
|
struct ieee80211_nwid nwid;
|
|
struct ifreq ifr;
|
|
int s;
|
|
|
|
ssid[0] = '\0';
|
|
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
|
|
return MCDE_NET_OTHER;
|
|
memset(&ifr, 0, sizeof ifr);
|
|
memset(&nwid, 0, sizeof nwid);
|
|
strlcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
|
|
ifr.ifr_data = (void *) &nwid;
|
|
if (ioctl(s, SIOCG80211NWID, &ifr) == 0) {
|
|
size_t n = nwid.i_len < len - 1 ? nwid.i_len : len - 1;
|
|
|
|
memcpy(ssid, nwid.i_nwid, n);
|
|
ssid[n] = '\0';
|
|
close(s);
|
|
return MCDE_NET_WIFI;
|
|
}
|
|
close(s);
|
|
return MCDE_NET_WIRED;
|
|
}
|
|
|
|
/* ---- sound without a sound server: mixerctl(1), outputs.master (0..255) */
|
|
static char mixerctl[] = "/usr/bin/mixerctl";
|
|
|
|
int
|
|
mcde_sys_mixer_get(int *percent, int *muted)
|
|
{
|
|
char out[128];
|
|
char *v[] = { mixerctl, "-n", "outputs.master", NULL };
|
|
char *m[] = { mixerctl, "-n", "outputs.master.mute", NULL };
|
|
|
|
if (mcde_sys_capture(v, out, sizeof out) != MCDE_SYS_OK)
|
|
return mcde_sys_fail(MCDE_SYS_ENOTSUP, "The mixer has no "
|
|
"outputs.master.");
|
|
*percent = (atoi(out) * 100 + 127) / 255; /* "l,r": the left */
|
|
*muted = mcde_sys_capture(m, out, sizeof out) == MCDE_SYS_OK &&
|
|
strncmp(out, "on", 2) == 0;
|
|
return MCDE_SYS_OK;
|
|
}
|
|
|
|
int
|
|
mcde_sys_mixer_set(int percent)
|
|
{
|
|
char val[64];
|
|
char *argv[] = { mixerctl, "-q", "-w", val, NULL };
|
|
int v = (percent * 255 + 50) / 100;
|
|
|
|
snprintf(val, sizeof val, "outputs.master=%d,%d", v, v);
|
|
return mcde_sys_run(argv);
|
|
}
|
|
|
|
int
|
|
mcde_sys_mixer_mute(int muted)
|
|
{
|
|
char val[64];
|
|
char *argv[] = { mixerctl, "-q", "-w", val, NULL };
|
|
|
|
snprintf(val, sizeof val, "outputs.master.mute=%s", muted ? "on" : "off");
|
|
return mcde_sys_run(argv);
|
|
}
|
|
|
|
/* ---- Bluetooth: btconfig(8); "ubt0: bdaddr ... flags=0x3<UP,RUNNING>" */
|
|
static char btconfig[] = "/usr/sbin/btconfig";
|
|
|
|
int
|
|
mcde_sys_bluetooth(McdeSysBluetooth *bt)
|
|
{
|
|
char out[2048], *colon;
|
|
char *argv[] = { btconfig, NULL };
|
|
|
|
memset(bt, 0, sizeof *bt);
|
|
if (mcde_sys_capture(argv, out, sizeof out) != MCDE_SYS_OK ||
|
|
(colon = strchr(out, ':')) == NULL)
|
|
return MCDE_SYS_OK;
|
|
*colon = '\0';
|
|
snprintf(bt->name, sizeof bt->name, "%s", out);
|
|
bt->present = 1;
|
|
bt->on = strstr(colon + 1, "<UP") != NULL;
|
|
return MCDE_SYS_OK;
|
|
}
|
|
|
|
int
|
|
mcde_sys_bluetooth_power(int on)
|
|
{
|
|
McdeSysBluetooth bt;
|
|
char *argv[] = { btconfig, NULL, NULL, NULL };
|
|
|
|
(void) mcde_sys_bluetooth(&bt);
|
|
if (!bt.present)
|
|
return mcde_sys_fail(MCDE_SYS_ENOENT, "There is no Bluetooth "
|
|
"controller.");
|
|
argv[1] = bt.name;
|
|
argv[2] = on ? "up" : "down";
|
|
return mcde_sys_run(argv);
|
|
}
|
|
|
|
/* ---- the battery: the envsys(4) dictionary of /dev/sysmon, read as
|
|
* envstat(8) reads it (src/usr.sbin/envstat/envstat.c:527): acpibat(4)
|
|
* publishes "present", "charge" (uWh, max-value the last full charge),
|
|
* "charging" and "discharge rate" (uW); acpiacad(4) "connected"
|
|
* (src/sys/dev/acpi/acpi_bat.c:710, acpi_acad.c:262;
|
|
* docs/reference/battery.txt). A sensor in state "invalid" says nothing. */
|
|
static int
|
|
Sensor(prop_array_t sensors, const char *desc, int64_t *cur, int64_t *max)
|
|
{
|
|
prop_object_iterator_t it;
|
|
prop_object_t s, o;
|
|
int found = 0;
|
|
|
|
if ((it = prop_array_iterator(sensors)) == NULL)
|
|
return 0;
|
|
while ((s = prop_object_iterator_next(it)) != NULL) {
|
|
o = prop_dictionary_get(s, "description");
|
|
if (o == NULL || !prop_string_equals_string(o, desc))
|
|
continue;
|
|
o = prop_dictionary_get(s, "state");
|
|
if (o != NULL && prop_string_equals_string(o, "invalid"))
|
|
break;
|
|
if ((o = prop_dictionary_get(s, "cur-value")) == NULL)
|
|
break;
|
|
*cur = prop_number_signed_value(o);
|
|
if (max != NULL) {
|
|
o = prop_dictionary_get(s, "max-value");
|
|
*max = o != NULL ? prop_number_signed_value(o) : 0;
|
|
}
|
|
found = 1;
|
|
break;
|
|
}
|
|
prop_object_iterator_release(it);
|
|
return found;
|
|
}
|
|
|
|
int
|
|
mcde_sys_battery(McdeSysBattery *bat)
|
|
{
|
|
prop_dictionary_t dict;
|
|
prop_object_iterator_t it;
|
|
prop_object_t key;
|
|
prop_array_t sensors;
|
|
const char *dev;
|
|
int64_t cur, max, now = 0, full = 0, rate = 0;
|
|
int fd, n = 0, discharging = 0;
|
|
|
|
memset(bat, 0, sizeof *bat);
|
|
bat->minutes = -1;
|
|
if ((fd = open(_PATH_SYSMON, O_RDONLY)) == -1)
|
|
return MCDE_SYS_OK;
|
|
if (prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict) != 0) {
|
|
close(fd);
|
|
return MCDE_SYS_OK;
|
|
}
|
|
close(fd);
|
|
if ((it = prop_dictionary_iterator(dict)) == NULL) {
|
|
prop_object_release(dict);
|
|
return MCDE_SYS_OK;
|
|
}
|
|
while ((key = prop_object_iterator_next(it)) != NULL) {
|
|
dev = prop_dictionary_keysym_value(key);
|
|
sensors = prop_dictionary_get_keysym(dict, key);
|
|
if (prop_object_type(sensors) != PROP_TYPE_ARRAY)
|
|
continue;
|
|
if (strncmp(dev, "acpiacad", 8) == 0) {
|
|
if (Sensor(sensors, "connected", &cur, NULL) && cur)
|
|
bat->ac = 1;
|
|
continue;
|
|
}
|
|
if (strncmp(dev, "acpibat", 7) != 0 ||
|
|
!Sensor(sensors, "present", &cur, NULL) || !cur)
|
|
continue;
|
|
n++;
|
|
if (Sensor(sensors, "charge", &cur, &max) && max > 0) {
|
|
now += cur;
|
|
full += max;
|
|
}
|
|
if (Sensor(sensors, "charging", &cur, NULL) && cur)
|
|
bat->charging = 1;
|
|
else if (Sensor(sensors, "discharge rate", &cur, NULL) &&
|
|
cur > 0) {
|
|
discharging = 1;
|
|
rate += cur;
|
|
}
|
|
}
|
|
prop_object_iterator_release(it);
|
|
prop_object_release(dict);
|
|
bat->present = n > 0;
|
|
if (full > 0)
|
|
bat->percent = (int) ((now * 100 + full / 2) / full);
|
|
if (bat->percent > 100)
|
|
bat->percent = 100;
|
|
if (bat->charging)
|
|
bat->ac = 1;
|
|
if (discharging && !bat->ac && rate > 0)
|
|
bat->minutes = (int) (now * 60 / rate);
|
|
return MCDE_SYS_OK;
|
|
}
|
|
|
|
/* ---- Wi-Fi: as the installer does it (src/usr.sbin/sysinst/net.c:
|
|
* a network block in /etc/wpa_supplicant.conf, wpa_supplicant and dhcpcd in
|
|
* rc.conf); the networks in reach from wpa_cli(8) when wpa_supplicant runs,
|
|
* else from ifconfig(8) "list scan" (root). docs/reference/wifi.txt. */
|
|
#define WPA_CONF "/etc/wpa_supplicant.conf"
|
|
#define RC_CONF "/etc/rc.conf"
|
|
static char wpacli[] = "/usr/sbin/wpa_cli";
|
|
static char ifconfigp[] = "/sbin/ifconfig";
|
|
|
|
/* signal in dBm (wpa_cli) or the driver's RSSI (ifconfig) as 0..100 */
|
|
static int
|
|
SignalPercent(int v)
|
|
{
|
|
if (v < 0)
|
|
v = 2 * (v + 100);
|
|
return v < 0 ? 0 : v > 100 ? 100 : v;
|
|
}
|
|
|
|
static int
|
|
AddNet(McdeSysWifi *nets, int n, size_t max, const char *ssid, int signal,
|
|
int secured)
|
|
{
|
|
int i;
|
|
|
|
if (ssid[0] == '\0')
|
|
return n;
|
|
for (i = 0; i < n; i++)
|
|
if (strcmp(nets[i].ssid, ssid) == 0) {
|
|
if (signal > nets[i].signal)
|
|
nets[i].signal = signal;
|
|
return n;
|
|
}
|
|
if ((size_t) n >= max)
|
|
return n;
|
|
snprintf(nets[n].ssid, sizeof nets[n].ssid, "%s", ssid);
|
|
nets[n].signal = signal;
|
|
nets[n].secured = secured;
|
|
return n + 1;
|
|
}
|
|
|
|
int
|
|
mcde_sys_wifi_scan(const char *ifname, McdeSysWifi *nets, size_t max)
|
|
{
|
|
static char out[16384];
|
|
char *argv[6], *line, *last = NULL;
|
|
int n = 0;
|
|
|
|
/* wpa_cli: "bssid / frequency / signal level / flags / ssid", tabs */
|
|
argv[0] = wpacli;
|
|
argv[1] = "-i";
|
|
argv[2] = (char *) ifname;
|
|
argv[3] = "scan_results";
|
|
argv[4] = NULL;
|
|
if (mcde_sys_capture(argv, out, sizeof out) == MCDE_SYS_OK &&
|
|
strstr(out, "bssid") != NULL) {
|
|
for (line = strtok_r(out, "\n", &last); line != NULL;
|
|
line = strtok_r(NULL, "\n", &last)) {
|
|
char *f[5], *l2 = NULL;
|
|
int k;
|
|
|
|
f[0] = strtok_r(line, "\t", &l2);
|
|
for (k = 1; k < 5; k++)
|
|
f[k] = strtok_r(NULL, "\t", &l2);
|
|
if (f[4] == NULL || strchr(f[0], ':') == NULL)
|
|
continue;
|
|
n = AddNet(nets, n, max, f[4],
|
|
SignalPercent(atoi(f[2])),
|
|
strstr(f[3], "WPA") != NULL ||
|
|
strstr(f[3], "WEP") != NULL);
|
|
}
|
|
return n;
|
|
}
|
|
/* ifconfig -v IF list scan: "SSID BSSID CHAN RATE S:N INT CAPS",
|
|
* the SSID padded; the interface must be up (root) */
|
|
if (geteuid() != 0)
|
|
return mcde_sys_fail(MCDE_SYS_EPERM, "Only the administrator "
|
|
"can look for Wi-Fi networks while none is joined.");
|
|
argv[0] = ifconfigp;
|
|
argv[1] = (char *) ifname;
|
|
argv[2] = "up";
|
|
argv[3] = NULL;
|
|
(void) mcde_sys_run(argv);
|
|
argv[1] = "-v";
|
|
argv[2] = (char *) ifname;
|
|
argv[3] = "list";
|
|
argv[4] = "scan";
|
|
argv[5] = NULL;
|
|
if (mcde_sys_capture(argv, out, sizeof out) != MCDE_SYS_OK)
|
|
return -1;
|
|
for (line = strtok_r(out, "\n", &last); line != NULL;
|
|
line = strtok_r(NULL, "\n", &last)) {
|
|
char *p, *bssid = NULL, *sn, *caps, ssid[64];
|
|
int rssi = 0;
|
|
|
|
/* the BSSID: 17 characters, colons at 2, 5, 8, 11, 14 */
|
|
for (p = line; *p != '\0' && strlen(p) >= 17; p++)
|
|
if (p[2] == ':' && p[5] == ':' && p[8] == ':' &&
|
|
p[11] == ':' && p[14] == ':' &&
|
|
(p == line || p[-1] == ' ')) {
|
|
bssid = p;
|
|
break;
|
|
}
|
|
if (bssid == NULL || bssid == line)
|
|
continue;
|
|
snprintf(ssid, sizeof ssid, "%.*s", (int) (bssid - line), line);
|
|
while (ssid[0] != '\0' && ssid[strlen(ssid) - 1] == ' ')
|
|
ssid[strlen(ssid) - 1] = '\0';
|
|
if ((sn = strchr(bssid + 17, ':')) != NULL) {
|
|
char *q = sn;
|
|
|
|
while (q > bssid + 17 && q[-1] != ' ')
|
|
q--;
|
|
rssi = atoi(q);
|
|
}
|
|
/* the capabilities are the last word: P is privacy */
|
|
caps = strrchr(line, ' ');
|
|
n = AddNet(nets, n, max, ssid, SignalPercent(rssi),
|
|
caps != NULL && strchr(caps, 'P') != NULL);
|
|
}
|
|
return n;
|
|
}
|
|
|
|
/* VAR=VALUE in rc.conf: the line of VAR replaced, or added at the end */
|
|
static int
|
|
RcConfSet(const char *var, const char *value)
|
|
{
|
|
char line[1024], tmp[] = "/etc/rc.conf.mcdeXXXXXX";
|
|
size_t vlen = strlen(var);
|
|
struct stat st;
|
|
FILE *from, *to;
|
|
int fd, done = 0;
|
|
|
|
if (stat(RC_CONF, &st) != 0 || (from = fopen(RC_CONF, "r")) == NULL)
|
|
return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot read %s.", RC_CONF);
|
|
if ((fd = mkstemp(tmp)) < 0 || (to = fdopen(fd, "w")) == NULL) {
|
|
fclose(from);
|
|
return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot write beside %s.",
|
|
RC_CONF);
|
|
}
|
|
while (fgets(line, sizeof line, from) != NULL) {
|
|
if (strncmp(line, var, vlen) == 0 && line[vlen] == '=') {
|
|
if (!done)
|
|
fprintf(to, "%s=%s\n", var, value);
|
|
done = 1;
|
|
continue;
|
|
}
|
|
fputs(line, to);
|
|
}
|
|
fclose(from);
|
|
if (!done)
|
|
fprintf(to, "%s=%s\n", var, value);
|
|
if (fchmod(fd, st.st_mode & 07777) != 0 || fclose(to) != 0 ||
|
|
rename(tmp, RC_CONF) != 0) {
|
|
unlink(tmp);
|
|
return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot write %s.", RC_CONF);
|
|
}
|
|
return MCDE_SYS_OK;
|
|
}
|
|
|
|
int
|
|
mcde_sys_wifi_join(const char *ifname, const char *ssid,
|
|
const char *passphrase)
|
|
{
|
|
char flags[256];
|
|
struct stat st;
|
|
FILE *fp;
|
|
int fresh;
|
|
char *argv[4];
|
|
|
|
if (geteuid() != 0)
|
|
return mcde_sys_fail(MCDE_SYS_EPERM, "Only the administrator "
|
|
"can join a Wi-Fi network.");
|
|
if (ifname == NULL || strspn(ifname, "abcdefghijklmnopqrstuvwxyz"
|
|
"0123456789") != strlen(ifname) || ssid == NULL ||
|
|
ssid[0] == '\0' || strlen(ssid) > 32 ||
|
|
strpbrk(ssid, "\"\n\\") != NULL ||
|
|
(passphrase != NULL && (strlen(passphrase) < 8 ||
|
|
strlen(passphrase) > 63 || strpbrk(passphrase, "\"\n\\") != NULL)))
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "The network name or the "
|
|
"password cannot be used (a password has 8 to 63 "
|
|
"characters, no quotes).");
|
|
fresh = stat(WPA_CONF, &st) != 0;
|
|
if ((fp = fopen(WPA_CONF, "a")) == NULL)
|
|
return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot write %s.",
|
|
WPA_CONF);
|
|
(void) fchmod(fileno(fp), 0600);
|
|
/* the example's head (share/examples/wpa_supplicant): wpa_cli for
|
|
* the wheel group, which lets System Settings see the networks */
|
|
if (fresh)
|
|
fprintf(fp, "ctrl_interface=/var/run/wpa_supplicant/\n"
|
|
"ctrl_interface_group=wheel\n");
|
|
fprintf(fp, "\n# Added by MCDE System Settings\nnetwork={\n"
|
|
"\tssid=\"%s\"\n", ssid);
|
|
if (passphrase != NULL)
|
|
fprintf(fp, "\tpsk=\"%s\"\n", passphrase);
|
|
else
|
|
fprintf(fp, "\tkey_mgmt=NONE\n");
|
|
fprintf(fp, "\tscan_ssid=1\n}\n");
|
|
if (fclose(fp) != 0)
|
|
return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot write %s.",
|
|
WPA_CONF);
|
|
/* sysinst's lines (net.c:1203-1209) */
|
|
snprintf(flags, sizeof flags,
|
|
"\"-B -s -i %s -D bsd -c " WPA_CONF "\"", ifname);
|
|
if (RcConfSet("wpa_supplicant", "YES") != MCDE_SYS_OK ||
|
|
RcConfSet("wpa_supplicant_flags", flags) != MCDE_SYS_OK ||
|
|
RcConfSet("dhcpcd", "YES") != MCDE_SYS_OK)
|
|
return MCDE_SYS_EFAIL;
|
|
/* now: wpa_supplicant (re)started with the new network, dhcpcd for
|
|
* the address */
|
|
argv[0] = "/etc/rc.d/wpa_supplicant";
|
|
argv[1] = "restart";
|
|
argv[2] = NULL;
|
|
if (mcde_sys_run(argv) != MCDE_SYS_OK) {
|
|
argv[1] = "start";
|
|
if (mcde_sys_run(argv) != MCDE_SYS_OK)
|
|
return MCDE_SYS_EFAIL;
|
|
}
|
|
argv[0] = "/etc/rc.d/dhcpcd";
|
|
argv[1] = "restart";
|
|
if (mcde_sys_run(argv) != MCDE_SYS_OK) {
|
|
argv[1] = "start";
|
|
(void) mcde_sys_run(argv);
|
|
}
|
|
return MCDE_SYS_OK;
|
|
}
|
|
|
|
/* ---- users: wheel administers (su(1) takes only wheel); the password is
|
|
* hashed here, as passwd(1) does, with pw_gensalt(3) and crypt(3), and only
|
|
* the hash goes to usermod(8) -- the password itself is on no command line */
|
|
const char *
|
|
mcde_sys_admin_group(void)
|
|
{
|
|
return "wheel";
|
|
}
|
|
|
|
const char *
|
|
mcde_sys_default_shell(void)
|
|
{
|
|
return "/bin/ksh";
|
|
}
|
|
|
|
static char usermod[] = "/usr/sbin/usermod";
|
|
|
|
int
|
|
mcde_sys_user_set_password(const char *login, const char *password)
|
|
{
|
|
char salt[128], *hash;
|
|
char *argv[] = { usermod, "-p", NULL, NULL, NULL };
|
|
|
|
if (getpwnam(login) == NULL)
|
|
return mcde_sys_fail(MCDE_SYS_ENOENT, "There is no account "
|
|
"\"%s\".", login);
|
|
if (geteuid() != 0)
|
|
return mcde_sys_fail(MCDE_SYS_EPERM, "Only the administrator "
|
|
"can set a password: unlock first.");
|
|
if (pw_gensalt(salt, sizeof salt, "argon2id", NULL) != 0 &&
|
|
pw_gensalt(salt, sizeof salt, "blowfish", "10") != 0)
|
|
return mcde_sys_fail(MCDE_SYS_EFAIL, "No salt for the password.");
|
|
if ((hash = crypt(password, salt)) == NULL)
|
|
return mcde_sys_fail(MCDE_SYS_EFAIL, "The password cannot be "
|
|
"hashed.");
|
|
argv[2] = hash;
|
|
argv[3] = (char *) login;
|
|
return mcde_sys_run(argv);
|
|
}
|
|
|
|
/* Out of a group: NetBSD has no tool that removes a member (usermod -G only
|
|
* appends, groupmod has no members: user.c, measured 2026-11-10), so the
|
|
* line of the group is rewritten the way user.c's append_group() writes
|
|
* /etc/group: a temporary file beside it, then rename(2). */
|
|
static int
|
|
RemoveFromGroup(const char *login, const char *group)
|
|
{
|
|
char line[4096], tmp[] = "/etc/group.mcdeXXXXXX";
|
|
size_t glen = strlen(group);
|
|
struct stat st;
|
|
FILE *from, *to;
|
|
int fd, changed = 0;
|
|
|
|
if (stat(_PATH_GROUP, &st) != 0 || (from = fopen(_PATH_GROUP, "r")) == NULL)
|
|
return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot read %s.",
|
|
_PATH_GROUP);
|
|
if ((fd = mkstemp(tmp)) < 0 || (to = fdopen(fd, "w")) == NULL) {
|
|
fclose(from);
|
|
return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot write beside %s.",
|
|
_PATH_GROUP);
|
|
}
|
|
while (fgets(line, sizeof line, from) != NULL) {
|
|
char *members, *m, *next, out[4096];
|
|
size_t at;
|
|
int first = 1;
|
|
|
|
/* "wheel:*:0:root,tester,mcdet2" */
|
|
if (strncmp(line, group, glen) != 0 || line[glen] != ':' ||
|
|
(members = strrchr(line, ':')) == NULL) {
|
|
fputs(line, to);
|
|
continue;
|
|
}
|
|
members++;
|
|
members[strcspn(members, "\n")] = '\0';
|
|
at = (size_t) (members - line);
|
|
memcpy(out, line, at);
|
|
out[at] = '\0';
|
|
for (m = members; m != NULL && *m != '\0'; m = next) {
|
|
next = strchr(m, ',');
|
|
if (next != NULL)
|
|
*next++ = '\0';
|
|
if (strcmp(m, login) == 0) {
|
|
changed = 1;
|
|
continue;
|
|
}
|
|
at += (size_t) snprintf(out + at, sizeof out - at, "%s%s",
|
|
first ? "" : ",", m);
|
|
first = 0;
|
|
}
|
|
fprintf(to, "%s\n", out);
|
|
}
|
|
fclose(from);
|
|
if (fchmod(fd, st.st_mode & 07777) != 0 ||
|
|
fchown(fd, st.st_uid, st.st_gid) != 0 || fclose(to) != 0 ||
|
|
rename(tmp, _PATH_GROUP) != 0) {
|
|
unlink(tmp);
|
|
return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot write %s.",
|
|
_PATH_GROUP);
|
|
}
|
|
(void) changed;
|
|
return MCDE_SYS_OK;
|
|
}
|
|
|
|
int
|
|
mcde_sys_user_set_admin(const char *login, int admin)
|
|
{
|
|
char *argv[] = { usermod, "-G", "wheel", NULL, NULL };
|
|
|
|
if (getpwnam(login) == NULL)
|
|
return mcde_sys_fail(MCDE_SYS_ENOENT, "There is no account "
|
|
"\"%s\".", login);
|
|
if (geteuid() != 0)
|
|
return mcde_sys_fail(MCDE_SYS_EPERM, "Only the administrator "
|
|
"can change that: unlock first.");
|
|
if (!admin)
|
|
return RemoveFromGroup(login, "wheel");
|
|
if (mcde_sys_user_is_admin(login))
|
|
return MCDE_SYS_OK;
|
|
argv[3] = (char *) login; /* usermod -G appends (user.c) */
|
|
return mcde_sys_run(argv);
|
|
}
|
|
|
|
/* ---- software update: /etc/makronix/update names the source, a Makronix
|
|
* release directory (the one build.sh makes, with the sets and the
|
|
* packages); its RELEASE file has the first line of the release's
|
|
* /etc/release. sysupgrade(8) (pkgsrc sysutils/sysupgrade) brings the
|
|
* system, pkg_add -uu (pkg_add(1), base) the packages. */
|
|
static const char updateConf[] = "/etc/makronix/update";
|
|
|
|
static int
|
|
UpdateSource(char *url, size_t len)
|
|
{
|
|
char line[512];
|
|
FILE *fp = fopen(updateConf, "r");
|
|
int found = 0;
|
|
|
|
url[0] = '\0';
|
|
if (fp == NULL)
|
|
return 0;
|
|
while (!found && fgets(line, sizeof line, fp) != NULL) {
|
|
line[strcspn(line, "\r\n")] = '\0';
|
|
if (line[0] == '\0' || line[0] == '#')
|
|
continue;
|
|
snprintf(url, len, "%s", line);
|
|
found = 1;
|
|
}
|
|
fclose(fp);
|
|
return found;
|
|
}
|
|
|
|
int
|
|
mcde_sys_update_check(McdeSysUpdate *u)
|
|
{
|
|
char url[512], out[512], *e;
|
|
char *argv[] = { "/usr/bin/ftp", "-V", "-o", "-", url, NULL };
|
|
McdeSysInfo i;
|
|
|
|
memset(u, 0, sizeof *u);
|
|
u->packages = -1;
|
|
(void) mcde_sys_info(&i);
|
|
snprintf(u->current, sizeof u->current, "%s", i.version);
|
|
if (!UpdateSource(u->source, sizeof u->source)) {
|
|
snprintf(u->note, sizeof u->note, "No update source is set "
|
|
"(%s).", updateConf);
|
|
return MCDE_SYS_OK;
|
|
}
|
|
snprintf(url, sizeof url, "%s/RELEASE", u->source);
|
|
if (mcde_sys_capture(argv, out, sizeof out) != MCDE_SYS_OK)
|
|
return mcde_sys_fail(MCDE_SYS_EFAIL, "The update source does "
|
|
"not answer: %s", u->source);
|
|
/* "Makronix 0.99.10 NAME/amd64 (based on NetBSD ...)" */
|
|
if ((e = strchr(out, '\n')) != NULL)
|
|
*e = '\0';
|
|
{
|
|
char sys[64], ver[64], name[64];
|
|
|
|
if (sscanf(out, "%63s %63s %63[^/ ]", sys, ver, name) == 3)
|
|
snprintf(u->available, sizeof u->available, "%s (%s)",
|
|
ver, name);
|
|
else
|
|
snprintf(u->available, sizeof u->available, "%s", out);
|
|
}
|
|
if (strcmp(u->available, u->current) == 0)
|
|
snprintf(u->note, sizeof u->note, "The system is up to date.");
|
|
return MCDE_SYS_OK;
|
|
}
|
|
|
|
int
|
|
mcde_sys_update_apply(void)
|
|
{
|
|
static char out[65536];
|
|
char url[512], pkgpath[600];
|
|
char *sysupgrade[] = { "/usr/pkg/sbin/sysupgrade", "auto", url, NULL };
|
|
char *pkginfo[] = { "/usr/sbin/pkg_info", NULL };
|
|
char *argv[520];
|
|
char *line, *next;
|
|
int n = 0, r;
|
|
|
|
if (geteuid() != 0)
|
|
return mcde_sys_fail(MCDE_SYS_EPERM, "Only the administrator can "
|
|
"update the computer: unlock first.");
|
|
if (!UpdateSource(url, sizeof url))
|
|
return mcde_sys_fail(MCDE_SYS_ENOENT, "No update source is set "
|
|
"(%s).", updateConf);
|
|
if ((r = mcde_sys_run(sysupgrade)) != MCDE_SYS_OK)
|
|
return r;
|
|
/* the installed packages, by name ("vim-9.2.1202 comment" -> vim),
|
|
* updated from the packages of the release: pkg_add -uu */
|
|
snprintf(pkgpath, sizeof pkgpath, "%s/packages/All", url);
|
|
if (setenv("PKG_PATH", pkgpath, 1) != 0)
|
|
return mcde_sys_fail(MCDE_SYS_EFAIL, "No PKG_PATH.");
|
|
if ((r = mcde_sys_capture(pkginfo, out, sizeof out)) != MCDE_SYS_OK)
|
|
return r;
|
|
argv[n++] = "/usr/sbin/pkg_add";
|
|
argv[n++] = "-uu";
|
|
for (line = out; line != NULL && *line != '\0' &&
|
|
n < (int) (sizeof argv / sizeof argv[0]) - 1; line = next) {
|
|
char *sp, *dash;
|
|
|
|
next = strchr(line, '\n');
|
|
if (next != NULL)
|
|
*next++ = '\0';
|
|
if ((sp = strpbrk(line, " \t")) != NULL)
|
|
*sp = '\0';
|
|
if ((dash = strrchr(line, '-')) != NULL && dash != line)
|
|
*dash = '\0';
|
|
if (line[0] != '\0')
|
|
argv[n++] = line;
|
|
}
|
|
argv[n] = NULL;
|
|
if (n == 2)
|
|
return MCDE_SYS_OK;
|
|
return mcde_sys_run(argv);
|
|
}
|
|
|
|
/* ---- a process's arguments: sysctl kern.proc_args.PID.argv, NUL
|
|
* separated (sysctl(7): KERN_PROC_ARGS, KERN_PROC_ARGV) */
|
|
int
|
|
mcde_sys_process_args(pid_t pid, char *buf, size_t len)
|
|
{
|
|
int mib[4] = { CTL_KERN, KERN_PROC_ARGS, (int) pid, KERN_PROC_ARGV };
|
|
size_t n = len - 1;
|
|
|
|
if (sysctl(mib, 4, buf, &n, NULL, 0) != 0 || n == 0)
|
|
return -1;
|
|
if (buf[n - 1] != '\0')
|
|
buf[n++] = '\0';
|
|
return (int) n;
|
|
}
|
|
|
|
/* ---- sharing: rc.conf and the rc.d scripts (rc.subr(8)); the servers of
|
|
* pkgsrc bring their scripts in share/examples/rc.d, copied to /etc/rc.d
|
|
* when first switched on, as PKG_RCD_SCRIPTS=YES would
|
|
* (docs/reference/sharing.txt) */
|
|
static const struct {
|
|
const char *vars[5]; /* rc.conf switches, the first one tells */
|
|
const char *scripts[5]; /* rc.d scripts, in start order */
|
|
const char *main; /* the one whose status tells "running" */
|
|
const char *binary; /* there when installed */
|
|
} services[MCDE_SHARE_COUNT] = {
|
|
[MCDE_SHARE_SSH] = { { "sshd" }, { "sshd" }, "sshd",
|
|
"/usr/sbin/sshd" },
|
|
[MCDE_SHARE_SMB] = { { "samba" }, { "samba" }, "samba",
|
|
"/usr/pkg/sbin/smbd" },
|
|
/* rpcbind is switched on, never off: others may need it. statd
|
|
* and lockd share one script, nfslocking (src/etc/rc.d/nfslocking) */
|
|
[MCDE_SHARE_NFS] = { { "nfs_server", "mountd", "statd", "lockd",
|
|
"rpcbind" }, { "rpcbind", "mountd", "nfsd", "nfslocking" }, "nfsd",
|
|
"/usr/sbin/nfsd" },
|
|
[MCDE_SHARE_AFP] = { { "netatalk" }, { "netatalk" }, "netatalk",
|
|
"/usr/pkg/sbin/netatalk" },
|
|
[MCDE_SHARE_APPLETALK] = { { "atalkd", "papd" }, { "atalkd", "papd" },
|
|
"atalkd", "/usr/pkg/sbin/atalkd" },
|
|
};
|
|
|
|
static int
|
|
RcScript(const char *name, const char *how)
|
|
{
|
|
char path[128];
|
|
char *argv[3];
|
|
|
|
snprintf(path, sizeof path, "/etc/rc.d/%s", name);
|
|
if (access(path, X_OK) != 0)
|
|
return MCDE_SYS_ENOENT;
|
|
argv[0] = path;
|
|
argv[1] = (char *) how;
|
|
argv[2] = NULL;
|
|
return mcde_sys_run(argv);
|
|
}
|
|
|
|
/* a pkgsrc rc.d script into /etc/rc.d, if it is only an example yet */
|
|
static void
|
|
InstallScript(const char *name)
|
|
{
|
|
char from[160], to[128], buf[4096];
|
|
FILE *in, *out;
|
|
size_t n;
|
|
|
|
snprintf(to, sizeof to, "/etc/rc.d/%s", name);
|
|
snprintf(from, sizeof from, "/usr/pkg/share/examples/rc.d/%s", name);
|
|
if (access(to, F_OK) == 0 || (in = fopen(from, "r")) == NULL)
|
|
return;
|
|
if ((out = fopen(to, "w")) != NULL) {
|
|
while ((n = fread(buf, 1, sizeof buf, in)) > 0)
|
|
(void) fwrite(buf, 1, n, out);
|
|
(void) fchmod(fileno(out), 0555);
|
|
fclose(out);
|
|
}
|
|
fclose(in);
|
|
}
|
|
|
|
int
|
|
mcde_sys_service_state(McdeSysShareService s, McdeSysServiceState *st)
|
|
{
|
|
char cmd[256];
|
|
char *argv[4];
|
|
|
|
memset(st, 0, sizeof *st);
|
|
if ((int) s < 0 || s >= MCDE_SHARE_COUNT)
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "No such service.");
|
|
st->installed = access(services[s].binary, X_OK) == 0;
|
|
if (!st->installed)
|
|
return MCDE_SYS_OK;
|
|
/* rc.subr reads rc.conf and the defaults as the boot does */
|
|
snprintf(cmd, sizeof cmd, ". /etc/rc.subr; load_rc_config %s; "
|
|
"checkyesno %s", services[s].vars[0], services[s].vars[0]);
|
|
argv[0] = "/bin/sh";
|
|
argv[1] = "-c";
|
|
argv[2] = cmd;
|
|
argv[3] = NULL;
|
|
st->enabled = mcde_sys_run(argv) == MCDE_SYS_OK;
|
|
st->running = RcScript(services[s].main, "onestatus") == MCDE_SYS_OK;
|
|
return MCDE_SYS_OK;
|
|
}
|
|
|
|
int
|
|
mcde_sys_service_set(McdeSysShareService s, int on)
|
|
{
|
|
int i, r = MCDE_SYS_OK;
|
|
|
|
if ((int) s < 0 || s >= MCDE_SHARE_COUNT)
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "No such service.");
|
|
if (geteuid() != 0)
|
|
return mcde_sys_fail(MCDE_SYS_EPERM, "Only the administrator "
|
|
"can switch a service.");
|
|
if (access(services[s].binary, X_OK) != 0)
|
|
return mcde_sys_fail(MCDE_SYS_ENOENT, "The server is not "
|
|
"installed (%s).", services[s].binary);
|
|
for (i = 0; i < 5 && services[s].vars[i] != NULL; i++) {
|
|
/* rpcbind: on with NFS, left as it is when NFS goes off */
|
|
if (!on && strcmp(services[s].vars[i], "rpcbind") == 0)
|
|
continue;
|
|
if (RcConfSet(services[s].vars[i], on ? "YES" : "NO") !=
|
|
MCDE_SYS_OK)
|
|
return MCDE_SYS_EFAIL;
|
|
}
|
|
/* mountd does not start without /etc/exports (rc.d/mountd: "is not
|
|
* readable", measured): the file with the shares there are */
|
|
if (on && s == MCDE_SHARE_NFS && access("/etc/exports", F_OK) != 0) {
|
|
FILE *fp = fopen("/etc/exports", "w");
|
|
|
|
if (fp != NULL)
|
|
fclose(fp);
|
|
}
|
|
if (on) {
|
|
for (i = 0; i < 5 && services[s].scripts[i] != NULL; i++) {
|
|
InstallScript(services[s].scripts[i]);
|
|
if (RcScript(services[s].scripts[i], "onestatus") !=
|
|
MCDE_SYS_OK &&
|
|
RcScript(services[s].scripts[i], "start") !=
|
|
MCDE_SYS_OK)
|
|
r = mcde_sys_fail(MCDE_SYS_EFAIL, "%s did not "
|
|
"start.", services[s].scripts[i]);
|
|
}
|
|
} else {
|
|
for (i = 4; i >= 0; i--)
|
|
if (services[s].scripts[i] != NULL &&
|
|
strcmp(services[s].scripts[i], "rpcbind") != 0)
|
|
(void) RcScript(services[s].scripts[i],
|
|
"onestop");
|
|
}
|
|
return r;
|
|
}
|
|
|
|
const char *
|
|
mcde_sys_exports_file(void)
|
|
{
|
|
return "/etc/exports";
|
|
}
|
|
|
|
const char *
|
|
mcde_sys_smb_conf(void)
|
|
{
|
|
return access(services[MCDE_SHARE_SMB].binary, X_OK) == 0 ?
|
|
"/usr/pkg/etc/samba/smb.conf" : NULL;
|
|
}
|
|
|
|
const char *
|
|
mcde_sys_afp_conf(void)
|
|
{
|
|
return access(services[MCDE_SHARE_AFP].binary, X_OK) == 0 ?
|
|
"/usr/pkg/etc/afp.conf" : NULL;
|
|
}
|
|
|
|
/* exports(5): the folder to every host, root mapped to nobody */
|
|
void
|
|
mcde_sys_export_line(const char *path, char *buf, size_t len)
|
|
{
|
|
snprintf(buf, len, "%s -maproot=nobody", path);
|
|
}
|
|
|
|
void
|
|
mcde_sys_shares_reload(void)
|
|
{
|
|
/* mountd reads /etc/exports again on reload (rc.d/mountd) */
|
|
if (RcScript("mountd", "onestatus") == MCDE_SYS_OK)
|
|
(void) RcScript("mountd", "reload");
|
|
if (RcScript("samba", "onestatus") == MCDE_SYS_OK)
|
|
(void) RcScript("samba", "restart");
|
|
if (RcScript("netatalk", "onestatus") == MCDE_SYS_OK)
|
|
(void) RcScript("netatalk", "restart");
|
|
}
|
|
|
|
/* ---- the network: DNS-SD through mdnsd (dns_sd.h, libdns_sd in base):
|
|
* one DNSServiceBrowse per kind, a DNSServiceResolve per service found,
|
|
* their sockets read with select(2) until the time is up
|
|
* (docs/reference/dns-sd.txt) */
|
|
#define MAX_RESOLVES 64
|
|
|
|
static const struct {
|
|
const char *type;
|
|
int kind;
|
|
} browseTypes[] = {
|
|
{ "_smb._tcp", MCDE_NET_HAS_SMB },
|
|
{ "_afpovertcp._tcp", MCDE_NET_HAS_AFP },
|
|
{ "_nfs._tcp", MCDE_NET_HAS_NFS },
|
|
{ "_ssh._tcp", MCDE_NET_HAS_SSH },
|
|
{ "_sftp-ssh._tcp", MCDE_NET_HAS_SSH },
|
|
{ "_workstation._tcp", MCDE_NET_HAS_ANY },
|
|
};
|
|
#define NTYPES (sizeof browseTypes / sizeof browseTypes[0])
|
|
|
|
typedef struct {
|
|
McdeSysNetHost *hosts;
|
|
size_t max;
|
|
int n;
|
|
DNSServiceRef res[MAX_RESOLVES];
|
|
int kind[MAX_RESOLVES];
|
|
char name[MAX_RESOLVES][128];
|
|
int nres;
|
|
} BrowseState;
|
|
|
|
static BrowseState *browse;
|
|
|
|
static void DNSSD_API
|
|
Resolved(DNSServiceRef ref, DNSServiceFlags flags, uint32_t ifindex,
|
|
DNSServiceErrorType err, const char *fullname, const char *hosttarget,
|
|
uint16_t port, uint16_t txtLen, const unsigned char *txt, void *ctx)
|
|
{
|
|
int i = (int) (intptr_t) ctx;
|
|
|
|
(void) ref; (void) flags; (void) ifindex; (void) fullname;
|
|
(void) port; (void) txtLen; (void) txt;
|
|
if (err != kDNSServiceErr_NoError || browse == NULL || i < 0 ||
|
|
i >= browse->nres)
|
|
return;
|
|
browse->n = mcde_sys_net_add(browse->hosts, browse->n, browse->max,
|
|
browse->name[i], hosttarget, browse->kind[i]);
|
|
}
|
|
|
|
static void DNSSD_API
|
|
Found(DNSServiceRef ref, DNSServiceFlags flags, uint32_t ifindex,
|
|
DNSServiceErrorType err, const char *name, const char *regtype,
|
|
const char *domain, void *ctx)
|
|
{
|
|
int kind = (int) (intptr_t) ctx, i;
|
|
|
|
(void) ref;
|
|
if (err != kDNSServiceErr_NoError || !(flags & kDNSServiceFlagsAdd) ||
|
|
browse == NULL || browse->nres >= MAX_RESOLVES)
|
|
return;
|
|
i = browse->nres;
|
|
browse->kind[i] = kind;
|
|
snprintf(browse->name[i], sizeof browse->name[i], "%s", name);
|
|
if (DNSServiceResolve(&browse->res[i], 0, ifindex, name, regtype,
|
|
domain, Resolved, (void *) (intptr_t) i) == kDNSServiceErr_NoError)
|
|
browse->nres++;
|
|
}
|
|
|
|
int
|
|
mcde_sys_net_browse(McdeSysNetHost *hosts, size_t max, int timeout_ms)
|
|
{
|
|
static BrowseState st;
|
|
DNSServiceRef refs[NTYPES];
|
|
struct timeval end, now;
|
|
size_t t;
|
|
int i, started = 0;
|
|
|
|
memset(&st, 0, sizeof st);
|
|
st.hosts = hosts;
|
|
st.max = max;
|
|
browse = &st;
|
|
for (t = 0; t < NTYPES; t++) {
|
|
refs[t] = NULL;
|
|
if (DNSServiceBrowse(&refs[t], 0, 0, browseTypes[t].type, NULL,
|
|
Found, (void *) (intptr_t) browseTypes[t].kind) ==
|
|
kDNSServiceErr_NoError)
|
|
started++;
|
|
else
|
|
refs[t] = NULL;
|
|
}
|
|
if (started == 0) {
|
|
browse = NULL;
|
|
return mcde_sys_fail(MCDE_SYS_ENOTSUP, "The computers on the "
|
|
"network cannot be looked for: mdnsd does not run "
|
|
"(mdnsd=YES in rc.conf).");
|
|
}
|
|
gettimeofday(&end, NULL);
|
|
end.tv_sec += timeout_ms / 1000;
|
|
end.tv_usec += (timeout_ms % 1000) * 1000;
|
|
if (end.tv_usec >= 1000000) {
|
|
end.tv_sec++;
|
|
end.tv_usec -= 1000000;
|
|
}
|
|
for (;;) {
|
|
fd_set fds;
|
|
struct timeval left;
|
|
int maxfd = -1;
|
|
|
|
gettimeofday(&now, NULL);
|
|
left.tv_sec = end.tv_sec - now.tv_sec;
|
|
left.tv_usec = end.tv_usec - now.tv_usec;
|
|
if (left.tv_usec < 0) {
|
|
left.tv_sec--;
|
|
left.tv_usec += 1000000;
|
|
}
|
|
if (left.tv_sec < 0)
|
|
break;
|
|
FD_ZERO(&fds);
|
|
for (t = 0; t < NTYPES; t++)
|
|
if (refs[t] != NULL) {
|
|
int fd = DNSServiceRefSockFD(refs[t]);
|
|
|
|
FD_SET(fd, &fds);
|
|
if (fd > maxfd)
|
|
maxfd = fd;
|
|
}
|
|
for (i = 0; i < st.nres; i++) {
|
|
int fd = DNSServiceRefSockFD(st.res[i]);
|
|
|
|
FD_SET(fd, &fds);
|
|
if (fd > maxfd)
|
|
maxfd = fd;
|
|
}
|
|
if (select(maxfd + 1, &fds, NULL, NULL, &left) <= 0)
|
|
break;
|
|
for (t = 0; t < NTYPES; t++)
|
|
if (refs[t] != NULL &&
|
|
FD_ISSET(DNSServiceRefSockFD(refs[t]), &fds))
|
|
(void) DNSServiceProcessResult(refs[t]);
|
|
for (i = 0; i < st.nres; i++)
|
|
if (FD_ISSET(DNSServiceRefSockFD(st.res[i]), &fds))
|
|
(void) DNSServiceProcessResult(st.res[i]);
|
|
}
|
|
for (t = 0; t < NTYPES; t++)
|
|
if (refs[t] != NULL)
|
|
DNSServiceRefDeallocate(refs[t]);
|
|
for (i = 0; i < st.nres; i++)
|
|
DNSServiceRefDeallocate(st.res[i]);
|
|
browse = NULL;
|
|
return st.n;
|
|
}
|