From 1153f00f889e5bb310e895d319eed75bc93deef4 Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Wed, 3 Feb 1999 00:49:24 +0000 Subject: cache unix groups so that two-level getgrent calls don't occur. (This used to be commit f7dfa55a2e191ae780d399026bce48f68cda4bf0) --- source3/lib/replace.c | 1 + source3/lib/util.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) (limited to 'source3/lib') diff --git a/source3/lib/replace.c b/source3/lib/replace.c index a768e9ce47..c6a4259417 100644 --- a/source3/lib/replace.c +++ b/source3/lib/replace.c @@ -168,6 +168,7 @@ Corrections by richard.kettlewell@kewill.com struct group *g; char *gr; + setgrent(); grouplst[0] = id; i = 1; while (i < NGROUPS_MAX && diff --git a/source3/lib/util.c b/source3/lib/util.c index 066984962b..127f69dc7d 100644 --- a/source3/lib/util.c +++ b/source3/lib/util.c @@ -2384,6 +2384,77 @@ int get_unixgroups(char *user, uid_t uid, gid_t gid, int *p_ngroups, gid_t **p_g return 0; } +/**************************************************************************** +get all unix groups. copying group members is hideous on memory, so it's +NOT done here. however, names of unix groups _are_ string-allocated so +free_unix_grps() must be called. +****************************************************************************/ +BOOL get_unix_grps(int *p_ngroups, struct group **p_groups) +{ + struct group *grp; + + DEBUG(10,("get_unix_grps\n")); + + if (p_ngroups == NULL || *p_groups == NULL) + { + return False; + } + + (*p_ngroups) = 0; + (*p_groups) = NULL; + + setgrent(); + + while ((grp = getgrent()) != NULL) + { + struct group *copy_grp; + + (*p_groups) = (struct group*)Realloc((*p_groups), (size_t)((*p_ngroups)+1) * sizeof(struct group)); + if ((*p_groups) == NULL) + { + (*p_ngroups) = 0; + endgrent(); + + return False; + } + + copy_grp = &(*p_groups)[*p_ngroups]; + memcpy(copy_grp, grp, sizeof(*grp)); + copy_grp->gr_name = strdup(copy_grp->gr_name); + copy_grp->gr_mem = NULL; + + (*p_ngroups)++; + } + + endgrent(); + + DEBUG(10,("get_unix_grps: %d groups\n", (*p_ngroups))); + return True; +} + +/**************************************************************************** +free memory associated with unix groups. +****************************************************************************/ +void free_unix_grps(int ngroups, struct group *p_groups) +{ + int i; + + if (p_groups == NULL) + { + return; + } + + for (i = 0; i < ngroups; i++) + { + if (p_groups[i].gr_name != NULL) + { + free(p_groups[i].gr_name); + } + } + + free(p_groups); +} + /******************************************************************* turn a uid into a user name ********************************************************************/ -- cgit