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