/* * 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 #include #include #include #include #include #include #include #include #include #include #include #include #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; }