plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
| 3 | * Ported by Wayne Boyer |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 4 | * |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 9 | * |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 14 | * |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 21 | * ALGORITHM |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 22 | * As root sets the current group id to ltpuser1, verify the results |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | */ |
| 24 | #include <pwd.h> |
| 25 | #include <errno.h> |
Stanislav Kholmanskikh | 1c44244 | 2013-09-11 13:25:43 +0400 | [diff] [blame] | 26 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 27 | #include "test.h" |
Stanislav Kholmanskikh | 1c44244 | 2013-09-11 13:25:43 +0400 | [diff] [blame] | 28 | #include <compat_16.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 29 | |
subrata_modak | 585950c | 2008-08-20 10:55:19 +0000 | [diff] [blame] | 30 | TCID_DEFINE(setgid03); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 31 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 32 | |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 33 | static char ltpuser1[] = "nobody"; |
| 34 | static char root[] = "root"; |
Vinson Lee | 3a7395c | 2013-09-12 13:24:39 -0700 | [diff] [blame] | 35 | static struct passwd *ltpuser1pwent, *rootpwent; |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 36 | static gid_t mygid; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 37 | |
Vinson Lee | 3a7395c | 2013-09-12 13:24:39 -0700 | [diff] [blame] | 38 | static void setup(void); |
| 39 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 40 | |
robbiew | 8541eeb | 2003-03-27 19:59:32 +0000 | [diff] [blame] | 41 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 42 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 43 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 44 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 46 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 47 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 48 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 49 | setup(); |
| 50 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 52 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | |
Stanislav Kholmanskikh | 1c44244 | 2013-09-11 13:25:43 +0400 | [diff] [blame] | 54 | TEST(SETGID(cleanup, ltpuser1pwent->pw_gid)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | |
| 56 | if (TEST_RETURN == -1) { |
| 57 | tst_resm(TFAIL, "call failed unexpectedly"); |
| 58 | continue; |
| 59 | } |
| 60 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | if (getgid() != ltpuser1pwent->pw_gid) { |
| 62 | tst_resm(TFAIL, "setgid failed to set gid to " |
| 63 | "ltpuser1's gid"); |
| 64 | } else { |
| 65 | tst_resm(TPASS, "functionality of getgid() is correct"); |
| 66 | } |
| 67 | } |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 68 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 70 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 73 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | { |
Stanislav Kholmanskikh | 1c44244 | 2013-09-11 13:25:43 +0400 | [diff] [blame] | 75 | tst_require_root(NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 78 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | TEST_PAUSE; |
| 80 | |
| 81 | if ((rootpwent = getpwnam(root)) == NULL) { |
| 82 | tst_brkm(TBROK, cleanup, "getpwnam failed for " |
| 83 | "user id %s", root); |
| 84 | } |
| 85 | |
| 86 | mygid = getgid(); |
| 87 | |
| 88 | if (mygid != rootpwent->pw_gid) { |
| 89 | tst_brkm(TBROK, cleanup, "real group id is not root"); |
| 90 | } |
| 91 | |
| 92 | if ((ltpuser1pwent = getpwnam(ltpuser1)) == NULL) { |
| 93 | tst_brkm(TBROK, cleanup, "getpwnam failed for user " |
| 94 | "id %s", ltpuser1); |
| 95 | } |
subrata_modak | da53b08 | 2008-08-04 12:06:21 +0000 | [diff] [blame] | 96 | |
Stanislav Kholmanskikh | 1c44244 | 2013-09-11 13:25:43 +0400 | [diff] [blame] | 97 | GID16_CHECK(ltpuser1pwent->pw_gid, setgid, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Cyril Hrubis | 7dc42c9 | 2013-09-12 14:55:55 +0200 | [diff] [blame] | 100 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | { |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 102 | } |