Files
mcde/programs/dtsystemsettings/sharing.c
T
lotherkandClaude Sonnet 5.5 d53ea094e7 licence: CNCL for what Makronix wrote itself
LICENSE at the root; the 103 files new in ext/src/mcde (none was in the CDE
import) carry the CNCL line instead of the LGPL notice.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-14 11:48:02 +00:00

412 lines
12 KiB
C

/*
* sharing.c -- dtsystemsettings: what this computer shares
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* 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/TextF.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 addrs[MCDE_SHARE_COUNT], detail[MCDE_SHARE_COUNT];
static Widget svcList, nameField, nameNote, foldersBox;
static Widget shareList, shareNote;
static char shares[MAX_SHARES][MCDE_SYS_SHARE_LEN];
static int nshares;
static int isRoot;
static int chosen;
/* the address others use for this service: the first interface with one */
static void
AddressOf(McdeSysShareService s, const char *host, char *buf, size_t len)
{
McdeSysInterface ifs[16];
int n = mcde_sys_interfaces(ifs, 16), i;
const char *addr = NULL;
for (i = 0; i < n; i++)
if (ifs[i].up && ifs[i].address[0] != '\0') {
addr = ifs[i].address;
break;
}
switch (s) {
case MCDE_SHARE_SSH:
(void)snprintf(buf, len, "ssh %s@%s", getenv("USER") != NULL ?
getenv("USER") : "you", addr != NULL ? addr : host);
break;
case MCDE_SHARE_SMB:
(void)snprintf(buf, len, "smb://%s/", addr != NULL ? addr :
host);
break;
case MCDE_SHARE_NFS:
(void)snprintf(buf, len, "%s:/folder", addr != NULL ? addr :
host);
break;
case MCDE_SHARE_AFP:
(void)snprintf(buf, len, "afp://%s/", addr != NULL ? addr :
host);
break;
default:
(void)snprintf(buf, len, "the AppleTalk zone of %s", host);
break;
}
}
static void
ReadServices(void)
{
char host[256], text[400];
int s;
if (mcde_sys_hostname_get(host, sizeof host) != MCDE_SYS_OK)
(void)snprintf(host, sizeof host, "this computer");
for (s = 0; s < MCDE_SHARE_COUNT; s++) {
McdeSysServiceState st;
char addr[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);
AddressOf((McdeSysShareService) s, host, addr, sizeof addr);
XtVaSetValues(addrs[s], XmNlabelString, XmStringCreateLocalized(
addr), NULL);
if (!st.installed)
(void)snprintf(text, sizeof text, "not installed on "
"this computer");
else if (st.running)
(void)snprintf(text, sizeof text, "On -- others reach "
"it at %s", addr);
else if (st.enabled)
(void)snprintf(text, sizeof text, "On at start, not "
"running now (%s)", addr);
else
(void)snprintf(text, sizeof text, "Off -- %s", addr);
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();
}
/* the service chosen at the left: its page at the right */
static void
ServiceChosen(Widget w, XtPointer data, XtPointer call)
{
XmListCallbackStruct *c = (XmListCallbackStruct *) call;
int i = c->item_position - 1;
(void)w;
(void)data;
if (i < 0 || i >= MCDE_SHARE_COUNT)
return;
chosen = i;
for (i = 0; i < MCDE_SHARE_COUNT; i++)
if (i == chosen)
XtManageChild(detail[i]);
else
XtUnmanageChild(detail[i]);
XtManageChild(foldersBox);
XtUnmanageChild(foldersBox);
/* the folders belong to the file sharing services */
if (chosen == MCDE_SHARE_SMB || chosen == MCDE_SHARE_NFS ||
chosen == MCDE_SHARE_AFP)
XtManageChild(foldersBox);
}
/* the host name: NAME.local, the name the neighbours see */
static void
NameEdit(Widget w, XtPointer data, XtPointer call)
{
char *name = XmTextFieldGetString(nameField);
char cmd[512], line[512];
int i;
(void)w;
(void)data;
(void)call;
if (name == NULL)
return;
/* a host name: letters, digits and '-', not starting with '-' */
if (name[0] == '-' || name[0] == '\0' ||
strspn(name, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789-.") != strlen(name)) {
SetText(nameNote, "A computer name has letters, digits and '-'.");
XtFree(name);
return;
}
if (!isRoot) {
(void)snprintf(cmd, sizeof cmd, "dtsystemsettings -set-hostname "
"%s", name);
AsRoot("Change Computer Name", cmd, line, sizeof line);
if (line[0] != '\0')
Spawn(line);
SetText(nameNote, "The administrator's password is asked for "
"in the window that opened.");
XtFree(name);
return;
}
if (mcde_sys_hostname_set(name) != MCDE_SYS_OK)
SetText(nameNote, mcde_sys_error());
else
SetText(nameNote, "The name is set; the neighbours learn it "
"from the network.");
XtFree(name);
}
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);
return;
}
FillShares();
}
static void
FolderChosen(Widget w, XtPointer data, XtPointer call)
{
XmFileSelectionBoxCallbackStruct *c =
(XmFileSelectionBoxCallbackStruct *) call;
char *path = NULL;
(void)data;
if (c != NULL && c->reason == XmCR_OK && c->value != NULL &&
XmStringGetLtoR(c->value, XmFONTLIST_DEFAULT_TAG, &path) &&
path != NULL) {
if (nshares < MAX_SHARES) {
(void)snprintf(shares[nshares], sizeof shares[0], "%s",
path);
nshares++;
WriteShares(w);
}
XtFree(path);
}
if (w != NULL)
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, 6, NULL);
Widget nameRow, nameLab, edit, split, leftCol, rightCol;
Widget row;
XmString s;
char host[256], line[512];
int i;
isRoot = geteuid() == 0;
if (mcde_sys_hostname_get(host, sizeof host) != MCDE_SYS_OK)
(void)snprintf(host, sizeof host, "computer");
/* the name the neighbours see */
nameRow = XtVaCreateManagedWidget("nameRow", xmRowColumnWidgetClass,
box, XmNorientation, XmHORIZONTAL, XmNspacing, 6,
XmNmarginWidth, 0, NULL);
nameLab = Text(nameRow, "nameLabel", "Computer Name:");
(void)nameLab;
nameField = XtVaCreateManagedWidget("computerName",
xmTextFieldWidgetClass, nameRow, XmNcolumns, 20, NULL);
XmTextFieldSetString(nameField, host);
edit = Button(nameRow, "Edit...", NameEdit, NULL);
(void)edit;
nameNote = Text(box, "nameNote", "");
(void)snprintf(line, sizeof line, "Computers on your local network can "
"access this computer at %s.local.", host);
(void) Text(box, "netLine", line);
XtVaCreateManagedWidget("sep", xmSeparatorWidgetClass, box, NULL);
/* the services at the left, what the chosen one is at the right */
split = XtVaCreateManagedWidget("split", xmRowColumnWidgetClass, box,
XmNorientation, XmHORIZONTAL, XmNspacing, 12, XmNmarginWidth, 0,
NULL);
leftCol = XtVaCreateManagedWidget("serviceCol", xmRowColumnWidgetClass,
split, XmNspacing, 4, XmNmarginWidth, 0, NULL);
(void) Text(leftCol, "serviceLabel", "On Service");
svcList = XmCreateScrolledList(leftCol, "services", NULL, 0);
XtVaSetValues(svcList, XmNvisibleItemCount, MCDE_SHARE_COUNT,
XmNselectionPolicy, XmBROWSE_SELECT, XmNwidth, 250, NULL);
for (i = 0; i < MCDE_SHARE_COUNT; i++) {
s = XmStringCreateLocalized((char *) service[i].label);
XmListAddItemUnselected(svcList, s, 0);
XmStringFree(s);
}
XtAddCallback(svcList, XmNbrowseSelectionCallback, ServiceChosen, NULL);
XtManageChild(svcList);
rightCol = XtVaCreateManagedWidget("detailCol", xmRowColumnWidgetClass,
split, XmNspacing, 4, XmNmarginWidth, 0, NULL);
for (i = 0; i < MCDE_SHARE_COUNT; i++) {
detail[i] = XtVaCreateWidget("servicePage",
xmRowColumnWidgetClass, rightCol, XmNspacing, 4,
XmNmarginWidth, 0, NULL);
toggles[i] = XtVaCreateManagedWidget(service[i].label,
xmToggleButtonWidgetClass, detail[i],
XmNalignment, XmALIGNMENT_BEGINNING, NULL);
XtAddCallback(toggles[i], XmNvalueChangedCallback,
ServiceChanged, (XtPointer) (intptr_t) i);
states[i] = Text(detail[i], "state", "");
addrs[i] = Text(detail[i], "address", "");
}
chosen = MCDE_SHARE_SSH;
XtManageChild(detail[chosen]);
/* the folders of the file sharing services */
foldersBox = XtVaCreateManagedWidget("folders",
xmRowColumnWidgetClass, rightCol, XmNspacing, 4,
XmNmarginWidth, 0, NULL);
(void) Text(foldersBox, "foldersLabel", "Shared Folders:");
{
Arg a[3];
XtSetArg(a[0], XmNvisibleItemCount, 4);
XtSetArg(a[1], XmNselectionPolicy, XmBROWSE_SELECT);
XtSetArg(a[2], XmNwidth, 420);
shareList = XmCreateScrolledList(foldersBox, "shares", a, 3);
XtManageChild(shareList);
}
row = XtVaCreateManagedWidget("folderButtons", xmRowColumnWidgetClass,
foldersBox, XmNorientation, XmHORIZONTAL, XmNmarginWidth, 0, NULL);
XtSetSensitive(Button(row, "Add...", AddShare, NULL), isRoot);
XtSetSensitive(Button(row, "Remove", RemoveShare, NULL), isRoot);
XtUnmanageChild(foldersBox); /* SSH at first: no folders */
XtVaCreateManagedWidget("sep", xmSeparatorWidgetClass, box, NULL);
if (!isRoot) {
(void) Button(box, "Unlock...", Unlock, NULL);
shareNote = Text(box, "note", "Changing what is shared needs the "
"administrator: Unlock... asks for the password.");
} else
shareNote = Text(box, "note", "");
XtSetSensitive(nameField, True);
nshares = mcde_sys_shares(shares, MAX_SHARES);
FillShares();
ReadServices();
return box;
}