plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
| 3 | * Ported by Wayne Boyer |
| 4 | * Copyright (c) Cyril Hrubis <chrubis@suse.cz> 2013 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 5 | * |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 10 | * |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 14 | * the GNU General Public License for more details. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 15 | * |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 21 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 22 | * Test Description: |
| 23 | * Verify that, getgroups() system call gets the supplementary group IDs |
| 24 | * of the calling process. |
| 25 | * |
| 26 | * Expected Result: |
| 27 | * The call succeeds in getting all the supplementary group IDs of the |
| 28 | * calling process. The effective group ID may or may not be returned. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 29 | */ |
| 30 | |
| 31 | #include <stdio.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <unistd.h> |
| 34 | #include <errno.h> |
| 35 | #include <string.h> |
| 36 | #include <signal.h> |
| 37 | #include <grp.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <sys/param.h> |
robbiew | 05e6175 | 2001-08-31 18:11:58 +0000 | [diff] [blame] | 40 | #include <pwd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 41 | |
| 42 | #include "test.h" |
Stanislav Kholmanskikh | 0f3ac8c | 2013-09-05 11:01:49 +0400 | [diff] [blame] | 43 | #include "compat_16.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 45 | #define TESTUSER "root" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 46 | |
Stanislav Kholmanskikh | 15f7303 | 2013-09-17 14:55:01 +0400 | [diff] [blame] | 47 | TCID_DEFINE(getgroups03); |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 48 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 49 | |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 50 | static int ngroups; |
Stanislav Kholmanskikh | 0f3ac8c | 2013-09-05 11:01:49 +0400 | [diff] [blame] | 51 | static GID_T groups_list[NGROUPS]; |
| 52 | static GID_T groups[NGROUPS]; |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 53 | |
| 54 | static void verify_groups(int ret_ngroups); |
| 55 | static void setup(void); |
| 56 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 58 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 60 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 61 | const char *msg; |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 62 | int gidsetsize = NGROUPS; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 63 | |
Garrett Cooper | ebf7df2 | 2010-12-19 01:52:15 -0800 | [diff] [blame] | 64 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 66 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | setup(); |
| 68 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 70 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 71 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | |
Stanislav Kholmanskikh | 0f3ac8c | 2013-09-05 11:01:49 +0400 | [diff] [blame] | 73 | TEST(GETGROUPS(cleanup, gidsetsize, groups_list)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 74 | |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 75 | if (TEST_RETURN == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 76 | tst_resm(TFAIL | TTERRNO, "getgroups failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | continue; |
| 78 | } |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 79 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 80 | verify_groups(TEST_RETURN); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 81 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 84 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 85 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | /* |
Stanislav Kholmanskikh | 0f3ac8c | 2013-09-05 11:01:49 +0400 | [diff] [blame] | 88 | * readgroups(GID_T *) - Read supplimentary group ids of "root" user |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | * Scans the /etc/group file to get IDs of all the groups to which TESTUSER |
| 90 | * belongs and puts them into the array passed. |
| 91 | * Returns the no of gids read. |
| 92 | */ |
Stanislav Kholmanskikh | 0f3ac8c | 2013-09-05 11:01:49 +0400 | [diff] [blame] | 93 | static int readgroups(GID_T groups[NGROUPS]) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | { |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 95 | struct group *grp; |
| 96 | int ngrps = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | int i; |
robbiew | b1cae06 | 2003-04-29 14:20:11 +0000 | [diff] [blame] | 98 | int found; |
Stanislav Kholmanskikh | 0f3ac8c | 2013-09-05 11:01:49 +0400 | [diff] [blame] | 99 | GID_T g; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 100 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | setgrent(); |
| 102 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 103 | while ((grp = getgrent()) != 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | for (i = 0; grp->gr_mem[i]; i++) { |
| 105 | if (strcmp(grp->gr_mem[i], TESTUSER) == 0) { |
| 106 | groups[ngrps++] = grp->gr_gid; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
robbiew | b1cae06 | 2003-04-29 14:20:11 +0000 | [diff] [blame] | 111 | /* The getgroups specification says: |
| 112 | It is unspecified whether the effective group ID of the |
| 113 | calling process is included in the returned list. (Thus, |
| 114 | an application should also call getegid(2) and add or |
| 115 | remove the resulting value.). So, add the value here if |
| 116 | it's not in. */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 117 | |
robbiew | b1cae06 | 2003-04-29 14:20:11 +0000 | [diff] [blame] | 118 | found = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 119 | g = getegid(); |
robbiew | b1cae06 | 2003-04-29 14:20:11 +0000 | [diff] [blame] | 120 | |
| 121 | for (i = 0; i < ngrps; i++) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 122 | if (groups[i] == g) |
| 123 | found = 1; |
robbiew | b1cae06 | 2003-04-29 14:20:11 +0000 | [diff] [blame] | 124 | } |
| 125 | if (found == 0) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 126 | groups[ngrps++] = g; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 127 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | endgrent(); |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 129 | return ngrps; |
| 130 | } |
| 131 | |
| 132 | static void setup(void) |
| 133 | { |
| 134 | tst_require_root(NULL); |
| 135 | |
| 136 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 137 | |
| 138 | TEST_PAUSE; |
| 139 | |
| 140 | /* |
| 141 | * Get the IDs of all the groups of "root" |
| 142 | * from /etc/group file |
| 143 | */ |
| 144 | ngroups = readgroups(groups); |
| 145 | |
| 146 | /* Setgroups is called by the login(1) process |
| 147 | * if the testcase is executed via an ssh session this |
| 148 | * testcase will fail. So execute setgroups() before executing |
| 149 | * getgroups() |
| 150 | */ |
Stanislav Kholmanskikh | 0f3ac8c | 2013-09-05 11:01:49 +0400 | [diff] [blame] | 151 | if (SETGROUPS(cleanup, ngroups, groups) == -1) |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 152 | tst_brkm(TBROK | TERRNO, cleanup, "setgroups failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /* |
| 156 | * verify_groups(int) - Verify supplimentary group id values. |
| 157 | * This function verifies the gid values returned by getgroups() with |
| 158 | * the read values from /etc/group file. |
| 159 | * This function returns flag value which indicates success or failure |
| 160 | * of verification. |
| 161 | */ |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 162 | static void verify_groups(int ret_ngroups) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | { |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 164 | int i, j; |
Stanislav Kholmanskikh | 0f3ac8c | 2013-09-05 11:01:49 +0400 | [diff] [blame] | 165 | GID_T egid; |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 166 | int egid_flag = 1; |
| 167 | int fflag = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 168 | |
| 169 | /* |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 170 | * Loop through the array to verify the gids |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | * returned by getgroups(). |
| 172 | * First, compare each element of the array |
| 173 | * returned by getgroups() with that read from |
| 174 | * group file. |
| 175 | */ |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 176 | for (i = 0; i < ret_ngroups; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 177 | for (j = 0; j < ngroups; j++) { |
| 178 | if (groups_list[i] != groups[j]) { |
| 179 | /* If loop ends and gids are not matching */ |
| 180 | if (j == ngroups - 1) { |
Garrett Cooper | ebf7df2 | 2010-12-19 01:52:15 -0800 | [diff] [blame] | 181 | tst_resm(TFAIL, "getgroups returned " |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | "incorrect gid %d", |
| 183 | groups_list[i]); |
| 184 | fflag = 0; |
| 185 | } else { |
| 186 | continue; |
| 187 | } |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 188 | } else { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 189 | break; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /* Now do the reverse comparison */ |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 195 | egid = getegid(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 196 | for (i = 0; i < ngroups; i++) { |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 197 | for (j = 0; j < ret_ngroups; j++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 198 | if (groups[i] != groups_list[j]) { |
| 199 | /* |
| 200 | * If the loop ends & gids are not matching |
| 201 | * if gid is not egid, exit with error |
| 202 | * else egid is returned by getgroups() |
| 203 | */ |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 204 | if (j == (ret_ngroups - 1)) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 205 | if (groups[i] != egid) { |
Garrett Cooper | ebf7df2 | 2010-12-19 01:52:15 -0800 | [diff] [blame] | 206 | tst_resm(TFAIL, "getgroups " |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 207 | "didn't return %d one " |
| 208 | "of the gids of %s", |
| 209 | groups[i], TESTUSER); |
| 210 | fflag = 0; |
| 211 | } else { |
| 212 | /* |
| 213 | * egid is not present in |
| 214 | * group_list. |
| 215 | * Reset the egid flag |
| 216 | */ |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 217 | egid_flag = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 218 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 219 | } |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 220 | } else { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 221 | break; |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * getgroups() should return the no. of gids for TESTUSER with |
| 228 | * or without egid taken into account. |
| 229 | * Decrement ngroups, if egid is not returned by getgroups() |
| 230 | * Now, if ngroups matches ret_val, as above comparisons of the array |
| 231 | * are successful, this implies that the array contents match. |
| 232 | */ |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 233 | if (egid_flag == 0) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 234 | ngroups--; |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 235 | if (ngroups != ret_ngroups) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 236 | tst_resm(TFAIL, |
Garrett Cooper | ebf7df2 | 2010-12-19 01:52:15 -0800 | [diff] [blame] | 237 | "getgroups(2) returned incorrect no. of gids %d " |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 238 | "(expected %d)", ret_ngroups, ngroups); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 239 | fflag = 0; |
| 240 | } |
| 241 | |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 242 | if (fflag) |
| 243 | tst_resm(TPASS, "getgroups functionality correct"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 244 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 245 | |
Cyril Hrubis | c515be2 | 2013-04-04 16:46:39 +0200 | [diff] [blame] | 246 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 247 | { |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 248 | } |