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