blob: 86205144bf9f644e27a0b7702a67c4ebf550e962 [file] [log] [blame]
plarsd758be32002-11-01 15:59:31 +00001/*
2 * Copyright (c) Wipro Technologies Ltd, 2002. 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 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080014 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plarsd758be32002-11-01 15:59:31 +000015 *
16 */
subrata_modak4bb656a2009-02-26 12:02:09 +000017/**********************************************************
plarsd758be32002-11-01 15:59:31 +000018 *
19 * TEST IDENTIFIER : setdomainname03
20 *
21 * EXECUTED BY : root / superuser
22 *
23 * TEST TITLE : test for EPERM error value when run as non superuser
24 *
25 * TEST CASE TOTAL : 1
26 *
27 * AUTHOR : Saji Kumar.V.R <saji.kumar@wipro.com>
28 *
29 * SIGNALS
30 * Uses SIGUSR1 to pause before test if option set.
31 * (See the parse_opts(3) man page).
32 *
subrata_modak4bb656a2009-02-26 12:02:09 +000033 * DESCRIPTION
plarsd758be32002-11-01 15:59:31 +000034 * Verify that, setdomainname(2) returns -1 and sets errno to EPERM
35 * if the effective user id of the caller is not super-user.
36 *
37 * Algorithm:
38 * Setup:
39 * Setup signal handling.
40 * Pause for SIGUSR1 if option specified.
41 * save current domainname
42 * change effective user id to "nobody" user
43 *
44 * Test:
45 * Loop if the proper options are given.
46 * Execute system call
47 * Check return code, Check return code, if (system call failed (return=-1)) &
48 * (errno set == expected errno)
49 * Issue sys call fails with expected return value and errno.
50 * Otherwise,
51 * Issue sys call fails with unexpected errno.
52 * Otherwise,
53 * Issue sys call returns unexpected value.
54 *
55 * Cleanup:
56 * Change effective user id to root
57 * Restore old domainname
58 * Print errno log and/or timing stats if options given
59 *
60 * Usage: <for command-line>
61 * setdomainname03 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
62 * where, -c n : Run n copies concurrently.
63 * -e : Turn on errno logging.
64 * -h : Show help screen
65 * -f : Turn off functional testing
66 * -i n : Execute test n times.
67 * -I x : Execute test for x seconds.
68 * -p : Pause for SIGUSR1 before starting
69 * -P x : Pause for x seconds between iterations.
70 * -t : Turn on syscall timing.
71 *
72 ****************************************************************/
73
mridge3a6b4742004-09-13 21:55:16 +000074#include <string.h>
plarsd758be32002-11-01 15:59:31 +000075#include <errno.h>
76#include <pwd.h>
77#include <linux/utsname.h>
78
79#include "test.h"
plarsd758be32002-11-01 15:59:31 +000080
81#define MAX_NAME_LEN __NEW_UTS_LEN
82
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020083char *TCID = "setdomainname03";
84int TST_TOTAL = 1;
plarsd758be32002-11-01 15:59:31 +000085
86static char nobody_uid[] = "nobody";
87struct passwd *ltpuser;
88
subrata_modak56207ce2009-03-23 13:35:39 +000089static char test_domain_name[MAX_NAME_LEN] = "test_dom";
90static char old_domain_name[MAX_NAME_LEN];
plarsd758be32002-11-01 15:59:31 +000091
subrata_modak56207ce2009-03-23 13:35:39 +000092static void setup(); /* setup function for the tests */
93static void cleanup(); /* cleanup function for the tests */
plarsd758be32002-11-01 15:59:31 +000094
subrata_modak56207ce2009-03-23 13:35:39 +000095int main(int ac, char **av)
plarsd758be32002-11-01 15:59:31 +000096{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020097 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020098 const char *msg;
plarsd758be32002-11-01 15:59:31 +000099
Garrett Cooper45e285d2010-11-22 12:19:25 -0800100 msg = parse_opts(ac, av, NULL, NULL);
101 if (msg != NULL) {
plarsd758be32002-11-01 15:59:31 +0000102 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800103
plarsd758be32002-11-01 15:59:31 +0000104 }
105
106 /*
107 * Invoke setup function to call individual test setup functions
108 * for the test which run as root/super-user.
109 */
110 setup();
111
plarsd758be32002-11-01 15:59:31 +0000112 for (lc = 0; TEST_LOOPING(lc); lc++) {
113
Caspar Zhangd59a6592013-03-07 14:59:12 +0800114 tst_count = 0;
plarsd758be32002-11-01 15:59:31 +0000115
116 /*
117 * Call setdomainname(2)
118 */
119 TEST(setdomainname(test_domain_name, MAX_NAME_LEN));
120 if ((TEST_RETURN == -1) && (TEST_ERRNO == EPERM)) {
subrata_modak56207ce2009-03-23 13:35:39 +0000121 tst_resm(TPASS, "expected failure; Got EPERM");
plarsd758be32002-11-01 15:59:31 +0000122 } else {
123 tst_resm(TFAIL, "Call failed to produce "
subrata_modak56207ce2009-03-23 13:35:39 +0000124 "expected error; Expected errno: %d "
125 "Got : %d, %s", EPERM, TEST_ERRNO,
126 strerror(TEST_ERRNO));
plarsd758be32002-11-01 15:59:31 +0000127 }
plarsd758be32002-11-01 15:59:31 +0000128
Garrett Cooper2c282152010-12-16 00:55:50 -0800129 }
plarsd758be32002-11-01 15:59:31 +0000130
131 /*
132 * Invoke cleanup() to delete the test directories created
133 * in the setup().
134 */
135 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800136 tst_exit();
plarsd758be32002-11-01 15:59:31 +0000137
Garrett Cooper2c282152010-12-16 00:55:50 -0800138}
plarsd758be32002-11-01 15:59:31 +0000139
140/*
141 * setup(void) - performs all ONE TIME setup for this test.
142 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400143void setup(void)
plarsd758be32002-11-01 15:59:31 +0000144{
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200145 tst_require_root(NULL);
subrata_modakbdbaec52009-02-26 12:14:51 +0000146
plarsd758be32002-11-01 15:59:31 +0000147 /* Capture unexpected signals */
148 tst_sig(NOFORK, DEF_HANDLER, cleanup);
149
plarsd758be32002-11-01 15:59:31 +0000150 /* Switch to nobody user for correct error code collection */
subrata_modak56207ce2009-03-23 13:35:39 +0000151 if ((ltpuser = getpwnam(nobody_uid)) == NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -0800152 tst_brkm(TBROK, NULL, "\"nobody\" user not present");
plarsd758be32002-11-01 15:59:31 +0000153 }
154 if (seteuid(ltpuser->pw_uid) == -1) {
155 tst_resm(TWARN, "seteuid failed to "
subrata_modak56207ce2009-03-23 13:35:39 +0000156 "to set the effective uid to %d", ltpuser->pw_uid);
plarsd758be32002-11-01 15:59:31 +0000157 perror("seteuid");
158 }
159
160 /* Save current domainname */
subrata_modak56207ce2009-03-23 13:35:39 +0000161 if ((getdomainname(old_domain_name, MAX_NAME_LEN)) < 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800162 tst_brkm(TBROK, NULL, "getdomainname() failed while"
subrata_modak56207ce2009-03-23 13:35:39 +0000163 " getting current domain name");
plarsd758be32002-11-01 15:59:31 +0000164 }
165
plarsd758be32002-11-01 15:59:31 +0000166 TEST_PAUSE;
167
168}
169
170/*
171 * cleanup() - Performs all ONE TIME cleanup for this test at
172 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400173void cleanup(void)
plarsd758be32002-11-01 15:59:31 +0000174{
175
plarsd758be32002-11-01 15:59:31 +0000176 /* Set effective user id back to root */
177 if (seteuid(0) == -1) {
178 tst_resm(TWARN, "seteuid failed to "
subrata_modak56207ce2009-03-23 13:35:39 +0000179 "to set the effective uid to root");
plarsd758be32002-11-01 15:59:31 +0000180 perror("seteuid");
181 }
182
183 /* Restore domain name */
subrata_modak56207ce2009-03-23 13:35:39 +0000184 if ((setdomainname(old_domain_name, strlen(old_domain_name)))
185 < 0) {
plarsd758be32002-11-01 15:59:31 +0000186 tst_resm(TWARN, "setdomainname() failed while restoring"
subrata_modak56207ce2009-03-23 13:35:39 +0000187 " domainname to \"%s\"", old_domain_name);
plarsd758be32002-11-01 15:59:31 +0000188 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000189
Chris Dearmanec6edca2012-10-17 19:54:01 -0700190}