share.c's seven and users.c's six mcde_sys_fail calls are messages 57-67 of mcdesys.cat now: cannot write beside/into a file, cannot replace, only the administrator can change what is shared, no folder that can be shared; no name for an account, an account exists already, a name has no colon, only the administrator can add/delete an account (unlock first), no such account. The German copy in programs/localized/de_DE.UTF-8/msg/mcdesys.msg keeps the numbers. Measured with a runner (setlocale + mcde_sys_shares_set on a missing folder, mcde_sys_user_delete on a missing login): LANG=C "Only the administrator can change what is shared." and "There is no account \"nosuchuser_xyz\"."; LANG=de_DE.UTF-8 "Nur der Administrator kann ändern, was freigegeben ist." and "Es gibt kein Konto \"nosuchuser_xyz\"." make mcde-linux MCDE_DIRS="install lib/McdeSys" and "install programs/localized/C/msg" exit=0; "check lib/McdeSys": # FAIL: 0, exit=0; MCDE_DIRS=plist: ok.
128 lines
3.9 KiB
C
128 lines
3.9 KiB
C
/*
|
|
* users.c -- MCDE: the people who use the computer (McdeSys.h, "Users")
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*
|
|
* 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, mcde_sys_msg(MSG_USR_BAD_NAME, "\"%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, mcde_sys_msg(MSG_USR_EXISTS, "There is an account \"%s\" already."), user->login);
|
|
if (strchr(user->fullname, ':') != NULL)
|
|
return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_USR_COLON, "A name has no colon."));
|
|
if (geteuid() != 0)
|
|
return mcde_sys_fail(MCDE_SYS_EPERM, mcde_sys_msg(MSG_USR_ADD_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, mcde_sys_msg(MSG_USR_NOT_FOUND, "There is no account \"%s\"."), login);
|
|
if (geteuid() != 0)
|
|
return mcde_sys_fail(MCDE_SYS_EPERM, mcde_sys_msg(MSG_USR_DEL_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);
|
|
}
|