1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001
4f08c3bdfSopenharmony_ci * Copyright (c) 2013 Fujitsu Ltd.
5f08c3bdfSopenharmony_ci * Copyright (c) Linux Test Project, 2003-2023
6f08c3bdfSopenharmony_ci * Ported to LTP: Wayne Boyer
7f08c3bdfSopenharmony_ci * 11/2013 Ported by Xiaoguang Wang <wangxg.fnst@cn.fujitsu.com>
8f08c3bdfSopenharmony_ci * 11/2016 Modified by Guangwen Feng <fenggw-fnst@cn.fujitsu.com>
9f08c3bdfSopenharmony_ci */
10f08c3bdfSopenharmony_ci
11f08c3bdfSopenharmony_ci/*\
12f08c3bdfSopenharmony_ci * [Description]
13f08c3bdfSopenharmony_ci *
14f08c3bdfSopenharmony_ci * -  access() fails with -1 return value and sets errno to EINVAL
15f08c3bdfSopenharmony_ci *    if the specified access mode argument is invalid.
16f08c3bdfSopenharmony_ci * -  access() fails with -1 return value and sets errno to ENOENT
17f08c3bdfSopenharmony_ci *    if the specified file doesn't exist (or pathname is NULL).
18f08c3bdfSopenharmony_ci * -  access() fails with -1 return value and sets errno to ENAMETOOLONG
19f08c3bdfSopenharmony_ci *    if the pathname size is > PATH_MAX characters.
20f08c3bdfSopenharmony_ci * -  access() fails with -1 return value and sets errno to ENOTDIR
21f08c3bdfSopenharmony_ci *    if a component used as a directory in pathname is not a directory.
22f08c3bdfSopenharmony_ci * -  access() fails with -1 return value and sets errno to ELOOP
23f08c3bdfSopenharmony_ci *    if too many symbolic links were encountered in resolving pathname.
24f08c3bdfSopenharmony_ci * -  access() fails with -1 return value and sets errno to EROFS
25f08c3bdfSopenharmony_ci *    if write permission was requested for files on a read-only file system.
26f08c3bdfSopenharmony_ci */
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_ci#include <errno.h>
29f08c3bdfSopenharmony_ci#include <pwd.h>
30f08c3bdfSopenharmony_ci#include <string.h>
31f08c3bdfSopenharmony_ci#include <sys/mount.h>
32f08c3bdfSopenharmony_ci#include <sys/types.h>
33f08c3bdfSopenharmony_ci#include <unistd.h>
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci#include "tst_test.h"
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_ci#define FNAME1	"accessfile1"
38f08c3bdfSopenharmony_ci#define FNAME2	"accessfile2/accessfile2"
39f08c3bdfSopenharmony_ci#define DNAME	"accessfile2"
40f08c3bdfSopenharmony_ci#define SNAME1	"symlink1"
41f08c3bdfSopenharmony_ci#define SNAME2	"symlink2"
42f08c3bdfSopenharmony_ci#define MNT_POINT	"mntpoint"
43f08c3bdfSopenharmony_ci#define LONGPATHSIZE (PATH_MAX + 2)
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_cistatic uid_t uid;
46f08c3bdfSopenharmony_cistatic char *longpathname;
47f08c3bdfSopenharmony_cistatic char *fname1;
48f08c3bdfSopenharmony_cistatic char *fname2;
49f08c3bdfSopenharmony_cistatic char *sname1;
50f08c3bdfSopenharmony_cistatic char *empty_fname;
51f08c3bdfSopenharmony_cistatic char *mnt_point;
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_cistatic struct tcase {
54f08c3bdfSopenharmony_ci	char **pathname;
55f08c3bdfSopenharmony_ci	int mode;
56f08c3bdfSopenharmony_ci	int exp_errno;
57f08c3bdfSopenharmony_ci} tcases[] = {
58f08c3bdfSopenharmony_ci	{&fname1, -1, EINVAL},
59f08c3bdfSopenharmony_ci	{&empty_fname, W_OK, ENOENT},
60f08c3bdfSopenharmony_ci	{&longpathname, R_OK, ENAMETOOLONG},
61f08c3bdfSopenharmony_ci	{&fname2, R_OK, ENOTDIR},
62f08c3bdfSopenharmony_ci	{&sname1, R_OK, ELOOP},
63f08c3bdfSopenharmony_ci	{&mnt_point, W_OK, EROFS}
64f08c3bdfSopenharmony_ci};
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_cistatic void access_test(struct tcase *tc, const char *user)
67f08c3bdfSopenharmony_ci{
68f08c3bdfSopenharmony_ci	TST_EXP_FAIL(access(*tc->pathname, tc->mode), tc->exp_errno,
69f08c3bdfSopenharmony_ci	             "access as %s", user);
70f08c3bdfSopenharmony_ci}
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_cistatic void verify_access(unsigned int n)
73f08c3bdfSopenharmony_ci{
74f08c3bdfSopenharmony_ci	struct tcase *tc = tcases + n;
75f08c3bdfSopenharmony_ci	pid_t pid;
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_ci	access_test(tc, "root");
78f08c3bdfSopenharmony_ci
79f08c3bdfSopenharmony_ci	pid = SAFE_FORK();
80f08c3bdfSopenharmony_ci	if (pid) {
81f08c3bdfSopenharmony_ci		SAFE_WAITPID(pid, NULL, 0);
82f08c3bdfSopenharmony_ci	} else {
83f08c3bdfSopenharmony_ci		SAFE_SETUID(uid);
84f08c3bdfSopenharmony_ci		access_test(tc, "nobody");
85f08c3bdfSopenharmony_ci	}
86f08c3bdfSopenharmony_ci}
87f08c3bdfSopenharmony_ci
88f08c3bdfSopenharmony_cistatic void setup(void)
89f08c3bdfSopenharmony_ci{
90f08c3bdfSopenharmony_ci	struct passwd *pw;
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_ci	pw = SAFE_GETPWNAM("nobody");
93f08c3bdfSopenharmony_ci
94f08c3bdfSopenharmony_ci	uid = pw->pw_uid;
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_ci	memset(longpathname, 'a', LONGPATHSIZE - 1);
97f08c3bdfSopenharmony_ci	longpathname[LONGPATHSIZE-1] = 0;
98f08c3bdfSopenharmony_ci
99f08c3bdfSopenharmony_ci	SAFE_TOUCH(FNAME1, 0333, NULL);
100f08c3bdfSopenharmony_ci	SAFE_TOUCH(DNAME, 0644, NULL);
101f08c3bdfSopenharmony_ci
102f08c3bdfSopenharmony_ci	SAFE_SYMLINK(SNAME1, SNAME2);
103f08c3bdfSopenharmony_ci	SAFE_SYMLINK(SNAME2, SNAME1);
104f08c3bdfSopenharmony_ci}
105f08c3bdfSopenharmony_ci
106f08c3bdfSopenharmony_cistatic struct tst_test test = {
107f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
108f08c3bdfSopenharmony_ci	.needs_root = 1,
109f08c3bdfSopenharmony_ci	.forks_child = 1,
110f08c3bdfSopenharmony_ci	.needs_rofs = 1,
111f08c3bdfSopenharmony_ci	.mntpoint = MNT_POINT,
112f08c3bdfSopenharmony_ci	.setup = setup,
113f08c3bdfSopenharmony_ci	.test = verify_access,
114f08c3bdfSopenharmony_ci	.bufs = (struct tst_buffers []) {
115f08c3bdfSopenharmony_ci		{&fname1, .str = FNAME1},
116f08c3bdfSopenharmony_ci		{&fname2, .str = FNAME2},
117f08c3bdfSopenharmony_ci		{&sname1, .str = SNAME1},
118f08c3bdfSopenharmony_ci		{&empty_fname, .str = ""},
119f08c3bdfSopenharmony_ci		{&longpathname, .size = LONGPATHSIZE},
120f08c3bdfSopenharmony_ci		{&mnt_point, .str = MNT_POINT},
121f08c3bdfSopenharmony_ci		{}
122f08c3bdfSopenharmony_ci	}
123f08c3bdfSopenharmony_ci};
124