📄 test34.c
字号:
if (st1.st_ctime > time2) e(20);
#ifndef V1_FILESYSTEM
if (st2.st_ctime < time1) e(21);
#endif
if (st2.st_ctime > time2) e(22);
if (st1.st_atime != st2.st_atime) e(23);
if (st1.st_mtime != st2.st_mtime) e(24);
Stat(MaxName, &st1);
if (time(&time1) == (time_t) -1) e(25);
if (chmod(MaxName, mod) != 0) e(26);
Stat(MaxName, &st2);
if (time(&time2) == (time_t) -1) e(27);
if (superuser)
if ((st2.st_mode & ALL_BITS) != mod) e(28);
if (!superuser)
if ((st2.st_mode & ALL_RWXB) != (mod & ALL_RWXB)) e(29);
/* Test the C time field. */
if (st1.st_ctime > st2.st_ctime) e(30);
if (st1.st_ctime > time1) e(31);
if (st1.st_ctime > time2) e(32);
#ifndef V1_FILESYSTEM
if (st2.st_ctime < time1) e(33);
#endif
if (st2.st_ctime > time2) e(34);
if (st1.st_atime != st2.st_atime) e(35);
if (st1.st_mtime != st2.st_mtime) e(36);
Stat(MaxPath, &st1);
if (time(&time1) == (time_t) -1) e(37);
if (chmod(MaxPath, mod) != 0) e(38);
Stat(MaxPath, &st2);
if (time(&time2) == (time_t) -1) e(39);
if (superuser)
if ((st2.st_mode & ALL_BITS) != mod) e(40);
if (!superuser)
if ((st2.st_mode & ALL_RWXB) != (mod & ALL_RWXB)) e(41);
/* Test the C time field. */
if (st1.st_ctime > st2.st_ctime) e(42);
if (st1.st_ctime > time1) e(43);
if (st1.st_ctime > time2) e(44);
#ifndef V1_FILESYSTEM
if (st2.st_ctime < time1) e(45);
#endif
if (st2.st_ctime > time2) e(46);
if (st1.st_atime != st2.st_atime) e(47);
if (st1.st_mtime != st2.st_mtime) e(48);
}
if (chmod("dir", 0777) != 0) e(49);
if (chmod("fifo", 0777) != 0) e(50);
if (chmod(MaxName, 0777) != 0) e(51);
if (chmod(MaxPath, 0777) != 0) e(52);
(void) system("chmod 777 ../DIR_34/* > /dev/null 2> /dev/null");
System("rm -rf ../DIR_34/*");
}
void test34c()
{
struct stat st;
uid_t uid, uid2;
gid_t gid, gid2;
int stat_loc;
subtest = 3;
Mkdir("dir");
Creat("dir/try_me");
/* Disalow search permission and see if chmod() and chown() return
* EACCES.
*/
if (chmod("dir", ALL_BITS & ~S_IXUSR) != 0) e(1);
if (!superuser) {
if (chmod("dir/try_me", 0) != -1) e(2);
if (errno != EACCES) e(3);
if (I_can_chown) {
if (chown("dir/try_me", geteuid(), getegid()) != -1) e(4);
if (errno != EACCES) e(5);
}
}
/* Check ENOTDIR. */
Mkfifo("fifo");
if (chmod("fifo/try_me", 0) != -1) e(6);
if (errno != ENOTDIR) e(7);
if (chown("fifo/try_me", geteuid(), getegid()) != -1) e(8);
if (errno != ENOTDIR) e(9);
Creat("file");
if (chmod("file/try_me", 0) != -1) e(10);
if (errno != ENOTDIR) e(11);
if (chown("file/try_me", geteuid(), getegid()) != -1) e(12);
if (errno != ENOTDIR) e(13);
/* Check empty path. */
if (chmod("", 0) != -1) e(14);
if (errno != ENOENT) e(15);
if (chown("", geteuid(), getegid()) != -1) e(16);
if (errno != ENOENT) e(17);
/* Check non existing file name. */
if (chmod("non_exist", 0) != -1) e(18);
if (errno != ENOENT) e(19);
if (chown("non_exist", geteuid(), getegid()) != -1) e(20);
if (errno != ENOENT) e(21);
/* Check what we get if we do not have permisson. */
if (!superuser) {
Stat("/", &st);
if (st.st_uid == geteuid()) e(22);
/* First I had 0, I changed it to st.st_mode 8-). */
if (chmod("/", st.st_mode) != -1) e(23);
if (errno != EPERM) e(24);
}
if (!I_can_chown) {
Stat("/", &st);
if (st.st_uid == geteuid()) e(25);
if (chown("/", geteuid(), getegid()) != -1) e(26);
if (errno != EPERM) e(27);
}
/* If we are superuser, we can test all id combinations. */
if (superuser) {
switch (fork()) {
case -1: printf("Can't fork\n"); break;
case 0:
alarm(20);
getids(&uid, &gid);
if (uid == 0) {
getids(&uid, &gid);
if (uid == 0) e(28);
}
getids(&uid2, &gid2);
if (gid == gid2) e(29);
if (uid == uid2) e(30);
/* Creat boo, owned by root. */
Creat("boo");
if (chmod("boo", ALL_BITS) != 0) e(31);
/* Creat boo for user uid2 of group gid2. */
Creat("bar");
if (chown("bar", uid2, gid2) != 0) e(32);
if (chmod("bar", ALL_BITS) != 0) e(33);
/* Creat my_gid for user uid2 of group gid. */
Creat("my_gid");
if (chown("my_gid", uid2, gid) != 0) e(34);
if (chmod("my_gid", ALL_BITS) != 0) e(35);
/* Creat my_uid for user uid of uid gid. */
Creat("my_uid");
if (chown("my_uid", uid, gid) != 0) e(36);
if (chmod("my_uid", ALL_BITS) != 0) e(37);
/* We now become user uid of uid gid. */
setgid(gid);
setuid(uid);
if (chown("boo", uid, gid) != -1) e(38);
if (errno != EPERM) e(39);
if (chown("bar", uid, gid) != -1) e(40);
if (errno != EPERM) e(41);
if (chown("my_gid", uid, gid) != -1) e(42);
if (errno != EPERM) e(43);
if (chown("my_uid", uid, gid2) != -1) e(44);
/* The EPERM is not strict POSIX. */
if (errno != EPERM) e(45);
if (chmod("boo", 0) != -1) e(46);
if (errno != EPERM) e(47);
if (chmod("bar", 0) != -1) e(48);
if (errno != EPERM) e(49);
if (chmod("my_gid", 0) != -1) e(50);
if (errno != EPERM) e(51);
exit(0);
default:
wait(&stat_loc);
if (stat_loc != 0) e(52); /* Alarm? */
}
}
/* Check too long path ed. */
Creat(NameTooLong);
if (chmod(NameTooLong, 0777) != 0) e(57);
if (chown(NameTooLong, geteuid(), getegid()) != 0) e(58);
/* Make PathTooLong contain ././.../a */
PathTooLong[strlen(PathTooLong) - 2] = '/';
PathTooLong[strlen(PathTooLong) - 1] = 'a';
Creat("a");
if (chmod(PathTooLong, 0777) != -1) e(59);
if (errno != ENAMETOOLONG) e(60);
if (chown(PathTooLong, geteuid(), getegid()) != -1) e(61);
if (errno != ENAMETOOLONG) e(62);
(void) system("chmod 777 ../DIR_34/* > /dev/null 2> /dev/null");
System("rm -rf ../DIR_34/*");
}
void makelongnames()
{
register int i;
memset(MaxName, 'a', NAME_MAX);
MaxName[NAME_MAX] = '\0';
for (i = 0; i < PATH_MAX - 1; i++) { /* idem path */
MaxPath[i++] = '.';
MaxPath[i] = '/';
}
MaxPath[PATH_MAX - 1] = '\0';
strcpy(NameTooLong, MaxName); /* copy them Max to TooLong */
strcpy(PathTooLong, MaxPath);
NameTooLong[NAME_MAX] = 'a';
NameTooLong[NAME_MAX + 1] = '\0'; /* extend NameTooLong by one too many*/
PathTooLong[PATH_MAX - 1] = '/';
PathTooLong[PATH_MAX] = '\0'; /* inc PathTooLong by one */
}
void e(n)
int n;
{
int err_num = errno; /* Save in case printf clobbers it. */
printf("Subtest %d, error %d errno=%d: ", subtest, n, errno);
errno = err_num;
perror("");
if (errct++ > MAX_ERROR) {
printf("Too many errors; test aborted\n");
chdir("..");
system("rm -rf DIR*");
system("rm -rf DIR_34");
exit(1);
}
errno = 0;
}
void quit()
{
Chdir("..");
(void) system("chmod 777 DIR_34/* > /dev/null 2> /dev/null");
System("rm -rf DIR_34");
if (errct == 0) {
printf("ok\n");
exit(0);
} else {
printf("%d errors\n", errct);
exit(1);
}
}
/* Getids returns a valid uid and gid. Is used PASSWD FILE.
* It assumes the following format for a passwd file line:
* <user_name>:<passwd>:<uid>:<gid>:<other_stuff>
* If no uids and gids can be found, it will only return 0 ids.
*/
void getids(r_uid, r_gid)
uid_t * r_uid;
gid_t * r_gid;
{
char line[N];
char *p;
uid_t uid;
gid_t gid;
FILE *fp;
int i;
static uid_t a_uid[N]; /* Array for uids. */
static gid_t a_gid[N]; /* Array for gids. */
static int nuid = 0, ngid = 0;/* The number of user & group ids. */
static int cuid = 0, cgid = 0;/* The current id index. */
/* If we don't have any uids go read some from the passwd file. */
if (nuid == 0) {
a_uid[nuid++] = 0; /* Root uid and gid. */
a_gid[ngid++] = 0;
if ((fp = fopen(PASSWD_FILE, "r")) == NULL) {
printf("Can't open ");
perror(PASSWD_FILE);
}
while (fp != NULL && fgets(line, sizeof(line), fp) != NULL) {
p = strchr(line, ':');
if (p != NULL) p = strchr(p + 1, ':');
if (p != NULL) {
p++;
uid = 0;
while (isdigit(*p)) {
uid *= 10;
uid += (uid_t) (*p - '0');
p++;
}
if (*p != ':') continue;
p++;
gid = 0;
while (isdigit(*p)) {
gid *= 10;
gid += (gid_t) (*p - '0');
p++;
}
if (*p != ':') continue;
if (nuid < N) {
for (i = 0; i < nuid; i++)
if (a_uid[i] == uid) break;
if (i == nuid) a_uid[nuid++] = uid;
}
if (ngid < N) {
for (i = 0; i < ngid; i++)
if (a_gid[i] == gid) break;
if (i == ngid) a_gid[ngid++] = gid;
}
if (nuid >= N && ngid >= N) break;
}
}
if (fp != NULL) fclose(fp);
}
/* We now have uids and gids in a_uid and a_gid. */
if (cuid >= nuid) cuid = 0;
if (cgid >= ngid) cgid = 0;
*r_uid = a_uid[cuid++];
*r_gid = a_gid[cgid++];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -