| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#include <sys/types.h> |
| 38 |
#if HAVE_BOTH_TIME_H |
| 39 |
#include <sys/time.h> |
| 40 |
#include <time.h> |
| 41 |
#elif HAVE_SYS_TIME_H |
| 42 |
#include <sys/time.h> |
| 43 |
#elif HAVE_TIME_H |
| 44 |
#include <time.h> |
| 45 |
#endif |
| 46 |
#include <sys/stat.h> |
| 47 |
#include <ctype.h> |
| 48 |
#if HAVE_GRP_H |
| 49 |
#include <grp.h> |
| 50 |
#endif |
| 51 |
#include <pwd.h> |
| 52 |
#include <stdio.h> |
| 53 |
#include <stdlib.h> |
| 54 |
#include <string.h> |
| 55 |
#if HAVE_STRINGS_H |
| 56 |
#include <strings.h> |
| 57 |
#endif |
| 58 |
|
| 59 |
#include "pax.h" |
| 60 |
#include "extern.h" |
| 61 |
|
| 62 |
__RCSID("$MirOS: src/bin/pax/sel_subs.c,v 1.11 2020/04/07 11:56:43 tg Exp $"); |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
#define USR_TB_SZ 317 |
| 69 |
#define GRP_TB_SZ 317 |
| 70 |
|
| 71 |
typedef struct usrt { |
| 72 |
uid_t uid; |
| 73 |
struct usrt *fow; |
| 74 |
} USRT; |
| 75 |
|
| 76 |
typedef struct grpt { |
| 77 |
gid_t gid; |
| 78 |
struct grpt *fow; |
| 79 |
} GRPT; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
#define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2; |
| 86 |
|
| 87 |
typedef struct time_rng { |
| 88 |
time_t low_time; |
| 89 |
time_t high_time; |
| 90 |
int flgs; |
| 91 |
#define HASLOW 0x01 |
| 92 |
#define HASHIGH 0x02 |
| 93 |
#define CMPMTME 0x04 |
| 94 |
#define CMPCTME 0x08 |
| 95 |
#define CMPBOTH (CMPMTME | CMPCTME) |
| 96 |
struct time_rng *fow; |
| 97 |
} TIME_RNG; |
| 98 |
|
| 99 |
static int str_sec(const char *, time_t *); |
| 100 |
static int usr_match(ARCHD *); |
| 101 |
static int grp_match(ARCHD *); |
| 102 |
static int trng_match(ARCHD *); |
| 103 |
|
| 104 |
static TIME_RNG *trhead = NULL; |
| 105 |
static TIME_RNG *trtail = NULL; |
| 106 |
static USRT **usrtb = NULL; |
| 107 |
static GRPT **grptb = NULL; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
int |
| 121 |
sel_chk(ARCHD *arcn) |
| 122 |
{ |
| 123 |
if (((usrtb != NULL) && usr_match(arcn)) || |
| 124 |
((grptb != NULL) && grp_match(arcn)) || |
| 125 |
((trhead != NULL) && trng_match(arcn))) |
| 126 |
return(1); |
| 127 |
return(0); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
int |
| 146 |
usr_add(char *str) |
| 147 |
{ |
| 148 |
unsigned int indx; |
| 149 |
USRT *pt; |
| 150 |
uid_t uid; |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
if ((str == NULL) || (*str == '\0')) |
| 156 |
return(-1); |
| 157 |
if ((usrtb == NULL) && |
| 158 |
((usrtb = calloc(USR_TB_SZ, sizeof(USRT *))) == NULL)) { |
| 159 |
paxwarn(1, "%s for %s", "Out of memory", |
| 160 |
"user selection table"); |
| 161 |
return (-1); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
if (str[0] != '#') { |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
if ((str[0] == '\\') && (str[1] == '#')) |
| 172 |
++str; |
| 173 |
if (uid_uncached(str, &uid) < 0) { |
| 174 |
paxwarn(1, "Unable to find uid for user %s", str); |
| 175 |
return (-1); |
| 176 |
} |
| 177 |
} else |
| 178 |
uid = (uid_t)strtoul(str+1, NULL, 10); |
| 179 |
endpwent(); |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
indx = ((unsigned)uid) % USR_TB_SZ; |
| 185 |
if ((pt = usrtb[indx]) != NULL) { |
| 186 |
while (pt != NULL) { |
| 187 |
if (pt->uid == uid) |
| 188 |
return(0); |
| 189 |
pt = pt->fow; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
if ((pt = malloc(sizeof(USRT))) != NULL) { |
| 197 |
pt->uid = uid; |
| 198 |
pt->fow = usrtb[indx]; |
| 199 |
usrtb[indx] = pt; |
| 200 |
return(0); |
| 201 |
} |
| 202 |
paxwarn(1, "%s for %s", "Out of memory", |
| 203 |
"user selection table"); |
| 204 |
return (-1); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
static int |
| 215 |
usr_match(ARCHD *arcn) |
| 216 |
{ |
| 217 |
USRT *pt; |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
pt = usrtb[((unsigned)arcn->sb.st_uid) % USR_TB_SZ]; |
| 223 |
while (pt != NULL) { |
| 224 |
if (pt->uid == arcn->sb.st_uid) |
| 225 |
return(0); |
| 226 |
pt = pt->fow; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
return(1); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
int |
| 243 |
grp_add(char *str) |
| 244 |
{ |
| 245 |
unsigned int indx; |
| 246 |
GRPT *pt; |
| 247 |
gid_t gid; |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
if ((str == NULL) || (*str == '\0')) |
| 253 |
return(-1); |
| 254 |
if ((grptb == NULL) && |
| 255 |
((grptb = calloc(GRP_TB_SZ, sizeof(GRPT *))) == NULL)) { |
| 256 |
paxwarn(1, "%s for %s", "Out of memory", |
| 257 |
"group selection table"); |
| 258 |
return (-1); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
if (str[0] != '#') { |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
if ((str[0] == '\\') && (str[1] == '#')) |
| 269 |
++str; |
| 270 |
if (gid_uncached(str, &gid) < 0) { |
| 271 |
paxwarn(1, "Unable to find gid for group %s", str); |
| 272 |
return (-1); |
| 273 |
} |
| 274 |
} else |
| 275 |
gid = (gid_t)strtoul(str+1, NULL, 10); |
| 276 |
endgrent(); |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
indx = ((unsigned)gid) % GRP_TB_SZ; |
| 282 |
if ((pt = grptb[indx]) != NULL) { |
| 283 |
while (pt != NULL) { |
| 284 |
if (pt->gid == gid) |
| 285 |
return(0); |
| 286 |
pt = pt->fow; |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
if ((pt = malloc(sizeof(GRPT))) != NULL) { |
| 294 |
pt->gid = gid; |
| 295 |
pt->fow = grptb[indx]; |
| 296 |
grptb[indx] = pt; |
| 297 |
return(0); |
| 298 |
} |
| 299 |
paxwarn(1, "%s for %s", "Out of memory", |
| 300 |
"group selection table"); |
| 301 |
return (-1); |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
static int |
| 312 |
grp_match(ARCHD *arcn) |
| 313 |
{ |
| 314 |
GRPT *pt; |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
pt = grptb[((unsigned)arcn->sb.st_gid) % GRP_TB_SZ]; |
| 320 |
while (pt != NULL) { |
| 321 |
if (pt->gid == arcn->sb.st_gid) |
| 322 |
return(0); |
| 323 |
pt = pt->fow; |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
return(1); |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
int |
| 362 |
trng_add(char *str) |
| 363 |
{ |
| 364 |
TIME_RNG *pt; |
| 365 |
char *up_pt = NULL; |
| 366 |
char *stpt; |
| 367 |
char *flgpt; |
| 368 |
int dot = 0; |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
if ((str == NULL) || (*str == '\0')) { |
| 374 |
paxwarn(1, "Empty time range string"); |
| 375 |
return(-1); |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
if ((flgpt = strrchr(str, '/')) != NULL) |
| 382 |
*flgpt++ = '\0'; |
| 383 |
|
| 384 |
for (stpt = str; *stpt != '\0'; ++stpt) { |
| 385 |
if ((*stpt >= '0') && (*stpt <= '9')) |
| 386 |
continue; |
| 387 |
if ((*stpt == ',') && (up_pt == NULL)) { |
| 388 |
*stpt = '\0'; |
| 389 |
up_pt = stpt + 1; |
| 390 |
dot = 0; |
| 391 |
continue; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
if ((*stpt == '.') && (!dot)) { |
| 398 |
++dot; |
| 399 |
continue; |
| 400 |
} |
| 401 |
paxwarn(1, "Improperly specified time range: %s", str); |
| 402 |
goto out; |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
if ((pt = malloc(sizeof(TIME_RNG))) == NULL) { |
| 409 |
paxwarn(1, "%s for %s", "Out of memory", |
| 410 |
"time range"); |
| 411 |
return (-1); |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
if ((flgpt == NULL) || (*flgpt == '\0')) |
| 419 |
pt->flgs = CMPMTME; |
| 420 |
else { |
| 421 |
pt->flgs = 0; |
| 422 |
while (*flgpt != '\0') { |
| 423 |
switch (*flgpt) { |
| 424 |
case 'M': |
| 425 |
case 'm': |
| 426 |
pt->flgs |= CMPMTME; |
| 427 |
break; |
| 428 |
case 'C': |
| 429 |
case 'c': |
| 430 |
pt->flgs |= CMPCTME; |
| 431 |
break; |
| 432 |
default: |
| 433 |
paxwarn(1, "Bad option %c with time range %s", |
| 434 |
*flgpt, str); |
| 435 |
free(pt); |
| 436 |
goto out; |
| 437 |
} |
| 438 |
++flgpt; |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
pt->low_time = pt->high_time = time(NULL); |
| 446 |
if (*str != '\0') { |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
if (str_sec(str, &(pt->low_time)) < 0) { |
| 451 |
paxwarn(1, "Illegal %ser time range %s", "low", str); |
| 452 |
free(pt); |
| 453 |
goto out; |
| 454 |
} |
| 455 |
pt->flgs |= HASLOW; |
| 456 |
} |
| 457 |
|
| 458 |
if ((up_pt != NULL) && (*up_pt != '\0')) { |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
if (str_sec(up_pt, &(pt->high_time)) < 0) { |
| 463 |
paxwarn(1, "Illegal %ser time range %s", "upp", up_pt); |
| 464 |
free(pt); |
| 465 |
goto out; |
| 466 |
} |
| 467 |
pt->flgs |= HASHIGH; |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
if (pt->flgs & HASLOW) { |
| 473 |
if (pt->low_time > pt->high_time) { |
| 474 |
paxwarn(1, "Upper %s and lower %s time overlap", |
| 475 |
up_pt, str); |
| 476 |
free(pt); |
| 477 |
return(-1); |
| 478 |
} |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
pt->fow = NULL; |
| 483 |
if (trhead == NULL) { |
| 484 |
trtail = trhead = pt; |
| 485 |
return(0); |
| 486 |
} |
| 487 |
trtail->fow = pt; |
| 488 |
trtail = pt; |
| 489 |
return(0); |
| 490 |
|
| 491 |
out: |
| 492 |
paxwarn(1, "Time range format is: [[[[[cc]yy]mm]dd]HH]MM[.SS][/[c][m]]"); |
| 493 |
return(-1); |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
static int |
| 504 |
trng_match(ARCHD *arcn) |
| 505 |
{ |
| 506 |
TIME_RNG *pt; |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
pt = trhead; |
| 513 |
while (pt != NULL) { |
| 514 |
switch (pt->flgs & CMPBOTH) { |
| 515 |
case CMPBOTH: |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
if (((pt->flgs & HASLOW) && |
| 521 |
(arcn->sb.st_mtime < pt->low_time) && |
| 522 |
(arcn->sb.st_ctime < pt->low_time)) || |
| 523 |
((pt->flgs & HASHIGH) && |
| 524 |
(arcn->sb.st_mtime > pt->high_time) && |
| 525 |
(arcn->sb.st_ctime > pt->high_time))) { |
| 526 |
pt = pt->fow; |
| 527 |
continue; |
| 528 |
} |
| 529 |
break; |
| 530 |
case CMPCTME: |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
if (((pt->flgs & HASLOW) && |
| 535 |
(arcn->sb.st_ctime < pt->low_time)) || |
| 536 |
((pt->flgs & HASHIGH) && |
| 537 |
(arcn->sb.st_ctime > pt->high_time))) { |
| 538 |
pt = pt->fow; |
| 539 |
continue; |
| 540 |
} |
| 541 |
break; |
| 542 |
case CMPMTME: |
| 543 |
default: |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
if (((pt->flgs & HASLOW) && |
| 548 |
(arcn->sb.st_mtime < pt->low_time)) || |
| 549 |
((pt->flgs & HASHIGH) && |
| 550 |
(arcn->sb.st_mtime > pt->high_time))) { |
| 551 |
pt = pt->fow; |
| 552 |
continue; |
| 553 |
} |
| 554 |
break; |
| 555 |
} |
| 556 |
break; |
| 557 |
} |
| 558 |
|
| 559 |
if (pt == NULL) |
| 560 |
return(1); |
| 561 |
return(0); |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
static int |
| 573 |
str_sec(const char *p, time_t *tval) |
| 574 |
{ |
| 575 |
struct tm *lt; |
| 576 |
const char *dot, *t; |
| 577 |
size_t len; |
| 578 |
int bigyear; |
| 579 |
int yearset; |
| 580 |
|
| 581 |
yearset = 0; |
| 582 |
len = strlen(p); |
| 583 |
|
| 584 |
for (t = p, dot = NULL; *t; ++t) { |
| 585 |
if (isdigit((unsigned char)*t)) |
| 586 |
continue; |
| 587 |
if (*t == '.' && dot == NULL) { |
| 588 |
dot = t; |
| 589 |
continue; |
| 590 |
} |
| 591 |
return(-1); |
| 592 |
} |
| 593 |
|
| 594 |
lt = localtime(tval); |
| 595 |
|
| 596 |
if (dot != NULL) { |
| 597 |
if (strlen(++dot) != 2) |
| 598 |
return(-1); |
| 599 |
lt->tm_sec = ATOI2(dot); |
| 600 |
if (lt->tm_sec > 61) |
| 601 |
return(-1); |
| 602 |
len -= 3; |
| 603 |
} else |
| 604 |
lt->tm_sec = 0; |
| 605 |
|
| 606 |
switch (len) { |
| 607 |
case 12: |
| 608 |
bigyear = ATOI2(p); |
| 609 |
lt->tm_year = (bigyear * 100LL) - 1900LL; |
| 610 |
yearset = 1; |
| 611 |
|
| 612 |
case 10: |
| 613 |
if (yearset) { |
| 614 |
lt->tm_year += ATOI2(p); |
| 615 |
} else { |
| 616 |
lt->tm_year = ATOI2(p); |
| 617 |
if (lt->tm_year < 69) |
| 618 |
lt->tm_year += (2000 - 1900); |
| 619 |
} |
| 620 |
|
| 621 |
case 8: |
| 622 |
lt->tm_mon = ATOI2(p); |
| 623 |
if ((lt->tm_mon > 12) || !lt->tm_mon) |
| 624 |
return(-1); |
| 625 |
--lt->tm_mon; |
| 626 |
|
| 627 |
case 6: |
| 628 |
lt->tm_mday = ATOI2(p); |
| 629 |
if ((lt->tm_mday > 31) || !lt->tm_mday) |
| 630 |
return(-1); |
| 631 |
|
| 632 |
case 4: |
| 633 |
lt->tm_hour = ATOI2(p); |
| 634 |
if (lt->tm_hour > 23) |
| 635 |
return(-1); |
| 636 |
|
| 637 |
case 2: |
| 638 |
lt->tm_min = ATOI2(p); |
| 639 |
if (lt->tm_min > 59) |
| 640 |
return(-1); |
| 641 |
break; |
| 642 |
default: |
| 643 |
return(-1); |
| 644 |
} |
| 645 |
|
| 646 |
|
| 647 |
if ((*tval = mktime(lt)) == -1) |
| 648 |
return(-1); |
| 649 |
return(0); |
| 650 |
} |