Merge pull request #82 from t8m/ingroup
newgrp: avoid unnecessary group lookups
This commit is contained in:
commit
0c2939b331
58
src/newgrp.c
58
src/newgrp.c
@ -83,16 +83,30 @@ static void usage (void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
static bool ingroup(const char *name, struct group *gr)
|
||||||
* find_matching_group - search all groups of a given group id for
|
|
||||||
* membership of a given username
|
|
||||||
*/
|
|
||||||
static /*@null@*/struct group *find_matching_group (const char *name, gid_t gid)
|
|
||||||
{
|
{
|
||||||
struct group *gr;
|
|
||||||
char **look;
|
char **look;
|
||||||
bool notfound = true;
|
bool notfound = true;
|
||||||
|
|
||||||
|
look = gr->gr_mem;
|
||||||
|
while (*look && notfound)
|
||||||
|
notfound = strcmp (*look++, name);
|
||||||
|
|
||||||
|
return !notfound;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* find_matching_group - search all groups of a gr's group id for
|
||||||
|
* membership of a given username
|
||||||
|
* but check gr itself first
|
||||||
|
*/
|
||||||
|
static /*@null@*/struct group *find_matching_group (const char *name, struct group *gr)
|
||||||
|
{
|
||||||
|
gid_t gid = gr->gr_gid;
|
||||||
|
|
||||||
|
if (ingroup(name, gr))
|
||||||
|
return gr;
|
||||||
|
|
||||||
setgrent ();
|
setgrent ();
|
||||||
while ((gr = getgrent ()) != NULL) {
|
while ((gr = getgrent ()) != NULL) {
|
||||||
if (gr->gr_gid != gid) {
|
if (gr->gr_gid != gid) {
|
||||||
@ -103,14 +117,8 @@ static /*@null@*/struct group *find_matching_group (const char *name, gid_t gid)
|
|||||||
* A group with matching GID was found.
|
* A group with matching GID was found.
|
||||||
* Test for membership of 'name'.
|
* Test for membership of 'name'.
|
||||||
*/
|
*/
|
||||||
look = gr->gr_mem;
|
if (ingroup(name, gr))
|
||||||
while ((NULL != *look) && notfound) {
|
|
||||||
notfound = (strcmp (*look, name) != 0);
|
|
||||||
look++;
|
|
||||||
}
|
|
||||||
if (!notfound) {
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
endgrent ();
|
endgrent ();
|
||||||
return gr;
|
return gr;
|
||||||
@ -643,17 +651,19 @@ int main (int argc, char **argv)
|
|||||||
* groups of the same GID like the requested group for
|
* groups of the same GID like the requested group for
|
||||||
* membership of the current user.
|
* membership of the current user.
|
||||||
*/
|
*/
|
||||||
grp = find_matching_group (name, grp->gr_gid);
|
if (!is_member) {
|
||||||
if (NULL == grp) {
|
grp = find_matching_group (name, grp);
|
||||||
/*
|
if (NULL == grp) {
|
||||||
* No matching group found. As we already know that
|
/*
|
||||||
* the group exists, this happens only in the case
|
* No matching group found. As we already know that
|
||||||
* of a requested group where the user is not member.
|
* the group exists, this happens only in the case
|
||||||
*
|
* of a requested group where the user is not member.
|
||||||
* Re-read the group entry for further processing.
|
*
|
||||||
*/
|
* Re-read the group entry for further processing.
|
||||||
grp = xgetgrnam (group);
|
*/
|
||||||
assert (NULL != grp);
|
grp = xgetgrnam (group);
|
||||||
|
assert (NULL != grp);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#ifdef SHADOWGRP
|
#ifdef SHADOWGRP
|
||||||
sgrp = getsgnam (group);
|
sgrp = getsgnam (group);
|
||||||
|
Loading…
Reference in New Issue
Block a user