Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Implement getgroupmembership(3). This is similar to getgrou...



details:   https://anonhg.NetBSD.org/src/rev/0fd25de0c9dd
branches:  trunk
changeset: 572514:0fd25de0c9dd
user:      lukem <lukem%NetBSD.org@localhost>
date:      Thu Jan 06 15:10:45 2005 +0000

description:
Implement getgroupmembership(3).  This is similar to getgrouplist(3), except
that the "int maxgroups" and "int *ngroups" parameters are separated into
two separate parameters which makes it possible to call multiple nsswitch
back-ends and have the results correctly merged.
getgrouplist(3) is now implemented using getgroupmembership(3).

Proposed on tech-userlevel on December 1, 2004.

diffstat:

 distrib/utils/libhack/getgrent.c  |   72 ++++-
 include/unistd.h                  |    3 +-
 lib/libc/gen/Makefile.inc         |    6 +-
 lib/libc/gen/getgrouplist.c       |  178 +---------------
 lib/libc/gen/getgroupmembership.c |  411 ++++++++++++++++++++++++++++++++++++++
 lib/libc/include/namespace.h      |    3 +-
 6 files changed, 478 insertions(+), 195 deletions(-)

diffs (truncated from 807 to 300 lines):

diff -r 824519d66a6b -r 0fd25de0c9dd distrib/utils/libhack/getgrent.c
--- a/distrib/utils/libhack/getgrent.c  Thu Jan 06 15:00:45 2005 +0000
+++ b/distrib/utils/libhack/getgrent.c  Thu Jan 06 15:10:45 2005 +0000
@@ -1,7 +1,7 @@
-/*     $NetBSD: getgrent.c,v 1.8 2003/10/13 15:36:33 agc Exp $ */
+/*     $NetBSD: getgrent.c,v 1.9 2005/01/06 15:10:45 lukem Exp $       */
 
 /*
- * Copyright (c) 1989, 1993
+ * Copyright (c) 1989, 1991, 1993
  *     The Regents of the University of California.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -69,6 +69,7 @@
 #define getgrnam               _getgrnam
 #define setgrent               _setgrent
 #define setgroupent            _setgroupent
+#define getgroupmembership     _getgroupmembership
 
 __weak_alias(endgrent,_endgrent)
 __weak_alias(getgrent,_getgrent)
@@ -76,9 +77,10 @@
 __weak_alias(getgrnam,_getgrnam)
 __weak_alias(setgrent,_setgrent)
 __weak_alias(setgroupent,_setgroupent)
+__weak_alias(getgroupmembership,_getgroupmembership)
 #endif
 
-#include <sys/types.h>
+#include <sys/param.h>
 
 #include <grp.h>
 #include <limits.h>
@@ -86,8 +88,6 @@
 #include <stdlib.h>
 #include <string.h>
 
-struct group *_getgrent_user(const char *);
-
 static FILE            *_gr_fp;
 static struct group    _gr_group;
 static int             _gr_stayopen;
@@ -112,21 +112,6 @@
        return &_gr_group;
 }
 
-/*
- * _getgrent_user() is designed only to be called by getgrouplist(3) and
- * hence makes no guarantees about filling the entire structure that it
- * returns.  It may only fill in the group name and gid fields.
- */
-
-struct group *
-_getgrent_user(const char *user)
-{
-
-       if ((!_gr_fp && !grstart()) || !grscan(0, 0, NULL, user))
-               return (NULL);
-       return &_gr_group;
-}
-
 struct group *
 getgrnam(const char *name)
 {
@@ -193,6 +178,53 @@
        }
 }
 
+int
+getgroupmembership(const char *uname, gid_t agroup,
+    gid_t *groups, int maxgroups, int *grpcnt)
+{
+       struct group *grp;
+       int i, ngroups, ret;
+
+       ret = 0;
+       ngroups = 0;
+
+       /*
+        * install primary group
+        */
+       if (ngroups < maxgroups)
+               groups[ngroups] = agroup;
+       else
+               ret = -1;
+       ngroups++;
+
+       /*
+        * Scan the group file to find additional groups.
+        */
+       setgrent();
+ nextgroup:
+       while ((grp = getgrent()) != NULL) {
+               if (grp->gr_gid == agroup)
+                       continue;
+               for (i = 0; grp->gr_mem[i]; i++) {
+                       if (strcmp(grp->gr_mem[i], uname) != 0)
+                               continue;
+                       for (i = 0; i < MIN(ngroups, maxgroups); i++) {
+                               if (grp->gr_gid == groups[i])
+                                       goto nextgroup;
+                       }
+                       if (ngroups < maxgroups)
+                               groups[ngroups] = grp->gr_gid;
+                       else
+                               ret = -1;
+                       ngroups++;
+                       break;
+               }
+       }
+       endgrent();
+       *grpcnt = ngroups;
+       return ret;
+}
+
 static int
 grscan(int search, gid_t gid, const char *name, const char *user)
 {
diff -r 824519d66a6b -r 0fd25de0c9dd include/unistd.h
--- a/include/unistd.h  Thu Jan 06 15:00:45 2005 +0000
+++ b/include/unistd.h  Thu Jan 06 15:10:45 2005 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: unistd.h,v 1.99 2004/06/01 16:10:29 kleink Exp $       */
+/*     $NetBSD: unistd.h,v 1.100 2005/01/06 15:10:45 lukem Exp $       */
 
 /*-
  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
@@ -311,6 +311,7 @@
 int     fsync_range __P((int, int, off_t, off_t));
 int     getdomainname __P((char *, size_t));
 int     getgrouplist __P((const char *, gid_t, gid_t *, int *));
+int     getgroupmembership __P((const char *, gid_t, gid_t *, int, int *));
 mode_t  getmode __P((const void *, mode_t));
 int     getsubopt __P((char **, char * const *, char **));
 __aconst char *getusershell __P((void));
diff -r 824519d66a6b -r 0fd25de0c9dd lib/libc/gen/Makefile.inc
--- a/lib/libc/gen/Makefile.inc Thu Jan 06 15:00:45 2005 +0000
+++ b/lib/libc/gen/Makefile.inc Thu Jan 06 15:10:45 2005 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile.inc,v 1.138 2005/01/02 16:43:26 thorpej Exp $
+#      $NetBSD: Makefile.inc,v 1.139 2005/01/06 15:10:45 lukem Exp $
 #      from: @(#)Makefile.inc  8.6 (Berkeley) 5/4/95
 
 # gen sources
@@ -10,8 +10,8 @@
        execle.c execlp.c execv.c execvp.c extattr.c \
        fmtcheck.c fmtmsg.c fnmatch.c \
        fstab.c ftok.c __fts13.c fts.c getbsize.c getcap.c getcwd.c \
-       getdevmajor.c \
-       getdomainname.c getgrent.c getgrouplist.c gethostname.c \
+       getdevmajor.c getdomainname.c getgrent.c \
+       getgrouplist.c getgroupmembership.c gethostname.c \
        getloadavg.c getlogin.c getmntinfo.c __getmntinfo13.c \
        getnetgrent.c getpagesize.c \
        getpass.c getprogname.c getpwent.c getttyent.c \
diff -r 824519d66a6b -r 0fd25de0c9dd lib/libc/gen/getgrouplist.c
--- a/lib/libc/gen/getgrouplist.c       Thu Jan 06 15:00:45 2005 +0000
+++ b/lib/libc/gen/getgrouplist.c       Thu Jan 06 15:10:45 2005 +0000
@@ -1,7 +1,7 @@
-/*     $NetBSD: getgrouplist.c,v 1.20 2004/09/28 10:46:19 lukem Exp $  */
+/*     $NetBSD: getgrouplist.c,v 1.21 2005/01/06 15:10:45 lukem Exp $  */
 
 /*-
- * Copyright (c) 2004 The NetBSD Foundation, Inc.
+ * Copyright (c) 2004-2005 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
@@ -36,41 +36,12 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-/*
- * Copyright (c) 1991, 1993
- *     The Regents of the University of California.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
 #if 0
 static char sccsid[] = "@(#)getgrouplist.c     8.2 (Berkeley) 12/8/94";
 #else
-__RCSID("$NetBSD: getgrouplist.c,v 1.20 2004/09/28 10:46:19 lukem Exp $");
+__RCSID("$NetBSD: getgrouplist.c,v 1.21 2005/01/06 15:10:45 lukem Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -82,158 +53,25 @@
 #include <sys/param.h>
 
 #include <assert.h>
-#include <errno.h>
-#include <grp.h>
 #include <nsswitch.h>
 #include <stdarg.h>
-#include <stdlib.h>
-#include <string.h>
 #include <unistd.h>
 
-#ifdef HESIOD
-#include <hesiod.h>
-#endif
-
 #ifdef __weak_alias
 __weak_alias(getgrouplist,_getgrouplist)
 #endif
 
-#ifdef HESIOD
-
-/*ARGSUSED*/
-static int
-_nss_dns_getgrouplist(void *retval, void *cb_data, va_list ap)
-{
-       int             *result = va_arg(ap, int *);
-       const char      *uname  = va_arg(ap, const char *);
-       gid_t            agroup = va_arg(ap, gid_t);
-       gid_t           *groups = va_arg(ap, gid_t *);
-       int             *grpcnt = va_arg(ap, int *);
-
-       unsigned long   id;
-       void            *context;
-       char            **hp, *cp, *ep;
-       int             rv, ret, ngroups, maxgroups;
-
-       hp = NULL;
-       rv = NS_NOTFOUND;
-       ret = 0;
-
-       if (hesiod_init(&context) == -1)                /* setup hesiod */
-               return NS_UNAVAIL;
-
-       hp = hesiod_resolve(context, uname, "grplist"); /* find grplist */
-       if (hp == NULL) {
-               if (errno != ENOENT)
-                       rv = NS_NOTFOUND;
-               goto dnsgrouplist_out;
-       }
-
-       if ((ep = strchr(hp[0], '\n')) != NULL)
-               *ep = '\0';                             /* clear trailing \n */
-
-       ret = 0;
-       ngroups = 0;
-       maxgroups = *grpcnt;
-
-       if (ngroups < maxgroups)                        /* add primary gid */
-               groups[ngroups] = agroup;
-       else
-               ret = -1;
-       ngroups++;
-
-       for (cp = hp[0]; *cp != '\0'; ) {               /* parse grplist */
-               if ((cp = strchr(cp, ':')) == NULL)     /* skip grpname */
-                       break;
-               cp++;
-               id = strtoul(cp, &ep, 10);              /* parse gid */
-               if (id > GID_MAX || (*ep != ':' && *ep != '\0')) {
-                       rv = NS_UNAVAIL;
-                       goto dnsgrouplist_out;
-               }
-               cp = ep;
-               if (*cp == ':')
-                       cp++;
-               if (ngroups < maxgroups)                /* add this gid */
-                       groups[ngroups] = (gid_t)id;
-               else
-                       ret = -1;
-               ngroups++;
-       }
-
-       *result = ret;
-       *grpcnt = ngroups;
-       rv = NS_SUCCESS;
-
- dnsgrouplist_out:
-       if (hp)



Home | Main Index | Thread Index | Old Index