Files
mcde/programs/dtsystemsettings/sharing.c
T
lotherkandClaude Opus 5.5 64bfd2048f mcde: Sharing in System Settings: SSH, SMB, NFS, AFP, AppleTalk
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>
2026-11-11 12:36:46 +00:00

252 lines
7.2 KiB
C

/*
* sharing.c -- dtsystemsettings: what this computer shares
*
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
*
* The services that let others in -- remote login (SSH), file sharing by
* SMB, NFS and AFP, and the AppleTalk network -- each with a switch, and
* the folders the file sharing offers. Everything goes through McdeSys
* (mcde_sys_service_state/_set, mcde_sys_shares/_set: rc.conf and rc.d on
* NetBSD, systemctl on Linux; one list of folders for SMB, NFS and AFP).
*
* Switching a service or changing the folders changes the system, so the
* pane does it only as root; otherwise it shows what is and offers
* "Unlock...": a terminal where su(1) asks for the administrator's password
* and starts this pane as root, as Accounts does.
*
* C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md).
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pwd.h>
#include <X11/Intrinsic.h>
#include <Xm/Xm.h>
#include <Xm/FileSB.h>
#include <Xm/List.h>
#include <Xm/RowColumn.h>
#include <Xm/Separator.h>
#include <Xm/ToggleB.h>
#include <Mcde/McdeSys.h>
#include "ui.h"
#define MAX_SHARES 32
static const struct {
const char *label;
const char *how; /* how others reach it; %s is the host */
} service[MCDE_SHARE_COUNT] = {
[MCDE_SHARE_SSH] = { "Remote Login (SSH)", "ssh %s" },
[MCDE_SHARE_SMB] = { "File Sharing by SMB (Windows and others)",
"smb://%s/" },
[MCDE_SHARE_NFS] = { "File Sharing by NFS (Unix)", "%s:/folder" },
[MCDE_SHARE_AFP] = { "File Sharing by AFP", "afp://%s/" },
[MCDE_SHARE_APPLETALK] = { "AppleTalk network (files and printers)",
"the AppleTalk zone" },
};
static Widget toggles[MCDE_SHARE_COUNT], states[MCDE_SHARE_COUNT];
static Widget shareList, shareNote;
static char shares[MAX_SHARES][MCDE_SYS_SHARE_LEN];
static int nshares;
static int isRoot;
static void
ReadServices(void)
{
char host[256], text[400];
int s;
if (mcde_sys_hostname_get(host, sizeof host) != MCDE_SYS_OK)
snprintf(host, sizeof host, "this computer");
for (s = 0; s < MCDE_SHARE_COUNT; s++) {
McdeSysServiceState st;
char how[300];
if (mcde_sys_service_state((McdeSysShareService) s, &st) !=
MCDE_SYS_OK)
memset(&st, 0, sizeof st);
XmToggleButtonSetState(toggles[s], st.enabled, False);
XtSetSensitive(toggles[s], isRoot && st.installed);
snprintf(how, sizeof how, service[s].how, host);
if (!st.installed)
snprintf(text, sizeof text, "not installed");
else if (st.running)
snprintf(text, sizeof text, "on -- others reach it at %s",
how);
else if (st.enabled)
snprintf(text, sizeof text, "on at start, not running now");
else
snprintf(text, sizeof text, "off");
SetText(states[s], text);
}
}
static void
ServiceChanged(Widget w, XtPointer data, XtPointer call)
{
int s = (int) (intptr_t) data;
int on = ((XmToggleButtonCallbackStruct *) call)->set;
if (mcde_sys_service_set((McdeSysShareService) s, on) != MCDE_SYS_OK)
Message(w, mcde_sys_error(), 1);
ReadServices();
}
static void
FillShares(void)
{
int i;
XmListDeleteAllItems(shareList);
for (i = 0; i < nshares; i++) {
XmString s = XmStringCreateLocalized(shares[i]);
XmListAddItemUnselected(shareList, s, 0);
XmStringFree(s);
}
}
static void
WriteShares(Widget w)
{
if (mcde_sys_shares_set(shares, (size_t) nshares) != MCDE_SYS_OK) {
Message(w, mcde_sys_error(), 1);
nshares = mcde_sys_shares(shares, MAX_SHARES);
}
FillShares();
}
static void
FolderChosen(Widget w, XtPointer data, XtPointer call)
{
XmFileSelectionBoxCallbackStruct *cb =
(XmFileSelectionBoxCallbackStruct *) call;
char *dir = NULL;
(void) data;
if (cb->reason == XmCR_OK && nshares < MAX_SHARES &&
(dir = XmStringUnparse(cb->value, NULL, XmCHARSET_TEXT,
XmCHARSET_TEXT, NULL, 0, XmOUTPUT_ALL)) != NULL && dir[0] == '/') {
size_t n = strlen(dir);
while (n > 1 && dir[n - 1] == '/')
dir[--n] = '\0';
snprintf(shares[nshares++], MCDE_SYS_SHARE_LEN, "%s", dir);
WriteShares(XtParent(w));
}
XtFree(dir);
XtDestroyWidget(XtParent(w));
}
static void
AddShare(Widget w, XtPointer data, XtPointer call)
{
Arg a[2];
Widget fsb;
XmString t = XmStringCreateLocalized("Choose a Folder to Share");
(void) data;
(void) call;
XtSetArg(a[0], XmNfileTypeMask, XmFILE_DIRECTORY);
XtSetArg(a[1], XmNdialogTitle, t);
fsb = XmCreateFileSelectionDialog(w, "shareFolder", a, 2);
XmStringFree(t);
XtAddCallback(fsb, XmNokCallback, FolderChosen, NULL);
XtAddCallback(fsb, XmNcancelCallback, FolderChosen, NULL);
XtUnmanageChild(XmFileSelectionBoxGetChild(fsb, XmDIALOG_HELP_BUTTON));
XtManageChild(fsb);
}
static void
RemoveShare(Widget w, XtPointer data, XtPointer call)
{
int *pos = NULL, n = 0, i;
(void) data;
(void) call;
if (!XmListGetSelectedPos(shareList, &pos, &n) || n < 1 ||
pos[0] < 1 || pos[0] > nshares) {
XtFree((char *) pos);
return;
}
i = pos[0] - 1;
XtFree((char *) pos);
memmove(shares + i, shares + i + 1,
(size_t) (nshares - i - 1) * sizeof shares[0]);
nshares--;
WriteShares(w);
}
static void
Unlock(Widget w, XtPointer data, XtPointer call)
{
char cmd[512];
(void) w;
(void) data;
(void) call;
AsRoot("Unlock Sharing", "dtsystemsettings -pane sharing", cmd,
sizeof cmd);
Spawn(cmd);
}
Widget
MakeSharing(Widget parent)
{
Widget box = XtVaCreateWidget("sharing", xmRowColumnWidgetClass, parent,
XmNspacing, 4, NULL);
Widget row, table;
Arg a[4];
int s;
isRoot = geteuid() == 0;
(void) Text(box, "label", "What this computer offers others on the "
"network:");
table = XtVaCreateManagedWidget("services", xmRowColumnWidgetClass,
box, XmNorientation, XmVERTICAL, XmNpacking, XmPACK_COLUMN,
XmNnumColumns, MCDE_SHARE_COUNT, XmNisAligned, False,
XmNspacing, 2, NULL);
/* a row each: the switch, then what it is now; packed in columns, a
* vertical row column fills its rows left to right */
XtVaSetValues(table, XmNorientation, XmHORIZONTAL, XmNnumColumns,
MCDE_SHARE_COUNT, NULL);
for (s = 0; s < MCDE_SHARE_COUNT; s++) {
toggles[s] = XtVaCreateManagedWidget(service[s].label,
xmToggleButtonWidgetClass, table,
XmNalignment, XmALIGNMENT_BEGINNING, NULL);
XtAddCallback(toggles[s], XmNvalueChangedCallback,
ServiceChanged, (XtPointer) (intptr_t) s);
states[s] = Text(table, "state", "");
}
XtVaCreateManagedWidget("sep", xmSeparatorWidgetClass, box, NULL);
(void) Text(box, "label", "Folders shared by SMB, NFS and AFP:");
XtSetArg(a[0], XmNvisibleItemCount, 5);
XtSetArg(a[1], XmNselectionPolicy, XmBROWSE_SELECT);
XtSetArg(a[2], XmNwidth, 460);
shareList = XmCreateScrolledList(box, "shares", a, 3);
XtManageChild(shareList);
row = XtVaCreateManagedWidget("buttons", xmRowColumnWidgetClass, box,
XmNorientation, XmHORIZONTAL, XmNmarginWidth, 0, NULL);
XtSetSensitive(Button(row, "Add...", AddShare, NULL), isRoot);
XtSetSensitive(Button(row, "Remove", RemoveShare, NULL), isRoot);
if (!isRoot) {
(void) Button(row, "Unlock...", Unlock, NULL);
shareNote = Text(box, "note", "Changing what is shared needs the "
"administrator: Unlock... asks for the password.");
} else
shareNote = Text(box, "note", "");
nshares = mcde_sys_shares(shares, MAX_SHARES);
FillShares();
ReadServices();
return box;
}