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