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>
280 lines
8.6 KiB
C
280 lines
8.6 KiB
C
/*
|
|
* netmount.c -- MCDE: what other computers share, and connecting to it
|
|
*
|
|
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
|
|
*
|
|
* The tools are the same on NetBSD and Linux, found where each system keeps
|
|
* them (base, or /usr/pkg from pkgsrc):
|
|
*
|
|
* showmount -e HOST NFS exports ("Exports list on HOST:", then a
|
|
* line per path)
|
|
* smbclient -g -N -L HOST SMB shares, one "Disk|NAME|COMMENT" a line
|
|
* sshfs sftp:// -- a password, if one is needed, on
|
|
* its standard input (-o password_stdin)
|
|
* mount_nfs / mount.nfs nfs:// -- NetBSD lets a user mount on a
|
|
* folder of his own when vfs.generic.usermount
|
|
* is 1 (sysctl(7)); Linux wants root
|
|
* mount_afp afp:// (afpfs-ng); it takes the password in
|
|
* the URL
|
|
*
|
|
* Finding the computers is the backend's (mcde_sys_net_browse: DNS-SD).
|
|
* C99 and POSIX (ext/src/mcde/AGENTS.md).
|
|
*/
|
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <strings.h>
|
|
#include <unistd.h>
|
|
|
|
#include <Mcde/McdeSys.h>
|
|
|
|
#include "private.h"
|
|
|
|
static const char *const showmountPaths[] = { "/usr/bin/showmount",
|
|
"/sbin/showmount", "/usr/sbin/showmount", NULL };
|
|
static const char *const smbclientPaths[] = { "/usr/pkg/bin/smbclient",
|
|
"/usr/bin/smbclient", NULL };
|
|
static const char *const sshfsPaths[] = { "/usr/pkg/bin/sshfs",
|
|
"/usr/bin/sshfs", NULL };
|
|
static const char *const mountNfsPaths[] = { "/sbin/mount_nfs",
|
|
"/sbin/mount.nfs", "/usr/sbin/mount.nfs", NULL };
|
|
static const char *const mountAfpPaths[] = { "/usr/pkg/bin/mount_afp",
|
|
"/usr/bin/mount_afp", NULL };
|
|
static const char *const fusermountPaths[] = { "/usr/bin/fusermount",
|
|
"/bin/fusermount", "/usr/bin/fusermount3", NULL };
|
|
static const char *const umountPaths[] = { "/sbin/umount", "/bin/umount",
|
|
"/usr/bin/umount", NULL };
|
|
|
|
/* a host name: letters, digits, dots, dashes -- nothing else reaches a
|
|
* command line */
|
|
static int
|
|
GoodHost(const char *h)
|
|
{
|
|
return h != NULL && h[0] != '\0' && h[0] != '-' &&
|
|
strspn(h, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
"0123456789.-_:") == strlen(h);
|
|
}
|
|
|
|
int
|
|
mcde_sys_net_shares(const char *host, int kind, char (*names)[256],
|
|
size_t max)
|
|
{
|
|
static char out[16384];
|
|
char *argv[8], *line, *last = NULL;
|
|
const char *tool;
|
|
size_t n = 0;
|
|
|
|
if (!GoodHost(host))
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "\"%s\" is no computer "
|
|
"name.", host != NULL ? host : "");
|
|
if (kind == MCDE_NET_HAS_NFS) {
|
|
if ((tool = mcde_sys_which(showmountPaths)) == NULL)
|
|
return mcde_sys_fail(MCDE_SYS_ENOTSUP, "showmount is "
|
|
"not there.");
|
|
argv[0] = (char *) tool;
|
|
argv[1] = "-e";
|
|
argv[2] = (char *) host;
|
|
argv[3] = NULL;
|
|
} else if (kind == MCDE_NET_HAS_SMB) {
|
|
if ((tool = mcde_sys_which(smbclientPaths)) == NULL)
|
|
return mcde_sys_fail(MCDE_SYS_ENOTSUP, "smbclient "
|
|
"(Samba) is not there.");
|
|
argv[0] = (char *) tool;
|
|
argv[1] = "-g";
|
|
argv[2] = "-N";
|
|
argv[3] = "-L";
|
|
argv[4] = (char *) host;
|
|
argv[5] = NULL;
|
|
} else
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "No list of shares for "
|
|
"that kind.");
|
|
/* the other computer may not answer: a while, not for ever */
|
|
if (mcde_sys_capture_limited(argv, out, sizeof out, 8) != MCDE_SYS_OK)
|
|
return -1;
|
|
for (line = strtok_r(out, "\n", &last); line != NULL && n < max;
|
|
line = strtok_r(NULL, "\n", &last)) {
|
|
if (kind == MCDE_NET_HAS_NFS) {
|
|
if (line[0] != '/')
|
|
continue;
|
|
line[strcspn(line, " \t")] = '\0';
|
|
snprintf(names[n++], 256, "%s", line);
|
|
} else {
|
|
char *bar;
|
|
|
|
if (strncmp(line, "Disk|", 5) != 0)
|
|
continue;
|
|
if ((bar = strchr(line + 5, '|')) != NULL)
|
|
*bar = '\0';
|
|
/* the administrative shares end in $ */
|
|
if (line[5] == '\0' ||
|
|
line[5 + strlen(line + 5) - 1] == '$')
|
|
continue;
|
|
snprintf(names[n++], 256, "%s", line + 5);
|
|
}
|
|
}
|
|
return (int) n;
|
|
}
|
|
|
|
/* SCHEME://[USER@]HOST[/PATH] into its parts */
|
|
static int
|
|
ParseUrl(const char *url, char *scheme, char *user, char *host, char *path)
|
|
{
|
|
const char *p = strstr(url, "://"), *at, *slash;
|
|
size_t n;
|
|
|
|
scheme[0] = user[0] = host[0] = path[0] = '\0';
|
|
if (p == NULL || (size_t) (p - url) >= 16)
|
|
return 0;
|
|
snprintf(scheme, 16, "%.*s", (int) (p - url), url);
|
|
p += 3;
|
|
slash = strchr(p, '/');
|
|
at = strchr(p, '@');
|
|
if (at != NULL && (slash == NULL || at < slash)) {
|
|
snprintf(user, 64, "%.*s", (int) (at - p), p);
|
|
p = at + 1;
|
|
}
|
|
n = slash != NULL ? (size_t) (slash - p) : strlen(p);
|
|
if (n == 0 || n >= 256)
|
|
return 0;
|
|
snprintf(host, 256, "%.*s", (int) n, p);
|
|
if (slash != NULL)
|
|
snprintf(path, 512, "%s", slash);
|
|
return GoodHost(host) &&
|
|
strspn(user, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
"0123456789._-") == strlen(user) && strchr(path, '\n') == NULL;
|
|
}
|
|
|
|
int
|
|
mcde_sys_net_mount(const char *url, const char *mountpoint,
|
|
const char *password)
|
|
{
|
|
char scheme[16], user[64], host[256], path[512], target[900];
|
|
const char *tool;
|
|
char *argv[12];
|
|
int n = 0;
|
|
|
|
if (url == NULL || mountpoint == NULL || mountpoint[0] != '/' ||
|
|
!ParseUrl(url, scheme, user, host, path))
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "\"%s\" is no address "
|
|
"this computer can connect to.", url != NULL ? url : "");
|
|
if (strcmp(scheme, "sftp") == 0 || strcmp(scheme, "ssh") == 0) {
|
|
if ((tool = mcde_sys_which(sshfsPaths)) == NULL)
|
|
return mcde_sys_fail(MCDE_SYS_ENOTSUP, "sshfs is not "
|
|
"there.");
|
|
snprintf(target, sizeof target, "%s%s%s:%s", user,
|
|
user[0] != '\0' ? "@" : "", host, path[0] != '\0' ? path :
|
|
"");
|
|
argv[n++] = (char *) tool;
|
|
argv[n++] = target;
|
|
argv[n++] = (char *) mountpoint;
|
|
argv[n++] = "-o";
|
|
/* a computer not met before is taken, as a first ssh asks */
|
|
argv[n++] = "StrictHostKeyChecking=accept-new";
|
|
if (password != NULL) {
|
|
argv[n++] = "-o";
|
|
argv[n++] = "password_stdin";
|
|
} else {
|
|
argv[n++] = "-o";
|
|
argv[n++] = "BatchMode=yes";
|
|
}
|
|
argv[n] = NULL;
|
|
if (password != NULL) {
|
|
char input[300];
|
|
|
|
snprintf(input, sizeof input, "%s\n", password);
|
|
n = mcde_sys_run_input(argv, input);
|
|
memset(input, 0, sizeof input);
|
|
return n;
|
|
}
|
|
return mcde_sys_run(argv);
|
|
}
|
|
if (strcmp(scheme, "nfs") == 0) {
|
|
if ((tool = mcde_sys_which(mountNfsPaths)) == NULL)
|
|
return mcde_sys_fail(MCDE_SYS_ENOTSUP, "No NFS client.");
|
|
snprintf(target, sizeof target, "%s:%s", host,
|
|
path[0] != '\0' ? path : "/");
|
|
argv[n++] = (char *) tool;
|
|
argv[n++] = target;
|
|
argv[n++] = (char *) mountpoint;
|
|
argv[n] = NULL;
|
|
return mcde_sys_run(argv);
|
|
}
|
|
if (strcmp(scheme, "afp") == 0) {
|
|
if ((tool = mcde_sys_which(mountAfpPaths)) == NULL)
|
|
return mcde_sys_fail(MCDE_SYS_ENOTSUP, "mount_afp "
|
|
"(afpfs-ng) is not there.");
|
|
if (password != NULL && strpbrk(password, "@/:") != NULL)
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "This password "
|
|
"cannot go into an AFP address.");
|
|
snprintf(target, sizeof target, "afp://%s%s%s%s%s%s",
|
|
user, password != NULL ? ":" : "",
|
|
password != NULL ? password : "",
|
|
user[0] != '\0' || password != NULL ? "@" : "", host, path);
|
|
argv[n++] = (char *) tool;
|
|
argv[n++] = target;
|
|
argv[n++] = (char *) mountpoint;
|
|
argv[n] = NULL;
|
|
n = mcde_sys_run(argv);
|
|
memset(target, 0, sizeof target);
|
|
return n;
|
|
}
|
|
return mcde_sys_fail(MCDE_SYS_ENOTSUP, "%s:// cannot be connected to "
|
|
"here yet.", scheme);
|
|
}
|
|
|
|
int
|
|
mcde_sys_net_unmount(const char *mountpoint)
|
|
{
|
|
const char *tool;
|
|
char *argv[4];
|
|
|
|
if (mountpoint == NULL || mountpoint[0] != '/')
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "No folder.");
|
|
/* FUSE on Linux unmounts as the user with fusermount; NetBSD's
|
|
* puffs and NFS with umount */
|
|
if ((tool = mcde_sys_which(fusermountPaths)) != NULL) {
|
|
argv[0] = (char *) tool;
|
|
argv[1] = "-u";
|
|
argv[2] = (char *) mountpoint;
|
|
argv[3] = NULL;
|
|
if (mcde_sys_run(argv) == MCDE_SYS_OK)
|
|
return MCDE_SYS_OK;
|
|
}
|
|
if ((tool = mcde_sys_which(umountPaths)) == NULL)
|
|
return mcde_sys_fail(MCDE_SYS_ENOTSUP, "No umount.");
|
|
argv[0] = (char *) tool;
|
|
argv[1] = (char *) mountpoint;
|
|
argv[2] = NULL;
|
|
return mcde_sys_run(argv);
|
|
}
|
|
|
|
int
|
|
mcde_sys_net_add(McdeSysNetHost *hosts, int n, size_t max, const char *name,
|
|
const char *host, int kind)
|
|
{
|
|
char h[256];
|
|
size_t l;
|
|
int i;
|
|
|
|
snprintf(h, sizeof h, "%s", host);
|
|
/* "kitt.local." -> "kitt.local" */
|
|
if ((l = strlen(h)) > 0 && h[l - 1] == '.')
|
|
h[l - 1] = '\0';
|
|
if (h[0] == '\0')
|
|
return n;
|
|
for (i = 0; i < n; i++)
|
|
if (strcasecmp(hosts[i].host, h) == 0) {
|
|
hosts[i].kinds |= kind;
|
|
return n;
|
|
}
|
|
if ((size_t) n >= max)
|
|
return n;
|
|
snprintf(hosts[n].name, sizeof hosts[n].name, "%s", name);
|
|
snprintf(hosts[n].host, sizeof hosts[n].host, "%s", h);
|
|
hosts[n].kinds = kind;
|
|
return n + 1;
|
|
}
|