* src/groupmems.c: Added functions add_user(), remove_user(), and

purge_members() to ease the support of gshadow.
This commit is contained in:
nekral-guest 2008-08-30 18:34:24 +00:00
parent cdf963b2b3
commit 72d75d50d9
2 changed files with 76 additions and 33 deletions

View File

@ -1,3 +1,8 @@
2008-08-27 Nicolas François <nicolas.francois@centraliens.net>
* src/groupmems.c: Added functions add_user(), remove_user(), and
purge_members() to ease the support of gshadow.
2008-08-27 Nicolas François <nicolas.francois@centraliens.net> 2008-08-27 Nicolas François <nicolas.francois@centraliens.net>
* src/expiry.c: Use Basename for Prog. * src/expiry.c: Use Basename for Prog.

View File

@ -71,6 +71,11 @@ static char *Prog;
static bool gr_locked = false; static bool gr_locked = false;
static char *whoami (void); static char *whoami (void);
static void add_user (const char *user,
struct group *grp);
static void remove_user (const char *user,
struct group *grp);
static void purge_members (struct group *grp);
static void display_members (char **members); static void display_members (char **members);
static void usage (void); static void usage (void);
static void process_flags (int argc, char **argv); static void process_flags (int argc, char **argv);
@ -94,6 +99,69 @@ static char *whoami (void)
} }
} }
/*
* add_user - Add an user to the specified group
*/
static void add_user (const char *user,
struct group *grp)
{
/* Make sure the user is not already part of the group */
if (is_on_list (grp->gr_mem, user)) {
fprintf (stderr,
_("%s: user '%s' is already a member of '%s'\n"),
Prog, user, grp->gr_name);
fail_exit (EXIT_MEMBER_EXISTS);
}
/* Add the user to the /etc/group group */
grp->gr_mem = add_list (grp->gr_mem, user);
if (gr_update (grp) == 0) {
fprintf (stderr,
_("%s: failed to prepare the new %s entry '%s'\n"),
Prog, gr_dbname (), grp->gr_name);
fail_exit (13);
}
}
/*
* remove_user - Remove an user from a given group
*/
static void remove_user (const char *user,
struct group *grp)
{
/* Check if the user is a member of the specified group */
if (!is_on_list (grp->gr_mem, user)) {
fprintf (stderr,
_("%s: user '%s' is not a member of '%s'\n"),
Prog, user, grp->gr_name);
fail_exit (EXIT_NOT_MEMBER);
}
/* Remove the user from the /etc/group group */
grp->gr_mem = del_list (grp->gr_mem, user);
if (gr_update (grp) == 0) {
fprintf (stderr,
_("%s: failed to prepare the new %s entry '%s'\n"),
Prog, gr_dbname (), grp->gr_name);
fail_exit (13);
}
}
/*
* purge_members - Rmeove every members of the specified group
*/
static void purge_members (struct group *grp)
{
/* Remove all the members of the /etc/group group */
grp->gr_mem[0] = NULL;
if (gr_update (grp) == 0) {
fprintf (stderr,
_("%s: failed to prepare the new %s entry '%s'\n"),
Prog, gr_dbname (), grp->gr_name);
fail_exit (13);
}
}
static void display_members (char **members) static void display_members (char **members)
{ {
int i; int i;
@ -277,41 +345,11 @@ int main (int argc, char **argv)
if (list) { if (list) {
display_members (grp->gr_mem); display_members (grp->gr_mem);
} else if (NULL != adduser) { } else if (NULL != adduser) {
if (is_on_list (grp->gr_mem, adduser)) { add_user (adduser, grp);
fprintf (stderr,
_("%s: user '%s' is already a member of '%s'\n"),
Prog, adduser, grp->gr_name);
fail_exit (EXIT_MEMBER_EXISTS);
}
grp->gr_mem = add_list (grp->gr_mem, adduser);
if (gr_update (grp) == 0) {
fprintf (stderr,
_("%s: failed to prepare the new %s entry '%s'\n"),
Prog, gr_dbname (), grp->gr_name);
fail_exit (13);
}
} else if (NULL != deluser) { } else if (NULL != deluser) {
if (!is_on_list (grp->gr_mem, deluser)) { remove_user (deluser, grp);
fprintf (stderr,
_("%s: user '%s' is not a member of '%s'\n"),
Prog, deluser, grp->gr_name);
fail_exit (EXIT_NOT_MEMBER);
}
grp->gr_mem = del_list (grp->gr_mem, deluser);
if (gr_update (grp) == 0) {
fprintf (stderr,
_("%s: failed to prepare the new %s entry '%s'\n"),
Prog, gr_dbname (), grp->gr_name);
fail_exit (13);
}
} else if (purge) { } else if (purge) {
grp->gr_mem[0] = NULL; purge_members (grp);
if (gr_update (grp) == 0) {
fprintf (stderr,
_("%s: failed to prepare the new %s entry '%s'\n"),
Prog, gr_dbname (), grp->gr_name);
fail_exit (13);
}
} }
if (gr_close () == 0) { if (gr_close () == 0) {