English Language flag
// Log In
// CVSweb
Project: FreeWRT
// Summary // Activity // Search // Tracker // Lists // News // SCM // Wiki

SCM Repository

ViewVC logotype

Contents of /branches/freewrt_1_0/tools/paxmirabilis/src/sel_subs.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2715 - (show annotations) (download)
Tue Jun 5 14:52:44 2007 UTC (6 years, 8 months ago) by tg
File MIME type: text/plain
File size: 14113 byte(s)
• 1.0 -> scripts/param.h, paxmirabilis: MFC the new version from trunk
• both 1.0 and trunk: implement “make targz”, “make tarbz2”
  (I like the gzip(1)d versions better though)
• 1.0 -> package/config/Makefile: quieten the “clean” target to be consistent
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 #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 <tzfile.h>
47 #include <unistd.h>
48 #include "pax.h"
49 #include "sel_subs.h"
50 #include "extern.h"
51
52 __SCCSID("@(#)sel_subs.c 8.1 (Berkeley) 5/31/93");
53 __RCSID("$MirOS: src/bin/pax/sel_subs.c,v 1.2 2007/02/17 04:52:41 tg Exp $");
54
55 static int str_sec(const char *, time_t *);
56 static int usr_match(ARCHD *);
57 static int grp_match(ARCHD *);
58 static int trng_match(ARCHD *);
59
60 static TIME_RNG *trhead = NULL; /* time range list head */
61 static TIME_RNG *trtail = NULL; /* time range list tail */
62 static USRT **usrtb = NULL; /* user selection table */
63 static 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
76 int
77 sel_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 the 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
101 int
102 usr_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 uid = (uid_t)strtoul(str+1, NULL, 10);
136 endpwent();
137
138 /*
139 * hash it and go down the hash chain (if any) looking for it
140 */
141 indx = ((unsigned)uid) % USR_TB_SZ;
142 if ((pt = usrtb[indx]) != NULL) {
143 while (pt != NULL) {
144 if (pt->uid == uid)
145 return(0);
146 pt = pt->fow;
147 }
148 }
149
150 /*
151 * uid is not yet in the table, add it to the front of the chain
152 */
153 if ((pt = (USRT *)malloc(sizeof(USRT))) != NULL) {
154 pt->uid = uid;
155 pt->fow = usrtb[indx];
156 usrtb[indx] = pt;
157 return(0);
158 }
159 paxwarn(1, "User selection table out of memory");
160 return(-1);
161 }
162
163 /*
164 * usr_match()
165 * check if this files uid matches a selected uid.
166 * Return:
167 * 0 if this archive member should be processed, 1 if it should be skipped
168 */
169
170 static int
171 usr_match(ARCHD *arcn)
172 {
173 USRT *pt;
174
175 /*
176 * hash and look for it in the table
177 */
178 pt = usrtb[((unsigned)arcn->sb.st_uid) % USR_TB_SZ];
179 while (pt != NULL) {
180 if (pt->uid == arcn->sb.st_uid)
181 return(0);
182 pt = pt->fow;
183 }
184
185 /*
186 * not found
187 */
188 return(1);
189 }
190
191 /*
192 * grp_add()
193 * add a group match to the group match hash table
194 * Return:
195 * 0 if added ok, -1 otherwise;
196 */
197
198 int
199 grp_add(char *str)
200 {
201 u_int indx;
202 GRPT *pt;
203 struct group *gr;
204 gid_t gid;
205
206 /*
207 * create the table if it doesn't exist
208 */
209 if ((str == NULL) || (*str == '\0'))
210 return(-1);
211 if ((grptb == NULL) &&
212 ((grptb = (GRPT **)calloc(GRP_TB_SZ, sizeof(GRPT *))) == NULL)) {
213 paxwarn(1, "Unable to allocate memory fo group selection table");
214 return(-1);
215 }
216
217 /*
218 * figure out user spec
219 */
220 if (str[0] != '#') {
221 /*
222 * it is a group name, \# escapes # as first char in group name
223 */
224 if ((str[0] == '\\') && (str[1] == '#'))
225 ++str;
226 if ((gr = getgrnam(str)) == NULL) {
227 paxwarn(1,"Cannot determine gid for group name: %s", str);
228 return(-1);
229 }
230 gid = (gid_t)gr->gr_gid;
231 } else
232 gid = (gid_t)strtoul(str+1, NULL, 10);
233 endgrent();
234
235 /*
236 * hash it and go down the hash chain (if any) looking for it
237 */
238 indx = ((unsigned)gid) % GRP_TB_SZ;
239 if ((pt = grptb[indx]) != NULL) {
240 while (pt != NULL) {
241 if (pt->gid == gid)
242 return(0);
243 pt = pt->fow;
244 }
245 }
246
247 /*
248 * gid not in the table, add it to the front of the chain
249 */
250 if ((pt = (GRPT *)malloc(sizeof(GRPT))) != NULL) {
251 pt->gid = gid;
252 pt->fow = grptb[indx];
253 grptb[indx] = pt;
254 return(0);
255 }
256 paxwarn(1, "Group selection table out of memory");
257 return(-1);
258 }
259
260 /*
261 * grp_match()
262 * check if this files gid matches a selected gid.
263 * Return:
264 * 0 if this archive member should be processed, 1 if it should be skipped
265 */
266
267 static int
268 grp_match(ARCHD *arcn)
269 {
270 GRPT *pt;
271
272 /*
273 * hash and look for it in the table
274 */
275 pt = grptb[((unsigned)arcn->sb.st_gid) % GRP_TB_SZ];
276 while (pt != NULL) {
277 if (pt->gid == arcn->sb.st_gid)
278 return(0);
279 pt = pt->fow;
280 }
281
282 /*
283 * not found
284 */
285 return(1);
286 }
287
288 /*
289 * Time range selection routines
290 *
291 * Routines to handle user selection of files based on the modification and/or
292 * inode change time falling within a specified time range (the non-standard
293 * -T flag). The user may specify any number of different file time ranges.
294 * Time ranges are checked one at a time until a match is found (if at all).
295 * If the file has a mtime (and/or ctime) which lies within one of the time
296 * ranges, the file is selected. Time ranges may have a lower and/or a upper
297 * value. These ranges are inclusive. When no time ranges are supplied to pax
298 * with the -T option, all members in the archive will be selected by the time
299 * range routines. When only a lower range is supplied, only files with a
300 * mtime (and/or ctime) equal to or younger are selected. When only a upper
301 * range is supplied, only files with a mtime (and/or ctime) equal to or older
302 * are selected. When the lower time range is equal to the upper time range,
303 * only files with a mtime (or ctime) of exactly that time are selected.
304 */
305
306 /*
307 * trng_add()
308 * add a time range match to the time range list.
309 * This is a non-standard pax option. Lower and upper ranges are in the
310 * format: [[[[[cc]yy]mm]dd]HH]MM[.SS] and are comma separated.
311 * Time ranges are based on current time, so 1234 would specify a time of
312 * 12:34 today.
313 * Return:
314 * 0 if the time range was added to the list, -1 otherwise
315 */
316
317 int
318 trng_add(char *str)
319 {
320 TIME_RNG *pt;
321 char *up_pt = NULL;
322 char *stpt;
323 char *flgpt;
324 int dot = 0;
325
326 /*
327 * throw out the badly formed time ranges
328 */
329 if ((str == NULL) || (*str == '\0')) {
330 paxwarn(1, "Empty time range string");
331 return(-1);
332 }
333
334 /*
335 * locate optional flags suffix /{cm}.
336 */
337 if ((flgpt = strrchr(str, '/')) != NULL)
338 *flgpt++ = '\0';
339
340 for (stpt = str; *stpt != '\0'; ++stpt) {
341 if ((*stpt >= '0') && (*stpt <= '9'))
342 continue;
343 if ((*stpt == ',') && (up_pt == NULL)) {
344 *stpt = '\0';
345 up_pt = stpt + 1;
346 dot = 0;
347 continue;
348 }
349
350 /*
351 * allow only one dot per range (secs)
352 */
353 if ((*stpt == '.') && (!dot)) {
354 ++dot;
355 continue;
356 }
357 paxwarn(1, "Improperly specified time range: %s", str);
358 goto out;
359 }
360
361 /*
362 * allocate space for the time range and store the limits
363 */
364 if ((pt = (TIME_RNG *)malloc(sizeof(TIME_RNG))) == NULL) {
365 paxwarn(1, "Unable to allocate memory for time range");
366 return(-1);
367 }
368
369 /*
370 * by default we only will check file mtime, but user can specify
371 * mtime, ctime (inode change time) or both.
372 */
373 if ((flgpt == NULL) || (*flgpt == '\0'))
374 pt->flgs = CMPMTME;
375 else {
376 pt->flgs = 0;
377 while (*flgpt != '\0') {
378 switch (*flgpt) {
379 case 'M':
380 case 'm':
381 pt->flgs |= CMPMTME;
382 break;
383 case 'C':
384 case 'c':
385 pt->flgs |= CMPCTME;
386 break;
387 default:
388 paxwarn(1, "Bad option %c with time range %s",
389 *flgpt, str);
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
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
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 }

root@freewrt.org:443
ViewVC Help
Powered by ViewVC 1.1.20