1272343Sngie/*	$NetBSD: t_getgrent.c,v 1.2 2011/05/11 19:06:45 njoly Exp $ */
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2011 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * This code is derived from software contributed to The NetBSD Foundation
8272343Sngie * by Jukka Ruohonen.
9272343Sngie *
10272343Sngie * Redistribution and use in source and binary forms, with or without
11272343Sngie * modification, are permitted provided that the following conditions
12272343Sngie * are met:
13272343Sngie * 1. Redistributions of source code must retain the above copyright
14272343Sngie *    notice, this list of conditions and the following disclaimer.
15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
16272343Sngie *    notice, this list of conditions and the following disclaimer in the
17272343Sngie *    documentation and/or other materials provided with the distribution.
18272343Sngie *
19272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29272343Sngie * POSSIBILITY OF SUCH DAMAGE.
30272343Sngie */
31272343Sngie
32272343Sngie/*
33272343Sngie * Copyright (c) 2009, Stathis Kamperis
34272343Sngie * All rights reserved.
35272343Sngie *
36272343Sngie * Redistribution and use in source and binary forms, with or without
37272343Sngie * modification, are permitted provided that the following conditions
38272343Sngie * are met:
39272343Sngie * 1. Redistributions of source code must retain the above copyright
40272343Sngie *    notice, this list of conditions and the following disclaimer.
41272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
42272343Sngie *    notice, this list of conditions and the following disclaimer in the
43272343Sngie *    documentation and/or other materials provided with the distribution.
44272343Sngie *
45272343Sngie * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
46272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
47272343Sngie * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
48272343Sngie * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
49272343Sngie * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
50272343Sngie * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
51272343Sngie * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
52272343Sngie * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
53272343Sngie * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
54272343Sngie * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
55272343Sngie * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56272343Sngie * SUCH DAMAGE.
57272343Sngie */
58272343Sngie#include <sys/cdefs.h>
59272343Sngie__RCSID("$NetBSD: t_getgrent.c,v 1.2 2011/05/11 19:06:45 njoly Exp $");
60272343Sngie
61272343Sngie#include <sys/wait.h>
62272343Sngie
63272343Sngie#include <atf-c.h>
64272343Sngie#include <grp.h>
65272343Sngie#include <stdlib.h>
66272343Sngie#include <unistd.h>
67272343Sngie
68272343SngieATF_TC(getgrent_loop);
69272343SngieATF_TC_HEAD(getgrent_loop, tc)
70272343Sngie{
71272343Sngie	atf_tc_set_md_var(tc, "descr", "Test sequential getgrent(2)");
72272343Sngie}
73272343Sngie
74272343SngieATF_TC_BODY(getgrent_loop, tc)
75272343Sngie{
76272343Sngie	struct group *gr;
77272343Sngie	size_t i, j;
78272343Sngie
79272343Sngie	/*
80272343Sngie	 * Loop over the group database. The first
81272343Sngie	 * call returns the first entry and subsequent
82272343Sngie	 * calls return the rest of the entries.
83272343Sngie	 */
84272343Sngie	i = j = 0;
85272343Sngie
86272343Sngie	while((gr = getgrent()) != NULL)
87272343Sngie		i++;
88272343Sngie
89272343Sngie	/*
90272343Sngie	 * Rewind the database to the beginning
91272343Sngie	 * and loop over again until the end.
92272343Sngie	 */
93272343Sngie	setgrent();
94272343Sngie
95272343Sngie        while((gr = getgrent()) != NULL)
96272343Sngie		j++;
97272343Sngie
98272343Sngie	if (i != j)
99272343Sngie		atf_tc_fail("sequential getgrent(3) failed");
100272343Sngie
101272343Sngie	/*
102272343Sngie	 * Close the database and reopen it.
103272343Sngie	 * The getgrent(3) call should always
104272343Sngie	 * automatically rewind the database.
105272343Sngie	 */
106272343Sngie	endgrent();
107272343Sngie
108272343Sngie        j = 0;
109272343Sngie
110272343Sngie        while((gr = getgrent()) != NULL)
111272343Sngie                j++;
112272343Sngie
113272343Sngie	if (i != j)
114272343Sngie		atf_tc_fail("getgrent(3) did not rewind");
115272343Sngie}
116272343Sngie
117272343SngieATF_TC(getgrent_setgid);
118272343SngieATF_TC_HEAD(getgrent_setgid, tc)
119272343Sngie{
120272343Sngie	atf_tc_set_md_var(tc, "descr", "Test consistency of the group db");
121272343Sngie	atf_tc_set_md_var(tc, "require.user", "root");
122272343Sngie}
123272343Sngie
124272343SngieATF_TC_BODY(getgrent_setgid, tc)
125272343Sngie{
126272343Sngie	struct group *gr, *gr1, *gr2;
127272343Sngie	int rv, sta;
128272343Sngie	pid_t pid;
129272343Sngie
130272343Sngie	/*
131272343Sngie	 * Verify that the database is consistent.
132272343Sngie	 *
133272343Sngie	 * Note that because of the static buffers
134272343Sngie	 * used by getgrent(3), fork(2) is required,
135272343Sngie	 * even without the setgid(2) check.
136272343Sngie	 */
137272343Sngie        while((gr = getgrent()) != NULL) {
138272343Sngie
139272343Sngie		pid = fork();
140272343Sngie		ATF_REQUIRE(pid >= 0);
141272343Sngie
142272343Sngie		if (pid == 0) {
143272343Sngie
144272343Sngie			gr1 = getgrgid(gr->gr_gid);
145272343Sngie
146272343Sngie			if (gr1 == NULL)
147272343Sngie				_exit(EXIT_FAILURE);
148272343Sngie
149272343Sngie			gr2 = getgrnam(gr->gr_name);
150272343Sngie
151272343Sngie			if (gr2 == NULL)
152272343Sngie				_exit(EXIT_FAILURE);
153272343Sngie
154272343Sngie			rv = setgid(gr->gr_gid);
155272343Sngie
156272343Sngie			if (rv != 0)
157272343Sngie				_exit(EXIT_FAILURE);
158272343Sngie
159272343Sngie			_exit(EXIT_SUCCESS);
160272343Sngie		}
161272343Sngie
162272343Sngie		(void)wait(&sta);
163272343Sngie
164272343Sngie		if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
165272343Sngie			goto fail;
166272343Sngie	}
167272343Sngie
168272343Sngie	return;
169272343Sngie
170272343Sngiefail:
171272343Sngie	atf_tc_fail("group database is inconsistent");
172272343Sngie}
173272343Sngie
174272343SngieATF_TP_ADD_TCS(tp)
175272343Sngie{
176272343Sngie
177272343Sngie	ATF_TP_ADD_TC(tp, getgrent_loop);
178272343Sngie	ATF_TP_ADD_TC(tp, getgrent_setgid);
179272343Sngie
180272343Sngie	return atf_no_error();
181272343Sngie}
182