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[] = "@(#)recipient.c 8.39 (Berkeley) 01/10/94"; 11 #endif /* not lint */ 12 13 # include "sendmail.h" 14 # include <pwd.h> 15 16 /* 17 ** SENDTOLIST -- Designate a send list. 18 ** 19 ** The parameter is a comma-separated list of people to send to. 20 ** This routine arranges to send to all of them. 21 ** 22 ** Parameters: 23 ** list -- the send list. 24 ** ctladdr -- the address template for the person to 25 ** send to -- effective uid/gid are important. 26 ** This is typically the alias that caused this 27 ** expansion. 28 ** sendq -- a pointer to the head of a queue to put 29 ** these people into. 30 ** e -- the envelope in which to add these recipients. 31 ** 32 ** Returns: 33 ** The number of addresses actually on the list. 34 ** 35 ** Side Effects: 36 ** none. 37 */ 38 39 # define MAXRCRSN 10 40 41 sendtolist(list, ctladdr, sendq, e) 42 char *list; 43 ADDRESS *ctladdr; 44 ADDRESS **sendq; 45 register ENVELOPE *e; 46 { 47 register char *p; 48 register ADDRESS *al; /* list of addresses to send to */ 49 bool firstone; /* set on first address sent */ 50 char delimiter; /* the address delimiter */ 51 int naddrs; 52 char *oldto = e->e_to; 53 54 if (list == NULL) 55 { 56 syserr("sendtolist: null list"); 57 return 0; 58 } 59 60 if (tTd(25, 1)) 61 { 62 printf("sendto: %s\n ctladdr=", list); 63 printaddr(ctladdr, FALSE); 64 } 65 66 /* heuristic to determine old versus new style addresses */ 67 if (ctladdr == NULL && 68 (strchr(list, ',') != NULL || strchr(list, ';') != NULL || 69 strchr(list, '<') != NULL || strchr(list, '(') != NULL)) 70 e->e_flags &= ~EF_OLDSTYLE; 71 delimiter = ' '; 72 if (!bitset(EF_OLDSTYLE, e->e_flags) || ctladdr != NULL) 73 delimiter = ','; 74 75 firstone = TRUE; 76 al = NULL; 77 naddrs = 0; 78 79 for (p = list; *p != '\0'; ) 80 { 81 auto char *delimptr; 82 register ADDRESS *a; 83 84 /* parse the address */ 85 while ((isascii(*p) && isspace(*p)) || *p == ',') 86 p++; 87 a = parseaddr(p, NULLADDR, RF_COPYALL, delimiter, &delimptr, e); 88 p = delimptr; 89 if (a == NULL) 90 continue; 91 a->q_next = al; 92 a->q_alias = ctladdr; 93 94 /* see if this should be marked as a primary address */ 95 if (ctladdr == NULL || 96 (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags))) 97 a->q_flags |= QPRIMARY; 98 99 if (ctladdr != NULL && sameaddr(ctladdr, a)) 100 ctladdr->q_flags |= QSELFREF; 101 al = a; 102 firstone = FALSE; 103 } 104 105 /* arrange to send to everyone on the local send list */ 106 while (al != NULL) 107 { 108 register ADDRESS *a = al; 109 110 al = a->q_next; 111 a = recipient(a, sendq, e); 112 113 /* arrange to inherit full name */ 114 if (a->q_fullname == NULL && ctladdr != NULL) 115 a->q_fullname = ctladdr->q_fullname; 116 naddrs++; 117 } 118 119 e->e_to = oldto; 120 return (naddrs); 121 } 122 /* 123 ** RECIPIENT -- Designate a message recipient 124 ** 125 ** Saves the named person for future mailing. 126 ** 127 ** Parameters: 128 ** a -- the (preparsed) address header for the recipient. 129 ** sendq -- a pointer to the head of a queue to put the 130 ** recipient in. Duplicate supression is done 131 ** in this queue. 132 ** e -- the current envelope. 133 ** 134 ** Returns: 135 ** The actual address in the queue. This will be "a" if 136 ** the address is not a duplicate, else the original address. 137 ** 138 ** Side Effects: 139 ** none. 140 */ 141 142 ADDRESS * 143 recipient(a, sendq, e) 144 register ADDRESS *a; 145 register ADDRESS **sendq; 146 register ENVELOPE *e; 147 { 148 register ADDRESS *q; 149 ADDRESS **pq; 150 register struct mailer *m; 151 register char *p; 152 bool quoted = FALSE; /* set if the addr has a quote bit */ 153 int findusercount = 0; 154 char buf[MAXNAME]; /* unquoted image of the user name */ 155 extern int safefile(); 156 157 e->e_to = a->q_paddr; 158 m = a->q_mailer; 159 errno = 0; 160 if (tTd(26, 1)) 161 { 162 printf("\nrecipient: "); 163 printaddr(a, FALSE); 164 } 165 166 /* if this is primary, add it to the original recipient list */ 167 if (a->q_alias == NULL) 168 { 169 if (e->e_origrcpt == NULL) 170 e->e_origrcpt = a->q_paddr; 171 else if (e->e_origrcpt != a->q_paddr) 172 e->e_origrcpt = ""; 173 } 174 175 /* break aliasing loops */ 176 if (AliasLevel > MAXRCRSN) 177 { 178 usrerr("554 aliasing/forwarding loop broken"); 179 return (a); 180 } 181 182 /* 183 ** Finish setting up address structure. 184 */ 185 186 /* set the queue timeout */ 187 a->q_timeout = TimeOuts.to_q_return; 188 189 /* get unquoted user for file, program or user.name check */ 190 (void) strcpy(buf, a->q_user); 191 for (p = buf; *p != '\0' && !quoted; p++) 192 { 193 if (*p == '\\') 194 quoted = TRUE; 195 } 196 stripquotes(buf); 197 198 /* check for direct mailing to restricted mailers */ 199 if (m == ProgMailer) 200 { 201 if (a->q_alias == NULL) 202 { 203 a->q_flags |= QBADADDR; 204 usrerr("550 Cannot mail directly to programs"); 205 } 206 else if (bitset(QBOGUSSHELL, a->q_alias->q_flags)) 207 { 208 a->q_flags |= QBADADDR; 209 usrerr("550 User %s@%s doesn't have a valid shell for mailing to programs", 210 a->q_alias->q_ruser, MyHostName); 211 } 212 else if (bitset(QUNSAFEADDR, a->q_alias->q_flags)) 213 { 214 a->q_flags |= QBADADDR; 215 usrerr("550 Address %s is unsafe for mailing to programs", 216 a->q_alias->q_paddr); 217 } 218 } 219 220 /* 221 ** Look up this person in the recipient list. 222 ** If they are there already, return, otherwise continue. 223 ** If the list is empty, just add it. Notice the cute 224 ** hack to make from addresses suppress things correctly: 225 ** the QDONTSEND bit will be set in the send list. 226 ** [Please note: the emphasis is on "hack."] 227 */ 228 229 for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next) 230 { 231 if (sameaddr(q, a)) 232 { 233 if (tTd(26, 1)) 234 { 235 printf("%s in sendq: ", a->q_paddr); 236 printaddr(q, FALSE); 237 } 238 if (!bitset(QPRIMARY, q->q_flags)) 239 { 240 if (!bitset(QDONTSEND, a->q_flags)) 241 message("duplicate suppressed"); 242 q->q_flags |= a->q_flags; 243 } 244 else if (bitset(QSELFREF, q->q_flags)) 245 q->q_flags |= a->q_flags & ~QDONTSEND; 246 a = q; 247 goto testselfdestruct; 248 } 249 } 250 251 /* add address on list */ 252 *pq = a; 253 a->q_next = NULL; 254 255 /* 256 ** Alias the name and handle special mailer types. 257 */ 258 259 trylocaluser: 260 if (tTd(29, 7)) 261 printf("at trylocaluser %s\n", a->q_user); 262 263 if (bitset(QDONTSEND|QBADADDR|QVERIFIED, a->q_flags)) 264 goto testselfdestruct; 265 266 if (m == InclMailer) 267 { 268 a->q_flags |= QDONTSEND; 269 if (a->q_alias == NULL) 270 { 271 a->q_flags |= QBADADDR; 272 usrerr("550 Cannot mail directly to :include:s"); 273 } 274 else 275 { 276 int ret; 277 278 message("including file %s", a->q_user); 279 ret = include(a->q_user, FALSE, a, sendq, e); 280 if (transienterror(ret)) 281 { 282 #ifdef LOG 283 if (LogLevel > 2) 284 syslog(LOG_ERR, "%s: include %s: transient error: %e", 285 e->e_id, a->q_user, errstring(ret)); 286 #endif 287 a->q_flags |= QQUEUEUP; 288 a->q_flags &= ~QDONTSEND; 289 usrerr("451 Cannot open %s: %s", 290 a->q_user, errstring(ret)); 291 } 292 else if (ret != 0) 293 { 294 a->q_flags |= QBADADDR; 295 usrerr("550 Cannot open %s: %s", 296 a->q_user, errstring(ret)); 297 } 298 } 299 } 300 else if (m == FileMailer) 301 { 302 extern bool writable(); 303 304 /* check if writable or creatable */ 305 if (a->q_alias == NULL) 306 { 307 a->q_flags |= QBADADDR; 308 usrerr("550 Cannot mail directly to files"); 309 } 310 else if (bitset(QBOGUSSHELL, a->q_alias->q_flags)) 311 { 312 a->q_flags |= QBADADDR; 313 usrerr("550 User %s@%s doesn't have a valid shell for mailing to files", 314 a->q_alias->q_ruser, MyHostName); 315 } 316 else if (bitset(QUNSAFEADDR, a->q_alias->q_flags)) 317 { 318 a->q_flags |= QBADADDR; 319 usrerr("550 Address %s is unsafe for mailing to files", 320 a->q_alias->q_paddr); 321 } 322 else if (!writable(buf, getctladdr(a), SFF_ANYFILE)) 323 { 324 a->q_flags |= QBADADDR; 325 giveresponse(EX_CANTCREAT, m, NULL, a->q_alias, e); 326 } 327 } 328 329 if (m != LocalMailer) 330 { 331 if (!bitset(QDONTSEND, a->q_flags)) 332 e->e_nrcpts++; 333 goto testselfdestruct; 334 } 335 336 /* try aliasing */ 337 alias(a, sendq, e); 338 339 # ifdef USERDB 340 /* if not aliased, look it up in the user database */ 341 if (!bitset(QDONTSEND|QNOTREMOTE|QVERIFIED, a->q_flags)) 342 { 343 extern int udbexpand(); 344 345 if (udbexpand(a, sendq, e) == EX_TEMPFAIL) 346 { 347 a->q_flags |= QQUEUEUP; 348 if (e->e_message == NULL) 349 e->e_message = newstr("Deferred: user database error"); 350 # ifdef LOG 351 if (LogLevel > 8) 352 syslog(LOG_INFO, "%s: deferred: udbexpand: %s", 353 e->e_id, errstring(errno)); 354 # endif 355 message("queued (user database error): %s", 356 errstring(errno)); 357 e->e_nrcpts++; 358 goto testselfdestruct; 359 } 360 } 361 # endif 362 363 /* if it was an alias or a UDB expansion, just return now */ 364 if (bitset(QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags)) 365 goto testselfdestruct; 366 367 /* 368 ** If we have a level two config file, then pass the name through 369 ** Ruleset 5 before sending it off. Ruleset 5 has the right 370 ** to send rewrite it to another mailer. This gives us a hook 371 ** after local aliasing has been done. 372 */ 373 374 if (tTd(29, 5)) 375 { 376 printf("recipient: testing local? cl=%d, rr5=%x\n\t", 377 ConfigLevel, RewriteRules[5]); 378 printaddr(a, FALSE); 379 } 380 if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 && 381 RewriteRules[5] != NULL) 382 { 383 maplocaluser(a, sendq, e); 384 } 385 386 /* 387 ** If it didn't get rewritten to another mailer, go ahead 388 ** and deliver it. 389 */ 390 391 if (!bitset(QDONTSEND|QQUEUEUP, a->q_flags)) 392 { 393 auto bool fuzzy; 394 register struct passwd *pw; 395 extern struct passwd *finduser(); 396 397 /* warning -- finduser may trash buf */ 398 pw = finduser(buf, &fuzzy); 399 if (pw == NULL) 400 { 401 a->q_flags |= QBADADDR; 402 giveresponse(EX_NOUSER, m, NULL, a->q_alias, e); 403 } 404 else 405 { 406 char nbuf[MAXNAME]; 407 408 if (fuzzy) 409 { 410 /* name was a fuzzy match */ 411 a->q_user = newstr(pw->pw_name); 412 if (findusercount++ > 3) 413 { 414 a->q_flags |= QBADADDR; 415 usrerr("554 aliasing/forwarding loop for %s broken", 416 pw->pw_name); 417 return (a); 418 } 419 420 /* see if it aliases */ 421 (void) strcpy(buf, pw->pw_name); 422 goto trylocaluser; 423 } 424 a->q_home = newstr(pw->pw_dir); 425 a->q_uid = pw->pw_uid; 426 a->q_gid = pw->pw_gid; 427 a->q_ruser = newstr(pw->pw_name); 428 a->q_flags |= QGOODUID; 429 buildfname(pw->pw_gecos, pw->pw_name, nbuf); 430 if (nbuf[0] != '\0') 431 a->q_fullname = newstr(nbuf); 432 if (pw->pw_shell != NULL && pw->pw_shell[0] != '\0' && 433 !usershellok(pw->pw_shell)) 434 { 435 a->q_flags |= QBOGUSSHELL; 436 } 437 if (!quoted) 438 forward(a, sendq, e); 439 } 440 } 441 if (!bitset(QDONTSEND, a->q_flags)) 442 e->e_nrcpts++; 443 444 testselfdestruct: 445 if (tTd(26, 8)) 446 { 447 printf("testselfdestruct: "); 448 printaddr(a, TRUE); 449 } 450 if (a->q_alias == NULL && a != &e->e_from && 451 bitset(QDONTSEND, a->q_flags)) 452 { 453 q = *sendq; 454 while (q != NULL && bitset(QDONTSEND, q->q_flags)) 455 q = q->q_next; 456 if (q == NULL) 457 { 458 a->q_flags |= QBADADDR; 459 usrerr("554 aliasing/forwarding loop broken"); 460 } 461 } 462 return (a); 463 } 464 /* 465 ** FINDUSER -- find the password entry for a user. 466 ** 467 ** This looks a lot like getpwnam, except that it may want to 468 ** do some fancier pattern matching in /etc/passwd. 469 ** 470 ** This routine contains most of the time of many sendmail runs. 471 ** It deserves to be optimized. 472 ** 473 ** Parameters: 474 ** name -- the name to match against. 475 ** fuzzyp -- an outarg that is set to TRUE if this entry 476 ** was found using the fuzzy matching algorithm; 477 ** set to FALSE otherwise. 478 ** 479 ** Returns: 480 ** A pointer to a pw struct. 481 ** NULL if name is unknown or ambiguous. 482 ** 483 ** Side Effects: 484 ** may modify name. 485 */ 486 487 struct passwd * 488 finduser(name, fuzzyp) 489 char *name; 490 bool *fuzzyp; 491 { 492 register struct passwd *pw; 493 register char *p; 494 extern struct passwd *getpwent(); 495 extern struct passwd *getpwnam(); 496 497 if (tTd(29, 4)) 498 printf("finduser(%s): ", name); 499 500 *fuzzyp = FALSE; 501 502 /* DEC Hesiod getpwnam accepts numeric strings -- short circuit it */ 503 for (p = name; *p != '\0'; p++) 504 if (!isascii(*p) || !isdigit(*p)) 505 break; 506 if (*p == '\0') 507 { 508 if (tTd(29, 4)) 509 printf("failed (numeric input)\n"); 510 return NULL; 511 } 512 513 /* look up this login name using fast path */ 514 if ((pw = getpwnam(name)) != NULL) 515 { 516 if (tTd(29, 4)) 517 printf("found (non-fuzzy)\n"); 518 return (pw); 519 } 520 521 #ifdef MATCHGECOS 522 /* see if fuzzy matching allowed */ 523 if (!MatchGecos) 524 { 525 if (tTd(29, 4)) 526 printf("not found (fuzzy disabled)\n"); 527 return NULL; 528 } 529 530 /* search for a matching full name instead */ 531 for (p = name; *p != '\0'; p++) 532 { 533 if (*p == (SpaceSub & 0177) || *p == '_') 534 *p = ' '; 535 } 536 (void) setpwent(); 537 while ((pw = getpwent()) != NULL) 538 { 539 char buf[MAXNAME]; 540 541 buildfname(pw->pw_gecos, pw->pw_name, buf); 542 if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name)) 543 { 544 if (tTd(29, 4)) 545 printf("fuzzy matches %s\n", pw->pw_name); 546 message("sending to login name %s", pw->pw_name); 547 *fuzzyp = TRUE; 548 return (pw); 549 } 550 } 551 if (tTd(29, 4)) 552 printf("no fuzzy match found\n"); 553 #else 554 if (tTd(29, 4)) 555 printf("not found (fuzzy disabled)\n"); 556 #endif 557 return (NULL); 558 } 559 /* 560 ** WRITABLE -- predicate returning if the file is writable. 561 ** 562 ** This routine must duplicate the algorithm in sys/fio.c. 563 ** Unfortunately, we cannot use the access call since we 564 ** won't necessarily be the real uid when we try to 565 ** actually open the file. 566 ** 567 ** Notice that ANY file with ANY execute bit is automatically 568 ** not writable. This is also enforced by mailfile. 569 ** 570 ** Parameters: 571 ** filename -- the file name to check. 572 ** ctladdr -- the controlling address for this file. 573 ** flags -- SFF_* flags to control the function. 574 ** 575 ** Returns: 576 ** TRUE -- if we will be able to write this file. 577 ** FALSE -- if we cannot write this file. 578 ** 579 ** Side Effects: 580 ** none. 581 */ 582 583 bool 584 writable(filename, ctladdr, flags) 585 char *filename; 586 ADDRESS *ctladdr; 587 int flags; 588 { 589 uid_t euid; 590 gid_t egid; 591 int bits; 592 register char *p; 593 char *uname; 594 struct stat stb; 595 extern char RealUserName[]; 596 597 if (tTd(29, 5)) 598 printf("writable(%s, %x)\n", filename, flags); 599 600 #ifdef HASLSTAT 601 if ((bitset(SFF_NOSLINK, flags) ? lstat(filename, &stb) 602 : stat(filename, &stb)) < 0) 603 #else 604 if (stat(filename, &stb) < 0) 605 #endif 606 { 607 /* file does not exist -- see if directory is safe */ 608 p = strrchr(filename, '/'); 609 if (p == NULL) 610 { 611 errno = ENOTDIR; 612 return FALSE; 613 } 614 *p = '\0'; 615 errno = safefile(filename, RealUid, RealGid, RealUserName, 616 SFF_MUSTOWN, S_IWRITE|S_IEXEC); 617 *p = '/'; 618 return errno == 0; 619 } 620 621 #ifdef SUID_ROOT_FILES_OK 622 /* really ought to be passed down -- and not a good idea */ 623 flags |= SFF_ROOTOK; 624 #endif 625 626 /* 627 ** File does exist -- check that it is writable. 628 */ 629 630 if (bitset(0111, stb.st_mode)) 631 { 632 if (tTd(29, 5)) 633 printf("failed (mode %o: x bits)\n", stb.st_mode); 634 errno = EPERM; 635 return (FALSE); 636 } 637 638 if (ctladdr != NULL && geteuid() == 0) 639 { 640 euid = ctladdr->q_uid; 641 egid = ctladdr->q_gid; 642 uname = ctladdr->q_user; 643 } 644 else 645 { 646 euid = RealUid; 647 egid = RealGid; 648 uname = RealUserName; 649 } 650 if (euid == 0) 651 { 652 euid = DefUid; 653 uname = DefUser; 654 } 655 if (egid == 0) 656 egid = DefGid; 657 if (geteuid() == 0) 658 { 659 if (bitset(S_ISUID, stb.st_mode) && 660 (stb.st_uid != 0 || bitset(SFF_ROOTOK, flags))) 661 { 662 euid = stb.st_uid; 663 uname = NULL; 664 } 665 if (bitset(S_ISGID, stb.st_mode) && 666 (stb.st_gid != 0 || bitset(SFF_ROOTOK, flags))) 667 egid = stb.st_gid; 668 } 669 670 if (tTd(29, 5)) 671 printf("\teu/gid=%d/%d, st_u/gid=%d/%d\n", 672 euid, egid, stb.st_uid, stb.st_gid); 673 674 errno = safefile(filename, euid, egid, uname, flags, S_IWRITE); 675 return errno == 0; 676 } 677 /* 678 ** INCLUDE -- handle :include: specification. 679 ** 680 ** Parameters: 681 ** fname -- filename to include. 682 ** forwarding -- if TRUE, we are reading a .forward file. 683 ** if FALSE, it's a :include: file. 684 ** ctladdr -- address template to use to fill in these 685 ** addresses -- effective user/group id are 686 ** the important things. 687 ** sendq -- a pointer to the head of the send queue 688 ** to put these addresses in. 689 ** 690 ** Returns: 691 ** open error status 692 ** 693 ** Side Effects: 694 ** reads the :include: file and sends to everyone 695 ** listed in that file. 696 */ 697 698 static jmp_buf CtxIncludeTimeout; 699 static int includetimeout(); 700 701 #ifndef S_IWOTH 702 # define S_IWOTH (S_IWRITE >> 6) 703 #endif 704 705 int 706 include(fname, forwarding, ctladdr, sendq, e) 707 char *fname; 708 bool forwarding; 709 ADDRESS *ctladdr; 710 ADDRESS **sendq; 711 ENVELOPE *e; 712 { 713 register FILE *fp = NULL; 714 char *oldto = e->e_to; 715 char *oldfilename = FileName; 716 int oldlinenumber = LineNumber; 717 register EVENT *ev = NULL; 718 int nincludes; 719 register ADDRESS *ca; 720 uid_t saveduid, uid; 721 gid_t savedgid, gid; 722 char *uname; 723 int rval = 0; 724 int sfflags = forwarding ? SFF_MUSTOWN : SFF_ANYFILE; 725 struct stat st; 726 char buf[MAXLINE]; 727 728 if (tTd(27, 2)) 729 printf("include(%s)\n", fname); 730 if (tTd(27, 4)) 731 printf(" ruid=%d euid=%d\n", getuid(), geteuid()); 732 if (tTd(27, 14)) 733 { 734 printf("ctladdr "); 735 printaddr(ctladdr, FALSE); 736 } 737 738 if (tTd(27, 9)) 739 printf("include: old uid = %d/%d\n", getuid(), geteuid()); 740 741 ca = getctladdr(ctladdr); 742 if (ca == NULL) 743 { 744 uid = DefUid; 745 gid = DefGid; 746 uname = DefUser; 747 saveduid = -1; 748 } 749 else 750 { 751 uid = ca->q_uid; 752 gid = ca->q_gid; 753 uname = ca->q_user; 754 #ifdef HASSETREUID 755 saveduid = geteuid(); 756 savedgid = getegid(); 757 if (saveduid == 0) 758 { 759 initgroups(uname, gid); 760 if (uid != 0) 761 (void) setreuid(0, uid); 762 } 763 #endif 764 } 765 766 if (tTd(27, 9)) 767 printf("include: new uid = %d/%d\n", getuid(), geteuid()); 768 769 /* 770 ** If home directory is remote mounted but server is down, 771 ** this can hang or give errors; use a timeout to avoid this 772 */ 773 774 if (setjmp(CtxIncludeTimeout) != 0) 775 { 776 ctladdr->q_flags |= QQUEUEUP; 777 errno = 0; 778 779 /* return pseudo-error code */ 780 rval = EOPENTIMEOUT; 781 goto resetuid; 782 } 783 ev = setevent((time_t) 60, includetimeout, 0); 784 785 /* the input file must be marked safe */ 786 rval = safefile(fname, uid, gid, uname, sfflags, S_IREAD); 787 if (rval != 0) 788 { 789 /* don't use this :include: file */ 790 if (tTd(27, 4)) 791 printf("include: not safe (uid=%d): %s\n", 792 uid, errstring(rval)); 793 } 794 else 795 { 796 fp = fopen(fname, "r"); 797 if (fp == NULL) 798 { 799 rval = errno; 800 if (tTd(27, 4)) 801 printf("include: open: %s\n", errstring(rval)); 802 } 803 } 804 clrevent(ev); 805 806 resetuid: 807 808 #ifdef HASSETREUID 809 if (saveduid == 0) 810 { 811 if (uid != 0) 812 if (setreuid(-1, 0) < 0 || setreuid(RealUid, 0) < 0) 813 syserr("setreuid(%d, 0) failure (real=%d, eff=%d)", 814 RealUid, getuid(), geteuid()); 815 setgid(savedgid); 816 } 817 #endif 818 819 if (tTd(27, 9)) 820 printf("include: reset uid = %d/%d\n", getuid(), geteuid()); 821 822 if (rval == EOPENTIMEOUT) 823 usrerr("451 open timeout on %s", fname); 824 825 if (fp == NULL) 826 return rval; 827 828 if (fstat(fileno(fp), &st) < 0) 829 { 830 rval = errno; 831 syserr("Cannot fstat %s!", fname); 832 return rval; 833 } 834 835 if (ca == NULL) 836 { 837 ctladdr->q_uid = st.st_uid; 838 ctladdr->q_gid = st.st_gid; 839 ctladdr->q_flags |= QGOODUID; 840 } 841 if (ca != NULL && ca->q_uid == st.st_uid) 842 { 843 /* optimization -- avoid getpwuid if we already have info */ 844 ctladdr->q_flags |= ca->q_flags & QBOGUSSHELL; 845 ctladdr->q_ruser = ca->q_ruser; 846 } 847 else 848 { 849 register struct passwd *pw; 850 851 pw = getpwuid(st.st_uid); 852 if (pw == NULL || !usershellok(pw->pw_shell)) 853 { 854 ctladdr->q_ruser = newstr(pw->pw_name); 855 ctladdr->q_flags |= QBOGUSSHELL; 856 } 857 } 858 859 if (bitset(EF_VRFYONLY, e->e_flags)) 860 { 861 /* don't do any more now */ 862 ctladdr->q_flags |= QVERIFIED; 863 e->e_nrcpts++; 864 xfclose(fp, "include", fname); 865 return rval; 866 } 867 868 /* 869 ** Check to see if some bad guy can write this file 870 ** 871 ** This should really do something clever with group 872 ** permissions; currently we just view world writable 873 ** as unsafe. Also, we don't check for writable 874 ** directories in the path. We've got to leave 875 ** something for the local sysad to do. 876 */ 877 878 if (bitset(S_IWOTH, st.st_mode)) 879 ctladdr->q_flags |= QUNSAFEADDR; 880 881 /* read the file -- each line is a comma-separated list. */ 882 FileName = fname; 883 LineNumber = 0; 884 ctladdr->q_flags &= ~QSELFREF; 885 nincludes = 0; 886 while (fgets(buf, sizeof buf, fp) != NULL) 887 { 888 register char *p = strchr(buf, '\n'); 889 890 LineNumber++; 891 if (p != NULL) 892 *p = '\0'; 893 if (buf[0] == '#' || buf[0] == '\0') 894 continue; 895 e->e_to = NULL; 896 message("%s to %s", 897 forwarding ? "forwarding" : "sending", buf); 898 #ifdef LOG 899 if (forwarding && LogLevel > 9) 900 syslog(LOG_INFO, "%s: forward %s => %s", 901 e->e_id, oldto, buf); 902 #endif 903 904 AliasLevel++; 905 nincludes += sendtolist(buf, ctladdr, sendq, e); 906 AliasLevel--; 907 } 908 909 if (ferror(fp) && tTd(27, 3)) 910 printf("include: read error: %s\n", errstring(errno)); 911 if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags)) 912 { 913 if (tTd(27, 5)) 914 { 915 printf("include: QDONTSEND "); 916 printaddr(ctladdr, FALSE); 917 } 918 ctladdr->q_flags |= QDONTSEND; 919 } 920 921 (void) xfclose(fp, "include", fname); 922 FileName = oldfilename; 923 LineNumber = oldlinenumber; 924 e->e_to = oldto; 925 return rval; 926 } 927 928 static 929 includetimeout() 930 { 931 longjmp(CtxIncludeTimeout, 1); 932 } 933 /* 934 ** SENDTOARGV -- send to an argument vector. 935 ** 936 ** Parameters: 937 ** argv -- argument vector to send to. 938 ** e -- the current envelope. 939 ** 940 ** Returns: 941 ** none. 942 ** 943 ** Side Effects: 944 ** puts all addresses on the argument vector onto the 945 ** send queue. 946 */ 947 948 sendtoargv(argv, e) 949 register char **argv; 950 register ENVELOPE *e; 951 { 952 register char *p; 953 954 while ((p = *argv++) != NULL) 955 { 956 (void) sendtolist(p, NULLADDR, &e->e_sendqueue, e); 957 } 958 } 959 /* 960 ** GETCTLADDR -- get controlling address from an address header. 961 ** 962 ** If none, get one corresponding to the effective userid. 963 ** 964 ** Parameters: 965 ** a -- the address to find the controller of. 966 ** 967 ** Returns: 968 ** the controlling address. 969 ** 970 ** Side Effects: 971 ** none. 972 */ 973 974 ADDRESS * 975 getctladdr(a) 976 register ADDRESS *a; 977 { 978 while (a != NULL && !bitset(QGOODUID, a->q_flags)) 979 a = a->q_alias; 980 return (a); 981 } 982