1 /* 2 * Copyright (c) 1983 Eric P. Allman 3 * Copyright (c) 1988, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * %sccs.include.redist.c% 7 */ 8 9 #ifndef lint 10 static char sccsid[] = "@(#)util.c 8.62 (Berkeley) 04/11/95"; 11 #endif /* not lint */ 12 13 # include "sendmail.h" 14 # include <sysexits.h> 15 /* 16 ** STRIPQUOTES -- Strip quotes & quote bits from a string. 17 ** 18 ** Runs through a string and strips off unquoted quote 19 ** characters and quote bits. This is done in place. 20 ** 21 ** Parameters: 22 ** s -- the string to strip. 23 ** 24 ** Returns: 25 ** none. 26 ** 27 ** Side Effects: 28 ** none. 29 ** 30 ** Called By: 31 ** deliver 32 */ 33 34 stripquotes(s) 35 char *s; 36 { 37 register char *p; 38 register char *q; 39 register char c; 40 41 if (s == NULL) 42 return; 43 44 p = q = s; 45 do 46 { 47 c = *p++; 48 if (c == '\\') 49 c = *p++; 50 else if (c == '"') 51 continue; 52 *q++ = c; 53 } while (c != '\0'); 54 } 55 /* 56 ** XALLOC -- Allocate memory and bitch wildly on failure. 57 ** 58 ** THIS IS A CLUDGE. This should be made to give a proper 59 ** error -- but after all, what can we do? 60 ** 61 ** Parameters: 62 ** sz -- size of area to allocate. 63 ** 64 ** Returns: 65 ** pointer to data region. 66 ** 67 ** Side Effects: 68 ** Memory is allocated. 69 */ 70 71 char * 72 xalloc(sz) 73 register int sz; 74 { 75 register char *p; 76 77 /* some systems can't handle size zero mallocs */ 78 if (sz <= 0) 79 sz = 1; 80 81 p = malloc((unsigned) sz); 82 if (p == NULL) 83 { 84 syserr("Out of memory!!"); 85 abort(); 86 /* exit(EX_UNAVAILABLE); */ 87 } 88 return (p); 89 } 90 /* 91 ** COPYPLIST -- copy list of pointers. 92 ** 93 ** This routine is the equivalent of newstr for lists of 94 ** pointers. 95 ** 96 ** Parameters: 97 ** list -- list of pointers to copy. 98 ** Must be NULL terminated. 99 ** copycont -- if TRUE, copy the contents of the vector 100 ** (which must be a string) also. 101 ** 102 ** Returns: 103 ** a copy of 'list'. 104 ** 105 ** Side Effects: 106 ** none. 107 */ 108 109 char ** 110 copyplist(list, copycont) 111 char **list; 112 bool copycont; 113 { 114 register char **vp; 115 register char **newvp; 116 117 for (vp = list; *vp != NULL; vp++) 118 continue; 119 120 vp++; 121 122 newvp = (char **) xalloc((int) (vp - list) * sizeof *vp); 123 bcopy((char *) list, (char *) newvp, (int) (vp - list) * sizeof *vp); 124 125 if (copycont) 126 { 127 for (vp = newvp; *vp != NULL; vp++) 128 *vp = newstr(*vp); 129 } 130 131 return (newvp); 132 } 133 /* 134 ** COPYQUEUE -- copy address queue. 135 ** 136 ** This routine is the equivalent of newstr for address queues 137 ** addresses marked with QDONTSEND aren't copied 138 ** 139 ** Parameters: 140 ** addr -- list of address structures to copy. 141 ** 142 ** Returns: 143 ** a copy of 'addr'. 144 ** 145 ** Side Effects: 146 ** none. 147 */ 148 149 ADDRESS * 150 copyqueue(addr) 151 ADDRESS *addr; 152 { 153 register ADDRESS *newaddr; 154 ADDRESS *ret; 155 register ADDRESS **tail = &ret; 156 157 while (addr != NULL) 158 { 159 if (!bitset(QDONTSEND, addr->q_flags)) 160 { 161 newaddr = (ADDRESS *) xalloc(sizeof(ADDRESS)); 162 STRUCTCOPY(*addr, *newaddr); 163 *tail = newaddr; 164 tail = &newaddr->q_next; 165 } 166 addr = addr->q_next; 167 } 168 *tail = NULL; 169 170 return ret; 171 } 172 /* 173 ** PRINTAV -- print argument vector. 174 ** 175 ** Parameters: 176 ** av -- argument vector. 177 ** 178 ** Returns: 179 ** none. 180 ** 181 ** Side Effects: 182 ** prints av. 183 */ 184 185 printav(av) 186 register char **av; 187 { 188 while (*av != NULL) 189 { 190 if (tTd(0, 44)) 191 printf("\n\t%08x=", *av); 192 else 193 (void) putchar(' '); 194 xputs(*av++); 195 } 196 (void) putchar('\n'); 197 } 198 /* 199 ** LOWER -- turn letter into lower case. 200 ** 201 ** Parameters: 202 ** c -- character to turn into lower case. 203 ** 204 ** Returns: 205 ** c, in lower case. 206 ** 207 ** Side Effects: 208 ** none. 209 */ 210 211 char 212 lower(c) 213 register char c; 214 { 215 return((isascii(c) && isupper(c)) ? tolower(c) : c); 216 } 217 /* 218 ** XPUTS -- put string doing control escapes. 219 ** 220 ** Parameters: 221 ** s -- string to put. 222 ** 223 ** Returns: 224 ** none. 225 ** 226 ** Side Effects: 227 ** output to stdout 228 */ 229 230 xputs(s) 231 register char *s; 232 { 233 register int c; 234 register struct metamac *mp; 235 extern struct metamac MetaMacros[]; 236 237 if (s == NULL) 238 { 239 printf("<null>"); 240 return; 241 } 242 while ((c = (*s++ & 0377)) != '\0') 243 { 244 if (!isascii(c)) 245 { 246 if (c == MATCHREPL) 247 { 248 putchar('$'); 249 continue; 250 } 251 if (c == MACROEXPAND) 252 { 253 putchar('$'); 254 if (bitset(0200, *s)) 255 printf("{%s}", macname(*s++ & 0377)); 256 continue; 257 } 258 for (mp = MetaMacros; mp->metaname != '\0'; mp++) 259 { 260 if ((mp->metaval & 0377) == c) 261 { 262 printf("$%c", mp->metaname); 263 break; 264 } 265 } 266 if (mp->metaname != '\0') 267 continue; 268 (void) putchar('\\'); 269 c &= 0177; 270 } 271 if (isprint(c)) 272 { 273 putchar(c); 274 continue; 275 } 276 277 /* wasn't a meta-macro -- find another way to print it */ 278 switch (c) 279 { 280 case '\n': 281 c = 'n'; 282 break; 283 284 case '\r': 285 c = 'r'; 286 break; 287 288 case '\t': 289 c = 't'; 290 break; 291 292 default: 293 (void) putchar('^'); 294 (void) putchar(c ^ 0100); 295 continue; 296 } 297 (void) putchar('\\'); 298 (void) putchar(c); 299 } 300 (void) fflush(stdout); 301 } 302 /* 303 ** MAKELOWER -- Translate a line into lower case 304 ** 305 ** Parameters: 306 ** p -- the string to translate. If NULL, return is 307 ** immediate. 308 ** 309 ** Returns: 310 ** none. 311 ** 312 ** Side Effects: 313 ** String pointed to by p is translated to lower case. 314 ** 315 ** Called By: 316 ** parse 317 */ 318 319 void 320 makelower(p) 321 register char *p; 322 { 323 register char c; 324 325 if (p == NULL) 326 return; 327 for (; (c = *p) != '\0'; p++) 328 if (isascii(c) && isupper(c)) 329 *p = tolower(c); 330 } 331 /* 332 ** BUILDFNAME -- build full name from gecos style entry. 333 ** 334 ** This routine interprets the strange entry that would appear 335 ** in the GECOS field of the password file. 336 ** 337 ** Parameters: 338 ** p -- name to build. 339 ** login -- the login name of this user (for &). 340 ** buf -- place to put the result. 341 ** 342 ** Returns: 343 ** none. 344 ** 345 ** Side Effects: 346 ** none. 347 */ 348 349 buildfname(gecos, login, buf) 350 register char *gecos; 351 char *login; 352 char *buf; 353 { 354 register char *p; 355 register char *bp = buf; 356 int l; 357 358 if (*gecos == '*') 359 gecos++; 360 361 /* find length of final string */ 362 l = 0; 363 for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++) 364 { 365 if (*p == '&') 366 l += strlen(login); 367 else 368 l++; 369 } 370 371 /* now fill in buf */ 372 for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++) 373 { 374 if (*p == '&') 375 { 376 (void) strcpy(bp, login); 377 *bp = toupper(*bp); 378 while (*bp != '\0') 379 bp++; 380 } 381 else 382 *bp++ = *p; 383 } 384 *bp = '\0'; 385 } 386 /* 387 ** SAFEFILE -- return true if a file exists and is safe for a user. 388 ** 389 ** Parameters: 390 ** fn -- filename to check. 391 ** uid -- user id to compare against. 392 ** gid -- group id to compare against. 393 ** uname -- user name to compare against (used for group 394 ** sets). 395 ** flags -- modifiers: 396 ** SFF_MUSTOWN -- "uid" must own this file. 397 ** SFF_NOSLINK -- file cannot be a symbolic link. 398 ** mode -- mode bits that must match. 399 ** st -- if set, points to a stat structure that will 400 ** get the stat info for the file. 401 ** 402 ** Returns: 403 ** 0 if fn exists, is owned by uid, and matches mode. 404 ** An errno otherwise. The actual errno is cleared. 405 ** 406 ** Side Effects: 407 ** none. 408 */ 409 410 #include <grp.h> 411 412 #ifndef S_IXOTH 413 # define S_IXOTH (S_IEXEC >> 6) 414 #endif 415 416 #ifndef S_IXGRP 417 # define S_IXGRP (S_IEXEC >> 3) 418 #endif 419 420 #ifndef S_IXUSR 421 # define S_IXUSR (S_IEXEC) 422 #endif 423 424 #define ST_MODE_NOFILE 0171147 /* unlikely to occur */ 425 426 int 427 safefile(fn, uid, gid, uname, flags, mode, st) 428 char *fn; 429 uid_t uid; 430 gid_t gid; 431 char *uname; 432 int flags; 433 int mode; 434 struct stat *st; 435 { 436 register char *p; 437 register struct group *gr = NULL; 438 struct stat stbuf; 439 440 if (tTd(54, 4)) 441 printf("safefile(%s, uid=%d, gid=%d, flags=%x, mode=%o):\n", 442 fn, uid, gid, flags, mode); 443 errno = 0; 444 if (st == NULL) 445 st = &stbuf; 446 447 if (!bitset(SFF_NOPATHCHECK, flags) || 448 (uid == 0 && !bitset(SFF_ROOTOK, flags))) 449 { 450 /* check the path to the file for acceptability */ 451 for (p = fn; (p = strchr(++p, '/')) != NULL; *p = '/') 452 { 453 *p = '\0'; 454 if (stat(fn, &stbuf) < 0) 455 break; 456 if (uid == 0 && bitset(S_IWGRP|S_IWOTH, stbuf.st_mode)) 457 message("051 WARNING: writable directory %s", 458 fn); 459 if (uid == 0 && !bitset(SFF_ROOTOK, flags)) 460 { 461 if (bitset(S_IXOTH, stbuf.st_mode)) 462 continue; 463 break; 464 } 465 if (stbuf.st_uid == uid && 466 bitset(S_IXUSR, stbuf.st_mode)) 467 continue; 468 if (stbuf.st_gid == gid && 469 bitset(S_IXGRP, stbuf.st_mode)) 470 continue; 471 #ifndef NO_GROUP_SET 472 if (uname != NULL && 473 ((gr != NULL && gr->gr_gid == stbuf.st_gid) || 474 (gr = getgrgid(stbuf.st_gid)) != NULL)) 475 { 476 register char **gp; 477 478 for (gp = gr->gr_mem; gp != NULL && *gp != NULL; gp++) 479 if (strcmp(*gp, uname) == 0) 480 break; 481 if (gp != NULL && *gp != NULL && 482 bitset(S_IXGRP, stbuf.st_mode)) 483 continue; 484 } 485 #endif 486 if (!bitset(S_IXOTH, stbuf.st_mode)) 487 break; 488 } 489 if (p != NULL) 490 { 491 int ret = errno; 492 493 if (ret == 0) 494 ret = EACCES; 495 if (tTd(54, 4)) 496 printf("\t[dir %s] %s\n", fn, errstring(ret)); 497 *p = '/'; 498 return ret; 499 } 500 } 501 502 #ifdef HASLSTAT 503 if ((bitset(SFF_NOSLINK, flags) ? lstat(fn, st) 504 : stat(fn, st)) < 0) 505 #else 506 if (stat(fn, st) < 0) 507 #endif 508 { 509 int ret = errno; 510 511 if (tTd(54, 4)) 512 printf("\t%s\n", errstring(ret)); 513 514 errno = 0; 515 if (!bitset(SFF_CREAT, flags)) 516 return ret; 517 518 /* check to see if legal to create the file */ 519 p = strrchr(fn, '/'); 520 if (p == NULL) 521 return ENOTDIR; 522 *p = '\0'; 523 if (stat(fn, &stbuf) >= 0) 524 { 525 int md = S_IWRITE|S_IEXEC; 526 if (stbuf.st_uid != uid) 527 md >>= 6; 528 if ((stbuf.st_mode & md) != md) 529 errno = EACCES; 530 } 531 ret = errno; 532 if (tTd(54, 4)) 533 printf("\t[final dir %s uid %d mode %o] %s\n", 534 fn, stbuf.st_uid, stbuf.st_mode, 535 errstring(ret)); 536 *p = '/'; 537 st->st_mode = ST_MODE_NOFILE; 538 return ret; 539 } 540 541 #ifdef S_ISLNK 542 if (bitset(SFF_NOSLINK, flags) && S_ISLNK(st->st_mode)) 543 { 544 if (tTd(54, 4)) 545 printf("\t[slink mode %o]\tEPERM\n", st->st_mode); 546 return EPERM; 547 } 548 #endif 549 if (bitset(SFF_REGONLY, flags) && !S_ISREG(st->st_mode)) 550 { 551 if (tTd(54, 4)) 552 printf("\t[non-reg mode %o]\tEPERM\n", st->st_mode); 553 return EPERM; 554 } 555 if (bitset(S_IWUSR|S_IWGRP|S_IWOTH, mode) && bitset(0111, st->st_mode)) 556 { 557 if (tTd(29, 5)) 558 printf("failed (mode %o: x bits)\n", st->st_mode); 559 return EPERM; 560 } 561 562 if (bitset(SFF_SETUIDOK, flags)) 563 { 564 if (bitset(S_ISUID, st->st_mode) && 565 (st->st_uid != 0 || bitset(SFF_ROOTOK, flags))) 566 { 567 uid = st->st_uid; 568 uname = NULL; 569 } 570 if (bitset(S_ISGID, st->st_mode) && 571 (st->st_gid != 0 || bitset(SFF_ROOTOK, flags))) 572 gid = st->st_gid; 573 } 574 575 if (uid == 0 && !bitset(SFF_ROOTOK, flags)) 576 mode >>= 6; 577 else if (st->st_uid != uid) 578 { 579 mode >>= 3; 580 if (st->st_gid == gid) 581 ; 582 #ifndef NO_GROUP_SET 583 else if (uname != NULL && 584 ((gr != NULL && gr->gr_gid == st->st_gid) || 585 (gr = getgrgid(st->st_gid)) != NULL)) 586 { 587 register char **gp; 588 589 for (gp = gr->gr_mem; *gp != NULL; gp++) 590 if (strcmp(*gp, uname) == 0) 591 break; 592 if (*gp == NULL) 593 mode >>= 3; 594 } 595 #endif 596 else 597 mode >>= 3; 598 } 599 if (tTd(54, 4)) 600 printf("\t[uid %d, stat %o, mode %o] ", 601 st->st_uid, st->st_mode, mode); 602 if ((st->st_uid == uid || st->st_uid == 0 || 603 !bitset(SFF_MUSTOWN, flags)) && 604 (st->st_mode & mode) == mode) 605 { 606 if (tTd(54, 4)) 607 printf("\tOK\n"); 608 return 0; 609 } 610 if (tTd(54, 4)) 611 printf("\tEACCES\n"); 612 return EACCES; 613 } 614 /* 615 ** SAFEFOPEN -- do a file open with extra checking 616 ** 617 ** Parameters: 618 ** fn -- the file name to open. 619 ** omode -- the open-style mode flags. 620 ** cmode -- the create-style mode flags. 621 ** sff -- safefile flags. 622 ** 623 ** Returns: 624 ** Same as fopen. 625 */ 626 627 #ifndef O_ACCMODE 628 # define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR) 629 #endif 630 631 FILE * 632 safefopen(fn, omode, cmode, sff) 633 char *fn; 634 int omode; 635 int cmode; 636 int sff; 637 { 638 int rval; 639 FILE *fp; 640 int smode; 641 struct stat stb, sta; 642 extern char RealUserName[]; 643 644 if (bitset(O_CREAT, omode)) 645 sff |= SFF_CREAT; 646 smode = 0; 647 switch (omode & O_ACCMODE) 648 { 649 case O_RDONLY: 650 smode = S_IREAD; 651 break; 652 653 case O_WRONLY: 654 smode = S_IWRITE; 655 break; 656 657 case O_RDWR: 658 smode = S_IREAD|S_IWRITE; 659 break; 660 661 default: 662 smode = 0; 663 break; 664 } 665 if (bitset(SFF_OPENASROOT, sff)) 666 rval = safefile(fn, 0, 0, NULL, sff, smode, &stb); 667 else 668 rval = safefile(fn, RealUid, RealGid, RealUserName, 669 sff, smode, &stb); 670 if (rval != 0) 671 { 672 errno = rval; 673 return NULL; 674 } 675 if (stb.st_mode == ST_MODE_NOFILE) 676 omode |= O_EXCL; 677 678 fp = dfopen(fn, omode, cmode); 679 if (fp == NULL) 680 return NULL; 681 if (bitset(O_EXCL, omode)) 682 return fp; 683 if (fstat(fileno(fp), &sta) < 0 || 684 sta.st_nlink != stb.st_nlink || 685 sta.st_dev != stb.st_dev || 686 sta.st_ino != stb.st_ino || 687 sta.st_uid != stb.st_uid || 688 sta.st_gid != stb.st_gid) 689 { 690 syserr("554 cannot open: file %s changed after open", fn); 691 fclose(fp); 692 errno = EPERM; 693 return NULL; 694 } 695 return fp; 696 } 697 /* 698 ** FIXCRLF -- fix <CR><LF> in line. 699 ** 700 ** Looks for the <CR><LF> combination and turns it into the 701 ** UNIX canonical <NL> character. It only takes one line, 702 ** i.e., it is assumed that the first <NL> found is the end 703 ** of the line. 704 ** 705 ** Parameters: 706 ** line -- the line to fix. 707 ** stripnl -- if true, strip the newline also. 708 ** 709 ** Returns: 710 ** none. 711 ** 712 ** Side Effects: 713 ** line is changed in place. 714 */ 715 716 fixcrlf(line, stripnl) 717 char *line; 718 bool stripnl; 719 { 720 register char *p; 721 722 p = strchr(line, '\n'); 723 if (p == NULL) 724 return; 725 if (p > line && p[-1] == '\r') 726 p--; 727 if (!stripnl) 728 *p++ = '\n'; 729 *p = '\0'; 730 } 731 /* 732 ** DFOPEN -- determined file open 733 ** 734 ** This routine has the semantics of fopen, except that it will 735 ** keep trying a few times to make this happen. The idea is that 736 ** on very loaded systems, we may run out of resources (inodes, 737 ** whatever), so this tries to get around it. 738 */ 739 740 struct omodes 741 { 742 int mask; 743 int mode; 744 char *farg; 745 } OpenModes[] = 746 { 747 O_ACCMODE, O_RDONLY, "r", 748 O_ACCMODE|O_APPEND, O_WRONLY, "w", 749 O_ACCMODE|O_APPEND, O_WRONLY|O_APPEND, "a", 750 O_TRUNC, 0, "w+", 751 O_APPEND, O_APPEND, "a+", 752 0, 0, "r+", 753 }; 754 755 FILE * 756 dfopen(filename, omode, cmode) 757 char *filename; 758 int omode; 759 int cmode; 760 { 761 register int tries; 762 int fd; 763 register struct omodes *om; 764 struct stat st; 765 766 for (om = OpenModes; om->mask != 0; om++) 767 if ((omode & om->mask) == om->mode) 768 break; 769 770 for (tries = 0; tries < 10; tries++) 771 { 772 sleep((unsigned) (10 * tries)); 773 errno = 0; 774 fd = open(filename, omode, cmode); 775 if (fd >= 0) 776 break; 777 switch (errno) 778 { 779 case ENFILE: /* system file table full */ 780 case EINTR: /* interrupted syscall */ 781 #ifdef ETXTBSY 782 case ETXTBSY: /* Apollo: net file locked */ 783 #endif 784 continue; 785 } 786 break; 787 } 788 if (fd >= 0 && fstat(fd, &st) >= 0 && S_ISREG(st.st_mode)) 789 { 790 int locktype; 791 792 /* lock the file to avoid accidental conflicts */ 793 if ((omode & O_ACCMODE) != O_RDONLY) 794 locktype = LOCK_EX; 795 else 796 locktype = LOCK_SH; 797 (void) lockfile(fd, filename, NULL, locktype); 798 errno = 0; 799 } 800 if (fd < 0) 801 return NULL; 802 else 803 return fdopen(fd, om->farg); 804 } 805 /* 806 ** PUTLINE -- put a line like fputs obeying SMTP conventions 807 ** 808 ** This routine always guarantees outputing a newline (or CRLF, 809 ** as appropriate) at the end of the string. 810 ** 811 ** Parameters: 812 ** l -- line to put. 813 ** mci -- the mailer connection information. 814 ** 815 ** Returns: 816 ** none 817 ** 818 ** Side Effects: 819 ** output of l to fp. 820 */ 821 822 putline(l, mci) 823 register char *l; 824 register MCI *mci; 825 { 826 extern void putxline(); 827 828 putxline(l, mci, TRUE); 829 } 830 831 void 832 putxline(l, mci, mapfrom) 833 register char *l; 834 register MCI *mci; 835 bool mapfrom; 836 { 837 register char *p; 838 register char svchar; 839 int slop = 0; 840 841 /* strip out 0200 bits -- these can look like TELNET protocol */ 842 if (bitset(MCIF_7BIT, mci->mci_flags)) 843 { 844 for (p = l; (svchar = *p) != '\0'; ++p) 845 if (bitset(0200, svchar)) 846 *p = svchar &~ 0200; 847 } 848 849 do 850 { 851 /* find the end of the line */ 852 p = strchr(l, '\n'); 853 if (p == NULL) 854 p = &l[strlen(l)]; 855 856 if (TrafficLogFile != NULL) 857 fprintf(TrafficLogFile, "%05d >>> ", getpid()); 858 859 /* check for line overflow */ 860 while (mci->mci_mailer->m_linelimit > 0 && 861 (p - l + slop) > mci->mci_mailer->m_linelimit) 862 { 863 register char *q = &l[mci->mci_mailer->m_linelimit - slop - 1]; 864 865 svchar = *q; 866 *q = '\0'; 867 if (l[0] == '.' && slop == 0 && 868 bitnset(M_XDOT, mci->mci_mailer->m_flags)) 869 { 870 (void) putc('.', mci->mci_out); 871 if (TrafficLogFile != NULL) 872 (void) putc('.', TrafficLogFile); 873 } 874 else if (l[0] == 'F' && slop == 0 && mapfrom && 875 strncmp(l, "From ", 5) == 0 & 876 bitnset(M_ESCFROM, mci->mci_mailer->m_flags)) 877 { 878 (void) putc('>', mci->mci_out); 879 if (TrafficLogFile != NULL) 880 (void) putc('>', TrafficLogFile); 881 } 882 fputs(l, mci->mci_out); 883 (void) putc('!', mci->mci_out); 884 fputs(mci->mci_mailer->m_eol, mci->mci_out); 885 (void) putc(' ', mci->mci_out); 886 if (TrafficLogFile != NULL) 887 fprintf(TrafficLogFile, "%s!\n%05d >>> ", 888 l, getpid()); 889 *q = svchar; 890 l = q; 891 slop = 1; 892 } 893 894 /* output last part */ 895 if (l[0] == '.' && slop == 0 && 896 bitnset(M_XDOT, mci->mci_mailer->m_flags)) 897 { 898 (void) putc('.', mci->mci_out); 899 if (TrafficLogFile != NULL) 900 (void) putc('.', TrafficLogFile); 901 } 902 if (TrafficLogFile != NULL) 903 fprintf(TrafficLogFile, "%.*s\n", p - l, l); 904 for ( ; l < p; ++l) 905 (void) putc(*l, mci->mci_out); 906 fputs(mci->mci_mailer->m_eol, mci->mci_out); 907 if (*l == '\n') 908 ++l; 909 } while (l[0] != '\0'); 910 } 911 /* 912 ** XUNLINK -- unlink a file, doing logging as appropriate. 913 ** 914 ** Parameters: 915 ** f -- name of file to unlink. 916 ** 917 ** Returns: 918 ** none. 919 ** 920 ** Side Effects: 921 ** f is unlinked. 922 */ 923 924 xunlink(f) 925 char *f; 926 { 927 register int i; 928 929 # ifdef LOG 930 if (LogLevel > 98) 931 syslog(LOG_DEBUG, "%s: unlink %s", CurEnv->e_id, f); 932 # endif /* LOG */ 933 934 i = unlink(f); 935 # ifdef LOG 936 if (i < 0 && LogLevel > 97) 937 syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno); 938 # endif /* LOG */ 939 } 940 /* 941 ** XFCLOSE -- close a file, doing logging as appropriate. 942 ** 943 ** Parameters: 944 ** fp -- file pointer for the file to close 945 ** a, b -- miscellaneous crud to print for debugging 946 ** 947 ** Returns: 948 ** none. 949 ** 950 ** Side Effects: 951 ** fp is closed. 952 */ 953 954 xfclose(fp, a, b) 955 FILE *fp; 956 char *a, *b; 957 { 958 if (tTd(53, 99)) 959 printf("xfclose(%x) %s %s\n", fp, a, b); 960 #ifdef XDEBUG 961 if (fileno(fp) == 1) 962 syserr("xfclose(%s %s): fd = 1", a, b); 963 #endif 964 if (fclose(fp) < 0 && tTd(53, 99)) 965 printf("xfclose FAILURE: %s\n", errstring(errno)); 966 } 967 /* 968 ** SFGETS -- "safe" fgets -- times out and ignores random interrupts. 969 ** 970 ** Parameters: 971 ** buf -- place to put the input line. 972 ** siz -- size of buf. 973 ** fp -- file to read from. 974 ** timeout -- the timeout before error occurs. 975 ** during -- what we are trying to read (for error messages). 976 ** 977 ** Returns: 978 ** NULL on error (including timeout). This will also leave 979 ** buf containing a null string. 980 ** buf otherwise. 981 ** 982 ** Side Effects: 983 ** none. 984 */ 985 986 static jmp_buf CtxReadTimeout; 987 static void readtimeout(); 988 989 char * 990 sfgets(buf, siz, fp, timeout, during) 991 char *buf; 992 int siz; 993 FILE *fp; 994 time_t timeout; 995 char *during; 996 { 997 register EVENT *ev = NULL; 998 register char *p; 999 1000 if (fp == NULL) 1001 { 1002 buf[0] = '\0'; 1003 return NULL; 1004 } 1005 1006 /* set the timeout */ 1007 if (timeout != 0) 1008 { 1009 if (setjmp(CtxReadTimeout) != 0) 1010 { 1011 # ifdef LOG 1012 syslog(LOG_NOTICE, 1013 "timeout waiting for input from %s during %s\n", 1014 CurHostName? CurHostName: "local", during); 1015 # endif 1016 errno = 0; 1017 usrerr("451 timeout waiting for input during %s", 1018 during); 1019 buf[0] = '\0'; 1020 #ifdef XDEBUG 1021 checkfd012(during); 1022 #endif 1023 return (NULL); 1024 } 1025 ev = setevent(timeout, readtimeout, 0); 1026 } 1027 1028 /* try to read */ 1029 p = NULL; 1030 while (!feof(fp) && !ferror(fp)) 1031 { 1032 errno = 0; 1033 p = fgets(buf, siz, fp); 1034 if (p != NULL || errno != EINTR) 1035 break; 1036 clearerr(fp); 1037 } 1038 1039 /* clear the event if it has not sprung */ 1040 clrevent(ev); 1041 1042 /* clean up the books and exit */ 1043 LineNumber++; 1044 if (p == NULL) 1045 { 1046 buf[0] = '\0'; 1047 if (TrafficLogFile != NULL) 1048 fprintf(TrafficLogFile, "%05d <<< [EOF]\n", getpid()); 1049 return (NULL); 1050 } 1051 if (TrafficLogFile != NULL) 1052 fprintf(TrafficLogFile, "%05d <<< %s", getpid(), buf); 1053 if (SevenBitInput) 1054 { 1055 for (p = buf; *p != '\0'; p++) 1056 *p &= ~0200; 1057 } 1058 else if (!HasEightBits) 1059 { 1060 for (p = buf; *p != '\0'; p++) 1061 { 1062 if (bitset(0200, *p)) 1063 { 1064 HasEightBits = TRUE; 1065 break; 1066 } 1067 } 1068 } 1069 return (buf); 1070 } 1071 1072 static void 1073 readtimeout(timeout) 1074 time_t timeout; 1075 { 1076 longjmp(CtxReadTimeout, 1); 1077 } 1078 /* 1079 ** FGETFOLDED -- like fgets, but know about folded lines. 1080 ** 1081 ** Parameters: 1082 ** buf -- place to put result. 1083 ** n -- bytes available. 1084 ** f -- file to read from. 1085 ** 1086 ** Returns: 1087 ** input line(s) on success, NULL on error or EOF. 1088 ** This will normally be buf -- unless the line is too 1089 ** long, when it will be xalloc()ed. 1090 ** 1091 ** Side Effects: 1092 ** buf gets lines from f, with continuation lines (lines 1093 ** with leading white space) appended. CRLF's are mapped 1094 ** into single newlines. Any trailing NL is stripped. 1095 */ 1096 1097 char * 1098 fgetfolded(buf, n, f) 1099 char *buf; 1100 register int n; 1101 FILE *f; 1102 { 1103 register char *p = buf; 1104 char *bp = buf; 1105 register int i; 1106 1107 n--; 1108 while ((i = getc(f)) != EOF) 1109 { 1110 if (i == '\r') 1111 { 1112 i = getc(f); 1113 if (i != '\n') 1114 { 1115 if (i != EOF) 1116 (void) ungetc(i, f); 1117 i = '\r'; 1118 } 1119 } 1120 if (--n <= 0) 1121 { 1122 /* allocate new space */ 1123 char *nbp; 1124 int nn; 1125 1126 nn = (p - bp); 1127 if (nn < MEMCHUNKSIZE) 1128 nn *= 2; 1129 else 1130 nn += MEMCHUNKSIZE; 1131 nbp = xalloc(nn); 1132 bcopy(bp, nbp, p - bp); 1133 p = &nbp[p - bp]; 1134 if (bp != buf) 1135 free(bp); 1136 bp = nbp; 1137 n = nn - (p - bp); 1138 } 1139 *p++ = i; 1140 if (i == '\n') 1141 { 1142 LineNumber++; 1143 i = getc(f); 1144 if (i != EOF) 1145 (void) ungetc(i, f); 1146 if (i != ' ' && i != '\t') 1147 break; 1148 } 1149 } 1150 if (p == bp) 1151 return (NULL); 1152 *--p = '\0'; 1153 return (bp); 1154 } 1155 /* 1156 ** CURTIME -- return current time. 1157 ** 1158 ** Parameters: 1159 ** none. 1160 ** 1161 ** Returns: 1162 ** the current time. 1163 ** 1164 ** Side Effects: 1165 ** none. 1166 */ 1167 1168 time_t 1169 curtime() 1170 { 1171 auto time_t t; 1172 1173 (void) time(&t); 1174 return (t); 1175 } 1176 /* 1177 ** ATOBOOL -- convert a string representation to boolean. 1178 ** 1179 ** Defaults to "TRUE" 1180 ** 1181 ** Parameters: 1182 ** s -- string to convert. Takes "tTyY" as true, 1183 ** others as false. 1184 ** 1185 ** Returns: 1186 ** A boolean representation of the string. 1187 ** 1188 ** Side Effects: 1189 ** none. 1190 */ 1191 1192 bool 1193 atobool(s) 1194 register char *s; 1195 { 1196 if (s == NULL || *s == '\0' || strchr("tTyY", *s) != NULL) 1197 return (TRUE); 1198 return (FALSE); 1199 } 1200 /* 1201 ** ATOOCT -- convert a string representation to octal. 1202 ** 1203 ** Parameters: 1204 ** s -- string to convert. 1205 ** 1206 ** Returns: 1207 ** An integer representing the string interpreted as an 1208 ** octal number. 1209 ** 1210 ** Side Effects: 1211 ** none. 1212 */ 1213 1214 atooct(s) 1215 register char *s; 1216 { 1217 register int i = 0; 1218 1219 while (*s >= '0' && *s <= '7') 1220 i = (i << 3) | (*s++ - '0'); 1221 return (i); 1222 } 1223 /* 1224 ** WAITFOR -- wait for a particular process id. 1225 ** 1226 ** Parameters: 1227 ** pid -- process id to wait for. 1228 ** 1229 ** Returns: 1230 ** status of pid. 1231 ** -1 if pid never shows up. 1232 ** 1233 ** Side Effects: 1234 ** none. 1235 */ 1236 1237 int 1238 waitfor(pid) 1239 int pid; 1240 { 1241 #ifdef WAITUNION 1242 union wait st; 1243 #else 1244 auto int st; 1245 #endif 1246 int i; 1247 1248 do 1249 { 1250 errno = 0; 1251 i = wait(&st); 1252 } while ((i >= 0 || errno == EINTR) && i != pid); 1253 if (i < 0) 1254 return -1; 1255 #ifdef WAITUNION 1256 return st.w_status; 1257 #else 1258 return st; 1259 #endif 1260 } 1261 /* 1262 ** BITINTERSECT -- tell if two bitmaps intersect 1263 ** 1264 ** Parameters: 1265 ** a, b -- the bitmaps in question 1266 ** 1267 ** Returns: 1268 ** TRUE if they have a non-null intersection 1269 ** FALSE otherwise 1270 ** 1271 ** Side Effects: 1272 ** none. 1273 */ 1274 1275 bool 1276 bitintersect(a, b) 1277 BITMAP a; 1278 BITMAP b; 1279 { 1280 int i; 1281 1282 for (i = BITMAPBYTES / sizeof (int); --i >= 0; ) 1283 if ((a[i] & b[i]) != 0) 1284 return (TRUE); 1285 return (FALSE); 1286 } 1287 /* 1288 ** BITZEROP -- tell if a bitmap is all zero 1289 ** 1290 ** Parameters: 1291 ** map -- the bit map to check 1292 ** 1293 ** Returns: 1294 ** TRUE if map is all zero. 1295 ** FALSE if there are any bits set in map. 1296 ** 1297 ** Side Effects: 1298 ** none. 1299 */ 1300 1301 bool 1302 bitzerop(map) 1303 BITMAP map; 1304 { 1305 int i; 1306 1307 for (i = BITMAPBYTES / sizeof (int); --i >= 0; ) 1308 if (map[i] != 0) 1309 return (FALSE); 1310 return (TRUE); 1311 } 1312 /* 1313 ** STRCONTAINEDIN -- tell if one string is contained in another 1314 ** 1315 ** Parameters: 1316 ** a -- possible substring. 1317 ** b -- possible superstring. 1318 ** 1319 ** Returns: 1320 ** TRUE if a is contained in b. 1321 ** FALSE otherwise. 1322 */ 1323 1324 bool 1325 strcontainedin(a, b) 1326 register char *a; 1327 register char *b; 1328 { 1329 int la; 1330 int lb; 1331 int c; 1332 1333 la = strlen(a); 1334 lb = strlen(b); 1335 c = *a; 1336 if (isascii(c) && isupper(c)) 1337 c = tolower(c); 1338 for (; lb-- >= la; b++) 1339 { 1340 if (*b != c && isascii(*b) && isupper(*b) && tolower(*b) != c) 1341 continue; 1342 if (strncasecmp(a, b, la) == 0) 1343 return TRUE; 1344 } 1345 return FALSE; 1346 } 1347 /* 1348 ** CHECKFD012 -- check low numbered file descriptors 1349 ** 1350 ** File descriptors 0, 1, and 2 should be open at all times. 1351 ** This routine verifies that, and fixes it if not true. 1352 ** 1353 ** Parameters: 1354 ** where -- a tag printed if the assertion failed 1355 ** 1356 ** Returns: 1357 ** none 1358 */ 1359 1360 checkfd012(where) 1361 char *where; 1362 { 1363 #ifdef XDEBUG 1364 register int i; 1365 struct stat stbuf; 1366 1367 for (i = 0; i < 3; i++) 1368 { 1369 if (fstat(i, &stbuf) < 0 && errno != EOPNOTSUPP) 1370 { 1371 /* oops.... */ 1372 int fd; 1373 1374 syserr("%s: fd %d not open", where, i); 1375 fd = open("/dev/null", i == 0 ? O_RDONLY : O_WRONLY, 0666); 1376 if (fd != i) 1377 { 1378 (void) dup2(fd, i); 1379 (void) close(fd); 1380 } 1381 } 1382 } 1383 #endif /* XDEBUG */ 1384 } 1385 /* 1386 ** PRINTOPENFDS -- print the open file descriptors (for debugging) 1387 ** 1388 ** Parameters: 1389 ** logit -- if set, send output to syslog; otherwise 1390 ** print for debugging. 1391 ** 1392 ** Returns: 1393 ** none. 1394 */ 1395 1396 #include <arpa/inet.h> 1397 1398 printopenfds(logit) 1399 bool logit; 1400 { 1401 register int fd; 1402 extern int DtableSize; 1403 1404 for (fd = 0; fd < DtableSize; fd++) 1405 dumpfd(fd, FALSE, logit); 1406 } 1407 /* 1408 ** DUMPFD -- dump a file descriptor 1409 ** 1410 ** Parameters: 1411 ** fd -- the file descriptor to dump. 1412 ** printclosed -- if set, print a notification even if 1413 ** it is closed; otherwise print nothing. 1414 ** logit -- if set, send output to syslog instead of stdout. 1415 */ 1416 1417 dumpfd(fd, printclosed, logit) 1418 int fd; 1419 bool printclosed; 1420 bool logit; 1421 { 1422 register char *p; 1423 char *hp; 1424 char *fmtstr; 1425 SOCKADDR sa; 1426 auto int slen; 1427 struct stat st; 1428 char buf[200]; 1429 extern char *hostnamebyanyaddr(); 1430 1431 p = buf; 1432 sprintf(p, "%3d: ", fd); 1433 p += strlen(p); 1434 1435 if (fstat(fd, &st) < 0) 1436 { 1437 if (printclosed || errno != EBADF) 1438 { 1439 sprintf(p, "CANNOT STAT (%s)", errstring(errno)); 1440 goto printit; 1441 } 1442 return; 1443 } 1444 1445 slen = fcntl(fd, F_GETFL, NULL); 1446 if (slen != -1) 1447 { 1448 sprintf(p, "fl=0x%x, ", slen); 1449 p += strlen(p); 1450 } 1451 1452 sprintf(p, "mode=%o: ", st.st_mode); 1453 p += strlen(p); 1454 switch (st.st_mode & S_IFMT) 1455 { 1456 #ifdef S_IFSOCK 1457 case S_IFSOCK: 1458 sprintf(p, "SOCK "); 1459 p += strlen(p); 1460 slen = sizeof sa; 1461 if (getsockname(fd, &sa.sa, &slen) < 0) 1462 sprintf(p, "(badsock)"); 1463 else 1464 { 1465 hp = hostnamebyanyaddr(&sa); 1466 if (sa.sa.sa_family == AF_INET) 1467 sprintf(p, "%s/%d", hp, ntohs(sa.sin.sin_port)); 1468 else 1469 sprintf(p, "%s", hp); 1470 } 1471 p += strlen(p); 1472 sprintf(p, "->"); 1473 p += strlen(p); 1474 slen = sizeof sa; 1475 if (getpeername(fd, &sa.sa, &slen) < 0) 1476 sprintf(p, "(badsock)"); 1477 else 1478 { 1479 hp = hostnamebyanyaddr(&sa); 1480 if (sa.sa.sa_family == AF_INET) 1481 sprintf(p, "%s/%d", hp, ntohs(sa.sin.sin_port)); 1482 else 1483 sprintf(p, "%s", hp); 1484 } 1485 break; 1486 #endif 1487 1488 case S_IFCHR: 1489 sprintf(p, "CHR: "); 1490 p += strlen(p); 1491 goto defprint; 1492 1493 case S_IFBLK: 1494 sprintf(p, "BLK: "); 1495 p += strlen(p); 1496 goto defprint; 1497 1498 #if defined(S_IFIFO) && (!defined(S_IFSOCK) || S_IFIFO != S_IFSOCK) 1499 case S_IFIFO: 1500 sprintf(p, "FIFO: "); 1501 p += strlen(p); 1502 goto defprint; 1503 #endif 1504 1505 #ifdef S_IFDIR 1506 case S_IFDIR: 1507 sprintf(p, "DIR: "); 1508 p += strlen(p); 1509 goto defprint; 1510 #endif 1511 1512 #ifdef S_IFLNK 1513 case S_IFLNK: 1514 sprintf(p, "LNK: "); 1515 p += strlen(p); 1516 goto defprint; 1517 #endif 1518 1519 default: 1520 defprint: 1521 if (sizeof st.st_size > sizeof (long)) 1522 fmtstr = "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%qd"; 1523 else 1524 fmtstr = "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%ld"; 1525 sprintf(p, fmtstr, 1526 major(st.st_dev), minor(st.st_dev), st.st_ino, 1527 st.st_nlink, st.st_uid, st.st_gid, st.st_size); 1528 break; 1529 } 1530 1531 printit: 1532 #ifdef LOG 1533 if (logit) 1534 syslog(LOG_DEBUG, "%s", buf); 1535 else 1536 #endif 1537 printf("%s\n", buf); 1538 } 1539 /* 1540 ** SHORTENSTRING -- return short version of a string 1541 ** 1542 ** If the string is already short, just return it. If it is too 1543 ** long, return the head and tail of the string. 1544 ** 1545 ** Parameters: 1546 ** s -- the string to shorten. 1547 ** m -- the max length of the string. 1548 ** 1549 ** Returns: 1550 ** Either s or a short version of s. 1551 */ 1552 1553 #ifndef MAXSHORTSTR 1554 # define MAXSHORTSTR 203 1555 #endif 1556 1557 char * 1558 shortenstring(s, m) 1559 register char *s; 1560 int m; 1561 { 1562 int l; 1563 static char buf[MAXSHORTSTR + 1]; 1564 1565 l = strlen(s); 1566 if (l < m) 1567 return s; 1568 if (m > MAXSHORTSTR) 1569 m = MAXSHORTSTR; 1570 else if (m < 10) 1571 { 1572 if (m < 5) 1573 { 1574 strncpy(buf, s, m); 1575 buf[m] = '\0'; 1576 return buf; 1577 } 1578 strncpy(buf, s, m - 3); 1579 strcpy(buf + m - 3, "..."); 1580 return buf; 1581 } 1582 m = (m - 3) / 2; 1583 strncpy(buf, s, m); 1584 strcpy(buf + m, "..."); 1585 strcpy(buf + m + 3, s + l - m); 1586 return buf; 1587 } 1588 /* 1589 ** GET_COLUMN -- look up a Column in a line buffer 1590 ** 1591 ** Parameters: 1592 ** line -- the raw text line to search. 1593 ** col -- the column number to fetch. 1594 ** delim -- the delimiter between columns. If null, 1595 ** use white space. 1596 ** buf -- the output buffer. 1597 ** 1598 ** Returns: 1599 ** buf if successful. 1600 ** NULL otherwise. 1601 */ 1602 1603 char * 1604 get_column(line, col, delim, buf) 1605 char line[]; 1606 int col; 1607 char delim; 1608 char buf[]; 1609 { 1610 char *p; 1611 char *begin, *end; 1612 int i; 1613 char delimbuf[3]; 1614 1615 if (delim == '\0') 1616 strcpy(delimbuf, "\t "); 1617 else 1618 { 1619 delimbuf[0] = delim; 1620 delimbuf[1] = '\0'; 1621 } 1622 1623 p = line; 1624 if (*p == '\0') 1625 return NULL; /* line empty */ 1626 if (*p == delim && col == 0) 1627 return NULL; /* first column empty */ 1628 1629 begin = line; 1630 1631 if (col == 0 && delim == '\0') 1632 { 1633 while (*begin && isspace(*begin)) 1634 begin++; 1635 } 1636 1637 for (i = 0; i < col; i++) 1638 { 1639 if ((begin = strpbrk(begin, delimbuf)) == NULL) 1640 return NULL; /* no such column */ 1641 begin++; 1642 if (delim == '\0') 1643 { 1644 while (*begin && isspace(*begin)) 1645 begin++; 1646 } 1647 } 1648 1649 end = strpbrk(begin, delimbuf); 1650 if (end == NULL) 1651 { 1652 strcpy(buf, begin); 1653 } 1654 else 1655 { 1656 strncpy(buf, begin, end - begin); 1657 buf[end - begin] = '\0'; 1658 } 1659 return buf; 1660 } 1661 /* 1662 ** CLEANSTRCPY -- copy string keeping out bogus characters 1663 ** 1664 ** Parameters: 1665 ** t -- "to" string. 1666 ** f -- "from" string. 1667 ** l -- length of space available in "to" string. 1668 ** 1669 ** Returns: 1670 ** none. 1671 */ 1672 1673 void 1674 cleanstrcpy(t, f, l) 1675 register char *t; 1676 register char *f; 1677 int l; 1678 { 1679 #ifdef LOG 1680 /* check for newlines and log if necessary */ 1681 (void) denlstring(f, TRUE, TRUE); 1682 #endif 1683 1684 l--; 1685 while (l > 0 && *f != '\0') 1686 { 1687 if (isascii(*f) && 1688 (isalnum(*f) || strchr("!#$%&'*+-./^_`{|}~", *f) != NULL)) 1689 { 1690 l--; 1691 *t++ = *f; 1692 } 1693 f++; 1694 } 1695 *t = '\0'; 1696 } 1697 /* 1698 ** DENLSTRING -- convert newlines in a string to spaces 1699 ** 1700 ** Parameters: 1701 ** s -- the input string 1702 ** strict -- if set, don't permit continuation lines. 1703 ** logattacks -- if set, log attempted attacks. 1704 ** 1705 ** Returns: 1706 ** A pointer to a version of the string with newlines 1707 ** mapped to spaces. This should be copied. 1708 */ 1709 1710 char * 1711 denlstring(s, strict, logattacks) 1712 char *s; 1713 bool strict; 1714 bool logattacks; 1715 { 1716 register char *p; 1717 int l; 1718 static char *bp = NULL; 1719 static int bl = 0; 1720 1721 p = s; 1722 while ((p = strchr(p, '\n')) != NULL) 1723 if (strict || (*++p != ' ' && *p != '\t')) 1724 break; 1725 if (p == NULL) 1726 return s; 1727 1728 l = strlen(s) + 1; 1729 if (bl < l) 1730 { 1731 /* allocate more space */ 1732 if (bp != NULL) 1733 free(bp); 1734 bp = xalloc(l); 1735 bl = l; 1736 } 1737 strcpy(bp, s); 1738 for (p = bp; (p = strchr(p, '\n')) != NULL; ) 1739 *p++ = ' '; 1740 1741 /* 1742 #ifdef LOG 1743 if (logattacks) 1744 { 1745 syslog(LOG_NOTICE, "POSSIBLE ATTACK from %s: newline in string \"%s\"", 1746 RealHostName == NULL ? "[UNKNOWN]" : RealHostName, 1747 shortenstring(bp, 80)); 1748 } 1749 #endif 1750 */ 1751 1752 return bp; 1753 } 1754