#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <limits.h>
#include <string.h>
#include <userdefs.h>
#include <user_attr.h>
#include <nss_dbdefs.h>
#include <errno.h>
#include <project.h>
#include "users.h"
#include "messages.h"
#include "funcs.h"
extern int **valid_lgroup(), isbusy(), get_default_zfs_flags();
extern int valid_uid(), check_perm(), create_home(), move_dir();
extern int valid_expire(), edit_group(), call_passmgmt();
extern projid_t **valid_lproject();
static uid_t uid;
static gid_t gid;
static char *new_logname = NULL;
static char *uidstr = NULL;
static char *group = NULL;
static char *grps = NULL;
static char *dir = NULL;
static char *shell = NULL;
static char *comment = NULL;
static char *logname = NULL;
static char *inactstr = NULL;
static char *expire = NULL;
static char *projects = NULL;
static char *usertype;
char *cmdname;
static char gidstring[32], uidstring[32];
char inactstring[10];
char *
strcpmalloc(str)
char *str;
{
if (str == NULL)
return (NULL);
return (strdup(str));
}
struct passwd *
passwd_cpmalloc(opw)
struct passwd *opw;
{
struct passwd *npw;
if (opw == NULL)
return (NULL);
npw = malloc(sizeof (struct passwd));
npw->pw_name = strcpmalloc(opw->pw_name);
npw->pw_passwd = strcpmalloc(opw->pw_passwd);
npw->pw_uid = opw->pw_uid;
npw->pw_gid = opw->pw_gid;
npw->pw_age = strcpmalloc(opw->pw_age);
npw->pw_comment = strcpmalloc(opw->pw_comment);
npw->pw_gecos = strcpmalloc(opw->pw_gecos);
npw->pw_dir = strcpmalloc(opw->pw_dir);
npw->pw_shell = strcpmalloc(opw->pw_shell);
return (npw);
}
int
main(argc, argv)
int argc;
char **argv;
{
int ch, ret = EX_SUCCESS, call_pass = 0, oflag = 0, zfs_flags = 0;
int tries, mflag = 0, inact, **gidlist, flag = 0, zflag = 0, Zflag = 0;
boolean_t fail_if_busy = B_FALSE;
char *ptr;
struct passwd *pstruct;
struct passwd *pw;
struct group *g_ptr;
struct stat statbuf;
#ifndef att
FILE *pwf;
#endif
int warning;
projid_t **projlist;
char **nargv;
int argindex;
userattr_t *ua;
char *val;
int isrole;
cmdname = argv[0];
if (geteuid() != 0) {
errmsg(M_PERM_DENIED);
exit(EX_NO_PERM);
}
opterr = 0;
usertype = getusertype(argv[0]);
while ((ch = getopt(argc, argv,
"c:d:e:f:G:g:l:mzZop:s:u:A:P:R:K:")) != EOF)
switch (ch) {
case 'c':
comment = optarg;
flag++;
break;
case 'd':
dir = optarg;
fail_if_busy = B_TRUE;
flag++;
break;
case 'e':
expire = optarg;
flag++;
break;
case 'f':
inactstr = optarg;
flag++;
break;
case 'G':
grps = optarg;
flag++;
break;
case 'g':
group = optarg;
fail_if_busy = B_TRUE;
flag++;
break;
case 'l':
new_logname = optarg;
fail_if_busy = B_TRUE;
flag++;
break;
case 'm':
mflag++;
flag++;
fail_if_busy = B_TRUE;
break;
case 'o':
oflag++;
flag++;
fail_if_busy = B_TRUE;
break;
case 'p':
projects = optarg;
flag++;
break;
case 's':
shell = optarg;
flag++;
break;
case 'u':
uidstr = optarg;
flag++;
fail_if_busy = B_TRUE;
break;
case 'Z':
Zflag++;
break;
case 'z':
zflag++;
break;
case 'A':
change_key(USERATTR_AUTHS_KW, optarg);
flag++;
break;
case 'P':
change_key(USERATTR_PROFILES_KW, optarg);
flag++;
break;
case 'R':
change_key(USERATTR_ROLES_KW, optarg);
flag++;
break;
case 'K':
change_key(NULL, optarg);
flag++;
break;
default:
case '?':
if (is_role(usertype))
errmsg(M_MRUSAGE);
else
errmsg(M_MUSAGE);
exit(EX_SYNTAX);
}
if (((!mflag) && (zflag || Zflag)) || (zflag && Zflag) ||
(mflag > 1 && (zflag || Zflag))) {
if (is_role(usertype))
errmsg(M_ARUSAGE);
else
errmsg(M_AUSAGE);
exit(EX_SYNTAX);
}
if (optind != argc - 1 || flag == 0) {
if (is_role(usertype))
errmsg(M_MRUSAGE);
else
errmsg(M_MUSAGE);
exit(EX_SYNTAX);
}
if ((!uidstr && oflag) || (mflag && !dir)) {
if (is_role(usertype))
errmsg(M_MRUSAGE);
else
errmsg(M_MUSAGE);
exit(EX_SYNTAX);
}
logname = argv[optind];
if ((ua = getusernam(logname)) == NULL ||
(val = kva_match(ua->attr, USERATTR_TYPE_KW)) == NULL ||
strcmp(val, USERATTR_TYPE_NONADMIN_KW) != 0)
isrole = 0;
else
isrole = 1;
if (isrole != is_role(usertype)) {
if (isrole)
errmsg(M_ISROLE);
else
errmsg(M_ISUSER);
exit(EX_SYNTAX);
}
usertype = getsetdefval(USERATTR_TYPE_KW, usertype);
if (is_role(usertype)) {
if (getsetdefval(USERATTR_ROLES_KW, NULL) != NULL) {
errmsg(M_MRUSAGE);
exit(EX_SYNTAX);
}
if (!isrole)
change_key(USERATTR_ROLES_KW, "");
}
#ifdef att
pw = getpwnam(logname);
#else
if ((pwf = fopen("/etc/passwd", "r")) == NULL) {
errmsg(M_OOPS, "open", "/etc/passwd");
exit(EX_FAILURE);
}
while ((pw = fgetpwent(pwf)) != NULL)
if (strcmp(pw->pw_name, logname) == 0)
break;
fclose(pwf);
#endif
if (pw == NULL) {
char pwdb[NSS_BUFLEN_PASSWD];
struct passwd pwd;
if (getpwnam_r(logname, &pwd, pwdb, sizeof (pwdb)) == NULL) {
errmsg(M_EXIST, logname);
exit(EX_NAME_NOT_EXIST);
} else {
errmsg(M_NONLOCAL, logname);
exit(EX_NOT_LOCAL);
}
}
pstruct = passwd_cpmalloc(pw);
if (isbusy(logname)) {
if (fail_if_busy) {
errmsg(M_BUSY, logname, "change");
exit(EX_BUSY);
}
warningmsg(WARN_LOGGED_IN, logname);
}
if (new_logname && strcmp(new_logname, logname)) {
switch (valid_login(new_logname, (struct passwd **)NULL,
&warning)) {
case INVALID:
errmsg(M_INVALID, new_logname, "login name");
exit(EX_BADARG);
case NOTUNIQUE:
errmsg(M_USED, new_logname);
exit(EX_NAME_EXISTS);
case LONGNAME:
errmsg(M_TOO_LONG, new_logname);
exit(EX_BADARG);
default:
call_pass = 1;
break;
}
if (warning)
warningmsg(warning, logname);
}
if (uidstr) {
errno = 0;
uid = (uid_t)strtol(uidstr, &ptr, (int)10);
if (*ptr || errno == ERANGE) {
errmsg(M_INVALID, uidstr, "user id");
exit(EX_BADARG);
}
if (uid != pstruct->pw_uid) {
switch (valid_uid(uid, NULL)) {
case NOTUNIQUE:
if (!oflag) {
errmsg(M_UID_USED, uid);
exit(EX_ID_EXISTS);
}
break;
case RESERVED:
errmsg(M_RESERVED, uid);
break;
case TOOBIG:
errmsg(M_TOOBIG, "uid", uid);
exit(EX_BADARG);
break;
}
call_pass = 1;
} else {
uidstr = NULL;
oflag = 0;
}
} else uid = pstruct->pw_uid;
if (group) {
switch (valid_group(group, &g_ptr, &warning)) {
case INVALID:
errmsg(M_INVALID, group, "group id");
exit(EX_BADARG);
case TOOBIG:
errmsg(M_TOOBIG, "gid", group);
exit(EX_BADARG);
case UNIQUE:
errmsg(M_GRP_NOTUSED, group);
exit(EX_NAME_NOT_EXIST);
case RESERVED:
gid = (gid_t)strtol(group, &ptr, (int)10);
errmsg(M_RESERVED_GID, gid);
break;
}
if (warning)
warningmsg(warning, group);
if (g_ptr != NULL)
gid = g_ptr->gr_gid;
else
gid = pstruct->pw_gid;
if (gid != pstruct->pw_gid)
call_pass = 1;
else group = NULL;
} else gid = pstruct->pw_gid;
if (grps && *grps) {
if (!(gidlist = valid_lgroup(grps, gid)))
exit(EX_BADARG);
} else
gidlist = (int **)0;
if (projects && *projects) {
if (! (projlist = valid_lproject(projects)))
exit(EX_BADARG);
} else
projlist = (projid_t **)0;
if (dir) {
if (REL_PATH(dir)) {
errmsg(M_RELPATH, dir);
exit(EX_BADARG);
}
if (strcmp(pstruct->pw_dir, dir) == 0) {
dir = NULL;
mflag = 0;
} else call_pass = 1;
}
if (mflag) {
if (stat(dir, &statbuf) == 0) {
if (check_perm(statbuf, pstruct->pw_uid,
pstruct->pw_gid, S_IWOTH|S_IXOTH) != 0) {
errmsg(M_NO_PERM, logname, dir);
exit(EX_NO_PERM);
}
} else {
zfs_flags = get_default_zfs_flags();
if (zflag || mflag > 1)
zfs_flags |= MANAGE_ZFS;
else if (Zflag)
zfs_flags &= ~MANAGE_ZFS;
ret = create_home(dir, NULL, uid, gid, zfs_flags);
}
if (ret == EX_SUCCESS)
ret = move_dir(pstruct->pw_dir, dir,
logname, zfs_flags);
if (ret != EX_SUCCESS)
exit(ret);
}
if (shell) {
if (REL_PATH(shell)) {
errmsg(M_RELPATH, shell);
exit(EX_BADARG);
}
if (strcmp(pstruct->pw_shell, shell) == 0) {
shell = NULL;
} else {
if (stat(shell, &statbuf) < 0 ||
(statbuf.st_mode & S_IFMT) != S_IFREG ||
(statbuf.st_mode & 0555) != 0555) {
errmsg(M_INVALID, shell, "shell");
exit(EX_BADARG);
}
call_pass = 1;
}
}
if (comment) {
if (strcmp(pstruct->pw_comment, comment))
call_pass = 1;
else
comment = NULL;
}
if (inactstr) {
inact = (int)strtol(inactstr, &ptr, 10);
if (*ptr || inact < 0) {
errmsg(M_INVALID, inactstr, "inactivity period");
exit(EX_BADARG);
}
call_pass = 1;
}
if (expire) {
if (*expire &&
valid_expire(expire, (time_t *)0) == INVALID) {
errmsg(M_INVALID, expire, "expiration date");
exit(EX_BADARG);
}
call_pass = 1;
}
if (nkeys > 0)
call_pass = 1;
if (grps) {
ret = edit_group(logname, new_logname, gidlist, 1);
if (ret != EX_SUCCESS) {
errmsg(M_UPDATE, "modified");
exit(ret);
}
}
if (projects) {
ret = edit_project(logname, (char *)NULL, projlist, 0);
if (ret != EX_SUCCESS) {
errmsg(M_UPDATE, "modified");
exit(ret);
}
}
if (!call_pass) exit(ret);
nargv = malloc((30 + nkeys * 2) * sizeof (char *));
argindex = 0;
nargv[argindex++] = PASSMGMT;
nargv[argindex++] = "-m";
if (comment) {
nargv[argindex++] = "-c";
nargv[argindex++] = comment;
}
if (dir) {
nargv[argindex++] = "-h";
nargv[argindex++] = dir;
}
if (group) {
nargv[argindex++] = "-g";
(void) sprintf(gidstring, "%u", gid);
nargv[argindex++] = gidstring;
}
if (shell) {
nargv[argindex++] = "-s";
nargv[argindex++] = shell;
}
if (inactstr) {
nargv[argindex++] = "-f";
nargv[argindex++] = inactstr;
}
if (expire) {
nargv[argindex++] = "-e";
nargv[argindex++] = expire;
}
if (uidstr) {
nargv[argindex++] = "-u";
(void) sprintf(uidstring, "%u", uid);
nargv[argindex++] = uidstring;
}
if (oflag) nargv[argindex++] = "-o";
if (new_logname) {
nargv[argindex++] = "-l";
nargv[argindex++] = new_logname;
}
if (nkeys > 0)
addkey_args(nargv, &argindex);
nargv[argindex++] = logname;
nargv[argindex++] = NULL;
ret = PEX_FAILED;
for (tries = 3; ret != PEX_SUCCESS && tries--; ) {
switch (ret = call_passmgmt(nargv)) {
case PEX_SUCCESS:
case PEX_BUSY:
break;
case PEX_HOSED_FILES:
errmsg(M_HOSED_FILES);
exit(EX_INCONSISTENT);
break;
case PEX_SYNTAX:
case PEX_BADARG:
if (is_role(usertype))
errmsg(M_MRUSAGE);
else
errmsg(M_MUSAGE);
exit(EX_SYNTAX);
break;
case PEX_BADUID:
errmsg(M_UID_USED, uid);
exit(EX_ID_EXISTS);
break;
case PEX_BADNAME:
errmsg(M_USED, logname);
exit(EX_NAME_EXISTS);
break;
default:
errmsg(M_UPDATE, "modified");
exit(ret);
break;
}
}
if (tries == 0) {
errmsg(M_UPDATE, "modified");
}
exit(ret);
}