summaryrefslogtreecommitdiff
path: root/source3/lib
diff options
context:
space:
mode:
authorLuke Leighton <lkcl@samba.org>1999-02-03 00:49:24 +0000
committerLuke Leighton <lkcl@samba.org>1999-02-03 00:49:24 +0000
commit1153f00f889e5bb310e895d319eed75bc93deef4 (patch)
tree992b761fc48443b56324b1403708d4d0ff22c813 /source3/lib
parentfe609d810e145d5491968fee5d691d6eee41e152 (diff)
downloadsamba-1153f00f889e5bb310e895d319eed75bc93deef4.tar.gz
samba-1153f00f889e5bb310e895d319eed75bc93deef4.tar.bz2
samba-1153f00f889e5bb310e895d319eed75bc93deef4.zip
cache unix groups so that two-level getgrent calls don't occur.
(This used to be commit f7dfa55a2e191ae780d399026bce48f68cda4bf0)
Diffstat (limited to 'source3/lib')
-rw-r--r--source3/lib/replace.c1
-rw-r--r--source3/lib/util.c71
2 files changed, 72 insertions, 0 deletions
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
********************************************************************/