17 char *envp
[3] = {"spring", "summer", 0};
18 char *passwd_file
= "/etc/passwd";
20 _PROTOTYPE(int main
, (int argc
, char *argv
[]));
21 _PROTOTYPE(void test11a
, (void));
22 _PROTOTYPE(void test11b
, (void));
23 _PROTOTYPE(void test11c
, (void));
24 _PROTOTYPE(void test11d
, (void));
25 _PROTOTYPE(void e
, (int n
));
33 if (argc
== 2) m
= atoi(argv
[1]);
36 fflush(stdout
); /* have to flush for child's benefit */
39 printf("must be setuid root; test aborted\n");
43 printf("must be setuid root logged in as someone else; test aborted\n");
48 system("rm -rf DIR_11; mkdir DIR_11");
52 for (i
= 0; i
< ITERATIONS
; i
++) {
53 if (m
& 0001) test11a();
54 if (m
& 0002) test11b();
55 if (m
& 0004) test11c();
56 if (m
& 0010) test11d();
61 printf(" %d errors\n", errct
);
65 system("rm -rf DIR_11");
84 if (chown("t11a", 10, 20) < 0) e(2);
87 /* The following call should fail because the mode has no X
88 * bits on. If a bug lets it unexpectedly succeed, the child
89 * will print an error message since the arguments are wrong.
91 execl("t11a", (char *) 0, envp
); /* should fail -- no X bits */
93 /* Control should come here after the failed execl(). */
95 if ((fd
= creat("t1", 0600)) != 3) e(3);
96 if (close(fd
) < 0) e(4);
97 if (open("t1", O_RDWR
) != 3) e(5);
98 if (chown("t1", 10, 99) < 0) e(6);
99 if ((fd
= creat("t2", 0060)) != 4) e(7);
100 if (close(fd
) < 0) e(8);
101 if (open("t2", O_RDWR
) != 4) e(9);
102 if (chown("t2", 99, 20) < 0) e(10);
103 if (setgid(6) < 0) e(11);
104 if (setuid(5) < 0) e(12);
105 if (getuid() != 5) e(13);
106 if (geteuid() != 5) e(14);
107 if (getgid() != 6) e(15);
108 if (getegid() != 6) e(16);
113 if (write(3, aa
, 4) != 4) e(17);
115 execle("t11a", "t11a", "arg0", "arg1", "arg2", (char *) 0, envp
);
117 printf("Can't exec t11a\n");
130 if (n
!= (75 << 8)) e(20);
132 /* Child tests execv. */
145 /* Test getlogin() and cuserid(). This test MUST run setuid root. */
149 char *lnamep
, *cnamep
, *p
;
150 char array
[L_cuserid
], save
[L_cuserid
], save2
[L_cuserid
];
154 errno
= -2000; /* None of these calls set errno. */
162 strcpy(save
, lnamep
);
163 cnamep
= cuserid(array
);
164 strcpy(save2
, cnamep
);
166 /* Because we are setuid root, cuser == array == 'root'; login != 'root' */
169 if (strcmp(cnamep
, "root") != 0) e(3);
170 if (strcmp(array
, "root") != 0) e(4);
171 if ( (n
= strlen(save
)) == 0) e(5);
172 if (strcmp(save
, cnamep
) == 0) e(6); /* they must be different */
173 cnamep
= cuserid(NULL
);
174 if (strcmp(cnamep
, save2
) != 0) e(7);
176 /* Check login against passwd file. First lookup login in /etc/passwd. */
177 if (n
== 0) return; /* if login not found, don't look it up */
178 if ( (stream
= fopen(passwd_file
, "r")) == NULL
) e(8);
179 while (fgets(array
, L_cuserid
, stream
) != NULL
) {
180 if (strncmp(array
, save
, n
) == 0) {
181 p
= &array
[0]; /* hunt for uid */
182 while (*p
!= ':') p
++;
184 while (*p
!= ':') p
++;
185 p
++; /* p now points to uid */
187 if (etc_uid
!= ruid
) e(9);
188 break; /* 1 entry per login please */
200 fd
= creat("T11.1", 0750);
202 if (chown("T11.1", 8, 1) != 0) e(2);
203 if (chmod("T11.1", 0666) != 0) e(3);
204 if (stat("T11.1", &statbuf
) != 0) e(4);
205 if ((statbuf
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
)) != 0666) e(5);
206 if (close(fd
) != 0) e(6);
207 if (unlink("T11.1") != 0) e(7);
213 int err_num
= errno
; /* save errno in case printf clobbers it */
215 printf("Subtest %d, error %d errno=%d ", subtest
, n
, errno
);
216 errno
= err_num
; /* restore errno, just in case */
218 if (errct
++ > MAX_ERROR
) {
219 printf("Too many errors; test aborted\n");
221 system("rm -rf DIR*");