#ifndef lint
static const char rcsid[] =
- "$Id: pw_user.c,v 1.28 1999/03/02 00:53:33 davidn Exp $";
+ "$FreeBSD$";
#endif /* not lint */
#include <ctype.h>
#endif
static int randinit;
+static char locked_str[] = "*LOCKED*";
static int print_user(struct passwd * pwd, int pretty, int v7);
static uid_t pw_uidpolicy(struct userconf * cnf, struct cargs * args);
int
pw_user(struct userconf * cnf, int mode, struct cargs * args)
{
- int r, r1;
+ int rc, edited = 0;
char *p = NULL;
+ char *passtmp;
struct carg *a_name;
struct carg *a_uid;
struct carg *arg;
0
};
+
/*
* With M_NEXT, we only need to return the
* next uid to stdout
errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home);
}
-
if ((arg = getarg(args, 'e')) != NULL)
cnf->expire_days = atoi(arg->val);
cnf->default_class = pw_checkname((u_char *)arg->val, 0);
if ((arg = getarg(args, 'G')) != NULL && arg->val) {
- int i = 0;
+ int i = 0;
for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
if ((grp = GETGRNAM(p)) == NULL) {
while (i < cnf->numgroups)
cnf->groups[i++] = NULL;
}
+
if ((arg = getarg(args, 'k')) != NULL) {
if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode))
errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir);
}
+
if ((arg = getarg(args, 's')) != NULL)
cnf->shell_default = arg->val;
warn("config update");
return EX_IOERR;
}
+
if (mode == M_PRINT && getarg(args, 'a')) {
int pretty = getarg(args, 'P') != NULL;
int v7 = getarg(args, '7') != NULL;
ENDPWENT();
return EXIT_SUCCESS;
}
+
if ((a_name = getarg(args, 'n')) != NULL)
pwd = GETPWNAM(pw_checkname((u_char *)a_name->val, 0));
a_uid = getarg(args, 'u');
a_name = NULL;
}
}
+
/*
* Update, delete & print require that the user exists
*/
- if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) {
+ if (mode == M_UPDATE || mode == M_DELETE ||
+ mode == M_PRINT || mode == M_LOCK || mode == M_UNLOCK) {
+
if (a_name == NULL && pwd == NULL) /* Try harder */
pwd = GETPWUID(atoi(a_uid->val));
errx(EX_NOUSER, "no such uid `%s'", a_uid->val);
errx(EX_NOUSER, "no such user `%s'", a_name->val);
}
+
if (a_name == NULL) /* May be needed later */
a_name = addarg(args, 'n', newstr(pwd->pw_name));
/*
- * Handle deletions now
+ * The M_LOCK and M_UNLOCK functions simply add or remove
+ * a "*LOCKED*" prefix from in front of the password to
+ * prevent it decoding correctly, and therefore prevents
+ * access. Of course, this only prevents access via
+ * password authentication (not ssh, kerberos or any
+ * other method that does not use the UNIX password) but
+ * that is a known limitation.
*/
- if (mode == M_DELETE) {
+
+ if (mode == M_LOCK) {
+ if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0)
+ errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name);
+ passtmp = malloc(strlen(pwd->pw_passwd) + sizeof(locked_str));
+ if (passtmp == NULL) /* disaster */
+ errx(EX_UNAVAILABLE, "out of memory");
+ strcpy(passtmp, locked_str);
+ strcat(passtmp, pwd->pw_passwd);
+ pwd->pw_passwd = passtmp;
+ edited = 1;
+ } else if (mode == M_UNLOCK) {
+ if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) != 0)
+ errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name);
+ pwd->pw_passwd += sizeof(locked_str)-1;
+ edited = 1;
+ } else if (mode == M_DELETE) {
+ /*
+ * Handle deletions now
+ */
char file[MAXPATHLEN];
char home[MAXPATHLEN];
uid_t uid = pwd->pw_uid;
strncpy(home, pwd->pw_dir, sizeof home);
home[sizeof home - 1] = '\0';
- if (!delpwent(pwd))
- err(EX_IOERR, "error updating passwd file");
+ rc = delpwent(pwd);
+ if (rc == -1)
+ err(EX_IOERR, "user '%s' does not exist", pwd->pw_name);
+ else if (rc != 0) {
+ warnc(rc, "passwd update");
+ return EX_IOERR;
+ }
+
+ if (cnf->nispasswd && *cnf->nispasswd=='/') {
+ rc = delnispwent(cnf->nispasswd, a_name->val);
+ if (rc == -1)
+ warnx("WARNING: user '%s' does not exist in NIS passwd", pwd->pw_name);
+ else if (rc != 0)
+ warnc(rc, "WARNING: NIS passwd update");
+ /* non-fatal */
+ }
- if (cnf->nispasswd && *cnf->nispasswd=='/' && !delnispwent(cnf->nispasswd, a_name->val))
- warn("WARNING: NIS passwd update");
-
editgroups(a_name->val, NULL);
pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid);
if (strcmp(pwd->pw_name, "root") == 0)
errx(EX_DATAERR, "can't rename `root' account");
pwd->pw_name = pw_checkname((u_char *)arg->val, 0);
+ edited = 1;
}
+
if ((arg = getarg(args, 'u')) != NULL && isdigit(*arg->val)) {
pwd->pw_uid = (uid_t) atol(arg->val);
+ edited = 1;
if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0)
errx(EX_DATAERR, "can't change uid of `root' account");
if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name);
}
- if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) /* Already checked this */
- pwd->pw_gid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid;
+
+ if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) { /* Already checked this */
+ gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid;
+ if (newgid != pwd->pw_gid) {
+ edited = 1;
+ pwd->pw_gid = pwd->pw_gid;
+ }
+ }
if ((arg = getarg(args, 'p')) != NULL) {
- if (*arg->val == '\0' || strcmp(arg->val, "0") == 0)
- pwd->pw_change = 0;
+ if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
+ if (pwd->pw_change != 0) {
+ pwd->pw_change = 0;
+ edited = 1;
+ }
+ }
else {
time_t now = time(NULL);
time_t expire = parse_date(now, arg->val);
if (now == expire)
errx(EX_DATAERR, "invalid password change date `%s'", arg->val);
- pwd->pw_change = expire;
+ if (pwd->pw_change != expire) {
+ pwd->pw_change = expire;
+ edited = 1;
+ }
}
}
+
if ((arg = getarg(args, 'e')) != NULL) {
- if (*arg->val == '\0' || strcmp(arg->val, "0") == 0)
- pwd->pw_expire = 0;
+ if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
+ if (pwd->pw_expire != 0) {
+ pwd->pw_expire = 0;
+ edited = 1;
+ }
+ }
else {
time_t now = time(NULL);
time_t expire = parse_date(now, arg->val);
if (now == expire)
errx(EX_DATAERR, "invalid account expiry date `%s'", arg->val);
- pwd->pw_expire = expire;
+ if (pwd->pw_expire != expire) {
+ pwd->pw_expire = expire;
+ edited = 1;
+ }
}
}
- if ((arg = getarg(args, 's')) != NULL)
- pwd->pw_shell = shell_path(cnf->shelldir, cnf->shells, arg->val);
- if (getarg(args, 'L'))
- pwd->pw_class = cnf->default_class;
+ if ((arg = getarg(args, 's')) != NULL) {
+ char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val);
+ if (shell == NULL)
+ shell = "";
+ if (strcmp(shell, pwd->pw_shell) != 0) {
+ pwd->pw_shell = shell;
+ edited = 1;
+ }
+ }
+
+ if (getarg(args, 'L')) {
+ if (cnf->default_class == NULL)
+ cnf->default_class = "";
+ if (strcmp(pwd->pw_class, cnf->default_class) != 0) {
+ pwd->pw_class = cnf->default_class;
+ edited = 1;
+ }
+ }
if ((arg = getarg(args, 'd')) != NULL) {
if (stat(pwd->pw_dir = arg->val, &st) == -1) {
warnx("WARNING: home `%s' is not a directory", pwd->pw_dir);
}
- if ((arg = getarg(args, 'w')) != NULL && getarg(args, 'h') == NULL)
+ if ((arg = getarg(args, 'w')) != NULL && getarg(args, 'h') == NULL) {
pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
+ edited = 1;
+ }
} else {
+
+ /*
+ * Add code
+ */
+
if (a_name == NULL) /* Required */
errx(EX_DATAERR, "login name required");
else if ((pwd = GETPWNAM(a_name->val)) != NULL) /* Exists */
pwd->pw_expire = pw_exppolicy(cnf, args);
pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name);
pwd->pw_shell = pw_shellpolicy(cnf, args, NULL);
+ edited = 1;
if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name);
/*
* Shared add/edit code
*/
- if ((arg = getarg(args, 'c')) != NULL)
- pwd->pw_gecos = pw_checkname((u_char *)arg->val, 1);
+ if ((arg = getarg(args, 'c')) != NULL) {
+ char *gecos = pw_checkname((u_char *)arg->val, 1);
+ if (strcmp(pwd->pw_gecos, gecos) != 0) {
+ pwd->pw_gecos = gecos;
+ edited = 1;
+ }
+ }
if ((arg = getarg(args, 'h')) != NULL) {
if (strcmp(arg->val, "-") == 0)
if (!*line)
errx(EX_DATAERR, "empty password read on file descriptor %d", fd);
pwd->pw_passwd = pw_pwcrypt(line);
+ edited = 1;
}
}
getarg(args, 'P') != NULL,
getarg(args, '7') != NULL);
- r = r1 = 1;
if (mode == M_ADD) {
- r = addpwent(pwd);
- if (r && cnf->nispasswd && *cnf->nispasswd=='/')
- r1 = addnispwent(cnf->nispasswd, pwd);
- } else if (mode == M_UPDATE) {
- r = chgpwent(a_name->val, pwd);
- if (r && cnf->nispasswd && *cnf->nispasswd=='/')
- r1 = chgnispwent(cnf->nispasswd, a_name->val, pwd);
- }
-
- if (!r) {
- warn("password update");
- return EX_IOERR;
- } else if (!r1) {
- warn("WARNING: NIS password update");
- /* Keep on trucking */
+ edited = 1; /* Always */
+ rc = addpwent(pwd);
+ if (rc == -1) {
+ warnx("user '%s' already exists", pwd->pw_name);
+ return EX_IOERR;
+ } else if (rc != 0) {
+ warnc(rc, "passwd file update");
+ return EX_IOERR;
+ }
+ if (cnf->nispasswd && *cnf->nispasswd=='/') {
+ rc = addnispwent(cnf->nispasswd, pwd);
+ if (rc == -1)
+ warnx("User '%s' already exists in NIS passwd", pwd->pw_name);
+ else
+ warnc(rc, "NIS passwd update");
+ /* NOTE: we treat NIS-only update errors as non-fatal */
+ }
+ } else if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) {
+ if (edited) { /* Only updated this if required */
+ rc = chgpwent(a_name->val, pwd);
+ if (rc == -1) {
+ warnx("user '%s' does not exist (NIS?)", pwd->pw_name);
+ return EX_IOERR;
+ } else if (rc != 0) {
+ warnc(rc, "passwd file update");
+ return EX_IOERR;
+ }
+ if ( cnf->nispasswd && *cnf->nispasswd=='/') {
+ rc = chgnispwent(cnf->nispasswd, a_name->val, pwd);
+ if (rc == -1)
+ warn("User '%s' not found in NIS passwd", pwd->pw_name);
+ else
+ warnc(rc, "NIS passwd update");
+ /* NOTE: NIS-only update errors are not fatal */
+ }
+ }
}
/*
}
}
}
+
/*
* Finally, let's create and populate the user's home directory. Note
* that this also `works' for editing users if -m is used, but
pw_log(cnf, mode, W_USER, "%s(%ld) home %s made",
pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir);
}
+
return EXIT_SUCCESS;
}