1 /*
2  *
3  *   Copyright (c) Wipro Technologies, 2002.  All Rights Reserved.
4  *
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.
9  *
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.
14  *
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
18  */
19 
20 /****************************************************************************
21  *    TEST IDENTIFIER	: sethostname03
22  *
23  *    EXECUTED BY	: root / superuser
24  *
25  *    TEST TITLE	: test for EPERM error value when sethostname(2) is
26  *                        called from as non superuser
27  *
28  *    TEST CASE TOTAL	: 1
29  *
30  *    AUTHOR		: Suresh Babu V. <suresh.babu@wipro.com>
31  *
32  *    SIGNALS
33  *      Uses SIGUSR1 to pause before test if option set.
34  *      (See the parse_opts(3) man page).
35  *
36  *    DESCRIPTION
37  *      Verify that, sethostname(2) returns -1 and sets errno to EPERM
38  *      if the effective userid id of the caller is not super-user.
39  *
40  *  Setup:
41  *   Setup signal handling.
42  *   Save the current host name.
43  *   Pause for SIGUSR1 if option specified.
44  *
45  *  Test:
46  *   Loop if the proper options are given.
47  *   Execute system call
48  *   Check return code, if system call failed (return=-1)
49  *	if errno set == expected errno
50  *		Issue sys call fails with expected return value and errno.
51  *	Otherwise,
52  *		Issue sys call fails with unexpected errno.
53  *
54  *  Cleanup:
55  *   Restore old hostname
56  *   Print errno log and/or timing stats if options given
57  *
58  * Usage:  <for command-line>
59  *  sethostname03 [-c n] [-e] [-i n] [-I x] [-P x] [-p] [-t]
60  *	where,  -c n : Run n copies concurrently.
61  *		-e   : Turn on errno logging.
62  *		-i n : Execute test n times.
63  *		-I x : Execute test for x seconds.
64  *		-P x : Pause for x seconds between iterations.
65  *		-p   : Pause for SIGUSR1 before starting
66  *		-t   : Turn on syscall timing.
67  *
68  *************************************************************************/
69 
70 #include <stdio.h>
71 #include <string.h>
72 #include <errno.h>
73 #include <pwd.h>
74 #include <linux/utsname.h>
75 
76 #include "test.h"
77 
78 #define MAX_LENGTH __NEW_UTS_LEN
79 
80 char *TCID = "sethostname03";
81 int TST_TOTAL = 1;
82 
83 static char ltpthost[] = "ltphost";
84 static char hname[MAX_LENGTH];
85 
86 static char nobody_uid[] = "nobody";
87 struct passwd *ltpuser;
88 
89 static void setup(void);
90 static void cleanup(void);
91 
main(int ac,char ** av)92 int main(int ac, char **av)
93 {
94 	int lc;
95 
96 	tst_parse_opts(ac, av, NULL, NULL);
97 
98 	/* Do initial setup */
99 	setup();
100 
101 	/* Check for looping state if -i option is given */
102 	for (lc = 0; TEST_LOOPING(lc); lc++) {
103 
104 		tst_count = 0;
105 
106 		/* call sethostname() */
107 		TEST(sethostname(ltpthost, sizeof(ltpthost)));
108 
109 		if ((TEST_RETURN == -1) && (TEST_ERRNO == EPERM)) {
110 			tst_resm(TPASS, "Expected Failure; Got EPERM");
111 		} else {
112 			tst_resm(TFAIL, "call failed to produce "
113 				 "expected error;  errno: %d : %s",
114 				 TEST_ERRNO, strerror(TEST_ERRNO));
115 		}
116 
117 	}
118 
119 	/* cleanup and exit */
120 	cleanup();
121 	tst_exit();
122 
123 }
124 
125 /*
126  * setup() - performs all one time setup for this test.
127  */
setup(void)128 void setup(void)
129 {
130 	int ret;
131 
132 	tst_require_root();
133 
134 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
135 
136 	/* Switch to nobody user for correct error code collection */
137 	if ((ltpuser = getpwnam(nobody_uid)) == NULL) {
138 		tst_brkm(TBROK, NULL, "Required user \"nobody\" not"
139 			 " present");
140 	}
141 
142 	if (seteuid(ltpuser->pw_uid) == -1) {
143 		tst_resm(TWARN, "seteuid failed to "
144 			 "to set the effective uid to %d", ltpuser->pw_uid);
145 		perror("seteuid");
146 	}
147 
148 	/* Keep current hostname */
149 	if ((ret = gethostname(hname, sizeof(hname))) < 0) {
150 		tst_brkm(TBROK, NULL, "gethostname() failed while"
151 			 " getting current host name");
152 	}
153 
154 	TEST_PAUSE;
155 
156 }
157 
158 /*
159  * cleanup()  - performs all one time cleanup for this test
160  *		completion or premature exit.
161  */
cleanup(void)162 void cleanup(void)
163 {
164 	int ret;
165 
166 	/* Set effective user id back to root/super user */
167 	if (seteuid(0) == -1) {
168 		tst_resm(TWARN, "seteuid failed to "
169 			 "to set the effective uid to root");
170 		perror("seteuid");
171 	}
172 
173 	/* Restore host name */
174 	if ((ret = sethostname(hname, strlen(hname))) < 0) {
175 		tst_resm(TWARN, "sethostname() failed while restoring"
176 			 " hostname to \"%s\"", hname);
177 	}
178 
179 }
180