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