McdeSys gets the services that let others in (mcde_sys_service_state, mcde_sys_service_set) and one list of shared folders for SMB, NFS and AFP (mcde_sys_shares, mcde_sys_shares_set: /etc/mcde-shares, and a block between "# MCDE shares begin" and "... end" in exports, smb.conf and afp.conf; the rest of each file stays as written). NetBSD switches rc.conf and runs the rc.d scripts -- sshd; rpcbind, mountd, nfsd and nfslocking for NFS; samba, netatalk, atalkd and papd from pkgsrc, whose example rc.d scripts are copied to /etc/rc.d first, as PKG_RCD_SCRIPTS=YES would. Linux uses systemctl (ssh, smbd and nmbd, nfs-server, netatalk; no AppleTalk). NetBSD's kernel has AppleTalk (GENERIC: options NETATALK) and pkgsrc's netatalk4 builds atalkd on NetBSD. docs/reference/sharing.txt. The Sharing pane (Internet & Network) shows each service with its switch and how others reach it (ssh host, smb://host/, host:/folder, afp://host/), and the shared folders with Add and Remove; as a user it offers Unlock..., as Accounts does. Measured: on the Linux host, read only: ssh, smb, nfs installed, enabled, running; afp and appletalk not installed (the pane on Xvfb says so). In the build VM as root: NFS on started rpcbind, mountd, nfsd, statd, lockd; sharing /tmp/mcdeshare made showmount -e list it for Everyone; NFS off stopped them; the VM's rc.conf was restored after. First found there: mountd will not start without /etc/exports, and statd/lockd are one script, nfslocking. Samba and netatalk are not in the makronix set yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
193 lines
5.2 KiB
C
193 lines
5.2 KiB
C
/*
|
|
* share.c -- MCDE: the shared folders, for SMB, NFS and AFP alike
|
|
*
|
|
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
|
|
*
|
|
* The list is /etc/mcde-shares, one absolute path a line. From it MCDE
|
|
* writes one block into each server's configuration, between the lines
|
|
* "# MCDE shares begin" and "# MCDE shares end", and leaves the rest of the
|
|
* file as the administrator wrote it:
|
|
*
|
|
* exports(5) a line per folder, as the backend words it
|
|
* (src/usr.sbin/mountd/exports.5; Linux: exports(5));
|
|
* smb.conf a section per folder: path, read only = no;
|
|
* afp.conf a section per folder: path (netatalk's afp.conf(5)).
|
|
*
|
|
* The files are written beside the old ones and renamed over them. Where
|
|
* a server is not installed its file is left alone. Running servers read
|
|
* their configuration again (the backend's mcde_sys_shares_reload).
|
|
* docs/reference/sharing.txt in the Makronix tree.
|
|
*
|
|
* POSIX (ext/src/mcde/AGENTS.md).
|
|
*/
|
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <sys/stat.h>
|
|
|
|
#include <Mcde/McdeSys.h>
|
|
|
|
#include "private.h"
|
|
|
|
#define SHARES_FILE "/etc/mcde-shares"
|
|
#define BEGIN_LINE "# MCDE shares begin"
|
|
#define END_LINE "# MCDE shares end"
|
|
|
|
int
|
|
mcde_sys_shares(char (*paths)[MCDE_SYS_SHARE_LEN], size_t max)
|
|
{
|
|
FILE *fp = fopen(SHARES_FILE, "r");
|
|
char line[MCDE_SYS_SHARE_LEN + 8];
|
|
size_t n = 0;
|
|
|
|
if (fp == NULL)
|
|
return 0;
|
|
while (n < max && fgets(line, sizeof line, fp) != NULL) {
|
|
line[strcspn(line, "\r\n")] = '\0';
|
|
if (line[0] != '/')
|
|
continue;
|
|
snprintf(paths[n++], MCDE_SYS_SHARE_LEN, "%s", line);
|
|
}
|
|
fclose(fp);
|
|
return (int) n;
|
|
}
|
|
|
|
/* the last part of PATH, for a section's name: "Public" */
|
|
static void
|
|
ShareName(const char *path, char *buf, size_t len)
|
|
{
|
|
const char *base = strrchr(path, '/');
|
|
size_t i, n = 0;
|
|
|
|
base = base != NULL && base[1] != '\0' ? base + 1 : path;
|
|
/* a section name has no brackets */
|
|
for (i = 0; base[i] != '\0' && n + 1 < len; i++)
|
|
if (base[i] != '[' && base[i] != ']')
|
|
buf[n++] = base[i];
|
|
buf[n] = '\0';
|
|
}
|
|
|
|
typedef enum { FORM_EXPORTS, FORM_SMB, FORM_AFP } Form;
|
|
|
|
static void
|
|
WriteBlock(FILE *to, Form form, char (*paths)[MCDE_SYS_SHARE_LEN], size_t n)
|
|
{
|
|
char name[MCDE_SYS_SHARE_LEN], line[MCDE_SYS_SHARE_LEN * 2];
|
|
size_t i;
|
|
|
|
fprintf(to, "%s (System Settings, Sharing; edited there)\n",
|
|
BEGIN_LINE);
|
|
for (i = 0; i < n; i++) {
|
|
ShareName(paths[i], name, sizeof name);
|
|
switch (form) {
|
|
case FORM_EXPORTS:
|
|
mcde_sys_export_line(paths[i], line, sizeof line);
|
|
fprintf(to, "%s\n", line);
|
|
break;
|
|
case FORM_SMB:
|
|
fprintf(to, "[%s]\n\tpath = %s\n\tread only = no\n"
|
|
"\tbrowseable = yes\n", name, paths[i]);
|
|
break;
|
|
case FORM_AFP:
|
|
fprintf(to, "[%s]\n\tpath = %s\n", name, paths[i]);
|
|
break;
|
|
}
|
|
}
|
|
fprintf(to, "%s\n", END_LINE);
|
|
}
|
|
|
|
/* FILE with its MCDE block replaced (or added at the end) */
|
|
static int
|
|
Rewrite(const char *file, Form form, char (*paths)[MCDE_SYS_SHARE_LEN],
|
|
size_t n)
|
|
{
|
|
char tmp[512], line[2048];
|
|
FILE *from, *to;
|
|
struct stat st;
|
|
int inside = 0, written = 0;
|
|
|
|
if (file == NULL)
|
|
return MCDE_SYS_OK;
|
|
snprintf(tmp, sizeof tmp, "%s.mcde", file);
|
|
if ((to = fopen(tmp, "w")) == NULL)
|
|
return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot write beside %s: "
|
|
"%s.", file, strerror(errno));
|
|
if ((from = fopen(file, "r")) != NULL) {
|
|
while (fgets(line, sizeof line, from) != NULL) {
|
|
if (strncmp(line, BEGIN_LINE, strlen(BEGIN_LINE)) == 0) {
|
|
inside = 1;
|
|
continue;
|
|
}
|
|
if (inside) {
|
|
if (strncmp(line, END_LINE,
|
|
strlen(END_LINE)) == 0) {
|
|
inside = 0;
|
|
WriteBlock(to, form, paths, n);
|
|
written = 1;
|
|
}
|
|
continue;
|
|
}
|
|
fputs(line, to);
|
|
}
|
|
fclose(from);
|
|
}
|
|
if (!written)
|
|
WriteBlock(to, form, paths, n);
|
|
if (fclose(to) != 0) {
|
|
unlink(tmp);
|
|
return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot write %s.", file);
|
|
}
|
|
if (stat(file, &st) == 0)
|
|
(void) chmod(tmp, st.st_mode & 07777);
|
|
else
|
|
(void) chmod(tmp, 0644);
|
|
if (rename(tmp, file) != 0) {
|
|
unlink(tmp);
|
|
return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot replace %s: %s.",
|
|
file, strerror(errno));
|
|
}
|
|
return MCDE_SYS_OK;
|
|
}
|
|
|
|
int
|
|
mcde_sys_shares_set(char (*paths)[MCDE_SYS_SHARE_LEN], size_t n)
|
|
{
|
|
FILE *fp;
|
|
size_t i;
|
|
int r;
|
|
|
|
if (geteuid() != 0)
|
|
return mcde_sys_fail(MCDE_SYS_EPERM, "Only the administrator "
|
|
"can change what is shared.");
|
|
for (i = 0; i < n; i++) {
|
|
struct stat st;
|
|
|
|
if (paths[i][0] != '/' || strpbrk(paths[i], "\n\"") != NULL ||
|
|
stat(paths[i], &st) != 0 || !S_ISDIR(st.st_mode))
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, "\"%s\" is no "
|
|
"folder that can be shared.", paths[i]);
|
|
}
|
|
if ((fp = fopen(SHARES_FILE ".new", "w")) == NULL)
|
|
return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot write %s.",
|
|
SHARES_FILE);
|
|
for (i = 0; i < n; i++)
|
|
fprintf(fp, "%s\n", paths[i]);
|
|
if (fclose(fp) != 0 || rename(SHARES_FILE ".new", SHARES_FILE) != 0)
|
|
return mcde_sys_fail(MCDE_SYS_EFAIL, "Cannot write %s.",
|
|
SHARES_FILE);
|
|
if ((r = Rewrite(mcde_sys_exports_file(), FORM_EXPORTS, paths, n)) !=
|
|
MCDE_SYS_OK ||
|
|
(r = Rewrite(mcde_sys_smb_conf(), FORM_SMB, paths, n)) !=
|
|
MCDE_SYS_OK ||
|
|
(r = Rewrite(mcde_sys_afp_conf(), FORM_AFP, paths, n)) !=
|
|
MCDE_SYS_OK)
|
|
return r;
|
|
mcde_sys_shares_reload();
|
|
return MCDE_SYS_OK;
|
|
}
|