2003-10-13 19:36:33 +04:00
|
|
|
/* $NetBSD: getgrent.c,v 1.48 2003/10/13 15:36:33 agc Exp $ */
|
1995-02-27 07:12:15 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1995-02-27 07:12:15 +03:00
|
|
|
* Copyright (c) 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
2003-08-07 20:42:00 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
1995-07-28 09:43:01 +04:00
|
|
|
* Portions Copyright (c) 1994, Jason Downs. All Rights Reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2003-10-13 19:36:33 +04:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``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 AUTHOR(S) 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
|
1993-03-21 12:45:37 +03:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
1997-07-13 23:01:22 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
1995-02-27 07:12:15 +03:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)getgrent.c 8.2 (Berkeley) 3/21/94";
|
|
|
|
#else
|
2003-10-13 19:36:33 +04:00
|
|
|
__RCSID("$NetBSD: getgrent.c,v 1.48 2003/10/13 15:36:33 agc Exp $");
|
1995-02-27 07:12:15 +03:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* LIBC_SCCS and not lint */
|
|
|
|
|
1997-07-21 18:06:24 +04:00
|
|
|
#include "namespace.h"
|
1999-01-25 04:09:34 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <sys/types.h>
|
1999-01-25 04:09:34 +03:00
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
#include <assert.h>
|
1999-01-25 04:09:34 +03:00
|
|
|
#include <errno.h>
|
1999-01-16 10:47:18 +03:00
|
|
|
#include <grp.h>
|
1997-05-22 07:24:37 +04:00
|
|
|
#include <limits.h>
|
1999-01-16 10:47:18 +03:00
|
|
|
#include <nsswitch.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1993-05-04 09:10:45 +04:00
|
|
|
#include <string.h>
|
1999-01-16 10:47:18 +03:00
|
|
|
#include <syslog.h>
|
1999-01-25 04:09:34 +03:00
|
|
|
|
1999-04-18 06:04:04 +04:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
1999-01-16 10:47:18 +03:00
|
|
|
#ifdef HESIOD
|
|
|
|
#include <hesiod.h>
|
|
|
|
#endif
|
1993-05-04 09:10:45 +04:00
|
|
|
#ifdef YP
|
|
|
|
#include <rpc/rpc.h>
|
|
|
|
#include <rpcsvc/yp_prot.h>
|
|
|
|
#include <rpcsvc/ypclnt.h>
|
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1999-01-26 04:08:06 +03:00
|
|
|
#if defined(YP) || defined(HESIOD)
|
|
|
|
#define _GROUP_COMPAT
|
|
|
|
#endif
|
|
|
|
|
2003-02-16 04:22:44 +03:00
|
|
|
struct group *_getgrent_user(const char *);
|
|
|
|
|
1997-07-21 18:06:24 +04:00
|
|
|
#ifdef __weak_alias
|
2000-01-23 01:19:07 +03:00
|
|
|
__weak_alias(endgrent,_endgrent)
|
|
|
|
__weak_alias(getgrent,_getgrent)
|
|
|
|
__weak_alias(getgrgid,_getgrgid)
|
|
|
|
__weak_alias(getgrnam,_getgrnam)
|
|
|
|
__weak_alias(setgrent,_setgrent)
|
|
|
|
__weak_alias(setgroupent,_setgroupent)
|
1997-07-21 18:06:24 +04:00
|
|
|
#endif
|
|
|
|
|
1999-01-16 10:47:18 +03:00
|
|
|
static FILE *_gr_fp;
|
|
|
|
static struct group _gr_group;
|
|
|
|
static int _gr_stayopen;
|
1999-04-25 17:39:41 +04:00
|
|
|
static int _gr_filesdone;
|
1999-01-16 10:47:18 +03:00
|
|
|
|
2002-01-13 02:51:30 +03:00
|
|
|
static void grcleanup(void);
|
2003-02-16 04:22:44 +03:00
|
|
|
static int grscan(int, gid_t, const char *, const char *);
|
2002-02-02 18:21:29 +03:00
|
|
|
static int grstart(void);
|
2003-02-16 04:22:44 +03:00
|
|
|
static int grmatchline(int, gid_t, const char *, const char *);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
#define MAXGRP 200
|
|
|
|
#define MAXLINELENGTH 1024
|
1999-01-16 10:47:18 +03:00
|
|
|
|
|
|
|
static __aconst char *members[MAXGRP];
|
|
|
|
static char line[MAXLINELENGTH];
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1993-05-04 09:10:45 +04:00
|
|
|
#ifdef YP
|
1999-01-26 04:08:06 +03:00
|
|
|
static char *__ypcurrent, *__ypdomain;
|
|
|
|
static int __ypcurrentlen;
|
1999-04-25 17:39:41 +04:00
|
|
|
static int _gr_ypdone;
|
1999-01-16 10:47:18 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HESIOD
|
2003-02-16 04:22:44 +03:00
|
|
|
static int _gr_hesnum;
|
|
|
|
static struct group *_gr_hesgrplist = NULL;
|
|
|
|
static int _gr_hesgrplistnum;
|
|
|
|
static int _gr_hesgrplistmax;
|
1993-05-04 09:10:45 +04:00
|
|
|
#endif
|
|
|
|
|
1999-01-26 04:08:06 +03:00
|
|
|
#ifdef _GROUP_COMPAT
|
|
|
|
enum _grmode { GRMODE_NONE, GRMODE_FULL, GRMODE_NAME };
|
|
|
|
static enum _grmode __grmode;
|
|
|
|
#endif
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
struct group *
|
2002-01-13 02:51:30 +03:00
|
|
|
getgrent(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2002-01-13 02:51:30 +03:00
|
|
|
|
2003-02-16 04:22:44 +03:00
|
|
|
if ((!_gr_fp && !grstart()) || !grscan(0, 0, NULL, NULL))
|
|
|
|
return (NULL);
|
|
|
|
return &_gr_group;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-02-17 03:11:54 +03:00
|
|
|
* _getgrent_user() is designed only to be called by getgrouplist(3) and
|
2003-02-16 04:22:44 +03:00
|
|
|
* 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))
|
1999-04-25 17:39:41 +04:00
|
|
|
return (NULL);
|
1999-01-16 10:47:18 +03:00
|
|
|
return &_gr_group;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct group *
|
2002-01-13 02:51:30 +03:00
|
|
|
getgrnam(const char *name)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
int rval;
|
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(name != NULL);
|
|
|
|
|
2002-02-02 18:21:29 +03:00
|
|
|
if (!grstart())
|
1999-01-16 10:47:18 +03:00
|
|
|
return NULL;
|
2003-02-16 04:22:44 +03:00
|
|
|
rval = grscan(1, 0, name, NULL);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (!_gr_stayopen)
|
|
|
|
endgrent();
|
1999-01-16 10:47:18 +03:00
|
|
|
return (rval) ? &_gr_group : NULL;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct group *
|
2002-01-13 02:51:30 +03:00
|
|
|
getgrgid(gid_t gid)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
int rval;
|
|
|
|
|
2002-02-02 18:21:29 +03:00
|
|
|
if (!grstart())
|
1999-01-16 10:47:18 +03:00
|
|
|
return NULL;
|
2003-02-16 04:22:44 +03:00
|
|
|
rval = grscan(1, gid, NULL, NULL);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (!_gr_stayopen)
|
|
|
|
endgrent();
|
1999-01-16 10:47:18 +03:00
|
|
|
return (rval) ? &_gr_group : NULL;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1999-04-25 17:39:41 +04:00
|
|
|
void
|
2002-01-13 02:51:30 +03:00
|
|
|
grcleanup(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2002-01-13 02:51:30 +03:00
|
|
|
|
1999-04-25 17:39:41 +04:00
|
|
|
_gr_filesdone = 0;
|
1994-03-06 12:04:11 +03:00
|
|
|
#ifdef YP
|
1999-01-16 10:47:18 +03:00
|
|
|
if (__ypcurrent)
|
|
|
|
free(__ypcurrent);
|
|
|
|
__ypcurrent = NULL;
|
1999-04-25 17:39:41 +04:00
|
|
|
_gr_ypdone = 0;
|
1999-01-16 10:47:18 +03:00
|
|
|
#endif
|
|
|
|
#ifdef HESIOD
|
1999-04-25 17:39:41 +04:00
|
|
|
_gr_hesnum = 0;
|
2003-02-16 04:22:44 +03:00
|
|
|
if (!_gr_hesgrplist)
|
|
|
|
free(_gr_hesgrplist);
|
|
|
|
_gr_hesgrplist = NULL;
|
|
|
|
_gr_hesgrplistnum = -1;
|
|
|
|
_gr_hesgrplistmax = 0;
|
1999-01-26 04:08:06 +03:00
|
|
|
#endif
|
|
|
|
#ifdef _GROUP_COMPAT
|
|
|
|
__grmode = GRMODE_NONE;
|
1994-03-06 12:04:11 +03:00
|
|
|
#endif
|
1999-04-25 17:39:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-02-02 18:21:29 +03:00
|
|
|
grstart(void)
|
1999-04-25 17:39:41 +04:00
|
|
|
{
|
2002-01-13 02:51:30 +03:00
|
|
|
|
1999-04-25 17:39:41 +04:00
|
|
|
grcleanup();
|
1999-01-16 10:47:18 +03:00
|
|
|
if (_gr_fp) {
|
|
|
|
rewind(_gr_fp);
|
|
|
|
return 1;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1999-01-16 10:47:18 +03:00
|
|
|
return (_gr_fp = fopen(_PATH_GROUP, "r")) ? 1 : 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1993-10-26 02:36:51 +03:00
|
|
|
void
|
2002-01-13 02:51:30 +03:00
|
|
|
setgrent(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2002-01-13 02:51:30 +03:00
|
|
|
|
1993-10-26 02:36:51 +03:00
|
|
|
(void) setgroupent(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-01-13 02:51:30 +03:00
|
|
|
setgroupent(int stayopen)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2002-01-13 02:51:30 +03:00
|
|
|
|
2002-02-02 18:21:29 +03:00
|
|
|
if (!grstart())
|
1999-01-16 10:47:18 +03:00
|
|
|
return 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
_gr_stayopen = stayopen;
|
1999-01-16 10:47:18 +03:00
|
|
|
return 1;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-01-13 02:51:30 +03:00
|
|
|
endgrent(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2002-01-13 02:51:30 +03:00
|
|
|
|
1999-04-25 17:39:41 +04:00
|
|
|
grcleanup();
|
1993-03-21 12:45:37 +03:00
|
|
|
if (_gr_fp) {
|
|
|
|
(void)fclose(_gr_fp);
|
|
|
|
_gr_fp = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-16 10:47:18 +03:00
|
|
|
|
2002-01-13 02:51:30 +03:00
|
|
|
static int _local_grscan(void *, void *, va_list);
|
1999-01-16 10:47:18 +03:00
|
|
|
|
1999-01-18 23:36:32 +03:00
|
|
|
/*ARGSUSED*/
|
1993-05-04 09:10:45 +04:00
|
|
|
static int
|
2002-01-13 02:51:30 +03:00
|
|
|
_local_grscan(void *rv, void *cb_data, va_list ap)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1999-01-16 10:47:18 +03:00
|
|
|
int search = va_arg(ap, int);
|
|
|
|
gid_t gid = va_arg(ap, gid_t);
|
|
|
|
const char *name = va_arg(ap, const char *);
|
2003-02-16 04:22:44 +03:00
|
|
|
const char *user = va_arg(ap, const char *);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1999-04-25 17:39:41 +04:00
|
|
|
if (_gr_filesdone)
|
|
|
|
return NS_NOTFOUND;
|
1993-03-21 12:45:37 +03:00
|
|
|
for (;;) {
|
1999-01-16 10:47:18 +03:00
|
|
|
if (!fgets(line, sizeof(line), _gr_fp)) {
|
1999-04-25 17:39:41 +04:00
|
|
|
if (!search)
|
|
|
|
_gr_filesdone = 1;
|
1999-01-16 10:47:18 +03:00
|
|
|
return NS_NOTFOUND;
|
|
|
|
}
|
|
|
|
/* skip lines that are too big */
|
|
|
|
if (!strchr(line, '\n')) {
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
|
|
|
|
;
|
|
|
|
continue;
|
|
|
|
}
|
2003-02-16 04:22:44 +03:00
|
|
|
if (grmatchline(search, gid, name, user))
|
1999-01-16 10:47:18 +03:00
|
|
|
return NS_SUCCESS;
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HESIOD
|
2002-01-13 02:51:30 +03:00
|
|
|
static int _dns_grscan(void *, void *, va_list);
|
2003-02-16 04:22:44 +03:00
|
|
|
static int _dns_grplist(const char *);
|
1999-01-16 10:47:18 +03:00
|
|
|
|
1999-01-18 23:36:32 +03:00
|
|
|
/*ARGSUSED*/
|
1999-01-16 10:47:18 +03:00
|
|
|
static int
|
2002-01-13 02:51:30 +03:00
|
|
|
_dns_grscan(void *rv, void *cb_data, va_list ap)
|
1999-01-16 10:47:18 +03:00
|
|
|
{
|
|
|
|
int search = va_arg(ap, int);
|
|
|
|
gid_t gid = va_arg(ap, gid_t);
|
|
|
|
const char *name = va_arg(ap, const char *);
|
2003-02-16 04:22:44 +03:00
|
|
|
const char *user = va_arg(ap, const char *);
|
1999-01-16 10:47:18 +03:00
|
|
|
|
|
|
|
char **hp;
|
1999-01-25 04:09:34 +03:00
|
|
|
void *context;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = NS_UNAVAIL;
|
2003-02-16 04:22:44 +03:00
|
|
|
if (!search && user && _gr_hesgrplistmax != -1) {
|
|
|
|
r = _dns_grplist(user);
|
|
|
|
/* if we did not find user.grplist, just iterate */
|
|
|
|
if (!_gr_hesgrplist) {
|
|
|
|
_gr_hesgrplistmax = -1;
|
|
|
|
if (r != NS_NOTFOUND)
|
|
|
|
return r;
|
|
|
|
} else
|
|
|
|
return r;
|
|
|
|
}
|
1999-04-25 17:39:41 +04:00
|
|
|
if (!search && _gr_hesnum == -1)
|
|
|
|
return NS_NOTFOUND;
|
1999-01-25 04:09:34 +03:00
|
|
|
if (hesiod_init(&context) == -1)
|
|
|
|
return (r);
|
1999-01-16 10:47:18 +03:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (search) {
|
|
|
|
if (name)
|
2002-11-17 04:51:24 +03:00
|
|
|
strlcpy(line, name, sizeof(line));
|
1999-01-16 10:47:18 +03:00
|
|
|
else
|
1999-01-16 17:44:33 +03:00
|
|
|
snprintf(line, sizeof(line), "%u",
|
|
|
|
(unsigned int)gid);
|
1999-01-16 10:47:18 +03:00
|
|
|
} else {
|
1999-04-25 17:39:41 +04:00
|
|
|
snprintf(line, sizeof(line), "group-%u", _gr_hesnum);
|
|
|
|
_gr_hesnum++;
|
1999-01-16 10:47:18 +03:00
|
|
|
}
|
|
|
|
|
2003-02-03 07:22:20 +03:00
|
|
|
hp = NULL;
|
|
|
|
if (search && !name) {
|
|
|
|
hp = hesiod_resolve(context, line, "gid");
|
|
|
|
if (hp == NULL && errno != ENOENT)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (hp == NULL)
|
|
|
|
hp = hesiod_resolve(context, line, "group");
|
1999-01-16 10:47:18 +03:00
|
|
|
if (hp == NULL) {
|
1999-01-25 04:09:34 +03:00
|
|
|
if (errno == ENOENT) {
|
1999-04-25 17:39:41 +04:00
|
|
|
if (!search)
|
|
|
|
_gr_hesnum = -1;
|
|
|
|
r = NS_NOTFOUND;
|
1999-01-16 10:47:18 +03:00
|
|
|
}
|
1999-01-25 04:09:34 +03:00
|
|
|
break;
|
1999-01-16 10:47:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* only check first elem */
|
2002-11-17 04:51:24 +03:00
|
|
|
strlcpy(line, hp[0], sizeof(line));
|
1999-01-25 04:09:34 +03:00
|
|
|
hesiod_free_list(context, hp);
|
2003-02-16 04:22:44 +03:00
|
|
|
if (grmatchline(search, gid, name, user)) {
|
1999-01-25 04:09:34 +03:00
|
|
|
r = NS_SUCCESS;
|
|
|
|
break;
|
|
|
|
} else if (search) {
|
|
|
|
r = NS_NOTFOUND;
|
|
|
|
break;
|
|
|
|
}
|
1999-01-16 10:47:18 +03:00
|
|
|
}
|
1999-01-25 04:09:34 +03:00
|
|
|
hesiod_end(context);
|
|
|
|
return (r);
|
1999-01-16 10:47:18 +03:00
|
|
|
}
|
2003-02-16 04:22:44 +03:00
|
|
|
|
|
|
|
static int
|
|
|
|
_dns_grplist(const char *user)
|
|
|
|
{
|
|
|
|
void *context;
|
|
|
|
int r;
|
|
|
|
char **hp;
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
r = NS_UNAVAIL;
|
|
|
|
if (!_gr_hesgrplist) {
|
|
|
|
if (hesiod_init(&context) == -1)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
_gr_hesgrplistnum = -1;
|
|
|
|
hp = hesiod_resolve(context, user, "grplist");
|
|
|
|
if (!hp) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
r = NS_NOTFOUND;
|
|
|
|
hesiod_end(context);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
strlcpy(line, hp[0], sizeof(line));
|
|
|
|
hesiod_free_list(context, hp);
|
|
|
|
|
|
|
|
_gr_hesgrplistmax = 0;
|
|
|
|
for (cp=line; *cp; cp++)
|
|
|
|
if (*cp == ':')
|
|
|
|
_gr_hesgrplistmax++;
|
|
|
|
_gr_hesgrplistmax /= 2;
|
|
|
|
_gr_hesgrplistmax++;
|
|
|
|
|
|
|
|
_gr_hesgrplist = malloc(_gr_hesgrplistmax *
|
|
|
|
sizeof(*_gr_hesgrplist));
|
|
|
|
if (!_gr_hesgrplist) {
|
|
|
|
hesiod_end(context);
|
|
|
|
return NS_UNAVAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cp = line;
|
|
|
|
_gr_hesgrplistmax = 0;
|
|
|
|
for (;;) {
|
|
|
|
char *name;
|
|
|
|
char *num;
|
|
|
|
gid_t gid;
|
|
|
|
char *ep;
|
|
|
|
|
|
|
|
/* XXXrcd: error handling */
|
|
|
|
if (!(name = strsep(&cp, ":")))
|
|
|
|
break;
|
|
|
|
if (!(num = strsep(&cp, ":")))
|
|
|
|
break;
|
|
|
|
gid = (gid_t) strtoul(num, &ep, 10);
|
|
|
|
if (gid > GID_MAX || *ep != '\0')
|
|
|
|
break;
|
|
|
|
|
|
|
|
_gr_hesgrplist[_gr_hesgrplistmax].gr_name = name;
|
|
|
|
_gr_hesgrplist[_gr_hesgrplistmax].gr_gid = gid;
|
|
|
|
_gr_hesgrplistmax++;
|
|
|
|
}
|
|
|
|
|
|
|
|
hesiod_end(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we assume that _gr_hesgrplist is now defined */
|
|
|
|
if (++_gr_hesgrplistnum >= _gr_hesgrplistmax)
|
|
|
|
return NS_NOTFOUND;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now we copy the relevant information into _gr_group, so that
|
|
|
|
* it can be returned. Note that we only fill in the bare necessities
|
|
|
|
* as this will be used exclusively by getgrouplist(3) and we do
|
|
|
|
* not want to have to look up all of the information.
|
|
|
|
*/
|
|
|
|
_gr_group.gr_name = _gr_hesgrplist[_gr_hesgrplistnum].gr_name;
|
|
|
|
_gr_group.gr_passwd = NULL;
|
|
|
|
_gr_group.gr_gid = _gr_hesgrplist[_gr_hesgrplistnum].gr_gid;
|
|
|
|
_gr_group.gr_mem = NULL;
|
|
|
|
|
|
|
|
return NS_SUCCESS;
|
|
|
|
}
|
2002-01-13 02:51:30 +03:00
|
|
|
#endif /* HESIOD */
|
1999-01-16 10:47:18 +03:00
|
|
|
|
|
|
|
#ifdef YP
|
2002-01-13 02:51:30 +03:00
|
|
|
static int _nis_grscan(void *, void *, va_list);
|
1999-01-16 10:47:18 +03:00
|
|
|
|
1999-01-18 23:36:32 +03:00
|
|
|
/*ARGSUSED*/
|
1999-01-16 10:47:18 +03:00
|
|
|
static int
|
2002-01-13 02:51:30 +03:00
|
|
|
_nis_grscan(void *rv, void *cb_data, va_list ap)
|
1999-01-16 10:47:18 +03:00
|
|
|
{
|
|
|
|
int search = va_arg(ap, int);
|
|
|
|
gid_t gid = va_arg(ap, gid_t);
|
|
|
|
const char *name = va_arg(ap, const char *);
|
2003-02-16 04:22:44 +03:00
|
|
|
const char *user = va_arg(ap, const char *);
|
1999-01-16 10:47:18 +03:00
|
|
|
|
|
|
|
char *key, *data;
|
|
|
|
int keylen, datalen;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if(__ypdomain == NULL) {
|
|
|
|
switch (yp_get_default_domain(&__ypdomain)) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case YPERR_RESRC:
|
|
|
|
return NS_TRYAGAIN;
|
|
|
|
default:
|
|
|
|
return NS_UNAVAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (search) { /* specific group or gid */
|
|
|
|
if (name)
|
2002-11-17 04:51:24 +03:00
|
|
|
strlcpy(line, name, sizeof(line));
|
1999-01-16 10:47:18 +03:00
|
|
|
else
|
1999-01-16 17:44:33 +03:00
|
|
|
snprintf(line, sizeof(line), "%u", (unsigned int)gid);
|
1999-01-16 10:47:18 +03:00
|
|
|
data = NULL;
|
|
|
|
r = yp_match(__ypdomain,
|
|
|
|
(name) ? "group.byname" : "group.bygid",
|
|
|
|
line, (int)strlen(line), &data, &datalen);
|
|
|
|
switch (r) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case YPERR_KEY:
|
|
|
|
if (data)
|
|
|
|
free(data);
|
|
|
|
return NS_NOTFOUND;
|
|
|
|
default:
|
|
|
|
if (data)
|
1997-05-20 19:46:03 +04:00
|
|
|
free(data);
|
1999-01-16 10:47:18 +03:00
|
|
|
return NS_UNAVAIL;
|
|
|
|
}
|
|
|
|
data[datalen] = '\0'; /* clear trailing \n */
|
2002-11-17 04:51:24 +03:00
|
|
|
strlcpy(line, data, sizeof(line));
|
1999-01-16 10:47:18 +03:00
|
|
|
free(data);
|
2003-02-16 04:22:44 +03:00
|
|
|
if (grmatchline(search, gid, name, user))
|
1999-01-16 10:47:18 +03:00
|
|
|
return NS_SUCCESS;
|
|
|
|
else
|
|
|
|
return NS_NOTFOUND;
|
|
|
|
}
|
|
|
|
|
1999-04-25 17:39:41 +04:00
|
|
|
/* ! search */
|
|
|
|
if (_gr_ypdone)
|
|
|
|
return NS_NOTFOUND;
|
|
|
|
for (;;) {
|
1999-01-16 10:47:18 +03:00
|
|
|
data = NULL;
|
|
|
|
if(__ypcurrent) {
|
|
|
|
key = NULL;
|
|
|
|
r = yp_next(__ypdomain, "group.byname",
|
|
|
|
__ypcurrent, __ypcurrentlen,
|
|
|
|
&key, &keylen, &data, &datalen);
|
|
|
|
free(__ypcurrent);
|
|
|
|
switch (r) {
|
|
|
|
case 0:
|
1995-07-28 09:43:01 +04:00
|
|
|
break;
|
1999-01-16 10:47:18 +03:00
|
|
|
case YPERR_NOMORE:
|
|
|
|
__ypcurrent = NULL;
|
|
|
|
if (key)
|
|
|
|
free(key);
|
|
|
|
if (data)
|
|
|
|
free(data);
|
1999-04-25 17:39:41 +04:00
|
|
|
_gr_ypdone = 1;
|
|
|
|
return NS_NOTFOUND;
|
1999-01-16 10:47:18 +03:00
|
|
|
default:
|
|
|
|
if (key)
|
|
|
|
free(key);
|
|
|
|
if (data)
|
|
|
|
free(data);
|
|
|
|
return NS_UNAVAIL;
|
|
|
|
}
|
|
|
|
__ypcurrent = key;
|
|
|
|
__ypcurrentlen = keylen;
|
|
|
|
} else {
|
|
|
|
if (yp_first(__ypdomain, "group.byname",
|
|
|
|
&__ypcurrent, &__ypcurrentlen,
|
|
|
|
&data, &datalen)) {
|
2000-12-18 01:09:12 +03:00
|
|
|
if (data)
|
1993-05-04 09:10:45 +04:00
|
|
|
free(data);
|
1999-01-16 10:47:18 +03:00
|
|
|
return NS_UNAVAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data[datalen] = '\0'; /* clear trailing \n */
|
2002-11-17 04:51:24 +03:00
|
|
|
strlcpy(line, data, sizeof(line));
|
1999-01-16 10:47:18 +03:00
|
|
|
free(data);
|
2003-02-16 04:22:44 +03:00
|
|
|
if (grmatchline(search, gid, name, user))
|
1999-01-16 10:47:18 +03:00
|
|
|
return NS_SUCCESS;
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
2002-01-13 02:51:30 +03:00
|
|
|
#endif /* YP */
|
1999-01-16 10:47:18 +03:00
|
|
|
|
1999-01-26 04:08:06 +03:00
|
|
|
#ifdef _GROUP_COMPAT
|
1999-01-16 10:47:18 +03:00
|
|
|
/*
|
|
|
|
* log an error if "files" or "compat" is specified in group_compat database
|
|
|
|
*/
|
2002-01-13 02:51:30 +03:00
|
|
|
static int _bad_grscan(void *, void *, va_list);
|
1999-01-16 10:47:18 +03:00
|
|
|
|
1999-01-18 23:36:32 +03:00
|
|
|
/*ARGSUSED*/
|
1999-01-16 10:47:18 +03:00
|
|
|
static int
|
2002-01-13 02:51:30 +03:00
|
|
|
_bad_grscan(void *rv, void *cb_data, va_list ap)
|
1999-01-16 10:47:18 +03:00
|
|
|
{
|
|
|
|
static int warned;
|
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(cb_data != NULL);
|
|
|
|
|
1999-01-16 10:47:18 +03:00
|
|
|
if (!warned) {
|
|
|
|
syslog(LOG_ERR,
|
|
|
|
"nsswitch.conf group_compat database can't use '%s'",
|
|
|
|
(char *)cb_data);
|
|
|
|
}
|
|
|
|
warned = 1;
|
|
|
|
return NS_UNAVAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* when a name lookup in compat mode is required, look it up in group_compat
|
|
|
|
* nsswitch database. only Hesiod and NIS is supported - it doesn't make
|
|
|
|
* sense to lookup compat names from 'files' or 'compat'
|
|
|
|
*/
|
|
|
|
|
2003-02-16 04:22:44 +03:00
|
|
|
static int __grscancompat(int, gid_t, const char *, const char *);
|
1999-01-16 10:47:18 +03:00
|
|
|
|
|
|
|
static int
|
2003-02-16 04:22:44 +03:00
|
|
|
__grscancompat(int search, gid_t gid, const char *name, const char *user)
|
1999-01-16 10:47:18 +03:00
|
|
|
{
|
1999-01-19 11:30:46 +03:00
|
|
|
static const ns_dtab dtab[] = {
|
1999-01-19 11:07:58 +03:00
|
|
|
NS_FILES_CB(_bad_grscan, "files")
|
|
|
|
NS_DNS_CB(_dns_grscan, NULL)
|
|
|
|
NS_NIS_CB(_nis_grscan, NULL)
|
|
|
|
NS_COMPAT_CB(_bad_grscan, "compat")
|
1999-01-16 10:47:18 +03:00
|
|
|
{ 0 }
|
|
|
|
};
|
1999-01-20 05:59:37 +03:00
|
|
|
static const ns_src defaultnis[] = {
|
|
|
|
{ NSSRC_NIS, NS_SUCCESS },
|
|
|
|
{ 0 }
|
|
|
|
};
|
1999-01-16 10:47:18 +03:00
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(name != NULL);
|
|
|
|
|
1999-01-19 11:07:58 +03:00
|
|
|
return (nsdispatch(NULL, dtab, NSDB_GROUP_COMPAT, "grscancompat",
|
2003-02-16 04:22:44 +03:00
|
|
|
defaultnis, search, gid, name, user));
|
1999-01-16 10:47:18 +03:00
|
|
|
}
|
2002-01-13 02:51:30 +03:00
|
|
|
#endif /* GROUP_COMPAT */
|
1999-01-16 10:47:18 +03:00
|
|
|
|
|
|
|
|
2002-01-13 02:51:30 +03:00
|
|
|
static int _compat_grscan(void *, void *, va_list);
|
1999-01-16 10:47:18 +03:00
|
|
|
|
1999-01-18 23:36:32 +03:00
|
|
|
/*ARGSUSED*/
|
1999-01-16 10:47:18 +03:00
|
|
|
static int
|
2002-01-13 02:51:30 +03:00
|
|
|
_compat_grscan(void *rv, void *cb_data, va_list ap)
|
1999-01-16 10:47:18 +03:00
|
|
|
{
|
|
|
|
int search = va_arg(ap, int);
|
|
|
|
gid_t gid = va_arg(ap, gid_t);
|
|
|
|
const char *name = va_arg(ap, const char *);
|
2003-02-16 04:22:44 +03:00
|
|
|
const char *user = va_arg(ap, const char *);
|
1999-01-16 10:47:18 +03:00
|
|
|
|
1999-01-26 04:08:06 +03:00
|
|
|
#ifdef _GROUP_COMPAT
|
1999-01-16 10:47:18 +03:00
|
|
|
static char *grname = NULL;
|
1999-01-26 04:08:06 +03:00
|
|
|
#endif
|
1999-01-16 10:47:18 +03:00
|
|
|
|
|
|
|
for (;;) {
|
1999-01-26 04:08:06 +03:00
|
|
|
#ifdef _GROUP_COMPAT
|
1999-01-16 10:47:18 +03:00
|
|
|
if(__grmode != GRMODE_NONE) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
switch(__grmode) {
|
|
|
|
case GRMODE_FULL:
|
2003-02-16 04:22:44 +03:00
|
|
|
r = __grscancompat(search, gid, name, user);
|
1999-01-16 10:47:18 +03:00
|
|
|
if (r == NS_SUCCESS)
|
|
|
|
return r;
|
|
|
|
__grmode = GRMODE_NONE;
|
|
|
|
break;
|
|
|
|
case GRMODE_NAME:
|
|
|
|
if(grname == (char *)NULL) {
|
|
|
|
__grmode = GRMODE_NONE;
|
|
|
|
break;
|
|
|
|
}
|
2003-02-16 04:22:44 +03:00
|
|
|
r = __grscancompat(1, 0, grname, user);
|
1999-01-16 10:47:18 +03:00
|
|
|
free(grname);
|
|
|
|
grname = (char *)NULL;
|
|
|
|
if (r != NS_SUCCESS)
|
|
|
|
break;
|
|
|
|
if (!search)
|
|
|
|
return NS_SUCCESS;
|
|
|
|
if (name) {
|
|
|
|
if (! strcmp(_gr_group.gr_name, name))
|
|
|
|
return NS_SUCCESS;
|
1995-07-28 09:43:01 +04:00
|
|
|
} else {
|
1999-01-16 10:47:18 +03:00
|
|
|
if (_gr_group.gr_gid == gid)
|
|
|
|
return NS_SUCCESS;
|
1993-05-04 09:10:45 +04:00
|
|
|
}
|
1995-07-28 09:43:01 +04:00
|
|
|
break;
|
1999-01-16 10:47:18 +03:00
|
|
|
case GRMODE_NONE:
|
|
|
|
abort();
|
1993-05-04 09:10:45 +04:00
|
|
|
}
|
1999-01-16 10:47:18 +03:00
|
|
|
continue;
|
1993-05-04 09:10:45 +04:00
|
|
|
}
|
2002-01-13 02:51:30 +03:00
|
|
|
#endif /* _GROUP_COMPAT */
|
1999-01-16 10:47:18 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (!fgets(line, sizeof(line), _gr_fp))
|
1999-01-16 10:47:18 +03:00
|
|
|
return NS_NOTFOUND;
|
1993-03-21 12:45:37 +03:00
|
|
|
/* skip lines that are too big */
|
1993-11-24 22:43:49 +03:00
|
|
|
if (!strchr(line, '\n')) {
|
1993-03-21 12:45:37 +03:00
|
|
|
int ch;
|
|
|
|
|
|
|
|
while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
|
|
|
|
;
|
|
|
|
continue;
|
|
|
|
}
|
1999-01-16 10:47:18 +03:00
|
|
|
|
1999-01-26 04:08:06 +03:00
|
|
|
#ifdef _GROUP_COMPAT
|
1995-07-28 09:43:01 +04:00
|
|
|
if (line[0] == '+') {
|
1999-01-16 10:47:18 +03:00
|
|
|
char *tptr, *bp;
|
|
|
|
|
1995-07-28 09:43:01 +04:00
|
|
|
switch(line[1]) {
|
|
|
|
case ':':
|
|
|
|
case '\0':
|
|
|
|
case '\n':
|
1999-01-16 10:47:18 +03:00
|
|
|
__grmode = GRMODE_FULL;
|
1995-07-28 09:43:01 +04:00
|
|
|
break;
|
|
|
|
default:
|
1999-01-16 10:47:18 +03:00
|
|
|
__grmode = GRMODE_NAME;
|
|
|
|
bp = line;
|
|
|
|
tptr = strsep(&bp, ":\n");
|
|
|
|
grname = strdup(tptr + 1);
|
1995-07-28 09:43:01 +04:00
|
|
|
break;
|
1993-05-04 09:10:45 +04:00
|
|
|
}
|
1994-07-29 22:54:17 +04:00
|
|
|
continue;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2002-01-13 02:51:30 +03:00
|
|
|
#endif /* _GROUP_COMPAT */
|
2003-02-16 04:22:44 +03:00
|
|
|
if (grmatchline(search, gid, name, user))
|
1999-01-16 10:47:18 +03:00
|
|
|
return NS_SUCCESS;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
1999-01-16 10:47:18 +03:00
|
|
|
|
|
|
|
static int
|
2003-02-16 04:22:44 +03:00
|
|
|
grscan(int search, gid_t gid, const char *name, const char *user)
|
1999-01-16 10:47:18 +03:00
|
|
|
{
|
|
|
|
int r;
|
1999-01-19 11:30:46 +03:00
|
|
|
static const ns_dtab dtab[] = {
|
1999-01-19 11:07:58 +03:00
|
|
|
NS_FILES_CB(_local_grscan, NULL)
|
|
|
|
NS_DNS_CB(_dns_grscan, NULL)
|
|
|
|
NS_NIS_CB(_nis_grscan, NULL)
|
|
|
|
NS_COMPAT_CB(_compat_grscan, NULL)
|
1999-01-16 10:47:18 +03:00
|
|
|
{ 0 }
|
|
|
|
};
|
1999-01-20 05:59:37 +03:00
|
|
|
static const ns_src compatsrc[] = {
|
|
|
|
{ NSSRC_COMPAT, NS_SUCCESS },
|
|
|
|
{ 0 }
|
|
|
|
};
|
1999-01-16 10:47:18 +03:00
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
/* name may be NULL if search is nonzero */
|
|
|
|
|
1999-01-20 05:59:37 +03:00
|
|
|
r = nsdispatch(NULL, dtab, NSDB_GROUP, "grscan", compatsrc,
|
2003-02-16 04:22:44 +03:00
|
|
|
search, gid, name, user);
|
1999-01-16 10:47:18 +03:00
|
|
|
return (r == NS_SUCCESS) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-02-16 04:22:44 +03:00
|
|
|
grmatchline(int search, gid_t gid, const char *name, const char *user)
|
1999-01-16 10:47:18 +03:00
|
|
|
{
|
|
|
|
unsigned long id;
|
|
|
|
__aconst char **m;
|
|
|
|
char *cp, *bp, *ep;
|
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
/* name may be NULL if search is nonzero */
|
|
|
|
|
1999-01-16 10:47:18 +03:00
|
|
|
if (line[0] == '+')
|
|
|
|
return 0; /* sanity check to prevent recursion */
|
|
|
|
bp = line;
|
|
|
|
_gr_group.gr_name = strsep(&bp, ":\n");
|
|
|
|
if (search && name && strcmp(_gr_group.gr_name, name))
|
|
|
|
return 0;
|
|
|
|
_gr_group.gr_passwd = strsep(&bp, ":\n");
|
|
|
|
if (!(cp = strsep(&bp, ":\n")))
|
|
|
|
return 0;
|
|
|
|
id = strtoul(cp, &ep, 10);
|
|
|
|
if (id > GID_MAX || *ep != '\0')
|
|
|
|
return 0;
|
|
|
|
_gr_group.gr_gid = (gid_t)id;
|
|
|
|
if (search && name == NULL && _gr_group.gr_gid != gid)
|
|
|
|
return 0;
|
|
|
|
cp = NULL;
|
|
|
|
if (bp == NULL)
|
|
|
|
return 0;
|
|
|
|
for (_gr_group.gr_mem = m = members;; bp++) {
|
|
|
|
if (m == &members[MAXGRP - 1])
|
|
|
|
break;
|
|
|
|
if (*bp == ',') {
|
|
|
|
if (cp) {
|
|
|
|
*bp = '\0';
|
|
|
|
*m++ = cp;
|
|
|
|
cp = NULL;
|
|
|
|
}
|
|
|
|
} else if (*bp == '\0' || *bp == '\n' || *bp == ' ') {
|
|
|
|
if (cp) {
|
|
|
|
*bp = '\0';
|
|
|
|
*m++ = cp;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} else if (cp == NULL)
|
|
|
|
cp = bp;
|
|
|
|
}
|
|
|
|
*m = NULL;
|
2003-02-16 04:22:44 +03:00
|
|
|
if (user) {
|
|
|
|
for (m = members; *m; m++)
|
|
|
|
if (!strcmp(user, *m))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
1999-01-16 10:47:18 +03:00
|
|
|
return 1;
|
|
|
|
}
|