plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Stanislav Kholmanskikh | a801ded | 2014-01-13 18:13:56 +0400 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 3 | * |
Stanislav Kholmanskikh | a801ded | 2014-01-13 18:13:56 +0400 | [diff] [blame] | 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 8 | * |
Stanislav Kholmanskikh | a801ded | 2014-01-13 18:13:56 +0400 | [diff] [blame] | 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 12 | * the GNU General Public License for more details. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 13 | * |
Stanislav Kholmanskikh | a801ded | 2014-01-13 18:13:56 +0400 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 20 | * Test Description: |
| 21 | * Verify that, lchown(2) succeeds to change the owner and group of a file |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 22 | * specified by path to any numeric owner(uid)/group(gid) values when invoked |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | * by super-user. |
| 24 | * |
| 25 | * Expected Result: |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 26 | * lchown(2) should return 0 and the ownership set on the file should match |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 27 | * the numeric values contained in owner and group respectively. |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 28 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 29 | * HISTORY |
| 30 | * 07/2001 Ported by Wayne Boyer |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 31 | * 11/2010 Code cleanup by Cyril Hrubis chrubis@suse.cz |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 32 | */ |
| 33 | |
| 34 | #include <stdio.h> |
| 35 | #include <sys/types.h> |
| 36 | #include <sys/stat.h> |
| 37 | #include <sys/fcntl.h> |
| 38 | #include <errno.h> |
| 39 | #include <string.h> |
| 40 | #include <signal.h> |
| 41 | |
| 42 | #include "test.h" |
Stanislav Kholmanskikh | a801ded | 2014-01-13 18:13:56 +0400 | [diff] [blame] | 43 | #include "compat_16.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | |
Garrett Cooper | 7ec1c89 | 2010-12-13 20:54:23 -0800 | [diff] [blame] | 45 | #define FILE_MODE (S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 46 | #define TESTFILE "testfile" |
| 47 | #define SFILE "slink_file" |
| 48 | |
Stanislav Kholmanskikh | a801ded | 2014-01-13 18:13:56 +0400 | [diff] [blame] | 49 | TCID_DEFINE(lchown01); |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 50 | int TST_TOTAL = 5; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 52 | struct test_case_t { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | char *desc; |
| 54 | uid_t user_id; |
| 55 | gid_t group_id; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 58 | static struct test_case_t test_cases[] = { |
| 59 | {"Change Owner/Group ids", 700, 701}, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 60 | {"Change Owner id only", 702, -1}, |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 61 | {"Change Owner/Group ids", 703, 701}, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 62 | {"Change Group id only", -1, 704}, |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 63 | {"Change Group/Group ids", 703, 705}, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 64 | {"Change none", -1, -1}, |
| 65 | {NULL, 0, 0} |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 66 | }; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | |
Stanislav Kholmanskikh | a801ded | 2014-01-13 18:13:56 +0400 | [diff] [blame] | 68 | static void setup(void); |
| 69 | static void cleanup(void); |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 70 | |
| 71 | int main(int argc, char *argv[]) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | { |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 73 | struct stat stat_buf; |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 74 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 75 | const char *msg; |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 76 | int i; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 77 | |
Garrett Cooper | e1f008e | 2010-12-14 00:21:59 -0800 | [diff] [blame] | 78 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | setup(); |
| 82 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 84 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 85 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 87 | for (i = 0; test_cases[i].desc != NULL; i++) { |
| 88 | uid_t user_id = test_cases[i].user_id; |
| 89 | gid_t group_id = test_cases[i].group_id; |
| 90 | char *test_desc = test_cases[i].desc; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 92 | /* |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 93 | * Call lchown(2) with different user id and |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | * group id (numeric values) to set it on |
| 95 | * symlink of testfile. |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 96 | */ |
Stanislav Kholmanskikh | a801ded | 2014-01-13 18:13:56 +0400 | [diff] [blame] | 97 | TEST(LCHOWN(cleanup, SFILE, user_id, group_id)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | if (TEST_RETURN == -1) { |
| 100 | tst_resm(TFAIL, |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 101 | "lchown() Fails to %s, errno %d", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | test_desc, TEST_ERRNO); |
| 103 | continue; |
| 104 | } |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 105 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 106 | if (lstat(SFILE, &stat_buf) < 0) { |
| 107 | tst_brkm(TFAIL, cleanup, "lstat(2) " |
| 108 | "%s failed, errno %d", |
| 109 | SFILE, TEST_ERRNO); |
| 110 | } |
| 111 | |
| 112 | if (user_id == -1) { |
| 113 | if (i > 0) |
| 114 | user_id = |
| 115 | test_cases[i - 1].user_id; |
| 116 | else |
| 117 | user_id = geteuid(); |
| 118 | } |
| 119 | |
| 120 | if (group_id == -1) { |
| 121 | if (i > 0) |
| 122 | group_id = |
| 123 | test_cases[i - 1].group_id; |
| 124 | else |
| 125 | group_id = getegid(); |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Check for expected Ownership ids |
| 130 | * set on testfile. |
| 131 | */ |
| 132 | if ((stat_buf.st_uid != user_id) || |
| 133 | (stat_buf.st_gid != group_id)) { |
| 134 | tst_resm(TFAIL, |
| 135 | "%s: incorrect ownership set, " |
| 136 | "Expected %d %d", SFILE, |
| 137 | user_id, group_id); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 139 | tst_resm(TPASS, "lchown() succeeds to " |
| 140 | "%s of %s", test_desc, SFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 143 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | cleanup(); |
Garrett Cooper | 5ded27d | 2010-12-19 07:35:08 -0800 | [diff] [blame] | 146 | tst_exit(); |
Garrett Cooper | a99df35 | 2010-12-10 09:02:53 -0800 | [diff] [blame] | 147 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | |
Stanislav Kholmanskikh | a801ded | 2014-01-13 18:13:56 +0400 | [diff] [blame] | 149 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | { |
| 151 | int fd; |
| 152 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 154 | |
Garrett Cooper | 43088e1 | 2010-12-13 23:30:59 -0800 | [diff] [blame] | 155 | tst_require_root(NULL); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 156 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | tst_tmpdir(); |
| 159 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 160 | if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) { |
Garrett Cooper | 55535b8 | 2010-12-16 16:35:27 -0800 | [diff] [blame] | 161 | tst_brkm(TBROK, cleanup, "open failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | } |
| 163 | if (close(fd) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 164 | tst_brkm(TBROK | TERRNO, cleanup, "close failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 165 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 166 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 167 | if (symlink(TESTFILE, SFILE) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 168 | tst_brkm(TBROK | TERRNO, cleanup, "symlink failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Stanislav Kholmanskikh | a801ded | 2014-01-13 18:13:56 +0400 | [diff] [blame] | 172 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 174 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 175 | } |