Files
mcde/programs/dtsystemsettings/parental.c
T
lotherk cec3eb49e3 MCDE dtsystemsettings: Parental Controls, enforced by the launcher (t0142)
New programs/dtsystemsettings/parental.c: the accounts on the left and, for
the chosen one, three pages.  Programs: "All programs" or "Only the
applications selected below" with a list of the desktop's programs.  Time
Limits: weekday and weekend hours ("09:00-19:00" or "any").  Log: the
programs that were started, from ~/.dt/parental.log.  The values are kept in
~/.dt/parental.conf under the login's name ("lotherk.allow=dtpad,dtfile"),
English keys, the file the launcher reads.

The enforcement is in dtlaunch, the place where a program is started: the
list shows only the allowed names, a disallowed program or a start outside
the hours is refused with a sentence in the window ("Outside the allowed
hours (Parental Controls)."), and every allowed start is appended to
~/.dt/parental.log.  A refusal keeps the launcher open.

Measured under Xvfb with xdotool:
  - with "lotherk.only=1, allow=dtpad" the launcher offers dtpad only;
  - with hours 03:00-03:01 (now 13:4x) Return on dtpad is refused, the
    window stays and says why, and ~/.dt/parental.log stays absent;
  - with "any" the same Return starts dtpad and the log gets
    "2026-11-14 13:44 dtpad";
  - the pane shows the accounts, the three pages and the applied file
    (docs/screenshots/leopard/System-Preferences-Parental-Controls-mcde.png,
    ...-Time-Limits-mcde.png);
  - make programs/dtsystemsettings, programs/dtlaunch exit=0.

Not done (follow-up): system-wide enforcement -- taking the execute
permission away from programs outside the list needs root, ending a session
when the hours are over belongs to dtsession, and a child's own file needs
the administrator.  A toggle or radio set before its page is laid out keeps
its first painted state in this pane (measured); the file is right.
2026-11-14 13:53:51 +00:00

444 lines
14 KiB
C

/*
* parental.c -- dtsystemsettings: the Parental Controls (t0142)
*
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
*
* The accounts on the left, and for the chosen one three pages:
*
* Programs "Only allow selected applications" with a list of the
* desktop's programs; the launcher (dtlaunch, the way a
* program is started) shows only those and refuses the rest.
* Time Limits the hours a program may be started (weekday and weekend,
* "09:00-19:00" or "any"); the launcher refuses outside them.
* Log the programs that were started, as the launcher wrote them
* (~/.dt/parental.log).
*
* The values are kept in ~/.dt/parental.conf under the login's name
* ("lotherk.allow=dtpad,dtfile"), the file dtlaunch reads. Administrators
* are marked: the limits do not apply to them.
*
* What a pane cannot do alone: taking the execute permission away from
* programs outside the list, ending a session when the hours are over, and
* a child's own ~/.dt/parental.conf are follow-ups (they need root and a
* dtsession/dtlogin part); this page and dtlaunch are the desktop's part.
*
* 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/PushB.h>
#include <Xm/PushBG.h>
#include <Xm/RowColumn.h>
#include <Xm/ScrolledW.h>
#include <Xm/Text.h>
#include <Xm/TextF.h>
#include <Xm/ToggleB.h>
#include <Dt/McdePrefs.h>
#include <Mcde/McdeSys.h>
#include "ui.h"
#define MAX_PROGRAMS 12
static const char *const known[MAX_PROGRAMS] = {
"dtpad", "dtfile", "dtterm", "dtmail", "dtcm", "dtcalc",
"dtlaunch", "dthelp", "dtdiskutility", "dtprintinfo", "dtpaint",
"dtphoto"
};
static Widget acctList, status, onlyToggle, dayField, weekendField;
static Widget progList, allPrograms;
static Widget logText;
static Widget pageBox, pageContent;
static int pageShown = -1;
static char login[64];
/* ---- the account file (~/.dt/parental.conf, read by dtlaunch) ----------- */
static const char *
Get(const char *key, const char *def, char *buf, size_t len)
{
char k[128];
(void)snprintf(k, sizeof k, "%s.%s", login, key);
if (!McdePrefsConfGet("parental", k, buf, len) || buf[0] == '\0')
(void)snprintf(buf, len, "%s", def);
return buf;
}
static int
Allowed(const char *program, const char *list)
{
char copy[1024], *tok, *save = NULL;
(void)snprintf(copy, sizeof copy, "%s", list);
for (tok = strtok_r(copy, ", ", &save); tok != NULL;
tok = strtok_r(NULL, ", ", &save))
if (strcmp(tok, program) == 0)
return 1;
return 0;
}
static void
ApplyCB(Widget w, XtPointer data, XtPointer call)
{
char list[1024], key[128], v[8];
char *day, *weekend;
int i, n = 0;
(void)w;
(void)data;
(void)call;
if (login[0] == '\0') {
SetText(status, "Choose an account first.");
return;
}
{
int *pos = NULL, cnt = 0;
list[0] = '\0';
if (progList != NULL &&
XmListGetSelectedPos(progList, &pos, &cnt)) {
for (i = 0; i < cnt; i++) {
int at2 = pos[i] - 1;
size_t at = strlen(list);
if (at2 < 0 || at2 >= MAX_PROGRAMS)
continue;
(void)snprintf(list + at, sizeof list - at,
"%s%s", n > 0 ? "," : "", known[at2]);
n++;
}
XtFree((char *) pos);
}
}
(void)snprintf(key, sizeof key, "%s.only", login);
(void)snprintf(v, sizeof v, "%d",
onlyToggle != NULL && XmToggleButtonGetState(onlyToggle) ? 1 : 0);
(void)McdePrefsConfSet("parental", key, v);
(void)snprintf(key, sizeof key, "%s.allow", login);
(void)McdePrefsConfSet("parental", key, list);
day = XmTextFieldGetString(dayField);
weekend = XmTextFieldGetString(weekendField);
(void)snprintf(key, sizeof key, "%s.day", login);
(void)McdePrefsConfSet("parental", key,
day != NULL && day[0] != '\0' ? day : "any");
(void)snprintf(key, sizeof key, "%s.weekend", login);
(void)McdePrefsConfSet("parental", key,
weekend != NULL && weekend[0] != '\0' ? weekend : "any");
if (day != NULL)
XtFree(day);
if (weekend != NULL)
XtFree(weekend);
SetText(status, "Kept: the launcher enforces it in this account's "
"session.");
}
/* ---- the pages ---------------------------------------------------------- */
static Widget
ProgramsPage(Widget parent)
{
Widget page = XtVaCreateWidget("parentalPrograms",
xmRowColumnWidgetClass, parent, XmNorientation, XmVERTICAL,
XmNspacing, 6, NULL);
Widget radio;
char allow[1024], only[8];
int i;
/* a radio pair, not a lone check box: the state shows (measured: a
* toggle set from code kept its indicator off in this pane) */
radio = XtVaCreateManagedWidget("programMode",
xmRowColumnWidgetClass, page, XmNorientation, XmVERTICAL,
XmNradioBehavior, True, XmNspacing, 2, XmNmarginWidth, 0, NULL);
allPrograms = XtVaCreateManagedWidget("All programs",
xmToggleButtonWidgetClass, radio, NULL);
onlyToggle = XtVaCreateManagedWidget("Only the applications "
"selected below", xmToggleButtonWidgetClass, radio, NULL);
Get("only", "0", only, sizeof only);
XmToggleButtonSetState(atoi(only) != 0 ? onlyToggle : allPrograms,
True, False);
(void) Text(page, "onlyNote", "The launcher shows only the selected "
"ones; the rest are refused.");
(void) Text(page, "groupsLabel", "Applications:");
progList = XmCreateScrolledList(page, "programs", NULL, 0);
XtVaSetValues(progList, XmNvisibleItemCount, 10,
XmNselectionPolicy, XmMULTIPLE_SELECT, NULL);
XtVaSetValues(XtParent(progList), XmNwidth, 300, XmNheight, 240, NULL);
for (i = 0; i < MAX_PROGRAMS; i++) {
XmString s = XmStringCreateLocalized((char *) known[i]);
XmListAddItemUnselected(progList, s, 0);
XmStringFree(s);
}
XtManageChild(progList);
Get("allow", "", allow, sizeof allow);
for (i = 0; i < MAX_PROGRAMS; i++)
if (Allowed(known[i], allow))
XmListSelectPos(progList, i + 1, False);
return page;
}
static Widget
HoursPage(Widget parent)
{
Widget page = XtVaCreateWidget("parentalHours",
xmRowColumnWidgetClass, parent, XmNorientation, XmVERTICAL,
XmNspacing, 6, NULL);
Widget row = XtVaCreateManagedWidget("dayRow",
xmRowColumnWidgetClass, page, XmNorientation, XmHORIZONTAL,
XmNspacing, 8, XmNmarginWidth, 0, NULL);
char buf[64];
(void) Text(row, "dayLabel", "Weekday hours (HH:MM-HH:MM, or any):");
dayField = XtVaCreateManagedWidget("dayHours", xmTextFieldWidgetClass,
row, XmNcolumns, 16, NULL);
XmTextFieldSetString(dayField, (char *) Get("day", "any", buf,
sizeof buf));
row = XtVaCreateManagedWidget("weekendRow", xmRowColumnWidgetClass,
page, XmNorientation, XmHORIZONTAL, XmNspacing, 8,
XmNmarginWidth, 0, NULL);
(void) Text(row, "weekendLabel", "Weekend hours:");
weekendField = XtVaCreateManagedWidget("weekendHours",
xmTextFieldWidgetClass, row, XmNcolumns, 16, NULL);
XmTextFieldSetString(weekendField, (char *) Get("weekend", "any", buf,
sizeof buf));
(void) Text(page, "hoursNote", "The launcher refuses to start a "
"program outside these hours and says why.");
(void) Text(page, "hoursNote2", "Ending a running session when the "
"hours are over belongs to dtsession; that part is a follow-up.");
return page;
}
static Widget
LogPage(Widget parent)
{
Widget page = XtVaCreateWidget("parentalLog",
xmRowColumnWidgetClass, parent, XmNorientation, XmVERTICAL,
XmNspacing, 6, NULL);
Widget sw = XtVaCreateManagedWidget("logBox",
xmScrolledWindowWidgetClass, page, XmNwidth, 420,
XmNheight, 180, XmNscrollingPolicy, XmAUTOMATIC, NULL);
char path[1024], line[256];
FILE *f;
const char *home = getenv("HOME");
XmTextPosition pos = 0;
logText = XtVaCreateManagedWidget("logText", xmTextWidgetClass, sw,
XmNeditable, False, XmNcursorPositionVisible, False,
XmNwordWrap, False, NULL);
if (home != NULL && (size_t) snprintf(path, sizeof path,
"%s/.dt/parental.log", home) < sizeof path &&
(f = fopen(path, "r")) != NULL) {
while (fgets(line, sizeof line, f) != NULL) {
XmTextInsert(logText, pos, line);
pos += (XmTextPosition) strlen(line);
}
fclose(f);
}
(void) Text(page, "logNote", "One line per program the launcher "
"started; it is written when a program is allowed and started.");
return page;
}
static void SyncPrograms(void);
/* the toggles take the file's values: a state given before the page was
* managed does not show (measured with the Keyboard pane too) */
static void
SyncPrograms(void)
{
char allow[1024];
int i;
if (onlyToggle == NULL || progList == NULL)
return;
{
char only[8];
/* the pair again once the page is laid out (a radio state set
* before does not paint, measured) */
Get("only", "0", only, sizeof only);
XmToggleButtonSetState(allPrograms, False, True);
XmToggleButtonSetState(onlyToggle, False, True);
XmToggleButtonSetState(atoi(only) != 0 ? onlyToggle :
allPrograms, True, True);
}
Get("allow", "", allow, sizeof allow);
XmListDeselectAllItems(progList);
for (i = 0; i < MAX_PROGRAMS; i++)
if (Allowed(known[i], allow))
XmListSelectPos(progList, i + 1, False);
}
static void
SyncLater(XtPointer data, XtIntervalId *id)
{
(void) data;
(void) id;
SyncPrograms();
}
static void
ShowPage(int which)
{
if (pageContent != NULL) {
XtDestroyWidget(pageContent);
pageContent = NULL;
}
pageShown = which;
if (which == 1)
pageContent = HoursPage(pageBox);
else if (which == 2)
pageContent = LogPage(pageBox);
else
pageContent = ProgramsPage(pageBox);
XtManageChild(pageContent);
if (which == 0) {
/* once the page is laid out: a state given before does not paint */
XtAppAddTimeOut(app, 0, SyncLater, NULL);
SyncPrograms();
}
}
static void
TabChosen(Widget w, XtPointer data, XtPointer call)
{
int which = (int) (size_t) data;
(void) w;
if (call != NULL && !((XmToggleButtonCallbackStruct *) call)->set)
return;
if (which != pageShown)
ShowPage(which);
}
/* ---- the account list --------------------------------------------------- */
static void
AccountChosen(Widget w, XtPointer data, XtPointer call)
{
XmListCallbackStruct *c = (XmListCallbackStruct *) call;
char *text = NULL;
(void) w;
(void) data;
if (c->item_position < 1)
return;
if (!XmStringGetLtoR(c->item, XmFONTLIST_DEFAULT_TAG, &text) ||
text == NULL)
return;
/* "login (name)" or "login (name, administrator)" */
(void)snprintf(login, sizeof login, "%.*s",
(int) strcspn(text, " "), text);
XmStringFree((XmString) text);
ShowPage(pageShown < 0 ? 0 : pageShown);
SetText(status, "Editing the limits of this account.");
}
Widget
MakeParental(Widget parent)
{
Widget box = XtVaCreateWidget("parental", xmRowColumnWidgetClass,
parent, XmNorientation, XmVERTICAL, XmNspacing, 8, NULL);
Widget body, left, right, tabs, buttons;
McdeSysUser users[32], *u;
int n, i, admins = 0;
XmString s;
body = XtVaCreateManagedWidget("parentalBody", xmRowColumnWidgetClass,
box, XmNorientation, XmHORIZONTAL, XmNspacing, 16,
XmNmarginWidth, 0, NULL);
left = XtVaCreateManagedWidget("parentalAccounts",
xmRowColumnWidgetClass, body, XmNorientation, XmVERTICAL,
XmNspacing, 4, XmNmarginWidth, 0, NULL);
(void) Text(left, "accountsLabel", "Accounts:");
acctList = XmCreateScrolledList(left, "accounts", NULL, 0);
XtVaSetValues(acctList, XmNvisibleItemCount, 8, NULL);
XtVaSetValues(XtParent(acctList), XmNwidth, 220, XmNheight, 220,
NULL);
XtManageChild(acctList);
XtAddCallback(acctList, XmNbrowseSelectionCallback, AccountChosen,
NULL);
n = mcde_sys_users(users, 32);
for (i = 0; i < n; i++) {
char row[220];
u = &users[i];
(void)snprintf(row, sizeof row, "%s (%s%s)", u->login,
u->fullname[0] != '\0' ? u->fullname : "no name",
u->admin ? ", administrator" : "");
if (u->admin)
admins++;
s = XmStringCreateLocalized(row);
XmListAddItemUnselected(acctList, s, 0);
XmStringFree(s);
}
if (n > 0)
XmListSelectPos(acctList, 1, False);
right = XtVaCreateManagedWidget("parentalRight",
xmRowColumnWidgetClass, body, XmNorientation, XmVERTICAL,
XmNspacing, 6, XmNmarginWidth, 0, NULL);
tabs = XtVaCreateManagedWidget("parentalTabs",
xmRowColumnWidgetClass, right, XmNorientation, XmHORIZONTAL,
XmNspacing, 4, XmNmarginWidth, 0, XmNradioBehavior, True, NULL);
{
static const char *const labels[3] = { "Programs",
"Time Limits", "Log" };
int k;
for (k = 0; k < 3; k++) {
XmString ls = XmStringCreateLocalized((char *)
labels[k]);
Widget t = XtVaCreateManagedWidget("parentalTab",
xmToggleButtonWidgetClass, tabs,
XmNlabelString, ls, XmNindicatorOn, False,
XmNshadowThickness, 2, XmNfillOnSelect, True,
XmNmarginWidth, 10, NULL);
XmStringFree(ls);
XtAddCallback(t, XmNvalueChangedCallback, TabChosen,
(XtPointer) (size_t) k);
}
}
pageBox = XtVaCreateManagedWidget("parentalPage",
xmRowColumnWidgetClass, right, XmNorientation, XmVERTICAL,
XmNspacing, 4, XmNmarginWidth, 0, NULL);
buttons = XtVaCreateManagedWidget("parentalButtons",
xmRowColumnWidgetClass, box, XmNorientation, XmHORIZONTAL,
XmNspacing, 8, XmNmarginWidth, 0, NULL);
(void) Button(buttons, "Apply", ApplyCB, NULL);
status = Text(box, "parentalStatus", "");
{
/* the first account chosen, the first page shown */
char first[128];
Widget fl = XtNameToWidget(left, "accountsLabel");
(void) fl;
if (n > 0) {
char *text = NULL;
(void)snprintf(first, sizeof first, "%s",
users[0].login);
(void)snprintf(login, sizeof login, "%s", first);
}
ShowPage(0);
XmToggleButtonSetState(XtNameToWidget(tabs, "parentalTab"),
True, False);
}
if (admins == n)
SetText(status, "All accounts administer this computer; the "
"limits apply to a standard account.");
return box;
}