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