blob: c015517d66c640e9d88ef53d85d0babca5d9923f [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080020 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
plars865695b2001-08-27 22:15:12 +000022 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 *
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040032 * Author: William Roske
33 * Co-pilot: Dave Fenner
plars865695b2001-08-27 22:15:12 +000034 */
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040035
36/*
37 * Testcase to test the basic functionality of setreuid(2) system call.
38 */
plars865695b2001-08-27 22:15:12 +000039
40#include <errno.h>
41#include <string.h>
42#include <signal.h>
plars865695b2001-08-27 22:15:12 +000043#include <sys/types.h>
44
45#include "test.h"
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040046#include "compat_16.h"
plars865695b2001-08-27 22:15:12 +000047
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040048static void setup(void);
49static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000050
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040051TCID_DEFINE(setreuid01);
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020052int TST_TOTAL = 5;
plars865695b2001-08-27 22:15:12 +000053
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040054static uid_t ruid, euid; /* real and effective user ids */
plars865695b2001-08-27 22:15:12 +000055
subrata_modak56207ce2009-03-23 13:35:39 +000056int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000057{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020058 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020059 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +000060
Garrett Cooper45e285d2010-11-22 12:19:25 -080061 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080062 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000063
subrata_modak56207ce2009-03-23 13:35:39 +000064 setup();
plars865695b2001-08-27 22:15:12 +000065
subrata_modak56207ce2009-03-23 13:35:39 +000066 for (lc = 0; TEST_LOOPING(lc); lc++) {
plars865695b2001-08-27 22:15:12 +000067
Caspar Zhangd59a6592013-03-07 14:59:12 +080068 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000069
subrata_modak56207ce2009-03-23 13:35:39 +000070 /*
71 * TEST CASE:
72 * Don't change either real or effective uid
73 */
74 ruid = getuid(); /* get real uid */
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040075 UID16_CHECK(ruid, setreuid, cleanup);
76
subrata_modak56207ce2009-03-23 13:35:39 +000077 euid = geteuid(); /* get effective uid */
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040078 UID16_CHECK(euid, setreuid, cleanup);
plars865695b2001-08-27 22:15:12 +000079
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040080 TEST(SETREUID(cleanup, -1, -1));
plars865695b2001-08-27 22:15:12 +000081
subrata_modak56207ce2009-03-23 13:35:39 +000082 if (TEST_RETURN == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +000083 tst_resm(TFAIL,
84 "setreuid - Don't change either real or effective uid failed, errno=%d : %s",
85 TEST_ERRNO, strerror(TEST_ERRNO));
86 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +020087 tst_resm(TPASS,
88 "setreuid - Don't change either real or effective uid returned %ld",
89 TEST_RETURN);
subrata_modak56207ce2009-03-23 13:35:39 +000090 }
subrata_modakbdbaec52009-02-26 12:14:51 +000091
subrata_modak56207ce2009-03-23 13:35:39 +000092 /*
93 * TEST CASE:
94 * change effective to effective uid
95 */
subrata_modakbdbaec52009-02-26 12:14:51 +000096
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +040097 TEST(SETREUID(cleanup, -1, euid));
subrata_modakbdbaec52009-02-26 12:14:51 +000098
subrata_modak56207ce2009-03-23 13:35:39 +000099 if (TEST_RETURN == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000100 tst_resm(TFAIL,
101 "setreuid - change effective to effective uid failed, errno=%d : %s",
102 TEST_ERRNO, strerror(TEST_ERRNO));
103 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200104 tst_resm(TPASS,
105 "setreuid - change effective to effective uid returned %ld",
106 TEST_RETURN);
subrata_modak56207ce2009-03-23 13:35:39 +0000107 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000108
subrata_modak56207ce2009-03-23 13:35:39 +0000109 /*
110 * TEST CASE:
111 * change real to real uid
112 */
subrata_modakbdbaec52009-02-26 12:14:51 +0000113
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +0400114 TEST(SETREUID(cleanup, ruid, -1));
subrata_modakbdbaec52009-02-26 12:14:51 +0000115
subrata_modak56207ce2009-03-23 13:35:39 +0000116 if (TEST_RETURN == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000117 tst_resm(TFAIL,
118 "setreuid - change real to real uid failed, errno=%d : %s",
119 TEST_ERRNO, strerror(TEST_ERRNO));
120 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200121 tst_resm(TPASS,
122 "setreuid - change real to real uid returned %ld",
123 TEST_RETURN);
subrata_modak56207ce2009-03-23 13:35:39 +0000124 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000125
subrata_modak56207ce2009-03-23 13:35:39 +0000126 /*
127 * TEST CASE:
128 * change effective to real uid
129 */
subrata_modakbdbaec52009-02-26 12:14:51 +0000130
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +0400131 TEST(SETREUID(cleanup, -1, ruid));
subrata_modakbdbaec52009-02-26 12:14:51 +0000132
subrata_modak56207ce2009-03-23 13:35:39 +0000133 if (TEST_RETURN == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000134 tst_resm(TFAIL,
135 "setreuid - change effective to real uid failed, errno=%d : %s",
136 TEST_ERRNO, strerror(TEST_ERRNO));
137 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200138 tst_resm(TPASS,
139 "setreuid - change effective to real uid returned %ld",
140 TEST_RETURN);
subrata_modak56207ce2009-03-23 13:35:39 +0000141 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000142
subrata_modak56207ce2009-03-23 13:35:39 +0000143 /*
144 * TEST CASE:
145 * try to change real to current real
146 */
subrata_modakbdbaec52009-02-26 12:14:51 +0000147
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +0400148 TEST(SETREUID(cleanup, ruid, ruid));
subrata_modakbdbaec52009-02-26 12:14:51 +0000149
subrata_modak56207ce2009-03-23 13:35:39 +0000150 if (TEST_RETURN == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000151 tst_resm(TFAIL,
152 "setreuid - try to change real to current real failed, errno=%d : %s",
153 TEST_ERRNO, strerror(TEST_ERRNO));
154 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200155 tst_resm(TPASS,
156 "setreuid - try to change real to current real returned %ld",
157 TEST_RETURN);
subrata_modak56207ce2009-03-23 13:35:39 +0000158 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000159
Garrett Cooper2c282152010-12-16 00:55:50 -0800160 }
plars865695b2001-08-27 22:15:12 +0000161
subrata_modak56207ce2009-03-23 13:35:39 +0000162 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800163 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800164}
plars865695b2001-08-27 22:15:12 +0000165
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +0400166static void setup(void)
plars865695b2001-08-27 22:15:12 +0000167{
subrata_modak56207ce2009-03-23 13:35:39 +0000168 tst_sig(NOFORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000169
subrata_modak56207ce2009-03-23 13:35:39 +0000170 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000171
subrata_modak56207ce2009-03-23 13:35:39 +0000172 tst_tmpdir();
Garrett Cooper2c282152010-12-16 00:55:50 -0800173}
plars865695b2001-08-27 22:15:12 +0000174
Stanislav Kholmanskikhe0f85852013-10-21 11:25:15 +0400175static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000176{
subrata_modak56207ce2009-03-23 13:35:39 +0000177 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700178}