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.30 (Berkeley) 02/05/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 if (errno != ENFILE && errno != EINTR) 622 break; 623 } 624 if (fd >= 0 && fstat(fd, &st) >= 0 && S_ISREG(st.st_mode)) 625 { 626 int locktype; 627 628 /* lock the file to avoid accidental conflicts */ 629 if ((omode & O_ACCMODE) != O_RDONLY) 630 locktype = LOCK_EX; 631 else 632 locktype = LOCK_SH; 633 (void) lockfile(fd, filename, NULL, locktype); 634 errno = 0; 635 } 636 if (fd < 0) 637 return NULL; 638 else 639 return fdopen(fd, om->farg); 640 } 641 /* 642 ** PUTLINE -- put a line like fputs obeying SMTP conventions 643 ** 644 ** This routine always guarantees outputing a newline (or CRLF, 645 ** as appropriate) at the end of the string. 646 ** 647 ** Parameters: 648 ** l -- line to put. 649 ** mci -- the mailer connection information. 650 ** 651 ** Returns: 652 ** none 653 ** 654 ** Side Effects: 655 ** output of l to fp. 656 */ 657 658 putline(l, mci) 659 register char *l; 660 register MCI *mci; 661 { 662 register char *p; 663 register char svchar; 664 int slop = 0; 665 666 /* strip out 0200 bits -- these can look like TELNET protocol */ 667 if (bitset(MCIF_7BIT, mci->mci_flags)) 668 { 669 for (p = l; (svchar = *p) != '\0'; ++p) 670 if (bitset(0200, svchar)) 671 *p = svchar &~ 0200; 672 } 673 674 do 675 { 676 /* find the end of the line */ 677 p = strchr(l, '\n'); 678 if (p == NULL) 679 p = &l[strlen(l)]; 680 681 if (TrafficLogFile != NULL) 682 fprintf(TrafficLogFile, "%05d >>> ", getpid()); 683 684 /* check for line overflow */ 685 while (mci->mci_mailer->m_linelimit > 0 && 686 (p - l + slop) > mci->mci_mailer->m_linelimit) 687 { 688 register char *q = &l[mci->mci_mailer->m_linelimit - slop - 1]; 689 690 svchar = *q; 691 *q = '\0'; 692 if (l[0] == '.' && slop == 0 && 693 bitnset(M_XDOT, mci->mci_mailer->m_flags)) 694 { 695 (void) putc('.', mci->mci_out); 696 if (TrafficLogFile != NULL) 697 (void) putc('.', TrafficLogFile); 698 } 699 fputs(l, mci->mci_out); 700 (void) putc('!', mci->mci_out); 701 fputs(mci->mci_mailer->m_eol, mci->mci_out); 702 (void) putc(' ', mci->mci_out); 703 if (TrafficLogFile != NULL) 704 fprintf(TrafficLogFile, "%s!\n%05d >>> ", 705 l, getpid()); 706 *q = svchar; 707 l = q; 708 slop = 1; 709 } 710 711 /* output last part */ 712 if (l[0] == '.' && slop == 0 && 713 bitnset(M_XDOT, mci->mci_mailer->m_flags)) 714 { 715 (void) putc('.', mci->mci_out); 716 if (TrafficLogFile != NULL) 717 (void) putc('.', TrafficLogFile); 718 } 719 if (TrafficLogFile != NULL) 720 fprintf(TrafficLogFile, "%.*s\n", p - l, l); 721 for ( ; l < p; ++l) 722 (void) putc(*l, mci->mci_out); 723 fputs(mci->mci_mailer->m_eol, mci->mci_out); 724 if (*l == '\n') 725 ++l; 726 } while (l[0] != '\0'); 727 } 728 /* 729 ** XUNLINK -- unlink a file, doing logging as appropriate. 730 ** 731 ** Parameters: 732 ** f -- name of file to unlink. 733 ** 734 ** Returns: 735 ** none. 736 ** 737 ** Side Effects: 738 ** f is unlinked. 739 */ 740 741 xunlink(f) 742 char *f; 743 { 744 register int i; 745 746 # ifdef LOG 747 if (LogLevel > 98) 748 syslog(LOG_DEBUG, "%s: unlink %s", CurEnv->e_id, f); 749 # endif /* LOG */ 750 751 i = unlink(f); 752 # ifdef LOG 753 if (i < 0 && LogLevel > 97) 754 syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno); 755 # endif /* LOG */ 756 } 757 /* 758 ** XFCLOSE -- close a file, doing logging as appropriate. 759 ** 760 ** Parameters: 761 ** fp -- file pointer for the file to close 762 ** a, b -- miscellaneous crud to print for debugging 763 ** 764 ** Returns: 765 ** none. 766 ** 767 ** Side Effects: 768 ** fp is closed. 769 */ 770 771 xfclose(fp, a, b) 772 FILE *fp; 773 char *a, *b; 774 { 775 if (tTd(53, 99)) 776 printf("xfclose(%x) %s %s\n", fp, a, b); 777 #ifdef XDEBUG 778 if (fileno(fp) == 1) 779 syserr("xfclose(%s %s): fd = 1", a, b); 780 #endif 781 if (fclose(fp) < 0 && tTd(53, 99)) 782 printf("xfclose FAILURE: %s\n", errstring(errno)); 783 } 784 /* 785 ** SFGETS -- "safe" fgets -- times out and ignores random interrupts. 786 ** 787 ** Parameters: 788 ** buf -- place to put the input line. 789 ** siz -- size of buf. 790 ** fp -- file to read from. 791 ** timeout -- the timeout before error occurs. 792 ** during -- what we are trying to read (for error messages). 793 ** 794 ** Returns: 795 ** NULL on error (including timeout). This will also leave 796 ** buf containing a null string. 797 ** buf otherwise. 798 ** 799 ** Side Effects: 800 ** none. 801 */ 802 803 static jmp_buf CtxReadTimeout; 804 static int readtimeout(); 805 806 char * 807 sfgets(buf, siz, fp, timeout, during) 808 char *buf; 809 int siz; 810 FILE *fp; 811 time_t timeout; 812 char *during; 813 { 814 register EVENT *ev = NULL; 815 register char *p; 816 817 /* set the timeout */ 818 if (timeout != 0) 819 { 820 if (setjmp(CtxReadTimeout) != 0) 821 { 822 # ifdef LOG 823 syslog(LOG_NOTICE, 824 "timeout waiting for input from %s during %s\n", 825 CurHostName? CurHostName: "local", during); 826 # endif 827 errno = 0; 828 usrerr("451 timeout waiting for input during %s", 829 during); 830 buf[0] = '\0'; 831 #ifdef XDEBUG 832 checkfd012(during); 833 #endif 834 return (NULL); 835 } 836 ev = setevent(timeout, readtimeout, 0); 837 } 838 839 /* try to read */ 840 p = NULL; 841 while (!feof(fp) && !ferror(fp)) 842 { 843 errno = 0; 844 p = fgets(buf, siz, fp); 845 if (p != NULL || errno != EINTR) 846 break; 847 clearerr(fp); 848 } 849 850 /* clear the event if it has not sprung */ 851 clrevent(ev); 852 853 /* clean up the books and exit */ 854 LineNumber++; 855 if (p == NULL) 856 { 857 buf[0] = '\0'; 858 if (TrafficLogFile != NULL) 859 fprintf(TrafficLogFile, "%05d <<< [EOF]\n", getpid()); 860 return (NULL); 861 } 862 if (TrafficLogFile != NULL) 863 fprintf(TrafficLogFile, "%05d <<< %s", getpid(), buf); 864 if (SevenBit) 865 for (p = buf; *p != '\0'; p++) 866 *p &= ~0200; 867 return (buf); 868 } 869 870 static 871 readtimeout() 872 { 873 longjmp(CtxReadTimeout, 1); 874 } 875 /* 876 ** FGETFOLDED -- like fgets, but know about folded lines. 877 ** 878 ** Parameters: 879 ** buf -- place to put result. 880 ** n -- bytes available. 881 ** f -- file to read from. 882 ** 883 ** Returns: 884 ** input line(s) on success, NULL on error or EOF. 885 ** This will normally be buf -- unless the line is too 886 ** long, when it will be xalloc()ed. 887 ** 888 ** Side Effects: 889 ** buf gets lines from f, with continuation lines (lines 890 ** with leading white space) appended. CRLF's are mapped 891 ** into single newlines. Any trailing NL is stripped. 892 */ 893 894 char * 895 fgetfolded(buf, n, f) 896 char *buf; 897 register int n; 898 FILE *f; 899 { 900 register char *p = buf; 901 char *bp = buf; 902 register int i; 903 904 n--; 905 while ((i = getc(f)) != EOF) 906 { 907 if (i == '\r') 908 { 909 i = getc(f); 910 if (i != '\n') 911 { 912 if (i != EOF) 913 (void) ungetc(i, f); 914 i = '\r'; 915 } 916 } 917 if (--n <= 0) 918 { 919 /* allocate new space */ 920 char *nbp; 921 int nn; 922 923 nn = (p - bp); 924 if (nn < MEMCHUNKSIZE) 925 nn *= 2; 926 else 927 nn += MEMCHUNKSIZE; 928 nbp = xalloc(nn); 929 bcopy(bp, nbp, p - bp); 930 p = &nbp[p - bp]; 931 if (bp != buf) 932 free(bp); 933 bp = nbp; 934 n = nn - (p - bp); 935 } 936 *p++ = i; 937 if (i == '\n') 938 { 939 LineNumber++; 940 i = getc(f); 941 if (i != EOF) 942 (void) ungetc(i, f); 943 if (i != ' ' && i != '\t') 944 break; 945 } 946 } 947 if (p == bp) 948 return (NULL); 949 *--p = '\0'; 950 return (bp); 951 } 952 /* 953 ** CURTIME -- return current time. 954 ** 955 ** Parameters: 956 ** none. 957 ** 958 ** Returns: 959 ** the current time. 960 ** 961 ** Side Effects: 962 ** none. 963 */ 964 965 time_t 966 curtime() 967 { 968 auto time_t t; 969 970 (void) time(&t); 971 return (t); 972 } 973 /* 974 ** ATOBOOL -- convert a string representation to boolean. 975 ** 976 ** Defaults to "TRUE" 977 ** 978 ** Parameters: 979 ** s -- string to convert. Takes "tTyY" as true, 980 ** others as false. 981 ** 982 ** Returns: 983 ** A boolean representation of the string. 984 ** 985 ** Side Effects: 986 ** none. 987 */ 988 989 bool 990 atobool(s) 991 register char *s; 992 { 993 if (s == NULL || *s == '\0' || strchr("tTyY", *s) != NULL) 994 return (TRUE); 995 return (FALSE); 996 } 997 /* 998 ** ATOOCT -- convert a string representation to octal. 999 ** 1000 ** Parameters: 1001 ** s -- string to convert. 1002 ** 1003 ** Returns: 1004 ** An integer representing the string interpreted as an 1005 ** octal number. 1006 ** 1007 ** Side Effects: 1008 ** none. 1009 */ 1010 1011 atooct(s) 1012 register char *s; 1013 { 1014 register int i = 0; 1015 1016 while (*s >= '0' && *s <= '7') 1017 i = (i << 3) | (*s++ - '0'); 1018 return (i); 1019 } 1020 /* 1021 ** WAITFOR -- wait for a particular process id. 1022 ** 1023 ** Parameters: 1024 ** pid -- process id to wait for. 1025 ** 1026 ** Returns: 1027 ** status of pid. 1028 ** -1 if pid never shows up. 1029 ** 1030 ** Side Effects: 1031 ** none. 1032 */ 1033 1034 int 1035 waitfor(pid) 1036 int pid; 1037 { 1038 #ifdef WAITUNION 1039 union wait st; 1040 #else 1041 auto int st; 1042 #endif 1043 int i; 1044 1045 do 1046 { 1047 errno = 0; 1048 i = wait(&st); 1049 } while ((i >= 0 || errno == EINTR) && i != pid); 1050 if (i < 0) 1051 return -1; 1052 #ifdef WAITUNION 1053 return st.w_status; 1054 #else 1055 return st; 1056 #endif 1057 } 1058 /* 1059 ** BITINTERSECT -- tell if two bitmaps intersect 1060 ** 1061 ** Parameters: 1062 ** a, b -- the bitmaps in question 1063 ** 1064 ** Returns: 1065 ** TRUE if they have a non-null intersection 1066 ** FALSE otherwise 1067 ** 1068 ** Side Effects: 1069 ** none. 1070 */ 1071 1072 bool 1073 bitintersect(a, b) 1074 BITMAP a; 1075 BITMAP b; 1076 { 1077 int i; 1078 1079 for (i = BITMAPBYTES / sizeof (int); --i >= 0; ) 1080 if ((a[i] & b[i]) != 0) 1081 return (TRUE); 1082 return (FALSE); 1083 } 1084 /* 1085 ** BITZEROP -- tell if a bitmap is all zero 1086 ** 1087 ** Parameters: 1088 ** map -- the bit map to check 1089 ** 1090 ** Returns: 1091 ** TRUE if map is all zero. 1092 ** FALSE if there are any bits set in map. 1093 ** 1094 ** Side Effects: 1095 ** none. 1096 */ 1097 1098 bool 1099 bitzerop(map) 1100 BITMAP map; 1101 { 1102 int i; 1103 1104 for (i = BITMAPBYTES / sizeof (int); --i >= 0; ) 1105 if (map[i] != 0) 1106 return (FALSE); 1107 return (TRUE); 1108 } 1109 /* 1110 ** STRCONTAINEDIN -- tell if one string is contained in another 1111 ** 1112 ** Parameters: 1113 ** a -- possible substring. 1114 ** b -- possible superstring. 1115 ** 1116 ** Returns: 1117 ** TRUE if a is contained in b. 1118 ** FALSE otherwise. 1119 */ 1120 1121 bool 1122 strcontainedin(a, b) 1123 register char *a; 1124 register char *b; 1125 { 1126 int la; 1127 int lb; 1128 int c; 1129 1130 la = strlen(a); 1131 lb = strlen(b); 1132 c = *a; 1133 if (isascii(c) && isupper(c)) 1134 c = tolower(c); 1135 for (; lb-- >= la; b++) 1136 { 1137 if (*b != c && isascii(*b) && isupper(*b) && tolower(*b) != c) 1138 continue; 1139 if (strncasecmp(a, b, la) == 0) 1140 return TRUE; 1141 } 1142 return FALSE; 1143 } 1144 /* 1145 ** CHECKFD012 -- check low numbered file descriptors 1146 ** 1147 ** File descriptors 0, 1, and 2 should be open at all times. 1148 ** This routine verifies that, and fixes it if not true. 1149 ** 1150 ** Parameters: 1151 ** where -- a tag printed if the assertion failed 1152 ** 1153 ** Returns: 1154 ** none 1155 */ 1156 1157 checkfd012(where) 1158 char *where; 1159 { 1160 #ifdef XDEBUG 1161 register int i; 1162 struct stat stbuf; 1163 1164 for (i = 0; i < 3; i++) 1165 { 1166 if (fstat(i, &stbuf) < 0 && errno != EOPNOTSUPP) 1167 { 1168 /* oops.... */ 1169 int fd; 1170 1171 syserr("%s: fd %d not open", where, i); 1172 fd = open("/dev/null", i == 0 ? O_RDONLY : O_WRONLY, 0666); 1173 if (fd != i) 1174 { 1175 (void) dup2(fd, i); 1176 (void) close(fd); 1177 } 1178 } 1179 } 1180 #endif /* XDEBUG */ 1181 } 1182 /* 1183 ** PRINTOPENFDS -- print the open file descriptors (for debugging) 1184 ** 1185 ** Parameters: 1186 ** logit -- if set, send output to syslog; otherwise 1187 ** print for debugging. 1188 ** 1189 ** Returns: 1190 ** none. 1191 */ 1192 1193 #include <netdb.h> 1194 #include <arpa/inet.h> 1195 1196 printopenfds(logit) 1197 bool logit; 1198 { 1199 register int fd; 1200 extern int DtableSize; 1201 1202 for (fd = 0; fd < DtableSize; fd++) 1203 dumpfd(fd, FALSE, logit); 1204 } 1205 /* 1206 ** DUMPFD -- dump a file descriptor 1207 ** 1208 ** Parameters: 1209 ** fd -- the file descriptor to dump. 1210 ** printclosed -- if set, print a notification even if 1211 ** it is closed; otherwise print nothing. 1212 ** logit -- if set, send output to syslog instead of stdout. 1213 */ 1214 1215 dumpfd(fd, printclosed, logit) 1216 int fd; 1217 bool printclosed; 1218 bool logit; 1219 { 1220 register struct hostent *hp; 1221 register char *p; 1222 struct sockaddr_in sin; 1223 auto int slen; 1224 struct stat st; 1225 char buf[200]; 1226 1227 p = buf; 1228 sprintf(p, "%3d: ", fd); 1229 p += strlen(p); 1230 1231 if (fstat(fd, &st) < 0) 1232 { 1233 if (printclosed || errno != EBADF) 1234 { 1235 sprintf(p, "CANNOT STAT (%s)", errstring(errno)); 1236 goto printit; 1237 } 1238 return; 1239 } 1240 1241 slen = fcntl(fd, F_GETFL, NULL); 1242 if (slen != -1) 1243 { 1244 sprintf(p, "fl=0x%x, ", slen); 1245 p += strlen(p); 1246 } 1247 1248 sprintf(p, "mode=%o: ", st.st_mode); 1249 p += strlen(p); 1250 switch (st.st_mode & S_IFMT) 1251 { 1252 #ifdef S_IFSOCK 1253 case S_IFSOCK: 1254 sprintf(p, "SOCK "); 1255 p += strlen(p); 1256 slen = sizeof sin; 1257 if (getsockname(fd, (struct sockaddr *) &sin, &slen) < 0) 1258 sprintf(p, "(badsock)"); 1259 else 1260 { 1261 hp = gethostbyaddr((char *) &sin.sin_addr, slen, AF_INET); 1262 sprintf(p, "%s/%d", hp == NULL ? inet_ntoa(sin.sin_addr) 1263 : hp->h_name, ntohs(sin.sin_port)); 1264 } 1265 p += strlen(p); 1266 sprintf(p, "->"); 1267 p += strlen(p); 1268 slen = sizeof sin; 1269 if (getpeername(fd, (struct sockaddr *) &sin, &slen) < 0) 1270 sprintf(p, "(badsock)"); 1271 else 1272 { 1273 hp = gethostbyaddr((char *) &sin.sin_addr, slen, AF_INET); 1274 sprintf(p, "%s/%d", hp == NULL ? inet_ntoa(sin.sin_addr) 1275 : hp->h_name, ntohs(sin.sin_port)); 1276 } 1277 break; 1278 #endif 1279 1280 case S_IFCHR: 1281 sprintf(p, "CHR: "); 1282 p += strlen(p); 1283 goto defprint; 1284 1285 case S_IFBLK: 1286 sprintf(p, "BLK: "); 1287 p += strlen(p); 1288 goto defprint; 1289 1290 #ifdef S_IFIFO 1291 case S_IFIFO: 1292 sprintf(p, "FIFO: "); 1293 p += strlen(p); 1294 goto defprint; 1295 #endif 1296 1297 #ifdef S_IFDIR 1298 case S_IFDIR: 1299 sprintf(p, "DIR: "); 1300 p += strlen(p); 1301 goto defprint; 1302 #endif 1303 1304 #ifdef S_IFLNK 1305 case S_IFLNK: 1306 sprintf(p, "LNK: "); 1307 p += strlen(p); 1308 goto defprint; 1309 #endif 1310 1311 default: 1312 defprint: 1313 sprintf(p, "dev=%d/%d, ino=%d, nlink=%d, u/gid=%d/%d, size=%ld", 1314 major(st.st_dev), minor(st.st_dev), st.st_ino, 1315 st.st_nlink, st.st_uid, st.st_gid, st.st_size); 1316 break; 1317 } 1318 1319 printit: 1320 if (logit) 1321 syslog(LOG_DEBUG, "%s", buf); 1322 else 1323 printf("%s\n", buf); 1324 } 1325 /* 1326 ** SHORTENSTRING -- return short version of a string 1327 ** 1328 ** If the string is already short, just return it. If it is too 1329 ** long, return the head and tail of the string. 1330 ** 1331 ** Parameters: 1332 ** s -- the string to shorten. 1333 ** m -- the max length of the string. 1334 ** 1335 ** Returns: 1336 ** Either s or a short version of s. 1337 */ 1338 1339 #ifndef MAXSHORTSTR 1340 # define MAXSHORTSTR 203 1341 #endif 1342 1343 char * 1344 shortenstring(s, m) 1345 register char *s; 1346 int m; 1347 { 1348 int l; 1349 static char buf[MAXSHORTSTR + 1]; 1350 1351 l = strlen(s); 1352 if (l < m) 1353 return s; 1354 if (m > MAXSHORTSTR) 1355 m = MAXSHORTSTR; 1356 else if (m < 10) 1357 { 1358 if (m < 5) 1359 { 1360 strncpy(buf, s, m); 1361 buf[m] = '\0'; 1362 return buf; 1363 } 1364 strncpy(buf, s, m - 3); 1365 strcpy(buf + m - 3, "..."); 1366 return buf; 1367 } 1368 m = (m - 3) / 2; 1369 strncpy(buf, s, m); 1370 strcpy(buf + m, "..."); 1371 strcpy(buf + m + 3, s + l - m); 1372 return buf; 1373 } 1374