sel_subs.c

Go to the documentation of this file.
00001 /*-
00002  * Copyright (c) 1992 Keith Muller.
00003  * Copyright (c) 1992, 1993
00004  *      The Regents of the University of California.  All rights reserved.
00005  *
00006  * This code is derived from software contributed to Berkeley by
00007  * Keith Muller of the University of California, San Diego.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions
00011  * are met:
00012  * 1. Redistributions of source code must retain the above copyright
00013  *    notice, this list of conditions and the following disclaimer.
00014  * 2. Redistributions in binary form must reproduce the above copyright
00015  *    notice, this list of conditions and the following disclaimer in the
00016  *    documentation and/or other materials provided with the distribution.
00017  * 4. Neither the name of the University nor the names of its contributors
00018  *    may be used to endorse or promote products derived from this software
00019  *    without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00022  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00023  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00024  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00025  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00026  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00027  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00028  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00030  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00031  * SUCH DAMAGE.
00032  */
00033 
00034 #ifndef lint
00035 #if 0
00036 static char sccsid[] = "@(#)sel_subs.c  8.1 (Berkeley) 5/31/93";
00037 #endif
00038 #endif /* not lint */
00039 
00040 #include <sys/types.h>
00041 #include <sys/time.h>
00042 #include <sys/stat.h>
00043 #include <pwd.h>
00044 #include <time.h>
00045 #include <grp.h>
00046 #include <stdio.h>
00047 #include <string.h>
00048 #include <strings.h>
00049 #include <unistd.h>
00050 #include <stdlib.h>
00051 #include "pax.h"
00052 #include "sel_subs.h"
00053 #include "extern.h"
00054 
00055 static int str_sec(char *, time_t *);
00056 static int usr_match(ARCHD *);
00057 static int grp_match(ARCHD *);
00058 static int trng_match(ARCHD *);
00059 
00060 static TIME_RNG *trhead = NULL;         /* time range list head */
00061 static TIME_RNG *trtail = NULL;         /* time range list tail */
00062 static USRT **usrtb = NULL;             /* user selection table */
00063 static GRPT **grptb = NULL;             /* group selection table */
00064 
00065 /*
00066  * Routines for selection of archive members
00067  */
00068 
00069 /*
00070  * sel_chk()
00071  *      check if this file matches a specified uid, gid or time range
00072  * Return:
00073  *      0 if this archive member should be processed, 1 if it should be skipped
00074  */
00075 
00076 int
00077 sel_chk(ARCHD *arcn)
00078 {
00079         if (((usrtb != NULL) && usr_match(arcn)) ||
00080             ((grptb != NULL) && grp_match(arcn)) ||
00081             ((trhead != NULL) && trng_match(arcn)))
00082                 return(1);
00083         return(0);
00084 }
00085 
00086 /*
00087  * User/group selection routines
00088  *
00089  * Routines to handle user selection of files based on the file uid/gid. To
00090  * add an entry, the user supplies either then name or the uid/gid starting with
00091  * a # on the command line. A \# will escape the #.
00092  */
00093 
00094 /*
00095  * usr_add()
00096  *      add a user match to the user match hash table
00097  * Return:
00098  *      0 if added ok, -1 otherwise;
00099  */
00100 
00101 int
00102 usr_add(char *str)
00103 {
00104         u_int indx;
00105         USRT *pt;
00106         struct passwd *pw;
00107         uid_t uid;
00108 
00109         /*
00110          * create the table if it doesn't exist
00111          */
00112         if ((str == NULL) || (*str == '\0'))
00113                 return(-1);
00114         if ((usrtb == NULL) &&
00115             ((usrtb = (USRT **)calloc(USR_TB_SZ, sizeof(USRT *))) == NULL)) {
00116                 paxwarn(1, "Unable to allocate memory for user selection table");
00117                 return(-1);
00118         }
00119 
00120         /*
00121          * figure out user spec
00122          */
00123         if (str[0] != '#') {
00124                 /*
00125                  * it is a user name, \# escapes # as first char in user name
00126                  */
00127                 if ((str[0] == '\\') && (str[1] == '#'))
00128                         ++str;
00129                 if ((pw = getpwnam(str)) == NULL) {
00130                         paxwarn(1, "Unable to find uid for user: %s", str);
00131                         return(-1);
00132                 }
00133                 uid = (uid_t)pw->pw_uid;
00134         } else
00135 #               ifdef NET2_STAT
00136                 uid = (uid_t)atoi(str+1);
00137 #               else
00138                 uid = (uid_t)strtoul(str+1, NULL, 10);
00139 #               endif
00140         endpwent();
00141 
00142         /*
00143          * hash it and go down the hash chain (if any) looking for it
00144          */
00145         indx = ((unsigned)uid) % USR_TB_SZ;
00146         if ((pt = usrtb[indx]) != NULL) {
00147                 while (pt != NULL) {
00148                         if (pt->uid == uid)
00149                                 return(0);
00150                         pt = pt->fow;
00151                 }
00152         }
00153 
00154         /*
00155          * uid is not yet in the table, add it to the front of the chain
00156          */
00157         if ((pt = (USRT *)malloc(sizeof(USRT))) != NULL) {
00158                 pt->uid = uid;
00159                 pt->fow = usrtb[indx];
00160                 usrtb[indx] = pt;
00161                 return(0);
00162         }
00163         paxwarn(1, "User selection table out of memory");
00164         return(-1);
00165 }
00166 
00167 /*
00168  * usr_match()
00169  *      check if this files uid matches a selected uid.
00170  * Return:
00171  *      0 if this archive member should be processed, 1 if it should be skipped
00172  */
00173 
00174 static int
00175 usr_match(ARCHD *arcn)
00176 {
00177         USRT *pt;
00178 
00179         /*
00180          * hash and look for it in the table
00181          */
00182         pt = usrtb[((unsigned)arcn->sb.st_uid) % USR_TB_SZ];
00183         while (pt != NULL) {
00184                 if (pt->uid == arcn->sb.st_uid)
00185                         return(0);
00186                 pt = pt->fow;
00187         }
00188 
00189         /*
00190          * not found
00191          */
00192         return(1);
00193 }
00194 
00195 /*
00196  * grp_add()
00197  *      add a group match to the group match hash table
00198  * Return:
00199  *      0 if added ok, -1 otherwise;
00200  */
00201 
00202 int
00203 grp_add(char *str)
00204 {
00205         u_int indx;
00206         GRPT *pt;
00207         struct group *gr;
00208         gid_t gid;
00209 
00210         /*
00211          * create the table if it doesn't exist
00212          */
00213         if ((str == NULL) || (*str == '\0'))
00214                 return(-1);
00215         if ((grptb == NULL) &&
00216             ((grptb = (GRPT **)calloc(GRP_TB_SZ, sizeof(GRPT *))) == NULL)) {
00217                 paxwarn(1, "Unable to allocate memory fo group selection table");
00218                 return(-1);
00219         }
00220 
00221         /*
00222          * figure out user spec
00223          */
00224         if (str[0] != '#') {
00225                 /*
00226                  * it is a group name, \# escapes # as first char in group name
00227                  */
00228                 if ((str[0] == '\\') && (str[1] == '#'))
00229                         ++str;
00230                 if ((gr = getgrnam(str)) == NULL) {
00231                         paxwarn(1,"Cannot determine gid for group name: %s", str);
00232                         return(-1);
00233                 }
00234                 gid = gr->gr_gid;
00235         } else
00236 #               ifdef NET2_STAT
00237                 gid = (gid_t)atoi(str+1);
00238 #               else
00239                 gid = (gid_t)strtoul(str+1, NULL, 10);
00240 #               endif
00241         endgrent();
00242 
00243         /*
00244          * hash it and go down the hash chain (if any) looking for it
00245          */
00246         indx = ((unsigned)gid) % GRP_TB_SZ;
00247         if ((pt = grptb[indx]) != NULL) {
00248                 while (pt != NULL) {
00249                         if (pt->gid == gid)
00250                                 return(0);
00251                         pt = pt->fow;
00252                 }
00253         }
00254 
00255         /*
00256          * gid not in the table, add it to the front of the chain
00257          */
00258         if ((pt = (GRPT *)malloc(sizeof(GRPT))) != NULL) {
00259                 pt->gid = gid;
00260                 pt->fow = grptb[indx];
00261                 grptb[indx] = pt;
00262                 return(0);
00263         }
00264         paxwarn(1, "Group selection table out of memory");
00265         return(-1);
00266 }
00267 
00268 /*
00269  * grp_match()
00270  *      check if this files gid matches a selected gid.
00271  * Return:
00272  *      0 if this archive member should be processed, 1 if it should be skipped
00273  */
00274 
00275 static int
00276 grp_match(ARCHD *arcn)
00277 {
00278         GRPT *pt;
00279 
00280         /*
00281          * hash and look for it in the table
00282          */
00283         pt = grptb[((unsigned)arcn->sb.st_gid) % GRP_TB_SZ];
00284         while (pt != NULL) {
00285                 if (pt->gid == arcn->sb.st_gid)
00286                         return(0);
00287                 pt = pt->fow;
00288         }
00289 
00290         /*
00291          * not found
00292          */
00293         return(1);
00294 }
00295 
00296 /*
00297  * Time range selection routines
00298  *
00299  * Routines to handle user selection of files based on the modification and/or
00300  * inode change time falling within a specified time range (the non-standard
00301  * -T flag). The user may specify any number of different file time ranges.
00302  * Time ranges are checked one at a time until a match is found (if at all).
00303  * If the file has a mtime (and/or ctime) which lies within one of the time
00304  * ranges, the file is selected. Time ranges may have a lower and/or an upper
00305  * value. These ranges are inclusive. When no time ranges are supplied to pax
00306  * with the -T option, all members in the archive will be selected by the time
00307  * range routines. When only a lower range is supplied, only files with a
00308  * mtime (and/or ctime) equal to or younger are selected. When only an upper
00309  * range is supplied, only files with a mtime (and/or ctime) equal to or older
00310  * are selected. When the lower time range is equal to the upper time range,
00311  * only files with a mtime (or ctime) of exactly that time are selected.
00312  */
00313 
00314 /*
00315  * trng_add()
00316  *      add a time range match to the time range list.
00317  *      This is a non-standard pax option. Lower and upper ranges are in the
00318  *      format: [yy[mm[dd[hh]]]]mm[.ss] and are comma separated.
00319  *      Time ranges are based on current time, so 1234 would specify a time of
00320  *      12:34 today.
00321  * Return:
00322  *      0 if the time range was added to the list, -1 otherwise
00323  */
00324 
00325 int
00326 trng_add(char *str)
00327 {
00328         TIME_RNG *pt;
00329         char *up_pt = NULL;
00330         char *stpt;
00331         char *flgpt;
00332         int dot = 0;
00333 
00334         /*
00335          * throw out the badly formed time ranges
00336          */
00337         if ((str == NULL) || (*str == '\0')) {
00338                 paxwarn(1, "Empty time range string");
00339                 return(-1);
00340         }
00341 
00342         /*
00343          * locate optional flags suffix /{cm}.
00344          */
00345         if ((flgpt = strrchr(str, '/')) != NULL)
00346                 *flgpt++ = '\0';
00347 
00348         for (stpt = str; *stpt != '\0'; ++stpt) {
00349                 if ((*stpt >= '0') && (*stpt <= '9'))
00350                         continue;
00351                 if ((*stpt == ',') && (up_pt == NULL)) {
00352                         *stpt = '\0';
00353                         up_pt = stpt + 1;
00354                         dot = 0;
00355                         continue;
00356                 }
00357 
00358                 /*
00359                  * allow only one dot per range (secs)
00360                  */
00361                 if ((*stpt == '.') && (!dot)) {
00362                         ++dot;
00363                         continue;
00364                 }
00365                 paxwarn(1, "Improperly specified time range: %s", str);
00366                 goto out;
00367         }
00368 
00369         /*
00370          * allocate space for the time range and store the limits
00371          */
00372         if ((pt = (TIME_RNG *)malloc(sizeof(TIME_RNG))) == NULL) {
00373                 paxwarn(1, "Unable to allocate memory for time range");
00374                 return(-1);
00375         }
00376 
00377         /*
00378          * by default we only will check file mtime, but usee can specify
00379          * mtime, ctime (inode change time) or both.
00380          */
00381         if ((flgpt == NULL) || (*flgpt == '\0'))
00382                 pt->flgs = CMPMTME;
00383         else {
00384                 pt->flgs = 0;
00385                 while (*flgpt != '\0') {
00386                         switch(*flgpt) {
00387                         case 'M':
00388                         case 'm':
00389                                 pt->flgs |= CMPMTME;
00390                                 break;
00391                         case 'C':
00392                         case 'c':
00393                                 pt->flgs |= CMPCTME;
00394                                 break;
00395                         default:
00396                                 paxwarn(1, "Bad option %c with time range %s",
00397                                     *flgpt, str);
00398                                 goto out;
00399                         }
00400                         ++flgpt;
00401                 }
00402         }
00403 
00404         /*
00405          * start off with the current time
00406          */
00407         pt->low_time = pt->high_time = time(NULL);
00408         if (*str != '\0') {
00409                 /*
00410                  * add lower limit
00411                  */
00412                 if (str_sec(str, &(pt->low_time)) < 0) {
00413                         paxwarn(1, "Illegal lower time range %s", str);
00414                         (void)free((char *)pt);
00415                         goto out;
00416                 }
00417                 pt->flgs |= HASLOW;
00418         }
00419 
00420         if ((up_pt != NULL) && (*up_pt != '\0')) {
00421                 /*
00422                  * add upper limit
00423                  */
00424                 if (str_sec(up_pt, &(pt->high_time)) < 0) {
00425                         paxwarn(1, "Illegal upper time range %s", up_pt);
00426                         (void)free((char *)pt);
00427                         goto out;
00428                 }
00429                 pt->flgs |= HASHIGH;
00430 
00431                 /*
00432                  * check that the upper and lower do not overlap
00433                  */
00434                 if (pt->flgs & HASLOW) {
00435                         if (pt->low_time > pt->high_time) {
00436                                 paxwarn(1, "Upper %s and lower %s time overlap",
00437                                         up_pt, str);
00438                                 (void)free((char *)pt);
00439                                 return(-1);
00440                         }
00441                 }
00442         }
00443 
00444         pt->fow = NULL;
00445         if (trhead == NULL) {
00446                 trtail = trhead = pt;
00447                 return(0);
00448         }
00449         trtail->fow = pt;
00450         trtail = pt;
00451         return(0);
00452 
00453     out:
00454         paxwarn(1, "Time range format is: [yy[mm[dd[hh]]]]mm[.ss][/[c][m]]");
00455         return(-1);
00456 }
00457 
00458 /*
00459  * trng_match()
00460  *      check if this files mtime/ctime falls within any supplied time range.
00461  * Return:
00462  *      0 if this archive member should be processed, 1 if it should be skipped
00463  */
00464 
00465 static int
00466 trng_match(ARCHD *arcn)
00467 {
00468         TIME_RNG *pt;
00469 
00470         /*
00471          * have to search down the list one at a time looking for a match.
00472          * remember time range limits are inclusive.
00473          */
00474         pt = trhead;
00475         while (pt != NULL) {
00476                 switch(pt->flgs & CMPBOTH) {
00477                 case CMPBOTH:
00478                         /*
00479                          * user wants both mtime and ctime checked for this
00480                          * time range
00481                          */
00482                         if (((pt->flgs & HASLOW) &&
00483                             (arcn->sb.st_mtime < pt->low_time) &&
00484                             (arcn->sb.st_ctime < pt->low_time)) ||
00485                             ((pt->flgs & HASHIGH) &&
00486                             (arcn->sb.st_mtime > pt->high_time) &&
00487                             (arcn->sb.st_ctime > pt->high_time))) {
00488                                 pt = pt->fow;
00489                                 continue;
00490                         }
00491                         break;
00492                 case CMPCTME:
00493                         /*
00494                          * user wants only ctime checked for this time range
00495                          */
00496                         if (((pt->flgs & HASLOW) &&
00497                             (arcn->sb.st_ctime < pt->low_time)) ||
00498                             ((pt->flgs & HASHIGH) &&
00499                             (arcn->sb.st_ctime > pt->high_time))) {
00500                                 pt = pt->fow;
00501                                 continue;
00502                         }
00503                         break;
00504                 case CMPMTME:
00505                 default:
00506                         /*
00507                          * user wants only mtime checked for this time range
00508                          */
00509                         if (((pt->flgs & HASLOW) &&
00510                             (arcn->sb.st_mtime < pt->low_time)) ||
00511                             ((pt->flgs & HASHIGH) &&
00512                             (arcn->sb.st_mtime > pt->high_time))) {
00513                                 pt = pt->fow;
00514                                 continue;
00515                         }
00516                         break;
00517                 }
00518                 break;
00519         }
00520 
00521         if (pt == NULL)
00522                 return(1);
00523         return(0);
00524 }
00525 
00526 /*
00527  * str_sec()
00528  *      Convert a time string in the format of [yy[mm[dd[hh]]]]mm[.ss] to gmt
00529  *      seconds. Tval already has current time loaded into it at entry.
00530  * Return:
00531  *      0 if converted ok, -1 otherwise
00532  */
00533 
00534 static int
00535 str_sec(char *str, time_t *tval)
00536 {
00537         struct tm *lt;
00538         char *dot = NULL;
00539 
00540         lt = localtime(tval);
00541         if ((dot = strchr(str, '.')) != NULL) {
00542                 /*
00543                  * seconds (.ss)
00544                  */
00545                 *dot++ = '\0';
00546                 if (strlen(dot) != 2)
00547                         return(-1);
00548                 if ((lt->tm_sec = ATOI2(dot)) > 61)
00549                         return(-1);
00550         } else
00551                 lt->tm_sec = 0;
00552 
00553         switch (strlen(str)) {
00554         case 10:
00555                 /*
00556                  * year (yy)
00557                  * watch out for year 2000
00558                  */
00559                 if ((lt->tm_year = ATOI2(str)) < 69)
00560                         lt->tm_year += 100;
00561                 str += 2;
00562                 /* FALLTHROUGH */
00563         case 8:
00564                 /*
00565                  * month (mm)
00566                  * watch out months are from 0 - 11 internally
00567                  */
00568                 if ((lt->tm_mon = ATOI2(str)) > 12)
00569                         return(-1);
00570                 --lt->tm_mon;
00571                 str += 2;
00572                 /* FALLTHROUGH */
00573         case 6:
00574                 /*
00575                  * day (dd)
00576                  */
00577                 if ((lt->tm_mday = ATOI2(str)) > 31)
00578                         return(-1);
00579                 str += 2;
00580                 /* FALLTHROUGH */
00581         case 4:
00582                 /*
00583                  * hour (hh)
00584                  */
00585                 if ((lt->tm_hour = ATOI2(str)) > 23)
00586                         return(-1);
00587                 str += 2;
00588                 /* FALLTHROUGH */
00589         case 2:
00590                 /*
00591                  * minute (mm)
00592                  */
00593                 if ((lt->tm_min = ATOI2(str)) > 59)
00594                         return(-1);
00595                 break;
00596         default:
00597                 return(-1);
00598         }
00599         /*
00600          * convert broken-down time to GMT clock time seconds
00601          */
00602         if ((*tval = mktime(lt)) == -1)
00603                 return(-1);
00604         return(0);
00605 }

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