McdeSys: mcde_sys_users/user_add/user_delete (users.c, POSIX getpwent and useradd/userdel), user_set_password and user_set_admin per system (NetBSD: pw_gensalt+crypt for usermod -p, wheel; a group member removed by rewriting the group's line as user.c does, since usermod -G only appends -- measured; Linux: chpasswd on stdin, gpasswd, sudo), mcde_sys_update_check/apply (NetBSD: /etc/makronix/update, RELEASE, sysupgrade, pkg_add -uu; Linux: apt-get). A secret goes through a pipe, never on a command line. Tests: test_sysinfo refuses a bad login and, not as root, adding (PASS on Linux and NetBSD); as root in the NetBSD VM add/admin on/off/password/delete pass and /etc/group is as before. dtsystemsettings: Accounts (list, Add/Delete/Password/Administrator, Unlock... through su -m root in a terminal, password fields show stars), Software Update (running, available, new packages, source, Check Now, Update Now... runs -apply-update as root in a terminal; the check runs after the pane shows: apt took 21 s here). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
453 lines
11 KiB
C
453 lines
11 KiB
C
/*
|
|
* accounts.c -- dtsystemsettings: the people who use this computer
|
|
*
|
|
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
|
|
*
|
|
* The accounts from McdeSys (mcde_sys_users), and adding, deleting, a new
|
|
* password, administrator or not -- all through McdeSys, which does it the
|
|
* system's way and refuses unless it runs as root. Not as root, the pane
|
|
* says so and offers "Unlock...": a terminal where su(1) asks for the
|
|
* administrator's password and starts this pane as root. A password is
|
|
* typed into a field that shows stars and goes to McdeSys only.
|
|
*
|
|
* 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 <X11/Intrinsic.h>
|
|
#include <Xm/Xm.h>
|
|
#include <Xm/Form.h>
|
|
#include <Xm/Label.h>
|
|
#include <Xm/List.h>
|
|
#include <Xm/MessageB.h>
|
|
#include <Xm/PushB.h>
|
|
#include <Xm/RowColumn.h>
|
|
#include <Xm/TextF.h>
|
|
#include <Xm/ToggleB.h>
|
|
|
|
#include <Mcde/McdeSys.h>
|
|
|
|
#include "ui.h"
|
|
|
|
#define USERS_MAX 128
|
|
|
|
static Widget list, delButton, pwButton, adminButton;
|
|
static McdeSysUser users[USERS_MAX];
|
|
static int nusers;
|
|
|
|
/* ---- a password field: shows stars, keeps the text itself ------------- */
|
|
|
|
typedef struct {
|
|
char text[128];
|
|
size_t len;
|
|
} Secret;
|
|
|
|
static void
|
|
SecretVerify(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
XmTextVerifyCallbackStruct *cbs = (XmTextVerifyCallbackStruct *) call;
|
|
Secret *sec = (Secret *) data;
|
|
size_t from = (size_t) cbs->startPos, to = (size_t) cbs->endPos;
|
|
size_t add = cbs->text->ptr != NULL ? (size_t) cbs->text->length : 0;
|
|
size_t i;
|
|
|
|
(void) w;
|
|
if (to > sec->len)
|
|
to = sec->len;
|
|
if (from > to || sec->len - (to - from) + add >= sizeof sec->text) {
|
|
cbs->doit = False;
|
|
return;
|
|
}
|
|
memmove(sec->text + from + add, sec->text + to, sec->len - to);
|
|
if (add > 0)
|
|
memcpy(sec->text + from, cbs->text->ptr, add);
|
|
sec->len = sec->len - (to - from) + add;
|
|
sec->text[sec->len] = '\0';
|
|
for (i = 0; i < add; i++)
|
|
cbs->text->ptr[i] = '*';
|
|
}
|
|
|
|
static void
|
|
SecretFree(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
Secret *sec = (Secret *) data;
|
|
|
|
(void) w;
|
|
(void) call;
|
|
memset(sec, 0, sizeof *sec); /* no password left in memory */
|
|
free(sec);
|
|
}
|
|
|
|
static Widget
|
|
SecretField(Widget parent, const char *name, Secret **out)
|
|
{
|
|
Secret *sec = calloc(1, sizeof *sec);
|
|
Widget f = XtVaCreateManagedWidget(name, xmTextFieldWidgetClass, parent,
|
|
XmNcolumns, 24, NULL);
|
|
|
|
XtAddCallback(f, XmNmodifyVerifyCallback, SecretVerify, sec);
|
|
XtAddCallback(f, XmNdestroyCallback, SecretFree, sec);
|
|
*out = sec;
|
|
return f;
|
|
}
|
|
|
|
/* ---- the list ----------------------------------------------------------- */
|
|
|
|
static int
|
|
Selected(void)
|
|
{
|
|
int *pos = NULL, n = 0, at = -1;
|
|
|
|
if (XmListGetSelectedPos(list, &pos, &n) && n > 0)
|
|
at = pos[0] - 1;
|
|
if (pos != NULL)
|
|
XtFree((char *) pos);
|
|
return at >= 0 && at < nusers ? at : -1;
|
|
}
|
|
|
|
static void
|
|
Buttons(void)
|
|
{
|
|
int at = Selected();
|
|
|
|
XtSetSensitive(delButton, at >= 0);
|
|
XtSetSensitive(pwButton, at >= 0);
|
|
XtSetSensitive(adminButton, at >= 0);
|
|
SetText(adminButton, at >= 0 && users[at].admin ?
|
|
"Not Administrator" : "Make Administrator");
|
|
}
|
|
|
|
static void
|
|
Fill(void)
|
|
{
|
|
char line[256];
|
|
int i;
|
|
|
|
XmListDeleteAllItems(list);
|
|
nusers = mcde_sys_users(users, USERS_MAX);
|
|
for (i = 0; i < nusers; i++) {
|
|
XmString s;
|
|
|
|
snprintf(line, sizeof line, "%s (%s)%s", users[i].fullname[0] !=
|
|
'\0' ? users[i].fullname : users[i].login, users[i].login,
|
|
users[i].admin ? " - Administrator" : "");
|
|
s = XmStringCreateLocalized(line);
|
|
XmListAddItemUnselected(list, s, 0);
|
|
XmStringFree(s);
|
|
}
|
|
Buttons();
|
|
}
|
|
|
|
static void
|
|
SelectionChanged(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
(void) w;
|
|
(void) data;
|
|
(void) call;
|
|
Buttons();
|
|
}
|
|
|
|
static void
|
|
Done(int status, Widget parent)
|
|
{
|
|
if (status != MCDE_SYS_OK)
|
|
Message(parent, mcde_sys_error(), 1);
|
|
Fill();
|
|
}
|
|
|
|
/* ---- the dialogs -------------------------------------------------------- */
|
|
|
|
typedef struct {
|
|
Widget dialog, fullname, login, admin;
|
|
Secret *pw1, *pw2;
|
|
int user; /* the password dialog: whose */
|
|
} Form;
|
|
|
|
static void
|
|
FormGone(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
(void) w;
|
|
(void) call;
|
|
free(data);
|
|
}
|
|
|
|
static Widget
|
|
Row(Widget box, const char *label)
|
|
{
|
|
Widget row = XtVaCreateManagedWidget("row", xmRowColumnWidgetClass, box,
|
|
XmNorientation, XmHORIZONTAL, XmNpacking, XmPACK_COLUMN,
|
|
XmNnumColumns, 1, XmNmarginWidth, 0, NULL);
|
|
|
|
Text(row, "label", label);
|
|
return row;
|
|
}
|
|
|
|
static Widget
|
|
Dialog(Widget parent, const char *title, const char *ok, Form *f,
|
|
XtCallbackProc okProc)
|
|
{
|
|
XmString t = XmStringCreateLocalized((char *) title);
|
|
XmString o = XmStringCreateLocalized((char *) ok);
|
|
XmString c = XmStringCreateLocalized("Cancel");
|
|
Arg a[4];
|
|
Widget d;
|
|
|
|
/* a template dialog makes the OK and Cancel buttons from their
|
|
* labels (XmCreateTemplateDialog(3)) */
|
|
XtSetArg(a[0], XmNdialogTitle, t);
|
|
XtSetArg(a[1], XmNokLabelString, o);
|
|
XtSetArg(a[2], XmNcancelLabelString, c);
|
|
XtSetArg(a[3], XmNautoUnmanage, False);
|
|
d = XmCreateTemplateDialog(parent, "accountDialog", a, 4);
|
|
XmStringFree(t);
|
|
XmStringFree(o);
|
|
XmStringFree(c);
|
|
f->dialog = d;
|
|
XtAddCallback(d, XmNokCallback, okProc, f);
|
|
XtAddCallback(d, XmNdestroyCallback, FormGone, f);
|
|
return d;
|
|
}
|
|
|
|
static void
|
|
Close(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
Form *f = (Form *) data;
|
|
|
|
(void) w;
|
|
(void) call;
|
|
XtDestroyWidget(XtParent(f->dialog));
|
|
}
|
|
|
|
static int
|
|
SamePasswords(Form *f)
|
|
{
|
|
if (strcmp(f->pw1->text, f->pw2->text) != 0) {
|
|
Message(f->dialog, "The two passwords differ.", 1);
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
static void
|
|
AddOk(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
Form *f = (Form *) data;
|
|
McdeSysUser u;
|
|
char *s;
|
|
int r;
|
|
|
|
(void) w;
|
|
(void) call;
|
|
if (!SamePasswords(f))
|
|
return;
|
|
memset(&u, 0, sizeof u);
|
|
s = XmTextFieldGetString(f->login);
|
|
snprintf(u.login, sizeof u.login, "%s", s);
|
|
XtFree(s);
|
|
s = XmTextFieldGetString(f->fullname);
|
|
snprintf(u.fullname, sizeof u.fullname, "%s", s);
|
|
XtFree(s);
|
|
u.admin = XmToggleButtonGetState(f->admin);
|
|
r = mcde_sys_user_add(&u, f->pw1->text);
|
|
if (r != MCDE_SYS_OK) {
|
|
Message(f->dialog, mcde_sys_error(), 1);
|
|
return;
|
|
}
|
|
XtDestroyWidget(XtParent(f->dialog));
|
|
Fill();
|
|
}
|
|
|
|
static void
|
|
Add(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
Form *f = calloc(1, sizeof *f);
|
|
Widget d, box, row;
|
|
|
|
(void) data;
|
|
(void) call;
|
|
d = Dialog(w, "New Account", "Add Account", f, AddOk);
|
|
XtAddCallback(d, XmNcancelCallback, Close, f);
|
|
box = XtVaCreateManagedWidget("box", xmRowColumnWidgetClass, d, NULL);
|
|
row = Row(box, "Full name:");
|
|
f->fullname = XtVaCreateManagedWidget("fullname", xmTextFieldWidgetClass,
|
|
row, XmNcolumns, 24, NULL);
|
|
row = Row(box, "Account name:");
|
|
f->login = XtVaCreateManagedWidget("login", xmTextFieldWidgetClass, row,
|
|
XmNcolumns, 24, NULL);
|
|
row = Row(box, "Password:");
|
|
SecretField(row, "pw1", &f->pw1);
|
|
row = Row(box, "Again:");
|
|
SecretField(row, "pw2", &f->pw2);
|
|
f->admin = XtVaCreateManagedWidget("May administer this computer",
|
|
xmToggleButtonWidgetClass, box, NULL);
|
|
XtManageChild(d);
|
|
}
|
|
|
|
static void
|
|
PasswordOk(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
Form *f = (Form *) data;
|
|
int r;
|
|
|
|
(void) w;
|
|
(void) call;
|
|
if (!SamePasswords(f))
|
|
return;
|
|
r = mcde_sys_user_set_password(users[f->user].login, f->pw1->text);
|
|
if (r != MCDE_SYS_OK) {
|
|
Message(f->dialog, mcde_sys_error(), 1);
|
|
return;
|
|
}
|
|
XtDestroyWidget(XtParent(f->dialog));
|
|
Message(list, "The password is set.", 0);
|
|
}
|
|
|
|
static void
|
|
Password(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
Form *f;
|
|
Widget d, box, row;
|
|
char title[96];
|
|
int at = Selected();
|
|
|
|
(void) data;
|
|
(void) call;
|
|
if (at < 0)
|
|
return;
|
|
f = calloc(1, sizeof *f);
|
|
f->user = at;
|
|
snprintf(title, sizeof title, "New Password for %s", users[at].login);
|
|
d = Dialog(w, title, "Set Password", f, PasswordOk);
|
|
XtAddCallback(d, XmNcancelCallback, Close, f);
|
|
box = XtVaCreateManagedWidget("box", xmRowColumnWidgetClass, d, NULL);
|
|
row = Row(box, "New password:");
|
|
SecretField(row, "pw1", &f->pw1);
|
|
row = Row(box, "Again:");
|
|
SecretField(row, "pw2", &f->pw2);
|
|
XtManageChild(d);
|
|
}
|
|
|
|
static int deleteUser = -1;
|
|
|
|
static void
|
|
DeleteOk(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
Widget keep = (Widget) data;
|
|
int r;
|
|
|
|
(void) call;
|
|
if (deleteUser < 0)
|
|
return;
|
|
r = mcde_sys_user_delete(users[deleteUser].login,
|
|
XmToggleButtonGetState(keep));
|
|
XtDestroyWidget(XtParent(w));
|
|
Done(r, list);
|
|
}
|
|
|
|
static void
|
|
DeleteCancel(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
(void) data;
|
|
(void) call;
|
|
XtDestroyWidget(XtParent(w));
|
|
}
|
|
|
|
static void
|
|
Delete(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
char text[256];
|
|
XmString m;
|
|
Arg a[2];
|
|
Widget d, keep;
|
|
|
|
(void) data;
|
|
(void) call;
|
|
if ((deleteUser = Selected()) < 0)
|
|
return;
|
|
if (users[deleteUser].uid == getuid()) {
|
|
Message(w, "This is the account you are using now.", 1);
|
|
return;
|
|
}
|
|
snprintf(text, sizeof text, "Delete the account \"%s\"?",
|
|
users[deleteUser].login);
|
|
m = XmStringCreateLocalized(text);
|
|
XtSetArg(a[0], XmNmessageString, m);
|
|
d = XmCreateQuestionDialog(w, "delete", a, 1);
|
|
XmStringFree(m);
|
|
XtUnmanageChild(XmMessageBoxGetChild(d, XmDIALOG_HELP_BUTTON));
|
|
keep = XtVaCreateManagedWidget("Keep the home folder",
|
|
xmToggleButtonWidgetClass, d, XmNset, True, NULL);
|
|
XtAddCallback(d, XmNokCallback, DeleteOk, keep);
|
|
XtAddCallback(d, XmNcancelCallback, DeleteCancel, NULL);
|
|
XtManageChild(d);
|
|
}
|
|
|
|
static void
|
|
Admin(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
int at = Selected();
|
|
|
|
(void) data;
|
|
(void) call;
|
|
if (at < 0)
|
|
return;
|
|
if (users[at].admin && users[at].uid == getuid()) {
|
|
Message(w, "You cannot take the administrator's rights from "
|
|
"yourself here.", 1);
|
|
return;
|
|
}
|
|
Done(mcde_sys_user_set_admin(users[at].login, !users[at].admin), w);
|
|
}
|
|
|
|
static void
|
|
Unlock(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
char cmd[512];
|
|
|
|
(void) w;
|
|
(void) data;
|
|
(void) call;
|
|
AsRoot("Unlock Accounts", "dtsystemsettings -pane accounts", cmd,
|
|
sizeof cmd);
|
|
Spawn(cmd);
|
|
}
|
|
|
|
Widget
|
|
MakeAccounts(Widget parent)
|
|
{
|
|
Widget box = XtVaCreateWidget("accounts", xmRowColumnWidgetClass, parent,
|
|
XmNspacing, 8, NULL);
|
|
Widget row;
|
|
int root = geteuid() == 0;
|
|
|
|
if (!root) {
|
|
Widget lock = XtVaCreateManagedWidget("lock",
|
|
xmRowColumnWidgetClass, box, XmNorientation, XmHORIZONTAL,
|
|
XmNmarginWidth, 0, NULL);
|
|
|
|
Text(lock, "locked", "Changes need the administrator.");
|
|
Button(lock, "Unlock...", Unlock, NULL);
|
|
}
|
|
Text(box, "intro", "The people who use this computer:");
|
|
list = XmCreateScrolledList(box, "users", NULL, 0);
|
|
XtVaSetValues(list, XmNvisibleItemCount, 8, XmNwidth, 460,
|
|
XmNselectionPolicy, XmBROWSE_SELECT, NULL);
|
|
XtAddCallback(list, XmNbrowseSelectionCallback, SelectionChanged, NULL);
|
|
XtManageChild(list);
|
|
row = XtVaCreateManagedWidget("buttons", xmRowColumnWidgetClass, box,
|
|
XmNorientation, XmHORIZONTAL, XmNmarginWidth, 0, NULL);
|
|
Button(row, "Add...", Add, NULL);
|
|
delButton = Button(row, "Delete...", Delete, NULL);
|
|
pwButton = Button(row, "Password...", Password, NULL);
|
|
adminButton = Button(row, "Make Administrator", Admin, NULL);
|
|
XtSetSensitive(row, root);
|
|
Fill();
|
|
return box;
|
|
}
|