Files
mcde/lib/McdeSys/users.c
T
lotherkandClaude Opus 5.5 8ab668c689 mcde: Accounts and Software Update, in dtsystemsettings and McdeSys
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>
2026-11-10 12:38:21 +00:00

134 lines
3.7 KiB
C

/*
* users.c -- MCDE: the people who use the computer (McdeSys.h, "Users")
*
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
*
* The list comes from the password and group databases (getpwent(3),
* getgrnam(3): POSIX, XSI); an account is a person's when its uid is from
* 1000 to 59999 and its shell is not nologin -- the range useradd(8) gives
* on NetBSD and on Linux. An administrator is a member of the group the
* backend names (wheel, sudo). useradd(8) and userdel(8) take the same
* options on both systems; the password and the administrator's group are
* the backend's (netbsd.c, linux.c). Changing needs root.
*/
#define _XOPEN_SOURCE 700
#include <ctype.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <Mcde/McdeSys.h>
#include "private.h"
static char useradd[] = "/usr/sbin/useradd";
static char userdel[] = "/usr/sbin/userdel";
/* [a-z_][a-z0-9_-]*, at most 31: what useradd(8) takes on both systems */
static int
ValidLogin(const char *s)
{
size_t i, n = strlen(s);
if (n == 0 || n > 31 || !(islower((unsigned char) s[0]) || s[0] == '_'))
return 0;
for (i = 1; i < n; i++)
if (!(islower((unsigned char) s[i]) ||
isdigit((unsigned char) s[i]) || s[i] == '_' || s[i] == '-'))
return 0;
return 1;
}
int
mcde_sys_user_is_admin(const char *login)
{
struct group *g = getgrnam(mcde_sys_admin_group());
char **m;
if (g == NULL)
return 0;
for (m = g->gr_mem; *m != NULL; m++)
if (strcmp(*m, login) == 0)
return 1;
return 0;
}
int
mcde_sys_users(McdeSysUser *users, size_t max)
{
struct passwd *pw;
size_t n = 0;
setpwent();
while (n < max && (pw = getpwent()) != NULL) {
McdeSysUser *u;
if (pw->pw_uid < 1000 || pw->pw_uid >= 60000 ||
strstr(pw->pw_shell, "nologin") != NULL)
continue;
u = &users[n++];
memset(u, 0, sizeof *u);
snprintf(u->login, sizeof u->login, "%s", pw->pw_name);
snprintf(u->fullname, sizeof u->fullname, "%s", pw->pw_gecos);
u->fullname[strcspn(u->fullname, ",")] = '\0';
u->uid = pw->pw_uid;
u->admin = mcde_sys_user_is_admin(pw->pw_name);
}
endpwent();
return (int) n;
}
int
mcde_sys_user_add(const McdeSysUser *user, const char *password)
{
char fullname[160];
char *argv[] = { useradd, "-m", "-c", fullname, "-s", NULL, NULL, NULL };
int r;
if (!ValidLogin(user->login))
return mcde_sys_fail(MCDE_SYS_EINVAL, "\"%s\" is no name for an "
"account: small letters, digits, - and _, starting with a "
"letter.", user->login);
if (getpwnam(user->login) != NULL)
return mcde_sys_fail(MCDE_SYS_EEXIST, "There is an account "
"\"%s\" already.", user->login);
if (strchr(user->fullname, ':') != NULL)
return mcde_sys_fail(MCDE_SYS_EINVAL, "A name has no colon.");
if (geteuid() != 0)
return mcde_sys_fail(MCDE_SYS_EPERM, "Only the administrator "
"can add an account: unlock first.");
snprintf(fullname, sizeof fullname, "%s", user->fullname);
argv[5] = (char *) mcde_sys_default_shell();
argv[6] = (char *) user->login;
if ((r = mcde_sys_run(argv)) != MCDE_SYS_OK)
return r;
if (password != NULL && password[0] != '\0' &&
(r = mcde_sys_user_set_password(user->login, password)) != MCDE_SYS_OK)
return r;
if (user->admin)
return mcde_sys_user_set_admin(user->login, 1);
return MCDE_SYS_OK;
}
int
mcde_sys_user_delete(const char *login, int keep_home)
{
char *argv[] = { userdel, NULL, NULL, NULL };
int n = 1;
if (getpwnam(login) == NULL)
return mcde_sys_fail(MCDE_SYS_ENOENT, "There is no account "
"\"%s\".", login);
if (geteuid() != 0)
return mcde_sys_fail(MCDE_SYS_EPERM, "Only the administrator "
"can delete an account: unlock first.");
if (!keep_home)
argv[n++] = "-r";
argv[n] = (char *) login;
return mcde_sys_run(argv);
}