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