Debian Codegraph @

sel_subs.c

raw symbols SCIP
1 /* $OpenBSD: sel_subs.c,v 1.27 2018/09/13 12:33:43 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. 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.
23 *
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
34 * SUCH DAMAGE.
35 */
36
37 #include <sys/types.h>
38 #if HAVE_BOTH_TIME_H
39 #include <sys/time.h>
40 #include <time.h>
41 #elif HAVE_SYS_TIME_H
42 #include <sys/time.h>
43 #elif HAVE_TIME_H
44 #include <time.h>
45 #endif
46 #include <sys/stat.h>
47 #include <ctype.h>
48 #if HAVE_GRP_H
49 #include <grp.h>
50 #endif
51 #include <pwd.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #if HAVE_STRINGS_H
56 #include <strings.h>
57 #endif
58
59 #include "pax.h"
60 #include "extern.h"
61
62 __RCSID("$MirOS: src/bin/pax/sel_subs.c,v 1.11 2020/04/07 11:56:43 tg Exp $");
63
64 /*
65 * data structure for storing uid/grp selects (-U, -G non standard options)
66 */
67
68 #define USR_TB_SZ 317 /* user selection table size */
69 #define GRP_TB_SZ 317 /* user selection table size */
70
71 typedef struct usrt {
72 uid_t uid;
73 struct usrt *fow; /* next uid */
74 } USRT;
75
76 typedef struct grpt {
77 gid_t gid;
78 struct grpt *fow; /* next gid */
79 } GRPT;
80
81 /*
82 * data structure for storing user supplied time ranges (-T option)
83 */
84
85 #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
86
87 typedef struct time_rng {
88 time_t low_time; /* lower inclusive time limit */
89 time_t high_time; /* higher inclusive time limit */
90 int flgs; /* option flags */
91 #define HASLOW 0x01 /* has lower time limit */
92 #define HASHIGH 0x02 /* has higher time limit */
93 #define CMPMTME 0x04 /* compare file modification time */
94 #define CMPCTME 0x08 /* compare inode change time */
95 #define CMPBOTH (CMPMTME | CMPCTME) /* compare inode and mod time */
96 struct time_rng *fow; /* next pattern */
97 } TIME_RNG;
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 specified 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 unsigned int indx;
149 USRT *pt;
150 uid_t uid;
151
152 /*
153 * create the table if it doesn't exist
154 */
155 if ((str == NULL) || (*str == '\0'))
156 return(-1);
157 if ((usrtb == NULL) &&
158 ((usrtb = calloc(USR_TB_SZ, sizeof(USRT *))) == NULL)) {
159 paxwarn(1, "%s for %s", "Out of memory",
160 "user selection table");
161 return (-1);
162 }
163
164 /*
165 * figure out user spec
166 */
167 if (str[0] != '#') {
168 /*
169 * it is a user name, \# escapes # as first char in user name
170 */
171 if ((str[0] == '\\') && (str[1] == '#'))
172 ++str;
173 if (uid_uncached(str, &uid) < 0) {
174 paxwarn(1, "Unable to find uid for user %s", str);
175 return (-1);
176 }
177 } else
178 uid = (uid_t)strtoul(str+1, NULL, 10);
179 endpwent();
180
181 /*
182 * hash it and go down the hash chain (if any) looking for it
183 */
184 indx = ((unsigned)uid) % USR_TB_SZ;
185 if ((pt = usrtb[indx]) != NULL) {
186 while (pt != NULL) {
187 if (pt->uid == uid)
188 return(0);
189 pt = pt->fow;
190 }
191 }
192
193 /*
194 * uid is not yet in the table, add it to the front of the chain
195 */
196 if ((pt = malloc(sizeof(USRT))) != NULL) {
197 pt->uid = uid;
198 pt->fow = usrtb[indx];
199 usrtb[indx] = pt;
200 return(0);
201 }
202 paxwarn(1, "%s for %s", "Out of memory",
203 "user selection table");
204 return (-1);
205 }
206
207 /*
208 * usr_match()
209 * check if this files uid matches a selected uid.
210 * Return:
211 * 0 if this archive member should be processed, 1 if it should be skipped
212 */
213
214 static int
215 usr_match(ARCHD *arcn)
216 {
217 USRT *pt;
218
219 /*
220 * hash and look for it in the table
221 */
222 pt = usrtb[((unsigned)arcn->sb.st_uid) % USR_TB_SZ];
223 while (pt != NULL) {
224 if (pt->uid == arcn->sb.st_uid)
225 return(0);
226 pt = pt->fow;
227 }
228
229 /*
230 * not found
231 */
232 return(1);
233 }
234
235 /*
236 * grp_add()
237 * add a group match to the group match hash table
238 * Return:
239 * 0 if added ok, -1 otherwise;
240 */
241
242 int
243 grp_add(char *str)
244 {
245 unsigned int indx;
246 GRPT *pt;
247 gid_t gid;
248
249 /*
250 * create the table if it doesn't exist
251 */
252 if ((str == NULL) || (*str == '\0'))
253 return(-1);
254 if ((grptb == NULL) &&
255 ((grptb = calloc(GRP_TB_SZ, sizeof(GRPT *))) == NULL)) {
256 paxwarn(1, "%s for %s", "Out of memory",
257 "group selection table");
258 return (-1);
259 }
260
261 /*
262 * figure out group spec
263 */
264 if (str[0] != '#') {
265 /*
266 * it is a group name, \# escapes # as first char in group name
267 */
268 if ((str[0] == '\\') && (str[1] == '#'))
269 ++str;
270 if (gid_uncached(str, &gid) < 0) {
271 paxwarn(1, "Unable to find gid for group %s", str);
272 return (-1);
273 }
274 } else
275 gid = (gid_t)strtoul(str+1, NULL, 10);
276 endgrent();
277
278 /*
279 * hash it and go down the hash chain (if any) looking for it
280 */
281 indx = ((unsigned)gid) % GRP_TB_SZ;
282 if ((pt = grptb[indx]) != NULL) {
283 while (pt != NULL) {
284 if (pt->gid == gid)
285 return(0);
286 pt = pt->fow;
287 }
288 }
289
290 /*
291 * gid not in the table, add it to the front of the chain
292 */
293 if ((pt = malloc(sizeof(GRPT))) != NULL) {
294 pt->gid = gid;
295 pt->fow = grptb[indx];
296 grptb[indx] = pt;
297 return(0);
298 }
299 paxwarn(1, "%s for %s", "Out of memory",
300 "group selection table");
301 return (-1);
302 }
303
304 /*
305 * grp_match()
306 * check if this files gid matches a selected gid.
307 * Return:
308 * 0 if this archive member should be processed, 1 if it should be skipped
309 */
310
311 static int
312 grp_match(ARCHD *arcn)
313 {
314 GRPT *pt;
315
316 /*
317 * hash and look for it in the table
318 */
319 pt = grptb[((unsigned)arcn->sb.st_gid) % GRP_TB_SZ];
320 while (pt != NULL) {
321 if (pt->gid == arcn->sb.st_gid)
322 return(0);
323 pt = pt->fow;
324 }
325
326 /*
327 * not found
328 */
329 return(1);
330 }
331
332 /*
333 * Time range selection routines
334 *
335 * Routines to handle user selection of files based on the modification and/or
336 * inode change time falling within a specified time range (the non-standard
337 * -T flag). The user may specify any number of different file time ranges.
338 * Time ranges are checked one at a time until a match is found (if at all).
339 * If the file has a mtime (and/or ctime) which lies within one of the time
340 * ranges, the file is selected. Time ranges may have a lower and/or a upper
341 * value. These ranges are inclusive. When no time ranges are supplied to pax
342 * with the -T option, all members in the archive will be selected by the time
343 * range routines. When only a lower range is supplied, only files with a
344 * mtime (and/or ctime) equal to or younger are selected. When only a upper
345 * range is supplied, only files with a mtime (and/or ctime) equal to or older
346 * are selected. When the lower time range is equal to the upper time range,
347 * only files with a mtime (or ctime) of exactly that time are selected.
348 */
349
350 /*
351 * trng_add()
352 * add a time range match to the time range list.
353 * This is a non-standard pax option. Lower and upper ranges are in the
354 * format: [[[[[cc]yy]mm]dd]HH]MM[.SS] and are comma separated.
355 * Time ranges are based on current time, so 1234 would specify a time of
356 * 12:34 today.
357 * Return:
358 * 0 if the time range was added to the list, -1 otherwise
359 */
360
361 int
362 trng_add(char *str)
363 {
364 TIME_RNG *pt;
365 char *up_pt = NULL;
366 char *stpt;
367 char *flgpt;
368 int dot = 0;
369
370 /*
371 * throw out the badly formed time ranges
372 */
373 if ((str == NULL) || (*str == '\0')) {
374 paxwarn(1, "Empty time range string");
375 return(-1);
376 }
377
378 /*
379 * locate optional flags suffix /{cm}.
380 */
381 if ((flgpt = strrchr(str, '/')) != NULL)
382 *flgpt++ = '\0';
383
384 for (stpt = str; *stpt != '\0'; ++stpt) {
385 if ((*stpt >= '0') && (*stpt <= '9'))
386 continue;
387 if ((*stpt == ',') && (up_pt == NULL)) {
388 *stpt = '\0';
389 up_pt = stpt + 1;
390 dot = 0;
391 continue;
392 }
393
394 /*
395 * allow only one dot per range (secs)
396 */
397 if ((*stpt == '.') && (!dot)) {
398 ++dot;
399 continue;
400 }
401 paxwarn(1, "Improperly specified time range: %s", str);
402 goto out;
403 }
404
405 /*
406 * allocate space for the time range and store the limits
407 */
408 if ((pt = malloc(sizeof(TIME_RNG))) == NULL) {
409 paxwarn(1, "%s for %s", "Out of memory",
410 "time range");
411 return (-1);
412 }
413
414 /*
415 * by default we only will check file mtime, but user can specify
416 * mtime, ctime (inode change time) or both.
417 */
418 if ((flgpt == NULL) || (*flgpt == '\0'))
419 pt->flgs = CMPMTME;
420 else {
421 pt->flgs = 0;
422 while (*flgpt != '\0') {
423 switch (*flgpt) {
424 case 'M':
425 case 'm':
426 pt->flgs |= CMPMTME;
427 break;
428 case 'C':
429 case 'c':
430 pt->flgs |= CMPCTME;
431 break;
432 default:
433 paxwarn(1, "Bad option %c with time range %s",
434 *flgpt, str);
435 free(pt);
436 goto out;
437 }
438 ++flgpt;
439 }
440 }
441
442 /*
443 * start off with the current time
444 */
445 pt->low_time = pt->high_time = time(NULL);
446 if (*str != '\0') {
447 /*
448 * add lower limit
449 */
450 if (str_sec(str, &(pt->low_time)) < 0) {
451 paxwarn(1, "Illegal %ser time range %s", "low", str);
452 free(pt);
453 goto out;
454 }
455 pt->flgs |= HASLOW;
456 }
457
458 if ((up_pt != NULL) && (*up_pt != '\0')) {
459 /*
460 * add upper limit
461 */
462 if (str_sec(up_pt, &(pt->high_time)) < 0) {
463 paxwarn(1, "Illegal %ser time range %s", "upp", up_pt);
464 free(pt);
465 goto out;
466 }
467 pt->flgs |= HASHIGH;
468
469 /*
470 * check that the upper and lower do not overlap
471 */
472 if (pt->flgs & HASLOW) {
473 if (pt->low_time > pt->high_time) {
474 paxwarn(1, "Upper %s and lower %s time overlap",
475 up_pt, str);
476 free(pt);
477 return(-1);
478 }
479 }
480 }
481
482 pt->fow = NULL;
483 if (trhead == NULL) {
484 trtail = trhead = pt;
485 return(0);
486 }
487 trtail->fow = pt;
488 trtail = pt;
489 return(0);
490
491 out:
492 paxwarn(1, "Time range format is: [[[[[cc]yy]mm]dd]HH]MM[.SS][/[c][m]]");
493 return(-1);
494 }
495
496 /*
497 * trng_match()
498 * check if this files mtime/ctime falls within any supplied time range.
499 * Return:
500 * 0 if this archive member should be processed, 1 if it should be skipped
501 */
502
503 static int
504 trng_match(ARCHD *arcn)
505 {
506 TIME_RNG *pt;
507
508 /*
509 * have to search down the list one at a time looking for a match.
510 * remember time range limits are inclusive.
511 */
512 pt = trhead;
513 while (pt != NULL) {
514 switch (pt->flgs & CMPBOTH) {
515 case CMPBOTH:
516 /*
517 * user wants both mtime and ctime checked for this
518 * time range
519 */
520 if (((pt->flgs & HASLOW) &&
521 (arcn->sb.st_mtime < pt->low_time) &&
522 (arcn->sb.st_ctime < pt->low_time)) ||
523 ((pt->flgs & HASHIGH) &&
524 (arcn->sb.st_mtime > pt->high_time) &&
525 (arcn->sb.st_ctime > pt->high_time))) {
526 pt = pt->fow;
527 continue;
528 }
529 break;
530 case CMPCTME:
531 /*
532 * user wants only ctime checked for this time range
533 */
534 if (((pt->flgs & HASLOW) &&
535 (arcn->sb.st_ctime < pt->low_time)) ||
536 ((pt->flgs & HASHIGH) &&
537 (arcn->sb.st_ctime > pt->high_time))) {
538 pt = pt->fow;
539 continue;
540 }
541 break;
542 case CMPMTME:
543 default:
544 /*
545 * user wants only mtime checked for this time range
546 */
547 if (((pt->flgs & HASLOW) &&
548 (arcn->sb.st_mtime < pt->low_time)) ||
549 ((pt->flgs & HASHIGH) &&
550 (arcn->sb.st_mtime > pt->high_time))) {
551 pt = pt->fow;
552 continue;
553 }
554 break;
555 }
556 break;
557 }
558
559 if (pt == NULL)
560 return(1);
561 return(0);
562 }
563
564 /*
565 * str_sec()
566 * Convert a time string in the format of [[[[[cc]yy]mm]dd]HH]MM[.SS] to
567 * seconds UTC. Tval already has current time loaded into it at entry.
568 * Return:
569 * 0 if converted ok, -1 otherwise
570 */
571
572 static int
573 str_sec(const char *p, time_t *tval)
574 {
575 struct tm *lt;
576 const char *dot, *t;
577 size_t len;
578 int bigyear;
579 int yearset;
580
581 yearset = 0;
582 len = strlen(p);
583
584 for (t = p, dot = NULL; *t; ++t) {
585 if (isdigit((unsigned char)*t))
586 continue;
587 if (*t == '.' && dot == NULL) {
588 dot = t;
589 continue;
590 }
591 return(-1);
592 }
593
594 lt = localtime(tval);
595
596 if (dot != NULL) { /* .SS */
597 if (strlen(++dot) != 2)
598 return(-1);
599 lt->tm_sec = ATOI2(dot);
600 if (lt->tm_sec > 61)
601 return(-1);
602 len -= 3;
603 } else
604 lt->tm_sec = 0;
605
606 switch (len) {
607 case 12: /* cc */
608 bigyear = ATOI2(p);
609 lt->tm_year = (bigyear * 100LL) - 1900LL;
610 yearset = 1;
611 /* FALLTHROUGH */
612 case 10: /* yy */
613 if (yearset) {
614 lt->tm_year += ATOI2(p);
615 } else {
616 lt->tm_year = ATOI2(p);
617 if (lt->tm_year < 69) /* hack for 2000 ;-} */
618 lt->tm_year += (2000 - 1900);
619 }
620 /* FALLTHROUGH */
621 case 8: /* mm */
622 lt->tm_mon = ATOI2(p);
623 if ((lt->tm_mon > 12) || !lt->tm_mon)
624 return(-1);
625 --lt->tm_mon; /* time struct is 0 - 11 */
626 /* FALLTHROUGH */
627 case 6: /* dd */
628 lt->tm_mday = ATOI2(p);
629 if ((lt->tm_mday > 31) || !lt->tm_mday)
630 return(-1);
631 /* FALLTHROUGH */
632 case 4: /* HH */
633 lt->tm_hour = ATOI2(p);
634 if (lt->tm_hour > 23)
635 return(-1);
636 /* FALLTHROUGH */
637 case 2: /* MM */
638 lt->tm_min = ATOI2(p);
639 if (lt->tm_min > 59)
640 return(-1);
641 break;
642 default:
643 return(-1);
644 }
645
646 /* convert broken-down time to UTC clock time seconds */
647 if ((*tval = mktime(lt)) == -1)
648 return(-1);
649 return(0);
650 }