xref: /freebsd/contrib/netbsd-tests/lib/libc/gen/t_getgrent.c (revision 9268022b74279434ed6300244e3f977e56a8ceb5)
1*57718be8SEnji Cooper /*	$NetBSD: t_getgrent.c,v 1.2 2011/05/11 19:06:45 njoly Exp $ */
2*57718be8SEnji Cooper 
3*57718be8SEnji Cooper /*-
4*57718be8SEnji Cooper  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5*57718be8SEnji Cooper  * All rights reserved.
6*57718be8SEnji Cooper  *
7*57718be8SEnji Cooper  * This code is derived from software contributed to The NetBSD Foundation
8*57718be8SEnji Cooper  * by Jukka Ruohonen.
9*57718be8SEnji Cooper  *
10*57718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
11*57718be8SEnji Cooper  * modification, are permitted provided that the following conditions
12*57718be8SEnji Cooper  * are met:
13*57718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
14*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
15*57718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
16*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
17*57718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
18*57718be8SEnji Cooper  *
19*57718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*57718be8SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*57718be8SEnji Cooper  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*57718be8SEnji Cooper  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*57718be8SEnji Cooper  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*57718be8SEnji Cooper  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*57718be8SEnji Cooper  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*57718be8SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*57718be8SEnji Cooper  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*57718be8SEnji Cooper  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*57718be8SEnji Cooper  * POSSIBILITY OF SUCH DAMAGE.
30*57718be8SEnji Cooper  */
31*57718be8SEnji Cooper 
32*57718be8SEnji Cooper /*
33*57718be8SEnji Cooper  * Copyright (c) 2009, Stathis Kamperis
34*57718be8SEnji Cooper  * All rights reserved.
35*57718be8SEnji Cooper  *
36*57718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
37*57718be8SEnji Cooper  * modification, are permitted provided that the following conditions
38*57718be8SEnji Cooper  * are met:
39*57718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
40*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
41*57718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
42*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
43*57718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
44*57718be8SEnji Cooper  *
45*57718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
46*57718be8SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
47*57718be8SEnji Cooper  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
48*57718be8SEnji Cooper  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
49*57718be8SEnji Cooper  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
50*57718be8SEnji Cooper  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
51*57718be8SEnji Cooper  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
52*57718be8SEnji Cooper  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
53*57718be8SEnji Cooper  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
54*57718be8SEnji Cooper  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
55*57718be8SEnji Cooper  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56*57718be8SEnji Cooper  * SUCH DAMAGE.
57*57718be8SEnji Cooper  */
58*57718be8SEnji Cooper #include <sys/cdefs.h>
59*57718be8SEnji Cooper __RCSID("$NetBSD: t_getgrent.c,v 1.2 2011/05/11 19:06:45 njoly Exp $");
60*57718be8SEnji Cooper 
61*57718be8SEnji Cooper #include <sys/wait.h>
62*57718be8SEnji Cooper 
63*57718be8SEnji Cooper #include <atf-c.h>
64*57718be8SEnji Cooper #include <grp.h>
65*57718be8SEnji Cooper #include <stdlib.h>
66*57718be8SEnji Cooper #include <unistd.h>
67*57718be8SEnji Cooper 
68*57718be8SEnji Cooper ATF_TC(getgrent_loop);
ATF_TC_HEAD(getgrent_loop,tc)69*57718be8SEnji Cooper ATF_TC_HEAD(getgrent_loop, tc)
70*57718be8SEnji Cooper {
71*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test sequential getgrent(2)");
72*57718be8SEnji Cooper }
73*57718be8SEnji Cooper 
ATF_TC_BODY(getgrent_loop,tc)74*57718be8SEnji Cooper ATF_TC_BODY(getgrent_loop, tc)
75*57718be8SEnji Cooper {
76*57718be8SEnji Cooper 	struct group *gr;
77*57718be8SEnji Cooper 	size_t i, j;
78*57718be8SEnji Cooper 
79*57718be8SEnji Cooper 	/*
80*57718be8SEnji Cooper 	 * Loop over the group database. The first
81*57718be8SEnji Cooper 	 * call returns the first entry and subsequent
82*57718be8SEnji Cooper 	 * calls return the rest of the entries.
83*57718be8SEnji Cooper 	 */
84*57718be8SEnji Cooper 	i = j = 0;
85*57718be8SEnji Cooper 
86*57718be8SEnji Cooper 	while((gr = getgrent()) != NULL)
87*57718be8SEnji Cooper 		i++;
88*57718be8SEnji Cooper 
89*57718be8SEnji Cooper 	/*
90*57718be8SEnji Cooper 	 * Rewind the database to the beginning
91*57718be8SEnji Cooper 	 * and loop over again until the end.
92*57718be8SEnji Cooper 	 */
93*57718be8SEnji Cooper 	setgrent();
94*57718be8SEnji Cooper 
95*57718be8SEnji Cooper         while((gr = getgrent()) != NULL)
96*57718be8SEnji Cooper 		j++;
97*57718be8SEnji Cooper 
98*57718be8SEnji Cooper 	if (i != j)
99*57718be8SEnji Cooper 		atf_tc_fail("sequential getgrent(3) failed");
100*57718be8SEnji Cooper 
101*57718be8SEnji Cooper 	/*
102*57718be8SEnji Cooper 	 * Close the database and reopen it.
103*57718be8SEnji Cooper 	 * The getgrent(3) call should always
104*57718be8SEnji Cooper 	 * automatically rewind the database.
105*57718be8SEnji Cooper 	 */
106*57718be8SEnji Cooper 	endgrent();
107*57718be8SEnji Cooper 
108*57718be8SEnji Cooper         j = 0;
109*57718be8SEnji Cooper 
110*57718be8SEnji Cooper         while((gr = getgrent()) != NULL)
111*57718be8SEnji Cooper                 j++;
112*57718be8SEnji Cooper 
113*57718be8SEnji Cooper 	if (i != j)
114*57718be8SEnji Cooper 		atf_tc_fail("getgrent(3) did not rewind");
115*57718be8SEnji Cooper }
116*57718be8SEnji Cooper 
117*57718be8SEnji Cooper ATF_TC(getgrent_setgid);
ATF_TC_HEAD(getgrent_setgid,tc)118*57718be8SEnji Cooper ATF_TC_HEAD(getgrent_setgid, tc)
119*57718be8SEnji Cooper {
120*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test consistency of the group db");
121*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "require.user", "root");
122*57718be8SEnji Cooper }
123*57718be8SEnji Cooper 
ATF_TC_BODY(getgrent_setgid,tc)124*57718be8SEnji Cooper ATF_TC_BODY(getgrent_setgid, tc)
125*57718be8SEnji Cooper {
126*57718be8SEnji Cooper 	struct group *gr, *gr1, *gr2;
127*57718be8SEnji Cooper 	int rv, sta;
128*57718be8SEnji Cooper 	pid_t pid;
129*57718be8SEnji Cooper 
130*57718be8SEnji Cooper 	/*
131*57718be8SEnji Cooper 	 * Verify that the database is consistent.
132*57718be8SEnji Cooper 	 *
133*57718be8SEnji Cooper 	 * Note that because of the static buffers
134*57718be8SEnji Cooper 	 * used by getgrent(3), fork(2) is required,
135*57718be8SEnji Cooper 	 * even without the setgid(2) check.
136*57718be8SEnji Cooper 	 */
137*57718be8SEnji Cooper         while((gr = getgrent()) != NULL) {
138*57718be8SEnji Cooper 
139*57718be8SEnji Cooper 		pid = fork();
140*57718be8SEnji Cooper 		ATF_REQUIRE(pid >= 0);
141*57718be8SEnji Cooper 
142*57718be8SEnji Cooper 		if (pid == 0) {
143*57718be8SEnji Cooper 
144*57718be8SEnji Cooper 			gr1 = getgrgid(gr->gr_gid);
145*57718be8SEnji Cooper 
146*57718be8SEnji Cooper 			if (gr1 == NULL)
147*57718be8SEnji Cooper 				_exit(EXIT_FAILURE);
148*57718be8SEnji Cooper 
149*57718be8SEnji Cooper 			gr2 = getgrnam(gr->gr_name);
150*57718be8SEnji Cooper 
151*57718be8SEnji Cooper 			if (gr2 == NULL)
152*57718be8SEnji Cooper 				_exit(EXIT_FAILURE);
153*57718be8SEnji Cooper 
154*57718be8SEnji Cooper 			rv = setgid(gr->gr_gid);
155*57718be8SEnji Cooper 
156*57718be8SEnji Cooper 			if (rv != 0)
157*57718be8SEnji Cooper 				_exit(EXIT_FAILURE);
158*57718be8SEnji Cooper 
159*57718be8SEnji Cooper 			_exit(EXIT_SUCCESS);
160*57718be8SEnji Cooper 		}
161*57718be8SEnji Cooper 
162*57718be8SEnji Cooper 		(void)wait(&sta);
163*57718be8SEnji Cooper 
164*57718be8SEnji Cooper 		if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
165*57718be8SEnji Cooper 			goto fail;
166*57718be8SEnji Cooper 	}
167*57718be8SEnji Cooper 
168*57718be8SEnji Cooper 	return;
169*57718be8SEnji Cooper 
170*57718be8SEnji Cooper fail:
171*57718be8SEnji Cooper 	atf_tc_fail("group database is inconsistent");
172*57718be8SEnji Cooper }
173*57718be8SEnji Cooper 
ATF_TP_ADD_TCS(tp)174*57718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
175*57718be8SEnji Cooper {
176*57718be8SEnji Cooper 
177*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, getgrent_loop);
178*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, getgrent_setgid);
179*57718be8SEnji Cooper 
180*57718be8SEnji Cooper 	return atf_no_error();
181*57718be8SEnji Cooper }
182