sys/arch/x86/include updates
[minix.git] / test / test11.c
blob755ecf20b681a6569c6802db9dc1d961fb19f4aa
1 /* test 11 */
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <sys/wait.h>
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <stdio.h>
13 #define ITERATIONS 10
14 #define MAX_ERROR 1
16 int errct, subtest;
17 char *envp[3] = {"spring", "summer", 0};
18 char *passwd_file = "/etc/passwd";
20 #include "common.c"
22 int main(int argc, char *argv[]);
23 void test11a(void);
24 void test11b(void);
25 void test11c(void);
26 void test11d(void);
28 int main(argc, argv)
29 int argc;
30 char *argv[];
32 int i, m = 0xFFFF;
34 if (argc == 2) m = atoi(argv[1]);
36 start(11);
38 system("cp ../t11a .");
39 system("cp ../t11b .");
41 if (geteuid() != 0) {
42 printf("must be setuid root; test aborted\n");
43 exit(1);
45 if (getuid() == 0) {
46 printf("must be setuid root logged in as someone else; test aborted\n");
47 exit(1);
50 for (i = 0; i < ITERATIONS; i++) {
51 if (m & 0001) test11a();
52 if (m & 0002) test11b();
53 if (m & 0004) test11c();
54 if (m & 0010) test11d();
56 quit();
57 return(-1);
60 void test11a()
62 /* Test exec */
63 int n, fd;
64 char aa[4];
66 subtest = 1;
68 if (fork()) {
69 wait(&n);
70 if (n != 25600) e(1);
71 unlink("t1");
72 unlink("t2");
73 } else {
74 if (chown("t11a", 10, 20) < 0) e(2);
75 chmod("t11a", 0666);
77 /* The following call should fail because the mode has no X
78 * bits on. If a bug lets it unexpectedly succeed, the child
79 * will print an error message since the arguments are wrong.
81 execl("t11a", "t11a", (char *) 0); /* should fail -- no X bits */
83 /* Control should come here after the failed execl(). */
84 chmod("t11a", 06555);
85 if ((fd = creat("t1", 0600)) != 3) e(3);
86 if (close(fd) < 0) e(4);
87 if (open("t1", O_RDWR) != 3) e(5);
88 if (chown("t1", 10, 99) < 0) e(6);
89 if ((fd = creat("t2", 0060)) != 4) e(7);
90 if (close(fd) < 0) e(8);
91 if (open("t2", O_RDWR) != 4) e(9);
92 if (chown("t2", 99, 20) < 0) e(10);
93 if (setgid(6) < 0) e(11);
94 if (setuid(5) < 0) e(12);
95 if (getuid() != 5) e(13);
96 if (geteuid() != 5) e(14);
97 if (getgid() != 6) e(15);
98 if (getegid() != 6) e(16);
99 aa[0] = 3;
100 aa[1] = 5;
101 aa[2] = 7;
102 aa[3] = 9;
103 if (write(3, aa, 4) != 4) e(17);
104 lseek(3, 2L, 0);
105 execle("t11a", "t11a", "arg0", "arg1", "arg2", (char *) 0, envp);
106 e(18);
107 printf("Can't exec t11a\n");
108 exit(3);
112 void test11b()
114 int n;
115 char *argv[5];
117 subtest = 2;
118 if (fork()) {
119 wait(&n);
120 if (n != (75 << 8)) e(20);
121 } else {
122 /* Child tests execv. */
123 argv[0] = "t11b";
124 argv[1] = "abc";
125 argv[2] = "defghi";
126 argv[3] = "j";
127 argv[4] = 0;
128 execv("t11b", argv);
129 e(19);
133 void test11c()
135 /* Test getlogin(). This test MUST run setuid root. */
137 int n, etc_uid;
138 uid_t ruid, euid;
139 char *lnamep, *p;
140 #define MAXLINELEN 200
141 char array[MAXLINELEN], save[L_cuserid];
142 FILE *stream;
144 subtest = 3;
145 errno = -2000; /* None of these calls set errno. */
146 save[0] = '#';
147 save[1] = '0';
148 ruid = getuid();
149 euid = geteuid();
150 lnamep = getlogin();
151 strcpy(save, lnamep);
153 /* Because we are setuid, login != 'root' */
154 if (euid != 0) e(1);
155 if (ruid == 0) e(2);
156 if ( (n = strlen(save)) == 0) e(5);
158 /* Check login against passwd file. First lookup login in /etc/passwd. */
159 if (n == 0) return; /* if login not found, don't look it up */
160 if ( (stream = fopen(passwd_file, "r")) == NULL) e(8);
161 while (fgets(array, sizeof(array), stream) != NULL) {
162 if (strncmp(array, save, n) == 0) {
163 p = &array[0]; /* hunt for uid */
164 while (*p != ':') p++;
165 p++;
166 while (*p != ':') p++;
167 p++; /* p now points to uid */
168 etc_uid = atoi(p);
169 if (etc_uid != ruid) e(9);
170 break; /* 1 entry per login please */
173 fclose(stream);
176 void test11d()
178 int fd;
179 struct stat statbuf;
181 subtest = 4;
182 fd = creat("T11.1", 0750);
183 if (fd < 0) e(1);
184 if (chown("T11.1", 8, 1) != 0) e(2);
185 if (chmod("T11.1", 0666) != 0) e(3);
186 if (stat("T11.1", &statbuf) != 0) e(4);
187 if ((statbuf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) != 0666) e(5);
188 if (close(fd) != 0) e(6);
189 if (unlink("T11.1") != 0) e(7);