1724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew/*
2724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew * Copyright (C) Bull S.A. 2001
3724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew * Copyright (c) International Business Machines  Corp., 2001
4af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis *  06/2002 Ported by Jacky Malcles
5af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis * Copyright (c) 2014 Cyril Hrubis <chrubis@suse.cz>
6724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew *
7af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis * This program is free software;  you can redistribute it and/or modify
8af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis * it under the terms of the GNU General Public License as published by
9af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis * the Free Software Foundation; either version 2 of the License, or
10af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis * (at your option) any later version.
11724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew *
12af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis * This program is distributed in the hope that it will be useful,
13af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis * but WITHOUT ANY WARRANTY;  without even the implied warranty of
14af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
15af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis * the GNU General Public License for more details.
16724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew *
17af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis * You should have received a copy of the GNU General Public License
18af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis * along with this program;  if not, write to the Free Software
19af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew */
21724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew
22724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew/*
23af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis * Verify that, link() fails with -1 and sets errno to EACCES when one of the
24af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis * directories in oldpath or newpath did not allow search (execute) permission.
25724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew */
26af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis
27724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew#include <stdio.h>
28724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew#include <stdlib.h>
29724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew#include <unistd.h>
30724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew#include <fcntl.h>
31724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew#include <errno.h>
32724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew#include <string.h>
33724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew#include <signal.h>
34724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew#include <sys/types.h>
35724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew#include <sys/stat.h>
3691ec53d1a811594976df4811a43113a0913da222Garrett Cooper#include <pwd.h>
37724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew
38724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew#include "test.h"
39af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis#include "safe_macros.h"
40724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew
41724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew#define MODE_TO S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IXOTH|S_IROTH|S_IWOTH
42724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew#define MODE_TE S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH
43724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew#define MODE_RWX        S_IRWXU | S_IRWXG | S_IRWXO
44724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew#define DIR_TEMP        "testdir_1"
45724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew#define TEST_FILE2      "testdir_1/tfile_2"
46724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew#define NEW_TEST_FILE2  "testdir_1/new_tfile_2"
47724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew
48af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubisstatic void setup(void);
49af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubisstatic void cleanup(void);
50724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew
51fdce7d5e2a219d201a2b0e3bab6b61b01ec1d716Cyril Hrubischar *TCID = "link07";
52fdce7d5e2a219d201a2b0e3bab6b61b01ec1d716Cyril Hrubisint TST_TOTAL = 1;
53724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew
5456207cec7732e09c216c751c0b5f88a242bacae6subrata_modakint main(int ac, char **av)
55724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew{
5689af32a63ce8a780ea39337339e14caae244b5a4Cyril Hrubis	int lc;
5756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak
58d6d11d08678aac1ed2c370ea8e42e5f45aea07beCyril Hrubis	tst_parse_opts(ac, av, NULL, NULL);
592c28215423293e443469a07ae7011135d058b671Garrett Cooper
6056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	setup();
61724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew
6256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	for (lc = 0; TEST_LOOPING(lc); lc++) {
63d59a659cd639ca2780b00049d102acd2a783d585Caspar Zhang		tst_count = 0;
64bdbaec51a423e715c2b03ed9e497e9a1fba6103esubrata_modak
65af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis		TEST(link(TEST_FILE2, NEW_TEST_FILE2));
6656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak
6756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		/* Check return code from link(2) */
6856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		if (TEST_RETURN != -1) {
69af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis			tst_resm(TFAIL | TTERRNO, "link() returned %ld,"
70af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis				 "expected -1, errno=%d", TEST_RETURN, EACCES);
7156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak		} else {
72af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis			if (TEST_ERRNO == EACCES) {
73af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis				tst_resm(TPASS | TTERRNO,
74af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis				         "link() fails with expected error");
7556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			} else {
76af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis				tst_resm(TFAIL | TTERRNO, "link() failed"
77af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis				         ", expected errno=%d (EACCES)",
78af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis				         EACCES);
7956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak			}
80724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew		}
812c28215423293e443469a07ae7011135d058b671Garrett Cooper	}
82724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew
8356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	cleanup();
841e6f5a673655551de5734ff31ef48cd63b604e6dGarrett Cooper	tst_exit();
852c28215423293e443469a07ae7011135d058b671Garrett Cooper}
86724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew
87af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubisstatic void setup(void)
88724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew{
8991ec53d1a811594976df4811a43113a0913da222Garrett Cooper	struct passwd *nobody_pwd;
90724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew
9156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	tst_sig(NOFORK, DEF_HANDLER, cleanup);
9256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak
93d1e794d62b1bf619df8390535e4c2a58899b1145Cyril Hrubis	tst_require_root();
94af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis
9556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	TEST_PAUSE;
9656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak
9756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	tst_tmpdir();
9856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak
9956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	/* Modify mode permissions on test directory */
100af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis	SAFE_CHMOD(cleanup, ".", MODE_TO);
10156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak
102af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis	SAFE_MKDIR(cleanup, DIR_TEMP, MODE_RWX);
103af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis	SAFE_TOUCH(cleanup, TEST_FILE2, 0666, NULL);
10456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak
10556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	/* Modify mode permissions on test directory - test conditions */
106af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis	SAFE_CHMOD(cleanup, DIR_TEMP, MODE_TE);
107724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew
108af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis	nobody_pwd = SAFE_GETPWNAM(cleanup, "nobody");
109af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis	SAFE_SETEUID(cleanup, nobody_pwd->pw_uid);
1102c28215423293e443469a07ae7011135d058b671Garrett Cooper}
111724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew
112af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubisstatic void cleanup(void)
113724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew{
114af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis	if (seteuid(0))
115af0ea7b88071f940b4d9bc2e1e1deac8f6d38993Cyril Hrubis		tst_resm(TWARN | TERRNO, "seteuid(o) failed");
116724848a2aa34950958068ee2ce5e4d02d24ba42brobbiew
11756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	tst_rmdir();
118ec6edca7aa42b6affd989ef91b5897f96795e40fChris Dearman}
119