1 /*	$NetBSD: t_basic.c,v 1.5 2017/01/13 21:30:40 christos Exp $	*/
2 
3 #include <sys/types.h>
4 #include <sys/param.h>
5 #include <sys/mount.h>
6 
7 #include <atf-c.h>
8 #include <err.h>
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include <stdlib.h>
15 
16 #include <rump/rump.h>
17 #include <rump/rump_syscalls.h>
18 #include <rump/rumpvfs_if_pub.h>
19 
20 #include <fs/tmpfs/tmpfs_args.h>
21 #include <miscfs/umapfs/umap.h>
22 
23 #include "h_macros.h"
24 
25 ATF_TC(basic);
26 ATF_TC_HEAD(basic, tc)
27 {
28 	atf_tc_set_md_var(tc, "descr", "basic umapfs mapping");
29 }
30 
31 static void
32 xtouch(const char *path)
33 {
34 	int fd;
35 
36 	fd = rump_sys_open(path, O_CREAT | O_RDWR, 0777);
37 	if (fd == -1)
38 		atf_tc_fail_errno("create %s", path);
39 	rump_sys_close(fd);
40 }
41 
42 static void
43 xchown(const char *path, uid_t uid, gid_t gid)
44 {
45 
46 	if (rump_sys_chown(path, uid, gid) == -1)
47 		atf_tc_fail_errno("chown %s failed", path);
48 }
49 
50 static void
51 testuidgid(const char *path, uid_t uid, gid_t gid)
52 {
53 	struct stat sb;
54 
55 	if (rump_sys_stat(path, &sb) == -1)
56 		atf_tc_fail_errno("stat %s", path);
57 	if (uid != (uid_t)-1) {
58 		if (sb.st_uid != uid)
59 			atf_tc_fail("%s: expected uid %d, got %d",
60 			    path, uid, sb.st_uid);
61 	}
62 	if (gid != (gid_t)-1) {
63 		if (sb.st_gid != gid)
64 			atf_tc_fail("%s: expected gid %d, got %d",
65 			    path, gid, sb.st_gid);
66 	}
67 }
68 
69 ATF_TC_BODY(basic, tc)
70 {
71 	struct umap_args umargs;
72 	struct tmpfs_args targs;
73 	u_long umaps[2][2];
74 	u_long gmaps[2][2];
75 
76 	rump_init();
77 	if (rump_sys_mkdir("/td1", 0777) == -1)
78 		atf_tc_fail_errno("mp1");
79 	if (rump_sys_mkdir("/td2", 0777) == -1)
80 		atf_tc_fail_errno("mp1");
81 
82 	/* use tmpfs because rumpfs doesn't support ownership */
83 	memset(&targs, 0, sizeof(targs));
84 	targs.ta_version = TMPFS_ARGS_VERSION;
85 	targs.ta_root_mode = 0777;
86 	if (rump_sys_mount(MOUNT_TMPFS, "/td1", 0, &targs, sizeof(targs)) == -1)
87 		atf_tc_fail_errno("could not mount tmpfs td1");
88 
89 	memset(&umargs, 0, sizeof(umargs));
90 
91 	/*
92 	 * Map td1 uid 555 to td2 uid 777 (yes, IMHO the umapfs
93 	 * mapping format is counter-intuitive).
94 	 */
95 	umaps[0][0] = 777;
96 	umaps[0][1] = 555;
97 	umaps[1][0] = 0;
98 	umaps[1][1] = 0;
99 	gmaps[0][0] = 4321;
100 	gmaps[0][1] = 1234;
101 	gmaps[1][0] = 0;
102 	gmaps[1][1] = 0;
103 
104 	umargs.umap_target = __UNCONST("/td1");
105 	umargs.nentries = 2;
106 	umargs.gnentries = 2;
107 	umargs.mapdata = umaps;
108 	umargs.gmapdata = gmaps;
109 
110 	if (rump_sys_mount(MOUNT_UMAP, "/td2", 0, &umargs,sizeof(umargs)) == -1)
111 		atf_tc_fail_errno("could not mount umapfs");
112 
113 	xtouch("/td1/noch");
114 	testuidgid("/td1/noch", 0, 0);
115 	testuidgid("/td2/noch", 0, 0);
116 
117 	xtouch("/td1/nomap");
118 	xchown("/td1/nomap", 1, 2);
119 	testuidgid("/td1/nomap", 1, 2);
120 	testuidgid("/td2/nomap", -1, -1);
121 
122 	xtouch("/td1/forwmap");
123 	xchown("/td1/forwmap", 555, 1234);
124 	testuidgid("/td1/forwmap", 555, 1234);
125 	testuidgid("/td2/forwmap", 777, 4321);
126 
127 	/*
128 	 * this *CANNOT* be correct???
129 	 */
130 	xtouch("/td1/revmap");
131 	/*
132 	 * should be 777 / 4321 (?), but makes first test fail since
133 	 * it gets 777 / 4321, i.e. unmapped results.
134 	 */
135 	xchown("/td2/revmap", 555, 1234);
136 	testuidgid("/td1/revmap", 555, 1234);
137 	testuidgid("/td2/revmap", 777, 4321);
138 
139 }
140 
141 ATF_TP_ADD_TCS(tp)
142 {
143 	ATF_TP_ADD_TC(tp, basic);
144 	return 0; /*XXX?*/
145 }
146