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