apply new hurd patch to my tree
[debian/pax] / sel_subs.c
1 /*      $OpenBSD: sel_subs.c,v 1.18 2004/04/16 22:50:23 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 #ifndef lint
38 #if 0
39 static const char sccsid[] = "@(#)sel_subs.c    8.1 (Berkeley) 5/31/93";
40 #else
41 static const char rcsid[] = "$OpenBSD: sel_subs.c,v 1.18 2004/04/16 22:50:23 deraadt Exp $";
42 #endif
43 #endif /* not lint */
44
45 #include <sys/types.h>
46 #include <sys/time.h>
47 #include <time.h>
48 #include <sys/stat.h>
49 #include <sys/param.h>
50 #include <ctype.h>
51 #include <grp.h>
52 #include <pwd.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <time.h>
57 #include <unistd.h>
58 #include "pax.h"
59 #include "sel_subs.h"
60 #include "extern.h"
61
62 #define TM_YEAR_BASE 1900
63
64 static int str_sec(const char *, time_t *);
65 static int usr_match(ARCHD *);
66 static int grp_match(ARCHD *);
67 static int trng_match(ARCHD *);
68
69 static TIME_RNG *trhead = NULL;         /* time range list head */
70 static TIME_RNG *trtail = NULL;         /* time range list tail */
71 static USRT **usrtb = NULL;             /* user selection table */
72 static GRPT **grptb = NULL;             /* group selection table */
73
74 /*
75  * Routines for selection of archive members
76  */
77
78 /*
79  * sel_chk()
80  *      check if this file matches a specified uid, gid or time range
81  * Return:
82  *      0 if this archive member should be processed, 1 if it should be skipped
83  */
84
85 int
86 sel_chk(ARCHD *arcn)
87 {
88         if (((usrtb != NULL) && usr_match(arcn)) ||
89             ((grptb != NULL) && grp_match(arcn)) ||
90             ((trhead != NULL) && trng_match(arcn)))
91                 return(1);
92         return(0);
93 }
94
95 /*
96  * User/group selection routines
97  *
98  * Routines to handle user selection of files based on the file uid/gid. To
99  * add an entry, the user supplies either the name or the uid/gid starting with
100  * a # on the command line. A \# will escape the #.
101  */
102
103 /*
104  * usr_add()
105  *      add a user match to the user match hash table
106  * Return:
107  *      0 if added ok, -1 otherwise;
108  */
109
110 int
111 usr_add(char *str)
112 {
113         u_int indx;
114         USRT *pt;
115         struct passwd *pw;
116         uid_t uid;
117
118         /*
119          * create the table if it doesn't exist
120          */
121         if ((str == NULL) || (*str == '\0'))
122                 return(-1);
123         if ((usrtb == NULL) &&
124             ((usrtb = (USRT **)calloc(USR_TB_SZ, sizeof(USRT *))) == NULL)) {
125                 paxwarn(1, "Unable to allocate memory for user selection table");
126                 return(-1);
127         }
128
129         /*
130          * figure out user spec
131          */
132         if (str[0] != '#') {
133                 /*
134                  * it is a user name, \# escapes # as first char in user name
135                  */
136                 if ((str[0] == '\\') && (str[1] == '#'))
137                         ++str;
138                 if ((pw = getpwnam(str)) == NULL) {
139                         paxwarn(1, "Unable to find uid for user: %s", str);
140                         return(-1);
141                 }
142                 uid = (uid_t)pw->pw_uid;
143         } else
144                 uid = (uid_t)strtoul(str+1, NULL, 10);
145         endpwent();
146
147         /*
148          * hash it and go down the hash chain (if any) looking for it
149          */
150         indx = ((unsigned)uid) % USR_TB_SZ;
151         if ((pt = usrtb[indx]) != NULL) {
152                 while (pt != NULL) {
153                         if (pt->uid == uid)
154                                 return(0);
155                         pt = pt->fow;
156                 }
157         }
158
159         /*
160          * uid is not yet in the table, add it to the front of the chain
161          */
162         if ((pt = (USRT *)malloc(sizeof(USRT))) != NULL) {
163                 pt->uid = uid;
164                 pt->fow = usrtb[indx];
165                 usrtb[indx] = pt;
166                 return(0);
167         }
168         paxwarn(1, "User selection table out of memory");
169         return(-1);
170 }
171
172 /*
173  * usr_match()
174  *      check if this files uid matches a selected uid.
175  * Return:
176  *      0 if this archive member should be processed, 1 if it should be skipped
177  */
178
179 static int
180 usr_match(ARCHD *arcn)
181 {
182         USRT *pt;
183
184         /*
185          * hash and look for it in the table
186          */
187         pt = usrtb[((unsigned)arcn->sb.st_uid) % USR_TB_SZ];
188         while (pt != NULL) {
189                 if (pt->uid == arcn->sb.st_uid)
190                         return(0);
191                 pt = pt->fow;
192         }
193
194         /*
195          * not found
196          */
197         return(1);
198 }
199
200 /*
201  * grp_add()
202  *      add a group match to the group match hash table
203  * Return:
204  *      0 if added ok, -1 otherwise;
205  */
206
207 int
208 grp_add(char *str)
209 {
210         u_int indx;
211         GRPT *pt;
212         struct group *gr;
213         gid_t gid;
214
215         /*
216          * create the table if it doesn't exist
217          */
218         if ((str == NULL) || (*str == '\0'))
219                 return(-1);
220         if ((grptb == NULL) &&
221             ((grptb = (GRPT **)calloc(GRP_TB_SZ, sizeof(GRPT *))) == NULL)) {
222                 paxwarn(1, "Unable to allocate memory fo group selection table");
223                 return(-1);
224         }
225
226         /*
227          * figure out user spec
228          */
229         if (str[0] != '#') {
230                 /*
231                  * it is a group name, \# escapes # as first char in group name
232                  */
233                 if ((str[0] == '\\') && (str[1] == '#'))
234                         ++str;
235                 if ((gr = getgrnam(str)) == NULL) {
236                         paxwarn(1,"Cannot determine gid for group name: %s", str);
237                         return(-1);
238                 }
239                 gid = (gid_t)gr->gr_gid;
240         } else
241                 gid = (gid_t)strtoul(str+1, NULL, 10);
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
276 static int
277 grp_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 a 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 a 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: [[[[[cc]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
326 int
327 trng_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 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                                 goto out;
400                         }
401                         ++flgpt;
402                 }
403         }
404
405         /*
406          * start off with the current time
407          */
408         pt->low_time = pt->high_time = time(NULL);
409         if (*str != '\0') {
410                 /*
411                  * add lower limit
412                  */
413                 if (str_sec(str, &(pt->low_time)) < 0) {
414                         paxwarn(1, "Illegal lower time range %s", str);
415                         (void)free((char *)pt);
416                         goto out;
417                 }
418                 pt->flgs |= HASLOW;
419         }
420
421         if ((up_pt != NULL) && (*up_pt != '\0')) {
422                 /*
423                  * add upper limit
424                  */
425                 if (str_sec(up_pt, &(pt->high_time)) < 0) {
426                         paxwarn(1, "Illegal upper time range %s", up_pt);
427                         (void)free((char *)pt);
428                         goto out;
429                 }
430                 pt->flgs |= HASHIGH;
431
432                 /*
433                  * check that the upper and lower do not overlap
434                  */
435                 if (pt->flgs & HASLOW) {
436                         if (pt->low_time > pt->high_time) {
437                                 paxwarn(1, "Upper %s and lower %s time overlap",
438                                         up_pt, str);
439                                 (void)free((char *)pt);
440                                 return(-1);
441                         }
442                 }
443         }
444
445         pt->fow = NULL;
446         if (trhead == NULL) {
447                 trtail = trhead = pt;
448                 return(0);
449         }
450         trtail->fow = pt;
451         trtail = pt;
452         return(0);
453
454     out:
455         paxwarn(1, "Time range format is: [[[[[cc]yy]mm]dd]HH]MM[.SS][/[c][m]]");
456         return(-1);
457 }
458
459 /*
460  * trng_match()
461  *      check if this files mtime/ctime falls within any supplied time range.
462  * Return:
463  *      0 if this archive member should be processed, 1 if it should be skipped
464  */
465
466 static int
467 trng_match(ARCHD *arcn)
468 {
469         TIME_RNG *pt;
470
471         /*
472          * have to search down the list one at a time looking for a match.
473          * remember time range limits are inclusive.
474          */
475         pt = trhead;
476         while (pt != NULL) {
477                 switch (pt->flgs & CMPBOTH) {
478                 case CMPBOTH:
479                         /*
480                          * user wants both mtime and ctime checked for this
481                          * time range
482                          */
483                         if (((pt->flgs & HASLOW) &&
484                             (arcn->sb.st_mtime < pt->low_time) &&
485                             (arcn->sb.st_ctime < pt->low_time)) ||
486                             ((pt->flgs & HASHIGH) &&
487                             (arcn->sb.st_mtime > pt->high_time) &&
488                             (arcn->sb.st_ctime > pt->high_time))) {
489                                 pt = pt->fow;
490                                 continue;
491                         }
492                         break;
493                 case CMPCTME:
494                         /*
495                          * user wants only ctime checked for this time range
496                          */
497                         if (((pt->flgs & HASLOW) &&
498                             (arcn->sb.st_ctime < pt->low_time)) ||
499                             ((pt->flgs & HASHIGH) &&
500                             (arcn->sb.st_ctime > pt->high_time))) {
501                                 pt = pt->fow;
502                                 continue;
503                         }
504                         break;
505                 case CMPMTME:
506                 default:
507                         /*
508                          * user wants only mtime checked for this time range
509                          */
510                         if (((pt->flgs & HASLOW) &&
511                             (arcn->sb.st_mtime < pt->low_time)) ||
512                             ((pt->flgs & HASHIGH) &&
513                             (arcn->sb.st_mtime > pt->high_time))) {
514                                 pt = pt->fow;
515                                 continue;
516                         }
517                         break;
518                 }
519                 break;
520         }
521
522         if (pt == NULL)
523                 return(1);
524         return(0);
525 }
526
527 /*
528  * str_sec()
529  *      Convert a time string in the format of [[[[[cc]yy]mm]dd]HH]MM[.SS] to
530  *      seconds UTC. Tval already has current time loaded into it at entry.
531  * Return:
532  *      0 if converted ok, -1 otherwise
533  */
534
535 static int
536 str_sec(const char *p, time_t *tval)
537 {
538         struct tm *lt;
539         const char *dot, *t;
540         size_t len;
541         int bigyear;
542         int yearset;
543
544         yearset = 0;
545         len = strlen(p);
546
547         for (t = p, dot = NULL; *t; ++t) {
548                 if (isdigit(*t))
549                         continue;
550                 if (*t == '.' && dot == NULL) {
551                         dot = t;
552                         continue;
553                 }
554                 return(-1);
555         }
556
557         lt = localtime(tval);
558
559         if (dot != NULL) {                      /* .SS */
560                 if (strlen(++dot) != 2)
561                         return(-1);
562                 lt->tm_sec = ATOI2(dot);
563                 if (lt->tm_sec > 61)
564                         return(-1);
565                 len -= 3;
566         } else
567                 lt->tm_sec = 0;
568
569         switch (len) {
570         case 12:                                /* cc */
571                 bigyear = ATOI2(p);
572                 lt->tm_year = (bigyear * 100) - TM_YEAR_BASE;
573                 yearset = 1;
574                 /* FALLTHROUGH */
575         case 10:                                /* yy */
576                 if (yearset) {
577                         lt->tm_year += ATOI2(p);
578                 } else {
579                         lt->tm_year = ATOI2(p);
580                         if (lt->tm_year < 69)           /* hack for 2000 ;-} */
581                                 lt->tm_year += (2000 - TM_YEAR_BASE);
582                         else
583                                 lt->tm_year += (1900 - TM_YEAR_BASE);
584                 }
585                 /* FALLTHROUGH */
586         case 8:                                 /* mm */
587                 lt->tm_mon = ATOI2(p);
588                 if ((lt->tm_mon > 12) || !lt->tm_mon)
589                         return(-1);
590                 --lt->tm_mon;                   /* time struct is 0 - 11 */
591                 /* FALLTHROUGH */
592         case 6:                                 /* dd */
593                 lt->tm_mday = ATOI2(p);
594                 if ((lt->tm_mday > 31) || !lt->tm_mday)
595                         return(-1);
596                 /* FALLTHROUGH */
597         case 4:                                 /* HH */
598                 lt->tm_hour = ATOI2(p);
599                 if (lt->tm_hour > 23)
600                         return(-1);
601                 /* FALLTHROUGH */
602         case 2:                                 /* MM */
603                 lt->tm_min = ATOI2(p);
604                 if (lt->tm_min > 59)
605                         return(-1);
606                 break;
607         default:
608                 return(-1);
609         }
610
611         /* convert broken-down time to UTC clock time seconds */
612         if ((*tval = mktime(lt)) == -1)
613                 return(-1);
614         return(0);
615 }