test35.c

Go to the documentation of this file.
00001 /* test35: utime()              Author: Jan-Mark Wams (jms@cs.vu.nl) */
00002 
00003 #include <sys/types.h>
00004 #include <sys/stat.h>
00005 #include <sys/wait.h>
00006 #include <stdlib.h>
00007 #include <unistd.h>
00008 #include <string.h>
00009 #include <fcntl.h>
00010 #include <limits.h>
00011 #include <utime.h>
00012 #include <errno.h>
00013 #include <time.h>
00014 #include <ctype.h>
00015 #include <stdio.h>
00016 
00017 #define MAX_ERROR       1
00018 #define ITERATIONS     10
00019 #define N 100
00020 
00021 #define System(cmd)   if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
00022 #define Chdir(dir)    if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
00023 #define Stat(a,b)     if (stat(a,b) != 0) printf("Can't stat %s\n", a)
00024 #define Mkfifo(f)     if (mkfifo(f,0777)!=0) printf("Can't make fifo %s\n", f)
00025 #define Mkdir(f)      if (mkdir(f,0777)!=0) printf("Can't make dir %s\n", f)
00026 #define Creat(f)      if (close(creat(f,0777))!=0) printf("Can't creat %s\n",f)
00027 #define Time(t)       if (time(t) == (time_t)-1) printf("Time error\n")
00028 #define Chown(f,u,g)  if (chown(f,u,g) != 0) printf("Can't chown %s\n", f)
00029 #define Chmod(f,m)    if (chmod(f,m) != 0) printf("Can't chmod %s\n", f)
00030 
00031 #define PASSWD_FILE     "/etc/passwd"
00032 
00033 int errct = 0;
00034 int subtest = 1;
00035 int I_can_chown;
00036 int superuser;
00037 char MaxName[NAME_MAX + 1];     /* Name of maximum length */
00038 char MaxPath[PATH_MAX];         /* Same for path */
00039 char NameTooLong[NAME_MAX + 2]; /* Name of maximum +1 length */
00040 char PathTooLong[PATH_MAX + 1]; /* Same for path, both too long */
00041 
00042 _PROTOTYPE(void main, (int argc, char *argv[]));
00043 _PROTOTYPE(void test35a, (void));
00044 _PROTOTYPE(void test35b, (void));
00045 _PROTOTYPE(void test35c, (void));
00046 _PROTOTYPE(void makelongnames, (void));
00047 _PROTOTYPE(void e, (int number));
00048 _PROTOTYPE(void quit, (void));
00049 _PROTOTYPE(void getids, (uid_t * uid, gid_t * gid));
00050 
00051 void main(argc, argv)
00052 int argc;
00053 char *argv[];
00054 {
00055   int i, m = 0xFFFF;
00056 
00057   sync();
00058   if (argc == 2) m = atoi(argv[1]);
00059   printf("Test 35 ");
00060   fflush(stdout);
00061   System("rm -rf DIR_35; mkdir DIR_35");
00062   Chdir("DIR_35");
00063   makelongnames();
00064   superuser = (geteuid() == 0);
00065 
00066 #ifdef _POSIX_CHOWN_RESTRICTED
00067 # if _POSIX_CHOWN_RESTRICTED - 0 != -1
00068   I_can_chown = superuser;
00069 # else
00070   I_can_chown = 1;
00071 # endif
00072 #else
00073 # include "error, this case requires dynamic checks and is not handled"
00074 #endif
00075 
00076   for (i = 0; i < ITERATIONS; i++) {
00077         if (m & 0001) test35a();
00078         if (m & 0002) test35b();
00079         if (m & 0004) test35c();
00080   }
00081   quit();
00082 }
00083 
00084 void test35a()
00085 {                               /* Test normal operation. */
00086   struct stat st;
00087   struct utimbuf ub;
00088   time_t time1, time2;
00089   int cnt;
00090 
00091   subtest = 1;
00092 
00093   /* Creat scratch file. */
00094   Creat("foo");
00095 
00096   /* Set file times back two seconds. */
00097   Stat("foo", &st);
00098   ub.actime = st.st_atime - 2;
00099   ub.modtime = st.st_mtime - 2;
00100   Time(&time1);
00101   utime("foo", &ub);
00102   Time(&time2);
00103   Stat("foo", &st);
00104   if (ub.actime != st.st_atime) e(1);
00105   if (ub.modtime != st.st_mtime) e(2);
00106 
00107   /* The status changed time sould be changed. */
00108 #ifndef V1_FILESYSTEM
00109   if (st.st_ctime < time1) e(3);
00110 #endif
00111   if (st.st_ctime > time2) e(4);
00112 
00113   /* Add twenty seconds. */
00114   Stat("foo", &st);
00115   ub.actime = st.st_atime + 20;
00116   ub.modtime = st.st_mtime + 20;
00117   Time(&time1);
00118   utime("foo", &ub);
00119   Time(&time2);
00120   Stat("foo", &st);
00121   if (ub.actime != st.st_atime) e(5);
00122   if (ub.modtime != st.st_mtime) e(6);
00123   if (st.st_ctime < time1) e(7);
00124 #ifndef V1_FILESYSTEM
00125   if (st.st_ctime > time2) e(8);
00126 #endif
00127 
00128   /* Try 100 times to do utime in less than one second. */
00129   cnt = 0;
00130   do {
00131         Time(&time1);
00132         utime("foo", (struct utimbuf *) NULL);
00133         Time(&time2);
00134   } while (time1 != time2 && cnt++ < 100);
00135   if (time1 == time2) {
00136         Stat("foo", &st);
00137         Time(&time2);
00138         if (st.st_atime != time1) e(9);
00139         if (st.st_mtime != time1) e(10);
00140   } else {
00141         Stat("foo", &st);
00142         if (st.st_atime > time2) e(11);
00143         if (st.st_mtime > time2) e(12);
00144         Time(&time2);
00145         if (st.st_atime < time1) e(13);
00146         if (st.st_mtime < time1) e(14);
00147   }
00148   if (st.st_ctime < time1) e(15);
00149   if (st.st_ctime > time2) e(16);
00150 
00151   System("rm -rf ../DIR_35/*");
00152 }
00153 
00154 void test35b()
00155 {
00156   subtest = 2;
00157 
00158   /* MaxPath and MaxName checkup. */
00159   Creat(MaxName);
00160   MaxPath[strlen(MaxPath) - 2] = '/';
00161   MaxPath[strlen(MaxPath) - 1] = 'a';   /* make ././.../a */
00162   Creat(MaxPath);
00163   if (utime(MaxName, NULL) != 0) e(1);
00164   if (utime(MaxPath, NULL) != 0) e(2);
00165 
00166   /* The owner doesn't need write permisson to set  times. */
00167   Creat("foo");
00168   if (chmod("foo", 0) != 0) e(3);
00169   if (utime("foo", NULL) != 0) e(4);
00170   if (chmod("foo", 0777) != 0) e(5);
00171   if (utime("foo", NULL) != 0) e(6);
00172 
00173   System("rm -rf ../DIR_35/*");
00174 }
00175 
00176 void test35c()
00177 {
00178   gid_t gid, gid2;
00179   uid_t uid, uid2;
00180   struct utimbuf ub;
00181   int stat_loc;
00182 
00183   subtest = 3;
00184 
00185   /* Access problems. */
00186   Mkdir("bar");
00187   Creat("bar/tryme");
00188   if (superuser) {
00189         Chmod("bar", 0000);     /* No search permisson at all. */
00190         if (utime("bar/tryme", NULL) != 0) e(1);
00191   }
00192   if (!superuser) {
00193         Chmod("bar", 0677);     /* No search permisson. */
00194         if (utime("bar/tryme", NULL) != -1) e(2);
00195         if (errno != EACCES) e(3);
00196   }
00197   Chmod("bar", 0777);
00198 
00199   if (I_can_chown) {
00200         switch (fork()) {
00201             case -1:    printf("Can't fork\n"); break;
00202             case 0:
00203                 alarm(20);
00204 
00205                 /* Get two differend non root uids. */
00206                 if (superuser) {
00207                         getids(&uid, &gid);
00208                         if (uid == 0) getids(&uid, &gid);
00209                         if (uid == 0) e(4);
00210                 }
00211                 if (!superuser) {
00212                         uid = geteuid();
00213                         gid = getegid();
00214                 }
00215                 getids(&uid2, &gid);
00216                 if (uid == uid2) getids(&uid2, &gid2);
00217                 if (uid == uid2) e(5);
00218 
00219                 /* Creat a number of files for root, user and user2. */
00220                 Creat("rootfile");      /* Owned by root. */
00221                 Chmod("rootfile", 0600);
00222                 Chown("rootfile", 0, 0);
00223                 Creat("user2file");     /* Owned by user 2, writeable. */
00224                 Chmod("user2file", 0020);
00225                 Chown("user2file", uid2, gid);
00226                 Creat("user2private");  /* Owned by user 2, privately. */
00227                 Chmod("user2private", 0600);
00228                 Chown("user2private", uid2, gid);
00229 
00230                 if (superuser) {
00231                         setgid(gid);
00232                         setuid(uid);
00233                 }
00234 
00235                 /* We now are user ``uid'' from group ``gid''. */
00236                 ub.actime = (time_t) 12345L;
00237                 ub.modtime = (time_t) 12345L;
00238 
00239                 if (utime("rootfile", NULL) != -1) e(6);
00240                 if (errno != EACCES) e(7);
00241                 if (utime("rootfile", &ub) != -1) e(8);
00242                 if (errno != EPERM) e(9);
00243 
00244                 if (utime("user2file", NULL) != 0) e(10);
00245                 if (utime("user2file", &ub) != -1) e(11);
00246                 if (errno != EPERM) e(12);
00247 
00248                 if (utime("user2private", NULL) != -1) e(13);
00249                 if (errno != EACCES) e(14);
00250                 if (utime("user2private", &ub) != -1) e(15);
00251                 if (errno != EPERM) e(16);
00252 
00253                 exit(errct ? 1 : 0);
00254             default:
00255                 wait(&stat_loc);
00256                 if (stat_loc != 0) e(17);       /* Alarm? */
00257         }
00258   }
00259 
00260   /* Test names that are too long. */
00261 #ifdef _POSIX_NO_TRUNC
00262 # if _POSIX_NO_TRUNC - 0 != -1
00263   /* Not exist might also be a propper response? */
00264   if (utime(NameTooLong, NULL) != -1) e(18);
00265   if (errno != ENAMETOOLONG) e(19);
00266 # else
00267   Creat(NameTooLong);
00268   if (utime(NameTooLong, NULL) != 0) e(20);
00269 # endif
00270 #else
00271 # include "error, this case requires dynamic checks and is not handled"
00272 #endif
00273 
00274   /* Make PathTooLong contain ././.../a */
00275   PathTooLong[strlen(PathTooLong) - 2] = '/';
00276   PathTooLong[strlen(PathTooLong) - 1] = 'a';
00277   Creat("a");
00278   if (utime(PathTooLong, NULL) != -1) e(21);
00279   if (errno != ENAMETOOLONG) e(22);
00280 
00281   /* Non existing file name. */
00282   if (utime("nonexist", NULL) != -1) e(23);
00283   if (errno != ENOENT) e(24);
00284 
00285   /* Empty file name. */
00286   if (utime("", NULL) != -1) e(25);
00287   if (errno != ENOENT) e(26);
00288 
00289   System("rm -rf ../DIR_35/*");
00290 }
00291 
00292 void makelongnames()
00293 {
00294   register int i;
00295 
00296   memset(MaxName, 'a', NAME_MAX);
00297   MaxName[NAME_MAX] = '\0';
00298   for (i = 0; i < PATH_MAX - 1; i++) {  /* idem path */
00299         MaxPath[i++] = '.';
00300         MaxPath[i] = '/';
00301   }
00302   MaxPath[PATH_MAX - 1] = '\0';
00303 
00304   strcpy(NameTooLong, MaxName); /* copy them Max to TooLong */
00305   strcpy(PathTooLong, MaxPath);
00306 
00307   NameTooLong[NAME_MAX] = 'a';
00308   NameTooLong[NAME_MAX + 1] = '\0';     /* extend NameTooLong by one too many*/
00309   PathTooLong[PATH_MAX - 1] = '/';
00310   PathTooLong[PATH_MAX] = '\0'; /* inc PathTooLong by one */
00311 }
00312 
00313 void e(n)
00314 int n;
00315 {
00316   int err_num = errno;          /* Save in case printf clobbers it. */
00317 
00318   printf("Subtest %d,  error %d  errno=%d: ", subtest, n, errno);
00319   errno = err_num;
00320   perror("");
00321   if (errct++ > MAX_ERROR) {
00322         printf("Too many errors; test aborted\n");
00323         chdir("..");
00324         system("rm -rf DIR* > /dev/null 2>/dev/null");
00325         exit(1);
00326   }
00327   errno = 0;
00328 }
00329 
00330 void quit()
00331 {
00332   Chdir("..");
00333   System("rm -rf DIR_35");
00334 
00335   if (errct == 0) {
00336         printf("ok\n");
00337         exit(0);
00338   } else {
00339         printf("%d errors\n", errct);
00340         exit(1);
00341   }
00342 }
00343 
00344 /* Getids returns a valid uid and gid. Is used PASSWD FILE.
00345 ** It assumes the following format for a passwd file line:
00346 ** <user_name>:<passwd>:<uid>:<gid>:<other_stuff>
00347 ** If no uids and gids can be found, it will only return 0 ids.
00348 */
00349 void getids(r_uid, r_gid)
00350 uid_t *r_uid;
00351 gid_t *r_gid;
00352 {
00353   char line[N];
00354   char *p;
00355   uid_t uid;
00356   gid_t gid;
00357   FILE *fp;
00358   int i;
00359 
00360   static uid_t a_uid[N];        /* Array for uids. */
00361   static gid_t a_gid[N];        /* Array for gids. */
00362   static int nuid = 0, ngid = 0;/* The number of user & group ids. */
00363   static int cuid = 0, cgid = 0;/* The current id index. */
00364 
00365   /* If we don't have any uids go read some from the passwd file. */
00366   if (nuid == 0) {
00367         a_uid[nuid++] = 0;      /* Root uid and gid. */
00368         a_gid[ngid++] = 0;
00369         if ((fp = fopen(PASSWD_FILE, "r")) == NULL) {
00370                 printf("Can't open ");
00371                 perror(PASSWD_FILE);
00372         }
00373         while (fp != NULL && fgets(line, sizeof(line), fp) != NULL) {
00374                 p = strchr(line, ':');
00375                 if (p != NULL) p = strchr(p + 1, ':');
00376                 if (p != NULL) {
00377                         p++;
00378                         uid = 0;
00379                         while (isdigit(*p)) {
00380                                 uid *= 10;
00381                                 uid += (uid_t) (*p - '0');
00382                                 p++;
00383                         }
00384                         if (*p != ':') continue;
00385                         p++;
00386                         gid = 0;
00387                         while (isdigit(*p)) {
00388                                 gid *= 10;
00389                                 gid += (gid_t) (*p - '0');
00390                                 p++;
00391                         }
00392                         if (*p != ':') continue;
00393                         if (nuid < N) {
00394                                 for (i = 0; i < nuid; i++)
00395                                         if (a_uid[i] == uid) break;
00396                                 if (i == nuid) a_uid[nuid++] = uid;
00397                         }
00398                         if (ngid < N) {
00399                                 for (i = 0; i < ngid; i++)
00400                                         if (a_gid[i] == gid) break;
00401                                 if (i == ngid) a_gid[ngid++] = gid;
00402                         }
00403                         if (nuid >= N && ngid >= N) break;
00404                 }
00405         }
00406         if (fp != NULL) fclose(fp);
00407   }
00408 
00409   /* We now have uids and gids in a_uid and a_gid. */
00410   if (cuid >= nuid) cuid = 0;
00411   if (cgid >= ngid) cgid = 0;
00412   *r_uid = a_uid[cuid++];
00413   *r_gid = a_gid[cgid++];
00414 }

Generated on Fri Apr 14 22:57:35 2006 for minix by  doxygen 1.4.6