netmount.c's twelve sentences are messages 10-21 of mcdesys.cat now: no computer name, showmount/smbclient/sshfs/mount_afp/NFS/umount not there, no list of shares for that kind, no address this computer can connect to, a password that cannot be used, a scheme that is not there yet, no folder. The German copy in programs/localized/de_DE.UTF-8/msg/mcdesys.msg keeps the same numbers (ASCII in these low-level texts: they also reach programs without a render table). Measured with a small runner (setlocale + the public calls): LANG=C: "bogus:// cannot be connected to here yet.", "\"smb://bad host/share\" is no address this computer can connect to.", "No folder."; LANG=de_DE.UTF-8 (localedef/LOCPATH): "bogus:// kann hier noch nicht verbunden werden.", "\"smb://bad host/share\" ist keine Adresse, zu der dieser Rechner verbinden kann.", "Kein Ordner." make mcde-linux MCDE_DIRS="install lib/McdeSys", "install programs/localized/C/msg" and "check lib/McdeSys" exit=0; MCDE_DIRS=plist: "3487 installed names under dt/, 8682 in the PLISTs", ok.
393 lines
13 KiB
C
393 lines
13 KiB
C
/*
|
|
* netmount.c -- MCDE: what other computers share, and connecting to it
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*
|
|
* 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); the address carries "-" as the
|
|
* password and mount_afp asks getpass(3), which
|
|
* reads the standard input of the job runner (no
|
|
* controlling terminal): never in argv, ps(1)
|
|
*
|
|
* 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 <errno.h>
|
|
#include <poll.h>
|
|
#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[] = { MCDE_PKG_PREFIX "/bin/smbclient",
|
|
"/usr/bin/smbclient", NULL };
|
|
static const char *const sshfsPaths[] = { MCDE_PKG_PREFIX "/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[] = { MCDE_PKG_PREFIX "/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, mcde_sys_msg(MSG_NET_NO_HOST, "\"%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, mcde_sys_msg(MSG_NET_NO_SHOWMOUNT, "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, mcde_sys_msg(MSG_NET_NO_SMBCLIENT, "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, mcde_sys_msg(MSG_NET_NO_SHARE_LIST, "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);
|
|
/* a user that starts with "-" would be an option of sshfs: its first
|
|
* argument is "USER@HOST:PATH" (as a host may not start with "-") */
|
|
return GoodHost(host) && user[0] != '-' &&
|
|
strspn(user, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
"0123456789._-") == strlen(user) && strchr(path, '\n') == NULL;
|
|
}
|
|
|
|
/* The address mount_afp gets as argv[1] (it shows in ps(1)): never the
|
|
* password, only "-" where there is one -- mount_afp then asks getpass(3).
|
|
* The function is not given the password, so it cannot put it there. */
|
|
static void
|
|
AfpAddress(const char *user, const char *host, const char *path,
|
|
int has_password, char *buf, size_t len)
|
|
{
|
|
snprintf(buf, len, "afp://%s%s%s%s%s", user, has_password ? ":-" : "",
|
|
user[0] != '\0' || has_password ? "@" : "", host, path);
|
|
}
|
|
|
|
/* the address of URL for mount_afp with or without a password, into BUF; 0 if
|
|
* URL is no address (the tests' view of AfpAddress) */
|
|
int
|
|
mcde_sys_net_afp_address(const char *url, int has_password, char *buf,
|
|
size_t len)
|
|
{
|
|
char scheme[16], user[64], host[256], path[512];
|
|
|
|
if (url == NULL || !ParseUrl(url, scheme, user, host, path))
|
|
return 0;
|
|
AfpAddress(user, host, path, has_password, buf, len);
|
|
return 1;
|
|
}
|
|
|
|
/* 1 when URL would reach a command line (the tests' view of ParseUrl) */
|
|
int
|
|
mcde_sys_net_url_ok(const char *url)
|
|
{
|
|
char scheme[16], user[64], host[256], path[512];
|
|
|
|
return url != NULL && ParseUrl(url, scheme, user, host, path);
|
|
}
|
|
|
|
/* the command that connects URL to MOUNTPOINT: the tool, its arguments, and
|
|
* what goes to its standard input (a password for sshfs). Kept in one
|
|
* structure so that mcde_sys_net_mount (waits) and mcde_sys_net_mount_start
|
|
* (does not) run the same thing. A password is in here: the caller clears
|
|
* the plan when it has used it. */
|
|
typedef struct {
|
|
char target[900];
|
|
char input[300];
|
|
char *argv[12];
|
|
int has_input;
|
|
} MountPlan;
|
|
|
|
/* fills PLAN; MCDE_SYS_OK, or a failure STATUS with its sentence */
|
|
static int
|
|
MountPlanFor(const char *url, const char *mountpoint, const char *password,
|
|
MountPlan *plan)
|
|
{
|
|
char scheme[16], user[64], host[256], path[512];
|
|
const char *tool;
|
|
char **argv = plan->argv;
|
|
char *target = plan->target;
|
|
size_t tsize = sizeof plan->target;
|
|
int n = 0;
|
|
|
|
memset(plan, 0, sizeof *plan);
|
|
if (url == NULL || mountpoint == NULL || mountpoint[0] != '/' ||
|
|
!ParseUrl(url, scheme, user, host, path))
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_NET_NO_ADDRESS, "\"%s\" is no address this computer can connect to."), url != NULL ? url : "");
|
|
/* a password is one line of the tool's standard input */
|
|
if (password != NULL && (strchr(password, '\n') != NULL ||
|
|
strlen(password) + 2 > sizeof plan->input))
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_NET_BAD_PASSWORD, "This password cannot be used (no line break, at most %d characters)."),
|
|
(int) sizeof plan->input - 3);
|
|
if (strcmp(scheme, "sftp") == 0 || strcmp(scheme, "ssh") == 0) {
|
|
if ((tool = mcde_sys_which(sshfsPaths)) == NULL)
|
|
return mcde_sys_fail(MCDE_SYS_ENOTSUP, mcde_sys_msg(MSG_NET_NO_SSHFS, "sshfs is not there."));
|
|
snprintf(target, tsize, "%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";
|
|
snprintf(plan->input, sizeof plan->input, "%s\n",
|
|
password);
|
|
plan->has_input = 1;
|
|
} else {
|
|
argv[n++] = "-o";
|
|
argv[n++] = "BatchMode=yes";
|
|
}
|
|
argv[n] = NULL;
|
|
return MCDE_SYS_OK;
|
|
}
|
|
if (strcmp(scheme, "nfs") == 0) {
|
|
if ((tool = mcde_sys_which(mountNfsPaths)) == NULL)
|
|
return mcde_sys_fail(MCDE_SYS_ENOTSUP,
|
|
mcde_sys_msg(MSG_NET_NO_NFS, "No NFS client."));
|
|
snprintf(target, tsize, "%s:%s", host,
|
|
path[0] != '\0' ? path : "/");
|
|
argv[n++] = (char *) tool;
|
|
argv[n++] = target;
|
|
argv[n++] = (char *) mountpoint;
|
|
argv[n] = NULL;
|
|
return MCDE_SYS_OK;
|
|
}
|
|
if (strcmp(scheme, "afp") == 0) {
|
|
if ((tool = mcde_sys_which(mountAfpPaths)) == NULL)
|
|
return mcde_sys_fail(MCDE_SYS_ENOTSUP, mcde_sys_msg(MSG_NET_NO_MOUNT_AFP, "mount_afp (afpfs-ng) is not there."));
|
|
/* The password is not in the address, which is argv[1] and shows
|
|
* in ps(1): the address says "-" and mount_afp asks getpass(3)
|
|
* for it (afpfs-ng fuse/client.c, "if (strcmp(req->url.password,
|
|
* "-")==0)"). getpass reads the standard input where the
|
|
* process has no controlling terminal (src/lib/libc/gen/
|
|
* getpass.c:106-112), and the runner of a job has none
|
|
* (common.c, setsid). Any character is allowed in it now. */
|
|
AfpAddress(user, host, path, password != NULL, target, tsize);
|
|
if (password != NULL) {
|
|
snprintf(plan->input, sizeof plan->input, "%s\n",
|
|
password);
|
|
plan->has_input = 1;
|
|
}
|
|
argv[n++] = (char *) tool;
|
|
argv[n++] = target;
|
|
argv[n++] = (char *) mountpoint;
|
|
argv[n] = NULL;
|
|
return MCDE_SYS_OK;
|
|
}
|
|
return mcde_sys_fail(MCDE_SYS_ENOTSUP, mcde_sys_msg(MSG_NET_SCHEME,
|
|
"%s:// cannot be connected to here yet."), scheme);
|
|
}
|
|
|
|
/* start the job; the status of a refusal in *STATUS (MCDE_SYS_OK if it runs) */
|
|
static McdeSysJob *
|
|
MountStart(const char *url, const char *mountpoint, const char *password,
|
|
int *status)
|
|
{
|
|
MountPlan plan;
|
|
McdeSysJob *job;
|
|
|
|
if ((*status = MountPlanFor(url, mountpoint, password, &plan)) !=
|
|
MCDE_SYS_OK)
|
|
return NULL;
|
|
job = mcde_sys_job_start(plan.argv, plan.has_input ? plan.input : NULL);
|
|
memset(&plan, 0, sizeof plan);
|
|
*status = job != NULL ? MCDE_SYS_OK : MCDE_SYS_EFAIL;
|
|
return job;
|
|
}
|
|
|
|
McdeSysJob *
|
|
mcde_sys_net_mount_start(const char *url, const char *mountpoint,
|
|
const char *password)
|
|
{
|
|
int status;
|
|
|
|
return MountStart(url, mountpoint, password, &status);
|
|
}
|
|
|
|
/* the same as _start and then waiting for the end: the tool runs under a job
|
|
* runner in both, which has no controlling terminal (a password goes through
|
|
* the tool's standard input, not to a prompt on the caller's terminal) */
|
|
int
|
|
mcde_sys_net_mount(const char *url, const char *mountpoint,
|
|
const char *password)
|
|
{
|
|
McdeSysJob *job;
|
|
struct pollfd p;
|
|
int status;
|
|
|
|
if ((job = MountStart(url, mountpoint, password, &status)) == NULL)
|
|
return status;
|
|
p.fd = mcde_sys_job_fd(job);
|
|
p.events = POLLIN;
|
|
while (poll(&p, 1, -1) < 0 && errno == EINTR)
|
|
;
|
|
return mcde_sys_job_finish(job);
|
|
}
|
|
|
|
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,
|
|
mcde_sys_msg(MSG_NET_NO_FOLDER, "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,
|
|
mcde_sys_msg(MSG_NET_NO_UMOUNT, "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;
|
|
}
|