sel_subs.c revision 1.17 1 /* $NetBSD: sel_subs.c,v 1.17 2003/08/07 09:05:22 agc Exp $ */
2
3 /*-
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Keith Muller of the University of California, San Diego.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 /*-
36 * Copyright (c) 1992 Keith Muller.
37 *
38 * This code is derived from software contributed to Berkeley by
39 * Keith Muller of the University of California, San Diego.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. All advertising materials mentioning features or use of this software
50 * must display the following acknowledgement:
51 * This product includes software developed by the University of
52 * California, Berkeley and its contributors.
53 * 4. Neither the name of the University nor the names of its contributors
54 * may be used to endorse or promote products derived from this software
55 * without specific prior written permission.
56 *
57 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * SUCH DAMAGE.
68 */
69
70 #include <sys/cdefs.h>
71 #if defined(__RCSID) && !defined(lint)
72 #if 0
73 static char sccsid[] = "@(#)sel_subs.c 8.1 (Berkeley) 5/31/93";
74 #else
75 __RCSID("$NetBSD: sel_subs.c,v 1.17 2003/08/07 09:05:22 agc Exp $");
76 #endif
77 #endif /* not lint */
78
79 #include <sys/types.h>
80 #include <sys/time.h>
81 #include <sys/stat.h>
82 #include <sys/param.h>
83
84 #include <pwd.h>
85 #include <grp.h>
86 #include <stdio.h>
87 #include <ctype.h>
88 #include <string.h>
89 #include <strings.h>
90 #include <time.h>
91 #include <unistd.h>
92 #include <stdlib.h>
93 #include <tzfile.h>
94
95 #include "pax.h"
96 #include "sel_subs.h"
97 #include "extern.h"
98
99 static int str_sec(const char *, time_t *);
100 static int usr_match(ARCHD *);
101 static int grp_match(ARCHD *);
102 static int trng_match(ARCHD *);
103
104 static TIME_RNG *trhead = NULL; /* time range list head */
105 static TIME_RNG *trtail = NULL; /* time range list tail */
106 static USRT **usrtb = NULL; /* user selection table */
107 static GRPT **grptb = NULL; /* group selection table */
108
109 /*
110 * Routines for selection of archive members
111 */
112
113 /*
114 * sel_chk()
115 * check if this file matches a specfied uid, gid or time range
116 * Return:
117 * 0 if this archive member should be processed, 1 if it should be skipped
118 */
119
120 int
121 sel_chk(ARCHD *arcn)
122 {
123 if (((usrtb != NULL) && usr_match(arcn)) ||
124 ((grptb != NULL) && grp_match(arcn)) ||
125 ((trhead != NULL) && trng_match(arcn)))
126 return(1);
127 return(0);
128 }
129
130 /*
131 * User/group selection routines
132 *
133 * Routines to handle user selection of files based on the file uid/gid. To
134 * add an entry, the user supplies either the name or the uid/gid starting with
135 * a # on the command line. A \# will escape the #.
136 */
137
138 /*
139 * usr_add()
140 * add a user match to the user match hash table
141 * Return:
142 * 0 if added ok, -1 otherwise;
143 */
144
145 int
146 usr_add(char *str)
147 {
148 u_int indx;
149 USRT *pt;
150 struct passwd *pw;
151 uid_t uid;
152
153 /*
154 * create the table if it doesn't exist
155 */
156 if ((str == NULL) || (*str == '\0'))
157 return(-1);
158 if ((usrtb == NULL) &&
159 ((usrtb = (USRT **)calloc(USR_TB_SZ, sizeof(USRT *))) == NULL)) {
160 tty_warn(1,
161 "Unable to allocate memory for user selection table");
162 return(-1);
163 }
164
165 /*
166 * figure out user spec
167 */
168 if (str[0] != '#') {
169 /*
170 * it is a user name, \# escapes # as first char in user name
171 */
172 if ((str[0] == '\\') && (str[1] == '#'))
173 ++str;
174 if ((pw = getpwnam(str)) == NULL) {
175 tty_warn(1, "Unable to find uid for user: %s", str);
176 return(-1);
177 }
178 uid = (uid_t)pw->pw_uid;
179 } else
180 uid = (uid_t)strtoul(str+1, (char **)NULL, 10);
181 endpwent();
182
183 /*
184 * hash it and go down the hash chain (if any) looking for it
185 */
186 indx = ((unsigned)uid) % USR_TB_SZ;
187 if ((pt = usrtb[indx]) != NULL) {
188 while (pt != NULL) {
189 if (pt->uid == uid)
190 return(0);
191 pt = pt->fow;
192 }
193 }
194
195 /*
196 * uid is not yet in the table, add it to the front of the chain
197 */
198 if ((pt = (USRT *)malloc(sizeof(USRT))) != NULL) {
199 pt->uid = uid;
200 pt->fow = usrtb[indx];
201 usrtb[indx] = pt;
202 return(0);
203 }
204 tty_warn(1, "User selection table out of memory");
205 return(-1);
206 }
207
208 /*
209 * usr_match()
210 * check if this files uid matches a selected uid.
211 * Return:
212 * 0 if this archive member should be processed, 1 if it should be skipped
213 */
214
215 static int
216 usr_match(ARCHD *arcn)
217 {
218 USRT *pt;
219
220 /*
221 * hash and look for it in the table
222 */
223 pt = usrtb[((unsigned)arcn->sb.st_uid) % USR_TB_SZ];
224 while (pt != NULL) {
225 if (pt->uid == arcn->sb.st_uid)
226 return(0);
227 pt = pt->fow;
228 }
229
230 /*
231 * not found
232 */
233 return(1);
234 }
235
236 /*
237 * grp_add()
238 * add a group match to the group match hash table
239 * Return:
240 * 0 if added ok, -1 otherwise;
241 */
242
243 int
244 grp_add(char *str)
245 {
246 u_int indx;
247 GRPT *pt;
248 struct group *gr;
249 gid_t gid;
250
251 /*
252 * create the table if it doesn't exist
253 */
254 if ((str == NULL) || (*str == '\0'))
255 return(-1);
256 if ((grptb == NULL) &&
257 ((grptb = (GRPT **)calloc(GRP_TB_SZ, sizeof(GRPT *))) == NULL)) {
258 tty_warn(1,
259 "Unable to allocate memory fo group selection table");
260 return(-1);
261 }
262
263 /*
264 * figure out user spec
265 */
266 if (str[0] != '#') {
267 /*
268 * it is a group name, \# escapes # as first char in group name
269 */
270 if ((str[0] == '\\') && (str[1] == '#'))
271 ++str;
272 if ((gr = getgrnam(str)) == NULL) {
273 tty_warn(1,
274 "Cannot determine gid for group name: %s", str);
275 return(-1);
276 }
277 gid = (gid_t)gr->gr_gid;
278 } else
279 gid = (gid_t)strtoul(str+1, (char **)NULL, 10);
280 endgrent();
281
282 /*
283 * hash it and go down the hash chain (if any) looking for it
284 */
285 indx = ((unsigned)gid) % GRP_TB_SZ;
286 if ((pt = grptb[indx]) != NULL) {
287 while (pt != NULL) {
288 if (pt->gid == gid)
289 return(0);
290 pt = pt->fow;
291 }
292 }
293
294 /*
295 * gid not in the table, add it to the front of the chain
296 */
297 if ((pt = (GRPT *)malloc(sizeof(GRPT))) != NULL) {
298 pt->gid = gid;
299 pt->fow = grptb[indx];
300 grptb[indx] = pt;
301 return(0);
302 }
303 tty_warn(1, "Group selection table out of memory");
304 return(-1);
305 }
306
307 /*
308 * grp_match()
309 * check if this files gid matches a selected gid.
310 * Return:
311 * 0 if this archive member should be processed, 1 if it should be skipped
312 */
313
314 static int
315 grp_match(ARCHD *arcn)
316 {
317 GRPT *pt;
318
319 /*
320 * hash and look for it in the table
321 */
322 pt = grptb[((unsigned)arcn->sb.st_gid) % GRP_TB_SZ];
323 while (pt != NULL) {
324 if (pt->gid == arcn->sb.st_gid)
325 return(0);
326 pt = pt->fow;
327 }
328
329 /*
330 * not found
331 */
332 return(1);
333 }
334
335 /*
336 * Time range selection routines
337 *
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.
351 */
352
353 /*
354 * trng_add()
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
359 * 12:34 today.
360 * Return:
361 * 0 if the time range was added to the list, -1 otherwise
362 */
363
364 int
365 trng_add(char *str)
366 {
367 TIME_RNG *pt;
368 char *up_pt = NULL;
369 char *stpt;
370 char *flgpt;
371 int dot = 0;
372
373 /*
374 * throw out the badly formed time ranges
375 */
376 if ((str == NULL) || (*str == '\0')) {
377 tty_warn(1, "Empty time range string");
378 return(-1);
379 }
380
381 /*
382 * locate optional flags suffix /{cm}.
383 */
384 if ((flgpt = strrchr(str, '/')) != NULL)
385 *flgpt++ = '\0';
386
387 for (stpt = str; *stpt != '\0'; ++stpt) {
388 if ((*stpt >= '0') && (*stpt <= '9'))
389 continue;
390 if ((*stpt == ',') && (up_pt == NULL)) {
391 *stpt = '\0';
392 up_pt = stpt + 1;
393 dot = 0;
394 continue;
395 }
396
397 /*
398 * allow only one dot per range (secs)
399 */
400 if ((*stpt == '.') && (!dot)) {
401 ++dot;
402 continue;
403 }
404 tty_warn(1, "Improperly specified time range: %s", str);
405 goto out;
406 }
407
408 /*
409 * allocate space for the time range and store the limits
410 */
411 if ((pt = (TIME_RNG *)malloc(sizeof(TIME_RNG))) == NULL) {
412 tty_warn(1, "Unable to allocate memory for time range");
413 return(-1);
414 }
415
416 /*
417 * by default we only will check file mtime, but user can specify
418 * mtime, ctime (inode change time) or both.
419 */
420 if ((flgpt == NULL) || (*flgpt == '\0'))
421 pt->flgs = CMPMTME;
422 else {
423 pt->flgs = 0;
424 while (*flgpt != '\0') {
425 switch(*flgpt) {
426 case 'M':
427 case 'm':
428 pt->flgs |= CMPMTME;
429 break;
430 case 'C':
431 case 'c':
432 pt->flgs |= CMPCTME;
433 break;
434 default:
435 tty_warn(1, "Bad option %c with time range %s",
436 *flgpt, str);
437 goto out;
438 }
439 ++flgpt;
440 }
441 }
442
443 /*
444 * start off with the current time
445 */
446 pt->low_time = pt->high_time = time((time_t *)NULL);
447 if (*str != '\0') {
448 /*
449 * add lower limit
450 */
451 if (str_sec(str, &(pt->low_time)) < 0) {
452 tty_warn(1, "Illegal lower time range %s", str);
453 (void)free((char *)pt);
454 goto out;
455 }
456 pt->flgs |= HASLOW;
457 }
458
459 if ((up_pt != NULL) && (*up_pt != '\0')) {
460 /*
461 * add upper limit
462 */
463 if (str_sec(up_pt, &(pt->high_time)) < 0) {
464 tty_warn(1, "Illegal upper time range %s", up_pt);
465 (void)free((char *)pt);
466 goto out;
467 }
468 pt->flgs |= HASHIGH;
469
470 /*
471 * check that the upper and lower do not overlap
472 */
473 if (pt->flgs & HASLOW) {
474 if (pt->low_time > pt->high_time) {
475 tty_warn(1,
476 "Upper %s and lower %s time overlap",
477 up_pt, str);
478 (void)free((char *)pt);
479 return(-1);
480 }
481 }
482 }
483
484 pt->fow = NULL;
485 if (trhead == NULL) {
486 trtail = trhead = pt;
487 return(0);
488 }
489 trtail->fow = pt;
490 trtail = pt;
491 return(0);
492
493 out:
494 tty_warn(1, "Time range format is: [yy[mm[dd[hh]]]]mm[.ss][/[c][m]]");
495 return(-1);
496 }
497
498 /*
499 * trng_match()
500 * check if this files mtime/ctime falls within any supplied time range.
501 * Return:
502 * 0 if this archive member should be processed, 1 if it should be skipped
503 */
504
505 static int
506 trng_match(ARCHD *arcn)
507 {
508 TIME_RNG *pt;
509
510 /*
511 * have to search down the list one at a time looking for a match.
512 * remember time range limits are inclusive.
513 */
514 pt = trhead;
515 while (pt != NULL) {
516 switch(pt->flgs & CMPBOTH) {
517 case CMPBOTH:
518 /*
519 * user wants both mtime and ctime checked for this
520 * time range
521 */
522 if (((pt->flgs & HASLOW) &&
523 (arcn->sb.st_mtime < pt->low_time) &&
524 (arcn->sb.st_ctime < pt->low_time)) ||
525 ((pt->flgs & HASHIGH) &&
526 (arcn->sb.st_mtime > pt->high_time) &&
527 (arcn->sb.st_ctime > pt->high_time))) {
528 pt = pt->fow;
529 continue;
530 }
531 break;
532 case CMPCTME:
533 /*
534 * user wants only ctime checked for this time range
535 */
536 if (((pt->flgs & HASLOW) &&
537 (arcn->sb.st_ctime < pt->low_time)) ||
538 ((pt->flgs & HASHIGH) &&
539 (arcn->sb.st_ctime > pt->high_time))) {
540 pt = pt->fow;
541 continue;
542 }
543 break;
544 case CMPMTME:
545 default:
546 /*
547 * user wants only mtime checked for this time range
548 */
549 if (((pt->flgs & HASLOW) &&
550 (arcn->sb.st_mtime < pt->low_time)) ||
551 ((pt->flgs & HASHIGH) &&
552 (arcn->sb.st_mtime > pt->high_time))) {
553 pt = pt->fow;
554 continue;
555 }
556 break;
557 }
558 break;
559 }
560
561 if (pt == NULL)
562 return(1);
563 return(0);
564 }
565
566 /*
567 * str_sec()
568 * Convert a time string in the format of [yy[mm[dd[hh]]]]mm[.ss] to gmt
569 * seconds. Tval already has current time loaded into it at entry.
570 * Return:
571 * 0 if converted ok, -1 otherwise
572 */
573
574 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
575
576 static int
577 str_sec(const char *p, time_t *tval)
578 {
579 struct tm *lt;
580 const char *dot, *t;
581 int yearset, len;
582
583 for (t = p, dot = NULL; *t; ++t) {
584 if (isdigit((unsigned char)*t))
585 continue;
586 if (*t == '.' && dot == NULL) {
587 dot = t;
588 continue;
589 }
590 return(-1);
591 }
592
593 lt = localtime(tval);
594
595 if (dot != NULL) {
596 len = strlen(dot);
597 if (len != 3)
598 return(-1);
599 ++dot;
600 lt->tm_sec = ATOI2(dot);
601 } else {
602 len = 0;
603 lt->tm_sec = 0;
604 }
605
606 yearset = 0;
607 switch (strlen(p) - len) {
608 case 12:
609 lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
610 yearset = 1;
611 /* FALLTHROUGH */
612 case 10:
613 if (yearset) {
614 lt->tm_year += ATOI2(p);
615 } else {
616 yearset = ATOI2(p);
617 if (yearset < 69)
618 lt->tm_year = yearset + 2000 - TM_YEAR_BASE;
619 else
620 lt->tm_year = yearset + 1900 - TM_YEAR_BASE;
621 }
622 /* FALLTHROUGH */
623 case 8:
624 lt->tm_mon = ATOI2(p);
625 --lt->tm_mon;
626 /* FALLTHROUGH */
627 case 6:
628 lt->tm_mday = ATOI2(p);
629 /* FALLTHROUGH */
630 case 4:
631 lt->tm_hour = ATOI2(p);
632 /* FALLTHROUGH */
633 case 2:
634 lt->tm_min = ATOI2(p);
635 break;
636 default:
637 return(-1);
638 }
639
640 /*
641 * convert broken-down time to GMT clock time seconds
642 */
643 if ((*tval = mktime(lt)) == -1)
644 return(-1);
645 return(0);
646 }
647