1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1992 Keith Muller. 5 * Copyright (c) 1992, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Keith Muller of the University of California, San Diego. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 #endif /* not lint */ 38 #include <sys/cdefs.h> 39 #include <sys/types.h> 40 #include <sys/time.h> 41 #include <sys/stat.h> 42 43 #include <ctype.h> 44 #include <grp.h> 45 #include <pwd.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <strings.h> 50 51 #include "pax.h" 52 #include "sel_subs.h" 53 #include "extern.h" 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 = 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 an 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 an 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 the 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 free(pt); 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(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(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(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: [[[[[cc]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 [[[[[cc]yy]mm]dd]HH]MM[.SS] to 522 * seconds UTC. 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(const char *p, time_t *tval) 529 { 530 struct tm *lt; 531 const char *dot, *t; 532 size_t len; 533 int bigyear; 534 int yearset; 535 536 yearset = 0; 537 len = strlen(p); 538 539 for (t = p, dot = NULL; *t; ++t) { 540 if (isdigit((unsigned char)*t)) 541 continue; 542 if (*t == '.' && dot == NULL) { 543 dot = t; 544 continue; 545 } 546 return(-1); 547 } 548 549 lt = localtime(tval); 550 551 if (dot != NULL) { /* .SS */ 552 if (strlen(++dot) != 2) 553 return(-1); 554 lt->tm_sec = ATOI2(dot); 555 if (lt->tm_sec > 61) 556 return(-1); 557 len -= 3; 558 } else 559 lt->tm_sec = 0; 560 561 switch (len) { 562 case 12: /* cc */ 563 bigyear = ATOI2(p); 564 lt->tm_year = (bigyear * 100) - 1900; 565 yearset = 1; 566 /* FALLTHROUGH */ 567 case 10: /* yy */ 568 if (yearset) { 569 lt->tm_year += ATOI2(p); 570 } else { 571 lt->tm_year = ATOI2(p); 572 if (lt->tm_year < 69) /* hack for 2000 ;-} */ 573 lt->tm_year += (2000 - 1900); 574 } 575 /* FALLTHROUGH */ 576 case 8: /* mm */ 577 lt->tm_mon = ATOI2(p); 578 if ((lt->tm_mon > 12) || !lt->tm_mon) 579 return(-1); 580 --lt->tm_mon; /* time struct is 0 - 11 */ 581 /* FALLTHROUGH */ 582 case 6: /* dd */ 583 lt->tm_mday = ATOI2(p); 584 if ((lt->tm_mday > 31) || !lt->tm_mday) 585 return(-1); 586 /* FALLTHROUGH */ 587 case 4: /* HH */ 588 lt->tm_hour = ATOI2(p); 589 if (lt->tm_hour > 23) 590 return(-1); 591 /* FALLTHROUGH */ 592 case 2: /* MM */ 593 lt->tm_min = ATOI2(p); 594 if (lt->tm_min > 59) 595 return(-1); 596 break; 597 default: 598 return(-1); 599 } 600 601 /* convert broken-down time to UTC clock time seconds */ 602 if ((*tval = mktime(lt)) == -1) 603 return(-1); 604 return(0); 605 } 606