Files
lotherk 020dd9ff5d MCDE dtsystemsettings: Security, three tabs (t0157)
New programs/dtsystemsettings/security.c with the reference's three tabs:

  General     "Require password to wake this computer from sleep or screen
              saver" writes dtsession's lock time (_DtSetSmState with
              SM_STATE_LOCKTIMEOUT; the Screen Saver page reads and writes the
              same field, so the two pages show one setting).  On: the lock at
              the saver's time, or ten minutes without a saver.  "Use secure
              virtual memory" is NetBSD's vm.swap_encrypt through McdeSys
              (now and in /etc/sysctl.conf, root).  Automatic login and a
              log-out after N minutes are shown as what the desktop has:
              dtlogin always asks for a name and a password, and dtsession
              has no idle logout -- the pane says so and greys the controls.
  Encryption  the cgd(4) volumes of /etc/cgd/cgd.conf, and the sentence that
              encrypting a disk is a decision of the installation, not of a
              running system; changing a passphrase is a follow-up.
  Firewall    the npf(7) state (rc.conf npf=YES, /etc/npf.conf); the rules
              are a follow-up -- a fixed template with the shares of the
              Sharing page, never free text.

McdeSys gained the security state: mcde_sys_swap_encrypt_get/_set,
mcde_sys_cgd_volumes, mcde_sys_firewall_get.  netbsd.c implements them
(sysctl(8) vm.swap_encrypt, /etc/sysctl.conf through a KeySet() that rc.conf
now uses too, /etc/cgd/cgd.conf, rc.conf's npf), linux.c answers
MCDE_SYS_ENOTSUP with a sentence saying whose feature it is.
test_sysinfo prints all three.

The pane's icon is new: programs/icons/McdeLock.pm (a padlock, palette
colours); "security" is a real pane now, not a greyed entry.

Measured under Xvfb with xdotool (dtsession started in the session):

  - the lock checkbox: _DT_SM_STATE_INFO on dtsession's window before the
    click 0xfff, ... lockTimeout 0x0; after it 0x100 (SM_STATE_LOCKTIMEOUT),
    lockTimeout 0x258 (600 s); a second click puts it back to 0x0;
  - the three tabs and their content (docs/screenshots/leopard/
    System-Preferences-Security-mcde.png with the lock on, ...-Encryption-
    mcde.png, ...-Firewall-mcde.png);
  - test_sysinfo on this (Linux) host prints the three MCDE_SYS_ENOTSUP
    sentences -- the pane shows them instead of dead controls;
  - make check programs/dtsystemsettings: test_panes PASS; the Sound and
    Keyboard & Mouse panes still switch their tabs after the layout fix
    below;
  - make programs/dtsystemsettings, lib/McdeSys exit=0.

A Motif lesson, saved in AGENTS.md: a row column inside the pane area takes
the other orientation unless told, a child added later to a tight-packed
column goes into the next column, and a page managed for the first time after
its widgets were built keeps a height of one line.  The pane therefore builds
its page fresh inside an XmPACK_COLUMN container on a tab change; the same
XmNorientation, XmVERTICAL went into the Sound and Keyboard & Mouse page
containers.

Not measured, because this host is not NetBSD or has no root: swapping
vm.swap_encrypt, reading cgd volumes, the npf state and writing npf rules.
They are the follow-up task for the VM.
2026-11-14 12:44:32 +00:00

359 lines
11 KiB
C

/*
* security.c -- dtsystemsettings: the computer's security (t0157)
*
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
*
* Three tabs as the reference has them: General, Encryption, Firewall.
*
* General "Require password to wake this computer from sleep or
* screen saver" is dtsession's lock time (SmStateInfo,
* smLockTimeout; the Screen Saver page shows the same value --
* one setting, two pages). With the lock at the saver's time,
* dtsession locks the screen when the saver starts and needs
* the password to come back. "Use secure virtual memory" is
* NetBSD's vm.swap_encrypt through McdeSys; it needs the
* administrator. Automatic login and a log-out after some
* minutes of inactivity are shown with what the desktop can
* do today: dtlogin always asks, dtsession has no idle logout.
* Encryption the cgd(4) volumes of /etc/cgd/cgd.conf (McdeSys); making
* one is an install-time decision, so the page shows and does
* not change.
* Firewall the state of NetBSD's npf(7) (rc.conf npf=YES, its file);
* the page reads, the rules are a follow-up.
*
* 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 <X11/Intrinsic.h>
#include <Xm/Form.h>
#include <Xm/Label.h>
#include <Xm/List.h>
#include <Xm/RowColumn.h>
#include <Xm/Scale.h>
#include <Xm/ToggleB.h>
#include <Dt/SessionM.h>
#include <Mcde/McdeSys.h>
#include "ui.h"
static Widget genLock, genLockNote, genSwap, genSwapNote;
static Widget genLogout, genLogoutNote;
/* ---- the session manager's lock time ------------------------------------ */
static int
SessionState(Display *dpy, SmStateInfo *st)
{
Window sm = None;
memset(st, 0, sizeof *st);
if (_DtGetSmWindow(dpy, RootWindow(dpy, 0), &sm) != Success)
return 0;
if (_DtGetSmState(dpy, sm, st) != Success)
return 0;
return 1;
}
/* dtsession's lock time: 0 is no lock */
static void
LockRead(Widget w)
{
Display *dpy = XtDisplay(w);
SmStateInfo st;
if (!SessionState(dpy, &st)) {
/* not dead: the session manager may come later (a login);
* the change handler asks again */
XmToggleButtonSetState(genLock, False, False);
SetText(genLockNote, "No session manager runs at the moment; "
"a change is kept when one does.");
return;
}
XmToggleButtonSetState(genLock, st.smLockTimeout > 0, False);
}
/* on: lock when the saver starts (or after ten minutes without one);
* off: no lock */
static void
LockChanged(Widget w, XtPointer data, XtPointer call)
{
Display *dpy = XtDisplay(w);
SmStateInfo st;
Window sm = None;
Boolean on = XmToggleButtonGetState(genLock);
(void) data;
(void) call;
if (_DtGetSmWindow(dpy, RootWindow(dpy, 0), &sm) != Success) {
SetText(genLockNote, "No session manager runs, so the lock "
"was not set.");
return;
}
memset(&st, 0, sizeof st);
/* only this field: the saver's own time stays as it is */
st.flags = SM_STATE_LOCKTIMEOUT;
st.smLockTimeout = 0;
if (on) {
SmStateInfo now;
st.smLockTimeout = 600; /* ten minutes without a saver */
if (SessionState(dpy, &now) && now.smSaverTimeout > 0)
st.smLockTimeout = now.smSaverTimeout;
}
if (_DtSetSmState(dpy, sm, &st) != Success)
SetText(genLockNote, "The session manager did not take it.");
else
SetText(genLockNote, on ? "The screen locks when the saver "
"starts (or after ten minutes without one)." :
"The screen does not lock.");
XFlush(dpy);
}
/* ---- secure virtual memory ---------------------------------------------- */
static void
SwapRead(Widget w)
{
int on = 0;
int r = mcde_sys_swap_encrypt_get(&on);
if (r != MCDE_SYS_OK) {
XmToggleButtonSetState(genSwap, False, False);
XtSetSensitive(genSwap, False);
SetText(genSwapNote, mcde_sys_error());
return;
}
XmToggleButtonSetState(genSwap, on != 0, False);
SetText(genSwapNote, "The swap's pages are encrypted with a key that "
"is only in memory.");
}
static void
SwapChanged(Widget w, XtPointer data, XtPointer call)
{
Boolean on = XmToggleButtonGetState(genSwap);
int r = mcde_sys_swap_encrypt_set(on);
(void) w;
(void) data;
(void) call;
if (r != MCDE_SYS_OK) {
SetText(genSwapNote, mcde_sys_error());
/* the state the system really has */
SwapRead(genSwap);
return;
}
SetText(genSwapNote, on ? "Encrypted from now on, and after the next "
"start." : "Not encrypted any more, from the next start.");
}
/* ---- the pages ---------------------------------------------------------- */
static Widget
GeneralPage(Widget parent)
{
Widget page = XtVaCreateWidget("securityGeneral",
xmRowColumnWidgetClass, parent, XmNspacing, 8, NULL);
Widget row, l;
genLock = XtVaCreateManagedWidget("Require password to wake this "
"computer from sleep or screen saver",
xmToggleButtonWidgetClass, page, NULL);
XtAddCallback(genLock, XmNvalueChangedCallback, LockChanged, NULL);
genLockNote = Text(page, "lockNote", "");
(void) Text(page, "accountsLabel", "For all accounts on this "
"computer:");
row = XtVaCreateManagedWidget("accountsRow",
xmRowColumnWidgetClass, page, XmNorientation, XmVERTICAL,
XmNspacing, 2, XmNmarginLeft, 18, XmNmarginWidth, 0, NULL);
{
Widget autoLogin;
autoLogin = XtVaCreateManagedWidget(
"Disable automatic login", xmToggleButtonWidgetClass,
row, NULL);
XmToggleButtonSetState(autoLogin, True, False);
XtSetSensitive(autoLogin, False);
(void) Text(row, "autoNote", "dtlogin has no automatic login: "
"it always asks for a name and a password.");
}
{
Widget after = XtVaCreateManagedWidget("logoutRow",
xmRowColumnWidgetClass, row, XmNorientation,
XmHORIZONTAL, XmNspacing, 6, XmNmarginWidth, 0, NULL);
genLogout = XtVaCreateManagedWidget("Log out after minutes "
"of inactivity", xmToggleButtonWidgetClass, after,
NULL);
XtSetSensitive(genLogout, False);
l = XtVaCreateManagedWidget("logoutMinutes",
xmScaleWidgetClass, row, XmNorientation, XmHORIZONTAL,
XmNminimum, 5, XmNmaximum, 120, XmNvalue, 60,
XmNshowValue, True, XmNscaleWidth, 240, NULL);
XtSetSensitive(l, False);
(void) after;
genLogoutNote = Text(row, "logoutNote", "dtsession has no "
"log-out after inactivity yet; the setting waits for it.");
}
genSwap = XtVaCreateManagedWidget("Use secure virtual memory",
xmToggleButtonWidgetClass, page, NULL);
XtAddCallback(genSwap, XmNvalueChangedCallback, SwapChanged, NULL);
genSwapNote = Text(page, "swapNote", "");
LockRead(page);
SwapRead(page);
(void) row;
return page;
}
static Widget
EncryptionPage(Widget parent)
{
Widget page = XtVaCreateWidget("securityEncryption",
xmRowColumnWidgetClass, parent, XmNspacing, 8, NULL);
char vols[16][MCDE_SYS_CGD_ENTRY_LEN];
int n, i;
(void) Text(page, "cgdLabel", "Encrypted disks (cgd):");
n = mcde_sys_cgd_volumes(vols, 16);
if (n < 0) {
(void) Text(page, "cgdNone", mcde_sys_error());
} else if (n == 0) {
(void) Text(page, "cgdNone", "No encrypted disk is "
"configured (/etc/cgd/cgd.conf is empty).");
} else {
Widget list = XmCreateScrolledList(page, "cgdVolumes", NULL, 0);
for (i = 0; i < n; i++) {
XmString s = XmStringCreateLocalized(vols[i]);
XmListAddItemUnselected(list, s, 0);
XmStringFree(s);
}
XtManageChild(list);
}
(void) Text(page, "encryptNote", "Encrypting a disk is a decision of "
"the installation (sysinst): a running system");
(void) Text(page, "encryptNote2", "does not convert one. This page "
"shows what is encrypted; changing a cgd");
(void) Text(page, "encryptNote3", "passphrase is a follow-up.");
return page;
}
static Widget
FirewallPage(Widget parent)
{
Widget page = XtVaCreateWidget("securityFirewall",
xmRowColumnWidgetClass, parent, XmNspacing, 8, NULL);
char conf[128], line[256];
int on = 0;
(void) Text(page, "firewallLabel", "The packet filter (npf):");
if (mcde_sys_firewall_get(&on, conf, sizeof conf) != MCDE_SYS_OK) {
(void) Text(page, "firewallNone", mcde_sys_error());
} else {
(void)snprintf(line, sizeof line, "%s, configuration %s",
on ? "on: started at boot (npf=YES in /etc/rc.conf)" :
"off (npf=YES is not in /etc/rc.conf)", conf);
(void) Text(page, "firewallState", line);
}
(void) Text(page, "firewallNote", "Three modes as the reference has "
"them: all incoming, the shared services,");
(void) Text(page, "firewallNote2", "or a list of services. Writing "
"the rules is a follow-up: a fixed template");
(void) Text(page, "firewallNote3", "with the shares of the Sharing "
"page (SSH, SMB, AFP, NFS), never free text;");
(void) Text(page, "firewallNote4", "\"npfctl reload\" needs the "
"administrator.");
return page;
}
/* ---- the pane: three tabs ---------------------------------------------- */
static Widget secBox, secContent;
static int secShown = -1;
static void
ShowSecurityPage(int which)
{
if (secContent != NULL) {
XtDestroyWidget(secContent);
secContent = NULL;
}
genLock = genSwap = NULL;
secShown = which;
if (which == 1)
secContent = EncryptionPage(secBox);
else if (which == 2)
secContent = FirewallPage(secBox);
else
secContent = GeneralPage(secBox);
/* a page built while it was unmanaged keeps a height of one line when
* it is managed afterwards (measured); building it fresh and managing
* it at once lays it out as the first one */
XtManageChild(secContent);
}
static void
SecurityTab(Widget w, XtPointer data, XtPointer call)
{
int which = (int) (size_t) data;
(void) w;
if (call != NULL && !((XmToggleButtonCallbackStruct *) call)->set)
return;
if (which != secShown)
ShowSecurityPage(which);
}
Widget
MakeSecurity(Widget parent)
{
/* vertical, said out loud: a row column inside the pane area (which
* is vertical) would otherwise take the other direction */
Widget box = XtVaCreateWidget("security", xmRowColumnWidgetClass,
parent, XmNorientation, XmVERTICAL, XmNspacing, 8, NULL);
Widget tabs;
static const char *const labels[3] = { "General", "Encryption",
"Firewall" };
int i;
tabs = XtVaCreateManagedWidget("securityTabs", xmRowColumnWidgetClass,
box, XmNorientation, XmHORIZONTAL, XmNspacing, 4,
XmNmarginWidth, 0, XmNradioBehavior, True, NULL);
for (i = 0; i < 3; i++) {
XmString s = XmStringCreateLocalized((char *) labels[i]);
Widget t = XtVaCreateManagedWidget("securityTab",
xmToggleButtonWidgetClass, tabs, XmNlabelString, s,
XmNindicatorOn, False, XmNshadowThickness, 2,
XmNfillOnSelect, True, XmNmarginWidth, 10, NULL);
XmStringFree(s);
XtAddCallback(t, XmNvalueChangedCallback, SecurityTab,
(XtPointer) (size_t) i);
}
/* the pages' own container: one column, so the page rebuilt on a tab
* change takes the slot under the tabs (a tight-packed row column
* would put a new child into the next column) */
secBox = XtVaCreateManagedWidget("securityPages",
xmRowColumnWidgetClass, box, XmNorientation, XmVERTICAL,
XmNpacking, XmPACK_COLUMN, XmNspacing, 4, XmNmarginWidth, 0,
NULL);
ShowSecurityPage(0);
{
Widget first = XtNameToWidget(tabs, "securityTab");
if (first != NULL)
XmToggleButtonSetState(first, True, False);
}
return box;
}