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.31 (Berkeley) 02/06/94"; 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 p = malloc((unsigned) sz); 78 if (p == NULL) 79 { 80 syserr("Out of memory!!"); 81 abort(); 82 /* exit(EX_UNAVAILABLE); */ 83 } 84 return (p); 85 } 86 /* 87 ** COPYPLIST -- copy list of pointers. 88 ** 89 ** This routine is the equivalent of newstr for lists of 90 ** pointers. 91 ** 92 ** Parameters: 93 ** list -- list of pointers to copy. 94 ** Must be NULL terminated. 95 ** copycont -- if TRUE, copy the contents of the vector 96 ** (which must be a string) also. 97 ** 98 ** Returns: 99 ** a copy of 'list'. 100 ** 101 ** Side Effects: 102 ** none. 103 */ 104 105 char ** 106 copyplist(list, copycont) 107 char **list; 108 bool copycont; 109 { 110 register char **vp; 111 register char **newvp; 112 113 for (vp = list; *vp != NULL; vp++) 114 continue; 115 116 vp++; 117 118 newvp = (char **) xalloc((int) (vp - list) * sizeof *vp); 119 bcopy((char *) list, (char *) newvp, (int) (vp - list) * sizeof *vp); 120 121 if (copycont) 122 { 123 for (vp = newvp; *vp != NULL; vp++) 124 *vp = newstr(*vp); 125 } 126 127 return (newvp); 128 } 129 /* 130 ** COPYQUEUE -- copy address queue. 131 ** 132 ** This routine is the equivalent of newstr for address queues 133 ** addresses marked with QDONTSEND aren't copied 134 ** 135 ** Parameters: 136 ** addr -- list of address structures to copy. 137 ** 138 ** Returns: 139 ** a copy of 'addr'. 140 ** 141 ** Side Effects: 142 ** none. 143 */ 144 145 ADDRESS * 146 copyqueue(addr) 147 ADDRESS *addr; 148 { 149 register ADDRESS *newaddr; 150 ADDRESS *ret; 151 register ADDRESS **tail = &ret; 152 153 while (addr != NULL) 154 { 155 if (!bitset(QDONTSEND, addr->q_flags)) 156 { 157 newaddr = (ADDRESS *) xalloc(sizeof(ADDRESS)); 158 STRUCTCOPY(*addr, *newaddr); 159 *tail = newaddr; 160 tail = &newaddr->q_next; 161 } 162 addr = addr->q_next; 163 } 164 *tail = NULL; 165 166 return ret; 167 } 168 /* 169 ** PRINTAV -- print argument vector. 170 ** 171 ** Parameters: 172 ** av -- argument vector. 173 ** 174 ** Returns: 175 ** none. 176 ** 177 ** Side Effects: 178 ** prints av. 179 */ 180 181 printav(av) 182 register char **av; 183 { 184 while (*av != NULL) 185 { 186 if (tTd(0, 44)) 187 printf("\n\t%08x=", *av); 188 else 189 (void) putchar(' '); 190 xputs(*av++); 191 } 192 (void) putchar('\n'); 193 } 194 /* 195 ** LOWER -- turn letter into lower case. 196 ** 197 ** Parameters: 198 ** c -- character to turn into lower case. 199 ** 200 ** Returns: 201 ** c, in lower case. 202 ** 203 ** Side Effects: 204 ** none. 205 */ 206 207 char 208 lower(c) 209 register char c; 210 { 211 return((isascii(c) && isupper(c)) ? tolower(c) : c); 212 } 213 /* 214 ** XPUTS -- put string doing control escapes. 215 ** 216 ** Parameters: 217 ** s -- string to put. 218 ** 219 ** Returns: 220 ** none. 221 ** 222 ** Side Effects: 223 ** output to stdout 224 */ 225 226 xputs(s) 227 register char *s; 228 { 229 register int c; 230 register struct metamac *mp; 231 extern struct metamac MetaMacros[]; 232 233 if (s == NULL) 234 { 235 printf("<null>"); 236 return; 237 } 238 while ((c = (*s++ & 0377)) != '\0') 239 { 240 if (!isascii(c)) 241 { 242 if (c == MATCHREPL || c == MACROEXPAND) 243 { 244 putchar('$'); 245 continue; 246 } 247 for (mp = MetaMacros; mp->metaname != '\0'; mp++) 248 { 249 if ((mp->metaval & 0377) == c) 250 { 251 printf("$%c", mp->metaname); 252 break; 253 } 254 } 255 if (mp->metaname != '\0') 256 continue; 257 (void) putchar('\\'); 258 c &= 0177; 259 } 260 if (isprint(c)) 261 { 262 putchar(c); 263 continue; 264 } 265 266 /* wasn't a meta-macro -- find another way to print it */ 267 switch (c) 268 { 269 case '\0': 270 continue; 271 272 case '\n': 273 c = 'n'; 274 break; 275 276 case '\r': 277 c = 'r'; 278 break; 279 280 case '\t': 281 c = 't'; 282 break; 283 284 default: 285 (void) putchar('^'); 286 (void) putchar(c ^ 0100); 287 continue; 288 } 289 } 290 (void) fflush(stdout); 291 } 292 /* 293 ** MAKELOWER -- Translate a line into lower case 294 ** 295 ** Parameters: 296 ** p -- the string to translate. If NULL, return is 297 ** immediate. 298 ** 299 ** Returns: 300 ** none. 301 ** 302 ** Side Effects: 303 ** String pointed to by p is translated to lower case. 304 ** 305 ** Called By: 306 ** parse 307 */ 308 309 makelower(p) 310 register char *p; 311 { 312 register char c; 313 314 if (p == NULL) 315 return; 316 for (; (c = *p) != '\0'; p++) 317 if (isascii(c) && isupper(c)) 318 *p = tolower(c); 319 } 320 /* 321 ** BUILDFNAME -- build full name from gecos style entry. 322 ** 323 ** This routine interprets the strange entry that would appear 324 ** in the GECOS field of the password file. 325 ** 326 ** Parameters: 327 ** p -- name to build. 328 ** login -- the login name of this user (for &). 329 ** buf -- place to put the result. 330 ** 331 ** Returns: 332 ** none. 333 ** 334 ** Side Effects: 335 ** none. 336 */ 337 338 buildfname(gecos, login, buf) 339 register char *gecos; 340 char *login; 341 char *buf; 342 { 343 register char *p; 344 register char *bp = buf; 345 int l; 346 347 if (*gecos == '*') 348 gecos++; 349 350 /* find length of final string */ 351 l = 0; 352 for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++) 353 { 354 if (*p == '&') 355 l += strlen(login); 356 else 357 l++; 358 } 359 360 /* now fill in buf */ 361 for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++) 362 { 363 if (*p == '&') 364 { 365 (void) strcpy(bp, login); 366 *bp = toupper(*bp); 367 while (*bp != '\0') 368 bp++; 369 } 370 else 371 *bp++ = *p; 372 } 373 *bp = '\0'; 374 } 375 /* 376 ** SAFEFILE -- return true if a file exists and is safe for a user. 377 ** 378 ** Parameters: 379 ** fn -- filename to check. 380 ** uid -- user id to compare against. 381 ** gid -- group id to compare against. 382 ** uname -- user name to compare against (used for group 383 ** sets). 384 ** flags -- modifiers: 385 ** SFF_MUSTOWN -- "uid" must own this file. 386 ** SFF_NOSLINK -- file cannot be a symbolic link. 387 ** mode -- mode bits that must match. 388 ** 389 ** Returns: 390 ** 0 if fn exists, is owned by uid, and matches mode. 391 ** An errno otherwise. The actual errno is cleared. 392 ** 393 ** Side Effects: 394 ** none. 395 */ 396 397 #include <grp.h> 398 399 #ifndef S_IXOTH 400 # define S_IXOTH (S_IEXEC >> 6) 401 #endif 402 403 #ifndef S_IXGRP 404 # define S_IXGRP (S_IEXEC >> 3) 405 #endif 406 407 #ifndef S_IXUSR 408 # define S_IXUSR (S_IEXEC) 409 #endif 410 411 int 412 safefile(fn, uid, gid, uname, flags, mode) 413 char *fn; 414 uid_t uid; 415 gid_t gid; 416 char *uname; 417 int flags; 418 int mode; 419 { 420 register char *p; 421 register struct group *gr = NULL; 422 struct stat stbuf; 423 424 if (tTd(54, 4)) 425 printf("safefile(%s, uid=%d, gid=%d, flags=%x, mode=%o):\n", 426 fn, uid, gid, flags, mode); 427 errno = 0; 428 429 for (p = fn; (p = strchr(++p, '/')) != NULL; *p = '/') 430 { 431 *p = '\0'; 432 if (stat(fn, &stbuf) < 0) 433 break; 434 if (uid == 0 && !bitset(SFF_ROOTOK, flags)) 435 { 436 if (bitset(S_IXOTH, stbuf.st_mode)) 437 continue; 438 break; 439 } 440 if (stbuf.st_uid == uid && bitset(S_IXUSR, stbuf.st_mode)) 441 continue; 442 if (stbuf.st_gid == gid && bitset(S_IXGRP, stbuf.st_mode)) 443 continue; 444 #ifndef NO_GROUP_SET 445 if (uname != NULL && 446 ((gr != NULL && gr->gr_gid == stbuf.st_gid) || 447 (gr = getgrgid(stbuf.st_gid)) != NULL)) 448 { 449 register char **gp; 450 451 for (gp = gr->gr_mem; *gp != NULL; gp++) 452 if (strcmp(*gp, uname) == 0) 453 break; 454 if (*gp != NULL && bitset(S_IXGRP, stbuf.st_mode)) 455 continue; 456 } 457 #endif 458 if (!bitset(S_IXOTH, stbuf.st_mode)) 459 break; 460 } 461 if (p != NULL) 462 { 463 int ret = errno; 464 465 if (ret == 0) 466 ret = EACCES; 467 if (tTd(54, 4)) 468 printf("\t[dir %s] %s\n", fn, errstring(ret)); 469 *p = '/'; 470 return ret; 471 } 472 473 #ifdef HASLSTAT 474 if ((bitset(SFF_NOSLINK, flags) ? lstat(fn, &stbuf) 475 : stat(fn, &stbuf)) < 0) 476 #else 477 if (stat(fn, &stbuf) < 0) 478 #endif 479 { 480 int ret = errno; 481 482 if (tTd(54, 4)) 483 printf("\t%s\n", errstring(ret)); 484 485 errno = 0; 486 return ret; 487 } 488 489 #ifdef S_ISLNK 490 if (bitset(SFF_NOSLINK, flags) && S_ISLNK(stbuf.st_mode)) 491 { 492 if (tTd(54, 4)) 493 printf("\t[slink mode %o]\tEPERM\n", stbuf.st_mode); 494 return EPERM; 495 } 496 #endif 497 498 if (uid == 0 && !bitset(SFF_ROOTOK, flags)) 499 mode >>= 6; 500 else if (stbuf.st_uid != uid) 501 { 502 mode >>= 3; 503 if (stbuf.st_gid == gid) 504 ; 505 #ifndef NO_GROUP_SET 506 else if (uname != NULL && 507 ((gr != NULL && gr->gr_gid == stbuf.st_gid) || 508 (gr = getgrgid(stbuf.st_gid)) != NULL)) 509 { 510 register char **gp; 511 512 for (gp = gr->gr_mem; *gp != NULL; gp++) 513 if (strcmp(*gp, uname) == 0) 514 break; 515 if (*gp == NULL) 516 mode >>= 3; 517 } 518 #endif 519 else 520 mode >>= 3; 521 } 522 if (tTd(54, 4)) 523 printf("\t[uid %d, stat %o, mode %o] ", 524 stbuf.st_uid, stbuf.st_mode, mode); 525 if ((stbuf.st_uid == uid || stbuf.st_uid == 0 || 526 !bitset(SFF_MUSTOWN, flags)) && 527 (stbuf.st_mode & mode) == mode) 528 { 529 if (tTd(54, 4)) 530 printf("\tOK\n"); 531 return 0; 532 } 533 if (tTd(54, 4)) 534 printf("\tEACCES\n"); 535 return EACCES; 536 } 537 /* 538 ** FIXCRLF -- fix <CR><LF> in line. 539 ** 540 ** Looks for the <CR><LF> combination and turns it into the 541 ** UNIX canonical <NL> character. It only takes one line, 542 ** i.e., it is assumed that the first <NL> found is the end 543 ** of the line. 544 ** 545 ** Parameters: 546 ** line -- the line to fix. 547 ** stripnl -- if true, strip the newline also. 548 ** 549 ** Returns: 550 ** none. 551 ** 552 ** Side Effects: 553 ** line is changed in place. 554 */ 555 556 fixcrlf(line, stripnl) 557 char *line; 558 bool stripnl; 559 { 560 register char *p; 561 562 p = strchr(line, '\n'); 563 if (p == NULL) 564 return; 565 if (p > line && p[-1] == '\r') 566 p--; 567 if (!stripnl) 568 *p++ = '\n'; 569 *p = '\0'; 570 } 571 /* 572 ** DFOPEN -- determined file open 573 ** 574 ** This routine has the semantics of fopen, except that it will 575 ** keep trying a few times to make this happen. The idea is that 576 ** on very loaded systems, we may run out of resources (inodes, 577 ** whatever), so this tries to get around it. 578 */ 579 580 #ifndef O_ACCMODE 581 # define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR) 582 #endif 583 584 struct omodes 585 { 586 int mask; 587 int mode; 588 char *farg; 589 } OpenModes[] = 590 { 591 O_ACCMODE, O_RDONLY, "r", 592 O_ACCMODE|O_APPEND, O_WRONLY, "w", 593 O_ACCMODE|O_APPEND, O_WRONLY|O_APPEND, "a", 594 O_TRUNC, 0, "w+", 595 O_APPEND, O_APPEND, "a+", 596 0, 0, "r+", 597 }; 598 599 FILE * 600 dfopen(filename, omode, cmode) 601 char *filename; 602 int omode; 603 int cmode; 604 { 605 register int tries; 606 int fd; 607 register struct omodes *om; 608 struct stat st; 609 610 for (om = OpenModes; om->mask != 0; om++) 611 if ((omode & om->mask) == om->mode) 612 break; 613 614 for (tries = 0; tries < 10; tries++) 615 { 616 sleep((unsigned) (10 * tries)); 617 errno = 0; 618 fd = open(filename, omode, cmode); 619 if (fd >= 0) 620 break; 621 switch (errno) 622 { 623 case ENFILE: /* system file table full */ 624 case EINTR: /* interrupted syscall */ 625 #ifdef ETXTBSY 626 case ETXTBSY: /* Apollo: net file locked */ 627 #endif 628 break; 629 630 default: 631 continue; 632 } 633 break; 634 } 635 if (fd >= 0 && fstat(fd, &st) >= 0 && S_ISREG(st.st_mode)) 636 { 637 int locktype; 638 639 /* lock the file to avoid accidental conflicts */ 640 if ((omode & O_ACCMODE) != O_RDONLY) 641 locktype = LOCK_EX; 642 else 643 locktype = LOCK_SH; 644 (void) lockfile(fd, filename, NULL, locktype); 645 errno = 0; 646 } 647 if (fd < 0) 648 return NULL; 649 else 650 return fdopen(fd, om->farg); 651 } 652 /* 653 ** PUTLINE -- put a line like fputs obeying SMTP conventions 654 ** 655 ** This routine always guarantees outputing a newline (or CRLF, 656 ** as appropriate) at the end of the string. 657 ** 658 ** Parameters: 659 ** l -- line to put. 660 ** mci -- the mailer connection information. 661 ** 662 ** Returns: 663 ** none 664 ** 665 ** Side Effects: 666 ** output of l to fp. 667 */ 668 669 putline(l, mci) 670 register char *l; 671 register MCI *mci; 672 { 673 register char *p; 674 register char svchar; 675 int slop = 0; 676 677 /* strip out 0200 bits -- these can look like TELNET protocol */ 678 if (bitset(MCIF_7BIT, mci->mci_flags)) 679 { 680 for (p = l; (svchar = *p) != '\0'; ++p) 681 if (bitset(0200, svchar)) 682 *p = svchar &~ 0200; 683 } 684 685 do 686 { 687 /* find the end of the line */ 688 p = strchr(l, '\n'); 689 if (p == NULL) 690 p = &l[strlen(l)]; 691 692 if (TrafficLogFile != NULL) 693 fprintf(TrafficLogFile, "%05d >>> ", getpid()); 694 695 /* check for line overflow */ 696 while (mci->mci_mailer->m_linelimit > 0 && 697 (p - l + slop) > mci->mci_mailer->m_linelimit) 698 { 699 register char *q = &l[mci->mci_mailer->m_linelimit - slop - 1]; 700 701 svchar = *q; 702 *q = '\0'; 703 if (l[0] == '.' && slop == 0 && 704 bitnset(M_XDOT, mci->mci_mailer->m_flags)) 705 { 706 (void) putc('.', mci->mci_out); 707 if (TrafficLogFile != NULL) 708 (void) putc('.', TrafficLogFile); 709 } 710 fputs(l, mci->mci_out); 711 (void) putc('!', mci->mci_out); 712 fputs(mci->mci_mailer->m_eol, mci->mci_out); 713 (void) putc(' ', mci->mci_out); 714 if (TrafficLogFile != NULL) 715 fprintf(TrafficLogFile, "%s!\n%05d >>> ", 716 l, getpid()); 717 *q = svchar; 718 l = q; 719 slop = 1; 720 } 721 722 /* output last part */ 723 if (l[0] == '.' && slop == 0 && 724 bitnset(M_XDOT, mci->mci_mailer->m_flags)) 725 { 726 (void) putc('.', mci->mci_out); 727 if (TrafficLogFile != NULL) 728 (void) putc('.', TrafficLogFile); 729 } 730 if (TrafficLogFile != NULL) 731 fprintf(TrafficLogFile, "%.*s\n", p - l, l); 732 for ( ; l < p; ++l) 733 (void) putc(*l, mci->mci_out); 734 fputs(mci->mci_mailer->m_eol, mci->mci_out); 735 if (*l == '\n') 736 ++l; 737 } while (l[0] != '\0'); 738 } 739 /* 740 ** XUNLINK -- unlink a file, doing logging as appropriate. 741 ** 742 ** Parameters: 743 ** f -- name of file to unlink. 744 ** 745 ** Returns: 746 ** none. 747 ** 748 ** Side Effects: 749 ** f is unlinked. 750 */ 751 752 xunlink(f) 753 char *f; 754 { 755 register int i; 756 757 # ifdef LOG 758 if (LogLevel > 98) 759 syslog(LOG_DEBUG, "%s: unlink %s", CurEnv->e_id, f); 760 # endif /* LOG */ 761 762 i = unlink(f); 763 # ifdef LOG 764 if (i < 0 && LogLevel > 97) 765 syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno); 766 # endif /* LOG */ 767 } 768 /* 769 ** XFCLOSE -- close a file, doing logging as appropriate. 770 ** 771 ** Parameters: 772 ** fp -- file pointer for the file to close 773 ** a, b -- miscellaneous crud to print for debugging 774 ** 775 ** Returns: 776 ** none. 777 ** 778 ** Side Effects: 779 ** fp is closed. 780 */ 781 782 xfclose(fp, a, b) 783 FILE *fp; 784 char *a, *b; 785 { 786 if (tTd(53, 99)) 787 printf("xfclose(%x) %s %s\n", fp, a, b); 788 #ifdef XDEBUG 789 if (fileno(fp) == 1) 790 syserr("xfclose(%s %s): fd = 1", a, b); 791 #endif 792 if (fclose(fp) < 0 && tTd(53, 99)) 793 printf("xfclose FAILURE: %s\n", errstring(errno)); 794 } 795 /* 796 ** SFGETS -- "safe" fgets -- times out and ignores random interrupts. 797 ** 798 ** Parameters: 799 ** buf -- place to put the input line. 800 ** siz -- size of buf. 801 ** fp -- file to read from. 802 ** timeout -- the timeout before error occurs. 803 ** during -- what we are trying to read (for error messages). 804 ** 805 ** Returns: 806 ** NULL on error (including timeout). This will also leave 807 ** buf containing a null string. 808 ** buf otherwise. 809 ** 810 ** Side Effects: 811 ** none. 812 */ 813 814 static jmp_buf CtxReadTimeout; 815 static int readtimeout(); 816 817 char * 818 sfgets(buf, siz, fp, timeout, during) 819 char *buf; 820 int siz; 821 FILE *fp; 822 time_t timeout; 823 char *during; 824 { 825 register EVENT *ev = NULL; 826 register char *p; 827 828 /* set the timeout */ 829 if (timeout != 0) 830 { 831 if (setjmp(CtxReadTimeout) != 0) 832 { 833 # ifdef LOG 834 syslog(LOG_NOTICE, 835 "timeout waiting for input from %s during %s\n", 836 CurHostName? CurHostName: "local", during); 837 # endif 838 errno = 0; 839 usrerr("451 timeout waiting for input during %s", 840 during); 841 buf[0] = '\0'; 842 #ifdef XDEBUG 843 checkfd012(during); 844 #endif 845 return (NULL); 846 } 847 ev = setevent(timeout, readtimeout, 0); 848 } 849 850 /* try to read */ 851 p = NULL; 852 while (!feof(fp) && !ferror(fp)) 853 { 854 errno = 0; 855 p = fgets(buf, siz, fp); 856 if (p != NULL || errno != EINTR) 857 break; 858 clearerr(fp); 859 } 860 861 /* clear the event if it has not sprung */ 862 clrevent(ev); 863 864 /* clean up the books and exit */ 865 LineNumber++; 866 if (p == NULL) 867 { 868 buf[0] = '\0'; 869 if (TrafficLogFile != NULL) 870 fprintf(TrafficLogFile, "%05d <<< [EOF]\n", getpid()); 871 return (NULL); 872 } 873 if (TrafficLogFile != NULL) 874 fprintf(TrafficLogFile, "%05d <<< %s", getpid(), buf); 875 if (SevenBit) 876 for (p = buf; *p != '\0'; p++) 877 *p &= ~0200; 878 return (buf); 879 } 880 881 static 882 readtimeout() 883 { 884 longjmp(CtxReadTimeout, 1); 885 } 886 /* 887 ** FGETFOLDED -- like fgets, but know about folded lines. 888 ** 889 ** Parameters: 890 ** buf -- place to put result. 891 ** n -- bytes available. 892 ** f -- file to read from. 893 ** 894 ** Returns: 895 ** input line(s) on success, NULL on error or EOF. 896 ** This will normally be buf -- unless the line is too 897 ** long, when it will be xalloc()ed. 898 ** 899 ** Side Effects: 900 ** buf gets lines from f, with continuation lines (lines 901 ** with leading white space) appended. CRLF's are mapped 902 ** into single newlines. Any trailing NL is stripped. 903 */ 904 905 char * 906 fgetfolded(buf, n, f) 907 char *buf; 908 register int n; 909 FILE *f; 910 { 911 register char *p = buf; 912 char *bp = buf; 913 register int i; 914 915 n--; 916 while ((i = getc(f)) != EOF) 917 { 918 if (i == '\r') 919 { 920 i = getc(f); 921 if (i != '\n') 922 { 923 if (i != EOF) 924 (void) ungetc(i, f); 925 i = '\r'; 926 } 927 } 928 if (--n <= 0) 929 { 930 /* allocate new space */ 931 char *nbp; 932 int nn; 933 934 nn = (p - bp); 935 if (nn < MEMCHUNKSIZE) 936 nn *= 2; 937 else 938 nn += MEMCHUNKSIZE; 939 nbp = xalloc(nn); 940 bcopy(bp, nbp, p - bp); 941 p = &nbp[p - bp]; 942 if (bp != buf) 943 free(bp); 944 bp = nbp; 945 n = nn - (p - bp); 946 } 947 *p++ = i; 948 if (i == '\n') 949 { 950 LineNumber++; 951 i = getc(f); 952 if (i != EOF) 953 (void) ungetc(i, f); 954 if (i != ' ' && i != '\t') 955 break; 956 } 957 } 958 if (p == bp) 959 return (NULL); 960 *--p = '\0'; 961 return (bp); 962 } 963 /* 964 ** CURTIME -- return current time. 965 ** 966 ** Parameters: 967 ** none. 968 ** 969 ** Returns: 970 ** the current time. 971 ** 972 ** Side Effects: 973 ** none. 974 */ 975 976 time_t 977 curtime() 978 { 979 auto time_t t; 980 981 (void) time(&t); 982 return (t); 983 } 984 /* 985 ** ATOBOOL -- convert a string representation to boolean. 986 ** 987 ** Defaults to "TRUE" 988 ** 989 ** Parameters: 990 ** s -- string to convert. Takes "tTyY" as true, 991 ** others as false. 992 ** 993 ** Returns: 994 ** A boolean representation of the string. 995 ** 996 ** Side Effects: 997 ** none. 998 */ 999 1000 bool 1001 atobool(s) 1002 register char *s; 1003 { 1004 if (s == NULL || *s == '\0' || strchr("tTyY", *s) != NULL) 1005 return (TRUE); 1006 return (FALSE); 1007 } 1008 /* 1009 ** ATOOCT -- convert a string representation to octal. 1010 ** 1011 ** Parameters: 1012 ** s -- string to convert. 1013 ** 1014 ** Returns: 1015 ** An integer representing the string interpreted as an 1016 ** octal number. 1017 ** 1018 ** Side Effects: 1019 ** none. 1020 */ 1021 1022 atooct(s) 1023 register char *s; 1024 { 1025 register int i = 0; 1026 1027 while (*s >= '0' && *s <= '7') 1028 i = (i << 3) | (*s++ - '0'); 1029 return (i); 1030 } 1031 /* 1032 ** WAITFOR -- wait for a particular process id. 1033 ** 1034 ** Parameters: 1035 ** pid -- process id to wait for. 1036 ** 1037 ** Returns: 1038 ** status of pid. 1039 ** -1 if pid never shows up. 1040 ** 1041 ** Side Effects: 1042 ** none. 1043 */ 1044 1045 int 1046 waitfor(pid) 1047 int pid; 1048 { 1049 #ifdef WAITUNION 1050 union wait st; 1051 #else 1052 auto int st; 1053 #endif 1054 int i; 1055 1056 do 1057 { 1058 errno = 0; 1059 i = wait(&st); 1060 } while ((i >= 0 || errno == EINTR) && i != pid); 1061 if (i < 0) 1062 return -1; 1063 #ifdef WAITUNION 1064 return st.w_status; 1065 #else 1066 return st; 1067 #endif 1068 } 1069 /* 1070 ** BITINTERSECT -- tell if two bitmaps intersect 1071 ** 1072 ** Parameters: 1073 ** a, b -- the bitmaps in question 1074 ** 1075 ** Returns: 1076 ** TRUE if they have a non-null intersection 1077 ** FALSE otherwise 1078 ** 1079 ** Side Effects: 1080 ** none. 1081 */ 1082 1083 bool 1084 bitintersect(a, b) 1085 BITMAP a; 1086 BITMAP b; 1087 { 1088 int i; 1089 1090 for (i = BITMAPBYTES / sizeof (int); --i >= 0; ) 1091 if ((a[i] & b[i]) != 0) 1092 return (TRUE); 1093 return (FALSE); 1094 } 1095 /* 1096 ** BITZEROP -- tell if a bitmap is all zero 1097 ** 1098 ** Parameters: 1099 ** map -- the bit map to check 1100 ** 1101 ** Returns: 1102 ** TRUE if map is all zero. 1103 ** FALSE if there are any bits set in map. 1104 ** 1105 ** Side Effects: 1106 ** none. 1107 */ 1108 1109 bool 1110 bitzerop(map) 1111 BITMAP map; 1112 { 1113 int i; 1114 1115 for (i = BITMAPBYTES / sizeof (int); --i >= 0; ) 1116 if (map[i] != 0) 1117 return (FALSE); 1118 return (TRUE); 1119 } 1120 /* 1121 ** STRCONTAINEDIN -- tell if one string is contained in another 1122 ** 1123 ** Parameters: 1124 ** a -- possible substring. 1125 ** b -- possible superstring. 1126 ** 1127 ** Returns: 1128 ** TRUE if a is contained in b. 1129 ** FALSE otherwise. 1130 */ 1131 1132 bool 1133 strcontainedin(a, b) 1134 register char *a; 1135 register char *b; 1136 { 1137 int la; 1138 int lb; 1139 int c; 1140 1141 la = strlen(a); 1142 lb = strlen(b); 1143 c = *a; 1144 if (isascii(c) && isupper(c)) 1145 c = tolower(c); 1146 for (; lb-- >= la; b++) 1147 { 1148 if (*b != c && isascii(*b) && isupper(*b) && tolower(*b) != c) 1149 continue; 1150 if (strncasecmp(a, b, la) == 0) 1151 return TRUE; 1152 } 1153 return FALSE; 1154 } 1155 /* 1156 ** CHECKFD012 -- check low numbered file descriptors 1157 ** 1158 ** File descriptors 0, 1, and 2 should be open at all times. 1159 ** This routine verifies that, and fixes it if not true. 1160 ** 1161 ** Parameters: 1162 ** where -- a tag printed if the assertion failed 1163 ** 1164 ** Returns: 1165 ** none 1166 */ 1167 1168 checkfd012(where) 1169 char *where; 1170 { 1171 #ifdef XDEBUG 1172 register int i; 1173 struct stat stbuf; 1174 1175 for (i = 0; i < 3; i++) 1176 { 1177 if (fstat(i, &stbuf) < 0 && errno != EOPNOTSUPP) 1178 { 1179 /* oops.... */ 1180 int fd; 1181 1182 syserr("%s: fd %d not open", where, i); 1183 fd = open("/dev/null", i == 0 ? O_RDONLY : O_WRONLY, 0666); 1184 if (fd != i) 1185 { 1186 (void) dup2(fd, i); 1187 (void) close(fd); 1188 } 1189 } 1190 } 1191 #endif /* XDEBUG */ 1192 } 1193 /* 1194 ** PRINTOPENFDS -- print the open file descriptors (for debugging) 1195 ** 1196 ** Parameters: 1197 ** logit -- if set, send output to syslog; otherwise 1198 ** print for debugging. 1199 ** 1200 ** Returns: 1201 ** none. 1202 */ 1203 1204 #include <netdb.h> 1205 #include <arpa/inet.h> 1206 1207 printopenfds(logit) 1208 bool logit; 1209 { 1210 register int fd; 1211 extern int DtableSize; 1212 1213 for (fd = 0; fd < DtableSize; fd++) 1214 dumpfd(fd, FALSE, logit); 1215 } 1216 /* 1217 ** DUMPFD -- dump a file descriptor 1218 ** 1219 ** Parameters: 1220 ** fd -- the file descriptor to dump. 1221 ** printclosed -- if set, print a notification even if 1222 ** it is closed; otherwise print nothing. 1223 ** logit -- if set, send output to syslog instead of stdout. 1224 */ 1225 1226 dumpfd(fd, printclosed, logit) 1227 int fd; 1228 bool printclosed; 1229 bool logit; 1230 { 1231 register struct hostent *hp; 1232 register char *p; 1233 struct sockaddr_in sin; 1234 auto int slen; 1235 struct stat st; 1236 char buf[200]; 1237 1238 p = buf; 1239 sprintf(p, "%3d: ", fd); 1240 p += strlen(p); 1241 1242 if (fstat(fd, &st) < 0) 1243 { 1244 if (printclosed || errno != EBADF) 1245 { 1246 sprintf(p, "CANNOT STAT (%s)", errstring(errno)); 1247 goto printit; 1248 } 1249 return; 1250 } 1251 1252 slen = fcntl(fd, F_GETFL, NULL); 1253 if (slen != -1) 1254 { 1255 sprintf(p, "fl=0x%x, ", slen); 1256 p += strlen(p); 1257 } 1258 1259 sprintf(p, "mode=%o: ", st.st_mode); 1260 p += strlen(p); 1261 switch (st.st_mode & S_IFMT) 1262 { 1263 #ifdef S_IFSOCK 1264 case S_IFSOCK: 1265 sprintf(p, "SOCK "); 1266 p += strlen(p); 1267 slen = sizeof sin; 1268 if (getsockname(fd, (struct sockaddr *) &sin, &slen) < 0) 1269 sprintf(p, "(badsock)"); 1270 else 1271 { 1272 hp = gethostbyaddr((char *) &sin.sin_addr, slen, AF_INET); 1273 sprintf(p, "%s/%d", hp == NULL ? inet_ntoa(sin.sin_addr) 1274 : hp->h_name, ntohs(sin.sin_port)); 1275 } 1276 p += strlen(p); 1277 sprintf(p, "->"); 1278 p += strlen(p); 1279 slen = sizeof sin; 1280 if (getpeername(fd, (struct sockaddr *) &sin, &slen) < 0) 1281 sprintf(p, "(badsock)"); 1282 else 1283 { 1284 hp = gethostbyaddr((char *) &sin.sin_addr, slen, AF_INET); 1285 sprintf(p, "%s/%d", hp == NULL ? inet_ntoa(sin.sin_addr) 1286 : hp->h_name, ntohs(sin.sin_port)); 1287 } 1288 break; 1289 #endif 1290 1291 case S_IFCHR: 1292 sprintf(p, "CHR: "); 1293 p += strlen(p); 1294 goto defprint; 1295 1296 case S_IFBLK: 1297 sprintf(p, "BLK: "); 1298 p += strlen(p); 1299 goto defprint; 1300 1301 #ifdef S_IFIFO 1302 case S_IFIFO: 1303 sprintf(p, "FIFO: "); 1304 p += strlen(p); 1305 goto defprint; 1306 #endif 1307 1308 #ifdef S_IFDIR 1309 case S_IFDIR: 1310 sprintf(p, "DIR: "); 1311 p += strlen(p); 1312 goto defprint; 1313 #endif 1314 1315 #ifdef S_IFLNK 1316 case S_IFLNK: 1317 sprintf(p, "LNK: "); 1318 p += strlen(p); 1319 goto defprint; 1320 #endif 1321 1322 default: 1323 defprint: 1324 sprintf(p, "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%ld", 1325 major(st.st_dev), minor(st.st_dev), st.st_ino, 1326 st.st_nlink, st.st_uid, st.st_gid, st.st_size); 1327 break; 1328 } 1329 1330 printit: 1331 if (logit) 1332 syslog(LOG_DEBUG, "%s", buf); 1333 else 1334 printf("%s\n", buf); 1335 } 1336 /* 1337 ** SHORTENSTRING -- return short version of a string 1338 ** 1339 ** If the string is already short, just return it. If it is too 1340 ** long, return the head and tail of the string. 1341 ** 1342 ** Parameters: 1343 ** s -- the string to shorten. 1344 ** m -- the max length of the string. 1345 ** 1346 ** Returns: 1347 ** Either s or a short version of s. 1348 */ 1349 1350 #ifndef MAXSHORTSTR 1351 # define MAXSHORTSTR 203 1352 #endif 1353 1354 char * 1355 shortenstring(s, m) 1356 register char *s; 1357 int m; 1358 { 1359 int l; 1360 static char buf[MAXSHORTSTR + 1]; 1361 1362 l = strlen(s); 1363 if (l < m) 1364 return s; 1365 if (m > MAXSHORTSTR) 1366 m = MAXSHORTSTR; 1367 else if (m < 10) 1368 { 1369 if (m < 5) 1370 { 1371 strncpy(buf, s, m); 1372 buf[m] = '\0'; 1373 return buf; 1374 } 1375 strncpy(buf, s, m - 3); 1376 strcpy(buf + m - 3, "..."); 1377 return buf; 1378 } 1379 m = (m - 3) / 2; 1380 strncpy(buf, s, m); 1381 strcpy(buf + m, "..."); 1382 strcpy(buf + m + 3, s + l - m); 1383 return buf; 1384 } 1385