plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | 0022e57 | 2014-06-04 16:21:25 +0200 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
| 3 | * 07/2001 Ported by Wayne Boyer |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 4 | * |
Cyril Hrubis | 0022e57 | 2014-06-04 16:21:25 +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 | 0022e57 | 2014-06-04 16:21:25 +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 | 0022e57 | 2014-06-04 16:21:25 +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 | */ |
vapier | 37cf54a | 2007-04-13 20:58:17 +0000 | [diff] [blame] | 19 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 20 | * Test Description: |
| 21 | * Verify that, fchown(2) succeeds to change the owner and group of a file |
vapier | 37cf54a | 2007-04-13 20:58:17 +0000 | [diff] [blame] | 22 | * specified by file descriptor to any numeric owner(uid)/group(gid) values |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | * when invoked by super-user. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <sys/fcntl.h> |
| 30 | #include <errno.h> |
| 31 | #include <string.h> |
| 32 | #include <signal.h> |
| 33 | |
| 34 | #include "test.h" |
Cyril Hrubis | 0022e57 | 2014-06-04 16:21:25 +0200 | [diff] [blame] | 35 | #include "safe_macros.h" |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 36 | #include "compat_16.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 37 | |
| 38 | #define FILE_MODE S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH |
| 39 | #define TESTFILE "testfile" |
| 40 | |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 41 | TCID_DEFINE(fchown05); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 42 | |
Cyril Hrubis | 0022e57 | 2014-06-04 16:21:25 +0200 | [diff] [blame] | 43 | static struct test_case_t { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | char *desc; |
| 45 | uid_t user_id; |
| 46 | gid_t group_id; |
Cyril Hrubis | 0022e57 | 2014-06-04 16:21:25 +0200 | [diff] [blame] | 47 | } tc[] = { |
| 48 | {"Change Owner/Group ids", 700, 701}, |
| 49 | {"Change Owner id only", 702, -1}, |
| 50 | {"Change Owner id only", 703, 701}, |
| 51 | {"Change Group id only", -1, 704}, |
| 52 | {"Change Group id only", 703, 705}, |
| 53 | {NULL, 0, 0} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
Cyril Hrubis | 0022e57 | 2014-06-04 16:21:25 +0200 | [diff] [blame] | 56 | int TST_TOTAL = ARRAY_SIZE(tc); |
| 57 | |
| 58 | static void setup(void); |
| 59 | static void cleanup(void); |
| 60 | static int fildes; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | |
vapier | 37cf54a | 2007-04-13 20:58:17 +0000 | [diff] [blame] | 62 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | { |
Cyril Hrubis | 0022e57 | 2014-06-04 16:21:25 +0200 | [diff] [blame] | 64 | struct stat stat_buf; |
| 65 | int i, lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 66 | const char *msg; |
Cyril Hrubis | 0022e57 | 2014-06-04 16:21:25 +0200 | [diff] [blame] | 67 | uid_t user_id; |
| 68 | gid_t group_id; |
vapier | 37cf54a | 2007-04-13 20:58:17 +0000 | [diff] [blame] | 69 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 70 | msg = parse_opts(ac, av, NULL, NULL); |
Cyril Hrubis | 0022e57 | 2014-06-04 16:21:25 +0200 | [diff] [blame] | 71 | if (msg != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 73 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | setup(); |
| 75 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 77 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | |
Cyril Hrubis | 0022e57 | 2014-06-04 16:21:25 +0200 | [diff] [blame] | 79 | for (i = 0; tc[i].desc != NULL; i++) { |
| 80 | user_id = tc[i].user_id; |
| 81 | group_id = tc[i].group_id; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 83 | TEST(FCHOWN(cleanup, fildes, user_id, group_id)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | if (TEST_RETURN == -1) { |
Cyril Hrubis | 0022e57 | 2014-06-04 16:21:25 +0200 | [diff] [blame] | 86 | tst_resm(TFAIL | TTERRNO, |
| 87 | "fchown() Fails to %s", tc[i].desc); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | continue; |
| 89 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | |
Cyril Hrubis | 0022e57 | 2014-06-04 16:21:25 +0200 | [diff] [blame] | 91 | SAFE_FSTAT(cleanup, fildes, &stat_buf); |
| 92 | |
| 93 | if (user_id == (uid_t)-1) |
| 94 | user_id = tc[i - 1].user_id; |
| 95 | |
| 96 | if (group_id == (gid_t)-1) |
| 97 | group_id = tc[i - 1].group_id; |
| 98 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 99 | if ((stat_buf.st_uid != user_id) || |
| 100 | (stat_buf.st_gid != group_id)) { |
| 101 | tst_resm(TFAIL, "%s: Incorrect owner" |
| 102 | "ship set, Expected %d %d", |
| 103 | TESTFILE, user_id, group_id); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 105 | tst_resm(TPASS, |
| 106 | "fchown() succeeds to %s of %s", |
Cyril Hrubis | 0022e57 | 2014-06-04 16:21:25 +0200 | [diff] [blame] | 107 | tc[i].desc, TESTFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 110 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | cleanup(); |
Cyril Hrubis | 0022e57 | 2014-06-04 16:21:25 +0200 | [diff] [blame] | 113 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 114 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | |
Cyril Hrubis | 0022e57 | 2014-06-04 16:21:25 +0200 | [diff] [blame] | 116 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 119 | |
Cyril Hrubis | 0022e57 | 2014-06-04 16:21:25 +0200 | [diff] [blame] | 120 | tst_require_root(NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | TEST_PAUSE; |
vapier | 37cf54a | 2007-04-13 20:58:17 +0000 | [diff] [blame] | 123 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | tst_tmpdir(); |
| 125 | |
Cyril Hrubis | 0022e57 | 2014-06-04 16:21:25 +0200 | [diff] [blame] | 126 | fildes = SAFE_OPEN(cleanup, TESTFILE, O_RDWR | O_CREAT, FILE_MODE); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 127 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | |
Cyril Hrubis | 0022e57 | 2014-06-04 16:21:25 +0200 | [diff] [blame] | 129 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | { |
Cyril Hrubis | 0022e57 | 2014-06-04 16:21:25 +0200 | [diff] [blame] | 131 | if (fildes > 0 && close(fildes)) |
| 132 | tst_resm(TWARN | TERRNO, "close(%s) Failed", TESTFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 135 | } |