1 /* 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 14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 15 * 16 */ 17 /********************************************************** 18 * 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 * 33 * DESCRIPTION 34 * 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 74 #include <string.h> 75 #include <errno.h> 76 #include <pwd.h> 77 #include <linux/utsname.h> 78 79 #include "test.h" 80 81 #define MAX_NAME_LEN __NEW_UTS_LEN 82 83 char *TCID = "setdomainname03"; 84 int TST_TOTAL = 1; 85 86 static char nobody_uid[] = "nobody"; 87 struct passwd *ltpuser; 88 89 static char test_domain_name[MAX_NAME_LEN] = "test_dom"; 90 static char old_domain_name[MAX_NAME_LEN]; 91 92 static void setup(); /* setup function for the tests */ 93 static void cleanup(); /* cleanup function for the tests */ 94 95 int main(int ac, char **av) 96 { 97 int lc; 98 99 tst_parse_opts(ac, av, NULL, NULL); 100 101 /* 102 * Invoke setup function to call individual test setup functions 103 * for the test which run as root/super-user. 104 */ 105 setup(); 106 107 for (lc = 0; TEST_LOOPING(lc); lc++) { 108 109 tst_count = 0; 110 111 /* 112 * Call setdomainname(2) 113 */ 114 TEST(setdomainname(test_domain_name, MAX_NAME_LEN)); 115 if ((TEST_RETURN == -1) && (TEST_ERRNO == EPERM)) { 116 tst_resm(TPASS, "expected failure; Got EPERM"); 117 } else { 118 tst_resm(TFAIL, "Call failed to produce " 119 "expected error; Expected errno: %d " 120 "Got : %d, %s", EPERM, TEST_ERRNO, 121 strerror(TEST_ERRNO)); 122 } 123 124 } 125 126 /* 127 * Invoke cleanup() to delete the test directories created 128 * in the setup(). 129 */ 130 cleanup(); 131 tst_exit(); 132 133 } 134 135 /* 136 * setup(void) - performs all ONE TIME setup for this test. 137 */ 138 void setup(void) 139 { 140 tst_require_root(); 141 142 /* Capture unexpected signals */ 143 tst_sig(NOFORK, DEF_HANDLER, cleanup); 144 145 /* Switch to nobody user for correct error code collection */ 146 if ((ltpuser = getpwnam(nobody_uid)) == NULL) { 147 tst_brkm(TBROK, NULL, "\"nobody\" user not present"); 148 } 149 if (seteuid(ltpuser->pw_uid) == -1) { 150 tst_resm(TWARN, "seteuid failed to " 151 "to set the effective uid to %d", ltpuser->pw_uid); 152 perror("seteuid"); 153 } 154 155 /* Save current domainname */ 156 if ((getdomainname(old_domain_name, MAX_NAME_LEN)) < 0) { 157 tst_brkm(TBROK, NULL, "getdomainname() failed while" 158 " getting current domain name"); 159 } 160 161 TEST_PAUSE; 162 163 } 164 165 /* 166 * cleanup() - Performs all ONE TIME cleanup for this test at 167 */ 168 void cleanup(void) 169 { 170 171 /* Set effective user id back to root */ 172 if (seteuid(0) == -1) { 173 tst_resm(TWARN, "seteuid failed to " 174 "to set the effective uid to root"); 175 perror("seteuid"); 176 } 177 178 /* Restore domain name */ 179 if ((setdomainname(old_domain_name, strlen(old_domain_name))) 180 < 0) { 181 tst_resm(TWARN, "setdomainname() failed while restoring" 182 " domainname to \"%s\"", old_domain_name); 183 } 184 185 } 186