Files
mcde/lib/McdeSys/netmount.c
T
lotherkandClaude Opus 5.5 40f0cdaaee MCDE: no ssh option injection from mDNS -- McdeSys keeps only good host names, dtfile starts ssh without a shell
Review 2026-11-13 (tasksd t0076): dtfile's network view ran
system("dtterm -title '%s' -e ssh '%s' &") with the mDNS service name and
host target, and its comment said McdeSys had checked the host -- it had
not: mcde_sys_net_add() only copied it.  A host advertising _ssh._tcp with
the target "-oProxyCommand=..." reached ssh as an option, and ssh runs a
ProxyCommand through a shell.

- netmount.c mcde_sys_net_add(): GoodHost() once for every name from the
  network (no leading -, letters/digits/. - _ : only); control characters
  in the free-text service name become ?.
- NetView.c: SshTerminal() forks and execs dtterm -e ssh -- HOST, no shell,
  detached by a second fork (daemon(3) pattern); no system() is left in
  the MCDE programs.
- test_netmount (make check): 12 host names, kept or dropped as expected,
  and the control character; PASS with test_sysinfo
  (build/logs/mcde-linux-netview-2026-11-13.log; dtfile builds).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 22:38:53 +00:00

291 lines
9.1 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';
/*
* The name comes from the network (mDNS, a host answering a browse):
* it is checked here, once, so that every program that shows or opens
* the list may rely on it. A target such as "-oProxyCommand=..." would
* otherwise reach ssh as an option (review 2026-11-13, tasksd t0076).
*/
if (!GoodHost(h))
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);
/* the service name is free text: no control characters on screen */
for (l = 0; hosts[n].name[l] != '\0'; l++)
if ((unsigned char) hosts[n].name[l] < ' ' ||
hosts[n].name[l] == 0x7f)
hosts[n].name[l] = '?';
snprintf(hosts[n].host, sizeof hosts[n].host, "%s", h);
hosts[n].kinds = kind;
return n + 1;
}