1 /* $OpenBSD: sel_subs.c,v 1.7 1997/08/17 23:05:09 millert Exp $ */
2 /* $NetBSD: sel_subs.c,v 1.5 1995/03/21 09:07:42 cgd Exp $ */
5 * Copyright (c) 1992 Keith Muller.
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
9 * This code is derived from software contributed to Berkeley by
10 * Keith Muller of the University of California, San Diego.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 static char sccsid[] = "@(#)sel_subs.c 8.1 (Berkeley) 5/31/93";
45 static char rcsid[] = "$OpenBSD: sel_subs.c,v 1.7 1997/08/17 23:05:09 millert Exp $";
49 #include <sys/types.h>
53 #include <sys/param.h>
64 static int str_sec __P((register char *, time_t *));
65 static int usr_match __P((register ARCHD *));
66 static int grp_match __P((register ARCHD *));
67 static int trng_match __P((register ARCHD *));
69 static TIME_RNG *trhead = NULL; /* time range list head */
70 static TIME_RNG *trtail = NULL; /* time range list tail */
71 static USRT **usrtb = NULL; /* user selection table */
72 static GRPT **grptb = NULL; /* group selection table */
75 * Routines for selection of archive members
80 * check if this file matches a specfied uid, gid or time range
82 * 0 if this archive member should be processed, 1 if it should be skipped
87 sel_chk(register ARCHD *arcn)
94 if (((usrtb != NULL) && usr_match(arcn)) ||
95 ((grptb != NULL) && grp_match(arcn)) ||
96 ((trhead != NULL) && trng_match(arcn)))
102 * User/group selection routines
104 * Routines to handle user selection of files based on the file uid/gid. To
105 * add an entry, the user supplies either then name or the uid/gid starting with
106 * a # on the command line. A \# will eascape the #.
111 * add a user match to the user match hash table
113 * 0 if added ok, -1 otherwise;
118 usr_add(register char *str)
127 register struct passwd *pw;
131 * create the table if it doesn't exist
133 if ((str == NULL) || (*str == '\0'))
135 if ((usrtb == NULL) &&
136 ((usrtb = (USRT **)calloc(USR_TB_SZ, sizeof(USRT *))) == NULL)) {
137 paxwarn(1, "Unable to allocate memory for user selection table");
142 * figure out user spec
146 * it is a user name, \# escapes # as first char in user name
148 if ((str[0] == '\\') && (str[1] == '#'))
150 if ((pw = getpwnam(str)) == NULL) {
151 paxwarn(1, "Unable to find uid for user: %s", str);
154 uid = (uid_t)pw->pw_uid;
157 uid = (uid_t)atoi(str+1);
159 uid = (uid_t)strtoul(str+1, NULL, 10);
164 * hash it and go down the hash chain (if any) looking for it
166 indx = ((unsigned)uid) % USR_TB_SZ;
167 if ((pt = usrtb[indx]) != NULL) {
176 * uid is not yet in the table, add it to the front of the chain
178 if ((pt = (USRT *)malloc(sizeof(USRT))) != NULL) {
180 pt->fow = usrtb[indx];
184 paxwarn(1, "User selection table out of memory");
190 * check if this files uid matches a selected uid.
192 * 0 if this archive member should be processed, 1 if it should be skipped
197 usr_match(register ARCHD *arcn)
201 register ARCHD *arcn;
207 * hash and look for it in the table
209 pt = usrtb[((unsigned)arcn->sb.st_uid) % USR_TB_SZ];
211 if (pt->uid == arcn->sb.st_uid)
224 * add a group match to the group match hash table
226 * 0 if added ok, -1 otherwise;
231 grp_add(register char *str)
240 register struct group *gr;
244 * create the table if it doesn't exist
246 if ((str == NULL) || (*str == '\0'))
248 if ((grptb == NULL) &&
249 ((grptb = (GRPT **)calloc(GRP_TB_SZ, sizeof(GRPT *))) == NULL)) {
250 paxwarn(1, "Unable to allocate memory fo group selection table");
255 * figure out user spec
259 * it is a group name, \# escapes # as first char in group name
261 if ((str[0] == '\\') && (str[1] == '#'))
263 if ((gr = getgrnam(str)) == NULL) {
264 paxwarn(1,"Cannot determine gid for group name: %s", str);
267 gid = (gid_t)gr->gr_gid;
270 gid = (gid_t)atoi(str+1);
272 gid = (gid_t)strtoul(str+1, NULL, 10);
277 * hash it and go down the hash chain (if any) looking for it
279 indx = ((unsigned)gid) % GRP_TB_SZ;
280 if ((pt = grptb[indx]) != NULL) {
289 * gid not in the table, add it to the front of the chain
291 if ((pt = (GRPT *)malloc(sizeof(GRPT))) != NULL) {
293 pt->fow = grptb[indx];
297 paxwarn(1, "Group selection table out of memory");
303 * check if this files gid matches a selected gid.
305 * 0 if this archive member should be processed, 1 if it should be skipped
310 grp_match(register ARCHD *arcn)
314 register ARCHD *arcn;
320 * hash and look for it in the table
322 pt = grptb[((unsigned)arcn->sb.st_gid) % GRP_TB_SZ];
324 if (pt->gid == arcn->sb.st_gid)
336 * Time range selection routines
338 * Routines to handle user selection of files based on the modification and/or
339 * inode change time falling within a specified time range (the non-standard
340 * -T flag). The user may specify any number of different file time ranges.
341 * Time ranges are checked one at a time until a match is found (if at all).
342 * If the file has a mtime (and/or ctime) which lies within one of the time
343 * ranges, the file is selected. Time ranges may have a lower and/or a upper
344 * value. These ranges are inclusive. When no time ranges are supplied to pax
345 * with the -T option, all members in the archive will be selected by the time
346 * range routines. When only a lower range is supplied, only files with a
347 * mtime (and/or ctime) equal to or younger are selected. When only a upper
348 * range is supplied, only files with a mtime (and/or ctime) equal to or older
349 * are selected. When the lower time range is equal to the upper time range,
350 * only files with a mtime (or ctime) of exactly that time are selected.
355 * add a time range match to the time range list.
356 * This is a non-standard pax option. Lower and upper ranges are in the
357 * format: [yy[mm[dd[hh]]]]mm[.ss] and are comma separated.
358 * Time ranges are based on current time, so 1234 would specify a time of
361 * 0 if the time range was added to the list, -1 otherwise
366 trng_add(register char *str)
373 register TIME_RNG *pt;
374 register char *up_pt = NULL;
376 register char *flgpt;
377 register int dot = 0;
380 * throw out the badly formed time ranges
382 if ((str == NULL) || (*str == '\0')) {
383 paxwarn(1, "Empty time range string");
388 * locate optional flags suffix /{cm}.
390 if ((flgpt = strrchr(str, '/')) != NULL)
393 for (stpt = str; *stpt != '\0'; ++stpt) {
394 if ((*stpt >= '0') && (*stpt <= '9'))
396 if ((*stpt == ',') && (up_pt == NULL)) {
404 * allow only one dot per range (secs)
406 if ((*stpt == '.') && (!dot)) {
410 paxwarn(1, "Improperly specified time range: %s", str);
415 * allocate space for the time range and store the limits
417 if ((pt = (TIME_RNG *)malloc(sizeof(TIME_RNG))) == NULL) {
418 paxwarn(1, "Unable to allocate memory for time range");
423 * by default we only will check file mtime, but usee can specify
424 * mtime, ctime (inode change time) or both.
426 if ((flgpt == NULL) || (*flgpt == '\0'))
430 while (*flgpt != '\0') {
441 paxwarn(1, "Bad option %c with time range %s",
450 * start off with the current time
452 pt->low_time = pt->high_time = time(NULL);
457 if (str_sec(str, &(pt->low_time)) < 0) {
458 paxwarn(1, "Illegal lower time range %s", str);
459 (void)free((char *)pt);
465 if ((up_pt != NULL) && (*up_pt != '\0')) {
469 if (str_sec(up_pt, &(pt->high_time)) < 0) {
470 paxwarn(1, "Illegal upper time range %s", up_pt);
471 (void)free((char *)pt);
477 * check that the upper and lower do not overlap
479 if (pt->flgs & HASLOW) {
480 if (pt->low_time > pt->high_time) {
481 paxwarn(1, "Upper %s and lower %s time overlap",
483 (void)free((char *)pt);
490 if (trhead == NULL) {
491 trtail = trhead = pt;
499 paxwarn(1, "Time range format is: [yy[mm[dd[hh]]]]mm[.ss][/[c][m]]");
505 * check if this files mtime/ctime falls within any supplied time range.
507 * 0 if this archive member should be processed, 1 if it should be skipped
512 trng_match(register ARCHD *arcn)
516 register ARCHD *arcn;
519 register TIME_RNG *pt;
522 * have to search down the list one at a time looking for a match.
523 * remember time range limits are inclusive.
527 switch(pt->flgs & CMPBOTH) {
530 * user wants both mtime and ctime checked for this
533 if (((pt->flgs & HASLOW) &&
534 (arcn->sb.st_mtime < pt->low_time) &&
535 (arcn->sb.st_ctime < pt->low_time)) ||
536 ((pt->flgs & HASHIGH) &&
537 (arcn->sb.st_mtime > pt->high_time) &&
538 (arcn->sb.st_ctime > pt->high_time))) {
545 * user wants only ctime checked for this time range
547 if (((pt->flgs & HASLOW) &&
548 (arcn->sb.st_ctime < pt->low_time)) ||
549 ((pt->flgs & HASHIGH) &&
550 (arcn->sb.st_ctime > pt->high_time))) {
558 * user wants only mtime checked for this time range
560 if (((pt->flgs & HASLOW) &&
561 (arcn->sb.st_mtime < pt->low_time)) ||
562 ((pt->flgs & HASHIGH) &&
563 (arcn->sb.st_mtime > pt->high_time))) {
579 * Convert a time string in the format of [yy[mm[dd[hh]]]]mm[.ss] to gmt
580 * seconds. Tval already has current time loaded into it at entry.
582 * 0 if converted ok, -1 otherwise
587 str_sec(register char *str, time_t *tval)
595 register struct tm *lt;
596 register char *dot = NULL;
598 lt = localtime(tval);
599 if ((dot = strchr(str, '.')) != NULL) {
604 if (strlen(dot) != 2)
606 if ((lt->tm_sec = ATOI2(dot)) > 61)
611 switch (strlen(str)) {
615 * watch out for year 2000
617 if ((lt->tm_year = ATOI2(str)) < 69)
624 * watch out months are from 0 - 11 internally
626 if ((lt->tm_mon = ATOI2(str)) > 12)
635 if ((lt->tm_mday = ATOI2(str)) > 31)
643 if ((lt->tm_hour = ATOI2(str)) > 23)
651 if ((lt->tm_min = ATOI2(str)) > 59)
658 * convert broken-down time to GMT clock time seconds
660 if ((*tval = mktime(lt)) == -1)