1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
3f08c3bdfSopenharmony_ci *
4f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify it
5f08c3bdfSopenharmony_ci * under the terms of version 2 of the GNU General Public License as
6f08c3bdfSopenharmony_ci * published by the Free Software Foundation.
7f08c3bdfSopenharmony_ci *
8f08c3bdfSopenharmony_ci * This program is distributed in the hope that it would be useful, but
9f08c3bdfSopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
10f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License along
13f08c3bdfSopenharmony_ci * with this program; if not, write the Free Software Foundation, Inc.,
14f08c3bdfSopenharmony_ci * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15f08c3bdfSopenharmony_ci *
16f08c3bdfSopenharmony_ci * DESCRIPTION
17f08c3bdfSopenharmony_ci *	Verify that mount(2) returns -1 and sets errno to  EPERM if the user
18f08c3bdfSopenharmony_ci *	is not the super-user.
19f08c3bdfSopenharmony_ci */
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci#include <errno.h>
22f08c3bdfSopenharmony_ci#include <sys/mount.h>
23f08c3bdfSopenharmony_ci#include <sys/types.h>
24f08c3bdfSopenharmony_ci#include <sys/stat.h>
25f08c3bdfSopenharmony_ci#include <fcntl.h>
26f08c3bdfSopenharmony_ci#include <pwd.h>
27f08c3bdfSopenharmony_ci#include "test.h"
28f08c3bdfSopenharmony_ci#include "safe_macros.h"
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_cistatic void setup(void);
31f08c3bdfSopenharmony_cistatic void cleanup(void);
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_cichar *TCID = "mount04";
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci#define DIR_MODE	S_IRWXU | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_cistatic char *mntpoint = "mntpoint";
38f08c3bdfSopenharmony_cistatic const char *fs_type;
39f08c3bdfSopenharmony_cistatic const char *device;
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ciint TST_TOTAL = 1;
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_cistatic void verify_mount(void)
44f08c3bdfSopenharmony_ci{
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_ci	TEST(mount(device, mntpoint, fs_type, 0, NULL));
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci	if (TEST_RETURN == -1) {
49f08c3bdfSopenharmony_ci		if (TEST_ERRNO == EPERM)
50f08c3bdfSopenharmony_ci			tst_resm(TPASS | TTERRNO, "mount() failed expectedly");
51f08c3bdfSopenharmony_ci		else
52f08c3bdfSopenharmony_ci			tst_resm(TFAIL | TTERRNO,
53f08c3bdfSopenharmony_ci			         "mount() was expected to fail with EPERM");
54f08c3bdfSopenharmony_ci		return;
55f08c3bdfSopenharmony_ci	}
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	if (TEST_RETURN == 0) {
58f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "mount() succeeded unexpectedly");
59f08c3bdfSopenharmony_ci		if (tst_umount(mntpoint))
60f08c3bdfSopenharmony_ci			tst_brkm(TBROK, cleanup, "umount() failed");
61f08c3bdfSopenharmony_ci		return;
62f08c3bdfSopenharmony_ci	}
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ci	tst_resm(TFAIL | TTERRNO, "mount() returned %li", TEST_RETURN);
65f08c3bdfSopenharmony_ci}
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ciint main(int ac, char **av)
68f08c3bdfSopenharmony_ci{
69f08c3bdfSopenharmony_ci	int lc;
70f08c3bdfSopenharmony_ci
71f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_ci	setup();
74f08c3bdfSopenharmony_ci
75f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
76f08c3bdfSopenharmony_ci		tst_count = 0;
77f08c3bdfSopenharmony_ci		verify_mount();
78f08c3bdfSopenharmony_ci	}
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_ci	cleanup();
81f08c3bdfSopenharmony_ci	tst_exit();
82f08c3bdfSopenharmony_ci}
83f08c3bdfSopenharmony_ci
84f08c3bdfSopenharmony_cistatic void setup(void)
85f08c3bdfSopenharmony_ci{
86f08c3bdfSopenharmony_ci	char nobody_uid[] = "nobody";
87f08c3bdfSopenharmony_ci	struct passwd *ltpuser;
88f08c3bdfSopenharmony_ci
89f08c3bdfSopenharmony_ci	tst_sig(FORK, DEF_HANDLER, cleanup);
90f08c3bdfSopenharmony_ci
91f08c3bdfSopenharmony_ci	tst_require_root();
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_ci	tst_tmpdir();
94f08c3bdfSopenharmony_ci
95f08c3bdfSopenharmony_ci	fs_type = tst_dev_fs_type();
96f08c3bdfSopenharmony_ci	device = tst_acquire_device(cleanup);
97f08c3bdfSopenharmony_ci
98f08c3bdfSopenharmony_ci	if (!device)
99f08c3bdfSopenharmony_ci		tst_brkm(TCONF, cleanup, "Failed to obtain block device");
100f08c3bdfSopenharmony_ci
101f08c3bdfSopenharmony_ci	tst_mkfs(cleanup, device, fs_type, NULL, NULL);
102f08c3bdfSopenharmony_ci
103f08c3bdfSopenharmony_ci	ltpuser = SAFE_GETPWNAM(cleanup, nobody_uid);
104f08c3bdfSopenharmony_ci	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
105f08c3bdfSopenharmony_ci	SAFE_MKDIR(cleanup, mntpoint, DIR_MODE);
106f08c3bdfSopenharmony_ci
107f08c3bdfSopenharmony_ci	TEST_PAUSE;
108f08c3bdfSopenharmony_ci}
109f08c3bdfSopenharmony_ci
110f08c3bdfSopenharmony_cistatic void cleanup(void)
111f08c3bdfSopenharmony_ci{
112f08c3bdfSopenharmony_ci	if (seteuid(0))
113f08c3bdfSopenharmony_ci		tst_resm(TWARN | TERRNO, "seteuid(0) failed");
114f08c3bdfSopenharmony_ci
115f08c3bdfSopenharmony_ci	if (device)
116f08c3bdfSopenharmony_ci		tst_release_device(device);
117f08c3bdfSopenharmony_ci
118f08c3bdfSopenharmony_ci	tst_rmdir();
119f08c3bdfSopenharmony_ci}
120