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