blob: 261ceb99a3fa7b63413166044c7ca64e54a6ce43 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Stanislav Kholmanskikha801ded2014-01-13 18:13:56 +04002 * Copyright (c) International Business Machines Corp., 2001
plars865695b2001-08-27 22:15:12 +00003 *
Stanislav Kholmanskikha801ded2014-01-13 18:13:56 +04004 * 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.
plars865695b2001-08-27 22:15:12 +00008 *
Stanislav Kholmanskikha801ded2014-01-13 18:13:56 +04009 * 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.
plars865695b2001-08-27 22:15:12 +000013 *
Stanislav Kholmanskikha801ded2014-01-13 18:13:56 +040014 * 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
plars865695b2001-08-27 22:15:12 +000017 */
18
19/*
plars865695b2001-08-27 22:15:12 +000020 * Test Description:
21 * Verify that, lchown(2) succeeds to change the owner and group of a file
subrata_modak4bb656a2009-02-26 12:02:09 +000022 * specified by path to any numeric owner(uid)/group(gid) values when invoked
plars865695b2001-08-27 22:15:12 +000023 * by super-user.
24 *
25 * Expected Result:
subrata_modak4bb656a2009-02-26 12:02:09 +000026 * lchown(2) should return 0 and the ownership set on the file should match
plars865695b2001-08-27 22:15:12 +000027 * the numeric values contained in owner and group respectively.
subrata_modakbdbaec52009-02-26 12:14:51 +000028 *
plars865695b2001-08-27 22:15:12 +000029 * HISTORY
30 * 07/2001 Ported by Wayne Boyer
Garrett Coopera99df352010-12-10 09:02:53 -080031 * 11/2010 Code cleanup by Cyril Hrubis chrubis@suse.cz
plars865695b2001-08-27 22:15:12 +000032 */
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 Kholmanskikha801ded2014-01-13 18:13:56 +040043#include "compat_16.h"
plars865695b2001-08-27 22:15:12 +000044
Garrett Cooper7ec1c892010-12-13 20:54:23 -080045#define FILE_MODE (S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
plars865695b2001-08-27 22:15:12 +000046#define TESTFILE "testfile"
47#define SFILE "slink_file"
48
Stanislav Kholmanskikha801ded2014-01-13 18:13:56 +040049TCID_DEFINE(lchown01);
Garrett Coopera99df352010-12-10 09:02:53 -080050int TST_TOTAL = 5;
plars865695b2001-08-27 22:15:12 +000051
Garrett Coopera99df352010-12-10 09:02:53 -080052struct test_case_t {
plars865695b2001-08-27 22:15:12 +000053 char *desc;
54 uid_t user_id;
55 gid_t group_id;
plars865695b2001-08-27 22:15:12 +000056};
57
Garrett Coopera99df352010-12-10 09:02:53 -080058static struct test_case_t test_cases[] = {
59 {"Change Owner/Group ids", 700, 701},
Wanlong Gao354ebb42012-12-07 10:10:04 +080060 {"Change Owner id only", 702, -1},
Garrett Coopera99df352010-12-10 09:02:53 -080061 {"Change Owner/Group ids", 703, 701},
Wanlong Gao354ebb42012-12-07 10:10:04 +080062 {"Change Group id only", -1, 704},
Garrett Coopera99df352010-12-10 09:02:53 -080063 {"Change Group/Group ids", 703, 705},
Wanlong Gao354ebb42012-12-07 10:10:04 +080064 {"Change none", -1, -1},
65 {NULL, 0, 0}
Garrett Coopera99df352010-12-10 09:02:53 -080066};
plars865695b2001-08-27 22:15:12 +000067
Stanislav Kholmanskikha801ded2014-01-13 18:13:56 +040068static void setup(void);
69static void cleanup(void);
Garrett Coopera99df352010-12-10 09:02:53 -080070
71int main(int argc, char *argv[])
plars865695b2001-08-27 22:15:12 +000072{
Garrett Coopera99df352010-12-10 09:02:53 -080073 struct stat stat_buf;
Garrett Cooper43088e12010-12-13 23:30:59 -080074 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020075 const char *msg;
Garrett Cooper43088e12010-12-13 23:30:59 -080076 int i;
subrata_modak56207ce2009-03-23 13:35:39 +000077
Garrett Coopere1f008e2010-12-14 00:21:59 -080078 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +000079 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000080
plars865695b2001-08-27 22:15:12 +000081 setup();
82
plars865695b2001-08-27 22:15:12 +000083 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -080084
Caspar Zhangd59a6592013-03-07 14:59:12 +080085 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000086
Garrett Coopera99df352010-12-10 09:02:53 -080087 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;
plars865695b2001-08-27 22:15:12 +000091
subrata_modak4bb656a2009-02-26 12:02:09 +000092 /*
Garrett Cooper43088e12010-12-13 23:30:59 -080093 * Call lchown(2) with different user id and
plars865695b2001-08-27 22:15:12 +000094 * group id (numeric values) to set it on
95 * symlink of testfile.
subrata_modak56207ce2009-03-23 13:35:39 +000096 */
Stanislav Kholmanskikha801ded2014-01-13 18:13:56 +040097 TEST(LCHOWN(cleanup, SFILE, user_id, group_id));
plars865695b2001-08-27 22:15:12 +000098
plars865695b2001-08-27 22:15:12 +000099 if (TEST_RETURN == -1) {
100 tst_resm(TFAIL,
Garrett Coopera99df352010-12-10 09:02:53 -0800101 "lchown() Fails to %s, errno %d",
plars865695b2001-08-27 22:15:12 +0000102 test_desc, TEST_ERRNO);
103 continue;
104 }
Garrett Coopera99df352010-12-10 09:02:53 -0800105
Cyril Hrubise38b9612014-06-02 17:20:57 +0200106 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);
plars865695b2001-08-27 22:15:12 +0000138 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200139 tst_resm(TPASS, "lchown() succeeds to "
140 "%s of %s", test_desc, SFILE);
plars865695b2001-08-27 22:15:12 +0000141 }
142 }
Garrett Coopera99df352010-12-10 09:02:53 -0800143 }
plars865695b2001-08-27 22:15:12 +0000144
plars865695b2001-08-27 22:15:12 +0000145 cleanup();
Garrett Cooper5ded27d2010-12-19 07:35:08 -0800146 tst_exit();
Garrett Coopera99df352010-12-10 09:02:53 -0800147}
plars865695b2001-08-27 22:15:12 +0000148
Stanislav Kholmanskikha801ded2014-01-13 18:13:56 +0400149static void setup(void)
plars865695b2001-08-27 22:15:12 +0000150{
151 int fd;
152
plars865695b2001-08-27 22:15:12 +0000153 tst_sig(NOFORK, DEF_HANDLER, cleanup);
Garrett Cooper2c282152010-12-16 00:55:50 -0800154
Garrett Cooper43088e12010-12-13 23:30:59 -0800155 tst_require_root(NULL);
Garrett Cooper2c282152010-12-16 00:55:50 -0800156
plars865695b2001-08-27 22:15:12 +0000157 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000158 tst_tmpdir();
159
Wanlong Gao354ebb42012-12-07 10:10:04 +0800160 if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
Garrett Cooper55535b82010-12-16 16:35:27 -0800161 tst_brkm(TBROK, cleanup, "open failed");
plars865695b2001-08-27 22:15:12 +0000162 }
163 if (close(fd) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800164 tst_brkm(TBROK | TERRNO, cleanup, "close failed");
plars865695b2001-08-27 22:15:12 +0000165 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800166
plars865695b2001-08-27 22:15:12 +0000167 if (symlink(TESTFILE, SFILE) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800168 tst_brkm(TBROK | TERRNO, cleanup, "symlink failed");
plars865695b2001-08-27 22:15:12 +0000169 }
170}
171
Stanislav Kholmanskikha801ded2014-01-13 18:13:56 +0400172static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000173{
plars865695b2001-08-27 22:15:12 +0000174 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700175}