Home | History | Annotate | Download | only in link
      1 /*
      2  * Copyright (C) Bull S.A. 2001
      3  * Copyright (c) International Business Machines  Corp., 2001
      4  *  06/2002 Ported by Jacky Malcles
      5  * Copyright (c) 2014 Cyril Hrubis <chrubis (at) suse.cz>
      6  *
      7  * This program is free software;  you can redistribute it and/or modify
      8  * it under the terms of the GNU General Public License as published by
      9  * the Free Software Foundation; either version 2 of the License, or
     10  * (at your option) any later version.
     11  *
     12  * This program is distributed in the hope that it will be useful,
     13  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
     14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
     15  * the GNU General Public License for more details.
     16  *
     17  * You should have received a copy of the GNU General Public License
     18  * along with this program;  if not, write to the Free Software
     19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     20  */
     21 
     22 /*
     23  * Verify that, link() fails with -1 and sets errno to EACCES when one of the
     24  * directories in oldpath or newpath did not allow search (execute) permission.
     25  */
     26 
     27 #include <stdio.h>
     28 #include <stdlib.h>
     29 #include <unistd.h>
     30 #include <fcntl.h>
     31 #include <errno.h>
     32 #include <string.h>
     33 #include <signal.h>
     34 #include <sys/types.h>
     35 #include <sys/stat.h>
     36 #include <pwd.h>
     37 
     38 #include "test.h"
     39 #include "safe_macros.h"
     40 
     41 #define MODE_TO S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IXOTH|S_IROTH|S_IWOTH
     42 #define MODE_TE S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH
     43 #define MODE_RWX        S_IRWXU | S_IRWXG | S_IRWXO
     44 #define DIR_TEMP        "testdir_1"
     45 #define TEST_FILE2      "testdir_1/tfile_2"
     46 #define NEW_TEST_FILE2  "testdir_1/new_tfile_2"
     47 
     48 static void setup(void);
     49 static void cleanup(void);
     50 
     51 char *TCID = "link07";
     52 int TST_TOTAL = 1;
     53 
     54 int main(int ac, char **av)
     55 {
     56 	int lc;
     57 
     58 	tst_parse_opts(ac, av, NULL, NULL);
     59 
     60 	setup();
     61 
     62 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     63 		tst_count = 0;
     64 
     65 		TEST(link(TEST_FILE2, NEW_TEST_FILE2));
     66 
     67 		/* Check return code from link(2) */
     68 		if (TEST_RETURN != -1) {
     69 			tst_resm(TFAIL | TTERRNO, "link() returned %ld,"
     70 				 "expected -1, errno=%d", TEST_RETURN, EACCES);
     71 		} else {
     72 			if (TEST_ERRNO == EACCES) {
     73 				tst_resm(TPASS | TTERRNO,
     74 				         "link() fails with expected error");
     75 			} else {
     76 				tst_resm(TFAIL | TTERRNO, "link() failed"
     77 				         ", expected errno=%d (EACCES)",
     78 				         EACCES);
     79 			}
     80 		}
     81 	}
     82 
     83 	cleanup();
     84 	tst_exit();
     85 }
     86 
     87 static void setup(void)
     88 {
     89 	struct passwd *nobody_pwd;
     90 
     91 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
     92 
     93 	tst_require_root();
     94 
     95 	TEST_PAUSE;
     96 
     97 	tst_tmpdir();
     98 
     99 	/* Modify mode permissions on test directory */
    100 	SAFE_CHMOD(cleanup, ".", MODE_TO);
    101 
    102 	SAFE_MKDIR(cleanup, DIR_TEMP, MODE_RWX);
    103 	SAFE_TOUCH(cleanup, TEST_FILE2, 0666, NULL);
    104 
    105 	/* Modify mode permissions on test directory - test conditions */
    106 	SAFE_CHMOD(cleanup, DIR_TEMP, MODE_TE);
    107 
    108 	nobody_pwd = SAFE_GETPWNAM(cleanup, "nobody");
    109 	SAFE_SETEUID(cleanup, nobody_pwd->pw_uid);
    110 }
    111 
    112 static void cleanup(void)
    113 {
    114 	if (seteuid(0))
    115 		tst_resm(TWARN | TERRNO, "seteuid(o) failed");
    116 
    117 	tst_rmdir();
    118 }
    119