mld6query(8): Rename mld6.c -> mld6query.c
[dragonfly.git] / bin / pax / sel_subs.c
blob56cbe11731b0248bb494287ae4959eb3a7bb1eaf
1 /*-
2 * Copyright (c) 1992 Keith Muller.
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Keith Muller of the University of California, San Diego.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
33 * @(#)sel_subs.c 8.1 (Berkeley) 5/31/93
34 * $FreeBSD: src/bin/pax/sel_subs.c,v 1.11.2.1 2001/08/01 05:03:11 obrien Exp $
35 * $DragonFly: src/bin/pax/sel_subs.c,v 1.8 2006/09/27 21:58:08 pavalos Exp $
38 #include <sys/types.h>
39 #include <sys/time.h>
40 #include <sys/stat.h>
41 #include <pwd.h>
42 #include <grp.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <strings.h>
46 #include <unistd.h>
47 #include <stdlib.h>
48 #include "pax.h"
49 #include "sel_subs.h"
50 #include "extern.h"
52 static int str_sec (char *, time_t *);
53 static int usr_match (ARCHD *);
54 static int grp_match (ARCHD *);
55 static int trng_match (ARCHD *);
57 static TIME_RNG *trhead = NULL; /* time range list head */
58 static TIME_RNG *trtail = NULL; /* time range list tail */
59 static USRT **usrtb = NULL; /* user selection table */
60 static GRPT **grptb = NULL; /* group selection table */
63 * Routines for selection of archive members
67 * sel_chk()
68 * check if this file matches a specified uid, gid or time range
69 * Return:
70 * 0 if this archive member should be processed, 1 if it should be skipped
73 int
74 sel_chk(ARCHD *arcn)
76 if (((usrtb != NULL) && usr_match(arcn)) ||
77 ((grptb != NULL) && grp_match(arcn)) ||
78 ((trhead != NULL) && trng_match(arcn)))
79 return(1);
80 return(0);
84 * User/group selection routines
86 * Routines to handle user selection of files based on the file uid/gid. To
87 * add an entry, the user supplies either the name or the uid/gid starting with
88 * a # on the command line. A \# will escape the #.
92 * usr_add()
93 * add a user match to the user match hash table
94 * Return:
95 * 0 if added ok, -1 otherwise;
98 int
99 usr_add(char *str)
101 u_int indx;
102 USRT *pt;
103 struct passwd *pw;
104 uid_t uid;
107 * create the table if it doesn't exist
109 if ((str == NULL) || (*str == '\0'))
110 return(-1);
111 if ((usrtb == NULL) &&
112 ((usrtb = (USRT **)calloc(USR_TB_SZ, sizeof(USRT *))) == NULL)) {
113 paxwarn(1, "Unable to allocate memory for user selection table");
114 return(-1);
118 * figure out user spec
120 if (str[0] != '#') {
122 * it is a user name, \# escapes # as first char in user name
124 if ((str[0] == '\\') && (str[1] == '#'))
125 ++str;
126 if ((pw = getpwnam(str)) == NULL) {
127 paxwarn(1, "Unable to find uid for user: %s", str);
128 return(-1);
130 uid = (uid_t)pw->pw_uid;
131 } else
132 uid = (uid_t)strtoul(str+1, NULL, 10);
133 endpwent();
136 * hash it and go down the hash chain (if any) looking for it
138 indx = ((unsigned)uid) % USR_TB_SZ;
139 if ((pt = usrtb[indx]) != NULL) {
140 while (pt != NULL) {
141 if (pt->uid == uid)
142 return(0);
143 pt = pt->fow;
148 * uid is not yet in the table, add it to the front of the chain
150 if ((pt = (USRT *)malloc(sizeof(USRT))) != NULL) {
151 pt->uid = uid;
152 pt->fow = usrtb[indx];
153 usrtb[indx] = pt;
154 return(0);
156 paxwarn(1, "User selection table out of memory");
157 return(-1);
161 * usr_match()
162 * check if this files uid matches a selected uid.
163 * Return:
164 * 0 if this archive member should be processed, 1 if it should be skipped
167 static int
168 usr_match(ARCHD *arcn)
170 USRT *pt;
173 * hash and look for it in the table
175 pt = usrtb[((unsigned)arcn->sb.st_uid) % USR_TB_SZ];
176 while (pt != NULL) {
177 if (pt->uid == arcn->sb.st_uid)
178 return(0);
179 pt = pt->fow;
183 * not found
185 return(1);
189 * grp_add()
190 * add a group match to the group match hash table
191 * Return:
192 * 0 if added ok, -1 otherwise;
196 grp_add(char *str)
198 u_int indx;
199 GRPT *pt;
200 struct group *gr;
201 gid_t gid;
204 * create the table if it doesn't exist
206 if ((str == NULL) || (*str == '\0'))
207 return(-1);
208 if ((grptb == NULL) &&
209 ((grptb = (GRPT **)calloc(GRP_TB_SZ, sizeof(GRPT *))) == NULL)) {
210 paxwarn(1, "Unable to allocate memory fo group selection table");
211 return(-1);
215 * figure out user spec
217 if (str[0] != '#') {
219 * it is a group name, \# escapes # as first char in group name
221 if ((str[0] == '\\') && (str[1] == '#'))
222 ++str;
223 if ((gr = getgrnam(str)) == NULL) {
224 paxwarn(1,"Cannot determine gid for group name: %s", str);
225 return(-1);
227 gid = (gid_t)gr->gr_gid;
228 } else
229 gid = (gid_t)strtoul(str+1, NULL, 10);
230 endgrent();
233 * hash it and go down the hash chain (if any) looking for it
235 indx = ((unsigned)gid) % GRP_TB_SZ;
236 if ((pt = grptb[indx]) != NULL) {
237 while (pt != NULL) {
238 if (pt->gid == gid)
239 return(0);
240 pt = pt->fow;
245 * gid not in the table, add it to the front of the chain
247 if ((pt = (GRPT *)malloc(sizeof(GRPT))) != NULL) {
248 pt->gid = gid;
249 pt->fow = grptb[indx];
250 grptb[indx] = pt;
251 return(0);
253 paxwarn(1, "Group selection table out of memory");
254 return(-1);
258 * grp_match()
259 * check if this files gid matches a selected gid.
260 * Return:
261 * 0 if this archive member should be processed, 1 if it should be skipped
264 static int
265 grp_match(ARCHD *arcn)
267 GRPT *pt;
270 * hash and look for it in the table
272 pt = grptb[((unsigned)arcn->sb.st_gid) % GRP_TB_SZ];
273 while (pt != NULL) {
274 if (pt->gid == arcn->sb.st_gid)
275 return(0);
276 pt = pt->fow;
280 * not found
282 return(1);
286 * Time range selection routines
288 * Routines to handle user selection of files based on the modification and/or
289 * inode change time falling within a specified time range (the non-standard
290 * -T flag). The user may specify any number of different file time ranges.
291 * Time ranges are checked one at a time until a match is found (if at all).
292 * If the file has a mtime (and/or ctime) which lies within one of the time
293 * ranges, the file is selected. Time ranges may have a lower and/or a upper
294 * value. These ranges are inclusive. When no time ranges are supplied to pax
295 * with the -T option, all members in the archive will be selected by the time
296 * range routines. When only a lower range is supplied, only files with a
297 * mtime (and/or ctime) equal to or younger are selected. When only a upper
298 * range is supplied, only files with a mtime (and/or ctime) equal to or older
299 * are selected. When the lower time range is equal to the upper time range,
300 * only files with a mtime (or ctime) of exactly that time are selected.
304 * trng_add()
305 * add a time range match to the time range list.
306 * This is a non-standard pax option. Lower and upper ranges are in the
307 * format: [yy[mm[dd[hh]]]]mm[.ss] and are comma separated.
308 * Time ranges are based on current time, so 1234 would specify a time of
309 * 12:34 today.
310 * Return:
311 * 0 if the time range was added to the list, -1 otherwise
315 trng_add(char *str)
317 TIME_RNG *pt;
318 char *up_pt = NULL;
319 char *stpt;
320 char *flgpt;
321 int dot = 0;
324 * throw out the badly formed time ranges
326 if ((str == NULL) || (*str == '\0')) {
327 paxwarn(1, "Empty time range string");
328 return(-1);
332 * locate optional flags suffix /{cm}.
334 if ((flgpt = strrchr(str, '/')) != NULL)
335 *flgpt++ = '\0';
337 for (stpt = str; *stpt != '\0'; ++stpt) {
338 if ((*stpt >= '0') && (*stpt <= '9'))
339 continue;
340 if ((*stpt == ',') && (up_pt == NULL)) {
341 *stpt = '\0';
342 up_pt = stpt + 1;
343 dot = 0;
344 continue;
348 * allow only one dot per range (secs)
350 if ((*stpt == '.') && (!dot)) {
351 ++dot;
352 continue;
354 paxwarn(1, "Improperly specified time range: %s", str);
355 goto out;
359 * allocate space for the time range and store the limits
361 if ((pt = (TIME_RNG *)malloc(sizeof(TIME_RNG))) == NULL) {
362 paxwarn(1, "Unable to allocate memory for time range");
363 return(-1);
367 * by default we only will check file mtime, but user can specify
368 * mtime, ctime (inode change time) or both.
370 if ((flgpt == NULL) || (*flgpt == '\0'))
371 pt->flgs = CMPMTME;
372 else {
373 pt->flgs = 0;
374 while (*flgpt != '\0') {
375 switch(*flgpt) {
376 case 'M':
377 case 'm':
378 pt->flgs |= CMPMTME;
379 break;
380 case 'C':
381 case 'c':
382 pt->flgs |= CMPCTME;
383 break;
384 default:
385 paxwarn(1, "Bad option %c with time range %s",
386 *flgpt, str);
387 goto out;
389 ++flgpt;
394 * start off with the current time
396 pt->low_time = pt->high_time = time(NULL);
397 if (*str != '\0') {
399 * add lower limit
401 if (str_sec(str, &(pt->low_time)) < 0) {
402 paxwarn(1, "Illegal lower time range %s", str);
403 free((char *)pt);
404 goto out;
406 pt->flgs |= HASLOW;
409 if ((up_pt != NULL) && (*up_pt != '\0')) {
411 * add upper limit
413 if (str_sec(up_pt, &(pt->high_time)) < 0) {
414 paxwarn(1, "Illegal upper time range %s", up_pt);
415 free((char *)pt);
416 goto out;
418 pt->flgs |= HASHIGH;
421 * check that the upper and lower do not overlap
423 if (pt->flgs & HASLOW) {
424 if (pt->low_time > pt->high_time) {
425 paxwarn(1, "Upper %s and lower %s time overlap",
426 up_pt, str);
427 free((char *)pt);
428 return(-1);
433 pt->fow = NULL;
434 if (trhead == NULL) {
435 trtail = trhead = pt;
436 return(0);
438 trtail->fow = pt;
439 trtail = pt;
440 return(0);
442 out:
443 paxwarn(1, "Time range format is: [yy[mm[dd[hh]]]]mm[.ss][/[c][m]]");
444 return(-1);
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 skipped
454 static int
455 trng_match(ARCHD *arcn)
457 TIME_RNG *pt;
460 * have to search down the list one at a time looking for a match.
461 * remember time range limits are inclusive.
463 pt = trhead;
464 while (pt != NULL) {
465 switch(pt->flgs & CMPBOTH) {
466 case CMPBOTH:
468 * user wants both mtime and ctime checked for this
469 * time range
471 if (((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))) {
477 pt = pt->fow;
478 continue;
480 break;
481 case CMPCTME:
483 * user wants only ctime checked for this time range
485 if (((pt->flgs & HASLOW) &&
486 (arcn->sb.st_ctime < pt->low_time)) ||
487 ((pt->flgs & HASHIGH) &&
488 (arcn->sb.st_ctime > pt->high_time))) {
489 pt = pt->fow;
490 continue;
492 break;
493 case CMPMTME:
494 default:
496 * user wants only mtime checked for this time range
498 if (((pt->flgs & HASLOW) &&
499 (arcn->sb.st_mtime < pt->low_time)) ||
500 ((pt->flgs & HASHIGH) &&
501 (arcn->sb.st_mtime > pt->high_time))) {
502 pt = pt->fow;
503 continue;
505 break;
507 break;
510 if (pt == NULL)
511 return(1);
512 return(0);
516 * str_sec()
517 * Convert a time string in the format of [yy[mm[dd[hh]]]]mm[.ss] to gmt
518 * seconds. Tval already has current time loaded into it at entry.
519 * Return:
520 * 0 if converted ok, -1 otherwise
523 static int
524 str_sec(char *str, time_t *tval)
526 struct tm *lt;
527 char *dot = NULL;
529 lt = localtime(tval);
530 if ((dot = strchr(str, '.')) != NULL) {
532 * seconds (.ss)
534 *dot++ = '\0';
535 if (strlen(dot) != 2)
536 return(-1);
537 if ((lt->tm_sec = ATOI2(dot)) > 61)
538 return(-1);
539 } else
540 lt->tm_sec = 0;
542 switch (strlen(str)) {
543 case 10:
545 * year (yy)
546 * watch out for year 2000
548 if ((lt->tm_year = ATOI2(str)) < 69)
549 lt->tm_year += 100;
550 str += 2;
551 /* FALLTHROUGH */
552 case 8:
554 * month (mm)
555 * watch out months are from 0 - 11 internally
557 if ((lt->tm_mon = ATOI2(str)) > 12)
558 return(-1);
559 --lt->tm_mon;
560 str += 2;
561 /* FALLTHROUGH */
562 case 6:
564 * day (dd)
566 if ((lt->tm_mday = ATOI2(str)) > 31)
567 return(-1);
568 str += 2;
569 /* FALLTHROUGH */
570 case 4:
572 * hour (hh)
574 if ((lt->tm_hour = ATOI2(str)) > 23)
575 return(-1);
576 str += 2;
577 /* FALLTHROUGH */
578 case 2:
580 * minute (mm)
582 if ((lt->tm_min = ATOI2(str)) > 59)
583 return(-1);
584 break;
585 default:
586 return(-1);
589 * convert broken-down time to GMT clock time seconds
591 if ((*tval = mktime(lt)) == -1)
592 return(-1);
593 return(0);