diff --git a/include/Mcde/McdeSys.h b/include/Mcde/McdeSys.h index cbf6a3ae..2feb301b 100644 --- a/include/Mcde/McdeSys.h +++ b/include/Mcde/McdeSys.h @@ -16,7 +16,7 @@ * * Status: lib/McdeSys implements mcde_sys_backend, mcde_sys_error, * mcde_sys_info, mcde_sys_power, mcde_sys_interfaces, the sound, Bluetooth, - * battery, time zone, sharing, + * battery, time zone, sharing, the network browser, * user and software update functions (common.c, net.c, sound.c, users.c, * netbsd.c, linux.c); the network setup and the host name setter are the * contract still. @@ -210,6 +210,39 @@ int mcde_sys_shares(char (*paths)[MCDE_SYS_SHARE_LEN], size_t max); * configuration, which running servers read again (root) */ int mcde_sys_shares_set(char (*paths)[MCDE_SYS_SHARE_LEN], size_t n); +/* + * The network: the computers that announce what they offer (DNS-SD, + * multicast DNS), and connecting to what they share. A computer's kinds + * are bits of McdeSysNetKinds. + */ +enum { + MCDE_NET_HAS_SMB = 1 << 0, /* _smb._tcp */ + MCDE_NET_HAS_AFP = 1 << 1, /* _afpovertcp._tcp */ + MCDE_NET_HAS_NFS = 1 << 2, /* _nfs._tcp */ + MCDE_NET_HAS_SSH = 1 << 3, /* _ssh._tcp or _sftp-ssh._tcp */ + MCDE_NET_HAS_ANY = 1 << 4 /* _workstation._tcp, nothing shared */ +}; + +typedef struct { + char name[128]; /* as it calls itself: "kitt" */ + char host[256]; /* its address: "kitt.local" */ + int kinds; /* MCDE_NET_HAS_* */ +} McdeSysNetHost; + +/* the computers found within about TIMEOUT_MS; how many */ +int mcde_sys_net_browse(McdeSysNetHost *hosts, size_t max, int timeout_ms); +/* what HOST shares: NFS exports (showmount) or SMB shares (smbclient -L); + * KIND is MCDE_NET_HAS_NFS or _SMB; how many */ +int mcde_sys_net_shares(const char *host, int kind, char (*names)[256], + size_t max); +/* connect URL -- sftp://[user@]host[/path], nfs://host/path, + * afp://[user@]host/volume -- to the folder MOUNTPOINT, which must exist; + * PASSWORD may be NULL */ +int mcde_sys_net_mount(const char *url, const char *mountpoint, + const char *password); +/* disconnect what is mounted at MOUNTPOINT */ +int mcde_sys_net_unmount(const char *mountpoint); + /* * A process: the command line it was started with, as one line a shell * reads back (each argument in single quotes). For the front panel, which diff --git a/lib/McdeSys/Makefile.am b/lib/McdeSys/Makefile.am index fc110669..9acb9664 100644 --- a/lib/McdeSys/Makefile.am +++ b/lib/McdeSys/Makefile.am @@ -5,11 +5,12 @@ MAINTAINERCLEANFILES = Makefile.in lib_LTLIBRARIES = libMcdeSys.la libMcdeSys_la_CPPFLAGS = -I$(top_srcdir)/include -libMcdeSys_la_SOURCES = common.c net.c sound.c users.c share.c private.h +libMcdeSys_la_SOURCES = common.c net.c sound.c users.c share.c netmount.c private.h if NETBSD libMcdeSys_la_SOURCES += netbsd.c -# crypt(3), for the password hash; proplib(3), for the battery (netbsd.c) -libMcdeSys_la_LIBADD = -lcrypt -lprop +# crypt(3), for the password hash; proplib(3), for the battery; libdns_sd +# (base), for the computers on the network (netbsd.c) +libMcdeSys_la_LIBADD = -lcrypt -lprop -ldns_sd else libMcdeSys_la_SOURCES += linux.c endif diff --git a/lib/McdeSys/common.c b/lib/McdeSys/common.c index a695c647..5cf27404 100644 --- a/lib/McdeSys/common.c +++ b/lib/McdeSys/common.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -28,6 +29,7 @@ #include #include #include +#include #include #include @@ -166,14 +168,18 @@ mcde_sys_which(const char *const *paths) return NULL; } -int -mcde_sys_capture(char *const argv[], char *buf, size_t len) +/* SECONDS > 0: the child is ended when it takes longer (a tool that asks + * the network -- showmount waited minutes for an rpcbind that was not + * there, measured) */ +static int +Capture(char *const argv[], char *buf, size_t len, int seconds) { struct sigaction saved; - int fd[2], st, nul; + int fd[2], st, nul, late = 0; size_t got = 0; ssize_t r; pid_t pid; + time_t end = seconds > 0 ? time(NULL) + seconds : 0; buf[0] = '\0'; if (access(argv[0], X_OK) != 0) @@ -206,6 +212,22 @@ mcde_sys_capture(char *const argv[], char *buf, size_t len) for (;;) { char scratch[512]; + if (end != 0) { + struct pollfd pfd; + time_t left = end - time(NULL); + int pr; + + pfd.fd = fd[0]; + pfd.events = POLLIN; + pr = left > 0 ? poll(&pfd, 1, (int) left * 1000) : 0; + if (pr < 0 && errno == EINTR) + continue; + if (pr <= 0) { + late = 1; + kill(pid, SIGKILL); + break; + } + } if (got + 1 < len) r = read(fd[0], buf + got, len - 1 - got); else @@ -224,12 +246,28 @@ mcde_sys_capture(char *const argv[], char *buf, size_t len) if (errno != EINTR) break; ChildSignal(&saved, 1); + if (late) + return mcde_sys_fail(MCDE_SYS_EFAIL, "%s did not answer within " + "%d seconds.", argv[0], seconds); if (st != -1 && WIFEXITED(st) && WEXITSTATUS(st) == 0) return MCDE_SYS_OK; return mcde_sys_fail(MCDE_SYS_EFAIL, "%s did not succeed (status %d).", argv[0], WIFEXITED(st) ? WEXITSTATUS(st) : -1); } +int +mcde_sys_capture(char *const argv[], char *buf, size_t len) +{ + return Capture(argv, buf, len, 0); +} + +int +mcde_sys_capture_limited(char *const argv[], char *buf, size_t len, + int seconds) +{ + return Capture(argv, buf, len, seconds); +} + /* the first line of FILE that starts with KEY, the rest of it in BUF */ int mcde_sys_file_value(const char *file, const char *key, char *buf, size_t len) diff --git a/lib/McdeSys/linux.c b/lib/McdeSys/linux.c index 21cfdb1a..33007a47 100644 --- a/lib/McdeSys/linux.c +++ b/lib/McdeSys/linux.c @@ -666,3 +666,76 @@ mcde_sys_shares_reload(void) if (Systemctl("is-active", "-q", "netatalk") == MCDE_SYS_OK) (void) Systemctl("reload-or-restart", NULL, "netatalk"); } + +/* ---- the network: avahi-browse(1) -- -a every type, -t end when the list + * is complete, -r resolved, -p parsable: "=;IF;PROTO;NAME;TYPE;DOMAIN; + * HOST;ADDRESS;PORT;TXT" (names escaped as \DDD) */ +static const char *const avahiPaths[] = { "/usr/bin/avahi-browse", NULL }; + +/* avahi's \DDD (a decimal byte) back into the byte: "\032" is a blank */ +static void +Unescape(char *s) +{ + char *w = s; + + while (*s != '\0') { + if (s[0] == '\\' && s[1] >= '0' && s[1] <= '9' && + s[2] >= '0' && s[2] <= '9' && s[3] >= '0' && s[3] <= '9') { + *w++ = (char) ((s[1] - '0') * 100 + (s[2] - '0') * 10 + + (s[3] - '0')); + s += 4; + } else if (s[0] == '\\' && s[1] != '\0') { + *w++ = s[1]; + s += 2; + } else + *w++ = *s++; + } + *w = '\0'; +} + +int +mcde_sys_net_browse(McdeSysNetHost *hosts, size_t max, int timeout_ms) +{ + static char out[65536]; + const char *ab = mcde_sys_which(avahiPaths); + char *argv[] = { NULL, "-a", "-t", "-r", "-p", "-k", NULL }; + char *line, *last = NULL; + int n = 0; + + (void) timeout_ms; + if (ab == NULL) + return mcde_sys_fail(MCDE_SYS_ENOTSUP, "avahi-browse is not " + "there."); + argv[0] = (char *) ab; + 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 *f[8], *l2 = NULL; + int k, kind = 0; + + if (line[0] != '=') + continue; + f[0] = strtok_r(line, ";", &l2); + for (k = 1; k < 8; k++) + f[k] = strtok_r(NULL, ";", &l2); + if (f[6] == NULL) + continue; + if (strcmp(f[4], "_smb._tcp") == 0) + kind = MCDE_NET_HAS_SMB; + else if (strcmp(f[4], "_afpovertcp._tcp") == 0) + kind = MCDE_NET_HAS_AFP; + else if (strcmp(f[4], "_nfs._tcp") == 0) + kind = MCDE_NET_HAS_NFS; + else if (strcmp(f[4], "_ssh._tcp") == 0 || + strcmp(f[4], "_sftp-ssh._tcp") == 0) + kind = MCDE_NET_HAS_SSH; + else if (strcmp(f[4], "_workstation._tcp") == 0) + kind = MCDE_NET_HAS_ANY; + else + continue; + Unescape(f[3]); + n = mcde_sys_net_add(hosts, n, max, f[3], f[6], kind); + } + return n; +} diff --git a/lib/McdeSys/netbsd.c b/lib/McdeSys/netbsd.c index 44a34f19..e7cb94c7 100644 --- a/lib/McdeSys/netbsd.c +++ b/lib/McdeSys/netbsd.c @@ -32,6 +32,9 @@ #include #include #include +#include +#include +#include #include @@ -962,3 +965,153 @@ mcde_sys_shares_reload(void) 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; +} diff --git a/lib/McdeSys/netmount.c b/lib/McdeSys/netmount.c new file mode 100644 index 00000000..ff119cbd --- /dev/null +++ b/lib/McdeSys/netmount.c @@ -0,0 +1,279 @@ +/* + * 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 +#include +#include +#include +#include + +#include + +#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; +} diff --git a/lib/McdeSys/private.h b/lib/McdeSys/private.h index 7fcb0681..1e5fac41 100644 --- a/lib/McdeSys/private.h +++ b/lib/McdeSys/private.h @@ -20,6 +20,9 @@ int mcde_sys_run(char *const argv[]); int mcde_sys_capture(char *const argv[], char *buf, size_t len); /* the first of PATHS (NULL-terminated) that is an executable file, or NULL */ const char *mcde_sys_which(const char *const *paths); +/* the same, ending the child after SECONDS (tools that ask the network) */ +int mcde_sys_capture_limited(char *const argv[], char *buf, size_t len, + int seconds); /* run ARGV and write INPUT to its standard input (for chpasswd(8)) */ int mcde_sys_run_input(char *const argv[], const char *input); /* the value after KEY on the first line of FILE that starts with it */ @@ -48,6 +51,10 @@ const char *mcde_sys_afp_conf(void); void mcde_sys_export_line(const char *path, char *buf, size_t len); /* the backend: make running servers read their configuration again */ void mcde_sys_shares_reload(void); +/* netmount.c: a service of KIND found on HOST (NAME as it calls itself) + * into HOSTS, merged by host; the new count */ +int mcde_sys_net_add(McdeSysNetHost *hosts, int n, size_t max, + const char *name, const char *host, int kind); /* the backend: the mixer when no sound server runs (sound.c asks) */ int mcde_sys_mixer_get(int *percent, int *muted); int mcde_sys_mixer_set(int percent);