Make separate functions to show users and groups
This commit is contained in:
parent
20b6ee617f
commit
ae73dd9f37
@ -234,6 +234,9 @@ main(int argc, char *argv[])
|
|||||||
conf.config = optarg;
|
conf.config = optarg;
|
||||||
config = conf.config;
|
config = conf.config;
|
||||||
break;
|
break;
|
||||||
|
case 'F':
|
||||||
|
conf.force = true;
|
||||||
|
break;
|
||||||
case 'N':
|
case 'N':
|
||||||
conf.dryrun = true;
|
conf.dryrun = true;
|
||||||
break;
|
break;
|
||||||
@ -248,6 +251,9 @@ main(int argc, char *argv[])
|
|||||||
case 'Y':
|
case 'Y':
|
||||||
nis = true;
|
nis = true;
|
||||||
break;
|
break;
|
||||||
|
case 'a':
|
||||||
|
conf.all = true;
|
||||||
|
break;
|
||||||
case 'g':
|
case 'g':
|
||||||
if (which == 0) { /* for user* */
|
if (which == 0) { /* for user* */
|
||||||
addarg(&arglist, 'g', optarg);
|
addarg(&arglist, 'g', optarg);
|
||||||
|
@ -103,6 +103,37 @@ pw_groupnext(struct userconf *cnf, bool quiet)
|
|||||||
return (EXIT_SUCCESS);
|
return (EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
pw_groupshow(const char *name, long id, struct group *fakegroup)
|
||||||
|
{
|
||||||
|
struct group *grp = NULL;
|
||||||
|
|
||||||
|
if (id < 0 && name == NULL && !conf.all)
|
||||||
|
errx(EX_DATAERR, "groupname or id or '-a' required");
|
||||||
|
|
||||||
|
if (conf.all) {
|
||||||
|
SETGRENT();
|
||||||
|
while ((grp = GETGRENT()) != NULL)
|
||||||
|
print_group(grp);
|
||||||
|
ENDGRENT();
|
||||||
|
|
||||||
|
return (EXIT_SUCCESS);
|
||||||
|
}
|
||||||
|
|
||||||
|
grp = (name != NULL) ? GETGRNAM(name) : GETGRGID(id);
|
||||||
|
if (grp == NULL) {
|
||||||
|
if (conf.force) {
|
||||||
|
grp = fakegroup;
|
||||||
|
} else {
|
||||||
|
if (name == NULL)
|
||||||
|
errx(EX_DATAERR, "unknown gid `%ld'", id);
|
||||||
|
errx(EX_DATAERR, "unknown group `%s'", name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (print_group(grp));
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
pw_group(int mode, char *name, long id, struct cargs * args)
|
pw_group(int mode, char *name, long id, struct cargs * args)
|
||||||
{
|
{
|
||||||
@ -124,34 +155,22 @@ pw_group(int mode, char *name, long id, struct cargs * args)
|
|||||||
if (mode == M_NEXT)
|
if (mode == M_NEXT)
|
||||||
return (pw_groupnext(cnf, conf.quiet));
|
return (pw_groupnext(cnf, conf.quiet));
|
||||||
|
|
||||||
|
if (mode == M_PRINT)
|
||||||
|
return (pw_groupshow(name, id, &fakegroup));
|
||||||
|
|
||||||
if (mode == M_LOCK || mode == M_UNLOCK)
|
if (mode == M_LOCK || mode == M_UNLOCK)
|
||||||
errx(EX_USAGE, "'lock' command is not available for groups");
|
errx(EX_USAGE, "'lock' command is not available for groups");
|
||||||
|
|
||||||
if (mode == M_PRINT && getarg(args, 'a')) {
|
|
||||||
SETGRENT();
|
|
||||||
while ((grp = GETGRENT()) != NULL)
|
|
||||||
print_group(grp);
|
|
||||||
ENDGRENT();
|
|
||||||
return EXIT_SUCCESS;
|
|
||||||
}
|
|
||||||
if (id < 0 && name == NULL)
|
if (id < 0 && name == NULL)
|
||||||
errx(EX_DATAERR, "group name or id required");
|
errx(EX_DATAERR, "group name or id required");
|
||||||
|
|
||||||
grp = (name != NULL) ? GETGRNAM(name) : GETGRGID(id);
|
grp = (name != NULL) ? GETGRNAM(name) : GETGRGID(id);
|
||||||
|
|
||||||
if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) {
|
if (mode == M_UPDATE || mode == M_DELETE) {
|
||||||
if (name == NULL && grp == NULL) /* Try harder */
|
if (name == NULL && grp == NULL) /* Try harder */
|
||||||
grp = GETGRGID(id);
|
grp = GETGRGID(id);
|
||||||
|
|
||||||
if (grp == NULL) {
|
if (grp == NULL) {
|
||||||
if (mode == M_PRINT && getarg(args, 'F')) {
|
|
||||||
char *fmems[1];
|
|
||||||
fmems[0] = NULL;
|
|
||||||
fakegroup.gr_name = name ? name : "nogroup";
|
|
||||||
fakegroup.gr_gid = (gid_t) id;
|
|
||||||
fakegroup.gr_mem = fmems;
|
|
||||||
return print_group(&fakegroup);
|
|
||||||
}
|
|
||||||
if (name == NULL)
|
if (name == NULL)
|
||||||
errx(EX_DATAERR, "unknown group `%s'", name);
|
errx(EX_DATAERR, "unknown group `%s'", name);
|
||||||
else
|
else
|
||||||
|
@ -162,6 +162,36 @@ pw_usernext(struct userconf *cnf, bool quiet)
|
|||||||
return (EXIT_SUCCESS);
|
return (EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
pw_usershow(char *name, long id, struct passwd *fakeuser)
|
||||||
|
{
|
||||||
|
struct passwd *pwd = NULL;
|
||||||
|
|
||||||
|
if (id < 0 && name == NULL && !conf.all)
|
||||||
|
errx(EX_DATAERR, "username or id or '-a' required");
|
||||||
|
|
||||||
|
if (conf.all) {
|
||||||
|
SETPWENT();
|
||||||
|
while ((pwd = GETPWENT()) != NULL)
|
||||||
|
print_user(pwd);
|
||||||
|
ENDPWENT();
|
||||||
|
return (EXIT_SUCCESS);
|
||||||
|
}
|
||||||
|
|
||||||
|
pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id);
|
||||||
|
if (pwd == NULL) {
|
||||||
|
if (conf.force) {
|
||||||
|
pwd = fakeuser;
|
||||||
|
} else {
|
||||||
|
if (name == NULL)
|
||||||
|
errx(EX_NOUSER, "no such uid `%ld'", id);
|
||||||
|
errx(EX_NOUSER, "no such user `%s'", name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (print_user(pwd));
|
||||||
|
}
|
||||||
|
|
||||||
/*-
|
/*-
|
||||||
* -C config configuration file
|
* -C config configuration file
|
||||||
* -q quiet operation
|
* -q quiet operation
|
||||||
@ -213,7 +243,7 @@ pw_user(int mode, char *name, long id, struct cargs * args)
|
|||||||
|
|
||||||
static struct passwd fakeuser =
|
static struct passwd fakeuser =
|
||||||
{
|
{
|
||||||
NULL,
|
"nouser",
|
||||||
"*",
|
"*",
|
||||||
-1,
|
-1,
|
||||||
-1,
|
-1,
|
||||||
@ -233,6 +263,9 @@ pw_user(int mode, char *name, long id, struct cargs * args)
|
|||||||
if (mode == M_NEXT)
|
if (mode == M_NEXT)
|
||||||
return (pw_usernext(cnf, conf.quiet));
|
return (pw_usernext(cnf, conf.quiet));
|
||||||
|
|
||||||
|
if (mode == M_PRINT)
|
||||||
|
return (pw_usershow(name, id, &fakeuser));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* We can do all of the common legwork here
|
* We can do all of the common legwork here
|
||||||
*/
|
*/
|
||||||
@ -377,14 +410,6 @@ pw_user(int mode, char *name, long id, struct cargs * args)
|
|||||||
err(EX_IOERR, "config udpate");
|
err(EX_IOERR, "config udpate");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mode == M_PRINT && getarg(args, 'a')) {
|
|
||||||
SETPWENT();
|
|
||||||
while ((pwd = GETPWENT()) != NULL)
|
|
||||||
print_user(pwd);
|
|
||||||
ENDPWENT();
|
|
||||||
return EXIT_SUCCESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (name != NULL)
|
if (name != NULL)
|
||||||
pwd = GETPWNAM(pw_checkname(name, 0));
|
pwd = GETPWNAM(pw_checkname(name, 0));
|
||||||
|
|
||||||
@ -395,17 +420,12 @@ pw_user(int mode, char *name, long id, struct cargs * args)
|
|||||||
* Update, delete & print require that the user exists
|
* Update, delete & print require that the user exists
|
||||||
*/
|
*/
|
||||||
if (mode == M_UPDATE || mode == M_DELETE ||
|
if (mode == M_UPDATE || mode == M_DELETE ||
|
||||||
mode == M_PRINT || mode == M_LOCK || mode == M_UNLOCK) {
|
mode == M_LOCK || mode == M_UNLOCK) {
|
||||||
|
|
||||||
if (name == NULL && pwd == NULL) /* Try harder */
|
if (name == NULL && pwd == NULL) /* Try harder */
|
||||||
pwd = GETPWUID(id);
|
pwd = GETPWUID(id);
|
||||||
|
|
||||||
if (pwd == NULL) {
|
if (pwd == NULL) {
|
||||||
if (mode == M_PRINT && getarg(args, 'F')) {
|
|
||||||
fakeuser.pw_name = name ? name : "nouser";
|
|
||||||
fakeuser.pw_uid = (uid_t) id;
|
|
||||||
return print_user(&fakeuser);
|
|
||||||
}
|
|
||||||
if (name == NULL)
|
if (name == NULL)
|
||||||
errx(EX_NOUSER, "no such uid `%ld'", id);
|
errx(EX_NOUSER, "no such uid `%ld'", id);
|
||||||
errx(EX_NOUSER, "no such user `%s'", name);
|
errx(EX_NOUSER, "no such user `%s'", name);
|
||||||
@ -440,8 +460,6 @@ pw_user(int mode, char *name, long id, struct cargs * args)
|
|||||||
} else if (mode == M_DELETE)
|
} else if (mode == M_DELETE)
|
||||||
return (delete_user(cnf, pwd, name,
|
return (delete_user(cnf, pwd, name,
|
||||||
getarg(args, 'r') != NULL, mode));
|
getarg(args, 'r') != NULL, mode));
|
||||||
else if (mode == M_PRINT)
|
|
||||||
return print_user(pwd);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The rest is edit code
|
* The rest is edit code
|
||||||
|
@ -88,6 +88,8 @@ struct pwconf {
|
|||||||
int fd;
|
int fd;
|
||||||
int which;
|
int which;
|
||||||
bool quiet;
|
bool quiet;
|
||||||
|
bool force;
|
||||||
|
bool all;
|
||||||
bool dryrun;
|
bool dryrun;
|
||||||
bool pretty;
|
bool pretty;
|
||||||
bool v7;
|
bool v7;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user