blob: f2107be5d18db706257f25204495530a6f4d9922 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Cyril Hrubis0022e572014-06-04 16:21:25 +02002 * Copyright (c) International Business Machines Corp., 2001
3 * 07/2001 Ported by Wayne Boyer
plars865695b2001-08-27 22:15:12 +00004 *
Cyril Hrubis0022e572014-06-04 16:21:25 +02005 * 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.
plars865695b2001-08-27 22:15:12 +00009 *
Cyril Hrubis0022e572014-06-04 16:21:25 +020010 * 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.
plars865695b2001-08-27 22:15:12 +000014 *
Cyril Hrubis0022e572014-06-04 16:21:25 +020015 * 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
plars865695b2001-08-27 22:15:12 +000018 */
vapier37cf54a2007-04-13 20:58:17 +000019/*
plars865695b2001-08-27 22:15:12 +000020 * Test Description:
21 * Verify that, fchown(2) succeeds to change the owner and group of a file
vapier37cf54a2007-04-13 20:58:17 +000022 * specified by file descriptor to any numeric owner(uid)/group(gid) values
plars865695b2001-08-27 22:15:12 +000023 * when invoked by super-user.
plars865695b2001-08-27 22:15:12 +000024 */
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 Hrubis0022e572014-06-04 16:21:25 +020035#include "safe_macros.h"
Han Pingtian75fb0572014-11-28 16:33:41 +080036#include "compat_16.h"
plars865695b2001-08-27 22:15:12 +000037
38#define FILE_MODE S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
39#define TESTFILE "testfile"
40
Han Pingtian75fb0572014-11-28 16:33:41 +080041TCID_DEFINE(fchown05);
plars865695b2001-08-27 22:15:12 +000042
Cyril Hrubis0022e572014-06-04 16:21:25 +020043static struct test_case_t {
plars865695b2001-08-27 22:15:12 +000044 char *desc;
45 uid_t user_id;
46 gid_t group_id;
Cyril Hrubis0022e572014-06-04 16:21:25 +020047} 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}
plars865695b2001-08-27 22:15:12 +000054};
55
Cyril Hrubis0022e572014-06-04 16:21:25 +020056int TST_TOTAL = ARRAY_SIZE(tc);
57
58static void setup(void);
59static void cleanup(void);
60static int fildes;
plars865695b2001-08-27 22:15:12 +000061
vapier37cf54a2007-04-13 20:58:17 +000062int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000063{
Cyril Hrubis0022e572014-06-04 16:21:25 +020064 struct stat stat_buf;
65 int i, lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020066 const char *msg;
Cyril Hrubis0022e572014-06-04 16:21:25 +020067 uid_t user_id;
68 gid_t group_id;
vapier37cf54a2007-04-13 20:58:17 +000069
Garrett Cooper45e285d2010-11-22 12:19:25 -080070 msg = parse_opts(ac, av, NULL, NULL);
Cyril Hrubis0022e572014-06-04 16:21:25 +020071 if (msg != NULL)
plars865695b2001-08-27 22:15:12 +000072 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -080073
plars865695b2001-08-27 22:15:12 +000074 setup();
75
plars865695b2001-08-27 22:15:12 +000076 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080077 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000078
Cyril Hrubis0022e572014-06-04 16:21:25 +020079 for (i = 0; tc[i].desc != NULL; i++) {
80 user_id = tc[i].user_id;
81 group_id = tc[i].group_id;
plars865695b2001-08-27 22:15:12 +000082
Han Pingtian75fb0572014-11-28 16:33:41 +080083 TEST(FCHOWN(cleanup, fildes, user_id, group_id));
plars865695b2001-08-27 22:15:12 +000084
plars865695b2001-08-27 22:15:12 +000085 if (TEST_RETURN == -1) {
Cyril Hrubis0022e572014-06-04 16:21:25 +020086 tst_resm(TFAIL | TTERRNO,
87 "fchown() Fails to %s", tc[i].desc);
plars865695b2001-08-27 22:15:12 +000088 continue;
89 }
plars865695b2001-08-27 22:15:12 +000090
Cyril Hrubis0022e572014-06-04 16:21:25 +020091 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 Hrubise38b9612014-06-02 17:20:57 +020099 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);
plars865695b2001-08-27 22:15:12 +0000104 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200105 tst_resm(TPASS,
106 "fchown() succeeds to %s of %s",
Cyril Hrubis0022e572014-06-04 16:21:25 +0200107 tc[i].desc, TESTFILE);
plars865695b2001-08-27 22:15:12 +0000108 }
109 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800110 }
plars865695b2001-08-27 22:15:12 +0000111
plars865695b2001-08-27 22:15:12 +0000112 cleanup();
Cyril Hrubis0022e572014-06-04 16:21:25 +0200113 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800114}
plars865695b2001-08-27 22:15:12 +0000115
Cyril Hrubis0022e572014-06-04 16:21:25 +0200116static void setup(void)
plars865695b2001-08-27 22:15:12 +0000117{
plars865695b2001-08-27 22:15:12 +0000118 tst_sig(NOFORK, DEF_HANDLER, cleanup);
119
Cyril Hrubis0022e572014-06-04 16:21:25 +0200120 tst_require_root(NULL);
plars865695b2001-08-27 22:15:12 +0000121
plars865695b2001-08-27 22:15:12 +0000122 TEST_PAUSE;
vapier37cf54a2007-04-13 20:58:17 +0000123
plars865695b2001-08-27 22:15:12 +0000124 tst_tmpdir();
125
Cyril Hrubis0022e572014-06-04 16:21:25 +0200126 fildes = SAFE_OPEN(cleanup, TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
Garrett Cooper2c282152010-12-16 00:55:50 -0800127}
plars865695b2001-08-27 22:15:12 +0000128
Cyril Hrubis0022e572014-06-04 16:21:25 +0200129static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000130{
Cyril Hrubis0022e572014-06-04 16:21:25 +0200131 if (fildes > 0 && close(fildes))
132 tst_resm(TWARN | TERRNO, "close(%s) Failed", TESTFILE);
plars865695b2001-08-27 22:15:12 +0000133
plars865695b2001-08-27 22:15:12 +0000134 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700135}