Updating usr.sbin/user

Change-Id: I5968d25f0a0efd590e6d664d3a2d182a87755e3d
This commit is contained in:
Lionel Sambuc 2013-09-20 19:47:48 +02:00
parent 55d9e9954b
commit 3eaea2dc69
4 changed files with 78 additions and 36 deletions

View file

@ -275,7 +275,7 @@
2011/01/04 10:01:51,usr.sbin/pwd_mkdb
2013/04/05 12:00:00,usr.sbin/rdate
2012/10/17 12:00:00,usr.sbin/unlink
2011/01/04 10:30:21,usr.sbin/user
2012/10/17 12:00:00,usr.sbin/user
2009/04/19 00:44:49,usr.sbin/vipw
2013/07/31 12:00:00,usr.sbin/vnconfig
2009/04/22 15:23:10,usr.sbin/zic

View file

@ -1,4 +1,4 @@
/* $NetBSD: defs.h,v 1.6 2005/11/25 08:00:18 agc Exp $ */
/* $NetBSD: defs.h,v 1.7 2011/08/31 16:25:00 plunky Exp $ */
/*
* Copyright (c) 1999 Alistair G. Crooks. All rights reserved.
@ -31,14 +31,14 @@
#define DEFS_H_
#define NEWARRAY(type,ptr,size,action) do { \
if ((ptr = (type *) calloc(sizeof(type), size)) == (type *) NULL) { \
if ((ptr = (type *) calloc(sizeof(type), size)) == NULL) { \
warn("can't allocate %ld bytes", (long)(size * sizeof(type))); \
action; \
} \
} while( /* CONSTCOND */ 0)
#define RENEW(type,ptr,size,action) do { \
if ((ptr = (type *) realloc(ptr, sizeof(type) * size)) == (type *) NULL) { \
if ((ptr = (type *) realloc(ptr, sizeof(type) * size)) == NULL) { \
warn("can't realloc %ld bytes", (long)(size * sizeof(type))); \
action; \
} \

44
usr.sbin/user/pathnames.h Normal file
View file

@ -0,0 +1,44 @@
/* $NetBSD: pathnames.h,v 1.1 2011/12/01 00:34:05 dholland Exp $ */
/*
* Copyright (c) 1999 Alistair G. Crooks. All rights reserved.
* Copyright (c) 2005 Liam J. Foy. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <paths.h>
/* Full paths of programs used here */
#define _PATH_CHMOD "/bin/chmod"
#define _PATH_CHOWN "/usr/sbin/chown"
#define _PATH_LOGINCONF "/etc/login.conf"
#define _PATH_MKDIR "/bin/mkdir"
#define _PATH_MV "/bin/mv"
/* note that there's a _PATH_NOLOGIN in <paths.h> that's for /etc/nologin */
#define _PATH_SBIN_NOLOGIN "/sbin/nologin"
#define _PATH_PAX "/bin/pax"
#define _PATH_RM "/bin/rm"

View file

@ -1,4 +1,4 @@
/* $NetBSD: user.c,v 1.126 2011/01/04 10:30:21 wiz Exp $ */
/* $NetBSD: user.c,v 1.129 2011/12/01 00:34:05 dholland Exp $ */
/*
* Copyright (c) 1999 Alistair G. Crooks. All rights reserved.
@ -33,12 +33,13 @@
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1999\
The NetBSD Foundation, Inc. All rights reserved.");
__RCSID("$NetBSD: user.c,v 1.126 2011/01/04 10:30:21 wiz Exp $");
__RCSID("$NetBSD: user.c,v 1.129 2011/12/01 00:34:05 dholland Exp $");
#endif
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <ctype.h>
#include <dirent.h>
@ -48,7 +49,6 @@ __RCSID("$NetBSD: user.c,v 1.126 2011/01/04 10:30:21 wiz Exp $");
#ifdef EXTENSIONS
#include <login_cap.h>
#endif
#include <paths.h>
#include <pwd.h>
#include <regex.h>
#include <stdarg.h>
@ -61,6 +61,7 @@ __RCSID("$NetBSD: user.c,v 1.126 2011/01/04 10:30:21 wiz Exp $");
#include <util.h>
#include <errno.h>
#include "pathnames.h"
#include "defs.h"
#include "usermgmt.h"
@ -141,7 +142,6 @@ enum {
#define LOCK 1
#define LOCKED "*LOCKED*"
#define PATH_LOGINCONF "/etc/login.conf"
#ifndef DEF_GROUP
#define DEF_GROUP "users"
@ -211,20 +211,11 @@ enum {
DES_Len = 13,
};
/* Full paths of programs used here */
#define CHMOD "/bin/chmod"
#define CHOWN "/usr/bin/chown"
#define MKDIR "/bin/mkdir"
#define MV "/bin/mv"
#define NOLOGIN "/sbin/nologin"
#define PAX "/bin/pax"
#define RM "/bin/rm"
#define UNSET_INACTIVE "Null (unset)"
#define UNSET_EXPIRY "Null (unset)"
static int asystem(const char *fmt, ...)
__attribute__((__format__(__printf__, 1, 2)));
static int asystem(const char *fmt, ...) __printflike(1, 2);
static int is_number(const char *);
static struct group *find_group_info(const char *);
static int verbose;
@ -270,8 +261,13 @@ asystem(const char *fmt, ...)
if (verbose) {
(void)printf("Command: %s\n", buf);
}
if ((ret = system(buf)) != 0) {
ret = system(buf);
if (ret == -1) {
warn("Error running `%s'", buf);
} else if (WIFSIGNALED(ret)) {
warnx("Error running `%s': Signal %d", buf, WTERMSIG(ret));
} else if (WIFEXITED(ret) && WEXITSTATUS(ret) != 0) {
warnx("Error running `%s': Exit %d", buf, WEXITSTATUS(ret));
}
return ret;
}
@ -307,7 +303,8 @@ removehomedir(struct passwd *pwp)
(void)seteuid(pwp->pw_uid);
/* we add the "|| true" to keep asystem() quiet if there is a non-zero exit status. */
(void)asystem("%s -rf %s > /dev/null 2>&1 || true", RM, pwp->pw_dir);
(void)asystem("%s -rf %s > /dev/null 2>&1 || true", _PATH_RM,
pwp->pw_dir);
(void)seteuid(0);
if (rmdir(pwp->pw_dir) < 0) {
warn("Unable to remove all files in `%s'", pwp->pw_dir);
@ -364,12 +361,12 @@ copydotfiles(char *skeldir, int uid, int gid, char *dir, mode_t homeperm)
warnx("No \"dot\" initialisation files found");
} else {
(void)asystem("cd %s && %s -rw -pe %s . %s",
skeldir, PAX, (verbose) ? "-v" : "", dir);
skeldir, _PATH_PAX, (verbose) ? "-v" : "", dir);
}
(void)asystem("%s -R -h %d:%d %s", CHOWN, uid, gid, dir);
(void)asystem("%s -R u+w %s", CHMOD, dir);
(void)asystem("%s -R -h %d:%d %s", _PATH_CHOWN, uid, gid, dir);
(void)asystem("%s -R u+w %s", _PATH_CHMOD, dir);
#ifdef EXTENSIONS
(void)asystem("%s 0%o %s", CHMOD, homeperm, dir);
(void)asystem("%s 0%o %s", _PATH_CHMOD, homeperm, dir);
#endif
return n;
}
@ -390,7 +387,7 @@ creategid(char *group, int gid, const char *name)
warnx("Can't create group `%s': already exists", group);
return 0;
}
if ((from = fopen(_PATH_GROUP, "r+")) == NULL) {
if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
warn("Can't create group `%s': can't open `%s'", name,
_PATH_GROUP);
return 0;
@ -454,7 +451,7 @@ modify_gid(char *group, char *newent)
int fd;
int cc;
if ((from = fopen(_PATH_GROUP, "r+")) == NULL) {
if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
warn("Can't modify group `%s': can't open `%s'",
group, _PATH_GROUP);
return 0;
@ -577,7 +574,7 @@ append_group(char *user, int ngroups, const char **groups)
}
}
}
if ((from = fopen(_PATH_GROUP, "r+")) == NULL) {
if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
warn("Can't append group(s) for `%s': can't open `%s'",
user, _PATH_GROUP);
return 0;
@ -993,9 +990,9 @@ valid_class(char *class)
* user the actual login class does not exist.
*/
if (access(PATH_LOGINCONF, R_OK) == -1) {
if (access(_PATH_LOGINCONF, R_OK) == -1) {
warn("Access failed for `%s'; will not validate class `%s'",
PATH_LOGINCONF, class);
_PATH_LOGINCONF, class);
return 1;
}
@ -1020,7 +1017,7 @@ valid_shell(const char *shellname)
}
/* if nologin is used as a shell, consider it a valid shell */
if (strcmp(shellname, NOLOGIN) == 0)
if (strcmp(shellname, _PATH_SBIN_NOLOGIN) == 0)
return 1;
while ((shellp = getusershell()) != NULL)
@ -1092,7 +1089,7 @@ adduser(char *login_name, user_t *up)
login_name, up->u_class);
}
#endif
if ((masterfd = open(_PATH_MASTERPASSWD, O_RDWR)) < 0) {
if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) {
err(EXIT_FAILURE, "Can't add user `%s': can't open `%s'",
login_name, _PATH_MASTERPASSWD);
}
@ -1261,7 +1258,7 @@ adduser(char *login_name, user_t *up)
"Can't add user `%s': home directory `%s' "
"already exists", login_name, home);
} else {
if (asystem("%s -p %s", MKDIR, home) != 0) {
if (asystem("%s -p %s", _PATH_MKDIR, home) != 0) {
(void)close(ptmpfd);
(void)pw_abort();
errx(EXIT_FAILURE, "Can't add user `%s': "
@ -1325,7 +1322,7 @@ rm_user_from_groups(char *login_name)
buf);
return 0;
}
if ((from = fopen(_PATH_GROUP, "r+")) == NULL) {
if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
warn("Can't remove user `%s' from `%s': can't open `%s'",
login_name, _PATH_GROUP, _PATH_GROUP);
return 0;
@ -1454,7 +1451,7 @@ moduser(char *login_name, char *newlogin, user_t *up, int allow_samba)
/* keep dir name in case we need it for '-m' */
homedir = pwp->pw_dir;
if ((masterfd = open(_PATH_MASTERPASSWD, O_RDWR)) < 0) {
if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) {
err(EXIT_FAILURE, "Can't modify user `%s': can't open `%s'",
login_name, _PATH_MASTERPASSWD);
}
@ -1703,7 +1700,7 @@ moduser(char *login_name, char *newlogin, user_t *up, int allow_samba)
}
if (up != NULL) {
if ((up->u_flags & F_MKDIR) &&
asystem("%s %s %s", MV, homedir, pwp->pw_dir) != 0) {
asystem("%s %s %s", _PATH_MV, homedir, pwp->pw_dir) != 0) {
(void)close(ptmpfd);
(void)pw_abort();
errx(EXIT_FAILURE, "Can't modify user `%s': "
@ -2209,7 +2206,8 @@ userdel(int argc, char **argv)
}
if (up->u_preserve) {
up->u_flags |= F_SHELL;
memsave(&up->u_shell, NOLOGIN, strlen(NOLOGIN));
memsave(&up->u_shell, _PATH_SBIN_NOLOGIN,
strlen(_PATH_SBIN_NOLOGIN));
(void)memset(password, '*', DES_Len);
password[DES_Len] = 0;
memsave(&up->u_password, password, strlen(password));