/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1992 Keith Muller.4* Copyright (c) 1992, 19935* The Regents of the University of California. All rights reserved.6*7* This code is derived from software contributed to Berkeley by8* Keith Muller of the University of California, San Diego.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions and the following disclaimer.15* 2. Redistributions in binary form must reproduce the above copyright16* notice, this list of conditions and the following disclaimer in the17* documentation and/or other materials provided with the distribution.18* 3. Neither the name of the University nor the names of its contributors19* may be used to endorse or promote products derived from this software20* without specific prior written permission.21*22* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND23* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE24* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE25* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE26* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS28* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)29* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY31* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF32* SUCH DAMAGE.33*/3435#include <sys/types.h>36#include <sys/time.h>37#include <sys/stat.h>3839#include <ctype.h>40#include <grp.h>41#include <pwd.h>42#include <stdio.h>43#include <stdlib.h>44#include <string.h>45#include <strings.h>4647#include "pax.h"48#include "sel_subs.h"49#include "extern.h"5051static int str_sec(const char *, time_t *);52static int usr_match(ARCHD *);53static int grp_match(ARCHD *);54static int trng_match(ARCHD *);5556static TIME_RNG *trhead = NULL; /* time range list head */57static TIME_RNG *trtail = NULL; /* time range list tail */58static USRT **usrtb = NULL; /* user selection table */59static GRPT **grptb = NULL; /* group selection table */6061/*62* Routines for selection of archive members63*/6465/*66* sel_chk()67* check if this file matches a specified uid, gid or time range68* Return:69* 0 if this archive member should be processed, 1 if it should be skipped70*/7172int73sel_chk(ARCHD *arcn)74{75if (((usrtb != NULL) && usr_match(arcn)) ||76((grptb != NULL) && grp_match(arcn)) ||77((trhead != NULL) && trng_match(arcn)))78return(1);79return(0);80}8182/*83* User/group selection routines84*85* Routines to handle user selection of files based on the file uid/gid. To86* add an entry, the user supplies either the name or the uid/gid starting with87* a # on the command line. A \# will escape the #.88*/8990/*91* usr_add()92* add a user match to the user match hash table93* Return:94* 0 if added ok, -1 otherwise;95*/9697int98usr_add(char *str)99{100u_int indx;101USRT *pt;102struct passwd *pw;103uid_t uid;104105/*106* create the table if it doesn't exist107*/108if ((str == NULL) || (*str == '\0'))109return(-1);110if ((usrtb == NULL) &&111((usrtb = (USRT **)calloc(USR_TB_SZ, sizeof(USRT *))) == NULL)) {112paxwarn(1, "Unable to allocate memory for user selection table");113return(-1);114}115116/*117* figure out user spec118*/119if (str[0] != '#') {120/*121* it is a user name, \# escapes # as first char in user name122*/123if ((str[0] == '\\') && (str[1] == '#'))124++str;125if ((pw = getpwnam(str)) == NULL) {126paxwarn(1, "Unable to find uid for user: %s", str);127return(-1);128}129uid = (uid_t)pw->pw_uid;130} else131uid = (uid_t)strtoul(str+1, NULL, 10);132endpwent();133134/*135* hash it and go down the hash chain (if any) looking for it136*/137indx = ((unsigned)uid) % USR_TB_SZ;138if ((pt = usrtb[indx]) != NULL) {139while (pt != NULL) {140if (pt->uid == uid)141return(0);142pt = pt->fow;143}144}145146/*147* uid is not yet in the table, add it to the front of the chain148*/149if ((pt = (USRT *)malloc(sizeof(USRT))) != NULL) {150pt->uid = uid;151pt->fow = usrtb[indx];152usrtb[indx] = pt;153return(0);154}155paxwarn(1, "User selection table out of memory");156return(-1);157}158159/*160* usr_match()161* check if this files uid matches a selected uid.162* Return:163* 0 if this archive member should be processed, 1 if it should be skipped164*/165166static int167usr_match(ARCHD *arcn)168{169USRT *pt;170171/*172* hash and look for it in the table173*/174pt = usrtb[((unsigned)arcn->sb.st_uid) % USR_TB_SZ];175while (pt != NULL) {176if (pt->uid == arcn->sb.st_uid)177return(0);178pt = pt->fow;179}180181/*182* not found183*/184return(1);185}186187/*188* grp_add()189* add a group match to the group match hash table190* Return:191* 0 if added ok, -1 otherwise;192*/193194int195grp_add(char *str)196{197u_int indx;198GRPT *pt;199struct group *gr;200gid_t gid;201202/*203* create the table if it doesn't exist204*/205if ((str == NULL) || (*str == '\0'))206return(-1);207if ((grptb == NULL) &&208((grptb = (GRPT **)calloc(GRP_TB_SZ, sizeof(GRPT *))) == NULL)) {209paxwarn(1, "Unable to allocate memory fo group selection table");210return(-1);211}212213/*214* figure out user spec215*/216if (str[0] != '#') {217/*218* it is a group name, \# escapes # as first char in group name219*/220if ((str[0] == '\\') && (str[1] == '#'))221++str;222if ((gr = getgrnam(str)) == NULL) {223paxwarn(1,"Cannot determine gid for group name: %s", str);224return(-1);225}226gid = gr->gr_gid;227} else228gid = (gid_t)strtoul(str+1, NULL, 10);229endgrent();230231/*232* hash it and go down the hash chain (if any) looking for it233*/234indx = ((unsigned)gid) % GRP_TB_SZ;235if ((pt = grptb[indx]) != NULL) {236while (pt != NULL) {237if (pt->gid == gid)238return(0);239pt = pt->fow;240}241}242243/*244* gid not in the table, add it to the front of the chain245*/246if ((pt = (GRPT *)malloc(sizeof(GRPT))) != NULL) {247pt->gid = gid;248pt->fow = grptb[indx];249grptb[indx] = pt;250return(0);251}252paxwarn(1, "Group selection table out of memory");253return(-1);254}255256/*257* grp_match()258* check if this files gid matches a selected gid.259* Return:260* 0 if this archive member should be processed, 1 if it should be skipped261*/262263static int264grp_match(ARCHD *arcn)265{266GRPT *pt;267268/*269* hash and look for it in the table270*/271pt = grptb[((unsigned)arcn->sb.st_gid) % GRP_TB_SZ];272while (pt != NULL) {273if (pt->gid == arcn->sb.st_gid)274return(0);275pt = pt->fow;276}277278/*279* not found280*/281return(1);282}283284/*285* Time range selection routines286*287* Routines to handle user selection of files based on the modification and/or288* inode change time falling within a specified time range (the non-standard289* -T flag). The user may specify any number of different file time ranges.290* Time ranges are checked one at a time until a match is found (if at all).291* If the file has a mtime (and/or ctime) which lies within one of the time292* ranges, the file is selected. Time ranges may have a lower and/or an upper293* value. These ranges are inclusive. When no time ranges are supplied to pax294* with the -T option, all members in the archive will be selected by the time295* range routines. When only a lower range is supplied, only files with a296* mtime (and/or ctime) equal to or younger are selected. When only an upper297* range is supplied, only files with a mtime (and/or ctime) equal to or older298* are selected. When the lower time range is equal to the upper time range,299* only files with a mtime (or ctime) of exactly that time are selected.300*/301302/*303* trng_add()304* add a time range match to the time range list.305* This is a non-standard pax option. Lower and upper ranges are in the306* format: [[[[[cc]yy]mm]dd]HH]MM[.SS] and are comma separated.307* Time ranges are based on current time, so 1234 would specify a time of308* 12:34 today.309* Return:310* 0 if the time range was added to the list, -1 otherwise311*/312313int314trng_add(char *str)315{316TIME_RNG *pt;317char *up_pt = NULL;318char *stpt;319char *flgpt;320int dot = 0;321322/*323* throw out the badly formed time ranges324*/325if ((str == NULL) || (*str == '\0')) {326paxwarn(1, "Empty time range string");327return(-1);328}329330/*331* locate optional flags suffix /{cm}.332*/333if ((flgpt = strrchr(str, '/')) != NULL)334*flgpt++ = '\0';335336for (stpt = str; *stpt != '\0'; ++stpt) {337if ((*stpt >= '0') && (*stpt <= '9'))338continue;339if ((*stpt == ',') && (up_pt == NULL)) {340*stpt = '\0';341up_pt = stpt + 1;342dot = 0;343continue;344}345346/*347* allow only one dot per range (secs)348*/349if ((*stpt == '.') && (!dot)) {350++dot;351continue;352}353paxwarn(1, "Improperly specified time range: %s", str);354goto out;355}356357/*358* allocate space for the time range and store the limits359*/360if ((pt = (TIME_RNG *)malloc(sizeof(TIME_RNG))) == NULL) {361paxwarn(1, "Unable to allocate memory for time range");362return(-1);363}364365/*366* by default we only will check file mtime, but the user can specify367* mtime, ctime (inode change time) or both.368*/369if ((flgpt == NULL) || (*flgpt == '\0'))370pt->flgs = CMPMTME;371else {372pt->flgs = 0;373while (*flgpt != '\0') {374switch(*flgpt) {375case 'M':376case 'm':377pt->flgs |= CMPMTME;378break;379case 'C':380case 'c':381pt->flgs |= CMPCTME;382break;383default:384paxwarn(1, "Bad option %c with time range %s",385*flgpt, str);386free(pt);387goto out;388}389++flgpt;390}391}392393/*394* start off with the current time395*/396pt->low_time = pt->high_time = time(NULL);397if (*str != '\0') {398/*399* add lower limit400*/401if (str_sec(str, &(pt->low_time)) < 0) {402paxwarn(1, "Illegal lower time range %s", str);403free(pt);404goto out;405}406pt->flgs |= HASLOW;407}408409if ((up_pt != NULL) && (*up_pt != '\0')) {410/*411* add upper limit412*/413if (str_sec(up_pt, &(pt->high_time)) < 0) {414paxwarn(1, "Illegal upper time range %s", up_pt);415free(pt);416goto out;417}418pt->flgs |= HASHIGH;419420/*421* check that the upper and lower do not overlap422*/423if (pt->flgs & HASLOW) {424if (pt->low_time > pt->high_time) {425paxwarn(1, "Upper %s and lower %s time overlap",426up_pt, str);427free(pt);428return(-1);429}430}431}432433pt->fow = NULL;434if (trhead == NULL) {435trtail = trhead = pt;436return(0);437}438trtail->fow = pt;439trtail = pt;440return(0);441442out:443paxwarn(1, "Time range format is: [[[[[cc]yy]mm]dd]HH]MM[.SS][/[c][m]]");444return(-1);445}446447/*448* trng_match()449* check if this files mtime/ctime falls within any supplied time range.450* Return:451* 0 if this archive member should be processed, 1 if it should be skipped452*/453454static int455trng_match(ARCHD *arcn)456{457TIME_RNG *pt;458459/*460* have to search down the list one at a time looking for a match.461* remember time range limits are inclusive.462*/463pt = trhead;464while (pt != NULL) {465switch(pt->flgs & CMPBOTH) {466case CMPBOTH:467/*468* user wants both mtime and ctime checked for this469* time range470*/471if (((pt->flgs & HASLOW) &&472(arcn->sb.st_mtime < pt->low_time) &&473(arcn->sb.st_ctime < pt->low_time)) ||474((pt->flgs & HASHIGH) &&475(arcn->sb.st_mtime > pt->high_time) &&476(arcn->sb.st_ctime > pt->high_time))) {477pt = pt->fow;478continue;479}480break;481case CMPCTME:482/*483* user wants only ctime checked for this time range484*/485if (((pt->flgs & HASLOW) &&486(arcn->sb.st_ctime < pt->low_time)) ||487((pt->flgs & HASHIGH) &&488(arcn->sb.st_ctime > pt->high_time))) {489pt = pt->fow;490continue;491}492break;493case CMPMTME:494default:495/*496* user wants only mtime checked for this time range497*/498if (((pt->flgs & HASLOW) &&499(arcn->sb.st_mtime < pt->low_time)) ||500((pt->flgs & HASHIGH) &&501(arcn->sb.st_mtime > pt->high_time))) {502pt = pt->fow;503continue;504}505break;506}507break;508}509510if (pt == NULL)511return(1);512return(0);513}514515/*516* str_sec()517* Convert a time string in the format of [[[[[cc]yy]mm]dd]HH]MM[.SS] to518* seconds UTC. Tval already has current time loaded into it at entry.519* Return:520* 0 if converted ok, -1 otherwise521*/522523static int524str_sec(const char *p, time_t *tval)525{526struct tm *lt;527const char *dot, *t;528size_t len;529int bigyear;530int yearset;531532yearset = 0;533len = strlen(p);534535for (t = p, dot = NULL; *t; ++t) {536if (isdigit((unsigned char)*t))537continue;538if (*t == '.' && dot == NULL) {539dot = t;540continue;541}542return(-1);543}544545lt = localtime(tval);546547if (dot != NULL) { /* .SS */548if (strlen(++dot) != 2)549return(-1);550lt->tm_sec = ATOI2(dot);551if (lt->tm_sec > 61)552return(-1);553len -= 3;554} else555lt->tm_sec = 0;556557switch (len) {558case 12: /* cc */559bigyear = ATOI2(p);560lt->tm_year = (bigyear * 100) - 1900;561yearset = 1;562/* FALLTHROUGH */563case 10: /* yy */564if (yearset) {565lt->tm_year += ATOI2(p);566} else {567lt->tm_year = ATOI2(p);568if (lt->tm_year < 69) /* hack for 2000 ;-} */569lt->tm_year += (2000 - 1900);570}571/* FALLTHROUGH */572case 8: /* mm */573lt->tm_mon = ATOI2(p);574if ((lt->tm_mon > 12) || !lt->tm_mon)575return(-1);576--lt->tm_mon; /* time struct is 0 - 11 */577/* FALLTHROUGH */578case 6: /* dd */579lt->tm_mday = ATOI2(p);580if ((lt->tm_mday > 31) || !lt->tm_mday)581return(-1);582/* FALLTHROUGH */583case 4: /* HH */584lt->tm_hour = ATOI2(p);585if (lt->tm_hour > 23)586return(-1);587/* FALLTHROUGH */588case 2: /* MM */589lt->tm_min = ATOI2(p);590if (lt->tm_min > 59)591return(-1);592break;593default:594return(-1);595}596597/* convert broken-down time to UTC clock time seconds */598if ((*tval = mktime(lt)) == -1)599return(-1);600return(0);601}602603604