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