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 Write access
     24  * to the directory containing newpath is not allowed for the process's
     25  * effective uid.
     26  */
     27 
     28 #include <stdio.h>
     29 #include <stdlib.h>
     30 #include <unistd.h>
     31 #include <fcntl.h>
     32 #include <errno.h>
     33 #include <string.h>
     34 #include <signal.h>
     35 #include <sys/types.h>
     36 #include <sys/stat.h>
     37 #include <pwd.h>
     38 
     39 #include "test.h"
     40 #include "safe_macros.h"
     41 
     42 #define NOBODY_USER	99
     43 #define MODE_TO S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IXOTH|S_IROTH
     44 
     45 static void setup(void);
     46 static void cleanup(void);
     47 
     48 char *TCID = "link06";
     49 int TST_TOTAL = 1;
     50 
     51 #define OLDPATH "oldpath"
     52 #define NEWPATH "newpath"
     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(OLDPATH, NEWPATH));
     66 
     67 		if (TEST_RETURN != -1) {
     68 			tst_resm(TFAIL, "link() returned %ld,"
     69 				 "expected -1, errno=%d", TEST_RETURN,
     70 				 EACCES);
     71 		} else {
     72 			if (TEST_ERRNO == EACCES) {
     73 				tst_resm(TPASS, "link() fails with expected "
     74 					 "error EACCES errno:%d", TEST_ERRNO);
     75 			} else {
     76 				tst_resm(TFAIL, "link() fails with "
     77 					 "errno=%d, expected errno=%d",
     78 					 TEST_ERRNO, 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_TOUCH(cleanup, OLDPATH, 0777, NULL);
    103 	nobody_pwd = SAFE_GETPWNAM(cleanup, "nobody");
    104 	SAFE_SETEUID(cleanup, nobody_pwd->pw_uid);
    105 }
    106 
    107 static void cleanup(void)
    108 {
    109 	if (seteuid(0))
    110 		tst_resm(TWARN | TERRNO, "seteuid(0) failed");
    111 
    112 	tst_rmdir();
    113 }
    114