1 /* $OpenBSD: sel_subs.c,v 1.18 2004/04/16 22:50:23 deraadt 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. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static const char sccsid[] = "@(#)sel_subs.c 8.1 (Berkeley) 5/31/93";
41 static const char rcsid[] = "$OpenBSD: sel_subs.c,v 1.18 2004/04/16 22:50:23 deraadt Exp $";
45 #include <sys/types.h>
48 #include <sys/param.h>
61 static int str_sec(const char *, time_t *);
62 static int usr_match(ARCHD *);
63 static int grp_match(ARCHD *);
64 static int trng_match(ARCHD *);
66 static TIME_RNG *trhead = NULL; /* time range list head */
67 static TIME_RNG *trtail = NULL; /* time range list tail */
68 static USRT **usrtb = NULL; /* user selection table */
69 static GRPT **grptb = NULL; /* group selection table */
72 * Routines for selection of archive members
77 * check if this file matches a specified uid, gid or time range
79 * 0 if this archive member should be processed, 1 if it should be skipped
85 if (((usrtb != NULL) && usr_match(arcn)) ||
86 ((grptb != NULL) && grp_match(arcn)) ||
87 ((trhead != NULL) && trng_match(arcn)))
93 * User/group selection routines
95 * Routines to handle user selection of files based on the file uid/gid. To
96 * add an entry, the user supplies either the name or the uid/gid starting with
97 * a # on the command line. A \# will escape the #.
102 * add a user match to the user match hash table
104 * 0 if added ok, -1 otherwise;
116 * create the table if it doesn't exist
118 if ((str == NULL) || (*str == '\0'))
120 if ((usrtb == NULL) &&
121 ((usrtb = (USRT **)calloc(USR_TB_SZ, sizeof(USRT *))) == NULL)) {
122 paxwarn(1, "Unable to allocate memory for user selection table");
127 * figure out user spec
131 * it is a user name, \# escapes # as first char in user name
133 if ((str[0] == '\\') && (str[1] == '#'))
135 if ((pw = getpwnam(str)) == NULL) {
136 paxwarn(1, "Unable to find uid for user: %s", str);
139 uid = (uid_t)pw->pw_uid;
141 uid = (uid_t)strtoul(str+1, NULL, 10);
145 * hash it and go down the hash chain (if any) looking for it
147 indx = ((unsigned)uid) % USR_TB_SZ;
148 if ((pt = usrtb[indx]) != NULL) {
157 * uid is not yet in the table, add it to the front of the chain
159 if ((pt = (USRT *)malloc(sizeof(USRT))) != NULL) {
161 pt->fow = usrtb[indx];
165 paxwarn(1, "User selection table out of memory");
171 * check if this files uid matches a selected uid.
173 * 0 if this archive member should be processed, 1 if it should be skipped
177 usr_match(ARCHD *arcn)
182 * hash and look for it in the table
184 pt = usrtb[((unsigned)arcn->sb.st_uid) % USR_TB_SZ];
186 if (pt->uid == arcn->sb.st_uid)
199 * add a group match to the group match hash table
201 * 0 if added ok, -1 otherwise;
213 * create the table if it doesn't exist
215 if ((str == NULL) || (*str == '\0'))
217 if ((grptb == NULL) &&
218 ((grptb = (GRPT **)calloc(GRP_TB_SZ, sizeof(GRPT *))) == NULL)) {
219 paxwarn(1, "Unable to allocate memory fo group selection table");
224 * figure out user spec
228 * it is a group name, \# escapes # as first char in group name
230 if ((str[0] == '\\') && (str[1] == '#'))
232 if ((gr = getgrnam(str)) == NULL) {
233 paxwarn(1,"Cannot determine gid for group name: %s", str);
236 gid = (gid_t)gr->gr_gid;
238 gid = (gid_t)strtoul(str+1, NULL, 10);
242 * hash it and go down the hash chain (if any) looking for it
244 indx = ((unsigned)gid) % GRP_TB_SZ;
245 if ((pt = grptb[indx]) != NULL) {
254 * gid not in the table, add it to the front of the chain
256 if ((pt = (GRPT *)malloc(sizeof(GRPT))) != NULL) {
258 pt->fow = grptb[indx];
262 paxwarn(1, "Group selection table out of memory");
268 * check if this files gid matches a selected gid.
270 * 0 if this archive member should be processed, 1 if it should be skipped
274 grp_match(ARCHD *arcn)
279 * hash and look for it in the table
281 pt = grptb[((unsigned)arcn->sb.st_gid) % GRP_TB_SZ];
283 if (pt->gid == arcn->sb.st_gid)
295 * Time range selection routines
297 * Routines to handle user selection of files based on the modification and/or
298 * inode change time falling within a specified time range (the non-standard
299 * -T flag). The user may specify any number of different file time ranges.
300 * Time ranges are checked one at a time until a match is found (if at all).
301 * If the file has a mtime (and/or ctime) which lies within one of the time
302 * ranges, the file is selected. Time ranges may have a lower and/or a upper
303 * value. These ranges are inclusive. When no time ranges are supplied to pax
304 * with the -T option, all members in the archive will be selected by the time
305 * range routines. When only a lower range is supplied, only files with a
306 * mtime (and/or ctime) equal to or younger are selected. When only a upper
307 * range is supplied, only files with a mtime (and/or ctime) equal to or older
308 * are selected. When the lower time range is equal to the upper time range,
309 * only files with a mtime (or ctime) of exactly that time are selected.
314 * add a time range match to the time range list.
315 * This is a non-standard pax option. Lower and upper ranges are in the
316 * format: [[[[[cc]yy]mm]dd]HH]MM[.SS] and are comma separated.
317 * Time ranges are based on current time, so 1234 would specify a time of
320 * 0 if the time range was added to the list, -1 otherwise
333 * throw out the badly formed time ranges
335 if ((str == NULL) || (*str == '\0')) {
336 paxwarn(1, "Empty time range string");
341 * locate optional flags suffix /{cm}.
343 if ((flgpt = strrchr(str, '/')) != NULL)
346 for (stpt = str; *stpt != '\0'; ++stpt) {
347 if ((*stpt >= '0') && (*stpt <= '9'))
349 if ((*stpt == ',') && (up_pt == NULL)) {
357 * allow only one dot per range (secs)
359 if ((*stpt == '.') && (!dot)) {
363 paxwarn(1, "Improperly specified time range: %s", str);
368 * allocate space for the time range and store the limits
370 if ((pt = (TIME_RNG *)malloc(sizeof(TIME_RNG))) == NULL) {
371 paxwarn(1, "Unable to allocate memory for time range");
376 * by default we only will check file mtime, but user can specify
377 * mtime, ctime (inode change time) or both.
379 if ((flgpt == NULL) || (*flgpt == '\0'))
383 while (*flgpt != '\0') {
394 paxwarn(1, "Bad option %c with time range %s",
403 * start off with the current time
405 pt->low_time = pt->high_time = time(NULL);
410 if (str_sec(str, &(pt->low_time)) < 0) {
411 paxwarn(1, "Illegal lower time range %s", str);
412 (void)free((char *)pt);
418 if ((up_pt != NULL) && (*up_pt != '\0')) {
422 if (str_sec(up_pt, &(pt->high_time)) < 0) {
423 paxwarn(1, "Illegal upper time range %s", up_pt);
424 (void)free((char *)pt);
430 * check that the upper and lower do not overlap
432 if (pt->flgs & HASLOW) {
433 if (pt->low_time > pt->high_time) {
434 paxwarn(1, "Upper %s and lower %s time overlap",
436 (void)free((char *)pt);
443 if (trhead == NULL) {
444 trtail = trhead = pt;
452 paxwarn(1, "Time range format is: [[[[[cc]yy]mm]dd]HH]MM[.SS][/[c][m]]");
458 * check if this files mtime/ctime falls within any supplied time range.
460 * 0 if this archive member should be processed, 1 if it should be skipped
464 trng_match(ARCHD *arcn)
469 * have to search down the list one at a time looking for a match.
470 * remember time range limits are inclusive.
474 switch (pt->flgs & CMPBOTH) {
477 * user wants both mtime and ctime checked for this
480 if (((pt->flgs & HASLOW) &&
481 (arcn->sb.st_mtime < pt->low_time) &&
482 (arcn->sb.st_ctime < pt->low_time)) ||
483 ((pt->flgs & HASHIGH) &&
484 (arcn->sb.st_mtime > pt->high_time) &&
485 (arcn->sb.st_ctime > pt->high_time))) {
492 * user wants only ctime checked for this time range
494 if (((pt->flgs & HASLOW) &&
495 (arcn->sb.st_ctime < pt->low_time)) ||
496 ((pt->flgs & HASHIGH) &&
497 (arcn->sb.st_ctime > pt->high_time))) {
505 * user wants only mtime checked for this time range
507 if (((pt->flgs & HASLOW) &&
508 (arcn->sb.st_mtime < pt->low_time)) ||
509 ((pt->flgs & HASHIGH) &&
510 (arcn->sb.st_mtime > pt->high_time))) {
526 * Convert a time string in the format of [[[[[cc]yy]mm]dd]HH]MM[.SS] to
527 * seconds UTC. Tval already has current time loaded into it at entry.
529 * 0 if converted ok, -1 otherwise
533 str_sec(const char *p, time_t *tval)
544 for (t = p, dot = NULL; *t; ++t) {
547 if (*t == '.' && dot == NULL) {
554 lt = localtime(tval);
556 if (dot != NULL) { /* .SS */
557 if (strlen(++dot) != 2)
559 lt->tm_sec = ATOI2(dot);
569 lt->tm_year = (bigyear * 100) - TM_YEAR_BASE;
574 lt->tm_year += ATOI2(p);
576 lt->tm_year = ATOI2(p);
577 if (lt->tm_year < 69) /* hack for 2000 ;-} */
578 lt->tm_year += (2000 - TM_YEAR_BASE);
580 lt->tm_year += (1900 - TM_YEAR_BASE);
584 lt->tm_mon = ATOI2(p);
585 if ((lt->tm_mon > 12) || !lt->tm_mon)
587 --lt->tm_mon; /* time struct is 0 - 11 */
590 lt->tm_mday = ATOI2(p);
591 if ((lt->tm_mday > 31) || !lt->tm_mday)
595 lt->tm_hour = ATOI2(p);
596 if (lt->tm_hour > 23)
600 lt->tm_min = ATOI2(p);
608 /* convert broken-down time to UTC clock time seconds */
609 if ((*tval = mktime(lt)) == -1)