source: trunk/minix/commands/pax/sel_subs.c@ 15

Last change on this file since 15 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

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