122347Spst/*	$NetBSD$	*/
222347Spst
322347Spst/*
429967Sache * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
559121Skris * Copyright (c) 1996-1999 by Internet Software Consortium.
622347Spst *
722347Spst * Permission to use, copy, modify, and distribute this software for any
822347Spst * purpose with or without fee is hereby granted, provided that the above
922347Spst * copyright notice and this permission notice appear in all copies.
1022347Spst *
1122347Spst * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
1222347Spst * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1322347Spst * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
1422347Spst * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1522347Spst * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1622347Spst * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
1722347Spst * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1859121Skris */
1959121Skris
2029967Sache#if !defined(LINT) && !defined(CODECENTER)
2122347Spststatic const char rcsid[] = "Id: getgrent.c,v 1.5 2005/04/27 04:56:24 sra Exp ";
2222347Spst#endif
2322347Spst
2422347Spst/* Imports */
2522347Spst
2622347Spst#include "port_before.h"
2722347Spst
2822347Spst#if !defined(WANT_IRS_GR) || defined(__BIND_NOSTATIC)
2922347Spststatic int __bind_irs_gr_unneeded;
3022347Spst#else
3122347Spst
3222347Spst#include <sys/types.h>
3322347Spst
3422347Spst#include <netinet/in.h>
3522347Spst#include <arpa/nameser.h>
3659121Skris
3759121Skris#include <errno.h>
3822347Spst#include <grp.h>
3922347Spst#include <resolv.h>
4029967Sache#include <stdio.h>
4122347Spst#include <string.h>
4222347Spst#include <unistd.h>
4322347Spst
4422347Spst#include <irs.h>
4522347Spst
4622347Spst#include "port_after.h"
4722347Spst
4822347Spst#include "irs_data.h"
4922347Spst
5022347Spst/* Forward */
5122347Spst
5222347Spststatic struct net_data *init(void);
5322347Spstvoid			endgrent(void);
5422347Spst
5522347Spst/* Public */
5622347Spst
5722347Spststruct group *
5822347Spstgetgrent() {
5922347Spst	struct net_data *net_data = init();
6022347Spst
6122347Spst	return (getgrent_p(net_data));
6222347Spst}
6322347Spst
6422347Spststruct group *
6522347Spstgetgrnam(const char *name) {
6622347Spst	struct net_data *net_data = init();
6722347Spst
6822347Spst	return (getgrnam_p(name, net_data));
6922347Spst}
7022347Spst
7122347Spststruct group *
7222347Spstgetgrgid(gid_t gid) {
7322347Spst	struct net_data *net_data = init();
7422347Spst
7522347Spst	return (getgrgid_p(gid, net_data));
7629967Sache}
7722347Spst
7823540Spstint
7922347Spstsetgroupent(int stayopen) {
8022347Spst	struct net_data *net_data = init();
8122347Spst
8222347Spst	return (setgroupent_p(stayopen, net_data));
8322347Spst}
8423540Spst
8522347Spst#ifdef SETGRENT_VOID
8622347Spstvoid
8722347Spstsetgrent(void) {
8822347Spst	struct net_data *net_data = init();
8922347Spst
9022347Spst	setgrent_p(net_data);
9122347Spst}
9223540Spst#else
9322347Spstint
9422347Spstsetgrent(void) {
9522347Spst	struct net_data *net_data = init();
9659300Skris
9759300Skris	return (setgrent_p(net_data));
9859300Skris}
9922347Spst#endif /* SETGRENT_VOID */
10022347Spst
10123540Spstvoid
10222347Spstendgrent() {
10323540Spst	struct net_data *net_data = init();
10422347Spst
10529967Sache	endgrent_p(net_data);
10622347Spst}
10722347Spst
10823540Spstint
10922347Spstgetgrouplist(GETGROUPLIST_ARGS) {
11029967Sache	struct net_data *net_data = init();
11129967Sache
11229967Sache	return (getgrouplist_p(name, basegid, groups, ngroups, net_data));
11329967Sache}
11429967Sache
11529967Sache/* Shared private. */
11629967Sache
11729967Sachestruct group *
11823540Spstgetgrent_p(struct net_data *net_data) {
11923540Spst	struct irs_gr *gr;
12022347Spst
12129967Sache	if (!net_data || !(gr = net_data->gr))
12222347Spst		return (NULL);
12322347Spst	net_data->gr_last = (*gr->next)(gr);
12429967Sache	return (net_data->gr_last);
12529967Sache}
12629967Sache
12759121Skrisstruct group *
12829972Sachegetgrnam_p(const char *name, struct net_data *net_data) {
12929967Sache	struct irs_gr *gr;
13059121Skris
13159121Skris	if (!net_data || !(gr = net_data->gr))
13259121Skris		return (NULL);
13359121Skris	if (net_data->gr_stayopen && net_data->gr_last &&
13422347Spst	    !strcmp(net_data->gr_last->gr_name, name))
135		return (net_data->gr_last);
136	net_data->gr_last = (*gr->byname)(gr, name);
137	if (!net_data->gr_stayopen)
138		endgrent();
139	return (net_data->gr_last);
140}
141
142struct group *
143getgrgid_p(gid_t gid, struct net_data *net_data) {
144	struct irs_gr *gr;
145
146	if (!net_data || !(gr = net_data->gr))
147		return (NULL);
148	if (net_data->gr_stayopen && net_data->gr_last &&
149	    (gid_t)net_data->gr_last->gr_gid == gid)
150		return (net_data->gr_last);
151	net_data->gr_last = (*gr->bygid)(gr, gid);
152	if (!net_data->gr_stayopen)
153		endgrent();
154	return (net_data->gr_last);
155}
156
157int
158setgroupent_p(int stayopen, struct net_data *net_data) {
159	struct irs_gr *gr;
160
161	if (!net_data || !(gr = net_data->gr))
162		return (0);
163	(*gr->rewind)(gr);
164	net_data->gr_stayopen = (stayopen != 0);
165	if (stayopen == 0)
166		net_data_minimize(net_data);
167	return (1);
168}
169
170#ifdef SETGRENT_VOID
171void
172setgrent_p(struct net_data *net_data) {
173	(void)setgroupent_p(0, net_data);
174}
175#else
176int
177setgrent_p(struct net_data *net_data) {
178	return (setgroupent_p(0, net_data));
179}
180#endif /* SETGRENT_VOID */
181
182void
183endgrent_p(struct net_data *net_data) {
184	struct irs_gr *gr;
185
186	if ((net_data != NULL) && ((gr = net_data->gr) != NULL))
187		(*gr->minimize)(gr);
188}
189
190int
191getgrouplist_p(const char *name, gid_t basegid, gid_t *groups, int *ngroups,
192	       struct net_data *net_data) {
193	struct irs_gr *gr;
194
195	if (!net_data || !(gr = net_data->gr)) {
196		*ngroups = 0;
197		return (-1);
198	}
199	return ((*gr->list)(gr, name, basegid, groups, ngroups));
200}
201
202/* Private */
203
204static struct net_data *
205init() {
206	struct net_data *net_data;
207
208	if (!(net_data = net_data_init(NULL)))
209		goto error;
210	if (!net_data->gr) {
211		net_data->gr = (*net_data->irs->gr_map)(net_data->irs);
212
213		if (!net_data->gr || !net_data->res) {
214 error:
215			errno = EIO;
216			return (NULL);
217		}
218		(*net_data->gr->res_set)(net_data->gr, net_data->res,
219					 NULL);
220	}
221
222	return (net_data);
223}
224
225#endif /* WANT_IRS_GR */
226/*! \file */
227