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